cybersecurity · intermediate · ~15 min · safe pentest lab

Is it a private (RFC1918) address?

Recognise the RFC1918 private ranges.

Challenge

Recognise the RFC 1918 private ranges — the only addresses a home lab should ever target.

Task

Implement int is_private_ip(int a, int b) that returns 1 if an address whose first two octets are a.b is in a private range, else 0.

Private ranges (based on the first two octets):

  • 10.x (first octet 10)
  • 192.168.x (192 then 168)
  • 172.16.x .. 172.31.x (172 then 16..31)

Input

  • a, b: the first and second octets of the address. The grader passes fixed values.

Output

Returns int: 1 if a.b falls in a private range, else 0.

Example

is_private_ip(10, 0)     ->   1
is_private_ip(192, 168)  ->   1
is_private_ip(172, 20)   ->   1
is_private_ip(8, 8)      ->   0   (public)
is_private_ip(172, 15)   ->   0   (just outside 172.16-31)

Edge cases

  • 172 is private only when the second octet is 16..31.

Rules

  • Pure arithmetic on the two octets.

Input format

The first two octets a, b of an address.

Output format

An int: 1 if a.b is in a private (RFC 1918) range, else 0.

Constraints

10.x; 192.168.x; 172.16-31.x; second octet matters for 172.

Starter code

int is_private_ip(int a, int b) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.