/* Extract User-Agent */ static str getUserAgent(struct sip_msg* msg) { static char buf[16] = "unknown-agent"; // buf is here for a reason. don't static str notfound = {buf, 13}; // use the constant string directly! str block, server; char *ptr; if ((parse_headers(msg, HDR_USERAGENT_F, 0)!=-1) && msg->user_agent && msg->user_agent->body.len>0) { return msg->user_agent->body; } // If we can't find user-agent, look after the Server: field // This is a temporary hack. Normally it should be extracted by ser // (either as the Server field, or if User-Agent is missing in place // of the User-Agent field) block.s = msg->buf; block.len = msg->len; ptr = findLineStartingWith(&block, "Server:", True); if (!ptr) return notfound; server.s = ptr + 7; server.len = findendline(server.s, block.s+block.len-server.s) - server.s; trim(&server); if (server.len == 0) return notfound; return server; }
// will return the ip address present in a `c=' line in the given block // returns: -1 on error, 0 if not found, 1 if found static int getMediaIPFromBlock(str *block, str *mediaip) { str tokens[3], zone; char *ptr; int count; ptr = findLineStartingWith(block, "c=", False); if (!ptr) { mediaip->s = NULL; mediaip->len = 0; return 0; } zone.s = ptr + 2; zone.len = findendline(zone.s, block->s + block->len - zone.s) - zone.s; count = getStrTokens(&zone, tokens, 3); if (count != 3) { LOG(L_ERR, "error: mediaproxy/getMediaIPFromBlock(): invalid `c=' " "line in SDP body\n"); return -1; } // can also check if tokens[1] == 'IP4' *mediaip = tokens[2]; return 1; }
static int getMediaStreams(str *sdp, str *sessionIP, StreamInfo *streams, int limit) { str tokens[2], block, zone; char *ptr, *sdpEnd; int i, count, streamCount, result; sdpEnd = sdp->s + sdp->len; for (i=0, block=*sdp; i<limit; i++) { ptr = findLineStartingWith(&block, "m=", False); if (!ptr) break; zone.s = ptr + 2; zone.len = findendline(zone.s, sdpEnd - zone.s) - zone.s; count = getStrTokens(&zone, tokens, 2); if (count != 2) { LM_ERR("invalid `m=' line in the SDP body\n"); return -1; } streams[i].type = tokens[0]; streams[i].port = tokens[1]; block.s = zone.s + zone.len; block.len = sdpEnd - block.s; } streamCount = i; for (i=0; i<streamCount; i++) { block.s = streams[i].port.s; if (i < streamCount-1) block.len = streams[i+1].port.s - block.s; else block.len = sdpEnd - block.s; result = getMediaIPFromBlock(&block, &(streams[i].ip)); if (result == -1) { LM_ERR("parse error while getting the contact IP for the " "media stream number %d\n", i+1); return -1; } else if (result == 0) { if (sessionIP->s == NULL) { LM_ERR("media stream number %d doesn't define a contact IP " "and the session-level IP is missing\n", i+1); return -1; } streams[i].ip = *sessionIP; streams[i].localIP = 0; } else { streams[i].localIP = 1; } } return streamCount; }