kamailio_rpc.xml 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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 writting a RPC
  36. transport module. The most used transport modules are
  37. <ulink url='http://www.kamailio.org/docs/modules/devel/modules/ctl/ctl.html'>
  38. <emphasis>ctl</emphasis>
  39. </ulink>,
  40. <ulink url='http://www.kamailio.org/docs/modules/devel/modules/xmlrpc/xmlrpc.html'>
  41. <emphasis>xmlrpc</emphasis>
  42. </ulink>
  43. and
  44. <ulink url='http://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. It is
  253. safer and recommended to use instead the
  254. <function>rpc_register_array()</function> function.
  255. </para></note>
  256. </para></listitem>
  257. </itemizedlist>
  258. </para>
  259. <para>
  260. By convention the name of every exported function consists of
  261. two parts delimited by a dot. The first part is the name of the
  262. module or &kamailio; subsystem this function belongs to. The second
  263. part is the name of the function.
  264. </para>
  265. </section>
  266. <section id="rpc.data_types">
  267. <title>Data Types</title>
  268. <para>
  269. The RPC API defines several basic and one compound data type
  270. that can be used in communication with the caller of RPC
  271. functions. The RPC API uses formating strings to describe data
  272. types. Each data type is described by exactly one character in
  273. the formating string. For example, if an RPC function calls
  274. function <function>add</function> of the RPC API and it passes
  275. two parameters to it, the first one of type string and the
  276. second one of type integer, the function parameters will look
  277. like:
  278. <programlisting>
  279. add("sd", string_param, int_param);
  280. </programlisting>
  281. Character "s" in the formating string tells to the function
  282. that the 2nd parameter should be interpreted as string,
  283. character "d" in the formating string tells to the function
  284. that the 3rd parameter should be interpreted as signed integer.
  285. </para>
  286. <formalpara>
  287. <title>Integer</title>
  288. <para>
  289. Integer type represents a signed 32-bit
  290. integer. Corresponding character in the formating string is
  291. "d". This parameter can be stored in C-style variable with
  292. type <varname>int</varname>.
  293. </para>
  294. </formalpara>
  295. <formalpara>
  296. <title>Float</title>
  297. <para>
  298. Float type represents a signed floating point
  299. number. Corresponding character in the formating string is
  300. "f". Data of this type can be stored in C-style variables
  301. of type <varname>double</varname>.
  302. </para>
  303. </formalpara>
  304. <formalpara>
  305. <title>String</title>
  306. <para>
  307. String type represents a string of characters. The string
  308. may contain zeroes. This data type is represented by two
  309. characters in the formatting string, either "s" or "S". "s"
  310. indicates to the conversion function that the result should
  311. be stored in a variable of type <varname>char*</varname>
  312. and it should be zero terminated. "S" indicates to the
  313. conversion function that the result will be stored in
  314. a variable of type <varname>str</varname> which contains
  315. both the pointer to the beginning of the string and its
  316. length.
  317. </para>
  318. </formalpara>
  319. <formalpara>
  320. <title>Structure</title>
  321. <para>
  322. Structure is the only compound data type currently defined
  323. in the API. A structure is a collection of attributes. Each
  324. attribute is identified using name (string) and each
  325. attribute can be one of the basic data types, that
  326. is integer, float, or string. Nesting of structures is not
  327. allowed (in other words, structure attributes cannot be of
  328. type struct again). Corresponding character in the
  329. formatting string is "{".
  330. </para>
  331. </formalpara>
  332. <formalpara>
  333. <title>Optional parameters</title>
  334. <para>
  335. Optional parameters can be used, but only in the
  336. <function>scan</function> function. For optional parameters the
  337. <function>scan</function> function will not automatically generate
  338. a rpc fault if the input ends. Note that in this case the
  339. <function>scan</function> will still return a negative value
  340. (minus the number of parameters successfully read).
  341. Optional parameters can be marked in the format string by
  342. preceding the first optional parameter type with a "*".
  343. All the parameters following a "*" are considered to be optional.
  344. For example for the format string "ds*dds", the last 3 parameters
  345. (2 ints and a string) are optional.
  346. </para>
  347. </formalpara>
  348. <table>
  349. <title>Data Type Overview</title>
  350. <tgroup cols="3">
  351. <tbody>
  352. <row rowsep="1">
  353. <entry>Name</entry>
  354. <entry>Formating String Char</entry>
  355. <entry>C-Style Variable</entry>
  356. </row>
  357. <row>
  358. <entry>Integer</entry>
  359. <entry>d</entry>
  360. <entry>int</entry>
  361. </row>
  362. <row>
  363. <entry>Float</entry>
  364. <entry>f</entry>
  365. <entry>double</entry>
  366. </row>
  367. <row>
  368. <entry>String</entry>
  369. <entry>s</entry>
  370. <entry>char*</entry>
  371. </row>
  372. <row>
  373. <entry>String</entry>
  374. <entry>S</entry>
  375. <entry>str*</entry>
  376. </row>
  377. <row>
  378. <entry>Optional modifier</entry>
  379. <entry>*</entry>
  380. <entry>marks all further parameters as optional</entry>
  381. </row>
  382. <row>
  383. <entry>Autoconvert modifier</entry>
  384. <entry>.</entry>
  385. <entry>requires auto-conversion for the next parameter</entry>
  386. </row>
  387. </tbody>
  388. </tgroup>
  389. </table>
  390. </section>
  391. <section id="rpc.getting_parameters">
  392. <title>Getting Parameters</title>
  393. <para>
  394. Each RPC function call can contain parameters. Parameters have
  395. no name, their meaning is determined by their position in the
  396. parameter set.
  397. <note>
  398. <para>
  399. You can pass all parameters to a function within a
  400. structure if you want to make them position
  401. independent. Then each parameter can be retrieved by
  402. its name regardless of its position.
  403. </para>
  404. </note>
  405. There are two functions in the RPC API that can be used to
  406. obtain function call parameters: <function>scan</function> and
  407. <function>struct_scan</function>.
  408. </para>
  409. <section id="rpc.scan">
  410. <title><function>scan</function></title>
  411. <para>
  412. Function <function>scan</function> can be used to retrieve
  413. parameters from the parameter set. The function accepts
  414. variable number of parameters. The first parameter is the
  415. formatting string that determines the type of the
  416. parameters to be retrieved. Each parameter is represented by
  417. exactly one parameter type character in the string.
  418. The variable part of parameters must contain as many pointers to C
  419. variables as there are formatting non-modifiers characters in the
  420. formatting string.
  421. <warning>
  422. <para>
  423. The function will crash if you fail to provide
  424. enough parameters.
  425. </para>
  426. </warning>
  427. </para>
  428. <para>
  429. Besides characters representing parameter types, the formatting
  430. string can contain two special modifiers: "*" and ".". The
  431. modifiers do not have a correspondent in the variable part of the
  432. parameters.
  433. </para>
  434. <para>
  435. The meaning of "*" modifier is that any further parameters
  436. (defined by other type characters in the formatting string) are
  437. optional (they can be missing in the input and no rpc fault will
  438. automatically be generated).
  439. </para>
  440. <para>
  441. The '.' modifiers turns on type autoconversion for the next
  442. parameter. This means that if the type of the next parameter
  443. differs from the type specified in the formatting string, the
  444. parameter will be automatically converted to the formatting string
  445. type (if possible) and if the automatic conversion succeeds, no
  446. fault will be generated.
  447. </para>
  448. <para>
  449. The function returns the number of parameters read on success
  450. (a number greater or equal 0) and - (minus) the number of
  451. parameters read on error (for example for an error after
  452. reading 2 parameters it will return -2).
  453. When a failure occurs (incorrect parameter type or no more
  454. parameters in the parameter set) the function will
  455. return a negative number (- number of parameters read so far)
  456. and it will also automatically change the reply that will be
  457. sent to the caller to indicate that a failure has occurred on
  458. the server (unless the "*" is used and the error is lack
  459. of more parameters).
  460. </para>
  461. <para>
  462. The prototype of the function is:
  463. <programlisting>
  464. int scan((void* ctx, char* fmt, ...)
  465. </programlisting>
  466. It is possible to either call the function once to scan all
  467. the parameters:
  468. <programlisting>
  469. rpc->scan(ctx, "sdf", &amp;string_val, &amp;int_val, &amp;double_val);
  470. </programlisting>
  471. Or you can call the same function several times and it will
  472. continue where it left off previously:
  473. <programlisting>
  474. rpc->scan(ctx, "s", &amp;string_val);
  475. rpc->scan(ctx, "d", &amp;int_val);
  476. rpc->scan(ctx, "f", &amp;double_val);
  477. </programlisting>
  478. </para>
  479. <para>
  480. </para>
  481. </section>
  482. <section>
  483. <title><function>struct_scan</function></title>
  484. <para>
  485. Function <function>struct_scan</function> can be used to
  486. retrieve named attributes from a parameter of type
  487. structure.
  488. <note><para>
  489. This function is obsolete and not implemented by all the
  490. rpc transports (e.g.: ctl / binrpc). Consider using the normal
  491. <function>scan</function> instead.
  492. </para></note>
  493. When retrieving a structure parameter from the
  494. parameter set:
  495. <programlisting>
  496. rpc->scan(ctx, "{", &amp;handle);
  497. </programlisting>
  498. The corresponding variable (named
  499. <varname>handle</varname> in the example above) will contain
  500. the index of the structure parameter within the parameter
  501. set, but the index cannot be used to retrieve the contents
  502. of the structure. To retrieve the contents of the structure
  503. you can use function <function>struct_scan</function>. The
  504. function gets the handle as the first parameter:
  505. <programlisting>
  506. rpc->struct_scan(handle, "sd", "str_attr", &amp;str_val, "int_attr", &amp;int_val);
  507. </programlisting>
  508. The second parameter is the formatting string followed by
  509. pairs of parameters. First parameter in each pair is the
  510. name of the attribute to retrieve (string) and the second
  511. parameter in each pair is the pointer to the variable to
  512. store the value of the parameter. The function returns the
  513. number of parameters (name value pairs) read on
  514. success and - number of parameters read so far on an error
  515. (just like the <function>scan</function> function). The function
  516. also indicates an error if a requested attribute is missing in
  517. the structure.
  518. </para>
  519. </section>
  520. <section><title>Retrieving Parameters Example</title>
  521. <example>
  522. <title>Retrieving Parameters</title>
  523. <programlisting>
  524. <![CDATA[
  525. static void rpc_delete_contact(rpc_t* rpc, void* ctx)
  526. {
  527. str aor, contact;
  528. char* table;
  529. void *handle;
  530. int expires;
  531. double q;
  532. if (rpc->scan(ctx, "sS{", &table, &aor, &handle) < 0) {
  533. /* Reply is set automatically by scan upon failure,
  534. * no need to do anything here
  535. */
  536. return;
  537. }
  538. if (rpc->struct_scan(handle, "Sdf", "Contact", &contact,
  539. "Expires", &expires,
  540. "Q", &q ) < 0) {
  541. /* Reply is set automatically by struct_scan upon failure,
  542. * no need to do anything here
  543. */
  544. return;
  545. }
  546. /* Process retrieved parameters here */
  547. }
  548. /* variable number of parameters:
  549. echo back all the parameters, string type required */
  550. static void core_prints(rpc_t* rpc, void* c)
  551. {
  552. char* string = 0;
  553. while((rpc->scan(c, "*s", &string)>0))
  554. rpc->add(c, "s", string);
  555. }
  556. /* variable number of parameters and auto conversion:
  557. echo back all the parameters, works with any type (everything is
  558. internally converted to string, notice the '.' modifier) */
  559. static void core_echo(rpc_t* rpc, void* c)
  560. {
  561. char* string = 0;
  562. while((rpc->scan(c, "*.s", &string)>0))
  563. rpc->add(c, "s", string);
  564. }
  565. ]]>
  566. </programlisting>
  567. </example>
  568. </section>
  569. </section>
  570. <section id="rpc.creating_result">
  571. <title>Building Reply</title>
  572. <para>
  573. The RPC API contains several functions that can be used to
  574. modify and/or send a reply. The functions use formatting
  575. strings and parameter lists just like functions described in
  576. <xref linkend="rpc.getting_parameters"/>.
  577. </para>
  578. <para>
  579. Each RPC function call must return a reply. The reply can be
  580. either a failure reply or success reply. Failure replies
  581. contain only the status code and reason phrase. Success
  582. replies can have arbitrary amount of data attached to
  583. them. Status codes 3xx, 4xx, 5xx, and 6xx indicate
  584. failures. Status code 2xx indicates success.
  585. </para>
  586. <para>
  587. The default reply is 200 OK with no data attached to it. This
  588. is what will be returned by the RPC transport module if you do
  589. not call any of the reply-related functions described in this
  590. section.
  591. <example>
  592. <title>Sending default reply</title>
  593. <programlisting>
  594. <![CDATA[
  595. static void rpc_dummy(rpc_t* rpc, void *ctx)
  596. {
  597. /* 200 OK with no data will be returned */
  598. }
  599. ]]>
  600. </programlisting>
  601. </example>
  602. </para>
  603. <section>
  604. <title>fault</title>
  605. <para>
  606. You can use <function>fault</function> function to indicate
  607. that an error has occurred on the server to the caller. The
  608. function accepts two parameters. The first parameter is the
  609. status code and the second parameter is the reason phrase.
  610. <programlisting>
  611. <![CDATA[
  612. static void rpc_my_function(rpc_t* rpc, void *ctx)
  613. {
  614. rpc->fault(ctx, 600, "Not Yet Implemented");
  615. }
  616. ]]>
  617. </programlisting>
  618. If your function first creates some result using
  619. <function>add</function>, or <function>printf</function>
  620. functions then all the data will be lost once you call
  621. <function>fault</function> function. Failure replies must
  622. not contain any data:
  623. <programlisting>
  624. <![CDATA[
  625. static void rpc_my_function(rpc_t* rpc, void *ctx)
  626. {
  627. rpc->add(ctx, "s", "result1");
  628. rpc->add(ctx, "d", variable);
  629. /* Reply created by previous functions will be
  630. * deleted and a failure reply 600 Not Yet Implemented
  631. * will be created instead
  632. */
  633. rpc->fault(ctx, 600, "Not Yet Implemented");
  634. /* You can also add data here, but that will have no
  635. * effect
  636. */
  637. rpc->add(ctx, "s", "result2");
  638. }
  639. ]]>
  640. </programlisting>
  641. Similarly you can also call <function>add</function> or
  642. <function>printf</function> functions after calling
  643. <function>fault</function>, in this case they will have no
  644. effect:
  645. <programlisting>
  646. <![CDATA[
  647. static void rpc_my_function(rpc_t* rpc, void *ctx)
  648. {
  649. rpc->fault(ctx, 600, "Not Yet Implemented");
  650. /* You can also add data here, but that will have no
  651. * effect and only 600 Not Yet Implemented will be returned
  652. */
  653. rpc->add(ctx, "s", "result2");
  654. }
  655. ]]>
  656. </programlisting>
  657. </para>
  658. </section>
  659. <section>
  660. <title>send</title>
  661. <para>
  662. RPC functions can use function <function>send</function> to
  663. explicitly send the reply. Each RPC function call generates
  664. exactly one reply. No reply will be sent after the function
  665. finishes if it already sent the reply using
  666. <function>send</function> function explicitly. This
  667. function is especially useful if the RPC function needs to
  668. perform some (potentially destructive) actions after the
  669. reply has been sent.
  670. </para>
  671. <example>
  672. <title>Kill the server</title>
  673. <programlisting>
  674. <![CDATA[
  675. static void core_kill(rpc_t* rpc, void *ctx)
  676. {
  677. int sig_no;
  678. if (rpc->scan(ctx, "d", &sig_no) < 0) return;
  679. rpc->send(ctx, ); /* First send a reply */
  680. kill(0, sig_no); /* Then kill the server */
  681. }
  682. ]]>
  683. </programlisting>
  684. </example>
  685. </section>
  686. <section>
  687. <title>add</title>
  688. <para>
  689. Function <function>add</function> can be used to add
  690. arbitrary data to the result set. Its parameters and use
  691. are analogical to <function>scan</function> function
  692. described in <xref linkend="rpc.scan"/>. The first
  693. parameter of the function is the formatting string that
  694. determines the types of additional parameters:
  695. <programlisting>
  696. <![CDATA[
  697. static void rpc_func(rpc_t* rpc, void *ctx)
  698. {
  699. str str_result;
  700. int int_result;
  701. void *handle;
  702. double float_result;
  703. if (rpc->add(ctx, "Sdf{", &str_result, int_result, float_result, &handle) < 0) return;
  704. }
  705. ]]>
  706. </programlisting>
  707. Naturally you can call this function several times, adding
  708. only one piece of data at a time. The function returns 0 on
  709. success and -1 on an error. In case of an error the reply
  710. is set automatically with corresponding error code and
  711. reason phrase.
  712. </para>
  713. <para>
  714. The last character in the formatting string of the function
  715. above indicates that the last data to be added will be a
  716. structure. This deserves some clarification. In this case,
  717. the function will create an empty structure and the handle
  718. to the newly created structure will be stored in
  719. <varname>handle</varname> variable (hence the last
  720. parameter is pointer to an integer). In this particular
  721. example parameters <varname>str_result</varname>,
  722. <varname>int_result</varname>, and
  723. <varname>float_result</varname> will be used for reading
  724. while parameter <varname>handle</varname> will be used for
  725. writing by the function.
  726. </para>
  727. <para>
  728. You can set the attributes of the newly created structure
  729. using <function>struct_add</function> function described in
  730. <xref linkend="rpc.struct_add"/>.
  731. </para>
  732. </section>
  733. <section>
  734. <title>rpl_printf</title>
  735. <para>
  736. <varname>rpl_printf</varname> is a convenience function. The
  737. function adds data of type string to the result set. The
  738. first parameter of the function is again a formatting
  739. string, but this time it is standard
  740. <function>printf</function>-like formatting string:
  741. <programlisting>
  742. <![CDATA[
  743. if (rpc->rpl_printf(ctx, "Unable to delete %d entries from table %s", num_entries, table_name) < 0) return;
  744. ]]>
  745. </programlisting>
  746. The return value of the function is the same as of
  747. <function>add</function> function.
  748. </para>
  749. </section>
  750. <section id="rpc.struct_add">
  751. <title>struct_add</title>
  752. <para>
  753. Function <function>struct_add</function> can be used to add
  754. attributes to a structure (created previously by
  755. <function>add</function> function). The first parameter of
  756. the function is handle obtained through
  757. <function>add</function> function, the second parameters is
  758. formatting string that determines the types of attributes
  759. to be added. There must be two parameters per each
  760. character in the formatting string, the first one is the
  761. name of the attribute, the second parameter is the value
  762. of the attribute. If a parameter with such a name already
  763. exist in the structure then it will be overwritten with the
  764. new value.
  765. <programlisting>
  766. <![CDATA[
  767. static void rpc_func(rpc_t* rpc, void *ctx)
  768. {
  769. void *handle;
  770. /* Create empty structure and obtain its handle */
  771. if (rpc->add(ctx, "{", &handle) < 0) return;
  772. /* Fill-in the structure */
  773. if (rpc->struct_add(handle, "sd", "attr1", str_val,
  774. "attr2", int_val ) < 0)
  775. return;
  776. }
  777. ]]>
  778. </programlisting>
  779. The function returns -1 on an error (and sets the status
  780. code and reason phrase of the reply accordingly) and 0 on success.
  781. </para>
  782. </section>
  783. </section>
  784. <section>
  785. <title>Real World Example</title>
  786. <para>
  787. The following example illustrates the use of most of the
  788. functions from the API together:
  789. </para>
  790. <example>
  791. <title>Real World Example RPC Function</title>
  792. <programlisting>
  793. <![CDATA[
  794. static void rpc_register(rpc_t* rpc, void *ctx)
  795. {
  796. char* domain;
  797. str aor;
  798. contact_t contact, new_contact;
  799. void *handle;
  800. /* Extract the domain, address of record from the request */
  801. if (rpc->scan(ctx, "sS{", &domain, &aor, &handle) < 0) return;
  802. /* Extract the structure describing the contact to be processed */
  803. if (rpc->struct_scan(handle, "Sdf", "Contact", &contact.c,
  804. "Expires", &contact.expires,
  805. "Q", &contact.q ) < 0)
  806. return;
  807. /* Process the contact, new_contact will contain updated value after processing */
  808. if (process_contact(domain, &aor, &new_contact, &contact) < 0) {
  809. /* Processing failed, indicate the failure to the caller */
  810. rpc->fault(ctx, 500, "Error While Processing Contact");
  811. return;
  812. }
  813. /* Return the domain and the address of record */
  814. rpc->add(ctx, "sS{", &domain, &aor, &handle) < 0) return;
  815. /* And also add the new values for contact, q, and expires parameters */
  816. rpc->struct_add(handle, "Sdf", "Contact", &new_contact.c,
  817. "Expires", &new_contact.expires,
  818. "Q", &new_contact.q );
  819. }
  820. ]]>
  821. </programlisting>
  822. </example>
  823. </section>
  824. </section>
  825. <section id="rpc.client_examples">
  826. <title>Client Examples</title>
  827. <para>
  828. <itemizedlist>
  829. <listitem><para>
  830. <emphasis>sercmd</emphasis> (C application that uses the
  831. <emphasis>binrpc</emphasis> interface implemented by the
  832. <emphasis>ctl</emphasis> module).
  833. </para></listitem>
  834. <listitem><para>
  835. <emphasis>ser_ctl</emphasis> (python application that uses the
  836. <emphasis>XML-RPC</emphasis> interface implemented by the
  837. <emphasis>xmlrpc</emphasis>
  838. module).
  839. </para></listitem>
  840. <listitem><para>
  841. <emphasis>serweb</emphasis> (php application that can use
  842. the <emphasis>XML-RPC</emphasis> interface to call ser
  843. functions).
  844. </para></listitem>
  845. </itemizedlist>
  846. </para>
  847. </section>
  848. <section id="rpc.new_transport">
  849. <title>Implementing New Transports</title>
  850. <remark>
  851. To be done.
  852. </remark>
  853. <!-- TODO:
  854. - create a new module
  855. - take a look at sip_router/rpc.h
  856. - implement all functions in that header field
  857. - no garbage collection in rpc functions, the module needs to keep
  858. track of all allocated data
  859. - return value default to true
  860. - parameter type conflict is an error
  861. - missing struct attribute is an error
  862. - always send a reply, prepare replies so that you do not have to
  863. allocate memory at runtime
  864. - str strings do not have to be zero terminated
  865. - no structure/array nesting allowed
  866. - printf creates string attribute
  867. -->
  868. <para>
  869. Examples:
  870. <itemizedlist>
  871. <listitem><para>
  872. <emphasis>ctl</emphasis>
  873. </para></listitem>
  874. <listitem><para>
  875. <emphasis>xmlrpc</emphasis>
  876. </para></listitem>
  877. </itemizedlist>
  878. </para>
  879. </section>
  880. <section id="rpc.xmlrpc_examples">
  881. <title>Examples using xmlrpc</title>
  882. <para>See the <varname>xmlrpc</varname> module documentation:
  883. <ulink url='http://www.kamailio.org/docs/modules/devel/modules/xmlrpc.html'>modules/xmlrpc/README</ulink>.
  884. </para>
  885. </section>
  886. </section>