Skip to content

Commit

Permalink
proto_ws: enclose IPv6 Host IPs with square brackets
Browse files Browse the repository at this point in the history
Many thanks to @alekseiZh on GitHub for reporting it
Close #3198

(cherry picked from commit 3b4beb2)
  • Loading branch information
razvancrainea committed Sep 21, 2023
1 parent 6a60715 commit 09e8705
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions modules/proto_ws/ws_handshake_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"Sec-WebSocket-Protocol: " WS_PROTO_SIP HTTP_END
#define HTTP_HANDSHAKE_END_LEN (sizeof(HTTP_HANDSHAKE_END) - 1)

#define MAX_HOST_LEN IP_ADDR_MAX_STR_SIZE /*IP*/ + 1 /*':'*/ + 5 /*65535*/
#define MAX_HOST_LEN IP_ADDR_MAX_STR_SIZE /*IP*/ + 2 /* '[' & ']' for ipv6 */ + 1 /*':'*/ + 5 /*65535*/

#include "../../sha1.h"

Expand Down Expand Up @@ -1273,6 +1273,7 @@ static int ws_start_handshake(struct tcp_connection *c)
char *port;
int port_len;
static char host_orig_buf[MAX_HOST_LEN];
char *h;

str trace_str = { ws_trace_buf, 0 };

Expand Down Expand Up @@ -1303,14 +1304,21 @@ static int ws_start_handshake(struct tcp_connection *c)
ip = ip_addr2a(&c->rcv.src_ip);
port = int2str(c->rcv.src_port, &port_len);
n = strlen(ip);
memcpy(host_orig_buf, ip, n);
host_orig_buf[n] = ':';
memcpy(host_orig_buf + n + 1, port, port_len);
h = host_orig_buf;
if (c->rcv.src_ip.af == AF_INET6)
*h++ = '[';
memcpy(h, ip, n);
h += n;
if (c->rcv.src_ip.af == AF_INET6)
*h++ = ']';
*h++ = ':';
memcpy(h, port, port_len);
h += port_len;

iov[2].iov_base = _ws_common_resource.s;
iov[2].iov_len = _ws_common_resource.len;

iov[7].iov_len = n + port_len + 1;
iov[7].iov_len = h - host_orig_buf;
iov[10].iov_len = iov[7].iov_len;

iov[13].iov_base = WS_KEY(c).s;
Expand Down

0 comments on commit 09e8705

Please sign in to comment.