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 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609
|
/* $Id: mediaproxy.c,v 1.10 2006/03/24 18:04:56 dan_pascu Exp $
*
* Copyright (C) 2004 Dan Pascu
*
* This file is part of openser, a free SIP server.
*
* openser 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
*
* openser 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
// TODO
//
// - make the asymmetric files use CFG_DIR
// - find a way to install the config files with make install
#include "../../sr_module.h"
#include "../../dprint.h"
#include "../../str.h"
#include "../../error.h"
#include "../../data_lump.h"
#include "../../forward.h"
#include "../../mem/mem.h"
#include "../../resolve.h"
#include "../../timer.h"
#include "../../ut.h"
#include "../../parser/parse_from.h"
#include "../../parser/parse_to.h"
#include "../../parser/parse_uri.h"
#include "../../msg_translator.h"
#include "../registrar/sip_msg.h"
#include "../usrloc/usrloc.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <regex.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/un.h>
MODULE_VERSION
// Although `AF_LOCAL' is mandated by POSIX.1g, `AF_UNIX' is portable to
// more systems. `AF_UNIX' was the traditional name stemming from BSD, so
// even most POSIX systems support it. It is also the name of choice in
// the Unix98 specification. So if there's no AF_LOCAL fallback to AF_UNIX
#ifndef AF_LOCAL
# define AF_LOCAL AF_UNIX
#endif
#define min(x, y) (((x) < (y)) ? (x) : (y))
#define isAnyAddress(adr) ((adr).len==7 && memcmp("0.0.0.0", (adr).s, 7)==0)
typedef int Bool;
#define True 1
#define False 0
typedef int (*CheckLocalPartyProc)(struct sip_msg* msg, char* s1, char* s2);
typedef Bool (*NatTestProc)(struct sip_msg* msg);
typedef enum {
NTNone=0,
NTPrivateContact=1,
NTSourceAddress=2,
NTPrivateVia=4
} NatTestType;
typedef enum {
STUnknown=0,
STAudio,
STVideo,
STAudioVideo
} StreamType;
typedef struct {
const char *name;
uint32_t address;
uint32_t mask;
} NetInfo;
typedef struct {
str ip;
str port;
str type; // stream type (`audio', `video', ...)
int localIP; // true if the IP is locally defined inside this media stream
} StreamInfo;
typedef struct {
NatTestType test;
NatTestProc proc;
} NatTest;
typedef struct {
char *file; // the file which lists the asymmetric clients
long timestamp; // for checking if it was modified
regex_t **clients; // the asymmetric clients regular expressions
int size; // size of array above
int count; // how many clients are in array above
} AsymmetricClients;
/* Function prototypes */
static int ClientNatTest(struct sip_msg *msg, char *str1, char *str2);
static int FixContact(struct sip_msg *msg, char *str1, char *str2);
static int UseMediaProxy(struct sip_msg *msg, char *str1, char *str2);
static int EndMediaSession(struct sip_msg *msg, char *str1, char *str2);
static int mod_init(void);
static int fixstring2int(void **param, int param_count);
static Bool testPrivateContact(struct sip_msg* msg);
static Bool testSourceAddress(struct sip_msg* msg);
static Bool testPrivateVia(struct sip_msg* msg);
/* Local global variables */
static char *mediaproxySocket = "/var/run/proxydispatcher.sock";
static int natpingInterval = 60; // 60 seconds
static usrloc_api_t userLocation;
static AsymmetricClients sipAsymmetrics = {
"/etc/openser/sip-asymmetric-clients",
//CFG_DIR"/sip-asymmetric-clients",
0,
NULL,
0,
0
};
static AsymmetricClients rtpAsymmetrics = {
"/etc/openser/rtp-asymmetric-clients",
0,
NULL,
0,
0
};
static CheckLocalPartyProc isFromLocal;
//static CheckLocalPartyProc isToLocal;
static CheckLocalPartyProc isDestinationLocal;
NetInfo rfc1918nets[] = {
{"10.0.0.0", 0x0a000000UL, 0xff000000UL},
{"172.16.0.0", 0xac100000UL, 0xfff00000UL},
{"192.168.0.0", 0xc0a80000UL, 0xffff0000UL},
{NULL, 0UL, 0UL}
};
NatTest natTests[] = {
{NTPrivateContact, testPrivateContact},
{NTSourceAddress, testSourceAddress},
{NTPrivateVia, testPrivateVia},
{NTNone, NULL}
};
static cmd_export_t commands[] = {
{"fix_contact", FixContact, 0, 0,
REQUEST_ROUTE | ONREPLY_ROUTE | BRANCH_ROUTE },
{"use_media_proxy", UseMediaProxy, 0, 0,
REQUEST_ROUTE | ONREPLY_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE},
{"end_media_session", EndMediaSession, 0, 0,
REQUEST_ROUTE | ONREPLY_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE},
{"client_nat_test", ClientNatTest, 1, fixstring2int,
REQUEST_ROUTE | ONREPLY_ROUTE | FAILURE_ROUTE | BRANCH_ROUTE},
{0, 0, 0, 0, 0}
};
static param_export_t parameters[] = {
{"mediaproxy_socket", STR_PARAM, &mediaproxySocket},
{"sip_asymmetrics", STR_PARAM, &(sipAsymmetrics.file)},
{"rtp_asymmetrics", STR_PARAM, &(rtpAsymmetrics.file)},
{"natping_interval", INT_PARAM, &natpingInterval},
{0, 0, 0}
};
struct module_exports exports = {
"mediaproxy", // module name
commands, // module exported functions
parameters, // module exported parameters
NULL, // exported statistics
mod_init, // module init (before any kid is created. kids will inherit)
NULL, // reply processing
NULL, // destroy function
NULL // child_init
};
/* Helper functions */
// Functions dealing with strings
/*
* strfind() finds the start of the first occurrence of the substring needle
* of length nlen in the memory area haystack of length len.
*/
static void*
strfind(const void *haystack, size_t len, const void *needle, size_t nlen)
{
char *sp;
/* Sanity check */
if(!(haystack && needle && nlen && len>=nlen))
return NULL;
for (sp = (char*)haystack; sp <= (char*)haystack + len - nlen; sp++) {
if (*sp == *(char*)needle && memcmp(sp, needle, nlen)==0) {
return sp;
}
}
return NULL;
}
/*
* strcasefind() finds the start of the first occurrence of the substring
* needle of length nlen in the memory area haystack of length len by doing
* a case insensitive search
*/
static void*
strcasefind(const char *haystack, size_t len, const char *needle, size_t nlen)
{
char *sp;
/* Sanity check */
if(!(haystack && needle && nlen && len>=nlen))
return NULL;
for (sp = (char*)haystack; sp <= (char*)haystack + len - nlen; sp++) {
if (tolower(*sp) == tolower(*(char*)needle) &&
strncasecmp(sp, needle, nlen)==0) {
return sp;
}
}
return NULL;
}
/* returns string with whitespace trimmed from left end */
static inline void
ltrim(str *string)
{
while (string->len>0 && isspace((int)*(string->s))) {
string->len--;
string->s++;
}
}
/* returns string with whitespace trimmed from right end */
static inline void
rtrim(str *string)
{
char *ptr;
ptr = string->s + string->len - 1;
while (string->len>0 && (*ptr==0 || isspace((int)*ptr))) {
string->len--;
ptr--;
}
}
/* returns string with whitespace trimmed from both ends */
static inline void
trim(str *string)
{
ltrim(string);
rtrim(string);
}
/* returns a pointer to first CR or LF char found or the end of string */
static char*
findendline(char *string, int len)
{
char *ptr = string;
while(ptr - string < len && *ptr != '\n' && *ptr != '\r')
ptr++;
return ptr;
}
static int
strtoint(str *data)
{
long int result;
char c;
// hack to avoid copying the string
c = data->s[data->len];
data->s[data->len] = 0;
result = strtol(data->s, NULL, 10);
data->s[data->len] = c;
return (int)result;
}
/* Free the returned string with pkg_free() after you've done with it */
static char*
encodeQuopri(str buf)
{
char *result;
int i, j;
char c;
result = pkg_malloc(buf.len*3+1);
if (!result) {
LOG(L_ERR, "error: mediaproxy/encodeQuopri(): out of memory\n");
return NULL;
}
for (i=0, j=0; i<buf.len; i++) {
c = buf.s[i];
if ((c>0x20 && c<0x7f && c!='=') || c=='\n' || c=='\r') {
result[j++] = c;
} else {
result[j++] = '=';
sprintf(&result[j], "%02X", (c & 0xff));
j += 2;
}
}
result[j] = 0;
return result;
}
/* Find a line in str `block' that starts with `start'. */
static char*
findLineStartingWith(str *block, char *start, int ignoreCase)
{
char *ptr, *bend;
str zone;
int tlen;
bend = block->s + block->len;
tlen = strlen(start);
ptr = NULL;
for (zone = *block; zone.len > 0; zone.len = bend - zone.s) {
if (ignoreCase)
ptr = strcasefind(zone.s, zone.len, start, tlen);
else
ptr = strfind(zone.s, zone.len, start, tlen);
if (!ptr || ptr==zone.s || ptr[-1]=='\n' || ptr[-1]=='\r')
break;
zone.s = ptr + tlen;
}
return ptr;
}
/* get up to `limit' whitespace separated tokens from `char *string' */
static int
getTokens(char *string, str *tokens, int limit)
{
int i, len, size;
char *ptr;
if (!string) {
return 0;
}
len = strlen(string);
for (ptr=string, i=0; i<limit && len>0; i++) {
size = strspn(ptr, " \t\n\r");
ptr += size;
len -= size;
if (len <= 0)
break;
size = strcspn(ptr, " \t\n\r");
if (size==0)
break;
tokens[i].s = ptr;
tokens[i].len = size;
ptr += size;
len -= size;
}
return i;
}
/* get up to `limit' whitespace separated tokens from `str *string' */
static int
getStrTokens(str *string, str *tokens, int limit)
{
int count;
char c;
if (!string || !string->s) {
return 0;
}
c = string->s[string->len];
string->s[string->len] = 0;
count = getTokens(string->s, tokens, limit);
string->s[string->len] = c;
return count;
}
/* Functions to extract the info we need from the SIP/SDP message */
/* Extract Call-ID value. */
static Bool
getCallId(struct sip_msg* msg, str *cid)
{
if (msg->callid == NULL) {
if (parse_headers(msg, HDR_CALLID_F, 0) == -1) {
return False;
}
if (msg->callid == NULL) {
return False;
}
}
*cid = msg->callid->body;
trim(cid);
return True;
}
/* Get caller domain */
static str
getFromDomain(struct sip_msg* msg)
{
static char buf[16] = "unknown"; // buf is here for a reason. don't
static str notfound = {buf, 7}; // use the constant string directly!
static struct sip_uri puri;
str uri;
if (parse_from_header(msg) < 0) {
LOG(L_ERR, "error: mediaproxy/getFromDomain(): error parsing `From' header\n");
return notfound;
}
uri = get_from(msg)->uri;
if (parse_uri(uri.s, uri.len, &puri) < 0) {
LOG(L_ERR, "error: mediaproxy/getFromDomain(): error parsing `From' URI\n");
return notfound;
} else if (puri.host.len == 0) {
return notfound;
}
return puri.host;
}
/* Get called domain */
static str
getToDomain(struct sip_msg* msg)
{
static char buf[16] = "unknown"; // buf is here for a reason. don't
static str notfound = {buf, 7}; // use the constant string directly!
static struct sip_uri puri;
str uri;
uri = get_to(msg)->uri;
if (parse_uri(uri.s, uri.len, &puri) < 0) {
LOG(L_ERR, "error: mediaproxy/getToDomain(): error parsing `To' URI\n");
return notfound;
} else if (puri.host.len == 0) {
return notfound;
}
return puri.host;
}
/* Get destination domain */
// This function only works when called for a request although it's more
// reliable than the getToDomain function.
static str
getDestinationDomain(struct sip_msg* msg)
{
static char buf[16] = "unknown"; // buf is here for a reason. don't
static str notfound = {buf, 7}; // use the constant string directly!
if (parse_sip_msg_uri(msg) < 0) {
LOG(L_ERR, "error: mediaproxy/getDestinationDomain(): error parsing destination URI\n");
return notfound;
} else if (msg->parsed_uri.host.len==0) {
return notfound;
}
return msg->parsed_uri.host;
}
/* Get From tag */
static str
getFromAddress(struct sip_msg *msg)
{
static char buf[16] = "unknown"; // buf is here for a reason. don't
static str notfound = {buf, 7}; // use the constant string directly!
str uri;
char *ptr;
if (parse_from_header(msg)<0) {
LOG(L_ERR, "error: mediaproxy/getFromAddress(): error parsing From: field\n");
return notfound;
}
uri = get_from(msg)->uri;
if (uri.len == 0)
return notfound;
if (strncmp(uri.s, "sip:", 4)==0) {
uri.s += 4;
uri.len -= 4;
}
if ((ptr = strfind(uri.s, uri.len, ";", 1))!=NULL) {
uri.len = ptr - uri.s;
}
return uri;
}
/* Get To tag */
static str
getToAddress(struct sip_msg *msg)
{
static char buf[16] = "unknown"; // buf is here for a reason. don't
static str notfound = {buf, 7}; // use the constant string directly!
str uri;
char *ptr;
if (!msg->to) {
LOG(L_ERR, "error: mediaproxy/getToAddress(): missing To: field\n");
return notfound;
}
uri = get_to(msg)->uri;
if (uri.len == 0)
return notfound;
if (strncmp(uri.s, "sip:", 4)==0) {
uri.s += 4;
uri.len -= 4;
}
if ((ptr = strfind(uri.s, uri.len, ";", 1))!=NULL) {
uri.len = ptr - uri.s;
}
return uri;
}
/* Get From tag */
static str
getFromTag(struct sip_msg *msg)
{
static char buf[4] = ""; // buf is here for a reason. don't
static str notfound = {buf, 0}; // use the constant string directly!
str tag;
if (parse_from_header(msg)<0) {
LOG(L_ERR, "error: mediaproxy/getFromTag(): error parsing From: field\n");
return notfound;
}
tag = get_from(msg)->tag_value;
if (tag.len == 0)
return notfound;
return tag;
}
/* Get To tag */
static str
getToTag(struct sip_msg *msg)
{
static char buf[4] = ""; // buf is here for a reason. don't
static str notfound = {buf, 0}; // use the constant string directly!
str tag;
if (!msg->to) {
LOG(L_ERR, "error: mediaproxy/getToTag(): missing To: field\n");
return notfound;
}
tag = get_to(msg)->tag_value;
if (tag.len == 0)
return notfound;
return tag;
}
/* Extract User-Agent */
static str
getUserAgent(struct sip_msg* msg)
{
static char buf[16] = "unknown-agent"; // buf is here for a reason. don't
static str notfound = {buf, 13}; // use the constant string directly!
str block, server;
char *ptr;
if (parse_headers(msg, HDR_USERAGENT_F, 0)==0 && msg->user_agent &&
msg->user_agent->body.s && msg->user_agent->body.len>0) {
return msg->user_agent->body;
}
// If we can't find user-agent, look after the Server: field
// This is a temporary hack. Normally it should be extracted by ser
// (either as the Server field, or if User-Agent is missing in place
// of the User-Agent field)
block.s = msg->buf;
block.len = msg->len;
ptr = findLineStartingWith(&block, "Server:", True);
if (!ptr)
return notfound;
server.s = ptr + 7;
server.len = findendline(server.s, block.s+block.len-server.s) - server.s;
trim(&server);
if (server.len == 0)
return notfound;
return server;
}
// Get URI from the Contact: field.
static Bool
getContactURI(struct sip_msg* msg, struct sip_uri *uri, contact_t** _c)
{
if ((parse_headers(msg, HDR_CONTACT_F, 0) == -1) || !msg->contact)
return False;
if (!msg->contact->parsed && parse_contact(msg->contact) < 0) {
LOG(L_ERR, "error: mediaproxy/getContactURI(): cannot parse Contact header\n");
return False;
}
*_c = ((contact_body_t*)msg->contact->parsed)->contacts;
if (*_c == NULL) {
return False;
}
if (parse_uri((*_c)->uri.s, (*_c)->uri.len, uri) < 0 || uri->host.len <= 0) {
LOG(L_ERR, "error: mediaproxy/getContactURI(): cannot parse Contact URI\n");
return False;
}
return True;
}
// Functions to manipulate the SDP message body
static Bool
checkContentType(struct sip_msg *msg)
{
str type;
if (!msg->content_type) {
LOG(L_WARN, "warning: mediaproxy/checkContentType(): Content-Type "
"header missing! Let's assume the content is text/plain ;-)\n");
return True;
}
type = msg->content_type->body;
trim(&type);
if (strncasecmp(type.s, "application/sdp", 15) != 0) {
LOG(L_ERR, "error: mediaproxy/checkContentType(): invalid Content-Type "
"for SDP message\n");
return False;
}
if (!(isspace((int)type.s[15]) || type.s[15] == ';' || type.s[15] == 0)) {
LOG(L_ERR,"error: mediaproxy/checkContentType(): invalid character "
"after Content-Type!\n");
return False;
}
return True;
}
// Get the SDP message from SIP message and check it's Content-Type
// Return values:
// 1 - success
// -1 - error in getting body or invalid content type
// -2 - empty message
static int
getSDPMessage(struct sip_msg *msg, str *sdp)
{
sdp->s = get_body(msg);
if (sdp->s==NULL) {
LOG(L_ERR, "error: mediaproxy/getSDPMessage(): cannot get body from message\n");
return -1;
}
sdp->len = msg->buf + msg->len - sdp->s;
if (sdp->len == 0)
return -2;
if (!checkContentType(msg)) {
LOG(L_ERR, "error: mediaproxy/getSDPMessage(): content type is not `application/sdp'\n");
return -1;
}
return 1;
}
// will return the ip address present in a `c=' line in the given block
// returns: -1 on error, 0 if not found, 1 if found
static int
getMediaIPFromBlock(str *block, str *mediaip)
{
str tokens[3], zone;
char *ptr;
int count;
ptr = findLineStartingWith(block, "c=", False);
if (!ptr) {
mediaip->s = NULL;
mediaip->len = 0;
return 0;
}
zone.s = ptr + 2;
zone.len = findendline(zone.s, block->s + block->len - zone.s) - zone.s;
count = getStrTokens(&zone, tokens, 3);
if (count != 3) {
LOG(L_ERR, "error: mediaproxy/getMediaIPFromBlock(): invalid `c=' "
"line in SDP body\n");
return -1;
}
// can also check if tokens[1] == 'IP4'
*mediaip = tokens[2];
return 1;
}
// Get the session-level media IP defined by the SDP message
static Bool
getSessionLevelMediaIP(str *sdp, str *mediaip)
{
str block;
char *ptr;
// session IP can be found from the beginning up to the first media block
ptr = findLineStartingWith(sdp, "m=", False);
if (ptr) {
block.s = sdp->s;
block.len = ptr - block.s;
} else {
block = *sdp;
}
if (getMediaIPFromBlock(&block, mediaip) == -1) {
LOG(L_ERR, "error: mediaproxy/getSessionLevelMediaIP(): parse error "
"while getting session-level media IP from SDP body\n");
return False;
}
// it's not an error to be missing. it can be locally defined
// by each media stream. thus we return true even if not found
return True;
}
// will get all media streams
static int
getMediaStreams(str *sdp, str *sessionIP, StreamInfo *streams, int limit)
{
str tokens[2], block, zone;
char *ptr, *sdpEnd;
int i, count, streamCount, result;
sdpEnd = sdp->s + sdp->len;
for (i=0, block=*sdp; i<limit; i++) {
ptr = findLineStartingWith(&block, "m=", False);
if (!ptr)
break;
zone.s = ptr + 2;
zone.len = findendline(zone.s, sdpEnd - zone.s) - zone.s;
count = getStrTokens(&zone, tokens, 2);
if (count != 2) {
LOG(L_ERR, "error: mediaproxy/getMediaStreams(): invalid `m=' "
"line in SDP body\n");
return -1;
}
streams[i].type = tokens[0];
streams[i].port = tokens[1];
block.s = zone.s + zone.len;
block.len = sdpEnd - block.s;
}
streamCount = i;
for (i=0; i<streamCount; i++) {
block.s = streams[i].port.s;
if (i < streamCount-1)
block.len = streams[i+1].port.s - block.s;
else
block.len = sdpEnd - block.s;
result = getMediaIPFromBlock(&block, &(streams[i].ip));
if (result == -1) {
LOG(L_ERR, "error: mediaproxy/getMediaStreams(): parse error in "
"getting the contact IP for the media stream nr. %d\n", i+1);
return -1;
} else if (result == 0) {
if (sessionIP->s == NULL) {
LOG(L_ERR, "error: mediaproxy/getMediaStreams(): media stream "
"doesn't define a contact IP and the session-level IP "
"is missing\n");
return -1;
}
streams[i].ip = *sessionIP;
streams[i].localIP = 0;
} else {
streams[i].localIP = 1;
}
}
return streamCount;
}
static Bool
replaceElement(struct sip_msg *msg, str *oldElem, str *newElem)
{
struct lump* anchor;
char *buf;
if (newElem->len==oldElem->len &&
memcmp(newElem->s, oldElem->s, newElem->len)==0) {
return True;
}
buf = pkg_malloc(newElem->len);
if (!buf) {
LOG(L_ERR, "error: mediaproxy/replaceElement(): out of memory\n");
return False;
}
anchor = del_lump(msg, oldElem->s - msg->buf, oldElem->len, 0);
if (!anchor) {
LOG(L_ERR, "error: mediaproxy/replaceElement(): failed to delete old element\n");
pkg_free(buf);
return False;
}
memcpy(buf, newElem->s, newElem->len);
if (insert_new_lump_after(anchor, buf, newElem->len, 0)==0) {
LOG(L_ERR, "error: mediaproxy/replaceElement(): failed to insert new element\n");
pkg_free(buf);
return False;
}
return True;
}
// Functions dealing with the external mediaproxy helper
static inline size_t
uwrite(int fd, const void *buf, size_t count)
{
int len;
do
len = write(fd, buf, count);
while (len == -1 && errno == EINTR);
return len;
}
static inline size_t
uread(int fd, void *buf, size_t count)
{
int len;
do
len = read(fd, buf, count);
while (len == -1 && errno == EINTR);
return len;
}
static inline size_t
readall(int fd, void *buf, size_t count)
{
int len, total;
for (len=0, total=0; count-total>0; total+=len) {
len = uread(fd, (char*)buf+total, count-total);
if (len == -1) {
return -1;
}
if (len == 0) {
break;
}
}
return total;
}
static char*
sendMediaproxyCommand(char *command)
{
struct sockaddr_un addr;
int smpSocket, len;
static char buf[1024];
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_LOCAL;
strncpy(addr.sun_path, mediaproxySocket, sizeof(addr.sun_path) - 1);
#ifdef HAVE_SOCKADDR_SA_LEN
addr.sun_len = strlen(addr.sun_path);
#endif
smpSocket = socket(AF_LOCAL, SOCK_STREAM, 0);
if (smpSocket < 0) {
LOG(L_ERR, "error: mediaproxy/sendMediaproxyCommand(): can't create socket\n");
return NULL;
}
if (connect(smpSocket, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
close(smpSocket);
LOG(L_ERR, "error: mediaproxy/sendMediaproxyCommand(): can't connect to MediaProxy\n");
return NULL;
}
len = uwrite(smpSocket, command, strlen(command));
if (len <= 0) {
close(smpSocket);
LOG(L_ERR, "error: mediaproxy/sendMediaproxyCommand(): can't send command to MediaProxy\n");
return NULL;
}
len = readall(smpSocket, buf, sizeof(buf)-1);
close(smpSocket);
if (len < 0) {
LOG(L_ERR, "error: mediaproxy/sendMediaproxyCommand(): can't read reply from MediaProxy\n");
return NULL;
}
buf[len] = 0;
return buf;
}
// Miscellaneous helper functions
/* Test if IP in `address' belongs to a RFC1918 network */
static inline int
rfc1918address(str *address)
{
struct in_addr inaddr;
uint32_t netaddr;
int i, result;
char c;
c = address->s[address->len];
address->s[address->len] = 0;
result = inet_aton(address->s, &inaddr);
address->s[address->len] = c;
if (result==0)
return -1; /* invalid address to test */
netaddr = ntohl(inaddr.s_addr);
for (i=0; rfc1918nets[i].name!=NULL; i++) {
if ((netaddr & rfc1918nets[i].mask)==rfc1918nets[i].address) {
return 1;
}
}
return 0;
}
#define isPrivateAddress(x) (rfc1918address(x)==1 ? 1 : 0)
// test for a public address is more complex (also need to test for
// address not in 0.0.0.0/8, 127.0.0.0/8, 224.0.0.0/4).
// #define isPublicAddress(x) (rfc1918address(x)==0 ? 1 : 0)
// Check if the requested asymmetrics file has changed and reload it if needed
static void
checkAsymmetricFile(AsymmetricClients *aptr)
{
char buf[512], errbuf[256], *which;
regex_t *re, **regs;
int i, size, code;
Bool firstTime = False;
struct stat statbuf;
FILE *file;
str line;
if (stat(aptr->file, &statbuf) < 0)
return; // ignore missing file
if (statbuf.st_mtime <= aptr->timestamp)
return; // not changed
// now we have work to do
which = (aptr == &sipAsymmetrics ? "SIP" : "RTP");
if (!aptr->clients) {
// if we are here the first time allocate memory to hold the regexps
// initial size of array
// (hopefully not reached so we won't need to realloc)
size = 32;
aptr->clients = (regex_t**)pkg_malloc(size*sizeof(regex_t**));
if (!aptr->clients) {
LOG(L_WARN, "warning: mediaproxy/checkAsymmetricFile() cannot "
"allocate memory for the %s asymmetric client list. "
"%s asymmetric clients will not be handled properly.\n",
which, which);
return; // ignore as it is not fatal
}
aptr->size = size;
aptr->count = 0;
firstTime = True;
} else {
// else clean old stuff
for (i=0; i<aptr->count; i++) {
regfree(aptr->clients[i]);
pkg_free(aptr->clients[i]);
aptr->clients[i] = NULL;
}
aptr->count = 0;
}
// read new
file = fopen(aptr->file, "r");
i = 0; // this will count on which line are we in the file
while (!feof(file)) {
if (!fgets(buf, 512, file))
break;
i++;
line.s = buf;
line.len = strlen(buf);
trim(&line);
if (line.len == 0 || line.s[0] == '#')
continue; // comment or empty line. ignore
line.s[line.len] = 0;
re = (regex_t*)pkg_malloc(sizeof(regex_t));
if (!re) {
LOG(L_WARN, "warning: mediaproxy/checkAsymmetricFile(): "
"cannot allocate memory for all the %s asymmetric "
"clients listed in file. Some of them will not be "
"handled properly.\n", which);
break;
}
code = regcomp(re, line.s, REG_EXTENDED|REG_ICASE|REG_NOSUB);
if (code == 0) {
if (aptr->count+1 > aptr->size) {
size = aptr->size * 2;
regs = aptr->clients;
regs = (regex_t**)pkg_realloc(regs, size*sizeof(regex_t**));
if (!regs) {
LOG(L_WARN, "warning: mediaproxy/checkAsymmetricFile(): "
"cannot allocate memory for all the %s asymmetric "
"clients listed in file. Some of them will not be "
"handled properly.\n", which);
break;
}
aptr->clients = regs;
aptr->size = size;
}
aptr->clients[aptr->count] = re;
aptr->count++;
} else {
regerror(code, re, errbuf, 256);
LOG(L_WARN, "warning: mediaproxy/checkAsymmetricFile(): cannot "
"compile line %d of the %s asymmetric clients file into a "
"regular expression (will be ignored): %s", i, which, errbuf);
pkg_free(re);
}
}
aptr->timestamp = statbuf.st_mtime;
LOG(L_INFO, "info: mediaproxy: %sloaded %s asymmetric clients file "
"containing %d entr%s.\n", firstTime ? "" : "re",
which, aptr->count, aptr->count==1 ? "y" : "ies");
}
//
// This is a hack. Until a I find a better way to allow all children to update
// the asymmetric client list when the files change on disk, it stays as it is
// A timer registered from mod_init() only runs in one of the ser processes
// and the others will always see the file that was read at startup.
//
#include <time.h>
#define CHECK_INTERVAL 5
static void
periodicAsymmetricsCheck(void)
{
static time_t last = 0;
time_t now;
// this is not guaranteed to run at every CHECK_INTERVAL.
// it is only guaranteed that the files won't be checked more often.
now = time(NULL);
if (now > last + CHECK_INTERVAL) {
checkAsymmetricFile(&sipAsymmetrics);
checkAsymmetricFile(&rtpAsymmetrics);
last = now;
}
}
static Bool
isSIPAsymmetric(str userAgent)
{
int i, code;
char c;
periodicAsymmetricsCheck();
if (!sipAsymmetrics.clients || sipAsymmetrics.count==0)
return False;
c = userAgent.s[userAgent.len];
userAgent.s[userAgent.len] = 0;
for (i=0; i<sipAsymmetrics.count; i++) {
code = regexec(sipAsymmetrics.clients[i], userAgent.s, 0, NULL, 0);
if (code == 0) {
userAgent.s[userAgent.len] = c;
return True;
} else if (code != REG_NOMATCH) {
char errbuf[256];
regerror(code, sipAsymmetrics.clients[i], errbuf, 256);
LOG(L_WARN, "warning: mediaproxy/isSIPAsymmetric() failed to "
"match regexp: %s\n", errbuf);
}
}
userAgent.s[userAgent.len] = c;
return False;
}
static Bool
isRTPAsymmetric(str userAgent)
{
int i, code;
char c;
periodicAsymmetricsCheck();
if (!rtpAsymmetrics.clients || rtpAsymmetrics.count==0)
return False;
c = userAgent.s[userAgent.len];
userAgent.s[userAgent.len] = 0;
for (i=0; i<rtpAsymmetrics.count; i++) {
code = regexec(rtpAsymmetrics.clients[i], userAgent.s, 0, NULL, 0);
if (code == 0) {
userAgent.s[userAgent.len] = c;
return True;
} else if (code != REG_NOMATCH) {
char errbuf[256];
regerror(code, rtpAsymmetrics.clients[i], errbuf, 256);
LOG(L_WARN, "warning: mediaproxy/isRTPAsymmetric() failed to "
"match regexp: %s\n", errbuf);
}
}
userAgent.s[userAgent.len] = c;
return False;
}
// NAT tests
/* tests if address of signaling is different from address in 1st Via field */
static Bool
testSourceAddress(struct sip_msg* msg)
{
Bool diffIP, diffPort;
int via1Port;
diffIP = received_test(msg);
if (isSIPAsymmetric(getUserAgent(msg))) {
// ignore port test for asymmetric clients (it's always different)
diffPort = False;
} else {
via1Port = (msg->via1->port ? msg->via1->port : SIP_PORT);
diffPort = (msg->rcv.src_port != via1Port);
}
return (diffIP || diffPort);
}
/* tests if Contact field contains a private IP address as defined in RFC1918 */
static Bool
testPrivateContact(struct sip_msg* msg)
{
struct sip_uri uri;
contact_t* contact;
if (!getContactURI(msg, &uri, &contact))
return False;
return isPrivateAddress(&(uri.host));
}
/* tests if top Via field contains a private IP address as defined in RFC1918 */
static Bool
testPrivateVia(struct sip_msg* msg)
{
return isPrivateAddress(&(msg->via1->host));
}
#include "functions.h"
/* The public functions that are exported by this module */
static int
ClientNatTest(struct sip_msg* msg, char* str1, char* str2)
{
int tests, i;
tests = (int)(long)str1;
for (i=0; natTests[i].test!=NTNone; i++) {
if ((tests & natTests[i].test)!=0 && natTests[i].proc(msg)) {
return 1;
}
}
return -1; // all failed
}
static int
EndMediaSession(struct sip_msg* msg, char* str1, char* str2)
{
char *command, *result;
str callId;
if (!getCallId(msg, &callId)) {
LOG(L_ERR, "error: end_media_session(): can't get Call-Id\n");
return -1;
}
command = pkg_malloc(callId.len + 20);
if (command == NULL) {
LOG(L_ERR, "error: end_media_session(): out of memory\n");
return -1;
}
sprintf(command, "delete %.*s info=\n", callId.len, callId.s);
result = sendMediaproxyCommand(command);
pkg_free(command);
return result==NULL ? -1 : 1;
}
#define MSG_UNKNOWN 0
#define MSG_INVITE 1
#define MSG_ACK 2
#define MSG_REPLY 3
static int
UseMediaProxy(struct sip_msg* msg, char* str1, char* str2)
{
str sdp, sessionIP, callId, fromDomain, toDomain, userAgent, tokens[64];
str fromAddr, toAddr, fromTag, toTag;
char *clientIP, *ptr, *command, *result, *agent, *fromType, *toType, *info;
int streamCount, i, port, count, portCount, cmdlen, infolen, status, type;
StreamInfo streams[64], stream;
Bool request;
if (msg->first_line.type == SIP_REQUEST) {
if (msg->first_line.u.request.method_value == METHOD_INVITE)
type = MSG_INVITE;
else if (msg->first_line.u.request.method_value == METHOD_ACK)
type = MSG_ACK;
else
type = MSG_UNKNOWN;
} else if (msg->first_line.type == SIP_REPLY) {
type = MSG_REPLY;
} else {
type = MSG_UNKNOWN;
}
if (type==MSG_INVITE || type==MSG_ACK) {
request = True;
} else if (type==MSG_REPLY) {
request = False;
} else {
return -1;
}
if (!getCallId(msg, &callId)) {
LOG(L_ERR, "error: use_media_proxy(): can't get Call-Id\n");
return -1;
}
status = getSDPMessage(msg, &sdp);
// status = -1 is error, -2 is missing SDP body
if (status < 0)
return status;
if (!getSessionLevelMediaIP(&sdp, &sessionIP)) {
LOG(L_ERR, "error: use_media_proxy(): error parsing the SDP message\n");
return -1;
}
streamCount = getMediaStreams(&sdp, &sessionIP, streams, 64);
if (streamCount == -1) {
LOG(L_ERR, "error: use_media_proxy(): can't extract media streams "
"from the SDP message\n");
return -1;
}
if (streamCount == 0) {
// there are no media streams. we have nothing to do.
return 1;
}
fromDomain = getFromDomain(msg);
fromType = (isFromLocal(msg, NULL, NULL)>0) ? "local" : "remote";
fromAddr = getFromAddress(msg);
toAddr = getToAddress(msg);
fromTag = getFromTag(msg);
toTag = getToTag(msg);
userAgent = getUserAgent(msg);
if (request) {
toDomain = getDestinationDomain(msg); // call only for requests
toType = (isDestinationLocal(msg, NULL, NULL)>0) ? "local" : "remote";
} else {
toDomain = getToDomain(msg);
toType = "unknown"; //there's no function to check if To domain is local
}
clientIP = ip_addr2a(&msg->rcv.src_ip);
infolen = fromAddr.len + toAddr.len + fromTag.len + toTag.len + 64;
cmdlen = callId.len + strlen(clientIP) + fromDomain.len + toDomain.len +
userAgent.len*3 + infolen + 128;
for (i=0; i<streamCount; i++) {
stream = streams[i];
cmdlen += stream.ip.len + stream.port.len + stream.type.len + 4;
}
command = pkg_malloc(cmdlen);
if (!command) {
LOG(L_ERR, "error: use_media_proxy(): out of memory\n");
return -1;
}
if (request)
count = sprintf(command, "request %.*s", callId.len, callId.s);
else
count = sprintf(command, "lookup %.*s", callId.len, callId.s);
for (i=0, ptr=command+count; i<streamCount; i++) {
char c = (i==0 ? ' ' : ',');
count = sprintf(ptr, "%c%.*s:%.*s:%.*s", c,
streams[i].ip.len, streams[i].ip.s,
streams[i].port.len, streams[i].port.s,
streams[i].type.len, streams[i].type.s);
ptr += count;
}
agent = encodeQuopri(userAgent);
if (!agent) {
LOG(L_ERR, "error: use_media_proxy(): out of memory\n");
pkg_free(command);
return -1;
}
info = pkg_malloc(infolen);
if (!info) {
LOG(L_ERR, "error: use_media_proxy(): out of memory\n");
pkg_free(command);
pkg_free(agent);
return -1;
}
sprintf(info, "from:%.*s,to:%.*s,fromtag:%.*s,totag:%.*s",
fromAddr.len, fromAddr.s, toAddr.len, toAddr.s,
fromTag.len, fromTag.s, toTag.len, toTag.s);
if (isRTPAsymmetric(userAgent)) {
strcat(info, ",asymmetric");
}
snprintf(ptr, command + cmdlen - ptr, " %s %.*s %s %.*s %s %s info=%s\n",
clientIP, fromDomain.len, fromDomain.s, fromType,
toDomain.len, toDomain.s, toType, agent, info);
pkg_free(info);
pkg_free(agent);
result = sendMediaproxyCommand(command);
pkg_free(command);
if (result == NULL)
return -1;
count = getTokens(result, tokens, sizeof(tokens)/sizeof(str));
if (count == 0) {
LOG(L_ERR, "error: use_media_proxy(): empty response from mediaproxy\n");
return -1;
} else if (count<streamCount+1) {
if (request) {
LOG(L_ERR, "error: use_media_proxy(): insufficient ports returned "
"from mediaproxy: got %d, expected %d\n", count-1, streamCount);
return -1;
} else {
LOG(L_WARN, "warning: use_media_proxy(): broken client. Called UA "
"added extra media stream(s) in the OK reply\n");
}
}
if (sessionIP.s && !isAnyAddress(sessionIP)) {
if (!replaceElement(msg, &sessionIP, &tokens[0])) {
LOG(L_ERR, "error: use_media_proxy(): failed to replace "
"session-level media IP in SDP body\n");
return -1;
}
}
portCount = min(count-1, streamCount);
for (i=0; i<portCount; i++) {
// check. is this really necessary?
port = strtoint(&tokens[i+1]);
if (port <= 0 || port > 65535) {
LOG(L_ERR, "error: use_media_proxy(): invalid port returned "
"by mediaproxy: %.*s\n", tokens[i+1].len, tokens[i+1].s);
//return -1;
continue;
}
if (streams[i].port.len!=1 || streams[i].port.s[0]!='0') {
if (!replaceElement(msg, &(streams[i].port), &tokens[i+1])) {
LOG(L_ERR, "error: use_media_proxy(): failed to replace "
"port in media stream nr. %d\n", i+1);
return -1;
}
}
if (streams[i].localIP && !isAnyAddress(streams[i].ip)) {
if (!replaceElement(msg, &(streams[i].ip), &tokens[0])) {
LOG(L_ERR, "error: use_media_proxy(): failed to replace "
"IP address in media stream nr. %d\n", i+1);
return -1;
}
}
}
return 1;
}
/* Module management: initialization/destroy/function-parameter-fixing/... */
static int
mod_init(void)
{
bind_usrloc_t ul_bind_usrloc;
isFromLocal = (CheckLocalPartyProc)find_export("is_from_local", 0, 0);
isDestinationLocal = (CheckLocalPartyProc)find_export("is_uri_host_local", 0, 0);
if (!isFromLocal || !isDestinationLocal) {
LOG(L_ERR, "error: mediaproxy/mod_init(): can't find is_from_local "
"and/or is_uri_host_local functions. Check if domain.so is loaded\n");
return -1;
}
if (natpingInterval > 0) {
ul_bind_usrloc = (bind_usrloc_t)find_export("ul_bind_usrloc", 1, 0);
if (!ul_bind_usrloc) {
LOG(L_ERR, "error: mediaproxy/mod_init(): can't find the usrloc "
"module. Check if usrloc.so is loaded.\n");
return -1;
}
if (ul_bind_usrloc(&userLocation) < 0) {
LOG(L_ERR, "error: mediaproxy/mod_init(): can't access the usrloc module.\n");
return -1;
}
register_timer(pingClients, NULL, natpingInterval);
}
checkAsymmetricFile(&sipAsymmetrics);
checkAsymmetricFile(&rtpAsymmetrics);
// children won't benefit from this. figure another way
//register_timer(checkAsymmetricFiles, NULL, 5);
return 0;
}
// convert string parameter to integer for functions that expect an integer
static int
fixstring2int(void **param, int param_count)
{
unsigned long number;
int err;
if (param_count == 1) {
number = str2s(*param, strlen(*param), &err);
if (err == 0) {
pkg_free(*param);
*param = (void*)number;
return 0;
} else {
LOG(L_ERR, "error: mediaproxy/fixstring2int(): bad number `%s'\n",
(char*)(*param));
return E_CFG;
}
}
return 0;
}
|