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 #ifdef _WIN32
86 
87 typedef const struct rw_handle *event_t;
88 
89 #define UNDEFINED_EVENT (NULL)
90 
91 #else /* ifdef _WIN32 */
92 
93 typedef int event_t;
94 
95 #define UNDEFINED_EVENT (-1)
96 
97 #endif
98 
99 struct event_set;
100 struct event_set_return;
101 
103 {
104  void (*free)(struct event_set *es);
105  void (*reset)(struct event_set *es);
106  void (*del)(struct event_set *es, event_t event);
107  void (*ctl)(struct event_set *es, event_t event, unsigned int rwflags, void *arg);
108 
109  /*
110  * Return status for wait:
111  * -1 on signal or error
112  * 0 on timeout
113  * length of event_set_return if at least 1 event is returned
114  */
115  int (*wait)(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen);
116 };
117 
119 {
120  unsigned int rwflags;
121  void *arg;
122 };
123 
124 struct event_set
125 {
127 };
128 
129 /*
130  * maxevents on input: desired max number of event_t descriptors
131  * simultaneously set with event_ctl
132  * maxevents on output: may be modified down, depending on limitations
133  * of underlying API
134  * flags: EVENT_METHOD_x flags
135  */
136 struct event_set *event_set_init(int *maxevents, unsigned int flags);
137 
138 static inline void
140 {
141  if (es)
142  {
143  (*es->func.free)(es);
144  }
145 }
146 
147 static inline void
149 {
150  (*es->func.reset)(es);
151 }
152 
153 static inline void
154 event_del(struct event_set *es, event_t event)
155 {
156  (*es->func.del)(es, event);
157 }
158 
159 static inline void
160 event_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
161 {
162  (*es->func.ctl)(es, event, rwflags, arg);
163 }
164 
165 static inline int
166 event_wait(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
167 {
168  int ret;
170  ret = (*es->func.wait)(es, tv, out, outlen);
171  perf_pop();
172  return ret;
173 }
174 
175 static inline void
177 {
178  esr->rwflags = 0;
179  esr->arg = NULL;
180 }
181 
182 #ifdef _WIN32
183 
184 static inline void
185 wait_signal(struct event_set *es, void *arg)
186 {
188  {
190  }
191 }
192 
193 #else /* ifdef _WIN32 */
194 
195 static inline void
196 wait_signal(struct event_set *es, void *arg)
197 {
198 }
199 
200 #endif
201 
202 #endif /* ifndef EVENT_H */
event_set_functions::del
void(* del)(struct event_set *es, event_t event)
Definition: event.h:106
rw_handle::read
HANDLE read
Definition: win32.h:80
event_set::func
struct event_set_functions func
Definition: event.h:126
win32_signal::in
struct rw_handle in
Definition: win32.h:158
win32.h
es
struct env_set * es
Definition: test_pkcs11.c:133
event_set_functions::ctl
void(* ctl)(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
Definition: event.h:107
HANDLE_DEFINED
#define HANDLE_DEFINED(h)
Definition: win32.h:68
event_set_functions::reset
void(* reset)(struct event_set *es)
Definition: event.h:105
event_t
const struct rw_handle * event_t
Definition: event.h:87
event_set_functions
Definition: event.h:102
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:115
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:160
event_del
static void event_del(struct event_set *es, event_t event)
Definition: event.h:154
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:121
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:148
perf.h
event_free
static void event_free(struct event_set *es)
Definition: event.h:139
event_set
Definition: event.h:124
event_set_return
Definition: event.h:118
rw_handle
Definition: win32.h:79
wait_signal
static void wait_signal(struct event_set *es, void *arg)
Definition: event.h:185
sig.h
event_set_return::rwflags
unsigned int rwflags
Definition: event.h:120
event_set_return_init
static void event_set_return_init(struct event_set_return *esr)
Definition: event.h:176
event_set_functions::free
void(* free)(struct event_set *es)
Definition: event.h:104
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:166