2020-11-15 23:54:47 +03:00
|
|
|
module net
|
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
import io.util
|
|
|
|
import os
|
|
|
|
|
|
|
|
union AddrData {
|
|
|
|
Unix
|
|
|
|
Ip
|
|
|
|
Ip6
|
2020-11-15 23:54:47 +03:00
|
|
|
}
|
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
const (
|
2022-04-15 15:55:39 +03:00
|
|
|
addr_ip6_any = [16]u8{init: u8(0)}
|
|
|
|
addr_ip_any = [4]u8{init: u8(0)}
|
2021-06-13 23:53:38 +03:00
|
|
|
)
|
|
|
|
|
2023-01-10 00:47:03 +03:00
|
|
|
// new_ip6 creates a new Addr from the IP6 address family, based on the given port and addr
|
|
|
|
pub fn new_ip6(port u16, addr [16]u8) Addr {
|
2021-06-13 23:53:38 +03:00
|
|
|
a := Addr{
|
2022-04-15 15:55:39 +03:00
|
|
|
f: u8(AddrFamily.ip6)
|
2021-07-20 11:17:08 +03:00
|
|
|
addr: AddrData{
|
|
|
|
Ip6: Ip6{
|
2021-06-13 23:53:38 +03:00
|
|
|
port: u16(C.htons(port))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 16:10:15 +03:00
|
|
|
unsafe { vmemcpy(&a.addr.Ip6.addr[0], &addr[0], 16) }
|
2021-06-13 23:53:38 +03:00
|
|
|
|
|
|
|
return a
|
2021-01-08 19:41:52 +03:00
|
|
|
}
|
|
|
|
|
2023-01-10 00:47:03 +03:00
|
|
|
// new_ip creates a new Addr from the IPv4 address family, based on the given port and addr
|
|
|
|
pub fn new_ip(port u16, addr [4]u8) Addr {
|
2021-06-13 23:53:38 +03:00
|
|
|
a := Addr{
|
2022-04-15 15:55:39 +03:00
|
|
|
f: u8(AddrFamily.ip)
|
2021-07-20 11:17:08 +03:00
|
|
|
addr: AddrData{
|
|
|
|
Ip: Ip{
|
2021-06-13 23:53:38 +03:00
|
|
|
port: u16(C.htons(port))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 16:10:15 +03:00
|
|
|
unsafe { vmemcpy(&a.addr.Ip.addr[0], &addr[0], 4) }
|
2021-06-13 23:53:38 +03:00
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2022-10-16 09:28:57 +03:00
|
|
|
fn temp_unix() !Addr {
|
2021-06-13 23:53:38 +03:00
|
|
|
// create a temp file to get a filename
|
|
|
|
// close it
|
|
|
|
// remove it
|
|
|
|
// then reuse the filename
|
2022-10-16 09:28:57 +03:00
|
|
|
mut file, filename := util.temp_file()!
|
2021-06-13 23:53:38 +03:00
|
|
|
file.close()
|
2022-10-16 09:28:57 +03:00
|
|
|
os.rm(filename)!
|
|
|
|
addrs := resolve_addrs(filename, .unix, .udp)!
|
2021-06-13 23:53:38 +03:00
|
|
|
return addrs[0]
|
|
|
|
}
|
|
|
|
|
2023-01-10 00:47:03 +03:00
|
|
|
// family returns the family/kind of the given address `a`
|
2021-06-13 23:53:38 +03:00
|
|
|
pub fn (a Addr) family() AddrFamily {
|
2022-10-02 22:39:11 +03:00
|
|
|
return unsafe { AddrFamily(a.f) }
|
2020-11-15 23:54:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2021-06-13 23:53:38 +03:00
|
|
|
max_ip_len = 24
|
|
|
|
max_ip6_len = 46
|
2020-11-15 23:54:47 +03:00
|
|
|
)
|
|
|
|
|
2023-01-10 00:47:03 +03:00
|
|
|
// str returns a string representation of `a`
|
|
|
|
pub fn (a Ip) str() string {
|
2021-12-02 12:18:14 +03:00
|
|
|
buf := [net.max_ip_len]char{}
|
2021-06-13 23:53:38 +03:00
|
|
|
|
2021-12-02 12:18:14 +03:00
|
|
|
res := &char(C.inet_ntop(.ip, &a.addr, &buf[0], buf.len))
|
2021-06-13 23:53:38 +03:00
|
|
|
|
|
|
|
if res == 0 {
|
|
|
|
return '<Unknown>'
|
2020-11-15 23:54:47 +03:00
|
|
|
}
|
2021-06-13 23:53:38 +03:00
|
|
|
|
2021-12-02 12:18:14 +03:00
|
|
|
saddr := unsafe { cstring_to_vstring(&buf[0]) }
|
2021-06-13 23:53:38 +03:00
|
|
|
port := C.ntohs(a.port)
|
|
|
|
|
2022-11-15 16:53:13 +03:00
|
|
|
return '${saddr}:${port}'
|
2021-06-13 23:53:38 +03:00
|
|
|
}
|
|
|
|
|
2023-01-10 00:47:03 +03:00
|
|
|
// str returns a string representation of `a`
|
|
|
|
pub fn (a Ip6) str() string {
|
2022-01-03 16:10:15 +03:00
|
|
|
buf := [net.max_ip6_len]char{}
|
2021-06-13 23:53:38 +03:00
|
|
|
|
2021-12-02 12:18:14 +03:00
|
|
|
res := &char(C.inet_ntop(.ip6, &a.addr, &buf[0], buf.len))
|
2021-06-13 23:53:38 +03:00
|
|
|
|
|
|
|
if res == 0 {
|
|
|
|
return '<Unknown>'
|
|
|
|
}
|
|
|
|
|
2021-12-02 12:18:14 +03:00
|
|
|
saddr := unsafe { cstring_to_vstring(&buf[0]) }
|
2021-06-13 23:53:38 +03:00
|
|
|
port := C.ntohs(a.port)
|
|
|
|
|
2022-11-15 16:53:13 +03:00
|
|
|
return '[${saddr}]:${port}'
|
2021-06-13 23:53:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const aoffset = __offsetof(Addr, addr)
|
|
|
|
|
2023-01-10 00:47:03 +03:00
|
|
|
// len returns the length in bytes of the address `a`, depending on its family
|
|
|
|
pub fn (a Addr) len() u32 {
|
2021-06-13 23:53:38 +03:00
|
|
|
match a.family() {
|
|
|
|
.ip {
|
2021-06-14 10:08:41 +03:00
|
|
|
return sizeof(Ip) + net.aoffset
|
2020-11-15 23:54:47 +03:00
|
|
|
}
|
2021-06-13 23:53:38 +03:00
|
|
|
.ip6 {
|
2021-06-14 10:08:41 +03:00
|
|
|
return sizeof(Ip6) + net.aoffset
|
2021-06-13 23:53:38 +03:00
|
|
|
}
|
|
|
|
.unix {
|
2021-06-14 10:08:41 +03:00
|
|
|
return sizeof(Unix) + net.aoffset
|
2021-06-13 23:53:38 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
panic('Unknown address family')
|
2020-11-15 23:54:47 +03:00
|
|
|
}
|
|
|
|
}
|
2021-06-13 23:53:38 +03:00
|
|
|
}
|
2020-11-15 23:54:47 +03:00
|
|
|
|
2023-01-10 00:47:03 +03:00
|
|
|
// resolve_addrs converts the given `addr`, `family` and `@type` to a list of addresses
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn resolve_addrs(addr string, family AddrFamily, @type SocketType) ![]Addr {
|
2021-06-13 23:53:38 +03:00
|
|
|
match family {
|
|
|
|
.ip, .ip6, .unspec {
|
|
|
|
return resolve_ipaddrs(addr, family, @type)
|
|
|
|
}
|
|
|
|
.unix {
|
|
|
|
resolved := Unix{}
|
2020-11-15 23:54:47 +03:00
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
if addr.len > max_unix_path {
|
|
|
|
return error('net: resolve_addrs Unix socket address is too long')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the unix path into the address struct
|
|
|
|
unsafe {
|
|
|
|
C.memcpy(&resolved.path, addr.str, addr.len)
|
|
|
|
}
|
|
|
|
|
2021-09-21 16:20:09 +03:00
|
|
|
return [
|
|
|
|
Addr{
|
2022-04-15 15:55:39 +03:00
|
|
|
f: u8(AddrFamily.unix)
|
2021-09-21 16:20:09 +03:00
|
|
|
addr: AddrData{
|
|
|
|
Unix: resolved
|
|
|
|
}
|
|
|
|
},
|
|
|
|
]
|
2021-06-13 23:53:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 00:47:03 +03:00
|
|
|
// resolve_addrs converts the given `addr` and `@type` to a list of addresses
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn resolve_addrs_fuzzy(addr string, @type SocketType) ![]Addr {
|
2021-06-13 23:53:38 +03:00
|
|
|
if addr.len == 0 {
|
2022-10-16 09:28:57 +03:00
|
|
|
return error('none')
|
2020-11-15 23:54:47 +03:00
|
|
|
}
|
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
// Use a small heuristic to figure out what address family this is
|
|
|
|
// (out of the ones that we support)
|
|
|
|
|
|
|
|
if addr.contains(':') {
|
|
|
|
// Colon is a reserved character in unix paths
|
|
|
|
// so this must be an ip address
|
|
|
|
return resolve_addrs(addr, .unspec, @type)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolve_addrs(addr, .unix, @type)
|
2020-11-15 23:54:47 +03:00
|
|
|
}
|
|
|
|
|
2023-01-10 00:47:03 +03:00
|
|
|
// resolve_ipaddrs converts the given `addr`, `family` and `typ` to a list of addresses
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn resolve_ipaddrs(addr string, family AddrFamily, typ SocketType) ![]Addr {
|
|
|
|
address, port := split_address(addr)!
|
2020-11-15 23:54:47 +03:00
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
if addr[0] == `:` {
|
2021-12-06 12:10:25 +03:00
|
|
|
match family {
|
|
|
|
.ip6 {
|
|
|
|
return [new_ip6(port, net.addr_ip6_any)]
|
|
|
|
}
|
|
|
|
.ip, .unspec {
|
|
|
|
return [new_ip(port, net.addr_ip_any)]
|
|
|
|
}
|
|
|
|
else {}
|
|
|
|
}
|
2021-06-13 23:53:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mut hints := C.addrinfo{
|
|
|
|
// ai_family: int(family)
|
|
|
|
// ai_socktype: int(typ)
|
|
|
|
// ai_flags: C.AI_PASSIVE
|
|
|
|
}
|
2021-11-24 10:34:48 +03:00
|
|
|
unsafe { vmemset(&hints, 0, int(sizeof(hints))) }
|
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
|
2021-11-24 10:34:48 +03:00
|
|
|
|
2023-04-13 08:38:21 +03:00
|
|
|
results := &C.addrinfo(unsafe { nil })
|
2020-11-15 23:54:47 +03:00
|
|
|
|
2022-11-15 16:53:13 +03:00
|
|
|
sport := '${port}'
|
2020-11-15 23:54:47 +03:00
|
|
|
|
|
|
|
// This might look silly but is recommended by MSDN
|
|
|
|
$if windows {
|
2022-10-16 09:28:57 +03:00
|
|
|
socket_error(0 - C.getaddrinfo(&char(address.str), &char(sport.str), &hints, &results))!
|
2020-11-15 23:54:47 +03:00
|
|
|
} $else {
|
2021-06-13 23:53:38 +03:00
|
|
|
x := C.getaddrinfo(&char(address.str), &char(sport.str), &hints, &results)
|
2022-10-16 09:28:57 +03:00
|
|
|
wrap_error(x)!
|
2020-11-15 23:54:47 +03:00
|
|
|
}
|
|
|
|
|
2021-06-13 23:53:38 +03:00
|
|
|
defer {
|
|
|
|
C.freeaddrinfo(results)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have our linked list of addresses
|
|
|
|
// convert them into an array
|
|
|
|
mut addresses := []Addr{}
|
|
|
|
|
2022-06-21 13:23:21 +03:00
|
|
|
for result := unsafe { results }; !isnil(result); result = result.ai_next {
|
2022-10-02 22:39:11 +03:00
|
|
|
match unsafe { AddrFamily(result.ai_family) } {
|
2021-12-06 12:10:25 +03:00
|
|
|
.ip {
|
|
|
|
new_addr := Addr{
|
|
|
|
addr: AddrData{
|
|
|
|
Ip: Ip{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
C.memcpy(&new_addr, result.ai_addr, result.ai_addrlen)
|
|
|
|
}
|
|
|
|
addresses << new_addr
|
|
|
|
}
|
|
|
|
.ip6 {
|
2021-06-13 23:53:38 +03:00
|
|
|
new_addr := Addr{
|
2021-07-20 11:17:08 +03:00
|
|
|
addr: AddrData{
|
|
|
|
Ip6: Ip6{}
|
2021-06-13 23:53:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
C.memcpy(&new_addr, result.ai_addr, result.ai_addrlen)
|
|
|
|
}
|
|
|
|
addresses << new_addr
|
|
|
|
}
|
|
|
|
else {
|
2022-11-15 16:53:13 +03:00
|
|
|
panic('Unexpected address family ${result.ai_family}')
|
2021-06-13 23:53:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return addresses
|
|
|
|
}
|
|
|
|
|
2023-01-10 00:47:03 +03:00
|
|
|
// str returns a string representation of the address `a`
|
|
|
|
pub fn (a Addr) str() string {
|
2022-10-02 22:39:11 +03:00
|
|
|
match unsafe { AddrFamily(a.f) } {
|
2021-06-13 23:53:38 +03:00
|
|
|
.ip {
|
|
|
|
unsafe {
|
|
|
|
return a.addr.Ip.str()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.ip6 {
|
|
|
|
unsafe {
|
|
|
|
return a.addr.Ip6.str()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.unix {
|
|
|
|
unsafe {
|
|
|
|
return tos_clone(a.addr.Unix.path[0..max_unix_path].data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.unspec {
|
|
|
|
return '<.unspec>'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 00:47:03 +03:00
|
|
|
// addr_from_socket_handle returns an address, based on the given integer socket `handle`
|
2021-06-13 23:53:38 +03:00
|
|
|
pub fn addr_from_socket_handle(handle int) Addr {
|
|
|
|
addr := Addr{
|
2021-07-20 11:17:08 +03:00
|
|
|
addr: AddrData{
|
|
|
|
Ip6: Ip6{}
|
2021-06-13 23:53:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
size := sizeof(addr)
|
|
|
|
|
|
|
|
C.getsockname(handle, voidptr(&addr), &size)
|
|
|
|
|
|
|
|
return addr
|
2020-11-15 23:54:47 +03:00
|
|
|
}
|