7

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)

sven
  • 1,101
  • 7
  • 21
  • 44
  • 2
    If you can't normalize your dotted-decimal addresses to the standard variable-width notation, you'll have to write your own function approximately equivalent to `inet_aton` that handles non-standard fixed width, leading-zero padded IPv4 addresses. Have fun! – Jonathan Leffler Jan 06 '13 at 22:37

1 Answers1

12

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;
}
Flexo
  • 87,323
  • 22
  • 191
  • 272
  • 2
    What would that yield on `010.020.030.040`? Clearly, the first field would be `10` (decimal), but what about the others? – Jonathan Leffler Jan 06 '13 at 22:38
  • @JonathanLeffler still wouldn't fix that case. – Flexo Jan 06 '13 at 22:41
  • 4
    The correct solution would be to left-trim the 0's from all octets. +1 nevertheless. – jweyrich Jan 06 '13 at 22:41
  • Sven also said that `090` and `070` are also producing `8`, but `090` octal is not valid, and `070` octal is `56` decimal. – Remy Lebeau Jan 06 '13 at 22:42
  • @jweyrich: in this particular case, yes trim the leading zeros. But what if you actually want to convert a string with octals in it? The zeros are then needed. – Remy Lebeau Jan 06 '13 at 22:44
  • it was my fault '090' '070' outputs different values.I forgot to clean the project before run the code again. Thank you for the explanation and solution – sven Jan 06 '13 at 22:45
  • @sven - I added a possibly better solution – Flexo Jan 06 '13 at 22:53
  • I can't give you an extra +1, more's the pity. Short of packaging it as a function, there's not a lot more you can do. – Jonathan Leffler Jan 06 '13 at 22:55
  • @RemyLebeau you raised a very valid point. As the question stands, showing IPs that aren't base-8 but still and erroneously have leading zeroes, such as `192.168.000.001`, the only solution is to trim (or normalize) it. In the general scenario, one would not need to do any extra processing. The conversion done by `inet_aton` should work flawlessly for decimal, octal, and hexa. – jweyrich Jan 06 '13 at 23:04
  • Yet, I don't understand why **[this](http://ideone.com/vXJ703)** outputs `inet_aton: Success` to stderr. It should output something like `inet_aton: Undefined error: 0`. It seems that errno isn't being set properly. Bug? Sorry for the extended discussion here. I should be using chat I guess. – jweyrich Jan 06 '13 at 23:25