1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2015 Fujitsu Ltd.
3*49cdfc7eSAndroid Build Coastguard Worker *
4*49cdfc7eSAndroid Build Coastguard Worker * This program is free software: you can redistribute it and/or modify
5*49cdfc7eSAndroid Build Coastguard Worker * it under the terms of the GNU General Public License as published by
6*49cdfc7eSAndroid Build Coastguard Worker * the Free Software Foundation, either version 3 of the License, or
7*49cdfc7eSAndroid Build Coastguard Worker * (at your option) any later version.
8*49cdfc7eSAndroid Build Coastguard Worker *
9*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it will be useful,
10*49cdfc7eSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12*49cdfc7eSAndroid Build Coastguard Worker * GNU General Public License for more details.
13*49cdfc7eSAndroid Build Coastguard Worker *
14*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License
15*49cdfc7eSAndroid Build Coastguard Worker * along with this program. If not, see <http://www.gnu.org/licenses/>.
16*49cdfc7eSAndroid Build Coastguard Worker *
17*49cdfc7eSAndroid Build Coastguard Worker */
18*49cdfc7eSAndroid Build Coastguard Worker
19*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros_fn.h"
22*49cdfc7eSAndroid Build Coastguard Worker #include "safe_net_fn.h"
23*49cdfc7eSAndroid Build Coastguard Worker
tst_sock_addr(const struct sockaddr * sa,socklen_t salen,char * res,size_t len)24*49cdfc7eSAndroid Build Coastguard Worker char *tst_sock_addr(const struct sockaddr *sa, socklen_t salen, char *res,
25*49cdfc7eSAndroid Build Coastguard Worker size_t len)
26*49cdfc7eSAndroid Build Coastguard Worker {
27*49cdfc7eSAndroid Build Coastguard Worker char portstr[8];
28*49cdfc7eSAndroid Build Coastguard Worker
29*49cdfc7eSAndroid Build Coastguard Worker switch (sa->sa_family) {
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker case AF_INET: {
32*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_in *sin = (struct sockaddr_in *)sa;
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard Worker if (!inet_ntop(AF_INET, &sin->sin_addr, res, len))
35*49cdfc7eSAndroid Build Coastguard Worker return NULL;
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker if (ntohs(sin->sin_port) != 0) {
38*49cdfc7eSAndroid Build Coastguard Worker snprintf(portstr, sizeof(portstr), ":%d",
39*49cdfc7eSAndroid Build Coastguard Worker ntohs(sin->sin_port));
40*49cdfc7eSAndroid Build Coastguard Worker strcat(res, portstr);
41*49cdfc7eSAndroid Build Coastguard Worker }
42*49cdfc7eSAndroid Build Coastguard Worker
43*49cdfc7eSAndroid Build Coastguard Worker return res;
44*49cdfc7eSAndroid Build Coastguard Worker }
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker case AF_INET6: {
47*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker res[0] = '[';
50*49cdfc7eSAndroid Build Coastguard Worker if (!inet_ntop(AF_INET6, &sin6->sin6_addr, res + 1, len - 1))
51*49cdfc7eSAndroid Build Coastguard Worker return NULL;
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker if (ntohs(sin6->sin6_port) != 0) {
54*49cdfc7eSAndroid Build Coastguard Worker snprintf(portstr, sizeof(portstr), "]:%d",
55*49cdfc7eSAndroid Build Coastguard Worker ntohs(sin6->sin6_port));
56*49cdfc7eSAndroid Build Coastguard Worker strcat(res, portstr);
57*49cdfc7eSAndroid Build Coastguard Worker return res;
58*49cdfc7eSAndroid Build Coastguard Worker }
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker return res + 1;
61*49cdfc7eSAndroid Build Coastguard Worker }
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker case AF_UNIX: {
64*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_un *unp = (struct sockaddr_un *)sa;
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker if (unp->sun_path[0] == '\0')
67*49cdfc7eSAndroid Build Coastguard Worker strcpy(res, "(no pathname bound)");
68*49cdfc7eSAndroid Build Coastguard Worker else
69*49cdfc7eSAndroid Build Coastguard Worker snprintf(res, len, "%s", unp->sun_path);
70*49cdfc7eSAndroid Build Coastguard Worker
71*49cdfc7eSAndroid Build Coastguard Worker return res;
72*49cdfc7eSAndroid Build Coastguard Worker }
73*49cdfc7eSAndroid Build Coastguard Worker
74*49cdfc7eSAndroid Build Coastguard Worker default: {
75*49cdfc7eSAndroid Build Coastguard Worker snprintf(res, len,
76*49cdfc7eSAndroid Build Coastguard Worker "sock_ntop: unknown AF_xxx: %d, len: %d",
77*49cdfc7eSAndroid Build Coastguard Worker sa->sa_family, salen);
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker return res;
80*49cdfc7eSAndroid Build Coastguard Worker }
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker }
83*49cdfc7eSAndroid Build Coastguard Worker }
84*49cdfc7eSAndroid Build Coastguard Worker
tst_getsockport(const char * file,const int lineno,int sockfd)85*49cdfc7eSAndroid Build Coastguard Worker int tst_getsockport(const char *file, const int lineno, int sockfd)
86*49cdfc7eSAndroid Build Coastguard Worker {
87*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_storage ss;
88*49cdfc7eSAndroid Build Coastguard Worker socklen_t addrlen = sizeof(ss);
89*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr *sa = (struct sockaddr *)&ss;
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker safe_getsockname(file, lineno, NULL, sockfd, sa, &addrlen);
92*49cdfc7eSAndroid Build Coastguard Worker
93*49cdfc7eSAndroid Build Coastguard Worker switch (sa->sa_family) {
94*49cdfc7eSAndroid Build Coastguard Worker case AF_INET: {
95*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_in *sin = (struct sockaddr_in *)sa;
96*49cdfc7eSAndroid Build Coastguard Worker
97*49cdfc7eSAndroid Build Coastguard Worker return ntohs(sin->sin_port);
98*49cdfc7eSAndroid Build Coastguard Worker }
99*49cdfc7eSAndroid Build Coastguard Worker case AF_INET6: {
100*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard Worker return ntohs(sin6->sin6_port);
103*49cdfc7eSAndroid Build Coastguard Worker } }
104*49cdfc7eSAndroid Build Coastguard Worker
105*49cdfc7eSAndroid Build Coastguard Worker return -1;
106*49cdfc7eSAndroid Build Coastguard Worker }
107*49cdfc7eSAndroid Build Coastguard Worker
safe_socket(const char * file,const int lineno,void (cleanup_fn)(void),int domain,int type,int protocol)108*49cdfc7eSAndroid Build Coastguard Worker int safe_socket(const char *file, const int lineno, void (cleanup_fn)(void),
109*49cdfc7eSAndroid Build Coastguard Worker int domain, int type, int protocol)
110*49cdfc7eSAndroid Build Coastguard Worker {
111*49cdfc7eSAndroid Build Coastguard Worker int rval, ttype;
112*49cdfc7eSAndroid Build Coastguard Worker
113*49cdfc7eSAndroid Build Coastguard Worker rval = socket(domain, type, protocol);
114*49cdfc7eSAndroid Build Coastguard Worker
115*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
116*49cdfc7eSAndroid Build Coastguard Worker switch (errno) {
117*49cdfc7eSAndroid Build Coastguard Worker case EPROTONOSUPPORT:
118*49cdfc7eSAndroid Build Coastguard Worker case ESOCKTNOSUPPORT:
119*49cdfc7eSAndroid Build Coastguard Worker case EOPNOTSUPP:
120*49cdfc7eSAndroid Build Coastguard Worker case EPFNOSUPPORT:
121*49cdfc7eSAndroid Build Coastguard Worker case EAFNOSUPPORT:
122*49cdfc7eSAndroid Build Coastguard Worker ttype = TCONF;
123*49cdfc7eSAndroid Build Coastguard Worker break;
124*49cdfc7eSAndroid Build Coastguard Worker default:
125*49cdfc7eSAndroid Build Coastguard Worker ttype = TBROK;
126*49cdfc7eSAndroid Build Coastguard Worker }
127*49cdfc7eSAndroid Build Coastguard Worker
128*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, ttype | TERRNO, cleanup_fn,
129*49cdfc7eSAndroid Build Coastguard Worker "socket(%d, %d, %d) failed", domain, type, protocol);
130*49cdfc7eSAndroid Build Coastguard Worker } else if (rval < 0) {
131*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
132*49cdfc7eSAndroid Build Coastguard Worker "Invalid socket(%d, %d, %d) return value %d", domain,
133*49cdfc7eSAndroid Build Coastguard Worker type, protocol, rval);
134*49cdfc7eSAndroid Build Coastguard Worker }
135*49cdfc7eSAndroid Build Coastguard Worker
136*49cdfc7eSAndroid Build Coastguard Worker return rval;
137*49cdfc7eSAndroid Build Coastguard Worker }
138*49cdfc7eSAndroid Build Coastguard Worker
safe_socketpair(const char * file,const int lineno,int domain,int type,int protocol,int sv[])139*49cdfc7eSAndroid Build Coastguard Worker int safe_socketpair(const char *file, const int lineno, int domain, int type,
140*49cdfc7eSAndroid Build Coastguard Worker int protocol, int sv[])
141*49cdfc7eSAndroid Build Coastguard Worker {
142*49cdfc7eSAndroid Build Coastguard Worker int rval, ttype;
143*49cdfc7eSAndroid Build Coastguard Worker
144*49cdfc7eSAndroid Build Coastguard Worker rval = socketpair(domain, type, protocol, sv);
145*49cdfc7eSAndroid Build Coastguard Worker
146*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
147*49cdfc7eSAndroid Build Coastguard Worker switch (errno) {
148*49cdfc7eSAndroid Build Coastguard Worker case EPROTONOSUPPORT:
149*49cdfc7eSAndroid Build Coastguard Worker case EOPNOTSUPP:
150*49cdfc7eSAndroid Build Coastguard Worker case EAFNOSUPPORT:
151*49cdfc7eSAndroid Build Coastguard Worker ttype = TCONF;
152*49cdfc7eSAndroid Build Coastguard Worker break;
153*49cdfc7eSAndroid Build Coastguard Worker default:
154*49cdfc7eSAndroid Build Coastguard Worker ttype = TBROK;
155*49cdfc7eSAndroid Build Coastguard Worker }
156*49cdfc7eSAndroid Build Coastguard Worker
157*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, ttype | TERRNO, NULL,
158*49cdfc7eSAndroid Build Coastguard Worker "socketpair(%d, %d, %d, %p) failed", domain, type,
159*49cdfc7eSAndroid Build Coastguard Worker protocol, sv);
160*49cdfc7eSAndroid Build Coastguard Worker } else if (rval) {
161*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
162*49cdfc7eSAndroid Build Coastguard Worker "Invalid socketpair(%d, %d, %d, %p) return value %d",
163*49cdfc7eSAndroid Build Coastguard Worker domain, type, protocol, sv, rval);
164*49cdfc7eSAndroid Build Coastguard Worker }
165*49cdfc7eSAndroid Build Coastguard Worker
166*49cdfc7eSAndroid Build Coastguard Worker return rval;
167*49cdfc7eSAndroid Build Coastguard Worker }
168*49cdfc7eSAndroid Build Coastguard Worker
safe_getsockopt(const char * file,const int lineno,int sockfd,int level,int optname,void * optval,socklen_t * optlen)169*49cdfc7eSAndroid Build Coastguard Worker int safe_getsockopt(const char *file, const int lineno, int sockfd, int level,
170*49cdfc7eSAndroid Build Coastguard Worker int optname, void *optval, socklen_t *optlen)
171*49cdfc7eSAndroid Build Coastguard Worker {
172*49cdfc7eSAndroid Build Coastguard Worker int rval = getsockopt(sockfd, level, optname, optval, optlen);
173*49cdfc7eSAndroid Build Coastguard Worker
174*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
175*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
176*49cdfc7eSAndroid Build Coastguard Worker "getsockopt(%d, %d, %d, %p, %p) failed",
177*49cdfc7eSAndroid Build Coastguard Worker sockfd, level, optname, optval, optlen);
178*49cdfc7eSAndroid Build Coastguard Worker } else if (rval) {
179*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
180*49cdfc7eSAndroid Build Coastguard Worker "Invalid getsockopt(%d, %d, %d, %p, %p) return value %d",
181*49cdfc7eSAndroid Build Coastguard Worker sockfd, level, optname, optval, optlen, rval);
182*49cdfc7eSAndroid Build Coastguard Worker }
183*49cdfc7eSAndroid Build Coastguard Worker
184*49cdfc7eSAndroid Build Coastguard Worker return rval;
185*49cdfc7eSAndroid Build Coastguard Worker }
186*49cdfc7eSAndroid Build Coastguard Worker
safe_setsockopt(const char * file,const int lineno,int sockfd,int level,int optname,const void * optval,socklen_t optlen)187*49cdfc7eSAndroid Build Coastguard Worker int safe_setsockopt(const char *file, const int lineno, int sockfd, int level,
188*49cdfc7eSAndroid Build Coastguard Worker int optname, const void *optval, socklen_t optlen)
189*49cdfc7eSAndroid Build Coastguard Worker {
190*49cdfc7eSAndroid Build Coastguard Worker int rval;
191*49cdfc7eSAndroid Build Coastguard Worker
192*49cdfc7eSAndroid Build Coastguard Worker rval = setsockopt(sockfd, level, optname, optval, optlen);
193*49cdfc7eSAndroid Build Coastguard Worker
194*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
195*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
196*49cdfc7eSAndroid Build Coastguard Worker "setsockopt(%d, %d, %d, %p, %d) failed",
197*49cdfc7eSAndroid Build Coastguard Worker sockfd, level, optname, optval, optlen);
198*49cdfc7eSAndroid Build Coastguard Worker } else if (rval) {
199*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
200*49cdfc7eSAndroid Build Coastguard Worker "Invalid setsockopt(%d, %d, %d, %p, %d) return value %d",
201*49cdfc7eSAndroid Build Coastguard Worker sockfd, level, optname, optval, optlen, rval);
202*49cdfc7eSAndroid Build Coastguard Worker }
203*49cdfc7eSAndroid Build Coastguard Worker
204*49cdfc7eSAndroid Build Coastguard Worker return rval;
205*49cdfc7eSAndroid Build Coastguard Worker }
206*49cdfc7eSAndroid Build Coastguard Worker
safe_send(const char * file,const int lineno,char len_strict,int sockfd,const void * buf,size_t len,int flags)207*49cdfc7eSAndroid Build Coastguard Worker ssize_t safe_send(const char *file, const int lineno, char len_strict,
208*49cdfc7eSAndroid Build Coastguard Worker int sockfd, const void *buf, size_t len, int flags)
209*49cdfc7eSAndroid Build Coastguard Worker {
210*49cdfc7eSAndroid Build Coastguard Worker ssize_t rval;
211*49cdfc7eSAndroid Build Coastguard Worker
212*49cdfc7eSAndroid Build Coastguard Worker rval = send(sockfd, buf, len, flags);
213*49cdfc7eSAndroid Build Coastguard Worker
214*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1 || (len_strict && (size_t)rval != len)) {
215*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
216*49cdfc7eSAndroid Build Coastguard Worker "send(%d, %p, %zu, %d) failed", sockfd, buf, len,
217*49cdfc7eSAndroid Build Coastguard Worker flags);
218*49cdfc7eSAndroid Build Coastguard Worker } else if (rval < 0) {
219*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
220*49cdfc7eSAndroid Build Coastguard Worker "Invalid send(%d, %p, %zu, %d) return value %zd",
221*49cdfc7eSAndroid Build Coastguard Worker sockfd, buf, len, flags, rval);
222*49cdfc7eSAndroid Build Coastguard Worker }
223*49cdfc7eSAndroid Build Coastguard Worker
224*49cdfc7eSAndroid Build Coastguard Worker return rval;
225*49cdfc7eSAndroid Build Coastguard Worker }
226*49cdfc7eSAndroid Build Coastguard Worker
safe_sendto(const char * file,const int lineno,char len_strict,int sockfd,const void * buf,size_t len,int flags,const struct sockaddr * dest_addr,socklen_t addrlen)227*49cdfc7eSAndroid Build Coastguard Worker ssize_t safe_sendto(const char *file, const int lineno, char len_strict,
228*49cdfc7eSAndroid Build Coastguard Worker int sockfd, const void *buf, size_t len, int flags,
229*49cdfc7eSAndroid Build Coastguard Worker const struct sockaddr *dest_addr, socklen_t addrlen)
230*49cdfc7eSAndroid Build Coastguard Worker {
231*49cdfc7eSAndroid Build Coastguard Worker ssize_t rval;
232*49cdfc7eSAndroid Build Coastguard Worker char res[128];
233*49cdfc7eSAndroid Build Coastguard Worker
234*49cdfc7eSAndroid Build Coastguard Worker rval = sendto(sockfd, buf, len, flags, dest_addr, addrlen);
235*49cdfc7eSAndroid Build Coastguard Worker
236*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1 || (len_strict && (size_t)rval != len)) {
237*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
238*49cdfc7eSAndroid Build Coastguard Worker "sendto(%d, %p, %zu, %d, %s, %d) failed",
239*49cdfc7eSAndroid Build Coastguard Worker sockfd, buf, len, flags,
240*49cdfc7eSAndroid Build Coastguard Worker tst_sock_addr(dest_addr, addrlen, res, sizeof(res)),
241*49cdfc7eSAndroid Build Coastguard Worker addrlen);
242*49cdfc7eSAndroid Build Coastguard Worker } else if (rval < 0) {
243*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
244*49cdfc7eSAndroid Build Coastguard Worker "Invalid sendto(%d, %p, %zu, %d, %s, %d) return value %zd",
245*49cdfc7eSAndroid Build Coastguard Worker sockfd, buf, len, flags,
246*49cdfc7eSAndroid Build Coastguard Worker tst_sock_addr(dest_addr, addrlen, res, sizeof(res)),
247*49cdfc7eSAndroid Build Coastguard Worker addrlen, rval);
248*49cdfc7eSAndroid Build Coastguard Worker }
249*49cdfc7eSAndroid Build Coastguard Worker
250*49cdfc7eSAndroid Build Coastguard Worker return rval;
251*49cdfc7eSAndroid Build Coastguard Worker }
252*49cdfc7eSAndroid Build Coastguard Worker
safe_sendmsg(const char * file,const int lineno,size_t len,int sockfd,const struct msghdr * msg,int flags)253*49cdfc7eSAndroid Build Coastguard Worker ssize_t safe_sendmsg(const char *file, const int lineno, size_t len,
254*49cdfc7eSAndroid Build Coastguard Worker int sockfd, const struct msghdr *msg, int flags)
255*49cdfc7eSAndroid Build Coastguard Worker {
256*49cdfc7eSAndroid Build Coastguard Worker ssize_t rval;
257*49cdfc7eSAndroid Build Coastguard Worker
258*49cdfc7eSAndroid Build Coastguard Worker rval = sendmsg(sockfd, msg, flags);
259*49cdfc7eSAndroid Build Coastguard Worker
260*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
261*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
262*49cdfc7eSAndroid Build Coastguard Worker "sendmsg(%d, %p, %d) failed", sockfd, msg, flags);
263*49cdfc7eSAndroid Build Coastguard Worker } else if (rval < 0) {
264*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
265*49cdfc7eSAndroid Build Coastguard Worker "Invalid sendmsg(%d, %p, %d) return value %zd",
266*49cdfc7eSAndroid Build Coastguard Worker sockfd, msg, flags, rval);
267*49cdfc7eSAndroid Build Coastguard Worker } else if (len && (size_t)rval != len) {
268*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK, NULL,
269*49cdfc7eSAndroid Build Coastguard Worker "sendmsg(%d, %p, %d) ret(%zd) != len(%zu)",
270*49cdfc7eSAndroid Build Coastguard Worker sockfd, msg, flags, rval, len);
271*49cdfc7eSAndroid Build Coastguard Worker }
272*49cdfc7eSAndroid Build Coastguard Worker
273*49cdfc7eSAndroid Build Coastguard Worker return rval;
274*49cdfc7eSAndroid Build Coastguard Worker }
275*49cdfc7eSAndroid Build Coastguard Worker
safe_recv(const char * file,const int lineno,size_t len,int sockfd,void * buf,size_t size,int flags)276*49cdfc7eSAndroid Build Coastguard Worker ssize_t safe_recv(const char *file, const int lineno, size_t len,
277*49cdfc7eSAndroid Build Coastguard Worker int sockfd, void *buf, size_t size, int flags)
278*49cdfc7eSAndroid Build Coastguard Worker {
279*49cdfc7eSAndroid Build Coastguard Worker ssize_t rval;
280*49cdfc7eSAndroid Build Coastguard Worker
281*49cdfc7eSAndroid Build Coastguard Worker rval = recv(sockfd, buf, size, flags);
282*49cdfc7eSAndroid Build Coastguard Worker
283*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
284*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
285*49cdfc7eSAndroid Build Coastguard Worker "recv(%d, %p, %zu, %d) failed", sockfd, buf, size,
286*49cdfc7eSAndroid Build Coastguard Worker flags);
287*49cdfc7eSAndroid Build Coastguard Worker } else if (rval < 0) {
288*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
289*49cdfc7eSAndroid Build Coastguard Worker "Invalid recv(%d, %p, %zu, %d) return value %zd",
290*49cdfc7eSAndroid Build Coastguard Worker sockfd, buf, size, flags, rval);
291*49cdfc7eSAndroid Build Coastguard Worker } else if (len && (size_t)rval != len) {
292*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK, NULL,
293*49cdfc7eSAndroid Build Coastguard Worker "recv(%d, %p, %zu, %d) ret(%zd) != len(%zu)",
294*49cdfc7eSAndroid Build Coastguard Worker sockfd, buf, size, flags, rval, len);
295*49cdfc7eSAndroid Build Coastguard Worker }
296*49cdfc7eSAndroid Build Coastguard Worker
297*49cdfc7eSAndroid Build Coastguard Worker return rval;
298*49cdfc7eSAndroid Build Coastguard Worker
299*49cdfc7eSAndroid Build Coastguard Worker }
300*49cdfc7eSAndroid Build Coastguard Worker
safe_recvmsg(const char * file,const int lineno,size_t len,int sockfd,struct msghdr * msg,int flags)301*49cdfc7eSAndroid Build Coastguard Worker ssize_t safe_recvmsg(const char *file, const int lineno, size_t len,
302*49cdfc7eSAndroid Build Coastguard Worker int sockfd, struct msghdr *msg, int flags)
303*49cdfc7eSAndroid Build Coastguard Worker {
304*49cdfc7eSAndroid Build Coastguard Worker ssize_t rval;
305*49cdfc7eSAndroid Build Coastguard Worker
306*49cdfc7eSAndroid Build Coastguard Worker rval = recvmsg(sockfd, msg, flags);
307*49cdfc7eSAndroid Build Coastguard Worker
308*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
309*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
310*49cdfc7eSAndroid Build Coastguard Worker "recvmsg(%d, %p, %d) failed", sockfd, msg, flags);
311*49cdfc7eSAndroid Build Coastguard Worker } else if (rval < 0) {
312*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
313*49cdfc7eSAndroid Build Coastguard Worker "Invalid recvmsg(%d, %p, %d) return value %zd",
314*49cdfc7eSAndroid Build Coastguard Worker sockfd, msg, flags, rval);
315*49cdfc7eSAndroid Build Coastguard Worker } else if (len && (size_t)rval != len) {
316*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK, NULL,
317*49cdfc7eSAndroid Build Coastguard Worker "recvmsg(%d, %p, %d) ret(%zd) != len(%zu)",
318*49cdfc7eSAndroid Build Coastguard Worker sockfd, msg, flags, rval, len);
319*49cdfc7eSAndroid Build Coastguard Worker }
320*49cdfc7eSAndroid Build Coastguard Worker
321*49cdfc7eSAndroid Build Coastguard Worker return rval;
322*49cdfc7eSAndroid Build Coastguard Worker
323*49cdfc7eSAndroid Build Coastguard Worker }
324*49cdfc7eSAndroid Build Coastguard Worker
safe_bind(const char * file,const int lineno,void (cleanup_fn)(void),int socket,const struct sockaddr * address,socklen_t address_len)325*49cdfc7eSAndroid Build Coastguard Worker int safe_bind(const char *file, const int lineno, void (cleanup_fn)(void),
326*49cdfc7eSAndroid Build Coastguard Worker int socket, const struct sockaddr *address,
327*49cdfc7eSAndroid Build Coastguard Worker socklen_t address_len)
328*49cdfc7eSAndroid Build Coastguard Worker {
329*49cdfc7eSAndroid Build Coastguard Worker int i, ret;
330*49cdfc7eSAndroid Build Coastguard Worker char buf[128];
331*49cdfc7eSAndroid Build Coastguard Worker
332*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 120; i++) {
333*49cdfc7eSAndroid Build Coastguard Worker ret = bind(socket, address, address_len);
334*49cdfc7eSAndroid Build Coastguard Worker
335*49cdfc7eSAndroid Build Coastguard Worker if (!ret)
336*49cdfc7eSAndroid Build Coastguard Worker return 0;
337*49cdfc7eSAndroid Build Coastguard Worker
338*49cdfc7eSAndroid Build Coastguard Worker if (ret != -1) {
339*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
340*49cdfc7eSAndroid Build Coastguard Worker "Invalid bind(%d, %s, %d) return value %d",
341*49cdfc7eSAndroid Build Coastguard Worker socket, tst_sock_addr(address, address_len,
342*49cdfc7eSAndroid Build Coastguard Worker buf, sizeof(buf)), address_len, ret);
343*49cdfc7eSAndroid Build Coastguard Worker return ret;
344*49cdfc7eSAndroid Build Coastguard Worker } else if (errno != EADDRINUSE) {
345*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
346*49cdfc7eSAndroid Build Coastguard Worker "bind(%d, %s, %d) failed", socket,
347*49cdfc7eSAndroid Build Coastguard Worker tst_sock_addr(address, address_len, buf,
348*49cdfc7eSAndroid Build Coastguard Worker sizeof(buf)), address_len);
349*49cdfc7eSAndroid Build Coastguard Worker return ret;
350*49cdfc7eSAndroid Build Coastguard Worker }
351*49cdfc7eSAndroid Build Coastguard Worker
352*49cdfc7eSAndroid Build Coastguard Worker if ((i + 1) % 10 == 0) {
353*49cdfc7eSAndroid Build Coastguard Worker tst_resm_(file, lineno, TINFO,
354*49cdfc7eSAndroid Build Coastguard Worker "address is in use, waited %3i sec", i + 1);
355*49cdfc7eSAndroid Build Coastguard Worker }
356*49cdfc7eSAndroid Build Coastguard Worker
357*49cdfc7eSAndroid Build Coastguard Worker sleep(1);
358*49cdfc7eSAndroid Build Coastguard Worker }
359*49cdfc7eSAndroid Build Coastguard Worker
360*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
361*49cdfc7eSAndroid Build Coastguard Worker "Failed to bind(%d, %s, %d) after 120 retries", socket,
362*49cdfc7eSAndroid Build Coastguard Worker tst_sock_addr(address, address_len, buf, sizeof(buf)),
363*49cdfc7eSAndroid Build Coastguard Worker address_len);
364*49cdfc7eSAndroid Build Coastguard Worker return -1;
365*49cdfc7eSAndroid Build Coastguard Worker }
366*49cdfc7eSAndroid Build Coastguard Worker
safe_listen(const char * file,const int lineno,void (cleanup_fn)(void),int socket,int backlog)367*49cdfc7eSAndroid Build Coastguard Worker int safe_listen(const char *file, const int lineno, void (cleanup_fn)(void),
368*49cdfc7eSAndroid Build Coastguard Worker int socket, int backlog)
369*49cdfc7eSAndroid Build Coastguard Worker {
370*49cdfc7eSAndroid Build Coastguard Worker int rval;
371*49cdfc7eSAndroid Build Coastguard Worker int res = TBROK;
372*49cdfc7eSAndroid Build Coastguard Worker
373*49cdfc7eSAndroid Build Coastguard Worker rval = listen(socket, backlog);
374*49cdfc7eSAndroid Build Coastguard Worker
375*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
376*49cdfc7eSAndroid Build Coastguard Worker if (errno == ENOSYS)
377*49cdfc7eSAndroid Build Coastguard Worker res = TCONF;
378*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, res | TERRNO, cleanup_fn,
379*49cdfc7eSAndroid Build Coastguard Worker "listen(%d, %d) failed", socket, backlog);
380*49cdfc7eSAndroid Build Coastguard Worker } else if (rval) {
381*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
382*49cdfc7eSAndroid Build Coastguard Worker "Invalid listen(%d, %d) return value %d", socket,
383*49cdfc7eSAndroid Build Coastguard Worker backlog, rval);
384*49cdfc7eSAndroid Build Coastguard Worker }
385*49cdfc7eSAndroid Build Coastguard Worker
386*49cdfc7eSAndroid Build Coastguard Worker return rval;
387*49cdfc7eSAndroid Build Coastguard Worker }
388*49cdfc7eSAndroid Build Coastguard Worker
safe_accept(const char * file,const int lineno,void (cleanup_fn)(void),int sockfd,struct sockaddr * addr,socklen_t * addrlen)389*49cdfc7eSAndroid Build Coastguard Worker int safe_accept(const char *file, const int lineno, void (cleanup_fn)(void),
390*49cdfc7eSAndroid Build Coastguard Worker int sockfd, struct sockaddr *addr, socklen_t *addrlen)
391*49cdfc7eSAndroid Build Coastguard Worker {
392*49cdfc7eSAndroid Build Coastguard Worker int rval;
393*49cdfc7eSAndroid Build Coastguard Worker
394*49cdfc7eSAndroid Build Coastguard Worker rval = accept(sockfd, addr, addrlen);
395*49cdfc7eSAndroid Build Coastguard Worker
396*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
397*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
398*49cdfc7eSAndroid Build Coastguard Worker "accept(%d, %p, %d) failed", sockfd, addr, *addrlen);
399*49cdfc7eSAndroid Build Coastguard Worker } else if (rval < 0) {
400*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
401*49cdfc7eSAndroid Build Coastguard Worker "Invalid accept(%d, %p, %d) return value %d", sockfd,
402*49cdfc7eSAndroid Build Coastguard Worker addr, *addrlen, rval);
403*49cdfc7eSAndroid Build Coastguard Worker }
404*49cdfc7eSAndroid Build Coastguard Worker
405*49cdfc7eSAndroid Build Coastguard Worker return rval;
406*49cdfc7eSAndroid Build Coastguard Worker }
407*49cdfc7eSAndroid Build Coastguard Worker
safe_connect(const char * file,const int lineno,void (cleanup_fn)(void),int sockfd,const struct sockaddr * addr,socklen_t addrlen)408*49cdfc7eSAndroid Build Coastguard Worker int safe_connect(const char *file, const int lineno, void (cleanup_fn)(void),
409*49cdfc7eSAndroid Build Coastguard Worker int sockfd, const struct sockaddr *addr, socklen_t addrlen)
410*49cdfc7eSAndroid Build Coastguard Worker {
411*49cdfc7eSAndroid Build Coastguard Worker int rval;
412*49cdfc7eSAndroid Build Coastguard Worker char buf[128];
413*49cdfc7eSAndroid Build Coastguard Worker
414*49cdfc7eSAndroid Build Coastguard Worker rval = connect(sockfd, addr, addrlen);
415*49cdfc7eSAndroid Build Coastguard Worker
416*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
417*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
418*49cdfc7eSAndroid Build Coastguard Worker "connect(%d, %s, %d) failed", sockfd,
419*49cdfc7eSAndroid Build Coastguard Worker tst_sock_addr(addr, addrlen, buf, sizeof(buf)),
420*49cdfc7eSAndroid Build Coastguard Worker addrlen);
421*49cdfc7eSAndroid Build Coastguard Worker } else if (rval) {
422*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
423*49cdfc7eSAndroid Build Coastguard Worker "Invalid connect(%d, %s, %d) return value %d", sockfd,
424*49cdfc7eSAndroid Build Coastguard Worker tst_sock_addr(addr, addrlen, buf, sizeof(buf)),
425*49cdfc7eSAndroid Build Coastguard Worker addrlen, rval);
426*49cdfc7eSAndroid Build Coastguard Worker }
427*49cdfc7eSAndroid Build Coastguard Worker
428*49cdfc7eSAndroid Build Coastguard Worker return rval;
429*49cdfc7eSAndroid Build Coastguard Worker }
430*49cdfc7eSAndroid Build Coastguard Worker
safe_getsockname(const char * file,const int lineno,void (cleanup_fn)(void),int sockfd,struct sockaddr * addr,socklen_t * addrlen)431*49cdfc7eSAndroid Build Coastguard Worker int safe_getsockname(const char *file, const int lineno,
432*49cdfc7eSAndroid Build Coastguard Worker void (cleanup_fn)(void), int sockfd, struct sockaddr *addr,
433*49cdfc7eSAndroid Build Coastguard Worker socklen_t *addrlen)
434*49cdfc7eSAndroid Build Coastguard Worker {
435*49cdfc7eSAndroid Build Coastguard Worker int rval;
436*49cdfc7eSAndroid Build Coastguard Worker char buf[128];
437*49cdfc7eSAndroid Build Coastguard Worker
438*49cdfc7eSAndroid Build Coastguard Worker rval = getsockname(sockfd, addr, addrlen);
439*49cdfc7eSAndroid Build Coastguard Worker
440*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
441*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
442*49cdfc7eSAndroid Build Coastguard Worker "getsockname(%d, %s, %d) failed", sockfd,
443*49cdfc7eSAndroid Build Coastguard Worker tst_sock_addr(addr, *addrlen, buf, sizeof(buf)),
444*49cdfc7eSAndroid Build Coastguard Worker *addrlen);
445*49cdfc7eSAndroid Build Coastguard Worker } else if (rval) {
446*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
447*49cdfc7eSAndroid Build Coastguard Worker "Invalid getsockname(%d, %s, %d) return value %d",
448*49cdfc7eSAndroid Build Coastguard Worker sockfd, tst_sock_addr(addr, *addrlen, buf,
449*49cdfc7eSAndroid Build Coastguard Worker sizeof(buf)), *addrlen, rval);
450*49cdfc7eSAndroid Build Coastguard Worker }
451*49cdfc7eSAndroid Build Coastguard Worker
452*49cdfc7eSAndroid Build Coastguard Worker return rval;
453*49cdfc7eSAndroid Build Coastguard Worker }
454*49cdfc7eSAndroid Build Coastguard Worker
safe_gethostname(const char * file,const int lineno,char * name,size_t size)455*49cdfc7eSAndroid Build Coastguard Worker int safe_gethostname(const char *file, const int lineno,
456*49cdfc7eSAndroid Build Coastguard Worker char *name, size_t size)
457*49cdfc7eSAndroid Build Coastguard Worker {
458*49cdfc7eSAndroid Build Coastguard Worker int rval = gethostname(name, size);
459*49cdfc7eSAndroid Build Coastguard Worker
460*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
461*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
462*49cdfc7eSAndroid Build Coastguard Worker "gethostname(%p, %zu) failed", name, size);
463*49cdfc7eSAndroid Build Coastguard Worker } else if (rval) {
464*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
465*49cdfc7eSAndroid Build Coastguard Worker "Invalid gethostname(%p, %zu) return value %d", name,
466*49cdfc7eSAndroid Build Coastguard Worker size, rval);
467*49cdfc7eSAndroid Build Coastguard Worker }
468*49cdfc7eSAndroid Build Coastguard Worker
469*49cdfc7eSAndroid Build Coastguard Worker return rval;
470*49cdfc7eSAndroid Build Coastguard Worker }
471*49cdfc7eSAndroid Build Coastguard Worker
safe_sethostname(const char * file,const int lineno,const char * name,size_t size)472*49cdfc7eSAndroid Build Coastguard Worker int safe_sethostname(const char *file, const int lineno,
473*49cdfc7eSAndroid Build Coastguard Worker const char *name, size_t size)
474*49cdfc7eSAndroid Build Coastguard Worker {
475*49cdfc7eSAndroid Build Coastguard Worker int rval = sethostname(name, size);
476*49cdfc7eSAndroid Build Coastguard Worker
477*49cdfc7eSAndroid Build Coastguard Worker if (rval == -1) {
478*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
479*49cdfc7eSAndroid Build Coastguard Worker "sethostname(%p, %zu) failed", name, size);
480*49cdfc7eSAndroid Build Coastguard Worker } else if (rval) {
481*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK | TERRNO, NULL,
482*49cdfc7eSAndroid Build Coastguard Worker "Invalid sethostname(%p, %zu) return value %d", name,
483*49cdfc7eSAndroid Build Coastguard Worker size, rval);
484*49cdfc7eSAndroid Build Coastguard Worker }
485*49cdfc7eSAndroid Build Coastguard Worker
486*49cdfc7eSAndroid Build Coastguard Worker return rval;
487*49cdfc7eSAndroid Build Coastguard Worker }
488*49cdfc7eSAndroid Build Coastguard Worker
489*49cdfc7eSAndroid Build Coastguard Worker /*
490*49cdfc7eSAndroid Build Coastguard Worker * @return port in network byte order.
491*49cdfc7eSAndroid Build Coastguard Worker */
tst_get_unused_port(const char * file,const int lineno,void (cleanup_fn)(void),unsigned short family,int type)492*49cdfc7eSAndroid Build Coastguard Worker unsigned short tst_get_unused_port(const char *file, const int lineno,
493*49cdfc7eSAndroid Build Coastguard Worker void (cleanup_fn)(void), unsigned short family, int type)
494*49cdfc7eSAndroid Build Coastguard Worker {
495*49cdfc7eSAndroid Build Coastguard Worker int sock, ret;
496*49cdfc7eSAndroid Build Coastguard Worker socklen_t slen;
497*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_storage _addr;
498*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr *addr = (struct sockaddr *)&_addr;
499*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
500*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
501*49cdfc7eSAndroid Build Coastguard Worker
502*49cdfc7eSAndroid Build Coastguard Worker switch (family) {
503*49cdfc7eSAndroid Build Coastguard Worker case AF_INET:
504*49cdfc7eSAndroid Build Coastguard Worker addr4->sin_family = AF_INET;
505*49cdfc7eSAndroid Build Coastguard Worker addr4->sin_port = 0;
506*49cdfc7eSAndroid Build Coastguard Worker addr4->sin_addr.s_addr = INADDR_ANY;
507*49cdfc7eSAndroid Build Coastguard Worker slen = sizeof(*addr4);
508*49cdfc7eSAndroid Build Coastguard Worker break;
509*49cdfc7eSAndroid Build Coastguard Worker
510*49cdfc7eSAndroid Build Coastguard Worker case AF_INET6:
511*49cdfc7eSAndroid Build Coastguard Worker addr6->sin6_family = AF_INET6;
512*49cdfc7eSAndroid Build Coastguard Worker addr6->sin6_port = 0;
513*49cdfc7eSAndroid Build Coastguard Worker addr6->sin6_addr = in6addr_any;
514*49cdfc7eSAndroid Build Coastguard Worker slen = sizeof(*addr6);
515*49cdfc7eSAndroid Build Coastguard Worker break;
516*49cdfc7eSAndroid Build Coastguard Worker
517*49cdfc7eSAndroid Build Coastguard Worker default:
518*49cdfc7eSAndroid Build Coastguard Worker tst_brkm_(file, lineno, TBROK, cleanup_fn,
519*49cdfc7eSAndroid Build Coastguard Worker "%s(): Unsupported socket family %d", __func__,
520*49cdfc7eSAndroid Build Coastguard Worker family);
521*49cdfc7eSAndroid Build Coastguard Worker return -1;
522*49cdfc7eSAndroid Build Coastguard Worker }
523*49cdfc7eSAndroid Build Coastguard Worker
524*49cdfc7eSAndroid Build Coastguard Worker sock = safe_socket(file, lineno, cleanup_fn, addr->sa_family, type, 0);
525*49cdfc7eSAndroid Build Coastguard Worker
526*49cdfc7eSAndroid Build Coastguard Worker if (sock < 0)
527*49cdfc7eSAndroid Build Coastguard Worker return sock;
528*49cdfc7eSAndroid Build Coastguard Worker
529*49cdfc7eSAndroid Build Coastguard Worker ret = safe_bind(file, lineno, cleanup_fn, sock, addr, slen);
530*49cdfc7eSAndroid Build Coastguard Worker
531*49cdfc7eSAndroid Build Coastguard Worker if (ret)
532*49cdfc7eSAndroid Build Coastguard Worker return ret;
533*49cdfc7eSAndroid Build Coastguard Worker
534*49cdfc7eSAndroid Build Coastguard Worker ret = safe_getsockname(file, lineno, cleanup_fn, sock, addr, &slen);
535*49cdfc7eSAndroid Build Coastguard Worker
536*49cdfc7eSAndroid Build Coastguard Worker if (ret)
537*49cdfc7eSAndroid Build Coastguard Worker return ret;
538*49cdfc7eSAndroid Build Coastguard Worker
539*49cdfc7eSAndroid Build Coastguard Worker ret = safe_close(file, lineno, cleanup_fn, sock);
540*49cdfc7eSAndroid Build Coastguard Worker
541*49cdfc7eSAndroid Build Coastguard Worker if (ret)
542*49cdfc7eSAndroid Build Coastguard Worker return ret;
543*49cdfc7eSAndroid Build Coastguard Worker
544*49cdfc7eSAndroid Build Coastguard Worker switch (family) {
545*49cdfc7eSAndroid Build Coastguard Worker case AF_INET:
546*49cdfc7eSAndroid Build Coastguard Worker return addr4->sin_port;
547*49cdfc7eSAndroid Build Coastguard Worker case AF_INET6:
548*49cdfc7eSAndroid Build Coastguard Worker return addr6->sin6_port;
549*49cdfc7eSAndroid Build Coastguard Worker default:
550*49cdfc7eSAndroid Build Coastguard Worker return -1;
551*49cdfc7eSAndroid Build Coastguard Worker }
552*49cdfc7eSAndroid Build Coastguard Worker }
553