📄 osip_from.c
字号:
/* The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-) Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Aymeric MOIZARD jack@atosc.org This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*//* HISTORY: v0.5.0: created. v0.6.0: 16/07/2001 complete support for from: new structure new osip_from_parse() method*/#include <stdlib.h>#include <stdio.h>#include <osipparser2/osip_port.h>#include <osipparser2/osip_message.h>#include <osipparser2/osip_parser.h>#include "parser.h"/* adds the from header to message. *//* INPUT : char *hvalue | value of header. *//* OUTPUT: osip_message_t *sip | structure to save results. *//* returns -1 on error. */intosip_message_set_from (osip_message_t * sip, const char *hvalue){ int i; if (hvalue == NULL || hvalue[0] == '\0') return 0; if (sip->from != NULL) return -1; i = osip_from_init (&(sip->from)); if (i != 0) return -1; sip->message_property = 2; i = osip_from_parse (sip->from, hvalue); if (i != 0) { osip_from_free (sip->from); sip->from = NULL; return -1; } return 0;}#ifndef MINISIZE/* returns the from header. *//* INPUT : osip_message_t *sip | sip message. *//* returns null on error. */osip_from_t *osip_message_get_from (const osip_message_t * sip){ return sip->from;}#endifintosip_from_init (osip_from_t ** from){ *from = (osip_from_t *) osip_malloc (sizeof (osip_from_t)); if (*from == NULL) return -1; (*from)->displayname = NULL; (*from)->url = NULL; osip_list_init (&(*from)->gen_params); return 0;}/* deallocates a osip_from_t structure. *//* INPUT : osip_from_t *from | from. */voidosip_from_free (osip_from_t * from){ if (from == NULL) return; if (from->url != NULL) { osip_uri_free (from->url); } osip_free (from->displayname); osip_generic_param_freelist (&from->gen_params); osip_free (from);}/* parses the string as a from header. *//* INPUT : const char *string | pointer to a from string.*//* OUTPUT: osip_from_t *from | structure to save results. *//* returns -1 on error. */intosip_from_parse (osip_from_t * from, const char *hvalue){ const char *displayname; const char *url; const char *url_end; const char *gen_params; /* How to parse: we'll place the pointers: displayname => beginning of displayname url => beginning of url url_end => end of url gen_params => beginning of params examples: jack <sip:jack@atosc.org>;tag=34erZ ^ ^ ^ ^ sip:jack@atosc.org;tag=34erZ ^ ^^ */ displayname = strchr (hvalue, '"'); url = strchr (hvalue, '<'); if (url != NULL) { url_end = strchr (url, '>'); if (url_end == NULL) return -1; } /* SIPit day2: this case was not supported first '"' is placed after '<' and after '>' <sip:ixion@62.254.248.117;method=INVITE>;description="OPEN";expires=28800 if the fisrt quote is after '<' then this is not a quote for a displayname. */ if (displayname != NULL) { if (displayname > url) displayname = NULL; } if ((displayname == NULL) && (url != NULL)) { /* displayname IS A '*token' (not a quoted-string) */ if (hvalue != url) /* displayname exists */ { if (url - hvalue + 1 < 2) return -1; from->displayname = (char *) osip_malloc (url - hvalue + 1); if (from->displayname == NULL) return -1; osip_clrncpy (from->displayname, hvalue, url - hvalue); } url++; /* place pointer on the beginning of url */ } else { if ((displayname != NULL) && (url != NULL)) { /* displayname IS A quoted-string (not a '*token') */ const char *first; const char *second=NULL; /* search for quotes */ first = __osip_quote_find (hvalue); if (first == NULL) return -1; /* missing quote */ second = __osip_quote_find (first + 1); if (second == NULL) return -1; /* missing quote */ if ((first > url)) return -1; if (second - first + 2 >= 2) { from->displayname = (char *) osip_malloc (second - first + 2); if (from->displayname == NULL) return -1; osip_strncpy (from->displayname, first, second - first + 1); /* osip_clrspace(from->displayname); *//*should we do that? */ /* special case: "<sip:joe@big.org>" <sip:joe@really.big.com> */ } /* else displayname is empty? */ url = strchr (second + 1, '<'); if (url == NULL) return -1; /* '<' MUST exist */ url++; } else url = hvalue; /* field does not contains '<' and '>' */ } /* DISPLAY-NAME SET */ /* START of URL KNOWN */ url_end = strchr (url, '>'); if (url_end == NULL) /* sip:jack@atosc.org;tag=023 */ { /* We are sure ';' is the delimiter for from-parameters */ char *host = strchr (url, '@'); if (host != NULL) gen_params = strchr (host, ';'); else gen_params = strchr (url, ';'); if (gen_params != NULL) url_end = gen_params - 1; else url_end = url + strlen (url); } else /* jack <sip:jack@atosc.org;user=phone>;tag=azer */ { gen_params = strchr (url_end, ';'); url_end--; /* place pointer on the beginning of url */ } if (gen_params != NULL) /* now we are sure a param exist */ if (__osip_generic_param_parseall (&from->gen_params, gen_params) == -1) { return -1; } /* set the url */ { char *tmp; int i; if (url_end - url + 2 < 7) return -1; i = osip_uri_init (&(from->url)); if (i != 0) return -1; tmp = (char *) osip_malloc (url_end - url + 2); if (tmp == NULL) return -1; osip_strncpy (tmp, url, url_end - url + 1); i = osip_uri_parse (from->url, tmp); osip_free (tmp); if (i != 0) return -1; } return 0;}/* returns the from header as a string. *//* INPUT : osip_from_t *from | from header. *//* returns -1 on error. */intosip_from_to_str (const osip_from_t * from, char **dest){ char *url; char *buf; int i; size_t len; *dest = NULL; if ((from == NULL) || (from->url == NULL)) return -1; i = osip_uri_to_str (from->url, &url); if (i != 0) return -1; if (from->displayname == NULL) len = strlen (url) + 5; else len = strlen (url) + strlen (from->displayname) + 5; buf = (char *) osip_malloc (len); if (buf == NULL) { osip_free (url); return -1; } if (from->displayname != NULL) sprintf (buf, "%s <%s>", from->displayname, url); else /* from rfc2543bis-04: for authentication related issue! "The To and From header fields always include the < and > delimiters even if the display-name is empty." */ sprintf (buf, "<%s>", url); osip_free (url); { int pos = 0; osip_generic_param_t *u_param; size_t plen; char *tmp; while (!osip_list_eol (&from->gen_params, pos)) { u_param = (osip_generic_param_t *) osip_list_get (&from->gen_params, pos); if (u_param->gvalue == NULL) plen = strlen (u_param->gname) + 2; else plen = strlen (u_param->gname) + strlen (u_param->gvalue) + 3; len = len + plen; buf = (char *) osip_realloc (buf, len); tmp = buf; tmp = tmp + strlen (tmp); if (u_param->gvalue == NULL) sprintf (tmp, ";%s", u_param->gname); else sprintf (tmp, ";%s=%s", u_param->gname, u_param->gvalue); pos++; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -