📄 msg_parser.c
字号:
char *hvalue; char *end_of_header; int i; for (;;) { i = find_next_crlf (start_of_header, &end_of_header); if (i == -1) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "End of header Not found\n")); return -1; /* this is an error case! */ } if (end_of_header[0] == '\0') { /* final CRLF is missing */ OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "SIP message does not end with CRLFCRLF\n")); return -1; } /* find the header name */ colon_index = strchr (start_of_header, ':'); if (colon_index == NULL) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "End of header Not found\n")); return -1; /* this is also an error case */ } if (colon_index - start_of_header + 1 < 2) return -1; if (end_of_header <= colon_index) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "Malformed message\n")); return -1; } hname = (char *) smalloc (colon_index - start_of_header + 1); sstrncpy (hname, start_of_header, colon_index - start_of_header); sclrspace (hname); { char *end; /* END of header is (end_of_header-2) if header separation is CRLF */ /* END of header is (end_of_header-1) if header separation is CR or LF */ if ((end_of_header[-2] == '\r') || (end_of_header[-2] == '\n')) end = end_of_header - 2; else end = end_of_header - 1; if ((end) - colon_index < 2) hvalue = NULL; /* some headers (subject) can be empty */ else { hvalue = (char *) smalloc ((end) - colon_index); sstrncpy (hvalue, colon_index + 1, (end) - colon_index - 1); sclrspace (hvalue); } } /* hvalue MAY contains multiple value. In this case, they */ /* are separated by commas. But, a comma may be part of a */ /* quoted-string ("here, and there" is an example where the */ /* comma is not a separator!) */ i = msg_handle_multiple_values (sip, hname, hvalue); sfree (hname); sfree (hvalue); if (i == -1) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "End of header Not found\n")); return -1; } /* the list of headers MUST always end with */ /* CRLFCRLF (also CRCR and LFLF are allowed) */ if ((end_of_header[0] == '\r') || (end_of_header[0] == '\n')) { *body = end_of_header; return 0; /* end of header found */ } /* continue on the next header */ start_of_header = end_of_header; }/* Unreachable code OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_BUG, NULL, "This code cannot be reached\n")); */ return -1;}/* internal method to parse the body */intmsg_body_parse (sip_t * sip, char *start_of_buf, char **next_body){ char *start_of_body; char *end_of_body; char *tmp; int i; char *sep_boundary; generic_param_t *ct_param; /* if MIME-Version: does not exist we just have */ /* to deal with one body and no header... */ if (sip->mime_version == NULL) { /* Mime-Version header does NOT exist */ if (sip->content_type == NULL) return 0; /* no body is attached */ else { size_t body_len; if (start_of_buf[0] == '\0') return -1; /* final CRLF is missing */ /* get rid of the first CRLF */ if ('\r' == start_of_buf[0]) { if ('\n' == start_of_buf[1]) start_of_body = start_of_buf + 2; else start_of_body = start_of_buf + 1; } else if ('\n' == start_of_buf[0]) start_of_body = start_of_buf + 1; else return -1; /* message does not end with CRLFCRLF, CRCR or LFLF */ if (sip->contentlength != NULL) body_len = satoi (sip->contentlength->value); else { /* if content_length does not exist, set it. */ char *tmp = smalloc (8); if (tmp == NULL) return -1; body_len = strlen (start_of_body); sprintf (tmp, "%i", body_len); i = msg_setcontent_length (sip, tmp); sfree (tmp); if (i != 0) return -1; } if (body_len > strlen (start_of_body)) /* we do not receive the */ return -1; /* complete message */ /* end_of_body = start_of_body + strlen(start_of_body); */ end_of_body = start_of_body + body_len; tmp = smalloc (end_of_body - start_of_body + 2); if (tmp == NULL) return -1; sstrncpy (tmp, start_of_body, end_of_body - start_of_body); i = msg_setbody (sip, tmp); sfree (tmp); if (i != 0) return -1; return 0; } } /* find the boundary */ i = generic_param_getbyname (sip->content_type->gen_params, "boundary", &ct_param); if (i != 0) return -1; if (ct_param == NULL) return -1; if (ct_param->gvalue == NULL) return -1; /* No boundary but multiple headers??? */ sep_boundary = (char *) smalloc (strlen (ct_param->gvalue) + 3); sprintf (sep_boundary, "--%s", ct_param->gvalue); *next_body = NULL; start_of_body = start_of_buf; for (;;) { i = find_next_occurence (sep_boundary, start_of_body, &start_of_body); if (i == -1) { sfree (sep_boundary); return -1; } i = find_next_occurence (sep_boundary, start_of_body + strlen (sep_boundary), &end_of_body); if (i == -1) { sfree (sep_boundary); return -1; } /* this is the real beginning of body */ start_of_body = start_of_body + strlen (sep_boundary) + 2; tmp = smalloc (end_of_body - start_of_body + 1); sstrncpy (tmp, start_of_body, end_of_body - start_of_body); i = msg_setbody_mime (sip, tmp); sfree (tmp); if (i == -1) { sfree (sep_boundary); return -1; } if (strncmp (end_of_body + strlen (sep_boundary), "--", 2) == 0) { /* end of all bodies */ *next_body = end_of_body; sfree (sep_boundary); return 0; } /* continue on the next body */ start_of_body = end_of_body; } /* Unreachable code */ /* sfree (sep_boundary); */ return -1;}/* sip_t *sip is filled while analysing buf */intmsg_parse (sip_t * sip, char *buf){ int i; char *next_header_index; /* parse request or status line */ i = msg_startline_parse (sip->strtline, buf, &next_header_index); if (i == -1) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "error in msg_startline_init()\n")); return -1; } buf = next_header_index; /* parse headers */ i = msg_headers_parse (sip, buf, &next_header_index); if (i == -1) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "error in msg_headers_parse()\n")); return -1; } buf = next_header_index; /* this is a *very* simple test... (which handle most cases...) */ if (strlen (buf) < 3) { /* this is mantory in the oSIP stack */ if (sip->contentlength == NULL) msg_setcontent_length (sip, "0"); return 0; /* no body found */ } i = msg_body_parse (sip, buf, &next_header_index); if (i == -1) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "error in msg_body_parse()\n")); return -1; } buf = next_header_index; /* this is mandatory in the oSIP stack */ if (sip->contentlength == NULL) msg_setcontent_length (sip, "0"); return 0;}/* This method just add a received parameter in the Via as requested by rfc3261 */intmsg_fix_last_via_header (sip_t * request, char *ip_addr, int port){ generic_param_t *rport; via_t *via; /* get Top most Via header: */ if (request == NULL || request->strtline == NULL) return -1; if (MSG_IS_RESPONSE (request)) return 0; /* Don't fix Via header */ via = list_get (request->vias, 0); if (via == NULL || via->host == NULL) /* Hey, we could build it? */ return -1; via_param_getbyname (via, "rport", &rport); if (rport != NULL) { if (rport->gvalue == NULL) { rport->gvalue = (char *) smalloc (9);#if (defined WIN32 || defined _WIN32_WCE) _snprintf (rport->gvalue, 8, "%i", port);#else snprintf (rport->gvalue, 8, "%i", port);#endif } /* else bug? */ } /* only add the received parameter if the 'sent-by' value does not contains this ip address */ if (0 == strcmp (via->host, ip_addr)) /* don't need the received parameter */ return 0; via_set_received (via, sgetcopy (ip_addr)); return 0;}char *msg_getreason (int replycode){ int i; i = replycode / 100; if (i == 1) { /* 1xx */ if (replycode == 100) return sgetcopy ("Trying"); if (replycode == 180) return sgetcopy ("Ringing"); if (replycode == 181) return sgetcopy ("Call Is Being Forwarded"); if (replycode == 182) return sgetcopy ("Queued"); if (replycode == 183) return sgetcopy ("Session Progress"); } if (i == 2) { /* 2xx */ return sgetcopy ("OK"); } if (i == 3) { /* 3xx */ if (replycode == 300) return sgetcopy ("Multiple Choices"); if (replycode == 301) return sgetcopy ("Moved Permanently"); if (replycode == 302) return sgetcopy ("Moved Temporarily"); if (replycode == 305) return sgetcopy ("Use Proxy"); if (replycode == 380) return sgetcopy ("Alternative Service"); } if (i == 4) { /* 4xx */ if (replycode == 400) return sgetcopy ("Bad Request"); if (replycode == 401) return sgetcopy ("Unauthorized"); if (replycode == 402) return sgetcopy ("Payment Required"); if (replycode == 403) return sgetcopy ("Forbidden"); if (replycode == 404) return sgetcopy ("Not Found"); if (replycode == 405) return sgetcopy ("Method Not Allowed"); if (replycode == 406) return sgetcopy ("Not Acceptable"); if (replycode == 407) return sgetcopy ("Proxy Authentication Required"); if (replycode == 408) return sgetcopy ("Request Timeout"); if (replycode == 409) return sgetcopy ("Conflict"); if (replycode == 410) return sgetcopy ("Gone"); if (replycode == 411) return sgetcopy ("Length Required"); if (replycode == 413) return sgetcopy ("Request Entity Too Large"); if (replycode == 414) return sgetcopy ("Request-URI Too Large"); if (replycode == 415) return sgetcopy ("Unsupported Media Type"); if (replycode == 416) return sgetcopy ("Unsupported Uri Scheme"); if (replycode == 420) return sgetcopy ("Bad Extension"); /* Robin Nayathodan <roooot@softhome.net> N.K Electronics INDIA RFC 3261 10.3.7 */ if (replycode == 423) return sgetcopy ("Interval Too Short"); if (replycode == 480) return sgetcopy ("Temporarily not available"); if (replycode == 481) return sgetcopy ("Call Leg/Transaction Does Not Exist"); if (replycode == 482) return sgetcopy ("Loop Detected"); if (replycode == 483) return sgetcopy ("Too Many Hops"); if (replycode == 484) return sgetcopy ("Address Incomplete"); if (replycode == 485) return sgetcopy ("Ambiguous"); if (replycode == 486) return sgetcopy ("Busy Here"); if (replycode == 487) return sgetcopy ("Request Cancelled"); if (replycode == 488) return sgetcopy ("Not Acceptable Here"); if (replycode == 489) return sgetcopy ("Bad Event"); } if (i == 5) { /* 5xx */ if (replycode == 500) return sgetcopy ("Internal Server Error"); if (replycode == 501) return sgetcopy ("Not Implemented"); if (replycode == 502) return sgetcopy ("Bad Gateway"); if (replycode == 503) return sgetcopy ("Service Unavailable"); if (replycode == 504) return sgetcopy ("Gateway Time-out"); if (replycode == 505) return sgetcopy ("SIP Version not supported"); } if (i == 6) { /* 6xx */ if (replycode == 600) return sgetcopy ("Busy Everywhere"); if (replycode == 603) return sgetcopy ("Decline"); if (replycode == 604) return sgetcopy ("Does not exist anywhere"); if (replycode == 606) return sgetcopy ("Not Acceptable"); } return NULL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -