mirror of
https://github.com/vlang/v.git
synced 2025-09-13 09:25:45 -04:00
net: add a .port()! method for net.Addr (#21412)
This commit is contained in:
parent
598de74185
commit
a4cdc48542
@ -60,6 +60,28 @@ pub fn (a Addr) family() AddrFamily {
|
||||
return unsafe { AddrFamily(a.f) }
|
||||
}
|
||||
|
||||
// port returns the ip or ip6 port of the given address `a`
|
||||
pub fn (a Addr) port() !u16 {
|
||||
match unsafe { AddrFamily(a.f) } {
|
||||
.ip {
|
||||
unsafe {
|
||||
return conv.ntoh16(a.addr.Ip.port)
|
||||
}
|
||||
}
|
||||
.ip6 {
|
||||
unsafe {
|
||||
return conv.ntoh16(a.addr.Ip6.port)
|
||||
}
|
||||
}
|
||||
.unix {
|
||||
return error('unix addr has no port')
|
||||
}
|
||||
.unspec {
|
||||
return error('unspec addr family when obtain port')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const max_ip_len = 24
|
||||
const max_ip6_len = 46
|
||||
|
||||
|
@ -1,5 +1,10 @@
|
||||
module net
|
||||
|
||||
fn test_ip_port() {
|
||||
assert new_ip(1234, addr_ip_any).port()! == 1234
|
||||
assert new_ip6(1234, addr_ip6_any).port()! == 1234
|
||||
}
|
||||
|
||||
fn test_diagnostics() {
|
||||
dump(aoffset)
|
||||
eprintln('--------')
|
||||
|
Loading…
x
Reference in New Issue
Block a user