xref: /freebsd/contrib/unbound/util/netevent.h (revision 0c54932d50a0cbffdd083bf6b2e8d587902f90c9)
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
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE 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 "dnscrypt/dnscrypt.h"
64 
65 struct sldns_buffer;
66 struct comm_point;
67 struct comm_reply;
68 struct tcl_list;
69 struct ub_event_base;
70 
71 /* internal event notification data storage structure. */
72 struct internal_event;
73 struct internal_base;
74 struct internal_timer; /* A sub struct of the comm_timer super struct */
75 
76 /** callback from communication point function type */
77 typedef int comm_point_callback_type(struct comm_point*, void*, int,
78 	struct comm_reply*);
79 
80 /** to pass no_error to callback function */
81 #define NETEVENT_NOERROR 0
82 /** to pass closed connection to callback function */
83 #define NETEVENT_CLOSED -1
84 /** to pass timeout happened to callback function */
85 #define NETEVENT_TIMEOUT -2
86 /** to pass fallback from capsforID to callback function; 0x20 failed */
87 #define NETEVENT_CAPSFAIL -3
88 /** to pass done transfer to callback function; http file is complete */
89 #define NETEVENT_DONE -4
90 
91 /** timeout to slow accept calls when not possible, in msec. */
92 #define NETEVENT_SLOW_ACCEPT_TIME 2000
93 
94 /**
95  * A communication point dispatcher. Thread specific.
96  */
97 struct comm_base {
98 	/** behind the scenes structure. with say libevent info. alloced */
99 	struct internal_base* eb;
100 	/** callback to stop listening on accept sockets,
101 	 * performed when accept() will not function properly */
102 	void (*stop_accept)(void*);
103 	/** callback to start listening on accept sockets, performed
104 	 * after stop_accept() then a timeout has passed. */
105 	void (*start_accept)(void*);
106 	/** user argument for stop_accept and start_accept functions */
107 	void* cb_arg;
108 };
109 
110 /**
111  * Reply information for a communication point.
112  */
113 struct comm_reply {
114 	/** the comm_point with fd to send reply on to. */
115 	struct comm_point* c;
116 	/** the address (for UDP based communication) */
117 	struct sockaddr_storage addr;
118 	/** length of address */
119 	socklen_t addrlen;
120 	/** return type 0 (none), 4(IP4), 6(IP6) */
121 	int srctype;
122 	/* DnsCrypt context */
123 #ifdef USE_DNSCRYPT
124 	uint8_t client_nonce[crypto_box_HALF_NONCEBYTES];
125 	uint8_t nmkey[crypto_box_BEFORENMBYTES];
126 	const dnsccert *dnsc_cert;
127 	int is_dnscrypted;
128 #endif
129 	/** the return source interface data */
130 	union {
131 #ifdef IPV6_PKTINFO
132 		struct in6_pktinfo v6info;
133 #endif
134 #ifdef IP_PKTINFO
135 		struct in_pktinfo v4info;
136 #elif defined(IP_RECVDSTADDR)
137 		struct in_addr v4addr;
138 #endif
139 	}
140 		/** variable with return source data */
141 		pktinfo;
142 	/** max udp size for udp packets */
143 	size_t max_udp_size;
144 };
145 
146 /**
147  * Communication point to the network
148  * These behaviours can be accomplished by setting the flags
149  * and passing return values from the callback.
150  *    udp frontside: called after readdone. sendafter.
151  *    tcp frontside: called readdone, sendafter. close.
152  *    udp behind: called after readdone. No send after.
153  *    tcp behind: write done, read done, then called. No send after.
154  */
155 struct comm_point {
156 	/** behind the scenes structure, with say libevent info. alloced. */
157 	struct internal_event* ev;
158 
159 	/** file descriptor for communication point */
160 	int fd;
161 
162 	/** timeout (NULL if it does not). Malloced. */
163 	struct timeval* timeout;
164 
165 	/** buffer pointer. Either to perthread, or own buffer or NULL */
166 	struct sldns_buffer* buffer;
167 
168 	/* -------- TCP Handler -------- */
169 	/** Read/Write state for TCP */
170 	int tcp_is_reading;
171 	/** The current read/write count for TCP */
172 	size_t tcp_byte_count;
173 	/** parent communication point (for TCP sockets) */
174 	struct comm_point* tcp_parent;
175 	/** sockaddr from peer, for TCP handlers */
176 	struct comm_reply repinfo;
177 
178 	/* -------- TCP Accept -------- */
179 	/** the number of TCP handlers for this tcp-accept socket */
180 	int max_tcp_count;
181 	/** current number of tcp handler in-use for this accept socket */
182 	int cur_tcp_count;
183 	/** malloced array of tcp handlers for a tcp-accept,
184 	    of size max_tcp_count. */
185 	struct comm_point** tcp_handlers;
186 	/** linked list of free tcp_handlers to use for new queries.
187 	    For tcp_accept the first entry, for tcp_handlers the next one. */
188 	struct comm_point* tcp_free;
189 
190 	/* -------- SSL TCP DNS ------- */
191 	/** the SSL object with rw bio (owned) or for commaccept ctx ref */
192 	void* ssl;
193 	/** handshake state for init and renegotiate */
194 	enum {
195 		/** no handshake, it has been done */
196 		comm_ssl_shake_none = 0,
197 		/** ssl initial handshake wants to read */
198 		comm_ssl_shake_read,
199 		/** ssl initial handshake wants to write */
200 		comm_ssl_shake_write,
201 		/** ssl_write wants to read */
202 		comm_ssl_shake_hs_read,
203 		/** ssl_read wants to write */
204 		comm_ssl_shake_hs_write
205 	} ssl_shake_state;
206 
207 	/* -------- HTTP ------- */
208 	/** Currently reading in http headers */
209 	int http_in_headers;
210 	/** Currently reading in chunk headers, 0=not, 1=firstline, 2=unused
211 	 * (more lines), 3=trailer headers after chunk */
212 	int http_in_chunk_headers;
213 	/** chunked transfer */
214 	int http_is_chunked;
215 	/** http temp buffer (shared buffer for temporary work) */
216 	struct sldns_buffer* http_temp;
217 	/** http stored content in buffer */
218 	size_t http_stored;
219 
220 	/* -------- dnstap ------- */
221 	/** the dnstap environment */
222 	struct dt_env* dtenv;
223 
224 	/** is this a UDP, TCP-accept or TCP socket. */
225 	enum comm_point_type {
226 		/** UDP socket - handle datagrams. */
227 		comm_udp,
228 		/** TCP accept socket - only creates handlers if readable. */
229 		comm_tcp_accept,
230 		/** TCP handler socket - handle byteperbyte readwrite. */
231 		comm_tcp,
232 		/** HTTP handler socket */
233 		comm_http,
234 		/** AF_UNIX socket - for internal commands. */
235 		comm_local,
236 		/** raw - not DNS format - for pipe readers and writers */
237 		comm_raw
238 	}
239 		/** variable with type of socket, UDP,TCP-accept,TCP,pipe */
240 		type;
241 
242 	/* ---------- Behaviour ----------- */
243 	/** if set the connection is NOT closed on delete. */
244 	int do_not_close;
245 
246 	/** if set, the connection is closed on error, on timeout,
247 	    and after read/write completes. No callback is done. */
248 	int tcp_do_close;
249 
250 	/** if set, read/write completes:
251 		read/write state of tcp is toggled.
252 		buffer reset/bytecount reset.
253 		this flag cleared.
254 	    So that when that is done the callback is called. */
255 	int tcp_do_toggle_rw;
256 
257 	/** timeout in msec for TCP wait times for this connection */
258 	int tcp_timeout_msec;
259 
260 	/** if set, tcp keepalive is enabled on this connection */
261 	int tcp_keepalive;
262 
263 	/** if set, checks for pending error from nonblocking connect() call.*/
264 	int tcp_check_nb_connect;
265 
266 	/** if set, check for connection limit on tcp accept. */
267 	struct tcl_list* tcp_conn_limit;
268 	/** the entry for the connection. */
269 	struct tcl_addr* tcl_addr;
270 
271 	/** the structure to keep track of open requests on this channel */
272 	struct tcp_req_info* tcp_req_info;
273 
274 #ifdef USE_MSG_FASTOPEN
275 	/** used to track if the sendto() call should be done when using TFO. */
276 	int tcp_do_fastopen;
277 #endif
278 
279 #ifdef USE_DNSCRYPT
280 	/** Is this a dnscrypt channel */
281 	int dnscrypt;
282 	/** encrypted buffer pointer. Either to perthread, or own buffer or NULL */
283 	struct sldns_buffer* dnscrypt_buffer;
284 #endif
285 	/** number of queries outstanding on this socket, used by
286 	 * outside network for udp ports */
287 	int inuse;
288 
289 	/** callback when done.
290 	    tcp_accept does not get called back, is NULL then.
291 	    If a timeout happens, callback with timeout=1 is called.
292 	    If an error happens, callback is called with error set
293 	    nonzero. If not NETEVENT_NOERROR, it is an errno value.
294 	    If the connection is closed (by remote end) then the
295 	    callback is called with error set to NETEVENT_CLOSED=-1.
296 	    If a timeout happens on the connection, the error is set to
297 	    NETEVENT_TIMEOUT=-2.
298 	    The reply_info can be copied if the reply needs to happen at a
299 	    later time. It consists of a struct with commpoint and address.
300 	    It can be passed to a msg send routine some time later.
301 	    Note the reply information is temporary and must be copied.
302 	    NULL is passed for_reply info, in cases where error happened.
303 
304 	    declare as:
305 	    int my_callback(struct comm_point* c, void* my_arg, int error,
306 		struct comm_reply *reply_info);
307 
308 	    if the routine returns 0, nothing is done.
309 	    Notzero, the buffer will be sent back to client.
310 	    		For UDP this is done without changing the commpoint.
311 			In TCP it sets write state.
312 	*/
313 	comm_point_callback_type* callback;
314 	/** argument to pass to callback. */
315 	void *cb_arg;
316 };
317 
318 /**
319  * Structure only for making timeout events.
320  */
321 struct comm_timer {
322 	/** the internal event stuff (derived) */
323 	struct internal_timer* ev_timer;
324 
325 	/** callback function, takes user arg only */
326 	void (*callback)(void*);
327 
328 	/** callback user argument */
329 	void* cb_arg;
330 };
331 
332 /**
333  * Structure only for signal events.
334  */
335 struct comm_signal {
336 	/** the communication base */
337 	struct comm_base* base;
338 
339 	/** the internal event stuff */
340 	struct internal_signal* ev_signal;
341 
342 	/** callback function, takes signal number and user arg */
343 	void (*callback)(int, void*);
344 
345 	/** callback user argument */
346 	void* cb_arg;
347 };
348 
349 /**
350  * Create a new comm base.
351  * @param sigs: if true it attempts to create a default loop for
352  *   signal handling.
353  * @return: the new comm base. NULL on error.
354  */
355 struct comm_base* comm_base_create(int sigs);
356 
357 /**
358  * Create comm base that uses the given ub_event_base (underlying pluggable
359  * event mechanism pointer).
360  * @param base: underlying pluggable event base.
361  * @return: the new comm base. NULL on error.
362  */
363 struct comm_base* comm_base_create_event(struct ub_event_base* base);
364 
365 /**
366  * Delete comm base structure but not the underlying lib event base.
367  * All comm points must have been deleted.
368  * @param b: the base to delete.
369  */
370 void comm_base_delete_no_base(struct comm_base* b);
371 
372 /**
373  * Destroy a comm base.
374  * All comm points must have been deleted.
375  * @param b: the base to delete.
376  */
377 void comm_base_delete(struct comm_base* b);
378 
379 /**
380  * Obtain two pointers. The pointers never change (until base_delete()).
381  * The pointers point to time values that are updated regularly.
382  * @param b: the communication base that will update the time values.
383  * @param tt: pointer to time in seconds is returned.
384  * @param tv: pointer to time in microseconds is returned.
385  */
386 void comm_base_timept(struct comm_base* b, time_t** tt, struct timeval** tv);
387 
388 /**
389  * Dispatch the comm base events.
390  * @param b: the communication to perform.
391  */
392 void comm_base_dispatch(struct comm_base* b);
393 
394 /**
395  * Exit from dispatch loop.
396  * @param b: the communication base that is in dispatch().
397  */
398 void comm_base_exit(struct comm_base* b);
399 
400 /**
401  * Set the slow_accept mode handlers.  You can not provide these if you do
402  * not perform accept() calls.
403  * @param b: comm base
404  * @param stop_accept: function that stops listening to accept fds.
405  * @param start_accept: function that resumes listening to accept fds.
406  * @param arg: callback arg to pass to the functions.
407  */
408 void comm_base_set_slow_accept_handlers(struct comm_base* b,
409 	void (*stop_accept)(void*), void (*start_accept)(void*), void* arg);
410 
411 /**
412  * Access internal data structure (for util/tube.c on windows)
413  * @param b: comm base
414  * @return ub_event_base.
415  */
416 struct ub_event_base* comm_base_internal(struct comm_base* b);
417 
418 /**
419  * Create an UDP comm point. Calls malloc.
420  * setups the structure with the parameters you provide.
421  * @param base: in which base to alloc the commpoint.
422  * @param fd : file descriptor of open UDP socket.
423  * @param buffer: shared buffer by UDP sockets from this thread.
424  * @param callback: callback function pointer.
425  * @param callback_arg: will be passed to your callback function.
426  * @return: returns the allocated communication point. NULL on error.
427  * Sets timeout to NULL. Turns off TCP options.
428  */
429 struct comm_point* comm_point_create_udp(struct comm_base* base,
430 	int fd, struct sldns_buffer* buffer,
431 	comm_point_callback_type* callback, void* callback_arg);
432 
433 /**
434  * Create an UDP with ancillary data comm point. Calls malloc.
435  * Uses recvmsg instead of recv to get udp message.
436  * setups the structure with the parameters you provide.
437  * @param base: in which base to alloc the commpoint.
438  * @param fd : file descriptor of open UDP socket.
439  * @param buffer: shared buffer by UDP sockets from this thread.
440  * @param callback: callback function pointer.
441  * @param callback_arg: will be passed to your callback function.
442  * @return: returns the allocated communication point. NULL on error.
443  * Sets timeout to NULL. Turns off TCP options.
444  */
445 struct comm_point* comm_point_create_udp_ancil(struct comm_base* base,
446 	int fd, struct sldns_buffer* buffer,
447 	comm_point_callback_type* callback, void* callback_arg);
448 
449 /**
450  * Create a TCP listener comm point. Calls malloc.
451  * Setups the structure with the parameters you provide.
452  * Also Creates TCP Handlers, pre allocated for you.
453  * Uses the parameters you provide.
454  * @param base: in which base to alloc the commpoint.
455  * @param fd: file descriptor of open TCP socket set to listen nonblocking.
456  * @param num: becomes max_tcp_count, the routine allocates that
457  *	many tcp handler commpoints.
458  * @param idle_timeout: TCP idle timeout in ms.
459  * @param tcp_conn_limit: TCP connection limit info.
460  * @param bufsize: size of buffer to create for handlers.
461  * @param spoolbuf: shared spool buffer for tcp_req_info structures.
462  * 	or NULL to not create those structures in the tcp handlers.
463  * @param callback: callback function pointer for TCP handlers.
464  * @param callback_arg: will be passed to your callback function.
465  * @return: returns the TCP listener commpoint. You can find the
466  *  	TCP handlers in the array inside the listener commpoint.
467  *	returns NULL on error.
468  * Inits timeout to NULL. All handlers are on the free list.
469  */
470 struct comm_point* comm_point_create_tcp(struct comm_base* base,
471 	int fd, int num, int idle_timeout, struct tcl_list* tcp_conn_limit,
472 	size_t bufsize, struct sldns_buffer* spoolbuf,
473 	comm_point_callback_type* callback, void* callback_arg);
474 
475 /**
476  * Create an outgoing TCP commpoint. No file descriptor is opened, left at -1.
477  * @param base: in which base to alloc the commpoint.
478  * @param bufsize: size of buffer to create for handlers.
479  * @param callback: callback function pointer for the handler.
480  * @param callback_arg: will be passed to your callback function.
481  * @return: the commpoint or NULL on error.
482  */
483 struct comm_point* comm_point_create_tcp_out(struct comm_base* base,
484 	size_t bufsize, comm_point_callback_type* callback, void* callback_arg);
485 
486 /**
487  * Create an outgoing HTTP commpoint. No file descriptor is opened, left at -1.
488  * @param base: in which base to alloc the commpoint.
489  * @param bufsize: size of buffer to create for handlers.
490  * @param callback: callback function pointer for the handler.
491  * @param callback_arg: will be passed to your callback function.
492  * @param temp: sldns buffer, shared between other http_out commpoints, for
493  * 	temporary data when performing callbacks.
494  * @return: the commpoint or NULL on error.
495  */
496 struct comm_point* comm_point_create_http_out(struct comm_base* base,
497 	size_t bufsize, comm_point_callback_type* callback,
498 	void* callback_arg, struct sldns_buffer* temp);
499 
500 /**
501  * Create commpoint to listen to a local domain file descriptor.
502  * @param base: in which base to alloc the commpoint.
503  * @param fd: file descriptor of open AF_UNIX socket set to listen nonblocking.
504  * @param bufsize: size of buffer to create for handlers.
505  * @param callback: callback function pointer for the handler.
506  * @param callback_arg: will be passed to your callback function.
507  * @return: the commpoint or NULL on error.
508  */
509 struct comm_point* comm_point_create_local(struct comm_base* base,
510 	int fd, size_t bufsize,
511 	comm_point_callback_type* callback, void* callback_arg);
512 
513 /**
514  * Create commpoint to listen to a local domain pipe descriptor.
515  * @param base: in which base to alloc the commpoint.
516  * @param fd: file descriptor.
517  * @param writing: true if you want to listen to writes, false for reads.
518  * @param callback: callback function pointer for the handler.
519  * @param callback_arg: will be passed to your callback function.
520  * @return: the commpoint or NULL on error.
521  */
522 struct comm_point* comm_point_create_raw(struct comm_base* base,
523 	int fd, int writing,
524 	comm_point_callback_type* callback, void* callback_arg);
525 
526 /**
527  * Close a comm point fd.
528  * @param c: comm point to close.
529  */
530 void comm_point_close(struct comm_point* c);
531 
532 /**
533  * Close and deallocate (free) the comm point. If the comm point is
534  * a tcp-accept point, also its tcp-handler points are deleted.
535  * @param c: comm point to delete.
536  */
537 void comm_point_delete(struct comm_point* c);
538 
539 /**
540  * Send reply. Put message into commpoint buffer.
541  * @param repinfo: The reply info copied from a commpoint callback call.
542  */
543 void comm_point_send_reply(struct comm_reply* repinfo);
544 
545 /**
546  * Drop reply. Cleans up.
547  * @param repinfo: The reply info copied from a commpoint callback call.
548  */
549 void comm_point_drop_reply(struct comm_reply* repinfo);
550 
551 /**
552  * Send an udp message over a commpoint.
553  * @param c: commpoint to send it from.
554  * @param packet: what to send.
555  * @param addr: where to send it to.
556  * @param addrlen: length of addr.
557  * @return: false on a failure.
558  */
559 int comm_point_send_udp_msg(struct comm_point* c, struct sldns_buffer* packet,
560 	struct sockaddr* addr, socklen_t addrlen);
561 
562 /**
563  * Stop listening for input on the commpoint. No callbacks will happen.
564  * @param c: commpoint to disable. The fd is not closed.
565  */
566 void comm_point_stop_listening(struct comm_point* c);
567 
568 /**
569  * Start listening again for input on the comm point.
570  * @param c: commpoint to enable again.
571  * @param newfd: new fd, or -1 to leave fd be.
572  * @param msec: timeout in milliseconds, or -1 for no (change to the) timeout.
573  *	So seconds*1000.
574  */
575 void comm_point_start_listening(struct comm_point* c, int newfd, int msec);
576 
577 /**
578  * Stop listening and start listening again for reading or writing.
579  * @param c: commpoint
580  * @param rd: if true, listens for reading.
581  * @param wr: if true, listens for writing.
582  */
583 void comm_point_listen_for_rw(struct comm_point* c, int rd, int wr);
584 
585 /**
586  * Get size of memory used by comm point.
587  * For TCP handlers this includes subhandlers.
588  * For UDP handlers, this does not include the (shared) UDP buffer.
589  * @param c: commpoint.
590  * @return size in bytes.
591  */
592 size_t comm_point_get_mem(struct comm_point* c);
593 
594 /**
595  * create timer. Not active upon creation.
596  * @param base: event handling base.
597  * @param cb: callback function: void myfunc(void* myarg);
598  * @param cb_arg: user callback argument.
599  * @return: the new timer or NULL on error.
600  */
601 struct comm_timer* comm_timer_create(struct comm_base* base,
602 	void (*cb)(void*), void* cb_arg);
603 
604 /**
605  * disable timer. Stops callbacks from happening.
606  * @param timer: to disable.
607  */
608 void comm_timer_disable(struct comm_timer* timer);
609 
610 /**
611  * reset timevalue for timer.
612  * @param timer: timer to (re)set.
613  * @param tv: when the timer should activate. if NULL timer is disabled.
614  */
615 void comm_timer_set(struct comm_timer* timer, struct timeval* tv);
616 
617 /**
618  * delete timer.
619  * @param timer: to delete.
620  */
621 void comm_timer_delete(struct comm_timer* timer);
622 
623 /**
624  * see if timeout has been set to a value.
625  * @param timer: the timer to examine.
626  * @return: false if disabled or not set.
627  */
628 int comm_timer_is_set(struct comm_timer* timer);
629 
630 /**
631  * Get size of memory used by comm timer.
632  * @param timer: the timer to examine.
633  * @return size in bytes.
634  */
635 size_t comm_timer_get_mem(struct comm_timer* timer);
636 
637 /**
638  * Create a signal handler. Call signal_bind() later to bind to a signal.
639  * @param base: communication base to use.
640  * @param callback: called when signal is caught.
641  * @param cb_arg: user argument to callback
642  * @return: the signal struct or NULL on error.
643  */
644 struct comm_signal* comm_signal_create(struct comm_base* base,
645 	void (*callback)(int, void*), void* cb_arg);
646 
647 /**
648  * Bind signal struct to catch a signal. A signle comm_signal can be bound
649  * to multiple signals, calling comm_signal_bind multiple times.
650  * @param comsig: the communication point, with callback information.
651  * @param sig: signal number.
652  * @return: true on success. false on error.
653  */
654 int comm_signal_bind(struct comm_signal* comsig, int sig);
655 
656 /**
657  * Delete the signal communication point.
658  * @param comsig: to delete.
659  */
660 void comm_signal_delete(struct comm_signal* comsig);
661 
662 /**
663  * perform accept(2) with error checking.
664  * @param c: commpoint with accept fd.
665  * @param addr: remote end returned here.
666  * @param addrlen: length of remote end returned here.
667  * @return new fd, or -1 on error.
668  *	if -1, error message has been printed if necessary, simply drop
669  *	out of the reading handler.
670  */
671 int comm_point_perform_accept(struct comm_point* c,
672 	struct sockaddr_storage* addr, socklen_t* addrlen);
673 
674 /**** internal routines ****/
675 
676 /**
677  * This routine is published for checks and tests, and is only used internally.
678  * handle libevent callback for udp comm point.
679  * @param fd: file descriptor.
680  * @param event: event bits from libevent:
681  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
682  * @param arg: the comm_point structure.
683  */
684 void comm_point_udp_callback(int fd, short event, void* arg);
685 
686 /**
687  * This routine is published for checks and tests, and is only used internally.
688  * handle libevent callback for udp ancillary data comm point.
689  * @param fd: file descriptor.
690  * @param event: event bits from libevent:
691  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
692  * @param arg: the comm_point structure.
693  */
694 void comm_point_udp_ancil_callback(int fd, short event, void* arg);
695 
696 /**
697  * This routine is published for checks and tests, and is only used internally.
698  * handle libevent callback for tcp accept comm point
699  * @param fd: file descriptor.
700  * @param event: event bits from libevent:
701  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
702  * @param arg: the comm_point structure.
703  */
704 void comm_point_tcp_accept_callback(int fd, short event, void* arg);
705 
706 /**
707  * This routine is published for checks and tests, and is only used internally.
708  * handle libevent callback for tcp data comm point
709  * @param fd: file descriptor.
710  * @param event: event bits from libevent:
711  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
712  * @param arg: the comm_point structure.
713  */
714 void comm_point_tcp_handle_callback(int fd, short event, void* arg);
715 
716 /**
717  * This routine is published for checks and tests, and is only used internally.
718  * handle libevent callback for tcp data comm point
719  * @param fd: file descriptor.
720  * @param event: event bits from libevent:
721  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
722  * @param arg: the comm_point structure.
723  */
724 void comm_point_http_handle_callback(int fd, short event, void* arg);
725 
726 /**
727  * This routine is published for checks and tests, and is only used internally.
728  * handle libevent callback for timer comm.
729  * @param fd: file descriptor (always -1).
730  * @param event: event bits from libevent:
731  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
732  * @param arg: the comm_timer structure.
733  */
734 void comm_timer_callback(int fd, short event, void* arg);
735 
736 /**
737  * This routine is published for checks and tests, and is only used internally.
738  * handle libevent callback for signal comm.
739  * @param fd: file descriptor (used for the signal number).
740  * @param event: event bits from libevent:
741  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
742  * @param arg: the internal commsignal structure.
743  */
744 void comm_signal_callback(int fd, short event, void* arg);
745 
746 /**
747  * This routine is published for checks and tests, and is only used internally.
748  * libevent callback for AF_UNIX fds
749  * @param fd: file descriptor.
750  * @param event: event bits from libevent:
751  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
752  * @param arg: the comm_point structure.
753  */
754 void comm_point_local_handle_callback(int fd, short event, void* arg);
755 
756 /**
757  * This routine is published for checks and tests, and is only used internally.
758  * libevent callback for raw fd access.
759  * @param fd: file descriptor.
760  * @param event: event bits from libevent:
761  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
762  * @param arg: the comm_point structure.
763  */
764 void comm_point_raw_handle_callback(int fd, short event, void* arg);
765 
766 /**
767  * This routine is published for checks and tests, and is only used internally.
768  * libevent callback for timeout on slow accept.
769  * @param fd: file descriptor.
770  * @param event: event bits from libevent:
771  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
772  * @param arg: the comm_point structure.
773  */
774 void comm_base_handle_slow_accept(int fd, short event, void* arg);
775 
776 #ifdef USE_WINSOCK
777 /**
778  * Callback for openssl BIO to on windows detect WSAEWOULDBLOCK and notify
779  * the winsock_event of this for proper TCP nonblocking implementation.
780  * @param c: comm_point, fd must be set its struct event is registered.
781  * @param ssl: openssl SSL, fd must be set so it has a bio.
782  */
783 void comm_point_tcp_win_bio_cb(struct comm_point* c, void* ssl);
784 #endif
785 
786 /**
787  * See if errno for tcp connect has to be logged or not. This uses errno
788  * @param addr: apart from checking errno, the addr is checked for ip4mapped
789  * 	and broadcast type, hence passed.
790  * @param addrlen: length of the addr parameter.
791  * @return true if it needs to be logged.
792  */
793 int tcp_connect_errno_needs_log(struct sockaddr* addr, socklen_t addrlen);
794 
795 #ifdef HAVE_SSL
796 /**
797  * True if the ssl handshake error has to be squelched from the logs
798  * @param err: the error returned by the openssl routine, ERR_get_error.
799  * 	This is a packed structure with elements that are examined.
800  * @return true if the error is squelched (not logged).
801  */
802 int squelch_err_ssl_handshake(unsigned long err);
803 #endif
804 
805 #endif /* NET_EVENT_H */
806