I am having problem with inet_aton
to convert a network address. The code below works fine to convert the address 10.0.0.1
char *x1;
struct sockaddr_in si_other;
inet_aton("10.0.0.1", &si_other.sin_addr);
printf("si_other.sin_addr =%lu\n",si_other.sin_addr);
x1 = inet_ntoa(si_other.sin_addr);
printf("x1=%s\n",x1);
It outputs:
si_other.sin_addr =16777226
x1=10.0.0.01
No problem so far. However, the function works weird when 010.000.000.001
is passed
char *x2;
struct sockaddr_in si_other2;
inet_aton("010.000.000.001", &si_other2.sin_addr);
printf("si_other2.sin_addr =%lu\n",si_other2.sin_addr);
x2 = inet_ntoa(si_other2.sin_addr);
printf("x2=%s\n",x2);
outputs:
si_other.sin_addr2 =16777224
x2=8.0.0.01
The function works fine when 192.168.0.1
and 192.168.000.001
are passed.
Can anyone explain me what is the problem and how I can fix the problem? (note: I need to pass the IP address as 010.000.000.001
in my code)
The leading 0 is being interpreted as indicating the number is octal. 010 (oct) == 8 (dec). You need to modify the input to inet_aton
to avoid this, or convert it yourself in a different way.
const char *str = "010.000.000.001";
inet_aton(str[0] == '0' ? str+1:str, &si_other.sin_addr);
Is the simplest solution, but it would be better to fix whatever (snprintf
?) produces the string in the first place to avoid the confusion.
(As it stands that solution won't work with a number of edge cases still, including "001.0.0.1", "0xF.0.0.1", "1" and many more valid IPv4 addressees).
You can trivially "normalise" your input using sscanf
, even if you can't control how it gets generated in the first place (although that really ought to be a bug whever it came from in my view):
#include <stdio.h>
#include <stdlib.h>
int main() {
const char *str="010.020.030.040";
int parts[4];
sscanf(str, "%d.%d.%d.%d", parts+0, parts+1, parts+2, parts+3);
char *out = NULL;
asprintf(&out, "%d.%d.%d.%d", parts[0], parts[1], parts[2], parts[3]);
printf("%s\n", out);
free(out);
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With