1 /* Establishing and handling network connections.
2 Copyright (C) 1995, 1996, 1997, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Wget.
6 GNU Wget is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 GNU Wget is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Wget; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 In addition, as a special exception, the Free Software Foundation
21 gives permission to link the code of its release of Wget with the
22 OpenSSL project's "OpenSSL" library (or with modified versions of it
23 that use the same license as the "OpenSSL" library), and distribute
24 the linked executables. You must obey the GNU General Public License
25 in all respects for all of the code used other than "OpenSSL". If you
26 modify this file, you may extend this exception to your version of the
27 file, but you are not obligated to do so. If you do not wish to do
28 so, delete this exception statement from your version. */
34 #include <sys/types.h>
41 # include <sys/socket.h>
43 # include <netinet/in.h>
45 # include <arpa/inet.h>
47 #endif /* not WINDOWS */
54 #endif /* HAVE_STRING_H */
55 #ifdef HAVE_SYS_SELECT_H
56 # include <sys/select.h>
57 #endif /* HAVE_SYS_SELECT_H */
69 /* Fill SA as per the data in IP and PORT. SA shoult point to struct
70 sockaddr_storage if ENABLE_IPV6 is defined, to struct sockaddr_in
74 sockaddr_set_data (struct sockaddr *sa, const ip_address *ip, int port)
80 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
81 sin->sin_family = AF_INET;
82 sin->sin_port = htons (port);
83 sin->sin_addr = ADDRESS_IPV4_IN_ADDR (ip);
89 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
90 sin6->sin6_family = AF_INET6;
91 sin6->sin6_port = htons (port);
92 sin6->sin6_addr = ADDRESS_IPV6_IN6_ADDR (ip);
93 #ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
94 sin6->sin6_scope_id = ADDRESS_IPV6_SCOPE (ip);
98 #endif /* ENABLE_IPV6 */
104 /* Get the data of SA, specifically the IP address and the port. If
105 you're not interested in one or the other information, pass NULL as
109 sockaddr_get_data (const struct sockaddr *sa, ip_address *ip, int *port)
111 switch (sa->sa_family)
115 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
118 ip->type = IPV4_ADDRESS;
119 ADDRESS_IPV4_IN_ADDR (ip) = sin->sin_addr;
122 *port = ntohs (sin->sin_port);
128 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
131 ip->type = IPV6_ADDRESS;
132 ADDRESS_IPV6_IN6_ADDR (ip) = sin6->sin6_addr;
133 #ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
134 ADDRESS_IPV6_SCOPE (ip) = sin6->sin6_scope_id;
138 *port = ntohs (sin6->sin6_port);
147 /* Return the size of the sockaddr structure depending on its
151 sockaddr_size (const struct sockaddr *sa)
153 switch (sa->sa_family)
156 return sizeof (struct sockaddr_in);
159 return sizeof (struct sockaddr_in6);
163 return 0; /* so the compiler shuts up. */
168 resolve_bind_address (const char *host, struct sockaddr *sa, int flags)
170 struct address_list *al;
172 /* #### Shouldn't we do this only once? opt.bind_address won't
173 change during a Wget run! */
175 al = lookup_host (host, flags | LH_SILENT | LH_PASSIVE);
178 logprintf (LOG_NOTQUIET,
179 _("Unable to convert `%s' to a bind address. Reverting to ANY.\n"),
184 /* Pick the first address in the list and use it as bind address.
185 Perhaps we should try multiple addresses, but I don't think
186 that's necessary in practice. */
187 sockaddr_set_data (sa, address_list_address_at (al, 0), 0);
188 address_list_release (al);
194 const struct sockaddr *addr;
200 connect_with_timeout_callback (void *arg)
202 struct cwt_context *ctx = (struct cwt_context *)arg;
203 ctx->result = connect (ctx->fd, ctx->addr, ctx->addrlen);
206 /* Like connect, but specifies a timeout. If connecting takes longer
207 than TIMEOUT seconds, -1 is returned and errno is set to
211 connect_with_timeout (int fd, const struct sockaddr *addr, socklen_t addrlen,
214 struct cwt_context ctx;
217 ctx.addrlen = addrlen;
219 if (run_with_timeout (timeout, connect_with_timeout_callback, &ctx))
224 if (ctx.result == -1 && errno == EINTR)
229 /* Connect to a remote endpoint whose IP address is known. */
232 connect_to_ip (const ip_address *ip, int port, const char *print)
234 struct sockaddr_storage ss;
235 struct sockaddr *sa = (struct sockaddr *)&ss;
236 int sock, save_errno;
238 /* If PRINT is non-NULL, print the "Connecting to..." line, with
239 PRINT being the host name we're connecting to. */
242 const char *txt_addr = pretty_print_address (ip);
243 if (print && 0 != strcmp (print, txt_addr))
244 logprintf (LOG_VERBOSE,
245 _("Connecting to %s{%s}:%d... "), print, txt_addr, port);
247 logprintf (LOG_VERBOSE, _("Connecting to %s:%d... "), txt_addr, port);
250 /* Store the sockaddr info to SA. */
251 sockaddr_set_data (sa, ip, port);
253 /* Create the socket of the family appropriate for the address. */
254 sock = socket (sa->sa_family, SOCK_STREAM, 0);
258 /* For very small rate limits, set the buffer size (and hence,
259 hopefully, the kernel's TCP window size) to the per-second limit.
260 That way we should never have to sleep for more than 1s between
262 if (opt.limit_rate && opt.limit_rate < 8192)
264 int bufsize = opt.limit_rate;
266 bufsize = 512; /* avoid pathologically small values */
268 setsockopt (sock, SOL_SOCKET, SO_RCVBUF,
269 (void *)&bufsize, (socklen_t)sizeof (bufsize));
271 /* When we add limit_rate support for writing, which is useful
272 for POST, we should also set SO_SNDBUF here. */
275 if (opt.bind_address)
277 /* Bind the client side of the socket to the requested
279 struct sockaddr_storage bind_ss;
280 struct sockaddr *bind_sa = (struct sockaddr *)&bind_ss;
281 if (resolve_bind_address (opt.bind_address, bind_sa, 0))
283 if (bind (sock, bind_sa, sockaddr_size (bind_sa)) < 0)
292 /* Connect the socket to the remote endpoint. */
293 if (connect_with_timeout (sock, sa, sockaddr_size (sa),
294 opt.connect_timeout) < 0)
306 logprintf (LOG_VERBOSE, _("connected.\n"));
307 DEBUGP (("Created socket %d.\n", sock));
313 logprintf (LOG_VERBOSE, "failed: %s.\n", strerror (errno));
320 /* Connect to a remote endpoint specified by host name. */
323 connect_to_host (const char *host, int port)
326 struct address_list *al;
330 al = lookup_host (host, 0);
334 address_list_get_bounds (al, &start, &end);
335 for (i = start; i < end; i++)
337 const ip_address *ip = address_list_address_at (al, i);
338 sock = connect_to_ip (ip, port, host);
343 address_list_set_faulty (al, i);
345 /* The attempt to connect has failed. Continue with the loop
346 and try next address. */
348 address_list_release (al);
350 if (sock < 0 && address_list_cached_p (al))
352 /* We were unable to connect to any address in a list we've
353 obtained from cache. There is a possibility that the host is
354 under dynamic DNS and has changed its address. Resolve it
356 forget_host_lookup (host);
364 test_socket_open (int sock)
370 /* Check if we still have a valid (non-EOF) connection. From Andrew
371 * Maholski's code in the Unix Socket FAQ. */
373 FD_ZERO (&check_set);
374 FD_SET (sock, &check_set);
376 /* Wait one microsecond */
380 /* If we get a timeout, then that means still connected */
381 if (select (sock + 1, &check_set, NULL, NULL, &to) == 0)
383 /* Connection is valid (not EOF), so continue */
389 /* Without select, it's hard to know for sure. */
394 /* Create a socket and bind it to PORT locally. Calling accept() on
395 such a socket waits for and accepts incoming TCP connections. The
396 resulting socket is stored to LOCAL_SOCK. */
399 bindport (const ip_address *bind_address, int *port, int *local_sock)
402 int family = AF_INET;
404 struct sockaddr_storage ss;
405 struct sockaddr *sa = (struct sockaddr *)&ss;
406 memset (&ss, 0, sizeof (ss));
409 if (bind_address->type == IPV6_ADDRESS)
413 if ((msock = socket (family, SOCK_STREAM, 0)) < 0)
418 if (setsockopt (msock, SOL_SOCKET, SO_REUSEADDR,
419 (void *)&optval, (socklen_t)sizeof (optval)) < 0)
427 # ifdef HAVE_IPV6_V6ONLY
428 if (family == AF_INET6)
431 /* if setsockopt fails, go on anyway */
432 setsockopt (msock, IPPROTO_IPV6, IPV6_V6ONLY,
433 (void *)&optval, (socklen_t)sizeof (optval));
438 sockaddr_set_data (sa, bind_address, *port);
439 if (bind (msock, sa, sockaddr_size (sa)) < 0)
444 DEBUGP (("Local socket fd %d bound.\n", msock));
447 socklen_t sa_len = sockaddr_size (sa);
448 if (getsockname (msock, sa, &sa_len) < 0)
453 sockaddr_get_data (sa, NULL, port);
454 DEBUGP (("binding to address %s using port %i.\n",
455 pretty_print_address (bind_address), *port));
457 if (listen (msock, 1) < 0)
467 /* Wait for file descriptor FD to be available, timing out after
468 MAXTIME seconds. "Available" means readable if writep is 0,
471 Returns 1 if FD is available, 0 for timeout and -1 for error. */
474 select_fd (int fd, double maxtime, int writep)
477 fd_set *rd = NULL, *wrt = NULL;
478 struct timeval tmout;
483 *(writep ? &wrt : &rd) = &fds;
485 tmout.tv_sec = (long)maxtime;
486 tmout.tv_usec = 1000000L * (maxtime - (long)maxtime);
489 result = select (fd + 1, rd, wrt, NULL, &tmout);
490 while (result < 0 && errno == EINTR);
492 /* When we've timed out, set errno to ETIMEDOUT for the convenience
499 #endif /* HAVE_SELECT */
501 /* Accept a connection on LOCAL_SOCK, and store the new socket to
502 *SOCK. It blocks the caller until a connection is established. If
503 no connection is established for opt.connect_timeout seconds, the
504 function exits with an error status. */
507 acceptport (int local_sock, int *sock)
509 struct sockaddr_storage ss;
510 struct sockaddr *sa = (struct sockaddr *)&ss;
511 socklen_t addrlen = sizeof (ss);
514 if (select_fd (local_sock, opt.connect_timeout, 0) <= 0)
517 if ((*sock = accept (local_sock, sa, &addrlen)) < 0)
519 DEBUGP (("Created socket fd %d.\n", *sock));
523 /* Return the local IP address associated with the connection on FD. */
526 conaddr (int fd, ip_address *ip)
528 struct sockaddr_storage storage;
529 struct sockaddr *sockaddr = (struct sockaddr *)&storage;
530 socklen_t addrlen = sizeof (storage);
532 if (getsockname (fd, sockaddr, &addrlen) < 0)
535 switch (sockaddr->sa_family)
540 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&storage;
541 ip->type = IPV6_ADDRESS;
542 ADDRESS_IPV6_IN6_ADDR (ip) = sa6->sin6_addr;
543 #ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
544 ADDRESS_IPV6_SCOPE (ip) = sa6->sin6_scope_id;
546 DEBUGP (("conaddr is: %s\n", pretty_print_address (ip)));
552 struct sockaddr_in *sa = (struct sockaddr_in *)&storage;
553 ip->type = IPV4_ADDRESS;
554 ADDRESS_IPV4_IN_ADDR (ip) = sa->sin_addr;
555 DEBUGP (("conaddr is: %s\n", pretty_print_address (ip)));
565 /* Read at most LEN bytes from FD, storing them to BUF. This is
566 virtually the same as read(), but takes care of EINTR braindamage
567 and uses select() to timeout the stale connections (a connection is
568 stale if more than OPT.READ_TIMEOUT time is spent in select() or
572 iread (int fd, char *buf, int len)
577 if (opt.read_timeout)
578 if (select_fd (fd, opt.read_timeout, 0) <= 0)
582 res = READ (fd, buf, len);
583 while (res == -1 && errno == EINTR);
588 /* Write LEN bytes from BUF to FD. This is similar to iread(), but
589 unlike iread(), it makes sure that all of BUF is actually written
590 to FD, so callers needn't bother with checking that the return
591 value equals to LEN. Instead, you should simply check for -1. */
594 iwrite (int fd, char *buf, int len)
598 /* `write' may write less than LEN bytes, thus the outward loop
599 keeps trying it until all was written, or an error occurred. The
600 inner loop is reserved for the usual EINTR f*kage, and the
601 innermost loop deals with the same during select(). */
605 if (opt.read_timeout)
606 if (select_fd (fd, opt.read_timeout, 1) <= 0)
610 res = WRITE (fd, buf, len);
611 while (res == -1 && errno == EINTR);