kamailioxs.xs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  1. /*
  2. * $Id$
  3. *
  4. * Perl module for Kamailio
  5. *
  6. * Copyright (C) 2006 Collax GmbH
  7. * (Bastian Friedrich <[email protected]>)
  8. *
  9. * This file is part of kamailio, a free SIP server.
  10. *
  11. * Kamailio is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version
  15. *
  16. * Kamailio is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. */
  26. #include <EXTERN.h>
  27. #include <perl.h>
  28. #include <XSUB.h>
  29. #include <unistd.h>
  30. #undef load_module
  31. /* perl.h defines union semun */
  32. #ifdef USE_SYSV_SEM
  33. # undef _SEM_SEMUN_UNDEFINED
  34. #endif
  35. #include "../../sr_module.h"
  36. #include "../../parser/msg_parser.h"
  37. #include "../../parser/parse_uri.h"
  38. #include "../../usr_avp.h"
  39. #include "../../action.h"
  40. #include "../../flags.h"
  41. #include "../../pvar.h"
  42. #include "../../dset.h"
  43. #include "../../mem/mem.h"
  44. #include "../../route_struct.h"
  45. #include "../../qvalue.h"
  46. #include "../../dprint.h"
  47. extern int unsafemodfnc;
  48. enum xs_uri_members {
  49. XS_URI_USER = 0,
  50. XS_URI_PASSWD,
  51. XS_URI_HOST,
  52. XS_URI_PORT,
  53. XS_URI_PARAMS,
  54. XS_URI_HEADERS,
  55. XS_URI_TRANSPORT,
  56. XS_URI_TTL,
  57. XS_URI_USER_PARAM,
  58. XS_URI_MADDR,
  59. XS_URI_METHOD,
  60. XS_URI_LR,
  61. XS_URI_R2,
  62. XS_URI_TRANSPORT_VAL,
  63. XS_URI_TTL_VAL,
  64. XS_URI_USER_PARAM_VAL,
  65. XS_URI_MADDR_VAL,
  66. XS_URI_METHOD_VAL,
  67. XS_URI_LR_VAL,
  68. XS_URI_R2_VAL
  69. /* These members are no strings:
  70. unsigned short port_no;
  71. unsigned short proto; / * from transport * /
  72. uri_type type; / * uri scheme */
  73. };
  74. /*
  75. * Return the sip_msg struct referred to by perl reference sv
  76. */
  77. struct sip_msg * sv2msg(SV *sv) {
  78. struct sip_msg* m;
  79. if (SvROK(sv)) {
  80. sv = SvRV(sv);
  81. if (SvIOK(sv)) {
  82. m = INT2PTR(struct sip_msg*, SvIV(sv));
  83. return m;
  84. }
  85. }
  86. return NULL; /* In case of error above... */
  87. }
  88. struct sip_uri * sv2uri(SV *sv) {
  89. struct sip_uri* u;
  90. if (SvROK(sv)) {
  91. sv = SvRV(sv);
  92. if (SvIOK(sv)) {
  93. u = INT2PTR(struct sip_uri*, SvIV(sv));
  94. return u;
  95. }
  96. }
  97. return NULL; /* In case of error above... */
  98. }
  99. struct action * sv2action(SV *sv) {
  100. struct action* a;
  101. if (SvROK(sv)) {
  102. sv = SvRV(sv);
  103. if (SvIOK(sv)) {
  104. a = INT2PTR(struct action*, SvIV(sv));
  105. return a;
  106. }
  107. }
  108. return NULL; /* In case of error above... */
  109. }
  110. /*
  111. * We have a private function for two reasons:
  112. * a) Return SIP_INVALID even if type was sth different
  113. * b) easy access
  114. */
  115. inline static int getType(struct sip_msg *msg) {
  116. int t = SIP_INVALID;
  117. if (!msg) return SIP_INVALID;
  118. switch ((msg->first_line).type) {
  119. case SIP_REQUEST: t = SIP_REQUEST; break;
  120. case SIP_REPLY: t = SIP_REPLY; break;
  121. }
  122. return t;
  123. }
  124. SV *getStringFromURI(SV *self, enum xs_uri_members what) {
  125. struct sip_uri *myuri = sv2uri(self);
  126. str *ret = NULL;
  127. if (!myuri) {
  128. LM_ERR("Invalid URI reference\n");
  129. ret = NULL;
  130. } else {
  131. switch (what) {
  132. case XS_URI_USER: ret = &(myuri->user);
  133. break;
  134. case XS_URI_HOST: ret = &(myuri->host);
  135. break;
  136. case XS_URI_PASSWD: ret = &(myuri->passwd);
  137. break;
  138. case XS_URI_PORT: ret = &(myuri->port);
  139. break;
  140. case XS_URI_PARAMS: ret = &(myuri->params);
  141. break;
  142. case XS_URI_HEADERS: ret = &(myuri->headers);
  143. break;
  144. case XS_URI_TRANSPORT: ret = &(myuri->transport);
  145. break;
  146. case XS_URI_TTL: ret = &(myuri->ttl);
  147. break;
  148. case XS_URI_USER_PARAM: ret = &(myuri->user_param);
  149. break;
  150. case XS_URI_MADDR: ret = &(myuri->maddr);
  151. break;
  152. case XS_URI_METHOD: ret = &(myuri->method);
  153. break;
  154. case XS_URI_LR: ret = &(myuri->lr);
  155. break;
  156. case XS_URI_R2: ret = &(myuri->r2);
  157. break;
  158. case XS_URI_TRANSPORT_VAL: ret = &(myuri->transport_val);
  159. break;
  160. case XS_URI_TTL_VAL: ret = &(myuri->ttl_val);
  161. break;
  162. case XS_URI_USER_PARAM_VAL: ret = &(myuri->user_param_val);
  163. break;
  164. case XS_URI_MADDR_VAL: ret = &(myuri->maddr_val);
  165. break;
  166. case XS_URI_METHOD_VAL: ret = &(myuri->method_val);
  167. break;
  168. case XS_URI_LR_VAL: ret = &(myuri->lr_val);
  169. break;
  170. case XS_URI_R2_VAL: ret = &(myuri->r2_val);
  171. break;
  172. default: LM_INFO("Unknown URI element"
  173. " requested: %d\n", what);
  174. break;
  175. }
  176. }
  177. if ((ret) && (ret->len)) {
  178. return sv_2mortal(newSVpv(ret->s, ret->len));
  179. } else {
  180. return &PL_sv_undef;
  181. }
  182. }
  183. /*
  184. * Calls an exported function. Parameters are copied and fixup'd.
  185. *
  186. * Return codes:
  187. * -1 - Function not available (or other error).
  188. * 1 - Function was called. Its return value is returned via the retval
  189. * parameter.
  190. */
  191. int moduleFunc(struct sip_msg *m, char *func,
  192. char *param1, char *param2,
  193. int *retval) {
  194. sr31_cmd_export_t* exp_func_struct;
  195. struct action *act;
  196. unsigned mod_ver;
  197. char *argv[2];
  198. int argc = 0;
  199. struct run_act_ctx ra_ctx;
  200. if (!func) {
  201. LM_ERR("moduleFunc called with null function name. Error.");
  202. return -1;
  203. }
  204. if ((!param1) && param2) {
  205. LM_ERR("moduleFunc called with parameter 1 UNSET and"
  206. " parameter 2 SET. Error.");
  207. return -1;
  208. }
  209. if (param1) {
  210. argv[0] = (char *)pkg_malloc(strlen(param1)+1);
  211. if (!argv[0]) {
  212. LM_ERR("not enough pkg mem\n");
  213. return -1;
  214. }
  215. strcpy(argv[0], param1);
  216. argc++;
  217. } else {
  218. argv[0] = NULL;
  219. }
  220. if (param2) {
  221. argv[1] = (char *)pkg_malloc(strlen(param2)+1);
  222. if (!argv[1]) {
  223. LM_ERR("not enough pkg mem\n");
  224. if (argv[0]) pkg_free(argv[0]);
  225. return -1;
  226. }
  227. strcpy(argv[1], param2);
  228. argc++;
  229. } else {
  230. argv[1] = NULL;
  231. }
  232. exp_func_struct = find_export_record(func, argc, 0, &mod_ver);
  233. if (!exp_func_struct || mod_ver < 1) {
  234. LM_ERR("function '%s' called, but not available.", func);
  235. *retval = -1;
  236. if (argv[0]) pkg_free(argv[0]);
  237. if (argv[1]) pkg_free(argv[1]);
  238. return -1;
  239. }
  240. act = mk_action(MODULE2_T, 4 /* number of (type, value) pairs */,
  241. MODEXP_ST, exp_func_struct, /* function */
  242. NUMBER_ST, 2, /* parameter number */
  243. STRING_ST, argv[0], /* param. 1 */
  244. STRING_ST, argv[1] /* param. 2 */
  245. );
  246. if (!act) {
  247. LM_ERR("action structure could not be created. Error.");
  248. if (argv[0]) pkg_free(argv[0]);
  249. if (argv[1]) pkg_free(argv[1]);
  250. return -1;
  251. }
  252. if (exp_func_struct->fixup) {
  253. if (!unsafemodfnc) {
  254. LM_ERR("Module function '%s' is unsafe. Call is refused.\n", func);
  255. if (argv[0]) pkg_free(argv[0]);
  256. if (argv[1]) pkg_free(argv[1]);
  257. *retval = -1;
  258. return -1;
  259. }
  260. if (argc>=2) {
  261. *retval = exp_func_struct->fixup(&(act->val[3].u.data), 2);
  262. if (*retval < 0) {
  263. LM_ERR("Error in fixup (2)\n");
  264. return -1;
  265. }
  266. act->val[3].type = MODFIXUP_ST;
  267. }
  268. if (argc>=1) {
  269. *retval = exp_func_struct->fixup(&(act->val[2].u.data), 1);
  270. if (*retval < 0) {
  271. LM_ERR("Error in fixup (1)\n");
  272. return -1;
  273. }
  274. act->val[2].type = MODFIXUP_ST;
  275. }
  276. if (argc==0) {
  277. *retval = exp_func_struct->fixup(0, 0);
  278. if (*retval < 0) {
  279. LM_ERR("Error in fixup (0)\n");
  280. return -1;
  281. }
  282. }
  283. }
  284. init_run_actions_ctx(&ra_ctx);
  285. *retval = do_action(&ra_ctx, act, m);
  286. if ((act->val[3].type == MODFIXUP_ST) && (act->val[3].u.data)) {
  287. /* pkg_free(act->elem[3].u.data); */
  288. LM_WARN("moduleFunction: A fixup function was called. "
  289. "This currently creates a memory leak.\n");
  290. }
  291. if ((act->val[2].type == MODFIXUP_ST) && (act->val[2].u.data)) {
  292. /* pkg_free(act->elem[2].u.data); */
  293. LM_WARN("moduleFunction: A fixup function was called. "
  294. "This currently creates a memory leak.\n");
  295. }
  296. if (argv[0]) pkg_free(argv[0]);
  297. if (argv[1]) pkg_free(argv[1]);
  298. pkg_free(act);
  299. return 1;
  300. }
  301. /**
  302. * Rewrite Request-URI
  303. */
  304. static inline int rewrite_ruri(struct sip_msg* _m, char* _s)
  305. {
  306. struct action act;
  307. struct run_act_ctx ra_ctx;
  308. act.type = SET_URI_T;
  309. act.val[0].type = STRING_ST;
  310. act.val[0].u.string = _s;
  311. act.next = 0;
  312. init_run_actions_ctx(&ra_ctx);
  313. if (do_action(&ra_ctx, &act, _m) < 0)
  314. {
  315. LM_ERR("rewrite_ruri: Error in do_action\n");
  316. return -1;
  317. }
  318. return 0;
  319. }
  320. /**
  321. * Compile a string with pseudo variables substituted by their values.
  322. * A string buffer is allocated. Deallocate afterwards!
  323. */
  324. char *pv_sprintf(struct sip_msg *m, char *fmt) {
  325. int buf_size = 4096;
  326. static char out[4096];
  327. pv_elem_t *model;
  328. str s;
  329. char *ret;
  330. s.s = fmt; s.len = strlen(s.s);
  331. if(pv_parse_format(&s, &model) < 0) {
  332. LM_ERR("pv_sprintf: wrong format[%s]!\n",
  333. fmt);
  334. return NULL;
  335. }
  336. if(pv_printf(m, model, out, &buf_size) < 0) {
  337. LM_ERR("pv_printf: failed to print pv value\n");
  338. ret = NULL;
  339. } else {
  340. ret = strdup(out);
  341. }
  342. pv_elem_free_all(model);
  343. return ret;
  344. }
  345. /**
  346. * Convert an SV to an int_str struct. Needed in AVP package.
  347. * - val: SV to convert.
  348. * - is: pointer to resulting int_str
  349. * - flags: pointer to flags to set
  350. * - strflag: flag mask to be or-applied for string match
  351. */
  352. inline int sv2int_str(SV *val, int_str *is,
  353. unsigned short *flags, unsigned short strflag) {
  354. char *s;
  355. STRLEN len;
  356. if (!SvOK(val)) {
  357. LM_ERR("AVP:sv2int_str: Invalid value "
  358. "(not a scalar).\n");
  359. return 0;
  360. }
  361. if (SvIOK(val)) { /* numerical name */
  362. is->n = SvIV(val);
  363. return 1;
  364. } else if (SvPOK(val)) {
  365. s = SvPV(val, len);
  366. is->s.len = len;
  367. is->s.s = s;
  368. (*flags) |= strflag;
  369. return 1;
  370. } else {
  371. LM_ERR("AVP:sv2int_str: Invalid value "
  372. "(neither string nor integer).\n");
  373. return 0;
  374. }
  375. }
  376. /* ************************************************************************ */
  377. /* Object methods begin here */
  378. =head1 Kamailio
  379. This module provides access to a limited number of Kamailio core functions.
  380. As the most interesting functions deal with SIP messages, they are located
  381. in the Kamailio::Message class below.
  382. =cut
  383. MODULE = Kamailio PACKAGE = Kamailio
  384. =head2 log(level,message)
  385. Logs the message with Kamailio's logging facility. The logging level
  386. is one of the following:
  387. * L_ALERT
  388. * L_CRIT
  389. * L_ERR
  390. * L_WARN
  391. * L_NOTICE
  392. * L_INFO
  393. * L_DBG
  394. Please note that this method is I<NOT> automatically exported, as it collides
  395. with the perl function log (which calculates the logarithm). Either explicitly
  396. import the function (via C<use Kamailio qw ( log );>), or call it with its full
  397. name:
  398. Kamailio::log(L_INFO, "foobar");
  399. =cut
  400. void
  401. log(level, log)
  402. int level
  403. char *log
  404. PREINIT:
  405. INIT:
  406. CODE:
  407. switch (level) {
  408. case L_ALERT: LM_ALERT("%s", log); break;
  409. case L_CRIT: LM_CRIT("%s", log); break;
  410. case L_ERR: LM_ERR("%s", log); break;
  411. case L_WARN: LM_WARN("%s", log); break;
  412. case L_NOTICE: LM_NOTICE("%s", log); break;
  413. case L_INFO: LM_INFO("%s", log); break;
  414. default: LM_DBG("%s", log); break;
  415. }
  416. OUTPUT:
  417. MODULE = Kamailio PACKAGE = Kamailio::Message
  418. PROTOTYPES: ENABLE
  419. =head1 Kamailio::Message
  420. This package provides access functions for an Kamailio C<sip_msg> structure and
  421. its sub-components. Through its means it is possible to fully configure
  422. alternative routing decisions.
  423. =cut
  424. =head2 getType()
  425. Returns one of the constants SIP_REQUEST, SIP_REPLY, SIP_INVALID stating the
  426. type of the current message.
  427. =cut
  428. int
  429. getType(self)
  430. SV *self
  431. PREINIT:
  432. struct sip_msg *msg = sv2msg(self);
  433. INIT:
  434. CODE:
  435. RETVAL = getType(msg);
  436. OUTPUT:
  437. RETVAL
  438. =head2 getStatus()
  439. Returns the status code of the current Reply message. This function is invalid
  440. in Request context!
  441. =cut
  442. SV *
  443. getStatus(self)
  444. SV *self
  445. PREINIT:
  446. struct sip_msg *msg = sv2msg(self);
  447. str *ret;
  448. INIT:
  449. CODE:
  450. if (!msg) {
  451. LM_ERR("Invalid message reference\n");
  452. ST(0) = &PL_sv_undef;
  453. } else {
  454. if (getType(msg) != SIP_REPLY) {
  455. LM_ERR("getStatus: Status not available in"
  456. " non-reply messages.");
  457. ST(0) = &PL_sv_undef;
  458. } else {
  459. ret = &((msg->first_line).u.reply.status);
  460. ST(0) = sv_2mortal(newSVpv(ret->s, ret->len));
  461. }
  462. }
  463. =head2 getReason()
  464. Returns the reason of the current Reply message. This function is invalid
  465. in Request context!
  466. =cut
  467. SV *
  468. getReason(self)
  469. SV *self
  470. PREINIT:
  471. struct sip_msg *msg = sv2msg(self);
  472. str *ret;
  473. INIT:
  474. CODE:
  475. if (!msg) {
  476. LM_ERR("Invalid message reference\n");
  477. ST(0) = &PL_sv_undef;
  478. } else {
  479. if (getType(msg) != SIP_REPLY) {
  480. LM_ERR("getReason: Reason not available in"
  481. " non-reply messages.");
  482. ST(0) = &PL_sv_undef;
  483. } else {
  484. ret = &((msg->first_line).u.reply.reason);
  485. ST(0) = sv_2mortal(newSVpv(ret->s, ret->len));
  486. }
  487. }
  488. =head2 getVersion()
  489. Returns the version string of the current SIP message.
  490. =cut
  491. SV *
  492. getVersion(self)
  493. SV *self
  494. PREINIT:
  495. struct sip_msg *msg = sv2msg(self);
  496. str *ret;
  497. INIT:
  498. CODE:
  499. if (!msg) {
  500. LM_ERR("Invalid message reference\n");
  501. ST(0) = &PL_sv_undef;
  502. } else {
  503. if (getType(msg) == SIP_REQUEST) {
  504. ret = &((msg->first_line).u.request.version);
  505. } else { /* SIP_REPLY */
  506. ret = &((msg->first_line).u.reply.version);
  507. }
  508. ST(0) = sv_2mortal(newSVpv(ret->s, ret->len));
  509. }
  510. =head2 getRURI()
  511. This function returns the recipient URI of the present SIP message:
  512. C<< my $ruri = $m->getRURI(); >>
  513. getRURI returns a string. See L</"getParsedRURI()"> below how to receive a
  514. parsed structure.
  515. This function is valid in request messages only.
  516. =cut
  517. SV *
  518. getRURI(self)
  519. SV *self
  520. PREINIT:
  521. struct sip_msg *msg = sv2msg(self);
  522. str *ret;
  523. INIT:
  524. CODE:
  525. if (!msg) {
  526. LM_ERR("Invalid message reference\n");
  527. ST(0) = &PL_sv_undef;
  528. } else {
  529. if (getType(msg) != SIP_REQUEST) {
  530. LM_ERR("Not a request message - "
  531. "no RURI available.\n");
  532. ST(0) = &PL_sv_undef;
  533. } else {
  534. ret = &((msg->first_line).u.request.uri);
  535. ST(0) = sv_2mortal(newSVpv(ret->s, ret->len));
  536. }
  537. }
  538. =head2 getMethod()
  539. Returns the current method, such as C<INVITE>, C<REGISTER>, C<ACK> and so on.
  540. C<< my $method = $m->getMethod(); >>
  541. This function is valid in request messages only.
  542. =cut
  543. char *
  544. getMethod(self)
  545. SV *self
  546. PREINIT:
  547. struct sip_msg *msg = sv2msg(self);
  548. str *ret;
  549. INIT:
  550. CODE:
  551. if (!msg) {
  552. LM_ERR("Invalid message reference\n");
  553. ST(0) = &PL_sv_undef;
  554. } else {
  555. if (getType(msg) != SIP_REQUEST) {
  556. LM_ERR("Not a request message - "
  557. "no method available.\n");
  558. ST(0) = &PL_sv_undef;
  559. } else {
  560. ret = &((msg->first_line).u.request.method);
  561. ST(0) = sv_2mortal(newSVpv(ret->s, ret->len));
  562. }
  563. }
  564. =head2 getFullHeader()
  565. Returns the full message header as present in the current message.
  566. You might use this header to further work with it with your
  567. favorite MIME package.
  568. C<< my $hdr = $m->getFullHeader(); >>
  569. =cut
  570. SV *
  571. getFullHeader(self)
  572. SV *self
  573. PREINIT:
  574. struct sip_msg *msg = sv2msg(self);
  575. char *firsttoken;
  576. long headerlen;
  577. INIT:
  578. CODE:
  579. if (!msg) {
  580. LM_ERR("Invalid message reference\n");
  581. ST(0) = &PL_sv_undef;
  582. } else {
  583. if (getType(msg) == SIP_INVALID) {
  584. LM_ERR("getFullHeader: Invalid message type.\n");
  585. ST(0) = &PL_sv_undef;
  586. } else {
  587. parse_headers(msg, ~0, 0);
  588. if (getType(msg) == SIP_REQUEST) {
  589. firsttoken = (msg->first_line).u.request.method.s;
  590. } else { /* SIP_REPLY */
  591. firsttoken = (msg->first_line).u.reply.version.s;
  592. }
  593. if (msg->eoh == NULL)
  594. headerlen = 0;
  595. else
  596. headerlen = ((long)(msg->eoh))
  597. -((long)(firsttoken));
  598. if (headerlen > 0) {
  599. ST(0) =
  600. sv_2mortal(newSVpv(firsttoken, headerlen));
  601. } else {
  602. ST(0) = &PL_sv_undef;
  603. }
  604. }
  605. }
  606. =head2 getBody()
  607. Returns the message body.
  608. =cut
  609. SV *
  610. getBody(self)
  611. SV *self
  612. PREINIT:
  613. struct sip_msg *msg = sv2msg(self);
  614. INIT:
  615. CODE:
  616. if (!msg) {
  617. LM_ERR("Invalid message reference\n");
  618. ST(0) = &PL_sv_undef;
  619. } else {
  620. parse_headers(msg, ~0, 0);
  621. ST(0) = sv_2mortal(newSVpv(get_body(msg), 0));
  622. }
  623. =head2 getMessage()
  624. Returns the whole message including headers and body.
  625. =cut
  626. SV *
  627. getMessage(self)
  628. SV *self
  629. PREINIT:
  630. struct sip_msg *msg = sv2msg(self);
  631. INIT:
  632. CODE:
  633. if (!msg) {
  634. LM_ERR("Invalid message reference\n");
  635. ST(0) = &PL_sv_undef;
  636. } else {
  637. ST(0) = sv_2mortal(newSVpv(msg->buf, 0));
  638. }
  639. =head2 getHeader(name)
  640. Returns the body of the first message header with this name.
  641. C<< print $m->getHeader("To"); >>
  642. B<C<< "John" <sip:[email protected]> >>>
  643. =cut
  644. SV *
  645. getHeader(self, name)
  646. SV *self;
  647. char *name;
  648. PREINIT:
  649. struct sip_msg *msg = sv2msg(self);
  650. str *body = NULL;
  651. struct hdr_field *hf;
  652. int found = 0;
  653. int namelen = strlen(name);
  654. INIT:
  655. PPCODE:
  656. LM_DBG("searching '%s'\n", name);
  657. if (!msg) {
  658. LM_ERR("Invalid message reference\n");
  659. } else {
  660. parse_headers(msg, ~0, 0);
  661. for (hf = msg->headers; hf; hf = hf->next) {
  662. if (namelen == hf->name.len) {
  663. if (strncmp(name, hf->name.s, namelen) == 0) {
  664. /* Found the right header. */
  665. found = 1;
  666. body = &(hf->body);
  667. XPUSHs(sv_2mortal(newSVpv(body->s,
  668. body->len)));
  669. }
  670. }
  671. }
  672. }
  673. if (!found) {
  674. XPUSHs(&PL_sv_undef);
  675. }
  676. =head2 getHeaderNames()
  677. Returns an array of all header names. Duplicates possible!
  678. =cut
  679. AV *
  680. getHeaderNames(self)
  681. SV *self;
  682. PREINIT:
  683. struct sip_msg *msg = sv2msg(self);
  684. struct hdr_field *hf = NULL;
  685. int found = 0;
  686. PPCODE:
  687. if (!msg) {
  688. LM_ERR("Invalid message reference\n");
  689. } else {
  690. parse_headers(msg, ~0, 0);
  691. for (hf = msg->headers; hf; hf = hf->next) {
  692. found = 1;
  693. XPUSHs(sv_2mortal(newSVpv(hf->name.s, hf->name.len)));
  694. }
  695. }
  696. if (!found) {
  697. XPUSHs(&PL_sv_undef);
  698. }
  699. =head2 moduleFunction(func,string1,string2)
  700. Search for an arbitrary function in module exports and call it with the
  701. parameters self, string1, string2.
  702. C<string1> and/or C<string2> may be omitted.
  703. As this function provides access to the functions that are exported to the
  704. Kamailio configuration file, it is autoloaded for unknown functions. Instead of
  705. writing
  706. $m->moduleFunction("sl_send_reply", "500", "Internal Error");
  707. $m->moduleFunction("xlog", "L_INFO", "foo");
  708. you may as well write
  709. $m->sl_send_reply("500", "Internal Error");
  710. $m->xlog("L_INFO", "foo");
  711. WARNING
  712. In Kamailio 1.2, only a limited subset of module functions is available. This
  713. restriction will be removed in a later version.
  714. Here is a list of functions that are expected to be working (not claiming
  715. completeness):
  716. * alias_db_lookup
  717. * consume_credentials
  718. * is_rpid_user_e164
  719. * append_rpid_hf
  720. * bind_auth
  721. * avp_print
  722. * cpl_process_register
  723. * cpl_process_register_norpl
  724. * load_dlg
  725. * ds_next_dst
  726. * ds_next_domain
  727. * ds_mark_dst
  728. * ds_mark_dst
  729. * is_from_local
  730. * is_uri_host_local
  731. * dp_can_connect
  732. * dp_apply_policy
  733. * enum_query (without parameters)
  734. * enum_fquery (without parameters)
  735. * is_from_user_enum (without parameters)
  736. * i_enum_query (without parameters)
  737. * imc_manager
  738. * jab_* (all functions from the jabber module)
  739. * load_gws (without parameters)
  740. * next_gw
  741. * from_gw (without parameters)
  742. * to_gw (without parameters)
  743. * sdp_mangle_ip
  744. * sdp_mangle_port
  745. * encode_contact
  746. * decode_contact
  747. * decode_contact_header
  748. * fix_contact
  749. * use_media_proxy
  750. * end_media_session
  751. * m_store
  752. * m_dump
  753. * fix_nated_contact
  754. * unforce_rtp_proxy
  755. * force_rtp_proxy
  756. * fix_nated_register
  757. * add_rcv_param
  758. * options_reply
  759. * checkospheader
  760. * validateospheader
  761. * requestosprouting
  762. * checkosproute
  763. * prepareosproute
  764. * prepareallosproutes
  765. * checkcallingtranslation
  766. * reportospusage
  767. * mangle_pidf
  768. * mangle_message_cpim
  769. * add_path (without parameters)
  770. * add_path_received (without parameters)
  771. * prefix2domain
  772. * allow_routing (without parameters)
  773. * allow_trusted
  774. * pike_check_req
  775. * handle_publish
  776. * handle_subscribe
  777. * stored_pres_info
  778. * bind_pua
  779. * send_publish
  780. * send_subscribe
  781. * pua_set_publish
  782. * loose_route
  783. * record_route
  784. * load_rr
  785. * sip_trace
  786. * sl_reply_error
  787. * sms_send_msg
  788. * sd_lookup
  789. * sstCheckMin
  790. * append_time
  791. * has_body (without parameters)
  792. * is_peer_verified
  793. * t_newtran
  794. * t_release
  795. * t_relay (without parameters)
  796. * t_flush_flags
  797. * t_check_trans
  798. * t_was_cancelled
  799. * t_load_contacts
  800. * t_next_contacts
  801. * uac_restore_from
  802. * uac_auth
  803. * has_totag
  804. * tel2sip
  805. * check_to
  806. * check_from
  807. * radius_does_uri_exist
  808. * ul_* (All functions exported by the usrloc module for user access)
  809. * xmpp_send_message
  810. =cut
  811. int
  812. moduleFunction (self, func, string1 = NULL, string2 = NULL)
  813. SV *self;
  814. char *func;
  815. char *string1;
  816. char *string2;
  817. PREINIT:
  818. struct sip_msg *msg = sv2msg(self);
  819. int retval; /* Return value of called function */
  820. int ret; /* Return value of moduleFunc - < 0 for "non existing function" and other errors */
  821. INIT:
  822. CODE:
  823. LM_DBG("Calling exported func '%s', Param1 is '%s',"
  824. " Param2 is '%s'\n", func, string1, string2);
  825. ret = moduleFunc(msg, func, string1, string2, &retval);
  826. if (ret < 0) {
  827. LM_ERR("calling module function '%s' failed."
  828. " Missing loadmodule?\n", func);
  829. retval = -1;
  830. }
  831. RETVAL = retval;
  832. OUTPUT:
  833. RETVAL
  834. =head2 log(level,message) (deprecated type)
  835. Logs the message with Kamailio's logging facility. The logging level
  836. is one of the following:
  837. * L_ALERT
  838. * L_CRIT
  839. * L_ERR
  840. * L_WARN
  841. * L_NOTICE
  842. * L_INFO
  843. * L_DBG
  844. The logging function should be accessed via the Kamailio module variant. This
  845. one, located in Kamailio::Message, is deprecated.
  846. =cut
  847. void
  848. log(self, level, log)
  849. SV *self
  850. int level
  851. char *log
  852. PREINIT:
  853. INIT:
  854. CODE:
  855. switch (level) {
  856. case L_ALERT: LM_ALERT("%s", log); break;
  857. case L_CRIT: LM_CRIT("%s", log); break;
  858. case L_ERR: LM_ERR("%s", log); break;
  859. case L_WARN: LM_WARN("%s", log); break;
  860. case L_NOTICE: LM_NOTICE("%s", log); break;
  861. case L_INFO: LM_INFO("%s", log); break;
  862. default: LM_DBG("%s", log); break;
  863. }
  864. =head2 rewrite_ruri(newruri)
  865. Sets a new destination (recipient) URI. Useful for rerouting the
  866. current message/call.
  867. if ($m->getRURI() =~ m/\@somedomain.net/) {
  868. $m->rewrite_ruri("sip:dispatcher\@organization.net");
  869. }
  870. =cut
  871. int
  872. rewrite_ruri(self, newruri)
  873. SV *self;
  874. char *newruri;
  875. PREINIT:
  876. struct sip_msg *msg = sv2msg(self);
  877. INIT:
  878. CODE:
  879. if (!msg) {
  880. LM_ERR("Invalid message reference\n");
  881. RETVAL = -1;
  882. } else {
  883. if (getType(msg) != SIP_REQUEST) {
  884. LM_ERR("Not a Request. RURI rewrite unavailable.\n");
  885. RETVAL = -1;
  886. } else {
  887. LM_DBG("New R-URI is [%s]\n", newruri);
  888. RETVAL = rewrite_ruri(msg, newruri);
  889. }
  890. }
  891. OUTPUT:
  892. RETVAL
  893. =head2 setFlag(flag)
  894. Sets a message flag. The constants as known from the C API may be used,
  895. when Constants.pm is included.
  896. =cut
  897. int
  898. setFlag(self, flag)
  899. SV *self;
  900. unsigned int flag;
  901. PREINIT:
  902. struct sip_msg *msg = sv2msg(self);
  903. INIT:
  904. CODE:
  905. if (!msg) {
  906. LM_ERR("Invalid message reference\n");
  907. RETVAL = -1;
  908. } else {
  909. RETVAL = setflag(msg, flag);
  910. }
  911. OUTPUT:
  912. RETVAL
  913. =head2 resetFlag(flag)
  914. Resets a message flag.
  915. =cut
  916. int
  917. resetFlag(self, flag)
  918. SV *self;
  919. unsigned int flag;
  920. PREINIT:
  921. struct sip_msg *msg = sv2msg(self);
  922. INIT:
  923. CODE:
  924. if (!msg) {
  925. LM_ERR("Invalid message reference\n");
  926. RETVAL = -1;
  927. } else {
  928. RETVAL = resetflag(msg, flag);
  929. }
  930. OUTPUT:
  931. RETVAL
  932. =head2 isFlagSet(flag)
  933. Returns whether a message flag is set or not.
  934. =cut
  935. int
  936. isFlagSet(self, flag)
  937. SV *self;
  938. unsigned int flag;
  939. PREINIT:
  940. struct sip_msg *msg = sv2msg(self);
  941. INIT:
  942. CODE:
  943. if (!msg) {
  944. LM_ERR("Invalid message reference\n");
  945. RETVAL = -1;
  946. } else {
  947. RETVAL = isflagset(msg, flag) == 1 ? 1 : 0;
  948. }
  949. OUTPUT:
  950. RETVAL
  951. =head2 pseudoVar(string)
  952. Returns a new string where all pseudo variables are substituted by their values.
  953. Can be used to receive the values of single variables, too.
  954. B<Please remember that you need to escape the '$' sign in perl strings!>
  955. =cut
  956. SV *
  957. pseudoVar(self, varstring)
  958. SV *self;
  959. char *varstring;
  960. PREINIT:
  961. struct sip_msg *msg = sv2msg(self);
  962. char *ret;
  963. CODE:
  964. if (!msg) {
  965. LM_ERR("Invalid message reference\n");
  966. ST(0) = &PL_sv_undef;
  967. } else {
  968. ret = pv_sprintf(msg, varstring);
  969. if (ret) {
  970. ST(0) = sv_2mortal(newSVpv(ret, strlen(ret)));
  971. free(ret);
  972. } else {
  973. ST(0) = &PL_sv_undef;
  974. }
  975. }
  976. =head2 append_branch(branch,qval)
  977. Append a branch to current message.
  978. =cut
  979. int
  980. append_branch(self, branch = NULL, qval = NULL)
  981. SV *self;
  982. char *branch;
  983. char *qval;
  984. PREINIT:
  985. struct sip_msg *msg = sv2msg(self);
  986. qvalue_t q = Q_UNSPECIFIED;
  987. str b = {0, 0};
  988. INIT:
  989. CODE:
  990. if (!msg) {
  991. LM_ERR("Invalid message reference\n");
  992. RETVAL = -1;
  993. } else {
  994. if (qval) {
  995. if (str2q(&q, qval, strlen(qval)) < 0) {
  996. LM_ERR("append_branch: Bad q value.");
  997. } else { /* branch and qval set */
  998. b.s = branch;
  999. b.len = strlen(branch);
  1000. }
  1001. } else {
  1002. if (branch) { /* branch set, qval unset */
  1003. b.s = branch;
  1004. b.len = strlen(branch);
  1005. }
  1006. }
  1007. RETVAL = km_append_branch(msg, (b.s!=0)?&b:0, 0, 0, q, 0, 0);
  1008. }
  1009. OUTPUT:
  1010. RETVAL
  1011. =head2 getParsedRURI()
  1012. Returns the current destination URI as an Kamailio::URI object.
  1013. =cut
  1014. SV *
  1015. getParsedRURI(self)
  1016. SV *self;
  1017. PREINIT:
  1018. struct sip_msg *msg = sv2msg(self);
  1019. struct sip_uri *uri;
  1020. SV *ret;
  1021. INIT:
  1022. CODE:
  1023. if (!msg) {
  1024. LM_ERR("Invalid message reference\n");
  1025. ST(0) = NULL;
  1026. } else {
  1027. parse_sip_msg_uri(msg);
  1028. parse_headers(msg, ~0, 0);
  1029. uri = &(msg->parsed_uri);
  1030. ret = sv_newmortal();
  1031. sv_setref_pv(ret, "Kamailio::URI", (void *)uri);
  1032. SvREADONLY_on(SvRV(ret));
  1033. ST(0) = ret;
  1034. }
  1035. MODULE = Kamailio PACKAGE = Kamailio::URI
  1036. =head1 Kamailio::URI
  1037. This package provides functions for access to sip_uri structures.
  1038. =cut
  1039. =head2 user()
  1040. Returns the user part of this URI.
  1041. =cut
  1042. SV *
  1043. user(self)
  1044. SV *self;
  1045. CODE:
  1046. ST(0) = getStringFromURI(self, XS_URI_USER);
  1047. =head2 host()
  1048. Returns the host part of this URI.
  1049. =cut
  1050. SV *
  1051. host(self)
  1052. SV *self;
  1053. CODE:
  1054. ST(0) = getStringFromURI(self, XS_URI_HOST);
  1055. =head2 passwd()
  1056. Returns the passwd part of this URI.
  1057. =cut
  1058. SV *
  1059. passwd(self)
  1060. SV *self;
  1061. CODE:
  1062. ST(0) = getStringFromURI(self, XS_URI_PASSWD);
  1063. =head2 port()
  1064. Returns the port part of this URI.
  1065. =cut
  1066. SV *
  1067. port(self)
  1068. SV *self;
  1069. CODE:
  1070. ST(0) = getStringFromURI(self, XS_URI_PORT);
  1071. =head2 params()
  1072. Returns the params part of this URI.
  1073. =cut
  1074. SV *
  1075. params(self)
  1076. SV *self;
  1077. CODE:
  1078. ST(0) = getStringFromURI(self, XS_URI_PARAMS);
  1079. =head2 headers()
  1080. Returns the headers part of this URI.
  1081. =cut
  1082. SV *
  1083. headers(self)
  1084. SV *self;
  1085. CODE:
  1086. ST(0) = getStringFromURI(self, XS_URI_HEADERS);
  1087. =head2 transport()
  1088. Returns the transport part of this URI.
  1089. =cut
  1090. SV *
  1091. transport(self)
  1092. SV *self;
  1093. CODE:
  1094. ST(0) = getStringFromURI(self, XS_URI_TRANSPORT);
  1095. =head2 ttl()
  1096. Returns the ttl part of this URI.
  1097. =cut
  1098. SV *
  1099. ttl(self)
  1100. SV *self;
  1101. CODE:
  1102. ST(0) = getStringFromURI(self, XS_URI_TTL);
  1103. =head2 user_param()
  1104. Returns the user_param part of this URI.
  1105. =cut
  1106. SV *
  1107. user_param(self)
  1108. SV *self;
  1109. CODE:
  1110. ST(0) = getStringFromURI(self, XS_URI_USER_PARAM);
  1111. =head2 maddr()
  1112. Returns the maddr part of this URI.
  1113. =cut
  1114. SV *
  1115. maddr(self)
  1116. SV *self;
  1117. CODE:
  1118. ST(0) = getStringFromURI(self, XS_URI_MADDR);
  1119. =head2 method()
  1120. Returns the method part of this URI.
  1121. =cut
  1122. SV *
  1123. method(self)
  1124. SV *self;
  1125. CODE:
  1126. ST(0) = getStringFromURI(self, XS_URI_METHOD);
  1127. =head2 lr()
  1128. Returns the lr part of this URI.
  1129. =cut
  1130. SV *
  1131. lr(self)
  1132. SV *self;
  1133. CODE:
  1134. ST(0) = getStringFromURI(self, XS_URI_LR);
  1135. =head2 r2()
  1136. Returns the r2 part of this URI.
  1137. =cut
  1138. SV *
  1139. r2(self)
  1140. SV *self;
  1141. CODE:
  1142. ST(0) = getStringFromURI(self, XS_URI_R2);
  1143. =head2 transport_val()
  1144. Returns the transport_val part of this URI.
  1145. =cut
  1146. SV *
  1147. transport_val(self)
  1148. SV *self;
  1149. CODE:
  1150. ST(0) = getStringFromURI(self, XS_URI_TRANSPORT_VAL);
  1151. =head2 ttl_val()
  1152. Returns the ttl_val part of this URI.
  1153. =cut
  1154. SV *
  1155. ttl_val(self)
  1156. SV *self;
  1157. CODE:
  1158. ST(0) = getStringFromURI(self, XS_URI_TTL_VAL);
  1159. =head2 user_param_val()
  1160. Returns the user_param_val part of this URI.
  1161. =cut
  1162. SV *
  1163. user_param_val(self)
  1164. SV *self;
  1165. CODE:
  1166. ST(0) = getStringFromURI(self, XS_URI_USER_PARAM_VAL);
  1167. =head2 maddr_val()
  1168. Returns the maddr_val part of this URI.
  1169. =cut
  1170. SV *
  1171. maddr_val(self)
  1172. SV *self;
  1173. CODE:
  1174. ST(0) = getStringFromURI(self, XS_URI_MADDR_VAL);
  1175. =head2 method_val()
  1176. Returns the method_val part of this URI.
  1177. =cut
  1178. SV *
  1179. method_val(self)
  1180. SV *self;
  1181. CODE:
  1182. ST(0) = getStringFromURI(self, XS_URI_METHOD_VAL);
  1183. =head2 lr_val()
  1184. Returns the lr_val part of this URI.
  1185. =cut
  1186. SV *
  1187. lr_val(self)
  1188. SV *self;
  1189. CODE:
  1190. ST(0) = getStringFromURI(self, XS_URI_LR_VAL);
  1191. =head2 r2_val()
  1192. Returns the r2_val part of this URI.
  1193. =cut
  1194. SV *
  1195. r2_val(self)
  1196. SV *self;
  1197. CODE:
  1198. ST(0) = getStringFromURI(self, XS_URI_R2_VAL);
  1199. =head1 Kamailio::AVP
  1200. This package provides access functions for Kamailio's AVPs.
  1201. These variables can be created, evaluated, modified and removed through this
  1202. package.
  1203. Please note that these functions do NOT support the notation used
  1204. in the configuration file, but directly work on strings or numbers. See
  1205. documentation of add method below.
  1206. =cut
  1207. MODULE = Kamailio PACKAGE = Kamailio::AVP
  1208. =head2 add(name,val)
  1209. Add an AVP.
  1210. Add an Kamailio AVP to its environment. name and val may both be integers or
  1211. strings; this function will try to guess what is correct. Please note that
  1212. Kamailio::AVP::add("10", "10")
  1213. is something different than
  1214. Kamailio::AVP::add(10, 10)
  1215. due to this evaluation: The first will create _string_ AVPs with the name
  1216. 10, while the latter will create a numerical AVP.
  1217. You can modify/overwrite AVPs with this function.
  1218. =cut
  1219. int
  1220. add(p_name, p_val)
  1221. SV *p_name;
  1222. SV *p_val;
  1223. PREINIT:
  1224. int_str name;
  1225. int_str val;
  1226. unsigned short flags = 0;
  1227. char *s;
  1228. STRLEN len;
  1229. CODE:
  1230. RETVAL = 0;
  1231. if (SvOK(p_name) && SvOK(p_val)) {
  1232. if (!sv2int_str(p_name, &name, &flags, AVP_NAME_STR)) {
  1233. RETVAL = -1;
  1234. } else if (!sv2int_str(p_val, &val, &flags, AVP_VAL_STR)) {
  1235. RETVAL = -1;
  1236. }
  1237. if (RETVAL == 0) {
  1238. RETVAL = add_avp(flags, name, val);
  1239. }
  1240. }
  1241. OUTPUT:
  1242. RETVAL
  1243. =head2 get(name)
  1244. get an Kamailio AVP:
  1245. my $numavp = Kamailio::AVP::get(5);
  1246. my $stravp = Kamailio::AVP::get("foo");
  1247. =cut
  1248. int
  1249. get(p_name)
  1250. SV *p_name;
  1251. PREINIT:
  1252. struct usr_avp *first_avp;
  1253. int_str name;
  1254. int_str val;
  1255. unsigned short flags = 0;
  1256. SV *ret = &PL_sv_undef;
  1257. int err = 0;
  1258. char *s;
  1259. STRLEN len;
  1260. CODE:
  1261. if (SvOK(p_name)) {
  1262. if (!sv2int_str(p_name, &name, &flags, AVP_NAME_STR)) {
  1263. LM_ERR("AVP:get: Invalid name.");
  1264. err = 1;
  1265. }
  1266. } else {
  1267. LM_ERR("AVP:get: Invalid name.");
  1268. err = 1;
  1269. }
  1270. if (err == 0) {
  1271. first_avp = search_first_avp(flags, name, &val, NULL);
  1272. if (first_avp != NULL) { /* found correct AVP */
  1273. if (is_avp_str_val(first_avp)) {
  1274. ret = sv_2mortal(newSVpv(val.s.s, val.s.len));
  1275. } else {
  1276. ret = sv_2mortal(newSViv(val.n));
  1277. }
  1278. } else {
  1279. /* Empty AVP requested. */
  1280. }
  1281. }
  1282. ST(0) = ret;
  1283. =head2 destroy(name)
  1284. Destroy an AVP.
  1285. Kamailio::AVP::destroy(5);
  1286. Kamailio::AVP::destroy("foo");
  1287. =cut
  1288. int
  1289. destroy(p_name)
  1290. SV *p_name;
  1291. PREINIT:
  1292. struct usr_avp *first_avp;
  1293. int_str name;
  1294. int_str val;
  1295. unsigned short flags = 0;
  1296. SV *ret = &PL_sv_undef;
  1297. char *s;
  1298. STRLEN len;
  1299. CODE:
  1300. RETVAL = 1;
  1301. if (SvOK(p_name)) {
  1302. if (!sv2int_str(p_name, &name, &flags, AVP_NAME_STR)) {
  1303. RETVAL = 0;
  1304. LM_ERR("AVP:destroy: Invalid name.");
  1305. }
  1306. } else {
  1307. RETVAL = 0;
  1308. LM_ERR("VP:destroy: Invalid name.");
  1309. }
  1310. if (RETVAL == 1) {
  1311. first_avp = search_first_avp(flags, name, &val, NULL);
  1312. if (first_avp != NULL) { /* found correct AVP */
  1313. destroy_avp(first_avp);
  1314. } else {
  1315. RETVAL = 0;
  1316. /* Empty AVP requested. */
  1317. }
  1318. }
  1319. OUTPUT:
  1320. RETVAL