2020-11-15 23:54:47 +03:00
|
|
|
module net
|
|
|
|
|
|
|
|
// Addr represents an ip address
|
|
|
|
pub struct Addr {
|
|
|
|
addr C.sockaddr
|
2021-02-09 01:48:23 +03:00
|
|
|
len int
|
2020-11-15 23:54:47 +03:00
|
|
|
pub:
|
|
|
|
saddr string
|
2021-02-09 01:48:23 +03:00
|
|
|
port int
|
2020-11-15 23:54:47 +03:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:41:52 +03:00
|
|
|
struct C.addrinfo {
|
|
|
|
}
|
|
|
|
|
2020-11-15 23:54:47 +03:00
|
|
|
pub fn (a Addr) str() string {
|
2021-02-09 01:48:23 +03:00
|
|
|
return '$a.saddr:$a.port'
|
2020-11-15 23:54:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
max_ipv4_addr_len = 24
|
2021-02-09 01:48:23 +03:00
|
|
|
ipv4_addr_size = sizeof(C.sockaddr_in)
|
2020-11-15 23:54:47 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
fn new_addr(addr C.sockaddr) ?Addr {
|
2021-01-08 19:41:52 +03:00
|
|
|
addr_len := if addr.sa_family == int(SocketFamily.inet) {
|
2020-11-15 23:54:47 +03:00
|
|
|
sizeof(C.sockaddr)
|
|
|
|
} else {
|
|
|
|
// TODO NOOOOOOOOOOOO
|
|
|
|
0
|
|
|
|
}
|
|
|
|
// Convert to string representation
|
2021-02-09 01:48:23 +03:00
|
|
|
buf := []byte{len: net.max_ipv4_addr_len, init: 0}
|
2020-11-15 23:54:47 +03:00
|
|
|
$if windows {
|
|
|
|
res := C.WSAAddressToStringA(&addr, addr_len, C.NULL, buf.data, &buf.len)
|
|
|
|
if res != 0 {
|
2021-02-09 01:48:23 +03:00
|
|
|
socket_error(-1) ?
|
2020-11-15 23:54:47 +03:00
|
|
|
}
|
|
|
|
} $else {
|
2021-04-04 17:43:32 +03:00
|
|
|
res := &char(C.inet_ntop(SocketFamily.inet, &addr, buf.data, buf.len))
|
2020-11-15 23:54:47 +03:00
|
|
|
if res == 0 {
|
2021-02-09 01:48:23 +03:00
|
|
|
socket_error(-1) ?
|
2020-11-15 23:54:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mut saddr := buf.bytestr()
|
|
|
|
|
2021-02-09 01:48:23 +03:00
|
|
|
hport := unsafe { &C.sockaddr_in(&addr) }.sin_port
|
2020-11-15 23:54:47 +03:00
|
|
|
port := C.ntohs(hport)
|
|
|
|
|
|
|
|
$if windows {
|
|
|
|
// strip the port from the address string
|
|
|
|
saddr = saddr.split(':')[0]
|
|
|
|
}
|
|
|
|
|
2021-02-09 01:48:23 +03:00
|
|
|
return Addr{addr, int(addr_len), saddr, port}
|
2020-11-15 23:54:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resolve_addr(addr string, family SocketFamily, typ SocketType) ?Addr {
|
2021-02-09 01:48:23 +03:00
|
|
|
address, port := split_address(addr) ?
|
2020-11-15 23:54:47 +03:00
|
|
|
|
|
|
|
mut hints := C.addrinfo{}
|
2021-01-08 19:41:52 +03:00
|
|
|
hints.ai_family = int(family)
|
|
|
|
hints.ai_socktype = int(typ)
|
2020-11-15 23:54:47 +03:00
|
|
|
hints.ai_flags = C.AI_PASSIVE
|
|
|
|
hints.ai_protocol = 0
|
|
|
|
hints.ai_addrlen = 0
|
|
|
|
hints.ai_canonname = C.NULL
|
|
|
|
hints.ai_addr = C.NULL
|
|
|
|
hints.ai_next = C.NULL
|
|
|
|
info := &C.addrinfo(0)
|
|
|
|
|
|
|
|
sport := '$port'
|
|
|
|
|
|
|
|
// This might look silly but is recommended by MSDN
|
|
|
|
$if windows {
|
2021-02-09 01:48:23 +03:00
|
|
|
socket_error(0 - C.getaddrinfo(address.str, sport.str, &hints, &info)) ?
|
2020-11-15 23:54:47 +03:00
|
|
|
} $else {
|
2021-02-09 01:48:23 +03:00
|
|
|
x := C.getaddrinfo(address.str, sport.str, &hints, &info)
|
|
|
|
wrap_error(x) ?
|
2020-11-15 23:54:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return new_addr(*info.ai_addr)
|
|
|
|
}
|