1 /* 2 * util/netevent.h - event notification 3 * 4 * Copyright (c) 2007, NLnet Labs. All rights reserved. 5 * 6 * This software is open source. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * Redistributions of source code must retain the above copyright notice, 13 * this list of conditions and the following disclaimer. 14 * 15 * Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 19 * Neither the name of the NLNET LABS nor the names of its contributors may 20 * be used to endorse or promote products derived from this software without 21 * specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 /** 37 * \file 38 * 39 * This file contains event notification functions. 40 * 41 * There are three types of communication points 42 * o UDP socket - perthread buffer. 43 * o TCP-accept socket - array of TCP-sockets, socketcount. 44 * o TCP socket - own buffer, parent-TCPaccept, read/write state, 45 * number of bytes read/written, timeout. 46 * 47 * There are sockets aimed towards our clients and towards the internet. 48 * o frontside - aimed towards our clients, queries come in, answers back. 49 * o behind - aimed towards internet, to the authoritative DNS servers. 50 * 51 * Several event types are available: 52 * o comm_base - for thread safety of the comm points, one per thread. 53 * o comm_point - udp and tcp networking, with callbacks. 54 * o comm_timer - a timeout with callback. 55 * o comm_signal - callbacks when signal is caught. 56 * o comm_reply - holds reply info during networking callback. 57 * 58 */ 59 60 #ifndef NET_EVENT_H 61 #define NET_EVENT_H 62 63 #include <ldns/buffer.h> 64 struct comm_point; 65 struct comm_reply; 66 struct event_base; 67 68 /* internal event notification data storage structure. */ 69 struct internal_event; 70 struct internal_base; 71 struct internal_timer; 72 73 /** callback from communication point function type */ 74 typedef int comm_point_callback_t(struct comm_point*, void*, int, 75 struct comm_reply*); 76 77 /** to pass no_error to callback function */ 78 #define NETEVENT_NOERROR 0 79 /** to pass closed connection to callback function */ 80 #define NETEVENT_CLOSED -1 81 /** to pass timeout happened to callback function */ 82 #define NETEVENT_TIMEOUT -2 83 /** to pass fallback from capsforID to callback function; 0x20 failed */ 84 #define NETEVENT_CAPSFAIL -3 85 86 /** 87 * A communication point dispatcher. Thread specific. 88 */ 89 struct comm_base { 90 /** behind the scenes structure. with say libevent info. alloced */ 91 struct internal_base* eb; 92 }; 93 94 /** 95 * Reply information for a communication point. 96 */ 97 struct comm_reply { 98 /** the comm_point with fd to send reply on to. */ 99 struct comm_point* c; 100 /** the address (for UDP based communication) */ 101 struct sockaddr_storage addr; 102 /** length of address */ 103 socklen_t addrlen; 104 /** return type 0 (none), 4(IP4), 6(IP6) */ 105 int srctype; 106 /** the return source interface data */ 107 union { 108 #ifdef IPV6_PKTINFO 109 struct in6_pktinfo v6info; 110 #endif 111 #ifdef IP_PKTINFO 112 struct in_pktinfo v4info; 113 #elif defined(IP_RECVDSTADDR) 114 struct in_addr v4addr; 115 #endif 116 } 117 /** variable with return source data */ 118 pktinfo; 119 }; 120 121 /** 122 * Communication point to the network 123 * These behaviours can be accomplished by setting the flags 124 * and passing return values from the callback. 125 * udp frontside: called after readdone. sendafter. 126 * tcp frontside: called readdone, sendafter. close. 127 * udp behind: called after readdone. No send after. 128 * tcp behind: write done, read done, then called. No send after. 129 */ 130 struct comm_point { 131 /** behind the scenes structure, with say libevent info. alloced. */ 132 struct internal_event* ev; 133 134 /** file descriptor for communication point */ 135 int fd; 136 137 /** timeout (NULL if it does not). Malloced. */ 138 struct timeval* timeout; 139 140 /** buffer pointer. Either to perthread, or own buffer or NULL */ 141 ldns_buffer* buffer; 142 143 /* -------- TCP Handler -------- */ 144 /** Read/Write state for TCP */ 145 int tcp_is_reading; 146 /** The current read/write count for TCP */ 147 size_t tcp_byte_count; 148 /** parent communication point (for TCP sockets) */ 149 struct comm_point* tcp_parent; 150 /** sockaddr from peer, for TCP handlers */ 151 struct comm_reply repinfo; 152 153 /* -------- TCP Accept -------- */ 154 /** the number of TCP handlers for this tcp-accept socket */ 155 int max_tcp_count; 156 /** malloced array of tcp handlers for a tcp-accept, 157 of size max_tcp_count. */ 158 struct comm_point** tcp_handlers; 159 /** linked list of free tcp_handlers to use for new queries. 160 For tcp_accept the first entry, for tcp_handlers the next one. */ 161 struct comm_point* tcp_free; 162 163 /* -------- SSL TCP DNS ------- */ 164 /** the SSL object with rw bio (owned) or for commaccept ctx ref */ 165 void* ssl; 166 /** handshake state for init and renegotiate */ 167 enum { 168 /** no handshake, it has been done */ 169 comm_ssl_shake_none = 0, 170 /** ssl initial handshake wants to read */ 171 comm_ssl_shake_read, 172 /** ssl initial handshake wants to write */ 173 comm_ssl_shake_write, 174 /** ssl_write wants to read */ 175 comm_ssl_shake_hs_read, 176 /** ssl_read wants to write */ 177 comm_ssl_shake_hs_write 178 } ssl_shake_state; 179 180 /** is this a UDP, TCP-accept or TCP socket. */ 181 enum comm_point_type { 182 /** UDP socket - handle datagrams. */ 183 comm_udp, 184 /** TCP accept socket - only creates handlers if readable. */ 185 comm_tcp_accept, 186 /** TCP handler socket - handle byteperbyte readwrite. */ 187 comm_tcp, 188 /** AF_UNIX socket - for internal commands. */ 189 comm_local, 190 /** raw - not DNS format - for pipe readers and writers */ 191 comm_raw 192 } 193 /** variable with type of socket, UDP,TCP-accept,TCP,pipe */ 194 type; 195 196 /* ---------- Behaviour ----------- */ 197 /** if set the connection is NOT closed on delete. */ 198 int do_not_close; 199 200 /** if set, the connection is closed on error, on timeout, 201 and after read/write completes. No callback is done. */ 202 int tcp_do_close; 203 204 /** if set, read/write completes: 205 read/write state of tcp is toggled. 206 buffer reset/bytecount reset. 207 this flag cleared. 208 So that when that is done the callback is called. */ 209 int tcp_do_toggle_rw; 210 211 /** if set, checks for pending error from nonblocking connect() call.*/ 212 int tcp_check_nb_connect; 213 214 /** number of queries outstanding on this socket, used by 215 * outside network for udp ports */ 216 int inuse; 217 218 /** callback when done. 219 tcp_accept does not get called back, is NULL then. 220 If a timeout happens, callback with timeout=1 is called. 221 If an error happens, callback is called with error set 222 nonzero. If not NETEVENT_NOERROR, it is an errno value. 223 If the connection is closed (by remote end) then the 224 callback is called with error set to NETEVENT_CLOSED=-1. 225 If a timeout happens on the connection, the error is set to 226 NETEVENT_TIMEOUT=-2. 227 The reply_info can be copied if the reply needs to happen at a 228 later time. It consists of a struct with commpoint and address. 229 It can be passed to a msg send routine some time later. 230 Note the reply information is temporary and must be copied. 231 NULL is passed for_reply info, in cases where error happened. 232 233 declare as: 234 int my_callback(struct comm_point* c, void* my_arg, int error, 235 struct comm_reply *reply_info); 236 237 if the routine returns 0, nothing is done. 238 Notzero, the buffer will be sent back to client. 239 For UDP this is done without changing the commpoint. 240 In TCP it sets write state. 241 */ 242 comm_point_callback_t* callback; 243 /** argument to pass to callback. */ 244 void *cb_arg; 245 }; 246 247 /** 248 * Structure only for making timeout events. 249 */ 250 struct comm_timer { 251 /** the internal event stuff */ 252 struct internal_timer* ev_timer; 253 254 /** callback function, takes user arg only */ 255 void (*callback)(void*); 256 257 /** callback user argument */ 258 void* cb_arg; 259 }; 260 261 /** 262 * Structure only for signal events. 263 */ 264 struct comm_signal { 265 /** the communication base */ 266 struct comm_base* base; 267 268 /** the internal event stuff */ 269 struct internal_signal* ev_signal; 270 271 /** callback function, takes signal number and user arg */ 272 void (*callback)(int, void*); 273 274 /** callback user argument */ 275 void* cb_arg; 276 }; 277 278 /** 279 * Create a new comm base. 280 * @param sigs: if true it attempts to create a default loop for 281 * signal handling. 282 * @return: the new comm base. NULL on error. 283 */ 284 struct comm_base* comm_base_create(int sigs); 285 286 /** 287 * Destroy a comm base. 288 * All comm points must have been deleted. 289 * @param b: the base to delete. 290 */ 291 void comm_base_delete(struct comm_base* b); 292 293 /** 294 * Obtain two pointers. The pointers never change (until base_delete()). 295 * The pointers point to time values that are updated regularly. 296 * @param b: the communication base that will update the time values. 297 * @param tt: pointer to time in seconds is returned. 298 * @param tv: pointer to time in microseconds is returned. 299 */ 300 void comm_base_timept(struct comm_base* b, uint32_t** tt, struct timeval** tv); 301 302 /** 303 * Dispatch the comm base events. 304 * @param b: the communication to perform. 305 */ 306 void comm_base_dispatch(struct comm_base* b); 307 308 /** 309 * Exit from dispatch loop. 310 * @param b: the communication base that is in dispatch(). 311 */ 312 void comm_base_exit(struct comm_base* b); 313 314 /** 315 * Access internal data structure (for util/tube.c on windows) 316 * @param b: comm base 317 * @return event_base. Could be libevent, or internal event handler. 318 */ 319 struct event_base* comm_base_internal(struct comm_base* b); 320 321 /** 322 * Create an UDP comm point. Calls malloc. 323 * setups the structure with the parameters you provide. 324 * @param base: in which base to alloc the commpoint. 325 * @param fd : file descriptor of open UDP socket. 326 * @param buffer: shared buffer by UDP sockets from this thread. 327 * @param callback: callback function pointer. 328 * @param callback_arg: will be passed to your callback function. 329 * @return: returns the allocated communication point. NULL on error. 330 * Sets timeout to NULL. Turns off TCP options. 331 */ 332 struct comm_point* comm_point_create_udp(struct comm_base* base, 333 int fd, ldns_buffer* buffer, 334 comm_point_callback_t* callback, void* callback_arg); 335 336 /** 337 * Create an UDP with ancillary data comm point. Calls malloc. 338 * Uses recvmsg instead of recv to get udp message. 339 * setups the structure with the parameters you provide. 340 * @param base: in which base to alloc the commpoint. 341 * @param fd : file descriptor of open UDP socket. 342 * @param buffer: shared buffer by UDP sockets from this thread. 343 * @param callback: callback function pointer. 344 * @param callback_arg: will be passed to your callback function. 345 * @return: returns the allocated communication point. NULL on error. 346 * Sets timeout to NULL. Turns off TCP options. 347 */ 348 struct comm_point* comm_point_create_udp_ancil(struct comm_base* base, 349 int fd, ldns_buffer* buffer, 350 comm_point_callback_t* callback, void* callback_arg); 351 352 /** 353 * Create a TCP listener comm point. Calls malloc. 354 * Setups the structure with the parameters you provide. 355 * Also Creates TCP Handlers, pre allocated for you. 356 * Uses the parameters you provide. 357 * @param base: in which base to alloc the commpoint. 358 * @param fd: file descriptor of open TCP socket set to listen nonblocking. 359 * @param num: becomes max_tcp_count, the routine allocates that 360 * many tcp handler commpoints. 361 * @param bufsize: size of buffer to create for handlers. 362 * @param callback: callback function pointer for TCP handlers. 363 * @param callback_arg: will be passed to your callback function. 364 * @return: returns the TCP listener commpoint. You can find the 365 * TCP handlers in the array inside the listener commpoint. 366 * returns NULL on error. 367 * Inits timeout to NULL. All handlers are on the free list. 368 */ 369 struct comm_point* comm_point_create_tcp(struct comm_base* base, 370 int fd, int num, size_t bufsize, 371 comm_point_callback_t* callback, void* callback_arg); 372 373 /** 374 * Create an outgoing TCP commpoint. No file descriptor is opened, left at -1. 375 * @param base: in which base to alloc the commpoint. 376 * @param bufsize: size of buffer to create for handlers. 377 * @param callback: callback function pointer for the handler. 378 * @param callback_arg: will be passed to your callback function. 379 * @return: the commpoint or NULL on error. 380 */ 381 struct comm_point* comm_point_create_tcp_out(struct comm_base* base, 382 size_t bufsize, comm_point_callback_t* callback, void* callback_arg); 383 384 /** 385 * Create commpoint to listen to a local domain file descriptor. 386 * @param base: in which base to alloc the commpoint. 387 * @param fd: file descriptor of open AF_UNIX socket set to listen nonblocking. 388 * @param bufsize: size of buffer to create for handlers. 389 * @param callback: callback function pointer for the handler. 390 * @param callback_arg: will be passed to your callback function. 391 * @return: the commpoint or NULL on error. 392 */ 393 struct comm_point* comm_point_create_local(struct comm_base* base, 394 int fd, size_t bufsize, 395 comm_point_callback_t* callback, void* callback_arg); 396 397 /** 398 * Create commpoint to listen to a local domain pipe descriptor. 399 * @param base: in which base to alloc the commpoint. 400 * @param fd: file descriptor. 401 * @param writing: true if you want to listen to writes, false for reads. 402 * @param callback: callback function pointer for the handler. 403 * @param callback_arg: will be passed to your callback function. 404 * @return: the commpoint or NULL on error. 405 */ 406 struct comm_point* comm_point_create_raw(struct comm_base* base, 407 int fd, int writing, 408 comm_point_callback_t* callback, void* callback_arg); 409 410 /** 411 * Close a comm point fd. 412 * @param c: comm point to close. 413 */ 414 void comm_point_close(struct comm_point* c); 415 416 /** 417 * Close and deallocate (free) the comm point. If the comm point is 418 * a tcp-accept point, also its tcp-handler points are deleted. 419 * @param c: comm point to delete. 420 */ 421 void comm_point_delete(struct comm_point* c); 422 423 /** 424 * Send reply. Put message into commpoint buffer. 425 * @param repinfo: The reply info copied from a commpoint callback call. 426 */ 427 void comm_point_send_reply(struct comm_reply* repinfo); 428 429 /** 430 * Drop reply. Cleans up. 431 * @param repinfo: The reply info copied from a commpoint callback call. 432 */ 433 void comm_point_drop_reply(struct comm_reply* repinfo); 434 435 /** 436 * Send an udp message over a commpoint. 437 * @param c: commpoint to send it from. 438 * @param packet: what to send. 439 * @param addr: where to send it to. 440 * @param addrlen: length of addr. 441 * @return: false on a failure. 442 */ 443 int comm_point_send_udp_msg(struct comm_point* c, ldns_buffer* packet, 444 struct sockaddr* addr, socklen_t addrlen); 445 446 /** 447 * Stop listening for input on the commpoint. No callbacks will happen. 448 * @param c: commpoint to disable. The fd is not closed. 449 */ 450 void comm_point_stop_listening(struct comm_point* c); 451 452 /** 453 * Start listening again for input on the comm point. 454 * @param c: commpoint to enable again. 455 * @param newfd: new fd, or -1 to leave fd be. 456 * @param sec: timeout in seconds, or -1 for no (change to the) timeout. 457 */ 458 void comm_point_start_listening(struct comm_point* c, int newfd, int sec); 459 460 /** 461 * Stop listening and start listening again for reading or writing. 462 * @param c: commpoint 463 * @param rd: if true, listens for reading. 464 * @param wr: if true, listens for writing. 465 */ 466 void comm_point_listen_for_rw(struct comm_point* c, int rd, int wr); 467 468 /** 469 * Get size of memory used by comm point. 470 * For TCP handlers this includes subhandlers. 471 * For UDP handlers, this does not include the (shared) UDP buffer. 472 * @param c: commpoint. 473 * @return size in bytes. 474 */ 475 size_t comm_point_get_mem(struct comm_point* c); 476 477 /** 478 * create timer. Not active upon creation. 479 * @param base: event handling base. 480 * @param cb: callback function: void myfunc(void* myarg); 481 * @param cb_arg: user callback argument. 482 * @return: the new timer or NULL on error. 483 */ 484 struct comm_timer* comm_timer_create(struct comm_base* base, 485 void (*cb)(void*), void* cb_arg); 486 487 /** 488 * disable timer. Stops callbacks from happening. 489 * @param timer: to disable. 490 */ 491 void comm_timer_disable(struct comm_timer* timer); 492 493 /** 494 * reset timevalue for timer. 495 * @param timer: timer to (re)set. 496 * @param tv: when the timer should activate. if NULL timer is disabled. 497 */ 498 void comm_timer_set(struct comm_timer* timer, struct timeval* tv); 499 500 /** 501 * delete timer. 502 * @param timer: to delete. 503 */ 504 void comm_timer_delete(struct comm_timer* timer); 505 506 /** 507 * see if timeout has been set to a value. 508 * @param timer: the timer to examine. 509 * @return: false if disabled or not set. 510 */ 511 int comm_timer_is_set(struct comm_timer* timer); 512 513 /** 514 * Get size of memory used by comm timer. 515 * @param timer: the timer to examine. 516 * @return size in bytes. 517 */ 518 size_t comm_timer_get_mem(struct comm_timer* timer); 519 520 /** 521 * Create a signal handler. Call signal_bind() later to bind to a signal. 522 * @param base: communication base to use. 523 * @param callback: called when signal is caught. 524 * @param cb_arg: user argument to callback 525 * @return: the signal struct or NULL on error. 526 */ 527 struct comm_signal* comm_signal_create(struct comm_base* base, 528 void (*callback)(int, void*), void* cb_arg); 529 530 /** 531 * Bind signal struct to catch a signal. A signle comm_signal can be bound 532 * to multiple signals, calling comm_signal_bind multiple times. 533 * @param comsig: the communication point, with callback information. 534 * @param sig: signal number. 535 * @return: true on success. false on error. 536 */ 537 int comm_signal_bind(struct comm_signal* comsig, int sig); 538 539 /** 540 * Delete the signal communication point. 541 * @param comsig: to delete. 542 */ 543 void comm_signal_delete(struct comm_signal* comsig); 544 545 /** 546 * perform accept(2) with error checking. 547 * @param c: commpoint with accept fd. 548 * @param addr: remote end returned here. 549 * @param addrlen: length of remote end returned here. 550 * @return new fd, or -1 on error. 551 * if -1, error message has been printed if necessary, simply drop 552 * out of the reading handler. 553 */ 554 int comm_point_perform_accept(struct comm_point* c, 555 struct sockaddr_storage* addr, socklen_t* addrlen); 556 557 /**** internal routines ****/ 558 559 /** 560 * This routine is published for checks and tests, and is only used internally. 561 * handle libevent callback for udp comm point. 562 * @param fd: file descriptor. 563 * @param event: event bits from libevent: 564 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 565 * @param arg: the comm_point structure. 566 */ 567 void comm_point_udp_callback(int fd, short event, void* arg); 568 569 /** 570 * This routine is published for checks and tests, and is only used internally. 571 * handle libevent callback for udp ancillary data comm point. 572 * @param fd: file descriptor. 573 * @param event: event bits from libevent: 574 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 575 * @param arg: the comm_point structure. 576 */ 577 void comm_point_udp_ancil_callback(int fd, short event, void* arg); 578 579 /** 580 * This routine is published for checks and tests, and is only used internally. 581 * handle libevent callback for tcp accept comm point 582 * @param fd: file descriptor. 583 * @param event: event bits from libevent: 584 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 585 * @param arg: the comm_point structure. 586 */ 587 void comm_point_tcp_accept_callback(int fd, short event, void* arg); 588 589 /** 590 * This routine is published for checks and tests, and is only used internally. 591 * handle libevent callback for tcp data comm point 592 * @param fd: file descriptor. 593 * @param event: event bits from libevent: 594 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 595 * @param arg: the comm_point structure. 596 */ 597 void comm_point_tcp_handle_callback(int fd, short event, void* arg); 598 599 /** 600 * This routine is published for checks and tests, and is only used internally. 601 * handle libevent callback for timer comm. 602 * @param fd: file descriptor (always -1). 603 * @param event: event bits from libevent: 604 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 605 * @param arg: the comm_timer structure. 606 */ 607 void comm_timer_callback(int fd, short event, void* arg); 608 609 /** 610 * This routine is published for checks and tests, and is only used internally. 611 * handle libevent callback for signal comm. 612 * @param fd: file descriptor (used for the signal number). 613 * @param event: event bits from libevent: 614 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 615 * @param arg: the internal commsignal structure. 616 */ 617 void comm_signal_callback(int fd, short event, void* arg); 618 619 /** 620 * This routine is published for checks and tests, and is only used internally. 621 * libevent callback for AF_UNIX fds 622 * @param fd: file descriptor. 623 * @param event: event bits from libevent: 624 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 625 * @param arg: the comm_point structure. 626 */ 627 void comm_point_local_handle_callback(int fd, short event, void* arg); 628 629 /** 630 * This routine is published for checks and tests, and is only used internally. 631 * libevent callback for raw fd access. 632 * @param fd: file descriptor. 633 * @param event: event bits from libevent: 634 * EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT. 635 * @param arg: the comm_point structure. 636 */ 637 void comm_point_raw_handle_callback(int fd, short event, void* arg); 638 639 #ifdef USE_WINSOCK 640 /** 641 * Callback for openssl BIO to on windows detect WSAEWOULDBLOCK and notify 642 * the winsock_event of this for proper TCP nonblocking implementation. 643 * @param c: comm_point, fd must be set its struct event is registered. 644 * @param ssl: openssl SSL, fd must be set so it has a bio. 645 */ 646 void comm_point_tcp_win_bio_cb(struct comm_point* c, void* ssl); 647 #endif 648 649 #endif /* NET_EVENT_H */ 650