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