acf7a1be0865082dac59974063ddee4d2d91031a
[rsync/rsync.git] / socket.c
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
24 #include "rsync.h"
25
26 /* open a socket to a tcp remote host with the specified port 
27    based on code from Warren */
28 int open_socket_out(char *host, int port)
29 {
30         int type = SOCK_STREAM;
31         struct sockaddr_in sock_out;
32         int res;
33         struct hostent *hp;
34   
35
36         res = socket(PF_INET, type, 0);
37         if (res == -1) {
38                 return -1;
39         }
40
41         hp = gethostbyname(host);
42         if (!hp) {
43                 rprintf(FERROR,"unknown host: %s\n", host);
44                 return -1;
45         }
46
47         memcpy(&sock_out.sin_addr, hp->h_addr, hp->h_length);
48         sock_out.sin_port = htons(port);
49         sock_out.sin_family = PF_INET;
50
51         if (connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out))) {
52                 close(res);
53                 rprintf(FERROR,"failed to connect to %s - %s\n", host, strerror(errno));
54                 return -1;
55         }
56
57         return res;
58 }
59
60
61 /****************************************************************************
62 open a socket of the specified type, port and address for incoming data
63 ****************************************************************************/
64 static int open_socket_in(int type, int port)
65 {
66         struct hostent *hp;
67         struct sockaddr_in sock;
68         char host_name[200];
69         int res;
70         int one=1;
71
72         /* get my host name */
73         if (gethostname(host_name, sizeof(host_name)) == -1) { 
74                 rprintf(FERROR,"gethostname failed\n"); 
75                 return -1; 
76         } 
77
78         /* get host info */
79         if ((hp = gethostbyname(host_name)) == 0) {
80                 rprintf(FERROR,"gethostbyname: Unknown host %s\n",host_name);
81                 return -1;
82         }
83   
84         bzero((char *)&sock,sizeof(sock));
85         memcpy((char *)&sock.sin_addr,(char *)hp->h_addr, hp->h_length);
86         sock.sin_port = htons(port);
87         sock.sin_family = hp->h_addrtype;
88         sock.sin_addr.s_addr = INADDR_ANY;
89         res = socket(hp->h_addrtype, type, 0);
90         if (res == -1) { 
91                 rprintf(FERROR,"socket failed\n"); 
92                 return -1; 
93         }
94
95         setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
96
97         /* now we've got a socket - we need to bind it */
98         if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) == -1) { 
99                 rprintf(FERROR,"bind failed on port %d\n", port);
100                 close(res); 
101                 return -1;
102         }
103
104         return res;
105 }
106
107
108 /****************************************************************************
109 determine if a file descriptor is in fact a socket
110 ****************************************************************************/
111 int is_a_socket(int fd)
112 {
113   int v,l;
114   l = sizeof(int);
115   return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
116 }
117
118
119 void start_accept_loop(int port, int (*fn)(int ))
120 {
121         int s;
122
123         signal(SIGCHLD, SIG_IGN);
124
125         /* open an incoming socket */
126         s = open_socket_in(SOCK_STREAM, port);
127         if (s == -1)
128                 exit_cleanup(1);
129
130         /* ready to listen */
131         if (listen(s, 5) == -1) {
132                 close(s);
133                 exit_cleanup(1);
134         }
135
136
137         /* now accept incoming connections - forking a new process
138            for each incoming connection */
139         while (1) {
140                 fd_set fds;
141                 int fd;
142                 struct sockaddr addr;
143                 int in_addrlen = sizeof(addr);
144
145                 FD_ZERO(&fds);
146                 FD_SET(s, &fds);
147
148                 if (select(s+1, &fds, NULL, NULL, NULL) != 1) {
149                         continue;
150                 }
151
152                 if(!FD_ISSET(s, &fds)) continue;
153
154                 fd = accept(s,&addr,&in_addrlen);
155
156                 if (fd == -1) continue;
157
158                 if (fork()==0) {
159                         close(s);
160
161                         _exit(fn(fd));
162                 }
163
164                 close(fd);
165         }
166 }
167
168
169 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
170
171 struct
172 {
173   char *name;
174   int level;
175   int option;
176   int value;
177   int opttype;
178 } socket_options[] = {
179   {"SO_KEEPALIVE",      SOL_SOCKET,    SO_KEEPALIVE,    0,                 OPT_BOOL},
180   {"SO_REUSEADDR",      SOL_SOCKET,    SO_REUSEADDR,    0,                 OPT_BOOL},
181   {"SO_BROADCAST",      SOL_SOCKET,    SO_BROADCAST,    0,                 OPT_BOOL},
182 #ifdef TCP_NODELAY
183   {"TCP_NODELAY",       IPPROTO_TCP,   TCP_NODELAY,     0,                 OPT_BOOL},
184 #endif
185 #ifdef IPTOS_LOWDELAY
186   {"IPTOS_LOWDELAY",    IPPROTO_IP,    IP_TOS,          IPTOS_LOWDELAY,    OPT_ON},
187 #endif
188 #ifdef IPTOS_THROUGHPUT
189   {"IPTOS_THROUGHPUT",  IPPROTO_IP,    IP_TOS,          IPTOS_THROUGHPUT,  OPT_ON},
190 #endif
191 #ifdef SO_SNDBUF
192   {"SO_SNDBUF",         SOL_SOCKET,    SO_SNDBUF,       0,                 OPT_INT},
193 #endif
194 #ifdef SO_RCVBUF
195   {"SO_RCVBUF",         SOL_SOCKET,    SO_RCVBUF,       0,                 OPT_INT},
196 #endif
197 #ifdef SO_SNDLOWAT
198   {"SO_SNDLOWAT",       SOL_SOCKET,    SO_SNDLOWAT,     0,                 OPT_INT},
199 #endif
200 #ifdef SO_RCVLOWAT
201   {"SO_RCVLOWAT",       SOL_SOCKET,    SO_RCVLOWAT,     0,                 OPT_INT},
202 #endif
203 #ifdef SO_SNDTIMEO
204   {"SO_SNDTIMEO",       SOL_SOCKET,    SO_SNDTIMEO,     0,                 OPT_INT},
205 #endif
206 #ifdef SO_RCVTIMEO
207   {"SO_RCVTIMEO",       SOL_SOCKET,    SO_RCVTIMEO,     0,                 OPT_INT},
208 #endif
209   {NULL,0,0,0,0}};
210
211         
212
213 /****************************************************************************
214 set user socket options
215 ****************************************************************************/
216 void set_socket_options(int fd, char *options)
217 {
218         char *tok;
219         if (!options || !*options) return;
220
221         options = strdup(options);
222         
223         if (!options) out_of_memory("set_socket_options");
224
225         for (tok=strtok(options, " \t,"); tok; tok=strtok(NULL," \t,")) {
226                 int ret=0,i;
227                 int value = 1;
228                 char *p;
229                 int got_value = 0;
230
231                 if ((p = strchr(tok,'='))) {
232                         *p = 0;
233                         value = atoi(p+1);
234                         got_value = 1;
235                 }
236
237                 for (i=0;socket_options[i].name;i++)
238                         if (strcmp(socket_options[i].name,tok)==0)
239                                 break;
240
241                 if (!socket_options[i].name) {
242                         rprintf(FERROR,"Unknown socket option %s\n",tok);
243                         continue;
244                 }
245
246                 switch (socket_options[i].opttype) {
247                 case OPT_BOOL:
248                 case OPT_INT:
249                         ret = setsockopt(fd,socket_options[i].level,
250                                          socket_options[i].option,(char *)&value,sizeof(int));
251                         break;
252                         
253                 case OPT_ON:
254                         if (got_value)
255                                 rprintf(FERROR,"syntax error - %s does not take a value\n",tok);
256
257                         {
258                                 int on = socket_options[i].value;
259                                 ret = setsockopt(fd,socket_options[i].level,
260                                                  socket_options[i].option,(char *)&on,sizeof(int));
261                         }
262                         break;    
263                 }
264                 
265                 if (ret != 0)
266                         rprintf(FERROR,"Failed to set socket option %s\n",tok);
267         }
268
269         free(options);
270 }
271
272 /****************************************************************************
273 become a daemon, discarding the controlling terminal
274 ****************************************************************************/
275 void become_daemon(void)
276 {
277         if (fork())
278                 _exit(0);
279
280         /* detach from the terminal */
281 #ifdef HAVE_SETSID
282         setsid();
283 #else
284 #ifdef TIOCNOTTY
285         {
286                 int i = open("/dev/tty", O_RDWR);
287                 if (i >= 0) 
288                         {
289                                 ioctl(i, (int) TIOCNOTTY, (char *)0);      
290                                 close(i);
291                         }
292         }
293 #endif /* TIOCNOTTY */
294 #endif
295         close(0);
296         close(1);
297         close(2);
298 }
299
300 /*******************************************************************
301  return the IP addr of the client as a string 
302  ******************************************************************/
303 char *client_addr(int fd)
304 {
305         struct sockaddr sa;
306         struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
307         int     length = sizeof(sa);
308         static char addr_buf[100];
309
310         if (getpeername(fd, &sa, &length)) {
311                 exit_cleanup(1);
312         }
313
314         strlcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr), sizeof(addr_buf)-1);
315
316         return addr_buf;
317 }
318
319
320 /*******************************************************************
321  return the DNS name of the client 
322  ******************************************************************/
323 char *client_name(int fd)
324 {
325         struct sockaddr sa;
326         struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
327         int     length = sizeof(sa);
328         static char name_buf[100];
329         struct hostent *hp;
330         char **p;
331         char *def = "UNKNOWN";
332
333         strcpy(name_buf,def);
334
335         if (getpeername(fd, &sa, &length)) {
336                 exit_cleanup(1);
337         }
338
339         /* Look up the remote host name. */
340         if ((hp = gethostbyaddr((char *) &sockin->sin_addr,
341                                 sizeof(sockin->sin_addr),
342                                 AF_INET))) {
343                 strlcpy(name_buf,(char *)hp->h_name,sizeof(name_buf) - 1);
344         }
345
346
347         /* do a forward lookup as well to prevent spoofing */
348         hp = gethostbyname(name_buf);
349         if (!hp) {
350                 strcpy(name_buf,def);
351                 rprintf(FERROR,"reverse name lookup failed\n");
352         } else {
353                 for (p=hp->h_addr_list;*p;p++) {
354                         if (memcmp(*p, &sockin->sin_addr, hp->h_length) == 0) {
355                                 break;
356                         }
357                 }
358                 if (!*p) {
359                         strcpy(name_buf,def);
360                         rprintf(FERROR,"reverse name lookup mismatch - spoofed address?\n");
361                 } 
362         }
363
364         return name_buf;
365 }