| 23 |
|
*/ |
| 24 |
|
|
| 25 |
|
#include "stdinc.h" |
| 26 |
– |
#ifndef _WIN32 |
| 26 |
|
#include <netinet/in_systm.h> |
| 27 |
|
#include <netinet/ip.h> |
| 28 |
|
#include <netinet/tcp.h> |
| 29 |
< |
#endif |
| 29 |
> |
#include "list.h" |
| 30 |
|
#include "fdlist.h" |
| 31 |
|
#include "s_bsd.h" |
| 32 |
|
#include "client.h" |
| 34 |
|
#include "dbuf.h" |
| 35 |
|
#include "event.h" |
| 36 |
|
#include "irc_string.h" |
| 38 |
– |
#include "irc_getnameinfo.h" |
| 39 |
– |
#include "irc_getaddrinfo.h" |
| 37 |
|
#include "ircd.h" |
| 41 |
– |
#include "list.h" |
| 38 |
|
#include "listener.h" |
| 39 |
|
#include "numeric.h" |
| 40 |
|
#include "packet.h" |
| 41 |
|
#include "irc_res.h" |
| 46 |
– |
#include "inet_misc.h" |
| 42 |
|
#include "restart.h" |
| 43 |
|
#include "s_auth.h" |
| 44 |
|
#include "s_conf.h" |
| 55 |
|
|
| 56 |
|
struct Callback *setup_socket_cb = NULL; |
| 57 |
|
|
| 58 |
< |
static void comm_connect_callback(fde_t *fd, int status); |
| 58 |
> |
static void comm_connect_callback(fde_t *, int); |
| 59 |
|
static PF comm_connect_timeout; |
| 60 |
< |
static void comm_connect_dns_callback(void *vptr, struct DNSReply *reply); |
| 60 |
> |
static void comm_connect_dns_callback(void *, const struct irc_ssaddr *, const char *); |
| 61 |
|
static PF comm_connect_tryconnect; |
| 62 |
|
|
| 68 |
– |
extern void init_netio(void); |
| 63 |
|
|
| 64 |
|
/* check_can_use_v6() |
| 65 |
|
* Check if the system can open AF_INET6 sockets |
| 75 |
|
else |
| 76 |
|
{ |
| 77 |
|
ServerInfo.can_use_v6 = 1; |
| 84 |
– |
#ifdef _WIN32 |
| 85 |
– |
closesocket(v6); |
| 86 |
– |
#else |
| 78 |
|
close(v6); |
| 88 |
– |
#endif |
| 79 |
|
} |
| 80 |
|
#else |
| 81 |
|
ServerInfo.can_use_v6 = 0; |
| 91 |
|
int |
| 92 |
|
get_sockerr(int fd) |
| 93 |
|
{ |
| 104 |
– |
#ifndef _WIN32 |
| 94 |
|
int errtmp = errno; |
| 106 |
– |
#else |
| 107 |
– |
int errtmp = WSAGetLastError(); |
| 108 |
– |
#endif |
| 95 |
|
#ifdef SO_ERROR |
| 96 |
|
int err = 0; |
| 97 |
|
socklen_t len = sizeof(err); |
| 98 |
|
|
| 99 |
< |
if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*) &err, (socklen_t *)&len)) |
| 99 |
> |
if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len)) |
| 100 |
|
{ |
| 101 |
|
if (err) |
| 102 |
|
errtmp = err; |
| 146 |
|
int fd = va_arg(args, int); |
| 147 |
|
int opt = 1; |
| 148 |
|
|
| 149 |
< |
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, sizeof(opt)); |
| 149 |
> |
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt)); |
| 150 |
|
|
| 151 |
|
#ifdef IPTOS_LOWDELAY |
| 152 |
|
opt = IPTOS_LOWDELAY; |
| 153 |
< |
setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &opt, sizeof(opt)); |
| 153 |
> |
setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)); |
| 154 |
|
#endif |
| 155 |
|
|
| 170 |
– |
#ifndef _WIN32 |
| 156 |
|
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); |
| 172 |
– |
#endif |
| 157 |
|
|
| 158 |
|
return NULL; |
| 159 |
|
} |
| 195 |
|
send_queued_write(client_p); |
| 196 |
|
} |
| 197 |
|
|
| 198 |
< |
if (IsServer(client_p)) |
| 198 |
> |
if (IsClient(client_p)) |
| 199 |
> |
{ |
| 200 |
> |
++ServerStats.is_cl; |
| 201 |
> |
ServerStats.is_cbs += client_p->localClient->send.bytes; |
| 202 |
> |
ServerStats.is_cbr += client_p->localClient->recv.bytes; |
| 203 |
> |
ServerStats.is_cti += CurrentTime - client_p->firsttime; |
| 204 |
> |
} |
| 205 |
> |
else if (IsServer(client_p)) |
| 206 |
|
{ |
| 207 |
|
++ServerStats.is_sv; |
| 208 |
|
ServerStats.is_sbs += client_p->localClient->send.bytes; |
| 217 |
|
* If the connection has been up for a long amount of time, schedule |
| 218 |
|
* a 'quick' reconnect, else reset the next-connect cycle. |
| 219 |
|
*/ |
| 220 |
< |
if ((conf = find_conf_exact(SERVER_TYPE, |
| 221 |
< |
client_p->name, client_p->username, |
| 231 |
< |
client_p->host))) |
| 220 |
> |
if ((conf = find_conf_exact(SERVER_TYPE, client_p->name, |
| 221 |
> |
client_p->username, client_p->host))) |
| 222 |
|
{ |
| 223 |
|
/* |
| 224 |
|
* Reschedule a faster reconnect, if this was a automatically |
| 226 |
|
* a rehash in between, the status has been changed to |
| 227 |
|
* CONF_ILLEGAL). But only do this if it was a "good" link. |
| 228 |
|
*/ |
| 229 |
< |
aconf = (struct AccessItem *)map_to_conf(conf); |
| 230 |
< |
aclass = (struct ClassItem *)map_to_conf(aconf->class_ptr); |
| 229 |
> |
aconf = map_to_conf(conf); |
| 230 |
> |
aclass = map_to_conf(aconf->class_ptr); |
| 231 |
|
aconf->hold = time(NULL); |
| 232 |
|
aconf->hold += (aconf->hold - client_p->since > HANGONGOODLINK) ? |
| 233 |
|
HANGONRETRYDELAY : ConFreq(aclass); |
| 244 |
– |
if (nextconnect > aconf->hold) |
| 245 |
– |
nextconnect = aconf->hold; |
| 234 |
|
} |
| 235 |
|
} |
| 248 |
– |
else if (IsClient(client_p)) |
| 249 |
– |
{ |
| 250 |
– |
++ServerStats.is_cl; |
| 251 |
– |
ServerStats.is_cbs += client_p->localClient->send.bytes; |
| 252 |
– |
ServerStats.is_cbr += client_p->localClient->recv.bytes; |
| 253 |
– |
ServerStats.is_cti += CurrentTime - client_p->firsttime; |
| 254 |
– |
} |
| 236 |
|
else |
| 237 |
|
++ServerStats.is_ni; |
| 238 |
|
|
| 304 |
|
void |
| 305 |
|
add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd) |
| 306 |
|
{ |
| 307 |
< |
struct Client *new_client; |
| 327 |
< |
|
| 328 |
< |
assert(NULL != listener); |
| 329 |
< |
|
| 330 |
< |
new_client = make_client(NULL); |
| 307 |
> |
struct Client *new_client = make_client(NULL); |
| 308 |
|
|
| 309 |
|
fd_open(&new_client->localClient->fd, fd, 1, |
| 310 |
|
(listener->flags & LISTENER_SSL) ? |
| 311 |
|
"Incoming SSL connection" : "Incoming connection"); |
| 312 |
|
|
| 313 |
< |
/* |
| 313 |
> |
/* |
| 314 |
|
* copy address to 'sockhost' as a string, copy it to host too |
| 315 |
|
* so we have something valid to put into error messages... |
| 316 |
|
*/ |
| 317 |
|
memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr)); |
| 318 |
|
|
| 319 |
< |
irc_getnameinfo((struct sockaddr*)&new_client->localClient->ip, |
| 320 |
< |
new_client->localClient->ip.ss_len, new_client->sockhost, |
| 321 |
< |
HOSTIPLEN, NULL, 0, NI_NUMERICHOST); |
| 319 |
> |
getnameinfo((struct sockaddr *)&new_client->localClient->ip, |
| 320 |
> |
new_client->localClient->ip.ss_len, new_client->sockhost, |
| 321 |
> |
sizeof(new_client->sockhost), NULL, 0, NI_NUMERICHOST); |
| 322 |
|
new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family; |
| 346 |
– |
#ifdef IPV6 |
| 347 |
– |
if (new_client->sockhost[0] == ':') |
| 348 |
– |
strlcat(new_client->host, "0", HOSTLEN+1); |
| 323 |
|
|
| 324 |
< |
if (new_client->localClient->aftype == AF_INET6 && |
| 351 |
< |
ConfigFileEntry.dot_in_ip6_addr == 1) |
| 324 |
> |
if (new_client->sockhost[0] == ':' && new_client->sockhost[1] == ':') |
| 325 |
|
{ |
| 326 |
< |
strlcat(new_client->host, new_client->sockhost,HOSTLEN+1); |
| 327 |
< |
strlcat(new_client->host, ".", HOSTLEN+1); |
| 326 |
> |
strlcpy(new_client->host, "0", sizeof(new_client->host)); |
| 327 |
> |
strlcpy(new_client->host+1, new_client->sockhost, sizeof(new_client->host)-1); |
| 328 |
> |
memmove(new_client->sockhost+1, new_client->sockhost, sizeof(new_client->sockhost)-1); |
| 329 |
> |
new_client->sockhost[0] = '0'; |
| 330 |
|
} |
| 331 |
|
else |
| 332 |
< |
#endif |
| 358 |
< |
strlcat(new_client->host, new_client->sockhost,HOSTLEN+1); |
| 332 |
> |
strlcpy(new_client->host, new_client->sockhost, sizeof(new_client->host)); |
| 333 |
|
|
| 360 |
– |
new_client->connect_id = ++connect_id; |
| 334 |
|
new_client->localClient->listener = listener; |
| 335 |
|
++listener->ref_count; |
| 336 |
|
|
| 337 |
|
#ifdef HAVE_LIBCRYPTO |
| 338 |
|
if (listener->flags & LISTENER_SSL) |
| 339 |
|
{ |
| 340 |
< |
if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.ctx)) == NULL) |
| 340 |
> |
if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL) |
| 341 |
|
{ |
| 342 |
|
ilog(L_CRIT, "SSL_new() ERROR! -- %s", |
| 343 |
|
ERR_error_string(ERR_get_error(), NULL)); |
| 482 |
|
void *data, int aftype, int timeout) |
| 483 |
|
{ |
| 484 |
|
struct addrinfo hints, *res; |
| 485 |
< |
char portname[PORTNAMELEN+1]; |
| 485 |
> |
char portname[PORTNAMELEN + 1]; |
| 486 |
|
|
| 487 |
|
assert(callback); |
| 488 |
|
fd->connect.callback = callback; |
| 514 |
|
hints.ai_socktype = SOCK_STREAM; |
| 515 |
|
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; |
| 516 |
|
|
| 517 |
< |
snprintf(portname, PORTNAMELEN, "%d", port); |
| 517 |
> |
snprintf(portname, sizeof(portname), "%d", port); |
| 518 |
|
|
| 519 |
< |
if (irc_getaddrinfo(host, portname, &hints, &res)) |
| 519 |
> |
if (getaddrinfo(host, portname, &hints, &res)) |
| 520 |
|
{ |
| 521 |
|
/* Send the DNS request, for the next level */ |
| 549 |
– |
fd->dns_query = MyMalloc(sizeof(struct DNSQuery)); |
| 550 |
– |
fd->dns_query->ptr = fd; |
| 551 |
– |
fd->dns_query->callback = comm_connect_dns_callback; |
| 522 |
|
if (aftype == AF_INET6) |
| 523 |
< |
gethost_byname_type(host, fd->dns_query, T_AAAA); |
| 523 |
> |
gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA); |
| 524 |
|
else |
| 525 |
< |
gethost_byname_type(host, fd->dns_query, T_A); |
| 525 |
> |
gethost_byname_type(comm_connect_dns_callback, fd, host, T_A); |
| 526 |
|
} |
| 527 |
|
else |
| 528 |
|
{ |
| 532 |
|
memcpy(&fd->connect.hostaddr, res->ai_addr, res->ai_addrlen); |
| 533 |
|
fd->connect.hostaddr.ss_len = res->ai_addrlen; |
| 534 |
|
fd->connect.hostaddr.ss.ss_family = res->ai_family; |
| 535 |
< |
irc_freeaddrinfo(res); |
| 535 |
> |
freeaddrinfo(res); |
| 536 |
|
comm_settimeout(fd, timeout*1000, comm_connect_timeout, NULL); |
| 537 |
|
comm_connect_tryconnect(fd, NULL); |
| 538 |
|
} |
| 580 |
|
* otherwise we initiate the connect() |
| 581 |
|
*/ |
| 582 |
|
static void |
| 583 |
< |
comm_connect_dns_callback(void *vptr, struct DNSReply *reply) |
| 583 |
> |
comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name) |
| 584 |
|
{ |
| 585 |
|
fde_t *F = vptr; |
| 586 |
|
|
| 587 |
< |
if (reply == NULL) |
| 587 |
> |
if (name == NULL) |
| 588 |
|
{ |
| 619 |
– |
MyFree(F->dns_query); |
| 620 |
– |
F->dns_query = NULL; |
| 589 |
|
comm_connect_callback(F, COMM_ERR_DNS); |
| 590 |
|
return; |
| 591 |
|
} |
| 599 |
|
* the DNS record around, and the DNS cache is gone anyway.. |
| 600 |
|
* -- adrian |
| 601 |
|
*/ |
| 602 |
< |
memcpy(&F->connect.hostaddr, &reply->addr, reply->addr.ss_len); |
| 602 |
> |
memcpy(&F->connect.hostaddr, addr, addr->ss_len); |
| 603 |
|
/* The cast is hacky, but safe - port offset is same on v4 and v6 */ |
| 604 |
|
((struct sockaddr_in *) &F->connect.hostaddr)->sin_port = |
| 605 |
|
F->connect.hostaddr.ss_port; |
| 606 |
< |
F->connect.hostaddr.ss_len = reply->addr.ss_len; |
| 606 |
> |
F->connect.hostaddr.ss_len = addr->ss_len; |
| 607 |
|
|
| 608 |
|
/* Now, call the tryconnect() routine to try a connect() */ |
| 641 |
– |
MyFree(F->dns_query); |
| 642 |
– |
F->dns_query = NULL; |
| 609 |
|
comm_connect_tryconnect(F, NULL); |
| 610 |
|
} |
| 611 |
|
|
| 633 |
|
/* Error? */ |
| 634 |
|
if (retval < 0) |
| 635 |
|
{ |
| 670 |
– |
#ifdef _WIN32 |
| 671 |
– |
errno = WSAGetLastError(); |
| 672 |
– |
#endif |
| 636 |
|
/* |
| 637 |
|
* If we get EISCONN, then we've already connect()ed the socket, |
| 638 |
|
* which is a good thing. |
| 691 |
|
*/ |
| 692 |
|
fd = socket(family, sock_type, proto); |
| 693 |
|
if (fd < 0) |
| 731 |
– |
{ |
| 732 |
– |
#ifdef _WIN32 |
| 733 |
– |
errno = WSAGetLastError(); |
| 734 |
– |
#endif |
| 694 |
|
return -1; /* errno will be passed through, yay.. */ |
| 736 |
– |
} |
| 695 |
|
|
| 696 |
|
execute_callback(setup_socket_cb, fd); |
| 697 |
|
|
| 724 |
|
* reserved fd limit, but we can deal with that when comm_open() |
| 725 |
|
* also does it. XXX -- adrian |
| 726 |
|
*/ |
| 727 |
< |
newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, (socklen_t *)&addrlen); |
| 727 |
> |
newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, &addrlen); |
| 728 |
|
if (newfd < 0) |
| 771 |
– |
{ |
| 772 |
– |
#ifdef _WIN32 |
| 773 |
– |
errno = WSAGetLastError(); |
| 774 |
– |
#endif |
| 729 |
|
return -1; |
| 776 |
– |
} |
| 730 |
|
|
| 731 |
|
#ifdef IPV6 |
| 732 |
|
remove_ipv6_mapping(pn); |
| 742 |
|
|
| 743 |
|
/* |
| 744 |
|
* remove_ipv6_mapping() - Removes IPv4-In-IPv6 mapping from an address |
| 745 |
< |
* This function should really inspect the struct itself rather than relying |
| 793 |
< |
* on inet_pton and inet_ntop. OSes with IPv6 mapping listening on both |
| 745 |
> |
* OSes with IPv6 mapping listening on both |
| 746 |
|
* AF_INET and AF_INET6 map AF_INET connections inside AF_INET6 structures |
| 747 |
|
* |
| 748 |
|
*/ |
| 752 |
|
{ |
| 753 |
|
if (addr->ss.ss_family == AF_INET6) |
| 754 |
|
{ |
| 755 |
< |
struct sockaddr_in6 *v6; |
| 804 |
< |
|
| 805 |
< |
v6 = (struct sockaddr_in6*)addr; |
| 806 |
< |
if (IN6_IS_ADDR_V4MAPPED(&v6->sin6_addr)) |
| 755 |
> |
if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)addr)->sin6_addr)) |
| 756 |
|
{ |
| 757 |
< |
char v4ip[HOSTIPLEN]; |
| 758 |
< |
struct sockaddr_in *v4 = (struct sockaddr_in*)addr; |
| 759 |
< |
inetntop(AF_INET6, &v6->sin6_addr, v4ip, HOSTIPLEN); |
| 760 |
< |
inet_pton(AF_INET, v4ip, &v4->sin_addr); |
| 757 |
> |
struct sockaddr_in6 v6; |
| 758 |
> |
struct sockaddr_in *v4 = (struct sockaddr_in *)addr; |
| 759 |
> |
|
| 760 |
> |
memcpy(&v6, addr, sizeof(v6)); |
| 761 |
> |
memset(v4, 0, sizeof(struct sockaddr_in)); |
| 762 |
> |
memcpy(&v4->sin_addr, &v6.sin6_addr.s6_addr[12], sizeof(v4->sin_addr)); |
| 763 |
> |
|
| 764 |
|
addr->ss.ss_family = AF_INET; |
| 765 |
|
addr->ss_len = sizeof(struct sockaddr_in); |
| 766 |
|
} |