From: "its_Giaan (Code Review)" Date: Fri, 24 Jan 2025 11:45:38 +0000 In-Reply-To: References: Reply-To: gianmarco@...2726..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., gert@...1296... Message-ID: <7acd9e1f00dcc58b58f39efa19dbbf8708d922dd-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="GyKRPZwAe+0="; charset=UTF-8 Subject: [Openvpn-devel] [XL] Change in openvpn[master]: multiproto: move generic event handling code in dedicated files List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , cron2 , flichtenheld Cc: openvpn-devel --GyKRPZwAe+0= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, its_Giaan, plaisthos. Hello cron2, flichtenheld, plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/763?usp=3Demail to look at the new patch set (#14). Change subject: multiproto: move generic event handling code in dedicated f= iles ...................................................................... multiproto: move generic event handling code in dedicated files Introduced multi_io.h and multi_io.c files to centralize all codes related to multiple protocols. Renamed the struct mtcp to struct multi_io since it encompasses the event_set used by the parent context in server mode. Several methods have also been renamed and moved to fit the multiproto structure: - multi_tcp_init() -> multi_io_init(); - multi_tcp_free() -> multi_io_free(); - multi_tcp_wait() -> multi_io_wait(); and so forth. Change-Id: I1e5a84969988e4f027a18658d4ab268c13fbf929 Signed-off-by: Gianmarco De Gregori --- M CMakeLists.txt M src/openvpn/Makefile.am M src/openvpn/forward.h M src/openvpn/mtcp.c M src/openvpn/mtcp.h M src/openvpn/multi.c M src/openvpn/multi.h A src/openvpn/multi_io.c A src/openvpn/multi_io.h 9 files changed, 751 insertions(+), 620 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/63/763/14 diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ffcc89..ea8d006 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -458,6 +458,8 @@ src/openvpn/mudp.h src/openvpn/multi.c src/openvpn/multi.h + src/openvpn/multi_io.h + src/openvpn/multi_io.c src/openvpn/ntlm.c src/openvpn/ntlm.h src/openvpn/occ.c diff --git a/src/openvpn/Makefile.am b/src/openvpn/Makefile.am index d6d6592..37af683 100644 --- a/src/openvpn/Makefile.am +++ b/src/openvpn/Makefile.am @@ -93,6 +93,7 @@ mtu.c mtu.h \ mudp.c mudp.h \ multi.c multi.h \ + multi_io.c multi_io.h \ networking_freebsd.c \ networking_iproute2.c networking_iproute2.h \ networking_sitnl.c networking_sitnl.h \ diff --git a/src/openvpn/forward.h b/src/openvpn/forward.h index ca2a695..214a322 100644 --- a/src/openvpn/forward.h +++ b/src/openvpn/forward.h @@ -50,6 +50,7 @@ #include "openvpn.h" #include "occ.h" #include "ping.h" +#include "multi_io.h" #define IOW_TO_TUN (1<<0) #define IOW_TO_LINK (1<<1) diff --git a/src/openvpn/mtcp.c b/src/openvpn/mtcp.c index b5bbf13..6d1d5a0 100644 --- a/src/openvpn/mtcp.c +++ b/src/openvpn/mtcp.c @@ -29,6 +29,8 @@ #include "multi.h" #include "forward.h" +#include "mtcp.h" +#include "multi_io.h" #include "memdbg.h" @@ -36,30 +38,6 @@ #include #endif -/* - * TCP States - */ -#define TA_UNDEF 0 -#define TA_SOCKET_READ 1 -#define TA_SOCKET_READ_RESIDUAL 2 -#define TA_SOCKET_WRITE 3 -#define TA_SOCKET_WRITE_READY 4 -#define TA_SOCKET_WRITE_DEFERRED 5 -#define TA_TUN_READ 6 -#define TA_TUN_WRITE 7 -#define TA_INITIAL 8 -#define TA_TIMEOUT 9 -#define TA_TUN_WRITE_TIMEOUT 10 - -/* - * Special tags passed to event.[ch] functions - */ -#define MTCP_TUN ((void *)2) -#define MTCP_SIG ((void *)3) /* Only on Windows */ -#define MTCP_MANAGEMENT ((void *)4) -#define MTCP_FILE_CLOSE_WRITE ((void *)5) -#define MTCP_DCO ((void *)6) - struct ta_iow_flags { unsigned int flags; @@ -68,52 +46,7 @@ unsigned int sock; }; -#ifdef ENABLE_DEBUG -static const char * -pract(int action) -{ - switch (action) - { - case TA_UNDEF: - return "TA_UNDEF"; - - case TA_SOCKET_READ: - return "TA_SOCKET_READ"; - - case TA_SOCKET_READ_RESIDUAL: - return "TA_SOCKET_READ_RESIDUAL"; - - case TA_SOCKET_WRITE: - return "TA_SOCKET_WRITE"; - - case TA_SOCKET_WRITE_READY: - return "TA_SOCKET_WRITE_READY"; - - case TA_SOCKET_WRITE_DEFERRED: - return "TA_SOCKET_WRITE_DEFERRED"; - - case TA_TUN_READ: - return "TA_TUN_READ"; - - case TA_TUN_WRITE: - return "TA_TUN_WRITE"; - - case TA_INITIAL: - return "TA_INITIAL"; - - case TA_TIMEOUT: - return "TA_TIMEOUT"; - - case TA_TUN_WRITE_TIMEOUT: - return "TA_TUN_WRITE_TIMEOUT"; - - default: - return "?"; - } -} -#endif /* ENABLE_DEBUG */ - -static struct multi_instance * +struct multi_instance * multi_create_instance_tcp(struct multi_context *m, struct link_socket *ls) { struct gc_arena gc =3D gc_new(); @@ -193,139 +126,42 @@ mbuf_free(mi->tcp_link_out_deferred); } -struct multi_tcp * -multi_tcp_init(int maxevents, int *maxclients) -{ - struct multi_tcp *mtcp; - const int extra_events =3D BASE_N_EVENTS; - - ASSERT(maxevents >=3D 1); - ASSERT(maxclients); - - ALLOC_OBJ_CLEAR(mtcp, struct multi_tcp); - mtcp->maxevents =3D maxevents + extra_events; - mtcp->es =3D event_set_init(&mtcp->maxevents, 0); - wait_signal(mtcp->es, MTCP_SIG); - ALLOC_ARRAY(mtcp->esr, struct event_set_return, mtcp->maxevents); - *maxclients =3D max_int(min_int(mtcp->maxevents - extra_events, *maxcl= ients), 1); - msg(D_MULTI_LOW, "MULTI: TCP INIT maxclients=3D%d maxevents=3D%d", *ma= xclients, mtcp->maxevents); - return mtcp; -} - void -multi_tcp_delete_event(struct multi_tcp *mtcp, event_t event) +multi_tcp_delete_event(struct multi_io *multi_io, event_t event) { - if (mtcp && mtcp->es) + if (multi_io && multi_io->es) { - event_del(mtcp->es, event); + event_del(multi_io->es, event); } } void -multi_tcp_free(struct multi_tcp *mtcp) -{ - if (mtcp) - { - event_free(mtcp->es); - free(mtcp->esr); - free(mtcp); - } -} - -void -multi_tcp_dereference_instance(struct multi_tcp *mtcp, struct multi_instan= ce *mi) +multi_tcp_dereference_instance(struct multi_io *multi_io, struct multi_ins= tance *mi) { struct link_socket *ls =3D mi->context.c2.link_sockets[0]; if (ls && mi->socket_set_called) { - event_del(mtcp->es, socket_event_handle(ls)); + event_del(multi_io->es, socket_event_handle(ls)); mi->socket_set_called =3D false; } - mtcp->n_esr =3D 0; + multi_io->n_esr =3D 0; } -static inline void +void multi_tcp_set_global_rw_flags(struct multi_context *m, struct multi_instan= ce *mi) { if (mi) { mi->socket_set_called =3D true; socket_set(mi->context.c2.link_sockets[0], - m->mtcp->es, + m->multi_io->es, mbuf_defined(mi->tcp_link_out_deferred) ? EVENT_WRITE := EVENT_READ, &mi->ev_arg, &mi->tcp_rwflags); } } -static inline int -multi_tcp_wait(const struct context *c, - struct multi_tcp *mtcp) -{ - int status; - unsigned int *persistent =3D &mtcp->tun_rwflags; - - for (int i =3D 0; i < c->c1.link_sockets_num; i++) - { - socket_set_listen_persistent(c->c2.link_sockets[i], mtcp->es, - &c->c2.link_sockets[i]->ev_arg); - } - -#ifdef _WIN32 - if (tuntap_is_wintun(c->c1.tuntap)) - { - if (!tuntap_ring_empty(c->c1.tuntap)) - { - /* there is data in wintun ring buffer, read it immediately */ - mtcp->esr[0].arg =3D MTCP_TUN; - mtcp->esr[0].rwflags =3D EVENT_READ; - mtcp->n_esr =3D 1; - return 1; - } - persistent =3D NULL; - } -#endif - tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, persistent); -#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) - dco_event_set(&c->c1.tuntap->dco, mtcp->es, MTCP_DCO); -#endif - -#ifdef ENABLE_MANAGEMENT - if (management) - { - management_socket_set(management, mtcp->es, MTCP_MANAGEMENT, &mtcp= ->management_persist_flags); - } -#endif - -#ifdef ENABLE_ASYNC_PUSH - /* arm inotify watcher */ - event_ctl(mtcp->es, c->c2.inotify_fd, EVENT_READ, MTCP_FILE_CLOSE_WRIT= E); -#endif - - status =3D event_wait(mtcp->es, &c->c2.timeval, mtcp->esr, mtcp->maxev= ents); - update_time(); - mtcp->n_esr =3D 0; - if (status > 0) - { - mtcp->n_esr =3D status; - } - return status; -} - -static inline struct context * -multi_tcp_context(struct multi_context *m, struct multi_instance *mi) -{ - if (mi) - { - return &mi->context; - } - else - { - return &m->top; - } -} - -static bool +bool multi_tcp_process_outgoing_link_ready(struct multi_context *m, struct mult= i_instance *mi, const unsigned int mpp_flags) { struct mbuf_item item; @@ -349,7 +185,7 @@ return ret; } -static bool +bool multi_tcp_process_outgoing_link(struct multi_context *m, bool defer, const= unsigned int mpp_flags) { struct multi_instance *mi =3D multi_process_outgoing_link_pre(m); @@ -393,417 +229,6 @@ return ret; } -static int -multi_tcp_wait_lite(struct multi_context *m, struct multi_instance *mi, co= nst int action, bool *tun_input_pending) -{ - struct context *c =3D multi_tcp_context(m, mi); - unsigned int looking_for =3D 0; - - dmsg(D_MULTI_DEBUG, "MULTI TCP: multi_tcp_wait_lite a=3D%s mi=3D" ptr_= format, - pract(action), - (ptr_type)mi); - - tv_clear(&c->c2.timeval); /* ZERO-TIMEOUT */ - - switch (action) - { - case TA_TUN_READ: - looking_for =3D TUN_READ; - tun_input_pending =3D NULL; - io_wait(c, IOW_READ_TUN); - break; - - case TA_SOCKET_READ: - looking_for =3D SOCKET_READ; - tun_input_pending =3D NULL; - io_wait(c, IOW_READ_LINK); - break; - - case TA_TUN_WRITE: - looking_for =3D TUN_WRITE; - tun_input_pending =3D NULL; - c->c2.timeval.tv_sec =3D 1; /* For some reason, the Linux 2.2 = TUN/TAP driver hits this timeout */ - perf_push(PERF_PROC_OUT_TUN_MTCP); - io_wait(c, IOW_TO_TUN); - perf_pop(); - break; - - case TA_SOCKET_WRITE: - looking_for =3D SOCKET_WRITE; - io_wait(c, IOW_TO_LINK|IOW_READ_TUN_FORCE); - break; - - default: - msg(M_FATAL, "MULTI TCP: multi_tcp_wait_lite, unhandled action= =3D%d", action); - } - - if (tun_input_pending && (c->c2.event_set_status & TUN_READ)) - { - *tun_input_pending =3D true; - } - - if (c->c2.event_set_status & looking_for) - { - return action; - } - else - { - switch (action) - { - /* TCP socket output buffer is full */ - case TA_SOCKET_WRITE: - return TA_SOCKET_WRITE_DEFERRED; - - /* TUN device timed out on accepting write */ - case TA_TUN_WRITE: - return TA_TUN_WRITE_TIMEOUT; - } - - return TA_UNDEF; - } -} - -static struct multi_instance * -multi_tcp_dispatch(struct multi_context *m, struct multi_instance *mi, con= st int action) -{ - const unsigned int mpp_flags =3D MPP_PRE_SELECT|MPP_RECORD_TOUCH; - struct multi_instance *touched =3D mi; - m->mpp_touched =3D &touched; - - dmsg(D_MULTI_DEBUG, "MULTI TCP: multi_tcp_dispatch a=3D%s mi=3D" ptr_f= ormat, - pract(action), - (ptr_type)mi); - - switch (action) - { - case TA_TUN_READ: - read_incoming_tun(&m->top); - if (!IS_SIG(&m->top)) - { - multi_process_incoming_tun(m, mpp_flags); - } - break; - - case TA_SOCKET_READ: - case TA_SOCKET_READ_RESIDUAL: - ASSERT(mi); - ASSERT(mi->context.c2.link_sockets); - ASSERT(mi->context.c2.link_sockets[0]); - set_prefix(mi); - read_incoming_link(&mi->context, mi->context.c2.link_sockets[0= ]); - clear_prefix(); - if (!IS_SIG(&mi->context)) - { - multi_process_incoming_link(m, mi, mpp_flags, - mi->context.c2.link_sockets[0]= ); - if (!IS_SIG(&mi->context)) - { - stream_buf_read_setup(mi->context.c2.link_sockets[0]); - } - } - break; - - case TA_TIMEOUT: - multi_process_timeout(m, mpp_flags); - break; - - case TA_TUN_WRITE: - multi_process_outgoing_tun(m, mpp_flags); - break; - - case TA_TUN_WRITE_TIMEOUT: - multi_process_drop_outgoing_tun(m, mpp_flags); - break; - - case TA_SOCKET_WRITE_READY: - ASSERT(mi); - multi_tcp_process_outgoing_link_ready(m, mi, mpp_flags); - break; - - case TA_SOCKET_WRITE: - multi_tcp_process_outgoing_link(m, false, mpp_flags); - break; - - case TA_SOCKET_WRITE_DEFERRED: - multi_tcp_process_outgoing_link(m, true, mpp_flags); - break; - - case TA_INITIAL: - ASSERT(mi); - multi_tcp_set_global_rw_flags(m, mi); - multi_process_post(m, mi, mpp_flags); - break; - - default: - msg(M_FATAL, "MULTI TCP: multi_tcp_dispatch, unhandled action= =3D%d", action); - } - - m->mpp_touched =3D NULL; - return touched; -} - -static int -multi_tcp_post(struct multi_context *m, struct multi_instance *mi, const i= nt action) -{ - struct context *c =3D multi_tcp_context(m, mi); - int newaction =3D TA_UNDEF; - -#define MTP_NONE 0 -#define MTP_TUN_OUT (1<<0) -#define MTP_LINK_OUT (1<<1) - unsigned int flags =3D MTP_NONE; - - if (TUN_OUT(c)) - { - flags |=3D MTP_TUN_OUT; - } - if (LINK_OUT(c)) - { - flags |=3D MTP_LINK_OUT; - } - - switch (flags) - { - case MTP_TUN_OUT|MTP_LINK_OUT: - case MTP_TUN_OUT: - newaction =3D TA_TUN_WRITE; - break; - - case MTP_LINK_OUT: - newaction =3D TA_SOCKET_WRITE; - break; - - case MTP_NONE: - if (mi && sockets_read_residual(c)) - { - newaction =3D TA_SOCKET_READ_RESIDUAL; - } - else - { - multi_tcp_set_global_rw_flags(m, mi); - } - break; - - default: - { - struct gc_arena gc =3D gc_new(); - msg(M_FATAL, "MULTI TCP: multi_tcp_post bad state, mi=3D%s fla= gs=3D%d", - multi_instance_string(mi, false, &gc), - flags); - gc_free(&gc); - break; - } - } - - dmsg(D_MULTI_DEBUG, "MULTI TCP: multi_tcp_post %s -> %s", - pract(action), - pract(newaction)); - - return newaction; -} - -static void -multi_tcp_action(struct multi_context *m, struct multi_instance *mi, int a= ction, bool poll) -{ - bool tun_input_pending =3D false; - - do - { - dmsg(D_MULTI_DEBUG, "MULTI TCP: multi_tcp_action a=3D%s p=3D%d", - pract(action), - poll); - - /* - * If TA_SOCKET_READ_RESIDUAL, it means we still have pending - * input packets which were read by a prior TCP recv. - * - * Otherwise do a "lite" wait, which means we wait with 0 timeout - * on I/O events only related to the current instance, not - * the big list of events. - * - * On our first pass, poll will be false because we already know - * that input is available, and to call io_wait would be redundant. - */ - if (poll && action !=3D TA_SOCKET_READ_RESIDUAL) - { - const int orig_action =3D action; - action =3D multi_tcp_wait_lite(m, mi, action, &tun_input_pendi= ng); - if (action =3D=3D TA_UNDEF) - { - msg(M_FATAL, "MULTI TCP: I/O wait required blocking in mul= ti_tcp_action, action=3D%d", orig_action); - } - } - - /* - * Dispatch the action - */ - struct multi_instance *touched =3D multi_tcp_dispatch(m, mi, actio= n); - - /* - * Signal received or TCP connection - * reset by peer? - */ - if (touched && IS_SIG(&touched->context)) - { - if (mi =3D=3D touched) - { - mi =3D NULL; - } - multi_close_instance_on_signal(m, touched); - } - - - /* - * If dispatch produced any pending output - * for a particular instance, point to - * that instance. - */ - if (m->pending) - { - mi =3D m->pending; - } - - /* - * Based on the effects of the action, - * such as generating pending output, - * possibly transition to a new action state. - */ - action =3D multi_tcp_post(m, mi, action); - - /* - * If we are finished processing the original action, - * check if we have any TUN input. If so, transition - * our action state to processing this input. - */ - if (tun_input_pending && action =3D=3D TA_UNDEF) - { - action =3D TA_TUN_READ; - mi =3D NULL; - tun_input_pending =3D false; - poll =3D false; - } - else - { - poll =3D true; - } - - } while (action !=3D TA_UNDEF); -} - -static void -multi_tcp_process_io(struct multi_context *m) -{ - struct multi_tcp *mtcp =3D m->mtcp; - int i; - - for (i =3D 0; i < mtcp->n_esr; ++i) - { - struct event_set_return *e =3D &mtcp->esr[i]; - - /* incoming data for instance or listening socket? */ - if (e->arg >=3D MULTI_N) - { - struct event_arg *ev_arg =3D (struct event_arg *)e->arg; - switch (ev_arg->type) - { - struct multi_instance *mi; - - /* react to event on child instance */ - case EVENT_ARG_MULTI_INSTANCE: - if (!ev_arg->u.mi) - { - msg(D_MULTI_ERRORS, "MULTI: mtcp_proc_io: null min= stance"); - break; - } - - mi =3D ev_arg->u.mi; - if (e->rwflags & EVENT_WRITE) - { - multi_tcp_action(m, mi, TA_SOCKET_WRITE_READY, fal= se); - } - else if (e->rwflags & EVENT_READ) - { - multi_tcp_action(m, mi, TA_SOCKET_READ, false); - } - break; - - /* new incoming TCP client attempting to connect? */ - case EVENT_ARG_LINK_SOCKET: - if (!ev_arg->u.sock) - { - msg(D_MULTI_ERRORS, "MULTI: mtcp_proc_io: null soc= ket"); - break; - } - - socket_reset_listen_persistent(ev_arg->u.sock); - mi =3D multi_create_instance_tcp(m, ev_arg->u.sock); - if (mi) - { - multi_tcp_action(m, mi, TA_INITIAL, false); - } - break; - } - } - else - { -#ifdef ENABLE_MANAGEMENT - if (e->arg =3D=3D MTCP_MANAGEMENT) - { - ASSERT(management); - management_io(management); - } - else -#endif - /* incoming data on TUN? */ - if (e->arg =3D=3D MTCP_TUN) - { - if (e->rwflags & EVENT_WRITE) - { - multi_tcp_action(m, NULL, TA_TUN_WRITE, false); - } - else if (e->rwflags & EVENT_READ) - { - multi_tcp_action(m, NULL, TA_TUN_READ, false); - } - } -#if defined(ENABLE_DCO) && (defined(TARGET_LINUX) || defined(TARGET_FREEBS= D)) - /* incoming data on DCO? */ - else if (e->arg =3D=3D MTCP_DCO) - { - multi_process_incoming_dco(m); - } -#endif - /* signal received? */ - else if (e->arg =3D=3D MTCP_SIG) - { - get_signal(&m->top.sig->signal_received); - } -#ifdef ENABLE_ASYNC_PUSH - else if (e->arg =3D=3D MTCP_FILE_CLOSE_WRITE) - { - multi_process_file_closed(m, MPP_PRE_SELECT | MPP_RECORD_T= OUCH); - } -#endif - } - if (IS_SIG(&m->top)) - { - break; - } - } - mtcp->n_esr =3D 0; - - /* - * Process queued mbuf packets destined for TCP socket - */ - { - struct multi_instance *mi; - while (!IS_SIG(&m->top) && (mi =3D mbuf_peek(m->mbuf)) !=3D NULL) - { - multi_tcp_action(m, mi, TA_SOCKET_WRITE, true); - } - } -} - /* * Top level event loop for single-threaded operation. * TCP mode. @@ -851,7 +276,7 @@ /* wait on tun/socket list */ multi_get_timeout(&multi, &multi.top.c2.timeval); - status =3D multi_tcp_wait(&multi.top, multi.mtcp); + status =3D multi_io_wait(&multi); MULTI_CHECK_SIG(&multi); /* check on status of coarse timers */ @@ -861,12 +286,12 @@ if (status > 0) { /* process the I/O which triggered select */ - multi_tcp_process_io(&multi); + multi_io_process_io(&multi); MULTI_CHECK_SIG(&multi); } else if (status =3D=3D 0) { - multi_tcp_action(&multi, NULL, TA_TIMEOUT, false); + multi_io_action(&multi, NULL, TA_TIMEOUT, false); } perf_pop(); diff --git a/src/openvpn/mtcp.h b/src/openvpn/mtcp.h index ab968e9..0da0a7d 100644 --- a/src/openvpn/mtcp.h +++ b/src/openvpn/mtcp.h @@ -30,34 +30,27 @@ #include "event.h" -/* - * Extra state info needed for TCP mode - */ -struct multi_tcp -{ - struct event_set *es; - struct event_set_return *esr; - int n_esr; - int maxevents; - unsigned int tun_rwflags; -#ifdef ENABLE_MANAGEMENT - unsigned int management_persist_flags; -#endif -}; - +struct multi_context; struct multi_instance; struct context; -struct multi_tcp *multi_tcp_init(int maxevents, int *maxclients); - -void multi_tcp_free(struct multi_tcp *mtcp); - -void multi_tcp_dereference_instance(struct multi_tcp *mtcp, struct multi_i= nstance *mi); +void multi_tcp_dereference_instance(struct multi_io *multi_io, struct mult= i_instance *mi); bool multi_tcp_instance_specific_init(struct multi_context *m, struct mult= i_instance *mi); void multi_tcp_instance_specific_free(struct multi_instance *mi); +void multi_tcp_set_global_rw_flags(struct multi_context *m, struct multi_i= nstance *mi); + +bool multi_tcp_process_outgoing_link(struct multi_context *m, bool defer, = const unsigned int mpp_flags); + +bool multi_tcp_process_outgoing_link_ready(struct multi_context *m, struct= multi_instance *mi, const unsigned int mpp_flags); + +struct multi_instance *multi_create_instance_tcp(struct multi_context *m, = struct link_socket *ls); + +void multi_tcp_link_out_deferred(struct multi_context *m, struct multi_ins= tance *mi); + + /*************************************************************************= */ /** * Main event loop for OpenVPN in TCP server mode. @@ -68,6 +61,6 @@ void tunnel_server_tcp(struct context *top); -void multi_tcp_delete_event(struct multi_tcp *mtcp, event_t event); +void multi_tcp_delete_event(struct multi_io *multi_io, event_t event); #endif /* ifndef MTCP_H */ diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index f426b46..3f55dd7 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -440,7 +440,7 @@ */ if (tcp_mode) { - m->mtcp =3D multi_tcp_init(t->options.max_clients, &m->max_clients= ); + m->multi_io =3D multi_io_init(t->options.max_clients, &m->max_clie= nts); } m->tcp_queue_limit =3D t->options.tcp_queue_limit; @@ -665,9 +665,9 @@ mi->did_iroutes =3D false; } - if (m->mtcp) + if (m->multi_io) { - multi_tcp_dereference_instance(m->mtcp, mi); + multi_tcp_dereference_instance(m->multi_io, mi); } mbuf_dereference_instance(m->mbuf, mi); @@ -742,7 +742,7 @@ initial_rate_limit_free(m->initial_rate_limiter); multi_reap_free(m->reaper); mroute_helper_free(m->route_helper); - multi_tcp_free(m->mtcp); + multi_io_free(m->multi_io); } } @@ -3975,9 +3975,9 @@ management_delete_event(void *arg, event_t event) { struct multi_context *m =3D (struct multi_context *) arg; - if (m->mtcp) + if (m->multi_io) { - multi_tcp_delete_event(m->mtcp, event); + multi_tcp_delete_event(m->multi_io, event); } } diff --git a/src/openvpn/multi.h b/src/openvpn/multi.h index 9b6834a..eacfb52 100644 --- a/src/openvpn/multi.h +++ b/src/openvpn/multi.h @@ -38,6 +38,7 @@ #include "pool.h" #include "mudp.h" #include "mtcp.h" +#include "multi_io.h" #include "perf.h" #include "vlan.h" #include "reflect_filter.h" @@ -174,8 +175,7 @@ struct mbuf_set *mbuf; /**< Set of buffers for passing data * channel packets between VPN tunnel * instances. */ - struct multi_tcp *mtcp; /**< State specific to OpenVPN using TCP - * as external transport. */ + struct multi_io *multi_io; /**< I/O state and events tracker */ struct ifconfig_pool *ifconfig_pool; struct frequency_limit *new_connection_limiter; struct initial_packet_rate_limit *initial_rate_limiter; diff --git a/src/openvpn/multi_io.c b/src/openvpn/multi_io.c new file mode 100644 index 0000000..e4174dd --- /dev/null +++ b/src/openvpn/multi_io.c @@ -0,0 +1,632 @@ +/* + * 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. + * + * Copyright (C) 2002-2023 OpenVPN Inc + * + * 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. + * + * 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. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "syshead.h" + +#include "memdbg.h" + +#include "multi.h" +#include "forward.h" +#include "multi_io.h" + +#ifdef HAVE_SYS_INOTIFY_H +#include +#endif + +/* + * Special tags passed to event.[ch] functions + */ +#define MULTI_IO_SOCKET ((void *)1) +#define MULTI_IO_TUN ((void *)2) +#define MULTI_IO_SIG ((void *)3) /* Only on Windows */ +#define MULTI_IO_MANAGEMENT ((void *)4) +#define MULTI_IO_FILE_CLOSE_WRITE ((void *)5) +#define MULTI_IO_DCO ((void *)6) + +struct ta_iow_flags +{ + unsigned int flags; + unsigned int ret; + unsigned int tun; + unsigned int sock; +}; + +#ifdef ENABLE_DEBUG +static const char * +pract(int action) +{ + switch (action) + { + case TA_UNDEF: + return "TA_UNDEF"; + + case TA_SOCKET_READ: + return "TA_SOCKET_READ"; + + case TA_SOCKET_READ_RESIDUAL: + return "TA_SOCKET_READ_RESIDUAL"; + + case TA_SOCKET_WRITE: + return "TA_SOCKET_WRITE"; + + case TA_SOCKET_WRITE_READY: + return "TA_SOCKET_WRITE_READY"; + + case TA_SOCKET_WRITE_DEFERRED: + return "TA_SOCKET_WRITE_DEFERRED"; + + case TA_TUN_READ: + return "TA_TUN_READ"; + + case TA_TUN_WRITE: + return "TA_TUN_WRITE"; + + case TA_INITIAL: + return "TA_INITIAL"; + + case TA_TIMEOUT: + return "TA_TIMEOUT"; + + case TA_TUN_WRITE_TIMEOUT: + return "TA_TUN_WRITE_TIMEOUT"; + + default: + return "?"; + } +} +#endif /* ENABLE_DEBUG */ + +static inline struct context * +multi_get_context(struct multi_context *m, struct multi_instance *mi) +{ + if (mi) + { + return &mi->context; + } + else + { + return &m->top; + } +} + +struct multi_io * +multi_io_init(int maxevents, int *maxclients) +{ + struct multi_io *multi_io; + const int extra_events =3D BASE_N_EVENTS; + + ASSERT(maxevents >=3D 1); + ASSERT(maxclients); + + ALLOC_OBJ_CLEAR(multi_io, struct multi_io); + multi_io->maxevents =3D maxevents + extra_events; + multi_io->es =3D event_set_init(&multi_io->maxevents, 0); + wait_signal(multi_io->es, MULTI_IO_SIG); + ALLOC_ARRAY(multi_io->esr, struct event_set_return, multi_io->maxevent= s); + *maxclients =3D max_int(min_int(multi_io->maxevents - extra_events, *m= axclients), 1); + msg(D_MULTI_LOW, "MULTI IO: MULTI_IO INIT maxclients=3D%d maxevents=3D= %d", *maxclients, multi_io->maxevents); + return multi_io; +} + +void +multi_io_free(struct multi_io *multi_io) +{ + if (multi_io) + { + event_free(multi_io->es); + free(multi_io->esr); + free(multi_io); + } +} + +int +multi_io_wait(struct multi_context *m) +{ + int status, i; + unsigned int *persistent =3D &m->multi_io->tun_rwflags; + + for (i =3D 0; i < m->top.c1.link_sockets_num; i++) + { + socket_set_listen_persistent(m->top.c2.link_sockets[i], m->multi_i= o->es, + &m->top.c2.link_sockets[i]->ev_arg); + } + +#ifdef _WIN32 + if (tuntap_is_wintun(m->top.c1.tuntap)) + { + if (!tuntap_ring_empty(m->top.c1.tuntap)) + { + /* there is data in wintun ring buffer, read it immediately */ + m->multi_io->esr[0].arg =3D MULTI_IO_TUN; + m->multi_io->esr[0].rwflags =3D EVENT_READ; + m->multi_io->n_esr =3D 1; + return 1; + } + persistent =3D NULL; + } +#endif + tun_set(m->top.c1.tuntap, m->multi_io->es, EVENT_READ, MULTI_IO_TUN, p= ersistent); +#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) + dco_event_set(&m->top.c1.tuntap->dco, m->multi_io->es, MULTI_IO_DCO); +#endif + +#ifdef ENABLE_MANAGEMENT + if (management) + { + management_socket_set(management, m->multi_io->es, MULTI_IO_MANAGE= MENT, &m->multi_io->management_persist_flags); + } +#endif + +#ifdef ENABLE_ASYNC_PUSH + /* arm inotify watcher */ + event_ctl(m->multi_io->es, m->top.c2.inotify_fd, EVENT_READ, MULTI_IO_= FILE_CLOSE_WRITE); +#endif + + status =3D event_wait(m->multi_io->es, &m->top.c2.timeval, m->multi_io= ->esr, m->multi_io->maxevents); + update_time(); + m->multi_io->n_esr =3D 0; + if (status > 0) + { + m->multi_io->n_esr =3D status; + } + return status; +} + +static int +multi_io_wait_lite(struct multi_context *m, struct multi_instance *mi, con= st int action, bool *tun_input_pending) +{ + struct context *c =3D multi_get_context(m, mi); + unsigned int looking_for =3D 0; + + dmsg(D_MULTI_DEBUG, "MULTI IO: multi_io_wait_lite a=3D%s mi=3D" ptr_fo= rmat, + pract(action), + (ptr_type)mi); + + tv_clear(&c->c2.timeval); /* ZERO-TIMEOUT */ + + switch (action) + { + case TA_TUN_READ: + looking_for =3D TUN_READ; + tun_input_pending =3D NULL; + io_wait(c, IOW_READ_TUN); + break; + + case TA_SOCKET_READ: + looking_for =3D SOCKET_READ; + tun_input_pending =3D NULL; + io_wait(c, IOW_READ_LINK); + break; + + case TA_TUN_WRITE: + looking_for =3D TUN_WRITE; + tun_input_pending =3D NULL; + c->c2.timeval.tv_sec =3D 1; /* For some reason, the Linux 2.2 = TUN/TAP driver hits this timeout */ + perf_push(PERF_PROC_OUT_TUN_MTCP); + io_wait(c, IOW_TO_TUN); + perf_pop(); + break; + + case TA_SOCKET_WRITE: + looking_for =3D SOCKET_WRITE; + io_wait(c, IOW_TO_LINK|IOW_READ_TUN_FORCE); + break; + + default: + msg(M_FATAL, "MULTI IO: multi_io_wait_lite, unhandled action= =3D%d", action); + } + + if (tun_input_pending && (c->c2.event_set_status & TUN_READ)) + { + *tun_input_pending =3D true; + } + + if (c->c2.event_set_status & looking_for) + { + return action; + } + else + { + switch (action) + { + /* MULTI PROTOCOL socket output buffer is full */ + case TA_SOCKET_WRITE: + return TA_SOCKET_WRITE_DEFERRED; + + /* TUN device timed out on accepting write */ + case TA_TUN_WRITE: + return TA_TUN_WRITE_TIMEOUT; + } + + return TA_UNDEF; + } +} + +static struct multi_instance * +multi_io_dispatch(struct multi_context *m, struct multi_instance *mi, cons= t int action) +{ + const unsigned int mpp_flags =3D MPP_PRE_SELECT|MPP_RECORD_TOUCH; + struct multi_instance *touched =3D mi; + m->mpp_touched =3D &touched; + + dmsg(D_MULTI_DEBUG, "MULTI IO: multi_io_dispatch a=3D%s mi=3D" ptr_for= mat, + pract(action), + (ptr_type)mi); + + switch (action) + { + case TA_TUN_READ: + read_incoming_tun(&m->top); + if (!IS_SIG(&m->top)) + { + multi_process_incoming_tun(m, mpp_flags); + } + break; + + case TA_SOCKET_READ: + case TA_SOCKET_READ_RESIDUAL: + ASSERT(mi); + ASSERT(mi->context.c2.link_sockets); + ASSERT(mi->context.c2.link_sockets[0]); + set_prefix(mi); + read_incoming_link(&mi->context, mi->context.c2.link_sockets[0= ]); + clear_prefix(); + if (!IS_SIG(&mi->context)) + { + multi_process_incoming_link(m, mi, mpp_flags, + mi->context.c2.link_sockets[0]= ); + if (!IS_SIG(&mi->context)) + { + stream_buf_read_setup(mi->context.c2.link_sockets[0]); + } + } + break; + + case TA_TIMEOUT: + multi_process_timeout(m, mpp_flags); + break; + + case TA_TUN_WRITE: + multi_process_outgoing_tun(m, mpp_flags); + break; + + case TA_TUN_WRITE_TIMEOUT: + multi_process_drop_outgoing_tun(m, mpp_flags); + break; + + case TA_SOCKET_WRITE_READY: + ASSERT(mi); + multi_tcp_process_outgoing_link_ready(m, mi, mpp_flags); + break; + + case TA_SOCKET_WRITE: + multi_tcp_process_outgoing_link(m, false, mpp_flags); + break; + + case TA_SOCKET_WRITE_DEFERRED: + multi_tcp_process_outgoing_link(m, true, mpp_flags); + break; + + case TA_INITIAL: + ASSERT(mi); + multi_tcp_set_global_rw_flags(m, mi); + multi_process_post(m, mi, mpp_flags); + break; + + default: + msg(M_FATAL, "MULTI IO: multi_io_dispatch, unhandled action=3D= %d", action); + } + + m->mpp_touched =3D NULL; + return touched; +} + +static int +multi_io_post(struct multi_context *m, struct multi_instance *mi, const in= t action) +{ + struct context *c =3D multi_get_context(m, mi); + int newaction =3D TA_UNDEF; + +#define MTP_NONE 0 +#define MTP_TUN_OUT (1<<0) +#define MTP_LINK_OUT (1<<1) + unsigned int flags =3D MTP_NONE; + + if (TUN_OUT(c)) + { + flags |=3D MTP_TUN_OUT; + } + if (LINK_OUT(c)) + { + flags |=3D MTP_LINK_OUT; + } + + switch (flags) + { + case MTP_TUN_OUT|MTP_LINK_OUT: + case MTP_TUN_OUT: + newaction =3D TA_TUN_WRITE; + break; + + case MTP_LINK_OUT: + newaction =3D TA_SOCKET_WRITE; + break; + + case MTP_NONE: + if (mi && sockets_read_residual(c)) + { + newaction =3D TA_SOCKET_READ_RESIDUAL; + } + else + { + multi_tcp_set_global_rw_flags(m, mi); + } + break; + + default: + { + struct gc_arena gc =3D gc_new(); + msg(M_FATAL, "MULTI IO: multi_io_post bad state, mi=3D%s flags= =3D%d", + multi_instance_string(mi, false, &gc), + flags); + gc_free(&gc); + break; + } + } + + dmsg(D_MULTI_DEBUG, "MULTI IO: multi_io_post %s -> %s", + pract(action), + pract(newaction)); + + return newaction; +} + +void +multi_io_process_io(struct multi_context *m) +{ + struct multi_io *multi_io =3D m->multi_io; + int i; + + for (i =3D 0; i < multi_io->n_esr; ++i) + { + struct event_set_return *e =3D &multi_io->esr[i]; + struct event_arg *ev_arg =3D (struct event_arg *)e->arg; + + /* incoming data for instance or listening socket? */ + if (e->arg >=3D MULTI_N) + { + switch (ev_arg->type) + { + struct multi_instance *mi; + + /* react to event on child instance */ + case EVENT_ARG_MULTI_INSTANCE: + if (!ev_arg->u.mi) + { + msg(D_MULTI_ERRORS, "MULTI IO: multi_io_proc_io: n= ull minstance"); + break; + } + + mi =3D ev_arg->u.mi; + if (e->rwflags & EVENT_WRITE) + { + multi_io_action(m, mi, TA_SOCKET_WRITE_READY, fals= e); + } + else if (e->rwflags & EVENT_READ) + { + multi_io_action(m, mi, TA_SOCKET_READ, false); + } + break; + + /* new incoming TCP client attempting to connect? */ + case EVENT_ARG_LINK_SOCKET: + if (!ev_arg->u.sock) + { + msg(D_MULTI_ERRORS, "MULTI IO: multi_io_proc_io: n= ull socket"); + break; + } + + if (!proto_is_dgram(ev_arg->u.sock->info.proto)) + { + socket_reset_listen_persistent(ev_arg->u.sock); + mi =3D multi_create_instance_tcp(m, ev_arg->u.sock= ); + if (mi) + { + multi_io_action(m, mi, TA_INITIAL, false); + } + break; + } + } + } + else + { +#ifdef ENABLE_MANAGEMENT + if (e->arg =3D=3D MULTI_IO_MANAGEMENT) + { + ASSERT(management); + management_io(management); + } + else +#endif + /* incoming data on TUN? */ + if (e->arg =3D=3D MULTI_IO_TUN) + { + if (e->rwflags & EVENT_WRITE) + { + multi_io_action(m, NULL, TA_TUN_WRITE, false); + } + else if (e->rwflags & EVENT_READ) + { + multi_io_action(m, NULL, TA_TUN_READ, false); + } + } + /* new incoming TCP client attempting to connect? */ + else if (e->arg =3D=3D MULTI_IO_SOCKET) + { + struct multi_instance *mi; + ASSERT(m->top.c2.link_sockets[0]); + socket_reset_listen_persistent(m->top.c2.link_sockets[0]); + mi =3D multi_create_instance_tcp(m, m->top.c2.link_sockets= [0]); + if (mi) + { + multi_io_action(m, mi, TA_INITIAL, false); + } + } +#if defined(ENABLE_DCO) && (defined(TARGET_LINUX) || defined(TARGET_FREEBS= D)) + /* incoming data on DCO? */ + else if (e->arg =3D=3D MULTI_IO_DCO) + { + multi_process_incoming_dco(m); + } +#endif + /* signal received? */ + else if (e->arg =3D=3D MULTI_IO_SIG) + { + get_signal(&m->top.sig->signal_received); + } +#ifdef ENABLE_ASYNC_PUSH + else if (e->arg =3D=3D MULTI_IO_FILE_CLOSE_WRITE) + { + multi_process_file_closed(m, MPP_PRE_SELECT | MPP_RECORD_T= OUCH); + } +#endif + } + if (IS_SIG(&m->top)) + { + break; + } + } + multi_io->n_esr =3D 0; + + /* + * Process queued mbuf packets destined for TCP socket + */ + { + struct multi_instance *mi; + while (!IS_SIG(&m->top) && (mi =3D mbuf_peek(m->mbuf)) !=3D NULL) + { + multi_io_action(m, mi, TA_SOCKET_WRITE, true); + } + } +} + +void +multi_io_action(struct multi_context *m, struct multi_instance *mi, int ac= tion, bool poll) +{ + bool tun_input_pending =3D false; + + do + { + dmsg(D_MULTI_DEBUG, "MULTI IO: multi_io_action a=3D%s p=3D%d", + pract(action), + poll); + + /* + * If TA_SOCKET_READ_RESIDUAL, it means we still have pending + * input packets which were read by a prior recv. + * + * Otherwise do a "lite" wait, which means we wait with 0 timeout + * on I/O events only related to the current instance, not + * the big list of events. + * + * On our first pass, poll will be false because we already know + * that input is available, and to call io_wait would be redundant. + */ + if (poll && action !=3D TA_SOCKET_READ_RESIDUAL) + { + const int orig_action =3D action; + action =3D multi_io_wait_lite(m, mi, action, &tun_input_pendin= g); + if (action =3D=3D TA_UNDEF) + { + msg(M_FATAL, "MULTI IO: I/O wait required blocking in mult= i_io_action, action=3D%d", orig_action); + } + } + + /* + * Dispatch the action + */ + struct multi_instance *touched =3D multi_io_dispatch(m, mi, action= ); + + /* + * Signal received or connection + * reset by peer? + */ + if (touched && IS_SIG(&touched->context)) + { + if (mi =3D=3D touched) + { + mi =3D NULL; + } + multi_close_instance_on_signal(m, touched); + } + + + /* + * If dispatch produced any pending output + * for a particular instance, point to + * that instance. + */ + if (m->pending) + { + mi =3D m->pending; + } + + /* + * Based on the effects of the action, + * such as generating pending output, + * possibly transition to a new action state. + */ + action =3D multi_io_post(m, mi, action); + + /* + * If we are finished processing the original action, + * check if we have any TUN input. If so, transition + * our action state to processing this input. + */ + if (tun_input_pending && action =3D=3D TA_UNDEF) + { + action =3D TA_TUN_READ; + mi =3D NULL; + tun_input_pending =3D false; + poll =3D false; + } + else + { + poll =3D true; + } + + } while (action !=3D TA_UNDEF); +} + +void +multi_io_delete_event(struct multi_io *multi_io, event_t event) +{ + if (multi_io && multi_io->es) + { + event_del(multi_io->es, event); + } +} diff --git a/src/openvpn/multi_io.h b/src/openvpn/multi_io.h new file mode 100644 index 0000000..03d708c --- /dev/null +++ b/src/openvpn/multi_io.h @@ -0,0 +1,77 @@ +/* + * 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. + * + * Copyright (C) 2002-2023 OpenVPN Inc + * + * 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. + * + * 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. + */ + +/* + * Multi-protocol specific code for --mode server + */ + +#ifndef MULTI_IO_H +#define MULTI_IO_H + +#include "event.h" + +/* + * I/O processing States + */ + +#define TA_UNDEF 0 +#define TA_SOCKET_READ 1 +#define TA_SOCKET_READ_RESIDUAL 2 +#define TA_SOCKET_WRITE 3 +#define TA_SOCKET_WRITE_READY 4 +#define TA_SOCKET_WRITE_DEFERRED 5 +#define TA_TUN_READ 6 +#define TA_TUN_WRITE 7 +#define TA_INITIAL 8 +#define TA_TIMEOUT 9 +#define TA_TUN_WRITE_TIMEOUT 10 + +/* + * I/O state and events tracker + */ +struct multi_io +{ + struct event_set *es; + struct event_set_return *esr; + int n_esr; + int maxevents; + unsigned int tun_rwflags; + unsigned int udp_flags; +#ifdef ENABLE_MANAGEMENT + unsigned int management_persist_flags; +#endif +}; + +struct multi_io *multi_io_init(int maxevents, int *maxclients); + +void multi_io_free(struct multi_io *multi_io); + +int multi_io_wait(struct multi_context *m); + +void multi_io_process_io(struct multi_context *m); + +void multi_io_action(struct multi_context *m, struct multi_instance *mi, i= nt action, bool poll); + +void multi_io_delete_event(struct multi_io *multi_io, event_t event); + +#endif /* ifndef MULTI_IO_H */ -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/763?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I1e5a84969988e4f027a18658d4ab268c13fbf929 Gerrit-Change-Number: 763 Gerrit-PatchSet: 14 Gerrit-Owner: its_Giaan Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: its_Giaan Gerrit-Attention: flichtenheld Gerrit-MessageType: newpatchset --GyKRPZwAe+0= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld, its_Giaan, plaistho= s.

its_Giaan uploaded patch set #14 to this change.

= View Chan= ge

multiproto: move generic event handling code in dedicated files
<= br> Introduced multi_io.h and multi_io.c files to centralize
all = codes related to multiple protocols.

Renamed the struct mtcp to = struct multi_io since it
encompasses the event_set used by the paren= t context in server mode.

Several methods have also been renamed= and moved to fit the
multiproto structure:

- multi_tcp_i= nit() -> multi_io_init();
- multi_tcp_free() -> multi_io_free(= );
- multi_tcp_wait() -> multi_io_wait();
and so forth.
Change-Id: I1e5a84969988e4f027a18658d4ab268c13fbf929
Signed-off-by:= Gianmarco De Gregori <gianmarco@...2726...>
---
M CMakeLists.t= xt
M src/openvpn/Makefile.am
M src/openvpn/forward.h
M src/openvpn= /mtcp.c
M src/openvpn/mtcp.h
M src/openvpn/multi.c
M src/openvpn/m= ulti.h
A src/openvpn/multi_io.c
A src/openvpn/multi_io.h
9 files c= hanged, 751 insertions(+), 620 deletions(-)

git pull ssh://gerrit=
.openvpn.net:29418/openvpn refs/changes/63/763/14
diff --git a/CMak=
eLists.txt b/CMakeLists.txt
index 9ffcc89..ea8d006 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -458,6 +458,8 @@
src/openvpn/mudp.h
src/openvpn/multi.c
src/openvpn/mu= lti.h
+ src/openvp= n/multi_io.h
+ src= /openvpn/multi_io.c
src/openvpn/ntlm.c
src/openvpn/ntlm.h

src/openvpn/occ.c
<= span>diff --git a/src/openvpn/Makefile.am b/src/openvpn/Makefile.am
<= br>index d6d6592..37af683 100644
--- a/src/openvpn/Ma= kefile.am
+++ b/src/openvpn/Makefile.am
@@ = -93,6 +93,7 @@
mtu.c mtu.h \
mudp.c mud= p.h \
multi.c multi.h \
+ multi_io.c multi_io.h \
networking= _freebsd.c \
networking_iproute2.c networking_iproute2.h = \
networking_sitnl.c networking_sitnl.h \
diff --git a/src/openvpn/forward.h b/src/openvpn/forward.h

index ca2a695..214a322 100644
--- a/src/openvpn/forward.h<= /span>
+++ b/src/openvpn/forward.h
@@ -50,6 +50,7 = @@
#include "openvpn.h"
#includ= e "occ.h"
#include "ping.h"
= +#include "multi_io.h"= ;

#define IOW_TO_TUN (1<<= 0)
#define IOW_TO_LINK (1<<1)
diff --git a/src/openvpn/mtcp.c b/src/openvpn/mtcp.c

inde= x b5bbf13..6d1d5a0 100644
--- a/src/openvpn/mtcp.c+++ b/src/openvpn/mtcp.c
@@ -29,6 +29,8 @@
#include "multi.h"
#i= nclude "forward.h"
+#include "mtcp.h"
+#include "multi_io.h"
#include "memdbg.h"

@@= -36,30 +38,6 @@
#include <sys/inotify.h>
= #endif

-/*
- * TCP St= ates
- */
-#define TA_UNDEF 0<= /span>
-#define TA_SOCKET_READ= 1
-#define T= A_SOCKET_READ_RESIDUAL 2
-#define TA_SOCKET_WRITE 3
-#define TA_SOCKET_WRITE_READY 4
-#define TA_SOCKET_WRITE_DEFERRED 5-#define TA_TUN_READ = 6
-#define TA_TUN_WR= ITE 7
-#def= ine TA_INITIAL 8
-#define TA_TIMEOUT 9
-#define TA_TUN_WRITE_TIMEOUT 10
-
-/*
- * S= pecial tags passed to event.[ch] functions
- */
-#define MTCP_SIG ((void *)3) /* Only on Windows *= /
-#define MTCP_MANAGEM= ENT ((void *)4)
-#defin= e MTCP_FILE_CLOSE_WRITE ((void *)5)
-#define MTCP_DCO ((void *)6)
-
struct ta_iow_flags {
unsigned int flags;
@@ -68,5= 2 +46,7 @@
unsigned int sock;
};

-#ifdef E= NABLE_DEBUG
-static con= st char *
-pract(int ac= tion)
-{
- switch (action)

- {
- case TA_UNDEF:
- return "TA_UNDEF";
-
- case TA_SOCKET_READ:
- return "TA_SOCKET_READ";
-
- case TA_SOCKET_READ_RESIDUAL:

- return "TA= _SOCKET_READ_RESIDUAL";
-
- case T= A_SOCKET_WRITE:
- = return "TA_SOCKET_WRITE";
-
- = case TA_SOCKET_WRITE_READY:
- return "TA_SOCKET_WRITE_READY";-
- case TA_SOCKET_WRITE_DEFERRED:
- return "TA_SOCKET= _WRITE_DEFERRED";
= -
- case TA_TUN_= READ:
- retu= rn "TA_TUN_READ";
-
- case TA= _TUN_WRITE:
- = return "TA_TUN_WRITE";
-
- = case TA_INITIAL:
- = return "TA_INITIAL";
-
- = case TA_TIMEOUT:
- = return "TA_TIMEOUT";
-
- = case TA_TUN_WRITE_TIMEOUT:
- return "TA_TUN_WRITE_TIMEOUT";
<= span style=3D"color: hsl(0, 100%, 40%);">-
- default:
- return "?";
- }
-}
-#endif /= * ENABLE_DEBUG */
-
-static struct multi_insta= nce *
+struct multi_i= nstance *
multi_create_instance_tcp(struct multi_context *= m, struct link_socket *ls)
{
struct g= c_arena gc =3D gc_new();
@@ -193,139 +126,42 @@
<= span> mbuf_free(mi->tcp_link_out_deferred);

}

-struct mu= lti_tcp *
-multi_tcp_in= it(int maxevents, int *maxclients)
-{
- str= uct multi_tcp *mtcp;
- = const int extra_events =3D BASE_N_EVENTS;
-
- ASSERT(maxevents >=3D 1);
- ASSERT(maxclients);
-
- AL= LOC_OBJ_CLEAR(mtcp, struct multi_tcp);
- mtcp->maxevents =3D maxevents + extra_events;
- mtcp->es =3D event_s= et_init(&mtcp->maxevents, 0);
- wait_signal(mtcp->es, MTCP_SIG);
- ALLOC_ARRAY(mtcp->esr, struct event= _set_return, mtcp->maxevents);
- *maxclients =3D max_int(min_int(mtcp->maxevents - extra_= events, *maxclients), 1);
- msg(D_MULTI_LOW, "MULTI: TCP INIT maxclients=3D%d maxevents=3D= %d", *maxclients, mtcp->maxevents);
- return mtcp;
-}
-

void
-mu= lti_tcp_delete_event(struct multi_tcp *mtcp, event_t event)
+multi_tcp_delete_event(struct multi= _io *multi_io, event_t event)
{
- if (mtcp && mtcp->es)
<= span style=3D"color: hsl(120, 100%, 40%);">+ if (multi_io && mul= ti_io->es)
{
- event_del(mtcp->es, event);
+ event_del(multi_io->es, event)= ;
}
}

void
-multi_tcp_fre= e(struct multi_tcp *mtcp)
-{
- if (mtcp)
- {
- event_free(mtcp->es);<= br>- free(mtcp->esr);
- free(mtcp);

- }
-}
-
-void
-multi_tcp_dereference_in= stance(struct multi_tcp *mtcp, struct multi_instance *mi)
+multi_tcp_dereference_instance(struct= multi_io *multi_io, struct multi_instance *mi)
{ struct link_socket *ls =3D mi->context.c2.link_sockets[0];<= /span>
if (ls && mi->socket_set_called)
= {
- e= vent_del(mtcp->es, socket_event_handle(ls));
+ event_del(multi_io->es, socket_event= _handle(ls));
mi->socket_set_called =3D false;<= /span>
}
= - mtcp->n_esr =3D 0;
+ multi_io->n_esr =3D 0;
}

-static inline void
+void
mu= lti_tcp_set_global_rw_flags(struct multi_context *m, struct multi_instance = *mi)
{
if (mi)
{=
mi->socket_set_called =3D true;
socket_set(mi->context.c2.link_sockets[0],

- m->mtcp->es,
+ m-&= gt;multi_io->es,
mbuf_defined(mi->= ;tcp_link_out_deferred) ? EVENT_WRITE : EVENT_READ,
= &mi->ev_arg,
&m= i->tcp_rwflags);
}
}

-static inline int=
-multi_tcp_wait(const = struct context *c,
- = struct multi_tcp *mtcp)
-{
- in= t status;
- unsigned= int *persistent =3D &mtcp->tun_rwflags;
-
- for (int i =3D 0; i < c->c1.link_sockets_num; i++)- {
- socket_set_listen_persistent(c->c2.l= ink_sockets[i], mtcp->es,
- &c->c2.link_sockets[i]-&= gt;ev_arg);
- }
-
-#ifdef _WIN32
- if (tuntap_is_wintun(c->c1.tuntap))
- {

- if (!tuntap_ring_empty(c->c1.tuntap))
- {
- /* there is data in win= tun ring buffer, read it immediately */

- mtcp->esr[0].arg =3D MTCP_TUN;
<= span style=3D"color: hsl(0, 100%, 40%);">- mtcp->esr[0].rwfla= gs =3D EVENT_READ;

- = mtcp->n_esr =3D 1;
- return 1;
- }
- = persistent =3D NULL;
- }
-#endif

- tun_set(c->c1.tuntap= , mtcp->es, EVENT_READ, MTCP_TUN, persistent);
-#if defined(TARGET_LINUX) || defined(TARGET_FRE= EBSD)
- dco_event_se= t(&c->c1.tuntap->dco, mtcp->es, MTCP_DCO);
-#endif
-
-#i= fdef ENABLE_MANAGEMENT
= - if (management)
- = {
- managemen= t_socket_set(management, mtcp->es, MTCP_MANAGEMENT, &mtcp->manage= ment_persist_flags);
- = }
-#endif
= -
-#ifdef ENABLE_ASYNC_PUSH
- /* arm inotify watcher */
- event_ctl(mtcp->es, c->c2.inotif= y_fd, EVENT_READ, MTCP_FILE_CLOSE_WRITE);
-#endif
-
- status =3D e= vent_wait(mtcp->es, &c->c2.timeval, mtcp->esr, mtcp->maxeve= nts);
- update_time(= );
- mtcp->n_esr = =3D 0;
- if (status = > 0)
- {- mtcp->n_esr =3D sta= tus;
- }
<= span style=3D"color: hsl(0, 100%, 40%);">- return status;
-}
-
-st= atic inline struct context *
-multi_tcp_context(struct multi_context *m, struct multi_instance *mi= )
-{
- if (mi)
- {
- return &mi->context;
- }
- else
- {<= /span>
- return &m-= >top;
- }<= br>-}
-
-static bool
+boo= l
multi_tcp_process_outgoing_link_ready(struct multi_conte= xt *m, struct multi_instance *mi, const unsigned int mpp_flags)
<= span> {

struct mbuf_item item;
@@ -349= ,7 +185,7 @@
return ret;
}
=
-static bool
+bool
= multi_tcp_process_outgoing_link(struct multi_context *m, bool defer, const = unsigned int mpp_flags)
{
struct mult= i_instance *mi =3D multi_process_outgoing_link_pre(m);
@@ -= 393,417 +229,6 @@
return ret;
}

-static in= t
-multi_tcp_wait_lite(= struct multi_context *m, struct multi_instance *mi, const int action, bool = *tun_input_pending)
-{<= /span>
- struct context *c = =3D multi_tcp_context(m, mi);
- unsigned int looking_for =3D 0;
-
= - dmsg(D_MULTI_DEBUG, "MULTI TCP: multi_tcp_wait_lite a=3D%s mi=3D&= quot; ptr_format,
- = pract(action),
- = (ptr_type)mi);
-=
- tv_clear(&c-&= gt;c2.timeval); /* ZERO-TIMEOUT */
-
- swit= ch (action)
- {

- case TA_TUN_READ:<= /span>
- looking_fo= r =3D TUN_READ;
- = tun_input_pending =3D NULL;
- io_wait(c, IOW_READ_TUN);
- break;
-
- case TA_SOCKET_READ:
- looking_for =3D SOCKET_READ;
- tun_input_pending =3D NULL;
- io_wait(c, IO= W_READ_LINK);
- = break;
-
= - case TA_TUN_WRITE:
- looking_for =3D= TUN_WRITE;
- = tun_input_pending =3D NULL;
- c->c2.timeval.tv_sec =3D 1; /* For some reason, the = Linux 2.2 TUN/TAP driver hits this timeout */
- perf_push(PERF_PROC_OUT_TUN_MTCP);
- io_wait(c, IOW= _TO_TUN);
- = perf_pop();
- = break;
-
- case TA_SOCKET_WRITE:
- looking_for = =3D SOCKET_WRITE;
- = io_wait(c, IOW_TO_LINK|IOW_READ_TUN_FORCE);
- break;
-
- default:
- }
-- if (tun_input_pending &= ;& (c->c2.event_set_status & TUN_READ))
- {
- *tun_input_pending =3D true;
- }
-
- if (c= ->c2.event_set_status & looking_for)
- {
- return action;
- }
- else<= /span>
- {
- switch (action)
- {

- /* TCP socket output buffer is full = */
- case TA= _SOCKET_WRITE:
- = return TA_SOCKET_WRITE_DEFERRED;
-
- = /* TUN device timed out on accepting write */
- case TA_TUN_WRITE:- return TA_TUN_W= RITE_TIMEOUT;
- = }
-
- return TA_UNDEF;
- }
-}
= -
-static struct multi_= instance *
-multi_tcp_d= ispatch(struct multi_context *m, struct multi_instance *mi, const int actio= n)
-{
- const unsigned int mpp_flags =3D MP= P_PRE_SELECT|MPP_RECORD_TOUCH;
- struct multi_instance *touched =3D mi;
- m->mpp_touched =3D &touched;
-
- dmsg(D_MULTI_DEBUG, "MULTI TCP: multi= _tcp_dispatch a=3D%s mi=3D" ptr_format,
- pract(action),
- (ptr_type)mi);
-
- switch (action)
- {
- case= TA_TUN_READ:
- = read_incoming_tun(&m->top);
- if (!IS_SIG(&m->top))
- {
- multi_process_incoming_tun(m, m= pp_flags);
- = }
- break;<= /span>
-
- case TA_SOCKET_READ:
- case TA_SOCKET_READ_RESIDUAL= :

- ASSERT(m= i);
- ASSERT= (mi->context.c2.link_sockets);
- ASSERT(mi->context.c2.link_sockets[0]);- set_prefix(mi);

- read_incomin= g_link(&mi->context, mi->context.c2.link_sockets[0]);
<= span style=3D"color: hsl(0, 100%, 40%);">- clear_prefix();
- if (!IS_SIG(&am= p;mi->context))
- = {
- = multi_process_incoming_link(m, mi, mpp_flags,
- mi= ->context.c2.link_sockets[0]);
- if (!IS_SIG(&mi->context))
- {

- stream_buf_read_set= up(mi->context.c2.link_sockets[0]);
- }
- }
- break;
-
- case TA_TIM= EOUT:
- mult= i_process_timeout(m, mpp_flags);
- break;
-
- case = TA_TUN_WRITE:
- = multi_process_outgoing_tun(m, mpp_flags);
- break;
-
-= case TA_TUN_WRITE_TIMEOUT:
- multi_process_drop_outgoing_tun(m, mpp_flags);
- break;=
-
- case TA_SOCKET_WRITE_READY:
- ASSERT(mi);

<= span style=3D"color: hsl(0, 100%, 40%);">- multi_tcp_process_out= going_link_ready(m, mi, mpp_flags);

- break;
-
- ca= se TA_SOCKET_WRITE:
- = multi_tcp_process_outgoing_link(m, false, mpp_flags);
<= span style=3D"color: hsl(0, 100%, 40%);">- break;

-

- case TA_SOCKET_WRITE_DEFERRED:
- multi_tcp_process_outgoing_l= ink(m, true, mpp_flags);
-=
- case TA_INITI= AL:
- ASSERT= (mi);
- mult= i_tcp_set_global_rw_flags(m, mi);
- multi_process_post(m, mi, mpp_flags);
- break;
-
- default:
- msg(M_FATAL, "MULTI TCP: multi_tcp_dispatch, un= handled action=3D%d", action);
- }
-
- m->mpp_touched = =3D NULL;
- return t= ouched;
-}
-

-static int
-multi_tcp_post(struct multi_context *m, struct multi_instance *mi,= const int action)
-{
- struct context *c = =3D multi_tcp_context(m, mi);
- int newaction =3D TA_UNDEF;
-
-#de= fine MTP_NONE 0
-#define MTP_TUN_OUT (1<<0)
-#define MTP_LINK_OUT (1<<1)
- unsigned int flags =3D MTP_NONE;
-
- if (TUN_OUT(c))
- {
- flags |=3D MTP_TUN_OUT;
- }
- if (LINK_OUT(c))
- flags= |=3D MTP_LINK_OUT;
- = }
-
- switch (flags)
- {
- case MTP_TUN_OUT|MTP_LINK_OUT:
- case MTP_TUN_OUT:
- newaction =3D TA_TUN_WRIT= E;

- break;<= /span>
-
- case MTP_LINK_OUT:
- newaction =3D TA_SOCKET_WR= ITE;
- break= ;
-
- case MTP_NONE:
- if (mi && sockets_re= ad_residual(c))
- = {
- = newaction =3D TA_SOCKET_READ_RESIDUAL;
- }
- else
- {
- = multi_tcp_set_global_rw_flags(m, mi);
- }
- break;
-
-= default:
- = {
- struc= t gc_arena gc =3D gc_new();
- msg(M_FATAL, "MULTI TCP: multi_tcp_post bad state, m= i=3D%s flags=3D%d",
- flags);
<= span style=3D"color: hsl(0, 100%, 40%);">- gc_free(&gc);

- break;=
- }
- }
-
- = dmsg(D_MULTI_DEBUG, "MULTI TCP: multi_tcp_post %s -> %s",
- pract(action),<= /span>
- pract(newacti= on));
-
- return newaction;

-}

-
-sta= tic void
-multi_tcp_act= ion(struct multi_context *m, struct multi_instance *mi, int action, bool po= ll)
-{
- bool tun_input_pending =3D false;<= /span>
-
- do
- {
= - dmsg(D_MULTI_DEBUG, "MULTI TCP: multi_tcp_action a=3D%s p=3D%= d",
- = pract(action),
- = poll);
-- /*
- * If TA_SOCKET_READ_RESIDUAL, it= means we still have pending
- * input packets which were read by a prior TCP recv.=
- *
- * Otherwise do a "lite&qu= ot; wait, which means we wait with 0 timeout
- * on I/O events only related to the current= instance, not
- = * the big list of events.
- *
- = * On our first pass, poll will be false because we already know<= br>- * that input is avai= lable, and to call io_wait would be redundant.
- */
- if (poll && action !=3D TA_SOCKET_READ_RESID= UAL)
- {<= br>- const int orig_ac= tion =3D action;
- = action =3D multi_tcp_wait_lite(m, mi, action, &tun_input_pending= );
- if (act= ion =3D=3D TA_UNDEF)
- = {
- = msg(M_FATAL, "MULTI TCP: I/O wait required blocking in multi_tc= p_action, action=3D%d", orig_action);
- }
- }
= -
- /*- * Dispatch the action<= /span>
- */
= - struct multi_instance *t= ouched =3D multi_tcp_dispatch(m, mi, action);
-
- = * Signal received or TCP connection
- * reset by peer?
- */
- if (touched && IS_SIG(&touched->context))
- {
- if (mi =3D=3D touched)<= /span>
- {- mi =3D NULL;
- }- multi_close_instanc= e_on_signal(m, touched);
--
- /*
- * If dispatch produced any pending output
<= span style=3D"color: hsl(0, 100%, 40%);">- * for a particular insta= nce, point to

- = * that instance.
- = */
- if (m-= >pending)
- {=
- mi =3D m-= >pending;
- }=
-
- /*
- * Based on the effects of the action,=
- * such as generatin= g pending output,
- = * possibly transition to a new action state.
- */
- action =3D multi_tcp_post(m, mi, action);<= br>-
- /*
- * If we are finished processing the original action,=
- * check if w= e have any TUN input. If so, transition
- * our action state to processing this input.
- */
- if (tun_input_pending &= ;& action =3D=3D TA_UNDEF)

- {
- = action =3D TA_TUN_READ;
- mi =3D NULL;
- tun_input_pending =3D false;
- poll =3D false;
- }

- else
- {
- poll =3D true;
- }
-<= /span>
- } while (action != =3D TA_UNDEF);
-}
-
-static void
-multi_tcp_process_io(struct multi_context *m)
-{

- struct multi_tcp *mtcp =3D m->mtcp;
<= span style=3D"color: hsl(0, 100%, 40%);">- int i;

-
- for (i =3D 0; i < mtcp->n_esr; ++i)
- {
- struct event_set_return *e =3D &mtcp->esr= [i];
-
- /* incoming data for instance = or listening socket? */
- if (e->arg >=3D MULTI_N)
- {
- struct event_arg *ev_arg =3D (struct event_arg *)e->a= rg;
- switch= (ev_arg->type)
- = {
- = struct multi_instance *mi;
-
- = /* react to event on child instance */
- case EVENT_ARG_MULTI_INSTANCE:=
- if (!ev_= arg->u.mi)
- = {
- = msg(D_MULTI_ERRORS, "MULTI: mtcp_proc_io: null minstan= ce");
- = break;
- = }
-
- mi = =3D ev_arg->u.mi;
- = if (e->rwflags & EVENT_WRITE)
- {
- multi_tcp_action(= m, mi, TA_SOCKET_WRITE_READY, false);
- }
- else if (e->rwflags & EVENT_READ= )
- = {
- = multi_tcp_action(m, mi, TA_SOCKET_READ, false);
- }
- break;
-
- /* new incoming TCP client attempting to co= nnect? */
- = case EVENT_ARG_LINK_SOCKET:
- if (!ev_arg->u.sock)
- {
- msg(D_MULTI_ERRORS,= "MULTI: mtcp_proc_io: null socket");
- break;
- }
-
- socket_reset_listen_persistent(ev_arg->= ;u.sock);
- = mi =3D multi_create_instance_tcp(m, ev_arg->u.sock);
<= span style=3D"color: hsl(0, 100%, 40%);">- if (mi)
- {
- mul= ti_tcp_action(m, mi, TA_INITIAL, false);
- }
- break;
- }
- }
- else
- = {
-#ifdef ENABLE_MAN= AGEMENT
- if= (e->arg =3D=3D MTCP_MANAGEMENT)
- {
- ASSERT(management);
- management_io(management);
- }

- else
-#endif
- /* incoming data on TUN? */
- if (e->arg =3D=3D MTCP_TUN)<= /span>
- {- if (e->rwfl= ags & EVENT_WRITE)
= - {
- = multi_tcp_action(m, NULL, TA_TUN_WRITE, false);
= - }
- else if (e->rwfla= gs & EVENT_READ)

- = {
- = multi_tcp_action(m, NULL, TA_TUN_READ, false);
- }

- }
-#if defined(ENABLE_DCO) && (defined(TAR= GET_LINUX) || defined(TARGET_FREEBSD))
- /* incoming data on DCO? */
- else if (e->arg =3D=3D MT= CP_DCO)
- {<= /span>
- multi_= process_incoming_dco(m);
-#endi= f
- /* signa= l received? */
- = else if (e->arg =3D=3D MTCP_SIG)
- {
- get_signal(&m->top.sig->signal_receive= d);
- }
-#ifdef ENABLE_ASYNC_PUSH
- else if (e-= >arg =3D=3D MTCP_FILE_CLOSE_WRITE)
- {
- multi_process_file_closed(m, MPP_PRE_SELECT | MPP_R= ECORD_TOUCH);
- = }
-#endif- }
- if (IS_SIG(&m->top))<= br>- {
- break;
- }
- }
- mtcp->n_esr =3D 0;
-
- /*=
- * Process queued mbuf p= ackets destined for TCP socket
- */
- {=
- struct multi_= instance *mi;
- = while (!IS_SIG(&m->top) && (mi =3D mbuf_peek(m->mbuf)) != =3D NULL)
- {
- multi_tcp_ac= tion(m, mi, TA_SOCKET_WRITE, true);
- }
- }
-}
-

/*
* Top level event loop for single-threaded operation.

= * TCP mode.
@@ -851,7 +276,7 @@
<= br> /* wait on tun/socket list */
mu= lti_get_timeout(&multi, &multi.top.c2.timeval);
- status =3D multi_tcp_wait(&mu= lti.top, multi.mtcp);
+ status =3D multi_io_wait(&multi);
MU= LTI_CHECK_SIG(&multi);

/* ch= eck on status of coarse timers */
@@ -861,12 +286,12 @@
if (status > 0)
{<= br> /* process the I/O which triggered select */- multi_tcp_process_= io(&multi);
+ = multi_io_process_io(&multi);
MULTI= _CHECK_SIG(&multi);
}
= else if (status =3D=3D 0)
{
- multi_tcp_action(&multi, NU= LL, TA_TIMEOUT, false);
+ multi_io_action(&multi, NULL, TA_TIMEOUT, false);
}

perf_pop()= ;
diff --git a/src/openvpn/mtcp.h b/src/openvpn/mtcp.h
index ab968e9..0da0a7d 100644
--- a/src/openvpn= /mtcp.h
+++ b/src/openvpn/mtcp.h
@@ -30,34 = +30,27 @@

#include "event.h"

-/*
- * Extra state info need= ed for TCP mode
- */
-struct multi_tcp<= br>-{
- struct event_set *es;
- struct event_set_return *esr;- int n_esr;
- int maxevents;
- unsigned int tun_rwflags;
<= span style=3D"color: hsl(0, 100%, 40%);">-#ifdef ENABLE_MANAGEMENT
- unsigned int management_pe= rsist_flags;
-#endif
-};
-
+struct multi_context;
struct multi_instance;<= /span>
struct context;

-struct multi_tcp *multi_tcp_init(int maxevents= , int *maxclients);
-
-void multi_tcp_free(str= uct multi_tcp *mtcp);
-=
-void multi_tcp_derefe= rence_instance(struct multi_tcp *mtcp, struct multi_instance *mi);+void multi_tcp_dereference_i= nstance(struct multi_io *multi_io, struct multi_instance *mi);

bool multi_tcp_instance_specific_init(struct multi_c= ontext *m, struct multi_instance *mi);

v= oid multi_tcp_instance_specific_free(struct multi_instance *mi);
=
+void multi_t= cp_set_global_rw_flags(struct multi_context *m, struct multi_instance *mi);=
+
+bool multi_tcp_process_outgoing_link(s= truct multi_context *m, bool defer, const unsigned int mpp_flags);+
+bool multi_tcp_process_outgoing_link_ready(stru= ct multi_context *m, struct multi_instance *mi, const unsigned int mpp_flag= s);
+
+struct multi_instance *multi_create= _instance_tcp(struct multi_context *m, struct link_socket *ls);

<= span style=3D"color: hsl(120, 100%, 40%);">+

+void multi_tcp_link_out_deferred(struct multi_cont= ext *m, struct multi_instance *mi);
+
+
/***********************************************************= ***************/
/**
* Main event loop f= or OpenVPN in TCP server mode.
@@ -68,6 +61,6 @@
= void tunnel_server_tcp(struct context *top);


-void mult= i_tcp_delete_event(struct multi_tcp *mtcp, event_t event);
+void multi_tcp_delete_event(struct m= ulti_io *multi_io, event_t event);

#endi= f /* ifndef MTCP_H */
diff --git a/src/openvpn/multi.c b/sr= c/openvpn/multi.c
index f426b46..3f55dd7 100644
<= span>--- a/src/openvpn/multi.c

+++ b/src/openvpn/multi.c
@@ -440,7 +440,7 @@
*/
= if (tcp_mode)
{
- m->mtcp =3D multi_tcp_init(t->options.max= _clients, &m->max_clients);
+ m->multi_io =3D multi_io_init(t->options.max_c= lients, &m->max_clients);
}
= m->tcp_queue_limit =3D t->options.tcp_queue_limit;
=
@@ -665,9 +665,9 @@
mi->d= id_iroutes =3D false;
}
- if (m->mtcp)=
+ if (m->multi_io= )
{
- multi_tcp_dereference_instance(m->mtcp, mi);+ multi_tcp_derefe= rence_instance(m->multi_io, mi);
}

mbuf_dereference_instance(m->mbuf, mi);
@@ -742,7 +742,7 @@
initial_rate_li= mit_free(m->initial_rate_limiter);
multi_reap_f= ree(m->reaper);
mroute_helper_free(m->route_= helper);
- multi= _tcp_free(m->mtcp);
- if (m->mtc= p)
+ if (m->mul= ti_io)
{
- multi_tcp_delete_event(m->mtcp, event);
+ multi_tcp_delete_event(m->= ;multi_io, event);
}
}

diff --git a/src/openvpn/multi.h b/src/openvpn/multi.h<= /span>
index 9b6834a..eacfb52 100644
--- a/src/ope= nvpn/multi.h
+++ b/src/openvpn/multi.h
@@ -= 38,6 +38,7 @@
#include "pool.h"
= #include "mudp.h"
#include "mtcp.h"
+#include "multi_= io.h"
#include "perf.h"
#i= nclude "vlan.h"
#include "reflect_filter.h&= quot;
@@ -174,8 +175,7 @@
struct mbuf_= set *mbuf; /**< Set of buffers for passing data
= * channel packets between VPN tunnel
* instances. */
- struct multi_tcp *mtcp; /*= *< State specific to OpenVPN using TCP

- * as external transp= ort. */
+ struct m= ulti_io *multi_io; /**< I/O state and events tracker */
struct ifconfig_pool *ifconfig_pool;

struct f= requency_limit *new_connection_limiter;
struct initial= _packet_rate_limit *initial_rate_limiter;
diff --git a/src/= openvpn/multi_io.c b/src/openvpn/multi_io.c
new file mode 1= 00644
index 0000000..e4174dd
--- /dev/null<= /span>
+++ b/src/openvpn/multi_io.c
@@ -0,0 +1,632= @@
+/*
+ * 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 authenticat= ion, and
+ * = packet compression.
+ *
+ * Copyright= (C) 2002-2023 OpenVPN Inc <sales@...515...>
+ *
+ * This program is free software; you can redistribute it an= d/or modify
+ * it u= nder the terms of the GNU General Public License version 2
+ * as published by the Free Softwar= e Foundation.
+ *
+ * This program is dis= tributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the imp= lied warranty of
+ * = MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License f= or more details.

+ *<= /span>
+ * 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.
+ */
= +
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "syshead.h"
+
+#in= clude "memdbg.h"
+
+#include &qu= ot;multi.h"
+#in= clude "forward.h"
+#include "multi_io.h"
+
= +#ifdef HAVE_SYS_INOTIFY_H
+#include <sys/inotify.h>
+#endif
+
+/*
<= span style=3D"color: hsl(120, 100%, 40%);">+ * Special tags passed to event= .[ch] functions

+ */<= /span>
+#define MULTI_IO_SOC= KET ((void *)1)
= +#define MULTI_IO_TUN ((void *)2)
+#define MULTI_IO_SIG ((void *)3) /* Only on W= indows */
+#define MU= LTI_IO_MANAGEMENT ((void *)4)
+#define MULTI_IO_FILE_CLOSE_WRITE ((void *)5)
+#define MULTI_IO_DCO ((void *)6)=
+
+struct ta_iow_flags
+{
+ unsigned int flags;
+ unsigned int ret;
+ unsigned int tun;
+ unsigned int sock;
+};
+
+#ifdef= ENABLE_DEBUG
+static= const char *
+pract(= int action)
+{=
+ switch (action)=
+ {
+ case TA_UNDEF:
+ return "TA_UNDEF&quo= t;;
+
+ case TA_SOCKET_READ:
=
+ return "T= A_SOCKET_READ";
= +
+ case TA_SO= CKET_READ_RESIDUAL:
+= return "TA_SOCKET_READ_RESIDUAL";
+
+ case TA_SOCKET_WRITE:
+ return "TA_SOCKET_WRITE";<= /span>
+
+ case TA_SOCKET_WRITE_READY:
+ return &quo= t;TA_SOCKET_WRITE_READY";
+
+ = case TA_SOCKET_WRITE_DEFERRED:
+ return "TA_SOCKET_WRITE_DEFERRED";<= br>+
+ case TA_TUN_READ:
+ return "TA_TUN_READ&quo= t;;
+
+ case TA_TUN_WRITE:
+ return "TA_= TUN_WRITE";
+
+ case TA_INITIA= L:
+ retur= n "TA_INITIAL";
+
+ case = TA_TIMEOUT:
+ = return "TA_TIMEOUT";
+
+ = case TA_TUN_WRITE_TIMEOUT:
+ return "TA_TUN_WRITE_TIMEOUT";
<= span style=3D"color: hsl(120, 100%, 40%);">+

+ default:
+ return "?";
+ }
+}
+#endif /* ENABLE_DEBUG */
+
+static in= line struct context *
+multi_get_context(struct multi_context *m, struct multi_instance *mi)
+{
+ if (mi)
+ {
+ return &mi->context;
+ }
+ else
+ ret= urn &m->top;
+= }
+}
+

+struct multi_io *
+multi_io_init(int maxevents, int *maxclients)<= br>+{
+ struct multi_io *multi_io;
+ const int extra_events =3D BAS= E_N_EVENTS;

+<= br>+ ASSERT(maxevents >= =3D 1);
+ ASSERT(m= axclients);
+<= br>+ ALLOC_OBJ_CLEAR(multi_i= o, struct multi_io);
= + multi_io->maxevents =3D maxevents + extra_events;
+ multi_io->es =3D event_set_ini= t(&multi_io->maxevents, 0);
+ wait_signal(multi_io->es, MULTI_IO_SIG);
+ ALLOC_ARRAY(multi_io->esr= , struct event_set_return, multi_io->maxevents);

+ *maxclients =3D max_int(min_int(multi= _io->maxevents - extra_events, *maxclients), 1);
+ msg(D_MULTI_LOW, "MULTI IO: MULT= I_IO INIT maxclients=3D%d maxevents=3D%d", *maxclients, multi_io->m= axevents);
+ retur= n multi_io;
+}=
+
+void
+multi_io_free(struct multi_io *multi_io)
+{
+ if (multi_io)
+ {
+ event_free(multi_io->es);
+ free(multi_io->esr);
+ free(multi_io);
+ }
+}
+int
+multi_io_wait(struct multi_conte= xt *m)

+{
<= span style=3D"color: hsl(120, 100%, 40%);">+ int status, i;

+ unsigned int *persistent =3D= &m->multi_io->tun_rwflags;

+
+ = for (i =3D 0; i < m->top.c1.link_sockets_num; i++)
+ {

+ socket_set_listen_persistent(m->top.c2= .link_sockets[i], m->multi_io->es,
+ &m->top.c2= .link_sockets[i]->ev_arg);
+ }
+
+#ifdef _WIN32+ if (tuntap_is_wintun(m-&g= t;top.c1.tuntap))
+ = {
+ if (!tun= tap_ring_empty(m->top.c1.tuntap))
+ {
+ /* there is data in wintun ring buffer, read it immediat= ely */
+ m= ->multi_io->esr[0].arg =3D MULTI_IO_TUN;
+ m->multi_io->esr[0].rwflags = =3D EVENT_READ;
+ = m->multi_io->n_esr =3D 1;
+ return 1;
+ }
+ persistent =3D NULL;
+ }
+#endif
+ tu= n_set(m->top.c1.tuntap, m->multi_io->es, EVENT_READ, MULTI_IO_TUN,= persistent);
+#if de= fined(TARGET_LINUX) || defined(TARGET_FREEBSD)
+ dco_event_set(&m->top.c1.tuntap->d= co, m->multi_io->es, MULTI_IO_DCO);
+#endif
+
+#ifdef ENA= BLE_MANAGEMENT
+ i= f (management)
+ {=
+ management_= socket_set(management, m->multi_io->es, MULTI_IO_MANAGEMENT, &m-&= gt;multi_io->management_persist_flags);
+ }
+#endif
+
+#ifdef ENABLE_ASYNC_PUS= H
+ /* arm inotify= watcher */
+ even= t_ctl(m->multi_io->es, m->top.c2.inotify_fd, EVENT_READ, MULTI_IO_= FILE_CLOSE_WRITE);
+#= endif
+
+ status =3D event_wait(m->m= ulti_io->es, &m->top.c2.timeval, m->multi_io->esr, m->mu= lti_io->maxevents);

+ m->multi_io->n_esr =3D 0;
+ if (status > 0)
+ {
+ m->multi_io->n_esr =3D status;
+ }
+ return status;
+}
= +
+static int<= br>+multi_io_wait_lite(struct m= ulti_context *m, struct multi_instance *mi, const int action, bool *tun_inp= ut_pending)
+{=
+ struct context *c =3D = multi_get_context(m, mi);
+ unsigned int looking_for =3D 0;
+
+ = pract(action),
+ (ptr_type)mi);
+
+ tv_clear(= &c->c2.timeval); /* ZERO-TIMEOUT */
+
+ switch (action)
+ {
+ ca= se TA_TUN_READ:
+ = looking_for =3D TUN_READ;
+ tun_input_pending =3D NULL;
+ io_wait(c, IOW_READ_TUN);
+ break;
+
+ case TA_SOCKET_READ:
+ looking_for =3D SOCK= ET_READ;

+ = tun_input_pending =3D NULL;
+ io_wait(c, IOW_READ_LINK);
+ break;
+
+ case TA_TUN_WRITE:
+ looking_for =3D TUN_WRITE;
+ tun_input_pending =3D NULL;
+ c->c2.= timeval.tv_sec =3D 1; /* For some reason, the Linux 2.2 TUN/TAP driver hits= this timeout */
+ = perf_push(PERF_PROC_OUT_TUN_MTCP);
+ io_wait(c, IOW_TO_TUN);
+ perf_pop();

<= span style=3D"color: hsl(120, 100%, 40%);">+ break;

+

+ case TA_SOCKET_WRITE:
+ looking_for =3D SOCKET_WRITE;=
+ io_wait= (c, IOW_TO_LINK|IOW_READ_TUN_FORCE);
+ break;
+
+ = default:
+ = msg(M_FATAL, "MULTI IO: multi_io_wait_lite, unhandled action=3D%= d", action);
+ = }
+
+ if (tun_input_pending &&= ; (c->c2.event_set_status & TUN_READ))

+ {
+ *tun_input_pending =3D true;
+ }
+
+ = if (c->c2.event_set_status & looking_for)
+ {
+ return action;
+ }
+ else
+ {
+ switch (actio= n)
+ {<= br>+ /* MULTI PROTOC= OL socket output buffer is full */
+ case TA_SOCKET_WRITE:
+ return TA_SOCKET_WRITE_DEFERRE= D;
+
+ /* TUN device timed out = on accepting write */
+ case TA_TUN_WRITE:
+ return TA_TUN_WRITE_TIMEOUT;
+ }
+
+ return TA_UNDEF;
+ }
+= }
+
+static struct multi_instance *=
+multi_io_dispatch(struct m= ulti_context *m, struct multi_instance *mi, const int action)
+{

+ const unsigned int mpp_flags =3D MPP_PRE_SELECT= |MPP_RECORD_TOUCH;
+ = struct multi_instance *touched =3D mi;
+ m->mpp_touched =3D &touched;
+

+ dmsg(D_MULTI_DEBUG, "MULTI IO: multi_io_dis= patch a=3D%s mi=3D" ptr_format,
+ pract(action),
+ (ptr_type)mi);
+
+ switch (action)
+ {
+ = case TA_TUN_READ:
+ = read_incoming_tun(&m->top);
+ if (!IS_SIG(&m->top))+ {
+ multi_process_incom= ing_tun(m, mpp_flags);

+ = break;
+
+ case TA_SOCKET_= READ:
+ case T= A_SOCKET_READ_RESIDUAL:
+ ASSERT(mi);
+ ASSERT(mi->context.c2.link_sockets);
+ ASSERT(mi->context.c= 2.link_sockets[0]);

+= set_prefix(mi);
+ read_incoming_link(&mi->context, mi->context.= c2.link_sockets[0]);
= + clear_prefix();
+ if (!IS_SIG(&mi->context))
+ {
+ multi_process_incoming_link(m,= mi, mpp_flags,
+ = mi->context.c2.link_sockets[0]);=
+ if = (!IS_SIG(&mi->context))
+ {
+ stream_buf_read_setup(mi->context.c2.link_s= ockets[0]);
+ = }
+ = }
+ brea= k;
+
+ case TA_TIMEOUT:
<= span style=3D"color: hsl(120, 100%, 40%);">+ multi_process_timeo= ut(m, mpp_flags);

+ = break;
+
+ case TA_TUN_WR= ITE:
+ mul= ti_process_outgoing_tun(m, mpp_flags);
+ break;
+
+ = case TA_TUN_WRITE_TIMEOUT:
+ multi_process_drop_outgoing_tun(m, mpp_flags);
+ break;
+
+ case TA_SOCKET_WRITE_READY:
+ ASSERT(mi);
+ multi_tcp= _process_outgoing_link_ready(m, mi, mpp_flags);
+ break;
+
+ case TA_SOCKET_WRITE:
+ multi_tcp_process_outgoing_link(m, false, mpp= _flags);
+ = break;
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ case TA_SOCKET_WRITE_DE= FERRED:

+ = multi_tcp_process_outgoing_link(m, true, mpp_flags);
+ break;
+
+ case TA_INITIAL:
+ ASSERT(mi);
+ multi_tcp_set_global_rw_flags(m, mi);
+ multi_pro= cess_post(m, mi, mpp_flags);
+ break;
+
+ def= ault:
+ ms= g(M_FATAL, "MULTI IO: multi_io_dispatch, unhandled action=3D%d", = action);
+ }
+
+ m->mpp_touched =3D NULL;
+ return touched;

+}

+
+static int
+mul= ti_io_post(struct multi_context *m, struct multi_instance *mi, const int ac= tion)
+{
+ struct context *c =3D multi_= get_context(m, mi);

+= int newaction =3D TA_UNDEF;
+
+#define= MTP_NONE 0
+= #define MTP_TUN_OUT (1<<0)
+#define MTP_LINK_OUT (1<<1)
+ unsigned int flags =3D MTP_NONE;
+
+ if (TUN_OUT(c))
+ {
+ flags |=3D MTP_TUN_OUT;
+ }
+ if (LINK_OUT(c))
+ {
+ flags |=3D MTP_LINK_OUT;
+ }
+
+ switch (f= lags)
+ {+ case MTP_TUN_OUT|MTP= _LINK_OUT:
+ c= ase MTP_TUN_OUT:
+ = newaction =3D TA_TUN_WRITE;
+ break;
+
+ = case MTP_LINK_OUT:
+ newaction =3D TA_SOCKET_WRITE;
+ break;
+
+ case MTP_NONE:
+ if (mi && sockets_read_residual(c))+ {
+ newaction =3D TA_SO= CKET_READ_RESIDUAL;

+= }
+ = else
+ = {
+ = multi_tcp_set_global_rw_flags(m, mi);
+ }
+ break;
+
+ = default:
+ {<= /span>
+ struct g= c_arena gc =3D gc_new();
+ msg(M_FATAL, "MULTI IO: multi_io_post bad state, mi= =3D%s flags=3D%d",
+ multi_instance_string(mi, false, &gc),
+ flags);
+ gc_free(&gc)= ;
+ break;=
+ }+ }
+
+ dmsg(D_MULTI_DEBUG, "MULTI IO: multi_io_post %s -&g= t; %s",
+ = pract(action),
+ = pract(newaction));
+
+ return new= action;
+}
= +
+void
+multi_io_process_io(struct multi_context *m)
+{
+ struct multi_io *multi_io =3D m->multi_io;<= br>+ int i;
+
+ for (i =3D 0; i < multi_io->n_esr; ++i)
+ {
+ struct event_set_return *e =3D = &multi_io->esr[i];
+ struct event_arg *ev_arg =3D (struct event_arg *)e->arg;
+
+ /* incoming data for instance or= listening socket? */
+ if (e->arg >=3D MULTI_N)
+ {
+ switch (ev_arg->type)
+ {
+ struct multi_instance *mi;
= +
+ /* react to event on child instan= ce */
+ = case EVENT_ARG_MULTI_INSTANCE:
+ if (!ev_arg->u.mi)
+ {
+ msg(D_MULTI_ER= RORS, "MULTI IO: multi_io_proc_io: null minstance");
+ break;
+ }=
+
+ mi =3D ev_arg->= u.mi;
+ = if (e->rwflags & EVENT_WRITE)
+ {
+ multi_io_action(m, mi, TA_= SOCKET_WRITE_READY, false);
+ }
+ else if (e->rwflags & EVENT_READ)
+ {

+ = multi_io_action(m, mi, TA_SOCKET_READ, false);
+ }
+ break;
+
+ /* new incoming TCP client attempting to= connect? */
+ = case EVENT_ARG_LINK_SOCKET:
+ if (!ev_arg->u.sock)
+ {

+ msg(D_MU= LTI_ERRORS, "MULTI IO: multi_io_proc_io: null socket");
+ break= ;
+ = }
+
+ if (!proto_is_d= gram(ev_arg->u.sock->info.proto))

+ {
+ socket_reset_listen_persisten= t(ev_arg->u.sock);
+ mi =3D multi_create_instance_tcp(m, ev_arg->u.= sock);
+ = if (mi)
+ = {
+ multi_io_action(m, mi, TA_INITIAL, false);=
+ = }
+ = break;
+ = }
+= }
+ = }
+ else
+ {
+#ifdef ENABLE_MANAGEMENT
+ if (e->arg = =3D=3D MULTI_IO_MANAGEMENT)
+ {
+ ASSERT(management);
+ management_io(management);
+ }

+ else
+#endif
+ /* incoming data on TUN? */
+ if (e->arg =3D=3D M= ULTI_IO_TUN)

+ = {
+ = if (e->rwflags & EVENT_WRITE)
+ {
+ multi_io_action(m, NULL, TA_TUN_WRITE= , false);
+ = }
+ = else if (e->rwflags & EVENT_READ)
+ {
+ multi_io_action(m, NULL, TA_TUN_= READ, false);
+ = }
+ = }
+ /*= new incoming TCP client attempting to connect? */
+ else if (e->arg =3D=3D MULTI_= IO_SOCKET)
+ = {
+ = struct multi_instance *mi;
+ ASSERT(m->top.c2.link_sockets[0]);
+ socket_reset_list= en_persistent(m->top.c2.link_sockets[0]);

+ mi =3D multi_create_instance_tcp(m= , m->top.c2.link_sockets[0]);
+ if (mi)
+ {
+ multi_io_action(m, mi, TA_INITIAL, false= );
+ }=
+ }
+#if defined(ENABLE_DCO) &= amp;& (defined(TARGET_LINUX) || defined(TARGET_FREEBSD))
+ /* incoming data on DC= O? */

+ el= se if (e->arg =3D=3D MULTI_IO_DCO)
+ {
+ multi_process_incoming_dco(m);
+ }
+#endif
+ /* signal received? */
+ else if (e->arg =3D=3D M= ULTI_IO_SIG)
+ = {
+ = get_signal(&m->top.sig->signal_received);
+ }
+#ifdef ENABLE_ASYNC_PUSH
+ else if (e->arg =3D=3D MUL= TI_IO_FILE_CLOSE_WRITE)
+ {
+ = multi_process_file_closed(m, MPP_PRE_SELECT | MPP_RECORD_TOUC= H);
+ }
+#endif
+ }

+ if (IS_SIG(&m->top))+ {
+ break;
+ }
+ }
+ multi_io->n_esr =3D 0;
+
+ /*
+ * Proc= ess queued mbuf packets destined for TCP socket
+ */
+ {
+ struct multi_instance *mi;
+ while (!IS_SIG(&m->top) && (mi =3D = mbuf_peek(m->mbuf)) !=3D NULL)
+ {
+ multi_io_action(m, mi, TA_SOCKET_WRITE, true);
+ }

+ }
+}
+
+void
+multi_io_action(struct multi_con= text *m, struct multi_instance *mi, int action, bool poll)
+{
+ bool tun_input_pending =3D false;
+
+ do
+ {
+ = dmsg(D_MULTI_DEBUG, "MULTI IO: multi_io_action a=3D%s p=3D%d",
+ pract(ac= tion),
+ = poll);
+
+ /*

+ * If TA_SOCKET_READ_RESIDUAL, it= means we still have pending
+ * input packets which were read by a prior recv.+ *
+ * Otherwise do a "lite&= quot; wait, which means we wait with 0 timeout
+ * on I/O events only related to the cur= rent instance, not
+ = * the big list of events.
+ *
+ * On our first pass, poll will be false because we already kn= ow
+ * that i= nput is available, and to call io_wait would be redundant.
+ */
+ if (poll && action !=3D TA_S= OCKET_READ_RESIDUAL)
= + {
+ = const int orig_action =3D action;
+ action =3D multi_io_wait_lite(m, mi, action, &= amp;tun_input_pending);
+ if (action =3D=3D TA_UNDEF)
+ {
+ msg(M_FATAL, "MULTI IO: I/O wait req= uired blocking in multi_io_action, action=3D%d", orig_action);<= br>+ }
+ }
+
+ /*
+ * Dispatch the action
+ */
+ struct multi_instance *touched =3D multi_io_dispatch(m, mi= , action);
++ /*
+ * Signal received or connect= ion
+ * reset= by peer?
+ *= /
+ if (touche= d && IS_SIG(&touched->context))
+ {
+ if (mi =3D=3D touched)
+ {
+ mi =3D NULL;
+ }
+ multi_close_instance_on_signal(m= , touched);
+ = }
+
+
+ /*
+ * If dispatch produced any pending output
+ * for a particular instan= ce, point to
+ = * that instance.
+ = */
+ i= f (m->pending)
+ = {
+ = mi =3D m->pending;
+ }
+<= br>+ /*
+ * Based on the effects of t= he action,
+ = * such as generating pending output,
+ * possibly transition to a new action state.

+ */
+ action =3D multi_io_post= (m, mi, action);
+

+ /*
<= span style=3D"color: hsl(120, 100%, 40%);">+ * If we are finished p= rocessing the original action,
+ * check if we have any TUN input. If so, transition

+ * our action = state to processing this input.
+ */
+ if (tun_input_pending && action =3D=3D TA_UNDEF)=
+ {
+ action =3D TA_TUN_READ;<= /span>
+ mi =3D N= ULL;
+ tun= _input_pending =3D false;
+ poll =3D false;
+ }
+ else
+ = {
+ p= oll =3D true;
+ = }
+
+ } while (action !=3D TA_UNDEF);=

+}
+
+void
+multi_io_delete_event(struct multi_io *multi_io, event_t event)<= br>+{
+ if (multi_io && multi_io->es)<= /span>
+ {
+ event_del(multi_io->es,= event);

+ }
+}
diff --= git a/src/openvpn/multi_io.h b/src/openvpn/multi_io.h
new f= ile mode 100644
index 0000000..03d708c
--- = /dev/null
+++ b/src/openvpn/multi_io.h
@@ -= 0,0 +1,77 @@
+/*
+ * OpenVPN -- An applic= ation to securely tunnel IP networks
+ * over a single TCP/UDP port, with support fo= r SSL/TLS-based
+ * = session authentication and key exchange,
+ * packet encryption, packet = authentication, and
+= * packet compression.
+ *
+ *= Copyright (C) 2002-2023 OpenVPN Inc <sales@...515...>
+ *

+ * This program is free software; you can redistr= ibute it and/or modify
+ * as published by the F= ree Software Foundation.
+ *
+ * This pro= gram is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without e= ven the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Publi= c License for more details.
+ *
+ * You s= hould have received a copy of the GNU General Public License along+ * with this program; if no= t, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA= 02110-1301 USA.
+ */=
+
+/*
+ * Multi-protocol specific code for --mode server
+ */
+
+#ifndef MULTI_IO_H
+#define MULTI_IO_H
+
+#inclu= de "event.h"
+/*
+ * I/O processing States
+ */
+
+#define TA_UNDEF 0
+#define TA_SOCKET_READ 1
+#define TA_SOCKET_READ_RESIDUAL = 2

+#define TA_SOCKET_= WRITE 3
+#de= fine TA_SOCKET_WRITE_READY 4
+#define TA_SOCKET_WRITE_DEFERRED 5
+#define TA_TUN_READ 6
+#define TA_TUN_WRITE = 7

+#define TA_INITIAL= 8
+#de= fine TA_TIMEOUT 9
+#define TA_TUN_WRITE_TIMEOUT 10
+
+/*
+ * I/O s= tate and events tracker
+ */
+struct multi= _io
+{
+ struct event_set *es;+ struct event_set_return = *esr;
+ int n_esr;=
+ int maxevents;<= /span>
+ unsigned int tun= _rwflags;
+ unsign= ed int udp_flags;
+#i= fdef ENABLE_MANAGEMENT
+#endif
+};
+
+struct multi_io *multi_= io_init(int maxevents, int *maxclients);
+
+void multi_io_free(struct multi_io *multi_io);
+
+int multi_io_wait(struct multi_context *m);
+
+void multi_io_process_io(struct multi_context *m);+
+void multi_io_action(struct multi_context *m, st= ruct multi_instance *mi, int action, bool poll);
+
+void multi_io_delete_event(struct multi_io *multi_io, event_t eve= nt);
+
+#endif /* ifndef MULTI_IO_H */

To view, visit change 763. To unsubscribe, or f= or help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I1e5a84969988e4f027a18658d4a= b268c13fbf929
Gerrit-Change-Number: 763
Gerrit-PatchSet: 14
Gerrit-Owner: its_Giaan <gianmarco@...2786...= ..>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: its_Giaan <gianmarco@...1665...= 726...>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newpatchset
--GyKRPZwAe+0=--