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-2010 OpenVPN Technologies, 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
20 *  along with this program (see the file COPYING included with this
21 *  distribution); if not, write to the Free Software Foundation, Inc.,
22 *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25#ifndef EVENT_H
26#define EVENT_H
27
28#include "win32.h"
29#include "sig.h"
30#include "perf.h"
31
32/*
33 * rwflags passed to event_ctl and returned by
34 * struct event_set_return.
35 */
36#define EVENT_UNDEF    4
37#define EVENT_READ     (1<<0)
38#define EVENT_WRITE    (1<<1)
39/*
40 * Initialization flags passed to event_set_init
41 */
42#define EVENT_METHOD_US_TIMEOUT   (1<<0)
43#define EVENT_METHOD_FAST         (1<<1)
44
45#ifdef WIN32
46
47typedef const struct rw_handle *event_t;
48
49#define UNDEFINED_EVENT (NULL)
50
51#else
52
53typedef int event_t;
54
55#define UNDEFINED_EVENT (-1)
56
57#endif
58
59struct event_set;
60struct event_set_return;
61
62struct event_set_functions
63{
64  void (*free)(struct event_set *es);
65  void (*reset)(struct event_set *es);
66  void (*del)(struct event_set *es, event_t event);
67  void (*ctl)(struct event_set *es, event_t event, unsigned int rwflags, void *arg);
68
69  /*
70   * Return status for wait:
71   * -1 on signal or error
72   * 0 on timeout
73   * length of event_set_return if at least 1 event is returned
74   */
75  int  (*wait)(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen);
76};
77
78struct event_set_return
79{
80  unsigned int rwflags;
81  void *arg;
82};
83
84struct event_set
85{
86  struct event_set_functions func;
87};
88
89/*
90 * maxevents on input:  desired max number of event_t descriptors
91 *                      simultaneously set with event_ctl
92 * maxevents on output: may be modified down, depending on limitations
93 *                      of underlying API
94 * flags:               EVENT_METHOD_x flags
95 */
96struct event_set *event_set_init (int *maxevents, unsigned int flags);
97
98static inline void
99event_free (struct event_set *es)
100{
101  if (es)
102    (*es->func.free)(es);
103}
104
105static inline void
106event_reset (struct event_set *es)
107{
108  (*es->func.reset)(es);
109}
110
111static inline void
112event_del (struct event_set *es, event_t event)
113{
114  (*es->func.del)(es, event);
115}
116
117static inline void
118event_ctl (struct event_set *es, event_t event, unsigned int rwflags, void *arg)
119{
120  (*es->func.ctl)(es, event, rwflags, arg);
121}
122
123static inline int
124event_wait (struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
125{
126  int ret;
127  perf_push (PERF_IO_WAIT);
128  ret = (*es->func.wait)(es, tv, out, outlen);
129  perf_pop ();
130  return ret;
131}
132
133static inline void
134event_set_return_init (struct event_set_return *esr)
135{
136  esr->rwflags = 0;
137  esr->arg = NULL;
138}
139
140#ifdef WIN32
141
142static inline void
143wait_signal (struct event_set *es, void *arg)
144{
145  if (HANDLE_DEFINED (win32_signal.in.read))
146    event_ctl (es, &win32_signal.in, EVENT_READ, arg);
147}
148
149#else
150
151static inline void
152wait_signal (struct event_set *es, void *arg)
153{
154}
155
156#endif
157
158#endif
159