📄 osip_message_parse.c
字号:
{ osip_body_len = strlen (start_of_body); sprintf (tmp, "%i", osip_body_len); i = osip_message_set_content_length (sip, tmp); if (i != 0) return -1; } else return -1; /* Content-type may be non binary data */ } if (length < osip_body_len) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "Message was not receieved enterely. length=%i osip_body_len=%i\n", length, osip_body_len)); return -1; } end_of_body = start_of_body + osip_body_len; tmp = osip_malloc (end_of_body - start_of_body + 2); if (tmp == NULL) return -1; memcpy (tmp, start_of_body, end_of_body - start_of_body); tmp[end_of_body - start_of_body] = '\0'; i = osip_message_set_body (sip, tmp, end_of_body - start_of_body); osip_free (tmp); if (i != 0) return -1; return 0; } /* find the boundary */ i = osip_generic_param_get_byname (&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??? */ { const char *boundary_prefix = "\n--"; size_t len = strlen (ct_param->gvalue); sep_boundary = (char *) osip_malloc (len + sizeof (boundary_prefix)); sprintf (sep_boundary, boundary_prefix); if (ct_param->gvalue[0] == '"' && ct_param->gvalue[len - 1] == '"') strncat (sep_boundary, ct_param->gvalue + 1, len - 2); else strncat (sep_boundary, ct_param->gvalue, len); } len_sep_boundary = strlen (sep_boundary); *next_body = NULL; start_of_body = start_of_buf; end_of_buf = start_of_buf + length; for (;;) { size_t body_len = 0; i = __osip_find_next_occurence (sep_boundary, start_of_body, &start_of_body, end_of_buf); if (i == -1) { osip_free (sep_boundary); return -1; } i = __osip_find_next_occurence (sep_boundary, start_of_body + len_sep_boundary, &end_of_body, end_of_buf); if (i == -1) { osip_free (sep_boundary); return -1; } /* this is the real beginning of body */ start_of_body = start_of_body + len_sep_boundary + 1; if ('\n' == start_of_body[0] || '\r' == start_of_body[0]) start_of_body++; body_len = end_of_body - start_of_body; /* Skip CR before end boundary. */ if (*(end_of_body - 1) == '\r') body_len--; tmp = osip_malloc (body_len + 2); if (tmp == NULL) { osip_free (sep_boundary); return -1; } memcpy (tmp, start_of_body, body_len); tmp[body_len] = '\0'; i = osip_message_set_body_mime (sip, tmp, body_len); osip_free (tmp); if (i == -1) { osip_free (sep_boundary); return -1; } if (strncmp (end_of_body + len_sep_boundary, "--", 2) == 0) { /* end of all bodies */ *next_body = end_of_body; osip_free (sep_boundary); return 0; } /* continue on the next body */ start_of_body = end_of_body; } /* Unreachable code */ /* osip_free (sep_boundary); */ return -1;}/* osip_message_t *sip is filled while analysing buf */static int_osip_message_parse (osip_message_t * sip, const char *buf, size_t length, int sipfrag){ int i; const char *next_header_index; char *tmp; char *beg; tmp = osip_malloc (length + 2); if (tmp == NULL) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "Could not allocate memory.\n")); return -1; } beg = tmp; memcpy (tmp, buf, length); /* may contain binary data */ tmp[length] = '\0'; osip_util_replace_all_lws (tmp); /* parse request or status line */ i = __osip_message_startline_parse (sip, tmp, &next_header_index); if (i == -1 && !sipfrag) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "Could not parse start line of message.\n")); osip_free (beg); return -1; } tmp = (char *) next_header_index; /* parse headers */ i = msg_headers_parse (sip, tmp, &next_header_index); if (i == -1) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "error in msg_headers_parse()\n")); osip_free (beg); return -1; } tmp = (char *) next_header_index; /* this is a *very* simple test... (which handle most cases...) */ if (tmp[0] == '\0' || tmp[1] == '\0' || tmp[2] == '\0') { /* this is mantory in the oSIP stack */ if (sip->content_length == NULL) osip_message_set_content_length (sip, "0"); osip_free (beg); return 0; /* no body found */ } i = msg_osip_body_parse (sip, tmp, &next_header_index, length - (tmp - beg)); osip_free (beg); if (i == -1) { OSIP_TRACE (osip_trace (__FILE__, __LINE__, OSIP_ERROR, NULL, "error in msg_osip_body_parse()\n")); return -1; } /* this is mandatory in the oSIP stack */ if (sip->content_length == NULL) osip_message_set_content_length (sip, "0"); return 0;}intosip_message_parse (osip_message_t * sip, const char *buf, size_t length){ return _osip_message_parse (sip, buf, length, 0);}intosip_message_parse_sipfrag (osip_message_t * sip, const char *buf, size_t length){ return _osip_message_parse (sip, buf, length, 1);}/* This method just add a received parameter in the Via as requested by rfc3261 */intosip_message_fix_last_via_header (osip_message_t * request, const char *ip_addr, int port){ osip_generic_param_t *rport; osip_via_t *via; /* get Top most Via header: */ if (request == NULL) return -1; if (MSG_IS_RESPONSE (request)) return 0; /* Don't fix Via header */ via = osip_list_get (&request->vias, 0); if (via == NULL || via->host == NULL) /* Hey, we could build it? */ return -1; osip_via_param_get_byname (via, "rport", &rport); if (rport != NULL) { if (rport->gvalue == NULL) { rport->gvalue = (char *) osip_malloc (9);#if !defined __PALMOS__ && (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; osip_via_set_received (via, osip_strdup (ip_addr)); return 0;}const char *osip_message_get_reason (int replycode){ struct code_to_reason { int code; const char *reason; }; static const struct code_to_reason reasons1xx[] = { {100, "Trying"}, {180, "Ringing"}, {181, "Call Is Being Forwarded"}, {182, "Queued"}, {183, "Session Progress"}, }; static const struct code_to_reason reasons2xx[] = { {200, "OK"}, {202, "Accepted"}, }; static const struct code_to_reason reasons3xx[] = { {300, "Multiple Choices"}, {301, "Moved Permanently"}, {302, "Moved Temporarily"}, {305, "Use Proxy"}, {380, "Alternative Service"}, }; static const struct code_to_reason reasons4xx[] = { {400, "Bad Request"}, {401, "Unauthorized"}, {402, "Payment Required"}, {403, "Forbidden"}, {404, "Not Found"}, {405, "Method Not Allowed"}, {406, "Not Acceptable"}, {407, "Proxy Authentication Required"}, {408, "Request Timeout"}, {409, "Conflict"}, {410, "Gone"}, {411, "Length Required"}, {412, "Conditional Request Failed"}, {413, "Request Entity Too Large"}, {414, "Request-URI Too Large"}, {415, "Unsupported Media Type"}, {416, "Unsupported Uri Scheme"}, {420, "Bad Extension"}, {422, "Session Interval Too Small"}, {423, "Interval Too Short"}, {480, "Temporarily not available"}, {481, "Call Leg/Transaction Does Not Exist"}, {482, "Loop Detected"}, {483, "Too Many Hops"}, {484, "Address Incomplete"}, {485, "Ambiguous"}, {486, "Busy Here"}, {487, "Request Cancelled"}, {488, "Not Acceptable Here"}, {489, "Bad Event"}, {491, "Request Pending"}, {493, "Undecipherable"}, }; static const struct code_to_reason reasons5xx[] = { {500, "Internal Server Error"}, {501, "Not Implemented"}, {502, "Bad Gateway"}, {503, "Service Unavailable"}, {504, "Gateway Time-out"}, {505, "SIP Version not supported"}, }; static const struct code_to_reason reasons6xx[] = { {600, "Busy Everywhere"}, {603, "Decline"}, {604, "Does not exist anywhere"}, {606, "Not Acceptable"} }; const struct code_to_reason *reasons; int len, i; switch (replycode / 100) { case 1: reasons = reasons1xx; len = sizeof (reasons1xx) / sizeof (*reasons); break; case 2: reasons = reasons2xx; len = sizeof (reasons2xx) / sizeof (*reasons); break; case 3: reasons = reasons3xx; len = sizeof (reasons3xx) / sizeof (*reasons); break; case 4: reasons = reasons4xx; len = sizeof (reasons4xx) / sizeof (*reasons); break; case 5: reasons = reasons5xx; len = sizeof (reasons5xx) / sizeof (*reasons); break; case 6: reasons = reasons6xx; len = sizeof (reasons6xx) / sizeof (*reasons); break; default: return NULL; } for (i = 0; i < len; i++) if (reasons[i].code == replycode) return reasons[i].reason; /* Not found. */ return NULL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -