xref: /iperf/src/net.c (revision 76b5942f)
1 /*
2  * Copyright (c) 2009-2014, The Regents of the University of California,
3  * through Lawrence Berkeley National Laboratory (subject to receipt of any
4  * required approvals from the U.S. Dept. of Energy).  All rights reserved.
5  *
6  * This code is distributed under a BSD style license, see the LICENSE file
7  * for complete information.
8  */
9 #include "iperf_config.h"
10 
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <errno.h>
14 #include <sys/socket.h>
15 #include <sys/types.h>
16 #include <sys/errno.h>
17 #include <netinet/in.h>
18 #include <netinet/tcp.h>
19 #include <assert.h>
20 #include <netdb.h>
21 #include <string.h>
22 #include <sys/fcntl.h>
23 
24 #ifdef HAVE_SENDFILE
25 #ifdef linux
26 #include <sys/sendfile.h>
27 #else
28 #ifdef __FreeBSD__
29 #include <sys/uio.h>
30 #else
31 #if defined(__APPLE__) && defined(__MACH__)	/* OS X */
32 #include <AvailabilityMacros.h>
33 #if defined(MAC_OS_X_VERSION_10_6)
34 #include <sys/uio.h>
35 #endif
36 #endif
37 #endif
38 #endif
39 #endif /* HAVE_SENDFILE */
40 
41 #include "iperf_util.h"
42 #include "net.h"
43 #include "timer.h"
44 
45 /* netdial and netannouce code comes from libtask: http://swtch.com/libtask/
46  * Copyright: http://swtch.com/libtask/COPYRIGHT
47 */
48 
49 /* make connection to server */
50 int
51 netdial(int domain, int proto, char *local, char *server, int port)
52 {
53     struct addrinfo hints, *local_res, *server_res;
54     int s;
55 
56     if (local) {
57         memset(&hints, 0, sizeof(hints));
58         hints.ai_family = domain;
59         hints.ai_socktype = proto;
60         if (getaddrinfo(local, NULL, &hints, &local_res) != 0)
61             return -1;
62     }
63 
64     memset(&hints, 0, sizeof(hints));
65     hints.ai_family = domain;
66     hints.ai_socktype = proto;
67     if (getaddrinfo(server, NULL, &hints, &server_res) != 0)
68         return -1;
69 
70     s = socket(server_res->ai_family, proto, 0);
71     if (s < 0) {
72 	if (local)
73 	    freeaddrinfo(local_res);
74 	freeaddrinfo(server_res);
75         return -1;
76     }
77 
78     if (local) {
79         if (bind(s, (struct sockaddr *) local_res->ai_addr, local_res->ai_addrlen) < 0) {
80 	    close(s);
81 	    freeaddrinfo(local_res);
82 	    freeaddrinfo(server_res);
83             return -1;
84 	}
85         freeaddrinfo(local_res);
86     }
87 
88     ((struct sockaddr_in *) server_res->ai_addr)->sin_port = htons(port);
89     if (connect(s, (struct sockaddr *) server_res->ai_addr, server_res->ai_addrlen) < 0 && errno != EINPROGRESS) {
90 	close(s);
91 	freeaddrinfo(server_res);
92         return -1;
93     }
94 
95     freeaddrinfo(server_res);
96     return s;
97 }
98 
99 /***************************************************************/
100 
101 int
102 netannounce(int domain, int proto, char *local, int port)
103 {
104     struct addrinfo hints, *res;
105     char portstr[6];
106     int s, opt;
107 
108     snprintf(portstr, 6, "%d", port);
109     memset(&hints, 0, sizeof(hints));
110     /*
111      * If binding to the wildcard address with no explicit address
112      * family specified, then force us to get an AF_INET6 socket.  On
113      * CentOS 6 and MacOS, getaddrinfo(3) with AF_UNSPEC in ai_family,
114      * and ai_flags containing AI_PASSIVE returns a result structure
115      * with ai_family set to AF_INET, with the result that we create
116      * and bind an IPv4 address wildcard address and by default, we
117      * can't accept IPv6 connections.
118      *
119      * On FreeBSD, under the above circumstances, ai_family in the
120      * result structure is set to AF_INET6.
121      */
122     if (domain == AF_UNSPEC && !local) {
123 	hints.ai_family = AF_INET6;
124     }
125     else {
126 	hints.ai_family = domain;
127     }
128     hints.ai_socktype = proto;
129     hints.ai_flags = AI_PASSIVE;
130     if (getaddrinfo(local, portstr, &hints, &res) != 0)
131         return -1;
132 
133     s = socket(res->ai_family, proto, 0);
134     if (s < 0) {
135 	freeaddrinfo(res);
136         return -1;
137     }
138 
139     opt = 1;
140     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
141 		   (char *) &opt, sizeof(opt)) < 0) {
142 	close(s);
143 	freeaddrinfo(res);
144 	return -1;
145     }
146 
147     /*
148      * If we got an IPv6 socket, figure out if it should accept IPv4
149      * connections as well.  We do that if and only if no address
150      * family was specified explicitly.
151      */
152     if (res->ai_family == AF_INET6 && (domain == AF_UNSPEC || domain == AF_INET6)) {
153 	if (domain == AF_UNSPEC)
154 	    opt = 0;
155 	else
156 	    opt = 1;
157 	if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
158 		       (char *) &opt, sizeof(opt)) < 0) {
159 	    close(s);
160 	    freeaddrinfo(res);
161 	    return -1;
162 	}
163     }
164 
165     if (bind(s, (struct sockaddr *) res->ai_addr, res->ai_addrlen) < 0) {
166         close(s);
167 	freeaddrinfo(res);
168         return -1;
169     }
170 
171     freeaddrinfo(res);
172 
173     if (proto == SOCK_STREAM) {
174         if (listen(s, 5) < 0) {
175 	    close(s);
176             return -1;
177         }
178     }
179 
180     return s;
181 }
182 
183 
184 /*******************************************************************/
185 /* reads 'count' bytes from a socket  */
186 /********************************************************************/
187 
188 int
189 Nread(int fd, char *buf, size_t count, int prot)
190 {
191     register ssize_t r;
192     register size_t nleft = count;
193 
194     while (nleft > 0) {
195         r = read(fd, buf, nleft);
196         if (r < 0) {
197             if (errno == EINTR || errno == EAGAIN)
198                 break;
199             else
200                 return NET_HARDERROR;
201         } else if (r == 0)
202             break;
203 
204         nleft -= r;
205         buf += r;
206     }
207     return count - nleft;
208 }
209 
210 
211 /*
212  *                      N W R I T E
213  */
214 
215 int
216 Nwrite(int fd, const char *buf, size_t count, int prot)
217 {
218     register ssize_t r;
219     register size_t nleft = count;
220 
221     while (nleft > 0) {
222 	r = write(fd, buf, nleft);
223 	if (r < 0) {
224 	    switch (errno) {
225 		case EINTR:
226 		case EAGAIN:
227 		return count - nleft;
228 
229 		case ENOBUFS:
230 		return NET_SOFTERROR;
231 
232 		default:
233 		return NET_HARDERROR;
234 	    }
235 	} else if (r == 0)
236 	    return NET_SOFTERROR;
237 	nleft -= r;
238 	buf += r;
239     }
240     return count;
241 }
242 
243 
244 int
245 has_sendfile(void)
246 {
247 #if defined(HAVE_SENDFILE)
248     return 1;
249 #else /* HAVE_SENDFILE */
250     return 0;
251 #endif /* HAVE_SENDFILE */
252 
253 }
254 
255 
256 /*
257  *                      N S E N D F I L E
258  */
259 
260 int
261 Nsendfile(int fromfd, int tofd, const char *buf, size_t count)
262 {
263     off_t offset;
264 #if defined(HAVE_SENDFILE)
265 #if defined(__FreeBSD__) || (defined(__APPLE__) && defined(__MACH__) && defined(MAC_OS_X_VERSION_10_6))
266     off_t sent;
267 #endif
268     register size_t nleft;
269     register ssize_t r;
270 
271     nleft = count;
272     while (nleft > 0) {
273 	offset = count - nleft;
274 #ifdef linux
275 	r = sendfile(tofd, fromfd, &offset, nleft);
276 #else
277 #ifdef __FreeBSD__
278 	r = sendfile(fromfd, tofd, offset, nleft, NULL, &sent, 0);
279 	if (r == 0)
280 	    r = sent;
281 #else
282 #if defined(__APPLE__) && defined(__MACH__) && defined(MAC_OS_X_VERSION_10_6)	/* OS X */
283 	sent = nleft;
284 	r = sendfile(fromfd, tofd, offset, &sent, NULL, 0);
285 	if (r == 0)
286 	    r = sent;
287 #else
288 	/* Shouldn't happen. */
289 	r = -1;
290 	errno = ENOSYS;
291 #endif
292 #endif
293 #endif
294 	if (r < 0) {
295 	    switch (errno) {
296 		case EINTR:
297 		case EAGAIN:
298 		return count - nleft;
299 
300 		case ENOBUFS:
301 		case ENOMEM:
302 		return NET_SOFTERROR;
303 
304 		default:
305 		return NET_HARDERROR;
306 	    }
307 	} else if (r == 0)
308 	    return NET_SOFTERROR;
309 	nleft -= r;
310     }
311     return count;
312 #else /* HAVE_SENDFILE */
313     errno = ENOSYS;	/* error if somehow get called without HAVE_SENDFILE */
314     return -1;
315 #endif /* HAVE_SENDFILE */
316 }
317 
318 /*************************************************************************/
319 
320 /**
321  * getsock_tcp_mss - Returns the MSS size for TCP
322  *
323  */
324 
325 int
326 getsock_tcp_mss(int inSock)
327 {
328     int             mss = 0;
329 
330     int             rc;
331     socklen_t       len;
332 
333     assert(inSock >= 0); /* print error and exit if this is not true */
334 
335     /* query for mss */
336     len = sizeof(mss);
337     rc = getsockopt(inSock, IPPROTO_TCP, TCP_MAXSEG, (char *)&mss, &len);
338     if (rc == -1) {
339 	perror("getsockopt TCP_MAXSEG");
340 	return -1;
341     }
342 
343     return mss;
344 }
345 
346 
347 
348 /*************************************************************/
349 
350 /* sets TCP_NODELAY and TCP_MAXSEG if requested */
351 // XXX: This function is not being used.
352 
353 int
354 set_tcp_options(int sock, int no_delay, int mss)
355 {
356     socklen_t len;
357     int rc;
358     int new_mss;
359 
360     if (no_delay == 1) {
361         len = sizeof(no_delay);
362         rc = setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&no_delay, len);
363         if (rc == -1) {
364             perror("setsockopt TCP_NODELAY");
365             return -1;
366         }
367     }
368 #ifdef TCP_MAXSEG
369     if (mss > 0) {
370         len = sizeof(new_mss);
371         assert(sock != -1);
372 
373         /* set */
374         new_mss = mss;
375         len = sizeof(new_mss);
376         rc = setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, (char *)&new_mss, len);
377         if (rc == -1) {
378             perror("setsockopt TCP_MAXSEG");
379             return -1;
380         }
381         /* verify results */
382         rc = getsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, (char *)&new_mss, &len);
383         if (rc == -1) {
384             perror("getsockopt TCP_MAXSEG");
385             return -1;
386         }
387         if (new_mss != mss) {
388             perror("setsockopt value mismatch");
389             return -1;
390         }
391     }
392 #endif
393     return 0;
394 }
395 
396 /****************************************************************************/
397 
398 int
399 setnonblocking(int fd, int nonblocking)
400 {
401     int flags, newflags;
402 
403     flags = fcntl(fd, F_GETFL, 0);
404     if (flags < 0) {
405         perror("fcntl(F_GETFL)");
406         return -1;
407     }
408     if (nonblocking)
409 	newflags = flags | (int) O_NONBLOCK;
410     else
411 	newflags = flags & ~((int) O_NONBLOCK);
412     if (newflags != flags)
413 	if (fcntl(fd, F_SETFL, newflags) < 0) {
414 	    perror("fcntl(F_SETFL)");
415 	    return -1;
416 	}
417     return 0;
418 }
419 
420 /****************************************************************************/
421 
422 int
423 getsockdomain(int sock)
424 {
425     struct sockaddr sa;
426     socklen_t len = sizeof(sa);
427 
428     if (getsockname(sock, &sa, &len) < 0)
429 	return -1;
430     return sa.sa_family;
431 }
432