This commit is contained in:
ge
2026-04-29 21:45:37 +03:00
parent e54f013ef7
commit 4ff0e46719
14 changed files with 1183 additions and 880 deletions
+29 -22
View File
@@ -24,22 +24,22 @@ mut:
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 {}
sock_addr.push(addr[..]) or {}
sock_addr.write(binary.big_endian_get_u16(port)) or {}
sock_addr.write(addr[..]) or {}
}
return sock_addr
}
// 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.
// Use `find_network_interface()` to get an integer scope_id from its string representation.
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 {}
sock_addr.push(binary.big_endian_get_u32(flow_info)) or {}
sock_addr.push(addr[..]) or {}
sock_addr.push(binary.big_endian_get_u32(scope_id)) or {}
sock_addr.write(binary.big_endian_get_u16(port)) or {}
sock_addr.write(binary.big_endian_get_u32(flow_info)) or {}
sock_addr.write(addr[..]) or {}
sock_addr.write(binary.big_endian_get_u32(scope_id)) or {}
}
return sock_addr
}
@@ -52,7 +52,7 @@ pub fn SocketAddr.new_unix(path string) !SocketAddr {
}
mut sock_addr := unsafe { SocketAddr.new(af_unix, usize(max_unix_path_len) + 2) }
unsafe {
sock_addr.push(path.bytes()) or {}
sock_addr.write(path.bytes()) or {}
}
return sock_addr
}
@@ -65,7 +65,7 @@ pub fn SocketAddr.new_unix(path string) !SocketAddr {
//
// SocketAddr is a builder for
// [sockaddr(3type)](https://www.man7.org/linux/man-pages/man3/sockaddr.3type.html) objects.
// Use this function only if you understand what you do. Using the `push()` method you must
// Use this function only if you understand what you do. Using the `write()` method you must
// write the data for the desired socket address, ensuring the correct sizes of all types,
// the order of the fields in the struct, the byte order, and the total size of the struct.
// The sizes and byte order may vary by platform, so you'll need to keep an eye on that as
@@ -102,8 +102,8 @@ pub fn SocketAddr.new_unix(path string) !SocketAddr {
//
// 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])!
// sa.write(binary.big_endian_get_u16(u16(1080)))!
// sa.write([u8(127), 0, 0, 1])!
// }
// println(sa)
// ```
@@ -116,9 +116,9 @@ pub fn SocketAddr.new(af AddrFamily, size isize) SocketAddr {
}
unsafe {
$if little_endian {
sock_addr.push(binary.little_endian_get_u16(u16(af))) or {}
sock_addr.write(binary.little_endian_get_u16(u16(af))) or {}
} $else {
sock_addr.push(binary.big_endian_get_u16(u16(af))) or {}
sock_addr.write(binary.big_endian_get_u16(u16(af))) or {}
}
}
return sock_addr
@@ -170,32 +170,39 @@ pub fn (a SocketAddr) is_empty() bool {
if isnil(a.data) {
return true
}
if a.u8_array().all(|e| e == 0) {
return true
for i := 0; i < a.len; i++ {
if unsafe { a.data[i] } != 0 {
return false
}
}
return false
return true
}
// push appends the `inp` bytes into internal data buffer.
// write writes `buf.len` bytes of data to the socket address memory area
// and returns the number of bytes written.
// write will return an error if the socket address pointer is nil or
// the buffer length, including the length of the data already written,
// exceeds the size of the socket address.
@[unsafe]
pub fn (mut a SocketAddr) push(inp []u8) ! {
pub fn (mut a SocketAddr) write(buf []u8) !int {
if isnil(a.data) {
return error('${@METHOD}: SocketAddr is nil')
}
if a.pos + inp.len > a.len {
if a.pos + buf.len > a.len {
return error('${@METHOD}: data overflow')
}
mut i := 0
for a.pos + 1 < a.len {
unsafe {
a.data[a.pos + i] = inp[i]
a.data[a.pos + i] = buf[i]
}
i++
if i >= inp.len {
if i >= buf.len {
break
}
}
a.pos += inp.len
a.pos += i
return i
}
// ptr returns the pointer to sockaddr data.