2
0

parse_from.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. * For a license to use the ser software under conditions
  12. * other than those described here, or to purchase support for this
  13. * software, please contact iptel.org by e-mail at the following addresses:
  14. * [email protected]
  15. *
  16. * ser is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. /*! \file
  26. * \brief Parser :: SIP From header parsing
  27. *
  28. * \ingroup parser
  29. */
  30. #include "parse_from.h"
  31. #include "parse_to.h"
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include "../dprint.h"
  35. #include "msg_parser.h"
  36. #include "parse_uri.h"
  37. #include "../ut.h"
  38. #include "../mem/mem.h"
  39. /*! \brief
  40. * This method is used to parse the from header.
  41. *
  42. * \note It was decided not to parse
  43. * anything in core that is not *needed* so this method gets called by
  44. * rad_acc module and any other modules that needs the FROM header.
  45. *
  46. * params: msg : sip msg
  47. * returns 0 on success,
  48. * -1 on failure.
  49. */
  50. int parse_from_header( struct sip_msg *msg)
  51. {
  52. struct to_body* from_b;
  53. if ( !msg->from && ( parse_headers(msg,HDR_FROM_F,0)==-1 || !msg->from)) {
  54. LOG(L_ERR,"ERROR:parse_from_header: bad msg or missing FROM header\n");
  55. goto error;
  56. }
  57. /* maybe the header is already parsed! */
  58. if (msg->from->parsed)
  59. return 0;
  60. /* bad luck! :-( - we have to parse it */
  61. /* first, get some memory */
  62. from_b = pkg_malloc(sizeof(struct to_body));
  63. if (from_b == 0) {
  64. LOG(L_ERR, "ERROR:parse_from_header: out of pkg_memory\n");
  65. goto error;
  66. }
  67. /* now parse it!! */
  68. memset(from_b, 0, sizeof(struct to_body));
  69. parse_to(msg->from->body.s,msg->from->body.s+msg->from->body.len+1,from_b);
  70. if (from_b->error == PARSE_ERROR) {
  71. LOG(L_ERR, "ERROR:parse_from_header: bad from header [%.*s]\n",
  72. msg->from->body.len, msg->from->body.s);
  73. free_to(from_b);
  74. goto error;
  75. }
  76. msg->from->parsed = from_b;
  77. return 0;
  78. error:
  79. return -1;
  80. }
  81. sip_uri_t *parse_from_uri(sip_msg_t *msg)
  82. {
  83. to_body_t *tb = NULL;
  84. if(msg==NULL)
  85. return NULL;
  86. if(parse_from_header(msg)<0)
  87. {
  88. LM_ERR("cannot parse FROM header\n");
  89. return NULL;
  90. }
  91. if(msg->from==NULL || get_from(msg)==NULL)
  92. return NULL;
  93. tb = get_from(msg);
  94. if(tb->parsed_uri.user.s!=NULL || tb->parsed_uri.host.s!=NULL)
  95. return &tb->parsed_uri;
  96. if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
  97. {
  98. LM_ERR("failed to parse From uri\n");
  99. memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
  100. return NULL;
  101. }
  102. return &tb->parsed_uri;
  103. }