net: use conv.hton* consistently, instead of $if tinyc { conv.hton16(port) } $else { u16(C.htons(port)) }

This commit is contained in:
Delyan Angelov 2023-10-20 06:52:03 +03:00
parent b3d1b041f8
commit 9cb8eac48a
No known key found for this signature in database
GPG key ID: 66886C0F12D595ED
3 changed files with 9 additions and 56 deletions

View file

@ -17,12 +17,7 @@ const (
// 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 {
n_port := $if tinyc {
conv.hton16(port)
} $else {
u16(C.htons(port))
}
n_port := conv.hton16(port)
a := Addr{
f: u8(AddrFamily.ip6)
addr: AddrData{
@ -31,20 +26,13 @@ pub fn new_ip6(port u16, addr [16]u8) Addr {
}
}
}
unsafe { vmemcpy(&a.addr.Ip6.addr[0], &addr[0], 16) }
return a
}
// 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 {
n_port := $if tinyc {
conv.hton16(port)
} $else {
u16(C.htons(port))
}
n_port := conv.hton16(port)
a := Addr{
f: u8(AddrFamily.ip)
addr: AddrData{
@ -53,9 +41,7 @@ pub fn new_ip(port u16, addr [4]u8) Addr {
}
}
}
unsafe { vmemcpy(&a.addr.Ip.addr[0], &addr[0], 4) }
return a
}
@ -92,12 +78,7 @@ pub fn (a Ip) str() string {
}
saddr := unsafe { cstring_to_vstring(&buf[0]) }
port := $if tinyc {
conv.hton16(a.port)
} $else {
C.ntohs(a.port)
}
port := conv.ntoh16(a.port)
return '${saddr}:${port}'
}
@ -112,12 +93,7 @@ pub fn (a Ip6) str() string {
}
saddr := unsafe { cstring_to_vstring(&buf[0]) }
port := $if tinyc {
conv.hton16(a.port)
} $else {
C.ntohs(a.port)
}
port := conv.ntoh16(a.port)
return '[${saddr}]:${port}'
}

View file

@ -262,12 +262,7 @@ pub fn (mut ws Client) write_ptr(bytes &u8, payload_len int, code OPCode) !int {
if payload_len <= 125 {
header[1] = u8(payload_len)
} else if payload_len > 125 && payload_len <= 0xffff {
len16 := $if tinyc {
conv.hton16(u16(payload_len))
} $else {
C.htons(payload_len)
}
len16 := conv.hton16(u16(payload_len))
header[1] = 126
unsafe { C.memcpy(&header[2], &len16, 2) }
} else if payload_len > 0xffff && payload_len <= 0x7fffffff {
@ -283,11 +278,7 @@ pub fn (mut ws Client) write_ptr(bytes &u8, payload_len int, code OPCode) !int {
header[4] = masking_key[2]
header[5] = masking_key[3]
} else if payload_len > 125 && payload_len <= 0xffff {
len16 := $if tinyc {
conv.hton16(u16(payload_len))
} $else {
C.htons(payload_len)
}
len16 := conv.hton16(u16(payload_len))
header[1] = (126 | 0x80)
unsafe { C.memcpy(&header[2], &len16, 2) }
header[4] = masking_key[0]
@ -356,11 +347,7 @@ pub fn (mut ws Client) close(code int, message string) ! {
ws.set_state(.closing)
// mut code32 := 0
if code > 0 {
code_ := $if tinyc {
conv.hton16(u16(code))
} $else {
C.htons(code)
}
code_ := conv.hton16(u16(code))
message_len := message.len + 2
mut close_frame := []u8{len: message_len}
close_frame[0] = u8(code_ & 0xFF)

View file

@ -125,18 +125,8 @@ fn listen(config Config) int {
// addr settings
sin_port := $if tinyc {
conv.hton16(u16(config.port))
} $else {
C.htons(config.port)
}
sin_addr := $if tinyc {
conv.hton32(u32(C.INADDR_ANY))
} $else {
C.htonl(C.INADDR_ANY)
}
sin_port := conv.hton16(u16(config.port))
sin_addr := conv.hton32(u32(C.INADDR_ANY))
mut addr := C.sockaddr_in{
sin_family: u8(C.AF_INET)
sin_port: sin_port