Networking in C · intermediate · ~12 min
Extract the request method (INVITE, REGISTER, OPTIONS, BYE) from a SIP message.
Walk up to the first space, validate that every character before it is in A-Z, bounded-copy into the output.
SIP gateways live on the public internet. Rejecting malformed methods at the door is cheap; missing the check is expensive.
SIP — the protocol behind every VoIP call and WebRTC signalling exchange — looks almost exactly like HTTP: a request line, headers, a blank line, an optional body. The bug surface is similar too: input validation on the request line.
We extract just the method here. The follow-up modules in later waves can pull out the URI and the Via chain.
INVITE sip:alice@example.com SIP/2.0\r\n
Via: SIP/2.0/UDP 10.0.0.1:5060;branch=z9hG4bK1234\r\n
...
Implement int parse_sip_method(const char *msg, char *out, size_t cap)
that copies the leading method (up to the first space) into out,
NUL-terminating, and returns the number of bytes written.
Return -1 on any failure:
msg or out or cap == 0capA-Z.First token, uppercase only, bounded copy, NUL-terminate, return length.