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