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