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