| 19 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 20 |
|
* USA |
| 21 |
|
* |
| 22 |
< |
* $Id: s_bsd.c,v 7.254 2005/09/29 00:13:34 adx Exp $ |
| 22 |
> |
* $Id$ |
| 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> |
| 30 |
– |
#endif |
| 29 |
|
#include "fdlist.h" |
| 30 |
|
#include "s_bsd.h" |
| 31 |
|
#include "client.h" |
| 47 |
|
#include "s_conf.h" |
| 48 |
|
#include "s_log.h" |
| 49 |
|
#include "s_serv.h" |
| 52 |
– |
#include "s_stats.h" |
| 50 |
|
#include "send.h" |
| 51 |
|
#include "memory.h" |
| 52 |
|
#include "s_user.h" |
| 60 |
|
|
| 61 |
|
static void comm_connect_callback(fde_t *fd, int status); |
| 62 |
|
static PF comm_connect_timeout; |
| 63 |
< |
static void comm_connect_dns_callback(void *vptr, struct DNSReply *reply); |
| 63 |
> |
static void comm_connect_dns_callback(void *, const struct irc_ssaddr *, const char *); |
| 64 |
|
static PF comm_connect_tryconnect; |
| 65 |
|
|
| 69 |
– |
extern void init_netio(void); |
| 66 |
|
|
| 67 |
|
/* check_can_use_v6() |
| 68 |
|
* Check if the system can open AF_INET6 sockets |
| 78 |
|
else |
| 79 |
|
{ |
| 80 |
|
ServerInfo.can_use_v6 = 1; |
| 85 |
– |
#ifdef _WIN32 |
| 86 |
– |
closesocket(v6); |
| 87 |
– |
#else |
| 81 |
|
close(v6); |
| 89 |
– |
#endif |
| 82 |
|
} |
| 83 |
|
#else |
| 84 |
|
ServerInfo.can_use_v6 = 0; |
| 94 |
|
int |
| 95 |
|
get_sockerr(int fd) |
| 96 |
|
{ |
| 105 |
– |
#ifndef _WIN32 |
| 97 |
|
int errtmp = errno; |
| 107 |
– |
#else |
| 108 |
– |
int errtmp = WSAGetLastError(); |
| 109 |
– |
#endif |
| 98 |
|
#ifdef SO_ERROR |
| 99 |
|
int err = 0; |
| 100 |
|
socklen_t len = sizeof(err); |
| 101 |
|
|
| 102 |
< |
if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*) &err, (socklen_t *)&len)) |
| 102 |
> |
if (-1 < fd && !getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len)) |
| 103 |
|
{ |
| 104 |
|
if (err) |
| 105 |
|
errtmp = err; |
| 149 |
|
int fd = va_arg(args, int); |
| 150 |
|
int opt = 1; |
| 151 |
|
|
| 152 |
< |
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, sizeof(opt)); |
| 152 |
> |
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt)); |
| 153 |
|
|
| 154 |
|
#ifdef IPTOS_LOWDELAY |
| 155 |
|
opt = IPTOS_LOWDELAY; |
| 156 |
< |
setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &opt, sizeof(opt)); |
| 156 |
> |
setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)); |
| 157 |
|
#endif |
| 158 |
|
|
| 171 |
– |
#ifndef _WIN32 |
| 159 |
|
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); |
| 173 |
– |
#endif |
| 160 |
|
|
| 161 |
|
return NULL; |
| 162 |
|
} |
| 187 |
|
|
| 188 |
|
assert(NULL != client_p); |
| 189 |
|
|
| 190 |
+ |
if (!IsDead(client_p)) |
| 191 |
+ |
{ |
| 192 |
+ |
/* attempt to flush any pending dbufs. Evil, but .. -- adrian */ |
| 193 |
+ |
/* there is still a chance that we might send data to this socket |
| 194 |
+ |
* even if it is marked as blocked (COMM_SELECT_READ handler is called |
| 195 |
+ |
* before COMM_SELECT_WRITE). Let's try, nothing to lose.. -adx |
| 196 |
+ |
*/ |
| 197 |
+ |
ClearSendqBlocked(client_p); |
| 198 |
+ |
send_queued_write(client_p); |
| 199 |
+ |
} |
| 200 |
+ |
|
| 201 |
|
if (IsServer(client_p)) |
| 202 |
|
{ |
| 203 |
< |
ServerStats->is_sv++; |
| 204 |
< |
ServerStats->is_sbs += client_p->localClient->send.bytes; |
| 205 |
< |
ServerStats->is_sbr += client_p->localClient->recv.bytes; |
| 206 |
< |
ServerStats->is_sti += CurrentTime - client_p->firsttime; |
| 203 |
> |
++ServerStats.is_sv; |
| 204 |
> |
ServerStats.is_sbs += client_p->localClient->send.bytes; |
| 205 |
> |
ServerStats.is_sbr += client_p->localClient->recv.bytes; |
| 206 |
> |
ServerStats.is_sti += CurrentTime - client_p->firsttime; |
| 207 |
|
|
| 208 |
|
/* XXX Does this even make any sense at all anymore? |
| 209 |
|
* scheduling a 'quick' reconnect could cause a pile of |
| 234 |
|
} |
| 235 |
|
else if (IsClient(client_p)) |
| 236 |
|
{ |
| 237 |
< |
ServerStats->is_cl++; |
| 238 |
< |
ServerStats->is_cbs += client_p->localClient->send.bytes; |
| 239 |
< |
ServerStats->is_cbr += client_p->localClient->recv.bytes; |
| 240 |
< |
ServerStats->is_cti += CurrentTime - client_p->firsttime; |
| 237 |
> |
++ServerStats.is_cl; |
| 238 |
> |
ServerStats.is_cbs += client_p->localClient->send.bytes; |
| 239 |
> |
ServerStats.is_cbr += client_p->localClient->recv.bytes; |
| 240 |
> |
ServerStats.is_cti += CurrentTime - client_p->firsttime; |
| 241 |
|
} |
| 242 |
|
else |
| 243 |
< |
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 |
< |
} |
| 243 |
> |
++ServerStats.is_ni; |
| 244 |
|
|
| 245 |
|
#ifdef HAVE_LIBCRYPTO |
| 246 |
|
if (client_p->localClient->fd.ssl) |
| 247 |
< |
SSL_shutdown(client_p->localClient->fd.ssl); |
| 247 |
> |
{ |
| 248 |
> |
SSL_set_shutdown(client_p->localClient->fd.ssl, SSL_RECEIVED_SHUTDOWN); |
| 249 |
> |
|
| 250 |
> |
if (!SSL_shutdown(client_p->localClient->fd.ssl)) |
| 251 |
> |
SSL_shutdown(client_p->localClient->fd.ssl); |
| 252 |
> |
} |
| 253 |
|
#endif |
| 254 |
|
if (client_p->localClient->fd.flags.open) |
| 255 |
|
fd_close(&client_p->localClient->fd); |
| 308 |
|
* any client list yet. |
| 309 |
|
*/ |
| 310 |
|
void |
| 311 |
< |
add_connection(struct Listener* listener, int fd) |
| 311 |
> |
add_connection(struct Listener *listener, struct irc_ssaddr *irn, int fd) |
| 312 |
|
{ |
| 313 |
|
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 |
– |
*/ |
| 314 |
|
|
| 315 |
< |
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 |
< |
} |
| 315 |
> |
assert(NULL != listener); |
| 316 |
|
|
| 349 |
– |
#ifdef IPV6 |
| 350 |
– |
remove_ipv6_mapping(&irn); |
| 351 |
– |
#else |
| 352 |
– |
irn.ss_len = len; |
| 353 |
– |
#endif |
| 317 |
|
new_client = make_client(NULL); |
| 318 |
+ |
|
| 319 |
|
fd_open(&new_client->localClient->fd, fd, 1, |
| 320 |
|
(listener->flags & LISTENER_SSL) ? |
| 321 |
|
"Incoming SSL connection" : "Incoming connection"); |
| 358 |
– |
memset(&new_client->localClient->ip, 0, sizeof(struct irc_ssaddr)); |
| 322 |
|
|
| 323 |
|
/* |
| 324 |
|
* copy address to 'sockhost' as a string, copy it to host too |
| 325 |
|
* so we have something valid to put into error messages... |
| 326 |
|
*/ |
| 327 |
< |
new_client->localClient->port = ntohs(irn.ss_port); |
| 365 |
< |
memcpy(&new_client->localClient->ip, &irn, sizeof(struct irc_ssaddr)); |
| 327 |
> |
memcpy(&new_client->localClient->ip, irn, sizeof(struct irc_ssaddr)); |
| 328 |
|
|
| 329 |
|
irc_getnameinfo((struct sockaddr*)&new_client->localClient->ip, |
| 330 |
|
new_client->localClient->ip.ss_len, new_client->sockhost, |
| 331 |
|
HOSTIPLEN, NULL, 0, NI_NUMERICHOST); |
| 332 |
|
new_client->localClient->aftype = new_client->localClient->ip.ss.ss_family; |
| 371 |
– |
|
| 372 |
– |
*new_client->host = '\0'; |
| 333 |
|
#ifdef IPV6 |
| 334 |
< |
if (*new_client->sockhost == ':') |
| 334 |
> |
if (new_client->sockhost[0] == ':') |
| 335 |
|
strlcat(new_client->host, "0", HOSTLEN+1); |
| 336 |
|
|
| 337 |
|
if (new_client->localClient->aftype == AF_INET6 && |
| 339 |
|
{ |
| 340 |
|
strlcat(new_client->host, new_client->sockhost,HOSTLEN+1); |
| 341 |
|
strlcat(new_client->host, ".", HOSTLEN+1); |
| 342 |
< |
} else |
| 342 |
> |
} |
| 343 |
> |
else |
| 344 |
|
#endif |
| 345 |
|
strlcat(new_client->host, new_client->sockhost,HOSTLEN+1); |
| 346 |
|
|
| 347 |
|
new_client->localClient->listener = listener; |
| 348 |
|
++listener->ref_count; |
| 349 |
|
|
| 389 |
– |
connect_id++; |
| 390 |
– |
new_client->connect_id = connect_id; |
| 391 |
– |
|
| 350 |
|
#ifdef HAVE_LIBCRYPTO |
| 351 |
< |
if ((listener->flags & LISTENER_SSL)) |
| 351 |
> |
if (listener->flags & LISTENER_SSL) |
| 352 |
|
{ |
| 353 |
< |
if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.ctx)) == NULL) |
| 353 |
> |
if ((new_client->localClient->fd.ssl = SSL_new(ServerInfo.server_ctx)) == NULL) |
| 354 |
|
{ |
| 355 |
|
ilog(L_CRIT, "SSL_new() ERROR! -- %s", |
| 356 |
|
ERR_error_string(ERR_get_error(), NULL)); |
| 495 |
|
void *data, int aftype, int timeout) |
| 496 |
|
{ |
| 497 |
|
struct addrinfo hints, *res; |
| 498 |
< |
char portname[PORTNAMELEN+1]; |
| 498 |
> |
char portname[PORTNAMELEN + 1]; |
| 499 |
|
|
| 500 |
|
assert(callback); |
| 501 |
|
fd->connect.callback = callback; |
| 527 |
|
hints.ai_socktype = SOCK_STREAM; |
| 528 |
|
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; |
| 529 |
|
|
| 530 |
< |
snprintf(portname, PORTNAMELEN, "%d", port); |
| 530 |
> |
snprintf(portname, sizeof(portname), "%d", port); |
| 531 |
|
|
| 532 |
|
if (irc_getaddrinfo(host, portname, &hints, &res)) |
| 533 |
|
{ |
| 534 |
|
/* Send the DNS request, for the next level */ |
| 535 |
< |
fd->dns_query = MyMalloc(sizeof(struct DNSQuery)); |
| 536 |
< |
fd->dns_query->ptr = fd; |
| 537 |
< |
fd->dns_query->callback = comm_connect_dns_callback; |
| 538 |
< |
gethost_byname(host, fd->dns_query); |
| 535 |
> |
if (aftype == AF_INET6) |
| 536 |
> |
gethost_byname_type(comm_connect_dns_callback, fd, host, T_AAAA); |
| 537 |
> |
else |
| 538 |
> |
gethost_byname_type(comm_connect_dns_callback, fd, host, T_A); |
| 539 |
|
} |
| 540 |
|
else |
| 541 |
|
{ |
| 593 |
|
* otherwise we initiate the connect() |
| 594 |
|
*/ |
| 595 |
|
static void |
| 596 |
< |
comm_connect_dns_callback(void *vptr, struct DNSReply *reply) |
| 596 |
> |
comm_connect_dns_callback(void *vptr, const struct irc_ssaddr *addr, const char *name) |
| 597 |
|
{ |
| 598 |
|
fde_t *F = vptr; |
| 599 |
|
|
| 600 |
< |
if (reply == NULL) |
| 600 |
> |
if (name == NULL) |
| 601 |
|
{ |
| 644 |
– |
MyFree(F->dns_query); |
| 645 |
– |
F->dns_query = NULL; |
| 602 |
|
comm_connect_callback(F, COMM_ERR_DNS); |
| 603 |
|
return; |
| 604 |
|
} |
| 612 |
|
* the DNS record around, and the DNS cache is gone anyway.. |
| 613 |
|
* -- adrian |
| 614 |
|
*/ |
| 615 |
< |
memcpy(&F->connect.hostaddr, &reply->addr, reply->addr.ss_len); |
| 615 |
> |
memcpy(&F->connect.hostaddr, addr, addr->ss_len); |
| 616 |
|
/* The cast is hacky, but safe - port offset is same on v4 and v6 */ |
| 617 |
|
((struct sockaddr_in *) &F->connect.hostaddr)->sin_port = |
| 618 |
|
F->connect.hostaddr.ss_port; |
| 619 |
< |
F->connect.hostaddr.ss_len = reply->addr.ss_len; |
| 619 |
> |
F->connect.hostaddr.ss_len = addr->ss_len; |
| 620 |
|
|
| 621 |
|
/* Now, call the tryconnect() routine to try a connect() */ |
| 666 |
– |
MyFree(F->dns_query); |
| 667 |
– |
F->dns_query = NULL; |
| 622 |
|
comm_connect_tryconnect(F, NULL); |
| 623 |
|
} |
| 624 |
|
|
| 646 |
|
/* Error? */ |
| 647 |
|
if (retval < 0) |
| 648 |
|
{ |
| 695 |
– |
#ifdef _WIN32 |
| 696 |
– |
errno = WSAGetLastError(); |
| 697 |
– |
#endif |
| 649 |
|
/* |
| 650 |
|
* If we get EISCONN, then we've already connect()ed the socket, |
| 651 |
|
* which is a good thing. |
| 704 |
|
*/ |
| 705 |
|
fd = socket(family, sock_type, proto); |
| 706 |
|
if (fd < 0) |
| 756 |
– |
{ |
| 757 |
– |
#ifdef _WIN32 |
| 758 |
– |
errno = WSAGetLastError(); |
| 759 |
– |
#endif |
| 707 |
|
return -1; /* errno will be passed through, yay.. */ |
| 761 |
– |
} |
| 708 |
|
|
| 709 |
|
execute_callback(setup_socket_cb, fd); |
| 710 |
|
|
| 739 |
|
*/ |
| 740 |
|
newfd = accept(lptr->fd.fd, (struct sockaddr *)pn, (socklen_t *)&addrlen); |
| 741 |
|
if (newfd < 0) |
| 796 |
– |
{ |
| 797 |
– |
#ifdef _WIN32 |
| 798 |
– |
errno = WSAGetLastError(); |
| 799 |
– |
#endif |
| 742 |
|
return -1; |
| 801 |
– |
} |
| 743 |
|
|
| 744 |
|
#ifdef IPV6 |
| 745 |
|
remove_ipv6_mapping(pn); |