2
1
Identify whether an IP address is internal, global, link-local, or reserved.
Input should be an IPv4 address in decimal octets separated by periods.
Output should be 0
for link-local (RFC 3927), 1
for internal (RFC 1918), 2
for global IPs (Internet), and 3
for otherwise reserved addresses.
All these address types are described in RFC 5735:
Global and Other Specialized Address Blocks
0.0.0.0/8 - Addresses in this block refer to source hosts on "this" network. Address 0.0.0.0/32 may be used as a source address for this host on this network; other addresses within 0.0.0.0/8 may be used to refer to specified hosts on this network ([RFC1122], Section 3.2.1.3).
10.0.0.0/8 - This block is set aside for use in private networks. Its intended use is documented in [RFC1918]. As described in that
RFC, addresses within this block do not legitimately appear on the
public Internet. These addresses can be used without any
coordination with IANA or an Internet registry.127.0.0.0/8 - This block is assigned for use as the Internet host loopback address. A datagram sent by a higher-level protocol to an
address anywhere within this block loops back inside the host. This
is ordinarily implemented using only 127.0.0.1/32 for loopback. As
described in [RFC1122], Section 3.2.1.3, addresses within the entire 127.0.0.0/8 block do not legitimately appear on any network anywhere.169.254.0.0/16 - This is the "link local" block. As described in [RFC3927], it is allocated for communication between hosts on a
single link. Hosts obtain these addresses by auto-configuration,
such as when a DHCP server cannot be found.172.16.0.0/12 - This block is set aside for use in private networks. Its intended use is documented in [RFC1918]. As described in that RFC, addresses within this block do not legitimately appear on the public Internet. These addresses can be used without any coordination with IANA or an Internet registry.
192.0.0.0/24 - This block is reserved for IETF protocol assignments. At the time of writing this document, there are no current assignments. Allocation policy for future assignments is given in [RFC5736].
192.0.2.0/24 - This block is assigned as "TEST-NET-1" for use in documentation and example code. It is often used in conjunction with domain names example.com or example.net in vendor and protocol
documentation. As described in [RFC5737], addresses within this
block do not legitimately appear on the public Internet and can be
used without any coordination with IANA or an Internet registry. See [RFC1166].192.88.99.0/24 - This block is allocated for use as 6to4 relay anycast addresses, in [RFC3068]. In contrast with previously
described blocks, packets destined to addresses from this block do
appear in the public Internet. [RFC3068], Section 7, describes
operational practices to prevent the malicious use of this block in
routing protocols.192.168.0.0/16 - This block is set aside for use in private networks. Its intended use is documented in [RFC1918]. As described in that RFC, addresses within this block do not legitimately appear on the public Internet. These addresses can be used without any coordination with IANA or an Internet registry.
198.18.0.0/15 - This block has been allocated for use in benchmark tests of network interconnect devices. [RFC2544] explains that this
range was assigned to minimize the chance of conflict in case a
testing device were to be accidentally connected to part of the
Internet. Packets with source addresses from this range are not
meant to be forwarded across the Internet.198.51.100.0/24 - This block is assigned as "TEST-NET-2" for use in documentation and example code. It is often used in conjunction with domain names example.com or example.net in vendor and protocol
documentation. As described in [RFC5737], addresses within this
block do not legitimately appear on the public Internet and can be
used without any coordination with IANA or an Internet registry.203.0.113.0/24 - This block is assigned as "TEST-NET-3" for use in documentation and example code. It is often used in conjunction with domain names example.com or example.net in vendor and protocol
documentation. As described in [RFC5737], addresses within this
block do not legitimately appear on the public Internet and can be
used without any coordination with IANA or an Internet registry.224.0.0.0/4 - This block, formerly known as the Class D address space, is allocated for use in IPv4 multicast address assignments.
The IANA guidelines for assignments from this space are described in
[RFC3171].240.0.0.0/4 - This block, formerly known as the Class E address space, is reserved for future use; see [RFC1112], Section 4.
The one exception to this is the "limited broadcast" destination
address 255.255.255.255. As described in [RFC0919] and [RFC0922],
packets with this destination address are not forwarded at the IP
layer.4. Summary Table
Address Block Present Use Reference 0.0.0.0/8 "This" Network RFC 1122, Section 3.2.1.3 10.0.0.0/8 Private-Use Networks RFC 1918 127.0.0.0/8 Loopback RFC 1122, Section 3.2.1.3 169.254.0.0/16 Link Local RFC 3927 172.16.0.0/12 Private-Use Networks RFC 1918 192.0.0.0/24 IETF Protocol Assignments RFC 5736 192.0.2.0/24 TEST-NET-1 RFC 5737 192.88.99.0/24 6to4 Relay Anycast RFC 3068 192.168.0.0/16 Private-Use Networks RFC 1918 198.18.0.0/15 Network Interconnect Device Benchmark Testing RFC 2544 198.51.100.0/24 TEST-NET-2 RFC 5737 203.0.113.0/24 TEST-NET-3 RFC 5737 224.0.0.0/4 Multicast RFC 3171 240.0.0.0/4 Reserved for Future Use RFC 1112, Section 4 255.255.255.255/32 Limited Broadcast RFC 919, Section 7 RFC 922, Section 7
Rules
- Standard loopholes apply.
- This is code-golf, shortest answer wins.
1For those of us who do not know this stuff, how do the single values here identify "blocks" and what values are "in" said blocks? Also which blocks are (2) and which are (3) (or are all non RFC3927/1918 (3) and everything else (2))? – Jonathan Allan – 2016-11-12T02:38:56.537
@JonathanAllan The number after the slash in the IP address is the bitmask. http://networkengineering.stackexchange.com/questions/3697/the-slash-after-an-ip-address-cidr-notation
– dkudriavtsev – 2016-11-12T02:41:20.6801That does not help me - I did not even think we would have that part in the input - "decimal octets separated by periods". – Jonathan Allan – 2016-11-12T02:44:32.257
@JonathanAllan That's not for the input - it is the explanation of the blocks. – dkudriavtsev – 2016-11-12T02:45:14.490
So the effective test for each block is to check if the bitwise-and of the slash-identified number of leading 1s matches the dot-delimited-identified value? – Jonathan Allan – 2016-11-12T02:53:57.797
@JonathanAllan I guess... – dkudriavtsev – 2016-11-12T02:54:32.753
Let us continue this discussion in chat.
– Jonathan Allan – 2016-11-12T02:54:39.9602Could you please reformat the summary table so that it exactly shows what should return 0, what should return 1, etc. – orlp – 2016-11-12T06:52:12.873
@orlp The summary table is actually directly taken from the linked document. – dkudriavtsev – 2016-11-12T18:55:12.080
2This question should have some example inputs and outputs. – Paŭlo Ebermann – 2017-05-12T17:07:18.423