Skip to content

Commit 3b93fa3

Browse files
committed
Fix byte ordering in sockets API for systems where host byte order == network byte order
- Documented data structures used for storing IP addresses. - Fixed bug in bind and connect methods by updating a code in wasi_socket_ext.
1 parent 32c9416 commit 3b93fa3

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

core/iwasm/libraries/lib-socket/inc/wasi_socket_ext.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ typedef uint16_t __wasi_ip_port_t;
2020

2121
typedef enum { IPv4 = 0, IPv6 } __wasi_addr_type_t;
2222

23-
/* n0.n1.n2.n3 */
23+
/*
24+
n0.n1.n2.n3
25+
Example:
26+
IP Address: 127.0.0.1
27+
Structure: {n0: 127, n1: 0, n2: 0, n3: 1}
28+
*/
2429
typedef struct __wasi_addr_ip4_t {
2530
uint8_t n0;
2631
uint8_t n1;
@@ -30,9 +35,18 @@ typedef struct __wasi_addr_ip4_t {
3035

3136
typedef struct __wasi_addr_ip4_port_t {
3237
__wasi_addr_ip4_t addr;
33-
__wasi_ip_port_t port;
38+
__wasi_ip_port_t port; /* host byte order */
3439
} __wasi_addr_ip4_port_t;
3540

41+
/*
42+
n0:n1:n2:n3:h0:h1:h2:h3, each 16bit value uses host byte order
43+
Example (little-endian system)
44+
IP Address fe80::3ba2:893b:4be0:e3dd
45+
Structure: {
46+
n0: 0xfe80, n1:0x0, n2: 0x0, n3: 0x0,
47+
h0: 0x3ba2, h1: 0x893b, h2: 0x4be0, h3: 0xe3dd
48+
}
49+
*/
3650
typedef struct __wasi_addr_ip6_t {
3751
uint16_t n0;
3852
uint16_t n1;
@@ -46,7 +60,7 @@ typedef struct __wasi_addr_ip6_t {
4660

4761
typedef struct __wasi_addr_ip6_port_t {
4862
__wasi_addr_ip6_t addr;
49-
__wasi_ip_port_t port;
63+
__wasi_ip_port_t port; /* host byte order */
5064
} __wasi_addr_ip6_port_t;
5165

5266
typedef struct __wasi_addr_t {

core/iwasm/libraries/lib-socket/src/wasi/wasi_socket_ext.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
static void
2323
ipv4_addr_to_wasi_addr(uint32_t addr_num, uint16_t port, __wasi_addr_t *out)
2424
{
25+
addr_num = ntohl(addr_num);
2526
out->kind = IPv4;
2627
out->addr.ip4.port = ntohs(port);
27-
out->addr.ip4.addr.n3 = (addr_num & 0xFF000000) >> 24;
28-
out->addr.ip4.addr.n2 = (addr_num & 0x00FF0000) >> 16;
29-
out->addr.ip4.addr.n1 = (addr_num & 0x0000FF00) >> 8;
30-
out->addr.ip4.addr.n0 = (addr_num & 0x000000FF);
28+
out->addr.ip4.addr.n0 = (addr_num & 0xFF000000) >> 24;
29+
out->addr.ip4.addr.n1 = (addr_num & 0x00FF0000) >> 16;
30+
out->addr.ip4.addr.n2 = (addr_num & 0x0000FF00) >> 8;
31+
out->addr.ip4.addr.n3 = (addr_num & 0x000000FF);
3132
}
3233

3334
static __wasi_errno_t
@@ -57,6 +58,7 @@ static __wasi_errno_t
5758
sock_addr_remote(__wasi_fd_t fd, struct sockaddr *sock_addr, socklen_t *addrlen)
5859
{
5960
__wasi_addr_t wasi_addr = { 0 };
61+
uint32_t s_addr;
6062
__wasi_errno_t error;
6163

6264
error =
@@ -68,11 +70,13 @@ sock_addr_remote(__wasi_fd_t fd, struct sockaddr *sock_addr, socklen_t *addrlen)
6870
if (IPv4 == wasi_addr.kind) {
6971
struct sockaddr_in sock_addr_in = { 0 };
7072

73+
s_addr = (wasi_addr.addr.ip4.addr.n0 << 24)
74+
| (wasi_addr.addr.ip4.addr.n1 << 16)
75+
| (wasi_addr.addr.ip4.addr.n2 << 8)
76+
| wasi_addr.addr.ip4.addr.n3;
77+
7178
sock_addr_in.sin_family = AF_INET;
72-
sock_addr_in.sin_addr.s_addr = (wasi_addr.addr.ip4.addr.n3 << 24)
73-
| (wasi_addr.addr.ip4.addr.n2 << 16)
74-
| (wasi_addr.addr.ip4.addr.n1 << 8)
75-
| wasi_addr.addr.ip4.addr.n0;
79+
sock_addr_in.sin_addr.s_addr = htonl(s_addr);
7680
sock_addr_in.sin_port = htons(wasi_addr.addr.ip4.port);
7781
memcpy(sock_addr, &sock_addr_in, sizeof(sock_addr_in));
7882

core/shared/platform/common/posix/posix_socket.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ os_socket_bind(bh_socket_t socket, const char *host, int *port)
5959
goto fail;
6060
}
6161

62-
addr.sin_addr.s_addr = inet_addr(host);
63-
addr.sin_port = htons(*port);
64-
addr.sin_family = AF_INET;
62+
textual_addr_to_sockaddr(host, *port, &addr);
6563

6664
ret = bind(socket, (struct sockaddr *)&addr, sizeof(addr));
6765
if (ret < 0) {

0 commit comments

Comments
 (0)