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