parse_allow.c 2.8 KB

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