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