From: "flichtenheld (Code Review)" Date: Tue, 2 Sep 2025 12:05:21 +0000 References: Reply-To: frank@...2641..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="y33lhxOrUZY="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: socks: factor out socks_proxy_recv_char() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos Cc: openvpn-devel --y33lhxOrUZY= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: plaisthos. Hello plaisthos, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/1165?usp=3Demail to review the following change. Change subject: socks: factor out socks_proxy_recv_char() ...................................................................... socks: factor out socks_proxy_recv_char() This is basically identical code duplicated three times. Change-Id: I6e848df8104b6e3a6e2c3245ece91a20e473fb40 Signed-off-by: Frank Lichtenheld --- M src/openvpn/socks.c 1 file changed, 51 insertions(+), 121 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/65/1165/1 diff --git a/src/openvpn/socks.c b/src/openvpn/socks.c index 481d3fb..1102421 100644 --- a/src/openvpn/socks.c +++ b/src/openvpn/socks.c @@ -81,6 +81,54 @@ } static bool +socks_proxy_recv_char(char *c, const char *name, socket_descriptor_t sd, + struct event_timeout *server_poll_timeout, + volatile int *signal_received) +{ + fd_set reads; + FD_ZERO(&reads); + openvpn_fd_set(sd, &reads); + + struct timeval tv; + tv.tv_sec =3D get_server_poll_remaining_time(server_poll_timeout); + tv.tv_usec =3D 0; + + const int status =3D 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; +} + +static bool socks_username_password_auth(struct socks_proxy_info *p, socket_descriptor= _t sd, struct event_timeout *server_poll_timeout, volatile int *signal_received) @@ -121,52 +169,12 @@ while (len < 2) { - int status; - ssize_t size; - fd_set reads; - struct timeval tv; char c; - FD_ZERO(&reads); - openvpn_fd_set(sd, &reads); - tv.tv_sec =3D get_server_poll_remaining_time(server_poll_timeout); - tv.tv_usec =3D 0; - - status =3D select(sd + 1, &reads, NULL, NULL, &tv); - - get_signal(signal_received); - if (*signal_received) + if (!socks_proxy_recv_char(&c, __func__, sd, server_poll_timeout, = signal_received)) { goto cleanup; } - - /* timeout? */ - if (status =3D=3D 0) - { - msg(D_LINK_ERRORS | M_ERRNO, - "socks_username_password_auth: TCP port read timeout expir= ed"); - goto cleanup; - } - - /* error */ - if (status < 0) - { - msg(D_LINK_ERRORS | M_ERRNO, - "socks_username_password_auth: TCP port read failed on sel= ect()"); - goto cleanup; - } - - /* read single char */ - size =3D recv(sd, &c, 1, MSG_NOSIGNAL); - - /* error? */ - if (size !=3D 1) - { - msg(D_LINK_ERRORS | M_ERRNO, - "socks_username_password_auth: TCP port read failed on rec= v()"); - goto cleanup; - } - /* store char in buffer */ buf[len++] =3D c; } @@ -208,49 +216,12 @@ while (len < 2) { - int status; - ssize_t size; - fd_set reads; - struct timeval tv; char c; - FD_ZERO(&reads); - openvpn_fd_set(sd, &reads); - tv.tv_sec =3D get_server_poll_remaining_time(server_poll_timeout); - tv.tv_usec =3D 0; - - status =3D select(sd + 1, &reads, NULL, NULL, &tv); - - get_signal(signal_received); - if (*signal_received) + if (!socks_proxy_recv_char(&c, __func__, sd, server_poll_timeout, = signal_received)) { return false; } - - /* timeout? */ - if (status =3D=3D 0) - { - msg(D_LINK_ERRORS | M_ERRNO, "socks_handshake: TCP port read t= imeout expired"); - return false; - } - - /* error */ - if (status < 0) - { - msg(D_LINK_ERRORS | M_ERRNO, "socks_handshake: TCP port read f= ailed on select()"); - return false; - } - - /* read single char */ - size =3D recv(sd, &c, 1, MSG_NOSIGNAL); - - /* error? */ - if (size !=3D 1) - { - msg(D_LINK_ERRORS | M_ERRNO, "socks_handshake: TCP port read f= ailed on recv()"); - return false; - } - /* store char in buffer */ buf[len++] =3D c; } @@ -317,54 +288,13 @@ while (len < 4 + alen + 2) { - int status; - ssize_t size; - fd_set reads; - struct timeval tv; char c; - FD_ZERO(&reads); - openvpn_fd_set(sd, &reads); - tv.tv_sec =3D get_server_poll_remaining_time(server_poll_timeout); - tv.tv_usec =3D 0; - - status =3D select(sd + 1, &reads, NULL, NULL, &tv); - - get_signal(signal_received); - if (*signal_received) + if (!socks_proxy_recv_char(&c, __func__, sd, server_poll_timeout, = signal_received)) { return false; } - /* timeout? */ - if (status =3D=3D 0) - { - msg(D_LINK_ERRORS | M_ERRNO, "recv_socks_reply: TCP port read = timeout expired"); - return false; - } - - /* error */ - if (status < 0) - { - msg(D_LINK_ERRORS | M_ERRNO, "recv_socks_reply: TCP port read = failed on select()"); - return false; - } - - /* read single char */ - size =3D recv(sd, &c, 1, MSG_NOSIGNAL); - - /* error? */ - if (size < 0) - { - msg(D_LINK_ERRORS | M_ERRNO, "recv_socks_reply: TCP port read = failed on recv()"); - return false; - } - else if (size =3D=3D 0) - { - msg(D_LINK_ERRORS, "ERROR: recv_socks_reply: empty response fr= om socks server"); - return false; - } - if (len =3D=3D 3) { atyp =3D c; -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1165?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: I6e848df8104b6e3a6e2c3245ece91a20e473fb40 Gerrit-Change-Number: 1165 Gerrit-PatchSet: 1 Gerrit-Owner: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-MessageType: newchange --y33lhxOrUZY= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: plaisthos.

flichtenheld would like plaisthos to review this change.

V= iew Change

socks: factor out socks_proxy_recv_char()

This is bas= ically identical code duplicated three
times.

Change-Id: I6e848df= 8104b6e3a6e2c3245ece91a20e473fb40
Signed-off-by: Frank Lichtenheld <f= rank@...2641...>
---
M src/openvpn/socks.c
1 file changed, 51 i= nsertions(+), 121 deletions(-)

git pull ssh://gerrit.openvpn.net:=
29418/openvpn refs/changes/65/1165/1
diff --git a/src/openvpn/socks=
.c b/src/openvpn/socks.c
index 481d3fb..1102421 100644
--- a/src/openvpn/socks.c
+++ b/src/openvpn/soc= ks.c
@@ -81,6 +81,54 @@
}
=
static bool
+socks_proxy_recv_char(char *c, const char *name, socket_descript= or_t sd,
+ = struct event_timeout *server_poll_timeout,
+ volatile int *signa= l_received)
+{=
+ fd_set reads;+ FD_ZERO(&reads);
+ openvpn_fd_set(sd, = &reads);
+=
+ struct timeval tv;
+ tv.tv_sec =3D get_s= erver_poll_remaining_time(server_poll_timeout);
+ tv.tv_usec =3D 0;
+
+ const int status =3D select(sd + 1, &reads, NULL, NULL, = &tv);
++ get_signal(signal_receive= d);
+ if (*signal_= received)
+ {
+ return false;
+ }
+
+ /* timeout? */
+ if (status =3D=3D 0)
+ {
+ msg(D_LINK_ERRORS | M_ERRNO, "%s: TCP port read time= out expired", name);
+ return false;
+ }
+
+ /* error */+ if (status < 0)=
+ {
+ msg(D_LINK_ERRORS | M_ERRNO, &qu= ot;%s: TCP port read failed on select()", name);
+ return false;
+ }
+
+ 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 fa= iled on recv()", name);
+ return false;
+ }
+
+ return true;
+}
+
+static bool
socks_username_password_auth(st= ruct socks_proxy_info *p, socket_descriptor_t sd,
= struct event_timeout *server_poll_timeout,
<= span> volatile int *signal_received)
@@ -121,52 +169,12 @@

while (= len < 2)
{
- int status;
- ssize_t size;
- fd_set reads;
- struct timeval tv;
char c;

- = FD_ZERO(&reads);
- = openvpn_fd_set(sd, &reads);
- tv.tv_sec =3D get_server_poll_remaining_time(serve= r_poll_timeout);
- = tv.tv_usec =3D 0;
-<= /span>
- status =3D sel= ect(sd + 1, &reads, NULL, NULL, &tv);
-
- if (*signal_received)
+ if (!socks_proxy_recv_char(&c, __fu= nc__, sd, server_poll_timeout, signal_received))
{=
goto cleanup;
}
-
- /* timeout? */
- if (status =3D=3D 0)
- {
- msg(D_LINK_ERRORS | M_ERRNO,
= - "socks_user= name_password_auth: TCP port read timeout expired");
- goto cleanup;
- }

-
- /* error */
- if (status < 0)
- {
- = msg(D_LINK_ERRORS | M_ERRNO,
- "socks_username_password_auth: TCP po= rt read failed on select()");
- goto cleanup;
- }
-
- /* read = single char */
- = size =3D recv(sd, &c, 1, MSG_NOSIGNAL);
-
- /* error? */
= - if (size !=3D 1)
- {
- = msg(D_LINK_ERRORS | M_ERRNO,
- "socks_username_password_auth: TCP port rea= d failed on recv()");
- goto cleanup;
- }
-
/* store char in buffer */
= buf[len++] =3D c;
}
@@ -208,49 +216,12= @@

while (len < 2)
{

- int = status;
- ssize_= t size;
- fd_set= reads;
- struct= timeval tv;
char c;

<= span style=3D"color: hsl(0, 100%, 40%);">- FD_ZERO(&reads);
- openvpn_fd_set(sd,= &reads);
- = tv.tv_sec =3D get_server_poll_remaining_time(server_poll_timeout);- tv.tv_usec =3D 0;
-
- status =3D select(sd + 1, &reads, N= ULL, NULL, &tv);
-<= /span>
- get_signal(sig= nal_received);
- = if (*signal_received)
-
- if (status =3D=3D 0)
- {
- = msg(D_LINK_ERRORS | M_ERRNO, "socks_handshake: TCP port rea= d timeout expired");
- return false;
- }
-
- /* error */
- if (status < 0)<= /span>
- {
- msg(D_LINK_ERRORS | M_= ERRNO, "socks_handshake: TCP port read failed on select()");
- return false;<= /span>
- }
-

- /* read single char */
- size =3D recv(sd, &c, 1, MSG_NOSIG= NAL);
-
- /* error? */

- if (size !=3D 1)
- {

- msg(D_LINK_ERRORS | M_ERRNO, "= socks_handshake: TCP port read failed on recv()");
- return false;
- }

-
/* store char in buffer= */
buf[len++] =3D c;
}
@@ -317,54 +288,13 @@

whi= le (len < 4 + alen + 2)
{
- int status;
- ssize_t size;
- fd_set reads;
- struct timeval tv;
= char c;

- FD_ZERO(&reads);
- openvpn_fd_set(sd, &reads);
- tv.tv_sec =3D get_server_poll_remai= ning_time(server_poll_timeout);
- tv.tv_usec =3D 0;
-
- = status =3D select(sd + 1, &reads, NULL, NULL, &tv);
-

- get_signal(signal_received);
- if (*signal_received)
+ if (!socks_proxy_recv_ch= ar(&c, __func__, sd, server_poll_timeout, signal_received))

<= span> {

return false;
= }

- /* timeout? */
- if (status =3D=3D 0)
- {
- return false;
- }
= -
- /* error */<= /span>
- if (status <= ; 0)
- {<= br>- msg(D_LINK_ERRORS= | M_ERRNO, "recv_socks_reply: TCP port read failed on select()")= ;
- return f= alse;
- }=
-
- /* read single char */
- size =3D recv(sd, &c, 1, MSG= _NOSIGNAL);
-- /* error? */
= - if (size < 0)<= br>- {
- msg(D_LINK_ERRORS | M_ERRNO, = "recv_socks_reply: TCP port read failed on recv()");
- return false;
- }
- else if (size =3D=3D 0)
= - {
- msg(D_LINK_ERRORS, "ERROR:= recv_socks_reply: empty response from socks server");
- return false;

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

-
if (len =3D=3D 3)=
{
atyp =3D c;=

To view, visit change 1165. To unsubscribe, or fo= r help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I6e848df8104b6e3a6e2c3245ece= 91a20e473fb40
Gerrit-Change-Number: 1165
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <frank@...2723...= .>
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-MessageType: newchange
--y33lhxOrUZY=--