49 |
|
#include "s_conf.h" |
50 |
|
#include "s_log.h" |
51 |
|
#include "s_serv.h" |
52 |
– |
#include "s_stats.h" |
52 |
|
#include "send.h" |
53 |
|
#include "memory.h" |
54 |
|
#include "s_user.h" |
62 |
|
|
63 |
|
static void comm_connect_callback(fde_t *fd, int status); |
64 |
|
static PF comm_connect_timeout; |
65 |
< |
static void comm_connect_dns_callback(void *vptr, struct DNSReply *reply); |
65 |
> |
static void comm_connect_dns_callback(void *, const struct irc_ssaddr *, const char *); |
66 |
|
static PF comm_connect_tryconnect; |
67 |
|
|
69 |
– |
extern void init_netio(void); |
68 |
|
|
69 |
|
/* check_can_use_v6() |
70 |
|
* Check if the system can open AF_INET6 sockets |
109 |
|
int err = 0; |
110 |
|
socklen_t len = sizeof(err); |
111 |
|
|
112 |
< |
if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*) &err, (socklen_t *)&len)) |
112 |
> |
if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len)) |
113 |
|
{ |
114 |
|
if (err) |
115 |
|
errtmp = err; |
159 |
|
int fd = va_arg(args, int); |
160 |
|
int opt = 1; |
161 |
|
|
162 |
< |
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, sizeof(opt)); |
162 |
> |
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt)); |
163 |
|
|
164 |
|
#ifdef IPTOS_LOWDELAY |
165 |
|
opt = IPTOS_LOWDELAY; |
166 |
< |
setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &opt, sizeof(opt)); |
166 |
> |
setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)); |
167 |
|
#endif |
168 |
|
|
169 |
|
#ifndef _WIN32 |
199 |
|
|
200 |
|
assert(NULL != client_p); |
201 |
|
|
202 |
+ |
if (!IsDead(client_p)) |
203 |
+ |
{ |
204 |
+ |
/* attempt to flush any pending dbufs. Evil, but .. -- adrian */ |
205 |
+ |
/* there is still a chance that we might send data to this socket |
206 |
+ |
* even if it is marked as blocked (COMM_SELECT_READ handler is called |
207 |
+ |
* before COMM_SELECT_WRITE). Let's try, nothing to lose.. -adx |
208 |
+ |
*/ |
209 |
+ |
ClearSendqBlocked(client_p); |
210 |
+ |
send_queued_write(client_p); |
211 |
+ |
} |
212 |
+ |
|
213 |
|
if (IsServer(client_p)) |
214 |
|
{ |
215 |
< |
ServerStats->is_sv++; |
216 |
< |
ServerStats->is_sbs += client_p->localClient->send.bytes; |
217 |
< |
ServerStats->is_sbr += client_p->localClient->recv.bytes; |
218 |
< |
ServerStats->is_sti += CurrentTime - client_p->firsttime; |
215 |
> |
++ServerStats.is_sv; |
216 |
> |
ServerStats.is_sbs += client_p->localClient->send.bytes; |
217 |
> |
ServerStats.is_sbr += client_p->localClient->recv.bytes; |
218 |
> |
ServerStats.is_sti += CurrentTime - client_p->firsttime; |
219 |
|
|
220 |
|
/* XXX Does this even make any sense at all anymore? |
221 |
|
* scheduling a 'quick' reconnect could cause a pile of |
246 |
|
} |
247 |
|
else if (IsClient(client_p)) |
248 |
|
{ |
249 |
< |
ServerStats->is_cl++; |
250 |
< |
ServerStats->is_cbs += client_p->localClient->send.bytes; |
251 |
< |
ServerStats->is_cbr += client_p->localClient->recv.bytes; |
252 |
< |
ServerStats->is_cti += CurrentTime - client_p->firsttime; |
249 |
> |
++ServerStats.is_cl; |
250 |
> |
ServerStats.is_cbs += client_p->localClient->send.bytes; |
251 |
> |
ServerStats.is_cbr += client_p->localClient->recv.bytes; |
252 |
> |
ServerStats.is_cti += CurrentTime - client_p->firsttime; |
253 |
|
} |
254 |
|
else |
255 |
< |
ServerStats->is_ni++; |
247 |
< |
|
248 |
< |
if (!IsDead(client_p)) |
249 |
< |
{ |
250 |
< |
/* attempt to flush any pending dbufs. Evil, but .. -- adrian */ |
251 |
< |
/* there is still a chance that we might send data to this socket |
252 |
< |
* even if it is marked as blocked (COMM_SELECT_READ handler is called |
253 |
< |
* before COMM_SELECT_WRITE). Let's try, nothing to lose.. -adx |
254 |
< |
*/ |
255 |
< |
ClearSendqBlocked(client_p); |
256 |
< |
send_queued_write(client_p); |
257 |
< |
} |
255 |
> |
++ServerStats.is_ni; |
256 |
|
|
257 |
|
#ifdef HAVE_LIBCRYPTO |
258 |
|
if (client_p->localClient->fd.ssl) |
259 |
< |
SSL_shutdown(client_p->localClient->fd.ssl); |
259 |
> |
{ |
260 |
> |
SSL_set_shutdown(client_p->localClient->fd.ssl, SSL_RECEIVED_SHUTDOWN); |
261 |
> |
|
262 |
> |
if (!SSL_shutdown(client_p->localClient->fd.ssl)) |
263 |
> |
SSL_shutdown(client_p->localClient->fd.ssl); |
264 |
> |
} |
265 |
|
#endif |
266 |
|
if (client_p->localClient->fd.flags.open) |
267 |
|
fd_close(&client_p->localClient->fd); |
320 |
|
* any client list yet. |
321 |
|
*/ |
322 |
|
void |
323 |
< |
add_connection(struct Listener* listener, int fd) |
323 |
> |
add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd) |
324 |
|
{ |
325 |
|
struct Client *new_client; |
323 |
– |
socklen_t len = sizeof(struct irc_ssaddr); |
324 |
– |
struct irc_ssaddr irn; |
325 |
– |
assert(NULL != listener); |
326 |
– |
|
327 |
– |
/* |
328 |
– |
* get the client socket name from the socket |
329 |
– |
* the client has already been checked out in accept_connection |
330 |
– |
*/ |
326 |
|
|
327 |
< |
memset(&irn, 0, sizeof(irn)); |
333 |
< |
if (getpeername(fd, (struct sockaddr *)&irn, (socklen_t *)&len)) |
334 |
< |
{ |
335 |
< |
#ifdef _WIN32 |
336 |
< |
errno = WSAGetLastError(); |
337 |
< |
#endif |
338 |
< |
report_error(L_ALL, "Failed in adding new connection %s :%s", |
339 |
< |
get_listener_name(listener), errno); |
340 |
< |
ServerStats->is_ref++; |
341 |
< |
#ifdef _WIN32 |
342 |
< |
closesocket(fd); |
343 |
< |
#else |
344 |
< |
close(fd); |
345 |
< |
#endif |
346 |
< |
return; |
347 |
< |
} |
327 |
> |
assert(NULL != listener); |
328 |
|
|
349 |
– |
#ifdef IPV6 |
350 |
– |
remove_ipv6_mapping(&irn); |
351 |
– |
#else |
352 |
– |
irn.ss_len = len; |
353 |
– |
#endif |
329 |
|
new_client = make_client(NULL); |
330 |
+ |
|
331 |
|
fd_open(&new_client->localClient->fd, fd, 1, |
332 |
|
(listener->flags & LISTENER_SSL) ? |
333 |
|
"Incoming SSL connection" : "Incoming connection"); |
358 |
– |
memset(&new_client->localClient->ip, 0, sizeof(struct irc_ssaddr)); |
334 |
|
|
335 |
|
/* |
336 |
|
* copy address to 'sockhost' as a string, copy it to host too |
337 |
|
* so we have something valid to put into error messages... |
338 |
|
*/ |
339 |
< |
new_client->localClient->port = ntohs(irn.ss_port); |
365 |
< |
memcpy(&new_client->localClient->ip, &irn, sizeof(struct irc_ssaddr)); |
339 |
> |
memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr)); |
340 |
|
|
341 |
|
irc_getnameinfo((struct sockaddr*)&new_client->localClient->ip, |
342 |
|
new_client->localClient->ip.ss_len, new_client->sockhost, |
343 |
|
HOSTIPLEN, NULL, 0, NI_NUMERICHOST); |
344 |
|
new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family; |
371 |
– |
|
372 |
– |
*new_client->host = '\0'; |
345 |
|
#ifdef IPV6 |
346 |
< |
if (*new_client->sockhost == ':') |
346 |
> |
if (new_client->sockhost[0] == ':') |
347 |
|
strlcat(new_client->host, "0", HOSTLEN+1); |
348 |
|
|
349 |
|
if (new_client->localClient->aftype == AF_INET6 && |
351 |
|
{ |
352 |
|
strlcat(new_client->host, new_client->sockhost,HOSTLEN+1); |
353 |
|
strlcat(new_client->host, ".", HOSTLEN+1); |
354 |
< |
} else |
354 |
> |
} |
355 |
> |
else |
356 |
|
#endif |
357 |
|
strlcat(new_client->host, new_client->sockhost,HOSTLEN+1); |
358 |
|
|
359 |
|
new_client->localClient->listener = listener; |
360 |
|
++listener->ref_count; |
361 |
|
|
389 |
– |
connect_id++; |
390 |
– |
new_client->connect_id = connect_id; |
391 |
– |
|
362 |
|
#ifdef HAVE_LIBCRYPTO |
363 |
< |
if ((listener->flags & LISTENER_SSL)) |
363 |
> |
if (listener->flags & LISTENER_SSL) |
364 |
|
{ |
365 |
< |
if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.ctx)) == NULL) |
365 |
> |
if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL) |
366 |
|
{ |
367 |
|
ilog(L_CRIT, "SSL_new() ERROR! -- %s", |
368 |
|
ERR_error_string(ERR_get_error(), NULL)); |
507 |
|
void *data, int aftype, int timeout) |
508 |
|
{ |
509 |
|
struct addrinfo hints, *res; |
510 |
< |
char portname[PORTNAMELEN+1]; |
510 |
> |
char portname[PORTNAMELEN + 1]; |
511 |
|
|
512 |
|
assert(callback); |
513 |
|
fd->connect.callback = callback; |
539 |
|
hints.ai_socktype = SOCK_STREAM; |
540 |
|
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; |
541 |
|
|
542 |
< |
snprintf(portname, PORTNAMELEN, "%d", port); |
542 |
> |
snprintf(portname, sizeof(portname), "%d", port); |
543 |
|
|
544 |
|
if (irc_getaddrinfo(host, portname, &hints, &res)) |
545 |
|
{ |
546 |
|
/* Send the DNS request, for the next level */ |
547 |
< |
fd->dns_query = MyMalloc(sizeof(struct DNSQuery)); |
548 |
< |
fd->dns_query->ptr = fd; |
549 |
< |
fd->dns_query->callback = comm_connect_dns_callback; |
550 |
< |
gethost_byname(host, fd->dns_query); |
547 |
> |
if (aftype == AF_INET6) |
548 |
> |
gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA); |
549 |
> |
else |
550 |
> |
gethost_byname_type(comm_connect_dns_callback, fd, host, T_A); |
551 |
|
} |
552 |
|
else |
553 |
|
{ |
605 |
|
* otherwise we initiate the connect() |
606 |
|
*/ |
607 |
|
static void |
608 |
< |
comm_connect_dns_callback(void *vptr, struct DNSReply *reply) |
608 |
> |
comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name) |
609 |
|
{ |
610 |
|
fde_t *F = vptr; |
611 |
|
|
612 |
< |
if (reply == NULL) |
612 |
> |
if (name == NULL) |
613 |
|
{ |
644 |
– |
MyFree(F->dns_query); |
645 |
– |
F->dns_query = NULL; |
614 |
|
comm_connect_callback(F, COMM_ERR_DNS); |
615 |
|
return; |
616 |
|
} |
624 |
|
* the DNS record around, and the DNS cache is gone anyway.. |
625 |
|
* -- adrian |
626 |
|
*/ |
627 |
< |
memcpy(&F->connect.hostaddr, &reply->addr, reply->addr.ss_len); |
627 |
> |
memcpy(&F->connect.hostaddr, addr, addr->ss_len); |
628 |
|
/* The cast is hacky, but safe - port offset is same on v4 and v6 */ |
629 |
|
((struct sockaddr_in *) &F->connect.hostaddr)->sin_port = |
630 |
|
F->connect.hostaddr.ss_port; |
631 |
< |
F->connect.hostaddr.ss_len = reply->addr.ss_len; |
631 |
> |
F->connect.hostaddr.ss_len = addr->ss_len; |
632 |
|
|
633 |
|
/* Now, call the tryconnect() routine to try a connect() */ |
666 |
– |
MyFree(F->dns_query); |
667 |
– |
F->dns_query = NULL; |
634 |
|
comm_connect_tryconnect(F, NULL); |
635 |
|
} |
636 |
|
|