OpenVPN
event.h
Go to the documentation of this file.
1 /*
2  * OpenVPN -- An application to securely tunnel IP networks
3  * over a single TCP/UDP port, with support for SSL/TLS-based
4  * session authentication and key exchange,
5  * packet encryption, packet authentication, and
6  * packet compression.
7  *
8  * Copyright (C) 2002-2024 OpenVPN Inc <sales@openvpn.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2
12  * as published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #ifndef EVENT_H
25 #define EVENT_H
26 
27 #include "win32.h"
28 #include "sig.h"
29 #include "perf.h"
30 
31 /*
32  * rwflags passed to event_ctl and returned by
33  * struct event_set_return.
34  */
35 #define READ_SHIFT 0
36 #define WRITE_SHIFT 1
37 
38 #define EVENT_UNDEF 4
39 #define EVENT_READ (1 << READ_SHIFT)
40 #define EVENT_WRITE (1 << WRITE_SHIFT)
41 
42 /* event flags returned by io_wait.
43  *
44  * All these events are defined as bits in a bitfield.
45  * Each event 'type' owns two bits in the bitfield: one for the READ
46  * event and one for the WRITE event.
47  *
48  * For this reason, the specific event bit is calculated by adding
49  * the event type identifier (always a multiple of 2, as defined
50  * below) to 0 for READ and 1 for WRITE.
51  *
52  * E.g.
53  * MANAGEMENT_SHIFT = 6; <---- event type identifier
54  * MANAGEMENT_READ = (1 << (6 + 0)), <---- READ event
55  * MANAGEMENT_WRITE = (1 << (6 + 1)) <---- WRITE event
56  *
57  * 'error' and 'file_close' are special and use read/write for different
58  * signals.
59  */
60 
61 #define SOCKET_SHIFT 0
62 #define SOCKET_READ (1 << (SOCKET_SHIFT + READ_SHIFT))
63 #define SOCKET_WRITE (1 << (SOCKET_SHIFT + WRITE_SHIFT))
64 #define TUN_SHIFT 2
65 #define TUN_READ (1 << (TUN_SHIFT + READ_SHIFT))
66 #define TUN_WRITE (1 << (TUN_SHIFT + WRITE_SHIFT))
67 #define ERR_SHIFT 4
68 #define ES_ERROR (1 << (ERR_SHIFT + READ_SHIFT))
69 #define ES_TIMEOUT (1 << (ERR_SHIFT + WRITE_SHIFT))
70 #define MANAGEMENT_SHIFT 6
71 #define MANAGEMENT_READ (1 << (MANAGEMENT_SHIFT + READ_SHIFT))
72 #define MANAGEMENT_WRITE (1 << (MANAGEMENT_SHIFT + WRITE_SHIFT))
73 #define FILE_SHIFT 8
74 #define FILE_CLOSED (1 << (FILE_SHIFT + READ_SHIFT))
75 #define DCO_SHIFT 10
76 #define DCO_READ (1 << (DCO_SHIFT + READ_SHIFT))
77 #define DCO_WRITE (1 << (DCO_SHIFT + WRITE_SHIFT))
78 
79 /*
80  * Initialization flags passed to event_set_init
81  */
82 #define EVENT_METHOD_US_TIMEOUT (1<<0)
83 #define EVENT_METHOD_FAST (1<<1)
84 
85 /*
86  * The following constant is used as boundary between integer value
87  * and real addresses when passing arguments to event handlers as (void *)
88  */
89 #define MULTI_N ((void *)16) /* upper bound on MTCP_x */
90 
91 #ifdef _WIN32
92 
93 typedef const struct rw_handle *event_t;
94 
95 #define UNDEFINED_EVENT (NULL)
96 
97 #else /* ifdef _WIN32 */
98 
99 typedef int event_t;
100 
101 #define UNDEFINED_EVENT (-1)
102 
103 #endif
104 
105 struct event_set;
106 struct event_set_return;
107 
109 {
110  void (*free)(struct event_set *es);
111  void (*reset)(struct event_set *es);
112  void (*del)(struct event_set *es, event_t event);
113  void (*ctl)(struct event_set *es, event_t event, unsigned int rwflags, void *arg);
114 
115  /*
116  * Return status for wait:
117  * -1 on signal or error
118  * 0 on timeout
119  * length of event_set_return if at least 1 event is returned
120  */
121  int (*wait)(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen);
122 };
123 
125 {
126  unsigned int rwflags;
127  void *arg;
128 };
129 
130 struct event_set
131 {
133 };
134 
135 typedef enum {
138 } event_arg_t;
139 
140 /* generic event argument object to pass to event_ctl() */
141 struct event_arg
142 {
144  union {
145  struct multi_instance *mi; /* if type = EVENT_ARG_MULTI_INSTANCE */
146  struct link_socket *sock; /* if type = EVENT_ARG_LINK_SOCKET */
147  } u;
148 };
149 
150 /*
151  * maxevents on input: desired max number of event_t descriptors
152  * simultaneously set with event_ctl
153  * maxevents on output: may be modified down, depending on limitations
154  * of underlying API
155  * flags: EVENT_METHOD_x flags
156  */
157 struct event_set *event_set_init(int *maxevents, unsigned int flags);
158 
159 static inline void
161 {
162  if (es)
163  {
164  (*es->func.free)(es);
165  }
166 }
167 
168 static inline void
170 {
171  (*es->func.reset)(es);
172 }
173 
174 static inline void
175 event_del(struct event_set *es, event_t event)
176 {
177  (*es->func.del)(es, event);
178 }
179 
180 static inline void
181 event_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
182 {
183  (*es->func.ctl)(es, event, rwflags, arg);
184 }
185 
186 static inline int
187 event_wait(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
188 {
189  int ret;
191  ret = (*es->func.wait)(es, tv, out, outlen);
192  perf_pop();
193  return ret;
194 }
195 
196 static inline void
198 {
199  esr->rwflags = 0;
200  esr->arg = NULL;
201 }
202 
203 #ifdef _WIN32
204 
205 static inline void
206 wait_signal(struct event_set *es, void *arg)
207 {
209  {
211  }
212 }
213 
214 #else /* ifdef _WIN32 */
215 
216 static inline void
217 wait_signal(struct event_set *es, void *arg)
218 {
219 }
220 
221 #endif
222 
223 #endif /* ifndef EVENT_H */
event_set_functions::del
void(* del)(struct event_set *es, event_t event)
Definition: event.h:112
multi_instance
Server-mode state structure for one single VPN tunnel.
Definition: multi.h:103
rw_handle::read
HANDLE read
Definition: win32.h:80
event_set::func
struct event_set_functions func
Definition: event.h:132
win32_signal::in
struct rw_handle in
Definition: win32.h:158
win32.h
es
struct env_set * es
Definition: test_pkcs11.c:141
event_set_functions::ctl
void(* ctl)(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
Definition: event.h:113
EVENT_ARG_MULTI_INSTANCE
@ EVENT_ARG_MULTI_INSTANCE
Definition: event.h:136
event_arg::sock
struct link_socket * sock
Definition: event.h:146
HANDLE_DEFINED
#define HANDLE_DEFINED(h)
Definition: win32.h:68
event_set_functions::reset
void(* reset)(struct event_set *es)
Definition: event.h:111
event_t
const struct rw_handle * event_t
Definition: event.h:93
event_arg::u
union event_arg::@1 u
event_set_functions
Definition: event.h:108
EVENT_READ
#define EVENT_READ
Definition: event.h:39
event_set_functions::wait
int(* wait)(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
Definition: event.h:121
event_arg_t
event_arg_t
Definition: event.h:135
win32_signal
Definition: win32.h:153
event_ctl
static void event_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
Definition: event.h:181
event_del
static void event_del(struct event_set *es, event_t event)
Definition: event.h:175
event_set_init
struct event_set * event_set_init(int *maxevents, unsigned int flags)
Definition: event.c:1186
event_set_return::arg
void * arg
Definition: event.h:127
perf_pop
static void perf_pop(void)
Definition: perf.h:82
PERF_IO_WAIT
#define PERF_IO_WAIT
Definition: perf.h:43
event_reset
static void event_reset(struct event_set *es)
Definition: event.h:169
event_arg
Definition: event.h:141
event_arg::mi
struct multi_instance * mi
Definition: event.h:145
perf.h
event_free
static void event_free(struct event_set *es)
Definition: event.h:160
event_set
Definition: event.h:130
event_set_return
Definition: event.h:124
rw_handle
Definition: win32.h:79
wait_signal
static void wait_signal(struct event_set *es, void *arg)
Definition: event.h:206
EVENT_ARG_LINK_SOCKET
@ EVENT_ARG_LINK_SOCKET
Definition: event.h:137
sig.h
event_set_return::rwflags
unsigned int rwflags
Definition: event.h:126
event_set_return_init
static void event_set_return_init(struct event_set_return *esr)
Definition: event.h:197
event_arg::type
event_arg_t type
Definition: event.h:143
event_set_functions::free
void(* free)(struct event_set *es)
Definition: event.h:110
perf_push
static void perf_push(int type)
Definition: perf.h:78
event_wait
static int event_wait(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
Definition: event.h:187