Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 705 lines (614 sloc) 16.352 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
/*
* atheme-services: A collection of minimalist IRC services
* connection.c: Connection and I/O management
*
* Copyright (c) 2005-2007 Atheme Project (http://www.atheme.org)
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "atheme.h"
#include "internal.h"
#include "datastream.h"

mowgli_list_t connection_list;

#ifdef _WIN32
# define EWOULDBLOCK WSAEWOULDBLOCK
# define EINPROGRESS WSAEINPROGRESS
# define SHUT_RDWR SD_BOTH
#endif

static int socket_setnonblocking(mowgli_descriptor_t sck)
{
#ifndef _WIN32
int flags;

slog(LG_DEBUG, "socket_setnonblocking(): setting file descriptor %d as non-blocking", sck);

flags = fcntl(sck, F_GETFL, 0);
flags |= O_NONBLOCK;

if (fcntl(sck, F_SETFL, flags))
return -1;

return 0;
#else
u_long yes = 1;

ioctlsocket(sck, FIONBIO, &yes);
#endif
}

/*
* connection_trampoline()
*
* inputs:
* mowgli.eventloop object
* mowgli.eventloop.pollable object
* poll direction
* user-specified data
*
* outputs:
* none
*
* side effects:
* whatever happens from the connection_t i/o handlers
*/
static void connection_trampoline(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io,
mowgli_eventloop_io_dir_t dir, void *userdata)
{
connection_t *cptr = userdata;

switch (dir) {
case MOWGLI_EVENTLOOP_IO_READ:
return cptr->read_handler(cptr);
break;
case MOWGLI_EVENTLOOP_IO_WRITE:
default:
return cptr->write_handler(cptr);
break;
}
}

/*
* connection_add()
*
* inputs:
* connection name, file descriptor, flag bitmask,
* read handler, write handler.
*
* outputs:
* a connection object for the connection information given.
*
* side effects:
* a connection is added to the socket queue.
*/
connection_t *connection_add(const char *name, int fd, unsigned int flags,
void (*read_handler)(connection_t *),
void (*write_handler)(connection_t *))
{
connection_t *cptr;

if ((cptr = connection_find(fd)))
{
slog(LG_DEBUG, "connection_add(): connection %d is already registered as %s",
fd, cptr->name);
return NULL;
}

slog(LG_DEBUG, "connection_add(): adding connection '%s', fd=%d", name, fd);

cptr = smalloc(sizeof(connection_t));

cptr->fd = fd;
cptr->pollslot = -1;
cptr->flags = flags;
cptr->first_recv = CURRTIME;
cptr->last_recv = CURRTIME;

cptr->pollable = mowgli_pollable_create(base_eventloop, cptr->fd, cptr);

connection_setselect_read(cptr, read_handler);
connection_setselect_write(cptr, write_handler);

/* set connection name */
mowgli_strlcpy(cptr->name, name, HOSTLEN);

#ifndef _WIN32
if (cptr->fd > -1)
{
cptr->saddr_size = sizeof(cptr->saddr);
getpeername(cptr->fd, &cptr->saddr.sa, &cptr->saddr_size);

inet_ntop(cptr->saddr.sa.sa_family,
&cptr->saddr.sin6.sin6_addr,
cptr->hbuf, BUFSIZE);

socket_setnonblocking(cptr->fd);
}
#endif

mowgli_node_add(cptr, mowgli_node_create(), &connection_list);

return cptr;
}

/*
* connection_find()
*
* inputs:
* the file descriptor to search by
*
* outputs:
* the connection_t object associated with that fd
*
* side effects:
* none
*/
connection_t *connection_find(int fd)
{
connection_t *cptr;
mowgli_node_t *nptr;

MOWGLI_ITER_FOREACH(nptr, connection_list.head)
{
cptr = nptr->data;

if (cptr->fd == fd)
return cptr;
}

return NULL;
}

/*
* connection_close()
*
* inputs:
* the connection being closed.
*
* outputs:
* none
*
* side effects:
* the connection is closed.
*/
void connection_close(connection_t *cptr)
{
mowgli_node_t *nptr;
int errno1, errno2;
#ifdef SO_ERROR
socklen_t len = sizeof(errno2);
#endif

if (!cptr)
{
slog(LG_DEBUG, "connection_close(): no connection to close!");
return;
}

nptr = mowgli_node_find(cptr, &connection_list);
if (!nptr)
{
slog(LG_ERROR, "connection_close(): connection %p is not registered!",
cptr);
return;
}

errno1 = ioerrno();
#ifdef SO_ERROR
if (!getsockopt(cptr->fd, SOL_SOCKET, SO_ERROR, (char *) &errno2, (socklen_t *) &len))
{
if (errno2 != 0)
errno1 = errno2;
}
#endif

if (errno1)
slog(cptr->flags & CF_UPLINK ? LG_ERROR : LG_DEBUG,
"connection_close(): connection %s[%d] closed due to error %d (%s)",
cptr->name, cptr->fd, errno1, strerror(errno1));

if (cptr->close_handler)
cptr->close_handler(cptr);

/* close the fd */
mowgli_pollable_destroy(base_eventloop, cptr->pollable);

shutdown(cptr->fd, SHUT_RDWR);
close(cptr->fd);

mowgli_node_delete(nptr, &connection_list);
mowgli_node_free(nptr);

sendqrecvq_free(cptr);

free(cptr);
}

/* This one is only safe for use by connection_close_soon(),
* it will cause infinite loops otherwise
*/
static void empty_handler(connection_t *cptr)
{
}

/*
* connection_close_soon()
*
* inputs:
* the connection being closed.
*
* outputs:
* none
*
* side effects:
* the connection is marked to be closed soon
* handlers reset
* close_handler called
*/
void connection_close_soon(connection_t *cptr)
{
if (cptr == NULL)
return;
cptr->flags |= CF_DEAD;
/* these two cannot be NULL */
cptr->read_handler = empty_handler;
cptr->write_handler = empty_handler;
cptr->recvq_handler = NULL;
if (cptr->close_handler)
cptr->close_handler(cptr);
cptr->close_handler = NULL;
cptr->listener = NULL;
}

/*
* connection_close_soon_children()
*
* inputs:
* a listener.
*
* outputs:
* none
*
* side effects:
* connection_close_soon() called on the connection itself and
* for all connections accepted on this listener
*/
void connection_close_soon_children(connection_t *cptr)
{
mowgli_node_t *n;
connection_t *cptr2;

if (cptr == NULL)
return;

if (CF_IS_LISTENING(cptr))
{
MOWGLI_ITER_FOREACH(n, connection_list.head)
{
cptr2 = n->data;
if (cptr2->listener == cptr)
connection_close_soon(cptr2);
}
}
connection_close_soon(cptr);
}

/*
* connection_close_all()
*
* inputs:
* none
*
* outputs:
* none
*
* side effects:
* connection_close() called on all registered connections
*/
void connection_close_all(void)
{
mowgli_node_t *n, *tn;
connection_t *cptr;

MOWGLI_ITER_FOREACH_SAFE(n, tn, connection_list.head)
{
cptr = n->data;
connection_close(cptr);
}
}

/*
* connection_close_all_fds()
*
* Close all file descriptors of the connection subsystem in a child process.
*
* inputs:
* none
*
* outputs:
* none
*
* side effects:
* file descriptors belonging to all connections are closed
* no handlers called
*/
void connection_close_all_fds(void)
{
mowgli_node_t *n, *tn;
connection_t *cptr;

MOWGLI_ITER_FOREACH_SAFE(n, tn, connection_list.head)
{
cptr = n->data;
close(cptr->fd);
}
}

/*
* connection_open_tcp()
*
* inputs:
* hostname to connect to, vhost to use, port,
* read handler, write handler
*
* outputs:
* the connection_t on success, NULL on failure.
*
* side effects:
* a TCP/IP connection is opened to the host,
* and interest is registered in read/write events.
*/
connection_t *connection_open_tcp(char *host, char *vhost, unsigned int port,
void (*read_handler)(connection_t *),
void (*write_handler)(connection_t *))
{
connection_t *cptr;
char buf[BUFSIZE];
struct addrinfo *addr = NULL;
int s, error;
unsigned int optval;

snprintf(buf, BUFSIZE, "tcp connection: %s", host);

/* resolve it */
if ((error = getaddrinfo(host, NULL, NULL, &addr)))
{
slog(LG_ERROR, "connection_open_tcp(): unable to resolve %s: %s", host, gai_strerror(error));
return NULL;
}

/* some getaddrinfo() do this... */
if (addr->ai_addr == NULL)
{
slog(LG_ERROR, "connection_open_tcp(): host %s is not resolveable.", vhost);
freeaddrinfo(addr);
return NULL;
}

if (!(s = socket(addr->ai_family, SOCK_STREAM, 0)))
{
slog(LG_ERROR, "connection_open_tcp(): unable to create socket.");
freeaddrinfo(addr);
return NULL;
}

/* Has the highest fd gotten any higher yet? */
if (s > claro_state.maxfd)
claro_state.maxfd = s;

if (vhost != NULL)
{
struct addrinfo hints, *bind_addr = NULL;

memset(&hints, 0, sizeof hints);
hints.ai_family = addr->ai_family;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ((error = getaddrinfo(vhost, NULL, &hints, &bind_addr)))
{
slog(LG_ERROR, "connection_open_tcp(): cant resolve vhost %s: %s", vhost, gai_strerror(error));
close(s);
freeaddrinfo(addr);
return NULL;
}

/* some getaddrinfo() do this... */
if (bind_addr->ai_addr == NULL)
{
slog(LG_ERROR, "connection_open_tcp(): vhost %s is not resolveable.", vhost);
close(s);
freeaddrinfo(addr);
freeaddrinfo(bind_addr);
return NULL;
}

optval = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&optval, sizeof(optval));

if (bind(s, bind_addr->ai_addr, bind_addr->ai_addrlen) < 0)
{
slog(LG_ERROR, "connection_open_tcp(): unable to bind to vhost %s: %s", vhost, strerror(ioerrno()));
close(s);
freeaddrinfo(addr);
freeaddrinfo(bind_addr);
return NULL;
}

freeaddrinfo(bind_addr);
}

socket_setnonblocking(s);

switch(addr->ai_family)
{
case AF_INET:
((struct sockaddr_in *) addr->ai_addr)->sin_port = htons(port);
break;
case AF_INET6:
((struct sockaddr_in6 *) addr->ai_addr)->sin6_port = htons(port);
break;
}

if ((connect(s, addr->ai_addr, addr->ai_addrlen) == -1) && ioerrno() != EINPROGRESS && ioerrno() != EINTR && ioerrno() != EWOULDBLOCK)
{
if (vhost)
slog(LG_ERROR, "connection_open_tcp(): failed to connect to %s using vhost %s: %d (%s)", host, vhost, ioerrno(), strerror(ioerrno()));
else
slog(LG_ERROR, "connection_open_tcp(): failed to connect to %s: %d (%s)", host, ioerrno(), strerror(ioerrno()));
close(s);
return NULL;
}

freeaddrinfo(addr);

cptr = connection_add(buf, s, CF_CONNECTING, read_handler, write_handler);

return cptr;
}

/*
* connection_open_listener_tcp()
*
* inputs:
* host and port to listen on,
* accept handler
*
* outputs:
* the connection_t on success, NULL on failure.
*
* side effects:
* a TCP/IP connection is opened to the host,
* and interest is registered in read/write events.
*/
connection_t *connection_open_listener_tcp(char *host, unsigned int port,
void (*read_handler)(connection_t *))
{
connection_t *cptr;
char buf[BUFSIZE];
struct addrinfo *addr;
int s, error;
unsigned int optval;

/* resolve it */
if ((error = getaddrinfo(host, NULL, NULL, &addr)))
{
slog(LG_ERROR, "connection_open_listener_tcp(): unable to resolve %s: %s", host, gai_strerror(error));
return NULL;
}

/* some getaddrinfo() do this... */
if (addr->ai_addr == NULL)
{
slog(LG_ERROR, "connection_open_listener_tcp(): host %s is not resolveable.", host);
freeaddrinfo(addr);
return NULL;
}

if (!(s = socket(addr->ai_family, SOCK_STREAM, 0)))
{
slog(LG_ERROR, "connection_open_listener_tcp(): unable to create socket.");
freeaddrinfo(addr);
return NULL;
}

/* Has the highest fd gotten any higher yet? */
if (s > claro_state.maxfd)
claro_state.maxfd = s;

snprintf(buf, BUFSIZE, "listener: %s[%d]", host, port);

optval = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&optval, sizeof(optval));

switch(addr->ai_family)
{
case AF_INET:
((struct sockaddr_in *) addr->ai_addr)->sin_port = htons(port);
break;
case AF_INET6:
((struct sockaddr_in6 *) addr->ai_addr)->sin6_port = htons(port);
break;
}

if (bind(s, addr->ai_addr, addr->ai_addrlen) < 0)
{
close(s);
slog(LG_ERROR, "connection_open_listener_tcp(): unable to bind listener %s[%d], errno [%d]", host, port,
ioerrno());
freeaddrinfo(addr);
return NULL;
}

freeaddrinfo(addr);

socket_setnonblocking(s);

/* XXX we need to have some sort of handling for SOMAXCONN */
if (listen(s, 5) < 0)
{
close(s);
slog(LG_ERROR, "connection_open_listener_tcp(): error: %s", strerror(ioerrno()));
return NULL;
}

cptr = connection_add(buf, s, CF_LISTENING, read_handler, NULL);

return cptr;
}

/*
* connection_accept_tcp()
*
* inputs:
* listener to accept from, read handler, write handler
*
* outputs:
* the connection_t on success, NULL on failure.
*
* side effects:
* a TCP/IP connection is accepted from the host,
* and interest is registered in read/write events.
*/
connection_t *connection_accept_tcp(connection_t *cptr,
void (*read_handler)(connection_t *),
void (*write_handler)(connection_t *))
{
char buf[BUFSIZE];
connection_t *newptr;
int s;

if (!(s = accept(cptr->fd, NULL, NULL)))
{
slog(LG_INFO, "connection_accept_tcp(): accept failed");
return NULL;
}

/* Has the highest fd gotten any higher yet? */
if (s > claro_state.maxfd)
claro_state.maxfd = s;

socket_setnonblocking(s);

mowgli_strlcpy(buf, "incoming connection", BUFSIZE);
newptr = connection_add(buf, s, 0, read_handler, write_handler);
newptr->listener = cptr;
return newptr;
}

/*
* connection_setselect_read()
*
* inputs:
* connection_t to register/deregister interest on,
* replacement handler (NULL = no interest)
*
* outputs:
* none
*
* side effects:
* the read handler is changed for the connection_t.
*/
void connection_setselect_read(connection_t *cptr,
void (*read_handler)(connection_t *))
{
cptr->read_handler = read_handler;
mowgli_pollable_setselect(base_eventloop, cptr->pollable, MOWGLI_EVENTLOOP_IO_READ, cptr->read_handler != NULL ? connection_trampoline : NULL);
}

/*
* connection_setselect_write()
*
* inputs:
* connection_t to register/deregister interest on,
* replacement handler (NULL = no interest)
*
* outputs:
* none
*
* side effects:
* the write handler is changed for the connection_t.
*/
void connection_setselect_write(connection_t *cptr,
void (*write_handler)(connection_t *))
{
cptr->write_handler = write_handler;
mowgli_pollable_setselect(base_eventloop, cptr->pollable, MOWGLI_EVENTLOOP_IO_WRITE, cptr->write_handler != NULL ? connection_trampoline : NULL);
}

/*
* connection_stats()
*
* inputs:
* callback function, data for callback function
*
* outputs:
* none
*
* side effects:
* callback function is called with a series of lines
*/
void connection_stats(void (*stats_cb)(const char *, void *), void *privdata)
{
mowgli_node_t *n;
char buf[160];
char buf2[20];

MOWGLI_ITER_FOREACH(n, connection_list.head)
{
connection_t *c = (connection_t *) n->data;

snprintf(buf, sizeof buf, "fd %-3d desc '%s'", c->fd, c->name);
if (c->listener != NULL)
{
snprintf(buf2, sizeof buf2, " listener %d", c->listener->fd);
mowgli_strlcat(buf, buf2, sizeof buf);
}
if (c->flags & (CF_CONNECTING | CF_DEAD | CF_NONEWLINE | CF_SEND_EOF | CF_SEND_DEAD))
{
mowgli_strlcat(buf, " status", sizeof buf);
if (c->flags & CF_CONNECTING)
mowgli_strlcat(buf, " connecting", sizeof buf);
if (c->flags & CF_DEAD)
mowgli_strlcat(buf, " dead", sizeof buf);
if (c->flags & CF_NONEWLINE)
mowgli_strlcat(buf, " nonewline", sizeof buf);
if (c->flags & CF_SEND_DEAD)
mowgli_strlcat(buf, " send_dead", sizeof buf);
else if (c->flags & CF_SEND_EOF)
mowgli_strlcat(buf, " send_eof", sizeof buf);
}
stats_cb(buf, privdata);
}
}

/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
* vim:ts=8
* vim:sw=8
* vim:noexpandtab
*/
Something went wrong with that request. Please try again.