This commit is contained in:
ge
2026-04-04 00:47:35 +03:00
parent 4e43912583
commit 1cf6d878b5
19 changed files with 438 additions and 302 deletions
+49 -34
View File
@@ -1,6 +1,5 @@
module netio
import dlmalloc
import encoding.binary
import os
@@ -18,9 +17,10 @@ mut:
ai_next voidptr
}
fn C.getaddrinfo(node &char, srvc &char, hints &C.addrinfo, res &&C.addrinfo) int
fn C.freeaddrinfo(info &C.addrinfo)
fn C.getaddrinfo(&char, &char, &C.addrinfo, &&C.addrinfo) i32
fn C.freeaddrinfo(&C.addrinfo)
// max_unix_path_size value is used to pad the sockaddr_un struct.
const max_unix_path_size = $if linux {
108
} $else $if windows {
@@ -51,16 +51,16 @@ pub:
flags int
}
// translate_addr translates the network address.
// See also [getaddrinfo(3)](https://man7.org/linux/man-pages/man3/getaddrinfo.3.html).
// translate_addr translates the network address. This is a low-level wrapper around
// the [getaddrinfo(3)](https://man7.org/linux/man-pages/man3/getaddrinfo.3.html) C API.
// Example:
// ```v
// import os
// import netio
//
// // Resolve the host FQND
// addrs := netio.translate_addr(node: os.hostname()!, flags: C.AI_CANONNAME)!
// for addr in addrs {
// addr_info := netio.translate_addr(node: os.hostname()!, flags: ai_canonname)!
// for addr in addr_info {
// println(addr.canonical)
// }
// ```
@@ -107,11 +107,10 @@ pub fn translate_addr(hints TranslateAddrHints) ![]AddrInfo {
}
// SocketAddr.ipv4 creates new AF_INET socket address.
// addr must be in big-endian byte order.
// addr must be set in network (big-endian) byte order.
pub fn SocketAddr.ipv4(addr [4]u8, port u16) SocketAddr {
mut sock_addr := unsafe { SocketAddr.empty(af_inet, 16) }
mut sock_addr := unsafe { SocketAddr.new(af_inet, 16) }
unsafe {
sock_addr.push(binary.little_endian_get_u16(u16(af_inet))) or {}
sock_addr.push(binary.big_endian_get_u16(port)) or {}
sock_addr.push(addr[..]) or {}
}
@@ -119,9 +118,10 @@ pub fn SocketAddr.ipv4(addr [4]u8, port u16) SocketAddr {
}
// SocketAddr.ipv6 creates new AF_INET6 socket address.
// addr must be in big-endian byte order.
// addr must be set in network (big-endian) byte order.
// Use if_nametoindex(3) to get an integer scope_id from its string representation.
pub fn SocketAddr.ipv6(addr [16]u8, port u16, flow_info u32, scope_id u32) SocketAddr {
mut sock_addr := unsafe { SocketAddr.empty(af_inet6, 28) }
mut sock_addr := unsafe { SocketAddr.new(af_inet6, 28) }
unsafe {
sock_addr.push(binary.big_endian_get_u16(port)) or {}
sock_addr.push(binary.big_endian_get_u32(flow_info)) or {}
@@ -132,22 +132,25 @@ pub fn SocketAddr.ipv6(addr [16]u8, port u16, flow_info u32, scope_id u32) Socke
}
// SocketAddr.unix creates new AF_UNIX socket address.
pub fn SocketAddr.unix(path string) SocketAddr {
mut sock_addr := unsafe { SocketAddr.empty(af_unix, usize(max_unix_path_size) + 2) }
pub fn SocketAddr.unix(path string) !SocketAddr {
if path.len > max_unix_path_size {
return error('Too long path to socket')
}
mut sock_addr := unsafe { SocketAddr.new(af_unix, usize(max_unix_path_size) + 2) }
unsafe {
sock_addr.push(path.bytes()) or {}
}
return sock_addr
}
// SocketAddr.empty creates new instance of SocketAddr with specified address family and size.
// SocketAddr.new creates new instance of SocketAddr with specified address family and size.
//
// Note: This function only allocates memory (zero filled) for the address, but **does not**
// initialize the address itself, you need to do that manually. The benefit is that you can
// create the any type of address.
// This function allocates memory (zero filled) for the address, but does not initialize
// the address itself, you need to do that manually. The benefit is that you can create the
// any kind of socket address.
//
// Note: This function sets the address family for you.
// This field is always first and have 2-byte size (`u16` type).
// Note: This function sets the address family struct field for you, it is always first
// and 2-byte size (`u16` type).
//
// SocketAddr is a "constructor" for
// [sockaddr(3type)](https://www.man7.org/linux/man-pages/man3/sockaddr.3type.html) objects.
@@ -158,15 +161,15 @@ pub fn SocketAddr.unix(path string) SocketAddr {
// A mistake while creating an address will crash your application. So this function is
// marked as `unsafe`.
//
// The example below creates a `sockaddr_in` struct describing the loopback IPv4-address
// The example below creates a sockaddr_in struct describing the loopback IPv4-address
// 127.0.0.1 with port number 1080. Note the comment in the example. This is a fragment
// of [sockaddr_in(3type)](https://www.man7.org/linux/man-pages/man3/sockaddr.3type.html)
// manual page, which shows the target C struct. Summing the field sizes yields 8
// bytes, but we need to allocate 16 bytes according to the <netinet/in.h>.
// Data must be padded to sockaddr struct size which is 16 bytes. Each field is then
// written in turn, from top to bottom. Keep in mind that address family field (sin_family
// in this case) are already written. According to the manual page, the address and port
// are written using the network (big endian) byte order.
// written in turn, from top to bottom. Keep in mind that two-byte address family field
// (sin_family in this case) is already written. According to the manual page, the
// address and port are written using the network (big endian) byte order.
//
// Example:
// ```v
@@ -186,15 +189,15 @@ pub fn SocketAddr.unix(path string) SocketAddr {
// // typedef uint32_t in_addr_t;
// // typedef uint16_t in_port_t;
//
// mut sa := unsafe { netio.SocketAddr.empty(u16(C.AF_INET), 16) }
// mut sa := unsafe { netio.SocketAddr.new(netio.af_inet, 16) }
// unsafe {
// sa.push(binary.big_endian_get_u16(u16(1080)))!
// sa.push([u8(127), 0, 0, 1])!
// }
// ```
@[unsafe]
pub fn SocketAddr.empty(af AddrFamily, size isize) SocketAddr {
ptr := unsafe { dlmalloc.calloc(usize(size)) }
pub fn SocketAddr.new(af AddrFamily, size isize) SocketAddr {
ptr := unsafe { vcalloc(usize(size)) }
mut sock_addr := SocketAddr{
data: ptr
len: int(size)
@@ -212,7 +215,7 @@ pub fn SocketAddr.empty(af AddrFamily, size isize) SocketAddr {
// SocketAddr.from_ptr creates new socket address by copying data from specified pointer.
@[unsafe]
pub fn SocketAddr.from_ptr(ptr voidptr, size isize) !SocketAddr {
data := unsafe { dlmalloc.calloc(usize(size)) }
data := unsafe { vcalloc(usize(size)) }
unsafe {
vmemcpy(data, ptr, size)
}
@@ -222,7 +225,7 @@ pub fn SocketAddr.from_ptr(ptr voidptr, size isize) !SocketAddr {
}
}
struct SocketAddr {
pub struct SocketAddr {
mut:
data &u8 = unsafe { nil }
len int
@@ -231,7 +234,7 @@ mut:
// family returns the socket address family.
pub fn (a SocketAddr) family() AddrFamily {
mut f := u16(-1)
mut f := AddrFamily(0)
unsafe { vmemcpy(&f, a.data, isize(2)) }
return f
}
@@ -265,9 +268,16 @@ pub fn (a SocketAddr) size() u32 {
return u32(a.len)
}
// u8_array returns the socket address data as is as bytes array.
pub fn (a SocketAddr) u8_array() []u8 {
mut addr := []u8{len: int(a.size()), init: 0}
unsafe { vmemcpy(addr.data, a.ptr(), a.size()) }
return addr
}
// str returns the string representation of socket address.
pub fn (a SocketAddr) str() string {
family := a.family()
match family {
match a.family() {
af_inet {
mut addr := [4]u8{}
mut port := [2]u8{}
@@ -276,7 +286,12 @@ pub fn (a SocketAddr) str() string {
vmemcpy(addr, a.ptr() + 4, 4)
}
port_int := binary.big_endian_u16_fixed(port)
return '${addr[0]}.${addr[1]}.${addr[2]}.${addr[3]}:${port_int}'
// vfmt off
return addr[0].str() + '.'
+ addr[1].str() + '.'
+ addr[2].str() + '.'
+ addr[3].str() + ':' + port_int.str()
// vfmt on
}
af_inet6 {
mut addr := [16]u8{}
@@ -293,7 +308,7 @@ pub fn (a SocketAddr) str() string {
}
}
port_int := binary.big_endian_u16_fixed(port)
return res + ':' + port_int.str()
return '[' + res + ']:' + port_int.str()
}
af_unix {
mut path := [max_unix_path_size]u8{}