From: "cron2 (Code Review)" Date: Thu, 16 Oct 2025 11:21:13 +0000 In-Reply-To: References: Reply-To: gert@...1296..., 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="e3EGYaoK6do="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: proxy: factor out recv_char code common with socks proxy List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld , plaisthos Cc: openvpn-devel --e3EGYaoK6do= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#3) to the change originally created by= flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1278?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by cron2 Change subject: proxy: factor out recv_char code common with socks proxy ...................................................................... proxy: factor out recv_char code common with socks proxy Change-Id: I70620aca638847168f06b0fb23cc04bd279d7df9 Signed-off-by: Frank Lichtenheld Acked-by: Gert Doering Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1278 Message-Id: <20251016103143.4461-1-gert@...1296...> URL: https://sourceforge.net/p/openvpn/mailman/message/59247456/ Signed-off-by: Gert Doering --- M src/openvpn/proxy.c M src/openvpn/proxy.h M src/openvpn/socks.c 3 files changed, 65 insertions(+), 94 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/78/1278/3 diff --git a/src/openvpn/proxy.c b/src/openvpn/proxy.c index dfe1e59..4205991 100644 --- a/src/openvpn/proxy.c +++ b/src/openvpn/proxy.c @@ -57,6 +57,50 @@ /* cached proxy username/password */ static struct user_pass static_proxy_user_pass; +bool +proxy_recv_char(uint8_t *c, const char *name, socket_descriptor_t sd, + struct timeval *timeout, volatile int *signal_received) +{ + fd_set reads; + FD_ZERO(&reads); + openvpn_fd_set(sd, &reads); + + const int status =3D openvpn_select(sd + 1, &reads, NULL, NULL, timeou= t); + + get_signal(signal_received); + if (*signal_received) + { + return false; + } + + /* timeout? */ + if (status =3D=3D 0) + { + msg(D_LINK_ERRORS | M_ERRNO, "%s: TCP port read timeout expired", = name); + return false; + } + + /* error */ + if (status < 0) + { + msg(D_LINK_ERRORS | M_ERRNO, "%s: TCP port read failed on select()= ", name); + return false; + } + + /* read single char */ + const ssize_t size =3D recv(sd, (void *)c, 1, MSG_NOSIGNAL); + + /* error? */ + if (size !=3D 1) + { + msg(D_LINK_ERRORS | M_ERRNO, "%s: TCP port read failed on recv()",= name); + return false; + } + + return true; +} + + static bool recv_line(socket_descriptor_t sd, char *buf, int len, const int timeout_se= c, const bool verbose, struct buffer *lookahead, volatile int *signal_received) @@ -72,9 +116,6 @@ while (true) { - int status; - ssize_t size; - fd_set reads; struct timeval tv; uint8_t c; @@ -83,50 +124,12 @@ ASSERT(buf_init(&la, 0)); } - FD_ZERO(&reads); - openvpn_fd_set(sd, &reads); tv.tv_sec =3D timeout_sec; tv.tv_usec =3D 0; - status =3D openvpn_select(sd + 1, &reads, NULL, NULL, &tv); - - get_signal(signal_received); - if (*signal_received) + if (!proxy_recv_char(&c, "recv_line", sd, &tv, signal_received)) { - goto error; - } - - /* timeout? */ - if (status =3D=3D 0) - { - if (verbose) - { - msg(D_LINK_ERRORS | M_ERRNO, "recv_line: TCP port read tim= eout expired"); - } - goto error; - } - - /* error */ - if (status < 0) - { - if (verbose) - { - msg(D_LINK_ERRORS | M_ERRNO, "recv_line: TCP port read fai= led on select()"); - } - goto error; - } - - /* read single char */ - size =3D recv(sd, (void *)&c, 1, MSG_NOSIGNAL); - - /* error? */ - if (size !=3D 1) - { - if (verbose) - { - msg(D_LINK_ERRORS | M_ERRNO, "recv_line: TCP port read fai= led on recv()"); - } - goto error; + return false; } #if 0 @@ -179,9 +182,6 @@ } return true; - -error: - return false; } static bool diff --git a/src/openvpn/proxy.h b/src/openvpn/proxy.h index be16d83..3bfa687 100644 --- a/src/openvpn/proxy.h +++ b/src/openvpn/proxy.h @@ -80,6 +80,9 @@ void http_proxy_close(struct http_proxy_info *hp); +bool proxy_recv_char(uint8_t *c, const char *name, socket_descriptor_t sd, + struct timeval *timeout, volatile int *signal_receive= d); + bool establish_http_proxy_passthru(struct http_proxy_info *p, socket_descriptor_t sd, /* already open= to proxy */ const char *host, /* openvpn serv= er remote */ diff --git a/src/openvpn/socks.c b/src/openvpn/socks.c index d383ef7..9dc013e 100644 --- a/src/openvpn/socks.c +++ b/src/openvpn/socks.c @@ -81,7 +81,7 @@ } static bool -socks_proxy_recv_char(char *c, const char *name, socket_descriptor_t sd, +socks_proxy_recv_char(uint8_t *c, const char *name, socket_descriptor_t sd, struct event_timeout *server_poll_timeout, volatile int *signal_received) { @@ -93,39 +93,7 @@ tv.tv_sec =3D get_server_poll_remaining_time(server_poll_timeout); tv.tv_usec =3D 0; - const int status =3D openvpn_select(sd + 1, &reads, NULL, NULL, &tv); - - get_signal(signal_received); - if (*signal_received) - { - return false; - } - - /* timeout? */ - if (status =3D=3D 0) - { - msg(D_LINK_ERRORS | M_ERRNO, "%s: TCP port read timeout expired", = name); - return false; - } - - /* error */ - if (status < 0) - { - msg(D_LINK_ERRORS | M_ERRNO, "%s: TCP port read failed on select()= ", name); - return false; - } - - /* read single char */ - const ssize_t size =3D recv(sd, c, 1, MSG_NOSIGNAL); - - /* error? */ - if (size !=3D 1) - { - msg(D_LINK_ERRORS | M_ERRNO, "%s: TCP port read failed on recv()",= name); - return false; - } - - return true; + return proxy_recv_char(c, name, sd, &tv, signal_received); } static bool @@ -165,10 +133,10 @@ } int len =3D 0; - char buf[2]; + uint8_t buf[2]; while (len < 2) { - char c; + uint8_t c; if (!socks_proxy_recv_char(&c, __func__, sd, server_poll_timeout, = signal_received)) { @@ -196,12 +164,12 @@ socks_handshake(struct socks_proxy_info *p, socket_descriptor_t sd, struct event_timeout *server_poll_timeout, volatile int *s= ignal_received) { - char buf[2]; + uint8_t buf[2]; int len =3D 0; ssize_t size; /* VER =3D 5, NMETHODS =3D 1, METHODS =3D [0 (no auth)] */ - char method_sel[3] =3D { 0x05, 0x01, 0x00 }; + uint8_t method_sel[3] =3D { 0x05, 0x01, 0x00 }; if (p->authfile[0]) { method_sel[2] =3D 0x02; /* METHODS =3D [2 (plain login)] */ @@ -215,7 +183,7 @@ while (len < 2) { - char c; + uint8_t c; if (!socks_proxy_recv_char(&c, __func__, sd, server_poll_timeout, = signal_received)) { @@ -226,7 +194,7 @@ } /* VER =3D=3D 5 */ - if (buf[0] !=3D '\x05') + if (buf[0] !=3D 5) { msg(D_LINK_ERRORS, "socks_handshake: Socks proxy returned bad stat= us"); return false; @@ -273,7 +241,7 @@ recv_socks_reply(socket_descriptor_t sd, struct openvpn_sockaddr *addr, struct event_timeout *server_poll_timeout, volatile int *= signal_received) { - char atyp =3D '\0'; + uint8_t atyp =3D 0; int alen =3D 0; int len =3D 0; char buf[270]; /* 4 + alen(max 256) + 2 */ @@ -287,7 +255,7 @@ while (len < 4 + alen + 2) { - char c; + uint8_t c; if (!socks_proxy_recv_char(&c, __func__, sd, server_poll_timeout, = signal_received)) { @@ -303,18 +271,18 @@ { switch (atyp) { - case '\x01': /* IP V4 */ + case 1: /* IP V4 */ alen =3D 4; break; - case '\x03': /* DOMAINNAME */ + case 3: /* DOMAINNAME */ /* RFC 1928, section 5: 1 byte length, bytes name, * so the total "address length" is (length+1) */ - alen =3D (unsigned char)c + 1; + alen =3D c + 1; break; - case '\x04': /* IP V6 */ + case 4: /* IP V6 */ alen =3D 16; break; @@ -333,14 +301,14 @@ } /* VER =3D=3D 5 && REP =3D=3D 0 (succeeded) */ - if (buf[0] !=3D '\x05' || buf[1] !=3D '\x00') + if (buf[0] !=3D 5 || buf[1] !=3D 0) { msg(D_LINK_ERRORS, "recv_socks_reply: Socks proxy returned bad rep= ly"); return false; } /* ATYP =3D=3D 1 (IP V4 address) */ - if (atyp =3D=3D '\x01' && addr !=3D NULL) + if (atyp =3D=3D 1 && addr !=3D NULL) { memcpy(&addr->addr.in4.sin_addr, buf + 4, sizeof(addr->addr.in4.si= n_addr)); memcpy(&addr->addr.in4.sin_port, buf + 8, sizeof(addr->addr.in4.si= n_port)); -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1278?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: newpatchset Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I70620aca638847168f06b0fb23cc04bd279d7df9 Gerrit-Change-Number: 1278 Gerrit-PatchSet: 3 Gerrit-Owner: flichtenheld Gerrit-Reviewer: cron2 Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel --e3EGYaoK6do= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 upl= oaded patch set #3 to the change originally created by flichtenhel= d.

View Change

The following approvals got outdated and were remove= d: Code-Review+2 by cron2

proxy: factor out recv_char code co=
mmon with socks proxy

Change-Id: I70620aca638847168f06b0fb23cc04bd27= 9d7df9
Signed-off-by: Frank Lichtenheld <frank@...2641...>
Acke= d-by: Gert Doering <gert@...1296...>
Gerrit URL: https://gerrit.op= envpn.net/c/openvpn/+/1278
Message-Id: <20251016103143.4461-1-gert@...1186...= .1296...>
URL: https://sourceforge.net/p/openvpn/mailman/message/5924= 7456/
Signed-off-by: Gert Doering <gert@...1296...>
---
M sr= c/openvpn/proxy.c
M src/openvpn/proxy.h
M src/openvpn/socks.c
3 fi= les changed, 65 insertions(+), 94 deletions(-)

g=
it pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/78/1278/3
=
diff --git a/src/openvpn/proxy.c b/src/openvpn/proxy.c
i= ndex dfe1e59..4205991 100644
--- a/src/openvpn/proxy.c
+++ b/src/openvpn/proxy.c
@@ -57,6 +57,50 @@
/* cached proxy username/password */
static= struct user_pass static_proxy_user_pass;

+bool

+proxy_recv_char(uint8_t *c, const char *name, sock= et_descriptor_t sd,
+= struct timeval *timeout, volatile int *signal_received)

+{
+ fd_set reads;
+ FD_ZERO(&reads);
+ openvpn_fd_set(sd, &reads);<= /span>
+
+ const int status =3D openvpn_select= (sd + 1, &reads, NULL, NULL, timeout);
+
+ get_signal(signal_received);
+ if (*signal_received)
+ {
+ return false;
+ }
+
+ /* timeout? */
+ if (status =3D=3D 0= )
+ {
+ msg(D_LINK_ERRORS | M_ER= RNO, "%s: TCP port read timeout expired", name);

+ return false;
+ }

+
+ /* error */
+ if (status < 0)
+ {
+ = msg(D_LINK_ERRORS | M_ERRNO, "%s: TCP port read failed on select()&qu= ot;, name);
+ = return false;
+ }<= /span>
+
+ /* read single char */
+ const ssize_t size =3D recv(= sd, (void *)c, 1, MSG_NOSIGNAL);

+
+ /*= error? */
+ if (s= ize !=3D 1)
+ {
+ msg(D_LINK_ERR= ORS | M_ERRNO, "%s: TCP port read failed on recv()", name);
+ return false;
+ }
+
+ return true;
+}
+
+
sta= tic bool
recv_line(socket_descriptor_t sd, char *buf, int = len, const int timeout_sec, const bool verbose,
= struct buffer *lookahead, volatile int *signal_received)
@@= -72,9 +116,6 @@

while (true)=
{
- = int status;
- = ssize_t size;
- = fd_set reads;
struct timeval tv;
= uint8_t c;

@@ -83,50 +124,12 @@<= /span>
ASSERT(buf_init(&la, 0));
= }

- FD_ZERO(&reads);
- openvpn_fd_set(sd, &reads);
= tv.tv_sec =3D timeout_sec;
tv.tv_usec =3D 0;

- = status =3D openvpn_select(sd + 1, &reads, NULL, NULL, &tv);
-
- get_signal(signal_received);
= - if (*signal_received)
+ if (!proxy_rec= v_char(&c, "recv_line", sd, &tv, signal_received))=
{
- = goto error;
= - }
-
= - /* timeout? */- if (status =3D=3D 0)
- {
- if (verbose)

<= span style=3D"color: hsl(0, 100%, 40%);">- {

- msg(D_LINK_ERRORS | M_ERR= NO, "recv_line: TCP port read timeout expired");
- }
- goto error;
- }
-
- = /* error */
- = if (status < 0)
- {
- = if (verbose)
- = {
- = msg(D_LINK_ERRORS | M_ERRNO, "recv_line: TCP port read failed on sele= ct()");
- = }
- goto = error;
- }
-
- /* read single char */
- size =3D recv(sd, (void *)&= c, 1, MSG_NOSIGNAL);
-<= /span>
- /* error? */
- if (size !=3D 1= )
- {
= - if (verbose)<= br>- {
- msg(D_LINK_ERRORS | M= _ERRNO, "recv_line: TCP port read failed on recv()");

<= span style=3D"color: hsl(0, 100%, 40%);">- }

- goto error;
+ return false;
<= span> }


#if 0
@@= -179,9 +182,6 @@
}

= return true;
-
-error:
- return false;
}

static bool
diff --git a/sr= c/openvpn/proxy.h b/src/openvpn/proxy.h
index be16d83..3bfa= 687 100644
--- a/src/openvpn/proxy.h
+++ b/= src/openvpn/proxy.h
@@ -80,6 +80,9 @@

void http_proxy_close(struct http_proxy_info *hp);
=
+bool proxy_r= ecv_char(uint8_t *c, const char *name, socket_descriptor_t sd,
+ struct time= val *timeout, volatile int *signal_received);

+
bool establish_http_proxy_passt= hru(struct http_proxy_info *p,
= socket_descriptor_t sd, /* already open to proxy */
const char *host, /* openvpn ser= ver remote */

diff --git a/src/openvpn/socks.c b/src/openvp= n/socks.c
index d383ef7..9dc013e 100644
---= a/src/openvpn/socks.c
+++ b/src/openvpn/socks.c
= @@ -81,7 +81,7 @@
}

static bool

-socks_p= roxy_recv_char(char *c, const char *name, socket_descriptor_t sd,+socks_proxy_recv_char(uint8_t= *c, const char *name, socket_descriptor_t sd,
= struct event_timeout *server_poll_timeout,
= volatile int *signal_received)
{<= br>@@ -93,39 +93,7 @@
tv.tv_sec =3D get_server_p= oll_remaining_time(server_poll_timeout);
tv.tv_usec = =3D 0;

-
<= span style=3D"color: hsl(0, 100%, 40%);">- get_signal(signal_received);<= /span>
- if (*signal_receiv= ed)
- {
- return false;

- }

-
- /* timeout? */
= - if (status =3D=3D 0)
- {
- msg(= D_LINK_ERRORS | M_ERRNO, "%s: TCP port read timeout expired", nam= e);
- return fal= se;
- }
-

- /* error */
- if (status < 0)
- {
- = msg(D_LINK_ERRORS | M_ERRNO, "%s: TCP port read failed on selec= t()", name);
- = return false;
- = }
-
- /* read single char */
- const ssize_t size =3D recv(sd, = c, 1, MSG_NOSIGNAL);

-<= /span>
- /* error? */
- if (size !=3D 1)=
- {
- msg(D_LINK_ERRORS | M_ERRNO, "= %s: TCP port read failed on recv()", name);
- return false;
- }
-
- return= true;
+ return pr= oxy_recv_char(c, name, sd, &tv, signal_received);
}

static bool
@@ -165,10 +13= 3,10 @@
}

int l= en =3D 0;
- char buf= [2];
+ uint8_t buf= [2];
while (len < 2)
{<= br>- char c;
+ uint8_t c;


if (!socks_proxy_recv_char(&c, __func__, s= d, server_poll_timeout, signal_received))
{=
@@ -196,12 +164,12 @@
socks_handshake(struct soc= ks_proxy_info *p, socket_descriptor_t sd,
= struct event_timeout *server_poll_timeout, volatile int *signal_received)
{
- = char buf[2];
+ uin= t8_t buf[2];
int len =3D 0;
ssize= _t size;

/* VER =3D 5, NMETHODS =3D = 1, METHODS =3D [0 (no auth)] */
- char method_sel[3] =3D { 0x05, 0x01, 0x00 };
+ uint8_t method_sel[3] =3D { 0x05= , 0x01, 0x00 };
if (p->authfile[0])
{

method_sel[2] =3D 0x02; /* METHODS =3D [2 = (plain login)] */
@@ -215,7 +183,7 @@

while (len < 2)
{
- char c;

+ uint8_t c;

if (!socks_proxy_recv_char(&c, __func__, sd, serve= r_poll_timeout, signal_received))
{
@@ -226,7 +194,7 @@

}

/* VER =3D=3D 5 */

- if (buf[0] !=3D '\x05')
+ if (buf[0] !=3D 5)
{<= br> msg(D_LINK_ERRORS, "socks_handshake: Socks proxy ret= urned bad status");
return false;
<= span>@@ -273,7 +241,7 @@

recv_socks_reply(socket_descripto= r_t sd, struct openvpn_sockaddr *addr,
st= ruct event_timeout *server_poll_timeout, volatile int *signal_received)
{
- ch= ar atyp =3D '\0';
+ uint8_t atyp =3D 0;
int alen =3D 0; int len =3D 0;
char buf[270]; /* 4 + alen= (max 256) + 2 */
@@ -287,7 +255,7 @@

while (len < 4 + alen + 2)
{
- char c;
<= span style=3D"color: hsl(120, 100%, 40%);">+ uint8_t c;


if (!socks_proxy_recv_char(&c, __func__,= sd, server_poll_timeout, signal_received))
{
@@ -303,18 +271,18 @@
{
switch (atyp)

{
- case '\x01': = /* IP V4 */

+ = case 1: /* IP V4 */
alen =3D 4= ;
break;

<= span style=3D"color: hsl(0, 100%, 40%);">- case '\x03= 9;: /* DOMAINNAME */

= + case 3: /* DOMAINNAME */
= /* RFC 1928, section 5: 1 byte length, <n> bytes name,
= * so the total "address length" is (l= ength+1)
*/
- alen =3D (unsigned char)c += 1;
+ = alen =3D c + 1;
break;
<= span>

- = case '\x04': /* IP V6 */
+ case 4: /* IP V6 */
= alen =3D 16;
break;=

@@ -333,14 +301,14 @@
}

/* VER =3D=3D 5 && REP =3D=3D = 0 (succeeded) */
- i= f (buf[0] !=3D '\x05' || buf[1] !=3D '\x00')
+ if (buf[0] !=3D 5 || buf[1] != =3D 0)

{
msg(D_LINK_ERRORS, &= quot;recv_socks_reply: Socks proxy returned bad reply");
return false;

}
/* ATYP =3D=3D 1 (IP V4 address) */
- if (atyp =3D=3D '\x01' && ad= dr !=3D NULL)
+ if= (atyp =3D=3D 1 && addr !=3D NULL)
{ memcpy(&addr->addr.in4.sin_addr, buf + 4, sizeof(add= r->addr.in4.sin_addr));
memcpy(&addr->ad= dr.in4.sin_port, buf + 8, sizeof(addr->addr.in4.sin_port));

To view, visit change 1278. To unsubscribe, or for help= writing mail filters, visit settings.

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I70620aca638847168f06b0fb23c= c04bd279d7df9
Gerrit-Change-Number: 1278
Gerrit-PatchSet: 3
Gerrit-Owner: flichtenheld <frank@...2723...= .>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
= --e3EGYaoK6do=--