kamailio_rpc.xml 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"
  4. [ <!ENTITY % local.common.attrib
  5. "xmlns:xi CDATA #FIXED 'http://www.w3.org/2001/XInclude'">
  6. <!ENTITY % docentities SYSTEM "../../docbook/entities.xml">
  7. %docentities;
  8. ]>
  9. <!-- Include general documentation entities -->
  10. <section id="rpc.main" xmlns:xi="http://www.w3.org/2001/XInclude">
  11. <!--
  12. <sectioninfo>
  13. <releaseinfo role="cvs">$Revision$</releaseinfo>
  14. <pubdate role="cvs">$Date$</pubdate>
  15. </sectioninfo>
  16. -->
  17. <title>
  18. The Kamailio RPC Control Interface
  19. </title>
  20. <section id="rpc.overview">
  21. <title>Overview of Operation</title>
  22. <para>
  23. The RPC (Remote Procedure Call) interface is an interface for
  24. communicating with external applications. Using it an external
  25. application can call a function or procedure that will be executed
  26. inside Kamailio. Function parameters are supported as well as returning
  27. multiple values as results.
  28. </para>
  29. <para>
  30. By itself RPC consists of two APIs, one for defining RPC functions
  31. in a transport independent way (called the rpc module api) and one
  32. for implementing RPC transports.
  33. </para>
  34. <para>
  35. The RPC transports are implemented by writing a RPC
  36. transport module. The most used transport modules are
  37. <ulink url='https://www.kamailio.org/docs/modules/devel/modules/ctl/ctl.html'>
  38. <emphasis>ctl</emphasis>
  39. </ulink>,
  40. <ulink url='https://www.kamailio.org/docs/modules/devel/modules/xmlrpc/xmlrpc.html'>
  41. <emphasis>xmlrpc</emphasis>
  42. </ulink>
  43. and
  44. <ulink url='https://www.kamailio.org/docs/modules/devel/modules/jsonrpc-s/jsonrpc-s.html'>
  45. <emphasis>jsonrpc-s</emphasis>
  46. </ulink>.
  47. </para>
  48. <para>
  49. ctl implements a proprietary fast and space efficient
  50. RPC encoding over different protocols (unix sockets, UDP, TCP, fifo).
  51. </para>
  52. <para>
  53. xmlrpc uses the de-facto XML-RPC standard encoding
  54. (over HTTP TCP or TLS).
  55. </para>
  56. <para>
  57. jsonrpc-s uses the de-facto JSON-RPC standard encoding
  58. (over HTTP TCP or TLS).
  59. </para>
  60. <para>
  61. For more information about the existing transport modules, please
  62. refer to their documentation.
  63. </para>
  64. <para>
  65. When writing a RPC procedure or function, one needs only use the
  66. RPC API and it will work automatically with all the transports and
  67. encodings. One needs only to load the desired RPC transport module
  68. (e.g. xmlrpc).
  69. </para>
  70. <para>
  71. The RPC interface (or API) was created in such a way that would allow
  72. supporting XML-RPC (because XML-RPC is a de-facto standard), while in
  73. the same time being very easy to use.
  74. </para>
  75. </section>
  76. <section id="rpc.module_api">
  77. <title>Module API</title>
  78. <para>
  79. Each module can export RPC functions just like it can export
  80. parameters and functions to be called from the script. Whenever &kamailio;
  81. receives an RPC request, it will search through the list of
  82. exported RPC functions and the function with matching name will be
  83. executed. A couple of essential RPC functions are also embedded into
  84. the SIP server core.
  85. </para>
  86. <para>
  87. This section gives a detailed overview of the whole RPC API. <xref
  88. linkend="rpc.rpc_functions"/> describes the prototype and
  89. conventions used in RPC functions. <xref linkend="rpc.data_types"/>
  90. gives a detailed overview of available data types that can be used
  91. in function parameters and return value. <xref
  92. linkend="rpc.getting_parameters"/> describes functions of the RPC
  93. API that can be used to retrieve parameters of the function, and
  94. finally <xref linkend="rpc.creating_result"/> describes functions of
  95. the API that can be used to build the result value that will be
  96. sent in the reply to the caller.
  97. </para>
  98. <para>
  99. The whole RPC API is described in header file
  100. <filename>kamailio/rpc.h</filename>. This file defines the set
  101. of functions that must be implemented by RPC transport modules, as
  102. described in <xref linkend="rpc.new_transport"/>, prototypes of RPC
  103. functions and structures used for the communication between RPC
  104. transport modules and ordinary modules exporting RPC functions.
  105. </para>
  106. <section id="rpc.rpc_functions">
  107. <title>RPC Functions</title>
  108. <para>
  109. RPC functions are standard C functions with the following
  110. prototype:
  111. <programlisting>
  112. typedef void (*rpc_function_t)(rpc_t* rpc, void* ctx);
  113. </programlisting>
  114. RPC functions take two parameters, first parameter is a pointer
  115. to rpc_t structure and the context. The rpc_t structure
  116. contains references to all API functions available to the RPC
  117. function as well as all data necessary to create the
  118. response. RPC functions do not return any value, instead the
  119. return value is created using functions from the context. The
  120. motivation for this decision is the fact that RPC functions
  121. should always return a response and even the API functions
  122. called from RPC functions should have the possibility to
  123. indicate an error (and should not rely on RPC functions doing so).
  124. </para>
  125. <para>
  126. If no reply is sent explicitely, the RPC transport module will
  127. automatically send a "success" reply (e.g. 200 OK for XML-RPC)
  128. when the RPC function finishes.
  129. If no values are added to the response, the reponse will be an
  130. empty "success" reply (e.g. a 200 OK with empty body for
  131. XML-RPC).
  132. RPC API functions will automatically send an error reply upon a
  133. failure.
  134. </para>
  135. <para>
  136. Each RPC function has associated an array of documentation
  137. strings. The purpose of the documentation strings is to give a
  138. short overview of the function, accepted parameters, and format
  139. of the reply. By convention the name of the documentation string
  140. array is same as the name of the function with "_doc" suffix.
  141. </para>
  142. <para>
  143. Each module containing RPC functions has to export all the
  144. RPC functions to the &kamailio; core in order to make them visible to the RPC
  145. transport modules.
  146. The export process involves a <emphasis>rpc_export_t</emphasis>
  147. structure (either by itself or in an array):
  148. <programlisting>
  149. <emphasis>
  150. typedef struct rpc_export {
  151. const char* name; /* Name of the RPC function (null terminated) */
  152. rpc_function_t function; /* Pointer to the function */
  153. const char** doc_str; /* Documentation strings, method signature and description */
  154. unsigned int flags; /* Various flags, reserved for future use */
  155. } rpc_export_t;
  156. </emphasis>
  157. </programlisting>
  158. </para>
  159. <para>
  160. The <varname>flags</varname> attribute of the
  161. <varname>rpc_export</varname> structure is reserved for future
  162. use and is currently unused.
  163. </para>
  164. <para>
  165. There are several ways of exporting the RPC functions to the &kamailio; core:
  166. <itemizedlist>
  167. <listitem><para>
  168. register a null terminated array of rpc_export_t structures
  169. using the <function>rpc_register_array()</function> function
  170. (defined in rpc_lookup.h), from the module init function
  171. (mod_init()). This is the <emphasis>recommended</emphasis>
  172. method for all the new modules.
  173. <example><title>usrloc RPC Exports Declaration</title>
  174. <para>
  175. The <varname>rpc_export_t</varname> array for the modules_s/usrloc
  176. module looks like:
  177. </para>
  178. <programlisting>
  179. <emphasis>
  180. rpc_export_t ul_rpc[] = {
  181. {"usrloc.statistics", rpc_stats, rpc_stats_doc, 0},
  182. {"usrloc.delete_aor", rpc_delete_aor, rpc_delete_aor_doc, 0},
  183. {"usrloc.delete_contact", rpc_delete_contact, rpc_delete_contact_doc, 0},
  184. {"usrloc.dump", rpc_dump, rpc_dump_doc, 0},
  185. {"usrloc.flush", rpc_flush, rpc_flush_doc, 0},
  186. {"usrloc.add_contact", rpc_add_contact, rpc_add_contact_doc, 0},
  187. {"usrloc.show_contacts", rpc_show_contacts, rpc_show_contacts_doc, 0},
  188. {0, 0, 0, 0}
  189. };
  190. </emphasis>
  191. </programlisting>
  192. <para>
  193. To register it from the module init function one would use
  194. something similar to:
  195. </para>
  196. <programlisting>
  197. if (rpc_register_array(ul_rpc) != 0) {
  198. ERR("failed to register RPC commands\n");
  199. return -1;
  200. }
  201. </programlisting>
  202. </example>
  203. </para></listitem>
  204. <listitem><para>
  205. register RPCs one by one using the
  206. <function>rpc_register_function()</function>
  207. (defined in rpc_lookup.h), from the module init function.
  208. </para></listitem>
  209. <listitem><para>
  210. register a null terminated array of rpc_export_t structures
  211. using the &kamailio; module interface SER_MOD_INTERFACE
  212. For this purpose, the
  213. <varname>module_exports</varname> structure of the &kamailio; module API
  214. contains a new attribute called <varname>rpc_methods</varname>:
  215. <programlisting>
  216. struct module_exports {
  217. char* name; /* null terminated module name */
  218. cmd_export_t* cmds; /* null terminated array of the exported commands */
  219. <emphasis>rpc_export_t* rpc_methods;</emphasis> /* null terminated array of exported rpc methods */
  220. param_export_t* params; /* null terminated array of the exported module parameters */
  221. init_function init_f; /* Initialization function */
  222. response_function response_f; /* function used for responses */
  223. destroy_function destroy_f; /* function called upon shutdown */
  224. onbreak_function onbreak_f;
  225. child_init_function init_child_f; /* function called by all processes after the fork */
  226. };
  227. </programlisting>
  228. <varname>rpc_methods</varname> is a pointer to an array of
  229. rpc_export_t structures. The last element of the array is a
  230. bumper containing zeroes in all the attributes of the
  231. structure. The following program listing shows the exported RPC
  232. functions of the modules_s/usrloc module, using the rpc_export_t array
  233. <emphasis>ul_rpc</emphasis> defined above, in the
  234. rpc_register_array() example:
  235. <example><title>usrloc Module Exports Declaration</title>
  236. <programlisting>
  237. struct module_exports exports = {
  238. "usrloc",
  239. cmds, /* Exported functions */
  240. <emphasis>ul_rpc</emphasis>, /* RPC methods */
  241. params, /* Export parameters */
  242. mod_init, /* Module initialization function */
  243. 0, /* Response function */
  244. destroy, /* Destroy function */
  245. 0, /* OnCancel function */
  246. child_init /* Child initialization function */ };
  247. </programlisting>
  248. </example>
  249. <note><para>
  250. This mode works only with modules using the SER flavour module
  251. interface. It does not work for &kamailio; modules and it
  252. will probably not work for future sip-router modules.
  253. <emphasis>It is now completely obselete.</emphasis>
  254. The correct way is to use instead the
  255. <function>rpc_register_array()</function> function.
  256. </para></note>
  257. </para></listitem>
  258. </itemizedlist>
  259. </para>
  260. <para>
  261. By convention the name of every exported function consists of
  262. two parts delimited by a dot. The first part is the name of the
  263. module or &kamailio; subsystem this function belongs to. The second
  264. part is the name of the function.
  265. </para>
  266. </section>
  267. <section id="rpc.data_types">
  268. <title>Data Types</title>
  269. <para>
  270. The RPC API defines several basic and one compound data type
  271. that can be used in communication with the caller of RPC
  272. functions. The RPC API uses formating strings to describe data
  273. types. Each data type is described by exactly one character in
  274. the formating string. For example, if an RPC function calls
  275. function <function>add</function> of the RPC API and it passes
  276. two parameters to it, the first one of type string and the
  277. second one of type integer, the function parameters will look
  278. like:
  279. <programlisting>
  280. add("sd", string_param, int_param);
  281. </programlisting>
  282. Character "s" in the formating string tells to the function
  283. that the 2nd parameter should be interpreted as string,
  284. character "d" in the formating string tells to the function
  285. that the 3rd parameter should be interpreted as signed integer.
  286. </para>
  287. <formalpara>
  288. <title>Integer</title>
  289. <para>
  290. Integer type represents a signed 32-bit
  291. integer. Corresponding character in the formating string is
  292. "d". This parameter can be stored in C-style variable with
  293. type <varname>int</varname>.
  294. </para>
  295. </formalpara>
  296. <formalpara>
  297. <title>Float</title>
  298. <para>
  299. Float type represents a signed floating point
  300. number. Corresponding character in the formating string is
  301. "f". Data of this type can be stored in C-style variables
  302. of type <varname>double</varname>.
  303. </para>
  304. </formalpara>
  305. <formalpara>
  306. <title>String</title>
  307. <para>
  308. String type represents a string of characters. The string
  309. may contain zeroes. This data type is represented by two
  310. characters in the formatting string, either "s" or "S". "s"
  311. indicates to the conversion function that the result should
  312. be stored in a variable of type <varname>char*</varname>
  313. and it should be zero terminated. "S" indicates to the
  314. conversion function that the result will be stored in
  315. a variable of type <varname>str</varname> which contains
  316. both the pointer to the beginning of the string and its
  317. length.
  318. </para>
  319. </formalpara>
  320. <formalpara>
  321. <title>Structure</title>
  322. <para>
  323. Structure is the only compound data type currently defined
  324. in the API. A structure is a collection of attributes. Each
  325. attribute is identified using name (string) and each
  326. attribute can be one of the basic data types, that
  327. is integer, float, or string. Nesting of structures is not
  328. allowed (in other words, structure attributes cannot be of
  329. type struct again). Corresponding character in the
  330. formatting string is "{".
  331. </para>
  332. </formalpara>
  333. <formalpara>
  334. <title>Optional parameters</title>
  335. <para>
  336. Optional parameters can be used, but only in the
  337. <function>scan</function> function. For optional parameters the
  338. <function>scan</function> function will not automatically generate
  339. a rpc fault if the input ends. Note that in this case the
  340. <function>scan</function> will still return a negative value
  341. (minus the number of parameters successfully read).
  342. Optional parameters can be marked in the format string by
  343. preceding the first optional parameter type with a "*".
  344. All the parameters following a "*" are considered to be optional.
  345. For example for the format string "ds*dds", the last 3 parameters
  346. (2 ints and a string) are optional.
  347. </para>
  348. </formalpara>
  349. <table>
  350. <title>Data Type Overview</title>
  351. <tgroup cols="3">
  352. <tbody>
  353. <row rowsep="1">
  354. <entry>Name</entry>
  355. <entry>Formating String Char</entry>
  356. <entry>C-Style Variable</entry>
  357. </row>
  358. <row>
  359. <entry>Integer</entry>
  360. <entry>d</entry>
  361. <entry>int</entry>
  362. </row>
  363. <row>
  364. <entry>Unsigned Integer</entry>
  365. <entry>u</entry>
  366. <entry>int</entry>
  367. </row>
  368. <row>
  369. <entry>Boolean</entry>
  370. <entry>b</entry>
  371. <entry>int</entry>
  372. </row>
  373. <row>
  374. <entry>Long</entry>
  375. <entry>l</entry>
  376. <entry>long</entry>
  377. </row>
  378. <row>
  379. <entry>Unsigned Long</entry>
  380. <entry>j</entry>
  381. <entry>unsigned long</entry>
  382. </row>
  383. <row>
  384. <entry>Long Long</entry>
  385. <entry>L</entry>
  386. <entry>long long</entry>
  387. </row>
  388. <row>
  389. <entry>Unsigned Long Long</entry>
  390. <entry>J</entry>
  391. <entry>unsigned long long</entry>
  392. </row>
  393. <row>
  394. <entry>Float</entry>
  395. <entry>f</entry>
  396. <entry>double</entry>
  397. </row>
  398. <row>
  399. <entry>String</entry>
  400. <entry>s</entry>
  401. <entry>char*</entry>
  402. </row>
  403. <row>
  404. <entry>String</entry>
  405. <entry>S</entry>
  406. <entry>str*</entry>
  407. </row>
  408. <row>
  409. <entry>time</entry>
  410. <entry>t</entry>
  411. <entry>int</entry>
  412. </row>
  413. <row>
  414. <entry>Struct</entry>
  415. <entry>{</entry>
  416. <entry>struct rpc_struct_l </entry>
  417. </row>
  418. <row>
  419. <entry>Optional modifier</entry>
  420. <entry>*</entry>
  421. <entry>marks all further parameters as optional</entry>
  422. </row>
  423. <row>
  424. <entry>Autoconvert modifier</entry>
  425. <entry>.</entry>
  426. <entry>requires auto-conversion for the next parameter</entry>
  427. </row>
  428. </tbody>
  429. </tgroup>
  430. </table>
  431. <note>
  432. <para>
  433. The <emphasis>time</emphasis> parameter is of type <emphasis>int</emphasis>. Recent OS use 64-bit time_t (typedef long), so if you are using a 64-bit OS, you should use <emphasis>l</emphasis> or <emphasis>LL</emphasis> instead of <emphasis>t</emphasis> when trying to add/read a time_t value to avoid overflowing.
  434. </para>
  435. </note>
  436. </section>
  437. <section id="rpc.getting_parameters">
  438. <title>Getting Parameters</title>
  439. <para>
  440. Each RPC function call can contain parameters. Parameters have
  441. no name, their meaning is determined by their position in the
  442. parameter set.
  443. <note>
  444. <para>
  445. You can pass all parameters to a function within a
  446. structure if you want to make them position
  447. independent. Then each parameter can be retrieved by
  448. its name regardless of its position.
  449. </para>
  450. </note>
  451. There are two functions in the RPC API that can be used to
  452. obtain function call parameters: <function>scan</function> and
  453. <function>struct_scan</function>.
  454. </para>
  455. <section id="rpc.scan">
  456. <title><function>scan</function></title>
  457. <para>
  458. Function <function>scan</function> can be used to retrieve
  459. parameters from the parameter set. The function accepts
  460. variable number of parameters. The first parameter is the
  461. formatting string that determines the type of the
  462. parameters to be retrieved. Each parameter is represented by
  463. exactly one parameter type character in the string.
  464. The variable part of parameters must contain as many pointers to C
  465. variables as there are formatting non-modifiers characters in the
  466. formatting string.
  467. <warning>
  468. <para>
  469. The function will crash if you fail to provide
  470. enough parameters.
  471. </para>
  472. </warning>
  473. </para>
  474. <para>
  475. Besides characters representing parameter types, the formatting
  476. string can contain two special modifiers: "*" and ".". The
  477. modifiers do not have a correspondent in the variable part of the
  478. parameters.
  479. </para>
  480. <para>
  481. The meaning of "*" modifier is that any further parameters
  482. (defined by other type characters in the formatting string) are
  483. optional (they can be missing in the input and no rpc fault will
  484. automatically be generated).
  485. </para>
  486. <para>
  487. The '.' modifiers turns on type autoconversion for the next
  488. parameter. This means that if the type of the next parameter
  489. differs from the type specified in the formatting string, the
  490. parameter will be automatically converted to the formatting string
  491. type (if possible) and if the automatic conversion succeeds, no
  492. fault will be generated.
  493. </para>
  494. <para>
  495. The function returns the number of parameters read on success
  496. (a number greater or equal 0) and - (minus) the number of
  497. parameters read on error (for example for an error after
  498. reading 2 parameters it will return -2).
  499. When a failure occurs (incorrect parameter type or no more
  500. parameters in the parameter set) the function will
  501. return a negative number (- number of parameters read so far)
  502. and it will also automatically change the reply that will be
  503. sent to the caller to indicate that a failure has occurred on
  504. the server (unless the "*" is used and the error is lack
  505. of more parameters).
  506. </para>
  507. <para>
  508. The prototype of the function is:
  509. <programlisting>
  510. int scan((void* ctx, char* fmt, ...)
  511. </programlisting>
  512. It is possible to either call the function once to scan all
  513. the parameters:
  514. <programlisting>
  515. rpc->scan(ctx, "sdf", &amp;string_val, &amp;int_val, &amp;double_val);
  516. </programlisting>
  517. Or you can call the same function several times and it will
  518. continue where it left off previously:
  519. <programlisting>
  520. rpc->scan(ctx, "s", &amp;string_val);
  521. rpc->scan(ctx, "d", &amp;int_val);
  522. rpc->scan(ctx, "f", &amp;double_val);
  523. </programlisting>
  524. </para>
  525. <para>
  526. </para>
  527. </section>
  528. <section>
  529. <title><function>struct_scan</function></title>
  530. <para>
  531. Function <function>struct_scan</function> can be used to
  532. retrieve named attributes from a parameter of type
  533. structure.
  534. <note><para>
  535. This function is obsolete and not implemented by all the
  536. rpc transports (e.g.: ctl / binrpc). Consider using the normal
  537. <function>scan</function> instead.
  538. </para></note>
  539. When retrieving a structure parameter from the
  540. parameter set:
  541. <programlisting>
  542. rpc->scan(ctx, "{", &amp;handle);
  543. </programlisting>
  544. The corresponding variable (named
  545. <varname>handle</varname> in the example above) will contain
  546. the index of the structure parameter within the parameter
  547. set, but the index cannot be used to retrieve the contents
  548. of the structure. To retrieve the contents of the structure
  549. you can use function <function>struct_scan</function>. The
  550. function gets the handle as the first parameter:
  551. <programlisting>
  552. rpc->struct_scan(handle, "sd", "str_attr", &amp;str_val, "int_attr", &amp;int_val);
  553. </programlisting>
  554. The second parameter is the formatting string followed by
  555. pairs of parameters. First parameter in each pair is the
  556. name of the attribute to retrieve (string) and the second
  557. parameter in each pair is the pointer to the variable to
  558. store the value of the parameter. The function returns the
  559. number of parameters (name value pairs) read on
  560. success and - number of parameters read so far on an error
  561. (just like the <function>scan</function> function). The function
  562. also indicates an error if a requested attribute is missing in
  563. the structure.
  564. </para>
  565. </section>
  566. <section><title>Retrieving Parameters Example</title>
  567. <example>
  568. <title>Retrieving Parameters</title>
  569. <programlisting>
  570. <![CDATA[
  571. static void rpc_delete_contact(rpc_t* rpc, void* ctx)
  572. {
  573. str aor, contact;
  574. char* table;
  575. void *handle;
  576. int expires;
  577. double q;
  578. if (rpc->scan(ctx, "sS{", &table, &aor, &handle) < 0) {
  579. /* Reply is set automatically by scan upon failure,
  580. * no need to do anything here
  581. */
  582. return;
  583. }
  584. if (rpc->struct_scan(handle, "Sdf", "Contact", &contact,
  585. "Expires", &expires,
  586. "Q", &q ) < 0) {
  587. /* Reply is set automatically by struct_scan upon failure,
  588. * no need to do anything here
  589. */
  590. return;
  591. }
  592. /* Process retrieved parameters here */
  593. }
  594. /* variable number of parameters:
  595. echo back all the parameters, string type required */
  596. static void core_prints(rpc_t* rpc, void* c)
  597. {
  598. char* string = 0;
  599. while((rpc->scan(c, "*s", &string)>0))
  600. rpc->add(c, "s", string);
  601. }
  602. /* variable number of parameters and auto conversion:
  603. echo back all the parameters, works with any type (everything is
  604. internally converted to string, notice the '.' modifier) */
  605. static void core_echo(rpc_t* rpc, void* c)
  606. {
  607. char* string = 0;
  608. while((rpc->scan(c, "*.s", &string)>0))
  609. rpc->add(c, "s", string);
  610. }
  611. ]]>
  612. </programlisting>
  613. </example>
  614. </section>
  615. </section>
  616. <section id="rpc.creating_result">
  617. <title>Building Reply</title>
  618. <para>
  619. The RPC API contains several functions that can be used to
  620. modify and/or send a reply. The functions use formatting
  621. strings and parameter lists just like functions described in
  622. <xref linkend="rpc.getting_parameters"/>.
  623. </para>
  624. <para>
  625. Each RPC function call must return a reply. The reply can be
  626. either a failure reply or success reply. Failure replies
  627. contain only the status code and reason phrase. Success
  628. replies can have arbitrary amount of data attached to
  629. them. Status codes 3xx, 4xx, 5xx, and 6xx indicate
  630. failures. Status code 2xx indicates success.
  631. </para>
  632. <para>
  633. The default reply is 200 OK with no data attached to it. This
  634. is what will be returned by the RPC transport module if you do
  635. not call any of the reply-related functions described in this
  636. section.
  637. <example>
  638. <title>Sending default reply</title>
  639. <programlisting>
  640. <![CDATA[
  641. static void rpc_dummy(rpc_t* rpc, void *ctx)
  642. {
  643. /* 200 OK with no data will be returned */
  644. }
  645. ]]>
  646. </programlisting>
  647. </example>
  648. </para>
  649. <section>
  650. <title>fault</title>
  651. <para>
  652. You can use <function>fault</function> function to indicate
  653. that an error has occurred on the server to the caller. The
  654. function accepts two parameters. The first parameter is the
  655. status code and the second parameter is the reason phrase.
  656. <programlisting>
  657. <![CDATA[
  658. static void rpc_my_function(rpc_t* rpc, void *ctx)
  659. {
  660. rpc->fault(ctx, 600, "Not Yet Implemented");
  661. }
  662. ]]>
  663. </programlisting>
  664. If your function first creates some result using
  665. <function>add</function>, or <function>printf</function>
  666. functions then all the data will be lost once you call
  667. <function>fault</function> function. Failure replies must
  668. not contain any data:
  669. <programlisting>
  670. <![CDATA[
  671. static void rpc_my_function(rpc_t* rpc, void *ctx)
  672. {
  673. rpc->add(ctx, "s", "result1");
  674. rpc->add(ctx, "d", variable);
  675. /* Reply created by previous functions will be
  676. * deleted and a failure reply 600 Not Yet Implemented
  677. * will be created instead
  678. */
  679. rpc->fault(ctx, 600, "Not Yet Implemented");
  680. /* You can also add data here, but that will have no
  681. * effect
  682. */
  683. rpc->add(ctx, "s", "result2");
  684. }
  685. ]]>
  686. </programlisting>
  687. Similarly you can also call <function>add</function> or
  688. <function>printf</function> functions after calling
  689. <function>fault</function>, in this case they will have no
  690. effect:
  691. <programlisting>
  692. <![CDATA[
  693. static void rpc_my_function(rpc_t* rpc, void *ctx)
  694. {
  695. rpc->fault(ctx, 600, "Not Yet Implemented");
  696. /* You can also add data here, but that will have no
  697. * effect and only 600 Not Yet Implemented will be returned
  698. */
  699. rpc->add(ctx, "s", "result2");
  700. }
  701. ]]>
  702. </programlisting>
  703. </para>
  704. </section>
  705. <section>
  706. <title>send</title>
  707. <para>
  708. RPC functions can use function <function>send</function> to
  709. explicitly send the reply. Each RPC function call generates
  710. exactly one reply. No reply will be sent after the function
  711. finishes if it already sent the reply using
  712. <function>send</function> function explicitly. This
  713. function is especially useful if the RPC function needs to
  714. perform some (potentially destructive) actions after the
  715. reply has been sent.
  716. </para>
  717. <example>
  718. <title>Kill the server</title>
  719. <programlisting>
  720. <![CDATA[
  721. static void core_kill(rpc_t* rpc, void *ctx)
  722. {
  723. int sig_no;
  724. if (rpc->scan(ctx, "d", &sig_no) < 0) return;
  725. rpc->send(ctx, ); /* First send a reply */
  726. kill(0, sig_no); /* Then kill the server */
  727. }
  728. ]]>
  729. </programlisting>
  730. </example>
  731. </section>
  732. <section>
  733. <title>add</title>
  734. <para>
  735. Function <function>add</function> can be used to add
  736. arbitrary data to the result set. Its parameters and use
  737. are analogical to <function>scan</function> function
  738. described in <xref linkend="rpc.scan"/>. The first
  739. parameter of the function is the formatting string that
  740. determines the types of additional parameters:
  741. <programlisting>
  742. <![CDATA[
  743. static void rpc_func(rpc_t* rpc, void *ctx)
  744. {
  745. str str_result;
  746. int int_result;
  747. void *handle;
  748. double float_result;
  749. if (rpc->add(ctx, "Sdf{", &str_result, int_result, float_result, &handle) < 0) return;
  750. }
  751. ]]>
  752. </programlisting>
  753. Naturally you can call this function several times, adding
  754. only one piece of data at a time. The function returns 0 on
  755. success and -1 on an error. In case of an error the reply
  756. is set automatically with corresponding error code and
  757. reason phrase.
  758. </para>
  759. <para>
  760. The last character in the formatting string of the function
  761. above indicates that the last data to be added will be a
  762. structure. This deserves some clarification. In this case,
  763. the function will create an empty structure and the handle
  764. to the newly created structure will be stored in
  765. <varname>handle</varname> variable (hence the last
  766. parameter is pointer to an integer). In this particular
  767. example parameters <varname>str_result</varname>,
  768. <varname>int_result</varname>, and
  769. <varname>float_result</varname> will be used for reading
  770. while parameter <varname>handle</varname> will be used for
  771. writing by the function.
  772. </para>
  773. <para>
  774. You can set the attributes of the newly created structure
  775. using <function>struct_add</function> function described in
  776. <xref linkend="rpc.struct_add"/>.
  777. </para>
  778. </section>
  779. <section>
  780. <title>rpl_printf</title>
  781. <para>
  782. <varname>rpl_printf</varname> is a convenience function. The
  783. function adds data of type string to the result set. The
  784. first parameter of the function is again a formatting
  785. string, but this time it is standard
  786. <function>printf</function>-like formatting string:
  787. <programlisting>
  788. <![CDATA[
  789. if (rpc->rpl_printf(ctx, "Unable to delete %d entries from table %s", num_entries, table_name) < 0) return;
  790. ]]>
  791. </programlisting>
  792. The return value of the function is the same as of
  793. <function>add</function> function.
  794. </para>
  795. </section>
  796. <section id="rpc.struct_add">
  797. <title>struct_add</title>
  798. <para>
  799. Function <function>struct_add</function> can be used to add
  800. attributes to a structure (created previously by
  801. <function>add</function> function). The first parameter of
  802. the function is handle obtained through
  803. <function>add</function> function, the second parameters is
  804. formatting string that determines the types of attributes
  805. to be added. There must be two parameters per each
  806. character in the formatting string, the first one is the
  807. name of the attribute, the second parameter is the value
  808. of the attribute. If a parameter with such a name already
  809. exist in the structure then it will be overwritten with the
  810. new value.
  811. <programlisting>
  812. <![CDATA[
  813. static void rpc_func(rpc_t* rpc, void *ctx)
  814. {
  815. void *handle;
  816. /* Create empty structure and obtain its handle */
  817. if (rpc->add(ctx, "{", &handle) < 0) return;
  818. /* Fill-in the structure */
  819. if (rpc->struct_add(handle, "sd", "attr1", str_val,
  820. "attr2", int_val ) < 0)
  821. return;
  822. }
  823. ]]>
  824. </programlisting>
  825. The function returns -1 on an error (and sets the status
  826. code and reason phrase of the reply accordingly) and 0 on success.
  827. </para>
  828. </section>
  829. </section>
  830. <section>
  831. <title>Real World Example</title>
  832. <para>
  833. The following example illustrates the use of most of the
  834. functions from the API together:
  835. </para>
  836. <example>
  837. <title>Real World Example RPC Function</title>
  838. <programlisting>
  839. <![CDATA[
  840. static void rpc_register(rpc_t* rpc, void *ctx)
  841. {
  842. char* domain;
  843. str aor;
  844. contact_t contact, new_contact;
  845. void *handle;
  846. /* Extract the domain, address of record from the request */
  847. if (rpc->scan(ctx, "sS{", &domain, &aor, &handle) < 0) return;
  848. /* Extract the structure describing the contact to be processed */
  849. if (rpc->struct_scan(handle, "Sdf", "Contact", &contact.c,
  850. "Expires", &contact.expires,
  851. "Q", &contact.q ) < 0)
  852. return;
  853. /* Process the contact, new_contact will contain updated value after processing */
  854. if (process_contact(domain, &aor, &new_contact, &contact) < 0) {
  855. /* Processing failed, indicate the failure to the caller */
  856. rpc->fault(ctx, 500, "Error While Processing Contact");
  857. return;
  858. }
  859. /* Return the domain and the address of record */
  860. rpc->add(ctx, "sS{", &domain, &aor, &handle) < 0) return;
  861. /* And also add the new values for contact, q, and expires parameters */
  862. rpc->struct_add(handle, "Sdf", "Contact", &new_contact.c,
  863. "Expires", &new_contact.expires,
  864. "Q", &new_contact.q );
  865. }
  866. ]]>
  867. </programlisting>
  868. </example>
  869. </section>
  870. </section>
  871. <section id="rpc.client_examples">
  872. <title>Client Examples</title>
  873. <para>
  874. <itemizedlist>
  875. <listitem><para>
  876. <emphasis>sercmd</emphasis> (C application that uses the
  877. <emphasis>binrpc</emphasis> interface implemented by the
  878. <emphasis>ctl</emphasis> module).
  879. </para></listitem>
  880. <listitem><para>
  881. <emphasis>ser_ctl</emphasis> (python application that uses the
  882. <emphasis>XML-RPC</emphasis> interface implemented by the
  883. <emphasis>xmlrpc</emphasis>
  884. module).
  885. </para></listitem>
  886. <listitem><para>
  887. <emphasis>siremis</emphasis> (php application that can use
  888. the <emphasis>XML-RPC</emphasis> interface to call &kamailioname;
  889. functions).
  890. </para></listitem>
  891. </itemizedlist>
  892. </para>
  893. </section>
  894. <section id="rpc.new_transport">
  895. <title>Implementing New Transports</title>
  896. <remark>
  897. To be done.
  898. </remark>
  899. <!-- TODO:
  900. - create a new module
  901. - take a look at sip_router/rpc.h
  902. - implement all functions in that header field
  903. - no garbage collection in rpc functions, the module needs to keep
  904. track of all allocated data
  905. - return value default to true
  906. - parameter type conflict is an error
  907. - missing struct attribute is an error
  908. - always send a reply, prepare replies so that you do not have to
  909. allocate memory at runtime
  910. - str strings do not have to be zero terminated
  911. - no structure/array nesting allowed
  912. - printf creates string attribute
  913. -->
  914. <para>
  915. Examples:
  916. <itemizedlist>
  917. <listitem><para>
  918. <emphasis>ctl</emphasis>
  919. </para></listitem>
  920. <listitem><para>
  921. <emphasis>xmlrpc</emphasis>
  922. </para></listitem>
  923. </itemizedlist>
  924. </para>
  925. </section>
  926. <section id="rpc.xmlrpc_examples">
  927. <title>Examples using xmlrpc</title>
  928. <para>See the <varname>xmlrpc</varname> module documentation:
  929. <ulink url='https://www.kamailio.org/docs/modules/devel/modules/xmlrpc.html'>modules/xmlrpc/README</ulink>.
  930. </para>
  931. </section>
  932. </section>