cybersecurity · intermediate · ~15 min · safe pentest lab

Count open ports in nmap XML

Tally a marker in tool output.

Challenge

Tally the open ports in a saved nmap XML report by counting a marker substring.

Task

Implement int count_open_ports(const char *xml) that returns the number of occurrences of the substring state="open".

Input

  • xml: a NUL-terminated string holding saved nmap XML, baked into the harness.

Output

Returns int: how many times state="open" appears in xml.

Example

xml contains two open and one closed port
count_open_ports(xml)   ->   2

Edge cases

  • No open ports returns 0.
  • Overlapping is not possible; advance past each match.

Rules

  • Pure string scan on a static buffer — never a live scan.

Input format

A NUL-terminated string xml of saved nmap output.

Output format

An int: the number of occurrences of state="open".

Constraints

Count the exact substring state="open". Static buffer only.

Starter code

#include <string.h>

int count_open_ports(const char *xml) {
    /* TODO */
    return 0;
}

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