parse_ppi.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
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 
00029 #include "parse_ppi.h"
00030 #include "../../parser/parse_to.h"
00031 #include "../../parser/parse_uri.h"
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include "../../dprint.h"
00035 #include "../../parser/msg_parser.h"
00036 #include "../../ut.h"
00037 #include "../../mem/mem.h"
00038 
00039 
00048 int parse_ppi_header( struct sip_msg *msg )
00049 {
00050     struct to_body* ppi_b;
00051 
00052     if ( !msg->ppi &&
00053          (parse_headers(msg, HDR_PPI_F,0)==-1 || !msg->ppi)) {
00054         goto error;
00055     }
00056  
00057     /* maybe the header is already parsed! */
00058     if (msg->ppi->parsed)
00059         return 0;
00060  
00061     /* bad luck! :-( - we have to parse it */
00062     /* first, get some memory */
00063     ppi_b = pkg_malloc(sizeof(struct to_body));
00064     if (ppi_b == 0) {
00065         LM_ERR("out of pkg_memory\n");
00066         goto error;
00067     }
00068  
00069     /* now parse it!! */
00070     memset(ppi_b, 0, sizeof(struct to_body));
00071     parse_to(msg->ppi->body.s,
00072              msg->ppi->body.s + msg->ppi->body.len+1,
00073              ppi_b);
00074     if (ppi_b->error == PARSE_ERROR) {
00075         LM_ERR("bad P-Preferred-Identity header\n");
00076         free_to(ppi_b);
00077         goto error;
00078     }
00079         msg->ppi->parsed = ppi_b;
00080  
00081         return 0;
00082  error:
00083         return -1;
00084 }
00085 
00086 
00090 struct sip_uri *parse_ppi_uri(struct sip_msg *msg)
00091 {
00092         struct to_body *tb = NULL;
00093         
00094         if(msg==NULL)
00095                 return NULL;
00096 
00097         if(parse_ppi_header(msg)<0)
00098         {
00099                 LM_ERR("cannot parse P-P-I header\n");
00100                 return NULL;
00101         }
00102         
00103         if(msg->ppi==NULL || get_ppi(msg)==NULL)
00104                 return NULL;
00105 
00106         tb = get_ppi(msg);
00107 
00108         if(tb->parsed_uri.user.s!=NULL || tb->parsed_uri.host.s!=NULL)
00109                 return &tb->parsed_uri;
00110 
00111         if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
00112         {
00113                 LM_ERR("failed to parse P-P-I URI\n");
00114                 memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
00115                 return NULL;
00116         }
00117 
00118         return &tb->parsed_uri;
00119 }