cr_fixup.c

Go to the documentation of this file.
00001 /*
00002  * $Id: cr_fixup.c 5189 2008-11-12 15:53:01Z henningw $
00003  *
00004  * Copyright (C) 2007-2008 1&1 Internet AG
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 cr_fixup.c
00025  * \brief Fixup functions.
00026  * \ingroup carrierroute
00027  * - Module; \ref carrierroute
00028  */
00029 
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 
00040 /**
00041  * The fixup funcions will use the initial mapping.
00042  * If the mapping changes afterwards (eg. due to cr_reload_routes),
00043  * the names used in the routing script will not be mapped
00044  * to the correct IDs!
00045  * @param name carrier name
00046  * @return carrier id
00047  */
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 
00064 /**
00065  * The fixup funcions will use the initial mapping.
00066  * If the mapping changes afterwards (eg. due to cr_reload_routes),
00067  * the names used in the routing script will not be mapped
00068  * to the correct IDs!
00069  * @param name domain name
00070  * @return domain id 
00071  */
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 
00088 /**
00089  * Fixes the hash source to enum values
00090  *
00091  * @param my_hash_source the hash source as string
00092  *
00093  * @return the enum value on success, -1 on failure
00094  */
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 {
00107       return shs_error;
00108    }
00109 }
00110 
00111 
00112 /**
00113  * Fixes the module functions' parameters if it is a carrier.
00114  * supports name string and PVs.
00115  *
00116  * @param param the parameter
00117  *
00118  * @return 0 on success, -1 on failure
00119  */
00120 static int carrier_fixup(void ** param) {
00121    int id;
00122 
00123    if (fixup_spve_null(param, 1) !=0) {
00124       LM_ERR("could not fixup parameter");
00125       return -1;
00126    }
00127 
00128    if (((gparam_p)(*param))->type == GPARAM_TYPE_STR) {
00129       /* This is a name string, convert to a int */
00130       ((gparam_p)(*param))->type=GPARAM_TYPE_INT;
00131       /* get carrier id */
00132       if ((id = carrier_name_2_id(&((gparam_p)(*param))->v.sval)) < 0) {
00133          LM_ERR("could not find carrier name '%.*s' in map\n", ((gparam_p)(*param))->v.sval.len, ((gparam_p)(*param))->v.sval.s);
00134          pkg_free(*param);
00135          return -1;
00136       }
00137       ((gparam_p)(*param))->v.ival = id;
00138    }
00139    return 0;
00140 }
00141 
00142 
00143 /**
00144  * Fixes the module functions' parameters if it is a domain.
00145  * supports name string, and PVs.
00146  *
00147  * @param param the parameter
00148  *
00149  * @return 0 on success, -1 on failure
00150  */
00151 static int domain_fixup(void ** param) {
00152    int id;
00153 
00154    if (fixup_spve_null(param, 1) !=0) {
00155       LM_ERR("could not fixup parameter");
00156       return -1;
00157    }
00158 
00159    if (((gparam_p)(*param))->type == GPARAM_TYPE_STR) {
00160       /* This is a name string, convert to a int */
00161       ((gparam_p)(*param))->type=GPARAM_TYPE_INT;
00162       /* get domain id */
00163       if ((id = domain_name_2_id(&(((gparam_p)(*param))->v.sval))) < 0) {
00164          LM_ERR("could not find domain name '%.*s' in map\n", ((gparam_p)(*param))->v.sval.len, ((gparam_p)(*param))->v.sval.s);
00165          pkg_free(*param);
00166          return -1;
00167       }
00168       ((gparam_p)(*param))->v.ival = id;
00169    }
00170    return 0;
00171 }
00172 
00173 
00174 /**
00175  * Fixes the module functions' parameters in case of AVP names.
00176  *
00177  * @param param the parameter
00178  *
00179  * @return 0 on success, -1 on failure
00180  */
00181 static int avp_name_fixup(void ** param) {
00182 
00183    if (fixup_spve_null(param, 1) !=0) {
00184       LM_ERR("could not fixup parameter");
00185       return -1;
00186    }
00187    if (((gparam_p)(*param))->v.pve->spec.type == PVT_AVP &&
00188          ((gparam_p)(*param))->v.pve->spec.pvp.pvn.u.isname.name.s.len == 0 &&
00189          ((gparam_p)(*param))->v.pve->spec.pvp.pvn.u.isname.name.s.s == 0) {
00190       LM_ERR("malformed or non AVP type definition\n");
00191       return -1;
00192    }
00193    return 0;
00194 }
00195 
00196 
00197 /**
00198  * Fixes the module functions' parameters, i.e. it maps
00199  * the routing domain names to numbers for faster access
00200  * at runtime
00201  *
00202  * @param param the parameter
00203  * @param param_no the number of the parameter
00204  *
00205  * @return 0 on success, -1 on failure
00206  */
00207 int cr_route_fixup(void ** param, int param_no) {
00208    enum hash_source my_hash_source;
00209 
00210    if (param_no == 1) {
00211       /* carrier */
00212       if (carrier_fixup(param) < 0) {
00213          LM_ERR("cannot fixup parameter %d\n", param_no);
00214          return -1;
00215       }
00216    }
00217    else if (param_no == 2) {
00218       /* domain */
00219       if (domain_fixup(param) < 0) {
00220          LM_ERR("cannot fixup parameter %d\n", param_no);
00221          return -1;
00222       }
00223    }
00224    else if ((param_no == 3) || (param_no == 4)){
00225       /* prefix matching, rewrite user */
00226       if (fixup_spve_null(param, 1) != 0) {
00227          LM_ERR("cannot fixup parameter %d\n", param_no);
00228          return -1;
00229       }
00230    }
00231    else if (param_no == 5) {
00232       /* hash source */
00233       if ((my_hash_source = hash_fixup((char *)*param)) == shs_error) {
00234          LM_ERR("invalid hash source\n");
00235          return -1;
00236       }
00237       pkg_free(*param);
00238       *param = (void *)my_hash_source;
00239    }
00240    else if (param_no == 6) {
00241       /* destination avp name */
00242       if (avp_name_fixup(param) < 0) {
00243          LM_ERR("cannot fixup parameter %d\n", param_no);
00244          return -1;
00245       }
00246    }
00247 
00248    return 0;
00249 }
00250 
00251 
00252 /**
00253  * fixes the module functions' parameters, i.e. it maps
00254  * the routing domain names to numbers for faster access
00255  * at runtime
00256  *
00257  * @param param the parameter
00258  * @param param_no the number of the parameter
00259  *
00260  * @return 0 on success, -1 on failure
00261  */
00262 int cr_load_next_domain_fixup(void ** param, int param_no) {
00263    if (param_no == 1) {
00264       /* carrier */
00265       if (carrier_fixup(param) < 0) {
00266          LM_ERR("cannot fixup parameter %d\n", param_no);
00267          return -1;
00268       }
00269    }
00270    else if (param_no == 2) {
00271       /* domain */
00272       if (domain_fixup(param) < 0) {
00273          LM_ERR("cannot fixup parameter %d\n", param_no);
00274          return -1;
00275       }
00276    }
00277    else if ((param_no == 3) || (param_no == 4) || (param_no == 5)) {
00278       /* prefix matching, host, reply code */
00279       if (fixup_spve_null(param, 1) != 0) {
00280          LM_ERR("cannot fixup parameter %d\n", param_no);
00281          return -1;
00282       }
00283    }
00284    else if (param_no == 6) {
00285       /* destination avp name */
00286       if (avp_name_fixup(param) < 0) {
00287          LM_ERR("cannot fixup parameter %d\n", param_no);
00288          return -1;
00289       }
00290    }
00291 
00292    return 0;
00293 }
00294 
00295 
00296 /**
00297  * Fixes the module functions' parameters.
00298  *
00299  * @param param the parameter
00300  * @param param_no the number of the parameter
00301  *
00302  * @return 0 on success, -1 on failure
00303  */
00304 int cr_load_user_carrier_fixup(void ** param, int param_no) {
00305    if (mode == CARRIERROUTE_MODE_FILE) {
00306       LM_ERR("command cr_user_rewrite_uri can't be used in file mode\n");
00307       return -1;
00308    }
00309 
00310    if ((param_no == 1) || (param_no == 2)) {
00311       /* user, domain */
00312       if (fixup_spve_null(param, 1) != 0) {
00313          LM_ERR("cannot fixup parameter %d\n", param_no);
00314          return -1;
00315       }
00316    }
00317    else if (param_no == 3) {
00318       /* destination avp name */
00319       if (avp_name_fixup(param) < 0) {
00320          LM_ERR("cannot fixup parameter %d\n", param_no);
00321          return -1;
00322       }
00323    }
00324 
00325    return 0;
00326 }

Generated on Mon May 21 18:00:25 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6