From: "its_Giaan (Code Review)" Date: Thu, 17 Oct 2024 14:47:28 +0000 In-Reply-To: References: Reply-To: gianmarco@...2726..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., a@...2181... Message-ID: <107dc2fd266bb97c571b9afa0b95834c4bdda7d5-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="3tOhxAKj5Vo="; 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 --3tOhxAKj5Vo= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, its_Giaan, ordex, plais= thos. Hello flichtenheld, ordex, plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/757?usp=3Demail to look at the new patch set (#5). The following approvals got outdated and were removed: Code-Review+2 by flichtenheld The change is no longer submittable: Code-Review and checks~ChecksSubmitRul= e are unsatisfied now. 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 poked 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. Change-Id: Icd7f6a2ad350cdc2312b3e80fa0dbdd7e4311d2e Signed-off-by: Antonio Quartulli Signed-off-by: Gianmarco De Gregori --- 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, 68 insertions(+), 28 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/57/757/5 diff --git a/src/openvpn/event.h b/src/openvpn/event.h index 856551a..844ea7b 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 96408d1..c002a38 100644 --- a/src/openvpn/mtcp.c +++ b/src/openvpn/mtcp.c @@ -54,7 +54,6 @@ /* * Special tags passed to event.[ch] functions */ -#define MTCP_SOCKET ((void *)1) #define MTCP_TUN ((void *)2) #define MTCP_SIG ((void *)3) /* Only on Windows */ #define MTCP_MANAGEMENT ((void *)4) @@ -253,7 +252,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); } } @@ -263,8 +262,8 @@ struct multi_tcp *mtcp) { 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)) @@ -280,7 +279,7 @@ persistent =3D NULL; } #endif - tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, persistent); + tun_set(c->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, &mtcp->tun_rwfla= gs); #if defined(TARGET_LINUX) || defined(TARGET_FREEBSD) dco_event_set(&c->c1.tuntap->dco, mtcp->es, MTCP_DCO); #endif @@ -693,20 +692,43 @@ { struct event_set_return *e =3D &mtcp->esr[i]; - /* 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) + struct event_arg *ev_arg =3D (struct event_arg *)e->arg; + 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 @@ -731,18 +753,6 @@ multi_tcp_action(m, NULL, TA_TUN_READ, false); } } - /* new incoming TCP client attempting to connect? */ - else if (e->arg =3D=3D MTCP_SOCKET) - { - struct multi_instance *mi; - 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); - } - } #if defined(ENABLE_DCO) && (defined(TARGET_LINUX) || defined(TARGET_FREEBS= D)) /* incoming data on DCO? */ else if (e->arg =3D=3D MTCP_DCO) diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index 0509911..45b3cfa 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 7167639..1b99ef7 100644 --- a/src/openvpn/multi.h +++ b/src/openvpn/multi.h @@ -102,6 +102,12 @@ 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; /**< this struct will store a pointer to ei= ther mi or + * link_socket, depending on the event type= , to keep + * it accessible it's placed within the sam= e struct + * it points to. */ + struct gc_arena gc; bool halt; int refcount; diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c index 7b1e603..cf04090 100644 --- a/src/openvpn/socket.c +++ b/src/openvpn/socket.c @@ -1830,6 +1830,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 2e583af..c152ab0 100644 --- a/src/openvpn/socket.h +++ b/src/openvpn/socket.h @@ -178,6 +178,11 @@ { struct link_socket_info info; + struct event_arg ev_arg; /**< this struct will store a pointer to ei= ther mi or + * link_socket, depending on the event type= , to keep + * it accessible it's placed within the sam= e struct + * it points to. */ + socket_descriptor_t sd; socket_descriptor_t ctrl_sd; /* only used for UDP over Socks */ -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/757?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: Icd7f6a2ad350cdc2312b3e80fa0dbdd7e4311d2e Gerrit-Change-Number: 757 Gerrit-PatchSet: 5 Gerrit-Owner: its_Giaan Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: ordex Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: its_Giaan Gerrit-Attention: flichtenheld Gerrit-Attention: ordex Gerrit-MessageType: newpatchset --3tOhxAKj5Vo= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld, its_Giaan, ordex, p= laisthos.

its_Giaan uploaded patch set #5 to this change.

<= a href=3D"http://gerrit.openvpn.net/c/openvpn/+/757?usp=3Demail">View Chang= e

The following approvals got outdated and were removed: Code-Review+2 by flichtenheld

The change is no longer submittable: Co= de-Review and checks~ChecksSubmitRule are unsatisfied now.

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

In order to pr= epare the event handling code to deal with multiple
listening sockets, w= e have to make sure that it is possible to
distinguish which of these so= ckets have been poked by an incoming
connection request.

To achie= ve that, this patch changes the object being passed as
event handler arg= ument, from a "partly integer-evaluated variable"
to a full st= ruct with a proper type attribute.

This struct will allow the code t= o carry around the particular
listening socket where the connection is b= eing established.

This change affects the TCP server code path only = as UDP servers
use only one socket to handle all clients.

Change-= Id: Icd7f6a2ad350cdc2312b3e80fa0dbdd7e4311d2e
Signed-off-by: Antonio Qua= rtulli <a@...2181...>
Signed-off-by: Gianmarco De Gregori <gian= marco@...2726...>
---
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, 68 insertions(+), 28 delet= ions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/chan=
ges/57/757/5
diff --git a/src/openvpn/event.h b/src/openvpn/event.h=

index 856551a..844ea7b 100644
--- a/src/op= envpn/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 EVE= NT_ARG_MULTI_INSTANCE */
+ } u;
+};
+
/*
* maxevents on input: desired max number of event_t descri= ptors
* simultaneously set with even= t_ctl
diff --git a/src/openvpn/mtcp.c b/src/openvpn/mtcp.c<= /span>
index 96408d1..c002a38 100644
--- a/src/ope= nvpn/mtcp.c
+++ b/src/openvpn/mtcp.c
@@ -54= ,7 +54,6 @@
/*
* Special tags passed to = event.[ch] functions
*/
-#define MTCP_SOCKET ((void *)1)
#= define MTCP_TUN ((void *)2)
#define MTCP_SIG = ((void *)3) /* Only on Windows */
#define MTCP_MANAGEME= NT ((void *)4)
@@ -253,7 +252,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,<= /span>
&mi->tcp_rwflags);
}

}
@@ -263,8 +262,8 @@
= struct multi_tcp *mtcp)
{
= int status;
= - unsigned int *persistent =3D &mtcp->tun_rwflags;
- socket_set_listen_persistent(c-&= gt;c2.link_socket, mtcp->es, MTCP_SOCKET);

+ socket_set_listen_persistent(c->c2.link_so= cket, mtcp->es,
+ = &c->c2.link_socket->ev_arg);

#ifdef _WIN32
if (tunta= p_is_wintun(c->c1.tuntap))
@@ -280,7 +279,7 @@ persistent =3D NULL;
}
#endif

- tun_set(c= ->c1.tuntap, mtcp->es, EVENT_READ, MTCP_TUN, persistent);
<= span style=3D"color: hsl(120, 100%, 40%);">+ tun_set(c->c1.tuntap, mt= cp->es, EVENT_READ, MTCP_TUN, &mtcp->tun_rwflags);

#if defined(TARGET_LINUX) || defined(TARGET_FREEBSD)

= dco_event_set(&c->c1.tuntap->dco, mtcp->es, MTCP_DCO);
#endif
@@ -693,20 +692,43 @@
= {
struct event_set_return *e =3D &mtcp->= esr[i];

- /* incoming data for instance? */
+ /* incoming data for instance or listenin= g socket? */
if (e->arg >=3D MTCP_N)<= br> {
- = struct multi_instance *mi =3D (struct multi_instance *) e->arg= ;
- if (mi)<= /span>
+ struct e= vent_arg *ev_arg =3D (struct event_arg *)e->arg;
+ 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, "MULT= I: mtcp_proc_io: null minstance");
+ break;
+ }
+
+ mi =3D ev_arg->u.mi;
+ if (e->rwflag= s & EVENT_WRITE)
= + {
+ = }
+ = else if (e->rwflags & EVENT_READ)
+ {
+ multi_tcp_action(= m, mi, TA_SOCKET_READ, false);
+ }
+ break;
+
= + /* new incoming TCP client attempting to connect? */
+ case EVEN= T_ARG_LINK_SOCKET:
+ = ASSERT(m->top.c2.link_socket);
+ socket_reset_listen_p= ersistent(m->top.c2.link_socket);
+ mi =3D multi_create_instance_tcp(m);
+ i= f (mi)
+ = {
+ = multi_tcp_action(m, mi, TA_INITIAL, false);
+ }
+ break; }
}
e= lse
@@ -731,18 +753,6 @@
= multi_tcp_action(m, NULL, TA_TUN_READ, false);
= }
}
- /* new incoming TCP client attempting to con= nect? */
- e= lse if (e->arg =3D=3D MTCP_SOCKET)
- {
- struct multi_instance *mi;
- ASSERT(m->top.c2.link_socke= t);
- so= cket_reset_listen_persistent(m->top.c2.link_socket);
- mi =3D multi_create_insta= nce_tcp(m);
- = if (mi)
- = {
- = multi_tcp_action(m, mi, TA_INITIAL, false);
- }
- }
#if defined(ENAB= LE_DCO) && (defined(TARGET_LINUX) || defined(TARGET_FREEBSD))
/* incoming data on DCO? */
= else if (e->arg =3D=3D MTCP_DCO)
diff --git a/src/= openvpn/multi.c b/src/openvpn/multi.c
index 0509911..45b3cf= a 100644
--- a/src/openvpn/multi.c
+++ b/sr= c/openvpn/multi.c
@@ -823,6 +823,9 @@
= goto err;
}

+ mi->ev_arg.type =3D EVENT_ARG_MUL= TI_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 7167639..1b99ef7 100644=
--- a/src/openvpn/multi.h
+++ b/src/openvp= n/multi.h
@@ -102,6 +102,12 @@
struct = schedule_entry se; /* this must be the first element of the structure,
* We cast between this and sc= hedule_entry so the
* begi= nning of the struct must be identical */
+
+ struct event_arg ev_arg; /**< this struct will store a pointer t= o either mi or
+ = * link_socket, depending on the event type, to k= eep
+ = * it accessible it's placed within the same struct
+ = * it points to. */
+
struct gc_arena gc;
bool = halt;
int refcount;
diff --git a/src/o= penvpn/socket.c b/src/openvpn/socket.c
index 7b1e603..cf040= 90 100644
--- a/src/openvpn/socket.c
+++ b/= src/openvpn/socket.c
@@ -1830,6 +1830,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 2e583af..c152ab0 100644--- a/src/openvpn/socket.h
+++ b/src/openvpn/socket.= h
@@ -178,6 +178,11 @@
{
= struct link_socket_info info;

+ struct event_arg ev_arg; /**< th= is struct will store a pointer to either mi or
+ * link_socket, d= epending on the event type, to keep
+ * it accessible it's pl= aced within the same struct
+ * it points to. */
+
socket_descrip= tor_t sd;
socket_descriptor_t ctrl_sd; /* only used fo= r UDP over Socks */


T= o view, visit change 757. To unsubscribe, or for help writing mail filters, visi= t settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Icd7f6a2ad350cdc2312b3e80fa0= dbdd7e4311d2e
Gerrit-Change-Number: 757
Gerrit-PatchSet: 5
Gerrit-Owner: its_Giaan <gianmarco@...2786...= ..>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: ordex <a@...2181...>
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-Attention: ordex <a@...2181...> <= /div>
Gerrit-MessageType: newpatchset
--3tOhxAKj5Vo=--