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 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
|
/*
* $Id$
*
* Perl module for Kamailio
*
* Copyright (C) 2006 Collax GmbH
* (Bastian Friedrich <bastian.friedrich@collax.com>)
*
* 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
*
*/
#include <EXTERN.h>
#include <perl.h>
#include <XSUB.h>
#include <unistd.h>
#undef load_module
/* perl.h defines union semun */
#ifdef USE_SYSV_SEM
# undef _SEM_SEMUN_UNDEFINED
#endif
#include "../../sr_module.h"
#include "../../parser/msg_parser.h"
#include "../../parser/parse_uri.h"
#include "../../usr_avp.h"
#include "../../action.h"
#include "../../flags.h"
#include "../../pvar.h"
#include "../../dset.h"
#include "../../mem/mem.h"
#include "../../route_struct.h"
#include "../../qvalue.h"
#include "../../dprint.h"
extern int unsafemodfnc;
enum xs_uri_members {
XS_URI_USER = 0,
XS_URI_PASSWD,
XS_URI_HOST,
XS_URI_PORT,
XS_URI_PARAMS,
XS_URI_HEADERS,
XS_URI_TRANSPORT,
XS_URI_TTL,
XS_URI_USER_PARAM,
XS_URI_MADDR,
XS_URI_METHOD,
XS_URI_LR,
XS_URI_R2,
XS_URI_TRANSPORT_VAL,
XS_URI_TTL_VAL,
XS_URI_USER_PARAM_VAL,
XS_URI_MADDR_VAL,
XS_URI_METHOD_VAL,
XS_URI_LR_VAL,
XS_URI_R2_VAL
/* These members are no strings:
unsigned short port_no;
unsigned short proto; / * from transport * /
uri_type type; / * uri scheme */
};
/*
* Return the sip_msg struct referred to by perl reference sv
*/
struct sip_msg * sv2msg(SV *sv) {
struct sip_msg* m;
if (SvROK(sv)) {
sv = SvRV(sv);
if (SvIOK(sv)) {
m = INT2PTR(struct sip_msg*, SvIV(sv));
return m;
}
}
return NULL; /* In case of error above... */
}
struct sip_uri * sv2uri(SV *sv) {
struct sip_uri* u;
if (SvROK(sv)) {
sv = SvRV(sv);
if (SvIOK(sv)) {
u = INT2PTR(struct sip_uri*, SvIV(sv));
return u;
}
}
return NULL; /* In case of error above... */
}
struct action * sv2action(SV *sv) {
struct action* a;
if (SvROK(sv)) {
sv = SvRV(sv);
if (SvIOK(sv)) {
a = INT2PTR(struct action*, SvIV(sv));
return a;
}
}
return NULL; /* In case of error above... */
}
/*
* We have a private function for two reasons:
* a) Return SIP_INVALID even if type was sth different
* b) easy access
*/
inline static int getType(struct sip_msg *msg) {
int t = SIP_INVALID;
if (!msg) return SIP_INVALID;
switch ((msg->first_line).type) {
case SIP_REQUEST: t = SIP_REQUEST; break;
case SIP_REPLY: t = SIP_REPLY; break;
}
return t;
}
SV *getStringFromURI(SV *self, enum xs_uri_members what) {
struct sip_uri *myuri = sv2uri(self);
str *ret = NULL;
if (!myuri) {
LM_ERR("Invalid URI reference\n");
ret = NULL;
} else {
switch (what) {
case XS_URI_USER: ret = &(myuri->user);
break;
case XS_URI_HOST: ret = &(myuri->host);
break;
case XS_URI_PASSWD: ret = &(myuri->passwd);
break;
case XS_URI_PORT: ret = &(myuri->port);
break;
case XS_URI_PARAMS: ret = &(myuri->params);
break;
case XS_URI_HEADERS: ret = &(myuri->headers);
break;
case XS_URI_TRANSPORT: ret = &(myuri->transport);
break;
case XS_URI_TTL: ret = &(myuri->ttl);
break;
case XS_URI_USER_PARAM: ret = &(myuri->user_param);
break;
case XS_URI_MADDR: ret = &(myuri->maddr);
break;
case XS_URI_METHOD: ret = &(myuri->method);
break;
case XS_URI_LR: ret = &(myuri->lr);
break;
case XS_URI_R2: ret = &(myuri->r2);
break;
case XS_URI_TRANSPORT_VAL: ret = &(myuri->transport_val);
break;
case XS_URI_TTL_VAL: ret = &(myuri->ttl_val);
break;
case XS_URI_USER_PARAM_VAL: ret = &(myuri->user_param_val);
break;
case XS_URI_MADDR_VAL: ret = &(myuri->maddr_val);
break;
case XS_URI_METHOD_VAL: ret = &(myuri->method_val);
break;
case XS_URI_LR_VAL: ret = &(myuri->lr_val);
break;
case XS_URI_R2_VAL: ret = &(myuri->r2_val);
break;
default: LM_INFO("Unknown URI element"
" requested: %d\n", what);
break;
}
}
if ((ret) && (ret->len)) {
return sv_2mortal(newSVpv(ret->s, ret->len));
} else {
return &PL_sv_undef;
}
}
/*
* Calls an exported function. Parameters are copied and fixup'd.
*
* Return codes:
* -1 - Function not available (or other error).
* 1 - Function was called. Its return value is returned via the retval
* parameter.
*/
int moduleFunc(struct sip_msg *m, char *func,
char *param1, char *param2,
int *retval) {
sr31_cmd_export_t* exp_func_struct;
struct action *act;
unsigned mod_ver;
char *argv[2];
int argc = 0;
struct run_act_ctx ra_ctx;
if (!func) {
LM_ERR("moduleFunc called with null function name. Error.");
return -1;
}
if ((!param1) && param2) {
LM_ERR("moduleFunc called with parameter 1 UNSET and"
" parameter 2 SET. Error.");
return -1;
}
if (param1) {
argv[0] = (char *)pkg_malloc(strlen(param1)+1);
strcpy(argv[0], param1);
argc++;
} else {
argv[0] = NULL;
}
if (param2) {
argv[1] = (char *)pkg_malloc(strlen(param2)+1);
strcpy(argv[1], param2);
argc++;
} else {
argv[1] = NULL;
}
exp_func_struct = find_export_record(func, argc, 0, &mod_ver);
if (!exp_func_struct || mod_ver < 1) {
LM_ERR("function '%s' called, but not available.", func);
*retval = -1;
if (argv[0]) pkg_free(argv[0]);
if (argv[1]) pkg_free(argv[1]);
return -1;
}
act = mk_action(MODULE2_T, 4 /* number of (type, value) pairs */,
MODEXP_ST, exp_func_struct, /* function */
NUMBER_ST, 2, /* parameter number */
STRING_ST, argv[0], /* param. 1 */
STRING_ST, argv[1] /* param. 2 */
);
if (!act) {
LM_ERR("action structure could not be created. Error.");
if (argv[0]) pkg_free(argv[0]);
if (argv[1]) pkg_free(argv[1]);
return -1;
}
if (exp_func_struct->fixup) {
if (!unsafemodfnc) {
LM_ERR("Module function '%s' is unsafe. Call is refused.\n", func);
if (argv[0]) pkg_free(argv[0]);
if (argv[1]) pkg_free(argv[1]);
*retval = -1;
return -1;
}
if (argc>=2) {
*retval = exp_func_struct->fixup(&(act->val[3].u.data), 2);
if (*retval < 0) {
LM_ERR("Error in fixup (2)\n");
return -1;
}
act->val[3].type = MODFIXUP_ST;
}
if (argc>=1) {
*retval = exp_func_struct->fixup(&(act->val[2].u.data), 1);
if (*retval < 0) {
LM_ERR("Error in fixup (1)\n");
return -1;
}
act->val[2].type = MODFIXUP_ST;
}
if (argc==0) {
*retval = exp_func_struct->fixup(0, 0);
if (*retval < 0) {
LM_ERR("Error in fixup (0)\n");
return -1;
}
}
}
init_run_actions_ctx(&ra_ctx);
*retval = do_action(&ra_ctx, act, m);
if ((act->val[3].type == MODFIXUP_ST) && (act->val[3].u.data)) {
/* pkg_free(act->elem[3].u.data); */
LM_WARN("moduleFunction: A fixup function was called. "
"This currently creates a memory leak.\n");
}
if ((act->val[2].type == MODFIXUP_ST) && (act->val[2].u.data)) {
/* pkg_free(act->elem[2].u.data); */
LM_WARN("moduleFunction: A fixup function was called. "
"This currently creates a memory leak.\n");
}
if (argv[0]) pkg_free(argv[0]);
if (argv[1]) pkg_free(argv[1]);
pkg_free(act);
return 1;
}
/**
* Rewrite Request-URI
*/
static inline int rewrite_ruri(struct sip_msg* _m, char* _s)
{
struct action act;
struct run_act_ctx ra_ctx;
act.type = SET_URI_T;
act.val[0].type = STRING_ST;
act.val[0].u.string = _s;
act.next = 0;
init_run_actions_ctx(&ra_ctx);
if (do_action(&ra_ctx, &act, _m) < 0)
{
LM_ERR("rewrite_ruri: Error in do_action\n");
return -1;
}
return 0;
}
/**
* Compile a string with pseudo variables substituted by their values.
* A string buffer is allocated. Deallocate afterwards!
*/
char *pv_sprintf(struct sip_msg *m, char *fmt) {
int buf_size = 4096;
char out[4096];
pv_elem_t *model;
str s;
char *ret = NULL;
s.s = fmt; s.len = strlen(s.s);
if(pv_parse_format(&s, &model) < 0) {
LM_ERR("pv_sprintf: ERROR: wrong format[%s]!\n",
fmt);
return NULL;
}
if(pv_printf(m, model, out, &buf_size) < 0) {
ret = NULL;
} else {
ret = strdup(out);
}
pv_elem_free_all(model);
return ret;
}
/**
* Convert an SV to an int_str struct. Needed in AVP package.
* - val: SV to convert.
* - is: pointer to resulting int_str
* - flags: pointer to flags to set
* - strflag: flag mask to be or-applied for string match
*/
inline int sv2int_str(SV *val, int_str *is,
unsigned short *flags, unsigned short strflag) {
char *s;
STRLEN len;
if (!SvOK(val)) {
LM_ERR("AVP:sv2int_str: Invalid value "
"(not a scalar).\n");
return 0;
}
if (SvIOK(val)) { /* numerical name */
is->n = SvIV(val);
return 1;
} else if (SvPOK(val)) {
s = SvPV(val, len);
is->s.len = len;
is->s.s = s;
(*flags) |= strflag;
return 1;
} else {
LM_ERR("AVP:sv2int_str: Invalid value "
"(neither string nor integer).\n");
return 0;
}
}
/* ************************************************************************ */
/* Object methods begin here */
=head1 Kamailio
This module provides access to a limited number of Kamailio core functions.
As the most interesting functions deal with SIP messages, they are located
in the Kamailio::Message class below.
=cut
MODULE = Kamailio PACKAGE = Kamailio
=head2 log(level,message)
Logs the message with Kamailio's logging facility. The logging level
is one of the following:
* L_ALERT
* L_CRIT
* L_ERR
* L_WARN
* L_NOTICE
* L_INFO
* L_DBG
Please note that this method is I<NOT> automatically exported, as it collides
with the perl function log (which calculates the logarithm). Either explicitly
import the function (via C<use Kamailio qw ( log );>), or call it with its full
name:
Kamailio::log(L_INFO, "foobar");
=cut
void
log(level, log)
int level
char *log
PREINIT:
INIT:
CODE:
switch (level) {
case L_ALERT: LM_ALERT("%s", log); break;
case L_CRIT: LM_CRIT("%s", log); break;
case L_ERR: LM_ERR("%s", log); break;
case L_WARN: LM_WARN("%s", log); break;
case L_NOTICE: LM_NOTICE("%s", log); break;
case L_INFO: LM_INFO("%s", log); break;
default: LM_DBG("%s", log); break;
}
OUTPUT:
MODULE = Kamailio PACKAGE = Kamailio::Message
PROTOTYPES: ENABLE
=head1 Kamailio::Message
This package provides access functions for an Kamailio C<sip_msg> structure and
its sub-components. Through its means it is possible to fully configure
alternative routing decisions.
=cut
=head2 getType()
Returns one of the constants SIP_REQUEST, SIP_REPLY, SIP_INVALID stating the
type of the current message.
=cut
int
getType(self)
SV *self
PREINIT:
struct sip_msg *msg = sv2msg(self);
INIT:
CODE:
RETVAL = getType(msg);
OUTPUT:
RETVAL
=head2 getStatus()
Returns the status code of the current Reply message. This function is invalid
in Request context!
=cut
SV *
getStatus(self)
SV *self
PREINIT:
struct sip_msg *msg = sv2msg(self);
str *ret;
INIT:
CODE:
if (!msg) {
LM_ERR("Invalid message reference\n");
ST(0) = &PL_sv_undef;
} else {
if (getType(msg) != SIP_REPLY) {
LM_ERR("getStatus: Status not available in"
" non-reply messages.");
ST(0) = &PL_sv_undef;
} else {
ret = &((msg->first_line).u.reply.status);
ST(0) = sv_2mortal(newSVpv(ret->s, ret->len));
}
}
=head2 getReason()
Returns the reason of the current Reply message. This function is invalid
in Request context!
=cut
SV *
getReason(self)
SV *self
PREINIT:
struct sip_msg *msg = sv2msg(self);
str *ret;
INIT:
CODE:
if (!msg) {
LM_ERR("Invalid message reference\n");
ST(0) = &PL_sv_undef;
} else {
if (getType(msg) != SIP_REPLY) {
LM_ERR("getReason: Reason not available in"
" non-reply messages.");
ST(0) = &PL_sv_undef;
} else {
ret = &((msg->first_line).u.reply.reason);
ST(0) = sv_2mortal(newSVpv(ret->s, ret->len));
}
}
=head2 getVersion()
Returns the version string of the current SIP message.
=cut
SV *
getVersion(self)
SV *self
PREINIT:
struct sip_msg *msg = sv2msg(self);
str *ret;
INIT:
CODE:
if (!msg) {
LM_ERR("Invalid message reference\n");
ST(0) = &PL_sv_undef;
} else {
if (getType(msg) == SIP_REQUEST) {
ret = &((msg->first_line).u.request.version);
} else { /* SIP_REPLY */
ret = &((msg->first_line).u.reply.version);
}
ST(0) = sv_2mortal(newSVpv(ret->s, ret->len));
}
=head2 getRURI()
This function returns the recipient URI of the present SIP message:
C<< my $ruri = $m->getRURI(); >>
getRURI returns a string. See L</"getParsedRURI()"> below how to receive a
parsed structure.
This function is valid in request messages only.
=cut
SV *
getRURI(self)
SV *self
PREINIT:
struct sip_msg *msg = sv2msg(self);
str *ret;
INIT:
CODE:
if (!msg) {
LM_ERR("Invalid message reference\n");
ST(0) = &PL_sv_undef;
} else {
if (getType(msg) != SIP_REQUEST) {
LM_ERR("Not a request message - "
"no RURI available.\n");
ST(0) = &PL_sv_undef;
} else {
ret = &((msg->first_line).u.request.uri);
ST(0) = sv_2mortal(newSVpv(ret->s, ret->len));
}
}
=head2 getMethod()
Returns the current method, such as C<INVITE>, C<REGISTER>, C<ACK> and so on.
C<< my $method = $m->getMethod(); >>
This function is valid in request messages only.
=cut
char *
getMethod(self)
SV *self
PREINIT:
struct sip_msg *msg = sv2msg(self);
str *ret;
INIT:
CODE:
if (!msg) {
LM_ERR("Invalid message reference\n");
ST(0) = &PL_sv_undef;
} else {
if (getType(msg) != SIP_REQUEST) {
LM_ERR("Not a request message - "
"no method available.\n");
ST(0) = &PL_sv_undef;
} else {
ret = &((msg->first_line).u.request.method);
ST(0) = sv_2mortal(newSVpv(ret->s, ret->len));
}
}
=head2 getFullHeader()
Returns the full message header as present in the current message.
You might use this header to further work with it with your
favorite MIME package.
C<< my $hdr = $m->getFullHeader(); >>
=cut
SV *
getFullHeader(self)
SV *self
PREINIT:
struct sip_msg *msg = sv2msg(self);
char *firsttoken;
long headerlen;
INIT:
CODE:
if (!msg) {
LM_ERR("Invalid message reference\n");
ST(0) = &PL_sv_undef;
} else {
if (getType(msg) == SIP_INVALID) {
LM_ERR("getFullHeader: Invalid message type.\n");
ST(0) = &PL_sv_undef;
} else {
parse_headers(msg, ~0, 0);
if (getType(msg) == SIP_REQUEST) {
firsttoken = (msg->first_line).u.request.method.s;
} else { /* SIP_REPLY */
firsttoken = (msg->first_line).u.reply.version.s;
}
if (msg->eoh == NULL)
headerlen = 0;
else
headerlen = ((long)(msg->eoh))
-((long)(firsttoken));
if (headerlen > 0) {
ST(0) =
sv_2mortal(newSVpv(firsttoken, headerlen));
} else {
ST(0) = &PL_sv_undef;
}
}
}
=head2 getBody()
Returns the message body.
=cut
SV *
getBody(self)
SV *self
PREINIT:
struct sip_msg *msg = sv2msg(self);
INIT:
CODE:
if (!msg) {
LM_ERR("Invalid message reference\n");
ST(0) = &PL_sv_undef;
} else {
parse_headers(msg, ~0, 0);
ST(0) = sv_2mortal(newSVpv(get_body(msg), 0));
}
=head2 getMessage()
Returns the whole message including headers and body.
=cut
SV *
getMessage(self)
SV *self
PREINIT:
struct sip_msg *msg = sv2msg(self);
INIT:
CODE:
if (!msg) {
LM_ERR("Invalid message reference\n");
ST(0) = &PL_sv_undef;
} else {
ST(0) = sv_2mortal(newSVpv(msg->buf, 0));
}
=head2 getHeader(name)
Returns the body of the first message header with this name.
C<< print $m->getHeader("To"); >>
B<C<< "John" <sip:john@doe.example> >>>
=cut
SV *
getHeader(self, name)
SV *self;
char *name;
PREINIT:
struct sip_msg *msg = sv2msg(self);
str *body = NULL;
struct hdr_field *hf;
int found = 0;
int namelen = strlen(name);
INIT:
PPCODE:
LM_DBG("searching '%s'\n", name);
if (!msg) {
LM_ERR("Invalid message reference\n");
} else {
parse_headers(msg, ~0, 0);
for (hf = msg->headers; hf; hf = hf->next) {
if (namelen == hf->name.len) {
if (strncmp(name, hf->name.s, namelen) == 0) {
/* Found the right header. */
found = 1;
body = &(hf->body);
XPUSHs(sv_2mortal(newSVpv(body->s,
body->len)));
}
}
}
}
if (!found) {
XPUSHs(&PL_sv_undef);
}
=head2 getHeaderNames()
Returns an array of all header names. Duplicates possible!
=cut
AV *
getHeaderNames(self)
SV *self;
PREINIT:
struct sip_msg *msg = sv2msg(self);
struct hdr_field *hf = NULL;
int found = 0;
PPCODE:
if (!msg) {
LM_ERR("Invalid message reference\n");
} else {
parse_headers(msg, ~0, 0);
for (hf = msg->headers; hf; hf = hf->next) {
found = 1;
XPUSHs(sv_2mortal(newSVpv(hf->name.s, hf->name.len)));
}
}
if (!found) {
XPUSHs(&PL_sv_undef);
}
=head2 moduleFunction(func,string1,string2)
Search for an arbitrary function in module exports and call it with the
parameters self, string1, string2.
C<string1> and/or C<string2> may be omitted.
As this function provides access to the functions that are exported to the
Kamailio configuration file, it is autoloaded for unknown functions. Instead of
writing
$m->moduleFunction("sl_send_reply", "500", "Internal Error");
$m->moduleFunction("xlog", "L_INFO", "foo");
you may as well write
$m->sl_send_reply("500", "Internal Error");
$m->xlog("L_INFO", "foo");
WARNING
In Kamailio 1.2, only a limited subset of module functions is available. This
restriction will be removed in a later version.
Here is a list of functions that are expected to be working (not claiming
completeness):
* alias_db_lookup
* consume_credentials
* is_rpid_user_e164
* append_rpid_hf
* bind_auth
* avp_print
* cpl_process_register
* cpl_process_register_norpl
* load_dlg
* ds_next_dst
* ds_next_domain
* ds_mark_dst
* ds_mark_dst
* is_from_local
* is_uri_host_local
* dp_can_connect
* dp_apply_policy
* enum_query (without parameters)
* enum_fquery (without parameters)
* is_from_user_enum (without parameters)
* i_enum_query (without parameters)
* imc_manager
* jab_* (all functions from the jabber module)
* load_gws (without parameters)
* next_gw
* from_gw (without parameters)
* to_gw (without parameters)
* sdp_mangle_ip
* sdp_mangle_port
* encode_contact
* decode_contact
* decode_contact_header
* fix_contact
* use_media_proxy
* end_media_session
* m_store
* m_dump
* fix_nated_contact
* unforce_rtp_proxy
* force_rtp_proxy
* fix_nated_register
* add_rcv_param
* options_reply
* checkospheader
* validateospheader
* requestosprouting
* checkosproute
* prepareosproute
* prepareallosproutes
* checkcallingtranslation
* reportospusage
* mangle_pidf
* mangle_message_cpim
* add_path (without parameters)
* add_path_received (without parameters)
* prefix2domain
* allow_routing (without parameters)
* allow_trusted
* pike_check_req
* handle_publish
* handle_subscribe
* stored_pres_info
* bind_pua
* send_publish
* send_subscribe
* pua_set_publish
* loose_route
* record_route
* load_rr
* sip_trace
* sl_reply_error
* sms_send_msg
* sd_lookup
* sstCheckMin
* append_time
* has_body (without parameters)
* is_peer_verified
* t_newtran
* t_release
* t_relay (without parameters)
* t_flush_flags
* t_check_trans
* t_was_cancelled
* t_load_contacts
* t_next_contacts
* uac_restore_from
* uac_auth
* has_totag
* tel2sip
* check_to
* check_from
* radius_does_uri_exist
* ul_* (All functions exported by the usrloc module for user access)
* xmpp_send_message
=cut
int
moduleFunction (self, func, string1 = NULL, string2 = NULL)
SV *self;
char *func;
char *string1;
char *string2;
PREINIT:
struct sip_msg *msg = sv2msg(self);
int retval; /* Return value of called function */
int ret; /* Return value of moduleFunc - < 0 for "non existing function" and other errors */
INIT:
CODE:
LM_DBG("Calling exported func '%s', Param1 is '%s',"
" Param2 is '%s'\n", func, string1, string2);
ret = moduleFunc(msg, func, string1, string2, &retval);
if (ret < 0) {
LM_ERR("calling module function '%s' failed."
" Missing loadmodule?\n", func);
retval = -1;
}
RETVAL = retval;
OUTPUT:
RETVAL
=head2 log(level,message) (deprecated type)
Logs the message with Kamailio's logging facility. The logging level
is one of the following:
* L_ALERT
* L_CRIT
* L_ERR
* L_WARN
* L_NOTICE
* L_INFO
* L_DBG
The logging function should be accessed via the Kamailio module variant. This
one, located in Kamailio::Message, is deprecated.
=cut
void
log(self, level, log)
SV *self
int level
char *log
PREINIT:
INIT:
CODE:
switch (level) {
case L_ALERT: LM_ALERT("%s", log); break;
case L_CRIT: LM_CRIT("%s", log); break;
case L_ERR: LM_ERR("%s", log); break;
case L_WARN: LM_WARN("%s", log); break;
case L_NOTICE: LM_NOTICE("%s", log); break;
case L_INFO: LM_INFO("%s", log); break;
default: LM_DBG("%s", log); break;
}
=head2 rewrite_ruri(newruri)
Sets a new destination (recipient) URI. Useful for rerouting the
current message/call.
if ($m->getRURI() =~ m/\@somedomain.net/) {
$m->rewrite_ruri("sip:dispatcher\@organization.net");
}
=cut
int
rewrite_ruri(self, newruri)
SV *self;
char *newruri;
PREINIT:
struct sip_msg *msg = sv2msg(self);
INIT:
CODE:
if (!msg) {
LM_ERR("Invalid message reference\n");
RETVAL = -1;
} else {
if (getType(msg) != SIP_REQUEST) {
LM_ERR("Not a Request. RURI rewrite unavailable.\n");
RETVAL = -1;
} else {
LM_DBG("New R-URI is [%s]\n", newruri);
RETVAL = rewrite_ruri(msg, newruri);
}
}
OUTPUT:
RETVAL
=head2 setFlag(flag)
Sets a message flag. The constants as known from the C API may be used,
when Constants.pm is included.
=cut
int
setFlag(self, flag)
SV *self;
unsigned int flag;
PREINIT:
struct sip_msg *msg = sv2msg(self);
INIT:
CODE:
if (!msg) {
LM_ERR("Invalid message reference\n");
RETVAL = -1;
} else {
RETVAL = setflag(msg, flag);
}
OUTPUT:
RETVAL
=head2 resetFlag(flag)
Resets a message flag.
=cut
int
resetFlag(self, flag)
SV *self;
unsigned int flag;
PREINIT:
struct sip_msg *msg = sv2msg(self);
INIT:
CODE:
if (!msg) {
LM_ERR("Invalid message reference\n");
RETVAL = -1;
} else {
RETVAL = resetflag(msg, flag);
}
OUTPUT:
RETVAL
=head2 isFlagSet(flag)
Returns whether a message flag is set or not.
=cut
int
isFlagSet(self, flag)
SV *self;
unsigned int flag;
PREINIT:
struct sip_msg *msg = sv2msg(self);
INIT:
CODE:
if (!msg) {
LM_ERR("Invalid message reference\n");
RETVAL = -1;
} else {
RETVAL = isflagset(msg, flag) == 1 ? 1 : 0;
}
OUTPUT:
RETVAL
=head2 pseudoVar(string)
Returns a new string where all pseudo variables are substituted by their values.
Can be used to receive the values of single variables, too.
B<Please remember that you need to escape the '$' sign in perl strings!>
=cut
SV *
pseudoVar(self, varstring)
SV *self;
char *varstring;
PREINIT:
struct sip_msg *msg = sv2msg(self);
char *ret;
CODE:
if (!msg) {
LM_ERR("Invalid message reference\n");
ST(0) = &PL_sv_undef;
} else {
ret = pv_sprintf(msg, varstring);
if (ret) {
ST(0) = sv_2mortal(newSVpv(ret, strlen(ret)));
free(ret);
} else {
ST(0) = &PL_sv_undef;
}
}
=head2 append_branch(branch,qval)
Append a branch to current message.
=cut
int
append_branch(self, branch = NULL, qval = NULL)
SV *self;
char *branch;
char *qval;
PREINIT:
struct sip_msg *msg = sv2msg(self);
qvalue_t q = Q_UNSPECIFIED;
str b = {0, 0};
INIT:
CODE:
if (!msg) {
LM_ERR("Invalid message reference\n");
RETVAL = -1;
} else {
if (qval) {
if (str2q(&q, qval, strlen(qval)) < 0) {
LM_ERR("append_branch: Bad q value.");
} else { /* branch and qval set */
b.s = branch;
b.len = strlen(branch);
}
} else {
if (branch) { /* branch set, qval unset */
b.s = branch;
b.len = strlen(branch);
}
}
RETVAL = km_append_branch(msg, (b.s!=0)?&b:0, 0, 0, q, 0, 0);
}
OUTPUT:
RETVAL
=head2 getParsedRURI()
Returns the current destination URI as an Kamailio::URI object.
=cut
SV *
getParsedRURI(self)
SV *self;
PREINIT:
struct sip_msg *msg = sv2msg(self);
struct sip_uri *uri;
SV *ret;
INIT:
CODE:
if (!msg) {
LM_ERR("Invalid message reference\n");
ST(0) = NULL;
} else {
parse_sip_msg_uri(msg);
parse_headers(msg, ~0, 0);
uri = &(msg->parsed_uri);
ret = sv_newmortal();
sv_setref_pv(ret, "Kamailio::URI", (void *)uri);
SvREADONLY_on(SvRV(ret));
ST(0) = ret;
}
MODULE = Kamailio PACKAGE = Kamailio::URI
=head1 Kamailio::URI
This package provides functions for access to sip_uri structures.
=cut
=head2 user()
Returns the user part of this URI.
=cut
SV *
user(self)
SV *self;
CODE:
ST(0) = getStringFromURI(self, XS_URI_USER);
=head2 host()
Returns the host part of this URI.
=cut
SV *
host(self)
SV *self;
CODE:
ST(0) = getStringFromURI(self, XS_URI_HOST);
=head2 passwd()
Returns the passwd part of this URI.
=cut
SV *
passwd(self)
SV *self;
CODE:
ST(0) = getStringFromURI(self, XS_URI_PASSWD);
=head2 port()
Returns the port part of this URI.
=cut
SV *
port(self)
SV *self;
CODE:
ST(0) = getStringFromURI(self, XS_URI_PORT);
=head2 params()
Returns the params part of this URI.
=cut
SV *
params(self)
SV *self;
CODE:
ST(0) = getStringFromURI(self, XS_URI_PARAMS);
=head2 headers()
Returns the headers part of this URI.
=cut
SV *
headers(self)
SV *self;
CODE:
ST(0) = getStringFromURI(self, XS_URI_HEADERS);
=head2 transport()
Returns the transport part of this URI.
=cut
SV *
transport(self)
SV *self;
CODE:
ST(0) = getStringFromURI(self, XS_URI_TRANSPORT);
=head2 ttl()
Returns the ttl part of this URI.
=cut
SV *
ttl(self)
SV *self;
CODE:
ST(0) = getStringFromURI(self, XS_URI_TTL);
=head2 user_param()
Returns the user_param part of this URI.
=cut
SV *
user_param(self)
SV *self;
CODE:
ST(0) = getStringFromURI(self, XS_URI_USER_PARAM);
=head2 maddr()
Returns the maddr part of this URI.
=cut
SV *
maddr(self)
SV *self;
CODE:
ST(0) = getStringFromURI(self, XS_URI_MADDR);
=head2 method()
Returns the method part of this URI.
=cut
SV *
method(self)
SV *self;
CODE:
ST(0) = getStringFromURI(self, XS_URI_METHOD);
=head2 lr()
Returns the lr part of this URI.
=cut
SV *
lr(self)
SV *self;
CODE:
ST(0) = getStringFromURI(self, XS_URI_LR);
=head2 r2()
Returns the r2 part of this URI.
=cut
SV *
r2(self)
SV *self;
CODE:
ST(0) = getStringFromURI(self, XS_URI_R2);
=head2 transport_val()
Returns the transport_val part of this URI.
=cut
SV *
transport_val(self)
SV *self;
CODE:
ST(0) = getStringFromURI(self, XS_URI_TRANSPORT_VAL);
=head2 ttl_val()
Returns the ttl_val part of this URI.
=cut
SV *
ttl_val(self)
SV *self;
CODE:
ST(0) = getStringFromURI(self, XS_URI_TTL_VAL);
=head2 user_param_val()
Returns the user_param_val part of this URI.
=cut
SV *
user_param_val(self)
SV *self;
CODE:
ST(0) = getStringFromURI(self, XS_URI_USER_PARAM_VAL);
=head2 maddr_val()
Returns the maddr_val part of this URI.
=cut
SV *
maddr_val(self)
SV *self;
CODE:
ST(0) = getStringFromURI(self, XS_URI_MADDR_VAL);
=head2 method_val()
Returns the method_val part of this URI.
=cut
SV *
method_val(self)
SV *self;
CODE:
ST(0) = getStringFromURI(self, XS_URI_METHOD_VAL);
=head2 lr_val()
Returns the lr_val part of this URI.
=cut
SV *
lr_val(self)
SV *self;
CODE:
ST(0) = getStringFromURI(self, XS_URI_LR_VAL);
=head2 r2_val()
Returns the r2_val part of this URI.
=cut
SV *
r2_val(self)
SV *self;
CODE:
ST(0) = getStringFromURI(self, XS_URI_R2_VAL);
=head1 Kamailio::AVP
This package provides access functions for Kamailio's AVPs.
These variables can be created, evaluated, modified and removed through this
package.
Please note that these functions do NOT support the notation used
in the configuration file, but directly work on strings or numbers. See
documentation of add method below.
=cut
MODULE = Kamailio PACKAGE = Kamailio::AVP
=head2 add(name,val)
Add an AVP.
Add an Kamailio AVP to its environment. name and val may both be integers or
strings; this function will try to guess what is correct. Please note that
Kamailio::AVP::add("10", "10")
is something different than
Kamailio::AVP::add(10, 10)
due to this evaluation: The first will create _string_ AVPs with the name
10, while the latter will create a numerical AVP.
You can modify/overwrite AVPs with this function.
=cut
int
add(p_name, p_val)
SV *p_name;
SV *p_val;
PREINIT:
int_str name;
int_str val;
unsigned short flags = 0;
char *s;
STRLEN len;
CODE:
RETVAL = 0;
if (SvOK(p_name) && SvOK(p_val)) {
if (!sv2int_str(p_name, &name, &flags, AVP_NAME_STR)) {
RETVAL = -1;
} else if (!sv2int_str(p_val, &val, &flags, AVP_VAL_STR)) {
RETVAL = -1;
}
if (RETVAL == 0) {
RETVAL = add_avp(flags, name, val);
}
}
OUTPUT:
RETVAL
=head2 get(name)
get an Kamailio AVP:
my $numavp = Kamailio::AVP::get(5);
my $stravp = Kamailio::AVP::get("foo");
=cut
int
get(p_name)
SV *p_name;
PREINIT:
struct usr_avp *first_avp;
int_str name;
int_str val;
unsigned short flags = 0;
SV *ret = &PL_sv_undef;
int err = 0;
char *s;
STRLEN len;
CODE:
if (SvOK(p_name)) {
if (!sv2int_str(p_name, &name, &flags, AVP_NAME_STR)) {
LM_ERR("AVP:get: Invalid name.");
err = 1;
}
} else {
LM_ERR("AVP:get: Invalid name.");
err = 1;
}
if (err == 0) {
first_avp = search_first_avp(flags, name, &val, NULL);
if (first_avp != NULL) { /* found correct AVP */
if (is_avp_str_val(first_avp)) {
ret = sv_2mortal(newSVpv(val.s.s, val.s.len));
} else {
ret = sv_2mortal(newSViv(val.n));
}
} else {
/* Empty AVP requested. */
}
}
ST(0) = ret;
=head2 destroy(name)
Destroy an AVP.
Kamailio::AVP::destroy(5);
Kamailio::AVP::destroy("foo");
=cut
int
destroy(p_name)
SV *p_name;
PREINIT:
struct usr_avp *first_avp;
int_str name;
int_str val;
unsigned short flags = 0;
SV *ret = &PL_sv_undef;
char *s;
STRLEN len;
CODE:
RETVAL = 1;
if (SvOK(p_name)) {
if (!sv2int_str(p_name, &name, &flags, AVP_NAME_STR)) {
RETVAL = 0;
LM_ERR("AVP:destroy: Invalid name.");
}
} else {
RETVAL = 0;
LM_ERR("VP:destroy: Invalid name.");
}
if (RETVAL == 1) {
first_avp = search_first_avp(flags, name, &val, NULL);
if (first_avp != NULL) { /* found correct AVP */
destroy_avp(first_avp);
} else {
RETVAL = 0;
/* Empty AVP requested. */
}
}
OUTPUT:
RETVAL
|