networking · beginner · ~15 min

Is it a safe test target?

Enforce the authorized-scope rule in code.

Challenge

Encode the authorized-scope rule in code: only loopback is a safe target without explicit authorization.

Task

Implement int safe_test_target(const char *host).

Input

  • host: a NUL-terminated host string.

Output

Return 1 only if host is a localhost address ("localhost", "127.0.0.1", or "::1"); any other host returns 0.

Example

safe_test_target("127.0.0.1")       ->  1
safe_test_target("8.8.8.8")         ->  0
safe_test_target("victim.example")  ->  0

Input format

host: a NUL-terminated host string.

Output format

1 only for a localhost address ("localhost", "127.0.0.1", "::1"); else 0.

Constraints

Only loopback is in scope; refuse every other host.

Starter code

#include <string.h>

int safe_test_target(const char *host) {
    /* TODO */
    return 0;
}

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