parse_diversion.c

Go to the documentation of this file.
00001 /*
00002  * $Id: parse_diversion.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 
00023 /*!
00024  * \file
00025  * \brief Parse diversion header field
00026  * \ingroup parser
00027  */
00028 
00029 #include <stdlib.h>
00030 #include <string.h> 
00031 #include "../dprint.h"
00032 #include "../ut.h"
00033 #include "../mem/mem.h"
00034 #include "parse_from.h"
00035 #include "parse_to.h"
00036 #include "msg_parser.h"
00037 
00038 /*!
00039  * \brief This method is used to parse the DIVERSION header
00040  * \param msg sip msg
00041  * \return 0 on success, -1 on failure.
00042  */
00043 int parse_diversion_header(struct sip_msg *msg)
00044 {
00045    struct to_body* diversion_b;
00046    
00047    if (!msg->diversion && (parse_headers(msg, HDR_DIVERSION_F, 0) == -1 ||
00048             !msg->diversion)) {
00049       goto error;
00050    }
00051  
00052    /* maybe the header is already parsed! */
00053    if (msg->diversion->parsed)
00054       return 0;
00055  
00056    /* bad luck! :-( - we have to parse it */
00057    /* first, get some memory */
00058    diversion_b = pkg_malloc(sizeof(struct to_body));
00059    if (diversion_b == 0) {
00060       LM_ERR("out of pkg_memory\n");
00061       goto error;
00062    }
00063  
00064    /* now parse it!! */
00065    memset(diversion_b, 0, sizeof(struct to_body));
00066    parse_to(msg->diversion->body.s, msg->diversion->body.s + msg->diversion->body.len + 1, diversion_b);
00067    if (diversion_b->error == PARSE_ERROR) {
00068       LM_ERR("bad diversion header\n");
00069       pkg_free(diversion_b);
00070       goto error;
00071    }
00072    msg->diversion->parsed = diversion_b;
00073    
00074    return 0;
00075  error:
00076    return -1;
00077 }
00078 
00079 
00080 /*!
00081  * Get value of given diversion parameter
00082  */
00083 str *diversion_param(struct sip_msg *msg, str name)
00084 {
00085     struct to_param *params;
00086 
00087     if (parse_diversion_header(msg) == -1) {
00088    LM_ERR("could not get diversion parameter\n");
00089    return 0;
00090     }
00091 
00092     params =  ((struct to_body*)(msg->diversion->parsed))->param_lst;
00093 
00094     while (params) {
00095    if ((params->name.len == name.len) &&
00096        (strncmp(params->name.s, name.s, name.len) == 0)) {
00097        return &(params->value);
00098    }
00099    params = params->next;
00100     }
00101 
00102     return 0;
00103 }

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