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