Use socklen_t if defined, or otherwise int. This tries to fix
[rsync/rsync.git] / socket.c
CommitLineData
7c1b4daa
MP
1/* -*- c-file-style: "linux" -*-
2
660c6fbd 3 Copyright (C) 1998-2001 by Andrew Tridgell
bc2e93eb
AT
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20/*
21 socket functions used in rsync
22
23 */
24
f0fca04e
AT
25#include "rsync.h"
26
4c3b4b25 27
660c6fbd
MP
28/* Establish a proxy connection on an open socket to a web roxy by
29 * using the CONNECT method. */
4c3b4b25
AT
30static int establish_proxy_connection(int fd, char *host, int port)
31{
32 char buffer[1024];
33 char *cp;
34
8950ac03 35 snprintf(buffer, sizeof(buffer), "CONNECT %s:%d HTTP/1.0\r\n\r\n", host, port);
4c3b4b25 36 if (write(fd, buffer, strlen(buffer)) != strlen(buffer)) {
660c6fbd 37 rprintf(FERROR, "failed to write to proxy: %s\n",
4c3b4b25
AT
38 strerror(errno));
39 return -1;
40 }
41
42 for (cp = buffer; cp < &buffer[sizeof(buffer) - 1]; cp++) {
43 if (read(fd, cp, 1) != 1) {
660c6fbd
MP
44 rprintf(FERROR, "failed to read from proxy: %s\n",
45 strerror(errno));
4c3b4b25
AT
46 return -1;
47 }
48 if (*cp == '\n')
49 break;
50 }
51
52 if (*cp != '\n')
53 cp++;
54 *cp-- = '\0';
55 if (*cp == '\r')
56 *cp = '\0';
57 if (strncmp(buffer, "HTTP/", 5) != 0) {
58 rprintf(FERROR, "bad response from proxy - %s\n",
59 buffer);
60 return -1;
61 }
62 for (cp = &buffer[5]; isdigit(*cp) || (*cp == '.'); cp++)
63 ;
64 while (*cp == ' ')
65 cp++;
66 if (*cp != '2') {
67 rprintf(FERROR, "bad response from proxy - %s\n",
68 buffer);
69 return -1;
70 }
71 /* throw away the rest of the HTTP header */
72 while (1) {
73 for (cp = buffer; cp < &buffer[sizeof(buffer) - 1];
74 cp++) {
75 if (read(fd, cp, 1) != 1) {
660c6fbd
MP
76 rprintf(FERROR, "failed to read from proxy: %s\n",
77 strerror(errno));
4c3b4b25
AT
78 return -1;
79 }
80 if (*cp == '\n')
81 break;
82 }
83 if ((cp > buffer) && (*cp == '\n'))
84 cp--;
85 if ((cp == buffer) && ((*cp == '\n') || (*cp == '\r')))
86 break;
87 }
88 return 0;
89}
90
91
f0fca04e 92/* open a socket to a tcp remote host with the specified port
4c3b4b25
AT
93 based on code from Warren
94 proxy support by Stephen Rothwell */
e30f0657 95int open_socket_out(char *host, int port, struct in_addr *address)
bc2e93eb 96{
f0fca04e
AT
97 int type = SOCK_STREAM;
98 struct sockaddr_in sock_out;
e30f0657 99 struct sockaddr_in sock;
f0fca04e
AT
100 int res;
101 struct hostent *hp;
4c3b4b25
AT
102 char *h;
103 unsigned p;
104 int proxied = 0;
105 char buffer[1024];
106 char *cp;
107
660c6fbd
MP
108 /* if we have a RSYNC_PROXY env variable then redirect our
109 * connetcion via a web proxy at the given address. The format
110 * is hostname:port */
4c3b4b25
AT
111 h = getenv("RSYNC_PROXY");
112 proxied = (h != NULL) && (*h != '\0');
113
114 if (proxied) {
115 strlcpy(buffer, h, sizeof(buffer));
116 cp = strchr(buffer, ':');
117 if (cp == NULL) {
660c6fbd
MP
118 rprintf(FERROR,
119 "invalid proxy specification: should be HOST:PORT\n");
4c3b4b25
AT
120 return -1;
121 }
122 *cp++ = '\0';
123 p = atoi(cp);
124 h = buffer;
125 } else {
126 h = host;
127 p = port;
128 }
f0fca04e
AT
129
130 res = socket(PF_INET, type, 0);
131 if (res == -1) {
132 return -1;
133 }
134
4c3b4b25 135 hp = gethostbyname(h);
f0fca04e 136 if (!hp) {
4c3b4b25
AT
137 rprintf(FERROR,"unknown host: %s\n", h);
138 close(res);
f0fca04e
AT
139 return -1;
140 }
141
142 memcpy(&sock_out.sin_addr, hp->h_addr, hp->h_length);
4c3b4b25 143 sock_out.sin_port = htons(p);
f0fca04e
AT
144 sock_out.sin_family = PF_INET;
145
e30f0657
AT
146 if (address) {
147 sock.sin_addr = *address;
148 sock.sin_port = 0;
149 sock.sin_family = hp->h_addrtype;
150 bind(res, (struct sockaddr * ) &sock,sizeof(sock));
151 }
152
f0fca04e 153 if (connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out))) {
4c3b4b25
AT
154 rprintf(FERROR,"failed to connect to %s - %s\n", h, strerror(errno));
155 close(res);
156 return -1;
157 }
158
159 if (proxied && establish_proxy_connection(res, host, port) != 0) {
f0fca04e 160 close(res);
f0fca04e
AT
161 return -1;
162 }
163
164 return res;
165}
166
167
168/****************************************************************************
169open a socket of the specified type, port and address for incoming data
170****************************************************************************/
5c9730a4 171static int open_socket_in(int type, int port, struct in_addr *address)
f0fca04e
AT
172{
173 struct hostent *hp;
174 struct sockaddr_in sock;
d41c7d02 175 char host_name[MAXHOSTNAMELEN];
f0fca04e
AT
176 int res;
177 int one=1;
178
179 /* get my host name */
180 if (gethostname(host_name, sizeof(host_name)) == -1) {
181 rprintf(FERROR,"gethostname failed\n");
182 return -1;
183 }
184
185 /* get host info */
186 if ((hp = gethostbyname(host_name)) == 0) {
187 rprintf(FERROR,"gethostbyname: Unknown host %s\n",host_name);
188 return -1;
189 }
190
f5780433 191 memset((char *)&sock,0,sizeof(sock));
f0fca04e
AT
192 memcpy((char *)&sock.sin_addr,(char *)hp->h_addr, hp->h_length);
193 sock.sin_port = htons(port);
194 sock.sin_family = hp->h_addrtype;
5c9730a4
AT
195 if (address) {
196 sock.sin_addr = *address;
197 } else {
198 sock.sin_addr.s_addr = INADDR_ANY;
199 }
f0fca04e
AT
200 res = socket(hp->h_addrtype, type, 0);
201 if (res == -1) {
660c6fbd
MP
202 rprintf(FERROR,"socket failed: %s\n",
203 strerror(errno));
f0fca04e
AT
204 return -1;
205 }
206
207 setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
208
209 /* now we've got a socket - we need to bind it */
210 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) == -1) {
660c6fbd
MP
211 rprintf(FERROR,"bind failed on port %d: %s\n", port,
212 strerror(errno));
4d66e00a
MP
213 if (errno == EACCES && port < 1024) {
214 rprintf(FERROR, "Note: you must be root to bind "
215 "to low-numbered ports");
216 }
f0fca04e
AT
217 close(res);
218 return -1;
219 }
220
221 return res;
222}
223
224
7c1b4daa
MP
225/*
226 * Determine if a file descriptor is in fact a socket
227 */
f0fca04e
AT
228int is_a_socket(int fd)
229{
ac2a1a44
MP
230 int v;
231 socklen_t l;
3eb38818 232 l = sizeof(int);
7c1b4daa
MP
233
234 /* Parameters to getsockopt, setsockopt etc are very
235 * unstandardized across platforms, so don't be surprised if
ac2a1a44
MP
236 * there are compiler warnings on e.g. SCO OpenSwerver or AIX.
237 * It seems they all eventually get the right idea.
7c1b4daa
MP
238 *
239 * Debian says: ``The fifth argument of getsockopt and
240 * setsockopt is in reality an int [*] (and this is what BSD
241 * 4.* and libc4 and libc5 have). Some POSIX confusion
242 * resulted in the present socklen_t. The draft standard has
243 * not been adopted yet, but glibc2 already follows it and
244 * also has socklen_t [*]. See also accept(2).''
245 *
246 * We now return to your regularly scheduled programming. */
3eb38818 247 return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
f0fca04e
AT
248}
249
250
8ef4ffd6 251void start_accept_loop(int port, int (*fn)(int ))
f0fca04e
AT
252{
253 int s;
5c9730a4 254 extern struct in_addr socket_address;
f0fca04e 255
f0fca04e 256 /* open an incoming socket */
5c9730a4 257 s = open_socket_in(SOCK_STREAM, port, &socket_address);
f0fca04e 258 if (s == -1)
65417579 259 exit_cleanup(RERR_SOCKETIO);
f0fca04e
AT
260
261 /* ready to listen */
262 if (listen(s, 5) == -1) {
263 close(s);
65417579 264 exit_cleanup(RERR_SOCKETIO);
f0fca04e
AT
265 }
266
267
268 /* now accept incoming connections - forking a new process
269 for each incoming connection */
270 while (1) {
271 fd_set fds;
272 int fd;
273 struct sockaddr addr;
274 int in_addrlen = sizeof(addr);
275
15b84e14
DD
276 /* close log file before the potentially very long select so
277 file can be trimmed by another process instead of growing
278 forever */
279 log_close();
45a83540 280
f0fca04e
AT
281 FD_ZERO(&fds);
282 FD_SET(s, &fds);
283
284 if (select(s+1, &fds, NULL, NULL, NULL) != 1) {
285 continue;
286 }
287
288 if(!FD_ISSET(s, &fds)) continue;
289
7c1b4daa 290 /* See note above prototypes. */
f0fca04e
AT
291 fd = accept(s,&addr,&in_addrlen);
292
293 if (fd == -1) continue;
294
31f440e6
AT
295 signal(SIGCHLD, SIG_IGN);
296
297 /* we shouldn't have any children left hanging around
298 but I have had reports that on Digital Unix zombies
299 are produced, so this ensures that they are reaped */
300#ifdef WNOHANG
0503f060 301 while (waitpid(-1, NULL, WNOHANG) > 0);
31f440e6
AT
302#endif
303
f0fca04e
AT
304 if (fork()==0) {
305 close(s);
306
15b84e14
DD
307 /* open log file in child before possibly giving
308 up privileges */
309 log_open();
310
f0fca04e
AT
311 _exit(fn(fd));
312 }
313
314 close(fd);
315 }
f0fca04e
AT
316}
317
318
319enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
320
321struct
322{
323 char *name;
324 int level;
325 int option;
326 int value;
327 int opttype;
328} socket_options[] = {
329 {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
330 {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
331 {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
332#ifdef TCP_NODELAY
333 {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
334#endif
335#ifdef IPTOS_LOWDELAY
336 {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
337#endif
338#ifdef IPTOS_THROUGHPUT
339 {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
340#endif
341#ifdef SO_SNDBUF
342 {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
343#endif
344#ifdef SO_RCVBUF
345 {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
346#endif
347#ifdef SO_SNDLOWAT
348 {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
349#endif
350#ifdef SO_RCVLOWAT
351 {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
352#endif
353#ifdef SO_SNDTIMEO
354 {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
355#endif
356#ifdef SO_RCVTIMEO
357 {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
358#endif
359 {NULL,0,0,0,0}};
360
361
362
363/****************************************************************************
364set user socket options
365****************************************************************************/
366void set_socket_options(int fd, char *options)
367{
368 char *tok;
a6801c39
AT
369 if (!options || !*options) return;
370
f0fca04e
AT
371 options = strdup(options);
372
373 if (!options) out_of_memory("set_socket_options");
374
375 for (tok=strtok(options, " \t,"); tok; tok=strtok(NULL," \t,")) {
376 int ret=0,i;
377 int value = 1;
378 char *p;
379 int got_value = 0;
380
381 if ((p = strchr(tok,'='))) {
382 *p = 0;
383 value = atoi(p+1);
384 got_value = 1;
385 }
386
387 for (i=0;socket_options[i].name;i++)
388 if (strcmp(socket_options[i].name,tok)==0)
389 break;
390
391 if (!socket_options[i].name) {
392 rprintf(FERROR,"Unknown socket option %s\n",tok);
393 continue;
394 }
395
396 switch (socket_options[i].opttype) {
397 case OPT_BOOL:
398 case OPT_INT:
399 ret = setsockopt(fd,socket_options[i].level,
400 socket_options[i].option,(char *)&value,sizeof(int));
401 break;
402
403 case OPT_ON:
404 if (got_value)
405 rprintf(FERROR,"syntax error - %s does not take a value\n",tok);
406
407 {
408 int on = socket_options[i].value;
409 ret = setsockopt(fd,socket_options[i].level,
410 socket_options[i].option,(char *)&on,sizeof(int));
411 }
412 break;
413 }
414
415 if (ret != 0)
660c6fbd
MP
416 rprintf(FERROR, "failed to set socket option %s: %s\n", tok,
417 strerror(errno));
f0fca04e
AT
418 }
419
420 free(options);
421}
422
423/****************************************************************************
424become a daemon, discarding the controlling terminal
425****************************************************************************/
426void become_daemon(void)
427{
b11ed3b1
AT
428 int i;
429
c46ded46 430 if (fork()) {
f0fca04e 431 _exit(0);
c46ded46 432 }
f0fca04e
AT
433
434 /* detach from the terminal */
435#ifdef HAVE_SETSID
436 setsid();
437#else
438#ifdef TIOCNOTTY
c46ded46
AT
439 i = open("/dev/tty", O_RDWR);
440 if (i >= 0) {
441 ioctl(i, (int) TIOCNOTTY, (char *)0);
442 close(i);
f0fca04e
AT
443 }
444#endif /* TIOCNOTTY */
445#endif
b11ed3b1
AT
446 /* make sure that stdin, stdout an stderr don't stuff things
447 up (library functions, for example) */
448 for (i=0;i<3;i++) {
449 close(i);
450 open("/dev/null", O_RDWR);
451 }
bc2e93eb 452}
ff8b29b8
AT
453
454/*******************************************************************
455 return the IP addr of the client as a string
456 ******************************************************************/
457char *client_addr(int fd)
458{
459 struct sockaddr sa;
460 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
461 int length = sizeof(sa);
462 static char addr_buf[100];
11a5a3c7
AT
463 static int initialised;
464
465 if (initialised) return addr_buf;
466
467 initialised = 1;
ff8b29b8
AT
468
469 if (getpeername(fd, &sa, &length)) {
65417579 470 exit_cleanup(RERR_SOCKETIO);
ff8b29b8 471 }
11a5a3c7 472
37f9805d 473 strlcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr), sizeof(addr_buf));
ff8b29b8
AT
474 return addr_buf;
475}
476
477
478/*******************************************************************
479 return the DNS name of the client
480 ******************************************************************/
481char *client_name(int fd)
482{
483 struct sockaddr sa;
484 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
485 int length = sizeof(sa);
486 static char name_buf[100];
487 struct hostent *hp;
de5fb374
AT
488 char **p;
489 char *def = "UNKNOWN";
11a5a3c7
AT
490 static int initialised;
491
492 if (initialised) return name_buf;
493
494 initialised = 1;
ff8b29b8 495
de5fb374 496 strcpy(name_buf,def);
ff8b29b8
AT
497
498 if (getpeername(fd, &sa, &length)) {
65417579 499 exit_cleanup(RERR_SOCKETIO);
ff8b29b8
AT
500 }
501
502 /* Look up the remote host name. */
503 if ((hp = gethostbyaddr((char *) &sockin->sin_addr,
504 sizeof(sockin->sin_addr),
505 AF_INET))) {
37f9805d 506 strlcpy(name_buf,(char *)hp->h_name,sizeof(name_buf));
ff8b29b8
AT
507 }
508
de5fb374
AT
509
510 /* do a forward lookup as well to prevent spoofing */
511 hp = gethostbyname(name_buf);
512 if (!hp) {
513 strcpy(name_buf,def);
514 rprintf(FERROR,"reverse name lookup failed\n");
515 } else {
516 for (p=hp->h_addr_list;*p;p++) {
517 if (memcmp(*p, &sockin->sin_addr, hp->h_length) == 0) {
518 break;
519 }
520 }
521 if (!*p) {
522 strcpy(name_buf,def);
523 rprintf(FERROR,"reverse name lookup mismatch - spoofed address?\n");
524 }
525 }
526
ff8b29b8
AT
527 return name_buf;
528}
5c9730a4
AT
529
530/*******************************************************************
531convert a string to an IP address. The string can be a name or
532dotted decimal number
533 ******************************************************************/
534struct in_addr *ip_address(const char *str)
535{
536 static struct in_addr ret;
537 struct hostent *hp;
538
539 /* try as an IP address */
540 if (inet_aton(str, &ret) != 0) {
541 return &ret;
542 }
543
544 /* otherwise assume it's a network name of some sort and use
545 gethostbyname */
546 if ((hp = gethostbyname(str)) == 0) {
547 rprintf(FERROR, "gethostbyname: Unknown host. %s\n",str);
548 return NULL;
549 }
550
551 if (hp->h_addr == NULL) {
552 rprintf(FERROR, "gethostbyname: host address is invalid for host %s\n",str);
553 return NULL;
554 }
555
556 if (hp->h_length > sizeof(ret)) {
557 rprintf(FERROR, "gethostbyname: host address is too large\n");
558 return NULL;
559 }
560
561 memcpy(&ret.s_addr, hp->h_addr, hp->h_length);
562
563 return(&ret);
564}