Networking in C · intermediate · ~12 min

Pull the method out of a SIP message

Extract the request method (INVITE, REGISTER, OPTIONS, BYE) from a SIP message.

Overview

Walk up to the first space, validate that every character before it is in A-Z, bounded-copy into the output.

Why it matters

SIP gateways live on the public internet. Rejecting malformed methods at the door is cheap; missing the check is expensive.

Lesson

Why this matters

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.

What the wire looks like

INVITE sip:alice@example.com SIP/2.0\r\n
Via: SIP/2.0/UDP 10.0.0.1:5060;branch=z9hG4bK1234\r\n
...

Your job

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:

  • NULL msg or out or cap == 0
  • No space found within the first 16 bytes
  • The method would overflow cap
  • Any non-letter character before the first space

Common mistakes

  • Allowing lowercase. Real SIP methods are uppercase ASCII.
  • Forgetting the NUL terminator on the bounded output.
  • Treating arbitrary bytes as a method — reject anything outside A-Z.

What this is NOT

  • A full SIP parser. Headers, SDP, dialog tracking — all out of scope.
  • A signalling stack. We're just classifying.

Summary

First token, uppercase only, bounded copy, NUL-terminate, return length.

Practice with these exercises