networking · beginner · ~15 min

Does binding need privilege?

Know which ports require privilege to bind.

Challenge

Tell whether binding a given port requires elevated privilege.

Task

Implement int needs_root_to_bind(int port).

Input

  • port: the port number to bind.

Output

Return 1 if port is a privileged port (1..1023), else 0.

Example

needs_root_to_bind(80)    ->  1   (privileged)
needs_root_to_bind(8080)  ->  0

Edge cases

  • Ports 1 through 1023 are privileged; 1024 and above are not.

Input format

port: the port number to bind.

Output format

1 if port is in 1..1023 (privileged), else 0.

Constraints

Binding ports below 1024 needs root or CAP_NET_BIND_SERVICE.

Starter code

int needs_root_to_bind(int port) {
    /* TODO */
    return 0;
}

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