Skip to content

Conversation

@loganek
Copy link
Collaborator

@loganek loganek commented Jul 25, 2022

  • Documented data structures used for storing IP addresses.
  • Fixed bug in bind and connect methods by updating a code in wasi_socket_ext.

@loganek loganek force-pushed the loganek/fix-byte-order branch from a9907cb to fb0d9b7 Compare July 25, 2022 09:20
… 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.
@loganek loganek force-pushed the loganek/fix-byte-order branch from fb0d9b7 to 3b93fa3 Compare July 27, 2022 08:58
@wenyongh wenyongh changed the base branch from main to dev/socket July 28, 2022 04:25
out->addr.ip4.addr.n1 = (addr_num & 0x00FF0000) >> 16;
out->addr.ip4.addr.n2 = (addr_num & 0x0000FF00) >> 8;
out->addr.ip4.addr.n3 = (addr_num & 0x000000FF);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's take a walk:

  • addr_num as a parameter, is the big-endian
  • L25, addr_num is host-byte-order
  • L28 - L31, store the MSB to n0 and the LSB to n3. So back to big-endian?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, n0 in this case is the most significant byte (e.g. given 127.0.0.1, n0 is 127), which is the same behavior as we have before this PR. What this PR fixes though is the case when the host system is big endian. Even though big endian system is currently not that popular, I think we should write the code in a way it supports both architectures.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • If the host system is little-endian, what we are doing is big-endian -> little-endian -> big-endian ?
  • if the host system is big-endian, what we are doing is big-endian -> big-endian -> big-endian?

So, why not directly store the parameter addr_num into _wasi_addr_t in network-byte-order?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm no sure if we can serialise addrnum to wasi_addr_t directly - as this is violation of strict aliasing rules. So I think more accurate description of what we do here is:
Little-endian host: big endian -> little endian (through ntohl) -> wasi_addr_t
Big-endian host: big endian -> no-op (as ntohl in that case shoud do nothing) -> wasi_addr_t

So there's no redundant operation in any of the stages.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to stick to using & operator for calculating each byte, w have to make sure the value is in the host byte order. The alternative here would be to convert addr_num to array of uint8 so we can safely rely on network byte order, but I find this approach a bit more elegant (that's very subjective though)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@wenyongh wenyongh merged commit 08fd714 into bytecodealliance:dev/socket Jul 29, 2022
wenyongh added a commit that referenced this pull request Sep 22, 2022
Implement more socket APIs, refer to #1336 and below PRs:
- Implement wasi_addr_resolve function (#1319)
- Fix socket-api byte order issue when host/network order are the same (#1327)
- Enhance sock_addr_local syscall (#1320)
- Implement sock_addr_remote syscall (#1360)
- Add support for IPv6 in WAMR (#1411)
- Implement ns lookup allowlist (#1420)
- Implement sock_send_to and sock_recv_from system calls (#1457)
- Added http downloader and multicast socket options (#1467)
- Fix `bind()` calls to receive the correct size of `sockaddr` structure (#1490)
- Assert on correct parameters (#1505)
- Copy only received bytes from socket recv buffer into the app buffer (#1497)

Co-authored-by: Marcin Kolny <[email protected]>
Co-authored-by: Marcin Kolny <[email protected]>
Co-authored-by: Callum Macmillan <[email protected]>
vickiegpt pushed a commit to vickiegpt/wamr-aot-gc-checkpoint-restore that referenced this pull request May 27, 2024
…ytecodealliance#1327)

Fix socket-api byte order issue for systems where host byte order
and network byte order are the same:
- Document data structures used for storing IP addresses
- Fix bug in bind and connect methods by updating code in wasi_socket_ext
vickiegpt pushed a commit to vickiegpt/wamr-aot-gc-checkpoint-restore that referenced this pull request May 27, 2024
Implement more socket APIs, refer to bytecodealliance#1336 and below PRs:
- Implement wasi_addr_resolve function (bytecodealliance#1319)
- Fix socket-api byte order issue when host/network order are the same (bytecodealliance#1327)
- Enhance sock_addr_local syscall (bytecodealliance#1320)
- Implement sock_addr_remote syscall (bytecodealliance#1360)
- Add support for IPv6 in WAMR (bytecodealliance#1411)
- Implement ns lookup allowlist (bytecodealliance#1420)
- Implement sock_send_to and sock_recv_from system calls (bytecodealliance#1457)
- Added http downloader and multicast socket options (bytecodealliance#1467)
- Fix `bind()` calls to receive the correct size of `sockaddr` structure (bytecodealliance#1490)
- Assert on correct parameters (bytecodealliance#1505)
- Copy only received bytes from socket recv buffer into the app buffer (bytecodealliance#1497)

Co-authored-by: Marcin Kolny <[email protected]>
Co-authored-by: Marcin Kolny <[email protected]>
Co-authored-by: Callum Macmillan <[email protected]>
@loganek loganek deleted the loganek/fix-byte-order branch June 10, 2024 12:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants