opensips/modules/lcr/lcr_mod.c

Wed, 10 Feb 2010 21:14:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 10 Feb 2010 21:14:04 +0100
changeset 16
c5c55937e44c
child 17
733187d496d0
permissions
-rw-r--r--

Import unmodified vendor sources for correction and improvement.

michael@16 1 /*
michael@16 2 * $Id: lcr_mod.c 6015 2009-08-22 21:45:06Z bogdan_iancu $
michael@16 3 *
michael@16 4 * Least Cost Routing module (also implements sequential forking)
michael@16 5 *
michael@16 6 * Copyright (C) 2005 Juha Heinanen
michael@16 7 * Copyright (C) 2006 Voice Sistem SRL
michael@16 8 *
michael@16 9 * This file is part of opensips, a free SIP server.
michael@16 10 *
michael@16 11 * opensips is free software; you can redistribute it and/or modify
michael@16 12 * it under the terms of the GNU General Public License as published by
michael@16 13 * the Free Software Foundation; either version 2 of the License, or
michael@16 14 * (at your option) any later version
michael@16 15 *
michael@16 16 * opensips is distributed in the hope that it will be useful,
michael@16 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
michael@16 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
michael@16 19 * GNU General Public License for more details.
michael@16 20 *
michael@16 21 * You should have received a copy of the GNU General Public License
michael@16 22 * along with this program; if not, write to the Free Software
michael@16 23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
michael@16 24 *
michael@16 25 * History:
michael@16 26 * -------
michael@16 27 * 2005-02-14: Introduced lcr module (jh)
michael@16 28 * 2005-02-20: Added sequential forking functions (jh)
michael@16 29 * 2005-02-25: Added support for int AVP names, combined addr and port
michael@16 30 * AVPs (jh)
michael@16 31 * 2005-07-28: Added support for gw URI scheme and transport,
michael@16 32 * backport from ser (kd)
michael@16 33 * 2005-08-20: Added support for gw prefixes (jh)
michael@16 34 * 2005-09-03: Request-URI user part can be modified between load_gws()
michael@16 35 * and first next_gw() calls.
michael@16 36 */
michael@16 37
michael@16 38 #include <stdio.h>
michael@16 39 #include <stdlib.h>
michael@16 40 #include <string.h>
michael@16 41 #include <sys/types.h>
michael@16 42 #include <sys/socket.h>
michael@16 43 #include <netinet/in.h>
michael@16 44 #include <arpa/inet.h>
michael@16 45 #include <regex.h>
michael@16 46 #include "../../sr_module.h"
michael@16 47 #include "../../dprint.h"
michael@16 48 #include "../../ut.h"
michael@16 49 #include "../../error.h"
michael@16 50 #include "../../mem/mem.h"
michael@16 51 #include "../../mem/shm_mem.h"
michael@16 52 #include "../../db/db.h"
michael@16 53 #include "../../usr_avp.h"
michael@16 54 #include "../../parser/parse_uri.h"
michael@16 55 #include "../../parser/parse_from.h"
michael@16 56 #include "../../parser/msg_parser.h"
michael@16 57 #include "../../action.h"
michael@16 58 #include "../../qvalue.h"
michael@16 59 #include "../../dset.h"
michael@16 60 #include "../../ip_addr.h"
michael@16 61 #include "../../resolve.h"
michael@16 62 #include "../../mi/mi.h"
michael@16 63 #include "../../mod_fix.h"
michael@16 64 #include "../../socket_info.h"
michael@16 65 #include "../../pvar.h"
michael@16 66 #include "../../mod_fix.h"
michael@16 67 #include "mi.h"
michael@16 68
michael@16 69
michael@16 70
michael@16 71 /*
michael@16 72 * Version of gw and lcr tables required by the module,
michael@16 73 * increment this value if you change the table in
michael@16 74 * an backwards incompatible way
michael@16 75 */
michael@16 76 #define GW_TABLE_VERSION 8
michael@16 77 #define LCR_TABLE_VERSION 3
michael@16 78
michael@16 79 /* usr_avp flag for sequential forking */
michael@16 80 #define Q_FLAG (1<<2)
michael@16 81
michael@16 82 static void destroy(void); /* Module destroy function */
michael@16 83 static int mi_child_init(void);
michael@16 84 static int mod_init(void); /* Module initialization function */
michael@16 85 static int fixstringloadgws(void **param, int param_count);
michael@16 86
michael@16 87 int reload_gws ( void );
michael@16 88
michael@16 89 #define GW_TABLE "gw"
michael@16 90
michael@16 91 #define GW_NAME_COL "gw_name"
michael@16 92
michael@16 93 #define GRP_ID_COL "grp_id"
michael@16 94
michael@16 95 #define IP_ADDR_COL "ip_addr"
michael@16 96
michael@16 97 #define PORT_COL "port"
michael@16 98
michael@16 99 #define URI_SCHEME_COL "uri_scheme"
michael@16 100
michael@16 101 #define TRANSPORT_COL "transport"
michael@16 102
michael@16 103 #define STRIP_COL "strip"
michael@16 104
michael@16 105 #define TAG_COL "tag"
michael@16 106
michael@16 107 #define FLAGS_COL "flags"
michael@16 108
michael@16 109 #define LCR_TABLE "lcr"
michael@16 110
michael@16 111 #define PREFIX_COL "prefix"
michael@16 112
michael@16 113 #define FROM_URI_COL "from_uri"
michael@16 114
michael@16 115 #define PRIORITY_COL "priority"
michael@16 116
michael@16 117 #define MAX_NO_OF_GWS 32
michael@16 118 #define MAX_NO_OF_LCRS 256
michael@16 119 #define MAX_PREFIX_LEN 256
michael@16 120 #define MAX_TAG_LEN 16
michael@16 121 #define MAX_FROM_URI_LEN 256
michael@16 122
michael@16 123 /* Default module parameter values */
michael@16 124 #define DEF_FR_INV_TIMER 90
michael@16 125 #define DEF_FR_INV_TIMER_NEXT 30
michael@16 126 #define DEF_PREFIX_MODE 0
michael@16 127
michael@16 128 /*
michael@16 129 * Type definitions
michael@16 130 */
michael@16 131
michael@16 132 typedef enum sip_protos uri_transport;
michael@16 133
michael@16 134 struct gw_info {
michael@16 135 unsigned int ip_addr;
michael@16 136 unsigned int port;
michael@16 137 unsigned int grp_id;
michael@16 138 uri_type scheme;
michael@16 139 uri_transport transport;
michael@16 140 unsigned int strip;
michael@16 141 char tag[MAX_TAG_LEN + 1];
michael@16 142 unsigned short tag_len;
michael@16 143 unsigned int flags;
michael@16 144 };
michael@16 145
michael@16 146 struct lcr_info {
michael@16 147 char prefix[MAX_PREFIX_LEN + 1];
michael@16 148 unsigned short prefix_len;
michael@16 149 char from_uri[MAX_FROM_URI_LEN + 1];
michael@16 150 unsigned short from_uri_len;
michael@16 151 unsigned int grp_id;
michael@16 152 unsigned short priority;
michael@16 153 unsigned short end_record;
michael@16 154 };
michael@16 155
michael@16 156 struct prefix_regex {
michael@16 157 regex_t re;
michael@16 158 short int valid;
michael@16 159 };
michael@16 160
michael@16 161 struct from_uri_regex {
michael@16 162 regex_t re;
michael@16 163 short int valid;
michael@16 164 };
michael@16 165
michael@16 166 struct mi {
michael@16 167 int gw_index;
michael@16 168 int route_index;
michael@16 169 int randomizer;
michael@16 170 };
michael@16 171
michael@16 172
michael@16 173 /*
michael@16 174 * Database variables
michael@16 175 */
michael@16 176 static db_con_t* db_handle = 0; /* Database connection handle */
michael@16 177 static db_func_t lcr_dbf;
michael@16 178
michael@16 179 /*
michael@16 180 * Module parameter variables
michael@16 181 */
michael@16 182
michael@16 183 /* database */
michael@16 184 static str db_url = str_init(DEFAULT_RODB_URL);
michael@16 185 static str gw_table = str_init(GW_TABLE);
michael@16 186 static str gw_name_col = str_init(GW_NAME_COL);
michael@16 187 static str grp_id_col = str_init(GRP_ID_COL);
michael@16 188 static str ip_addr_col = str_init(IP_ADDR_COL);
michael@16 189 static str port_col = str_init(PORT_COL);
michael@16 190 static str uri_scheme_col = str_init(URI_SCHEME_COL);
michael@16 191 static str transport_col = str_init(TRANSPORT_COL);
michael@16 192 static str strip_col = str_init(STRIP_COL);
michael@16 193 static str tag_col = str_init(TAG_COL);
michael@16 194 static str flags_col = str_init(FLAGS_COL);
michael@16 195 static str lcr_table = str_init(LCR_TABLE);
michael@16 196 static str prefix_col = str_init(PREFIX_COL);
michael@16 197 static str from_uri_col = str_init(FROM_URI_COL);
michael@16 198 static str priority_col = str_init(PRIORITY_COL);
michael@16 199
michael@16 200 /* timer */
michael@16 201 int fr_inv_timer = DEF_FR_INV_TIMER;
michael@16 202 int fr_inv_timer_next = DEF_FR_INV_TIMER_NEXT;
michael@16 203
michael@16 204 /* avps */
michael@16 205 static char *fr_inv_timer_avp_param = NULL;
michael@16 206 static char *gw_uri_avp_param = NULL;
michael@16 207 static char *ruri_user_avp_param = NULL;
michael@16 208 static char *contact_avp_param = NULL;
michael@16 209 static char *rpid_avp_param = NULL;
michael@16 210 static char *flags_avp_param = NULL;
michael@16 211
michael@16 212 /* prefix mode */
michael@16 213 int prefix_mode_param = DEF_PREFIX_MODE;
michael@16 214
michael@16 215 /*
michael@16 216 * Other module types and variables
michael@16 217 */
michael@16 218
michael@16 219 struct contact {
michael@16 220 str uri;
michael@16 221 qvalue_t q;
michael@16 222 str dst_uri;
michael@16 223 str path;
michael@16 224 unsigned int flags;
michael@16 225 struct socket_info* sock;
michael@16 226 unsigned short q_flag;
michael@16 227 struct contact *next;
michael@16 228 };
michael@16 229
michael@16 230 static int fr_inv_timer_avp_type;
michael@16 231 static int_str fr_inv_timer_avp;
michael@16 232 static int gw_uri_avp_type;
michael@16 233 static int_str gw_uri_avp;
michael@16 234 static int ruri_user_avp_type;
michael@16 235 static int_str ruri_user_avp;
michael@16 236 static int contact_avp_type;
michael@16 237 static int_str contact_avp;
michael@16 238 static int rpid_avp_type;
michael@16 239 static int_str rpid_avp;
michael@16 240 static int flags_avp_type;
michael@16 241 static int_str flags_avp;
michael@16 242
michael@16 243 struct gw_info **gws; /* Pointer to current gw table pointer */
michael@16 244 struct gw_info *gws_1; /* Pointer to gw table 1 */
michael@16 245 struct gw_info *gws_2; /* Pointer to gw table 2 */
michael@16 246
michael@16 247 struct lcr_info **lcrs; /* Pointer to current lcr table pointer */
michael@16 248 struct lcr_info *lcrs_1; /* Pointer to lcr table 1 */
michael@16 249 struct lcr_info *lcrs_2; /* Pointer to lcr table 2 */
michael@16 250
michael@16 251 unsigned int *lcrs_ws_reload_counter;
michael@16 252 unsigned int reload_counter;
michael@16 253
michael@16 254 struct prefix_regex prefix_reg[MAX_NO_OF_LCRS];
michael@16 255 struct from_uri_regex from_uri_reg[MAX_NO_OF_LCRS];
michael@16 256
michael@16 257 /*
michael@16 258 * Module functions that are defined later
michael@16 259 */
michael@16 260 static int load_gws_0(struct sip_msg* _m, char* _s1, char* _s2);
michael@16 261 static int load_gws_1(struct sip_msg* _m, char* _s1, char* _s2);
michael@16 262 static int load_gws_from_grp(struct sip_msg* _m, char* _s1, char* _s2);
michael@16 263 static int next_gw(struct sip_msg* _m, char* _s1, char* _s2);
michael@16 264 static int from_gw_0(struct sip_msg* _m, char* _s1, char* _s2);
michael@16 265 static int from_gw_1(struct sip_msg* _m, char* _s1, char* _s2);
michael@16 266 static int from_gw_grp(struct sip_msg* _m, char* _s1, char* _s2);
michael@16 267 static int to_gw(struct sip_msg* _m, char* _s1, char* _s2);
michael@16 268 static int to_gw_grp(struct sip_msg* _m, char* _s1, char* _s2);
michael@16 269 static int load_contacts (struct sip_msg*, char*, char*);
michael@16 270 static int next_contacts (struct sip_msg*, char*, char*);
michael@16 271
michael@16 272
michael@16 273 /*
michael@16 274 * Exported functions
michael@16 275 */
michael@16 276 static cmd_export_t cmds[] = {
michael@16 277 {"load_gws", (cmd_function)load_gws_0, 0, 0, 0, REQUEST_ROUTE},
michael@16 278 {"load_gws", (cmd_function)load_gws_1, 1, fixup_pvar_null,
michael@16 279 fixup_free_pvar_null, REQUEST_ROUTE},
michael@16 280 {"load_gws_from_grp", (cmd_function)load_gws_from_grp, 1,
michael@16 281 fixstringloadgws, 0, REQUEST_ROUTE},
michael@16 282 {"next_gw", (cmd_function)next_gw, 0, 0, 0,
michael@16 283 REQUEST_ROUTE | FAILURE_ROUTE},
michael@16 284 {"from_gw", (cmd_function)from_gw_0, 0, 0, 0,
michael@16 285 REQUEST_ROUTE | FAILURE_ROUTE | ONREPLY_ROUTE},
michael@16 286 {"from_gw", (cmd_function)from_gw_1, 1, fixup_pvar_null,
michael@16 287 fixup_free_pvar_null, REQUEST_ROUTE | FAILURE_ROUTE | ONREPLY_ROUTE},
michael@16 288 {"from_gw_grp", (cmd_function)from_gw_grp, 1, fixup_uint_null, 0,
michael@16 289 REQUEST_ROUTE | FAILURE_ROUTE | ONREPLY_ROUTE},
michael@16 290 {"to_gw", (cmd_function)to_gw, 0, 0, 0,
michael@16 291 REQUEST_ROUTE | FAILURE_ROUTE},
michael@16 292 {"to_gw", (cmd_function)to_gw_grp, 1, fixup_uint_null, 0,
michael@16 293 REQUEST_ROUTE | FAILURE_ROUTE},
michael@16 294 {"load_contacts", (cmd_function)load_contacts, 0, 0, 0,
michael@16 295 REQUEST_ROUTE},
michael@16 296 {"next_contacts", (cmd_function)next_contacts, 0, 0, 0,
michael@16 297 REQUEST_ROUTE | FAILURE_ROUTE},
michael@16 298 {0, 0, 0, 0, 0, 0}
michael@16 299 };
michael@16 300
michael@16 301
michael@16 302 /*
michael@16 303 * Exported parameters
michael@16 304 */
michael@16 305 static param_export_t params[] = {
michael@16 306 {"db_url", STR_PARAM, &db_url.s },
michael@16 307 {"gw_table", STR_PARAM, &gw_table.s },
michael@16 308 {"gw_name_column", STR_PARAM, &gw_name_col.s },
michael@16 309 {"grp_id_column", STR_PARAM, &grp_id_col.s },
michael@16 310 {"ip_addr_column", STR_PARAM, &ip_addr_col.s },
michael@16 311 {"port_column", STR_PARAM, &port_col.s },
michael@16 312 {"uri_scheme_column", STR_PARAM, &uri_scheme_col.s },
michael@16 313 {"transport_column", STR_PARAM, &transport_col.s },
michael@16 314 {"strip_column", STR_PARAM, &strip_col.s },
michael@16 315 {"tag_column", STR_PARAM, &tag_col.s },
michael@16 316 {"flags_column", STR_PARAM, &flags_col.s },
michael@16 317 {"lcr_table", STR_PARAM, &lcr_table.s },
michael@16 318 {"prefix_column", STR_PARAM, &prefix_col.s },
michael@16 319 {"from_uri_column", STR_PARAM, &from_uri_col.s },
michael@16 320 {"priority_column", STR_PARAM, &priority_col.s },
michael@16 321 {"fr_inv_timer_avp", STR_PARAM, &fr_inv_timer_avp_param },
michael@16 322 {"gw_uri_avp", STR_PARAM, &gw_uri_avp_param },
michael@16 323 {"ruri_user_avp", STR_PARAM, &ruri_user_avp_param },
michael@16 324 {"contact_avp", STR_PARAM, &contact_avp_param },
michael@16 325 {"rpid_avp", STR_PARAM, &rpid_avp_param },
michael@16 326 {"flags_avp", STR_PARAM, &flags_avp_param },
michael@16 327 {"fr_inv_timer", INT_PARAM, &fr_inv_timer },
michael@16 328 {"fr_inv_timer_next", INT_PARAM, &fr_inv_timer_next },
michael@16 329 {"prefix_mode", INT_PARAM, &prefix_mode_param },
michael@16 330 {0, 0, 0}
michael@16 331 };
michael@16 332
michael@16 333
michael@16 334 /*
michael@16 335 * Exported MI functions
michael@16 336 */
michael@16 337 static mi_export_t mi_cmds[] = {
michael@16 338 { MI_LCR_RELOAD, mi_lcr_reload, MI_NO_INPUT_FLAG, 0, mi_child_init },
michael@16 339 { MI_LCR_DUMP, mi_lcr_dump, MI_NO_INPUT_FLAG, 0, 0 },
michael@16 340 { 0, 0, 0, 0 ,0}
michael@16 341 };
michael@16 342
michael@16 343
michael@16 344 /*
michael@16 345 * Module interface
michael@16 346 */
michael@16 347 struct module_exports exports = {
michael@16 348 "lcr",
michael@16 349 MODULE_VERSION,
michael@16 350 DEFAULT_DLFLAGS, /* dlopen flags */
michael@16 351 cmds, /* Exported functions */
michael@16 352 params, /* Exported parameters */
michael@16 353 0, /* exported statistics */
michael@16 354 mi_cmds, /* exported MI functions */
michael@16 355 0, /* exported pseudo-variables */
michael@16 356 0, /* extra processes */
michael@16 357 mod_init, /* module initialization function */
michael@16 358 0, /* response function */
michael@16 359 destroy, /* destroy function */
michael@16 360 0 /* child initialization function */
michael@16 361 };
michael@16 362
michael@16 363
michael@16 364 static int lcr_db_init(const str* db_url)
michael@16 365 {
michael@16 366 if (lcr_dbf.init==0){
michael@16 367 LM_CRIT("Null lcr_dbf\n");
michael@16 368 goto error;
michael@16 369 }
michael@16 370 db_handle=lcr_dbf.init(db_url);
michael@16 371 if (db_handle==0){
michael@16 372 LM_ERR("Unable to connect to the database\n");
michael@16 373 goto error;
michael@16 374 }
michael@16 375 return 0;
michael@16 376 error:
michael@16 377 return -1;
michael@16 378 }
michael@16 379
michael@16 380
michael@16 381
michael@16 382 static int lcr_db_bind(const str* db_url)
michael@16 383 {
michael@16 384 if (db_bind_mod(db_url, &lcr_dbf)<0){
michael@16 385 LM_ERR("Unable to bind to the database module\n");
michael@16 386 return -1;
michael@16 387 }
michael@16 388
michael@16 389 if (!DB_CAPABILITY(lcr_dbf, DB_CAP_QUERY)) {
michael@16 390 LM_ERR("Database module does not implement 'query' function\n");
michael@16 391 return -1;
michael@16 392 }
michael@16 393
michael@16 394 return 0;
michael@16 395 }
michael@16 396
michael@16 397
michael@16 398 static void lcr_db_close(void)
michael@16 399 {
michael@16 400 if (db_handle && lcr_dbf.close){
michael@16 401 lcr_dbf.close(db_handle);
michael@16 402 db_handle=0;
michael@16 403 }
michael@16 404 }
michael@16 405
michael@16 406
michael@16 407 static int mi_child_init(void)
michael@16 408 {
michael@16 409 return lcr_db_init(&db_url);
michael@16 410 }
michael@16 411
michael@16 412
michael@16 413 /*
michael@16 414 * Module initialization function that is called before the main process forks
michael@16 415 */
michael@16 416 static int mod_init(void)
michael@16 417 {
michael@16 418 int i;
michael@16 419 pv_spec_t avp_spec;
michael@16 420 str s;
michael@16 421 unsigned short avp_flags;
michael@16 422
michael@16 423 LM_DBG("Initializing\n");
michael@16 424
michael@16 425 /* Update length of module variables */
michael@16 426 db_url.len = strlen(db_url.s);
michael@16 427 gw_table.len = strlen(gw_table.s);
michael@16 428 gw_name_col.len = strlen(gw_name_col.s);
michael@16 429 grp_id_col.len = strlen(grp_id_col.s);
michael@16 430 ip_addr_col.len = strlen(ip_addr_col.s);
michael@16 431 port_col.len = strlen(port_col.s);
michael@16 432 uri_scheme_col.len = strlen(uri_scheme_col.s);
michael@16 433 transport_col.len = strlen(transport_col.s);
michael@16 434 strip_col.len = strlen(strip_col.s);
michael@16 435 tag_col.len = strlen(tag_col.s);
michael@16 436 flags_col.len = strlen(flags_col.s);
michael@16 437 lcr_table.len = strlen(lcr_table.s);
michael@16 438 prefix_col.len = strlen(prefix_col.s);
michael@16 439 from_uri_col.len = strlen(from_uri_col.s);
michael@16 440 priority_col.len = strlen(priority_col.s);
michael@16 441
michael@16 442 /* Bind database */
michael@16 443 if (lcr_db_bind(&db_url)) {
michael@16 444 LM_ERR("No database module found\n");
michael@16 445 return -1;
michael@16 446 }
michael@16 447
michael@16 448 /* Check value of prefix_mode */
michael@16 449 if ((prefix_mode_param != 0) && (prefix_mode_param != 1)) {
michael@16 450 LM_ERR("Invalid prefix_mode value <%d>\n", prefix_mode_param);
michael@16 451 return -1;
michael@16 452 }
michael@16 453
michael@16 454 /* Process AVP params */
michael@16 455 if (fr_inv_timer_avp_param && *fr_inv_timer_avp_param) {
michael@16 456 s.s = fr_inv_timer_avp_param; s.len = strlen(s.s);
michael@16 457 if (pv_parse_spec(&s, &avp_spec)==0
michael@16 458 || avp_spec.type!=PVT_AVP) {
michael@16 459 LM_ERR("Malformed or non AVP definition <%s>\n",
michael@16 460 fr_inv_timer_avp_param);
michael@16 461 return -1;
michael@16 462 }
michael@16 463
michael@16 464 if(pv_get_avp_name(0, &(avp_spec.pvp), &fr_inv_timer_avp, &avp_flags)!=0) {
michael@16 465 LM_ERR("Invalid AVP definition <%s>\n", fr_inv_timer_avp_param);
michael@16 466 return -1;
michael@16 467 }
michael@16 468 fr_inv_timer_avp_type = avp_flags;
michael@16 469 } else {
michael@16 470 LM_ERR("AVP fr_inv_timer_avp has not been defined\n");
michael@16 471 return -1;
michael@16 472 }
michael@16 473
michael@16 474 if (gw_uri_avp_param && *gw_uri_avp_param) {
michael@16 475 s.s = gw_uri_avp_param; s.len = strlen(s.s);
michael@16 476 if (pv_parse_spec(&s, &avp_spec)==0
michael@16 477 || avp_spec.type!=PVT_AVP) {
michael@16 478 LM_ERR("Malformed or non AVP definition <%s>\n", gw_uri_avp_param);
michael@16 479 return -1;
michael@16 480 }
michael@16 481
michael@16 482 if(pv_get_avp_name(0, &(avp_spec.pvp), &gw_uri_avp, &avp_flags)!=0) {
michael@16 483 LM_ERR("Invalid AVP definition <%s>\n", gw_uri_avp_param);
michael@16 484 return -1;
michael@16 485 }
michael@16 486 gw_uri_avp_type = avp_flags;
michael@16 487 } else {
michael@16 488 LM_ERR("AVP gw_uri_avp has not been defined\n");
michael@16 489 return -1;
michael@16 490 }
michael@16 491
michael@16 492 if (ruri_user_avp_param && *ruri_user_avp_param) {
michael@16 493 s.s = ruri_user_avp_param; s.len = strlen(s.s);
michael@16 494 if (pv_parse_spec(&s, &avp_spec)==0
michael@16 495 || avp_spec.type!=PVT_AVP) {
michael@16 496 LM_ERR("Malformed or non AVP definition <%s>\n",
michael@16 497 ruri_user_avp_param);
michael@16 498 return -1;
michael@16 499 }
michael@16 500
michael@16 501 if(pv_get_avp_name(0, &(avp_spec.pvp), &ruri_user_avp, &avp_flags)!=0) {
michael@16 502 LM_ERR("Invalid AVP definition <%s>\n", ruri_user_avp_param);
michael@16 503 return -1;
michael@16 504 }
michael@16 505 ruri_user_avp_type = avp_flags;
michael@16 506 } else {
michael@16 507 LM_ERR("AVP ruri_user_avp has not been defined\n");
michael@16 508 return -1;
michael@16 509 }
michael@16 510
michael@16 511 if (contact_avp_param && *contact_avp_param) {
michael@16 512 s.s = contact_avp_param; s.len = strlen(s.s);
michael@16 513 if (pv_parse_spec(&s, &avp_spec)==0
michael@16 514 || avp_spec.type!=PVT_AVP) {
michael@16 515 LM_ERR("Malformed or non AVP definition <%s>\n",
michael@16 516 contact_avp_param);
michael@16 517 return -1;
michael@16 518 }
michael@16 519
michael@16 520 if(pv_get_avp_name(0, &(avp_spec.pvp), &contact_avp, &avp_flags)!=0) {
michael@16 521 LM_ERR("Invalid AVP definition <%s>\n", contact_avp_param);
michael@16 522 return -1;
michael@16 523 }
michael@16 524 contact_avp_type = avp_flags;
michael@16 525 } else {
michael@16 526 LM_ERR("AVP contact_avp has not been defined\n");
michael@16 527 return -1;
michael@16 528 }
michael@16 529
michael@16 530 if (rpid_avp_param && *rpid_avp_param) {
michael@16 531 s.s = rpid_avp_param; s.len = strlen(s.s);
michael@16 532 if (pv_parse_spec(&s, &avp_spec)==0
michael@16 533 || avp_spec.type!=PVT_AVP) {
michael@16 534 LM_ERR("Malformed or non AVP definition <%s>\n", rpid_avp_param);
michael@16 535 return -1;
michael@16 536 }
michael@16 537
michael@16 538 if(pv_get_avp_name(0, &(avp_spec.pvp), &rpid_avp, &avp_flags)!=0) {
michael@16 539 LM_ERR("Invalid AVP definition <%s>\n", rpid_avp_param);
michael@16 540 return -1;
michael@16 541 }
michael@16 542 rpid_avp_type = avp_flags;
michael@16 543 } else {
michael@16 544 LM_ERR("AVP rpid_avp has not been defined\n");
michael@16 545 return -1;
michael@16 546 }
michael@16 547
michael@16 548 if (flags_avp_param && *flags_avp_param) {
michael@16 549 s.s = flags_avp_param; s.len = strlen(s.s);
michael@16 550 if (pv_parse_spec(&s, &avp_spec)==0
michael@16 551 || avp_spec.type!=PVT_AVP) {
michael@16 552 LM_ERR("Malformed or non AVP definition <%s>\n", flags_avp_param);
michael@16 553 return -1;
michael@16 554 }
michael@16 555
michael@16 556 if(pv_get_avp_name(0, &(avp_spec.pvp), &flags_avp, &avp_flags)!=0) {
michael@16 557 LM_ERR("Invalid AVP definition <%s>\n", flags_avp_param);
michael@16 558 return -1;
michael@16 559 }
michael@16 560 flags_avp_type = avp_flags;
michael@16 561 } else {
michael@16 562 LM_ERR("AVP flags_avp has not been defined\n");
michael@16 563 return -1;
michael@16 564 }
michael@16 565
michael@16 566 /* Check table version */
michael@16 567 db_con_t* dbh;
michael@16 568 if (lcr_dbf.init==0){
michael@16 569 LM_CRIT("Unbound database\n");
michael@16 570 return -1;
michael@16 571 }
michael@16 572 dbh=lcr_dbf.init(&db_url);
michael@16 573 if (dbh==0){
michael@16 574 LM_ERR("Unable to open database connection\n");
michael@16 575 return -1;
michael@16 576 }
michael@16 577 if((db_check_table_version(&lcr_dbf, dbh, &gw_table, GW_TABLE_VERSION) < 0) ||
michael@16 578 (db_check_table_version(&lcr_dbf, dbh, &lcr_table, LCR_TABLE_VERSION) < 0)) {
michael@16 579 LM_ERR("error during table version check.\n");
michael@16 580 lcr_dbf.close(dbh);
michael@16 581 goto err;
michael@16 582 }
michael@16 583 lcr_dbf.close(dbh);
michael@16 584
michael@16 585 /* Initializing gw tables and gw table pointer variable */
michael@16 586 gws_1 = (struct gw_info *)shm_malloc(sizeof(struct gw_info) *
michael@16 587 (MAX_NO_OF_GWS + 1));
michael@16 588 if (gws_1 == 0) {
michael@16 589 LM_ERR("No memory for gw table\n");
michael@16 590 goto err;
michael@16 591 }
michael@16 592 gws_2 = (struct gw_info *)shm_malloc(sizeof(struct gw_info) *
michael@16 593 (MAX_NO_OF_GWS + 1));
michael@16 594 if (gws_2 == 0) {
michael@16 595 LM_ERR("No memory for gw table\n");
michael@16 596 goto err;
michael@16 597 }
michael@16 598 for (i = 0; i < MAX_NO_OF_GWS + 1; i++) {
michael@16 599 gws_1[i].ip_addr = gws_2[i].ip_addr = 0;
michael@16 600 }
michael@16 601 gws = (struct gw_info **)shm_malloc(sizeof(struct gw_info *));
michael@16 602 if (gws == 0) {
michael@16 603 LM_ERR("No memory for gw table pointer\n");
michael@16 604 }
michael@16 605 *gws = gws_1;
michael@16 606
michael@16 607 /* Initializing lcr tables and lcr table pointer variable */
michael@16 608 lcrs_1 = (struct lcr_info *)shm_malloc(sizeof(struct lcr_info) *
michael@16 609 (MAX_NO_OF_LCRS + 1));
michael@16 610 if (lcrs_1 == 0) {
michael@16 611 LM_ERR("No memory for lcr table\n");
michael@16 612 goto err;
michael@16 613 }
michael@16 614 lcrs_2 = (struct lcr_info *)shm_malloc(sizeof(struct lcr_info) *
michael@16 615 (MAX_NO_OF_LCRS + 1));
michael@16 616 if (lcrs_2 == 0) {
michael@16 617 LM_ERR("No memory for lcr table\n");
michael@16 618 goto err;
michael@16 619 }
michael@16 620 for (i = 0; i < MAX_NO_OF_LCRS + 1; i++) {
michael@16 621 lcrs_1[i].end_record = lcrs_2[i].end_record = 0;
michael@16 622 }
michael@16 623 lcrs = (struct lcr_info **)shm_malloc(sizeof(struct lcr_info *));
michael@16 624 if (lcrs == 0) {
michael@16 625 LM_ERR("No memory for lcr table pointer\n");
michael@16 626 goto err;
michael@16 627 }
michael@16 628 *lcrs = lcrs_1;
michael@16 629
michael@16 630 lcrs_ws_reload_counter = (unsigned int *)shm_malloc(sizeof(unsigned int));
michael@16 631 if (lcrs_ws_reload_counter == 0) {
michael@16 632 LM_ERR("No memory for reload counter\n");
michael@16 633 goto err;
michael@16 634 }
michael@16 635 *lcrs_ws_reload_counter = reload_counter = 0;
michael@16 636
michael@16 637 memset(prefix_reg, 0, sizeof(struct prefix_regex) * MAX_NO_OF_LCRS);
michael@16 638 memset(from_uri_reg, 0, sizeof(struct from_uri_regex) * MAX_NO_OF_LCRS);
michael@16 639
michael@16 640 /* First reload */
michael@16 641 if (reload_gws() == -1) {
michael@16 642 LM_CRIT("Failed to reload gateways and routes\n");
michael@16 643 goto err;
michael@16 644 }
michael@16 645
michael@16 646 return 0;
michael@16 647
michael@16 648 err:
michael@16 649 return -1;
michael@16 650 }
michael@16 651
michael@16 652
michael@16 653 static void destroy(void)
michael@16 654 {
michael@16 655 lcr_db_close();
michael@16 656 }
michael@16 657
michael@16 658 /*
michael@16 659 * Sort lcr records by prefix_len and priority.
michael@16 660 */
michael@16 661 static int comp_lcrs(const void *m1, const void *m2)
michael@16 662 {
michael@16 663 int result = -1;
michael@16 664
michael@16 665 struct mi *mi1 = (struct mi *) m1;
michael@16 666 struct mi *mi2 = (struct mi *) m2;
michael@16 667
michael@16 668 struct lcr_info lcr_record1 = (*lcrs)[mi1->route_index];
michael@16 669 struct lcr_info lcr_record2 = (*lcrs)[mi2->route_index];
michael@16 670
michael@16 671 if (prefix_mode_param == 0) {
michael@16 672 /* Sort by prefix. */
michael@16 673 if (lcr_record1.prefix_len > lcr_record2.prefix_len) {
michael@16 674 result = 1;
michael@16 675 } else if (lcr_record1.prefix_len == lcr_record2.prefix_len) {
michael@16 676 /* Sort by priority. */
michael@16 677 if (lcr_record1.priority < lcr_record2.priority) {
michael@16 678 result = 1;
michael@16 679 } else if (lcr_record1.priority == lcr_record2.priority) {
michael@16 680 /* Nothing to do. */
michael@16 681 result = 0;
michael@16 682 }
michael@16 683 }
michael@16 684 } else {
michael@16 685 if (lcr_record1.priority < lcr_record2.priority) {
michael@16 686 result = 1;
michael@16 687 } else if (lcr_record1.priority == lcr_record2.priority) {
michael@16 688 /* Nothing to do. */
michael@16 689 result = 0;
michael@16 690 }
michael@16 691 }
michael@16 692
michael@16 693 return result;
michael@16 694 }
michael@16 695
michael@16 696 /*
michael@16 697 * Sort lcr records by rand table.
michael@16 698 */
michael@16 699 static int rand_lcrs(const void *m1, const void *m2)
michael@16 700 {
michael@16 701 int result = -1;
michael@16 702
michael@16 703 struct mi mi1 = *((struct mi *) m1);
michael@16 704 struct mi mi2 = *((struct mi *) m2);
michael@16 705
michael@16 706 if (mi1.randomizer > mi2.randomizer) {
michael@16 707 result = 1;
michael@16 708 } else if (mi1.randomizer == mi2.randomizer) {
michael@16 709 result = 0;
michael@16 710 }
michael@16 711
michael@16 712 return result;
michael@16 713 }
michael@16 714
michael@16 715 /*
michael@16 716 * regcomp each prefix.
michael@16 717 */
michael@16 718 static int load_prefix_regex(void)
michael@16 719 {
michael@16 720 int i, status, result = 0;
michael@16 721
michael@16 722 for (i = 0; i < MAX_NO_OF_LCRS; i++) {
michael@16 723 if ((*lcrs)[i].end_record != 0) {
michael@16 724 break;
michael@16 725 }
michael@16 726 if (prefix_reg[i].valid) {
michael@16 727 regfree(&(prefix_reg[i].re));
michael@16 728 prefix_reg[i].valid = 0;
michael@16 729 }
michael@16 730 memset(&(prefix_reg[i].re), 0, sizeof(regex_t));
michael@16 731 if ((status=regcomp(&(prefix_reg[i].re),(*lcrs)[i].prefix,0))!=0){
michael@16 732 LM_ERR("bad prefix re <%s>, regcomp returned %d (check regex.h)\n",
michael@16 733 (*lcrs)[i].prefix, status);
michael@16 734 result = -1;
michael@16 735 break;
michael@16 736 }
michael@16 737 prefix_reg[i].valid = 1;
michael@16 738 }
michael@16 739
michael@16 740 return result;
michael@16 741 }
michael@16 742
michael@16 743 /*
michael@16 744 * regcomp each from_uri.
michael@16 745 */
michael@16 746 static int load_from_uri_regex(void)
michael@16 747 {
michael@16 748 int i, status, result = 0;
michael@16 749
michael@16 750 for (i = 0; i < MAX_NO_OF_LCRS; i++) {
michael@16 751 if ((*lcrs)[i].end_record != 0) {
michael@16 752 break;
michael@16 753 }
michael@16 754 if (from_uri_reg[i].valid) {
michael@16 755 regfree(&(from_uri_reg[i].re));
michael@16 756 from_uri_reg[i].valid = 0;
michael@16 757 }
michael@16 758 memset(&(from_uri_reg[i].re), 0, sizeof(regex_t));
michael@16 759 if ((status=regcomp(&(from_uri_reg[i].re),(*lcrs)[i].from_uri,0))!=0){
michael@16 760 LM_ERR("Bad from_uri re <%s>, regcomp returned %d (check regex.h)\n",
michael@16 761 (*lcrs)[i].from_uri, status);
michael@16 762 result = -1;
michael@16 763 break;
michael@16 764 }
michael@16 765 from_uri_reg[i].valid = 1;
michael@16 766 }
michael@16 767
michael@16 768 if (result != -1) {
michael@16 769 reload_counter = *lcrs_ws_reload_counter;
michael@16 770 }
michael@16 771 return result;
michael@16 772 }
michael@16 773
michael@16 774 static int load_all_regex(void)
michael@16 775 {
michael@16 776 int result =0;
michael@16 777
michael@16 778 if (prefix_mode_param != 0) {
michael@16 779 result = load_prefix_regex();
michael@16 780 }
michael@16 781
michael@16 782 if (result == 0) {
michael@16 783 result = load_from_uri_regex();
michael@16 784 } else {
michael@16 785 LM_ERR("Unable to load prefix regex\n");
michael@16 786 }
michael@16 787
michael@16 788 if (result == 0) {
michael@16 789 reload_counter = *lcrs_ws_reload_counter;
michael@16 790 } else {
michael@16 791 LM_ERR("Unable to load from_uri regex\n");
michael@16 792 }
michael@16 793
michael@16 794 return result;
michael@16 795 }
michael@16 796
michael@16 797 /*
michael@16 798 * Reload gws to unused gw table and lcrs to unused lcr table, and, when done
michael@16 799 * make unused gw and lcr table the one in use.
michael@16 800 */
michael@16 801 int reload_gws(void)
michael@16 802 {
michael@16 803 unsigned int i, port, strip, tag_len, prefix_len, from_uri_len,
michael@16 804 grp_id, priority;
michael@16 805 struct in_addr ip_addr;
michael@16 806 unsigned int flags;
michael@16 807 uri_type scheme;
michael@16 808 uri_transport transport;
michael@16 809 db_con_t* dbh;
michael@16 810 char *tag, *prefix, *from_uri;
michael@16 811 db_res_t* res = NULL;
michael@16 812 db_row_t* row;
michael@16 813 db_key_t gw_cols[8];
michael@16 814 db_key_t lcr_cols[4];
michael@16 815
michael@16 816 gw_cols[0] = &ip_addr_col;
michael@16 817 gw_cols[1] = &port_col;
michael@16 818 gw_cols[2] = &uri_scheme_col;
michael@16 819 gw_cols[3] = &transport_col;
michael@16 820 gw_cols[4] = &strip_col;
michael@16 821 gw_cols[5] = &tag_col;
michael@16 822 /* FIXME: is this ok if we have different names for grp_id
michael@16 823 in the two tables? (ge vw lcr) */
michael@16 824 gw_cols[6] = &grp_id_col;
michael@16 825 gw_cols[7] = &flags_col;
michael@16 826
michael@16 827 lcr_cols[0] = &prefix_col;
michael@16 828 lcr_cols[1] = &from_uri_col;
michael@16 829 /* FIXME: is this ok if we have different names for grp_id
michael@16 830 in the two tables? (ge vw lcr) */
michael@16 831 lcr_cols[2] = &grp_id_col;
michael@16 832 lcr_cols[3] = &priority_col;
michael@16 833
michael@16 834 if (lcr_dbf.init==0){
michael@16 835 LM_CRIT("Unbound database\n");
michael@16 836 return -1;
michael@16 837 }
michael@16 838 dbh=lcr_dbf.init(&db_url);
michael@16 839 if (dbh==0){
michael@16 840 LM_ERR("Unable to open database connection\n");
michael@16 841 return -1;
michael@16 842 }
michael@16 843
michael@16 844 if (lcr_dbf.use_table(dbh, &gw_table) < 0) {
michael@16 845 LM_ERR("Error while trying to use gw table\n");
michael@16 846 return -1;
michael@16 847 }
michael@16 848
michael@16 849 if (lcr_dbf.query(dbh, NULL, 0, NULL, gw_cols, 0, 8, 0, &res) < 0) {
michael@16 850 LM_ERR("Failed to query gw data\n");
michael@16 851 lcr_dbf.close(dbh);
michael@16 852 return -1;
michael@16 853 }
michael@16 854
michael@16 855 if (RES_ROW_N(res) + 1 > MAX_NO_OF_GWS) {
michael@16 856 LM_ERR("Too many gateways\n");
michael@16 857 lcr_dbf.free_result(dbh, res);
michael@16 858 lcr_dbf.close(dbh);
michael@16 859 return -1;
michael@16 860 }
michael@16 861
michael@16 862 for (i = 0; i < RES_ROW_N(res); i++) {
michael@16 863 row = RES_ROWS(res) + i;
michael@16 864 if (!((VAL_TYPE(ROW_VALUES(row)) == DB_STRING) &&
michael@16 865 !VAL_NULL(ROW_VALUES(row)) &&
michael@16 866 inet_aton((char *)VAL_STRING(ROW_VALUES(row)), &ip_addr) != 0)) {
michael@16 867 LM_ERR("Invalid IP address of gw <%s>\n",
michael@16 868 (char *)VAL_STRING(ROW_VALUES(row)));
michael@16 869 lcr_dbf.free_result(dbh, res);
michael@16 870 lcr_dbf.close(dbh);
michael@16 871 return -1;
michael@16 872 }
michael@16 873 if (VAL_NULL(ROW_VALUES(row) + 1) == 1) {
michael@16 874 port = 0;
michael@16 875 } else {
michael@16 876 port = (unsigned int)VAL_INT(ROW_VALUES(row) + 1);
michael@16 877 }
michael@16 878 if (port > 65536) {
michael@16 879 LM_ERR("Port of gw is too large <%u>\n", port);
michael@16 880 lcr_dbf.free_result(dbh, res);
michael@16 881 lcr_dbf.close(dbh);
michael@16 882 return -1;
michael@16 883 }
michael@16 884 if (VAL_NULL(ROW_VALUES(row) + 2) == 1) {
michael@16 885 scheme = SIP_URI_T;
michael@16 886 } else {
michael@16 887 scheme = (uri_type)VAL_INT(ROW_VALUES(row) + 2);
michael@16 888 if ((scheme != SIP_URI_T) && (scheme != SIPS_URI_T)) {
michael@16 889 LM_ERR("Unknown or unsupported URI scheme <%u>\n",
michael@16 890 (unsigned int)scheme);
michael@16 891 lcr_dbf.free_result(dbh, res);
michael@16 892 lcr_dbf.close(dbh);
michael@16 893 return -1;
michael@16 894 }
michael@16 895 }
michael@16 896 if (VAL_NULL(ROW_VALUES(row) + 3) == 1) {
michael@16 897 transport = PROTO_NONE;
michael@16 898 } else {
michael@16 899 transport = (uri_transport)VAL_INT(ROW_VALUES(row) + 3);
michael@16 900 if ((transport != PROTO_UDP) && (transport != PROTO_TCP) &&
michael@16 901 (transport != PROTO_TLS)) {
michael@16 902 LM_ERR("Unknown or unsupported transport <%u>\n",
michael@16 903 (unsigned int)transport);
michael@16 904 lcr_dbf.free_result(dbh, res);
michael@16 905 lcr_dbf.close(dbh);
michael@16 906 return -1;
michael@16 907 }
michael@16 908 }
michael@16 909 if (VAL_NULL(ROW_VALUES(row) + 4) == 1) {
michael@16 910 strip = 0;
michael@16 911 } else {
michael@16 912 strip = (unsigned int)VAL_INT(ROW_VALUES(row) + 4);
michael@16 913 }
michael@16 914 if (VAL_NULL(ROW_VALUES(row) + 5) == 1) {
michael@16 915 tag_len = 0;
michael@16 916 tag = (char *)0;
michael@16 917 } else {
michael@16 918 tag = (char *)VAL_STRING(ROW_VALUES(row) + 5);
michael@16 919 tag_len = strlen(tag);
michael@16 920 if (tag_len > MAX_TAG_LEN) {
michael@16 921 LM_ERR("Too long gw tag <%u>\n", tag_len);
michael@16 922 lcr_dbf.free_result(dbh, res);
michael@16 923 lcr_dbf.close(dbh);
michael@16 924 return -1;
michael@16 925 }
michael@16 926 }
michael@16 927 if (VAL_NULL(ROW_VALUES(row) + 6) == 1) {
michael@16 928 grp_id = 0;
michael@16 929 } else {
michael@16 930 grp_id = VAL_INT(ROW_VALUES(row) + 6);
michael@16 931 }
michael@16 932 if (!VAL_NULL(ROW_VALUES(row) + 7) &&
michael@16 933 (VAL_TYPE(ROW_VALUES(row) + 7) == DB_INT)) {
michael@16 934 flags = (unsigned int)VAL_INT(ROW_VALUES(row) + 7);
michael@16 935 } else {
michael@16 936 LM_ERR("Attribute flags is NULL or non-int\n");
michael@16 937 lcr_dbf.free_result(dbh, res);
michael@16 938 lcr_dbf.close(dbh);
michael@16 939 return -1;
michael@16 940 }
michael@16 941 if (*gws == gws_1) {
michael@16 942 gws_2[i].ip_addr = (unsigned int)ip_addr.s_addr;
michael@16 943 gws_2[i].port = port;
michael@16 944 gws_2[i].grp_id = grp_id;
michael@16 945 gws_2[i].scheme = scheme;
michael@16 946 gws_2[i].transport = transport;
michael@16 947 gws_2[i].flags = flags;
michael@16 948 gws_2[i].strip = strip;
michael@16 949 gws_2[i].tag_len = tag_len;
michael@16 950 if (tag_len)
michael@16 951 memcpy(&(gws_2[i].tag[0]), tag, tag_len);
michael@16 952 } else {
michael@16 953 gws_1[i].ip_addr = (unsigned int)ip_addr.s_addr;
michael@16 954 gws_1[i].port = port;
michael@16 955 gws_1[i].grp_id = grp_id;
michael@16 956 gws_1[i].scheme = scheme;
michael@16 957 gws_1[i].transport = transport;
michael@16 958 gws_1[i].flags = flags;
michael@16 959 gws_1[i].strip = strip;
michael@16 960 gws_1[i].tag_len = tag_len;
michael@16 961 if (tag_len)
michael@16 962 memcpy(&(gws_1[i].tag[0]), tag, tag_len);
michael@16 963 }
michael@16 964 }
michael@16 965
michael@16 966 lcr_dbf.free_result(dbh, res);
michael@16 967
michael@16 968 if (*gws == gws_1) {
michael@16 969 gws_2[i].ip_addr = 0;
michael@16 970 *gws = gws_2;
michael@16 971 } else {
michael@16 972 gws_1[i].ip_addr = 0;
michael@16 973 *gws = gws_1;
michael@16 974 }
michael@16 975
michael@16 976
michael@16 977 if (lcr_dbf.use_table(dbh, &lcr_table) < 0) {
michael@16 978 LM_ERR("Error while trying to use lcr table\n");
michael@16 979 return -1;
michael@16 980 }
michael@16 981
michael@16 982 if (lcr_dbf.query(dbh, NULL, 0, NULL, lcr_cols, 0, 4, 0, &res) < 0) {
michael@16 983 LM_ERR("Failed to query lcr data\n");
michael@16 984 lcr_dbf.close(dbh);
michael@16 985 return -1;
michael@16 986 }
michael@16 987
michael@16 988 if (RES_ROW_N(res) + 1 > MAX_NO_OF_LCRS) {
michael@16 989 LM_ERR("Too many lcr entries <%d>\n", RES_ROW_N(res));
michael@16 990 lcr_dbf.free_result(dbh, res);
michael@16 991 lcr_dbf.close(dbh);
michael@16 992 return -1;
michael@16 993 }
michael@16 994 for (i = 0; i < RES_ROW_N(res); i++) {
michael@16 995 row = RES_ROWS(res) + i;
michael@16 996 if (VAL_NULL(ROW_VALUES(row)) == 1) {
michael@16 997 prefix_len = 0;
michael@16 998 prefix = 0;
michael@16 999 } else {
michael@16 1000 prefix = (char *)VAL_STRING(ROW_VALUES(row));
michael@16 1001 prefix_len = strlen(prefix);
michael@16 1002 if (prefix_len > MAX_PREFIX_LEN) {
michael@16 1003 LM_ERR("Too long lcr prefix <%u>\n", prefix_len);
michael@16 1004 lcr_dbf.free_result(dbh, res);
michael@16 1005 lcr_dbf.close(dbh);
michael@16 1006 return -1;
michael@16 1007 }
michael@16 1008 }
michael@16 1009 if (VAL_NULL(ROW_VALUES(row) + 1) == 1) {
michael@16 1010 from_uri_len = 0;
michael@16 1011 from_uri = 0;
michael@16 1012 } else {
michael@16 1013 from_uri = (char *)VAL_STRING(ROW_VALUES(row) + 1);
michael@16 1014 from_uri_len = strlen(from_uri);
michael@16 1015 if (from_uri_len > MAX_FROM_URI_LEN) {
michael@16 1016 LM_ERR("Too long from_uri <%u>\n", from_uri_len);
michael@16 1017 lcr_dbf.free_result(dbh, res);
michael@16 1018 lcr_dbf.close(dbh);
michael@16 1019 return -1;
michael@16 1020 }
michael@16 1021 }
michael@16 1022 if (VAL_NULL(ROW_VALUES(row) + 2) == 1) {
michael@16 1023 LM_ERR("Route grp_id is NULL\n");
michael@16 1024 lcr_dbf.free_result(dbh, res);
michael@16 1025 lcr_dbf.close(dbh);
michael@16 1026 return -1;
michael@16 1027 }
michael@16 1028 grp_id = (unsigned int)VAL_INT(ROW_VALUES(row) + 2);
michael@16 1029 if (VAL_NULL(ROW_VALUES(row) + 3) == 1) {
michael@16 1030 LM_ERR("Route priority is NULL\n");
michael@16 1031 lcr_dbf.free_result(dbh, res);
michael@16 1032 lcr_dbf.close(dbh);
michael@16 1033 return -1;
michael@16 1034 }
michael@16 1035 priority = (unsigned int)VAL_INT(ROW_VALUES(row) + 3);
michael@16 1036
michael@16 1037 if (*lcrs == lcrs_1) {
michael@16 1038 lcrs_2[i].prefix_len = prefix_len;
michael@16 1039 if (prefix_len)
michael@16 1040 memcpy(&(lcrs_2[i].prefix[0]), prefix, prefix_len);
michael@16 1041 lcrs_2[i].from_uri_len = from_uri_len;
michael@16 1042 if (from_uri_len) {
michael@16 1043 memcpy(&(lcrs_2[i].from_uri[0]), from_uri, from_uri_len);
michael@16 1044 lcrs_2[i].from_uri[from_uri_len] = '\0';
michael@16 1045 }
michael@16 1046 lcrs_2[i].grp_id = grp_id;
michael@16 1047 lcrs_2[i].priority = priority;
michael@16 1048 lcrs_2[i].end_record = 0;
michael@16 1049 } else {
michael@16 1050 lcrs_1[i].prefix_len = prefix_len;
michael@16 1051 if (prefix_len)
michael@16 1052 memcpy(&(lcrs_1[i].prefix[0]), prefix, prefix_len);
michael@16 1053 lcrs_1[i].from_uri_len = from_uri_len;
michael@16 1054 if (from_uri_len) {
michael@16 1055 memcpy(&(lcrs_1[i].from_uri[0]), from_uri, from_uri_len);
michael@16 1056 lcrs_1[i].from_uri[from_uri_len] = '\0';
michael@16 1057 }
michael@16 1058 lcrs_1[i].grp_id = grp_id;
michael@16 1059 lcrs_1[i].priority = priority;
michael@16 1060 lcrs_1[i].end_record = 0;
michael@16 1061 }
michael@16 1062 }
michael@16 1063
michael@16 1064 lcr_dbf.free_result(dbh, res);
michael@16 1065 lcr_dbf.close(dbh);
michael@16 1066
michael@16 1067 if (*lcrs == lcrs_1) {
michael@16 1068 lcrs_2[i].end_record = 1;
michael@16 1069 *lcrs = lcrs_2;
michael@16 1070 } else {
michael@16 1071 lcrs_1[i].end_record = 1;
michael@16 1072 *lcrs = lcrs_1;
michael@16 1073 }
michael@16 1074
michael@16 1075 (*lcrs_ws_reload_counter)++;
michael@16 1076 if (0 != load_all_regex()) {
michael@16 1077
michael@16 1078 return -1;
michael@16 1079 }
michael@16 1080
michael@16 1081 return 1;
michael@16 1082 }
michael@16 1083
michael@16 1084
michael@16 1085 int mi_print_gws(struct mi_node* rpl)
michael@16 1086 {
michael@16 1087 unsigned int i;
michael@16 1088 struct mi_attr* attr;
michael@16 1089 uri_transport transport;
michael@16 1090 char *transp;
michael@16 1091 struct mi_node* node;
michael@16 1092 struct ip_addr address;
michael@16 1093 char* p;
michael@16 1094 int len;
michael@16 1095
michael@16 1096 for (i = 0; i < MAX_NO_OF_GWS; i++) {
michael@16 1097
michael@16 1098 if ((*gws)[i].ip_addr == 0)
michael@16 1099 break;
michael@16 1100
michael@16 1101 node= add_mi_node_child(rpl,0 ,"GW", 2, 0, 0);
michael@16 1102 if(node == NULL)
michael@16 1103 return -1;
michael@16 1104
michael@16 1105 p = int2str((unsigned long)(*gws)[i].grp_id, &len );
michael@16 1106 attr = add_mi_attr(node, MI_DUP_VALUE, "GRP_ID", 6, p, len );
michael@16 1107 if(attr == NULL)
michael@16 1108 return -1;
michael@16 1109
michael@16 1110 transport = (*gws)[i].transport;
michael@16 1111 if (transport == PROTO_UDP)
michael@16 1112 transp= ";transport=udp";
michael@16 1113 else if (transport == PROTO_TCP)
michael@16 1114 transp= ";transport=tcp";
michael@16 1115 else if (transport == PROTO_TLS)
michael@16 1116 transp= ";transport=tls";
michael@16 1117 else
michael@16 1118 transp= "";
michael@16 1119
michael@16 1120 address.af = AF_INET;
michael@16 1121 address.len = 4;
michael@16 1122 address.u.addr32[0] = (*gws)[i].ip_addr;
michael@16 1123 attr= addf_mi_attr(node,0 ,"URI", 3,"%s:%s:%d%s",
michael@16 1124 ((*gws)[i].scheme == SIP_URI_T)?"sip":"sips",
michael@16 1125 ip_addr2a(&address),
michael@16 1126 ((*gws)[i].port == 0)?5060:(*gws)[i].port,transp);
michael@16 1127 if(attr == NULL)
michael@16 1128 return -1;
michael@16 1129
michael@16 1130 p = int2str((unsigned long)(*gws)[i].strip, &len );
michael@16 1131 attr = add_mi_attr(node, MI_DUP_VALUE, "STRIP", 5, p, len);
michael@16 1132 if(attr == NULL)
michael@16 1133 return -1;
michael@16 1134
michael@16 1135 attr = add_mi_attr(node, MI_DUP_VALUE, "TAG", 3,
michael@16 1136 (*gws)[i].tag, (*gws)[i].tag_len );
michael@16 1137 if(attr == NULL)
michael@16 1138 return -1;
michael@16 1139
michael@16 1140 p = int2str((unsigned long)(*gws)[i].flags, &len );
michael@16 1141 attr = add_mi_attr(node, MI_DUP_VALUE, "FLAGS", 5, p, len);
michael@16 1142 if(attr == NULL)
michael@16 1143 return -1;
michael@16 1144 }
michael@16 1145
michael@16 1146 for (i = 0; i < MAX_NO_OF_LCRS; i++) {
michael@16 1147 if ((*lcrs)[i].end_record != 0)
michael@16 1148 break;
michael@16 1149
michael@16 1150 node= add_mi_node_child(rpl, 0, "RULE", 4, 0, 0);
michael@16 1151 attr = add_mi_attr(node, 0, "PREFIX", 6, (*lcrs)[i].prefix,
michael@16 1152 (*lcrs)[i].prefix_len );
michael@16 1153 if(attr== 0)
michael@16 1154 return -1;
michael@16 1155
michael@16 1156 attr = add_mi_attr(node, 0, "FROM_URI", 8, (*lcrs)[i].from_uri,
michael@16 1157 (*lcrs)[i].from_uri_len );
michael@16 1158 if(attr== 0)
michael@16 1159 return -1;
michael@16 1160
michael@16 1161 p = int2str((unsigned long)(*lcrs)[i].grp_id, &len );
michael@16 1162 attr = add_mi_attr(node, MI_DUP_VALUE, "GRP_ID", 6, p, len );
michael@16 1163 if(attr == NULL)
michael@16 1164 return -1;
michael@16 1165
michael@16 1166 p = int2str((unsigned long)(*lcrs)[i].priority, &len );
michael@16 1167 attr = add_mi_attr(node, MI_DUP_VALUE, "PRIORITY", 8, p, len );
michael@16 1168 if(attr == NULL)
michael@16 1169 return -1;
michael@16 1170
michael@16 1171 }
michael@16 1172
michael@16 1173 return 0;
michael@16 1174 }
michael@16 1175
michael@16 1176
michael@16 1177 /*
michael@16 1178 * Load info of matching GWs from database to gw_uri AVPs
michael@16 1179 */
michael@16 1180 static int do_load_gws(struct sip_msg* _m, str *_from_uri, int _grp_id)
michael@16 1181 {
michael@16 1182 str ruri_user, from_uri, value;
michael@16 1183 char from_uri_str[MAX_FROM_URI_LEN + 1];
michael@16 1184 char ruri[MAX_URI_SIZE];
michael@16 1185 unsigned int i, j, k, index, addr, port, strip, gw_index,
michael@16 1186 duplicated_gw, flags, have_rpid_avp;
michael@16 1187 uri_type scheme;
michael@16 1188 uri_transport transport;
michael@16 1189 struct ip_addr address;
michael@16 1190 str addr_str, port_str;
michael@16 1191 char *at, *tag, *strip_string, *flags_string;
michael@16 1192 struct usr_avp *avp;
michael@16 1193 int_str val;
michael@16 1194 struct mi matched_gws[MAX_NO_OF_GWS + 1];
michael@16 1195 unsigned short tag_len, prefix_len, priority;
michael@16 1196 int randomizer_start, randomizer_end, randomizer_flag,
michael@16 1197 strip_len, flags_len;
michael@16 1198 struct lcr_info lcr_rec;
michael@16 1199
michael@16 1200 /* Find Request-URI user */
michael@16 1201 if ((parse_sip_msg_uri(_m) < 0) || (!_m->parsed_uri.user.s)) {
michael@16 1202 LM_ERR("Error while parsing R-URI\n");
michael@16 1203 return -1;
michael@16 1204 }
michael@16 1205 ruri_user = _m->parsed_uri.user;
michael@16 1206
michael@16 1207 if (_from_uri) {
michael@16 1208 /* take caller uri from _from_uri argument */
michael@16 1209 from_uri = *_from_uri;
michael@16 1210 } else {
michael@16 1211 /* take caller uri from RPID or From URI */
michael@16 1212 have_rpid_avp = 0;
michael@16 1213 avp = search_first_avp(rpid_avp_type, rpid_avp, &val, 0);
michael@16 1214 if (avp != NULL) {
michael@16 1215 /* Get URI user from RPID if not empty */
michael@16 1216 if (avp->flags & AVP_VAL_STR) {
michael@16 1217 if (val.s.s && val.s.len) {
michael@16 1218 from_uri = val.s;
michael@16 1219 have_rpid_avp = 1;
michael@16 1220 }
michael@16 1221 } else {
michael@16 1222 from_uri.s = int2str(val.n, &from_uri.len);
michael@16 1223 have_rpid_avp = 1;
michael@16 1224 }
michael@16 1225 }
michael@16 1226 if (!have_rpid_avp) {
michael@16 1227 /* Get URI from From URI */
michael@16 1228 if ((!_m->from) && (parse_headers(_m, HDR_FROM_F, 0) == -1)) {
michael@16 1229 LM_ERR("Error while parsing headers\n");
michael@16 1230 return -1;
michael@16 1231 }
michael@16 1232 if (!_m->from) {
michael@16 1233 LM_ERR("From header field not found\n");
michael@16 1234 return -1;
michael@16 1235 }
michael@16 1236 if ((!(_m->from)->parsed) && (parse_from_header(_m) < 0)) {
michael@16 1237 LM_ERR("Error while parsing From header\n");
michael@16 1238 return -1;
michael@16 1239 }
michael@16 1240 from_uri = get_from(_m)->uri;
michael@16 1241 }
michael@16 1242 }
michael@16 1243 if (from_uri.len <= MAX_FROM_URI_LEN) {
michael@16 1244 strncpy(from_uri_str, from_uri.s, from_uri.len);
michael@16 1245 from_uri_str[from_uri.len] = '\0';
michael@16 1246 } else {
michael@16 1247 LM_ERR("From URI is too long <%u>\n", from_uri.len);
michael@16 1248 return -1;
michael@16 1249 }
michael@16 1250
michael@16 1251 /*
michael@16 1252 * Check if the gws and lcrs were reloaded
michael@16 1253 */
michael@16 1254 if (reload_counter != *lcrs_ws_reload_counter) {
michael@16 1255 if (load_all_regex() != 0) {
michael@16 1256 return -1;
michael@16 1257 }
michael@16 1258 }
michael@16 1259
michael@16 1260 /*
michael@16 1261 * Let's match the gws:
michael@16 1262 * 1. prefix matching
michael@16 1263 * 2. from_uri matching
michael@16 1264 * 3. _grp_id matching
michael@16 1265 *
michael@16 1266 * Note: A gateway must be in the list _only_ once.
michael@16 1267 */
michael@16 1268 gw_index = 0;
michael@16 1269 duplicated_gw = 0;
michael@16 1270 for (i = 0; i < MAX_NO_OF_LCRS; i++) {
michael@16 1271 lcr_rec = (*lcrs)[i];
michael@16 1272 if (lcr_rec.end_record != 0) {
michael@16 1273 break;
michael@16 1274 }
michael@16 1275 if ( ((prefix_mode_param == 0) && (lcr_rec.prefix_len <= ruri_user.len) &&
michael@16 1276 (strncmp(lcr_rec.prefix, ruri_user.s, lcr_rec.prefix_len)==0)) ||
michael@16 1277 ( (prefix_mode_param != 0) && ( (lcr_rec.prefix_len == 0) ||
michael@16 1278 (prefix_reg[i].valid &&
michael@16 1279 (regexec(&(prefix_reg[i].re), ruri_user.s, 0,
michael@16 1280 (regmatch_t *)NULL, 0) == 0)) ) ) ) {
michael@16 1281 /* 1. Prefix matching is done */
michael@16 1282 if ((lcr_rec.from_uri_len == 0) ||
michael@16 1283 (from_uri_reg[i].valid &&
michael@16 1284 (regexec(&(from_uri_reg[i].re), from_uri_str, 0,
michael@16 1285 (regmatch_t *)NULL, 0) == 0))) {
michael@16 1286 /* 2. from_uri matching is done */
michael@16 1287 for (j = 0; j < MAX_NO_OF_GWS; j++) {
michael@16 1288 if ((*gws)[j].ip_addr == 0) {
michael@16 1289 break;
michael@16 1290 }
michael@16 1291 if (lcr_rec.grp_id == (*gws)[j].grp_id &&
michael@16 1292 (_grp_id < 0 || (*gws)[j].grp_id == _grp_id)) {
michael@16 1293 /* 3. _grp_id matching is done */
michael@16 1294 for (k = 0; k < gw_index; k++) {
michael@16 1295 if ((*gws)[j].ip_addr ==
michael@16 1296 (*gws)[matched_gws[k].gw_index].ip_addr) {
michael@16 1297 /* Found the same gw in the list */
michael@16 1298 /* Let's keep the one with higher */
michael@16 1299 /* match on prefix len */
michael@16 1300 LM_DBG("Duplicate gw for index"
michael@16 1301 " %d [%d,%d] and current [%d,%d] \n",
michael@16 1302 k, matched_gws[k].route_index,
michael@16 1303 matched_gws[k].route_index, i, j);
michael@16 1304 duplicated_gw = 1;
michael@16 1305 if (lcr_rec.prefix_len >
michael@16 1306 (*lcrs)[matched_gws[k].route_index].prefix_len) {
michael@16 1307 /* Replace the old entry with the new one */
michael@16 1308 LM_DBG("Replace [%d,%d]"
michael@16 1309 " with [%d,%d] on index %d:"
michael@16 1310 " prefix reason %d>%d\n",
michael@16 1311 matched_gws[k].route_index,
michael@16 1312 matched_gws[k].gw_index, i, j, k,
michael@16 1313 lcr_rec.prefix_len,
michael@16 1314 (*lcrs)[matched_gws[k].route_index].prefix_len);
michael@16 1315 matched_gws[k].route_index = i;
michael@16 1316 matched_gws[k].gw_index = j;
michael@16 1317 /* Stop searching in the matched_gws list */
michael@16 1318 break;
michael@16 1319 } else if (lcr_rec.prefix_len ==
michael@16 1320 (*lcrs)[matched_gws[k].route_index].prefix_len) {
michael@16 1321 if (lcr_rec.priority >
michael@16 1322 (*lcrs)[matched_gws[k].route_index].priority) {
michael@16 1323 /* Replace the old entry with the new one */
michael@16 1324 LM_DBG("Replace [%d,%d] with"
michael@16 1325 " [%d,%d] on index %d:"
michael@16 1326 " priority reason %d>%d\n",
michael@16 1327 matched_gws[k].route_index,
michael@16 1328 matched_gws[k].gw_index,
michael@16 1329 i, j, k, lcr_rec.priority,
michael@16 1330 (*lcrs)[matched_gws[k].route_index].priority);
michael@16 1331 matched_gws[k].route_index = i;
michael@16 1332 matched_gws[k].gw_index = j;
michael@16 1333 /* Stop searching in the matched_gws list */
michael@16 1334 break;
michael@16 1335 }
michael@16 1336 }
michael@16 1337 }
michael@16 1338 }
michael@16 1339 if (duplicated_gw == 0) {
michael@16 1340 /* This is a new gw */
michael@16 1341 matched_gws[gw_index].route_index = i;
michael@16 1342 matched_gws[gw_index].gw_index = j;
michael@16 1343 LM_DBG("Added matched_gws[%d]=[%d,%d]\n",
michael@16 1344 gw_index, i, j);
michael@16 1345 gw_index++;
michael@16 1346 } else {
michael@16 1347 duplicated_gw = 0;
michael@16 1348 }
michael@16 1349 }
michael@16 1350 }
michael@16 1351 }
michael@16 1352 }
michael@16 1353 }
michael@16 1354 matched_gws[gw_index].route_index = -1;
michael@16 1355 matched_gws[gw_index].gw_index = -1;
michael@16 1356
michael@16 1357 /*
michael@16 1358 * Sort the gateways based on:
michael@16 1359 * 1. prefix len
michael@16 1360 * 2. priority
michael@16 1361 */
michael@16 1362 qsort(matched_gws, gw_index, sizeof(struct mi), comp_lcrs);
michael@16 1363 randomizer_start = 0;
michael@16 1364
michael@16 1365 /* Randomizing the gateways with same prefix_len and same priority */
michael@16 1366 randomizer_flag = 0;
michael@16 1367 prefix_len = (*lcrs)[matched_gws[0].route_index].prefix_len;
michael@16 1368 priority = (*lcrs)[matched_gws[0].route_index].priority;
michael@16 1369 for (i = 1; i < gw_index; i++) {
michael@16 1370 if ( prefix_len == (*lcrs)[matched_gws[i].route_index].prefix_len &&
michael@16 1371 priority == (*lcrs)[matched_gws[i].route_index].priority) {
michael@16 1372 /* we have a match */
michael@16 1373 if (randomizer_flag == 0) {
michael@16 1374 randomizer_flag = 1;
michael@16 1375 randomizer_start = i - 1;
michael@16 1376 }
michael@16 1377 matched_gws[i - 1].randomizer = rand();
michael@16 1378 }
michael@16 1379 else {
michael@16 1380 if (randomizer_flag == 1) {
michael@16 1381 randomizer_end = i - 1;
michael@16 1382 randomizer_flag = 0;
michael@16 1383 qsort(&matched_gws[randomizer_start],
michael@16 1384 randomizer_end - randomizer_start + 1,
michael@16 1385 sizeof(struct mi), rand_lcrs);
michael@16 1386 }
michael@16 1387 prefix_len = (*lcrs)[matched_gws[i].route_index].prefix_len;
michael@16 1388 priority = (*lcrs)[matched_gws[i].route_index].priority;
michael@16 1389 }
michael@16 1390 }
michael@16 1391 if (randomizer_flag == 1) {
michael@16 1392 randomizer_end = gw_index - 1;
michael@16 1393 matched_gws[i - 1].randomizer = rand();
michael@16 1394 qsort(&matched_gws[randomizer_start],
michael@16 1395 randomizer_end - randomizer_start + 1,
michael@16 1396 sizeof(struct mi), rand_lcrs);
michael@16 1397 }
michael@16 1398
michael@16 1399 for (i = 0; i < MAX_NO_OF_GWS; i++) {
michael@16 1400 index = matched_gws[i].gw_index;
michael@16 1401 if (index == -1) {
michael@16 1402 break;
michael@16 1403 }
michael@16 1404 addr = (*gws)[index].ip_addr;
michael@16 1405 port = (*gws)[index].port;
michael@16 1406 scheme = (*gws)[index].scheme;
michael@16 1407 transport = (*gws)[index].transport;
michael@16 1408 flags = (*gws)[index].flags;
michael@16 1409 strip = (*gws)[index].strip;
michael@16 1410 if (strip > ruri_user.len) {
michael@16 1411 LM_ERR("Strip count of gw is too large <%u>\n", strip);
michael@16 1412 goto skip;
michael@16 1413 }
michael@16 1414 tag_len = (*gws)[index].tag_len;
michael@16 1415 tag = (*gws)[index].tag;
michael@16 1416 if (6 + tag_len + 40 /* flags + strip */ + 1 + 15 + 1 + 5 + 1 + 14 >
michael@16 1417 MAX_URI_SIZE) {
michael@16 1418 LM_ERR("Request URI would be too long\n");
michael@16 1419 goto skip;
michael@16 1420 }
michael@16 1421 at = (char *)&(ruri[0]);
michael@16 1422 flags_string = int2str(flags, &flags_len);
michael@16 1423 memcpy(at, flags_string, flags_len);
michael@16 1424 at = at + flags_len;
michael@16 1425 if (scheme == SIP_URI_T) {
michael@16 1426 memcpy(at, "sip:", 4); at = at + 4;
michael@16 1427 } else if (scheme == SIPS_URI_T) {
michael@16 1428 memcpy(at, "sips:", 5); at = at + 5;
michael@16 1429 } else {
michael@16 1430 LM_ERR("Unknown or unsupported URI scheme <%u>\n",
michael@16 1431 (unsigned int)scheme);
michael@16 1432 goto skip;
michael@16 1433 }
michael@16 1434 if (tag_len) {
michael@16 1435 memcpy(at, tag, tag_len); at = at + tag_len;
michael@16 1436 }
michael@16 1437 /* Add strip in this form |number.
michael@16 1438 * For example: |3 means strip first 3 characters.
michael@16 1439 */
michael@16 1440 *at = '|'; at = at + 1;
michael@16 1441 strip_string = int2str(strip, &strip_len);
michael@16 1442 memcpy(at, strip_string, strip_len);
michael@16 1443 at = at + strip_len;
michael@16 1444 *at = '@'; at = at + 1;
michael@16 1445 address.af = AF_INET;
michael@16 1446 address.len = 4;
michael@16 1447 address.u.addr32[0] = addr;
michael@16 1448 addr_str.s = ip_addr2a(&address);
michael@16 1449 addr_str.len = strlen(addr_str.s);
michael@16 1450 memcpy(at, addr_str.s, addr_str.len); at = at + addr_str.len;
michael@16 1451 if (port != 0) {
michael@16 1452 if (port > 65536) {
michael@16 1453 LM_ERR("Port of GW is too large <%u>\n", port);
michael@16 1454 goto skip;
michael@16 1455 }
michael@16 1456 *at = ':'; at = at + 1;
michael@16 1457 port_str.s = int2str(port, &port_str.len);
michael@16 1458 memcpy(at, port_str.s, port_str.len); at = at + port_str.len;
michael@16 1459 }
michael@16 1460 if (transport != PROTO_NONE) {
michael@16 1461 memcpy(at, ";transport=", 11); at = at + 11;
michael@16 1462 if (transport == PROTO_UDP) {
michael@16 1463 memcpy(at, "udp", 3); at = at + 3;
michael@16 1464 } else if (transport == PROTO_TCP) {
michael@16 1465 memcpy(at, "tcp", 3); at = at + 3;
michael@16 1466 } else if (transport == PROTO_TLS) {
michael@16 1467 memcpy(at, "tls", 3); at = at + 3;
michael@16 1468 } else {
michael@16 1469 LM_ERR("Unknown or unsupported transport <%u>\n",
michael@16 1470 (unsigned int)transport);
michael@16 1471 goto skip;
michael@16 1472 }
michael@16 1473 }
michael@16 1474 value.s = (char *)&(ruri[0]);
michael@16 1475 value.len = at - value.s;
michael@16 1476 val.s = value;
michael@16 1477 add_avp(gw_uri_avp_type|AVP_VAL_STR, gw_uri_avp, val);
michael@16 1478 LM_DBG("Added gw_uri_avp <%.*s>\n", value.len, value.s);
michael@16 1479 skip:
michael@16 1480 continue;
michael@16 1481 }
michael@16 1482
michael@16 1483 return 1;
michael@16 1484 }
michael@16 1485
michael@16 1486 /*
michael@16 1487 * Load info of matching GWs from database to gw_uri AVPs
michael@16 1488 * taking into account the given group id. Caller URI is taken
michael@16 1489 * from request.
michael@16 1490 */
michael@16 1491 static int load_gws_from_grp(struct sip_msg* _m, char* _s1, char* _s2)
michael@16 1492 {
michael@16 1493 str grp_s;
michael@16 1494 unsigned int grp_id;
michael@16 1495
michael@16 1496 if(((pv_elem_p)_s1)->spec.getf!=NULL)
michael@16 1497 {
michael@16 1498 if(pv_printf_s(_m, (pv_elem_p)_s1, &grp_s)!=0)
michael@16 1499 return -1;
michael@16 1500 if(str2int(&grp_s, &grp_id)!=0)
michael@16 1501 return -1;
michael@16 1502 } else {
michael@16 1503 grp_id = ((pv_elem_p)_s1)->spec.pvp.pvn.u.isname.name.n;
michael@16 1504 }
michael@16 1505 if (grp_id > 0) return do_load_gws(_m, (str *)0, (int)grp_id);
michael@16 1506 else return -1;
michael@16 1507 }
michael@16 1508
michael@16 1509
michael@16 1510 /*
michael@16 1511 * Load info of matching GWs from database to gw_uri AVPs.
michael@16 1512 * Caller URI is taken from request.
michael@16 1513 */
michael@16 1514 static int load_gws_0(struct sip_msg* _m, char* _s1, char* _s2)
michael@16 1515 {
michael@16 1516 return do_load_gws(_m, (str *)0, -1);
michael@16 1517 }
michael@16 1518
michael@16 1519
michael@16 1520 /*
michael@16 1521 * Load info of matching GWs from database to gw_uri AVPs.
michael@16 1522 * Caller URI is taken from pseudo variable argument.
michael@16 1523 */
michael@16 1524 static int load_gws_1(struct sip_msg* _m, char* _sp, char* _s2)
michael@16 1525 {
michael@16 1526 pv_spec_t *sp;
michael@16 1527 pv_value_t pv_val;
michael@16 1528 sp = (pv_spec_t *)_sp;
michael@16 1529
michael@16 1530 if (sp && (pv_get_spec_value(_m, sp, &pv_val) == 0)) {
michael@16 1531 if (pv_val.flags & PV_VAL_STR) {
michael@16 1532 if (pv_val.rs.len == 0 || pv_val.rs.s == NULL) {
michael@16 1533 LM_DBG("missing from uri\n");
michael@16 1534 return -1;
michael@16 1535 }
michael@16 1536 return do_load_gws(_m, &(pv_val.rs), -1);
michael@16 1537 } else {
michael@16 1538 LM_DBG("pseudo variable value is not string\n");
michael@16 1539 return -1;
michael@16 1540 }
michael@16 1541 } else {
michael@16 1542 LM_DBG("cannot get pseudo variable value\n");
michael@16 1543 return -1;
michael@16 1544 }
michael@16 1545 }
michael@16 1546
michael@16 1547
michael@16 1548 /*
michael@16 1549 * Rewrites scheme, host, port, and transport parts of R-URI based on first
michael@16 1550 * gw_uri AVP value, which is then destroyed. Also saves R-URI user to
michael@16 1551 * ruri_user AVP for later use in failure route block.
michael@16 1552 * If called from failure route block, appends a new branch to request
michael@16 1553 * where scheme, host, port, and transport of URI are taken from the first
michael@16 1554 * gw_uri AVP value, which is then destroyed. URI user is taken from
michael@16 1555 * ruri_user AVP value saved earlier.
michael@16 1556 * Returns 1 upon success and -1 upon failure.
michael@16 1557 */
michael@16 1558 static int next_gw(struct sip_msg* _m, char* _s1, char* _s2)
michael@16 1559 {
michael@16 1560 int_str gw_uri_val, ruri_user_val, val;
michael@16 1561 struct usr_avp *gu_avp, *ru_avp;
michael@16 1562 int rval;
michael@16 1563 str new_ruri;
michael@16 1564 char *at, *at_char, *strip_char, *endptr;
michael@16 1565 unsigned int strip;
michael@16 1566
michael@16 1567 gu_avp = search_first_avp(gw_uri_avp_type, gw_uri_avp, &gw_uri_val, 0);
michael@16 1568 if (!gu_avp) return -1;
michael@16 1569
michael@16 1570 /* Set flags_avp from integer at the beginning of of gw_uri */
michael@16 1571 val.n = (int)strtoul(gw_uri_val.s.s, &at, 0);
michael@16 1572 add_avp(flags_avp_type, flags_avp, val);
michael@16 1573 LM_DBG("Added flags_avp <%u>\n", (unsigned int)val.n);
michael@16 1574
michael@16 1575 gw_uri_val.s.len = gw_uri_val.s.len - (at - gw_uri_val.s.s);
michael@16 1576 gw_uri_val.s.s = at;
michael@16 1577
michael@16 1578 /* Create new Request-URI taking URI user from ruri_user AVP
michael@16 1579 and other parts of from gateway URI AVP. */
michael@16 1580 ru_avp = search_first_avp(ruri_user_avp_type, ruri_user_avp,
michael@16 1581 &ruri_user_val, 0);
michael@16 1582 if (!ru_avp) {
michael@16 1583 LM_DBG("ruri_user AVP no yet set -> use RURI\n");
michael@16 1584 /* parse RURI and ger username */
michael@16 1585 if (parse_sip_msg_uri(_m) < 0) {
michael@16 1586 LM_ERR("Parsing of R-URI failed\n");
michael@16 1587 return -1;
michael@16 1588 }
michael@16 1589 ruri_user_val.s = _m->parsed_uri.user;
michael@16 1590 /* Save Request-URI user for use in FAILURE_ROUTE */
michael@16 1591 val.s = _m->parsed_uri.user;
michael@16 1592 add_avp(ruri_user_avp_type|AVP_VAL_STR, ruri_user_avp, val);
michael@16 1593 LM_DBG("Added ruri_user_avp <%.*s>\n", val.s.len, val.s.s);
michael@16 1594 }
michael@16 1595
michael@16 1596 new_ruri.s = pkg_malloc(gw_uri_val.s.len + ruri_user_val.s.len);
michael@16 1597 if (!new_ruri.s) {
michael@16 1598 LM_ERR("No memory for new R-URI.\n");
michael@16 1599 return -1;
michael@16 1600 }
michael@16 1601 at_char = memchr(gw_uri_val.s.s, '@', gw_uri_val.s.len);
michael@16 1602 if (!at_char) {
michael@16 1603 pkg_free(new_ruri.s);
michael@16 1604 LM_ERR("No @ in gateway URI <%.*s>\n",
michael@16 1605 gw_uri_val.s.len, gw_uri_val.s.s);
michael@16 1606 return -1;
michael@16 1607 }
michael@16 1608 strip_char = memchr(gw_uri_val.s.s, '|', gw_uri_val.s.len);
michael@16 1609 if (!strip_char || strip_char + 1 >= at_char) {
michael@16 1610 pkg_free(new_ruri.s);
michael@16 1611 LM_ERR("No strip char | and at least one "
michael@16 1612 "char before @ in gateway URI <%.*s>\n",
michael@16 1613 gw_uri_val.s.len, gw_uri_val.s.s);
michael@16 1614 return -1;
michael@16 1615 }
michael@16 1616 at = new_ruri.s;
michael@16 1617 memcpy(at, gw_uri_val.s.s, strip_char - gw_uri_val.s.s);
michael@16 1618 at = at + (strip_char - gw_uri_val.s.s);
michael@16 1619 strip = strtol(strip_char + 1, &endptr, 10);
michael@16 1620 if (endptr != at_char) {
michael@16 1621 pkg_free(new_ruri.s);
michael@16 1622 LM_ERR("Non-digit char between | and @ chars in gw URI <%.*s>\n",
michael@16 1623 gw_uri_val.s.len, gw_uri_val.s.s);
michael@16 1624 return -1;
michael@16 1625 }
michael@16 1626 if (ruri_user_val.s.len - strip > 0) {
michael@16 1627 memcpy(at, ruri_user_val.s.s + strip,
michael@16 1628 ruri_user_val.s.len - strip);
michael@16 1629 at = at + ruri_user_val.s.len - strip;
michael@16 1630 }
michael@16 1631 if (*(at - 1) != ':') {
michael@16 1632 memcpy(at, at_char, gw_uri_val.s.len - (at_char - gw_uri_val.s.s));
michael@16 1633 at = at + gw_uri_val.s.len - (at_char - gw_uri_val.s.s);
michael@16 1634 } else {
michael@16 1635 memcpy(at, at_char + 1, gw_uri_val.s.len -
michael@16 1636 (at_char + 1 - gw_uri_val.s.s));
michael@16 1637 at = at + gw_uri_val.s.len - (at_char + 1 - gw_uri_val.s.s);
michael@16 1638 }
michael@16 1639 new_ruri.len = at - new_ruri.s;
michael@16 1640
michael@16 1641 /* set new RURI */
michael@16 1642 rval = set_ruri( _m, &new_ruri);
michael@16 1643 pkg_free(new_ruri.s);
michael@16 1644 destroy_avp(gu_avp);
michael@16 1645 if (rval!=0) {
michael@16 1646 LM_ERR("failed to set new RURI\n");
michael@16 1647 return -1;
michael@16 1648 }
michael@16 1649
michael@16 1650 return 1;
michael@16 1651 }
michael@16 1652
michael@16 1653
michael@16 1654 /*
michael@16 1655 * Checks if request comes from a gateway
michael@16 1656 */
michael@16 1657 static int do_from_gw(struct sip_msg* _m, pv_spec_t *addr_sp, int grp_id)
michael@16 1658 {
michael@16 1659 int i;
michael@16 1660 unsigned int src_addr;
michael@16 1661 pv_value_t pv_val;
michael@16 1662 struct ip_addr *ip;
michael@16 1663 int_str val;
michael@16 1664
michael@16 1665 if (addr_sp && (pv_get_spec_value(_m, addr_sp, &pv_val) == 0)) {
michael@16 1666 if (pv_val.flags & PV_VAL_INT) {
michael@16 1667 src_addr = pv_val.ri;
michael@16 1668 } else if (pv_val.flags & PV_VAL_STR) {
michael@16 1669 if ( (ip=str2ip( &pv_val.rs)) == NULL) {
michael@16 1670 LM_ERR("failed to convert IP address string to in_addr\n");
michael@16 1671 return -1;
michael@16 1672 } else {
michael@16 1673 src_addr = ip->u.addr32[0];
michael@16 1674 }
michael@16 1675 } else {
michael@16 1676 LM_ERR("IP address PV empty value\n");
michael@16 1677 return -1;
michael@16 1678 }
michael@16 1679 } else {
michael@16 1680 src_addr = _m->rcv.src_ip.u.addr32[0];
michael@16 1681 }
michael@16 1682
michael@16 1683 for (i = 0; i < MAX_NO_OF_GWS; i++) {
michael@16 1684 if ((*gws)[i].ip_addr == 0) {
michael@16 1685 return -1;
michael@16 1686 }
michael@16 1687 if ((*gws)[i].ip_addr == src_addr &&
michael@16 1688 (grp_id < 0 || (*gws)[i].grp_id == grp_id)) {
michael@16 1689 LM_DBG("Request came from gw\n");
michael@16 1690 val.n = (int)(*gws)[i].flags;
michael@16 1691 add_avp(flags_avp_type, flags_avp, val);
michael@16 1692 LM_DBG("Added flags_avp <%u>\n", (unsigned int)val.n);
michael@16 1693 return 1;
michael@16 1694 }
michael@16 1695 }
michael@16 1696
michael@16 1697 LM_DBG("Request did not come from gw\n");
michael@16 1698 return -1;
michael@16 1699 }
michael@16 1700
michael@16 1701
michael@16 1702 /*
michael@16 1703 * Checks if request comes from a gateway, taking source address from request
michael@16 1704 * and taking into account the group id.
michael@16 1705 */
michael@16 1706 static int from_gw_grp(struct sip_msg* _m, char* _grp_id, char* _s2)
michael@16 1707 {
michael@16 1708 return do_from_gw(_m, (pv_spec_t *)0, (int)(long)_grp_id);
michael@16 1709 }
michael@16 1710
michael@16 1711
michael@16 1712 /*
michael@16 1713 * Checks if request comes from a gateway, taking src_address from request
michael@16 1714 * and ignoring group id.
michael@16 1715 */
michael@16 1716 static int from_gw_0(struct sip_msg* _m, char* _s1, char* _s2)
michael@16 1717 {
michael@16 1718 return do_from_gw(_m, (pv_spec_t *)0, -1);
michael@16 1719 }
michael@16 1720
michael@16 1721
michael@16 1722 /*
michael@16 1723 * Checks if request comes from a gateway, taking source address from pw
michael@16 1724 * and ignoring group id.
michael@16 1725 */
michael@16 1726 static int from_gw_1(struct sip_msg* _m, char* _addr_sp, char* _s2)
michael@16 1727 {
michael@16 1728 return do_from_gw(_m, (pv_spec_t *)_addr_sp, -1);
michael@16 1729 }
michael@16 1730
michael@16 1731
michael@16 1732 /*
michael@16 1733 * Checks if in-dialog request goes to gateway
michael@16 1734 */
michael@16 1735 static int do_to_gw(struct sip_msg* _m, int grp_id)
michael@16 1736 {
michael@16 1737 char host[16];
michael@16 1738 struct in_addr addr;
michael@16 1739 unsigned int i;
michael@16 1740
michael@16 1741 if((_m->parsed_uri_ok == 0) && (parse_sip_msg_uri(_m) < 0)) {
michael@16 1742 LM_ERR("Error while parsing the R-URI\n");
michael@16 1743 return -1;
michael@16 1744 }
michael@16 1745
michael@16 1746 if (_m->parsed_uri.host.len > 15) {
michael@16 1747 return -1;
michael@16 1748 }
michael@16 1749 memcpy(host, _m->parsed_uri.host.s, _m->parsed_uri.host.len);
michael@16 1750 host[_m->parsed_uri.host.len] = 0;
michael@16 1751
michael@16 1752 if (!inet_aton(host, &addr)) {
michael@16 1753 return -1;
michael@16 1754 }
michael@16 1755
michael@16 1756 for (i = 0; i < MAX_NO_OF_GWS; i++) {
michael@16 1757 if ((*gws)[i].ip_addr == 0) {
michael@16 1758 return -1;
michael@16 1759 }
michael@16 1760 if ((*gws)[i].ip_addr == addr.s_addr &&
michael@16 1761 (grp_id < 0 || (*gws)[i].grp_id == grp_id)) {
michael@16 1762 return 1;
michael@16 1763 }
michael@16 1764 }
michael@16 1765
michael@16 1766 return -1;
michael@16 1767 }
michael@16 1768
michael@16 1769
michael@16 1770 /*
michael@16 1771 * Checks if in-dialog request goes to gateway, taking
michael@16 1772 * into account the group id.
michael@16 1773 */
michael@16 1774 static int to_gw_grp(struct sip_msg* _m, char* _s1, char* _s2)
michael@16 1775 {
michael@16 1776 int grp_id;
michael@16 1777
michael@16 1778 grp_id = (int)(long)_s1;
michael@16 1779 return do_to_gw(_m, grp_id);
michael@16 1780 }
michael@16 1781
michael@16 1782
michael@16 1783 /*
michael@16 1784 * Checks if in-dialog request goes to gateway, ignoring
michael@16 1785 * the group id.
michael@16 1786 */
michael@16 1787 static int to_gw(struct sip_msg* _m, char* _s1, char* _s2)
michael@16 1788 {
michael@16 1789 return do_to_gw(_m, -1);
michael@16 1790 }
michael@16 1791
michael@16 1792
michael@16 1793 /*
michael@16 1794 * Frees contact list used by load_contacts function
michael@16 1795 */
michael@16 1796 static inline void free_contact_list(struct contact *curr) {
michael@16 1797 struct contact *prev;
michael@16 1798 while (curr) {
michael@16 1799 prev = curr;
michael@16 1800 curr = curr->next;
michael@16 1801 pkg_free(prev);
michael@16 1802 }
michael@16 1803 }
michael@16 1804
michael@16 1805 /* Encode branch info from contact struct to str */
michael@16 1806 static inline int encode_branch_info(str *info, struct contact *con)
michael@16 1807 {
michael@16 1808 char *at, *s;
michael@16 1809 int len;
michael@16 1810
michael@16 1811 info->len = con->uri.len + con->dst_uri.len +
michael@16 1812 con->path.len + MAX_SOCKET_STR + INT2STR_MAX_LEN + 5;
michael@16 1813 info->s = pkg_malloc(info->len);
michael@16 1814 if (!info->s) {
michael@16 1815 LM_ERR("No memory left for branch info\n");
michael@16 1816 return 0;
michael@16 1817 }
michael@16 1818 at = info->s;
michael@16 1819 memcpy(at, con->uri.s, con->uri.len);
michael@16 1820 at = at + con->uri.len;
michael@16 1821 *at = '\n';
michael@16 1822 at++;
michael@16 1823 memcpy(at, con->dst_uri.s, con->dst_uri.len);
michael@16 1824 at = at + con->dst_uri.len;
michael@16 1825 *at = '\n';
michael@16 1826 at++;
michael@16 1827 memcpy(at, con->path.s, con->path.len);
michael@16 1828 at = at + con->path.len;
michael@16 1829 *at = '\n';
michael@16 1830 at++;
michael@16 1831 if (con->sock) {
michael@16 1832 len = MAX_SOCKET_STR;
michael@16 1833 if (!socket2str(con->sock, at, &len, 1)) {
michael@16 1834 LM_ERR("Failed to convert socket to str\n");
michael@16 1835 return 0;
michael@16 1836 }
michael@16 1837 } else {
michael@16 1838 len = 0;
michael@16 1839 }
michael@16 1840 at = at + len;
michael@16 1841 *at = '\n';
michael@16 1842 at++;
michael@16 1843 s = int2str(con->flags, &len);
michael@16 1844 memcpy(at, s, len);
michael@16 1845 at = at + len;
michael@16 1846 *at = '\n';
michael@16 1847 info->len = at - info->s + 1;
michael@16 1848
michael@16 1849 return 1;
michael@16 1850 }
michael@16 1851
michael@16 1852
michael@16 1853 /* Encode branch info from str */
michael@16 1854 static inline int decode_branch_info(char *info, str *uri, str *dst, str *path,
michael@16 1855 struct socket_info **sock, unsigned int *flags)
michael@16 1856 {
michael@16 1857 str s, host;
michael@16 1858 int port, proto;
michael@16 1859 char *pos, *at;
michael@16 1860
michael@16 1861 pos = strchr(info, '\n');
michael@16 1862 uri->len = pos - info;
michael@16 1863 if (uri->len) {
michael@16 1864 uri->s = info;
michael@16 1865 } else {
michael@16 1866 uri->s = 0;
michael@16 1867 }
michael@16 1868 at = pos + 1;
michael@16 1869
michael@16 1870 pos = strchr(at, '\n');
michael@16 1871 dst->len = pos - at;
michael@16 1872 if (dst->len) {
michael@16 1873 dst->s = at;
michael@16 1874 } else {
michael@16 1875 dst->s = 0;
michael@16 1876 }
michael@16 1877 at = pos + 1;
michael@16 1878
michael@16 1879 pos = strchr(at, '\n');
michael@16 1880 path->len = pos - at;
michael@16 1881 if (path->len) {
michael@16 1882 path->s = at;
michael@16 1883 } else {
michael@16 1884 path->s = 0;
michael@16 1885 }
michael@16 1886 at = pos + 1;
michael@16 1887
michael@16 1888 pos = strchr(at, '\n');
michael@16 1889 s.len = pos - at;
michael@16 1890 if (s.len) {
michael@16 1891 s.s = at;
michael@16 1892 if (parse_phostport(s.s, s.len, &host.s, &host.len,
michael@16 1893 &port, &proto) != 0) {
michael@16 1894 LM_ERR("Parsing of socket info <%.*s> failed\n", s.len, s.s);
michael@16 1895 return 0;
michael@16 1896 }
michael@16 1897 *sock = grep_sock_info(&host, (unsigned short)port,
michael@16 1898 (unsigned short)proto);
michael@16 1899 if (*sock == 0) {
michael@16 1900 LM_ERR("Invalid socket <%.*s>\n", s.len, s.s);
michael@16 1901 return 0;
michael@16 1902 }
michael@16 1903 } else {
michael@16 1904 *sock = 0;
michael@16 1905 }
michael@16 1906 at = pos + 1;
michael@16 1907
michael@16 1908 pos = strchr(at, '\n');
michael@16 1909 s.len = pos - at;
michael@16 1910 if (s.len) {
michael@16 1911 s.s = at;
michael@16 1912 if (str2int(&s, flags) != 0) {
michael@16 1913 LM_ERR("Failed to decode flags <%.*s>\n", s.len, s.s);
michael@16 1914 return 0;
michael@16 1915 }
michael@16 1916 } else {
michael@16 1917 *flags = 0;
michael@16 1918 }
michael@16 1919
michael@16 1920 return 1;
michael@16 1921 }
michael@16 1922
michael@16 1923
michael@16 1924 /*
michael@16 1925 * Loads contacts in destination set into "lcr_contact" AVP in reverse
michael@16 1926 * priority order and associated each contact with Q_FLAG telling if
michael@16 1927 * contact is the last one in its priority class. Finally, removes
michael@16 1928 * all branches from destination set.
michael@16 1929 */
michael@16 1930 static int load_contacts(struct sip_msg* msg, char* key, char* value)
michael@16 1931 {
michael@16 1932 str uri, dst_uri, path, branch_info, *ruri;
michael@16 1933 qvalue_t q, ruri_q;
michael@16 1934 struct contact *contacts, *next, *prev, *curr;
michael@16 1935 int_str val;
michael@16 1936 int idx;
michael@16 1937 struct socket_info* sock;
michael@16 1938 unsigned int flags;
michael@16 1939
michael@16 1940 /* Check if anything needs to be done */
michael@16 1941 if (nr_branches == 0) {
michael@16 1942 LM_DBG("Nothing to do - no branches!\n");
michael@16 1943 return 1;
michael@16 1944 }
michael@16 1945
michael@16 1946 ruri = GET_RURI(msg);
michael@16 1947 if (!ruri) {
michael@16 1948 LM_ERR("No Request-URI found\n");
michael@16 1949 return -1;
michael@16 1950 }
michael@16 1951 ruri_q = get_ruri_q();
michael@16 1952
michael@16 1953 for(idx = 0; (uri.s = get_branch(idx, &uri.len, &q, 0, 0, 0, 0)) != 0;
michael@16 1954 idx++) {
michael@16 1955 if (q != ruri_q) {
michael@16 1956 goto rest;
michael@16 1957 }
michael@16 1958 }
michael@16 1959 LM_DBG("Nothing to do - all contacts have same q!\n");
michael@16 1960 return 1;
michael@16 1961
michael@16 1962 rest:
michael@16 1963 /* Insert Request-URI branch to contact list */
michael@16 1964 contacts = (struct contact *)pkg_malloc(sizeof(struct contact));
michael@16 1965 if (!contacts) {
michael@16 1966 LM_ERR("No memory for contact info\n");
michael@16 1967 return -1;
michael@16 1968 }
michael@16 1969 contacts->uri.s = ruri->s;
michael@16 1970 contacts->uri.len = ruri->len;
michael@16 1971 contacts->q = ruri_q;
michael@16 1972 contacts->dst_uri = msg->dst_uri;
michael@16 1973 contacts->sock = msg->force_send_socket;
michael@16 1974 contacts->flags = getb0flags();
michael@16 1975 contacts->path = msg->path_vec;
michael@16 1976 contacts->next = (struct contact *)0;
michael@16 1977
michael@16 1978 /* Insert branches to contact list in increasing q order */
michael@16 1979 for(idx = 0;
michael@16 1980 (uri.s = get_branch(idx,&uri.len,&q,&dst_uri,&path,&flags,&sock))
michael@16 1981 != 0;
michael@16 1982 idx++ ) {
michael@16 1983 next = (struct contact *)pkg_malloc(sizeof(struct contact));
michael@16 1984 if (!next) {
michael@16 1985 LM_ERR("No memory for contact info\n");
michael@16 1986 free_contact_list(contacts);
michael@16 1987 return -1;
michael@16 1988 }
michael@16 1989 next->uri = uri;
michael@16 1990 next->q = q;
michael@16 1991 next->dst_uri = dst_uri;
michael@16 1992 next->path = path;
michael@16 1993 next->flags = flags;
michael@16 1994 next->sock = sock;
michael@16 1995 next->next = (struct contact *)0;
michael@16 1996 prev = (struct contact *)0;
michael@16 1997 curr = contacts;
michael@16 1998 while (curr && (curr->q < q)) {
michael@16 1999 prev = curr;
michael@16 2000 curr = curr->next;
michael@16 2001 }
michael@16 2002 if (!curr) {
michael@16 2003 next->next = (struct contact *)0;
michael@16 2004 prev->next = next;
michael@16 2005 } else {
michael@16 2006 next->next = curr;
michael@16 2007 if (prev) {
michael@16 2008 prev->next = next;
michael@16 2009 } else {
michael@16 2010 contacts = next;
michael@16 2011 }
michael@16 2012 }
michael@16 2013 }
michael@16 2014
michael@16 2015 /* Assign values for q_flags */
michael@16 2016 curr = contacts;
michael@16 2017 curr->q_flag = 0;
michael@16 2018 while (curr->next) {
michael@16 2019 if (curr->q < curr->next->q) {
michael@16 2020 curr->next->q_flag = Q_FLAG;
michael@16 2021 } else {
michael@16 2022 curr->next->q_flag = 0;
michael@16 2023 }
michael@16 2024 curr = curr->next;
michael@16 2025 }
michael@16 2026
michael@16 2027 /* Add contacts to "contacts" AVP */
michael@16 2028 curr = contacts;
michael@16 2029 while (curr) {
michael@16 2030 if (encode_branch_info(&branch_info, curr) == 0) {
michael@16 2031 LM_ERR("Encoding of branch info failed\n");
michael@16 2032 free_contact_list(contacts);
michael@16 2033 if (branch_info.s) pkg_free(branch_info.s);
michael@16 2034 return -1;
michael@16 2035 }
michael@16 2036 val.s = branch_info;
michael@16 2037 add_avp(contact_avp_type|AVP_VAL_STR|(curr->q_flag),
michael@16 2038 contact_avp, val);
michael@16 2039 pkg_free(branch_info.s);
michael@16 2040 LM_DBG("Loaded contact <%.*s> with q_flag <%d>\n",
michael@16 2041 val.s.len, val.s.s, curr->q_flag);
michael@16 2042 curr = curr->next;
michael@16 2043 }
michael@16 2044
michael@16 2045 /* Clear all branches */
michael@16 2046 clear_branches();
michael@16 2047
michael@16 2048 /* Free contact list */
michael@16 2049 free_contact_list(contacts);
michael@16 2050
michael@16 2051 return 1;
michael@16 2052 }
michael@16 2053
michael@16 2054
michael@16 2055 /*
michael@16 2056 * Adds to request a destination set that includes all highest priority
michael@16 2057 * class contacts in "lcr_contact" AVP. If called from a route block,
michael@16 2058 * rewrites the request uri with first contact and adds the remaining
michael@16 2059 * contacts as branches. If called from failure route block, adds all
michael@16 2060 * contacts as branches. Removes added contacts from "lcr_contact" AVP.
michael@16 2061 */
michael@16 2062 static int next_contacts(struct sip_msg* msg, char* key, char* value)
michael@16 2063 {
michael@16 2064 struct usr_avp *avp, *prev;
michael@16 2065 int_str val;
michael@16 2066 str uri, dst, path;
michael@16 2067 struct socket_info *sock;
michael@16 2068 unsigned int flags;
michael@16 2069
michael@16 2070 /* Find first lcr_contact_avp value */
michael@16 2071 avp = search_first_avp(contact_avp_type, contact_avp, &val, 0);
michael@16 2072 if (!avp) {
michael@16 2073 LM_DBG("No AVPs -- we are done!\n");
michael@16 2074 return -1;
michael@16 2075 }
michael@16 2076
michael@16 2077 LM_DBG("Next contact is <%s>\n", val.s.s);
michael@16 2078
michael@16 2079 if (decode_branch_info(val.s.s, &uri, &dst, &path, &sock, &flags)== 0) {
michael@16 2080 LM_ERR("Decoding of branch info <%.*s> failed\n",
michael@16 2081 val.s.len, val.s.s);
michael@16 2082 destroy_avp(avp);
michael@16 2083 return -1;
michael@16 2084 }
michael@16 2085
michael@16 2086 set_ruri(msg, &uri);
michael@16 2087 set_dst_uri(msg, &dst);
michael@16 2088 set_path_vector(msg, &path);
michael@16 2089 msg->force_send_socket = sock;
michael@16 2090 setb0flags(flags);
michael@16 2091
michael@16 2092 if (avp->flags & Q_FLAG) {
michael@16 2093 destroy_avp(avp);
michael@16 2094 if (route_type == REQUEST_ROUTE) {
michael@16 2095 /* Set fr_inv_timer */
michael@16 2096 val.n = fr_inv_timer_next;
michael@16 2097 if (add_avp(fr_inv_timer_avp_type, fr_inv_timer_avp, val) != 0) {
michael@16 2098 LM_ERR("Setting of fr_inv_timer_avp failed\n");
michael@16 2099 return -1;
michael@16 2100 }
michael@16 2101 }
michael@16 2102 return 1;
michael@16 2103 }
michael@16 2104
michael@16 2105 /* Append branches until out of branches or Q_FLAG is set */
michael@16 2106 prev = avp;
michael@16 2107 while ((avp = search_next_avp(avp, &val))) {
michael@16 2108 destroy_avp(prev);
michael@16 2109
michael@16 2110 LM_DBG("Next contact is <%s>\n", val.s.s);
michael@16 2111
michael@16 2112 if (decode_branch_info(val.s.s, &uri, &dst, &path, &sock, &flags)== 0){
michael@16 2113 LM_ERR("Decoding of branch info <%.*s> failed\n",
michael@16 2114 val.s.len, val.s.s);
michael@16 2115 destroy_avp(avp);
michael@16 2116 return -1;
michael@16 2117 }
michael@16 2118
michael@16 2119 if (append_branch(msg, &uri, &dst, &path, 0, flags, sock) != 1) {
michael@16 2120 LM_ERR("Appending branch failed\n");
michael@16 2121 destroy_avp(avp);
michael@16 2122 return -1;
michael@16 2123 }
michael@16 2124
michael@16 2125 if (avp->flags & Q_FLAG) {
michael@16 2126 destroy_avp(avp);
michael@16 2127 if (route_type == REQUEST_ROUTE) {
michael@16 2128 val.n = fr_inv_timer_next;
michael@16 2129 if (add_avp(fr_inv_timer_avp_type, fr_inv_timer_avp, val)!= 0){
michael@16 2130 LM_ERR("Setting of fr_inv_timer_avp failed\n");
michael@16 2131 return -1;
michael@16 2132 }
michael@16 2133 }
michael@16 2134 return 1;
michael@16 2135 }
michael@16 2136 prev = avp;
michael@16 2137 }
michael@16 2138
michael@16 2139 /* Restore fr_inv_timer */
michael@16 2140 val.n = fr_inv_timer;
michael@16 2141 if (add_avp(fr_inv_timer_avp_type, fr_inv_timer_avp, val) != 0) {
michael@16 2142 LM_ERR("Setting of fr_inv_timer_avp failed\n");
michael@16 2143 return -1;
michael@16 2144 }
michael@16 2145
michael@16 2146 return 1;
michael@16 2147 }
michael@16 2148
michael@16 2149
michael@16 2150 /*
michael@16 2151 * Convert string parameter to integer for functions that expect an integer.
michael@16 2152 * Taken from sl module.
michael@16 2153 */
michael@16 2154 static int fixstringloadgws(void **param, int param_count)
michael@16 2155 {
michael@16 2156 pv_elem_t *model=NULL;
michael@16 2157 str s;
michael@16 2158
michael@16 2159 /* convert to str */
michael@16 2160 s.s = (char*)*param;
michael@16 2161 s.len = strlen(s.s);
michael@16 2162
michael@16 2163 model=NULL;
michael@16 2164 if (param_count==1) {
michael@16 2165 if(s.len==0) {
michael@16 2166 LM_ERR("No param <%d>!\n", param_count);
michael@16 2167 return -1;
michael@16 2168 }
michael@16 2169
michael@16 2170 if(pv_parse_format(&s,&model)<0 || model==NULL) {
michael@16 2171 LM_ERR("Wrong format <%s> for param <%d>!\n", s.s, param_count);
michael@16 2172 return -1;
michael@16 2173 }
michael@16 2174 if(model->spec.getf==NULL) {
michael@16 2175 if(param_count==1) {
michael@16 2176 if(str2int(&s, (unsigned int*)&model->spec.pvp.pvn.u.isname.name.n)!=0) {
michael@16 2177 LM_ERR("Wrong value <%s> for param <%d>!\n",
michael@16 2178 s.s, param_count);
michael@16 2179 return -1;
michael@16 2180 }
michael@16 2181 }
michael@16 2182 }
michael@16 2183 *param = (void*)model;
michael@16 2184 }
michael@16 2185
michael@16 2186 return 0;
michael@16 2187 }

mercurial