parse_allow.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2004 Juha Heinanen
  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 :: Allow header
  22. *
  23. * \ingroup parser
  24. */
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include "../dprint.h"
  28. #include "../mem/mem.h"
  29. #include "parse_allow.h"
  30. #include "parse_methods.h"
  31. #include "msg_parser.h"
  32. /*! \brief
  33. * This method is used to parse Allow header.
  34. *
  35. * \param _hf message header field
  36. * \return 0 on success, -1 on failure.
  37. */
  38. int parse_allow_header(struct hdr_field* _hf)
  39. {
  40. struct allow_body* ab = 0;
  41. if (!_hf) {
  42. LOG(L_ERR, "parse_allow_header: Invalid parameter value\n");
  43. return -1;
  44. }
  45. /* maybe the header is already parsed! */
  46. if (_hf->parsed) {
  47. return 0;
  48. }
  49. ab = (struct allow_body*)pkg_malloc(sizeof(struct allow_body));
  50. if (ab == 0) {
  51. LOG(L_ERR, "ERROR:parse_allow_header: out of pkg_memory\n");
  52. return -1;
  53. }
  54. memset(ab,'\0', sizeof(struct allow_body));
  55. if (parse_methods(&(_hf->body), &(ab->allow)) !=0 ) {
  56. LOG(L_ERR, "ERROR:parse_allow_header: bad allow body header\n");
  57. goto error;
  58. }
  59. ab->allow_all = 0;
  60. _hf->parsed = (void*)ab;
  61. return 0;
  62. error:
  63. if (ab) pkg_free(ab);
  64. return -1;
  65. }
  66. /*!
  67. * \brief This method is used to parse all Allow HF body.
  68. * \param msg sip msg
  69. * \return 0 on success,-1 on failure.
  70. */
  71. int parse_allow(struct sip_msg *msg)
  72. {
  73. unsigned int allow;
  74. struct hdr_field *hdr;
  75. /* maybe the header is already parsed! */
  76. if (msg->allow && msg->allow->parsed) {
  77. return 0;
  78. }
  79. /* parse to the end in order to get all ALLOW headers */
  80. if (parse_headers(msg,HDR_EOH_F,0)==-1 || !msg->allow) {
  81. return -1;
  82. }
  83. allow = 0;
  84. for(hdr = msg->allow ; hdr ; hdr = next_sibling_hdr(hdr)) {
  85. if (hdr->parsed == 0) {
  86. if(parse_allow_header(hdr) < 0) {
  87. return -1;
  88. }
  89. }
  90. allow |= ((struct allow_body*)hdr->parsed)->allow;
  91. }
  92. ((struct allow_body*)msg->allow->parsed)->allow_all = allow;
  93. return 0;
  94. }
  95. /*
  96. * Release memory
  97. */
  98. void free_allow_body(struct allow_body **ab)
  99. {
  100. if (ab && *ab) {
  101. pkg_free(*ab);
  102. *ab = 0;
  103. }
  104. }
  105. void free_allow_header(struct hdr_field* hf)
  106. {
  107. free_allow_body((struct allow_body**)(void*)(&(hf->parsed)));
  108. }