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;
}