`> inet:getaddr( "50.1.1.1", inet ).`
`{ok,{50,1,1,1}}`

`> inet:getaddr( "50.001.001.001", inet ).`
`{ok,{50,1,1,1}}`

`> inet:getaddr( "2.001.001.001", inet ).`
`{ok,{2,1,1,1}}`

`(> inet:getaddr( "002.001.001.001", inet ).`
`{ok,{2,1,1,1}}`

HOWEVER:

`> inet:getaddr( "050.001.001.001", inet ).`
`{ok,{`40`,1,1,1}}`

WHY?!

I guess, as `050` starts with a `0`, octal is being used. 50 octal is 40 decimal.

5 Likes

Following up, here is an easily readable explanation:

Thank you for a quick feedback.

Valid IP addresses necessarily need not look βvalidβ. IP addresses can be represented in idefinitely different ways, therefore it cannot be determined if a value is an IP address or not.

Not sure if the above statement has any bearing to this problem. It is clear that inet module can establish if something is valid IP address or not.

For example:

{error,nxdomain}

{ok,{142,251,47,174}}

{error,nxdomain}

If I were to speculate, Iβd say that whenever something does not look as βdot-IPv4" address, inet attempts to resolve it via DNS lookup.

The problem is that (under some circumstances) inet module βinventsβ values for a valid βdot-IP Addressβ.

For example:

{ok,{50,1,1,1}}

and

{ok,{40,1,1,1}}

Should be considered as equivalents, however, (A2) for some reason converts β050β to 40!

A similar behaviour is exhibited wherever the βmiddleβ digit is preceded by a leading zero, but itself is not a zero (as in β5β in β050β):

{ok,{1,1,1,40}}

{ok,{1,1,40, 1}}

etc.

Worse Yet

{ok,{40,1,1,1}}

{ok,{32,1,1,1}}

{ok,{24,1,1,1}}

{ok,{16,1,1,1}}

{ok,{8,1,1,1}}

{ok,{0,1,1,1}}

The above case (C1) reduces the original value by 10; (C2) by 8; (C3) by 6; (C4) by 4 and (C5) by 2, and (C6) by 0, thus returns a correct value.

But then, even that is not done consistently:

{ok,{57,1,1,1}}

{ok,{47,1,1,1}}

{ok,{58,1,1,1}}

{error,nxdomain}

Or:

{ok,{1,1,1,57}}

{ok,{1,1,1,47}}

{ok,{1,1,1,58}}

{error,nxdomain}

It seems to me like an optimisation that went wrong somewhere.

Go figure, but clearly a bug.

Kind regards

V/

Itβs perfectly consistent, anything prefixed by `0` is interpreted as octal. `057` is valid octal, `058` is not, hence `1.001.001.058` gets interpreted as a domain name while `050.001.001.001` gets interpreted as `40.1.1.1` (`50` in octal is `40` decimal).

1 Like

The error here is that β058β canβt ever be a valid octal number, because there is a number greater than 7 in the string. See also:

``````> 8#56.
46
> 8#57.
47
> 8#58.
* 1:4: syntax error before: 8
> 8#60.
48
``````

Hope this helps.

Good point β Iβve completely forgotten that β0β prefix is used to specify base 8.

Your explanation is consistent with behaviour Iβve observed.

BTW: Also, if I call `inet:getaddr( "0375.98.0xfe.3", inet ).`
I get back:
`{ok,{253,98,254,3}}`

Which is consistent but not terribly useful.

Thanks a lot.

Thanks.

Sorry β I did not see the linked article. Only when it was pointed out to me.
Thanks a lot.

1 Like