src/openvpn/event.h
6fbf66fa
 /*
  *  OpenVPN -- An application to securely tunnel IP networks
  *             over a single TCP/UDP port, with support for SSL/TLS-based
  *             session authentication and key exchange,
  *             packet encryption, packet authentication, and
  *             packet compression.
  *
49979459
  *  Copyright (C) 2002-2018 OpenVPN Inc <sales@openvpn.net>
6fbf66fa
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License version 2
  *  as published by the Free Software Foundation.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
caa54ac3
  *  You should have received a copy of the GNU General Public License along
  *  with this program; if not, write to the Free Software Foundation, Inc.,
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
6fbf66fa
  */
 
 #ifndef EVENT_H
 #define EVENT_H
 
 #include "win32.h"
 #include "sig.h"
 #include "perf.h"
 
 /*
  * rwflags passed to event_ctl and returned by
  * struct event_set_return.
  */
6add6b2f
 #define EVENT_UNDEF    4
6fbf66fa
 #define EVENT_READ     (1<<0)
 #define EVENT_WRITE    (1<<1)
 /*
  * Initialization flags passed to event_set_init
  */
 #define EVENT_METHOD_US_TIMEOUT   (1<<0)
 #define EVENT_METHOD_FAST         (1<<1)
 
445b192a
 #ifdef _WIN32
6fbf66fa
 
 typedef const struct rw_handle *event_t;
 
 #define UNDEFINED_EVENT (NULL)
 
81d882d5
 #else  /* ifdef _WIN32 */
6fbf66fa
 
 typedef int event_t;
 
 #define UNDEFINED_EVENT (-1)
 
 #endif
 
 struct event_set;
 struct event_set_return;
 
 struct event_set_functions
 {
81d882d5
     void (*free)(struct event_set *es);
     void (*reset)(struct event_set *es);
     void (*del)(struct event_set *es, event_t event);
     void (*ctl)(struct event_set *es, event_t event, unsigned int rwflags, void *arg);
 
     /*
      * Return status for wait:
      * -1 on signal or error
      * 0 on timeout
      * length of event_set_return if at least 1 event is returned
      */
     int (*wait)(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen);
6fbf66fa
 };
 
 struct event_set_return
 {
81d882d5
     unsigned int rwflags;
     void *arg;
6fbf66fa
 };
 
 struct event_set
 {
81d882d5
     struct event_set_functions func;
6fbf66fa
 };
 
 /*
  * maxevents on input:  desired max number of event_t descriptors
  *                      simultaneously set with event_ctl
  * maxevents on output: may be modified down, depending on limitations
  *                      of underlying API
  * flags:               EVENT_METHOD_x flags
  */
81d882d5
 struct event_set *event_set_init(int *maxevents, unsigned int flags);
6fbf66fa
 
 static inline void
81d882d5
 event_free(struct event_set *es)
6fbf66fa
 {
81d882d5
     if (es)
     {
         (*es->func.free)(es);
     }
6fbf66fa
 }
 
 static inline void
81d882d5
 event_reset(struct event_set *es)
6fbf66fa
 {
81d882d5
     (*es->func.reset)(es);
6fbf66fa
 }
 
 static inline void
81d882d5
 event_del(struct event_set *es, event_t event)
6fbf66fa
 {
81d882d5
     (*es->func.del)(es, event);
6fbf66fa
 }
 
 static inline void
81d882d5
 event_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
6fbf66fa
 {
81d882d5
     (*es->func.ctl)(es, event, rwflags, arg);
6fbf66fa
 }
 
 static inline int
81d882d5
 event_wait(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
6fbf66fa
 {
81d882d5
     int ret;
     perf_push(PERF_IO_WAIT);
     ret = (*es->func.wait)(es, tv, out, outlen);
     perf_pop();
     return ret;
6fbf66fa
 }
 
 static inline void
81d882d5
 event_set_return_init(struct event_set_return *esr)
6fbf66fa
 {
81d882d5
     esr->rwflags = 0;
     esr->arg = NULL;
6fbf66fa
 }
 
445b192a
 #ifdef _WIN32
6fbf66fa
 
 static inline void
81d882d5
 wait_signal(struct event_set *es, void *arg)
6fbf66fa
 {
81d882d5
     if (HANDLE_DEFINED(win32_signal.in.read))
     {
         event_ctl(es, &win32_signal.in, EVENT_READ, arg);
     }
6fbf66fa
 }
 
81d882d5
 #else  /* ifdef _WIN32 */
6fbf66fa
 
 static inline void
81d882d5
 wait_signal(struct event_set *es, void *arg)
6fbf66fa
 {
 }
 
 #endif
 
81d882d5
 #endif /* ifndef EVENT_H */