parse_from.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  * For a license to use the ser software under conditions
00014  * other than those described here, or to purchase support for this
00015  * software, please contact iptel.org by e-mail at the following addresses:
00016  *    info@iptel.org
00017  *
00018  * ser is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License 
00024  * along with this program; if not, write to the Free Software 
00025  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026  */
00027 
00034 #include "parse_from.h"
00035 #include "parse_to.h"
00036 #include <stdlib.h>
00037 #include <string.h>
00038 #include "../dprint.h"
00039 #include "msg_parser.h"
00040 #include "parse_uri.h"
00041 #include "../ut.h"
00042 #include "../mem/mem.h"
00043 
00055 int parse_from_header( struct sip_msg *msg)
00056 {
00057         struct to_body* from_b;
00058 
00059         if ( !msg->from && ( parse_headers(msg,HDR_FROM_F,0)==-1 || !msg->from)) {
00060                 LOG(L_ERR,"ERROR:parse_from_header: bad msg or missing FROM header\n");
00061                 goto error;
00062         }
00063 
00064         /* maybe the header is already parsed! */
00065         if (msg->from->parsed)
00066                 return 0;
00067 
00068         /* bad luck! :-( - we have to parse it */
00069         /* first, get some memory */
00070         from_b = pkg_malloc(sizeof(struct to_body));
00071         if (from_b == 0) {
00072                 LOG(L_ERR, "ERROR:parse_from_header: out of pkg_memory\n");
00073                 goto error;
00074         }
00075 
00076         /* now parse it!! */
00077         memset(from_b, 0, sizeof(struct to_body));
00078         parse_to(msg->from->body.s,msg->from->body.s+msg->from->body.len+1,from_b);
00079         if (from_b->error == PARSE_ERROR) {
00080                 LOG(L_ERR, "ERROR:parse_from_header: bad from header [%.*s]\n",
00081                                 msg->from->body.len, msg->from->body.s);
00082                 free_to(from_b);
00083                 goto error;
00084         }
00085         msg->from->parsed = from_b;
00086 
00087         return 0;
00088 error:
00089         return -1;
00090 }
00091 
00092 sip_uri_t *parse_from_uri(sip_msg_t *msg)
00093 {
00094         to_body_t *tb = NULL;
00095         
00096         if(msg==NULL)
00097                 return NULL;
00098 
00099         if(parse_from_header(msg)<0)
00100         {
00101                 LM_ERR("cannot parse FROM header\n");
00102                 return NULL;
00103         }
00104         
00105         if(msg->from==NULL || get_from(msg)==NULL)
00106                 return NULL;
00107 
00108         tb = get_from(msg);
00109         
00110         if(tb->parsed_uri.user.s!=NULL || tb->parsed_uri.host.s!=NULL)
00111                 return &tb->parsed_uri;
00112         
00113         if (parse_uri(tb->uri.s, tb->uri.len , &tb->parsed_uri)<0)
00114         {
00115                 LM_ERR("failed to parse From uri\n");
00116                 memset(&tb->parsed_uri, 0, sizeof(struct sip_uri));
00117                 return NULL;
00118         }
00119         return &tb->parsed_uri;
00120 }