1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
|
/*
* $Id$
*
* Copyright (C) 2009 1&1 Internet AG
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*!
* \file
* \brief SIP-router utils ::
* \ingroup utils
* Module: \ref utils
*/
#include "conf.h"
#include "../../mem/mem.h"
#include "../../mem/shm_mem.h"
#include "../../sr_module.h"
#include "../../proxy.h"
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define BUFSIZE 1000
/*! \brief special filter indices */
enum {
sfidx_request = 0,
sfidx_reply,
sfilter_cnt
};
/*! special filter masks */
static int sfilter_mask[sfilter_cnt] = { 1, 2 };
/*! special filter names */
static char *sfilter_str[sfilter_cnt] = {
"REQUEST",
"REPLY"
};
struct fwd_setting {
int active;
int sfilter;
char *filter_methods;
struct proxy_l* proxy;
};
static struct fwd_setting *fwd_settings = NULL;
static int fwd_max_id = 0;
/*!
* \brief Removes white spaces and new lines from s
* \todo check if we can use the functions from ut.h
* Removes white spaces and new lines from s, the contents of s are modified.
* \param s the string.
*/
static void remove_spaces(char *s)
{
char *p, *dst;
for (p = s, dst = s; *p != '\0'; ++p) {
if (!isspace(*p)) *dst++ = *p;
}
*dst = '\0';
}
/*!
* \brief Converts a string to integer.
* \todo check if we can use the functions from ut.h
* params:
* s: The string to be converted to int.
* returns:
* >=0 on success
* -1 otherwise
*/
static int conf_str2int(char *s)
{
if (s == NULL) return -1;
errno = 0;
char *end = NULL;
long int i = strtol(s, &end, 10);
if ((errno != 0) || (i == LONG_MIN) || (i == LONG_MAX) || (end == s)) {
LM_ERR("invalid string '%s'.\n", s);
return -1;
}
return i;
}
/*!
* \brief Converts string to integer and checks for validity.
* \todo check if we can use the functions from ut.h
* params:
* id_str: ID as string to be converted to int.
* returns:
* >=0 on success
* -1 otherwise
*/
int conf_str2id(char *id_str)
{
int id = conf_str2int(id_str);
if ((id<0) || (id > fwd_max_id)) {
LM_ERR("id %d is out of range.\n", id);
return -1;
}
return id;
}
/*!
* \brief Updates switch configuration
* \param id Update the configuration with this ID.
* \param param_str can be either "off" or "on".
* \return 0 on success, -1 otherwise
*/
static int update_switch(int id, char* param_str)
{
if (param_str == NULL) {
LM_ERR("param_str is NULL.\n");
return -1;
}
if (strcmp(param_str, "on") == 0) {
fwd_settings[id].active = 1;
return 0;
} else if (strcmp(param_str, "off") == 0) {
fwd_settings[id].active = 0;
return 0;
}
LM_ERR("invalid switch '%s'.\n", param_str);
return -1;
}
/*!
* \brief Updates filter configuration.
* Updates filter configuration.
* If filter_methods is not NULL, memory is freed.
* If filter methods are found, memory for the string is allocated,
* otherwise filter_methods is set to NULL.
* \param id update the configuration with this ID.
* \param flist a list of filter names.
* \return 0 on success, -1 otherwise
*/
static int update_filter(int id, char *flist)
{
if (flist == NULL) {
LM_ERR("flist is NULL.\n");
return -1;
}
/* reset special filter mask and filter methods*/
fwd_settings[id].sfilter = 0;
if (fwd_settings[id].filter_methods != NULL) {
shm_free(fwd_settings[id].filter_methods);
fwd_settings[id].filter_methods = NULL;
}
int i;
for (i=0; i<sfilter_cnt; i++) {
if (strstr(flist, sfilter_str[i]) != NULL) {
/* special filter name is found in flist -> add to special filter mask */
fwd_settings[id].sfilter |= sfilter_mask[i];
}
}
char buf[BUFSIZE+1], tmp[BUFSIZE+1];
buf[0] = '\0';
char *set_p = flist;
char *token = NULL;
while ((token = strsep(&set_p, ":"))) { /* iterate through list of filters */
int found = 0;
/* is it a special filter? */
for (i=0; i<sfilter_cnt; i++) {
if (strcmp(token, sfilter_str[i]) == 0) {
found = 1;
break;
}
}
if (found == 0) {
/* no special filter! */
if (buf[0]) {
strcpy(tmp, buf);
snprintf(buf, BUFSIZE, "%s:%s", tmp, token);
buf[BUFSIZE]='\0';
} else {
snprintf(buf, BUFSIZE, "%s", token);
buf[BUFSIZE]='\0';
}
}
}
int len = strlen(buf);
if (len > 0) {
char *flc = shm_malloc(len+1);
if (flc == NULL) {
SHM_MEM_ERROR;
return -1;
}
memcpy(flc, buf, len+1);
fwd_settings[id].filter_methods = flc;
}
return 0;
}
/*!
* Updates proxy configuration
* \param id update the configuration with this ID.
* \param host_str the destination host.
* \param port_str the port number as string.
* \return 0 on success, -1 otherwise
*/
static int update_proxy(int id, char *host_str, char *port_str)
{
if (host_str == NULL) {
LM_ERR("host_str is NULL.\n");
return -1;
}
if (port_str == NULL) {
LM_ERR("port_str is NULL.\n");
return -1;
}
int port = conf_str2int(port_str);
if (port < 0) {
LM_ERR("invalid port '%s'.\n", port_str);
return -1;
}
/* make copy of host string since mk_proxy does not */
str host;
host.len = strlen(host_str);
host.s = shm_malloc(host.len+1);
if (host.s == NULL) {
SHM_MEM_ERROR;
return -1;
}
strcpy(host.s, host_str);
/* make proxy in shared memory */
struct proxy_l* proxy;
proxy = mk_shm_proxy(&host, port, PROTO_UDP);
if (proxy == NULL) {
LM_ERR("cannot make proxy (host='%s', port=%d).\n", host_str, port);
shm_free(host.s);
return -1;
}
if (fwd_settings[id].proxy) {
/* cleaning up old proxy */
if (fwd_settings[id].proxy->name.s) {
shm_free(fwd_settings[id].proxy->name.s);
}
free_shm_proxy(fwd_settings[id].proxy);
shm_free(fwd_settings[id].proxy);
}
fwd_settings[id].proxy = proxy; /* new proxy is now acitvated */
return 0;
}
/*!
* \brief Parses configuration string for the switch
* Parses a configuration string for switch settings and updates
* the configuration structure.
* \param settings the configuration string in the following form:
\verbatim
* <id>=<switch>[,<id>=<switch>]...
\endverbatim
* \return 1 on success, -1 otherwise
*/
int conf_parse_switch(char *settings)
{
/* make a copy since we are modifying it */
int len = strlen(settings);
if (len==0) return 1;
char *strc = (char *)pkg_malloc(len+1);
if (strc == NULL) {
PKG_MEM_ERROR;
return -1;
}
memcpy(strc, settings, len+1);
remove_spaces(strc);
char *set_p = strc;
char *token = NULL;
while ((token = strsep(&set_p, ","))) { /* iterate through list of settings */
char *id_str = strsep(&token, "=");
int id = conf_str2id(id_str);
if (id < 0) {
LM_ERR("cannot parse id '%s'.\n", id_str);
pkg_free(strc);
return -1;
}
/* got all data for one setting -> update configuration now */
if (update_switch(id, token) < 0) {
LM_ERR("cannot update switch.\n");
pkg_free(strc);
return -1;
}
}
pkg_free(strc);
return 1;
}
/*!
* \brief Output configuration in FIFO format
* \param rpl_tree FIFO root
* \return 0 on success, -1 on failure
*/
int conf_show(struct mi_root* rpl_tree)
{
int id, sfilter;
struct mi_node * node = NULL;
node = addf_mi_node_child( &rpl_tree->node, 0, 0, 0, "id switch %30s proxy\n", "filter");
if(node == NULL)
goto error;
for (id=0; id<=fwd_max_id; id++) {
char buf[BUFSIZE+1];
char tmp[BUFSIZE+1];
buf[0]='\0';
for (sfilter=0; sfilter<sfilter_cnt; sfilter++) {
if (fwd_settings[id].sfilter&sfilter_mask[sfilter]) {
if (buf[0]) {
strcpy(tmp, buf);
snprintf(buf, BUFSIZE, "%s:%s", tmp, sfilter_str[sfilter]);
buf[BUFSIZE]='\0';
} else {
snprintf(buf, BUFSIZE, "%s", sfilter_str[sfilter]);
buf[BUFSIZE]='\0';
}
}
}
if (fwd_settings[id].filter_methods) {
if (buf[0]) {
strcpy(tmp, buf);
snprintf(buf, BUFSIZE, "%s:%s", tmp, fwd_settings[id].filter_methods);
buf[BUFSIZE]='\0';
} else {
snprintf(buf, BUFSIZE, "%s", fwd_settings[id].filter_methods);
buf[BUFSIZE]='\0';
}
}
node = addf_mi_node_child( &rpl_tree->node, 0, 0, 0, "%2d %s %33s %s:%d\n", id,
fwd_settings[id].active ? "on " : "off", buf,
fwd_settings[id].proxy ? fwd_settings[id].proxy->name.s : "",
fwd_settings[id].proxy ? fwd_settings[id].proxy->port : 0);
if(node == NULL)
goto error;
}
return 0;
error:
return -1;
}
/*!
* \brief Parses a configuration string for the filter
* Parses a configuration string for switch settings and
* updates the configuration structure.
* \param settings The configuration string in the following form:
\verbatim
* <id>=<filter>[:<filter>]...[,<id>=<filter>[:<filter>]...]...
\endverbatim
* \return 1 on success, -1 otherwise
*/
int conf_parse_filter(char *settings)
{
/* make a copy since we are modifying it */
int len = strlen(settings);
if (len==0) return 1;
char *strc = (char *)pkg_malloc(len+1);
if (strc == NULL) {
PKG_MEM_ERROR;
return -1;
}
memcpy(strc, settings, len+1);
remove_spaces(strc);
char *set_p = strc;
char *token = NULL;
while ((token = strsep(&set_p, ","))) { /* iterate through list of settings */
char *id_str = strsep(&token, "=");
int id = conf_str2id(id_str);
if (id<0) {
LM_ERR("cannot parse id '%s'.\n", id_str);
pkg_free(strc);
return -1;
}
if (update_filter(id, token) < 0) {
LM_ERR("cannot extract filters.\n");
pkg_free(strc);
return -1;
}
}
pkg_free(strc);
return 1;
}
/*!
* \brief Parses a configuration string for proxy settings
* Parses a configuration string for proxy settings and
* updates the configuration structure.
* \param settings: The configuration string in the following form:
\verbatim
* <id>=<host>:<port>[,<id>=<host>:<port>]...
\endverbatim
* \return: 1 on success, -1 otherwise
*/
int conf_parse_proxy(char *settings)
{
/* make a copy since we are modifying it */
int len = strlen(settings);
if (len==0) return 1;
char *strc = (char *)pkg_malloc(len+1);
if (strc == NULL) {
PKG_MEM_ERROR;
return -1;
}
memcpy(strc, settings, len+1);
remove_spaces(strc);
char *set_p = strc;
char *token = NULL;
while ((token = strsep(&set_p, ","))) { /* iterate through list of settings */
char *id_str = strsep(&token, "=");
int id = conf_str2id(id_str);
if (id<0) {
LM_ERR("cannot parse id '%s'.\n", id_str);
pkg_free(strc);
return -1;
}
char *host = strsep(&token, ":");
/* got all data for one setting -> update configuration now */
if (update_proxy(id, host, token) < 0) {
LM_ERR("cannot update proxy.\n");
pkg_free(strc);
return -1;
}
}
pkg_free(strc);
return 1;
}
/*!
* \brief Checks if method string is in filter_methods
* \param id use configuration with this ID when checking
* \param method method string to be searched for
* \param method_len length of method string
* \return 1 if method is found in filter_methods, 0 otherwise
*/
static int filter_methods_contains_request(int id, char *method, int method_len)
{
char *p = fwd_settings[id].filter_methods;
while (p != NULL) {
if (strncmp(p, method, method_len) == 0) {
return 1;
}
p = strchr(p, ':');
if (p != NULL) p++;
}
return 0;
}
/*!
* \brief Checks forwarding is needed
* \param msg the SIP message to be forwarded
* \param id use configuration with this ID when checking
* \return pointer to proxy structure of destination if forwarding is needed, NULL otherwise
*/
struct proxy_l *conf_needs_forward(struct sip_msg *msg, int id)
{
if ((msg == NULL) || (fwd_settings[id].active == 0)) {
return NULL;
}
if (msg->first_line.type == SIP_REPLY) {
if (fwd_settings[id].sfilter&sfilter_mask[sfidx_reply]) {
return fwd_settings[id].proxy;
}
}
if (msg->first_line.type == SIP_REQUEST) {
if (fwd_settings[id].sfilter&sfilter_mask[sfidx_request]) {
return fwd_settings[id].proxy;
}
if (filter_methods_contains_request(id, msg->first_line.u.request.method.s, msg->first_line.u.request.method.len) > 0) {
return fwd_settings[id].proxy;
}
}
return NULL;
}
/*!
* \brief Initialize configuration
* \param max_id number of configuration statements
* \return 0 on success, -1 on failure
*/
int conf_init(int max_id)
{
/* allocate and initialize memory for configuration */
fwd_settings = shm_malloc(sizeof(struct fwd_setting)*(max_id+1));
if (fwd_settings == NULL) {
SHM_MEM_ERROR;
return -1;
}
memset(fwd_settings, 0, sizeof(struct fwd_setting)*(max_id+1));
fwd_max_id = max_id;
return 0;
}
/*!
* \brief Destroy configuration
*/
void conf_destroy(void)
{
int id;
if (fwd_settings) {
for (id=0; id<=fwd_max_id; id++) {
fwd_settings[id].active = 0;
if (fwd_settings[id].proxy) {
if (fwd_settings[id].proxy->name.s) {
shm_free(fwd_settings[id].proxy->name.s);
}
free_shm_proxy(fwd_settings[id].proxy);
shm_free(fwd_settings[id].proxy);
}
}
shm_free(fwd_settings);
}
}
|