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