1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2018 ircd-hybrid development team |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file s_bsd.c |
23 |
* \brief Network functions. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include <netinet/in_systm.h> |
29 |
#include <netinet/ip.h> |
30 |
#include <netinet/tcp.h> |
31 |
#include "list.h" |
32 |
#include "fdlist.h" |
33 |
#include "s_bsd.h" |
34 |
#include "client.h" |
35 |
#include "dbuf.h" |
36 |
#include "event.h" |
37 |
#include "irc_string.h" |
38 |
#include "ircd.h" |
39 |
#include "listener.h" |
40 |
#include "numeric.h" |
41 |
#include "packet.h" |
42 |
#include "res.h" |
43 |
#include "restart.h" |
44 |
#include "conf.h" |
45 |
#include "log.h" |
46 |
#include "server.h" |
47 |
#include "send.h" |
48 |
#include "memory.h" |
49 |
#include "user.h" |
50 |
|
51 |
|
52 |
static const char *const comm_err_str[] = |
53 |
{ |
54 |
[COMM_OK] = "Comm OK", |
55 |
[COMM_ERR_BIND] = "Error during bind()", |
56 |
[COMM_ERR_DNS] = "Error during DNS lookup", |
57 |
[COMM_ERR_TIMEOUT] = "connect timeout", |
58 |
[COMM_ERR_CONNECT] = "Error during connect()", |
59 |
[COMM_ERROR] = "Comm Error" |
60 |
}; |
61 |
|
62 |
static void comm_connect_callback(fde_t *, int); |
63 |
static void comm_connect_timeout(fde_t *, void *); |
64 |
static void comm_connect_dns_callback(void *, const struct irc_ssaddr *, const char *, size_t); |
65 |
static void comm_connect_tryconnect(fde_t *, void *); |
66 |
|
67 |
|
68 |
/* comm_get_sockerr - get the error value from the socket or the current errno |
69 |
* |
70 |
* Get the *real* error from the socket (well try to anyway..). |
71 |
* This may only work when SO_DEBUG is enabled but its worth the |
72 |
* gamble anyway. |
73 |
*/ |
74 |
int |
75 |
comm_get_sockerr(int fd) |
76 |
{ |
77 |
int errtmp = errno; |
78 |
#ifdef SO_ERROR |
79 |
int err = 0; |
80 |
socklen_t len = sizeof(err); |
81 |
|
82 |
if (fd > -1 && !getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len)) |
83 |
{ |
84 |
if (err) |
85 |
errtmp = err; |
86 |
} |
87 |
|
88 |
errno = errtmp; |
89 |
#endif |
90 |
return errtmp; |
91 |
} |
92 |
|
93 |
/* |
94 |
* report_error - report an error from an errno. |
95 |
* Record error to log and also send a copy to all *LOCAL* opers online. |
96 |
* |
97 |
* text is a *format* string for outputing error. It must |
98 |
* contain only two '%s', the first will be replaced |
99 |
* by the sockhost from the client_p, and the latter will |
100 |
* be taken from sys_errlist[errno]. |
101 |
* |
102 |
* client_p if not NULL, is the *LOCAL* client associated with |
103 |
* the error. |
104 |
* |
105 |
* Cannot use perror() within daemon. stderr is closed in |
106 |
* ircd and cannot be used. And, worse yet, it might have |
107 |
* been reassigned to a normal connection... |
108 |
* |
109 |
* Actually stderr is still there IFF ircd was run with -s --Rodder |
110 |
*/ |
111 |
void |
112 |
report_error(int level, const char *text, const char *who, int error) |
113 |
{ |
114 |
who = (who) ? who : ""; |
115 |
|
116 |
sendto_realops_flags(UMODE_DEBUG, level, SEND_NOTICE, |
117 |
text, who, strerror(error)); |
118 |
ilog(LOG_TYPE_IRCD, text, who, strerror(error)); |
119 |
} |
120 |
|
121 |
/* |
122 |
* setup_socket() |
123 |
* |
124 |
* Set the socket non-blocking, and other wonderful bits. |
125 |
*/ |
126 |
static void |
127 |
setup_socket(int fd) |
128 |
{ |
129 |
int opt = 1; |
130 |
|
131 |
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt)); |
132 |
|
133 |
#ifdef IPTOS_LOWDELAY |
134 |
opt = IPTOS_LOWDELAY; |
135 |
setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)); |
136 |
#endif |
137 |
|
138 |
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); |
139 |
} |
140 |
|
141 |
/* |
142 |
* ssl_handshake - let OpenSSL initialize the protocol. Register for |
143 |
* read/write events if necessary. |
144 |
*/ |
145 |
static void |
146 |
ssl_handshake(fde_t *F, void *data) |
147 |
{ |
148 |
struct Client *client_p = data; |
149 |
|
150 |
assert(client_p); |
151 |
assert(client_p->connection); |
152 |
assert(client_p->connection->fd); |
153 |
assert(client_p->connection->fd == F); |
154 |
|
155 |
tls_handshake_status_t ret = tls_handshake(&F->ssl, TLS_ROLE_SERVER, NULL); |
156 |
if (ret != TLS_HANDSHAKE_DONE) |
157 |
{ |
158 |
if ((CurrentTime - client_p->connection->firsttime) > CONNECTTIMEOUT) |
159 |
{ |
160 |
exit_client(client_p, "Timeout during TLS handshake"); |
161 |
return; |
162 |
} |
163 |
|
164 |
switch (ret) |
165 |
{ |
166 |
case TLS_HANDSHAKE_WANT_WRITE: |
167 |
comm_setselect(F, COMM_SELECT_WRITE, ssl_handshake, client_p, CONNECTTIMEOUT); |
168 |
return; |
169 |
case TLS_HANDSHAKE_WANT_READ: |
170 |
comm_setselect(F, COMM_SELECT_READ, ssl_handshake, client_p, CONNECTTIMEOUT); |
171 |
return; |
172 |
default: |
173 |
exit_client(client_p, "Error during TLS handshake"); |
174 |
return; |
175 |
} |
176 |
} |
177 |
|
178 |
comm_settimeout(F, 0, NULL, NULL); |
179 |
|
180 |
if (tls_verify_cert(&F->ssl, ConfigServerInfo.message_digest_algorithm, &client_p->certfp) == false) |
181 |
ilog(LOG_TYPE_IRCD, "Client %s!%s@%s gave bad TLS client certificate", |
182 |
client_p->name, client_p->username, client_p->host); |
183 |
|
184 |
auth_start(client_p); |
185 |
} |
186 |
|
187 |
/* |
188 |
* add_connection - creates a client which has just connected to us on |
189 |
* the given fd. The sockhost field is initialized with the ip# of the host. |
190 |
* An unique id is calculated now, in case it is needed for auth. |
191 |
* The client is sent to the auth module for verification, and not put in |
192 |
* any client list yet. |
193 |
*/ |
194 |
void |
195 |
add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd) |
196 |
{ |
197 |
struct Client *client_p = client_make(NULL); |
198 |
|
199 |
client_p->connection->fd = fd_open(fd, true, (listener->flags & LISTENER_SSL) ? |
200 |
"Incoming SSL connection" : "Incoming connection"); |
201 |
|
202 |
/* |
203 |
* copy address to 'sockhost' as a string, copy it to host too |
204 |
* so we have something valid to put into error messages... |
205 |
*/ |
206 |
memcpy(&client_p->ip, irn, sizeof(client_p->ip)); |
207 |
|
208 |
getnameinfo((const struct sockaddr *)&client_p->ip, |
209 |
client_p->ip.ss_len, client_p->sockhost, |
210 |
sizeof(client_p->sockhost), NULL, 0, NI_NUMERICHOST); |
211 |
|
212 |
if (client_p->sockhost[0] == ':') |
213 |
{ |
214 |
client_p->sockhost[0] = '0'; |
215 |
memmove(client_p->sockhost + 1, client_p->sockhost, sizeof(client_p->sockhost) - 1); |
216 |
} |
217 |
|
218 |
strlcpy(client_p->host, client_p->sockhost, sizeof(client_p->host)); |
219 |
|
220 |
client_p->connection->listener = listener; |
221 |
++listener->ref_count; |
222 |
|
223 |
if (listener->flags & LISTENER_SSL) |
224 |
{ |
225 |
if (tls_new(&client_p->connection->fd->ssl, fd, TLS_ROLE_SERVER) == false) |
226 |
{ |
227 |
SetDead(client_p); |
228 |
exit_client(client_p, "TLS context initialization failed"); |
229 |
return; |
230 |
} |
231 |
|
232 |
AddFlag(client_p, FLAGS_SSL); |
233 |
ssl_handshake(client_p->connection->fd, client_p); |
234 |
} |
235 |
else |
236 |
auth_start(client_p); |
237 |
} |
238 |
|
239 |
/* |
240 |
* stolen from squid - its a neat (but overused! :) routine which we |
241 |
* can use to see whether we can ignore this errno or not. It is |
242 |
* generally useful for non-blocking network IO related errnos. |
243 |
* -- adrian |
244 |
*/ |
245 |
bool |
246 |
comm_ignore_errno(int ierrno) |
247 |
{ |
248 |
switch (ierrno) |
249 |
{ |
250 |
case EINPROGRESS: |
251 |
case EWOULDBLOCK: |
252 |
#if EAGAIN != EWOULDBLOCK |
253 |
case EAGAIN: |
254 |
#endif |
255 |
case EALREADY: |
256 |
case EINTR: |
257 |
#ifdef ERESTART |
258 |
case ERESTART: |
259 |
#endif |
260 |
return true; |
261 |
default: |
262 |
return false; |
263 |
} |
264 |
} |
265 |
|
266 |
/* |
267 |
* comm_settimeout() - set the socket timeout |
268 |
* |
269 |
* Set the timeout for the fd |
270 |
*/ |
271 |
void |
272 |
comm_settimeout(fde_t *F, uintmax_t timeout, void (*callback)(fde_t *, void *), void *cbdata) |
273 |
{ |
274 |
assert(F->flags.open == true); |
275 |
|
276 |
F->timeout = CurrentTime + (timeout / 1000); |
277 |
F->timeout_handler = callback; |
278 |
F->timeout_data = cbdata; |
279 |
} |
280 |
|
281 |
/* |
282 |
* comm_setflush() - set a flush function |
283 |
* |
284 |
* A flush function is simply a function called if found during |
285 |
* comm_timeouts(). Its basically a second timeout, except in this case |
286 |
* I'm too lazy to implement multiple timeout functions! :-) |
287 |
* its kinda nice to have it separate, since this is designed for |
288 |
* flush functions, and when comm_close() is implemented correctly |
289 |
* with close functions, we _actually_ don't call comm_close() here .. |
290 |
* -- originally Adrian's notes |
291 |
* comm_close() is replaced with fd_close() in fdlist.c |
292 |
*/ |
293 |
void |
294 |
comm_setflush(fde_t *F, uintmax_t timeout, void (*callback)(fde_t *, void *), void *cbdata) |
295 |
{ |
296 |
assert(F->flags.open == true); |
297 |
|
298 |
F->flush_timeout = CurrentTime + (timeout / 1000); |
299 |
F->flush_handler = callback; |
300 |
F->flush_data = cbdata; |
301 |
} |
302 |
|
303 |
/* |
304 |
* comm_checktimeouts() - check the socket timeouts |
305 |
* |
306 |
* All this routine does is call the given callback/cbdata, without closing |
307 |
* down the file descriptor. When close handlers have been implemented, |
308 |
* this will happen. |
309 |
*/ |
310 |
void |
311 |
comm_checktimeouts(void *unused) |
312 |
{ |
313 |
void (*hdl)(fde_t *, void *); |
314 |
void *data; |
315 |
|
316 |
for (int fd = 0; fd <= highest_fd; ++fd) |
317 |
{ |
318 |
fde_t *F = &fd_table[fd]; |
319 |
|
320 |
if (F->flags.open == false) |
321 |
continue; |
322 |
|
323 |
/* check flush functions */ |
324 |
if (F->flush_handler && F->flush_timeout > 0 && |
325 |
F->flush_timeout < CurrentTime) |
326 |
{ |
327 |
hdl = F->flush_handler; |
328 |
data = F->flush_data; |
329 |
|
330 |
comm_setflush(F, 0, NULL, NULL); |
331 |
hdl(F, data); |
332 |
} |
333 |
|
334 |
/* check timeouts */ |
335 |
if (F->timeout_handler && F->timeout > 0 && |
336 |
F->timeout < CurrentTime) |
337 |
{ |
338 |
/* Call timeout handler */ |
339 |
hdl = F->timeout_handler; |
340 |
data = F->timeout_data; |
341 |
|
342 |
comm_settimeout(F, 0, NULL, NULL); |
343 |
hdl(F, data); |
344 |
} |
345 |
} |
346 |
} |
347 |
|
348 |
/* |
349 |
* void comm_connect_tcp(int fd, const char *host, unsigned short port, |
350 |
* struct sockaddr *clocal, int socklen, |
351 |
* CNCB *callback, void *data, int aftype, int timeout) |
352 |
* Input: An fd to connect with, a host and port to connect to, |
353 |
* a local sockaddr to connect from + length(or NULL to use the |
354 |
* default), a callback, the data to pass into the callback, the |
355 |
* address family. |
356 |
* Output: None. |
357 |
* Side-effects: A non-blocking connection to the host is started, and |
358 |
* if necessary, set up for selection. The callback given |
359 |
* may be called now, or it may be called later. |
360 |
*/ |
361 |
void |
362 |
comm_connect_tcp(fde_t *F, const char *host, unsigned short port, struct sockaddr *clocal, |
363 |
int socklen, void (*callback)(fde_t *, int, void *), void *data, |
364 |
int aftype, uintmax_t timeout) |
365 |
{ |
366 |
struct addrinfo hints, *res; |
367 |
char portname[PORTNAMELEN + 1]; |
368 |
|
369 |
assert(callback); |
370 |
F->connect.callback = callback; |
371 |
F->connect.data = data; |
372 |
|
373 |
F->connect.hostaddr.ss.ss_family = aftype; |
374 |
F->connect.hostaddr.ss_port = htons(port); |
375 |
|
376 |
/* Note that we're using a passed sockaddr here. This is because |
377 |
* generally you'll be bind()ing to a sockaddr grabbed from |
378 |
* getsockname(), so this makes things easier. |
379 |
* XXX If NULL is passed as local, we should later on bind() to the |
380 |
* virtual host IP, for completeness. |
381 |
* -- adrian |
382 |
*/ |
383 |
if (clocal && bind(F->fd, clocal, socklen) < 0) |
384 |
{ |
385 |
/* Failure, call the callback with COMM_ERR_BIND */ |
386 |
comm_connect_callback(F, COMM_ERR_BIND); |
387 |
return; /* ... and quit */ |
388 |
} |
389 |
|
390 |
memset(&hints, 0, sizeof(hints)); |
391 |
|
392 |
hints.ai_family = AF_UNSPEC; |
393 |
hints.ai_socktype = SOCK_STREAM; |
394 |
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; |
395 |
|
396 |
snprintf(portname, sizeof(portname), "%d", port); |
397 |
|
398 |
/* |
399 |
* Next, if we have been given an IP address, get the address and skip the |
400 |
* DNS check (and head direct to comm_connect_tryconnect()). |
401 |
*/ |
402 |
if (getaddrinfo(host, portname, &hints, &res)) |
403 |
{ |
404 |
/* Send the DNS request, for the next level */ |
405 |
if (aftype == AF_INET6) |
406 |
gethost_byname_type(comm_connect_dns_callback, F, host, T_AAAA); |
407 |
else |
408 |
gethost_byname_type(comm_connect_dns_callback, F, host, T_A); |
409 |
} |
410 |
else |
411 |
{ |
412 |
/* We have a valid IP, so we just call tryconnect */ |
413 |
/* Make sure we actually set the timeout here .. */ |
414 |
assert(res); |
415 |
|
416 |
memcpy(&F->connect.hostaddr, res->ai_addr, res->ai_addrlen); |
417 |
F->connect.hostaddr.ss_len = res->ai_addrlen; |
418 |
F->connect.hostaddr.ss.ss_family = res->ai_family; |
419 |
|
420 |
freeaddrinfo(res); |
421 |
|
422 |
comm_settimeout(F, timeout * 1000, comm_connect_timeout, NULL); |
423 |
comm_connect_tryconnect(F, NULL); |
424 |
} |
425 |
} |
426 |
|
427 |
/* |
428 |
* comm_connect_callback() - call the callback, and continue with life |
429 |
*/ |
430 |
static void |
431 |
comm_connect_callback(fde_t *F, int status) |
432 |
{ |
433 |
void (*hdl)(fde_t *, int, void *); |
434 |
|
435 |
/* This check is gross..but probably necessary */ |
436 |
if (F->connect.callback == NULL) |
437 |
return; |
438 |
|
439 |
/* Clear the connect flag + handler */ |
440 |
hdl = F->connect.callback; |
441 |
F->connect.callback = NULL; |
442 |
|
443 |
/* Clear the timeout handler */ |
444 |
comm_settimeout(F, 0, NULL, NULL); |
445 |
|
446 |
/* Call the handler */ |
447 |
hdl(F, status, F->connect.data); |
448 |
} |
449 |
|
450 |
/* |
451 |
* comm_connect_timeout() - this gets called when the socket connection |
452 |
* times out. This *only* can be called once connect() is initially |
453 |
* called .. |
454 |
*/ |
455 |
static void |
456 |
comm_connect_timeout(fde_t *F, void *unused) |
457 |
{ |
458 |
/* error! */ |
459 |
comm_connect_callback(F, COMM_ERR_TIMEOUT); |
460 |
} |
461 |
|
462 |
/* |
463 |
* comm_connect_dns_callback() - called at the completion of the DNS request |
464 |
* |
465 |
* The DNS request has completed, so if we've got an error, return it, |
466 |
* otherwise we initiate the connect() |
467 |
*/ |
468 |
static void |
469 |
comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name, size_t namelength) |
470 |
{ |
471 |
fde_t *const F = vptr; |
472 |
|
473 |
if (addr == NULL) |
474 |
{ |
475 |
comm_connect_callback(F, COMM_ERR_DNS); |
476 |
return; |
477 |
} |
478 |
|
479 |
/* No error, set a 30 second timeout */ |
480 |
comm_settimeout(F, 30 * 1000, comm_connect_timeout, NULL); |
481 |
|
482 |
/* Copy over the DNS reply info so we can use it in the connect() */ |
483 |
/* |
484 |
* Note we don't fudge the refcount here, because we aren't keeping |
485 |
* the DNS record around, and the DNS cache is gone anyway.. |
486 |
* -- adrian |
487 |
*/ |
488 |
memcpy(&F->connect.hostaddr, addr, addr->ss_len); |
489 |
|
490 |
/* The cast is hacky, but safe - port offset is same on v4 and v6 */ |
491 |
((struct sockaddr_in *)&F->connect.hostaddr)->sin_port = F->connect.hostaddr.ss_port; |
492 |
F->connect.hostaddr.ss_len = addr->ss_len; |
493 |
|
494 |
/* Now, call the tryconnect() routine to try a connect() */ |
495 |
comm_connect_tryconnect(F, NULL); |
496 |
} |
497 |
|
498 |
/* static void comm_connect_tryconnect(fde_t *fd, void *unused) |
499 |
* Input: The fd, the handler data(unused). |
500 |
* Output: None. |
501 |
* Side-effects: Try and connect with pending connect data for the FD. If |
502 |
* we succeed or get a fatal error, call the callback. |
503 |
* Otherwise, it is still blocking or something, so register |
504 |
* to select for a write event on this FD. |
505 |
*/ |
506 |
static void |
507 |
comm_connect_tryconnect(fde_t *F, void *unused) |
508 |
{ |
509 |
/* This check is needed or re-entrant s_bsd_* like sigio break it. */ |
510 |
if (F->connect.callback == NULL) |
511 |
return; |
512 |
|
513 |
/* Try the connect() */ |
514 |
int retval = connect(F->fd, (struct sockaddr *)&F->connect.hostaddr, F->connect.hostaddr.ss_len); |
515 |
|
516 |
/* Error? */ |
517 |
if (retval < 0) |
518 |
{ |
519 |
/* |
520 |
* If we get EISCONN, then we've already connect()ed the socket, |
521 |
* which is a good thing. |
522 |
* -- adrian |
523 |
*/ |
524 |
if (errno == EISCONN) |
525 |
comm_connect_callback(F, COMM_OK); |
526 |
else if (comm_ignore_errno(errno)) |
527 |
/* Ignore error? Reschedule */ |
528 |
comm_setselect(F, COMM_SELECT_WRITE, comm_connect_tryconnect, NULL, 0); |
529 |
else |
530 |
/* Error? Fail with COMM_ERR_CONNECT */ |
531 |
comm_connect_callback(F, COMM_ERR_CONNECT); |
532 |
return; |
533 |
} |
534 |
|
535 |
/* If we get here, we've suceeded, so call with COMM_OK */ |
536 |
comm_connect_callback(F, COMM_OK); |
537 |
} |
538 |
|
539 |
/* |
540 |
* comm_errorstr() - return an error string for the given error condition |
541 |
*/ |
542 |
const char * |
543 |
comm_errstr(int error) |
544 |
{ |
545 |
if (error < 0 || error >= COMM_ERR_MAX) |
546 |
return "Invalid error number!"; |
547 |
return comm_err_str[error]; |
548 |
} |
549 |
|
550 |
/* |
551 |
* comm_open() - open a socket |
552 |
* |
553 |
* This is a highly highly cut down version of squid's comm_open() which |
554 |
* for the most part emulates socket(), *EXCEPT* it fails if we're about |
555 |
* to run out of file descriptors. |
556 |
*/ |
557 |
int |
558 |
comm_socket(int family, int sock_type, int proto) |
559 |
{ |
560 |
/* First, make sure we aren't going to run out of file descriptors */ |
561 |
if (number_fd >= hard_fdlimit) |
562 |
{ |
563 |
errno = ENFILE; |
564 |
return -1; |
565 |
} |
566 |
|
567 |
/* |
568 |
* Next, we try to open the socket. We *should* drop the reserved FD |
569 |
* limit if/when we get an error, but we can deal with that later. |
570 |
* XXX !!! -- adrian |
571 |
*/ |
572 |
int fd = socket(family, sock_type, proto); |
573 |
if (fd < 0) |
574 |
return -1; /* errno will be passed through, yay.. */ |
575 |
|
576 |
setup_socket(fd); |
577 |
|
578 |
return fd; |
579 |
} |
580 |
|
581 |
/* |
582 |
* comm_accept() - accept an incoming connection |
583 |
* |
584 |
* This is a simple wrapper for accept() which enforces FD limits like |
585 |
* comm_open() does. Returned fd must be either closed or tagged with |
586 |
* fd_open (this function no longer does it). |
587 |
*/ |
588 |
int |
589 |
comm_accept(int fd, struct irc_ssaddr *addr) |
590 |
{ |
591 |
socklen_t addrlen = sizeof(struct irc_ssaddr); |
592 |
|
593 |
if (number_fd >= hard_fdlimit) |
594 |
{ |
595 |
errno = ENFILE; |
596 |
return -1; |
597 |
} |
598 |
|
599 |
memset(addr, 0, sizeof(*addr)); |
600 |
|
601 |
/* |
602 |
* Next, do the accept(). if we get an error, we should drop the |
603 |
* reserved fd limit, but we can deal with that when comm_open() |
604 |
* also does it. XXX -- adrian |
605 |
*/ |
606 |
int new_fd = accept(fd, (struct sockaddr *)addr, &addrlen); |
607 |
if (new_fd < 0) |
608 |
return -1; |
609 |
|
610 |
remove_ipv6_mapping(addr); |
611 |
|
612 |
setup_socket(new_fd); |
613 |
|
614 |
/* .. and return */ |
615 |
return new_fd; |
616 |
} |
617 |
|
618 |
/* |
619 |
* remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address |
620 |
* OSes with IPv6 mapping listening on both |
621 |
* AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures |
622 |
* |
623 |
*/ |
624 |
void |
625 |
remove_ipv6_mapping(struct irc_ssaddr *addr) |
626 |
{ |
627 |
if (addr->ss.ss_family == AF_INET6) |
628 |
{ |
629 |
if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr)) |
630 |
{ |
631 |
struct sockaddr_in6 v6; |
632 |
struct sockaddr_in *v4 = (struct sockaddr_in *)addr; |
633 |
|
634 |
memcpy(&v6, addr, sizeof(v6)); |
635 |
memset(v4, 0, sizeof(struct sockaddr_in)); |
636 |
memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr)); |
637 |
|
638 |
addr->ss.ss_family = AF_INET; |
639 |
addr->ss_len = sizeof(struct sockaddr_in); |
640 |
} |
641 |
else |
642 |
addr->ss_len = sizeof(struct sockaddr_in6); |
643 |
} |
644 |
else |
645 |
addr->ss_len = sizeof(struct sockaddr_in); |
646 |
} |