cybersecurity · advanced · ~15 min · safe pentest lab

Count distinct client IPs

Deduplicate the source IPs in a log.

Challenge

Count how many distinct client IPs appear in an access log — this sizes the scope of activity in an authorized review.

Task

Implement int distinct_ips(const char *log) that returns the number of distinct leading IP strings. Each line begins with the client IP: the token up to the first space.

Input

  • log: a NUL-terminated, newline-separated access-log string baked into the harness. Each line starts with an IP, a space, then the rest.

Output

Returns int: the count of distinct leading-IP tokens across all lines.

Example

log: "10.0.0.1 GET /\n10.0.0.2 GET /\n10.0.0.1 GET /x\n"
distinct_ips(log)   ->   2

Edge cases

  • The same IP on many lines counts once.

Rules

  • Static buffer only — no real log-file access. A small fixed-size seen-table is fine.

Input format

A NUL-terminated newline-separated access-log string log; each line starts with an IP token.

Output format

An int: the count of distinct leading-IP tokens.

Constraints

The IP is the token before the first space on each line. Static buffer only.

Starter code

#include <string.h>

int distinct_ips(const char *log) {
    /* TODO */
    return 0;
}

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