user1500970
user1500970

Reputation: 107

perl gethostbyname when given IP

What happens if a wrong format IP is given to gethostbyname function in perl? One of our scripts was behaving weird when given a wrong format IP (say 1.1.1). On debugging, found that gethostbyname was returning a value when given 1.1.1 for example..any thoughts on this?... In my opinion, gethostbyname should return undef, right?

Upvotes: 1

Views: 986

Answers (2)

LeoNerd
LeoNerd

Reputation: 8542

Numeric IPv4 addresses can be written as 1, 2, 3 or 4 numeric components. Each non-final component represents 8 bits (1 octet), and the final represents as many bits required to give the full 32 bit address. Thus, the following all represent the local loopback address:

2130706433
127.1
127.0.1
127.0.0.1

Each component itself may be written in decimal, hex or octal; thus the following all also encode the same address

0x7f000001
127.0x01
0177.0.1
0x7f.0.0.1

Upvotes: 2

Alan Curry
Alan Curry

Reputation: 14741

In the beginning of IPv4, before CIDR, addresses were considered to be composed of a network part and a host part. The parts could be written sort of independently in dotted decimal form, and didn't need to be fully decomposed into bytes. So 1.1 is host 1 on network 1, equivalent to 1.0.0.1 or you can also write it as one big 32-bit number: 16777217. There was a time when people used URLs like http://16777127/ to show how clever they were. That was ruined when spammers started doing it to fool filters.

Somehow, when I ping 1.1.1, it goes to 1.1.0.1. I would have guessed 1.0.1.1. I'm not sure what the rule is to decide how it's broken up exactly.

These old forms are not widely supported (or even understood) anymore, but they haven't been completely rooted out from all the tools and libraries.

P.S. on my first attempt to submit this answer, stackoverflow said:

Your post contains a link to the invalid domain '16777127'. Please correct it by specifying a full domain or wrapping it in a code block.

Which is sort of what I meant by "not widely supported".

Upvotes: 3

Related Questions