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-2018 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 EVENT_UNDEF 4
36 #define EVENT_READ (1<<0)
37 #define EVENT_WRITE (1<<1)
38 /*
39  * Initialization flags passed to event_set_init
40  */
41 #define EVENT_METHOD_US_TIMEOUT (1<<0)
42 #define EVENT_METHOD_FAST (1<<1)
43 
44 #ifdef _WIN32
45 
46 typedef const struct rw_handle *event_t;
47 
48 #define UNDEFINED_EVENT (NULL)
49 
50 #else /* ifdef _WIN32 */
51 
52 typedef int event_t;
53 
54 #define UNDEFINED_EVENT (-1)
55 
56 #endif
57 
58 struct event_set;
59 struct event_set_return;
60 
62 {
63  void (*free)(struct event_set *es);
64  void (*reset)(struct event_set *es);
65  void (*del)(struct event_set *es, event_t event);
66  void (*ctl)(struct event_set *es, event_t event, unsigned int rwflags, void *arg);
67 
68  /*
69  * Return status for wait:
70  * -1 on signal or error
71  * 0 on timeout
72  * length of event_set_return if at least 1 event is returned
73  */
74  int (*wait)(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen);
75 };
76 
78 {
79  unsigned int rwflags;
80  void *arg;
81 };
82 
83 struct event_set
84 {
85  struct event_set_functions func;
86 };
87 
88 /*
89  * maxevents on input: desired max number of event_t descriptors
90  * simultaneously set with event_ctl
91  * maxevents on output: may be modified down, depending on limitations
92  * of underlying API
93  * flags: EVENT_METHOD_x flags
94  */
95 struct event_set *event_set_init(int *maxevents, unsigned int flags);
96 
97 static inline void
98 event_free(struct event_set *es)
99 {
100  if (es)
101  {
102  (*es->func.free)(es);
103  }
104 }
105 
106 static inline void
108 {
109  (*es->func.reset)(es);
110 }
111 
112 static inline void
113 event_del(struct event_set *es, event_t event)
114 {
115  (*es->func.del)(es, event);
116 }
117 
118 static inline void
119 event_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
120 {
121  (*es->func.ctl)(es, event, rwflags, arg);
122 }
123 
124 static inline int
125 event_wait(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
126 {
127  int ret;
129  ret = (*es->func.wait)(es, tv, out, outlen);
130  perf_pop();
131  return ret;
132 }
133 
134 static inline void
136 {
137  esr->rwflags = 0;
138  esr->arg = NULL;
139 }
140 
141 #ifdef _WIN32
142 
143 static inline void
144 wait_signal(struct event_set *es, void *arg)
145 {
147  {
148  event_ctl(es, &win32_signal.in, EVENT_READ, arg);
149  }
150 }
151 
152 #else /* ifdef _WIN32 */
153 
154 static inline void
155 wait_signal(struct event_set *es, void *arg)
156 {
157 }
158 
159 #endif
160 
161 #endif /* ifndef EVENT_H */
struct event_set_functions func
Definition: event.h:85
void(* free)(struct event_set *es)
Definition: event.h:63
const struct rw_handle * event_t
Definition: event.h:46
HANDLE read
Definition: win32.h:73
void(* del)(struct event_set *es, event_t event)
Definition: event.h:65
int(* wait)(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
Definition: event.h:74
void(* reset)(struct event_set *es)
Definition: event.h:64
static void perf_pop(void)
Definition: perf.h:82
static void perf_push(int type)
Definition: perf.h:78
static int event_wait(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
Definition: event.h:125
void * arg
Definition: event.h:80
list flags
static void event_set_return_init(struct event_set_return *esr)
Definition: event.h:135
static void event_del(struct event_set *es, event_t event)
Definition: event.h:113
static void event_reset(struct event_set *es)
Definition: event.h:107
static void event_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
Definition: event.h:119
static void event_free(struct event_set *es)
Definition: event.h:98
struct rw_handle in
Definition: win32.h:151
#define EVENT_READ
Definition: event.h:36
#define PERF_IO_WAIT
Definition: perf.h:43
static void wait_signal(struct event_set *es, void *arg)
Definition: event.h:144
struct event_set * event_set_init(int *maxevents, unsigned int flags)
Definition: event.c:1180
unsigned int rwflags
Definition: event.h:79
void(* ctl)(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
Definition: event.h:66
#define HANDLE_DEFINED(h)
Definition: win32.h:61