parse_ppi_pai.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. num_uri++;
  61. while ((*tmp == ',') && (num_uri < NUM_PAI_BODIES))
  62. {
  63. tmp++;
  64. tmp = parse_addr_spec(tmp, buf+len, &uri_b[num_uri], 1);
  65. if (uri_b[num_uri].error == PARSE_ERROR)
  66. {
  67. LM_ERR("Error parsing PAI/PPI body %u '%.*s'\n", num_uri, len, buf);
  68. return -1;
  69. }
  70. num_uri++;
  71. }
  72. if (num_uri >= NUM_PAI_BODIES)
  73. {
  74. LM_WARN("Too many bodies in PAI/PPI header '%.*s'\n", len, buf);
  75. LM_WARN("Ignoring bodies beyond %u\n", NUM_PAI_BODIES);
  76. }
  77. *body = pkg_malloc(sizeof(p_id_body_t) + num_uri * sizeof(to_body_t));
  78. if (*body == NULL)
  79. {
  80. LM_ERR("No pkg memory for pai/ppi body\n");
  81. return -1;
  82. }
  83. memset(*body, 0, sizeof(p_id_body_t));
  84. (*body)->id = (to_body_t*)((char*)(*body) + sizeof(p_id_body_t));
  85. (*body)->num_ids = num_uri;
  86. for (i=0; i< num_uri; i++)
  87. {
  88. memcpy(&(*body)->id[i], &uri_b[i], sizeof(to_body_t));
  89. }
  90. return 0;
  91. }
  92. int free_pai_ppi_body(p_id_body_t *pid_b)
  93. {
  94. if (pid_b != NULL)
  95. {
  96. pkg_free(pid_b);
  97. }
  98. return 0;
  99. }
  100. /*!
  101. * \brief Parse all P-Asserted-Identity headers
  102. * \param msg The SIP message structure
  103. * \return 0 on success, -1 on failure
  104. */
  105. int parse_pai_header(struct sip_msg* const msg)
  106. {
  107. p_id_body_t *pai_b;
  108. p_id_body_t **prev_pid_b;
  109. hdr_field_t *hf;
  110. void **vp;
  111. if ( !msg->pai )
  112. {
  113. if (parse_headers(msg, HDR_PAI_F, 0) < 0)
  114. {
  115. LM_ERR("Error parsing PAI header\n");
  116. return -1;
  117. }
  118. if ( !msg->pai )
  119. /* No PAI headers */
  120. return -1;
  121. }
  122. if ( msg->pai->parsed )
  123. return 0;
  124. vp = &msg->pai->parsed;
  125. prev_pid_b = (p_id_body_t**)vp;
  126. for (hf = msg->pai; hf != NULL; hf = next_sibling_hdr(hf))
  127. {
  128. if (parse_pai_ppi_body(hf->body.s, hf->body.len, &pai_b) < 0)
  129. {
  130. return -1;
  131. }
  132. hf->parsed = (void*)pai_b;
  133. *prev_pid_b = pai_b;
  134. prev_pid_b = &pai_b->next;
  135. if (parse_headers(msg, HDR_PAI_F, 1) < 0)
  136. {
  137. LM_ERR("Error looking for subsequent PAI header");
  138. return -1;
  139. }
  140. }
  141. return 0;
  142. }
  143. /*!
  144. * \brief Parse all P-Preferred-Identity headers
  145. * \param msg The SIP message structure
  146. * \return 0 on success, -1 on failure
  147. */
  148. int parse_ppi_header(struct sip_msg* const msg)
  149. {
  150. p_id_body_t *ppi_b, *prev_pidb;
  151. hdr_field_t *hf;
  152. if ( !msg->ppi )
  153. {
  154. if (parse_headers(msg, HDR_PPI_F, 0) < 0)
  155. {
  156. LM_ERR("Error parsing PPI header\n");
  157. return -1;
  158. }
  159. if ( !msg->ppi )
  160. /* No PPI headers */
  161. return -1;
  162. }
  163. if ( msg->ppi->parsed )
  164. return 0;
  165. if (parse_pai_ppi_body(msg->ppi->body.s, msg->ppi->body.len, &ppi_b) < 0)
  166. {
  167. return -1;
  168. }
  169. msg->ppi->parsed = (void*)ppi_b;
  170. if (parse_headers(msg, HDR_PPI_F, 1) < 0)
  171. {
  172. LM_ERR("Error looking for subsequent PPI header");
  173. return -1;
  174. }
  175. prev_pidb = ppi_b;
  176. hf = msg->ppi;
  177. if ((hf = next_sibling_hdr(hf)) != NULL)
  178. {
  179. if (parse_pai_ppi_body(hf->body.s, hf->body.len, &ppi_b) < 0)
  180. {
  181. return -1;
  182. }
  183. hf->parsed = (void*)ppi_b;
  184. if (parse_headers(msg, HDR_PPI_F, 1) < 0)
  185. {
  186. LM_ERR("Error looking for subsequent PPI header");
  187. return -1;
  188. }
  189. prev_pidb->next = ppi_b;
  190. prev_pidb = ppi_b;
  191. }
  192. return 0;
  193. }