From 09955b7ce8e82f6948c74aa6fba14ed68d899b59 Mon Sep 17 00:00:00 2001 From: Toby Webb Date: Mon, 6 Dec 2021 10:10:25 +0100 Subject: [PATCH] net.websocket: fix server not listening for IPv4 (#12717) --- vlib/net/address.v | 24 +++++++++++++++++++++--- vlib/net/udp.v | 6 +++++- vlib/net/udp_test.v | 20 ++++++-------------- vlib/net/websocket/websocket_test.v | 10 ++-------- 4 files changed, 34 insertions(+), 26 deletions(-) diff --git a/vlib/net/address.v b/vlib/net/address.v index bb204e152e..868f649a8a 100644 --- a/vlib/net/address.v +++ b/vlib/net/address.v @@ -164,8 +164,15 @@ pub fn resolve_ipaddrs(addr string, family AddrFamily, typ SocketType) ?[]Addr { address, port := split_address(addr) ? if addr[0] == `:` { - // Use in6addr_any - return [new_ip6(port, net.addr_ip6_any)] + match family { + .ip6 { + return [new_ip6(port, net.addr_ip6_any)] + } + .ip, .unspec { + return [new_ip(port, net.addr_ip_any)] + } + else {} + } } mut hints := C.addrinfo{ @@ -200,7 +207,18 @@ pub fn resolve_ipaddrs(addr string, family AddrFamily, typ SocketType) ?[]Addr { for result := results; !isnil(result); result = result.ai_next { match AddrFamily(result.ai_family) { - .ip, .ip6 { + .ip { + new_addr := Addr{ + addr: AddrData{ + Ip: Ip{} + } + } + unsafe { + C.memcpy(&new_addr, result.ai_addr, result.ai_addrlen) + } + addresses << new_addr + } + .ip6 { new_addr := Addr{ addr: AddrData{ Ip6: Ip6{} diff --git a/vlib/net/udp.v b/vlib/net/udp.v index 2fac9f3cc2..1da7396066 100644 --- a/vlib/net/udp.v +++ b/vlib/net/udp.v @@ -237,7 +237,11 @@ fn new_udp_socket_for_remote(raddr Addr) ?&UdpSocket { } } match raddr.family() { - .ip, .ip6 { + .ip { + // Use ip dualstack + addr = new_ip(0, addr_ip_any) + } + .ip6 { // Use ip6 dualstack addr = new_ip6(0, addr_ip6_any) } diff --git a/vlib/net/udp_test.v b/vlib/net/udp_test.v index 83675a2c75..a05121e576 100644 --- a/vlib/net/udp_test.v +++ b/vlib/net/udp_test.v @@ -15,21 +15,20 @@ fn echo_server(mut c net.UdpConn) { } const ( - local_addr = ':40003' - remote_addr = 'localhost:40003' + server_addr = '127.0.0.1:40003' ) fn echo() ? { - mut c := net.dial_udp(remote_addr) ? + mut c := net.dial_udp(server_addr) or { panic('could not net.dial_udp: $err') } defer { c.close() or {} } data := 'Hello from vlib/net!' - c.write_string(data) ? + c.write_string(data) or { panic('could not write_string: $err') } mut buf := []byte{len: 100, init: 0} - read, addr := c.read(mut buf) ? + read, addr := c.read(mut buf) or { panic('could not read: $err') } assert read == data.len println('Got address $addr') @@ -47,17 +46,10 @@ fn echo() ? { } fn test_udp() { - mut l := net.listen_udp(local_addr) or { - println(err) - assert false - panic('') - } + mut l := net.listen_udp(server_addr) or { panic('could not listen_udp: $err') } go echo_server(mut l) - echo() or { - println(err) - assert false - } + echo() or { panic('could not echo: $err') } l.close() or {} } diff --git a/vlib/net/websocket/websocket_test.v b/vlib/net/websocket/websocket_test.v index 35e15d3ffc..3ec1338143 100644 --- a/vlib/net/websocket/websocket_test.v +++ b/vlib/net/websocket/websocket_test.v @@ -4,11 +4,6 @@ import net.websocket import time import rand -// TODO: fix connecting to ipv4 websockets -// (the server seems to work with .ip, but -// Client can not connect, it needs to be passed -// .ip too?) - struct WebsocketTestResults { pub mut: nr_messages int @@ -34,8 +29,7 @@ fn test_ws_ipv6() { // tests with internal ws servers fn test_ws_ipv4() { - // TODO: fix client - if true || should_skip { + if should_skip { return } port := 30000 + rand.intn(1024) @@ -68,7 +62,7 @@ fn start_server(family net.AddrFamily, listen_port int) ? { s.on_close(fn (mut ws websocket.Client, code int, reason string) ? { // not used }) - s.listen() or {} + s.listen() or { panic('websocket server could not listen') } } // ws_test tests connect to the websocket server from websocket client