parse_ppi.c

Go to the documentation of this file.
00001 /*
00002  * $Id: parse_ppi.c 4720 2008-08-23 10:56:15Z henningw $
00003  *
00004  * Copyright (C) 2006 Juha Heinanen
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 
00023 /*!
00024  * \file
00025  * \brief P-Preferred-Identity header parser
00026  * \ingroup parser
00027  */
00028 
00029 #include "parse_ppi.h"
00030 #include "parse_to.h"
00031 #include "parse_uri.h"
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include "../dprint.h"
00035 #include "msg_parser.h"
00036 #include "../ut.h"
00037 #include "../errinfo.h"
00038 #include "../mem/mem.h"
00039 
00040 
00041 /*!
00042  * \brief This method is used to parse P-Preferred-Identity header (RFC 3325).
00043  *
00044  * Currently only one name-addr / addr-spec is supported in the header
00045  * and it must contain a sip or sips URI.
00046  * \param msg sip msg
00047  * \return 0 on success, -1 on failure.
00048  */
00049 int parse_ppi_header( struct sip_msg *msg )
00050 {
00051     struct to_body* ppi_b;
00052 
00053     if ( !msg->ppi &&
00054     (parse_headers(msg, HDR_PPI_F,0)==-1 || !msg->ppi)) {
00055    goto error;
00056     }
00057  
00058     /* maybe the header is already parsed! */
00059     if (msg->ppi->parsed)
00060    return 0;
00061  
00062     /* bad luck! :-( - we have to parse it */
00063     /* first, get some memory */
00064     ppi_b = pkg_malloc(sizeof(struct to_body));
00065     if (ppi_b == 0) {
00066    LM_ERR("out of pkg_memory\n");
00067    goto error;
00068     }
00069  
00070     /* now parse it!! */
00071     memset(ppi_b, 0, sizeof(struct to_body));
00072     parse_to(msg->ppi->body.s,
00073         msg->ppi->body.s + msg->ppi->body.len+1,
00074         ppi_b);
00075     if (ppi_b->error == PARSE_ERROR) {
00076    LM_ERR("bad P-Preferred-Identity header\n");
00077    pkg_free(ppi_b);
00078    goto error;
00079     }
00080    msg->ppi->parsed = ppi_b;
00081  
00082    return 0;
00083  error:
00084    return -1;
00085 }
00086 
00087 
00088 /*!
00089  * \brief Parse P-Preferred-Identity header URI
00090  */
00091 struct sip_uri *parse_ppi_uri(struct sip_msg *msg)
00092 {
00093    struct to_body *tb = NULL;
00094    
00095    if(msg==NULL)
00096       return NULL;
00097 
00098    if(parse_ppi_header(msg)<0)
00099    {
00100       LM_ERR("cannot parse P-P-I header\n");
00101       return NULL;
00102    }
00103    
00104    if(msg->ppi==NULL || get_ppi(msg)==NULL)
00105       return NULL;
00106 
00107    tb = get_ppi(msg);
00108 
00109    if(tb->parsed_uri.user.s!=NULL || tb->parsed_uri.host.s!=NULL)
00110       return &tb->parsed_uri;
00111 
00112    if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
00113    {
00114       LM_ERR("failed to parse P-P-I URI\n");
00115       memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
00116       set_err_info(OSER_EC_PARSER, OSER_EL_MEDIUM, "error parsing P-P-I URI");
00117       set_err_reply(400, "bad P-Preferred-Identity uri");
00118       return NULL;
00119    }
00120 
00121    return &tb->parsed_uri;
00122 }

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