cr_fixup.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Copyright (C) 2007-2008 1&1 Internet AG
00005  *
00006  * This file is part of SIP-router, a free SIP server.
00007  *
00008  * SIP-router 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  * SIP-router 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 "../../mod_fix.h"
00031 #include "../../mem/mem.h"
00032 #include "cr_fixup.h"
00033 #include "carrierroute.h"
00034 #include "cr_map.h"
00035 #include "cr_domain.h"
00036 #include "prime_hash.h"
00037 #include "cr_data.h"
00038 
00039 
00048 static int carrier_name_2_id(const str *name) {
00049         int id;
00050         struct route_data_t * rd;
00051         
00052         do {
00053                 rd = get_data();
00054         } while (rd == NULL);
00055 
00056         id = map_name2id(rd->carrier_map, rd->carrier_num, name);
00057         
00058         release_data(rd);
00059 
00060         return id;
00061 }
00062 
00063 
00072 static int domain_name_2_id(const str *name) {
00073         int id;
00074         struct route_data_t * rd;
00075 
00076         do {
00077                 rd = get_data();
00078         } while (rd == NULL);
00079         
00080         id = map_name2id(rd->domain_map, rd->domain_num, name);
00081         
00082         release_data(rd);
00083 
00084         return id;
00085 }
00086 
00087 
00095 static enum hash_source hash_fixup(const char * my_hash_source) {
00096         if (strcasecmp("call_id", my_hash_source) == 0) {
00097                 return shs_call_id;
00098         } else if (strcasecmp("from_uri", my_hash_source) == 0) {
00099                 return shs_from_uri;
00100         } else if (strcasecmp("from_user", my_hash_source) == 0) {
00101                 return shs_from_user;
00102         } else if (strcasecmp("to_uri", my_hash_source) == 0) {
00103                 return shs_to_uri;
00104         } else if (strcasecmp("to_user", my_hash_source) == 0) {
00105                 return shs_to_user;
00106         } else if (strcasecmp("rand", my_hash_source) == 0) {
00107                 return shs_rand;
00108         } else {
00109                 return shs_error;
00110         }
00111 }
00112 
00113 
00122 static int carrier_fixup(void ** param) {
00123         int id;
00124 
00125         if (fixup_spve_null(param, 1) !=0) {
00126                 LM_ERR("could not fixup parameter");
00127                 return -1;
00128         }
00129 
00130         if (((gparam_p)(*param))->type == GPARAM_TYPE_STR) {
00131                 /* This is a name string, convert to a int */
00132                 ((gparam_p)(*param))->type=GPARAM_TYPE_INT;
00133                 /* get carrier id */
00134                 if ((id = carrier_name_2_id(&((gparam_p)(*param))->v.str)) < 0) {
00135                         LM_ERR("could not find carrier name '%.*s' in map\n", ((gparam_p)(*param))->v.str.len, ((gparam_p)(*param))->v.str.s);
00136                         pkg_free(*param);
00137                         return -1;
00138                 }
00139                 ((gparam_p)(*param))->v.i = id;
00140         }
00141         return 0;
00142 }
00143 
00144 
00153 static int domain_fixup(void ** param) {
00154         int id;
00155 
00156         if (fixup_spve_null(param, 1) !=0) {
00157                 LM_ERR("could not fixup parameter");
00158                 return -1;
00159         }
00160 
00161         if (((gparam_p)(*param))->type == GPARAM_TYPE_STR) {
00162                 /* This is a name string, convert to a int */
00163                 ((gparam_p)(*param))->type=GPARAM_TYPE_INT;
00164                 /* get domain id */
00165                 if ((id = domain_name_2_id(&(((gparam_p)(*param))->v.str))) < 0) {
00166                         LM_ERR("could not find domain name '%.*s' in map\n", ((gparam_p)(*param))->v.str.len, ((gparam_p)(*param))->v.str.s);
00167                         pkg_free(*param);
00168                         return -1;
00169                 }
00170                 ((gparam_p)(*param))->v.i = id;
00171         }
00172         return 0;
00173 }
00174 
00175 
00183 static int avp_name_fixup(void ** param) {
00184 
00185         if (fixup_spve_null(param, 1) !=0) {
00186                 LM_ERR("could not fixup parameter");
00187                 return -1;
00188         }
00189         if (((gparam_p)(*param))->v.pve->spec.type == PVT_AVP &&
00190                         ((gparam_p)(*param))->v.pve->spec.pvp.pvn.u.isname.name.s.len == 0 &&
00191                         ((gparam_p)(*param))->v.pve->spec.pvp.pvn.u.isname.name.s.s == 0) {
00192                 LM_ERR("malformed or non AVP type definition\n");
00193                 return -1;
00194         }
00195         return 0;
00196 }
00197 
00198 
00209 int cr_route_fixup(void ** param, int param_no) {
00210         enum hash_source my_hash_source;
00211 
00212         if (param_no == 1) {
00213                 /* carrier */
00214                 if (carrier_fixup(param) < 0) {
00215                         LM_ERR("cannot fixup parameter %d\n", param_no);
00216                         return -1;
00217                 }
00218         }
00219         else if (param_no == 2) {
00220                 /* domain */
00221                 if (domain_fixup(param) < 0) {
00222                         LM_ERR("cannot fixup parameter %d\n", param_no);
00223                         return -1;
00224                 }
00225         }
00226         else if ((param_no == 3) || (param_no == 4)){
00227                 /* prefix matching, rewrite user */
00228                 if (fixup_spve_null(param, 1) != 0) {
00229                         LM_ERR("cannot fixup parameter %d\n", param_no);
00230                         return -1;
00231                 }
00232         }
00233         else if (param_no == 5) {
00234                 /* hash source */
00235                 if ((my_hash_source = hash_fixup((char *)*param)) == shs_error) {
00236                         LM_ERR("invalid hash source\n");
00237                         return -1;
00238                 }
00239                 pkg_free(*param);
00240                 *param = (void *)my_hash_source;
00241         }
00242         else if (param_no == 6) {
00243                 /* destination avp name */
00244                 if (avp_name_fixup(param) < 0) {
00245                         LM_ERR("cannot fixup parameter %d\n", param_no);
00246                         return -1;
00247                 }
00248         }
00249 
00250         return 0;
00251 }
00252 
00253 
00264 int cr_load_next_domain_fixup(void ** param, int param_no) {
00265         if (param_no == 1) {
00266                 /* carrier */
00267                 if (carrier_fixup(param) < 0) {
00268                         LM_ERR("cannot fixup parameter %d\n", param_no);
00269                         return -1;
00270                 }
00271         }
00272         else if (param_no == 2) {
00273                 /* domain */
00274                 if (domain_fixup(param) < 0) {
00275                         LM_ERR("cannot fixup parameter %d\n", param_no);
00276                         return -1;
00277                 }
00278         }
00279         else if ((param_no == 3) || (param_no == 4) || (param_no == 5)) {
00280                 /* prefix matching, host, reply code */
00281                 if (fixup_spve_null(param, 1) != 0) {
00282                         LM_ERR("cannot fixup parameter %d\n", param_no);
00283                         return -1;
00284                 }
00285         }
00286         else if (param_no == 6) {
00287                 /* destination avp name */
00288                 if (avp_name_fixup(param) < 0) {
00289                         LM_ERR("cannot fixup parameter %d\n", param_no);
00290                         return -1;
00291                 }
00292         }
00293 
00294         return 0;
00295 }
00296 
00297 
00306 int cr_load_user_carrier_fixup(void ** param, int param_no) {
00307         if (mode == CARRIERROUTE_MODE_FILE) {
00308                 LM_ERR("command cr_user_rewrite_uri can't be used in file mode\n");
00309                 return -1;
00310         }
00311 
00312         if ((param_no == 1) || (param_no == 2)) {
00313                 /* user, domain */
00314                 if (fixup_spve_null(param, 1) != 0) {
00315                         LM_ERR("cannot fixup parameter %d\n", param_no);
00316                         return -1;
00317                 }
00318         }
00319         else if (param_no == 3) {
00320                 /* destination avp name */
00321                 if (avp_name_fixup(param) < 0) {
00322                         LM_ERR("cannot fixup parameter %d\n", param_no);
00323                         return -1;
00324                 }
00325         }
00326 
00327         return 0;
00328 }