Electroneum
netevent.h File Reference
Include dependency graph for netevent.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  comm_base
 
struct  comm_reply
 
struct  comm_point
 
struct  comm_timer
 
struct  comm_signal
 

Macros

#define NETEVENT_NOERROR   0
 
#define NETEVENT_CLOSED   -1
 
#define NETEVENT_TIMEOUT   -2
 
#define NETEVENT_CAPSFAIL   -3
 
#define NETEVENT_DONE   -4
 
#define NETEVENT_SLOW_ACCEPT_TIME   2000
 

Typedefs

typedef int comm_point_callback_type(struct comm_point *, void *, int, struct comm_reply *)
 

Functions

struct comm_basecomm_base_create (int sigs)
 
struct comm_basecomm_base_create_event (struct ub_event_base *base)
 
void comm_base_delete_no_base (struct comm_base *b)
 
void comm_base_delete (struct comm_base *b)
 
void comm_base_timept (struct comm_base *b, time_t **tt, struct timeval **tv)
 
void comm_base_dispatch (struct comm_base *b)
 
void comm_base_exit (struct comm_base *b)
 
void comm_base_set_slow_accept_handlers (struct comm_base *b, void(*stop_accept)(void *), void(*start_accept)(void *), void *arg)
 
struct ub_event_basecomm_base_internal (struct comm_base *b)
 
struct comm_pointcomm_point_create_udp (struct comm_base *base, int fd, struct sldns_buffer *buffer, comm_point_callback_type *callback, void *callback_arg)
 
struct comm_pointcomm_point_create_udp_ancil (struct comm_base *base, int fd, struct sldns_buffer *buffer, comm_point_callback_type *callback, void *callback_arg)
 
struct comm_pointcomm_point_create_tcp (struct comm_base *base, int fd, int num, size_t bufsize, comm_point_callback_type *callback, void *callback_arg)
 
struct comm_pointcomm_point_create_tcp_out (struct comm_base *base, size_t bufsize, comm_point_callback_type *callback, void *callback_arg)
 
struct comm_pointcomm_point_create_http_out (struct comm_base *base, size_t bufsize, comm_point_callback_type *callback, void *callback_arg, struct sldns_buffer *temp)
 
struct comm_pointcomm_point_create_local (struct comm_base *base, int fd, size_t bufsize, comm_point_callback_type *callback, void *callback_arg)
 
struct comm_pointcomm_point_create_raw (struct comm_base *base, int fd, int writing, comm_point_callback_type *callback, void *callback_arg)
 
void comm_point_close (struct comm_point *c)
 
void comm_point_delete (struct comm_point *c)
 
void comm_point_send_reply (struct comm_reply *repinfo)
 
void comm_point_drop_reply (struct comm_reply *repinfo)
 
int comm_point_send_udp_msg (struct comm_point *c, struct sldns_buffer *packet, struct sockaddr *addr, socklen_t addrlen)
 
void comm_point_stop_listening (struct comm_point *c)
 
void comm_point_start_listening (struct comm_point *c, int newfd, int msec)
 
void comm_point_listen_for_rw (struct comm_point *c, int rd, int wr)
 
size_t comm_point_get_mem (struct comm_point *c)
 
struct comm_timercomm_timer_create (struct comm_base *base, void(*cb)(void *), void *cb_arg)
 
void comm_timer_disable (struct comm_timer *timer)
 
void comm_timer_set (struct comm_timer *timer, struct timeval *tv)
 
void comm_timer_delete (struct comm_timer *timer)
 
int comm_timer_is_set (struct comm_timer *timer)
 
size_t comm_timer_get_mem (struct comm_timer *timer)
 
struct comm_signalcomm_signal_create (struct comm_base *base, void(*callback)(int, void *), void *cb_arg)
 
int comm_signal_bind (struct comm_signal *comsig, int sig)
 
void comm_signal_delete (struct comm_signal *comsig)
 
int comm_point_perform_accept (struct comm_point *c, struct sockaddr_storage *addr, socklen_t *addrlen)
 
void comm_point_udp_callback (int fd, short event, void *arg)
 
void comm_point_udp_ancil_callback (int fd, short event, void *arg)
 
void comm_point_tcp_accept_callback (int fd, short event, void *arg)
 
void comm_point_tcp_handle_callback (int fd, short event, void *arg)
 
void comm_point_http_handle_callback (int fd, short event, void *arg)
 
void comm_timer_callback (int fd, short event, void *arg)
 
void comm_signal_callback (int fd, short event, void *arg)
 
void comm_point_local_handle_callback (int fd, short event, void *arg)
 
void comm_point_raw_handle_callback (int fd, short event, void *arg)
 
void comm_base_handle_slow_accept (int fd, short event, void *arg)
 
int tcp_connect_errno_needs_log (struct sockaddr *addr, socklen_t addrlen)
 

Detailed Description

This file contains event notification functions.

There are three types of communication points o UDP socket - perthread buffer. o TCP-accept socket - array of TCP-sockets, socketcount. o TCP socket - own buffer, parent-TCPaccept, read/write state, number of bytes read/written, timeout.

There are sockets aimed towards our clients and towards the internet. o frontside - aimed towards our clients, queries come in, answers back. o behind - aimed towards internet, to the authoritative DNS servers.

Several event types are available: o comm_base - for thread safety of the comm points, one per thread. o comm_point - udp and tcp networking, with callbacks. o comm_timer - a timeout with callback. o comm_signal - callbacks when signal is caught. o comm_reply - holds reply info during networking callback.

Definition in file netevent.h.

Macro Definition Documentation

◆ NETEVENT_CAPSFAIL

#define NETEVENT_CAPSFAIL   -3

to pass fallback from capsforID to callback function; 0x20 failed

Definition at line 86 of file netevent.h.

◆ NETEVENT_CLOSED

#define NETEVENT_CLOSED   -1

to pass closed connection to callback function

Definition at line 82 of file netevent.h.

◆ NETEVENT_DONE

#define NETEVENT_DONE   -4

to pass done transfer to callback function; http file is complete

Definition at line 88 of file netevent.h.

◆ NETEVENT_NOERROR

#define NETEVENT_NOERROR   0

to pass no_error to callback function

Definition at line 80 of file netevent.h.

◆ NETEVENT_SLOW_ACCEPT_TIME

#define NETEVENT_SLOW_ACCEPT_TIME   2000

timeout to slow accept calls when not possible, in msec.

Definition at line 91 of file netevent.h.

◆ NETEVENT_TIMEOUT

#define NETEVENT_TIMEOUT   -2

to pass timeout happened to callback function

Definition at line 84 of file netevent.h.

Typedef Documentation

◆ comm_point_callback_type

typedef int comm_point_callback_type(struct comm_point *, void *, int, struct comm_reply *)

callback from communication point function type

Definition at line 76 of file netevent.h.

Function Documentation

◆ comm_base_create()

struct comm_base* comm_base_create ( int  sigs)

Create a new comm base.

Parameters
sigsif true it attempts to create a default loop for signal handling.
Returns
: the new comm base. NULL on error.

◆ comm_base_create_event()

struct comm_base* comm_base_create_event ( struct ub_event_base base)

Create comm base that uses the given ub_event_base (underlying pluggable event mechanism pointer).

Parameters
baseunderlying pluggable event base.
Returns
: the new comm base. NULL on error.

◆ comm_base_delete()

void comm_base_delete ( struct comm_base b)

Destroy a comm base. All comm points must have been deleted.

Parameters
bthe base to delete.

◆ comm_base_delete_no_base()

void comm_base_delete_no_base ( struct comm_base b)

Delete comm base structure but not the underlying lib event base. All comm points must have been deleted.

Parameters
bthe base to delete.

◆ comm_base_dispatch()

void comm_base_dispatch ( struct comm_base b)

Dispatch the comm base events.

Parameters
bthe communication to perform.

◆ comm_base_exit()

void comm_base_exit ( struct comm_base b)

Exit from dispatch loop.

Parameters
bthe communication base that is in dispatch().

◆ comm_base_handle_slow_accept()

void comm_base_handle_slow_accept ( int  fd,
short  event,
void *  arg 
)

This routine is published for checks and tests, and is only used internally. libevent callback for timeout on slow accept.

Parameters
fdfile descriptor.
eventevent bits from libevent: EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
argthe comm_point structure.

◆ comm_base_internal()

struct ub_event_base* comm_base_internal ( struct comm_base b)

Access internal data structure (for util/tube.c on windows)

Parameters
bcomm base
Returns
ub_event_base.

◆ comm_base_set_slow_accept_handlers()

void comm_base_set_slow_accept_handlers ( struct comm_base b,
void(*)(void *)  stop_accept,
void(*)(void *)  start_accept,
void *  arg 
)

Set the slow_accept mode handlers. You can not provide these if you do not perform accept() calls.

Parameters
bcomm base
stop_acceptfunction that stops listening to accept fds.
start_acceptfunction that resumes listening to accept fds.
argcallback arg to pass to the functions.

◆ comm_base_timept()

void comm_base_timept ( struct comm_base b,
time_t **  tt,
struct timeval **  tv 
)

Obtain two pointers. The pointers never change (until base_delete()). The pointers point to time values that are updated regularly.

Parameters
bthe communication base that will update the time values.
ttpointer to time in seconds is returned.
tvpointer to time in microseconds is returned.

◆ comm_point_close()

void comm_point_close ( struct comm_point c)

Close a comm point fd.

Parameters
ccomm point to close.

◆ comm_point_create_http_out()

struct comm_point* comm_point_create_http_out ( struct comm_base base,
size_t  bufsize,
comm_point_callback_type callback,
void *  callback_arg,
struct sldns_buffer temp 
)

Create an outgoing HTTP commpoint. No file descriptor is opened, left at -1.

Parameters
basein which base to alloc the commpoint.
bufsizesize of buffer to create for handlers.
callbackcallback function pointer for the handler.
callback_argwill be passed to your callback function.
tempsldns buffer, shared between other http_out commpoints, for temporary data when performing callbacks.
Returns
: the commpoint or NULL on error.

◆ comm_point_create_local()

struct comm_point* comm_point_create_local ( struct comm_base base,
int  fd,
size_t  bufsize,
comm_point_callback_type callback,
void *  callback_arg 
)

Create commpoint to listen to a local domain file descriptor.

Parameters
basein which base to alloc the commpoint.
fdfile descriptor of open AF_UNIX socket set to listen nonblocking.
bufsizesize of buffer to create for handlers.
callbackcallback function pointer for the handler.
callback_argwill be passed to your callback function.
Returns
: the commpoint or NULL on error.

◆ comm_point_create_raw()

struct comm_point* comm_point_create_raw ( struct comm_base base,
int  fd,
int  writing,
comm_point_callback_type callback,
void *  callback_arg 
)

Create commpoint to listen to a local domain pipe descriptor.

Parameters
basein which base to alloc the commpoint.
fdfile descriptor.
writingtrue if you want to listen to writes, false for reads.
callbackcallback function pointer for the handler.
callback_argwill be passed to your callback function.
Returns
: the commpoint or NULL on error.

◆ comm_point_create_tcp()

struct comm_point* comm_point_create_tcp ( struct comm_base base,
int  fd,
int  num,
size_t  bufsize,
comm_point_callback_type callback,
void *  callback_arg 
)

Create a TCP listener comm point. Calls malloc. Setups the structure with the parameters you provide. Also Creates TCP Handlers, pre allocated for you. Uses the parameters you provide.

Parameters
basein which base to alloc the commpoint.
fdfile descriptor of open TCP socket set to listen nonblocking.
numbecomes max_tcp_count, the routine allocates that many tcp handler commpoints.
bufsizesize of buffer to create for handlers.
callbackcallback function pointer for TCP handlers.
callback_argwill be passed to your callback function.
Returns
: returns the TCP listener commpoint. You can find the TCP handlers in the array inside the listener commpoint. returns NULL on error. Inits timeout to NULL. All handlers are on the free list.

◆ comm_point_create_tcp_out()

struct comm_point* comm_point_create_tcp_out ( struct comm_base base,
size_t  bufsize,
comm_point_callback_type callback,
void *  callback_arg 
)

Create an outgoing TCP commpoint. No file descriptor is opened, left at -1.

Parameters
basein which base to alloc the commpoint.
bufsizesize of buffer to create for handlers.
callbackcallback function pointer for the handler.
callback_argwill be passed to your callback function.
Returns
: the commpoint or NULL on error.

◆ comm_point_create_udp()

struct comm_point* comm_point_create_udp ( struct comm_base base,
int  fd,
struct sldns_buffer buffer,
comm_point_callback_type callback,
void *  callback_arg 
)

Create an UDP comm point. Calls malloc. setups the structure with the parameters you provide.

Parameters
basein which base to alloc the commpoint.
fd: file descriptor of open UDP socket.
buffershared buffer by UDP sockets from this thread.
callbackcallback function pointer.
callback_argwill be passed to your callback function.
Returns
: returns the allocated communication point. NULL on error. Sets timeout to NULL. Turns off TCP options.

◆ comm_point_create_udp_ancil()

struct comm_point* comm_point_create_udp_ancil ( struct comm_base base,
int  fd,
struct sldns_buffer buffer,
comm_point_callback_type callback,
void *  callback_arg 
)

Create an UDP with ancillary data comm point. Calls malloc. Uses recvmsg instead of recv to get udp message. setups the structure with the parameters you provide.

Parameters
basein which base to alloc the commpoint.
fd: file descriptor of open UDP socket.
buffershared buffer by UDP sockets from this thread.
callbackcallback function pointer.
callback_argwill be passed to your callback function.
Returns
: returns the allocated communication point. NULL on error. Sets timeout to NULL. Turns off TCP options.

◆ comm_point_delete()

void comm_point_delete ( struct comm_point c)

Close and deallocate (free) the comm point. If the comm point is a tcp-accept point, also its tcp-handler points are deleted.

Parameters
ccomm point to delete.

◆ comm_point_drop_reply()

void comm_point_drop_reply ( struct comm_reply repinfo)

Drop reply. Cleans up.

Parameters
repinfoThe reply info copied from a commpoint callback call.

◆ comm_point_get_mem()

size_t comm_point_get_mem ( struct comm_point c)

Get size of memory used by comm point. For TCP handlers this includes subhandlers. For UDP handlers, this does not include the (shared) UDP buffer.

Parameters
ccommpoint.
Returns
size in bytes.

◆ comm_point_http_handle_callback()

void comm_point_http_handle_callback ( int  fd,
short  event,
void *  arg 
)

This routine is published for checks and tests, and is only used internally. handle libevent callback for tcp data comm point

Parameters
fdfile descriptor.
eventevent bits from libevent: EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
argthe comm_point structure.

◆ comm_point_listen_for_rw()

void comm_point_listen_for_rw ( struct comm_point c,
int  rd,
int  wr 
)

Stop listening and start listening again for reading or writing.

Parameters
ccommpoint
rdif true, listens for reading.
wrif true, listens for writing.

◆ comm_point_local_handle_callback()

void comm_point_local_handle_callback ( int  fd,
short  event,
void *  arg 
)

This routine is published for checks and tests, and is only used internally. libevent callback for AF_UNIX fds

Parameters
fdfile descriptor.
eventevent bits from libevent: EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
argthe comm_point structure.

◆ comm_point_perform_accept()

int comm_point_perform_accept ( struct comm_point c,
struct sockaddr_storage addr,
socklen_t *  addrlen 
)

perform accept(2) with error checking.

Parameters
ccommpoint with accept fd.
addrremote end returned here.
addrlenlength of remote end returned here.
Returns
new fd, or -1 on error. if -1, error message has been printed if necessary, simply drop out of the reading handler.

◆ comm_point_raw_handle_callback()

void comm_point_raw_handle_callback ( int  fd,
short  event,
void *  arg 
)

This routine is published for checks and tests, and is only used internally. libevent callback for raw fd access.

Parameters
fdfile descriptor.
eventevent bits from libevent: EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
argthe comm_point structure.

◆ comm_point_send_reply()

void comm_point_send_reply ( struct comm_reply repinfo)

Send reply. Put message into commpoint buffer.

Parameters
repinfoThe reply info copied from a commpoint callback call.

◆ comm_point_send_udp_msg()

int comm_point_send_udp_msg ( struct comm_point c,
struct sldns_buffer packet,
struct sockaddr *  addr,
socklen_t  addrlen 
)

Send an udp message over a commpoint.

Parameters
ccommpoint to send it from.
packetwhat to send.
addrwhere to send it to.
addrlenlength of addr.
Returns
: false on a failure.

◆ comm_point_start_listening()

void comm_point_start_listening ( struct comm_point c,
int  newfd,
int  msec 
)

Start listening again for input on the comm point.

Parameters
ccommpoint to enable again.
newfdnew fd, or -1 to leave fd be.
msectimeout in milliseconds, or -1 for no (change to the) timeout. So seconds*1000.

◆ comm_point_stop_listening()

void comm_point_stop_listening ( struct comm_point c)

Stop listening for input on the commpoint. No callbacks will happen.

Parameters
ccommpoint to disable. The fd is not closed.

◆ comm_point_tcp_accept_callback()

void comm_point_tcp_accept_callback ( int  fd,
short  event,
void *  arg 
)

This routine is published for checks and tests, and is only used internally. handle libevent callback for tcp accept comm point

Parameters
fdfile descriptor.
eventevent bits from libevent: EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
argthe comm_point structure.

◆ comm_point_tcp_handle_callback()

void comm_point_tcp_handle_callback ( int  fd,
short  event,
void *  arg 
)

This routine is published for checks and tests, and is only used internally. handle libevent callback for tcp data comm point

Parameters
fdfile descriptor.
eventevent bits from libevent: EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
argthe comm_point structure.

◆ comm_point_udp_ancil_callback()

void comm_point_udp_ancil_callback ( int  fd,
short  event,
void *  arg 
)

This routine is published for checks and tests, and is only used internally. handle libevent callback for udp ancillary data comm point.

Parameters
fdfile descriptor.
eventevent bits from libevent: EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
argthe comm_point structure.

◆ comm_point_udp_callback()

void comm_point_udp_callback ( int  fd,
short  event,
void *  arg 
)

This routine is published for checks and tests, and is only used internally. handle libevent callback for udp comm point.

Parameters
fdfile descriptor.
eventevent bits from libevent: EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
argthe comm_point structure.

◆ comm_signal_bind()

int comm_signal_bind ( struct comm_signal comsig,
int  sig 
)

Bind signal struct to catch a signal. A signle comm_signal can be bound to multiple signals, calling comm_signal_bind multiple times.

Parameters
comsigthe communication point, with callback information.
sigsignal number.
Returns
: true on success. false on error.

◆ comm_signal_callback()

void comm_signal_callback ( int  fd,
short  event,
void *  arg 
)

This routine is published for checks and tests, and is only used internally. handle libevent callback for signal comm.

Parameters
fdfile descriptor (used for the signal number).
eventevent bits from libevent: EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
argthe internal commsignal structure.

◆ comm_signal_create()

struct comm_signal* comm_signal_create ( struct comm_base base,
void(*)(int, void *)  callback,
void *  cb_arg 
)

Create a signal handler. Call signal_bind() later to bind to a signal.

Parameters
basecommunication base to use.
callbackcalled when signal is caught.
cb_arguser argument to callback
Returns
: the signal struct or NULL on error.

◆ comm_signal_delete()

void comm_signal_delete ( struct comm_signal comsig)

Delete the signal communication point.

Parameters
comsigto delete.

◆ comm_timer_callback()

void comm_timer_callback ( int  fd,
short  event,
void *  arg 
)

This routine is published for checks and tests, and is only used internally. handle libevent callback for timer comm.

Parameters
fdfile descriptor (always -1).
eventevent bits from libevent: EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
argthe comm_timer structure.

◆ comm_timer_create()

struct comm_timer* comm_timer_create ( struct comm_base base,
void(*)(void *)  cb,
void *  cb_arg 
)

create timer. Not active upon creation.

Parameters
baseevent handling base.
cbcallback function: void myfunc(void* myarg);
cb_arguser callback argument.
Returns
: the new timer or NULL on error.

◆ comm_timer_delete()

void comm_timer_delete ( struct comm_timer timer)

delete timer.

Parameters
timerto delete.

◆ comm_timer_disable()

void comm_timer_disable ( struct comm_timer timer)

disable timer. Stops callbacks from happening.

Parameters
timerto disable.

◆ comm_timer_get_mem()

size_t comm_timer_get_mem ( struct comm_timer timer)

Get size of memory used by comm timer.

Parameters
timerthe timer to examine.
Returns
size in bytes.

◆ comm_timer_is_set()

int comm_timer_is_set ( struct comm_timer timer)

see if timeout has been set to a value.

Parameters
timerthe timer to examine.
Returns
: false if disabled or not set.

◆ comm_timer_set()

void comm_timer_set ( struct comm_timer timer,
struct timeval *  tv 
)

reset timevalue for timer.

Parameters
timertimer to (re)set.
tvwhen the timer should activate. if NULL timer is disabled.

◆ tcp_connect_errno_needs_log()

int tcp_connect_errno_needs_log ( struct sockaddr *  addr,
socklen_t  addrlen 
)

see if errno for tcp connect has to be logged or not. This uses errno