parse_diversion.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (C) 2001-2003 FhG Fokus
  3. *
  4. * This file is part of ser, a free SIP server.
  5. *
  6. * ser 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. * ser 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. /*! \file
  21. * \brief Parser :: Diversion header
  22. *
  23. * \ingroup parser
  24. */
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include "../dprint.h"
  28. #include "../ut.h"
  29. #include "../mem/mem.h"
  30. #include "parse_from.h"
  31. #include "parse_to.h"
  32. #include "msg_parser.h"
  33. /*! \brief
  34. * This method is used to parse DIVERSION header.
  35. *
  36. * params: msg : sip msg
  37. * returns 0 on success,
  38. * -1 on failure.
  39. */
  40. int parse_diversion_header(struct sip_msg *msg)
  41. {
  42. struct to_body* diversion_b;
  43. if (!msg->diversion && (parse_headers(msg, HDR_DIVERSION_F, 0) == -1 ||
  44. !msg->diversion)) {
  45. goto error;
  46. }
  47. /* maybe the header is already parsed! */
  48. if (msg->diversion->parsed)
  49. return 0;
  50. /* bad luck! :-( - we have to parse it */
  51. /* first, get some memory */
  52. diversion_b = pkg_malloc(sizeof(struct to_body));
  53. if (diversion_b == 0) {
  54. LOG(L_ERR, "ERROR:parse_diversion_header: out of pkg_memory\n");
  55. goto error;
  56. }
  57. /* now parse it!! */
  58. memset(diversion_b, 0, sizeof(struct to_body));
  59. parse_to(msg->diversion->body.s, msg->diversion->body.s + msg->diversion->body.len + 1, diversion_b);
  60. if (diversion_b->error == PARSE_ERROR) {
  61. LOG(L_ERR, "ERROR:parse_diversion_header: bad diversion header\n");
  62. free_to(diversion_b);
  63. goto error;
  64. }
  65. msg->diversion->parsed = diversion_b;
  66. return 0;
  67. error:
  68. return -1;
  69. }
  70. /*! \brief
  71. * Get the value of a given diversion parameter
  72. */
  73. str *get_diversion_param(struct sip_msg *msg, str* name)
  74. {
  75. struct to_param *params;
  76. if (parse_diversion_header(msg) < 0) {
  77. ERR("could not get diversion parameter\n");
  78. return 0;
  79. }
  80. params = ((struct to_body*)(msg->diversion->parsed))->param_lst;
  81. while (params) {
  82. if ((params->name.len == name->len) &&
  83. (strncmp(params->name.s, name->s, name->len) == 0)) {
  84. return &params->value;
  85. }
  86. params = params->next;
  87. }
  88. return 0;
  89. }