parse_diversion.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * ser is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. /*! \file
  23. * \brief Parser :: Diversion header
  24. *
  25. * \ingroup parser
  26. */
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include "../dprint.h"
  30. #include "../ut.h"
  31. #include "../mem/mem.h"
  32. #include "parse_from.h"
  33. #include "parse_to.h"
  34. #include "msg_parser.h"
  35. /*! \brief
  36. * This method is used to parse DIVERSION header.
  37. *
  38. * params: msg : sip msg
  39. * returns 0 on success,
  40. * -1 on failure.
  41. */
  42. int parse_diversion_header(struct sip_msg *msg)
  43. {
  44. struct to_body* diversion_b;
  45. if (!msg->diversion && (parse_headers(msg, HDR_DIVERSION_F, 0) == -1 ||
  46. !msg->diversion)) {
  47. goto error;
  48. }
  49. /* maybe the header is already parsed! */
  50. if (msg->diversion->parsed)
  51. return 0;
  52. /* bad luck! :-( - we have to parse it */
  53. /* first, get some memory */
  54. diversion_b = pkg_malloc(sizeof(struct to_body));
  55. if (diversion_b == 0) {
  56. LOG(L_ERR, "ERROR:parse_diversion_header: out of pkg_memory\n");
  57. goto error;
  58. }
  59. /* now parse it!! */
  60. memset(diversion_b, 0, sizeof(struct to_body));
  61. parse_to(msg->diversion->body.s, msg->diversion->body.s + msg->diversion->body.len + 1, diversion_b);
  62. if (diversion_b->error == PARSE_ERROR) {
  63. LOG(L_ERR, "ERROR:parse_diversion_header: bad diversion header\n");
  64. free_to(diversion_b);
  65. goto error;
  66. }
  67. msg->diversion->parsed = diversion_b;
  68. return 0;
  69. error:
  70. return -1;
  71. }
  72. /*! \brief
  73. * Get the value of a given diversion parameter
  74. */
  75. str *get_diversion_param(struct sip_msg *msg, str* name)
  76. {
  77. struct to_param *params;
  78. if (parse_diversion_header(msg) < 0) {
  79. ERR("could not get diversion parameter\n");
  80. return 0;
  81. }
  82. params = ((struct to_body*)(msg->diversion->parsed))->param_lst;
  83. while (params) {
  84. if ((params->name.len == name->len) &&
  85. (strncmp(params->name.s, name->s, name->len) == 0)) {
  86. return &params->value;
  87. }
  88. params = params->next;
  89. }
  90. return 0;
  91. }