networking · intermediate · ~15 min

SIP request method

Extract the SIP method token.

Challenge

SIP borrows HTTP's request-line shape: METHOD SP URI SP VERSION, e.g. INVITE sip:bob@host SIP/2.0. Extract the method (first token).

Task

Implement int sip_method(const char *line, char *out, int outsz) that copies the first token of line (everything up to the first space) into out.

Input

  • line: the SIP request line.
  • out, outsz: destination buffer and its size.

Output

Returns the method length on success, or -1 if line has no space.

Example

sip_method("INVITE sip:bob@host SIP/2.0", out, 16)   ->   6, out="INVITE"
sip_method("REGISTER", out, 16)                      ->   -1

Edge cases

  • No space: return -1.
  • Copy bounded by outsz and NUL-terminate.

Input format

The SIP request line (line) and an output buffer (out, outsz).

Output format

The method length on success, or -1 if there is no space.

Constraints

Method is the first token up to the first space; bound the copy and NUL-terminate.

Starter code

#include <string.h>

int sip_method(const char *line, char *out, int outsz) {
    /* TODO */
    return -1;
}

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