parse_allow.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004 Juha Heinanen
00003  *
00004  * This file is part of ser, a free SIP server.
00005  *
00006  * ser is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version
00010  *
00011  * ser is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License 
00017  * along with this program; if not, write to the Free Software 
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00028 #include <stdlib.h>
00029 #include <string.h>
00030 #include "../dprint.h"
00031 #include "../mem/mem.h"
00032 #include "parse_allow.h"
00033 #include "parse_methods.h"
00034 #include "msg_parser.h"
00035 
00036  
00043 int parse_allow_header(struct hdr_field* _hf)
00044 {
00045         struct allow_body* ab = 0;
00046 
00047         if (!_hf) {
00048                 LOG(L_ERR, "parse_allow_header: Invalid parameter value\n");
00049                 return -1;
00050         }
00051         
00052         /* maybe the header is already parsed! */
00053         if (_hf->parsed) {
00054                 return 0;
00055         }
00056 
00057         ab = (struct allow_body*)pkg_malloc(sizeof(struct allow_body));
00058         if (ab == 0) {
00059                 LOG(L_ERR, "ERROR:parse_allow_header: out of pkg_memory\n");
00060                 return -1;
00061         }
00062         memset(ab,'\0', sizeof(struct allow_body));
00063         
00064         if (parse_methods(&(_hf->body), &(ab->allow)) !=0 ) {
00065                 LOG(L_ERR, "ERROR:parse_allow_header: bad allow body header\n");                
00066                 goto error;
00067         }
00068         
00069         ab->allow_all = 0;      
00070         _hf->parsed = (void*)ab;
00071         return 0;
00072 
00073 error:
00074         if (ab) pkg_free(ab);
00075         return -1;
00076 }
00077 
00083 int parse_allow(struct sip_msg *msg)
00084 {       
00085         unsigned int allow;
00086         struct hdr_field  *hdr;
00087 
00088         /* maybe the header is already parsed! */
00089         if (msg->allow && msg->allow->parsed) {
00090                 return 0;
00091         }
00092 
00093         /* parse to the end in order to get all ALLOW headers */
00094         if (parse_headers(msg,HDR_EOH_F,0)==-1 || !msg->allow) {
00095                 return -1;
00096         }
00097         allow = 0;
00098 
00099         for(hdr = msg->allow ; hdr ; hdr = next_sibling_hdr(hdr)) {
00100                 if (hdr->parsed == 0) {
00101                         if(parse_allow_header(hdr) < 0) {
00102                                 return -1;
00103                         }
00104                 }
00105 
00106                 allow |= ((struct allow_body*)hdr->parsed)->allow;
00107         }
00108         
00109         ((struct allow_body*)msg->allow->parsed)->allow_all = allow;
00110     return 0;
00111 }
00112 
00113 
00114 /*
00115  * Release memory
00116  */
00117 void free_allow_body(struct allow_body **ab)
00118 {
00119         if (ab && *ab) {        
00120                 pkg_free(*ab);          
00121                 *ab = 0;
00122         }
00123 }
00124 
00125 
00126 void free_allow_header(struct hdr_field* hf)
00127 {
00128         free_allow_body((struct allow_body**)(void*)(&(hf->parsed)));
00129 }