1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
* s_bsd.c: Network functions. |
4 |
* |
5 |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* This program is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
20 |
* USA |
21 |
* |
22 |
* $Id$ |
23 |
*/ |
24 |
|
25 |
#include "stdinc.h" |
26 |
#include <netinet/in_systm.h> |
27 |
#include <netinet/ip.h> |
28 |
#include <netinet/tcp.h> |
29 |
#include "list.h" |
30 |
#include "fdlist.h" |
31 |
#include "s_bsd.h" |
32 |
#include "client.h" |
33 |
#include "common.h" |
34 |
#include "dbuf.h" |
35 |
#include "event.h" |
36 |
#include "irc_string.h" |
37 |
#include "ircd.h" |
38 |
#include "listener.h" |
39 |
#include "numeric.h" |
40 |
#include "packet.h" |
41 |
#include "irc_res.h" |
42 |
#include "restart.h" |
43 |
#include "s_auth.h" |
44 |
#include "s_conf.h" |
45 |
#include "s_log.h" |
46 |
#include "s_serv.h" |
47 |
#include "send.h" |
48 |
#include "memory.h" |
49 |
#include "s_user.h" |
50 |
#include "hook.h" |
51 |
|
52 |
static const char *comm_err_str[] = { "Comm OK", "Error during bind()", |
53 |
"Error during DNS lookup", "connect timeout", "Error during connect()", |
54 |
"Comm Error" }; |
55 |
|
56 |
struct Callback *setup_socket_cb = NULL; |
57 |
|
58 |
static void comm_connect_callback(fde_t *, int); |
59 |
static PF comm_connect_timeout; |
60 |
static void comm_connect_dns_callback(void *, const struct irc_ssaddr *, const char *); |
61 |
static PF comm_connect_tryconnect; |
62 |
|
63 |
|
64 |
/* check_can_use_v6() |
65 |
* Check if the system can open AF_INET6 sockets |
66 |
*/ |
67 |
void |
68 |
check_can_use_v6(void) |
69 |
{ |
70 |
#ifdef IPV6 |
71 |
int v6; |
72 |
|
73 |
if ((v6 = socket(AF_INET6, SOCK_STREAM, 0)) < 0) |
74 |
ServerInfo.can_use_v6 = 0; |
75 |
else |
76 |
{ |
77 |
ServerInfo.can_use_v6 = 1; |
78 |
close(v6); |
79 |
} |
80 |
#else |
81 |
ServerInfo.can_use_v6 = 0; |
82 |
#endif |
83 |
} |
84 |
|
85 |
/* get_sockerr - get the error value from the socket or the current errno |
86 |
* |
87 |
* Get the *real* error from the socket (well try to anyway..). |
88 |
* This may only work when SO_DEBUG is enabled but its worth the |
89 |
* gamble anyway. |
90 |
*/ |
91 |
int |
92 |
get_sockerr(int fd) |
93 |
{ |
94 |
int errtmp = errno; |
95 |
#ifdef SO_ERROR |
96 |
int err = 0; |
97 |
socklen_t len = sizeof(err); |
98 |
|
99 |
if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len)) |
100 |
{ |
101 |
if (err) |
102 |
errtmp = err; |
103 |
} |
104 |
errno = errtmp; |
105 |
#endif |
106 |
return errtmp; |
107 |
} |
108 |
|
109 |
/* |
110 |
* report_error - report an error from an errno. |
111 |
* Record error to log and also send a copy to all *LOCAL* opers online. |
112 |
* |
113 |
* text is a *format* string for outputing error. It must |
114 |
* contain only two '%s', the first will be replaced |
115 |
* by the sockhost from the client_p, and the latter will |
116 |
* be taken from sys_errlist[errno]. |
117 |
* |
118 |
* client_p if not NULL, is the *LOCAL* client associated with |
119 |
* the error. |
120 |
* |
121 |
* Cannot use perror() within daemon. stderr is closed in |
122 |
* ircd and cannot be used. And, worse yet, it might have |
123 |
* been reassigned to a normal connection... |
124 |
* |
125 |
* Actually stderr is still there IFF ircd was run with -s --Rodder |
126 |
*/ |
127 |
|
128 |
void |
129 |
report_error(int level, const char* text, const char* who, int error) |
130 |
{ |
131 |
who = (who) ? who : ""; |
132 |
|
133 |
sendto_realops_flags(UMODE_DEBUG, level, text, who, strerror(error)); |
134 |
log_oper_action(LOG_IOERR_TYPE, NULL, "%s %s %s\n", who, text, strerror(error)); |
135 |
ilog(L_ERROR, text, who, strerror(error)); |
136 |
} |
137 |
|
138 |
/* |
139 |
* setup_socket() |
140 |
* |
141 |
* Set the socket non-blocking, and other wonderful bits. |
142 |
*/ |
143 |
static void * |
144 |
setup_socket(va_list args) |
145 |
{ |
146 |
int fd = va_arg(args, int); |
147 |
int opt = 1; |
148 |
|
149 |
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt)); |
150 |
|
151 |
#ifdef IPTOS_LOWDELAY |
152 |
opt = IPTOS_LOWDELAY; |
153 |
setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)); |
154 |
#endif |
155 |
|
156 |
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); |
157 |
|
158 |
return NULL; |
159 |
} |
160 |
|
161 |
/* |
162 |
* init_comm() |
163 |
* |
164 |
* Initializes comm subsystem. |
165 |
*/ |
166 |
void |
167 |
init_comm(void) |
168 |
{ |
169 |
setup_socket_cb = register_callback("setup_socket", setup_socket); |
170 |
init_netio(); |
171 |
} |
172 |
|
173 |
/* |
174 |
* close_connection |
175 |
* Close the physical connection. This function must make |
176 |
* MyConnect(client_p) == FALSE, and set client_p->from == NULL. |
177 |
*/ |
178 |
void |
179 |
close_connection(struct Client *client_p) |
180 |
{ |
181 |
struct ConfItem *conf; |
182 |
struct AccessItem *aconf; |
183 |
struct ClassItem *aclass; |
184 |
|
185 |
assert(NULL != client_p); |
186 |
|
187 |
if (!IsDead(client_p)) |
188 |
{ |
189 |
/* attempt to flush any pending dbufs. Evil, but .. -- adrian */ |
190 |
/* there is still a chance that we might send data to this socket |
191 |
* even if it is marked as blocked (COMM_SELECT_READ handler is called |
192 |
* before COMM_SELECT_WRITE). Let's try, nothing to lose.. -adx |
193 |
*/ |
194 |
ClearSendqBlocked(client_p); |
195 |
send_queued_write(client_p); |
196 |
} |
197 |
|
198 |
if (IsClient(client_p)) |
199 |
{ |
200 |
++ServerStats.is_cl; |
201 |
ServerStats.is_cbs += client_p->localClient->send.bytes; |
202 |
ServerStats.is_cbr += client_p->localClient->recv.bytes; |
203 |
ServerStats.is_cti += CurrentTime - client_p->firsttime; |
204 |
} |
205 |
else if (IsServer(client_p)) |
206 |
{ |
207 |
++ServerStats.is_sv; |
208 |
ServerStats.is_sbs += client_p->localClient->send.bytes; |
209 |
ServerStats.is_sbr += client_p->localClient->recv.bytes; |
210 |
ServerStats.is_sti += CurrentTime - client_p->firsttime; |
211 |
|
212 |
/* XXX Does this even make any sense at all anymore? |
213 |
* scheduling a 'quick' reconnect could cause a pile of |
214 |
* nick collides under TSora protocol... -db |
215 |
*/ |
216 |
/* |
217 |
* If the connection has been up for a long amount of time, schedule |
218 |
* a 'quick' reconnect, else reset the next-connect cycle. |
219 |
*/ |
220 |
if ((conf = find_conf_exact(SERVER_TYPE, client_p->name, |
221 |
client_p->username, client_p->host))) |
222 |
{ |
223 |
/* |
224 |
* Reschedule a faster reconnect, if this was a automatically |
225 |
* connected configuration entry. (Note that if we have had |
226 |
* a rehash in between, the status has been changed to |
227 |
* CONF_ILLEGAL). But only do this if it was a "good" link. |
228 |
*/ |
229 |
aconf = map_to_conf(conf); |
230 |
aclass = map_to_conf(aconf->class_ptr); |
231 |
aconf->hold = time(NULL); |
232 |
aconf->hold += (aconf->hold - client_p->since > HANGONGOODLINK) ? |
233 |
HANGONRETRYDELAY : ConFreq(aclass); |
234 |
} |
235 |
} |
236 |
else |
237 |
++ServerStats.is_ni; |
238 |
|
239 |
#ifdef HAVE_LIBCRYPTO |
240 |
if (client_p->localClient->fd.ssl) |
241 |
{ |
242 |
SSL_set_shutdown(client_p->localClient->fd.ssl, SSL_RECEIVED_SHUTDOWN); |
243 |
|
244 |
if (!SSL_shutdown(client_p->localClient->fd.ssl)) |
245 |
SSL_shutdown(client_p->localClient->fd.ssl); |
246 |
} |
247 |
#endif |
248 |
if (client_p->localClient->fd.flags.open) |
249 |
fd_close(&client_p->localClient->fd); |
250 |
|
251 |
if (HasServlink(client_p)) |
252 |
{ |
253 |
if (client_p->localClient->ctrlfd.flags.open) |
254 |
fd_close(&client_p->localClient->ctrlfd); |
255 |
} |
256 |
|
257 |
dbuf_clear(&client_p->localClient->buf_sendq); |
258 |
dbuf_clear(&client_p->localClient->buf_recvq); |
259 |
|
260 |
MyFree(client_p->localClient->passwd); |
261 |
detach_conf(client_p, CONF_TYPE); |
262 |
client_p->from = NULL; /* ...this should catch them! >:) --msa */ |
263 |
} |
264 |
|
265 |
#ifdef HAVE_LIBCRYPTO |
266 |
/* |
267 |
* ssl_handshake - let OpenSSL initialize the protocol. Register for |
268 |
* read/write events if necessary. |
269 |
*/ |
270 |
static void |
271 |
ssl_handshake(int fd, struct Client *client_p) |
272 |
{ |
273 |
int ret = SSL_accept(client_p->localClient->fd.ssl); |
274 |
|
275 |
if (ret <= 0) |
276 |
switch (SSL_get_error(client_p->localClient->fd.ssl, ret)) |
277 |
{ |
278 |
case SSL_ERROR_WANT_WRITE: |
279 |
comm_setselect(&client_p->localClient->fd, COMM_SELECT_WRITE, |
280 |
(PF *) ssl_handshake, client_p, 0); |
281 |
return; |
282 |
|
283 |
case SSL_ERROR_WANT_READ: |
284 |
comm_setselect(&client_p->localClient->fd, COMM_SELECT_READ, |
285 |
(PF *) ssl_handshake, client_p, 0); |
286 |
return; |
287 |
|
288 |
default: |
289 |
exit_client(client_p, client_p, "Error during SSL handshake"); |
290 |
return; |
291 |
} |
292 |
|
293 |
execute_callback(auth_cb, client_p); |
294 |
} |
295 |
#endif |
296 |
|
297 |
/* |
298 |
* add_connection - creates a client which has just connected to us on |
299 |
* the given fd. The sockhost field is initialized with the ip# of the host. |
300 |
* An unique id is calculated now, in case it is needed for auth. |
301 |
* The client is sent to the auth module for verification, and not put in |
302 |
* any client list yet. |
303 |
*/ |
304 |
void |
305 |
add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd) |
306 |
{ |
307 |
struct Client *new_client = make_client(NULL); |
308 |
|
309 |
fd_open(&new_client->localClient->fd, fd, 1, |
310 |
(listener->flags & LISTENER_SSL) ? |
311 |
"Incoming SSL connection" : "Incoming connection"); |
312 |
|
313 |
/* |
314 |
* copy address to 'sockhost' as a string, copy it to host too |
315 |
* so we have something valid to put into error messages... |
316 |
*/ |
317 |
memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr)); |
318 |
|
319 |
getnameinfo((struct sockaddr *)&new_client->localClient->ip, |
320 |
new_client->localClient->ip.ss_len, new_client->sockhost, |
321 |
sizeof(new_client->sockhost), NULL, 0, NI_NUMERICHOST); |
322 |
new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family; |
323 |
|
324 |
if (new_client->sockhost[0] == ':' && new_client->sockhost[1] == ':') |
325 |
{ |
326 |
strlcpy(new_client->host, "0", sizeof(new_client->host)); |
327 |
strlcpy(new_client->host+1, new_client->sockhost, sizeof(new_client->host)-1); |
328 |
memmove(new_client->sockhost+1, new_client->sockhost, sizeof(new_client->sockhost)-1); |
329 |
new_client->sockhost[0] = '0'; |
330 |
} |
331 |
else |
332 |
strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host)); |
333 |
|
334 |
new_client->localClient->listener = listener; |
335 |
++listener->ref_count; |
336 |
|
337 |
#ifdef HAVE_LIBCRYPTO |
338 |
if (listener->flags & LISTENER_SSL) |
339 |
{ |
340 |
if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL) |
341 |
{ |
342 |
ilog(L_CRIT, "SSL_new() ERROR! -- %s", |
343 |
ERR_error_string(ERR_get_error(), NULL)); |
344 |
|
345 |
SetDead(new_client); |
346 |
exit_client(new_client, new_client, "SSL_new failed"); |
347 |
return; |
348 |
} |
349 |
|
350 |
SSL_set_fd(new_client->localClient->fd.ssl, fd); |
351 |
ssl_handshake(0, new_client); |
352 |
} |
353 |
else |
354 |
#endif |
355 |
execute_callback(auth_cb, new_client); |
356 |
} |
357 |
|
358 |
/* |
359 |
* stolen from squid - its a neat (but overused! :) routine which we |
360 |
* can use to see whether we can ignore this errno or not. It is |
361 |
* generally useful for non-blocking network IO related errnos. |
362 |
* -- adrian |
363 |
*/ |
364 |
int |
365 |
ignoreErrno(int ierrno) |
366 |
{ |
367 |
switch (ierrno) |
368 |
{ |
369 |
case EINPROGRESS: |
370 |
case EWOULDBLOCK: |
371 |
#if EAGAIN != EWOULDBLOCK |
372 |
case EAGAIN: |
373 |
#endif |
374 |
case EALREADY: |
375 |
case EINTR: |
376 |
#ifdef ERESTART |
377 |
case ERESTART: |
378 |
#endif |
379 |
return 1; |
380 |
default: |
381 |
return 0; |
382 |
} |
383 |
} |
384 |
|
385 |
/* |
386 |
* comm_settimeout() - set the socket timeout |
387 |
* |
388 |
* Set the timeout for the fd |
389 |
*/ |
390 |
void |
391 |
comm_settimeout(fde_t *fd, time_t timeout, PF *callback, void *cbdata) |
392 |
{ |
393 |
assert(fd->flags.open); |
394 |
|
395 |
fd->timeout = CurrentTime + (timeout / 1000); |
396 |
fd->timeout_handler = callback; |
397 |
fd->timeout_data = cbdata; |
398 |
} |
399 |
|
400 |
/* |
401 |
* comm_setflush() - set a flush function |
402 |
* |
403 |
* A flush function is simply a function called if found during |
404 |
* comm_timeouts(). Its basically a second timeout, except in this case |
405 |
* I'm too lazy to implement multiple timeout functions! :-) |
406 |
* its kinda nice to have it separate, since this is designed for |
407 |
* flush functions, and when comm_close() is implemented correctly |
408 |
* with close functions, we _actually_ don't call comm_close() here .. |
409 |
* -- originally Adrian's notes |
410 |
* comm_close() is replaced with fd_close() in fdlist.c |
411 |
*/ |
412 |
void |
413 |
comm_setflush(fde_t *fd, time_t timeout, PF *callback, void *cbdata) |
414 |
{ |
415 |
assert(fd->flags.open); |
416 |
|
417 |
fd->flush_timeout = CurrentTime + (timeout / 1000); |
418 |
fd->flush_handler = callback; |
419 |
fd->flush_data = cbdata; |
420 |
} |
421 |
|
422 |
/* |
423 |
* comm_checktimeouts() - check the socket timeouts |
424 |
* |
425 |
* All this routine does is call the given callback/cbdata, without closing |
426 |
* down the file descriptor. When close handlers have been implemented, |
427 |
* this will happen. |
428 |
*/ |
429 |
void |
430 |
comm_checktimeouts(void *notused) |
431 |
{ |
432 |
int i; |
433 |
fde_t *F; |
434 |
PF *hdl; |
435 |
void *data; |
436 |
|
437 |
for (i = 0; i < FD_HASH_SIZE; i++) |
438 |
for (F = fd_hash[i]; F != NULL; F = fd_next_in_loop) |
439 |
{ |
440 |
assert(F->flags.open); |
441 |
fd_next_in_loop = F->hnext; |
442 |
|
443 |
/* check flush functions */ |
444 |
if (F->flush_handler && F->flush_timeout > 0 && |
445 |
F->flush_timeout < CurrentTime) |
446 |
{ |
447 |
hdl = F->flush_handler; |
448 |
data = F->flush_data; |
449 |
comm_setflush(F, 0, NULL, NULL); |
450 |
hdl(F, data); |
451 |
} |
452 |
|
453 |
/* check timeouts */ |
454 |
if (F->timeout_handler && F->timeout > 0 && |
455 |
F->timeout < CurrentTime) |
456 |
{ |
457 |
/* Call timeout handler */ |
458 |
hdl = F->timeout_handler; |
459 |
data = F->timeout_data; |
460 |
comm_settimeout(F, 0, NULL, NULL); |
461 |
hdl(F, data); |
462 |
} |
463 |
} |
464 |
} |
465 |
|
466 |
/* |
467 |
* void comm_connect_tcp(int fd, const char *host, unsigned short port, |
468 |
* struct sockaddr *clocal, int socklen, |
469 |
* CNCB *callback, void *data, int aftype, int timeout) |
470 |
* Input: An fd to connect with, a host and port to connect to, |
471 |
* a local sockaddr to connect from + length(or NULL to use the |
472 |
* default), a callback, the data to pass into the callback, the |
473 |
* address family. |
474 |
* Output: None. |
475 |
* Side-effects: A non-blocking connection to the host is started, and |
476 |
* if necessary, set up for selection. The callback given |
477 |
* may be called now, or it may be called later. |
478 |
*/ |
479 |
void |
480 |
comm_connect_tcp(fde_t *fd, const char *host, unsigned short port, |
481 |
struct sockaddr *clocal, int socklen, CNCB *callback, |
482 |
void *data, int aftype, int timeout) |
483 |
{ |
484 |
struct addrinfo hints, *res; |
485 |
char portname[PORTNAMELEN + 1]; |
486 |
|
487 |
assert(callback); |
488 |
fd->connect.callback = callback; |
489 |
fd->connect.data = data; |
490 |
|
491 |
fd->connect.hostaddr.ss.ss_family = aftype; |
492 |
fd->connect.hostaddr.ss_port = htons(port); |
493 |
|
494 |
/* Note that we're using a passed sockaddr here. This is because |
495 |
* generally you'll be bind()ing to a sockaddr grabbed from |
496 |
* getsockname(), so this makes things easier. |
497 |
* XXX If NULL is passed as local, we should later on bind() to the |
498 |
* virtual host IP, for completeness. |
499 |
* -- adrian |
500 |
*/ |
501 |
if ((clocal != NULL) && (bind(fd->fd, clocal, socklen) < 0)) |
502 |
{ |
503 |
/* Failure, call the callback with COMM_ERR_BIND */ |
504 |
comm_connect_callback(fd, COMM_ERR_BIND); |
505 |
/* ... and quit */ |
506 |
return; |
507 |
} |
508 |
|
509 |
/* Next, if we have been given an IP, get the addr and skip the |
510 |
* DNS check (and head direct to comm_connect_tryconnect(). |
511 |
*/ |
512 |
memset(&hints, 0, sizeof(hints)); |
513 |
hints.ai_family = AF_UNSPEC; |
514 |
hints.ai_socktype = SOCK_STREAM; |
515 |
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; |
516 |
|
517 |
snprintf(portname, sizeof(portname), "%d", port); |
518 |
|
519 |
if (getaddrinfo(host, portname, &hints, &res)) |
520 |
{ |
521 |
/* Send the DNS request, for the next level */ |
522 |
if (aftype == AF_INET6) |
523 |
gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA); |
524 |
else |
525 |
gethost_byname_type(comm_connect_dns_callback, fd, host, T_A); |
526 |
} |
527 |
else |
528 |
{ |
529 |
/* We have a valid IP, so we just call tryconnect */ |
530 |
/* Make sure we actually set the timeout here .. */ |
531 |
assert(res != NULL); |
532 |
memcpy(&fd->connect.hostaddr, res->ai_addr, res->ai_addrlen); |
533 |
fd->connect.hostaddr.ss_len = res->ai_addrlen; |
534 |
fd->connect.hostaddr.ss.ss_family = res->ai_family; |
535 |
freeaddrinfo(res); |
536 |
comm_settimeout(fd, timeout*1000, comm_connect_timeout, NULL); |
537 |
comm_connect_tryconnect(fd, NULL); |
538 |
} |
539 |
} |
540 |
|
541 |
/* |
542 |
* comm_connect_callback() - call the callback, and continue with life |
543 |
*/ |
544 |
static void |
545 |
comm_connect_callback(fde_t *fd, int status) |
546 |
{ |
547 |
CNCB *hdl; |
548 |
|
549 |
/* This check is gross..but probably necessary */ |
550 |
if (fd->connect.callback == NULL) |
551 |
return; |
552 |
|
553 |
/* Clear the connect flag + handler */ |
554 |
hdl = fd->connect.callback; |
555 |
fd->connect.callback = NULL; |
556 |
|
557 |
/* Clear the timeout handler */ |
558 |
comm_settimeout(fd, 0, NULL, NULL); |
559 |
|
560 |
/* Call the handler */ |
561 |
hdl(fd, status, fd->connect.data); |
562 |
} |
563 |
|
564 |
/* |
565 |
* comm_connect_timeout() - this gets called when the socket connection |
566 |
* times out. This *only* can be called once connect() is initially |
567 |
* called .. |
568 |
*/ |
569 |
static void |
570 |
comm_connect_timeout(fde_t *fd, void *notused) |
571 |
{ |
572 |
/* error! */ |
573 |
comm_connect_callback(fd, COMM_ERR_TIMEOUT); |
574 |
} |
575 |
|
576 |
/* |
577 |
* comm_connect_dns_callback() - called at the completion of the DNS request |
578 |
* |
579 |
* The DNS request has completed, so if we've got an error, return it, |
580 |
* otherwise we initiate the connect() |
581 |
*/ |
582 |
static void |
583 |
comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name) |
584 |
{ |
585 |
fde_t *F = vptr; |
586 |
|
587 |
if (name == NULL) |
588 |
{ |
589 |
comm_connect_callback(F, COMM_ERR_DNS); |
590 |
return; |
591 |
} |
592 |
|
593 |
/* No error, set a 10 second timeout */ |
594 |
comm_settimeout(F, 30*1000, comm_connect_timeout, NULL); |
595 |
|
596 |
/* Copy over the DNS reply info so we can use it in the connect() */ |
597 |
/* |
598 |
* Note we don't fudge the refcount here, because we aren't keeping |
599 |
* the DNS record around, and the DNS cache is gone anyway.. |
600 |
* -- adrian |
601 |
*/ |
602 |
memcpy(&F->connect.hostaddr, addr, addr->ss_len); |
603 |
/* The cast is hacky, but safe - port offset is same on v4 and v6 */ |
604 |
((struct sockaddr_in *) &F->connect.hostaddr)->sin_port = |
605 |
F->connect.hostaddr.ss_port; |
606 |
F->connect.hostaddr.ss_len = addr->ss_len; |
607 |
|
608 |
/* Now, call the tryconnect() routine to try a connect() */ |
609 |
comm_connect_tryconnect(F, NULL); |
610 |
} |
611 |
|
612 |
/* static void comm_connect_tryconnect(int fd, void *notused) |
613 |
* Input: The fd, the handler data(unused). |
614 |
* Output: None. |
615 |
* Side-effects: Try and connect with pending connect data for the FD. If |
616 |
* we succeed or get a fatal error, call the callback. |
617 |
* Otherwise, it is still blocking or something, so register |
618 |
* to select for a write event on this FD. |
619 |
*/ |
620 |
static void |
621 |
comm_connect_tryconnect(fde_t *fd, void *notused) |
622 |
{ |
623 |
int retval; |
624 |
|
625 |
/* This check is needed or re-entrant s_bsd_* like sigio break it. */ |
626 |
if (fd->connect.callback == NULL) |
627 |
return; |
628 |
|
629 |
/* Try the connect() */ |
630 |
retval = connect(fd->fd, (struct sockaddr *) &fd->connect.hostaddr, |
631 |
fd->connect.hostaddr.ss_len); |
632 |
|
633 |
/* Error? */ |
634 |
if (retval < 0) |
635 |
{ |
636 |
/* |
637 |
* If we get EISCONN, then we've already connect()ed the socket, |
638 |
* which is a good thing. |
639 |
* -- adrian |
640 |
*/ |
641 |
if (errno == EISCONN) |
642 |
comm_connect_callback(fd, COMM_OK); |
643 |
else if (ignoreErrno(errno)) |
644 |
/* Ignore error? Reschedule */ |
645 |
comm_setselect(fd, COMM_SELECT_WRITE, comm_connect_tryconnect, |
646 |
NULL, 0); |
647 |
else |
648 |
/* Error? Fail with COMM_ERR_CONNECT */ |
649 |
comm_connect_callback(fd, COMM_ERR_CONNECT); |
650 |
return; |
651 |
} |
652 |
|
653 |
/* If we get here, we've suceeded, so call with COMM_OK */ |
654 |
comm_connect_callback(fd, COMM_OK); |
655 |
} |
656 |
|
657 |
/* |
658 |
* comm_errorstr() - return an error string for the given error condition |
659 |
*/ |
660 |
const char * |
661 |
comm_errstr(int error) |
662 |
{ |
663 |
if (error < 0 || error >= COMM_ERR_MAX) |
664 |
return "Invalid error number!"; |
665 |
return comm_err_str[error]; |
666 |
} |
667 |
|
668 |
/* |
669 |
* comm_open() - open a socket |
670 |
* |
671 |
* This is a highly highly cut down version of squid's comm_open() which |
672 |
* for the most part emulates socket(), *EXCEPT* it fails if we're about |
673 |
* to run out of file descriptors. |
674 |
*/ |
675 |
int |
676 |
comm_open(fde_t *F, int family, int sock_type, int proto, const char *note) |
677 |
{ |
678 |
int fd; |
679 |
|
680 |
/* First, make sure we aren't going to run out of file descriptors */ |
681 |
if (number_fd >= hard_fdlimit) |
682 |
{ |
683 |
errno = ENFILE; |
684 |
return -1; |
685 |
} |
686 |
|
687 |
/* |
688 |
* Next, we try to open the socket. We *should* drop the reserved FD |
689 |
* limit if/when we get an error, but we can deal with that later. |
690 |
* XXX !!! -- adrian |
691 |
*/ |
692 |
fd = socket(family, sock_type, proto); |
693 |
if (fd < 0) |
694 |
return -1; /* errno will be passed through, yay.. */ |
695 |
|
696 |
execute_callback(setup_socket_cb, fd); |
697 |
|
698 |
/* update things in our fd tracking */ |
699 |
fd_open(F, fd, 1, note); |
700 |
return 0; |
701 |
} |
702 |
|
703 |
/* |
704 |
* comm_accept() - accept an incoming connection |
705 |
* |
706 |
* This is a simple wrapper for accept() which enforces FD limits like |
707 |
* comm_open() does. Returned fd must be either closed or tagged with |
708 |
* fd_open (this function no longer does it). |
709 |
*/ |
710 |
int |
711 |
comm_accept(struct Listener *lptr, struct irc_ssaddr *pn) |
712 |
{ |
713 |
int newfd; |
714 |
socklen_t addrlen = sizeof(struct irc_ssaddr); |
715 |
|
716 |
if (number_fd >= hard_fdlimit) |
717 |
{ |
718 |
errno = ENFILE; |
719 |
return -1; |
720 |
} |
721 |
|
722 |
/* |
723 |
* Next, do the accept(). if we get an error, we should drop the |
724 |
* reserved fd limit, but we can deal with that when comm_open() |
725 |
* also does it. XXX -- adrian |
726 |
*/ |
727 |
newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, &addrlen); |
728 |
if (newfd < 0) |
729 |
return -1; |
730 |
|
731 |
#ifdef IPV6 |
732 |
remove_ipv6_mapping(pn); |
733 |
#else |
734 |
pn->ss_len = addrlen; |
735 |
#endif |
736 |
|
737 |
execute_callback(setup_socket_cb, newfd); |
738 |
|
739 |
/* .. and return */ |
740 |
return newfd; |
741 |
} |
742 |
|
743 |
/* |
744 |
* remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address |
745 |
* OSes with IPv6 mapping listening on both |
746 |
* AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures |
747 |
* |
748 |
*/ |
749 |
#ifdef IPV6 |
750 |
void |
751 |
remove_ipv6_mapping(struct irc_ssaddr *addr) |
752 |
{ |
753 |
if (addr->ss.ss_family == AF_INET6) |
754 |
{ |
755 |
if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr)) |
756 |
{ |
757 |
struct sockaddr_in6 v6; |
758 |
struct sockaddr_in *v4 = (struct sockaddr_in *)addr; |
759 |
|
760 |
memcpy(&v6, addr, sizeof(v6)); |
761 |
memset(v4, 0, sizeof(struct sockaddr_in)); |
762 |
memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr)); |
763 |
|
764 |
addr->ss.ss_family = AF_INET; |
765 |
addr->ss_len = sizeof(struct sockaddr_in); |
766 |
} |
767 |
else |
768 |
addr->ss_len = sizeof(struct sockaddr_in6); |
769 |
} |
770 |
else |
771 |
addr->ss_len = sizeof(struct sockaddr_in); |
772 |
} |
773 |
#endif |