parse_diversion.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  *
00006  * This file is part of ser, a free SIP server.
00007  *
00008  * ser 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  * ser 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 
00030 #include <stdlib.h>
00031 #include <string.h> 
00032 #include "../dprint.h"
00033 #include "../ut.h"
00034 #include "../mem/mem.h"
00035 #include "parse_from.h"
00036 #include "parse_to.h"
00037 #include "msg_parser.h"
00038 
00046 int parse_diversion_header(struct sip_msg *msg)
00047 {
00048         struct to_body* diversion_b;
00049         
00050         if (!msg->diversion && (parse_headers(msg, HDR_DIVERSION_F, 0) == -1 ||
00051                                 !msg->diversion)) {
00052                 goto error;
00053         }
00054  
00055         /* maybe the header is already parsed! */
00056         if (msg->diversion->parsed)
00057                 return 0;
00058  
00059         /* bad luck! :-( - we have to parse it */
00060         /* first, get some memory */
00061         diversion_b = pkg_malloc(sizeof(struct to_body));
00062         if (diversion_b == 0) {
00063                 LOG(L_ERR, "ERROR:parse_diversion_header: out of pkg_memory\n");
00064                 goto error;
00065         }
00066  
00067         /* now parse it!! */
00068         memset(diversion_b, 0, sizeof(struct to_body));
00069         parse_to(msg->diversion->body.s, msg->diversion->body.s + msg->diversion->body.len + 1, diversion_b);
00070         if (diversion_b->error == PARSE_ERROR) {
00071                 LOG(L_ERR, "ERROR:parse_diversion_header: bad diversion header\n");
00072                 free_to(diversion_b);
00073                 goto error;
00074         }
00075         msg->diversion->parsed = diversion_b;
00076         
00077         return 0;
00078  error:
00079         return -1;
00080 }
00081 
00082 
00086 str *get_diversion_param(struct sip_msg *msg, str* name)
00087 {
00088     struct to_param *params;
00089 
00090     if (parse_diversion_header(msg) < 0) {
00091                 ERR("could not get diversion parameter\n");
00092                 return 0;
00093     }
00094 
00095     params =  ((struct to_body*)(msg->diversion->parsed))->param_lst;
00096 
00097     while (params) {
00098                 if ((params->name.len == name->len) &&
00099                         (strncmp(params->name.s, name->s, name->len) == 0)) {
00100                         return &params->value;
00101                 }
00102                 params = params->next;
00103     }
00104         
00105     return 0;
00106 }