From: Frank Lichtenheld Date: Thu, 16 Oct 2025 10:37:22 +0000 (+0200) Subject: proxy: factor out send code common with socks proxy X-Git-Tag: v2.7_rc1~41 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=23d70b08a94b150f5df76449e2acbf7a1160146a;p=thirdparty%2Fopenvpn.git proxy: factor out send code common with socks proxy Change-Id: Ieb18101dcf8143efdae1c39bde356e7166cbefa5 Signed-off-by: Frank Lichtenheld Acked-by: Gert Doering Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1279 Message-Id: <20251016103730.5319-1-gert@greenie.muc.de> URL: https://sourceforge.net/p/openvpn/mailman/message/59247465/ Signed-off-by: Gert Doering --- diff --git a/src/openvpn/proxy.c b/src/openvpn/proxy.c index 42059914f..ff6ea5a28 100644 --- a/src/openvpn/proxy.c +++ b/src/openvpn/proxy.c @@ -184,13 +184,13 @@ recv_line(socket_descriptor_t sd, char *buf, int len, const int timeout_sec, con return true; } -static bool -send_line(socket_descriptor_t sd, const char *buf) +bool +proxy_send(socket_descriptor_t sd, const void *buf, size_t buf_len) { - const ssize_t size = openvpn_send(sd, buf, strlen(buf), MSG_NOSIGNAL); - if (size != (ssize_t)strlen(buf)) + const ssize_t size = openvpn_send(sd, buf, buf_len, MSG_NOSIGNAL); + if (size != (ssize_t)buf_len) { - msg(D_LINK_ERRORS | M_ERRNO, "send_line: TCP port write failed on send()"); + msg(D_LINK_ERRORS | M_ERRNO, "proxy_send: TCP port write failed on send()"); return false; } return true; @@ -201,10 +201,10 @@ send_line_crlf(socket_descriptor_t sd, const char *src) { bool ret; - struct buffer buf = alloc_buf(strlen(src) + 3); + struct buffer buf = alloc_buf(strlen(src) + 2); ASSERT(buf_write(&buf, src, strlen(src))); - ASSERT(buf_write(&buf, "\r\n", 3)); - ret = send_line(sd, BSTR(&buf)); + ASSERT(buf_write(&buf, "\r\n", 2)); + ret = proxy_send(sd, BSTR(&buf), BLEN(&buf)); free_buf(&buf); return ret; } diff --git a/src/openvpn/proxy.h b/src/openvpn/proxy.h index 3bfa687aa..d14725c22 100644 --- a/src/openvpn/proxy.h +++ b/src/openvpn/proxy.h @@ -83,6 +83,8 @@ 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_received); +bool proxy_send(socket_descriptor_t sd, const void *buf, size_t buf_len); + bool establish_http_proxy_passthru(struct http_proxy_info *p, socket_descriptor_t sd, /* already open to proxy */ const char *host, /* openvpn server remote */ diff --git a/src/openvpn/socks.c b/src/openvpn/socks.c index 9dc013e8a..7ecf01a2a 100644 --- a/src/openvpn/socks.c +++ b/src/openvpn/socks.c @@ -123,12 +123,8 @@ socks_username_password_auth(struct socks_proxy_info *p, socket_descriptor_t sd, creds.username, (int)strlen(creds.password), creds.password); ASSERT(sret >= 0 && sret <= sizeof(to_send)); - ssize_t size = openvpn_send(sd, to_send, strlen(to_send), MSG_NOSIGNAL); - - if (size != (ssize_t)strlen(to_send)) + if (!proxy_send(sd, to_send, strlen(to_send))) { - msg(D_LINK_ERRORS | M_ERRNO, - "socks_username_password_auth: TCP port write failed on send()"); goto cleanup; } @@ -166,7 +162,6 @@ socks_handshake(struct socks_proxy_info *p, socket_descriptor_t sd, { uint8_t buf[2]; int len = 0; - ssize_t size; /* VER = 5, NMETHODS = 1, METHODS = [0 (no auth)] */ uint8_t method_sel[3] = { 0x05, 0x01, 0x00 }; @@ -174,10 +169,8 @@ socks_handshake(struct socks_proxy_info *p, socket_descriptor_t sd, { method_sel[2] = 0x02; /* METHODS = [2 (plain login)] */ } - size = openvpn_send(sd, method_sel, sizeof(method_sel), MSG_NOSIGNAL); - if (size != sizeof(method_sel)) + if (!proxy_send(sd, method_sel, sizeof(method_sel))) { - msg(D_LINK_ERRORS | M_ERRNO, "socks_handshake: TCP port write failed on send()"); return false; } @@ -380,18 +373,11 @@ establish_socks_proxy_passthru(struct socks_proxy_info *p, buf[5 + len] = (char)(port >> 8); buf[5 + len + 1] = (char)(port & 0xff); + if (!proxy_send(sd, buf, 5 + len + 2)) { - size_t send_len = 5 + len + 2; - const ssize_t size = openvpn_send(sd, buf, send_len, MSG_NOSIGNAL); - if (size != (ssize_t)send_len) - { - msg(D_LINK_ERRORS | M_ERRNO, - "establish_socks_proxy_passthru: TCP port write failed on send()"); - goto error; - } + goto error; } - /* receive reply from Socks proxy and discard */ if (!recv_socks_reply(sd, NULL, server_poll_timeout, &sig_info->signal_received)) {