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;
167238106Sdes	/** malloced array of tcp handlers for a tcp-accept,
168238106Sdes	    of size max_tcp_count. */
169238106Sdes	struct comm_point** tcp_handlers;
170238106Sdes	/** linked list of free tcp_handlers to use for new queries.
171238106Sdes	    For tcp_accept the first entry, for tcp_handlers the next one. */
172238106Sdes	struct comm_point* tcp_free;
173238106Sdes
174238106Sdes	/* -------- SSL TCP DNS ------- */
175238106Sdes	/** the SSL object with rw bio (owned) or for commaccept ctx ref */
176238106Sdes	void* ssl;
177238106Sdes	/** handshake state for init and renegotiate */
178238106Sdes	enum {
179238106Sdes		/** no handshake, it has been done */
180238106Sdes		comm_ssl_shake_none = 0,
181238106Sdes		/** ssl initial handshake wants to read */
182238106Sdes		comm_ssl_shake_read,
183238106Sdes		/** ssl initial handshake wants to write */
184238106Sdes		comm_ssl_shake_write,
185238106Sdes		/** ssl_write wants to read */
186238106Sdes		comm_ssl_shake_hs_read,
187238106Sdes		/** ssl_read wants to write */
188238106Sdes		comm_ssl_shake_hs_write
189238106Sdes	} ssl_shake_state;
190238106Sdes
191238106Sdes	/** is this a UDP, TCP-accept or TCP socket. */
192238106Sdes	enum comm_point_type {
193238106Sdes		/** UDP socket - handle datagrams. */
194238106Sdes		comm_udp,
195238106Sdes		/** TCP accept socket - only creates handlers if readable. */
196238106Sdes		comm_tcp_accept,
197238106Sdes		/** TCP handler socket - handle byteperbyte readwrite. */
198238106Sdes		comm_tcp,
199238106Sdes		/** AF_UNIX socket - for internal commands. */
200238106Sdes		comm_local,
201238106Sdes		/** raw - not DNS format - for pipe readers and writers */
202238106Sdes		comm_raw
203238106Sdes	}
204238106Sdes		/** variable with type of socket, UDP,TCP-accept,TCP,pipe */
205238106Sdes		type;
206238106Sdes
207238106Sdes	/* ---------- Behaviour ----------- */
208238106Sdes	/** if set the connection is NOT closed on delete. */
209238106Sdes	int do_not_close;
210238106Sdes
211238106Sdes	/** if set, the connection is closed on error, on timeout,
212238106Sdes	    and after read/write completes. No callback is done. */
213238106Sdes	int tcp_do_close;
214238106Sdes
215238106Sdes	/** if set, read/write completes:
216238106Sdes		read/write state of tcp is toggled.
217238106Sdes		buffer reset/bytecount reset.
218238106Sdes		this flag cleared.
219238106Sdes	    So that when that is done the callback is called. */
220238106Sdes	int tcp_do_toggle_rw;
221238106Sdes
222238106Sdes	/** if set, checks for pending error from nonblocking connect() call.*/
223238106Sdes	int tcp_check_nb_connect;
224238106Sdes
225238106Sdes	/** number of queries outstanding on this socket, used by
226238106Sdes	 * outside network for udp ports */
227238106Sdes	int inuse;
228238106Sdes
229238106Sdes	/** callback when done.
230238106Sdes	    tcp_accept does not get called back, is NULL then.
231238106Sdes	    If a timeout happens, callback with timeout=1 is called.
232238106Sdes	    If an error happens, callback is called with error set
233238106Sdes	    nonzero. If not NETEVENT_NOERROR, it is an errno value.
234238106Sdes	    If the connection is closed (by remote end) then the
235238106Sdes	    callback is called with error set to NETEVENT_CLOSED=-1.
236238106Sdes	    If a timeout happens on the connection, the error is set to
237238106Sdes	    NETEVENT_TIMEOUT=-2.
238238106Sdes	    The reply_info can be copied if the reply needs to happen at a
239238106Sdes	    later time. It consists of a struct with commpoint and address.
240238106Sdes	    It can be passed to a msg send routine some time later.
241238106Sdes	    Note the reply information is temporary and must be copied.
242238106Sdes	    NULL is passed for_reply info, in cases where error happened.
243238106Sdes
244238106Sdes	    declare as:
245238106Sdes	    int my_callback(struct comm_point* c, void* my_arg, int error,
246238106Sdes		struct comm_reply *reply_info);
247238106Sdes
248238106Sdes	    if the routine returns 0, nothing is done.
249238106Sdes	    Notzero, the buffer will be sent back to client.
250238106Sdes	    		For UDP this is done without changing the commpoint.
251238106Sdes			In TCP it sets write state.
252238106Sdes	*/
253238106Sdes	comm_point_callback_t* callback;
254238106Sdes	/** argument to pass to callback. */
255238106Sdes	void *cb_arg;
256238106Sdes};
257238106Sdes
258238106Sdes/**
259238106Sdes * Structure only for making timeout events.
260238106Sdes */
261238106Sdesstruct comm_timer {
262238106Sdes	/** the internal event stuff */
263238106Sdes	struct internal_timer* ev_timer;
264238106Sdes
265238106Sdes	/** callback function, takes user arg only */
266238106Sdes	void (*callback)(void*);
267238106Sdes
268238106Sdes	/** callback user argument */
269238106Sdes	void* cb_arg;
270238106Sdes};
271238106Sdes
272238106Sdes/**
273238106Sdes * Structure only for signal events.
274238106Sdes */
275238106Sdesstruct comm_signal {
276238106Sdes	/** the communication base */
277238106Sdes	struct comm_base* base;
278238106Sdes
279238106Sdes	/** the internal event stuff */
280238106Sdes	struct internal_signal* ev_signal;
281238106Sdes
282238106Sdes	/** callback function, takes signal number and user arg */
283238106Sdes	void (*callback)(int, void*);
284238106Sdes
285238106Sdes	/** callback user argument */
286238106Sdes	void* cb_arg;
287238106Sdes};
288238106Sdes
289238106Sdes/**
290238106Sdes * Create a new comm base.
291238106Sdes * @param sigs: if true it attempts to create a default loop for
292238106Sdes *   signal handling.
293238106Sdes * @return: the new comm base. NULL on error.
294238106Sdes */
295238106Sdesstruct comm_base* comm_base_create(int sigs);
296238106Sdes
297238106Sdes/**
298269257Sdes * Create comm base that uses the given event_base (underlying event
299269257Sdes * mechanism pointer).
300269257Sdes * @param base: underlying lib event base.
301269257Sdes * @return: the new comm base. NULL on error.
302269257Sdes */
303269257Sdesstruct comm_base* comm_base_create_event(struct event_base* base);
304269257Sdes
305269257Sdes/**
306269257Sdes * Delete comm base structure but not the underlying lib event base.
307269257Sdes * All comm points must have been deleted.
308269257Sdes * @param b: the base to delete.
309269257Sdes */
310269257Sdesvoid comm_base_delete_no_base(struct comm_base* b);
311269257Sdes
312269257Sdes/**
313238106Sdes * Destroy a comm base.
314238106Sdes * All comm points must have been deleted.
315238106Sdes * @param b: the base to delete.
316238106Sdes */
317238106Sdesvoid comm_base_delete(struct comm_base* b);
318238106Sdes
319238106Sdes/**
320238106Sdes * Obtain two pointers. The pointers never change (until base_delete()).
321238106Sdes * The pointers point to time values that are updated regularly.
322238106Sdes * @param b: the communication base that will update the time values.
323238106Sdes * @param tt: pointer to time in seconds is returned.
324238106Sdes * @param tv: pointer to time in microseconds is returned.
325238106Sdes */
326269257Sdesvoid comm_base_timept(struct comm_base* b, time_t** tt, struct timeval** tv);
327238106Sdes
328238106Sdes/**
329238106Sdes * Dispatch the comm base events.
330238106Sdes * @param b: the communication to perform.
331238106Sdes */
332238106Sdesvoid comm_base_dispatch(struct comm_base* b);
333238106Sdes
334238106Sdes/**
335238106Sdes * Exit from dispatch loop.
336238106Sdes * @param b: the communication base that is in dispatch().
337238106Sdes */
338238106Sdesvoid comm_base_exit(struct comm_base* b);
339238106Sdes
340238106Sdes/**
341238106Sdes * Set the slow_accept mode handlers.  You can not provide these if you do
342238106Sdes * not perform accept() calls.
343238106Sdes * @param b: comm base
344238106Sdes * @param stop_accept: function that stops listening to accept fds.
345238106Sdes * @param start_accept: function that resumes listening to accept fds.
346238106Sdes * @param arg: callback arg to pass to the functions.
347238106Sdes */
348238106Sdesvoid comm_base_set_slow_accept_handlers(struct comm_base* b,
349238106Sdes	void (*stop_accept)(void*), void (*start_accept)(void*), void* arg);
350238106Sdes
351238106Sdes/**
352238106Sdes * Access internal data structure (for util/tube.c on windows)
353238106Sdes * @param b: comm base
354238106Sdes * @return event_base. Could be libevent, or internal event handler.
355238106Sdes */
356238106Sdesstruct event_base* comm_base_internal(struct comm_base* b);
357238106Sdes
358238106Sdes/**
359238106Sdes * Create an UDP comm point. Calls malloc.
360238106Sdes * setups the structure with the parameters you provide.
361238106Sdes * @param base: in which base to alloc the commpoint.
362238106Sdes * @param fd : file descriptor of open UDP socket.
363238106Sdes * @param buffer: shared buffer by UDP sockets from this thread.
364238106Sdes * @param callback: callback function pointer.
365238106Sdes * @param callback_arg: will be passed to your callback function.
366238106Sdes * @return: returns the allocated communication point. NULL on error.
367238106Sdes * Sets timeout to NULL. Turns off TCP options.
368238106Sdes */
369238106Sdesstruct comm_point* comm_point_create_udp(struct comm_base* base,
370269257Sdes	int fd, struct sldns_buffer* buffer,
371238106Sdes	comm_point_callback_t* callback, void* callback_arg);
372238106Sdes
373238106Sdes/**
374238106Sdes * Create an UDP with ancillary data comm point. Calls malloc.
375238106Sdes * Uses recvmsg instead of recv to get udp message.
376238106Sdes * setups the structure with the parameters you provide.
377238106Sdes * @param base: in which base to alloc the commpoint.
378238106Sdes * @param fd : file descriptor of open UDP socket.
379238106Sdes * @param buffer: shared buffer by UDP sockets from this thread.
380238106Sdes * @param callback: callback function pointer.
381238106Sdes * @param callback_arg: will be passed to your callback function.
382238106Sdes * @return: returns the allocated communication point. NULL on error.
383238106Sdes * Sets timeout to NULL. Turns off TCP options.
384238106Sdes */
385238106Sdesstruct comm_point* comm_point_create_udp_ancil(struct comm_base* base,
386269257Sdes	int fd, struct sldns_buffer* buffer,
387238106Sdes	comm_point_callback_t* callback, void* callback_arg);
388238106Sdes
389238106Sdes/**
390238106Sdes * Create a TCP listener comm point. Calls malloc.
391238106Sdes * Setups the structure with the parameters you provide.
392238106Sdes * Also Creates TCP Handlers, pre allocated for you.
393238106Sdes * Uses the parameters you provide.
394238106Sdes * @param base: in which base to alloc the commpoint.
395238106Sdes * @param fd: file descriptor of open TCP socket set to listen nonblocking.
396238106Sdes * @param num: becomes max_tcp_count, the routine allocates that
397238106Sdes *	many tcp handler commpoints.
398238106Sdes * @param bufsize: size of buffer to create for handlers.
399238106Sdes * @param callback: callback function pointer for TCP handlers.
400238106Sdes * @param callback_arg: will be passed to your callback function.
401238106Sdes * @return: returns the TCP listener commpoint. You can find the
402238106Sdes *  	TCP handlers in the array inside the listener commpoint.
403238106Sdes *	returns NULL on error.
404238106Sdes * Inits timeout to NULL. All handlers are on the free list.
405238106Sdes */
406238106Sdesstruct comm_point* comm_point_create_tcp(struct comm_base* base,
407238106Sdes	int fd, int num, size_t bufsize,
408238106Sdes	comm_point_callback_t* callback, void* callback_arg);
409238106Sdes
410238106Sdes/**
411238106Sdes * Create an outgoing TCP commpoint. No file descriptor is opened, left at -1.
412238106Sdes * @param base: in which base to alloc the commpoint.
413238106Sdes * @param bufsize: size of buffer to create for handlers.
414238106Sdes * @param callback: callback function pointer for the handler.
415238106Sdes * @param callback_arg: will be passed to your callback function.
416238106Sdes * @return: the commpoint or NULL on error.
417238106Sdes */
418238106Sdesstruct comm_point* comm_point_create_tcp_out(struct comm_base* base,
419238106Sdes	size_t bufsize, comm_point_callback_t* callback, void* callback_arg);
420238106Sdes
421238106Sdes/**
422238106Sdes * Create commpoint to listen to a local domain file descriptor.
423238106Sdes * @param base: in which base to alloc the commpoint.
424238106Sdes * @param fd: file descriptor of open AF_UNIX socket set to listen nonblocking.
425238106Sdes * @param bufsize: size of buffer to create for handlers.
426238106Sdes * @param callback: callback function pointer for the handler.
427238106Sdes * @param callback_arg: will be passed to your callback function.
428238106Sdes * @return: the commpoint or NULL on error.
429238106Sdes */
430238106Sdesstruct comm_point* comm_point_create_local(struct comm_base* base,
431238106Sdes	int fd, size_t bufsize,
432238106Sdes	comm_point_callback_t* callback, void* callback_arg);
433238106Sdes
434238106Sdes/**
435238106Sdes * Create commpoint to listen to a local domain pipe descriptor.
436238106Sdes * @param base: in which base to alloc the commpoint.
437238106Sdes * @param fd: file descriptor.
438238106Sdes * @param writing: true if you want to listen to writes, false for reads.
439238106Sdes * @param callback: callback function pointer for the handler.
440238106Sdes * @param callback_arg: will be passed to your callback function.
441238106Sdes * @return: the commpoint or NULL on error.
442238106Sdes */
443238106Sdesstruct comm_point* comm_point_create_raw(struct comm_base* base,
444238106Sdes	int fd, int writing,
445238106Sdes	comm_point_callback_t* callback, void* callback_arg);
446238106Sdes
447238106Sdes/**
448238106Sdes * Close a comm point fd.
449238106Sdes * @param c: comm point to close.
450238106Sdes */
451238106Sdesvoid comm_point_close(struct comm_point* c);
452238106Sdes
453238106Sdes/**
454238106Sdes * Close and deallocate (free) the comm point. If the comm point is
455238106Sdes * a tcp-accept point, also its tcp-handler points are deleted.
456238106Sdes * @param c: comm point to delete.
457238106Sdes */
458238106Sdesvoid comm_point_delete(struct comm_point* c);
459238106Sdes
460238106Sdes/**
461238106Sdes * Send reply. Put message into commpoint buffer.
462238106Sdes * @param repinfo: The reply info copied from a commpoint callback call.
463238106Sdes */
464238106Sdesvoid comm_point_send_reply(struct comm_reply* repinfo);
465238106Sdes
466238106Sdes/**
467238106Sdes * Drop reply. Cleans up.
468238106Sdes * @param repinfo: The reply info copied from a commpoint callback call.
469238106Sdes */
470238106Sdesvoid comm_point_drop_reply(struct comm_reply* repinfo);
471238106Sdes
472238106Sdes/**
473238106Sdes * Send an udp message over a commpoint.
474238106Sdes * @param c: commpoint to send it from.
475238106Sdes * @param packet: what to send.
476238106Sdes * @param addr: where to send it to.
477238106Sdes * @param addrlen: length of addr.
478238106Sdes * @return: false on a failure.
479238106Sdes */
480269257Sdesint comm_point_send_udp_msg(struct comm_point* c, struct sldns_buffer* packet,
481238106Sdes	struct sockaddr* addr, socklen_t addrlen);
482238106Sdes
483238106Sdes/**
484238106Sdes * Stop listening for input on the commpoint. No callbacks will happen.
485238106Sdes * @param c: commpoint to disable. The fd is not closed.
486238106Sdes */
487238106Sdesvoid comm_point_stop_listening(struct comm_point* c);
488238106Sdes
489238106Sdes/**
490238106Sdes * Start listening again for input on the comm point.
491238106Sdes * @param c: commpoint to enable again.
492238106Sdes * @param newfd: new fd, or -1 to leave fd be.
493238106Sdes * @param sec: timeout in seconds, or -1 for no (change to the) timeout.
494238106Sdes */
495238106Sdesvoid comm_point_start_listening(struct comm_point* c, int newfd, int sec);
496238106Sdes
497238106Sdes/**
498238106Sdes * Stop listening and start listening again for reading or writing.
499238106Sdes * @param c: commpoint
500238106Sdes * @param rd: if true, listens for reading.
501238106Sdes * @param wr: if true, listens for writing.
502238106Sdes */
503238106Sdesvoid comm_point_listen_for_rw(struct comm_point* c, int rd, int wr);
504238106Sdes
505238106Sdes/**
506238106Sdes * Get size of memory used by comm point.
507238106Sdes * For TCP handlers this includes subhandlers.
508238106Sdes * For UDP handlers, this does not include the (shared) UDP buffer.
509238106Sdes * @param c: commpoint.
510238106Sdes * @return size in bytes.
511238106Sdes */
512238106Sdessize_t comm_point_get_mem(struct comm_point* c);
513238106Sdes
514238106Sdes/**
515238106Sdes * create timer. Not active upon creation.
516238106Sdes * @param base: event handling base.
517238106Sdes * @param cb: callback function: void myfunc(void* myarg);
518238106Sdes * @param cb_arg: user callback argument.
519238106Sdes * @return: the new timer or NULL on error.
520238106Sdes */
521238106Sdesstruct comm_timer* comm_timer_create(struct comm_base* base,
522238106Sdes	void (*cb)(void*), void* cb_arg);
523238106Sdes
524238106Sdes/**
525238106Sdes * disable timer. Stops callbacks from happening.
526238106Sdes * @param timer: to disable.
527238106Sdes */
528238106Sdesvoid comm_timer_disable(struct comm_timer* timer);
529238106Sdes
530238106Sdes/**
531238106Sdes * reset timevalue for timer.
532238106Sdes * @param timer: timer to (re)set.
533238106Sdes * @param tv: when the timer should activate. if NULL timer is disabled.
534238106Sdes */
535238106Sdesvoid comm_timer_set(struct comm_timer* timer, struct timeval* tv);
536238106Sdes
537238106Sdes/**
538238106Sdes * delete timer.
539238106Sdes * @param timer: to delete.
540238106Sdes */
541238106Sdesvoid comm_timer_delete(struct comm_timer* timer);
542238106Sdes
543238106Sdes/**
544238106Sdes * see if timeout has been set to a value.
545238106Sdes * @param timer: the timer to examine.
546238106Sdes * @return: false if disabled or not set.
547238106Sdes */
548238106Sdesint comm_timer_is_set(struct comm_timer* timer);
549238106Sdes
550238106Sdes/**
551238106Sdes * Get size of memory used by comm timer.
552238106Sdes * @param timer: the timer to examine.
553238106Sdes * @return size in bytes.
554238106Sdes */
555238106Sdessize_t comm_timer_get_mem(struct comm_timer* timer);
556238106Sdes
557238106Sdes/**
558238106Sdes * Create a signal handler. Call signal_bind() later to bind to a signal.
559238106Sdes * @param base: communication base to use.
560238106Sdes * @param callback: called when signal is caught.
561238106Sdes * @param cb_arg: user argument to callback
562238106Sdes * @return: the signal struct or NULL on error.
563238106Sdes */
564238106Sdesstruct comm_signal* comm_signal_create(struct comm_base* base,
565238106Sdes	void (*callback)(int, void*), void* cb_arg);
566238106Sdes
567238106Sdes/**
568238106Sdes * Bind signal struct to catch a signal. A signle comm_signal can be bound
569238106Sdes * to multiple signals, calling comm_signal_bind multiple times.
570238106Sdes * @param comsig: the communication point, with callback information.
571238106Sdes * @param sig: signal number.
572238106Sdes * @return: true on success. false on error.
573238106Sdes */
574238106Sdesint comm_signal_bind(struct comm_signal* comsig, int sig);
575238106Sdes
576238106Sdes/**
577238106Sdes * Delete the signal communication point.
578238106Sdes * @param comsig: to delete.
579238106Sdes */
580238106Sdesvoid comm_signal_delete(struct comm_signal* comsig);
581238106Sdes
582238106Sdes/**
583238106Sdes * perform accept(2) with error checking.
584238106Sdes * @param c: commpoint with accept fd.
585238106Sdes * @param addr: remote end returned here.
586238106Sdes * @param addrlen: length of remote end returned here.
587238106Sdes * @return new fd, or -1 on error.
588238106Sdes *	if -1, error message has been printed if necessary, simply drop
589238106Sdes *	out of the reading handler.
590238106Sdes */
591238106Sdesint comm_point_perform_accept(struct comm_point* c,
592238106Sdes	struct sockaddr_storage* addr, socklen_t* addrlen);
593238106Sdes
594238106Sdes/**** internal routines ****/
595238106Sdes
596238106Sdes/**
597238106Sdes * This routine is published for checks and tests, and is only used internally.
598238106Sdes * handle libevent callback for udp comm point.
599238106Sdes * @param fd: file descriptor.
600238106Sdes * @param event: event bits from libevent:
601238106Sdes *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
602238106Sdes * @param arg: the comm_point structure.
603238106Sdes */
604238106Sdesvoid comm_point_udp_callback(int fd, short event, void* arg);
605238106Sdes
606238106Sdes/**
607238106Sdes * This routine is published for checks and tests, and is only used internally.
608238106Sdes * handle libevent callback for udp ancillary data comm point.
609238106Sdes * @param fd: file descriptor.
610238106Sdes * @param event: event bits from libevent:
611238106Sdes *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
612238106Sdes * @param arg: the comm_point structure.
613238106Sdes */
614238106Sdesvoid comm_point_udp_ancil_callback(int fd, short event, void* arg);
615238106Sdes
616238106Sdes/**
617238106Sdes * This routine is published for checks and tests, and is only used internally.
618238106Sdes * handle libevent callback for tcp accept comm point
619238106Sdes * @param fd: file descriptor.
620238106Sdes * @param event: event bits from libevent:
621238106Sdes *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
622238106Sdes * @param arg: the comm_point structure.
623238106Sdes */
624238106Sdesvoid comm_point_tcp_accept_callback(int fd, short event, void* arg);
625238106Sdes
626238106Sdes/**
627238106Sdes * This routine is published for checks and tests, and is only used internally.
628238106Sdes * handle libevent callback for tcp data comm point
629238106Sdes * @param fd: file descriptor.
630238106Sdes * @param event: event bits from libevent:
631238106Sdes *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
632238106Sdes * @param arg: the comm_point structure.
633238106Sdes */
634238106Sdesvoid comm_point_tcp_handle_callback(int fd, short event, void* arg);
635238106Sdes
636238106Sdes/**
637238106Sdes * This routine is published for checks and tests, and is only used internally.
638238106Sdes * handle libevent callback for timer comm.
639238106Sdes * @param fd: file descriptor (always -1).
640238106Sdes * @param event: event bits from libevent:
641238106Sdes *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
642238106Sdes * @param arg: the comm_timer structure.
643238106Sdes */
644238106Sdesvoid comm_timer_callback(int fd, short event, void* arg);
645238106Sdes
646238106Sdes/**
647238106Sdes * This routine is published for checks and tests, and is only used internally.
648238106Sdes * handle libevent callback for signal comm.
649238106Sdes * @param fd: file descriptor (used for the signal number).
650238106Sdes * @param event: event bits from libevent:
651238106Sdes *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
652238106Sdes * @param arg: the internal commsignal structure.
653238106Sdes */
654238106Sdesvoid comm_signal_callback(int fd, short event, void* arg);
655238106Sdes
656238106Sdes/**
657238106Sdes * This routine is published for checks and tests, and is only used internally.
658238106Sdes * libevent callback for AF_UNIX fds
659238106Sdes * @param fd: file descriptor.
660238106Sdes * @param event: event bits from libevent:
661238106Sdes *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
662238106Sdes * @param arg: the comm_point structure.
663238106Sdes */
664238106Sdesvoid comm_point_local_handle_callback(int fd, short event, void* arg);
665238106Sdes
666238106Sdes/**
667238106Sdes * This routine is published for checks and tests, and is only used internally.
668238106Sdes * libevent callback for raw fd access.
669238106Sdes * @param fd: file descriptor.
670238106Sdes * @param event: event bits from libevent:
671238106Sdes *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
672238106Sdes * @param arg: the comm_point structure.
673238106Sdes */
674238106Sdesvoid comm_point_raw_handle_callback(int fd, short event, void* arg);
675238106Sdes
676238106Sdes/**
677238106Sdes * This routine is published for checks and tests, and is only used internally.
678238106Sdes * libevent callback for timeout on slow accept.
679238106Sdes * @param fd: file descriptor.
680238106Sdes * @param event: event bits from libevent:
681238106Sdes *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
682238106Sdes * @param arg: the comm_point structure.
683238106Sdes */
684238106Sdesvoid comm_base_handle_slow_accept(int fd, short event, void* arg);
685238106Sdes
686238106Sdes#ifdef USE_WINSOCK
687238106Sdes/**
688238106Sdes * Callback for openssl BIO to on windows detect WSAEWOULDBLOCK and notify
689238106Sdes * the winsock_event of this for proper TCP nonblocking implementation.
690238106Sdes * @param c: comm_point, fd must be set its struct event is registered.
691238106Sdes * @param ssl: openssl SSL, fd must be set so it has a bio.
692238106Sdes */
693238106Sdesvoid comm_point_tcp_win_bio_cb(struct comm_point* c, void* ssl);
694238106Sdes#endif
695238106Sdes
696238106Sdes/** see if errno for tcp connect has to be logged or not. This uses errno */
697238106Sdesint tcp_connect_errno_needs_log(struct sockaddr* addr, socklen_t addrlen);
698238106Sdes
699238106Sdes#endif /* NET_EVENT_H */
700