networking · intermediate · ~15 min
Extract the SIP method token.
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).
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.
line: the SIP request line.out, outsz: destination buffer and its size.Returns the method length on success, or -1 if line has no space.
sip_method("INVITE sip:bob@host SIP/2.0", out, 16) -> 6, out="INVITE"
sip_method("REGISTER", out, 16) -> -1
-1.outsz and NUL-terminate.The SIP request line (line) and an output buffer (out, outsz).
The method length on success, or -1 if there is no space.
Method is the first token up to the first space; bound the copy and NUL-terminate.
#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.