Prevent the "Connection reset by peer" messages often seen from Cygwin.
[rsync/rsync.git] / socket.c
1 /* -*- c-file-style: "linux" -*-
2    
3    rsync -- fast file replication program
4    
5    Copyright (C) 1992-2001 by Andrew Tridgell <tridge@samba.org>
6    Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 /**
24  * @file socket.c
25  * 
26  * Socket functions used in rsync.
27  *
28  * This file is now converted to use the new-style getaddrinfo()
29  * interface, which supports IPv6 but is also supported on recent
30  * IPv4-only machines.  On systems that don't have that interface, we
31  * emulate it using the KAME implementation.
32  **/
33
34 #include "rsync.h"
35
36
37 /**
38  * Establish a proxy connection on an open socket to a web proxy by
39  * using the HTTP CONNECT method.
40  **/
41 static int establish_proxy_connection(int fd, char *host, int port)
42 {
43         char buffer[1024];
44         char *cp;
45
46         snprintf(buffer, sizeof(buffer), "CONNECT %s:%d HTTP/1.0\r\n\r\n", host, port);
47         if (write(fd, buffer, strlen(buffer)) != (int) strlen(buffer)) {
48                 rprintf(FERROR, "failed to write to proxy: %s\n",
49                         strerror(errno));
50                 return -1;
51         }
52
53         for (cp = buffer; cp < &buffer[sizeof(buffer) - 1]; cp++) {
54                 if (read(fd, cp, 1) != 1) {
55                         rprintf(FERROR, "failed to read from proxy: %s\n",
56                                 strerror(errno));
57                         return -1;
58                 }
59                 if (*cp == '\n')
60                         break;
61         }
62
63         if (*cp != '\n')
64                 cp++;
65         *cp-- = '\0';
66         if (*cp == '\r')
67                 *cp = '\0';
68         if (strncmp(buffer, "HTTP/", 5) != 0) {
69                 rprintf(FERROR, "bad response from proxy - %s\n",
70                         buffer);
71                 return -1;
72         }
73         for (cp = &buffer[5]; isdigit(* (unsigned char *) cp) || (*cp == '.'); cp++)
74                 ;
75         while (*cp == ' ')
76                 cp++;
77         if (*cp != '2') {
78                 rprintf(FERROR, "bad response from proxy - %s\n",
79                         buffer);
80                 return -1;
81         }
82         /* throw away the rest of the HTTP header */
83         while (1) {
84                 for (cp = buffer; cp < &buffer[sizeof(buffer) - 1];
85                      cp++) {
86                         if (read(fd, cp, 1) != 1) {
87                                 rprintf(FERROR, "failed to read from proxy: %s\n",
88                                         strerror(errno));
89                                 return -1;
90                         }
91                         if (*cp == '\n')
92                                 break;
93                 }
94                 if ((cp > buffer) && (*cp == '\n'))
95                         cp--;
96                 if ((cp == buffer) && ((*cp == '\n') || (*cp == '\r')))
97                         break;
98         }
99         return 0;
100 }
101
102
103 /**
104  * Try to set the local address for a newly-created socket.  Return -1
105  * if this fails.
106  **/
107 int try_bind_local(int s,
108                    int ai_family, int ai_socktype,
109                    const char *bind_address)
110 {
111         int error;
112         struct addrinfo bhints, *bres_all, *r;
113
114         memset(&bhints, 0, sizeof(bhints));
115         bhints.ai_family = ai_family;
116         bhints.ai_socktype = ai_socktype;
117         bhints.ai_flags = AI_PASSIVE;
118         if ((error = getaddrinfo(bind_address, NULL, &bhints, &bres_all))) {
119                 rprintf(FERROR, RSYNC_NAME ": getaddrinfo %s: %s\n",
120                         bind_address, gai_strerror(error));
121                 return -1;
122         }
123
124         for (r = bres_all; r; r = r->ai_next) {
125                 if (bind(s, r->ai_addr, r->ai_addrlen) == -1)
126                         continue;
127                 freeaddrinfo(bres_all);
128                 return s;
129         }
130
131         /* no error message; there might be some problem that allows
132          * creation of the socket but not binding, perhaps if the
133          * machine has no ipv6 address of this name. */
134         freeaddrinfo(bres_all);
135         return -1;
136 }
137
138
139 /**
140  * Open a socket to a tcp remote host with the specified port .
141  *
142  * Based on code from Warren.  Proxy support by Stephen Rothwell.
143  * getaddrinfo() rewrite contributed by KAME.net.
144  *
145  * Now that we support IPv6 we need to look up the remote machine's
146  * address first, using @p af_hint to set a preference for the type
147  * of address.  Then depending on whether it has v4 or v6 addresses we
148  * try to open a connection.
149  *
150  * The loop allows for machines with some addresses which may not be
151  * reachable, perhaps because we can't e.g. route ipv6 to that network
152  * but we can get ip4 packets through.
153  *
154  * @param bind_address Local address to use.  Normally NULL to bind
155  * the wildcard address.
156  *
157  * @param af_hint Address family, e.g. AF_INET or AF_INET6.
158  **/
159 int open_socket_out(char *host, int port, const char *bind_address,
160                     int af_hint)
161 {
162         int type = SOCK_STREAM;
163         int error;
164         int s;
165         struct addrinfo hints, *res0, *res;
166         char portbuf[10];
167         char *h;
168         int proxied = 0;
169         char buffer[1024];
170         char *cp;
171
172         /* if we have a RSYNC_PROXY env variable then redirect our
173          * connetcion via a web proxy at the given address. The format
174          * is hostname:port */
175         h = getenv("RSYNC_PROXY");
176         proxied = (h != NULL) && (*h != '\0');
177
178         if (proxied) {
179                 strlcpy(buffer, h, sizeof(buffer));
180                 cp = strchr(buffer, ':');
181                 if (cp == NULL) {
182                         rprintf(FERROR,
183                                 "invalid proxy specification: should be HOST:PORT\n");
184                         return -1;
185                 }
186                 *cp++ = '\0';
187                 strcpy(portbuf, cp);
188                 h = buffer;
189                 if (verbose >= 2) {
190                         rprintf(FINFO, "connection via http proxy %s port %s\n",
191                                 h, portbuf);
192                 }
193         } else {
194                 snprintf(portbuf, sizeof(portbuf), "%d", port);
195                 h = host;
196         }
197
198         memset(&hints, 0, sizeof(hints));
199         hints.ai_family = af_hint;
200         hints.ai_socktype = type;
201         error = getaddrinfo(h, portbuf, &hints, &res0);
202         if (error) {
203                 rprintf(FERROR, RSYNC_NAME ": getaddrinfo: %s %s: %s\n",
204                         h, portbuf, gai_strerror(error));
205                 return -1;
206         }
207
208         s = -1;
209         /* Try to connect to all addresses for this machine until we get
210          * through.  It might e.g. be multi-homed, or have both IPv4 and IPv6
211          * addresses.  We need to create a socket for each record, since the
212          * address record tells us what protocol to use to try to connect. */
213         for (res = res0; res; res = res->ai_next) {
214                 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
215                 if (s < 0)
216                         continue;
217
218                 if (bind_address)
219                         if (try_bind_local(s, res->ai_family, type,
220                                            bind_address) == -1) {
221                                 close(s);
222                                 s = -1;
223                                 continue;
224                         }
225
226                 if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
227                         close(s);
228                         s = -1;
229                         continue;
230                 }
231                 if (proxied &&
232                     establish_proxy_connection(s, host, port) != 0) {
233                         close(s);
234                         s = -1;
235                         continue;
236                 } else
237                         break;
238         }
239         freeaddrinfo(res0);
240         if (s < 0) {
241                 rprintf(FERROR, RSYNC_NAME ": failed to connect to %s: %s\n",
242                         h, strerror(errno));
243                 return -1;
244         }
245         return s;
246 }
247
248
249 /**
250  * Open an outgoing socket, but allow for it to be intercepted by
251  * $RSYNC_CONNECT_PROG, which will execute a program across a TCP
252  * socketpair rather than really opening a socket.
253  *
254  * We use this primarily in testing to detect TCP flow bugs, but not
255  * cause security problems by really opening remote connections.
256  *
257  * This is based on the Samba LIBSMB_PROG feature.
258  *
259  * @param bind_address Local address to use.  Normally NULL to get the stack default.
260  **/
261 int open_socket_out_wrapped (char *host,
262                              int port,
263                              const char *bind_address,
264                              int af_hint)
265 {
266         char *prog;
267
268         if ((prog = getenv ("RSYNC_CONNECT_PROG")) != NULL) 
269                 return sock_exec (prog);
270         else 
271                 return open_socket_out (host, port, bind_address,
272                                         af_hint);
273 }
274
275
276
277 /**
278  * Open a socket of the specified type, port and address for incoming data
279  *
280  * Try to be better about handling the results of getaddrinfo(): when
281  * opening an inbound socket, we might get several address results,
282  * e.g. for the machine's ipv4 and ipv6 name.  
283  * 
284  * If binding a wildcard, then any one of them should do.  If an address
285  * was specified but it's insufficiently specific then that's not our
286  * fault.  
287  * 
288  * However, some of the advertized addresses may not work because e.g. we
289  * don't have IPv6 support in the kernel.  In that case go on and try all
290  * addresses until one succeeds.
291  * 
292  * @param bind_address Local address to bind, or NULL to allow it to
293  * default.
294  **/
295 static int open_socket_in(int type, int port, const char *bind_address,
296                           int af_hint)
297 {
298         int one=1;
299         int s;
300         struct addrinfo hints, *all_ai, *resp;
301         char portbuf[10];
302         int error;
303
304         memset(&hints, 0, sizeof(hints));
305         hints.ai_family = af_hint;
306         hints.ai_socktype = type;
307         hints.ai_flags = AI_PASSIVE;
308         snprintf(portbuf, sizeof(portbuf), "%d", port);
309         error = getaddrinfo(bind_address, portbuf, &hints, &all_ai);
310         if (error) {
311                 rprintf(FERROR, RSYNC_NAME ": getaddrinfo: bind address %s: %s\n",
312                         bind_address, gai_strerror(error));
313                 return -1;
314         }
315
316         /* We may not be able to create the socket, if for example the
317          * machine knows about IPv6 in the C library, but not in the
318          * kernel. */
319         for (resp = all_ai; resp; resp = resp->ai_next) {
320                 s = socket(resp->ai_family, resp->ai_socktype,
321                            resp->ai_protocol);
322
323                 if (s == -1) 
324                         /* See if there's another address that will work... */
325                         continue;
326                 
327                 setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
328                            (char *)&one, sizeof one);
329                 
330                 /* now we've got a socket - we need to bind it */
331                 if (bind(s, all_ai->ai_addr, all_ai->ai_addrlen) < 0) {
332                         /* Nope, try another */
333                         close(s);
334                         continue;
335                 }
336
337                 freeaddrinfo(all_ai);
338                 return s;
339         }
340
341         rprintf(FERROR, RSYNC_NAME ": open inbound socket on port %d failed: "
342                 "%s\n",
343                 port, 
344                 strerror(errno));
345
346         freeaddrinfo(all_ai);
347         return -1; 
348 }
349
350
351 /*
352  * Determine if a file descriptor is in fact a socket
353  */
354 int is_a_socket(int fd)
355 {
356         int v;
357         socklen_t l;
358         l = sizeof(int);
359
360         /* Parameters to getsockopt, setsockopt etc are very
361          * unstandardized across platforms, so don't be surprised if
362          * there are compiler warnings on e.g. SCO OpenSwerver or AIX.
363          * It seems they all eventually get the right idea.
364          *
365          * Debian says: ``The fifth argument of getsockopt and
366          * setsockopt is in reality an int [*] (and this is what BSD
367          * 4.* and libc4 and libc5 have).  Some POSIX confusion
368          * resulted in the present socklen_t.  The draft standard has
369          * not been adopted yet, but glibc2 already follows it and
370          * also has socklen_t [*]. See also accept(2).''
371          *
372          * We now return to your regularly scheduled programming.  */
373         return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
374 }
375
376
377 void start_accept_loop(int port, int (*fn)(int, int))
378 {
379         int s;
380         extern char *bind_address;
381         extern int default_af_hint;
382
383         /* open an incoming socket */
384         s = open_socket_in(SOCK_STREAM, port, bind_address, default_af_hint);
385         if (s == -1)
386                 exit_cleanup(RERR_SOCKETIO);
387
388         /* ready to listen */
389         if (listen(s, 5) == -1) {
390                 close(s);
391                 exit_cleanup(RERR_SOCKETIO);
392         }
393
394
395         /* now accept incoming connections - forking a new process
396            for each incoming connection */
397         while (1) {
398                 fd_set fds;
399                 pid_t pid;
400                 int fd;
401                 struct sockaddr_storage addr;
402                 socklen_t addrlen = sizeof addr;
403
404                 /* close log file before the potentially very long select so
405                    file can be trimmed by another process instead of growing
406                    forever */
407                 log_close();
408
409                 FD_ZERO(&fds);
410                 FD_SET(s, &fds);
411
412                 if (select(s+1, &fds, NULL, NULL, NULL) != 1) {
413                         continue;
414                 }
415
416                 if(!FD_ISSET(s, &fds)) continue;
417
418                 fd = accept(s,(struct sockaddr *)&addr,&addrlen);
419
420                 if (fd == -1) continue;
421
422                 signal(SIGCHLD, SIG_IGN);
423
424                 /* we shouldn't have any children left hanging around
425                    but I have had reports that on Digital Unix zombies
426                    are produced, so this ensures that they are reaped */
427 #ifdef WNOHANG
428                 while (waitpid(-1, NULL, WNOHANG) > 0);
429 #endif
430
431                 if ((pid = fork()) == 0) {
432                         int ret;
433                         close(s);
434                         /* open log file in child before possibly giving
435                            up privileges  */
436                         log_open();
437                         ret = fn(fd, fd);
438                         close_all();
439                         _exit(ret);
440                 } else if (pid < 0) {
441                         rprintf(FERROR,
442                                 RSYNC_NAME
443                                 ": could not create child server process: %s\n",
444                                 strerror(errno));
445                         close(fd);
446                         /* This might have happened because we're
447                          * overloaded.  Sleep briefly before trying to
448                          * accept again. */
449                         sleep(2);
450                 } else {
451                         /* Parent doesn't need this fd anymore. */
452                         close(fd);
453                 }
454         }
455 }
456
457
458 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
459
460 struct
461 {
462   char *name;
463   int level;
464   int option;
465   int value;
466   int opttype;
467 } socket_options[] = {
468   {"SO_KEEPALIVE",      SOL_SOCKET,    SO_KEEPALIVE,    0,                 OPT_BOOL},
469   {"SO_REUSEADDR",      SOL_SOCKET,    SO_REUSEADDR,    0,                 OPT_BOOL},
470   {"SO_BROADCAST",      SOL_SOCKET,    SO_BROADCAST,    0,                 OPT_BOOL},
471 #ifdef TCP_NODELAY
472   {"TCP_NODELAY",       IPPROTO_TCP,   TCP_NODELAY,     0,                 OPT_BOOL},
473 #endif
474 #ifdef IPTOS_LOWDELAY
475   {"IPTOS_LOWDELAY",    IPPROTO_IP,    IP_TOS,          IPTOS_LOWDELAY,    OPT_ON},
476 #endif
477 #ifdef IPTOS_THROUGHPUT
478   {"IPTOS_THROUGHPUT",  IPPROTO_IP,    IP_TOS,          IPTOS_THROUGHPUT,  OPT_ON},
479 #endif
480 #ifdef SO_SNDBUF
481   {"SO_SNDBUF",         SOL_SOCKET,    SO_SNDBUF,       0,                 OPT_INT},
482 #endif
483 #ifdef SO_RCVBUF
484   {"SO_RCVBUF",         SOL_SOCKET,    SO_RCVBUF,       0,                 OPT_INT},
485 #endif
486 #ifdef SO_SNDLOWAT
487   {"SO_SNDLOWAT",       SOL_SOCKET,    SO_SNDLOWAT,     0,                 OPT_INT},
488 #endif
489 #ifdef SO_RCVLOWAT
490   {"SO_RCVLOWAT",       SOL_SOCKET,    SO_RCVLOWAT,     0,                 OPT_INT},
491 #endif
492 #ifdef SO_SNDTIMEO
493   {"SO_SNDTIMEO",       SOL_SOCKET,    SO_SNDTIMEO,     0,                 OPT_INT},
494 #endif
495 #ifdef SO_RCVTIMEO
496   {"SO_RCVTIMEO",       SOL_SOCKET,    SO_RCVTIMEO,     0,                 OPT_INT},
497 #endif
498   {NULL,0,0,0,0}};
499
500         
501
502 /**
503  * Set user socket options
504  **/
505 void set_socket_options(int fd, char *options)
506 {
507         char *tok;
508         if (!options || !*options) return;
509
510         options = strdup(options);
511         
512         if (!options) out_of_memory("set_socket_options");
513
514         for (tok=strtok(options, " \t,"); tok; tok=strtok(NULL," \t,")) {
515                 int ret=0,i;
516                 int value = 1;
517                 char *p;
518                 int got_value = 0;
519
520                 if ((p = strchr(tok,'='))) {
521                         *p = 0;
522                         value = atoi(p+1);
523                         got_value = 1;
524                 }
525
526                 for (i=0;socket_options[i].name;i++)
527                         if (strcmp(socket_options[i].name,tok)==0)
528                                 break;
529
530                 if (!socket_options[i].name) {
531                         rprintf(FERROR,"Unknown socket option %s\n",tok);
532                         continue;
533                 }
534
535                 switch (socket_options[i].opttype) {
536                 case OPT_BOOL:
537                 case OPT_INT:
538                         ret = setsockopt(fd,socket_options[i].level,
539                                          socket_options[i].option,(char *)&value,sizeof(int));
540                         break;
541                         
542                 case OPT_ON:
543                         if (got_value)
544                                 rprintf(FERROR,"syntax error - %s does not take a value\n",tok);
545
546                         {
547                                 int on = socket_options[i].value;
548                                 ret = setsockopt(fd,socket_options[i].level,
549                                                  socket_options[i].option,(char *)&on,sizeof(int));
550                         }
551                         break;    
552                 }
553                 
554                 if (ret != 0)
555                         rprintf(FERROR, "failed to set socket option %s: %s\n", tok,
556                                 strerror(errno));
557         }
558
559         free(options);
560 }
561
562 /**
563  * Become a daemon, discarding the controlling terminal
564  **/
565 void become_daemon(void)
566 {
567         int i;
568
569         if (fork()) {
570                 _exit(0);
571         }
572
573         /* detach from the terminal */
574 #ifdef HAVE_SETSID
575         setsid();
576 #else
577 #ifdef TIOCNOTTY
578         i = open("/dev/tty", O_RDWR);
579         if (i >= 0) {
580                 ioctl(i, (int) TIOCNOTTY, (char *)0);      
581                 close(i);
582         }
583 #endif /* TIOCNOTTY */
584 #endif
585         /* make sure that stdin, stdout an stderr don't stuff things
586            up (library functions, for example) */
587         for (i=0;i<3;i++) {
588                 close(i); 
589                 open("/dev/null", O_RDWR);
590         }
591 }
592
593
594 /**
595  * This is like socketpair but uses tcp. It is used by the Samba
596  * regression test code.
597  * 
598  * The function guarantees that nobody else can attach to the socket,
599  * or if they do that this function fails and the socket gets closed
600  * returns 0 on success, -1 on failure the resulting file descriptors
601  * are symmetrical.
602  **/
603 static int socketpair_tcp(int fd[2])
604 {
605         int listener;
606         struct sockaddr_in sock;
607         struct sockaddr_in sock2;
608         socklen_t socklen = sizeof(sock);
609         int connect_done = 0;
610         
611         fd[0] = fd[1] = listener = -1;
612
613         memset(&sock, 0, sizeof(sock));
614         
615         if ((listener = socket(PF_INET, SOCK_STREAM, 0)) == -1) goto failed;
616
617         memset(&sock2, 0, sizeof(sock2));
618 #ifdef HAVE_SOCKADDR_LEN
619         sock2.sin_len = sizeof(sock2);
620 #endif
621         sock2.sin_family = PF_INET;
622
623         bind(listener, (struct sockaddr *)&sock2, sizeof(sock2));
624
625         if (listen(listener, 1) != 0) goto failed;
626
627         if (getsockname(listener, (struct sockaddr *)&sock, &socklen) != 0) goto failed;
628
629         if ((fd[1] = socket(PF_INET, SOCK_STREAM, 0)) == -1) goto failed;
630
631         set_nonblocking(fd[1]);
632
633         sock.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
634
635         if (connect(fd[1],(struct sockaddr *)&sock,sizeof(sock)) == -1) {
636                 if (errno != EINPROGRESS) goto failed;
637         } else {
638                 connect_done = 1;
639         }
640
641         if ((fd[0] = accept(listener, (struct sockaddr *)&sock, &socklen)) == -1) goto failed;
642
643         close(listener);
644         if (connect_done == 0) {
645                 if (connect(fd[1],(struct sockaddr *)&sock,sizeof(sock)) != 0
646                     && errno != EISCONN) goto failed;
647         }
648
649         set_blocking (fd[1]);
650
651         /* all OK! */
652         return 0;
653
654  failed:
655         if (fd[0] != -1) close(fd[0]);
656         if (fd[1] != -1) close(fd[1]);
657         if (listener != -1) close(listener);
658         return -1;
659 }
660
661
662
663 /**
664  * Run a program on a local tcp socket, so that we can talk to it's
665  * stdin and stdout.  This is used to fake a connection to a daemon
666  * for testing -- not for the normal case of running SSH.
667  *
668  * @return a socket which is attached to a subprocess running
669  * "prog". stdin and stdout are attached. stderr is left attached to
670  * the original stderr
671  **/
672 int sock_exec(const char *prog)
673 {
674         int fd[2];
675         
676         if (socketpair_tcp(fd) != 0) {
677                 rprintf (FERROR, RSYNC_NAME
678                          ": socketpair_tcp failed (%s)\n",
679                          strerror(errno));
680                 return -1;
681         }
682         if (fork() == 0) {
683                 close(fd[0]);
684                 close(0);
685                 close(1);
686                 dup(fd[1]);
687                 dup(fd[1]);
688                 if (verbose > 3) {
689                         /* Can't use rprintf because we've forked. */
690                         fprintf (stderr,
691                                  RSYNC_NAME ": execute socket program \"%s\"\n",
692                                  prog);
693                 }
694                 exit (system (prog));
695         }
696         close (fd[1]);
697         return fd[0];
698 }
699
700
701