2020-11-15 21:54:47 +01:00
|
|
|
module net
|
|
|
|
|
2021-06-13 22:53:38 +02:00
|
|
|
import io.util
|
|
|
|
import os
|
|
|
|
|
|
|
|
union AddrData {
|
|
|
|
Unix
|
|
|
|
Ip
|
|
|
|
Ip6
|
2020-11-15 21:54:47 +01:00
|
|
|
}
|
|
|
|
|
2021-06-13 22:53:38 +02:00
|
|
|
const (
|
|
|
|
addr_ip6_any = [16]byte{init: byte(0)}
|
|
|
|
addr_ip_any = [4]byte{init: byte(0)}
|
|
|
|
)
|
|
|
|
|
|
|
|
fn new_ip6(port u16, addr [16]byte) Addr {
|
|
|
|
a := Addr{
|
|
|
|
f: u16(AddrFamily.ip6)
|
2021-07-20 10:17:08 +02:00
|
|
|
addr: AddrData{
|
|
|
|
Ip6: Ip6{
|
2021-06-13 22:53:38 +02:00
|
|
|
port: u16(C.htons(port))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(a.addr.Ip6.addr[0..], addr[0..])
|
|
|
|
|
|
|
|
return a
|
2021-01-08 17:41:52 +01:00
|
|
|
}
|
|
|
|
|
2021-06-13 22:53:38 +02:00
|
|
|
fn new_ip(port u16, addr [4]byte) Addr {
|
|
|
|
a := Addr{
|
|
|
|
f: u16(AddrFamily.ip)
|
2021-07-20 10:17:08 +02:00
|
|
|
addr: AddrData{
|
|
|
|
Ip: Ip{
|
2021-06-13 22:53:38 +02:00
|
|
|
port: u16(C.htons(port))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(a.addr.Ip6.addr[0..], addr[0..])
|
|
|
|
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
fn temp_unix() ?Addr {
|
|
|
|
// create a temp file to get a filename
|
|
|
|
// close it
|
|
|
|
// remove it
|
|
|
|
// then reuse the filename
|
2021-07-20 10:17:08 +02:00
|
|
|
mut file, filename := util.temp_file() ?
|
2021-06-13 22:53:38 +02:00
|
|
|
file.close()
|
|
|
|
os.rm(filename) ?
|
|
|
|
addrs := resolve_addrs(filename, .unix, .udp) ?
|
|
|
|
return addrs[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (a Addr) family() AddrFamily {
|
|
|
|
return AddrFamily(a.f)
|
2020-11-15 21:54:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2021-06-13 22:53:38 +02:00
|
|
|
max_ip_len = 24
|
|
|
|
max_ip6_len = 46
|
2020-11-15 21:54:47 +01:00
|
|
|
)
|
|
|
|
|
2021-06-13 22:53:38 +02:00
|
|
|
fn (a Ip) str() string {
|
|
|
|
buf := []byte{len: net.max_ip_len, init: 0}
|
|
|
|
|
|
|
|
res := &char(C.inet_ntop(.ip, &a.addr, buf.data, buf.len))
|
|
|
|
|
|
|
|
if res == 0 {
|
|
|
|
return '<Unknown>'
|
2020-11-15 21:54:47 +01:00
|
|
|
}
|
2021-06-13 22:53:38 +02:00
|
|
|
|
|
|
|
saddr := buf.bytestr()
|
|
|
|
port := C.ntohs(a.port)
|
|
|
|
|
|
|
|
return '$saddr:$port'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (a Ip6) str() string {
|
|
|
|
buf := []byte{len: net.max_ip6_len, init: 0}
|
|
|
|
|
|
|
|
res := &char(C.inet_ntop(.ip6, &a.addr, buf.data, buf.len))
|
|
|
|
|
|
|
|
if res == 0 {
|
|
|
|
return '<Unknown>'
|
|
|
|
}
|
|
|
|
|
|
|
|
saddr := buf.bytestr()
|
|
|
|
port := C.ntohs(a.port)
|
|
|
|
|
|
|
|
return '[$saddr]:$port'
|
|
|
|
}
|
|
|
|
|
|
|
|
const aoffset = __offsetof(Addr, addr)
|
|
|
|
|
|
|
|
fn (a Addr) len() u32 {
|
|
|
|
match a.family() {
|
|
|
|
.ip {
|
2021-06-14 09:08:41 +02:00
|
|
|
return sizeof(Ip) + net.aoffset
|
2020-11-15 21:54:47 +01:00
|
|
|
}
|
2021-06-13 22:53:38 +02:00
|
|
|
.ip6 {
|
2021-06-14 09:08:41 +02:00
|
|
|
return sizeof(Ip6) + net.aoffset
|
2021-06-13 22:53:38 +02:00
|
|
|
}
|
|
|
|
.unix {
|
2021-06-14 09:08:41 +02:00
|
|
|
return sizeof(Unix) + net.aoffset
|
2021-06-13 22:53:38 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
panic('Unknown address family')
|
2020-11-15 21:54:47 +01:00
|
|
|
}
|
|
|
|
}
|
2021-06-13 22:53:38 +02:00
|
|
|
}
|
2020-11-15 21:54:47 +01:00
|
|
|
|
2021-06-13 22:53:38 +02:00
|
|
|
pub fn resolve_addrs(addr string, family AddrFamily, @type SocketType) ?[]Addr {
|
|
|
|
match family {
|
|
|
|
.ip, .ip6, .unspec {
|
|
|
|
return resolve_ipaddrs(addr, family, @type)
|
|
|
|
}
|
|
|
|
.unix {
|
|
|
|
resolved := Unix{}
|
2020-11-15 21:54:47 +01:00
|
|
|
|
2021-06-13 22:53:38 +02: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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return [Addr{
|
|
|
|
f: u16(AddrFamily.unix)
|
|
|
|
addr: AddrData{
|
|
|
|
Unix: resolved
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resolve_addrs_fuzzy(addr string, @type SocketType) ?[]Addr {
|
|
|
|
if addr.len == 0 {
|
|
|
|
return none
|
2020-11-15 21:54:47 +01:00
|
|
|
}
|
|
|
|
|
2021-06-13 22:53:38 +02: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 21:54:47 +01:00
|
|
|
}
|
|
|
|
|
2021-06-13 22:53:38 +02:00
|
|
|
pub fn resolve_ipaddrs(addr string, family AddrFamily, typ SocketType) ?[]Addr {
|
2021-02-08 23:48:23 +01:00
|
|
|
address, port := split_address(addr) ?
|
2020-11-15 21:54:47 +01:00
|
|
|
|
2021-06-13 22:53:38 +02:00
|
|
|
if addr[0] == `:` {
|
|
|
|
// Use in6addr_any
|
|
|
|
return [new_ip6(port, net.addr_ip6_any)]
|
|
|
|
}
|
|
|
|
|
|
|
|
mut hints := C.addrinfo{
|
|
|
|
// ai_family: int(family)
|
|
|
|
// ai_socktype: int(typ)
|
|
|
|
// ai_flags: C.AI_PASSIVE
|
|
|
|
}
|
2021-01-08 17:41:52 +01:00
|
|
|
hints.ai_family = int(family)
|
|
|
|
hints.ai_socktype = int(typ)
|
2020-11-15 21:54:47 +01:00
|
|
|
hints.ai_flags = C.AI_PASSIVE
|
|
|
|
hints.ai_protocol = 0
|
|
|
|
hints.ai_addrlen = 0
|
2021-06-13 22:53:38 +02:00
|
|
|
hints.ai_addr = voidptr(0)
|
|
|
|
hints.ai_canonname = voidptr(0)
|
|
|
|
hints.ai_next = voidptr(0)
|
|
|
|
results := &C.addrinfo(0)
|
2020-11-15 21:54:47 +01:00
|
|
|
|
|
|
|
sport := '$port'
|
|
|
|
|
|
|
|
// This might look silly but is recommended by MSDN
|
|
|
|
$if windows {
|
2021-07-13 11:01:24 +02:00
|
|
|
socket_error(0 - C.getaddrinfo(&char(address.str), &char(sport.str), &hints, &results)) ?
|
2020-11-15 21:54:47 +01:00
|
|
|
} $else {
|
2021-06-13 22:53:38 +02:00
|
|
|
x := C.getaddrinfo(&char(address.str), &char(sport.str), &hints, &results)
|
2021-02-08 23:48:23 +01:00
|
|
|
wrap_error(x) ?
|
2020-11-15 21:54:47 +01:00
|
|
|
}
|
|
|
|
|
2021-06-13 22:53:38 +02:00
|
|
|
defer {
|
|
|
|
C.freeaddrinfo(results)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have our linked list of addresses
|
|
|
|
// convert them into an array
|
|
|
|
mut addresses := []Addr{}
|
|
|
|
|
|
|
|
for result := results; !isnil(result); result = result.ai_next {
|
|
|
|
match AddrFamily(result.ai_family) {
|
|
|
|
.ip, .ip6 {
|
|
|
|
new_addr := Addr{
|
2021-07-20 10:17:08 +02:00
|
|
|
addr: AddrData{
|
|
|
|
Ip6: Ip6{}
|
2021-06-13 22:53:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
C.memcpy(&new_addr, result.ai_addr, result.ai_addrlen)
|
|
|
|
}
|
|
|
|
addresses << new_addr
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
panic('Unexpected address family $result.ai_family')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return addresses
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (a Addr) str() string {
|
|
|
|
match AddrFamily(a.f) {
|
|
|
|
.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>'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn addr_from_socket_handle(handle int) Addr {
|
|
|
|
addr := Addr{
|
2021-07-20 10:17:08 +02:00
|
|
|
addr: AddrData{
|
|
|
|
Ip6: Ip6{}
|
2021-06-13 22:53:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
size := sizeof(addr)
|
|
|
|
|
|
|
|
C.getsockname(handle, voidptr(&addr), &size)
|
|
|
|
|
|
|
|
return addr
|
2020-11-15 21:54:47 +01:00
|
|
|
}
|