2
0

parse_ppi_pai.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (C) 2013 Hugh Waite
  3. *
  4. * This file is part of Kamailio, a free SIP server.
  5. *
  6. * Kamailio is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * Kamailio is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * History:
  21. * ---------
  22. * 2013-03-06 Created (hpw)
  23. */
  24. /** Parser :: Parse P-Asserted-Identity: header.
  25. * @file
  26. * @ingroup parser
  27. */
  28. #include "parse_ppi_pai.h"
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include "../dprint.h"
  32. #include "msg_parser.h"
  33. #include "parse_uri.h"
  34. #include "../ut.h"
  35. #include "../mem/mem.h"
  36. /*
  37. * A P-Asserted-Identity or P-Preferred-Identity header value is an addr-spec or name-addr
  38. * There can be only one of any URI scheme (sip(s), tel etc), which may occur on separate
  39. * headers or can be comma separated in a single header.
  40. * RFC3325 only mentions sip(s) and tel schemes, but there is no reason why other schemes
  41. * cannot be used in the future.
  42. */
  43. /*!
  44. *
  45. */
  46. #define NUM_PAI_BODIES 10
  47. int parse_pai_ppi_body(char *buf, int len, p_id_body_t **body)
  48. {
  49. static to_body_t uri_b[NUM_PAI_BODIES]; /* Temporary storage */
  50. int num_uri = 0;
  51. char *tmp;
  52. int i;
  53. memset(uri_b, 0, NUM_PAI_BODIES * sizeof(to_body_t));
  54. tmp = parse_addr_spec(buf, buf+len, &uri_b[num_uri], 1);
  55. if (uri_b[num_uri].error == PARSE_ERROR)
  56. {
  57. LM_ERR("Error parsing PAI/PPI body %u '%.*s'\n", num_uri, len, buf);
  58. return -1;
  59. }
  60. /* should be no header params, but in case there are, free them */
  61. free_to_params(&uri_b[num_uri]);
  62. num_uri++;
  63. while ((*tmp == ',') && (num_uri < NUM_PAI_BODIES))
  64. {
  65. tmp++;
  66. tmp = parse_addr_spec(tmp, buf+len, &uri_b[num_uri], 1);
  67. if (uri_b[num_uri].error == PARSE_ERROR)
  68. {
  69. LM_ERR("Error parsing PAI/PPI body %u '%.*s'\n", num_uri, len, buf);
  70. return -1;
  71. }
  72. /* should be no header params, but in case there are, free them */
  73. free_to_params(&uri_b[num_uri]);
  74. num_uri++;
  75. }
  76. if (num_uri >= NUM_PAI_BODIES)
  77. {
  78. LM_WARN("Too many bodies in PAI/PPI header '%.*s'\n", len, buf);
  79. LM_WARN("Ignoring bodies beyond %u\n", NUM_PAI_BODIES);
  80. }
  81. *body = pkg_malloc(sizeof(p_id_body_t) + num_uri * sizeof(to_body_t));
  82. if (*body == NULL)
  83. {
  84. LM_ERR("No pkg memory for pai/ppi body\n");
  85. return -1;
  86. }
  87. memset(*body, 0, sizeof(p_id_body_t));
  88. (*body)->id = (to_body_t*)((char*)(*body) + sizeof(p_id_body_t));
  89. (*body)->num_ids = num_uri;
  90. for (i=0; i< num_uri; i++)
  91. {
  92. memcpy(&(*body)->id[i], &uri_b[i], sizeof(to_body_t));
  93. }
  94. return 0;
  95. }
  96. int free_pai_ppi_body(p_id_body_t *pid_b)
  97. {
  98. if (pid_b != NULL)
  99. {
  100. pkg_free(pid_b);
  101. }
  102. return 0;
  103. }
  104. /*!
  105. * \brief Parse all P-Asserted-Identity headers
  106. * \param msg The SIP message structure
  107. * \return 0 on success, -1 on failure
  108. */
  109. int parse_pai_header(struct sip_msg* const msg)
  110. {
  111. p_id_body_t *pai_b;
  112. p_id_body_t **prev_pid_b;
  113. hdr_field_t *hf;
  114. void **vp;
  115. if ( !msg->pai )
  116. {
  117. if (parse_headers(msg, HDR_PAI_F, 0) < 0)
  118. {
  119. LM_ERR("Error parsing PAI header\n");
  120. return -1;
  121. }
  122. if ( !msg->pai )
  123. /* No PAI headers */
  124. return -1;
  125. }
  126. if ( msg->pai->parsed )
  127. return 0;
  128. vp = &msg->pai->parsed;
  129. prev_pid_b = (p_id_body_t**)vp;
  130. for (hf = msg->pai; hf != NULL; hf = next_sibling_hdr(hf))
  131. {
  132. if (parse_pai_ppi_body(hf->body.s, hf->body.len, &pai_b) < 0)
  133. {
  134. return -1;
  135. }
  136. hf->parsed = (void*)pai_b;
  137. *prev_pid_b = pai_b;
  138. prev_pid_b = &pai_b->next;
  139. if (parse_headers(msg, HDR_PAI_F, 1) < 0)
  140. {
  141. LM_ERR("Error looking for subsequent PAI header");
  142. return -1;
  143. }
  144. }
  145. return 0;
  146. }
  147. /*!
  148. * \brief Parse all P-Preferred-Identity headers
  149. * \param msg The SIP message structure
  150. * \return 0 on success, -1 on failure
  151. */
  152. int parse_ppi_header(struct sip_msg* const msg)
  153. {
  154. p_id_body_t *ppi_b, *prev_pidb;
  155. hdr_field_t *hf;
  156. if ( !msg->ppi )
  157. {
  158. if (parse_headers(msg, HDR_PPI_F, 0) < 0)
  159. {
  160. LM_ERR("Error parsing PPI header\n");
  161. return -1;
  162. }
  163. if ( !msg->ppi )
  164. /* No PPI headers */
  165. return -1;
  166. }
  167. if ( msg->ppi->parsed )
  168. return 0;
  169. if (parse_pai_ppi_body(msg->ppi->body.s, msg->ppi->body.len, &ppi_b) < 0)
  170. {
  171. return -1;
  172. }
  173. msg->ppi->parsed = (void*)ppi_b;
  174. if (parse_headers(msg, HDR_PPI_F, 1) < 0)
  175. {
  176. LM_ERR("Error looking for subsequent PPI header");
  177. return -1;
  178. }
  179. prev_pidb = ppi_b;
  180. hf = msg->ppi;
  181. if ((hf = next_sibling_hdr(hf)) != NULL)
  182. {
  183. if (parse_pai_ppi_body(hf->body.s, hf->body.len, &ppi_b) < 0)
  184. {
  185. return -1;
  186. }
  187. hf->parsed = (void*)ppi_b;
  188. if (parse_headers(msg, HDR_PPI_F, 1) < 0)
  189. {
  190. LM_ERR("Error looking for subsequent PPI header");
  191. return -1;
  192. }
  193. prev_pidb->next = ppi_b;
  194. prev_pidb = ppi_b;
  195. }
  196. return 0;
  197. }