d3372da9 |
1 | /* |
2 | * Dolda Connect - Modular multiuser Direct Connect-style client |
3 | * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com) |
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 | */ |
19 | /* XXX: Implement SOCKS proxyability */ |
20 | |
21 | #ifdef HAVE_CONFIG_H |
22 | #include <config.h> |
23 | #endif |
24 | #include <string.h> |
25 | #include <stdlib.h> |
26 | #include <unistd.h> |
27 | #include <fcntl.h> |
28 | #include <sys/ioctl.h> |
29 | #include <sys/socket.h> |
30 | #include <sys/un.h> |
31 | #include <sys/poll.h> |
32 | #include <arpa/inet.h> |
33 | #include <netinet/in.h> |
34 | #include <netdb.h> |
35 | #include <sys/signal.h> |
7ab36fbd |
36 | #include <sys/stat.h> /* For rebindunix() */ |
d3372da9 |
37 | #ifdef HAVE_LINUX_SOCKIOS_H |
38 | #include <linux/sockios.h> |
39 | #endif |
40 | #include <errno.h> |
41 | #include <net/if.h> |
42 | |
43 | #include "conf.h" |
44 | #include "net.h" |
45 | #include "module.h" |
46 | #include "log.h" |
47 | #include "utils.h" |
48 | #include "sysevents.h" |
49 | |
50 | static struct configvar myvars[] = |
51 | { |
d9f89ef5 |
52 | /** The network mode to use. Currently supported values are 0 for |
53 | * active mode and 1 for passive mode. In the future, SOCKS5 proxy |
54 | * support may be added. */ |
d3372da9 |
55 | {CONF_VAR_INT, "mode", {.num = 0}}, |
d9f89ef5 |
56 | /** Set the SO_REUSEADDR socket option on listening sockets, so |
57 | * that dead TCP connections waiting for timeout are ignored. */ |
347d6d76 |
58 | {CONF_VAR_BOOL, "reuseaddr", {.num = 0}}, |
d9f89ef5 |
59 | /** Overrides the IPv4 address reported to other clients in active |
60 | * mode. Useful for servers behind NAT routers. If both this and |
61 | * net.publicif are unspecified the address of the hub connection |
62 | * is used. */ |
d3372da9 |
63 | {CONF_VAR_IPV4, "visibleipv4", {.ipv4 = {0}}}, |
d9f89ef5 |
64 | /** Specifies an interface name from which to fetch the IPv4 |
65 | * address reported to other clients in active mode. If both this |
66 | * and net.visibleipv4 are unspecified the address of the hub |
67 | * connection is used. */ |
d3372da9 |
68 | {CONF_VAR_STRING, "publicif", {.str = L""}}, |
b020fb3d |
69 | /* Diffserv should be supported on IPv4, too, but I don't know the |
70 | * API to do that. */ |
d9f89ef5 |
71 | /** The Diffserv value to use on IPv6 connections when the |
72 | * minimize cost TOS value is used (see the TOS VALUES |
73 | * section). */ |
b020fb3d |
74 | {CONF_VAR_INT, "diffserv-mincost", {.num = 0}}, |
d9f89ef5 |
75 | /** The Diffserv value to use on IPv6 connections when the |
76 | * maximize reliability TOS value is used (see the TOS VALUES |
77 | * section). */ |
b020fb3d |
78 | {CONF_VAR_INT, "diffserv-maxrel", {.num = 0}}, |
d9f89ef5 |
79 | /** The Diffserv value to use on IPv6 connections when the |
80 | * maximize throughput TOS value is used (see the TOS VALUES |
81 | * section). */ |
b020fb3d |
82 | {CONF_VAR_INT, "diffserv-maxtp", {.num = 0}}, |
d9f89ef5 |
83 | /** The Diffserv value to use on IPv6 connections when the |
84 | * minimize delay TOS value is used (see the TOS VALUES |
85 | * section). */ |
b020fb3d |
86 | {CONF_VAR_INT, "diffserv-mindelay", {.num = 0}}, |
d3372da9 |
87 | {CONF_VAR_END} |
88 | }; |
89 | |
90 | static struct socket *sockets = NULL; |
91 | int numsocks = 0; |
92 | |
93 | /* XXX: Get autoconf for all this... */ |
94 | int getpublicaddr(int af, struct sockaddr **addr, socklen_t *lenbuf) |
95 | { |
96 | struct sockaddr_in *ipv4; |
97 | struct configvar *var; |
98 | void *bufend; |
99 | int sock; |
100 | struct ifconf conf; |
101 | struct ifreq *ifr, req; |
102 | char *pif; |
103 | |
104 | if(af == AF_INET) |
105 | { |
106 | var = confgetvar("net", "visibleipv4"); |
107 | if(var->val.ipv4.s_addr != 0) |
108 | { |
109 | ipv4 = smalloc(sizeof(*ipv4)); |
110 | ipv4->sin_family = AF_INET; |
111 | ipv4->sin_addr.s_addr = var->val.ipv4.s_addr; |
112 | *addr = (struct sockaddr *)ipv4; |
113 | *lenbuf = sizeof(*ipv4); |
114 | return(0); |
115 | } |
bcb73bb3 |
116 | if((pif = icswcstombs(confgetstr("net", "publicif"), NULL, NULL)) == NULL) |
d3372da9 |
117 | { |
118 | flog(LOG_ERR, "could not convert net.publicif into local charset: %s", strerror(errno)); |
119 | return(-1); |
120 | } |
6fb0c6ac |
121 | if(!strcmp(pif, "")) |
122 | return(1); |
d3372da9 |
123 | if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) |
124 | return(-1); |
125 | conf.ifc_buf = smalloc(conf.ifc_len = 65536); |
126 | if(ioctl(sock, SIOCGIFCONF, &conf) < 0) |
127 | { |
128 | free(conf.ifc_buf); |
129 | close(sock); |
130 | return(-1); |
131 | } |
132 | bufend = ((char *)conf.ifc_buf) + conf.ifc_len; |
133 | ipv4 = NULL; |
134 | for(ifr = conf.ifc_ifcu.ifcu_req; (void *)ifr < bufend; ifr++) |
135 | { |
1315d02a |
136 | if(strcmp(ifr->ifr_name, pif)) |
6fb0c6ac |
137 | continue; |
d3372da9 |
138 | memset(&req, 0, sizeof(req)); |
139 | memcpy(req.ifr_name, ifr->ifr_name, sizeof(ifr->ifr_name)); |
140 | if(ioctl(sock, SIOCGIFFLAGS, &req) < 0) |
6fb0c6ac |
141 | break; |
142 | if(!(req.ifr_flags & IFF_UP)) |
d3372da9 |
143 | { |
6fb0c6ac |
144 | flog(LOG_WARNING, "public interface is down"); |
145 | break; |
d3372da9 |
146 | } |
6fb0c6ac |
147 | if(ifr->ifr_addr.sa_family != AF_INET) |
d3372da9 |
148 | { |
6fb0c6ac |
149 | flog(LOG_WARNING, "address of the public interface is not AF_INET"); |
150 | break; |
d3372da9 |
151 | } |
6fb0c6ac |
152 | ipv4 = smalloc(sizeof(*ipv4)); |
153 | memcpy(ipv4, &ifr->ifr_addr, sizeof(ifr->ifr_addr)); |
154 | break; |
d3372da9 |
155 | } |
bcb73bb3 |
156 | free(conf.ifc_buf); |
d3372da9 |
157 | close(sock); |
158 | if(ipv4 != NULL) |
159 | { |
160 | *addr = (struct sockaddr *)ipv4; |
161 | *lenbuf = sizeof(*ipv4); |
162 | return(0); |
163 | } |
164 | errno = ENETDOWN; |
165 | return(-1); |
166 | } |
6fb0c6ac |
167 | return(1); |
d3372da9 |
168 | } |
169 | |
170 | static struct socket *newsock(int type) |
171 | { |
172 | struct socket *new; |
173 | |
174 | new = smalloc(sizeof(*new)); |
175 | new->refcount = 2; |
176 | new->fd = -1; |
177 | new->isrealsocket = 1; |
178 | new->family = -1; |
179 | new->tos = 0; |
180 | new->type = type; |
181 | new->state = -1; |
182 | new->ignread = 0; |
183 | new->close = 0; |
184 | new->remote = NULL; |
185 | new->remotelen = 0; |
9e5f2b29 |
186 | memset(&new->ucred, 0, sizeof(new->ucred)); |
d3372da9 |
187 | switch(type) |
188 | { |
189 | case SOCK_STREAM: |
190 | new->outbuf.s.buf = NULL; |
191 | new->outbuf.s.bufsize = 0; |
192 | new->outbuf.s.datasize = 0; |
193 | new->inbuf.s.buf = NULL; |
194 | new->inbuf.s.bufsize = 0; |
195 | new->inbuf.s.datasize = 0; |
196 | break; |
197 | case SOCK_DGRAM: |
198 | new->outbuf.d.f = new->outbuf.d.l = NULL; |
199 | new->inbuf.d.f = new->inbuf.d.l = NULL; |
200 | break; |
201 | } |
cab0b442 |
202 | new->conncb = NULL; |
203 | new->errcb = NULL; |
204 | new->readcb = NULL; |
205 | new->writecb = NULL; |
206 | new->acceptcb = NULL; |
d3372da9 |
207 | new->next = sockets; |
208 | new->prev = NULL; |
209 | if(sockets != NULL) |
210 | sockets->prev = new; |
211 | sockets = new; |
212 | numsocks++; |
213 | return(new); |
214 | } |
215 | |
216 | static struct socket *mksock(int domain, int type) |
217 | { |
218 | int fd; |
219 | struct socket *new; |
220 | |
221 | if((fd = socket(domain, type, 0)) < 0) |
222 | { |
223 | flog(LOG_CRIT, "could not create socket: %s", strerror(errno)); |
224 | return(NULL); |
225 | } |
226 | new = newsock(type); |
227 | new->fd = fd; |
228 | new->family = domain; |
229 | fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); |
230 | return(new); |
231 | } |
232 | |
233 | struct socket *wrapsock(int fd) |
234 | { |
235 | struct socket *new; |
236 | |
237 | new = newsock(SOCK_STREAM); |
238 | new->fd = fd; |
239 | new->state = SOCK_EST; |
240 | new->isrealsocket = 0; |
241 | fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); |
242 | return(new); |
243 | } |
244 | |
245 | static void unlinksock(struct socket *sk) |
246 | { |
247 | if(sk->prev != NULL) |
248 | sk->prev->next = sk->next; |
249 | if(sk->next != NULL) |
250 | sk->next->prev = sk->prev; |
251 | if(sk == sockets) |
252 | sockets = sk->next; |
253 | putsock(sk); |
254 | numsocks--; |
255 | } |
256 | |
257 | void getsock(struct socket *sk) |
258 | { |
259 | sk->refcount++; |
260 | } |
261 | |
262 | void putsock(struct socket *sk) |
263 | { |
264 | struct dgrambuf *buf; |
265 | |
266 | if(--(sk->refcount) == 0) |
267 | { |
268 | switch(sk->type) |
269 | { |
270 | case SOCK_STREAM: |
271 | if(sk->outbuf.s.buf != NULL) |
272 | free(sk->outbuf.s.buf); |
273 | if(sk->inbuf.s.buf != NULL) |
274 | free(sk->inbuf.s.buf); |
275 | break; |
276 | case SOCK_DGRAM: |
277 | while((buf = sk->outbuf.d.f) != NULL) |
278 | { |
279 | sk->outbuf.d.f = buf->next; |
280 | free(buf->data); |
161604ea |
281 | free(buf->addr); |
d3372da9 |
282 | free(buf); |
283 | } |
284 | while((buf = sk->inbuf.d.f) != NULL) |
285 | { |
286 | sk->inbuf.d.f = buf->next; |
287 | free(buf->data); |
161604ea |
288 | free(buf->addr); |
d3372da9 |
289 | free(buf); |
290 | } |
291 | break; |
292 | } |
b870805e |
293 | closesock(sk); |
d3372da9 |
294 | if(sk->remote != NULL) |
295 | free(sk->remote); |
296 | free(sk); |
297 | } |
298 | } |
299 | |
336539c2 |
300 | void sockpushdata(struct socket *sk, void *buf, size_t size) |
301 | { |
302 | switch(sk->type) |
303 | { |
304 | case SOCK_STREAM: |
305 | sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + size, 1, 1); |
306 | memmove(sk->inbuf.s.buf + size, sk->inbuf.s.buf, sk->inbuf.s.datasize); |
307 | memcpy(sk->inbuf.s.buf, buf, size); |
308 | sk->inbuf.s.datasize += size; |
309 | break; |
310 | case SOCK_DGRAM: |
311 | /* XXX */ |
312 | break; |
313 | } |
314 | return; |
315 | } |
316 | |
d3372da9 |
317 | void *sockgetinbuf(struct socket *sk, size_t *size) |
318 | { |
319 | void *buf; |
320 | struct dgrambuf *dbuf; |
321 | |
322 | switch(sk->type) |
323 | { |
324 | case SOCK_STREAM: |
325 | if((sk->inbuf.s.buf == NULL) || (sk->inbuf.s.datasize == 0)) |
326 | { |
327 | *size = 0; |
328 | return(NULL); |
329 | } |
330 | buf = sk->inbuf.s.buf; |
331 | *size = sk->inbuf.s.datasize; |
332 | sk->inbuf.s.buf = NULL; |
333 | sk->inbuf.s.bufsize = sk->inbuf.s.datasize = 0; |
334 | return(buf); |
335 | case SOCK_DGRAM: |
336 | if((dbuf = sk->inbuf.d.f) == NULL) |
337 | return(NULL); |
338 | sk->inbuf.d.f = dbuf->next; |
339 | if(dbuf->next == NULL) |
340 | sk->inbuf.d.l = NULL; |
341 | buf = dbuf->data; |
342 | *size = dbuf->size; |
343 | free(dbuf->addr); |
344 | free(dbuf); |
345 | return(buf); |
346 | } |
347 | return(NULL); |
348 | } |
349 | |
9e5f2b29 |
350 | static void recvcmsg(struct socket *sk, struct msghdr *msg) |
351 | { |
352 | struct cmsghdr *cmsg; |
353 | struct ucred *cred; |
354 | |
355 | for(cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL; cmsg = CMSG_NXTHDR(msg, cmsg)) |
356 | { |
357 | if((cmsg->cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type == SCM_CREDENTIALS)) |
358 | { |
359 | if(sk->ucred.pid == 0) |
360 | { |
361 | cred = (struct ucred *)CMSG_DATA(cmsg); |
362 | memcpy(&sk->ucred, cred, sizeof(*cred)); |
9e5f2b29 |
363 | } |
364 | } |
365 | } |
366 | } |
367 | |
d3372da9 |
368 | static void sockrecv(struct socket *sk) |
369 | { |
370 | int ret, inq; |
371 | struct dgrambuf *dbuf; |
9e5f2b29 |
372 | struct msghdr msg; |
373 | char cbuf[65536]; |
374 | struct iovec bufvec; |
d3372da9 |
375 | |
9e5f2b29 |
376 | memset(&msg, 0, sizeof(msg)); |
377 | msg.msg_iov = &bufvec; |
378 | msg.msg_iovlen = 1; |
379 | msg.msg_control = cbuf; |
380 | msg.msg_controllen = sizeof(cbuf); |
d3372da9 |
381 | switch(sk->type) |
382 | { |
383 | case SOCK_STREAM: |
384 | #if defined(HAVE_LINUX_SOCKIOS_H) && defined(SIOCINQ) |
385 | /* SIOCINQ is Linux-specific AFAIK, but I really have no idea |
386 | * how to read the inqueue size on other OSs */ |
387 | if(ioctl(sk->fd, SIOCINQ, &inq)) |
388 | { |
389 | /* I don't really know what could go wrong here, so let's |
390 | * assume it's transient. */ |
391 | flog(LOG_WARNING, "SIOCINQ return %s on socket %i, falling back to 2048 bytes", strerror(errno), sk->fd); |
392 | inq = 2048; |
393 | } |
394 | #else |
395 | inq = 2048; |
396 | #endif |
397 | if(inq > 65536) |
398 | inq = 65536; |
399 | sizebuf(&sk->inbuf.s.buf, &sk->inbuf.s.bufsize, sk->inbuf.s.datasize + inq, 1, 1); |
987518f8 |
400 | if(sk->isrealsocket) |
401 | { |
402 | bufvec.iov_base = sk->inbuf.s.buf + sk->inbuf.s.datasize; |
403 | bufvec.iov_len = inq; |
404 | ret = recvmsg(sk->fd, &msg, 0); |
405 | } else { |
406 | ret = read(sk->fd, sk->inbuf.s.buf + sk->inbuf.s.datasize, inq); |
407 | msg.msg_controllen = 0; |
408 | msg.msg_flags = 0; |
409 | } |
d3372da9 |
410 | if(ret < 0) |
411 | { |
412 | if((errno == EINTR) || (errno == EAGAIN)) |
413 | return; |
cab0b442 |
414 | if(sk->errcb != NULL) |
415 | sk->errcb(sk, errno, sk->data); |
d3372da9 |
416 | closesock(sk); |
417 | return; |
418 | } |
9e5f2b29 |
419 | if(msg.msg_flags & MSG_CTRUNC) |
420 | flog(LOG_DEBUG, "ancillary data was truncated"); |
421 | else |
422 | recvcmsg(sk, &msg); |
d3372da9 |
423 | if(ret == 0) |
424 | { |
cab0b442 |
425 | if(sk->errcb != NULL) |
426 | sk->errcb(sk, 0, sk->data); |
d3372da9 |
427 | closesock(sk); |
428 | return; |
429 | } |
430 | sk->inbuf.s.datasize += ret; |
cab0b442 |
431 | if(sk->readcb != NULL) |
432 | sk->readcb(sk, sk->data); |
d3372da9 |
433 | break; |
434 | case SOCK_DGRAM: |
9e5f2b29 |
435 | #if defined(HAVE_LINUX_SOCKIOS_H) && defined(SIOCINQ) |
d3372da9 |
436 | if(ioctl(sk->fd, SIOCINQ, &inq)) |
437 | { |
438 | /* I don't really know what could go wrong here, so let's |
439 | * assume it's transient. */ |
440 | flog(LOG_WARNING, "SIOCINQ return %s on socket %i", strerror(errno), sk->fd); |
441 | return; |
442 | } |
9e5f2b29 |
443 | #else |
444 | inq = 65536; |
445 | #endif |
d3372da9 |
446 | dbuf = smalloc(sizeof(*dbuf)); |
447 | dbuf->data = smalloc(inq); |
448 | dbuf->addr = smalloc(dbuf->addrlen = sizeof(struct sockaddr_storage)); |
9e5f2b29 |
449 | /* |
d3372da9 |
450 | ret = recvfrom(sk->fd, dbuf->data, inq, 0, dbuf->addr, &dbuf->addrlen); |
9e5f2b29 |
451 | */ |
452 | msg.msg_name = dbuf->addr; |
453 | msg.msg_namelen = dbuf->addrlen; |
454 | bufvec.iov_base = dbuf->data; |
455 | bufvec.iov_len = inq; |
456 | ret = recvmsg(sk->fd, &msg, 0); |
457 | dbuf->addrlen = msg.msg_namelen; |
d3372da9 |
458 | if(ret < 0) |
459 | { |
460 | free(dbuf->addr); |
461 | free(dbuf->data); |
462 | free(dbuf); |
463 | if((errno == EINTR) || (errno == EAGAIN)) |
464 | return; |
cab0b442 |
465 | if(sk->errcb != NULL) |
466 | sk->errcb(sk, errno, sk->data); |
d3372da9 |
467 | closesock(sk); |
468 | return; |
469 | } |
9e5f2b29 |
470 | if(msg.msg_flags & MSG_CTRUNC) |
471 | flog(LOG_DEBUG, "ancillary data was truncated"); |
472 | else |
473 | recvcmsg(sk, &msg); |
d3372da9 |
474 | /* On UDP/IPv[46], ret == 0 doesn't mean EOF (since UDP can't |
475 | * have EOF), but rather an empty packet. I don't know if any |
476 | * other potential DGRAM protocols might have an EOF |
477 | * condition, so let's play safe. */ |
478 | if(ret == 0) |
479 | { |
480 | free(dbuf->addr); |
481 | free(dbuf->data); |
482 | free(dbuf); |
483 | if(!((sk->family == AF_INET) || (sk->family == AF_INET6))) |
484 | { |
cab0b442 |
485 | if(sk->errcb != NULL) |
486 | sk->errcb(sk, 0, sk->data); |
d3372da9 |
487 | closesock(sk); |
488 | } |
489 | return; |
490 | } |
491 | dbuf->addr = srealloc(dbuf->addr, dbuf->addrlen); |
492 | dbuf->data = srealloc(dbuf->data, dbuf->size = ret); |
493 | dbuf->next = NULL; |
494 | if(sk->inbuf.d.l != NULL) |
495 | sk->inbuf.d.l->next = dbuf; |
496 | else |
497 | sk->inbuf.d.f = dbuf; |
498 | sk->inbuf.d.l = dbuf; |
cab0b442 |
499 | if(sk->readcb != NULL) |
500 | sk->readcb(sk, sk->data); |
d3372da9 |
501 | break; |
502 | } |
503 | } |
504 | |
505 | static void sockflush(struct socket *sk) |
506 | { |
507 | int ret; |
508 | struct dgrambuf *dbuf; |
509 | |
510 | switch(sk->type) |
511 | { |
512 | case SOCK_STREAM: |
513 | if(sk->isrealsocket) |
514 | ret = send(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize, MSG_DONTWAIT | MSG_NOSIGNAL); |
515 | else |
516 | ret = write(sk->fd, sk->outbuf.s.buf, sk->outbuf.s.datasize); |
517 | if(ret < 0) |
518 | { |
519 | /* For now, assume transient error, since |
520 | * the socket is polled for errors */ |
521 | break; |
522 | } |
523 | if(ret > 0) |
524 | { |
525 | memmove(sk->outbuf.s.buf, ((char *)sk->outbuf.s.buf) + ret, sk->outbuf.s.datasize -= ret); |
cab0b442 |
526 | if(sk->writecb != NULL) |
527 | sk->writecb(sk, sk->data); |
d3372da9 |
528 | } |
529 | break; |
530 | case SOCK_DGRAM: |
531 | dbuf = sk->outbuf.d.f; |
532 | if((sk->outbuf.d.f = dbuf->next) == NULL) |
533 | sk->outbuf.d.l = NULL; |
534 | sendto(sk->fd, dbuf->data, dbuf->size, MSG_DONTWAIT | MSG_NOSIGNAL, dbuf->addr, dbuf->addrlen); |
535 | free(dbuf->data); |
536 | free(dbuf->addr); |
537 | free(dbuf); |
cab0b442 |
538 | if(sk->writecb != NULL) |
539 | sk->writecb(sk, sk->data); |
d3372da9 |
540 | break; |
541 | } |
542 | } |
543 | |
544 | void closesock(struct socket *sk) |
545 | { |
b870805e |
546 | struct sockaddr_un *un; |
547 | |
548 | if((sk->family == AF_UNIX) && !sockgetlocalname(sk, (struct sockaddr **)&un, NULL) && (un->sun_family == PF_UNIX)) |
549 | { |
7ab36fbd |
550 | if((sk->state == SOCK_LST) && strchr(un->sun_path, '/')) |
b870805e |
551 | { |
552 | if(unlink(un->sun_path)) |
7ab36fbd |
553 | flog(LOG_WARNING, "could not unlink Unix socket %s: %s", un->sun_path, strerror(errno)); |
b870805e |
554 | } |
555 | } |
d3372da9 |
556 | sk->state = SOCK_STL; |
557 | close(sk->fd); |
558 | sk->fd = -1; |
559 | sk->close = 0; |
560 | } |
561 | |
562 | void sockqueue(struct socket *sk, void *data, size_t size) |
563 | { |
564 | struct dgrambuf *new; |
565 | |
566 | if(sk->state == SOCK_STL) |
567 | return; |
568 | switch(sk->type) |
569 | { |
570 | case SOCK_STREAM: |
571 | sizebuf(&(sk->outbuf.s.buf), &(sk->outbuf.s.bufsize), sk->outbuf.s.datasize + size, 1, 1); |
572 | memcpy(sk->outbuf.s.buf + sk->outbuf.s.datasize, data, size); |
573 | sk->outbuf.s.datasize += size; |
574 | break; |
575 | case SOCK_DGRAM: |
576 | if(sk->remote == NULL) |
577 | return; |
578 | new = smalloc(sizeof(*new)); |
579 | new->next = NULL; |
580 | memcpy(new->data = smalloc(size), data, new->size = size); |
581 | memcpy(new->addr = smalloc(sk->remotelen), sk->remote, new->addrlen = sk->remotelen); |
582 | if(sk->outbuf.d.l == NULL) |
583 | { |
584 | sk->outbuf.d.l = sk->outbuf.d.f = new; |
585 | } else { |
586 | sk->outbuf.d.l->next = new; |
587 | sk->outbuf.d.l = new; |
588 | } |
589 | break; |
590 | } |
591 | } |
592 | |
593 | size_t sockgetdatalen(struct socket *sk) |
594 | { |
595 | struct dgrambuf *b; |
596 | size_t ret; |
597 | |
598 | switch(sk->type) |
599 | { |
600 | case SOCK_STREAM: |
601 | ret = sk->inbuf.s.datasize; |
602 | break; |
603 | case SOCK_DGRAM: |
604 | ret = 0; |
605 | for(b = sk->inbuf.d.f; b != NULL; b = b->next) |
606 | ret += b->size; |
607 | break; |
608 | } |
609 | return(ret); |
610 | } |
611 | |
612 | size_t sockqueuesize(struct socket *sk) |
613 | { |
614 | struct dgrambuf *b; |
615 | size_t ret; |
616 | |
617 | switch(sk->type) |
618 | { |
619 | case SOCK_STREAM: |
620 | ret = sk->outbuf.s.datasize; |
621 | break; |
622 | case SOCK_DGRAM: |
623 | ret = 0; |
624 | for(b = sk->outbuf.d.f; b != NULL; b = b->next) |
625 | ret += b->size; |
626 | break; |
627 | } |
628 | return(ret); |
629 | } |
630 | |
d3372da9 |
631 | /* |
7ab36fbd |
632 | * Seriously, I don't know if it's naughty or not to remove |
633 | * pre-existing Unix sockets. |
634 | */ |
635 | static int rebindunix(struct socket *sk, struct sockaddr *name, socklen_t namelen) |
636 | { |
637 | struct sockaddr_un *un; |
638 | struct stat sb; |
639 | |
640 | if((sk->family != AF_UNIX) || (name->sa_family != PF_UNIX)) |
641 | return(-1); |
642 | un = (struct sockaddr_un *)name; |
643 | if(stat(un->sun_path, &sb)) |
644 | return(-1); |
645 | if(!S_ISSOCK(sb.st_mode)) |
646 | return(-1); |
647 | if(unlink(un->sun_path)) |
648 | return(-1); |
649 | if(bind(sk->fd, name, namelen) < 0) |
650 | return(-1); |
651 | return(0); |
652 | } |
653 | |
654 | /* |
d3372da9 |
655 | * The difference between netcslisten() and netcslistenlocal() is that |
656 | * netcslistenlocal() always listens on the local host, instead of |
657 | * following proxy/passive mode directions. It is suitable for eg. the |
658 | * UI channel, while the file sharing networks should, naturally, use |
659 | * netcslisten() instead. |
660 | */ |
661 | |
cab0b442 |
662 | struct socket *netcslistenlocal(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data) |
d3372da9 |
663 | { |
664 | struct socket *sk; |
66c517d2 |
665 | int intbuf; |
d3372da9 |
666 | |
667 | /* I don't know if this is actually correct (it probably isn't), |
668 | * but since, at on least Linux systems, PF_* are specifically |
669 | * #define'd to their AF_* counterparts, it allows for a severely |
670 | * smoother implementation. If it breaks something on your |
671 | * platform, please tell me so. |
672 | */ |
673 | if((sk = mksock(name->sa_family, type)) == NULL) |
674 | return(NULL); |
675 | sk->state = SOCK_LST; |
687b2ee2 |
676 | if(confgetint("net", "reuseaddr")) |
677 | { |
678 | intbuf = 1; |
679 | setsockopt(sk->fd, SOL_SOCKET, SO_REUSEADDR, &intbuf, sizeof(intbuf)); |
680 | } |
7ab36fbd |
681 | if((bind(sk->fd, name, namelen) < 0) && ((errno != EADDRINUSE) || (rebindunix(sk, name, namelen) < 0))) |
d3372da9 |
682 | { |
683 | putsock(sk); |
684 | return(NULL); |
685 | } |
686 | if(listen(sk->fd, 16) < 0) |
687 | { |
688 | putsock(sk); |
689 | return(NULL); |
690 | } |
cab0b442 |
691 | sk->acceptcb = func; |
692 | sk->data = data; |
d3372da9 |
693 | return(sk); |
694 | } |
695 | |
cab0b442 |
696 | struct socket *netcslisten(int type, struct sockaddr *name, socklen_t namelen, void (*func)(struct socket *, struct socket *, void *), void *data) |
c23acc61 |
697 | { |
698 | if(confgetint("net", "mode") == 1) |
699 | { |
700 | errno = EOPNOTSUPP; |
701 | return(NULL); |
702 | } |
c23acc61 |
703 | if(confgetint("net", "mode") == 0) |
704 | return(netcslistenlocal(type, name, namelen, func, data)); |
705 | errno = EOPNOTSUPP; |
706 | return(NULL); |
707 | } |
708 | |
cab0b442 |
709 | struct socket *netcstcplisten(int port, int local, void (*func)(struct socket *, struct socket *, void *), void *data) |
0a1bc5b1 |
710 | { |
711 | struct sockaddr_in addr; |
712 | #ifdef HAVE_IPV6 |
713 | struct sockaddr_in6 addr6; |
714 | #endif |
cab0b442 |
715 | struct socket *(*csfunc)(int, struct sockaddr *, socklen_t, void (*)(struct socket *, struct socket *, void *), void *); |
0a1bc5b1 |
716 | struct socket *ret; |
717 | |
718 | if(local) |
719 | csfunc = netcslistenlocal; |
720 | else |
721 | csfunc = netcslisten; |
722 | #ifdef HAVE_IPV6 |
723 | memset(&addr6, 0, sizeof(addr6)); |
724 | addr6.sin6_family = AF_INET6; |
725 | addr6.sin6_port = htons(port); |
726 | addr6.sin6_addr = in6addr_any; |
727 | if((ret = csfunc(SOCK_STREAM, (struct sockaddr *)&addr6, sizeof(addr6), func, data)) != NULL) |
728 | return(ret); |
729 | if((ret == NULL) && (errno != EAFNOSUPPORT)) |
730 | return(NULL); |
731 | #endif |
732 | memset(&addr, 0, sizeof(addr)); |
733 | addr.sin_family = AF_INET; |
734 | addr.sin_port = htons(port); |
735 | return(csfunc(SOCK_STREAM, (struct sockaddr *)&addr, sizeof(addr), func, data)); |
736 | } |
737 | |
d3372da9 |
738 | struct socket *netcsdgram(struct sockaddr *name, socklen_t namelen) |
739 | { |
740 | struct socket *sk; |
741 | int mode; |
742 | |
743 | mode = confgetint("net", "mode"); |
744 | if((mode == 0) || (mode == 1)) |
745 | { |
746 | if((sk = mksock(name->sa_family, SOCK_DGRAM)) == NULL) |
747 | return(NULL); |
748 | if(bind(sk->fd, name, namelen) < 0) |
749 | { |
750 | putsock(sk); |
751 | return(NULL); |
752 | } |
753 | sk->state = SOCK_EST; |
754 | return(sk); |
755 | } |
756 | errno = EOPNOTSUPP; |
757 | return(NULL); |
758 | } |
759 | |
760 | struct socket *netdupsock(struct socket *sk) |
761 | { |
762 | struct socket *newsk; |
763 | |
764 | newsk = newsock(sk->type); |
765 | if((newsk->fd = dup(sk->fd)) < 0) |
766 | { |
767 | flog(LOG_WARNING, "could not dup() socket: %s", strerror(errno)); |
768 | putsock(newsk); |
769 | return(NULL); |
770 | } |
771 | newsk->state = sk->state; |
772 | newsk->ignread = sk->ignread; |
773 | if(sk->remote != NULL) |
774 | memcpy(newsk->remote = smalloc(sk->remotelen), sk->remote, newsk->remotelen = sk->remotelen); |
775 | return(newsk); |
776 | } |
777 | |
778 | void netdgramconn(struct socket *sk, struct sockaddr *addr, socklen_t addrlen) |
779 | { |
780 | if(sk->remote != NULL) |
781 | free(sk->remote); |
782 | memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen); |
783 | sk->ignread = 1; |
784 | } |
785 | |
cab0b442 |
786 | struct socket *netcsconn(struct sockaddr *addr, socklen_t addrlen, void (*func)(struct socket *, int, void *), void *data) |
d3372da9 |
787 | { |
788 | struct socket *sk; |
789 | int mode; |
790 | |
791 | mode = confgetint("net", "mode"); |
792 | if((mode == 0) || (mode == 1)) |
793 | { |
794 | if((sk = mksock(addr->sa_family, SOCK_STREAM)) == NULL) |
795 | return(NULL); |
796 | memcpy(sk->remote = smalloc(addrlen), addr, sk->remotelen = addrlen); |
797 | if(!connect(sk->fd, addr, addrlen)) |
798 | { |
799 | sk->state = SOCK_EST; |
800 | func(sk, 0, data); |
801 | return(sk); |
802 | } |
803 | if(errno == EINPROGRESS) |
804 | { |
805 | sk->state = SOCK_SYN; |
cab0b442 |
806 | sk->conncb = func; |
807 | sk->data = data; |
d3372da9 |
808 | return(sk); |
809 | } |
810 | putsock(sk); |
811 | return(NULL); |
812 | } |
813 | errno = EOPNOTSUPP; |
814 | return(NULL); |
815 | } |
816 | |
9e5f2b29 |
817 | static void acceptunix(struct socket *sk) |
818 | { |
819 | int buf; |
820 | |
821 | buf = 1; |
822 | if(setsockopt(sk->fd, SOL_SOCKET, SO_PASSCRED, &buf, sizeof(buf)) < 0) |
823 | flog(LOG_WARNING, "could not enable SO_PASSCRED on Unix socket %i: %s", sk->fd, strerror(errno)); |
824 | } |
825 | |
d3372da9 |
826 | int pollsocks(int timeout) |
827 | { |
eaa35963 |
828 | int i, num, ret; |
829 | socklen_t retlen; |
d3372da9 |
830 | int newfd; |
831 | struct pollfd *pfds; |
832 | struct socket *sk, *next, *newsk; |
833 | struct sockaddr_storage ss; |
834 | socklen_t sslen; |
835 | |
836 | pfds = smalloc(sizeof(*pfds) * (num = numsocks)); |
837 | for(i = 0, sk = sockets; i < num; sk = sk->next) |
838 | { |
839 | if(sk->state == SOCK_STL) |
840 | { |
841 | num--; |
842 | continue; |
843 | } |
844 | pfds[i].fd = sk->fd; |
845 | pfds[i].events = 0; |
846 | if(!sk->ignread) |
847 | pfds[i].events |= POLLIN; |
848 | if((sk->state == SOCK_SYN) || (sockqueuesize(sk) > 0)) |
849 | pfds[i].events |= POLLOUT; |
850 | pfds[i].revents = 0; |
851 | i++; |
852 | } |
853 | ret = poll(pfds, num, timeout); |
854 | if(ret < 0) |
855 | { |
856 | if(errno != EINTR) |
857 | { |
858 | flog(LOG_CRIT, "pollsocks: poll errored out: %s", strerror(errno)); |
859 | /* To avoid CPU hogging in case it's bad, which it |
860 | * probably is. */ |
861 | sleep(1); |
862 | } |
863 | free(pfds); |
864 | return(1); |
865 | } |
866 | for(sk = sockets; sk != NULL; sk = next) |
867 | { |
868 | next = sk->next; |
869 | for(i = 0; i < num; i++) |
870 | { |
871 | if(pfds[i].fd == sk->fd) |
872 | break; |
873 | } |
874 | if(i == num) |
875 | continue; |
876 | switch(sk->state) |
877 | { |
878 | case SOCK_LST: |
879 | if(pfds[i].revents & POLLIN) |
880 | { |
881 | sslen = sizeof(ss); |
882 | if((newfd = accept(sk->fd, (struct sockaddr *)&ss, &sslen)) < 0) |
cab0b442 |
883 | { |
884 | if(sk->errcb != NULL) |
885 | sk->errcb(sk, errno, sk->data); |
886 | } |
d3372da9 |
887 | newsk = newsock(sk->type); |
888 | newsk->fd = newfd; |
889 | newsk->family = sk->family; |
890 | newsk->state = SOCK_EST; |
891 | memcpy(newsk->remote = smalloc(sslen), &ss, sslen); |
892 | newsk->remotelen = sslen; |
9e5f2b29 |
893 | if(ss.ss_family == PF_UNIX) |
894 | acceptunix(newsk); |
cab0b442 |
895 | if(sk->acceptcb != NULL) |
896 | sk->acceptcb(sk, newsk, sk->data); |
9e5f2b29 |
897 | putsock(newsk); |
d3372da9 |
898 | } |
899 | if(pfds[i].revents & POLLERR) |
900 | { |
901 | retlen = sizeof(ret); |
902 | getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen); |
cab0b442 |
903 | if(sk->errcb != NULL) |
904 | sk->errcb(sk, ret, sk->data); |
d3372da9 |
905 | continue; |
906 | } |
907 | break; |
908 | case SOCK_SYN: |
909 | if(pfds[i].revents & POLLERR) |
910 | { |
911 | retlen = sizeof(ret); |
912 | getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen); |
cab0b442 |
913 | if(sk->conncb != NULL) |
914 | sk->conncb(sk, ret, sk->data); |
d3372da9 |
915 | closesock(sk); |
916 | continue; |
917 | } |
918 | if(pfds[i].revents & (POLLIN | POLLOUT)) |
919 | { |
920 | sk->state = SOCK_EST; |
cab0b442 |
921 | if(sk->conncb != NULL) |
922 | sk->conncb(sk, 0, sk->data); |
d3372da9 |
923 | } |
924 | break; |
925 | case SOCK_EST: |
926 | if(pfds[i].revents & POLLERR) |
927 | { |
928 | retlen = sizeof(ret); |
929 | getsockopt(sk->fd, SOL_SOCKET, SO_ERROR, &ret, &retlen); |
cab0b442 |
930 | if(sk->errcb != NULL) |
931 | sk->errcb(sk, ret, sk->data); |
d3372da9 |
932 | closesock(sk); |
933 | continue; |
934 | } |
935 | if(pfds[i].revents & POLLIN) |
936 | sockrecv(sk); |
937 | if(pfds[i].revents & POLLOUT) |
938 | { |
939 | if(sockqueuesize(sk) > 0) |
940 | sockflush(sk); |
941 | } |
942 | break; |
943 | } |
944 | if(pfds[i].revents & POLLNVAL) |
945 | { |
946 | flog(LOG_CRIT, "BUG: stale socket struct on fd %i", sk->fd); |
947 | sk->state = SOCK_STL; |
948 | unlinksock(sk); |
949 | continue; |
950 | } |
951 | if(pfds[i].revents & POLLHUP) |
952 | { |
cab0b442 |
953 | if(sk->errcb != NULL) |
954 | sk->errcb(sk, 0, sk->data); |
d3372da9 |
955 | closesock(sk); |
956 | unlinksock(sk); |
957 | continue; |
958 | } |
959 | } |
960 | free(pfds); |
961 | for(sk = sockets; sk != NULL; sk = next) |
962 | { |
963 | next = sk->next; |
964 | if(sk->refcount == 1 && (sockqueuesize(sk) == 0)) |
965 | { |
966 | unlinksock(sk); |
967 | continue; |
968 | } |
969 | if(sk->close && (sockqueuesize(sk) == 0)) |
970 | closesock(sk); |
971 | if(sk->state == SOCK_STL) |
972 | { |
973 | unlinksock(sk); |
974 | continue; |
975 | } |
976 | } |
977 | return(1); |
978 | } |
979 | |
980 | int socksettos(struct socket *sk, int tos) |
981 | { |
b020fb3d |
982 | int buf; |
983 | |
7ab36fbd |
984 | if(sk->family == AF_UNIX) |
985 | return(0); /* Unix sockets are always perfect. :) */ |
d3372da9 |
986 | if(sk->family == AF_INET) |
987 | { |
b020fb3d |
988 | switch(tos) |
989 | { |
b198bed6 |
990 | case 0: |
991 | buf = 0; |
992 | break; |
b020fb3d |
993 | case SOCK_TOS_MINCOST: |
18c1ae1d |
994 | buf = 0x02; |
b020fb3d |
995 | break; |
996 | case SOCK_TOS_MAXREL: |
18c1ae1d |
997 | buf = 0x04; |
b020fb3d |
998 | break; |
999 | case SOCK_TOS_MAXTP: |
18c1ae1d |
1000 | buf = 0x08; |
b020fb3d |
1001 | break; |
1002 | case SOCK_TOS_MINDELAY: |
18c1ae1d |
1003 | buf = 0x10; |
b020fb3d |
1004 | break; |
1005 | default: |
1006 | flog(LOG_WARNING, "attempted to set unknown TOS value %i to IPv4 sock", tos); |
1007 | return(-1); |
1008 | } |
1009 | if(setsockopt(sk->fd, SOL_IP, IP_TOS, &buf, sizeof(buf)) < 0) |
d3372da9 |
1010 | { |
1011 | flog(LOG_WARNING, "could not set sock TOS to %i: %s", tos, strerror(errno)); |
1012 | return(-1); |
1013 | } |
1014 | return(0); |
1015 | } |
b020fb3d |
1016 | if(sk->family == AF_INET6) |
1017 | { |
1018 | switch(tos) |
1019 | { |
b198bed6 |
1020 | case 0: |
1021 | buf = 0; |
b020fb3d |
1022 | case SOCK_TOS_MINCOST: |
1023 | buf = confgetint("net", "diffserv-mincost"); |
1024 | break; |
1025 | case SOCK_TOS_MAXREL: |
1026 | buf = confgetint("net", "diffserv-maxrel"); |
1027 | break; |
1028 | case SOCK_TOS_MAXTP: |
1029 | buf = confgetint("net", "diffserv-maxtp"); |
1030 | break; |
1031 | case SOCK_TOS_MINDELAY: |
1032 | buf = confgetint("net", "diffserv-mindelay"); |
1033 | break; |
1034 | default: |
1035 | flog(LOG_WARNING, "attempted to set unknown TOS value %i to IPv4 sock", tos); |
1036 | return(-1); |
1037 | } |
1038 | /* |
1039 | On Linux, the API IPv6 flow label management doesn't seem to |
1040 | be entirely complete, so I guess this will have to wait. |
1041 | |
1042 | if(setsockopt(...) < 0) |
1043 | { |
1044 | flog(LOG_WARNING, "could not set sock traffic class to %i: %s", tos, strerror(errno)); |
1045 | return(-1); |
1046 | } |
1047 | */ |
1048 | return(0); |
1049 | } |
d3372da9 |
1050 | flog(LOG_WARNING, "could not set TOS on sock of family %i", sk->family); |
1051 | return(1); |
1052 | } |
1053 | |
1054 | struct resolvedata |
1055 | { |
1056 | int fd; |
1057 | void (*callback)(struct sockaddr *addr, int addrlen, void *data); |
1058 | void *data; |
1059 | struct sockaddr_storage addr; |
1060 | int addrlen; |
1061 | }; |
1062 | |
1063 | static void resolvecb(pid_t pid, int status, struct resolvedata *data) |
1064 | { |
1065 | static char buf[80]; |
1066 | int ret; |
1067 | struct sockaddr_in *ipv4; |
1068 | |
1069 | if(!status) |
1070 | { |
1071 | if((ret = read(data->fd, buf, sizeof(buf))) != 4) |
1072 | { |
1073 | errno = ENONET; |
1074 | data->callback(NULL, 0, data->data); |
1075 | } else { |
1076 | ipv4 = (struct sockaddr_in *)&data->addr; |
1077 | memcpy(&ipv4->sin_addr, buf, 4); |
1078 | data->callback((struct sockaddr *)ipv4, sizeof(*ipv4), data->data); |
1079 | } |
1080 | } else { |
1081 | errno = ENONET; |
1082 | data->callback(NULL, 0, data->data); |
1083 | } |
1084 | close(data->fd); |
1085 | free(data); |
1086 | } |
1087 | |
1088 | int netresolve(char *addr, void (*callback)(struct sockaddr *addr, int addrlen, void *data), void *data) |
1089 | { |
1090 | int i; |
1091 | char *p; |
1092 | int port; |
1093 | int pfd[2]; |
1094 | pid_t child; |
1095 | struct resolvedata *rdata; |
1096 | struct sockaddr_in ipv4; |
1097 | struct hostent *he; |
1098 | sigset_t sigset; |
1099 | |
1100 | /* IPv4 */ |
1101 | port = -1; |
1102 | if((p = strchr(addr, ':')) != NULL) |
1103 | { |
1104 | *p = 0; |
1105 | port = atoi(p + 1); |
1106 | } |
1107 | ipv4.sin_family = AF_INET; |
1108 | ipv4.sin_port = htons(port); |
1109 | if(inet_aton(addr, &ipv4.sin_addr)) |
1110 | { |
1111 | callback((struct sockaddr *)&ipv4, sizeof(ipv4), data); |
1112 | } else { |
1113 | sigemptyset(&sigset); |
1114 | sigaddset(&sigset, SIGCHLD); |
1115 | sigprocmask(SIG_BLOCK, &sigset, NULL); |
1116 | if((pipe(pfd) < 0) || ((child = fork()) < 0)) |
1117 | { |
1118 | sigprocmask(SIG_UNBLOCK, &sigset, NULL); |
1119 | return(-1); |
1120 | } |
1121 | if(child == 0) |
1122 | { |
1123 | sigprocmask(SIG_UNBLOCK, &sigset, NULL); |
1124 | for(i = 3; i < FD_SETSIZE; i++) |
1125 | { |
1126 | if(i != pfd[1]) |
1127 | close(i); |
1128 | } |
1129 | signal(SIGALRM, SIG_DFL); |
1130 | alarm(30); |
1131 | if((he = gethostbyname(addr)) == NULL) |
1132 | exit(1); |
1133 | write(pfd[1], he->h_addr_list[0], 4); |
1134 | exit(0); |
1135 | } else { |
1136 | close(pfd[1]); |
1137 | fcntl(pfd[0], F_SETFL, fcntl(pfd[0], F_GETFL) | O_NONBLOCK); |
1138 | rdata = smalloc(sizeof(*rdata)); |
1139 | rdata->fd = pfd[0]; |
1140 | rdata->callback = callback; |
1141 | rdata->data = data; |
1142 | memcpy(&rdata->addr, &ipv4, rdata->addrlen = sizeof(ipv4)); |
1143 | childcallback(child, (void (*)(pid_t, int, void *))resolvecb, rdata); |
1144 | sigprocmask(SIG_UNBLOCK, &sigset, NULL); |
1145 | return(1); |
1146 | } |
1147 | } |
1148 | return(0); |
1149 | } |
1150 | |
1151 | int sockgetlocalname(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf) |
1152 | { |
1153 | socklen_t len; |
1154 | struct sockaddr_storage name; |
1155 | |
1156 | *namebuf = NULL; |
1157 | if((sk->state == SOCK_STL) || (sk->fd < 0)) |
1158 | return(-1); |
1159 | len = sizeof(name); |
1160 | if(getsockname(sk->fd, (struct sockaddr *)&name, &len) < 0) |
1161 | { |
6fb0c6ac |
1162 | flog(LOG_ERR, "BUG: alive socket with dead fd in sockgetlocalname (%s)", strerror(errno)); |
d3372da9 |
1163 | return(-1); |
1164 | } |
1165 | *namebuf = memcpy(smalloc(len), &name, len); |
b870805e |
1166 | if(lenbuf != NULL) |
1167 | *lenbuf = len; |
d3372da9 |
1168 | return(0); |
1169 | } |
1170 | |
eaa35963 |
1171 | static void sethostaddr(struct sockaddr *dst, struct sockaddr *src) |
1172 | { |
1173 | if(dst->sa_family != src->sa_family) |
1174 | { |
1175 | flog(LOG_ERR, "BUG: non-matching socket families in sethostaddr (%i -> %i)", src->sa_family, dst->sa_family); |
1176 | return; |
1177 | } |
1178 | switch(src->sa_family) |
1179 | { |
1180 | case AF_INET: |
1181 | ((struct sockaddr_in *)dst)->sin_addr = ((struct sockaddr_in *)src)->sin_addr; |
1182 | break; |
1183 | case AF_INET6: |
1184 | ((struct sockaddr_in6 *)dst)->sin6_addr = ((struct sockaddr_in6 *)src)->sin6_addr; |
1185 | break; |
1186 | default: |
1187 | flog(LOG_WARNING, "sethostaddr unimplemented for family %i", src->sa_family); |
1188 | break; |
1189 | } |
1190 | } |
1191 | |
1192 | static int makepublic(struct sockaddr *addr) |
d3372da9 |
1193 | { |
6fb0c6ac |
1194 | int ret; |
eaa35963 |
1195 | socklen_t plen; |
1196 | struct sockaddr *pname; |
1197 | |
1198 | if((ret = getpublicaddr(addr->sa_family, &pname, &plen)) < 0) |
1199 | { |
1200 | flog(LOG_ERR, "could not get public address: %s", strerror(errno)); |
1201 | return(-1); |
1202 | } |
1203 | if(ret) |
1204 | return(0); |
1205 | sethostaddr(addr, pname); |
1206 | free(pname); |
1207 | return(0); |
1208 | } |
1209 | |
1210 | int sockgetremotename(struct socket *sk, struct sockaddr **namebuf, socklen_t *lenbuf) |
1211 | { |
d3372da9 |
1212 | socklen_t len; |
6fb0c6ac |
1213 | struct sockaddr *name; |
d3372da9 |
1214 | |
1215 | switch(confgetint("net", "mode")) |
1216 | { |
1217 | case 0: |
1218 | *namebuf = NULL; |
1219 | if((sk->state == SOCK_STL) || (sk->fd < 0)) |
d3372da9 |
1220 | { |
eaa35963 |
1221 | errno = EBADF; |
d3372da9 |
1222 | return(-1); |
1223 | } |
6fb0c6ac |
1224 | if(!sockgetlocalname(sk, &name, &len)) |
1225 | { |
1226 | *namebuf = name; |
1227 | *lenbuf = len; |
eaa35963 |
1228 | makepublic(name); |
6fb0c6ac |
1229 | return(0); |
1230 | } |
1231 | flog(LOG_ERR, "could not get remotely accessible name by any means"); |
1232 | return(-1); |
d3372da9 |
1233 | case 1: |
1234 | errno = EOPNOTSUPP; |
1235 | return(-1); |
1236 | default: |
1237 | flog(LOG_CRIT, "unknown net mode %i active", confgetint("net", "mode")); |
1238 | errno = EOPNOTSUPP; |
1239 | return(-1); |
1240 | } |
1241 | } |
1242 | |
d29853b1 |
1243 | int sockgetremotename2(struct socket *sk, struct socket *sk2, struct sockaddr **namebuf, socklen_t *lenbuf) |
1244 | { |
1245 | struct sockaddr *name1, *name2; |
1246 | socklen_t len1, len2; |
1247 | |
1248 | if(sk->family != sk2->family) |
1249 | { |
1250 | flog(LOG_ERR, "using sockgetremotename2 with sockets of differing family: %i %i", sk->family, sk2->family); |
1251 | return(-1); |
1252 | } |
3804379d |
1253 | if(sockgetremotename(sk, &name1, &len1)) |
d29853b1 |
1254 | return(-1); |
3804379d |
1255 | if(sockgetremotename(sk2, &name2, &len2)) { |
d29853b1 |
1256 | free(name1); |
1257 | return(-1); |
1258 | } |
1259 | sethostaddr(name1, name2); |
1260 | free(name2); |
1261 | *namebuf = name1; |
1262 | *lenbuf = len1; |
1263 | return(0); |
1264 | } |
1265 | |
99a28d47 |
1266 | int addreq(struct sockaddr *x, struct sockaddr *y) |
1267 | { |
1268 | struct sockaddr_un *u1, *u2; |
1269 | struct sockaddr_in *n1, *n2; |
1270 | #ifdef HAVE_IPV6 |
1271 | struct sockaddr_in6 *s1, *s2; |
1272 | #endif |
1273 | |
1274 | if(x->sa_family != y->sa_family) |
1275 | return(0); |
1276 | switch(x->sa_family) { |
1277 | case AF_UNIX: |
1278 | u1 = (struct sockaddr_un *)x; u2 = (struct sockaddr_un *)y; |
1279 | if(strncmp(u1->sun_path, u2->sun_path, sizeof(u1->sun_path))) |
1280 | return(0); |
1281 | break; |
1282 | case AF_INET: |
1283 | n1 = (struct sockaddr_in *)x; n2 = (struct sockaddr_in *)y; |
1284 | if(n1->sin_port != n2->sin_port) |
1285 | return(0); |
1286 | if(n1->sin_addr.s_addr != n2->sin_addr.s_addr) |
1287 | return(0); |
1288 | break; |
c8a729d7 |
1289 | #ifdef HAVE_IPV6 |
99a28d47 |
1290 | case AF_INET6: |
1291 | s1 = (struct sockaddr_in6 *)x; s2 = (struct sockaddr_in6 *)y; |
1292 | if(s1->sin6_port != s2->sin6_port) |
1293 | return(0); |
1294 | if(memcmp(s1->sin6_addr.s6_addr, s2->sin6_addr.s6_addr, sizeof(s1->sin6_addr.s6_addr))) |
1295 | return(0); |
1296 | break; |
c8a729d7 |
1297 | #endif |
99a28d47 |
1298 | } |
1299 | return(1); |
1300 | } |
1301 | |
d3372da9 |
1302 | char *formataddress(struct sockaddr *arg, socklen_t arglen) |
1303 | { |
d3372da9 |
1304 | struct sockaddr_in *ipv4; |
1305 | #ifdef HAVE_IPV6 |
1306 | struct sockaddr_in6 *ipv6; |
1307 | #endif |
1308 | static char *ret = NULL; |
1309 | char buf[1024]; |
1310 | |
1311 | if(ret != NULL) |
1312 | free(ret); |
1313 | ret = NULL; |
1314 | switch(arg->sa_family) |
1315 | { |
1316 | case AF_UNIX: |
6a97462e |
1317 | ret = sstrdup("Unix socket"); |
d3372da9 |
1318 | break; |
1319 | case AF_INET: |
1320 | ipv4 = (struct sockaddr_in *)arg; |
1321 | if(inet_ntop(AF_INET, &ipv4->sin_addr, buf, sizeof(buf)) == NULL) |
1322 | return(NULL); |
1323 | ret = sprintf2("%s:%i", buf, (int)ntohs(ipv4->sin_port)); |
1324 | break; |
1325 | #ifdef HAVE_IPV6 |
1326 | case AF_INET6: |
1327 | ipv6 = (struct sockaddr_in6 *)arg; |
1328 | if(inet_ntop(AF_INET6, &ipv6->sin6_addr, buf, sizeof(buf)) == NULL) |
1329 | return(NULL); |
fc7f7735 |
1330 | ret = sprintf2("[%s]:%i", buf, (int)ntohs(ipv6->sin6_port)); |
d3372da9 |
1331 | break; |
1332 | #endif |
1333 | default: |
1334 | errno = EPFNOSUPPORT; |
1335 | break; |
1336 | } |
1337 | return(ret); |
1338 | } |
1339 | |
1340 | #if 0 |
1341 | |
1342 | /* |
1343 | * It was very nice to use this, but it seems |
1344 | * to mess things up, so I guess it has to go... :-( |
1345 | */ |
1346 | |
1347 | static int formataddress(FILE *stream, const struct printf_info *info, const void *const *args) |
1348 | { |
1349 | struct sockaddr *arg; |
1350 | socklen_t arglen; |
1351 | struct sockaddr_un *UNIX; /* Some wise guy has #defined unix with |
1352 | * lowercase letters to 1, so I do this |
1353 | * instead. */ |
1354 | struct sockaddr_in *ipv4; |
1355 | int ret; |
1356 | |
1357 | arg = *(struct sockaddr **)(args[0]); |
1358 | arglen = *(socklen_t *)(args[1]); |
1359 | switch(arg->sa_family) |
1360 | { |
1361 | case AF_UNIX: |
1362 | UNIX = (struct sockaddr_un *)arg; |
1363 | ret = fprintf(stream, "%s", UNIX->sun_path); |
1364 | break; |
1365 | case AF_INET: |
1366 | ipv4 = (struct sockaddr_in *)arg; |
1367 | ret = fprintf(stream, "%s:%i", inet_ntoa(ipv4->sin_addr), (int)ntohs(ipv4->sin_port)); |
1368 | break; |
1369 | default: |
1370 | ret = -1; |
1371 | errno = EPFNOSUPPORT; |
1372 | break; |
1373 | } |
1374 | return(ret); |
1375 | } |
1376 | |
1377 | static int formataddress_arginfo(const struct printf_info *info, size_t n, int *argtypes) |
1378 | { |
1379 | if(n > 0) |
1380 | argtypes[0] = PA_POINTER; |
1381 | if(n > 1) |
1382 | argtypes[1] = PA_INT; /* Sources tell me that socklen_t _must_ |
1383 | * be an int, so I guess this should be |
1384 | * safe. */ |
1385 | return(2); |
1386 | } |
1387 | #endif |
1388 | |
1389 | static int init(int hup) |
1390 | { |
1391 | if(!hup) |
1392 | { |
1393 | /* |
1394 | if(register_printf_function('N', formataddress, formataddress_arginfo)) |
1395 | { |
1396 | flog(LOG_CRIT, "could not register printf handler %%N: %s", strerror(errno)); |
1397 | return(1); |
1398 | } |
1399 | */ |
1400 | } |
1401 | return(0); |
1402 | } |
1403 | |
1404 | static void terminate(void) |
1405 | { |
1406 | while(sockets != NULL) |
1407 | unlinksock(sockets); |
1408 | } |
1409 | |
1410 | static struct module me = |
1411 | { |
1412 | .name = "net", |
1413 | .conf = |
1414 | { |
1415 | .vars = myvars |
1416 | }, |
1417 | .init = init, |
1418 | .terminate = terminate |
1419 | }; |
1420 | |
1421 | MODULE(me) |