-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[VRL] wrong values returned from ip_aton()
and ip_ntoa()
functions
#9182
Comments
Thanks for this @hhromic ! I think I might be misunderstanding the desired behavior here though. What we currently have seems to match #include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
int main(void) {
struct in_addr ia;
inet_aton("1.2.3.4", &ia);
printf("ia=0x%x\n", ia.s_addr);
printf("ia=%d\n", ia.s_addr);
return 0;
}
Which should be in network byte order. I'm, admittedly, getting very turned around by byte ordering 😄 |
Hi @jszwedko, I'm very sorry for all the confusion caused around this topic :( The C functions The correct way to print an printf("ia=0x%08x\n", ntohl(ia.s_addr));
printf("ia=%u\n", ntohl(ia.s_addr));
The Python equivalents for import socket
import struct
socket.inet_aton("1.2.3.4")
# b'\x01\x02\x03\x04'
# (0x01 << 24) + (0x02 << 16) + (0x03 << 8) + 0x04 = 16909060
socket.inet_ntoa(b'\x01\x02\x03\x04')
# '1.2.3.4' And the higher-level import ipaddress
ipaddress.IPv4Address(16909060)
# IPv4Address('1.2.3.4')
int(ipaddress.IPv4Address("1.2.3.4"))
# 16909060 |
@hhromic thanks for the thorough explanation! The annotated Python example helps illuminate. I can see I misunderstood the Rust stdlib. It is converting the host-endian bytes to big-endian before doing the conversion in both cases so us doing that beforehand was causing it to reverse it again. https://doc.rust-lang.org/stable/src/std/net/ip.rs.html#1006-1022 |
Yes I misunderstood the documentation as well, therefore giving bad feedback during the PR. |
Vector Version
Description
The
ip_aton()
andip_ntoa()
functions are wrongly byte-swapping integers for their conversions, thus giving wrong results. This is because I wrongly advised to usefrom_be()
andto_be()
to account for the host endianness in the PR:However, it turns out that the functions used for the conversions:
u32::from(Ipv4Addr)
Ipv4Addr::from(u32)
work with abstract
u32
integers which move away endianness from the programmer. This means that these conversions are actually portable and do not need any endianness conversion.There was a long discussion about the documentation being unclear in the
rust-lang
repository, however I now found this comment that clarifies pretty well that the above functions are portable indeed:The big question remains: how these functions in Vector are passing their tests currently?
I just realised that the test cases (and example values in the documentation) are using wrong numbers!
The numeric representation of
1.2.3.4
is not67305985
but is16909060
.Many apologies for the bad suggestions in the original PR! :(
The fix should be simple, eliminate
to_be()
andfrom_be()
conversions and update the tests/docs with the real numeric value for the example IP1.2.3.4
. Let me know if you don't have much time and I can take a stab on fixing this.The text was updated successfully, but these errors were encountered: