parse_from.c 3.1 KB

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