OpenVPN
event.c
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 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #elif defined(_MSC_VER)
27 #include "config-msvc.h"
28 #endif
29 
30 #include "syshead.h"
31 
32 #include "buffer.h"
33 #include "error.h"
34 #include "integer.h"
35 #include "event.h"
36 #include "fdmisc.h"
37 
38 #if EPOLL
39 #include <sys/epoll.h>
40 #endif
41 
42 #include "memdbg.h"
43 
44 /*
45  * Some OSes will prefer select() over poll()
46  * when both are available.
47  */
48 #if defined(TARGET_DARWIN)
49 #define SELECT_PREFERRED_OVER_POLL
50 #endif
51 
52 /*
53  * All non-windows OSes are assumed to have select()
54  */
55 #ifdef _WIN32
56 #define SELECT 0
57 #else
58 #define SELECT 1
59 #endif
60 
61 /*
62  * This should be set to the highest file descriptor
63  * which can be used in one of the FD_ macros.
64  */
65 #ifdef FD_SETSIZE
66 #define SELECT_MAX_FDS FD_SETSIZE
67 #else
68 #define SELECT_MAX_FDS 256
69 #endif
70 
71 static inline int
72 tv_to_ms_timeout(const struct timeval *tv)
73 {
74  if (tv->tv_sec == 0 && tv->tv_usec == 0)
75  {
76  return 0;
77  }
78  else
79  {
80  return max_int(tv->tv_sec * 1000 + (tv->tv_usec + 500) / 1000, 1);
81  }
82 }
83 
84 #ifdef _WIN32
85 
86 struct we_set
87 {
89  bool fast;
90  HANDLE *events;
92  int n_events;
93  int capacity;
94 };
95 
96 static inline void
97 we_set_event(struct we_set *wes, int i, event_t event, unsigned int rwflags, void *arg)
98 {
99  ASSERT(i >= 0 && i < wes->capacity);
100 
101  if (rwflags == EVENT_READ)
102  {
103  ASSERT(event->read != NULL);
104  wes->events[i] = event->read;
105  }
106  else if (rwflags == EVENT_WRITE)
107  {
108  ASSERT(event->write != NULL);
109  wes->events[i] = event->write;
110  }
111  else
112  {
113  msg(M_FATAL, "fatal error in we_set_events: rwflags=%d", rwflags);
114  }
115 
116  wes->esr[i].rwflags = rwflags;
117  wes->esr[i].arg = arg;
118 }
119 
120 static inline bool
121 we_append_event(struct we_set *wes, event_t event, unsigned int rwflags, void *arg)
122 {
123  if (rwflags & EVENT_WRITE)
124  {
125  if (wes->n_events < wes->capacity)
126  {
127  we_set_event(wes, wes->n_events, event, EVENT_WRITE, arg);
128  ++wes->n_events;
129  }
130  else
131  {
132  return false;
133  }
134  }
135  if (rwflags & EVENT_READ)
136  {
137  if (wes->n_events < wes->capacity)
138  {
139  we_set_event(wes, wes->n_events, event, EVENT_READ, arg);
140  ++wes->n_events;
141  }
142  else
143  {
144  return false;
145  }
146  }
147  return true;
148 }
149 
150 static void
151 we_del_event(struct we_set *wes, event_t event)
152 {
153  int i, j = 0;
154  const int len = wes->n_events;
155 
156  for (i = 0; i < len; ++i)
157  {
158  const HANDLE h = wes->events[i];
159  if (h == event->read || h == event->write)
160  {
161  --wes->n_events;
162  }
163  else
164  {
165  if (i != j)
166  {
167  wes->events[j] = wes->events[i];
168  wes->esr[j] = wes->esr[i];
169  }
170  ++j;
171  }
172  }
173 }
174 
175 static void
176 we_del_index(struct we_set *wes, int index)
177 {
178  int i;
179  ASSERT(index >= 0 && index < wes->n_events);
180  for (i = index; i < wes->n_events - 1; ++i)
181  {
182  wes->events[i] = wes->events[i+1];
183  wes->esr[i] = wes->esr[i+1];
184  }
185  --wes->n_events;
186 }
187 
188 static void
189 we_get_rw_indices(struct we_set *wes, event_t event, int *ri, int *wi)
190 {
191  int i;
192  *ri = *wi = -1;
193  for (i = 0; i < wes->n_events; ++i)
194  {
195  const HANDLE h = wes->events[i];
196  if (h == event->read)
197  {
198  ASSERT(*ri == -1);
199  *ri = i;
200  }
201  else if (h == event->write)
202  {
203  ASSERT(*wi == -1);
204  *wi = i;
205  }
206  }
207 }
208 
209 static void
210 we_free(struct event_set *es)
211 {
212  struct we_set *wes = (struct we_set *) es;
213  free(wes->events);
214  free(wes->esr);
215  free(wes);
216 }
217 
218 static void
219 we_reset(struct event_set *es)
220 {
221  struct we_set *wes = (struct we_set *) es;
222  ASSERT(wes->fast);
223  wes->n_events = 0;
224 }
225 
226 static void
227 we_del(struct event_set *es, event_t event)
228 {
229  struct we_set *wes = (struct we_set *) es;
230  ASSERT(!wes->fast);
231  we_del_event(wes, event);
232 }
233 
234 static void
235 we_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
236 {
237  struct we_set *wes = (struct we_set *) es;
238 
239  dmsg(D_EVENT_WAIT, "WE_CTL n=%d ev=%p rwflags=0x%04x arg=" ptr_format,
240  wes->n_events,
241  event,
242  rwflags,
243  (ptr_type)arg);
244 
245  if (wes->fast)
246  {
247  if (!we_append_event(wes, event, rwflags, arg))
248  {
249  goto err;
250  }
251  }
252  else
253  {
254  int ri, wi;
255  int one = -1;
256  int n = 0;
257 
258  we_get_rw_indices(wes, event, &ri, &wi);
259  if (wi >= 0)
260  {
261  one = wi;
262  ++n;
263  }
264  if (ri >= 0)
265  {
266  one = ri;
267  ++n;
268  }
269  switch (rwflags)
270  {
271  case 0:
272  switch (n)
273  {
274  case 0:
275  break;
276 
277  case 1:
278  we_del_index(wes, one);
279  break;
280 
281  case 2:
282  we_del_event(wes, event);
283  break;
284 
285  default:
286  ASSERT(0);
287  }
288  break;
289 
290  case EVENT_READ:
291  switch (n)
292  {
293  case 0:
294  if (!we_append_event(wes, event, EVENT_READ, arg))
295  {
296  goto err;
297  }
298  break;
299 
300  case 1:
301  we_set_event(wes, one, event, EVENT_READ, arg);
302  break;
303 
304  case 2:
305  we_del_index(wes, wi);
306  break;
307 
308  default:
309  ASSERT(0);
310  }
311  break;
312 
313  case EVENT_WRITE:
314  switch (n)
315  {
316  case 0:
317  if (!we_append_event(wes, event, EVENT_WRITE, arg))
318  {
319  goto err;
320  }
321  break;
322 
323  case 1:
324  we_set_event(wes, one, event, EVENT_WRITE, arg);
325  break;
326 
327  case 2:
328  we_del_index(wes, ri);
329  break;
330 
331  default:
332  ASSERT(0);
333  }
334  break;
335 
336  case EVENT_READ|EVENT_WRITE:
337  switch (n)
338  {
339  case 0:
340  if (!we_append_event(wes, event, EVENT_READ|EVENT_WRITE, arg))
341  {
342  goto err;
343  }
344  break;
345 
346  case 1:
347  if (ri == -1)
348  {
349  ASSERT(wi != -1);
350  if (!we_append_event(wes, event, EVENT_READ, arg))
351  {
352  goto err;
353  }
354  }
355  else if (wi == -1)
356  {
357  if (!we_append_event(wes, event, EVENT_WRITE, arg))
358  {
359  goto err;
360  }
361  }
362  else
363  {
364  ASSERT(0);
365  }
366  break;
367 
368  case 2:
369  break;
370 
371  default:
372  ASSERT(0);
373  }
374  break;
375 
376  default:
377  msg(M_FATAL, "fatal error in we_ctl: rwflags=%d", rwflags);
378  }
379  }
380  return;
381 
382 err:
383  msg(D_EVENT_ERRORS, "Error: Windows resource limit WSA_MAXIMUM_WAIT_EVENTS (%d) has been exceeded", WSA_MAXIMUM_WAIT_EVENTS);
384 }
385 
386 static int
387 we_wait(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
388 {
389  struct we_set *wes = (struct we_set *) es;
390  const int timeout = tv_to_ms_timeout(tv);
391  DWORD status;
392 
393  dmsg(D_EVENT_WAIT, "WE_WAIT enter n=%d to=%d", wes->n_events, timeout);
394 
395 #ifdef ENABLE_DEBUG
397  {
398  int i;
399  for (i = 0; i < wes->n_events; ++i)
400  {
401  dmsg(D_EVENT_WAIT, "[%d] ev=%p rwflags=0x%04x arg=" ptr_format,
402  i,
403  wes->events[i],
404  wes->esr[i].rwflags,
405  (ptr_type)wes->esr[i].arg);
406  }
407  }
408 #endif
409 
410  /*
411  * First poll our event list with 0 timeout
412  */
413  status = WSAWaitForMultipleEvents(
414  (DWORD) wes->n_events,
415  wes->events,
416  FALSE,
417  (DWORD) 0,
418  FALSE);
419 
420  /*
421  * If at least one event is already set, we must
422  * individually poll the whole list.
423  */
424  if (status >= WSA_WAIT_EVENT_0 && status < WSA_WAIT_EVENT_0 + (DWORD) wes->n_events)
425  {
426  int i;
427  int j = 0;
428  for (i = 0; i < wes->n_events; ++i)
429  {
430  if (j >= outlen)
431  {
432  break;
433  }
434  if (WaitForSingleObject(wes->events[i], 0) == WAIT_OBJECT_0)
435  {
436  *out = wes->esr[i];
437  dmsg(D_EVENT_WAIT, "WE_WAIT leave [%d,%d] rwflags=0x%04x arg=" ptr_format,
438  i, j, out->rwflags, (ptr_type)out->arg);
439  ++j;
440  ++out;
441  }
442  }
443  return j;
444  }
445  else
446  {
447  /*
448  * If caller specified timeout > 0, we know at this point
449  * that no events are set, so wait only for the first event
450  * (or timeout) and return at most one event_set_return object.
451  *
452  * If caller specified timeout == 0, the second call to
453  * WSAWaitForMultipleEvents would be redundant -- just
454  * return 0 indicating timeout.
455  */
456  if (timeout > 0)
457  {
458  status = WSAWaitForMultipleEvents(
459  (DWORD) wes->n_events,
460  wes->events,
461  FALSE,
462  (DWORD) timeout,
463  FALSE);
464  }
465 
466  if (outlen >= 1 && status >= WSA_WAIT_EVENT_0 && status < WSA_WAIT_EVENT_0 + (DWORD) wes->n_events)
467  {
468  *out = wes->esr[status - WSA_WAIT_EVENT_0];
469  dmsg(D_EVENT_WAIT, "WE_WAIT leave rwflags=0x%04x arg=" ptr_format,
470  out->rwflags, (ptr_type)out->arg);
471  return 1;
472  }
473  else if (status == WSA_WAIT_TIMEOUT)
474  {
475  return 0;
476  }
477  else
478  {
479  return -1;
480  }
481  }
482 }
483 
484 static struct event_set *
485 we_init(int *maxevents, unsigned int flags)
486 {
487  struct we_set *wes;
488 
489  dmsg(D_EVENT_WAIT, "WE_INIT maxevents=%d flags=0x%08x", *maxevents, flags);
490 
491  ALLOC_OBJ_CLEAR(wes, struct we_set);
492 
493  /* set dispatch functions */
494  wes->func.free = we_free;
495  wes->func.reset = we_reset;
496  wes->func.del = we_del;
497  wes->func.ctl = we_ctl;
498  wes->func.wait = we_wait;
499 
500  if (flags & EVENT_METHOD_FAST)
501  {
502  wes->fast = true;
503  }
504  wes->n_events = 0;
505 
506  /* Figure our event capacity */
507  ASSERT(*maxevents > 0);
508  wes->capacity = min_int(*maxevents * 2, WSA_MAXIMUM_WAIT_EVENTS);
509  *maxevents = min_int(*maxevents, WSA_MAXIMUM_WAIT_EVENTS);
510 
511  /* Allocate space for Win32 event handles */
512  ALLOC_ARRAY_CLEAR(wes->events, HANDLE, wes->capacity);
513 
514  /* Allocate space for event_set_return objects */
515  ALLOC_ARRAY_CLEAR(wes->esr, struct event_set_return, wes->capacity);
516 
517  dmsg(D_EVENT_WAIT, "WE_INIT maxevents=%d capacity=%d",
518  *maxevents, wes->capacity);
519 
520  return (struct event_set *) wes;
521 }
522 
523 #endif /* _WIN32 */
524 
525 #if EPOLL
526 
527 struct ep_set
528 {
529  struct event_set_functions func;
530  bool fast;
531  int epfd;
532  int maxevents;
533  struct epoll_event *events;
534 };
535 
536 static void
537 ep_free(struct event_set *es)
538 {
539  struct ep_set *eps = (struct ep_set *) es;
540  close(eps->epfd);
541  free(eps->events);
542  free(eps);
543 }
544 
545 static void
546 ep_reset(struct event_set *es)
547 {
548  const struct ep_set *eps = (struct ep_set *) es;
549  ASSERT(eps->fast);
550 }
551 
552 static void
553 ep_del(struct event_set *es, event_t event)
554 {
555  struct epoll_event ev;
556  struct ep_set *eps = (struct ep_set *) es;
557 
558  dmsg(D_EVENT_WAIT, "EP_DEL ev=%d", (int)event);
559 
560  ASSERT(!eps->fast);
561  CLEAR(ev);
562  if (epoll_ctl(eps->epfd, EPOLL_CTL_DEL, event, &ev) < 0)
563  {
564  msg(M_WARN|M_ERRNO, "EVENT: epoll_ctl EPOLL_CTL_DEL failed, sd=%d", (int)event);
565  }
566 }
567 
568 static void
569 ep_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
570 {
571  struct ep_set *eps = (struct ep_set *) es;
572  struct epoll_event ev;
573 
574  CLEAR(ev);
575 
576  ev.data.ptr = arg;
577  if (rwflags & EVENT_READ)
578  {
579  ev.events |= EPOLLIN;
580  }
581  if (rwflags & EVENT_WRITE)
582  {
583  ev.events |= EPOLLOUT;
584  }
585 
586  dmsg(D_EVENT_WAIT, "EP_CTL fd=%d rwflags=0x%04x ev=0x%08x arg=" ptr_format,
587  (int)event,
588  rwflags,
589  (unsigned int)ev.events,
590  (ptr_type)ev.data.ptr);
591 
592  if (epoll_ctl(eps->epfd, EPOLL_CTL_MOD, event, &ev) < 0)
593  {
594  if (errno == ENOENT)
595  {
596  if (epoll_ctl(eps->epfd, EPOLL_CTL_ADD, event, &ev) < 0)
597  {
598  msg(M_ERR, "EVENT: epoll_ctl EPOLL_CTL_ADD failed, sd=%d", (int)event);
599  }
600  }
601  else
602  {
603  msg(M_ERR, "EVENT: epoll_ctl EPOLL_CTL_MOD failed, sd=%d", (int)event);
604  }
605  }
606 }
607 
608 static int
609 ep_wait(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
610 {
611  struct ep_set *eps = (struct ep_set *) es;
612  int stat;
613 
614  if (outlen > eps->maxevents)
615  {
616  outlen = eps->maxevents;
617  }
618 
619  stat = epoll_wait(eps->epfd, eps->events, outlen, tv_to_ms_timeout(tv));
620  ASSERT(stat <= outlen);
621 
622  if (stat > 0)
623  {
624  int i;
625  const struct epoll_event *ev = eps->events;
626  struct event_set_return *esr = out;
627  for (i = 0; i < stat; ++i)
628  {
629  esr->rwflags = 0;
630  if (ev->events & (EPOLLIN|EPOLLPRI|EPOLLERR|EPOLLHUP))
631  {
632  esr->rwflags |= EVENT_READ;
633  }
634  if (ev->events & EPOLLOUT)
635  {
636  esr->rwflags |= EVENT_WRITE;
637  }
638  esr->arg = ev->data.ptr;
639  dmsg(D_EVENT_WAIT, "EP_WAIT[%d] rwflags=0x%04x ev=0x%08x arg=" ptr_format,
640  i, esr->rwflags, ev->events, (ptr_type)ev->data.ptr);
641  ++ev;
642  ++esr;
643  }
644  }
645  return stat;
646 }
647 
648 static struct event_set *
649 ep_init(int *maxevents, unsigned int flags)
650 {
651  struct ep_set *eps;
652  int fd;
653 
654  dmsg(D_EVENT_WAIT, "EP_INIT maxevents=%d flags=0x%08x", *maxevents, flags);
655 
656  /* open epoll file descriptor */
657  fd = epoll_create(*maxevents);
658  if (fd < 0)
659  {
660  return NULL;
661  }
662 
663  set_cloexec(fd);
664 
665  ALLOC_OBJ_CLEAR(eps, struct ep_set);
666 
667  /* set dispatch functions */
668  eps->func.free = ep_free;
669  eps->func.reset = ep_reset;
670  eps->func.del = ep_del;
671  eps->func.ctl = ep_ctl;
672  eps->func.wait = ep_wait;
673 
674  /* fast method ("sort of") corresponds to epoll one-shot */
675  if (flags & EVENT_METHOD_FAST)
676  {
677  eps->fast = true;
678  }
679 
680  /* allocate space for epoll_wait return */
681  ASSERT(*maxevents > 0);
682  eps->maxevents = *maxevents;
683  ALLOC_ARRAY_CLEAR(eps->events, struct epoll_event, eps->maxevents);
684 
685  /* set epoll control fd */
686  eps->epfd = fd;
687 
688  return (struct event_set *) eps;
689 }
690 #endif /* EPOLL */
691 
692 #if POLL
693 
694 struct po_set
695 {
696  struct event_set_functions func;
697  bool fast;
698  struct pollfd *events;
699  void **args;
700  int n_events;
701  int capacity;
702 };
703 
704 static void
705 po_free(struct event_set *es)
706 {
707  struct po_set *pos = (struct po_set *) es;
708  free(pos->events);
709  free(pos->args);
710  free(pos);
711 }
712 
713 static void
714 po_reset(struct event_set *es)
715 {
716  struct po_set *pos = (struct po_set *) es;
717  ASSERT(pos->fast);
718  pos->n_events = 0;
719 }
720 
721 static void
722 po_del(struct event_set *es, event_t event)
723 {
724  struct po_set *pos = (struct po_set *) es;
725  int i;
726 
727  dmsg(D_EVENT_WAIT, "PO_DEL ev=%d", (int)event);
728 
729  ASSERT(!pos->fast);
730  for (i = 0; i < pos->n_events; ++i)
731  {
732  if (pos->events[i].fd == event)
733  {
734  int j;
735  for (j = i; j < pos->n_events - 1; ++j)
736  {
737  pos->events[j] = pos->events[j+1];
738  pos->args[j] = pos->args[j+1];
739  }
740  --pos->n_events;
741  break;
742  }
743  }
744 }
745 
746 static inline void
747 po_set_pollfd_events(struct pollfd *pfdp, unsigned int rwflags)
748 {
749  pfdp->events = 0;
750  if (rwflags & EVENT_WRITE)
751  {
752  pfdp->events |= POLLOUT;
753  }
754  if (rwflags & EVENT_READ)
755  {
756  pfdp->events |= (POLLIN|POLLPRI);
757  }
758 }
759 
760 static inline bool
761 po_append_event(struct po_set *pos, event_t event, unsigned int rwflags, void *arg)
762 {
763  if (pos->n_events < pos->capacity)
764  {
765  struct pollfd *pfdp = &pos->events[pos->n_events];
766  pfdp->fd = event;
767  pos->args[pos->n_events] = arg;
768  po_set_pollfd_events(pfdp, rwflags);
769  ++pos->n_events;
770  return true;
771  }
772  else
773  {
774  return false;
775  }
776 }
777 
778 static void
779 po_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
780 {
781  struct po_set *pos = (struct po_set *) es;
782 
783  dmsg(D_EVENT_WAIT, "PO_CTL rwflags=0x%04x ev=%d arg=" ptr_format,
784  rwflags, (int)event, (ptr_type)arg);
785 
786  if (pos->fast)
787  {
788  if (!po_append_event(pos, event, rwflags, arg))
789  {
790  goto err;
791  }
792  }
793  else
794  {
795  int i;
796  for (i = 0; i < pos->n_events; ++i)
797  {
798  struct pollfd *pfdp = &pos->events[i];
799  if (pfdp->fd == event)
800  {
801  pos->args[i] = arg;
802  po_set_pollfd_events(pfdp, rwflags);
803  goto done;
804  }
805  }
806  if (!po_append_event(pos, event, rwflags, arg))
807  {
808  goto err;
809  }
810  }
811 
812 done:
813  return;
814 
815 err:
816  msg(D_EVENT_ERRORS, "Error: poll: too many I/O wait events");
817 }
818 
819 static int
820 po_wait(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
821 {
822  struct po_set *pos = (struct po_set *) es;
823  int stat;
824 
825  stat = poll(pos->events, pos->n_events, tv_to_ms_timeout(tv));
826 
827  ASSERT(stat <= pos->n_events);
828 
829  if (stat > 0)
830  {
831  int i, j = 0;
832  const struct pollfd *pfdp = pos->events;
833  for (i = 0; i < pos->n_events && j < outlen; ++i)
834  {
835  if (pfdp->revents & (POLLIN|POLLPRI|POLLERR|POLLHUP|POLLOUT))
836  {
837  out->rwflags = 0;
838  if (pfdp->revents & (POLLIN|POLLPRI|POLLERR|POLLHUP))
839  {
840  out->rwflags |= EVENT_READ;
841  }
842  if (pfdp->revents & POLLOUT)
843  {
844  out->rwflags |= EVENT_WRITE;
845  }
846  out->arg = pos->args[i];
847  dmsg(D_EVENT_WAIT, "PO_WAIT[%d,%d] fd=%d rev=0x%08x rwflags=0x%04x arg=" ptr_format " %s",
848  i, j, pfdp->fd, pfdp->revents, out->rwflags, (ptr_type)out->arg, pos->fast ? "" : "[scalable]");
849  ++out;
850  ++j;
851  }
852  else if (pfdp->revents)
853  {
854  msg(D_EVENT_ERRORS, "Error: poll: unknown revents=0x%04x for fd=%d",
855  (unsigned int)pfdp->revents, pfdp->fd);
856  }
857  ++pfdp;
858  }
859  return j;
860  }
861  return stat;
862 }
863 
864 static struct event_set *
865 po_init(int *maxevents, unsigned int flags)
866 {
867  struct po_set *pos;
868 
869  dmsg(D_EVENT_WAIT, "PO_INIT maxevents=%d flags=0x%08x", *maxevents, flags);
870 
871  ALLOC_OBJ_CLEAR(pos, struct po_set);
872 
873  /* set dispatch functions */
874  pos->func.free = po_free;
875  pos->func.reset = po_reset;
876  pos->func.del = po_del;
877  pos->func.ctl = po_ctl;
878  pos->func.wait = po_wait;
879 
880  if (flags & EVENT_METHOD_FAST)
881  {
882  pos->fast = true;
883  }
884 
885  pos->n_events = 0;
886 
887  /* Figure our event capacity */
888  ASSERT(*maxevents > 0);
889  pos->capacity = *maxevents;
890 
891  /* Allocate space for pollfd structures to be passed to poll() */
892  ALLOC_ARRAY_CLEAR(pos->events, struct pollfd, pos->capacity);
893 
894  /* Allocate space for event_set_return objects */
895  ALLOC_ARRAY_CLEAR(pos->args, void *, pos->capacity);
896 
897  return (struct event_set *) pos;
898 }
899 #endif /* POLL */
900 
901 #if SELECT
902 
903 struct se_set
904 {
905  struct event_set_functions func;
906  bool fast;
907  fd_set readfds;
908  fd_set writefds;
909  void **args; /* allocated to capacity size */
910  int maxfd; /* largest fd seen so far, always < capacity */
911  int capacity; /* fixed largest fd + 1 */
912 };
913 
914 static void
915 se_free(struct event_set *es)
916 {
917  struct se_set *ses = (struct se_set *) es;
918  free(ses->args);
919  free(ses);
920 }
921 
922 static void
923 se_reset(struct event_set *es)
924 {
925  struct se_set *ses = (struct se_set *) es;
926  int i;
927  ASSERT(ses->fast);
928 
929  dmsg(D_EVENT_WAIT, "SE_RESET");
930 
931  FD_ZERO(&ses->readfds);
932  FD_ZERO(&ses->writefds);
933  for (i = 0; i <= ses->maxfd; ++i)
934  {
935  ses->args[i] = NULL;
936  }
937  ses->maxfd = -1;
938 }
939 
940 static void
941 se_del(struct event_set *es, event_t event)
942 {
943  struct se_set *ses = (struct se_set *) es;
944  ASSERT(!ses->fast);
945 
946  dmsg(D_EVENT_WAIT, "SE_DEL ev=%d", (int)event);
947 
948  if (event >= 0 && event < ses->capacity)
949  {
950  FD_CLR(event, &ses->readfds);
951  FD_CLR(event, &ses->writefds);
952  ses->args[event] = NULL;
953  }
954  else
955  {
956  msg(D_EVENT_ERRORS, "Error: select/se_del: too many I/O wait events");
957  }
958  return;
959 }
960 
961 static void
962 se_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
963 {
964  struct se_set *ses = (struct se_set *) es;
965 
966  dmsg(D_EVENT_WAIT, "SE_CTL rwflags=0x%04x ev=%d fast=%d cap=%d maxfd=%d arg=" ptr_format,
967  rwflags, (int)event, (int)ses->fast, ses->capacity, ses->maxfd, (ptr_type)arg);
968 
969  if (event >= 0 && event < ses->capacity)
970  {
971  ses->maxfd = max_int(event, ses->maxfd);
972  ses->args[event] = arg;
973  if (ses->fast)
974  {
975  if (rwflags & EVENT_READ)
976  {
977  openvpn_fd_set(event, &ses->readfds);
978  }
979  if (rwflags & EVENT_WRITE)
980  {
981  openvpn_fd_set(event, &ses->writefds);
982  }
983  }
984  else
985  {
986  if (rwflags & EVENT_READ)
987  {
988  openvpn_fd_set(event, &ses->readfds);
989  }
990  else
991  {
992  FD_CLR(event, &ses->readfds);
993  }
994  if (rwflags & EVENT_WRITE)
995  {
996  openvpn_fd_set(event, &ses->writefds);
997  }
998  else
999  {
1000  FD_CLR(event, &ses->writefds);
1001  }
1002  }
1003  }
1004  else
1005  {
1006  msg(D_EVENT_ERRORS, "Error: select: too many I/O wait events, fd=%d cap=%d",
1007  (int) event,
1008  ses->capacity);
1009  }
1010 }
1011 
1012 static int
1013 se_wait_return(struct se_set *ses,
1014  fd_set *read,
1015  fd_set *write,
1016  struct event_set_return *out,
1017  int outlen)
1018 {
1019  int i, j = 0;
1020  for (i = 0; i <= ses->maxfd && j < outlen; ++i)
1021  {
1022  const bool r = FD_ISSET(i, read);
1023  const bool w = FD_ISSET(i, write);
1024  if (r || w)
1025  {
1026  out->rwflags = 0;
1027  if (r)
1028  {
1029  out->rwflags |= EVENT_READ;
1030  }
1031  if (w)
1032  {
1033  out->rwflags |= EVENT_WRITE;
1034  }
1035  out->arg = ses->args[i];
1036  dmsg(D_EVENT_WAIT, "SE_WAIT[%d,%d] rwflags=0x%04x arg=" ptr_format,
1037  i, j, out->rwflags, (ptr_type)out->arg);
1038  ++out;
1039  ++j;
1040  }
1041  }
1042  return j;
1043 }
1044 
1045 static int
1046 se_wait_fast(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
1047 {
1048  struct se_set *ses = (struct se_set *) es;
1049  struct timeval tv_tmp = *tv;
1050  int stat;
1051 
1052  dmsg(D_EVENT_WAIT, "SE_WAIT_FAST maxfd=%d tv=%" PRIi64 "/%ld",
1053  ses->maxfd,
1054  (int64_t)tv_tmp.tv_sec,
1055  (long)tv_tmp.tv_usec);
1056 
1057  stat = select(ses->maxfd + 1, &ses->readfds, &ses->writefds, NULL, &tv_tmp);
1058 
1059  if (stat > 0)
1060  {
1061  stat = se_wait_return(ses, &ses->readfds, &ses->writefds, out, outlen);
1062  }
1063 
1064  return stat;
1065 }
1066 
1067 static int
1068 se_wait_scalable(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
1069 {
1070  struct se_set *ses = (struct se_set *) es;
1071  struct timeval tv_tmp = *tv;
1072  fd_set read = ses->readfds;
1073  fd_set write = ses->writefds;
1074  int stat;
1075 
1076  dmsg(D_EVENT_WAIT, "SE_WAIT_SCALEABLE maxfd=%d tv=%" PRIi64 "/%ld",
1077  ses->maxfd, (int64_t)tv_tmp.tv_sec, (long)tv_tmp.tv_usec);
1078 
1079  stat = select(ses->maxfd + 1, &read, &write, NULL, &tv_tmp);
1080 
1081  if (stat > 0)
1082  {
1083  stat = se_wait_return(ses, &read, &write, out, outlen);
1084  }
1085 
1086  return stat;
1087 }
1088 
1089 static struct event_set *
1090 se_init(int *maxevents, unsigned int flags)
1091 {
1092  struct se_set *ses;
1093 
1094  dmsg(D_EVENT_WAIT, "SE_INIT maxevents=%d flags=0x%08x", *maxevents, flags);
1095 
1096  ALLOC_OBJ_CLEAR(ses, struct se_set);
1097 
1098  /* set dispatch functions */
1099  ses->func.free = se_free;
1100  ses->func.reset = se_reset;
1101  ses->func.del = se_del;
1102  ses->func.ctl = se_ctl;
1103  ses->func.wait = se_wait_scalable;
1104 
1105  if (flags & EVENT_METHOD_FAST)
1106  {
1107  ses->fast = true;
1108  ses->func.wait = se_wait_fast;
1109  }
1110 
1111  /* Select needs to be passed this value + 1 */
1112  ses->maxfd = -1;
1113 
1114  /* Set our event capacity */
1115  ASSERT(*maxevents > 0);
1116  *maxevents = min_int(*maxevents, SELECT_MAX_FDS);
1117  ses->capacity = SELECT_MAX_FDS;
1118 
1119  /* Allocate space for event_set_return void * args */
1120  ALLOC_ARRAY_CLEAR(ses->args, void *, ses->capacity);
1121 
1122  return (struct event_set *) ses;
1123 }
1124 #endif /* SELECT */
1125 
1126 static struct event_set *
1127 event_set_init_simple(int *maxevents, unsigned int flags)
1128 {
1129  struct event_set *ret = NULL;
1130 #ifdef _WIN32
1131  ret = we_init(maxevents, flags);
1132 #elif POLL && SELECT
1133 #if 0 /* Define to 1 if EVENT_METHOD_US_TIMEOUT should cause select to be favored over poll */
1134  if (flags & EVENT_METHOD_US_TIMEOUT)
1135  {
1136  ret = se_init(maxevents, flags);
1137  }
1138 #endif
1139 #ifdef SELECT_PREFERRED_OVER_POLL
1140  if (!ret)
1141  {
1142  ret = se_init(maxevents, flags);
1143  }
1144  if (!ret)
1145  {
1146  ret = po_init(maxevents, flags);
1147  }
1148 #else /* ifdef SELECT_PREFERRED_OVER_POLL */
1149  if (!ret)
1150  {
1151  ret = po_init(maxevents, flags);
1152  }
1153  if (!ret)
1154  {
1155  ret = se_init(maxevents, flags);
1156  }
1157 #endif
1158 #elif POLL
1159  ret = po_init(maxevents, flags);
1160 #elif SELECT
1161  ret = se_init(maxevents, flags);
1162 #else /* ifdef _WIN32 */
1163 #error At least one of poll, select, or WSAWaitForMultipleEvents must be supported by the kernel
1164 #endif /* ifdef _WIN32 */
1165  ASSERT(ret);
1166  return ret;
1167 }
1168 
1169 static struct event_set *
1170 event_set_init_scalable(int *maxevents, unsigned int flags)
1171 {
1172  struct event_set *ret = NULL;
1173 #if EPOLL
1174  ret = ep_init(maxevents, flags);
1175  if (!ret)
1176  {
1177  msg(M_WARN, "Note: sys_epoll API is unavailable, falling back to poll/select API");
1178  ret = event_set_init_simple(maxevents, flags);
1179  }
1180 #else /* if EPOLL */
1181  ret = event_set_init_simple(maxevents, flags);
1182 #endif
1183  ASSERT(ret);
1184  return ret;
1185 }
1186 
1187 struct event_set *
1188 event_set_init(int *maxevents, unsigned int flags)
1189 {
1190  if (flags & EVENT_METHOD_FAST)
1191  {
1192  return event_set_init_simple(maxevents, flags);
1193  }
1194  else
1195  {
1196  return event_set_init_scalable(maxevents, flags);
1197  }
1198 }
void(* free)(struct event_set *es)
Definition: event.h:101
static void we_del_index(struct we_set *wes, int index)
Definition: event.c:176
HANDLE * events
Definition: event.c:90
static int we_wait(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
Definition: event.c:387
HANDLE read
Definition: win32.h:76
void(* del)(struct event_set *es, event_t event)
Definition: event.h:103
unsigned long ptr_type
Definition: common.h:58
int(* wait)(struct event_set *es, const struct timeval *tv, struct event_set_return *out, int outlen)
Definition: event.h:112
void(* reset)(struct event_set *es)
Definition: event.h:102
static void we_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
Definition: event.c:235
#define D_EVENT_WAIT
Definition: errlevel.h:160
#define EVENT_METHOD_FAST
Definition: event.h:80
static bool we_append_event(struct we_set *wes, event_t event, unsigned int rwflags, void *arg)
Definition: event.c:121
Definition: event.c:86
static void we_del_event(struct we_set *wes, event_t event)
Definition: event.c:151
#define dmsg(flags,...)
Definition: error.h:153
bool fast
Definition: event.c:89
static struct event_set * event_set_init_simple(int *maxevents, unsigned int flags)
Definition: event.c:1127
static void we_reset(struct event_set *es)
Definition: event.c:219
#define ASSERT(x)
Definition: error.h:200
int capacity
Definition: event.c:93
static bool check_debug_level(unsigned int level)
Definition: error.h:224
void * arg
Definition: event.h:118
list flags
struct event_set * event_set_init(int *maxevents, unsigned int flags)
Definition: event.c:1188
struct event_set_return * esr
Definition: event.c:91
static int tv_to_ms_timeout(const struct timeval *tv)
Definition: event.c:72
#define CLEAR(x)
Definition: basic.h:33
struct event_set_functions func
Definition: event.c:88
int n_events
Definition: event.c:92
#define ALLOC_OBJ_CLEAR(dptr, type)
Definition: buffer.h:1050
static int pos(char c)
Definition: base64.c:107
static void openvpn_fd_set(socket_descriptor_t fd, fd_set *setp)
Definition: fdmisc.h:40
static void we_free(struct event_set *es)
Definition: event.c:210
#define M_ERRNO
Definition: error.h:99
#define EVENT_METHOD_US_TIMEOUT
Definition: event.h:79
static void we_set_event(struct we_set *wes, int i, event_t event, unsigned int rwflags, void *arg)
Definition: event.c:97
#define EVENT_READ
Definition: event.h:39
#define SELECT_MAX_FDS
Definition: event.c:68
static void we_get_rw_indices(struct we_set *wes, event_t event, int *ri, int *wi)
Definition: event.c:189
#define D_EVENT_ERRORS
Definition: errlevel.h:66
#define msg(flags,...)
Definition: error.h:149
static int max_int(int x, int y)
Definition: integer.h:69
#define ALLOC_ARRAY_CLEAR(dptr, type, n)
Definition: buffer.h:1066
HANDLE write
Definition: win32.h:77
#define M_ERR
Definition: error.h:110
static void we_del(struct event_set *es, event_t event)
Definition: event.c:227
#define M_FATAL
Definition: error.h:94
#define M_WARN
Definition: error.h:96
static int min_int(int x, int y)
Definition: integer.h:82
#define free
Definition: cmocka.c:1850
#define EVENT_WRITE
Definition: event.h:40
void set_cloexec(socket_descriptor_t fd)
Definition: fdmisc.c:81
static SERVICE_STATUS status
Definition: interactive.c:56
static struct event_set * event_set_init_scalable(int *maxevents, unsigned int flags)
Definition: event.c:1170
unsigned int rwflags
Definition: event.h:117
static struct event_set * we_init(int *maxevents, unsigned int flags)
Definition: event.c:485
void(* ctl)(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
Definition: event.h:104
#define ptr_format
Definition: common.h:49