OpenDNSSEC-signer  1.4.3
netio.h
Go to the documentation of this file.
1 /*
2  * $Id: netio.h 4958 2011-04-18 07:11:09Z matthijs $
3  *
4  * Copyright (c) 2001-2011, NLnet Labs. All rights reserved.
5  *
6  * See LICENSE for the license.
7  *
8  *
9  * The netio module implements event based I/O handling using
10  * pselect(2). Multiple event handlers can wait for a certain event
11  * to occur simultaneously. Each event handler is called when an
12  * event occurs that the event handler has indicated that it is
13  * willing to handle.
14  *
15  * There are four types of events that can be handled:
16  *
17  * NETIO_EVENT_READ: reading will not block.
18  * NETIO_EVENT_WRITE: writing will not block.
19  * NETIO_EVENT_EXCEPT: an exception occurred.
20  * NETIO_EVENT_TIMEOUT: the timeout expired.
21  *
22  * A file descriptor must be specified if the handler is interested in
23  * the first three event types. A timeout must be specified if the
24  * event handler is interested in timeouts. These event types can be
25  * OR'ed together if the handler is willing to handle multiple types
26  * of events.
27  *
28  * The special event type NETIO_EVENT_NONE is available if you wish to
29  * temporarily disable the event handler without removing and adding
30  * the handler to the netio structure.
31  *
32  * The event callbacks are free to modify the netio_handler_type
33  * structure to change the file descriptor, timeout, event types, user
34  * data, or handler functions.
35  *
36  * The main loop of the program must call netio_dispatch to check for
37  * events and dispatch them to the handlers. An additional timeout
38  * can be specified as well as the signal mask to install while
39  * blocked in pselect(2).
40  */
41 
47 #ifndef WIRE_NETIO_H_
48 #define WIRE_NETIO_H_
49 
50 #ifdef HAVE_SYS_SELECT_H
51 #include <sys/select.h>
52 #endif
53 
54 #include <signal.h>
55 
56 #include "config.h"
57 #include "shared/allocator.h"
58 
59 #ifndef PF_INET
60 #define PF_INET AF_INET
61 #endif
62 #ifndef PF_INET6
63 #define PF_INET6 AF_INET6
64 #endif
65 
66 /*
67  * The type of events a handler is interested in.
68  * These can be OR'ed together to specify multiple event types.
69  *
70  */
77 };
79 
80 typedef struct netio_struct netio_type;
83 
88 typedef void (*netio_event_handler_type)(netio_type *netio,
90 
98 };
99 
105  /*
106  * The file descriptor that should be checked for events. If
107  * the file descriptor is negative only timeout events are
108  * checked for.
109  */
110  int fd;
111  /*
112  * The time when no events should be checked for and the
113  * handler should be called with the NETIO_EVENT_TIMEOUT
114  * event type. Unlike most timeout parameters the time should
115  * be absolute, not relative!
116  */
117  struct timespec* timeout;
118  /*
119  * User data.
120  */
121  void* user_data;
122  /*
123  * The type of events that should be checked for. These types
124  * can be OR'ed together to wait for multiple types of events.
125  */
127  /*
128  * The event handler. The event_types parameter contains the
129  * OR'ed set of event types that actually triggered. The
130  * event handler is allowed to modify this handler object.
131  * The event handler SHOULD NOT block.
132  */
134 };
135 
140 struct netio_struct {
144  /*
145  * Cached value of the current time. The cached value is
146  * cleared at the start of netio_dispatch to calculate the
147  * relative timeouts of the event handlers and after calling
148  * pselect(2) so handlers can use it to calculate a new
149  * absolute timeout.
150  *
151  * Use netio_current_time() to read the current time.
152  */
154  struct timespec cached_current_time;
155  /*
156  * Next handler in the dispatch. Only valid during callbacks.
157  * To make sure that deletes respect the state of the iterator.
158  */
160 };
161 
162 /*
163  * Create a new netio instance.
164  * \param[in] allocator memory allocator
165  * \return netio_type* netio instance
166  *
167  */
169 
170 /*
171  * Add a new handler to netio.
172  * \param[in] netio netio instance
173  * \param[in] handler handler
174  *
175  */
176 void netio_add_handler(netio_type* netio, netio_handler_type* handler);
177 
178 /*
179  * Remove the handler from netio.
180  * \param[in] netio netio instance
181  * \param[in] handler handler
182  *
183  */
184 void netio_remove_handler(netio_type* netio, netio_handler_type* handler);
185 
186 /*
187  * Retrieve the current time (using gettimeofday(2)).
188  * \param[in] netio netio instance
189  * \return const struct timespec* current time
190  *
191  */
192 const struct timespec* netio_current_time(netio_type* netio);
193 
194 /*
195  * Check for events and dispatch them to the handlers.
196  * \param[in] netio netio instance
197  * \param[in] timeout if specified, the maximum time to wait for an
198  * event to arrive.
199  * \param[in] sigmask is passed to the underlying pselect(2) call
200  * \return int the number of non-timeout events dispatched, 0 on timeout,
201  * and -1 on error (with errno set appropriately).
202  *
203  */
204 int netio_dispatch(netio_type* netio, const struct timespec* timeout,
205  const sigset_t* sigmask);
206 
212 void netio_cleanup(netio_type* netio);
213 
220 void timespec_add(struct timespec* left, const struct timespec* right);
221 
222 
223 #ifdef __cplusplus
224 inline netio_events_type
225 operator | (netio_events_type lhs, netio_events_type rhs) {
226  return (netio_events_type) (lhs | rhs);
227 }
228 inline netio_events_type
229 operator |= (netio_events_type &lhs, netio_events_type rhs) {
230  lhs = (netio_events_type) (lhs | rhs);
231  return lhs;
232 }
233 #endif /* __cplusplus */
234 
235 #endif /* WIRE_NETIO_H_ */
void netio_remove_handler(netio_type *netio, netio_handler_type *handler)
Definition: netio.c:86
netio_handler_list_type * handlers
Definition: netio.h:142
netio_handler_type * handler
Definition: netio.h:97
enum netio_events_enum netio_events_type
Definition: netio.h:78
netio_type * netio_create(allocator_type *allocator)
Definition: netio.c:41
int have_current_time
Definition: netio.h:153
netio_handler_list_type * next
Definition: netio.h:96
const struct timespec * netio_current_time(netio_type *netio)
Definition: netio.c:181
void * user_data
Definition: netio.h:121
struct timespec cached_current_time
Definition: netio.h:154
void netio_cleanup(netio_type *netio)
Definition: netio.c:354
allocator_type * allocator
Definition: netio.h:141
netio_event_handler_type event_handler
Definition: netio.h:133
void netio_add_handler(netio_type *netio, netio_handler_type *handler)
Definition: netio.c:60
netio_events_enum
Definition: netio.h:71
netio_handler_list_type * deallocated
Definition: netio.h:143
void(* netio_event_handler_type)(netio_type *netio, netio_handler_type *handler, netio_events_type event_types)
Definition: netio.h:88
netio_events_type event_types
Definition: netio.h:126
void timespec_add(struct timespec *left, const struct timespec *right)
Definition: netio.c:147
int netio_dispatch(netio_type *netio, const struct timespec *timeout, const sigset_t *sigmask)
Definition: netio.c:205
struct timespec * timeout
Definition: netio.h:117
netio_handler_list_type * dispatch_next
Definition: netio.h:159