Commit | Line | Data |
---|---|---|
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 | ||
26 | /* open a socket to a tcp remote host with the specified port | |
27 | based on code from Warren */ | |
bc2e93eb AT |
28 | int open_socket_out(char *host, int port) |
29 | { | |
f0fca04e AT |
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 | ||
f5780433 | 84 | memset((char *)&sock,0,sizeof(sock)); |
f0fca04e AT |
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 | ||
8ef4ffd6 | 119 | void start_accept_loop(int port, int (*fn)(int )) |
f0fca04e AT |
120 | { |
121 | int s; | |
122 | ||
f0fca04e AT |
123 | /* open an incoming socket */ |
124 | s = open_socket_in(SOCK_STREAM, port); | |
125 | if (s == -1) | |
8d9dc9f9 | 126 | exit_cleanup(1); |
f0fca04e AT |
127 | |
128 | /* ready to listen */ | |
129 | if (listen(s, 5) == -1) { | |
130 | close(s); | |
8d9dc9f9 | 131 | exit_cleanup(1); |
f0fca04e AT |
132 | } |
133 | ||
134 | ||
135 | /* now accept incoming connections - forking a new process | |
136 | for each incoming connection */ | |
137 | while (1) { | |
138 | fd_set fds; | |
139 | int fd; | |
140 | struct sockaddr addr; | |
141 | int in_addrlen = sizeof(addr); | |
142 | ||
143 | FD_ZERO(&fds); | |
144 | FD_SET(s, &fds); | |
145 | ||
146 | if (select(s+1, &fds, NULL, NULL, NULL) != 1) { | |
147 | continue; | |
148 | } | |
149 | ||
150 | if(!FD_ISSET(s, &fds)) continue; | |
151 | ||
152 | fd = accept(s,&addr,&in_addrlen); | |
153 | ||
154 | if (fd == -1) continue; | |
155 | ||
31f440e6 AT |
156 | signal(SIGCHLD, SIG_IGN); |
157 | ||
158 | /* we shouldn't have any children left hanging around | |
159 | but I have had reports that on Digital Unix zombies | |
160 | are produced, so this ensures that they are reaped */ | |
161 | #ifdef WNOHANG | |
162 | waitpid(-1, NULL, WNOHANG); | |
163 | #endif | |
164 | ||
f0fca04e AT |
165 | if (fork()==0) { |
166 | close(s); | |
167 | ||
168 | _exit(fn(fd)); | |
169 | } | |
170 | ||
171 | close(fd); | |
172 | } | |
f0fca04e AT |
173 | } |
174 | ||
175 | ||
176 | enum SOCK_OPT_TYPES {OPT_BOOL,OPT_INT,OPT_ON}; | |
177 | ||
178 | struct | |
179 | { | |
180 | char *name; | |
181 | int level; | |
182 | int option; | |
183 | int value; | |
184 | int opttype; | |
185 | } socket_options[] = { | |
186 | {"SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, 0, OPT_BOOL}, | |
187 | {"SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, 0, OPT_BOOL}, | |
188 | {"SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, 0, OPT_BOOL}, | |
189 | #ifdef TCP_NODELAY | |
190 | {"TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY, 0, OPT_BOOL}, | |
191 | #endif | |
192 | #ifdef IPTOS_LOWDELAY | |
193 | {"IPTOS_LOWDELAY", IPPROTO_IP, IP_TOS, IPTOS_LOWDELAY, OPT_ON}, | |
194 | #endif | |
195 | #ifdef IPTOS_THROUGHPUT | |
196 | {"IPTOS_THROUGHPUT", IPPROTO_IP, IP_TOS, IPTOS_THROUGHPUT, OPT_ON}, | |
197 | #endif | |
198 | #ifdef SO_SNDBUF | |
199 | {"SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, 0, OPT_INT}, | |
200 | #endif | |
201 | #ifdef SO_RCVBUF | |
202 | {"SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, 0, OPT_INT}, | |
203 | #endif | |
204 | #ifdef SO_SNDLOWAT | |
205 | {"SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, 0, OPT_INT}, | |
206 | #endif | |
207 | #ifdef SO_RCVLOWAT | |
208 | {"SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, 0, OPT_INT}, | |
209 | #endif | |
210 | #ifdef SO_SNDTIMEO | |
211 | {"SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, 0, OPT_INT}, | |
212 | #endif | |
213 | #ifdef SO_RCVTIMEO | |
214 | {"SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, 0, OPT_INT}, | |
215 | #endif | |
216 | {NULL,0,0,0,0}}; | |
217 | ||
218 | ||
219 | ||
220 | /**************************************************************************** | |
221 | set user socket options | |
222 | ****************************************************************************/ | |
223 | void set_socket_options(int fd, char *options) | |
224 | { | |
225 | char *tok; | |
a6801c39 AT |
226 | if (!options || !*options) return; |
227 | ||
f0fca04e AT |
228 | options = strdup(options); |
229 | ||
230 | if (!options) out_of_memory("set_socket_options"); | |
231 | ||
232 | for (tok=strtok(options, " \t,"); tok; tok=strtok(NULL," \t,")) { | |
233 | int ret=0,i; | |
234 | int value = 1; | |
235 | char *p; | |
236 | int got_value = 0; | |
237 | ||
238 | if ((p = strchr(tok,'='))) { | |
239 | *p = 0; | |
240 | value = atoi(p+1); | |
241 | got_value = 1; | |
242 | } | |
243 | ||
244 | for (i=0;socket_options[i].name;i++) | |
245 | if (strcmp(socket_options[i].name,tok)==0) | |
246 | break; | |
247 | ||
248 | if (!socket_options[i].name) { | |
249 | rprintf(FERROR,"Unknown socket option %s\n",tok); | |
250 | continue; | |
251 | } | |
252 | ||
253 | switch (socket_options[i].opttype) { | |
254 | case OPT_BOOL: | |
255 | case OPT_INT: | |
256 | ret = setsockopt(fd,socket_options[i].level, | |
257 | socket_options[i].option,(char *)&value,sizeof(int)); | |
258 | break; | |
259 | ||
260 | case OPT_ON: | |
261 | if (got_value) | |
262 | rprintf(FERROR,"syntax error - %s does not take a value\n",tok); | |
263 | ||
264 | { | |
265 | int on = socket_options[i].value; | |
266 | ret = setsockopt(fd,socket_options[i].level, | |
267 | socket_options[i].option,(char *)&on,sizeof(int)); | |
268 | } | |
269 | break; | |
270 | } | |
271 | ||
272 | if (ret != 0) | |
273 | rprintf(FERROR,"Failed to set socket option %s\n",tok); | |
274 | } | |
275 | ||
276 | free(options); | |
277 | } | |
278 | ||
279 | /**************************************************************************** | |
280 | become a daemon, discarding the controlling terminal | |
281 | ****************************************************************************/ | |
282 | void become_daemon(void) | |
283 | { | |
284 | if (fork()) | |
285 | _exit(0); | |
286 | ||
287 | /* detach from the terminal */ | |
288 | #ifdef HAVE_SETSID | |
289 | setsid(); | |
290 | #else | |
291 | #ifdef TIOCNOTTY | |
292 | { | |
293 | int i = open("/dev/tty", O_RDWR); | |
294 | if (i >= 0) | |
295 | { | |
296 | ioctl(i, (int) TIOCNOTTY, (char *)0); | |
297 | close(i); | |
298 | } | |
299 | } | |
300 | #endif /* TIOCNOTTY */ | |
301 | #endif | |
302 | close(0); | |
303 | close(1); | |
304 | close(2); | |
bc2e93eb | 305 | } |
ff8b29b8 AT |
306 | |
307 | /******************************************************************* | |
308 | return the IP addr of the client as a string | |
309 | ******************************************************************/ | |
310 | char *client_addr(int fd) | |
311 | { | |
312 | struct sockaddr sa; | |
313 | struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa); | |
314 | int length = sizeof(sa); | |
315 | static char addr_buf[100]; | |
316 | ||
317 | if (getpeername(fd, &sa, &length)) { | |
8d9dc9f9 | 318 | exit_cleanup(1); |
ff8b29b8 AT |
319 | } |
320 | ||
321 | strlcpy(addr_buf,(char *)inet_ntoa(sockin->sin_addr), sizeof(addr_buf)-1); | |
322 | ||
323 | return addr_buf; | |
324 | } | |
325 | ||
326 | ||
327 | /******************************************************************* | |
328 | return the DNS name of the client | |
329 | ******************************************************************/ | |
330 | char *client_name(int fd) | |
331 | { | |
332 | struct sockaddr sa; | |
333 | struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa); | |
334 | int length = sizeof(sa); | |
335 | static char name_buf[100]; | |
336 | struct hostent *hp; | |
de5fb374 AT |
337 | char **p; |
338 | char *def = "UNKNOWN"; | |
ff8b29b8 | 339 | |
de5fb374 | 340 | strcpy(name_buf,def); |
ff8b29b8 AT |
341 | |
342 | if (getpeername(fd, &sa, &length)) { | |
8d9dc9f9 | 343 | exit_cleanup(1); |
ff8b29b8 AT |
344 | } |
345 | ||
346 | /* Look up the remote host name. */ | |
347 | if ((hp = gethostbyaddr((char *) &sockin->sin_addr, | |
348 | sizeof(sockin->sin_addr), | |
349 | AF_INET))) { | |
350 | strlcpy(name_buf,(char *)hp->h_name,sizeof(name_buf) - 1); | |
351 | } | |
352 | ||
de5fb374 AT |
353 | |
354 | /* do a forward lookup as well to prevent spoofing */ | |
355 | hp = gethostbyname(name_buf); | |
356 | if (!hp) { | |
357 | strcpy(name_buf,def); | |
358 | rprintf(FERROR,"reverse name lookup failed\n"); | |
359 | } else { | |
360 | for (p=hp->h_addr_list;*p;p++) { | |
361 | if (memcmp(*p, &sockin->sin_addr, hp->h_length) == 0) { | |
362 | break; | |
363 | } | |
364 | } | |
365 | if (!*p) { | |
366 | strcpy(name_buf,def); | |
367 | rprintf(FERROR,"reverse name lookup mismatch - spoofed address?\n"); | |
368 | } | |
369 | } | |
370 | ||
ff8b29b8 AT |
371 | return name_buf; |
372 | } |