1/* Establishing and handling network connections.
2   Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3   2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software
4   Foundation, Inc.
5
6This file is part of GNU Wget.
7
8GNU Wget is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13GNU Wget is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with Wget.  If not, see <http://www.gnu.org/licenses/>.
20
21Additional permission under GNU GPL version 3 section 7
22
23If you modify this program, or any covered work, by linking or
24combining it with the OpenSSL project's OpenSSL library (or a
25modified version of that library), containing parts covered by the
26terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
27grants you additional permission to convey the resulting work.
28Corresponding Source for a non-source form of such a combination
29shall include the source code for the parts of OpenSSL used as well
30as that of the covered work.  */
31
32#include "wget.h"
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <unistd.h>
37#include <assert.h>
38
39#include <sys/socket.h>
40#include <sys/select.h>
41
42#ifndef WINDOWS
43# ifdef __VMS
44#  include "vms_ip.h"
45# else /* def __VMS */
46#  include <netdb.h>
47# endif /* def __VMS [else] */
48# include <netinet/in.h>
49# ifndef __BEOS__
50#  include <arpa/inet.h>
51# endif
52#endif /* not WINDOWS */
53
54#include <errno.h>
55#include <string.h>
56#include <sys/time.h>
57#include "utils.h"
58#include "host.h"
59#include "connect.h"
60#include "hash.h"
61
62#include <stdint.h>
63
64/* Define sockaddr_storage where unavailable (presumably on IPv4-only
65   hosts).  */
66
67#ifndef ENABLE_IPV6
68# ifndef HAVE_STRUCT_SOCKADDR_STORAGE
69#  define sockaddr_storage sockaddr_in
70# endif
71#endif /* ENABLE_IPV6 */
72
73/* Fill SA as per the data in IP and PORT.  SA shoult point to struct
74   sockaddr_storage if ENABLE_IPV6 is defined, to struct sockaddr_in
75   otherwise.  */
76
77static void
78sockaddr_set_data (struct sockaddr *sa, const ip_address *ip, int port)
79{
80  switch (ip->family)
81    {
82    case AF_INET:
83      {
84        struct sockaddr_in *sin = (struct sockaddr_in *)sa;
85        xzero (*sin);
86        sin->sin_family = AF_INET;
87        sin->sin_port = htons (port);
88        sin->sin_addr = ip->data.d4;
89        break;
90      }
91#ifdef ENABLE_IPV6
92    case AF_INET6:
93      {
94        struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
95        xzero (*sin6);
96        sin6->sin6_family = AF_INET6;
97        sin6->sin6_port = htons (port);
98        sin6->sin6_addr = ip->data.d6;
99#ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
100        sin6->sin6_scope_id = ip->ipv6_scope;
101#endif
102        break;
103      }
104#endif /* ENABLE_IPV6 */
105    default:
106      abort ();
107    }
108}
109
110/* Get the data of SA, specifically the IP address and the port.  If
111   you're not interested in one or the other information, pass NULL as
112   the pointer.  */
113
114static void
115sockaddr_get_data (const struct sockaddr *sa, ip_address *ip, int *port)
116{
117  switch (sa->sa_family)
118    {
119    case AF_INET:
120      {
121        struct sockaddr_in *sin = (struct sockaddr_in *)sa;
122        if (ip)
123          {
124            ip->family = AF_INET;
125            ip->data.d4 = sin->sin_addr;
126          }
127        if (port)
128          *port = ntohs (sin->sin_port);
129        break;
130      }
131#ifdef ENABLE_IPV6
132    case AF_INET6:
133      {
134        struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
135        if (ip)
136          {
137            ip->family = AF_INET6;
138            ip->data.d6 = sin6->sin6_addr;
139#ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
140            ip->ipv6_scope = sin6->sin6_scope_id;
141#endif
142          }
143        if (port)
144          *port = ntohs (sin6->sin6_port);
145        break;
146      }
147#endif
148    default:
149      abort ();
150    }
151}
152
153/* Return the size of the sockaddr structure depending on its
154   family.  */
155
156static socklen_t
157sockaddr_size (const struct sockaddr *sa)
158{
159  switch (sa->sa_family)
160    {
161    case AF_INET:
162      return sizeof (struct sockaddr_in);
163#ifdef ENABLE_IPV6
164    case AF_INET6:
165      return sizeof (struct sockaddr_in6);
166#endif
167    default:
168      abort ();
169    }
170}
171
172/* Resolve the bind address specified via --bind-address and store it
173   to SA.  The resolved value is stored in a static variable and
174   reused after the first invocation of this function.
175
176   Returns true on success, false on failure.  */
177
178static bool
179resolve_bind_address (struct sockaddr *sa)
180{
181  struct address_list *al;
182
183  /* Make sure this is called only once.  opt.bind_address doesn't
184     change during a Wget run.  */
185  static bool called, should_bind;
186  static ip_address ip;
187  if (called)
188    {
189      if (should_bind)
190        sockaddr_set_data (sa, &ip, 0);
191      return should_bind;
192    }
193  called = true;
194
195  al = lookup_host (opt.bind_address, LH_BIND | LH_SILENT);
196  if (!al)
197    {
198      /* #### We should be able to print the error message here. */
199      logprintf (LOG_NOTQUIET,
200                 _("%s: unable to resolve bind address %s; disabling bind.\n"),
201                 exec_name, quote (opt.bind_address));
202      should_bind = false;
203      return false;
204    }
205
206  /* Pick the first address in the list and use it as bind address.
207     Perhaps we should try multiple addresses in succession, but I
208     don't think that's necessary in practice.  */
209  ip = *address_list_address_at (al, 0);
210  address_list_release (al);
211
212  sockaddr_set_data (sa, &ip, 0);
213  should_bind = true;
214  return true;
215}
216
217struct cwt_context {
218  int fd;
219  const struct sockaddr *addr;
220  socklen_t addrlen;
221  int result;
222};
223
224static void
225connect_with_timeout_callback (void *arg)
226{
227  struct cwt_context *ctx = (struct cwt_context *)arg;
228  ctx->result = connect (ctx->fd, ctx->addr, ctx->addrlen);
229}
230
231/* Like connect, but specifies a timeout.  If connecting takes longer
232   than TIMEOUT seconds, -1 is returned and errno is set to
233   ETIMEDOUT.  */
234
235static int
236connect_with_timeout (int fd, const struct sockaddr *addr, socklen_t addrlen,
237                      double timeout)
238{
239  struct cwt_context ctx;
240  ctx.fd = fd;
241  ctx.addr = addr;
242  ctx.addrlen = addrlen;
243
244  if (run_with_timeout (timeout, connect_with_timeout_callback, &ctx))
245    {
246      errno = ETIMEDOUT;
247      return -1;
248    }
249  if (ctx.result == -1 && errno == EINTR)
250    errno = ETIMEDOUT;
251  return ctx.result;
252}
253
254/* Connect via TCP to the specified address and port.
255
256   If PRINT is non-NULL, it is the host name to print that we're
257   connecting to.  */
258
259int
260connect_to_ip (const ip_address *ip, int port, const char *print)
261{
262  struct sockaddr_storage ss;
263  struct sockaddr *sa = (struct sockaddr *)&ss;
264  int sock;
265
266  /* If PRINT is non-NULL, print the "Connecting to..." line, with
267     PRINT being the host name we're connecting to.  */
268  if (print)
269    {
270      const char *txt_addr = print_address (ip);
271      if (0 != strcmp (print, txt_addr))
272        {
273          char *str = NULL, *name;
274
275          if (opt.enable_iri && (name = idn_decode ((char *) print)) != NULL)
276            {
277              int len = strlen (print) + strlen (name) + 4;
278              str = xmalloc (len);
279              snprintf (str, len, "%s (%s)", name, print);
280              str[len-1] = '\0';
281              xfree (name);
282            }
283
284          logprintf (LOG_VERBOSE, _("Connecting to %s|%s|:%d... "),
285                     str ? str : escnonprint_uri (print), txt_addr, port);
286
287          if (str)
288              xfree (str);
289        }
290      else
291        {
292           if (ip->family == AF_INET)
293               logprintf (LOG_VERBOSE, _("Connecting to %s:%d... "), txt_addr, port);
294#ifdef ENABLE_IPV6
295           else if (ip->family == AF_INET6)
296               logprintf (LOG_VERBOSE, _("Connecting to [%s]:%d... "), txt_addr, port);
297#endif
298        }
299    }
300
301  /* Store the sockaddr info to SA.  */
302  sockaddr_set_data (sa, ip, port);
303
304  /* Create the socket of the family appropriate for the address.  */
305  sock = socket (sa->sa_family, SOCK_STREAM, 0);
306  if (sock < 0)
307    goto err;
308
309#if defined(ENABLE_IPV6) && defined(IPV6_V6ONLY)
310  if (opt.ipv6_only) {
311    int on = 1;
312    /* In case of error, we will go on anyway... */
313    int err = setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on));
314    IF_DEBUG
315      if (err < 0)
316        DEBUGP (("Failed setting IPV6_V6ONLY: %s", strerror (errno)));
317  }
318#endif
319
320  /* For very small rate limits, set the buffer size (and hence,
321     hopefully, the kernel's TCP window size) to the per-second limit.
322     That way we should never have to sleep for more than 1s between
323     network reads.  */
324  if (opt.limit_rate && opt.limit_rate < 8192)
325    {
326      int bufsize = opt.limit_rate;
327      if (bufsize < 512)
328        bufsize = 512;          /* avoid pathologically small values */
329#ifdef SO_RCVBUF
330      setsockopt (sock, SOL_SOCKET, SO_RCVBUF,
331                  (void *)&bufsize, (socklen_t)sizeof (bufsize));
332#endif
333      /* When we add limit_rate support for writing, which is useful
334         for POST, we should also set SO_SNDBUF here.  */
335    }
336
337  if (opt.bind_address)
338    {
339      /* Bind the client side of the socket to the requested
340         address.  */
341      struct sockaddr_storage bind_ss;
342      struct sockaddr *bind_sa = (struct sockaddr *)&bind_ss;
343      if (resolve_bind_address (bind_sa))
344        {
345          if (bind (sock, bind_sa, sockaddr_size (bind_sa)) < 0)
346            goto err;
347        }
348    }
349
350  /* Connect the socket to the remote endpoint.  */
351  if (connect_with_timeout (sock, sa, sockaddr_size (sa),
352                            opt.connect_timeout) < 0)
353    goto err;
354
355  /* Success. */
356  assert (sock >= 0);
357  if (print)
358    logprintf (LOG_VERBOSE, _("connected.\n"));
359  DEBUGP (("Created socket %d.\n", sock));
360  return sock;
361
362 err:
363  {
364    /* Protect errno from possible modifications by close and
365       logprintf.  */
366    int save_errno = errno;
367    if (sock >= 0)
368      fd_close (sock);
369    if (print)
370      logprintf (LOG_VERBOSE, _("failed: %s.\n"), strerror (errno));
371    errno = save_errno;
372    return -1;
373  }
374}
375
376/* Connect via TCP to a remote host on the specified port.
377
378   HOST is resolved as an Internet host name.  If HOST resolves to
379   more than one IP address, they are tried in the order returned by
380   DNS until connecting to one of them succeeds.  */
381
382int
383connect_to_host (const char *host, int port)
384{
385  int i, start, end;
386  int sock;
387
388  struct address_list *al = lookup_host (host, 0);
389
390 retry:
391  if (!al)
392    {
393      logprintf (LOG_NOTQUIET,
394                 _("%s: unable to resolve host address %s\n"),
395                 exec_name, quote (host));
396      return E_HOST;
397    }
398
399  address_list_get_bounds (al, &start, &end);
400  for (i = start; i < end; i++)
401    {
402      const ip_address *ip = address_list_address_at (al, i);
403      sock = connect_to_ip (ip, port, host);
404      if (sock >= 0)
405        {
406          /* Success. */
407          address_list_set_connected (al);
408          address_list_release (al);
409          return sock;
410        }
411
412      /* The attempt to connect has failed.  Continue with the loop
413         and try next address. */
414
415      address_list_set_faulty (al, i);
416    }
417
418  /* Failed to connect to any of the addresses in AL. */
419
420  if (address_list_connected_p (al))
421    {
422      /* We connected to AL before, but cannot do so now.  That might
423         indicate that our DNS cache entry for HOST has expired.  */
424      address_list_release (al);
425      al = lookup_host (host, LH_REFRESH);
426      goto retry;
427    }
428  address_list_release (al);
429
430  return -1;
431}
432
433/* Create a socket, bind it to local interface BIND_ADDRESS on port
434   *PORT, set up a listen backlog, and return the resulting socket, or
435   -1 in case of error.
436
437   BIND_ADDRESS is the address of the interface to bind to.  If it is
438   NULL, the socket is bound to the default address.  PORT should
439   point to the port number that will be used for the binding.  If
440   that number is 0, the system will choose a suitable port, and the
441   chosen value will be written to *PORT.
442
443   Calling accept() on such a socket waits for and accepts incoming
444   TCP connections.  */
445
446int
447bind_local (const ip_address *bind_address, int *port)
448{
449  int sock;
450  struct sockaddr_storage ss;
451  struct sockaddr *sa = (struct sockaddr *)&ss;
452
453  /* For setting options with setsockopt. */
454  int setopt_val = 1;
455  void *setopt_ptr = (void *)&setopt_val;
456  socklen_t setopt_size = sizeof (setopt_val);
457
458  sock = socket (bind_address->family, SOCK_STREAM, 0);
459  if (sock < 0)
460    return -1;
461
462#ifdef SO_REUSEADDR
463  setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, setopt_ptr, setopt_size);
464#endif
465
466  xzero (ss);
467  sockaddr_set_data (sa, bind_address, *port);
468  if (bind (sock, sa, sockaddr_size (sa)) < 0)
469    {
470      fd_close (sock);
471      return -1;
472    }
473  DEBUGP (("Local socket fd %d bound.\n", sock));
474
475  /* If *PORT is 0, find out which port we've bound to.  */
476  if (*port == 0)
477    {
478      socklen_t addrlen = sockaddr_size (sa);
479      if (getsockname (sock, sa, &addrlen) < 0)
480        {
481          /* If we can't find out the socket's local address ("name"),
482             something is seriously wrong with the socket, and it's
483             unusable for us anyway because we must know the chosen
484             port.  */
485          fd_close (sock);
486          return -1;
487        }
488      sockaddr_get_data (sa, NULL, port);
489      DEBUGP (("binding to address %s using port %i.\n",
490               print_address (bind_address), *port));
491    }
492  if (listen (sock, 1) < 0)
493    {
494      fd_close (sock);
495      return -1;
496    }
497  return sock;
498}
499
500/* Like a call to accept(), but with the added check for timeout.
501
502   In other words, accept a client connection on LOCAL_SOCK, and
503   return the new socket used for communication with the client.
504   LOCAL_SOCK should have been bound, e.g. using bind_local().
505
506   The caller is blocked until a connection is established.  If no
507   connection is established for opt.connect_timeout seconds, the
508   function exits with an error status.  */
509
510int
511accept_connection (int local_sock)
512{
513  int sock;
514
515  /* We don't need the values provided by accept, but accept
516     apparently requires them to be present.  */
517  struct sockaddr_storage ss;
518  struct sockaddr *sa = (struct sockaddr *)&ss;
519  socklen_t addrlen = sizeof (ss);
520
521  if (opt.connect_timeout)
522    {
523      int test = select_fd (local_sock, opt.connect_timeout, WAIT_FOR_READ);
524      if (test == 0)
525        errno = ETIMEDOUT;
526      if (test <= 0)
527        return -1;
528    }
529  sock = accept (local_sock, sa, &addrlen);
530  DEBUGP (("Accepted client at socket %d.\n", sock));
531  return sock;
532}
533
534/* Get the IP address associated with the connection on FD and store
535   it to IP.  Return true on success, false otherwise.
536
537   If ENDPOINT is ENDPOINT_LOCAL, it returns the address of the local
538   (client) side of the socket.  Else if ENDPOINT is ENDPOINT_PEER, it
539   returns the address of the remote (peer's) side of the socket.  */
540
541bool
542socket_ip_address (int sock, ip_address *ip, int endpoint)
543{
544  struct sockaddr_storage storage;
545  struct sockaddr *sockaddr = (struct sockaddr *) &storage;
546  socklen_t addrlen = sizeof (storage);
547  int ret;
548
549  memset (sockaddr, 0, addrlen);
550  if (endpoint == ENDPOINT_LOCAL)
551    ret = getsockname (sock, sockaddr, &addrlen);
552  else if (endpoint == ENDPOINT_PEER)
553    ret = getpeername (sock, sockaddr, &addrlen);
554  else
555    abort ();
556  if (ret < 0)
557    return false;
558
559  memset(ip, 0, sizeof(ip_address));
560  ip->family = sockaddr->sa_family;
561  switch (sockaddr->sa_family)
562    {
563#ifdef ENABLE_IPV6
564    case AF_INET6:
565      {
566        struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&storage;
567        ip->data.d6 = sa6->sin6_addr;
568#ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
569        ip->ipv6_scope = sa6->sin6_scope_id;
570#endif
571        DEBUGP (("conaddr is: %s\n", print_address (ip)));
572        return true;
573      }
574#endif
575    case AF_INET:
576      {
577        struct sockaddr_in *sa = (struct sockaddr_in *)&storage;
578        ip->data.d4 = sa->sin_addr;
579        DEBUGP (("conaddr is: %s\n", print_address (ip)));
580        return true;
581      }
582    default:
583      abort ();
584    }
585}
586
587/* Get the socket family of connection on FD and store
588   Return family type on success, -1 otherwise.
589
590   If ENDPOINT is ENDPOINT_LOCAL, it returns the sock family of the local
591   (client) side of the socket.  Else if ENDPOINT is ENDPOINT_PEER, it
592   returns the sock family of the remote (peer's) side of the socket.  */
593
594int
595socket_family (int sock, int endpoint)
596{
597  struct sockaddr_storage storage;
598  struct sockaddr *sockaddr = (struct sockaddr *) &storage;
599  socklen_t addrlen = sizeof (storage);
600  int ret;
601
602  memset (sockaddr, 0, addrlen);
603
604  if (endpoint == ENDPOINT_LOCAL)
605    ret = getsockname (sock, sockaddr, &addrlen);
606  else if (endpoint == ENDPOINT_PEER)
607    ret = getpeername (sock, sockaddr, &addrlen);
608  else
609    abort ();
610
611  if (ret < 0)
612    return -1;
613
614  return sockaddr->sa_family;
615}
616
617/* Return true if the error from the connect code can be considered
618   retryable.  Wget normally retries after errors, but the exception
619   are the "unsupported protocol" type errors (possible on IPv4/IPv6
620   dual family systems) and "connection refused".  */
621
622bool
623retryable_socket_connect_error (int err)
624{
625  /* Have to guard against some of these values not being defined.
626     Cannot use a switch statement because some of the values might be
627     equal.  */
628  if (false
629#ifdef EAFNOSUPPORT
630      || err == EAFNOSUPPORT
631#endif
632#ifdef EPFNOSUPPORT
633      || err == EPFNOSUPPORT
634#endif
635#ifdef ESOCKTNOSUPPORT          /* no, "sockt" is not a typo! */
636      || err == ESOCKTNOSUPPORT
637#endif
638#ifdef EPROTONOSUPPORT
639      || err == EPROTONOSUPPORT
640#endif
641#ifdef ENOPROTOOPT
642      || err == ENOPROTOOPT
643#endif
644      /* Apparently, older versions of Linux and BSD used EINVAL
645         instead of EAFNOSUPPORT and such.  */
646      || err == EINVAL
647      )
648    return false;
649
650  if (!opt.retry_connrefused)
651    if (err == ECONNREFUSED
652#ifdef ENETUNREACH
653        || err == ENETUNREACH   /* network is unreachable */
654#endif
655#ifdef EHOSTUNREACH
656        || err == EHOSTUNREACH  /* host is unreachable */
657#endif
658        )
659      return false;
660
661  return true;
662}
663
664/* Wait for a single descriptor to become available, timing out after
665   MAXTIME seconds.  Returns 1 if FD is available, 0 for timeout and
666   -1 for error.  The argument WAIT_FOR can be a combination of
667   WAIT_FOR_READ and WAIT_FOR_WRITE.
668
669   This is a mere convenience wrapper around the select call, and
670   should be taken as such (for example, it doesn't implement Wget's
671   0-timeout-means-no-timeout semantics.)  */
672
673int
674select_fd (int fd, double maxtime, int wait_for)
675{
676  fd_set fdset;
677  fd_set *rd = NULL, *wr = NULL;
678  struct timeval tmout;
679  int result;
680
681  FD_ZERO (&fdset);
682  FD_SET (fd, &fdset);
683  if (wait_for & WAIT_FOR_READ)
684    rd = &fdset;
685  if (wait_for & WAIT_FOR_WRITE)
686    wr = &fdset;
687
688  tmout.tv_sec = (long) maxtime;
689  tmout.tv_usec = 1000000 * (maxtime - (long) maxtime);
690
691  do
692  {
693    result = select (fd + 1, rd, wr, NULL, &tmout);
694#ifdef WINDOWS
695    /* gnulib select() converts blocking sockets to nonblocking in windows.
696       wget uses blocking sockets so we must convert them back to blocking.  */
697    set_windows_fd_as_blocking_socket (fd);
698#endif
699  }
700  while (result < 0 && errno == EINTR);
701
702  return result;
703}
704
705/* Return true iff the connection to the remote site established
706   through SOCK is still open.
707
708   Specifically, this function returns true if SOCK is not ready for
709   reading.  This is because, when the connection closes, the socket
710   is ready for reading because EOF is about to be delivered.  A side
711   effect of this method is that sockets that have pending data are
712   considered non-open.  This is actually a good thing for callers of
713   this function, where such pending data can only be unwanted
714   leftover from a previous request.  */
715
716bool
717test_socket_open (int sock)
718{
719  fd_set check_set;
720  struct timeval to;
721  int ret = 0;
722
723  /* Check if we still have a valid (non-EOF) connection.  From Andrew
724   * Maholski's code in the Unix Socket FAQ.  */
725
726  FD_ZERO (&check_set);
727  FD_SET (sock, &check_set);
728
729  /* Wait one microsecond */
730  to.tv_sec = 0;
731  to.tv_usec = 1;
732
733  ret = select (sock + 1, &check_set, NULL, NULL, &to);
734#ifdef WINDOWS
735/* gnulib select() converts blocking sockets to nonblocking in windows.
736wget uses blocking sockets so we must convert them back to blocking
737*/
738  set_windows_fd_as_blocking_socket ( sock );
739#endif
740
741  if ( !ret )
742    /* We got a timeout, it means we're still connected. */
743    return true;
744  else
745    /* Read now would not wait, it means we have either pending data
746       or EOF/error. */
747    return false;
748}
749
750/* Basic socket operations, mostly EINTR wrappers.  */
751
752static int
753sock_read (int fd, char *buf, int bufsize)
754{
755  int res;
756  do
757    res = read (fd, buf, bufsize);
758  while (res == -1 && errno == EINTR);
759  return res;
760}
761
762static int
763sock_write (int fd, char *buf, int bufsize)
764{
765  int res;
766  do
767    res = write (fd, buf, bufsize);
768  while (res == -1 && errno == EINTR);
769  return res;
770}
771
772static int
773sock_poll (int fd, double timeout, int wait_for)
774{
775  return select_fd (fd, timeout, wait_for);
776}
777
778static int
779sock_peek (int fd, char *buf, int bufsize)
780{
781  int res;
782  do
783    res = recv (fd, buf, bufsize, MSG_PEEK);
784  while (res == -1 && errno == EINTR);
785  return res;
786}
787
788static void
789sock_close (int fd)
790{
791  close (fd);
792  DEBUGP (("Closed fd %d\n", fd));
793}
794#undef read
795#undef write
796#undef close
797
798/* Reading and writing from the network.  We build around the socket
799   (file descriptor) API, but support "extended" operations for things
800   that are not mere file descriptors under the hood, such as SSL
801   sockets.
802
803   That way the user code can call fd_read(fd, ...) and we'll run read
804   or SSL_read or whatever is necessary.  */
805
806static struct hash_table *transport_map;
807static unsigned int transport_map_modified_tick;
808
809struct transport_info {
810  struct transport_implementation *imp;
811  void *ctx;
812};
813
814/* Register the transport layer operations that will be used when
815   reading, writing, and polling FD.
816
817   This should be used for transport layers like SSL that piggyback on
818   sockets.  FD should otherwise be a real socket, on which you can
819   call getpeername, etc.  */
820
821void
822fd_register_transport (int fd, struct transport_implementation *imp, void *ctx)
823{
824  struct transport_info *info;
825
826  /* The file descriptor must be non-negative to be registered.
827     Negative values are ignored by fd_close(), and -1 cannot be used as
828     hash key.  */
829  assert (fd >= 0);
830
831  info = xnew (struct transport_info);
832  info->imp = imp;
833  info->ctx = ctx;
834  if (!transport_map)
835    transport_map = hash_table_new (0, NULL, NULL);
836  hash_table_put (transport_map, (void *)(intptr_t) fd, info);
837  ++transport_map_modified_tick;
838}
839
840/* Return context of the transport registered with
841   fd_register_transport.  This assumes fd_register_transport was
842   previously called on FD.  */
843
844void *
845fd_transport_context (int fd)
846{
847  struct transport_info *info = hash_table_get (transport_map, (void *)(intptr_t) fd);
848  return info->ctx;
849}
850
851/* When fd_read/fd_write are called multiple times in a loop, they should
852   remember the INFO pointer instead of fetching it every time.  It is
853   not enough to compare FD to LAST_FD because FD might have been
854   closed and reopened.  modified_tick ensures that changes to
855   transport_map will not be unnoticed.
856
857   This is a macro because we want the static storage variables to be
858   per-function.  */
859
860#define LAZY_RETRIEVE_INFO(info) do {                                   \
861  static struct transport_info *last_info;                              \
862  static int last_fd = -1;                                              \
863  static unsigned int last_tick;                                        \
864  if (!transport_map)                                                   \
865    info = NULL;                                                        \
866  else if (last_fd == fd && last_tick == transport_map_modified_tick)   \
867    info = last_info;                                                   \
868  else                                                                  \
869    {                                                                   \
870      info = hash_table_get (transport_map, (void *)(intptr_t) fd);     \
871      last_fd = fd;                                                     \
872      last_info = info;                                                 \
873      last_tick = transport_map_modified_tick;                          \
874    }                                                                   \
875} while (0)
876
877static bool
878poll_internal (int fd, struct transport_info *info, int wf, double timeout)
879{
880  if (timeout == -1)
881    timeout = opt.read_timeout;
882  if (timeout)
883    {
884      int test;
885      if (info && info->imp->poller)
886        test = info->imp->poller (fd, timeout, wf, info->ctx);
887      else
888        test = sock_poll (fd, timeout, wf);
889      if (test == 0)
890        errno = ETIMEDOUT;
891      if (test <= 0)
892        return false;
893    }
894  return true;
895}
896
897/* Read no more than BUFSIZE bytes of data from FD, storing them to
898   BUF.  If TIMEOUT is non-zero, the operation aborts if no data is
899   received after that many seconds.  If TIMEOUT is -1, the value of
900   opt.timeout is used for TIMEOUT.  */
901
902int
903fd_read (int fd, char *buf, int bufsize, double timeout)
904{
905  struct transport_info *info;
906  LAZY_RETRIEVE_INFO (info);
907  if (!poll_internal (fd, info, WAIT_FOR_READ, timeout))
908    return -1;
909  if (info && info->imp->reader)
910    return info->imp->reader (fd, buf, bufsize, info->ctx);
911  else
912    return sock_read (fd, buf, bufsize);
913}
914
915/* Like fd_read, except it provides a "preview" of the data that will
916   be read by subsequent calls to fd_read.  Specifically, it copies no
917   more than BUFSIZE bytes of the currently available data to BUF and
918   returns the number of bytes copied.  Return values and timeout
919   semantics are the same as those of fd_read.
920
921   CAVEAT: Do not assume that the first subsequent call to fd_read
922   will retrieve the same amount of data.  Reading can return more or
923   less data, depending on the TCP implementation and other
924   circumstances.  However, barring an error, it can be expected that
925   all the peeked data will eventually be read by fd_read.  */
926
927int
928fd_peek (int fd, char *buf, int bufsize, double timeout)
929{
930  struct transport_info *info;
931  LAZY_RETRIEVE_INFO (info);
932  if (!poll_internal (fd, info, WAIT_FOR_READ, timeout))
933    return -1;
934  if (info && info->imp->peeker)
935    return info->imp->peeker (fd, buf, bufsize, info->ctx);
936  else
937    return sock_peek (fd, buf, bufsize);
938}
939
940/* Write the entire contents of BUF to FD.  If TIMEOUT is non-zero,
941   the operation aborts if no data is received after that many
942   seconds.  If TIMEOUT is -1, the value of opt.timeout is used for
943   TIMEOUT.  */
944
945int
946fd_write (int fd, char *buf, int bufsize, double timeout)
947{
948  int res;
949  struct transport_info *info;
950  LAZY_RETRIEVE_INFO (info);
951
952  /* `write' may write less than LEN bytes, thus the loop keeps trying
953     it until all was written, or an error occurred.  */
954  res = 0;
955  while (bufsize > 0)
956    {
957      if (!poll_internal (fd, info, WAIT_FOR_WRITE, timeout))
958        return -1;
959      if (info && info->imp->writer)
960        res = info->imp->writer (fd, buf, bufsize, info->ctx);
961      else
962        res = sock_write (fd, buf, bufsize);
963      if (res <= 0)
964        break;
965      buf += res;
966      bufsize -= res;
967    }
968  return res;
969}
970
971/* Report the most recent error(s) on FD.  This should only be called
972   after fd_* functions, such as fd_read and fd_write, and only if
973   they return a negative result.  For errors coming from other calls
974   such as setsockopt or fopen, strerror should continue to be
975   used.
976
977   If the transport doesn't support error messages or doesn't supply
978   one, strerror(errno) is returned.  The returned error message
979   should not be used after fd_close has been called.  */
980
981const char *
982fd_errstr (int fd)
983{
984  /* Don't bother with LAZY_RETRIEVE_INFO, as this will only be called
985     in case of error, never in a tight loop.  */
986  struct transport_info *info = NULL;
987  if (transport_map)
988    info = hash_table_get (transport_map, (void *)(intptr_t) fd);
989
990  if (info && info->imp->errstr)
991    {
992      const char *err = info->imp->errstr (fd, info->ctx);
993      if (err)
994        return err;
995      /* else, fall through and print the system error. */
996    }
997  return strerror (errno);
998}
999
1000/* Close the file descriptor FD.  */
1001
1002void
1003fd_close (int fd)
1004{
1005  struct transport_info *info;
1006  if (fd < 0)
1007    return;
1008
1009  /* Don't use LAZY_RETRIEVE_INFO because fd_close() is only called once
1010     per socket, so that particular optimization wouldn't work.  */
1011  info = NULL;
1012  if (transport_map)
1013    info = hash_table_get (transport_map, (void *)(intptr_t) fd);
1014
1015  if (info && info->imp->closer)
1016    info->imp->closer (fd, info->ctx);
1017  else
1018    sock_close (fd);
1019
1020  if (info)
1021    {
1022      hash_table_remove (transport_map, (void *)(intptr_t) fd);
1023      xfree (info);
1024      ++transport_map_modified_tick;
1025    }
1026}
1027