net: fix errors of new_ip(), new_ip6() and ip6.str() (#13020)

pull/13022/head
yuyi 2022-01-03 21:10:15 +08:00 committed by GitHub
parent 88a973b617
commit da989e19ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 11 deletions

View File

@ -24,7 +24,7 @@ fn new_ip6(port u16, addr [16]byte) Addr {
} }
} }
copy(a.addr.Ip6.addr[..], addr[..]) unsafe { vmemcpy(&a.addr.Ip6.addr[0], &addr[0], 16) }
return a return a
} }
@ -39,7 +39,7 @@ fn new_ip(port u16, addr [4]byte) Addr {
} }
} }
copy(a.addr.Ip.addr[..], addr[..]) unsafe { vmemcpy(&a.addr.Ip.addr[0], &addr[0], 4) }
return a return a
} }
@ -81,7 +81,7 @@ fn (a Ip) str() string {
} }
fn (a Ip6) str() string { fn (a Ip6) str() string {
buf := [net.max_ip_len]char{} buf := [net.max_ip6_len]char{}
res := &char(C.inet_ntop(.ip6, &a.addr, &buf[0], buf.len)) res := &char(C.inet_ntop(.ip6, &a.addr, &buf[0], buf.len))

View File

@ -98,15 +98,28 @@ fn test_sizes_unix() {
} }
fn test_ip_str() { fn test_ip_str() {
ip := new_ip(1337, addr_ip_any).str() ip1 := new_ip(1337, addr_ip_any).str()
expected := '0.0.0.0:1337' expected1 := '0.0.0.0:1337'
assert ip.len == expected.len assert ip1.len == expected1.len
assert ip == expected assert ip1 == expected1
addr := [byte(2), 0, 2, 2]!
ip2 := new_ip(2202, addr).str()
expected2 := '2.0.2.2:2202'
assert ip2.len == expected2.len
assert ip2 == expected2
} }
fn test_ip6_str() { fn test_ip6_str() {
ip := new_ip6(1337, addr_ip6_any).str() ip1 := new_ip6(1337, addr_ip6_any).str()
expected := '[::]:1337' expected1 := '[::]:1337'
assert ip.len == expected.len assert ip1.len == expected1.len
assert ip == expected assert ip1 == expected1
addr := [byte(2), 0, 2, 2, 2, 0, 1, 1, 2, 3, 2, 1, 2, 3, 5, 2]!
ip2 := new_ip6(2022, addr).str()
println(ip2)
expected2 := '[200:202:200:101:203:201:203:502]:2022'
assert ip2.len == expected2.len
assert ip2 == expected2
} }