diff --git a/vlib/net/address.v b/vlib/net/address.v index f5b34f3035498a..39d8d6e5bffa0b 100644 --- a/vlib/net/address.v +++ b/vlib/net/address.v @@ -1,6 +1,7 @@ module net import io.util +import net.conv import os union AddrData { @@ -16,11 +17,17 @@ 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)) + } + a := Addr{ f: u8(AddrFamily.ip6) addr: AddrData{ Ip6: Ip6{ - port: u16(C.htons(port)) + port: n_port } } } @@ -32,11 +39,17 @@ pub fn new_ip6(port u16, addr [16]u8) Addr { // 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)) + } + a := Addr{ f: u8(AddrFamily.ip) addr: AddrData{ Ip: Ip{ - port: u16(C.htons(port)) + port: n_port } } } @@ -79,7 +92,11 @@ pub fn (a Ip) str() string { } saddr := unsafe { cstring_to_vstring(&buf[0]) } - port := C.ntohs(a.port) + port := $if tinyc { + conv.hton16(a.port) + } $else { + C.ntohs(a.port) + } return '${saddr}:${port}' } @@ -95,7 +112,11 @@ pub fn (a Ip6) str() string { } saddr := unsafe { cstring_to_vstring(&buf[0]) } - port := C.ntohs(a.port) + port := $if tinyc { + conv.hton16(a.port) + } $else { + C.ntohs(a.port) + } return '[${saddr}]:${port}' } diff --git a/vlib/net/websocket/websocket_client.v b/vlib/net/websocket/websocket_client.v index 7a69f6f078c5b9..ed2e1d9fefa564 100644 --- a/vlib/net/websocket/websocket_client.v +++ b/vlib/net/websocket/websocket_client.v @@ -4,6 +4,7 @@ module websocket import net +import net.conv import net.http import net.ssl import net.urllib @@ -261,7 +262,12 @@ 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 := C.htons(payload_len) + len16 := $if tinyc { + conv.hton16(u16(payload_len)) + } $else { + C.htons(payload_len) + } + header[1] = 126 unsafe { C.memcpy(&header[2], &len16, 2) } } else if payload_len > 0xffff && payload_len <= 0x7fffffff { @@ -277,7 +283,11 @@ 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 := C.htons(payload_len) + len16 := $if tinyc { + conv.hton16(u16(payload_len)) + } $else { + C.htons(payload_len) + } header[1] = (126 | 0x80) unsafe { C.memcpy(&header[2], &len16, 2) } header[4] = masking_key[0] @@ -346,7 +356,11 @@ pub fn (mut ws Client) close(code int, message string) ! { ws.set_state(.closing) // mut code32 := 0 if code > 0 { - code_ := C.htons(code) + code_ := $if tinyc { + conv.hton16(u16(code)) + } $else { + C.htons(code) + } message_len := message.len + 2 mut close_frame := []u8{len: message_len} close_frame[0] = u8(code_ & 0xFF) diff --git a/vlib/picoev/socket_util.c.v b/vlib/picoev/socket_util.c.v index 63149a894286c1..09b2c4f6df7635 100644 --- a/vlib/picoev/socket_util.c.v +++ b/vlib/picoev/socket_util.c.v @@ -1,6 +1,7 @@ module picoev import net +import net.conv import picohttpparser #include @@ -123,10 +124,23 @@ 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) + } + mut addr := C.sockaddr_in{ sin_family: u8(C.AF_INET) - sin_port: C.htons(config.port) - sin_addr: C.htonl(C.INADDR_ANY) + sin_port: sin_port + sin_addr: sin_addr } size := sizeof(C.sockaddr_in) bind_res := C.bind(fd, voidptr(unsafe { &net.Addr(&addr) }), size) diff --git a/vlib/v/gen/c/cheaders.v b/vlib/v/gen/c/cheaders.v index 38e94118acf28f..edb049068b8c7a 100644 --- a/vlib/v/gen/c/cheaders.v +++ b/vlib/v/gen/c/cheaders.v @@ -803,7 +803,7 @@ static inline uint64_t _wymix(uint64_t A, uint64_t B){ _wymum(&A,&B); return A^B #if (WYHASH_LITTLE_ENDIAN) static inline uint64_t _wyr8(const uint8_t *p) { uint64_t v; memcpy(&v, p, 8); return v;} static inline uint64_t _wyr4(const uint8_t *p) { uint32_t v; memcpy(&v, p, 4); return v;} -#elif defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__) +#elif !defined(__TINYC__) && (defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)) static inline uint64_t _wyr8(const uint8_t *p) { uint64_t v; memcpy(&v, p, 8); return __builtin_bswap64(v);} static inline uint64_t _wyr4(const uint8_t *p) { uint32_t v; memcpy(&v, p, 4); return __builtin_bswap32(v);} #elif defined(_MSC_VER)