From: "ordex (Code Review)" Date: Wed, 15 Nov 2023 13:45:05 +0000 References: Reply-To: a@...2181..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="AnJrMjNWgdY="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: event/multi: add event_arg object to make event handling more generic List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --AnJrMjNWgdY= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, plaisthos. Hello plaisthos, flichtenheld, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/430?usp=3Demail to review the following change. Change subject: event/multi: add event_arg object to make event handling mo= re generic ...................................................................... event/multi: add event_arg object to make event handling more generic In order to prepare the event handling code to deal with multiple listening sockets, we have to make sure that it is possible to distinguish which of these sockets have been pocked by an incoming connection request. To achieve that, this patch changes the object being passed as event handler argument, from a "partly integer evaluated variable" to a full struct with a proper type attribute. This struct will allow the code to carry around the particular listening socket where the connection is being established. This change affects the TCP server code path only as UDP servers use only one socket to handle all clients i/o. Change-Id: If11e901c26fc5aafdcfd59a214d70c6e6a548f40 Signed-off-by: Antonio Quartulli --- M src/openvpn/event.h M src/openvpn/mtcp.c M src/openvpn/multi.c M src/openvpn/multi.h M src/openvpn/socket.c M src/openvpn/socket.h 6 files changed, 58 insertions(+), 13 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/30/430/1 diff --git a/src/openvpn/event.h b/src/openvpn/event.h index 6282873..23a6799 100644 --- a/src/openvpn/event.h +++ b/src/openvpn/event.h @@ -126,6 +126,20 @@ struct event_set_functions func; }; +typedef enum { + EVENT_ARG_MULTI_INSTANCE =3D 0, + EVENT_ARG_LINK_SOCKET, +} event_arg_t; + +/* generic event argument object to pass to event_ctl() */ +struct event_arg +{ + event_arg_t type; + union { + struct multi_instance *mi; /* if type =3D EVENT_ARG_MULTI_INSTANCE= */ + } u; +}; + /* * maxevents on input: desired max number of event_t descriptors * simultaneously set with event_ctl diff --git a/src/openvpn/mtcp.c b/src/openvpn/mtcp.c index 62eb14b..9d383af 100644 --- a/src/openvpn/mtcp.c +++ b/src/openvpn/mtcp.c @@ -253,7 +253,7 @@ socket_set(mi->context.c2.link_socket, m->mtcp->es, mbuf_defined(mi->tcp_link_out_deferred) ? EVENT_WRITE := EVENT_READ, - mi, + &mi->ev_arg, &mi->tcp_rwflags); } } @@ -264,7 +264,8 @@ { int status; unsigned int *persistent =3D &mtcp->tun_rwflags; - socket_set_listen_persistent(c->c2.link_socket, mtcp->es, MTCP_SOCKET); + socket_set_listen_persistent(c->c2.link_socket, mtcp->es, + &c->c2.link_socket->ev_arg); #ifdef _WIN32 if (tuntap_is_wintun(c->c1.tuntap)) @@ -692,21 +693,43 @@ for (i =3D 0; i < mtcp->n_esr; ++i) { struct event_set_return *e =3D &mtcp->esr[i]; + struct event_arg *ev_arg =3D (struct event_arg *)e->arg; - /* incoming data for instance? */ + /* incoming data for instance or listening socket? */ if (e->arg >=3D MTCP_N) { - struct multi_instance *mi =3D (struct multi_instance *) e->arg; - if (mi) + switch (ev_arg->type) { - 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); - } + 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: + ASSERT(m->top.c2.link_socket); + socket_reset_listen_persistent(m->top.c2.link_socket); + mi =3D multi_create_instance_tcp(m); + if (mi) + { + multi_tcp_action(m, mi, TA_INITIAL, false); + } + break; } } else diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index 8b490ed..af04dc8 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -823,6 +823,9 @@ goto err; } + mi->ev_arg.type =3D EVENT_ARG_MULTI_INSTANCE; + mi->ev_arg.u.mi =3D mi; + perf_pop(); gc_free(&gc); return mi; diff --git a/src/openvpn/multi.h b/src/openvpn/multi.h index 6b1c68d..7656bac 100644 --- a/src/openvpn/multi.h +++ b/src/openvpn/multi.h @@ -102,6 +102,7 @@ struct schedule_entry se; /* this must be the first element of the st= ructure, * We cast between this and schedule_entry = so the * beginning of the struct must be identica= l */ + struct event_arg ev_arg; struct gc_arena gc; bool halt; int refcount; diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c index 903f98b..8ee14df 100644 --- a/src/openvpn/socket.c +++ b/src/openvpn/socket.c @@ -1827,6 +1827,8 @@ ALLOC_OBJ_CLEAR(sock, struct link_socket); sock->sd =3D SOCKET_UNDEFINED; sock->ctrl_sd =3D SOCKET_UNDEFINED; + sock->ev_arg.type =3D EVENT_ARG_LINK_SOCKET; + return sock; } diff --git a/src/openvpn/socket.h b/src/openvpn/socket.h index bfc1253..39e30b8 100644 --- a/src/openvpn/socket.h +++ b/src/openvpn/socket.h @@ -166,6 +166,8 @@ { struct link_socket_info info; + struct event_arg ev_arg; + socket_descriptor_t sd; socket_descriptor_t ctrl_sd; /* only used for UDP over Socks */ -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/430?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: If11e901c26fc5aafdcfd59a214d70c6e6a548f40 Gerrit-Change-Number: 430 Gerrit-PatchSet: 1 Gerrit-Owner: ordex Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-MessageType: newchange --AnJrMjNWgdY= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld, plaisthos.

ordex would like plaisthos and flichtenheld to review t= his change.

Vi= ew Change

event/multi: add event_arg object to make event handling more=
 generic

In order to prepare the event handling code to deal with mu= ltiple
listening sockets, we have to make sure that it is possible todistinguish which of these sockets have been pocked by an incoming
conn= ection request.

To achieve that, this patch changes the object being= passed as
event handler argument, from a "partly integer evaluated= variable"
to a full struct with a proper type attribute.

Th= is struct will allow the code to carry around the particular
listening s= ocket where the connection is being established.

This change affects= the TCP server code path only as UDP servers
use only one socket to han= dle all clients i/o.

Change-Id: If11e901c26fc5aafdcfd59a214d70c6e6a5= 48f40
Signed-off-by: Antonio Quartulli <a@...2181...>
---
M = src/openvpn/event.h
M src/openvpn/mtcp.c
M src/openvpn/multi.c
M s= rc/openvpn/multi.h
M src/openvpn/socket.c
M src/openvpn/socket.h
6= files changed, 58 insertions(+), 13 deletions(-)

git pull ssh:=
//gerrit.openvpn.net:29418/openvpn refs/changes/30/430/1
diff --git=
 a/src/openvpn/event.h b/src/openvpn/event.h
index 6282873.= .23a6799 100644
--- a/src/openvpn/event.h
+= ++ b/src/openvpn/event.h
@@ -126,6 +126,20 @@
struct event_set_functions func;
};

+typedef enum {
+ EVENT_ARG_MULTI_I= NSTANCE =3D 0,
+ E= VENT_ARG_LINK_SOCKET,
+} event_arg_t;
+
+/* generic event argum= ent object to pass to event_ctl() */
+struct event_arg
+{
+ = event_arg_t type;
+ = union {
+ st= ruct multi_instance *mi; /* if type =3D EVENT_ARG_MULTI_INSTANCE */<= br>+ } u;
+};
+
/*
* maxevents on i= nput: desired max number of event_t descriptors
* = simultaneously set with event_ctl
diff --git= a/src/openvpn/mtcp.c b/src/openvpn/mtcp.c
index 62eb14b..9= d383af 100644
--- a/src/openvpn/mtcp.c
+++ = b/src/openvpn/mtcp.c
@@ -253,7 +253,7 @@
= socket_set(mi->context.c2.link_socket,
= m->mtcp->es,
mbuf_defin= ed(mi->tcp_link_out_deferred) ? EVENT_WRITE : EVENT_READ,
- mi,

+ &mi->ev= _arg,

&mi->tcp_rwflags);<= br> }
}
@@ -264,7 +264,8 @@

{
int status;
unsign= ed int *persistent =3D &mtcp->tun_rwflags;
- socket_set_listen_persistent(c->c2.link_= socket, mtcp->es, MTCP_SOCKET);
+ socket_set_listen_persistent(c->c2.link_socket, mtcp-= >es,
+ = &c->c2.link_socket->ev_arg);

#ifdef _WIN32
if (tuntap_is_wintun= (c->c1.tuntap))
@@ -692,21 +693,43 @@
= for (i =3D 0; i < mtcp->n_esr; ++i)
{<= br> struct event_set_return *e =3D &mtcp->esr[i];
+ struct event_arg= *ev_arg =3D (struct event_arg *)e->arg;

- /* incoming data for instan= ce? */

+ /* in= coming data for instance or listening socket? */
i= f (e->arg >=3D MTCP_N)
{
- struct multi_instance *mi =3D= (struct multi_instance *) e->arg;
- if (mi)
+ switch (ev_arg->type)
= {
- = if (e->rwflags & EVENT_WRITE)
- {
- multi_tcp_action(m, mi, TA_SOCKET_WRITE_R= EADY, false);
- = }
- = else if (e->rwflags & EVENT_READ)
- {
- multi_tcp_action(m, mi, TA_SOCKET_RE= AD, false);
- = }
+ = struct multi_instance *mi;
+
+ = /* react to event on child instance */
+ case EVENT_ARG_MULTI_INSTANCE:
+ i= f (!ev_arg->u.mi)
= + {
+ 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 connect? */<= br>+ case EVENT_= ARG_LINK_SOCKET:
+ = ASSERT(m->top.c2.link_socket);
+ socket_reset_listen_pers= istent(m->top.c2.link_socket);
+ mi =3D multi_create_instance_tcp(m);
+ if (= mi)
+ = {
+ = multi_tcp_action(m, mi, TA_INITIAL, false);
+ }
+ break;
}

}
else=
diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c

index 8b490ed..af04dc8 100644
--- a/src/openvp= n/multi.c
+++ b/src/openvpn/multi.c
@@ -823= ,6 +823,9 @@
goto err;
}

+ mi= ->ev_arg.type =3D EVENT_ARG_MULTI_INSTANCE;
+ mi->ev_arg.u.mi =3D mi;
+
perf_pop();
gc_free(&gc);
return mi;<= br>diff --git a/src/openvpn/multi.h b/src/openvpn/multi.h
<= span>index 6b1c68d..7656bac 100644

--- a/src/openvpn/multi.= h
+++ b/src/openvpn/multi.h
@@ -102,6 +102,= 7 @@
struct schedule_entry se; /* this must be the fi= rst element of the structure,
= * We cast between this and schedule_entry so the
= * beginning of the struct must be identical */
+ struct event_arg = ev_arg;
struct gc_arena gc;
bool = halt;
int refcount;
diff --git a/src/o= penvpn/socket.c b/src/openvpn/socket.c
index 903f98b..8ee14= df 100644
--- a/src/openvpn/socket.c
+++ b/= src/openvpn/socket.c
@@ -1827,6 +1827,8 @@
= ALLOC_OBJ_CLEAR(sock, struct link_socket);
sock-&= gt;sd =3D SOCKET_UNDEFINED;
sock->ctrl_sd =3D SOCKE= T_UNDEFINED;
+ soc= k->ev_arg.type =3D EVENT_ARG_LINK_SOCKET;
+
return sock;
}


diff --git a/src/openvpn/socket.h b/s= rc/openvpn/socket.h
index bfc1253..39e30b8 100644--- a/src/openvpn/socket.h
+++ b/src/openvpn/socket.= h
@@ -166,6 +166,8 @@
{
= struct link_socket_info info;

+ struct event_arg ev_arg;
+

socket_descri= ptor_t sd;
socket_descriptor_t ctrl_sd; /* only used f= or UDP over Socks */


= To view, visit change 430. To unsubscribe, or for help writing mail filters, vis= it settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: If11e901c26fc5aafdcfd59a214d= 70c6e6a548f40
Gerrit-Change-Number: 430
Gerrit-PatchSet: 1
Gerrit-Owner: ordex <a@...2181...>
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: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newchange
--AnJrMjNWgdY=--