net.websocket: fix server not listening for IPv4 (#12717)

pull/12743/head
Toby Webb 2021-12-06 10:10:25 +01:00 committed by GitHub
parent d85111e3dd
commit 09955b7ce8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 26 deletions

View File

@ -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{}

View File

@ -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)
}

View File

@ -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 {}
}

View File

@ -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