2 Copyright (C) Andrew Tridgell 1998
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.
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.
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.
20 socket functions used in rsync
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)
30 int type = SOCK_STREAM;
31 struct sockaddr_in sock_out;
36 res = socket(PF_INET, type, 0);
41 hp = gethostbyname(host);
43 rprintf(FERROR,"unknown host: %s\n", host);
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;
51 if (connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out))) {
53 rprintf(FERROR,"failed to connect to %s - %s\n", host, strerror(errno));
63 /****************************************************************************
64 open a socket of the specified type, port and address for incoming data
65 ****************************************************************************/
66 static int open_socket_in(int type, int port)
69 struct sockaddr_in sock;
70 char host_name[MAXHOSTNAMELEN];
74 /* get my host name */
75 if (gethostname(host_name, sizeof(host_name)) == -1) {
76 rprintf(FERROR,"gethostname failed\n");
81 if ((hp = gethostbyname(host_name)) == 0) {
82 rprintf(FERROR,"gethostbyname: Unknown host %s\n",host_name);
86 memset((char *)&sock,0,sizeof(sock));
87 memcpy((char *)&sock.sin_addr,(char *)hp->h_addr, hp->h_length);
88 sock.sin_port = htons(port);
89 sock.sin_family = hp->h_addrtype;
90 sock.sin_addr.s_addr = INADDR_ANY;
91 res = socket(hp->h_addrtype, type, 0);
93 rprintf(FERROR,"socket failed\n");
97 setsockopt(res,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one));
99 /* now we've got a socket - we need to bind it */
100 if (bind(res, (struct sockaddr * ) &sock,sizeof(sock)) == -1) {
101 rprintf(FERROR,"bind failed on port %d\n", port);
110 /****************************************************************************
111 determine if a file descriptor is in fact a socket
112 ****************************************************************************/
113 int is_a_socket(int fd)
117 return(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&v, &l) == 0);
121 void start_accept_loop(int port, int (*fn)(int ))
125 /* open an incoming socket */
126 s = open_socket_in(SOCK_STREAM, port);
128 exit_cleanup(RERR_SOCKETIO);
130 /* ready to listen */
131 if (listen(s, 5) == -1) {
133 exit_cleanup(RERR_SOCKETIO);
137 /* now accept incoming connections - forking a new process
138 for each incoming connection */
142 struct sockaddr addr;
143 int in_addrlen = sizeof(addr);
148 if (select(s+1, &fds, NULL, NULL, NULL) != 1) {
152 if(!FD_ISSET(s, &fds)) continue;
154 fd = accept(s,&addr,&in_addrlen);
156 if (fd == -1) continue;
158 signal(SIGCHLD, SIG_IGN);
160 /* we shouldn't have any children left hanging around
161 but I have had reports that on Digital Unix zombies
162 are produced, so this ensures that they are reaped */
164 waitpid(-1, NULL, WNOHANG);
180 enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON};
189 } socket_options[] = {
190 {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL},
191 {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL},
192 {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL},
194 {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL},
196 #ifdef IPTOS_LOWDELAY
197 {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON},
199 #ifdef IPTOS_THROUGHPUT
200 {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON},
203 {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT},
206 {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT},
209 {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT},
212 {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT},
215 {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT},
218 {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT},
224 /****************************************************************************
225 set user socket options
226 ****************************************************************************/
227 void set_socket_options(int fd, char *options)
230 if (!options || !*options) return;
232 options = strdup(options);
234 if (!options) out_of_memory("set_socket_options");
236 for (tok=strtok(options, " \t,"); tok; tok=strtok(NULL," \t,")) {
242 if ((p = strchr(tok,'='))) {
248 for (i=0;socket_options[i].name;i++)
249 if (strcmp(socket_options[i].name,tok)==0)
252 if (!socket_options[i].name) {
253 rprintf(FERROR,"Unknown socket option %s\n",tok);
257 switch (socket_options[i].opttype) {
260 ret = setsockopt(fd,socket_options[i].level,
261 socket_options[i].option,(char *)&value,sizeof(int));
266 rprintf(FERROR,"syntax error - %s does not take a value\n",tok);
269 int on = socket_options[i].value;
270 ret = setsockopt(fd,socket_options[i].level,
271 socket_options[i].option,(char *)&on,sizeof(int));
277 rprintf(FERROR,"Failed to set socket option %s\n",tok);
283 /****************************************************************************
284 become a daemon, discarding the controlling terminal
285 ****************************************************************************/
286 void become_daemon(void)
294 /* detach from the terminal */
299 i = open("/dev/tty", O_RDWR);
301 ioctl(i, (int) TIOCNOTTY, (char *)0);
304 #endif /* TIOCNOTTY */
306 /* make sure that stdin, stdout an stderr don't stuff things
307 up (library functions, for example) */
310 open("/dev/null", O_RDWR);
314 /*******************************************************************
315 return the IP addr of the client as a string
316 ******************************************************************/
317 char *client_addr(int fd)
320 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
321 int length = sizeof(sa);
322 static char addr_buf[100];
323 static int initialised;
325 if (initialised) return addr_buf;
329 if (getpeername(fd, &sa, &length)) {
330 exit_cleanup(RERR_SOCKETIO);
333 strlcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr), sizeof(addr_buf));
338 /*******************************************************************
339 return the DNS name of the client
340 ******************************************************************/
341 char *client_name(int fd)
344 struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
345 int length = sizeof(sa);
346 static char name_buf[100];
349 char *def = "UNKNOWN";
350 static int initialised;
352 if (initialised) return name_buf;
356 strcpy(name_buf,def);
358 if (getpeername(fd, &sa, &length)) {
359 exit_cleanup(RERR_SOCKETIO);
362 /* Look up the remote host name. */
363 if ((hp = gethostbyaddr((char *) &sockin->sin_addr,
364 sizeof(sockin->sin_addr),
366 strlcpy(name_buf,(char *)hp->h_name,sizeof(name_buf));
370 /* do a forward lookup as well to prevent spoofing */
371 hp = gethostbyname(name_buf);
373 strcpy(name_buf,def);
374 rprintf(FERROR,"reverse name lookup failed\n");
376 for (p=hp->h_addr_list;*p;p++) {
377 if (memcmp(*p, &sockin->sin_addr, hp->h_length) == 0) {
382 strcpy(name_buf,def);
383 rprintf(FERROR,"reverse name lookup mismatch - spoofed address?\n");