xref: /iperf/src/net.c (revision 147d3369)
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      * If we got an IPv6 socket, figure out if it should accept IPv4
148      * connections as well.  We do that if and only if no address
149      * family was specified explicitly.
150      */
151 #ifdef IPV6_V6ONLY
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 #endif /* IPV6_V6ONLY */
165 
166     if (bind(s, (struct sockaddr *) res->ai_addr, res->ai_addrlen) < 0) {
167         close(s);
168 	freeaddrinfo(res);
169         return -1;
170     }
171 
172     freeaddrinfo(res);
173 
174     if (proto == SOCK_STREAM) {
175         if (listen(s, 5) < 0) {
176 	    close(s);
177             return -1;
178         }
179     }
180 
181     return s;
182 }
183 
184 
185 /*******************************************************************/
186 /* reads 'count' bytes from a socket  */
187 /********************************************************************/
188 
189 int
190 Nread(int fd, char *buf, size_t count, int prot)
191 {
192     register ssize_t r;
193     register size_t nleft = count;
194 
195     while (nleft > 0) {
196         r = read(fd, buf, nleft);
197         if (r < 0) {
198             if (errno == EINTR || errno == EAGAIN)
199                 break;
200             else
201                 return NET_HARDERROR;
202         } else if (r == 0)
203             break;
204 
205         nleft -= r;
206         buf += r;
207     }
208     return count - nleft;
209 }
210 
211 
212 /*
213  *                      N W R I T E
214  */
215 
216 int
217 Nwrite(int fd, const char *buf, size_t count, int prot)
218 {
219     register ssize_t r;
220     register size_t nleft = count;
221 
222     while (nleft > 0) {
223 	r = write(fd, buf, nleft);
224 	if (r < 0) {
225 	    switch (errno) {
226 		case EINTR:
227 		case EAGAIN:
228 		return count - nleft;
229 
230 		case ENOBUFS:
231 		return NET_SOFTERROR;
232 
233 		default:
234 		return NET_HARDERROR;
235 	    }
236 	} else if (r == 0)
237 	    return NET_SOFTERROR;
238 	nleft -= r;
239 	buf += r;
240     }
241     return count;
242 }
243 
244 
245 int
246 has_sendfile(void)
247 {
248 #if defined(HAVE_SENDFILE)
249     return 1;
250 #else /* HAVE_SENDFILE */
251     return 0;
252 #endif /* HAVE_SENDFILE */
253 
254 }
255 
256 
257 /*
258  *                      N S E N D F I L E
259  */
260 
261 int
262 Nsendfile(int fromfd, int tofd, const char *buf, size_t count)
263 {
264     off_t offset;
265 #if defined(HAVE_SENDFILE)
266 #if defined(__FreeBSD__) || (defined(__APPLE__) && defined(__MACH__) && defined(MAC_OS_X_VERSION_10_6))
267     off_t sent;
268 #endif
269     register size_t nleft;
270     register ssize_t r;
271 
272     nleft = count;
273     while (nleft > 0) {
274 	offset = count - nleft;
275 #ifdef linux
276 	r = sendfile(tofd, fromfd, &offset, nleft);
277 #else
278 #ifdef __FreeBSD__
279 	r = sendfile(fromfd, tofd, offset, nleft, NULL, &sent, 0);
280 	if (r == 0)
281 	    r = sent;
282 #else
283 #if defined(__APPLE__) && defined(__MACH__) && defined(MAC_OS_X_VERSION_10_6)	/* OS X */
284 	sent = nleft;
285 	r = sendfile(fromfd, tofd, offset, &sent, NULL, 0);
286 	if (r == 0)
287 	    r = sent;
288 #else
289 	/* Shouldn't happen. */
290 	r = -1;
291 	errno = ENOSYS;
292 #endif
293 #endif
294 #endif
295 	if (r < 0) {
296 	    switch (errno) {
297 		case EINTR:
298 		case EAGAIN:
299 		return count - nleft;
300 
301 		case ENOBUFS:
302 		case ENOMEM:
303 		return NET_SOFTERROR;
304 
305 		default:
306 		return NET_HARDERROR;
307 	    }
308 	} else if (r == 0)
309 	    return NET_SOFTERROR;
310 	nleft -= r;
311     }
312     return count;
313 #else /* HAVE_SENDFILE */
314     errno = ENOSYS;	/* error if somehow get called without HAVE_SENDFILE */
315     return -1;
316 #endif /* HAVE_SENDFILE */
317 }
318 
319 /*************************************************************************/
320 
321 /**
322  * getsock_tcp_mss - Returns the MSS size for TCP
323  *
324  */
325 
326 int
327 getsock_tcp_mss(int inSock)
328 {
329     int             mss = 0;
330 
331     int             rc;
332     socklen_t       len;
333 
334     assert(inSock >= 0); /* print error and exit if this is not true */
335 
336     /* query for mss */
337     len = sizeof(mss);
338     rc = getsockopt(inSock, IPPROTO_TCP, TCP_MAXSEG, (char *)&mss, &len);
339     if (rc == -1) {
340 	perror("getsockopt TCP_MAXSEG");
341 	return -1;
342     }
343 
344     return mss;
345 }
346 
347 
348 
349 /*************************************************************/
350 
351 /* sets TCP_NODELAY and TCP_MAXSEG if requested */
352 // XXX: This function is not being used.
353 
354 int
355 set_tcp_options(int sock, int no_delay, int mss)
356 {
357     socklen_t len;
358     int rc;
359     int new_mss;
360 
361     if (no_delay == 1) {
362         len = sizeof(no_delay);
363         rc = setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&no_delay, len);
364         if (rc == -1) {
365             perror("setsockopt TCP_NODELAY");
366             return -1;
367         }
368     }
369 #ifdef TCP_MAXSEG
370     if (mss > 0) {
371         len = sizeof(new_mss);
372         assert(sock != -1);
373 
374         /* set */
375         new_mss = mss;
376         len = sizeof(new_mss);
377         rc = setsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, (char *)&new_mss, len);
378         if (rc == -1) {
379             perror("setsockopt TCP_MAXSEG");
380             return -1;
381         }
382         /* verify results */
383         rc = getsockopt(sock, IPPROTO_TCP, TCP_MAXSEG, (char *)&new_mss, &len);
384         if (rc == -1) {
385             perror("getsockopt TCP_MAXSEG");
386             return -1;
387         }
388         if (new_mss != mss) {
389             perror("setsockopt value mismatch");
390             return -1;
391         }
392     }
393 #endif
394     return 0;
395 }
396 
397 /****************************************************************************/
398 
399 int
400 setnonblocking(int fd, int nonblocking)
401 {
402     int flags, newflags;
403 
404     flags = fcntl(fd, F_GETFL, 0);
405     if (flags < 0) {
406         perror("fcntl(F_GETFL)");
407         return -1;
408     }
409     if (nonblocking)
410 	newflags = flags | (int) O_NONBLOCK;
411     else
412 	newflags = flags & ~((int) O_NONBLOCK);
413     if (newflags != flags)
414 	if (fcntl(fd, F_SETFL, newflags) < 0) {
415 	    perror("fcntl(F_SETFL)");
416 	    return -1;
417 	}
418     return 0;
419 }
420 
421 /****************************************************************************/
422 
423 int
424 getsockdomain(int sock)
425 {
426     struct sockaddr sa;
427     socklen_t len = sizeof(sa);
428 
429     if (getsockname(sock, &sa, &len) < 0)
430 	return -1;
431     return sa.sa_family;
432 }
433