parse_from.c

Go to the documentation of this file.
00001 /*
00002  * $Id: parse_from.c 4720 2008-08-23 10:56:15Z henningw $
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License 
00019  * along with this program; if not, write to the Free Software 
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  * History:
00023  * --------
00024  * 2005-07-12 missing TAG is reported as error (bogdan)
00025  */
00026 
00027 /*!
00028  * \file
00029  * \brief Parse from header
00030  * \ingroup parser
00031  */
00032 
00033 #include "parse_from.h"
00034 #include "parse_to.h"
00035 #include "parse_uri.h"
00036 #include <stdlib.h>
00037 #include <string.h>
00038 #include "../dprint.h"
00039 #include "msg_parser.h"
00040 #include "../ut.h"
00041 #include "../errinfo.h"
00042 #include "../mem/mem.h"
00043 
00044 /*!
00045  * This method is used to parse the from header. It was decided not to parse
00046  * anything in core that is not *needed* so this method gets called by 
00047  * rad_acc module and any other modules that needs the FROM header.
00048  * \param msg sip msg
00049  * \return 0 on success, negative result on failure.
00050  */
00051 int parse_from_header( struct sip_msg *msg)
00052 {
00053    struct to_body* from_b;
00054 
00055    if ( !msg->from && ( parse_headers(msg,HDR_FROM_F,0)==-1 || !msg->from)) {
00056       LM_ERR("bad msg or missing FROM header\n");
00057       goto error;
00058    }
00059 
00060    /* maybe the header is already parsed! */
00061    if (msg->from->parsed)
00062       return 0;
00063 
00064    /* bad luck! :-( - we have to parse it */
00065    /* first, get some memory */
00066    from_b = pkg_malloc(sizeof(struct to_body));
00067    if (from_b == 0) {
00068       LM_ERR("out of pkg_memory\n");
00069       goto error;
00070    }
00071 
00072    /* now parse it!! */
00073    memset(from_b, 0, sizeof(struct to_body));
00074    parse_to(msg->from->body.s,msg->from->body.s+msg->from->body.len+1,from_b);
00075    if (from_b->error == PARSE_ERROR) {
00076       LM_ERR("bad from header\n");
00077       pkg_free(from_b);
00078       set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM, "error parsing From");
00079       set_err_reply(400, "bad From header");
00080       goto error;
00081    }
00082    /* REGISTER doesn't have a from tag :( -bogdan
00083    if (from_b->tag_value.len==0 || from_b->tag_value.s==0) {
00084       LM_ERR("missing TAG value\n");
00085       free_to(from_b);
00086       goto error;
00087    }
00088    */
00089    msg->from->parsed = from_b;
00090 
00091    return 0;
00092 error:
00093    return -1;
00094 }
00095 
00096 struct sip_uri *parse_from_uri(struct sip_msg *msg)
00097 {
00098    struct to_body *tb = NULL;
00099    
00100    if(msg==NULL)
00101       return NULL;
00102 
00103    if(parse_from_header(msg)<0)
00104    {
00105       LM_ERR("cannot parse FROM header\n");
00106       return NULL;
00107    }
00108    
00109    if(msg->from==NULL || get_from(msg)==NULL)
00110       return NULL;
00111 
00112    tb = get_from(msg);
00113 
00114    if(tb->parsed_uri.user.s!=NULL || tb->parsed_uri.host.s!=NULL)
00115       return &tb->parsed_uri;
00116 
00117    if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
00118    {
00119       LM_ERR("failed to parse From uri\n");
00120       memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
00121       set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM, "error parsing From uri");
00122       set_err_reply(400, "bad From uri");
00123       return NULL;
00124    }
00125 
00126    return &tb->parsed_uri;
00127 }

Generated on Thu May 24 00:00:28 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6