PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/libosip2-3.6.0/src/osipparser2/osip_header.c

#
C | 309 lines | 217 code | 47 blank | 45 comment | 75 complexity | be9351a626712af71f796ac481ac008d MD5 | raw file
Possible License(s): LGPL-2.0
  1. /*
  2. The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
  3. Copyright (C) 2001,2002,2003,2004,2005,2006,2007 Aymeric MOIZARD jack@atosc.org
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <osipparser2/osip_port.h>
  19. #include <osipparser2/osip_message.h>
  20. #include <osipparser2/osip_parser.h>
  21. /* Add a header to a SIP message. */
  22. /* INPUT : char *hname | pointer to a header name. */
  23. /* INPUT : char *hvalue | pointer to a header value. */
  24. /* OUTPUT: osip_message_t *sip | structure to save results. */
  25. /* returns -1 on error. */
  26. int
  27. osip_message_set_header(osip_message_t * sip, const char *hname,
  28. const char *hvalue)
  29. {
  30. osip_header_t *h;
  31. int i;
  32. if (sip == NULL || hname == NULL)
  33. return OSIP_BADPARAMETER;
  34. i = osip_header_init(&h);
  35. if (i != 0)
  36. return i;
  37. h->hname = (char *) osip_malloc(strlen(hname) + 1);
  38. if (h->hname == NULL) {
  39. osip_header_free(h);
  40. return OSIP_NOMEM;
  41. }
  42. osip_clrncpy(h->hname, hname, strlen(hname));
  43. if (hvalue != NULL) { /* some headers can be null ("subject:") */
  44. h->hvalue = (char *) osip_malloc(strlen(hvalue) + 1);
  45. if (h->hvalue == NULL) {
  46. osip_header_free(h);
  47. return OSIP_NOMEM;
  48. }
  49. osip_clrncpy(h->hvalue, hvalue, strlen(hvalue));
  50. } else
  51. h->hvalue = NULL;
  52. sip->message_property = 2;
  53. osip_list_add(&sip->headers, h, -1);
  54. return OSIP_SUCCESS; /* ok */
  55. }
  56. /* Add a or replace exising header to a SIP message. */
  57. /* INPUT : char *hname | pointer to a header name. */
  58. /* INPUT : char *hvalue | pointer to a header value. */
  59. /* OUTPUT: osip_message_t *sip | structure to save results. */
  60. /* returns -1 on error. */
  61. int
  62. osip_message_replace_header(osip_message_t * sip, const char *hname,
  63. const char *hvalue)
  64. {
  65. osip_header_t *h, *oldh;
  66. int i, oldpos = -1;
  67. if (sip == NULL || hname == NULL)
  68. return OSIP_BADPARAMETER;
  69. oldpos = osip_message_header_get_byname(sip, hname, 0, &oldh);
  70. i = osip_header_init(&h);
  71. if (i != 0)
  72. return i;
  73. h->hname = (char *) osip_malloc(strlen(hname) + 1);
  74. if (h->hname == NULL) {
  75. osip_header_free(h);
  76. return OSIP_NOMEM;
  77. }
  78. osip_clrncpy(h->hname, hname, strlen(hname));
  79. if (hvalue != NULL) { /* some headers can be null ("subject:") */
  80. h->hvalue = (char *) osip_malloc(strlen(hvalue) + 1);
  81. if (h->hvalue == NULL) {
  82. osip_header_free(h);
  83. return OSIP_NOMEM;
  84. }
  85. osip_clrncpy(h->hvalue, hvalue, strlen(hvalue));
  86. } else
  87. h->hvalue = NULL;
  88. if (oldpos != -1) {
  89. osip_list_remove(&sip->headers, oldpos);
  90. osip_header_free(oldh);
  91. }
  92. sip->message_property = 2;
  93. osip_list_add(&sip->headers, h, -1);
  94. return OSIP_SUCCESS; /* ok */
  95. }
  96. #ifndef MINISIZE
  97. /* Add a header to a SIP message at the top of the list. */
  98. /* INPUT : char *hname | pointer to a header name. */
  99. /* INPUT : char *hvalue | pointer to a header value. */
  100. /* OUTPUT: osip_message_t *sip | structure to save results. */
  101. /* returns -1 on error. */
  102. int
  103. osip_message_set_topheader(osip_message_t * sip, const char *hname,
  104. const char *hvalue)
  105. {
  106. osip_header_t *h;
  107. int i;
  108. if (sip == NULL || hname == NULL)
  109. return OSIP_BADPARAMETER;
  110. i = osip_header_init(&h);
  111. if (i != 0)
  112. return i;
  113. h->hname = (char *) osip_malloc(strlen(hname) + 1);
  114. if (h->hname == NULL) {
  115. osip_header_free(h);
  116. return OSIP_NOMEM;
  117. }
  118. osip_clrncpy(h->hname, hname, strlen(hname));
  119. if (hvalue != NULL) { /* some headers can be null ("subject:") */
  120. h->hvalue = (char *) osip_malloc(strlen(hvalue) + 1);
  121. if (h->hvalue == NULL) {
  122. osip_header_free(h);
  123. return OSIP_NOMEM;
  124. }
  125. osip_clrncpy(h->hvalue, hvalue, strlen(hvalue));
  126. } else
  127. h->hvalue = NULL;
  128. sip->message_property = 2;
  129. osip_list_add(&sip->headers, h, 0);
  130. return OSIP_SUCCESS; /* ok */
  131. }
  132. /* Get a header in a SIP message. */
  133. /* INPUT : int pos | position of number in message. */
  134. /* OUTPUT: osip_message_t *sip | structure to scan for a header .*/
  135. /* return null on error. */
  136. int
  137. osip_message_get_header(const osip_message_t * sip, int pos, osip_header_t ** dest)
  138. {
  139. *dest = NULL;
  140. if (osip_list_size(&sip->headers) <= pos)
  141. return OSIP_UNDEFINED_ERROR; /* NULL */
  142. *dest = (osip_header_t *) osip_list_get(&sip->headers, pos);
  143. return pos;
  144. }
  145. #endif
  146. /* Get a header in a SIP message. */
  147. /* INPUT : int pos | position where we start the search */
  148. /* OUTPUT: osip_message_t *sip | structure to look for header. */
  149. /* return the current position of the header found */
  150. /* and -1 on error. */
  151. int
  152. osip_message_header_get_byname(const osip_message_t * sip, const char *hname,
  153. int pos, osip_header_t ** dest)
  154. {
  155. int i;
  156. osip_header_t *tmp;
  157. *dest = NULL;
  158. i = pos;
  159. if (osip_list_size(&sip->headers) <= pos)
  160. return OSIP_UNDEFINED_ERROR; /* NULL */
  161. while (osip_list_size(&sip->headers) > i) {
  162. tmp = (osip_header_t *) osip_list_get(&sip->headers, i);
  163. if (osip_strcasecmp(tmp->hname, hname) == 0) {
  164. *dest = tmp;
  165. return i;
  166. }
  167. i++;
  168. }
  169. return OSIP_UNDEFINED_ERROR; /* not found */
  170. }
  171. int osip_header_init(osip_header_t ** header)
  172. {
  173. *header = (osip_header_t *) osip_malloc(sizeof(osip_header_t));
  174. if (*header == NULL)
  175. return OSIP_NOMEM;
  176. (*header)->hname = NULL;
  177. (*header)->hvalue = NULL;
  178. return OSIP_SUCCESS;
  179. }
  180. void osip_header_free(osip_header_t * header)
  181. {
  182. if (header == NULL)
  183. return;
  184. osip_free(header->hname);
  185. osip_free(header->hvalue);
  186. header->hname = NULL;
  187. header->hvalue = NULL;
  188. osip_free(header);
  189. }
  190. /* returns the header as a string. */
  191. /* INPUT : osip_header_t *header | header. */
  192. /* returns null on error. */
  193. int osip_header_to_str(const osip_header_t * header, char **dest)
  194. {
  195. size_t len, hlen;
  196. *dest = NULL;
  197. if ((header == NULL) || (header->hname == NULL))
  198. return OSIP_BADPARAMETER;
  199. len = 0; hlen = strlen(header->hname);
  200. if (header->hvalue != NULL)
  201. len = strlen(header->hvalue);
  202. *dest = (char *) osip_malloc(hlen + len + 3);
  203. if (*dest == NULL)
  204. return OSIP_NOMEM;
  205. if (header->hvalue != NULL)
  206. snprintf(*dest, hlen+len+3, "%s: %s", header->hname, header->hvalue);
  207. else
  208. snprintf(*dest, hlen + len + 3, "%s: ", header->hname);
  209. if (*dest[0] > 'a' && *dest[0] < 'z')
  210. *dest[0] = (*dest[0] - 32);
  211. return OSIP_SUCCESS;
  212. }
  213. char *osip_header_get_name(const osip_header_t * header)
  214. {
  215. if (header == NULL)
  216. return NULL;
  217. return header->hname;
  218. }
  219. void osip_header_set_name(osip_header_t * header, char *name)
  220. {
  221. header->hname = name;
  222. }
  223. char *osip_header_get_value(const osip_header_t * header)
  224. {
  225. if (header == NULL)
  226. return NULL;
  227. return header->hvalue;
  228. }
  229. void osip_header_set_value(osip_header_t * header, char *value)
  230. {
  231. header->hvalue = value;
  232. }
  233. int osip_header_clone(const osip_header_t * header, osip_header_t ** dest)
  234. {
  235. int i;
  236. osip_header_t *he;
  237. *dest = NULL;
  238. if (header == NULL)
  239. return OSIP_BADPARAMETER;
  240. if (header->hname == NULL)
  241. return OSIP_BADPARAMETER;
  242. i = osip_header_init(&he);
  243. if (i != 0)
  244. return i;
  245. he->hname = osip_strdup(header->hname);
  246. if (he->hname == NULL) {
  247. osip_header_free(he);
  248. return OSIP_NOMEM;
  249. }
  250. if (header->hvalue != NULL) {
  251. he->hvalue = osip_strdup(header->hvalue);
  252. if (he->hvalue == NULL) {
  253. osip_header_free(he);
  254. return OSIP_NOMEM;
  255. }
  256. }
  257. *dest = he;
  258. return OSIP_SUCCESS;
  259. }