1238106Sdes/*
2238106Sdes * util/netevent.h - event notification
3238106Sdes *
4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved.
5238106Sdes *
6238106Sdes * This software is open source.
7238106Sdes *
8238106Sdes * Redistribution and use in source and binary forms, with or without
9238106Sdes * modification, are permitted provided that the following conditions
10238106Sdes * are met:
11238106Sdes *
12238106Sdes * Redistributions of source code must retain the above copyright notice,
13238106Sdes * this list of conditions and the following disclaimer.
14238106Sdes *
15238106Sdes * Redistributions in binary form must reproduce the above copyright notice,
16238106Sdes * this list of conditions and the following disclaimer in the documentation
17238106Sdes * and/or other materials provided with the distribution.
18238106Sdes *
19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may
20238106Sdes * be used to endorse or promote products derived from this software without
21238106Sdes * specific prior written permission.
22238106Sdes *
23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24269257Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25269257Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26269257Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27269257Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28269257Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29269257Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30269257Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31269257Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32269257Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33269257Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34238106Sdes */
35238106Sdes
36238106Sdes/**
37238106Sdes * \file
38238106Sdes *
39238106Sdes * This file contains event notification functions.
40238106Sdes *
41238106Sdes * There are three types of communication points
42238106Sdes *    o UDP socket - perthread buffer.
43238106Sdes *    o TCP-accept socket - array of TCP-sockets, socketcount.
44238106Sdes *    o TCP socket - own buffer, parent-TCPaccept, read/write state,
45238106Sdes *                   number of bytes read/written, timeout.
46238106Sdes *
47238106Sdes * There are sockets aimed towards our clients and towards the internet.
48238106Sdes *    o frontside - aimed towards our clients, queries come in, answers back.
49238106Sdes *    o behind - aimed towards internet, to the authoritative DNS servers.
50238106Sdes *
51238106Sdes * Several event types are available:
52238106Sdes *    o comm_base - for thread safety of the comm points, one per thread.
53238106Sdes *    o comm_point - udp and tcp networking, with callbacks.
54238106Sdes *    o comm_timer - a timeout with callback.
55238106Sdes *    o comm_signal - callbacks when signal is caught.
56238106Sdes *    o comm_reply - holds reply info during networking callback.
57238106Sdes *
58238106Sdes */
59238106Sdes
60238106Sdes#ifndef NET_EVENT_H
61238106Sdes#define NET_EVENT_H
62238106Sdes
63269257Sdesstruct sldns_buffer;
64238106Sdesstruct comm_point;
65238106Sdesstruct comm_reply;
66238106Sdesstruct event_base;
67238106Sdes
68238106Sdes/* internal event notification data storage structure. */
69238106Sdesstruct internal_event;
70238106Sdesstruct internal_base;
71238106Sdesstruct internal_timer;
72238106Sdes
73238106Sdes/** callback from communication point function type */
74238106Sdestypedef int comm_point_callback_t(struct comm_point*, void*, int,
75238106Sdes	struct comm_reply*);
76238106Sdes
77238106Sdes/** to pass no_error to callback function */
78238106Sdes#define NETEVENT_NOERROR 0
79238106Sdes/** to pass closed connection to callback function */
80238106Sdes#define NETEVENT_CLOSED -1
81238106Sdes/** to pass timeout happened to callback function */
82238106Sdes#define NETEVENT_TIMEOUT -2
83238106Sdes/** to pass fallback from capsforID to callback function; 0x20 failed */
84238106Sdes#define NETEVENT_CAPSFAIL -3
85238106Sdes
86238106Sdes/** timeout to slow accept calls when not possible, in msec. */
87238106Sdes#define NETEVENT_SLOW_ACCEPT_TIME 2000
88238106Sdes
89238106Sdes/**
90238106Sdes * A communication point dispatcher. Thread specific.
91238106Sdes */
92238106Sdesstruct comm_base {
93238106Sdes	/** behind the scenes structure. with say libevent info. alloced */
94238106Sdes	struct internal_base* eb;
95238106Sdes	/** callback to stop listening on accept sockets,
96238106Sdes	 * performed when accept() will not function properly */
97238106Sdes	void (*stop_accept)(void*);
98238106Sdes	/** callback to start listening on accept sockets, performed
99238106Sdes	 * after stop_accept() then a timeout has passed. */
100238106Sdes	void (*start_accept)(void*);
101238106Sdes	/** user argument for stop_accept and start_accept functions */
102238106Sdes	void* cb_arg;
103238106Sdes};
104238106Sdes
105238106Sdes/**
106238106Sdes * Reply information for a communication point.
107238106Sdes */
108238106Sdesstruct comm_reply {
109238106Sdes	/** the comm_point with fd to send reply on to. */
110238106Sdes	struct comm_point* c;
111238106Sdes	/** the address (for UDP based communication) */
112238106Sdes	struct sockaddr_storage addr;
113238106Sdes	/** length of address */
114238106Sdes	socklen_t addrlen;
115238106Sdes	/** return type 0 (none), 4(IP4), 6(IP6) */
116238106Sdes	int srctype;
117238106Sdes	/** the return source interface data */
118238106Sdes	union {
119238106Sdes#ifdef IPV6_PKTINFO
120238106Sdes		struct in6_pktinfo v6info;
121238106Sdes#endif
122238106Sdes#ifdef IP_PKTINFO
123238106Sdes		struct in_pktinfo v4info;
124238106Sdes#elif defined(IP_RECVDSTADDR)
125238106Sdes		struct in_addr v4addr;
126238106Sdes#endif
127238106Sdes	}
128238106Sdes		/** variable with return source data */
129238106Sdes		pktinfo;
130238106Sdes};
131238106Sdes
132238106Sdes/**
133238106Sdes * Communication point to the network
134238106Sdes * These behaviours can be accomplished by setting the flags
135238106Sdes * and passing return values from the callback.
136238106Sdes *    udp frontside: called after readdone. sendafter.
137238106Sdes *    tcp frontside: called readdone, sendafter. close.
138238106Sdes *    udp behind: called after readdone. No send after.
139238106Sdes *    tcp behind: write done, read done, then called. No send after.
140238106Sdes */
141238106Sdesstruct comm_point {
142238106Sdes	/** behind the scenes structure, with say libevent info. alloced. */
143238106Sdes	struct internal_event* ev;
144238106Sdes
145238106Sdes	/** file descriptor for communication point */
146238106Sdes	int fd;
147238106Sdes
148238106Sdes	/** timeout (NULL if it does not). Malloced. */
149238106Sdes	struct timeval* timeout;
150238106Sdes
151238106Sdes	/** buffer pointer. Either to perthread, or own buffer or NULL */
152269257Sdes	struct sldns_buffer* buffer;
153238106Sdes
154238106Sdes	/* -------- TCP Handler -------- */
155238106Sdes	/** Read/Write state for TCP */
156238106Sdes	int tcp_is_reading;
157238106Sdes	/** The current read/write count for TCP */
158238106Sdes	size_t tcp_byte_count;
159238106Sdes	/** parent communication point (for TCP sockets) */
160238106Sdes	struct comm_point* tcp_parent;
161238106Sdes	/** sockaddr from peer, for TCP handlers */
162238106Sdes	struct comm_reply repinfo;
163238106Sdes
164238106Sdes	/* -------- TCP Accept -------- */
165238106Sdes	/** the number of TCP handlers for this tcp-accept socket */
166238106Sdes	int max_tcp_count;
167291767Sdes	/** current number of tcp handler in-use for this accept socket */
168291767Sdes	int cur_tcp_count;
169238106Sdes	/** malloced array of tcp handlers for a tcp-accept,
170238106Sdes	    of size max_tcp_count. */
171238106Sdes	struct comm_point** tcp_handlers;
172238106Sdes	/** linked list of free tcp_handlers to use for new queries.
173238106Sdes	    For tcp_accept the first entry, for tcp_handlers the next one. */
174238106Sdes	struct comm_point* tcp_free;
175238106Sdes
176238106Sdes	/* -------- SSL TCP DNS ------- */
177238106Sdes	/** the SSL object with rw bio (owned) or for commaccept ctx ref */
178238106Sdes	void* ssl;
179238106Sdes	/** handshake state for init and renegotiate */
180238106Sdes	enum {
181238106Sdes		/** no handshake, it has been done */
182238106Sdes		comm_ssl_shake_none = 0,
183238106Sdes		/** ssl initial handshake wants to read */
184238106Sdes		comm_ssl_shake_read,
185238106Sdes		/** ssl initial handshake wants to write */
186238106Sdes		comm_ssl_shake_write,
187238106Sdes		/** ssl_write wants to read */
188238106Sdes		comm_ssl_shake_hs_read,
189238106Sdes		/** ssl_read wants to write */
190238106Sdes		comm_ssl_shake_hs_write
191238106Sdes	} ssl_shake_state;
192238106Sdes
193285206Sdes	/* -------- dnstap ------- */
194285206Sdes	/** the dnstap environment */
195285206Sdes	struct dt_env* dtenv;
196285206Sdes
197238106Sdes	/** is this a UDP, TCP-accept or TCP socket. */
198238106Sdes	enum comm_point_type {
199238106Sdes		/** UDP socket - handle datagrams. */
200238106Sdes		comm_udp,
201238106Sdes		/** TCP accept socket - only creates handlers if readable. */
202238106Sdes		comm_tcp_accept,
203238106Sdes		/** TCP handler socket - handle byteperbyte readwrite. */
204238106Sdes		comm_tcp,
205238106Sdes		/** AF_UNIX socket - for internal commands. */
206238106Sdes		comm_local,
207238106Sdes		/** raw - not DNS format - for pipe readers and writers */
208238106Sdes		comm_raw
209238106Sdes	}
210238106Sdes		/** variable with type of socket, UDP,TCP-accept,TCP,pipe */
211238106Sdes		type;
212238106Sdes
213238106Sdes	/* ---------- Behaviour ----------- */
214238106Sdes	/** if set the connection is NOT closed on delete. */
215238106Sdes	int do_not_close;
216238106Sdes
217238106Sdes	/** if set, the connection is closed on error, on timeout,
218238106Sdes	    and after read/write completes. No callback is done. */
219238106Sdes	int tcp_do_close;
220238106Sdes
221238106Sdes	/** if set, read/write completes:
222238106Sdes		read/write state of tcp is toggled.
223238106Sdes		buffer reset/bytecount reset.
224238106Sdes		this flag cleared.
225238106Sdes	    So that when that is done the callback is called. */
226238106Sdes	int tcp_do_toggle_rw;
227238106Sdes
228238106Sdes	/** if set, checks for pending error from nonblocking connect() call.*/
229238106Sdes	int tcp_check_nb_connect;
230238106Sdes
231238106Sdes	/** number of queries outstanding on this socket, used by
232238106Sdes	 * outside network for udp ports */
233238106Sdes	int inuse;
234238106Sdes
235238106Sdes	/** callback when done.
236238106Sdes	    tcp_accept does not get called back, is NULL then.
237238106Sdes	    If a timeout happens, callback with timeout=1 is called.
238238106Sdes	    If an error happens, callback is called with error set
239238106Sdes	    nonzero. If not NETEVENT_NOERROR, it is an errno value.
240238106Sdes	    If the connection is closed (by remote end) then the
241238106Sdes	    callback is called with error set to NETEVENT_CLOSED=-1.
242238106Sdes	    If a timeout happens on the connection, the error is set to
243238106Sdes	    NETEVENT_TIMEOUT=-2.
244238106Sdes	    The reply_info can be copied if the reply needs to happen at a
245238106Sdes	    later time. It consists of a struct with commpoint and address.
246238106Sdes	    It can be passed to a msg send routine some time later.
247238106Sdes	    Note the reply information is temporary and must be copied.
248238106Sdes	    NULL is passed for_reply info, in cases where error happened.
249238106Sdes
250238106Sdes	    declare as:
251238106Sdes	    int my_callback(struct comm_point* c, void* my_arg, int error,
252238106Sdes		struct comm_reply *reply_info);
253238106Sdes
254238106Sdes	    if the routine returns 0, nothing is done.
255238106Sdes	    Notzero, the buffer will be sent back to client.
256238106Sdes	    		For UDP this is done without changing the commpoint.
257238106Sdes			In TCP it sets write state.
258238106Sdes	*/
259238106Sdes	comm_point_callback_t* callback;
260238106Sdes	/** argument to pass to callback. */
261238106Sdes	void *cb_arg;
262238106Sdes};
263238106Sdes
264238106Sdes/**
265238106Sdes * Structure only for making timeout events.
266238106Sdes */
267238106Sdesstruct comm_timer {
268238106Sdes	/** the internal event stuff */
269238106Sdes	struct internal_timer* ev_timer;
270238106Sdes
271238106Sdes	/** callback function, takes user arg only */
272238106Sdes	void (*callback)(void*);
273238106Sdes
274238106Sdes	/** callback user argument */
275238106Sdes	void* cb_arg;
276238106Sdes};
277238106Sdes
278238106Sdes/**
279238106Sdes * Structure only for signal events.
280238106Sdes */
281238106Sdesstruct comm_signal {
282238106Sdes	/** the communication base */
283238106Sdes	struct comm_base* base;
284238106Sdes
285238106Sdes	/** the internal event stuff */
286238106Sdes	struct internal_signal* ev_signal;
287238106Sdes
288238106Sdes	/** callback function, takes signal number and user arg */
289238106Sdes	void (*callback)(int, void*);
290238106Sdes
291238106Sdes	/** callback user argument */
292238106Sdes	void* cb_arg;
293238106Sdes};
294238106Sdes
295238106Sdes/**
296238106Sdes * Create a new comm base.
297238106Sdes * @param sigs: if true it attempts to create a default loop for
298238106Sdes *   signal handling.
299238106Sdes * @return: the new comm base. NULL on error.
300238106Sdes */
301238106Sdesstruct comm_base* comm_base_create(int sigs);
302238106Sdes
303238106Sdes/**
304269257Sdes * Create comm base that uses the given event_base (underlying event
305269257Sdes * mechanism pointer).
306269257Sdes * @param base: underlying lib event base.
307269257Sdes * @return: the new comm base. NULL on error.
308269257Sdes */
309269257Sdesstruct comm_base* comm_base_create_event(struct event_base* base);
310269257Sdes
311269257Sdes/**
312269257Sdes * Delete comm base structure but not the underlying lib event base.
313269257Sdes * All comm points must have been deleted.
314269257Sdes * @param b: the base to delete.
315269257Sdes */
316269257Sdesvoid comm_base_delete_no_base(struct comm_base* b);
317269257Sdes
318269257Sdes/**
319238106Sdes * Destroy a comm base.
320238106Sdes * All comm points must have been deleted.
321238106Sdes * @param b: the base to delete.
322238106Sdes */
323238106Sdesvoid comm_base_delete(struct comm_base* b);
324238106Sdes
325238106Sdes/**
326238106Sdes * Obtain two pointers. The pointers never change (until base_delete()).
327238106Sdes * The pointers point to time values that are updated regularly.
328238106Sdes * @param b: the communication base that will update the time values.
329238106Sdes * @param tt: pointer to time in seconds is returned.
330238106Sdes * @param tv: pointer to time in microseconds is returned.
331238106Sdes */
332269257Sdesvoid comm_base_timept(struct comm_base* b, time_t** tt, struct timeval** tv);
333238106Sdes
334238106Sdes/**
335238106Sdes * Dispatch the comm base events.
336238106Sdes * @param b: the communication to perform.
337238106Sdes */
338238106Sdesvoid comm_base_dispatch(struct comm_base* b);
339238106Sdes
340238106Sdes/**
341238106Sdes * Exit from dispatch loop.
342238106Sdes * @param b: the communication base that is in dispatch().
343238106Sdes */
344238106Sdesvoid comm_base_exit(struct comm_base* b);
345238106Sdes
346238106Sdes/**
347238106Sdes * Set the slow_accept mode handlers.  You can not provide these if you do
348238106Sdes * not perform accept() calls.
349238106Sdes * @param b: comm base
350238106Sdes * @param stop_accept: function that stops listening to accept fds.
351238106Sdes * @param start_accept: function that resumes listening to accept fds.
352238106Sdes * @param arg: callback arg to pass to the functions.
353238106Sdes */
354238106Sdesvoid comm_base_set_slow_accept_handlers(struct comm_base* b,
355238106Sdes	void (*stop_accept)(void*), void (*start_accept)(void*), void* arg);
356238106Sdes
357238106Sdes/**
358238106Sdes * Access internal data structure (for util/tube.c on windows)
359238106Sdes * @param b: comm base
360238106Sdes * @return event_base. Could be libevent, or internal event handler.
361238106Sdes */
362238106Sdesstruct event_base* comm_base_internal(struct comm_base* b);
363238106Sdes
364238106Sdes/**
365238106Sdes * Create an UDP comm point. Calls malloc.
366238106Sdes * setups the structure with the parameters you provide.
367238106Sdes * @param base: in which base to alloc the commpoint.
368238106Sdes * @param fd : file descriptor of open UDP socket.
369238106Sdes * @param buffer: shared buffer by UDP sockets from this thread.
370238106Sdes * @param callback: callback function pointer.
371238106Sdes * @param callback_arg: will be passed to your callback function.
372238106Sdes * @return: returns the allocated communication point. NULL on error.
373238106Sdes * Sets timeout to NULL. Turns off TCP options.
374238106Sdes */
375238106Sdesstruct comm_point* comm_point_create_udp(struct comm_base* base,
376269257Sdes	int fd, struct sldns_buffer* buffer,
377238106Sdes	comm_point_callback_t* callback, void* callback_arg);
378238106Sdes
379238106Sdes/**
380238106Sdes * Create an UDP with ancillary data comm point. Calls malloc.
381238106Sdes * Uses recvmsg instead of recv to get udp message.
382238106Sdes * setups the structure with the parameters you provide.
383238106Sdes * @param base: in which base to alloc the commpoint.
384238106Sdes * @param fd : file descriptor of open UDP socket.
385238106Sdes * @param buffer: shared buffer by UDP sockets from this thread.
386238106Sdes * @param callback: callback function pointer.
387238106Sdes * @param callback_arg: will be passed to your callback function.
388238106Sdes * @return: returns the allocated communication point. NULL on error.
389238106Sdes * Sets timeout to NULL. Turns off TCP options.
390238106Sdes */
391238106Sdesstruct comm_point* comm_point_create_udp_ancil(struct comm_base* base,
392269257Sdes	int fd, struct sldns_buffer* buffer,
393238106Sdes	comm_point_callback_t* callback, void* callback_arg);
394238106Sdes
395238106Sdes/**
396238106Sdes * Create a TCP listener comm point. Calls malloc.
397238106Sdes * Setups the structure with the parameters you provide.
398238106Sdes * Also Creates TCP Handlers, pre allocated for you.
399238106Sdes * Uses the parameters you provide.
400238106Sdes * @param base: in which base to alloc the commpoint.
401238106Sdes * @param fd: file descriptor of open TCP socket set to listen nonblocking.
402238106Sdes * @param num: becomes max_tcp_count, the routine allocates that
403238106Sdes *	many tcp handler commpoints.
404238106Sdes * @param bufsize: size of buffer to create for handlers.
405238106Sdes * @param callback: callback function pointer for TCP handlers.
406238106Sdes * @param callback_arg: will be passed to your callback function.
407238106Sdes * @return: returns the TCP listener commpoint. You can find the
408238106Sdes *  	TCP handlers in the array inside the listener commpoint.
409238106Sdes *	returns NULL on error.
410238106Sdes * Inits timeout to NULL. All handlers are on the free list.
411238106Sdes */
412238106Sdesstruct comm_point* comm_point_create_tcp(struct comm_base* base,
413238106Sdes	int fd, int num, size_t bufsize,
414238106Sdes	comm_point_callback_t* callback, void* callback_arg);
415238106Sdes
416238106Sdes/**
417238106Sdes * Create an outgoing TCP commpoint. No file descriptor is opened, left at -1.
418238106Sdes * @param base: in which base to alloc the commpoint.
419238106Sdes * @param bufsize: size of buffer to create for handlers.
420238106Sdes * @param callback: callback function pointer for the handler.
421238106Sdes * @param callback_arg: will be passed to your callback function.
422238106Sdes * @return: the commpoint or NULL on error.
423238106Sdes */
424238106Sdesstruct comm_point* comm_point_create_tcp_out(struct comm_base* base,
425238106Sdes	size_t bufsize, comm_point_callback_t* callback, void* callback_arg);
426238106Sdes
427238106Sdes/**
428238106Sdes * Create commpoint to listen to a local domain file descriptor.
429238106Sdes * @param base: in which base to alloc the commpoint.
430238106Sdes * @param fd: file descriptor of open AF_UNIX socket set to listen nonblocking.
431238106Sdes * @param bufsize: size of buffer to create for handlers.
432238106Sdes * @param callback: callback function pointer for the handler.
433238106Sdes * @param callback_arg: will be passed to your callback function.
434238106Sdes * @return: the commpoint or NULL on error.
435238106Sdes */
436238106Sdesstruct comm_point* comm_point_create_local(struct comm_base* base,
437238106Sdes	int fd, size_t bufsize,
438238106Sdes	comm_point_callback_t* callback, void* callback_arg);
439238106Sdes
440238106Sdes/**
441238106Sdes * Create commpoint to listen to a local domain pipe descriptor.
442238106Sdes * @param base: in which base to alloc the commpoint.
443238106Sdes * @param fd: file descriptor.
444238106Sdes * @param writing: true if you want to listen to writes, false for reads.
445238106Sdes * @param callback: callback function pointer for the handler.
446238106Sdes * @param callback_arg: will be passed to your callback function.
447238106Sdes * @return: the commpoint or NULL on error.
448238106Sdes */
449238106Sdesstruct comm_point* comm_point_create_raw(struct comm_base* base,
450238106Sdes	int fd, int writing,
451238106Sdes	comm_point_callback_t* callback, void* callback_arg);
452238106Sdes
453238106Sdes/**
454238106Sdes * Close a comm point fd.
455238106Sdes * @param c: comm point to close.
456238106Sdes */
457238106Sdesvoid comm_point_close(struct comm_point* c);
458238106Sdes
459238106Sdes/**
460238106Sdes * Close and deallocate (free) the comm point. If the comm point is
461238106Sdes * a tcp-accept point, also its tcp-handler points are deleted.
462238106Sdes * @param c: comm point to delete.
463238106Sdes */
464238106Sdesvoid comm_point_delete(struct comm_point* c);
465238106Sdes
466238106Sdes/**
467238106Sdes * Send reply. Put message into commpoint buffer.
468238106Sdes * @param repinfo: The reply info copied from a commpoint callback call.
469238106Sdes */
470238106Sdesvoid comm_point_send_reply(struct comm_reply* repinfo);
471238106Sdes
472238106Sdes/**
473238106Sdes * Drop reply. Cleans up.
474238106Sdes * @param repinfo: The reply info copied from a commpoint callback call.
475238106Sdes */
476238106Sdesvoid comm_point_drop_reply(struct comm_reply* repinfo);
477238106Sdes
478238106Sdes/**
479238106Sdes * Send an udp message over a commpoint.
480238106Sdes * @param c: commpoint to send it from.
481238106Sdes * @param packet: what to send.
482238106Sdes * @param addr: where to send it to.
483238106Sdes * @param addrlen: length of addr.
484238106Sdes * @return: false on a failure.
485238106Sdes */
486269257Sdesint comm_point_send_udp_msg(struct comm_point* c, struct sldns_buffer* packet,
487238106Sdes	struct sockaddr* addr, socklen_t addrlen);
488238106Sdes
489238106Sdes/**
490238106Sdes * Stop listening for input on the commpoint. No callbacks will happen.
491238106Sdes * @param c: commpoint to disable. The fd is not closed.
492238106Sdes */
493238106Sdesvoid comm_point_stop_listening(struct comm_point* c);
494238106Sdes
495238106Sdes/**
496238106Sdes * Start listening again for input on the comm point.
497238106Sdes * @param c: commpoint to enable again.
498238106Sdes * @param newfd: new fd, or -1 to leave fd be.
499238106Sdes * @param sec: timeout in seconds, or -1 for no (change to the) timeout.
500238106Sdes */
501238106Sdesvoid comm_point_start_listening(struct comm_point* c, int newfd, int sec);
502238106Sdes
503238106Sdes/**
504238106Sdes * Stop listening and start listening again for reading or writing.
505238106Sdes * @param c: commpoint
506238106Sdes * @param rd: if true, listens for reading.
507238106Sdes * @param wr: if true, listens for writing.
508238106Sdes */
509238106Sdesvoid comm_point_listen_for_rw(struct comm_point* c, int rd, int wr);
510238106Sdes
511238106Sdes/**
512238106Sdes * Get size of memory used by comm point.
513238106Sdes * For TCP handlers this includes subhandlers.
514238106Sdes * For UDP handlers, this does not include the (shared) UDP buffer.
515238106Sdes * @param c: commpoint.
516238106Sdes * @return size in bytes.
517238106Sdes */
518238106Sdessize_t comm_point_get_mem(struct comm_point* c);
519238106Sdes
520238106Sdes/**
521238106Sdes * create timer. Not active upon creation.
522238106Sdes * @param base: event handling base.
523238106Sdes * @param cb: callback function: void myfunc(void* myarg);
524238106Sdes * @param cb_arg: user callback argument.
525238106Sdes * @return: the new timer or NULL on error.
526238106Sdes */
527238106Sdesstruct comm_timer* comm_timer_create(struct comm_base* base,
528238106Sdes	void (*cb)(void*), void* cb_arg);
529238106Sdes
530238106Sdes/**
531238106Sdes * disable timer. Stops callbacks from happening.
532238106Sdes * @param timer: to disable.
533238106Sdes */
534238106Sdesvoid comm_timer_disable(struct comm_timer* timer);
535238106Sdes
536238106Sdes/**
537238106Sdes * reset timevalue for timer.
538238106Sdes * @param timer: timer to (re)set.
539238106Sdes * @param tv: when the timer should activate. if NULL timer is disabled.
540238106Sdes */
541238106Sdesvoid comm_timer_set(struct comm_timer* timer, struct timeval* tv);
542238106Sdes
543238106Sdes/**
544238106Sdes * delete timer.
545238106Sdes * @param timer: to delete.
546238106Sdes */
547238106Sdesvoid comm_timer_delete(struct comm_timer* timer);
548238106Sdes
549238106Sdes/**
550238106Sdes * see if timeout has been set to a value.
551238106Sdes * @param timer: the timer to examine.
552238106Sdes * @return: false if disabled or not set.
553238106Sdes */
554238106Sdesint comm_timer_is_set(struct comm_timer* timer);
555238106Sdes
556238106Sdes/**
557238106Sdes * Get size of memory used by comm timer.
558238106Sdes * @param timer: the timer to examine.
559238106Sdes * @return size in bytes.
560238106Sdes */
561238106Sdessize_t comm_timer_get_mem(struct comm_timer* timer);
562238106Sdes
563238106Sdes/**
564238106Sdes * Create a signal handler. Call signal_bind() later to bind to a signal.
565238106Sdes * @param base: communication base to use.
566238106Sdes * @param callback: called when signal is caught.
567238106Sdes * @param cb_arg: user argument to callback
568238106Sdes * @return: the signal struct or NULL on error.
569238106Sdes */
570238106Sdesstruct comm_signal* comm_signal_create(struct comm_base* base,
571238106Sdes	void (*callback)(int, void*), void* cb_arg);
572238106Sdes
573238106Sdes/**
574238106Sdes * Bind signal struct to catch a signal. A signle comm_signal can be bound
575238106Sdes * to multiple signals, calling comm_signal_bind multiple times.
576238106Sdes * @param comsig: the communication point, with callback information.
577238106Sdes * @param sig: signal number.
578238106Sdes * @return: true on success. false on error.
579238106Sdes */
580238106Sdesint comm_signal_bind(struct comm_signal* comsig, int sig);
581238106Sdes
582238106Sdes/**
583238106Sdes * Delete the signal communication point.
584238106Sdes * @param comsig: to delete.
585238106Sdes */
586238106Sdesvoid comm_signal_delete(struct comm_signal* comsig);
587238106Sdes
588238106Sdes/**
589238106Sdes * perform accept(2) with error checking.
590238106Sdes * @param c: commpoint with accept fd.
591238106Sdes * @param addr: remote end returned here.
592238106Sdes * @param addrlen: length of remote end returned here.
593238106Sdes * @return new fd, or -1 on error.
594238106Sdes *	if -1, error message has been printed if necessary, simply drop
595238106Sdes *	out of the reading handler.
596238106Sdes */
597238106Sdesint comm_point_perform_accept(struct comm_point* c,
598238106Sdes	struct sockaddr_storage* addr, socklen_t* addrlen);
599238106Sdes
600238106Sdes/**** internal routines ****/
601238106Sdes
602238106Sdes/**
603238106Sdes * This routine is published for checks and tests, and is only used internally.
604238106Sdes * handle libevent callback for udp comm point.
605238106Sdes * @param fd: file descriptor.
606238106Sdes * @param event: event bits from libevent:
607238106Sdes *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
608238106Sdes * @param arg: the comm_point structure.
609238106Sdes */
610238106Sdesvoid comm_point_udp_callback(int fd, short event, void* arg);
611238106Sdes
612238106Sdes/**
613238106Sdes * This routine is published for checks and tests, and is only used internally.
614238106Sdes * handle libevent callback for udp ancillary data comm point.
615238106Sdes * @param fd: file descriptor.
616238106Sdes * @param event: event bits from libevent:
617238106Sdes *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
618238106Sdes * @param arg: the comm_point structure.
619238106Sdes */
620238106Sdesvoid comm_point_udp_ancil_callback(int fd, short event, void* arg);
621238106Sdes
622238106Sdes/**
623238106Sdes * This routine is published for checks and tests, and is only used internally.
624238106Sdes * handle libevent callback for tcp accept comm point
625238106Sdes * @param fd: file descriptor.
626238106Sdes * @param event: event bits from libevent:
627238106Sdes *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
628238106Sdes * @param arg: the comm_point structure.
629238106Sdes */
630238106Sdesvoid comm_point_tcp_accept_callback(int fd, short event, void* arg);
631238106Sdes
632238106Sdes/**
633238106Sdes * This routine is published for checks and tests, and is only used internally.
634238106Sdes * handle libevent callback for tcp data comm point
635238106Sdes * @param fd: file descriptor.
636238106Sdes * @param event: event bits from libevent:
637238106Sdes *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
638238106Sdes * @param arg: the comm_point structure.
639238106Sdes */
640238106Sdesvoid comm_point_tcp_handle_callback(int fd, short event, void* arg);
641238106Sdes
642238106Sdes/**
643238106Sdes * This routine is published for checks and tests, and is only used internally.
644238106Sdes * handle libevent callback for timer comm.
645238106Sdes * @param fd: file descriptor (always -1).
646238106Sdes * @param event: event bits from libevent:
647238106Sdes *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
648238106Sdes * @param arg: the comm_timer structure.
649238106Sdes */
650238106Sdesvoid comm_timer_callback(int fd, short event, void* arg);
651238106Sdes
652238106Sdes/**
653238106Sdes * This routine is published for checks and tests, and is only used internally.
654238106Sdes * handle libevent callback for signal comm.
655238106Sdes * @param fd: file descriptor (used for the signal number).
656238106Sdes * @param event: event bits from libevent:
657238106Sdes *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
658238106Sdes * @param arg: the internal commsignal structure.
659238106Sdes */
660238106Sdesvoid comm_signal_callback(int fd, short event, void* arg);
661238106Sdes
662238106Sdes/**
663238106Sdes * This routine is published for checks and tests, and is only used internally.
664238106Sdes * libevent callback for AF_UNIX fds
665238106Sdes * @param fd: file descriptor.
666238106Sdes * @param event: event bits from libevent:
667238106Sdes *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
668238106Sdes * @param arg: the comm_point structure.
669238106Sdes */
670238106Sdesvoid comm_point_local_handle_callback(int fd, short event, void* arg);
671238106Sdes
672238106Sdes/**
673238106Sdes * This routine is published for checks and tests, and is only used internally.
674238106Sdes * libevent callback for raw fd access.
675238106Sdes * @param fd: file descriptor.
676238106Sdes * @param event: event bits from libevent:
677238106Sdes *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
678238106Sdes * @param arg: the comm_point structure.
679238106Sdes */
680238106Sdesvoid comm_point_raw_handle_callback(int fd, short event, void* arg);
681238106Sdes
682238106Sdes/**
683238106Sdes * This routine is published for checks and tests, and is only used internally.
684238106Sdes * libevent callback for timeout on slow accept.
685238106Sdes * @param fd: file descriptor.
686238106Sdes * @param event: event bits from libevent:
687238106Sdes *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
688238106Sdes * @param arg: the comm_point structure.
689238106Sdes */
690238106Sdesvoid comm_base_handle_slow_accept(int fd, short event, void* arg);
691238106Sdes
692238106Sdes#ifdef USE_WINSOCK
693238106Sdes/**
694238106Sdes * Callback for openssl BIO to on windows detect WSAEWOULDBLOCK and notify
695238106Sdes * the winsock_event of this for proper TCP nonblocking implementation.
696238106Sdes * @param c: comm_point, fd must be set its struct event is registered.
697238106Sdes * @param ssl: openssl SSL, fd must be set so it has a bio.
698238106Sdes */
699238106Sdesvoid comm_point_tcp_win_bio_cb(struct comm_point* c, void* ssl);
700238106Sdes#endif
701238106Sdes
702238106Sdes/** see if errno for tcp connect has to be logged or not. This uses errno */
703238106Sdesint tcp_connect_errno_needs_log(struct sockaddr* addr, socklen_t addrlen);
704238106Sdes
705238106Sdes#endif /* NET_EVENT_H */
706