upd5
This commit is contained in:
+27
-21
@@ -4,8 +4,7 @@ import encoding.binary
|
||||
|
||||
struct C.sockaddr_storage {}
|
||||
|
||||
// max_unix_path_size value is used to pad the sockaddr_un struct.
|
||||
const max_unix_path_size = $if linux {
|
||||
pub const max_unix_path_len = $if linux {
|
||||
108
|
||||
} $else $if windows {
|
||||
108
|
||||
@@ -20,9 +19,9 @@ mut:
|
||||
pos int
|
||||
}
|
||||
|
||||
// SocketAddr.ipv4 creates new AF_INET socket address.
|
||||
// SocketAddr.new_ipv4 creates new AF_INET socket address.
|
||||
// addr must be set in network (big-endian) byte order.
|
||||
pub fn SocketAddr.ipv4(addr [4]u8, port u16) SocketAddr {
|
||||
pub fn SocketAddr.new_ipv4(addr [4]u8, port u16) SocketAddr {
|
||||
mut sock_addr := unsafe { SocketAddr.new(af_inet, 16) }
|
||||
unsafe {
|
||||
sock_addr.push(binary.big_endian_get_u16(port)) or {}
|
||||
@@ -31,10 +30,10 @@ pub fn SocketAddr.ipv4(addr [4]u8, port u16) SocketAddr {
|
||||
return sock_addr
|
||||
}
|
||||
|
||||
// SocketAddr.ipv6 creates new AF_INET6 socket address.
|
||||
// SocketAddr.new_ipv6 creates new AF_INET6 socket address.
|
||||
// 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 {
|
||||
pub fn SocketAddr.new_ipv6(addr [16]u8, port u16, flow_info u32, scope_id u32) SocketAddr {
|
||||
mut sock_addr := unsafe { SocketAddr.new(af_inet6, 28) }
|
||||
unsafe {
|
||||
sock_addr.push(binary.big_endian_get_u16(port)) or {}
|
||||
@@ -45,12 +44,13 @@ pub fn SocketAddr.ipv6(addr [16]u8, port u16, flow_info u32, scope_id u32) Socke
|
||||
return sock_addr
|
||||
}
|
||||
|
||||
// SocketAddr.unix creates new AF_UNIX socket address.
|
||||
pub fn SocketAddr.unix(path string) !SocketAddr {
|
||||
if path.len > max_unix_path_size {
|
||||
// SocketAddr.new_unix creates new AF_UNIX socket address with given path. The path must
|
||||
// fit in platform dependent const `max_unix_path_len` value.
|
||||
pub fn SocketAddr.new_unix(path string) !SocketAddr {
|
||||
if path.len > max_unix_path_len {
|
||||
return error('Too long path to socket')
|
||||
}
|
||||
mut sock_addr := unsafe { SocketAddr.new(af_unix, usize(max_unix_path_size) + 2) }
|
||||
mut sock_addr := unsafe { SocketAddr.new(af_unix, usize(max_unix_path_len) + 2) }
|
||||
unsafe {
|
||||
sock_addr.push(path.bytes()) or {}
|
||||
}
|
||||
@@ -124,9 +124,9 @@ pub fn SocketAddr.new(af AddrFamily, size isize) SocketAddr {
|
||||
return sock_addr
|
||||
}
|
||||
|
||||
// SocketAddr.from_ptr creates new socket address by copying data from specified pointer.
|
||||
// SocketAddr.from_ptr_copy creates new socket address by copying data from specified pointer.
|
||||
@[unsafe]
|
||||
pub fn SocketAddr.from_ptr(ptr voidptr, size isize) !SocketAddr {
|
||||
pub fn SocketAddr.from_ptr_copy(ptr voidptr, size isize) !SocketAddr {
|
||||
if isnil(ptr) {
|
||||
return error('${@METHOD}: cannot accept nil ptr')
|
||||
}
|
||||
@@ -140,6 +140,19 @@ pub fn SocketAddr.from_ptr(ptr voidptr, size isize) !SocketAddr {
|
||||
}
|
||||
}
|
||||
|
||||
// SocketAddr.from_ptr creates new socket address from specified pointer.
|
||||
// Note: The data is reused, not copied. See also SocketAddr.from_ptr_copy().
|
||||
@[unsafe]
|
||||
pub fn SocketAddr.from_ptr(ptr voidptr, size isize) !SocketAddr {
|
||||
if isnil(ptr) {
|
||||
return error('${@METHOD}: cannot accept nil ptr')
|
||||
}
|
||||
return SocketAddr{
|
||||
data: ptr
|
||||
len: int(size)
|
||||
}
|
||||
}
|
||||
|
||||
// family returns the socket address family.
|
||||
// Note: It returns 0 if socket address is nil, see also `is_empty()`.
|
||||
pub fn (a SocketAddr) family() AddrFamily {
|
||||
@@ -195,13 +208,6 @@ 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.
|
||||
// Supported address families are AF_INET, AF_INET6, and AF_UNIX.
|
||||
// Examples: '172.16.16.132:1080', '[fdf1:72d1:0033:0000:0000:0000:0000:0247]:25535',
|
||||
@@ -242,10 +248,10 @@ pub fn (a SocketAddr) str() string {
|
||||
return '[' + res + ']:' + port_int.str()
|
||||
}
|
||||
af_unix {
|
||||
mut path := [max_unix_path_size]u8{}
|
||||
mut path := [max_unix_path_len]u8{}
|
||||
mut res := ''
|
||||
unsafe {
|
||||
vmemcpy(path, a.ptr() + 2, max_unix_path_size)
|
||||
vmemcpy(path, a.ptr() + 2, max_unix_path_len)
|
||||
res = tos_clone(&u8(path[..].data))
|
||||
}
|
||||
return res
|
||||
|
||||
Reference in New Issue
Block a user