app_perl_admin.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?xml version="1.0" encoding='ISO-8859-1'?>
  2. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
  4. <!-- Include general documentation entities -->
  5. <!ENTITY % docentities SYSTEM "../../../docbook/entities.xml">
  6. %docentities;
  7. ]>
  8. <!-- Module User's Guide -->
  9. <chapter>
  10. <title>&adminguide;</title>
  11. <section>
  12. <title>Overview</title>
  13. <para>
  14. The time needed when writing a new &kamailio; module unfortunately is quite high, while the
  15. options provided by the configuration file are limited to the features implemented in the
  16. modules.
  17. </para>
  18. <para>
  19. With this Perl module, you can easily implement your own &kamailio; extensions in Perl. This allows
  20. for simple access to the full world of CPAN modules. SIP URI rewriting could be implemented
  21. based on regular expressions; accessing arbitrary data backends, e.g. LDAP or Berkeley DB files,
  22. is now extremely simple.
  23. </para>
  24. </section>
  25. <section>
  26. <title>Installing the module</title>
  27. <para>
  28. This Perl module is loaded in &kamailioconfig; (just like all the other modules) with
  29. loadmodule("/path/to/perl.so");.
  30. </para>
  31. <para>
  32. For the Perl module to compile, you need a reasonably recent version of perl (tested
  33. with 5.8.8) linked dynamically. It is strongly advised to use a threaded version.
  34. The default binary packages from your favorite Linux distribution should work fine.
  35. </para>
  36. <para>
  37. Cross compilation is supported by the Makefile. You need to set the environment variables
  38. PERLLDOPTS, PERLCCOPTS and TYPEMAP to values similar to the output of
  39. </para>
  40. <programlisting format="linespecific">
  41. PERLLDOPTS: perl -MExtUtils::Embed -e ldopts
  42. PERLCCOPTS: perl -MExtUtils::Embed -e ccopts
  43. TYPEMAP: echo "`perl -MConfig -e 'print $Config{installprivlib}'`/ExtUtils/typemap"
  44. </programlisting>
  45. <para>
  46. The exact position of your (precompiled!) perl libraries depends on the setup of your
  47. environment.
  48. </para>
  49. </section>
  50. <section>
  51. <title>Using the module</title>
  52. <para>
  53. The Perl module has two interfaces: The perl side, and the &kamailio; side. Once a Perl
  54. function is defined and loaded via the module parameters (see below), it may be
  55. called in &kamailio;'s configuration at an arbitary point. E.g., you could write
  56. a function "ldap_alias" in Perl, and then execute <programlisting format="linespecific">
  57. ...
  58. if (perl_exec("ldap_alias")) {
  59. ...
  60. }
  61. ...
  62. </programlisting>
  63. just as you would have done with the current alias_db module.
  64. </para>
  65. <para>
  66. The functions you can use are listed in the "Exported Functions" section below.
  67. </para>
  68. <para>
  69. On the Perl side, there are a number of functions that let you read and modify the
  70. current SIP message, such as the RURI or the message flags. An introduction
  71. to the Perl interface and the full reference documentation can be found below.
  72. </para>
  73. </section>
  74. <section>
  75. <title>Dependencies</title>
  76. <section>
  77. <title>&kamailio; Modules</title>
  78. <para>
  79. The following modules must be loaded before this module:
  80. <itemizedlist>
  81. <listitem>
  82. <para>
  83. The "sl" module is needed for sending replies uppon fatal errors. All other modules
  84. can be accessed from the Perl module, though.
  85. </para>
  86. </listitem>
  87. </itemizedlist>
  88. </para>
  89. </section>
  90. <section>
  91. <title>External Libraries or Applications</title>
  92. <para>
  93. The following libraries or applications must be installed before
  94. running &kamailio; with this module loaded:
  95. <itemizedlist>
  96. <listitem>
  97. <para>
  98. <emphasis>Perl 5.8.x or later</emphasis>
  99. </para>
  100. </listitem>
  101. </itemizedlist>
  102. Additionally, a number of perl modules should be installed. The &kamailio;::LDAPUtils package
  103. relies on Net::LDAP to be installed. One of the sample scripts needs IPC::Shareable
  104. </para>
  105. <para>
  106. This module has been developed and tested with Perl 5.8.8, but should work with any
  107. 5.8.x release. Compilation is possible with 5.6.x, but its behavior is unsupported.
  108. Earlier versions do not work.
  109. </para>
  110. <para>
  111. On current Debian systems, at least the following packages should be installed:
  112. </para>
  113. <itemizedlist>
  114. <listitem><para>perl</para></listitem>
  115. <listitem><para>perl-base</para></listitem>
  116. <listitem><para>perl-modules</para></listitem>
  117. <listitem><para>libperl5.8</para></listitem>
  118. <listitem><para>libperl-dev</para></listitem>
  119. <listitem><para>libnet-ldap-perl</para></listitem>
  120. <listitem><para>libipc-shareable-perl</para></listitem>
  121. </itemizedlist>
  122. <para>
  123. It was reported that other Debian-style distributions (such as Ubuntu) need the
  124. same packages.
  125. </para>
  126. <para>
  127. On SuSE systems, at least the following packages should be installed:
  128. </para>
  129. <itemizedlist>
  130. <listitem><para>perl</para></listitem>
  131. <listitem><para>perl-ldap</para></listitem>
  132. <listitem><para>IPC::Shareable perl module from CPAN</para></listitem>
  133. </itemizedlist>
  134. <para>
  135. Although SuSE delivers a lot of perl modules, others may have to be fetched
  136. from CPAN. Consider using the program <quote>cpan2rpm</quote> - which, in turn,
  137. is available on CPAN. It creates RPM files from CPAN.
  138. </para>
  139. </section>
  140. </section>
  141. <section>
  142. <title>Parameters</title>
  143. <section id="app_perl.p.filename">
  144. <title><varname>filename</varname> (string)</title>
  145. <para>
  146. This is the file name of your script. This may be set once only, but it may include an arbitary
  147. number of functions and <quote>use</quote> as many Perl module as necessary.
  148. </para>
  149. <para>
  150. <emphasis>
  151. Must not be empty!
  152. </emphasis>
  153. </para>
  154. <example>
  155. <title>Set <varname>filename</varname> parameter</title>
  156. <programlisting format="linespecific">
  157. ...
  158. modparam("app_perl", "filename", "/home/test/kamailio/myperl.pl")
  159. ...
  160. </programlisting>
  161. </example>
  162. </section>
  163. <section id="app_perl.p.modpath">
  164. <title><varname>modpath</varname> (string)</title>
  165. <para>
  166. The path to the Perl modules included (Kamailio.pm et.al). It is not absolutely
  167. crucial to set this path,
  168. as you <emphasis>may</emphasis> install the Modules in Perl's standard path, or update
  169. the <quote>%INC</quote> variable from within your script. Using this module parameter
  170. is the standard behavior, though. Multiple paths may be specified by separating them with
  171. a <quote>:</quote> character. The maximum is 10 paths.
  172. </para>
  173. <example>
  174. <title>Set <varname>modpath</varname> parameter</title>
  175. <programlisting format="linespecific">
  176. ...
  177. modparam("app_perl", "modpath", "/usr/local/lib/kamailio/perl/")
  178. ...
  179. </programlisting>
  180. </example>
  181. </section>
  182. <section id="app_perl.p.reset_cycles">
  183. <title><varname>reset_cycles</varname> (int)</title>
  184. <para>
  185. The number of execution cycles after which the embedded perl interpreter
  186. is reset. Sometimes is hard to track the scope of variables in all used
  187. perl modules and that can result in leaks of system memory. Resetting the
  188. interpreter cleans the memory space.
  189. </para>
  190. <para>
  191. When the interpreter is reset, the perl script is loaded again. Note that
  192. not all &kamailio; processes will reset the interpreter at the same time.
  193. Each will do it when it has executed the script for the number of
  194. reset_cycles. Also, be aware that the reset of the interpreter is taking
  195. a bit of time (in the order of tens of mili-seconds).
  196. </para>
  197. <para>
  198. Default value is <emphasis>0</emphasis> - never reset the interpreter.
  199. </para>
  200. <example>
  201. <title>Set <varname>reset_cycles</varname> parameter</title>
  202. <programlisting format="linespecific">
  203. ...
  204. modparam("app_perl", "reset_cycles", 100000)
  205. ...
  206. </programlisting>
  207. </example>
  208. </section>
  209. <section id="app_perl.p.perl_destroy_func">
  210. <title><varname>perl_destroy_func</varname> (string)</title>
  211. <para>
  212. The name of Perl function to be executed before the interpreter is
  213. re-initialized (reset -- see reset_cycles parameter) at runtime.
  214. This could be useful to clean global variables or file descriptors
  215. from the Perl script.
  216. </para>
  217. <example>
  218. <title>Set <varname>perl_destroy_func</varname> parameter</title>
  219. <programlisting format="linespecific">
  220. ...
  221. modparam("app_perl", "perl_destroy_func", "my_perl_destroy")
  222. ...
  223. </programlisting>
  224. </example>
  225. </section>
  226. </section>
  227. <section>
  228. <title>Functions</title>
  229. <section id="app_perl.f.perl_exec_simple">
  230. <title>
  231. <function moreinfo="none">perl_exec_simple(func, [param])</function>
  232. </title>
  233. <para>
  234. Calls a perl function <emphasis>without</emphasis> passing it the current SIP message.
  235. May be used for very simple simple requests that do not have to fiddle with the message
  236. themselves, but rather return information values about the environment.
  237. </para>
  238. <para>
  239. The first parameter is the function to be called.
  240. An arbitrary string may optionally be passed as a parameter.
  241. </para>
  242. <para>
  243. This function can be used from REQUEST_ROUTE, FAILURE_ROUTE, ONREPLY_ROUTE and BRANCH_ROUTE.
  244. </para>
  245. <example>
  246. <title><function>perl_exec_simple()</function> usage</title>
  247. <programlisting format="linespecific">
  248. ...
  249. if (method=="INVITE") {
  250. perl_exec_simple("dosomething", "on invite messages");
  251. };
  252. ...
  253. </programlisting>
  254. </example>
  255. </section>
  256. <section id="app_perl.f.perl_exec">
  257. <title>
  258. <function moreinfo="none">perl_exec(func, [param])</function>
  259. </title>
  260. <para>
  261. Calls a perl function <emphasis>with</emphasis> passing it the current SIP message.
  262. The SIP message is reflected by a Perl module that gives you access to the information
  263. in the current SIP message (Kamailio::Message).
  264. </para>
  265. <para>
  266. The first parameter is the function to be called.
  267. An arbitrary string may be passed as a parameter.
  268. </para>
  269. <para>
  270. This function can be used from REQUEST_ROUTE, FAILURE_ROUTE, ONREPLY_ROUTE and BRANCH_ROUTE.
  271. </para>
  272. <example>
  273. <title><function>perl_exec()</function> usage</title>
  274. <programlisting format="linespecific">
  275. ...
  276. if (perl_exec("ldapalias")) {
  277. ...
  278. };
  279. ...
  280. </programlisting>
  281. </example>
  282. </section>
  283. </section>
  284. <section>
  285. <title>RPC Commands</title>
  286. <section id="app_perl.r.set_reset_cycles">
  287. <title>
  288. <function moreinfo="none">app_perl.set_reset_cycles</function>
  289. </title>
  290. <para>
  291. Set the value of the reset_cycle. The command has one integer
  292. parameter.
  293. </para>
  294. <example>
  295. <title><function>app_perl.set_reset_cycles</function> usage</title>
  296. <programlisting format="linespecific">
  297. ...
  298. kamcmd app_perl.set_reset_cycles 20000
  299. ...
  300. </programlisting>
  301. </example>
  302. </section>
  303. <section id="app_perl.r.get_reset_cycles">
  304. <title>
  305. <function moreinfo="none">app_perl.get_reset_cycles</function>
  306. </title>
  307. <para>
  308. Return the value of the reset_cycle.
  309. </para>
  310. <example>
  311. <title><function>app_perl.get_reset_cycles</function> usage</title>
  312. <programlisting format="linespecific">
  313. ...
  314. kamcmd app_perl.get_reset_cycles
  315. ...
  316. </programlisting>
  317. </example>
  318. </section>
  319. </section>
  320. </chapter>