intro.xml 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  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. <section id="ser_intro" xmlns:xi="http://www.w3.org/2001/XInclude">
  5. <sectioninfo>
  6. <revhistory>
  7. <revision>
  8. <revnumber>$Revision$</revnumber>
  9. <date>$Date$</date>
  10. </revision>
  11. </revhistory>
  12. </sectioninfo>
  13. <title>Introduction to SER</title>
  14. <section id="requestrouting">
  15. <title>Request Routing and SER Scripts</title>
  16. <para>
  17. The most important concept of every SIP server is that of
  18. request routing. The request routing logic determines the next
  19. hop of a request. It can be for example used to implement user
  20. location service or enforce static routing to a gateway. Real-world
  21. deployments actually ask for quite complex routing logic, which
  22. needs to reflect static routes to PSTN gateways, dynamic routes
  23. to registered users, authentication policy, capabilities of
  24. SIP devices, etc.
  25. </para>
  26. <para>
  27. SER's answer to this need for routing flexibility is a routing
  28. language, which allows administrators to define the SIP request
  29. processing logic in a detailed manner. They can for example easily
  30. split SIP traffic by method or destination, perform user location,
  31. trigger authentication, verify access permissions, and so on.
  32. </para>
  33. <para>
  34. The primary building block of the routing language are <emphasis>actions</emphasis>.
  35. There are built-in actions (like <command>forward</command> for stateless forwarding
  36. or <command>strip</command> for stripping URIs) as
  37. well as external actions imported from shared library modules. All actions can
  38. be combined in compound actions by enclosing them in braces,
  39. e.g. <command>{a1(); a2();}</command>.
  40. Actions are aggregated in one or more <emphasis>route blocks</emphasis>.
  41. Initially, only the default routing block denoted by <command>route[0]</command>
  42. is called. Other routing blocks can be called by the action
  43. <command>route(blocknumber)</command>, recursion is permitted.
  44. The language includes <emphasis>conditional statements</emphasis>.
  45. </para>
  46. <para>
  47. The routing script is executed for every received request in sequential order.
  48. Actions may return positive/negative/zero value.
  49. Positive values are considered success and evaluated as
  50. TRUE in conditional expressions. Negative values are considered FALSE.
  51. Zero value means error and leaves execution of currently processed
  52. route block. The route block is left too, if <command>break</command> is explicitly
  53. called from it.
  54. </para>
  55. <para>
  56. The easiest and still very useful way for <application>ser</application>
  57. users to affect request routing logic is
  58. to determine next hop statically. An example is
  59. routing to a PSTN gateway whose static IP address is well known.
  60. To configure static routing, simply use the action
  61. <command>forward( IP_address, port_number)</command>.
  62. This action forwards an incoming request "as is" to the
  63. destination described in action's parameters.
  64. </para>
  65. <example>
  66. <title>Static Forwarding</title>
  67. <programlisting>
  68. # if requests URI is numerical and starts with
  69. # zero, forward statelessly to a static destination
  70. if (uri=~"^sip:0[0-9]*@iptel.org") {
  71. forward( 192.168.99.3, 5080 );
  72. }
  73. </programlisting>
  74. </example>
  75. <para>
  76. However, static forwarding is not sufficient in many cases.
  77. Users desire mobility and change their location frequently.
  78. Lowering costs for termination of calls in PSTN requires
  79. locating a least-cost gateway. Which next-hop is taken may
  80. depend on user's preferences. These and many other scenarios
  81. need the routing logic to be more dynamic. We describe in
  82. <xref linkend="conditions"/> how to make request processing
  83. subject to various conditions and in
  84. <xref linkend="urirewriting"/> how to determine next SIP hop.
  85. </para>
  86. </section>
  87. <section id="conditions">
  88. <title>Conditional Statements</title>
  89. <para>
  90. A very useful feature is the ability to make routing
  91. logic depend on a condition. A script condition may for
  92. example distinguish between request processing for
  93. served and foreign domains, IP and PSTN routes,
  94. it may split traffic by method or username, it
  95. may determine whether a request should be authenticated
  96. or not, etc. <application>ser</application>
  97. allows administrators to form conditions based on
  98. properties of processed request, such as method or uri,
  99. as well as on virtually any piece of data on the
  100. Internet.
  101. </para>
  102. <example>
  103. <title>Conditional Statement</title>
  104. <para>
  105. This example shows how a conditional statement is
  106. used to split incoming requests between a PSTN
  107. gateway and a user location server based on
  108. request URI.
  109. </para>
  110. <programlisting>
  111. # if request URI is numerical, forward the request to PSTN gateway...
  112. if (uri=~"^sip:[0-9][email protected]") { # match using a regular expression
  113. forward( gateway.foo.bar, 5060 );
  114. } else { # ... forward the request to user location server otherwise
  115. forward( userloc.foo.bar, 5060 );
  116. };
  117. </programlisting>
  118. </example>
  119. <para>
  120. Conditional statements in <application>ser</application> scripts may depend
  121. on a variety of expressions. The simplest expressions are
  122. action calls. They return true if they completed successfully or false otherwise.
  123. An example of an action frequently used in conditional statements is
  124. <command moreinfo="none">search</command> imported from textops module.
  125. <command moreinfo="none">search</command> action leverages textual
  126. nature of SIP and compares SIP requests against a regular expression.
  127. The action returns true if the expression matched, false otherwise.
  128. <example>
  129. <title>Use of <command>search</command> Action in Conditional Expression</title>
  130. <programlisting>
  131. # prevent strangers from claiming to belong to our domain;
  132. # if sender claims to be in our domain in From header field,
  133. # better authenticate him
  134. if (search("(f|From): .*@mydomain.com)) {
  135. if (!(proxy_authorize("mydomain.com" /* realm */,"subscriber" /* table name */ ))) {
  136. proxy_challenge("mydomain.com /* ream */, "1" /* use qop */ );
  137. break;
  138. }
  139. }
  140. </programlisting>
  141. </example>
  142. </para>
  143. <para>
  144. As modules may be created, which export new functions, there is virtually
  145. no limitation on what functionality <application>ser</application>
  146. conditions are based on. Implementers may introduce new actions whose
  147. return status depends on request content or any external data as well. Such actions
  148. can query SQL, web, local file systems or any other place which can provide
  149. information wanted for request processing.
  150. </para>
  151. <para>
  152. Furthermore, many request properties may be examined using existing built-in operands
  153. and operators. Available left-hand-side operands and legal combination with
  154. operators and right-hand-side operands are described in <xref linkend="logicalexpr"/>.
  155. Expressions may be grouped together using logical operators:
  156. negation (<command>!</command>), AND (<command>&amp;&amp;</command>), OR (<command>
  157. ||</command> and precedence parentheses (<command>()</command>).
  158. </para>
  159. <section id="operators">
  160. <title>Operators and Operands</title>
  161. <para>
  162. There is a set of predefined operators and operands
  163. in ser, which in addition to actions may be evaluated
  164. in conditional expressions.
  165. </para>
  166. <para>
  167. Left hand-side operands, which <application>ser</application>
  168. understands are the following:
  169. <itemizedlist>
  170. <listitem>
  171. <para>
  172. <emphasis>method</emphasis>, which refers to
  173. request method
  174. such as REGISTER or INVITE
  175. </para>
  176. </listitem>
  177. <listitem>
  178. <para>
  179. <emphasis>uri</emphasis>, which refers to current request URI,
  180. such as
  181. "sip:[email protected]"
  182. <note>
  183. <para>
  184. Note that "uri" always refers to current
  185. value of URI, which is subject to change
  186. be uri-rewriting actions.
  187. </para>
  188. </note>
  189. </para>
  190. </listitem>
  191. <listitem>
  192. <para>
  193. <emphasis>src_ip</emphasis>, which refers to IP address from
  194. which a request came.
  195. </para>
  196. </listitem>
  197. <listitem>
  198. <para>
  199. <emphasis>dst_ip</emphasis> refers to server's IP address
  200. at which a request was received
  201. </para>
  202. </listitem>
  203. <listitem>
  204. <para>
  205. <emphasis>src_port</emphasis> port number from which a SIP
  206. request came
  207. </para>
  208. </listitem>
  209. </itemizedlist>
  210. </para>
  211. <para>
  212. ser understands the following operators:
  213. <itemizedlist>
  214. <listitem>
  215. <para>
  216. == stands for equity
  217. </para>
  218. </listitem>
  219. <listitem>
  220. <para>
  221. =~ stands for regular expression matching
  222. </para>
  223. </listitem>
  224. <listitem>
  225. <para>
  226. logical operators: and, or, negation, parentheses
  227. (C-notation for the operators may be used too)
  228. </para>
  229. </listitem>
  230. </itemizedlist>
  231. </para>
  232. <table id="logicalexpr">
  233. <title>Valid Combinations of Operands and Operators in Expressions</title>
  234. <tgroup cols="4">
  235. <thead>
  236. <row>
  237. <entry>
  238. left-hand-side operand
  239. </entry>
  240. <entry>
  241. valid operators
  242. </entry>
  243. <entry>
  244. valid right-hand side operators
  245. </entry>
  246. <entry>
  247. examples/comments
  248. </entry>
  249. </row>
  250. </thead>
  251. <tbody>
  252. <row>
  253. <entry>
  254. method
  255. </entry>
  256. <entry>
  257. == (exact match), =~ (regular expression matching)
  258. </entry>
  259. <entry>
  260. string
  261. </entry>
  262. <entry>
  263. method=="INVITE" || method=="ACK" || method=="CANCEL"
  264. </entry>
  265. </row>
  266. <row>
  267. <entry>
  268. uri
  269. </entry>
  270. <entry>
  271. == (exact match), =~ (regular expression matching)
  272. </entry>
  273. <entry>
  274. string
  275. </entry>
  276. <entry>
  277. uri=="sip:[email protected]" matches only if exactly this uri
  278. is in request URI
  279. </entry>
  280. </row>
  281. <row>
  282. <entry>
  283. </entry>
  284. <entry>
  285. == (exact match)
  286. </entry>
  287. <entry>
  288. myself
  289. </entry>
  290. <entry>
  291. the expression uri==myself is true if the host part in
  292. request URI equals a server name or a server alias (set using
  293. the alias option in configuration file)
  294. </entry>
  295. </row>
  296. <row>
  297. <entry>
  298. src_ip
  299. </entry>
  300. <entry>
  301. == (match)
  302. </entry>
  303. <entry>
  304. IP, IP/mask_length, IP/mask, hostname, myself
  305. </entry>
  306. <entry>
  307. src_ip==192.168.0.0/16 matches requests coming from
  308. a private network
  309. </entry>
  310. </row>
  311. <row>
  312. <entry>
  313. dst_ip
  314. </entry>
  315. <entry>
  316. == (match)
  317. </entry>
  318. <entry>
  319. IP, IP/mask_length, IP/mask, hostname, myself
  320. </entry>
  321. <entry>
  322. dst_ip==127.0.0.1 matches if a request was received
  323. via loopback interface
  324. </entry>
  325. </row>
  326. <row>
  327. <entry>
  328. src_port
  329. </entry>
  330. <entry>
  331. == (match)
  332. </entry>
  333. <entry>
  334. port number
  335. </entry>
  336. <entry>
  337. port number from which a request was sent, e.g. src_port==5060
  338. </entry>
  339. </row>
  340. </tbody>
  341. </tgroup>
  342. </table>
  343. <example>
  344. <title>
  345. More examples of use of <application>ser</application> operators and operands in conditional
  346. statements
  347. </title>
  348. <programlisting>
  349. # using an action as condition input; in this
  350. # case, an actions 'search' looks for Contacts
  351. # with private IP address in requests; the condition
  352. # is processed if such a contact header field is
  353. # found
  354. if (search("^(Contact|m): .*@(192\.168\.|10\.|172\.16)")) {
  355. # ....
  356. # this condition is true if request URI matches
  357. # the regular expression "@bat\.iptel\.org"
  358. if (uri=~"@bat\.iptel\.org") {
  359. # ...
  360. # and this condition is true if a request came
  361. # from an IP address (useful for example for
  362. # authentication by IP address if digest is not
  363. # supported) AND the request method is INVITE
  364. # if ( (src_ip==192.68.77.110 and method=="INVITE")
  365. # ...
  366. </programlisting>
  367. </example>
  368. </section> <!-- operators and operands -->
  369. <section>
  370. <title>URI Matching</title>
  371. <para>URI matching expressions have a broad use in a SIP server
  372. and deserve more explanation. Typical uses of
  373. URI matching include implementation of numbering plans,
  374. domain matching,
  375. binding external applications to specific URIs,
  376. etc. This section shows examples of typical applications
  377. of URI-matching.
  378. </para>
  379. <section id="domainmatching">
  380. <title>Domain Matching</title>
  381. <para>
  382. One of most important uses of URI matching is deciding
  383. whether a request is targeted to a served or outside domain.
  384. Typically, different request
  385. processing applies. Requests for outside domains
  386. are simply forwarded to them, whereas
  387. more complex logic applies to requests for a served domain.
  388. The logic may include saving user's contacts
  389. when REGISTER requests are received, forwarding requests
  390. to current user's location or a PSTN gateways,
  391. interaction with external applications, etc.
  392. </para>
  393. <para>
  394. The easiest way to decide whether a request belongs
  395. a served domain is using the <command>myself</command>
  396. operand.
  397. The expression "uri==myself" returns true if domain name
  398. in request URI matches name of the host at which
  399. <application>ser</application> is
  400. running. This may be insufficient in cases when
  401. server name is not equal to domain name for which the server
  402. is responsible. For example, the "uri==myself" condition
  403. does not match if a server "sipserver.foo.bar"
  404. receives a request for "sip:[email protected]". To
  405. match other names in URI than server's own,
  406. set up the <varname>alias</varname> configuration
  407. option. The option may be used multiple times,
  408. each its use adds a new item to a list of aliases.
  409. The myself condition returns then true
  410. also for any hostname on the list of aliases.
  411. <example>
  412. <title>Use of uri==myself Expression</title>
  413. <programlisting>
  414. # ser powers a domain "foo.bar" and runs at host sipserver.foo.bar;
  415. # Names of served domains need to be stated in the aliases
  416. # option; myself would not match them otherwise and would only
  417. # match requests with "sipserver.foo.bar" in request-URI
  418. alias="foo.bar"
  419. alias="sales.foo.bar"
  420. route[0] {
  421. if (uri==myself) {
  422. # the request either has server name or some of the
  423. # aliases in its URI
  424. log(1,"request for served domain")
  425. # some domain-specific logic follows here ....
  426. } else {
  427. # aha -- the server is not responsible for this
  428. # requests; that happens for example with the following URIs
  429. # - sip:[email protected]
  430. # - sip:[email protected]
  431. log(1,"request for outbound domain");
  432. # outbound forwarding
  433. t_relay();
  434. };
  435. }
  436. </programlisting>
  437. </example>
  438. </para>
  439. <para>
  440. It is possible to recognize whether a request belongs to
  441. a domain using regular expressions too. Care needs to
  442. be paid to construction of regular expressions. URI
  443. syntax is rich and an incorrect expression would result
  444. in incorrect call processing. The following example shows
  445. how an expression for domain matching can be formed.
  446. <example id="redomainmatching">
  447. <title>Domain Matching Using Regular Expressions</title>
  448. <para>
  449. In this example, server named "sip.foo.bar" with
  450. IP address 192.168.0.10 is responsible for the
  451. "foo.bar" domain. That means, requests with the
  452. following hostnames in URI should be matched:
  453. <itemizedlist>
  454. <listitem>
  455. <para>
  456. foo.bar, which is the name of server domain
  457. </para>
  458. </listitem>
  459. <listitem>
  460. <para>
  461. sip.foo.bar, since it is server's name and some
  462. devices put server's name in request URI
  463. </para>
  464. </listitem>
  465. <listitem>
  466. <para>
  467. 192.168.0.10, since it is server's IP address and
  468. some devices put server's IP address in request URI
  469. </para>
  470. </listitem>
  471. </itemizedlist>
  472. Note how this regular expression is constructed. In particular:
  473. <itemizedlist>
  474. <listitem>
  475. <para>
  476. User name is optional (it is for example never included
  477. in REGISTER requests) and there are no restrictions on
  478. what characters it contains. That is what
  479. <emphasis>(.+@)?</emphasis> mandates.
  480. </para>
  481. </listitem>
  482. <listitem>
  483. <para>
  484. Hostname must be followed by port number, parameters
  485. or headers -- that is what the delimiters
  486. <emphasis>[:;\?]</emphasis> are good for. If none
  487. it these follows, the URI must be ended
  488. (<emphasis>$</emphasis>). Otherwise, longer hostnames
  489. such as 192.168.0.101 or foo.bar.otherdomain.com would
  490. mistakenly match.
  491. </para>
  492. </listitem>
  493. <listitem>
  494. <para>
  495. Matches are case-insensitive. All hostnames "foo.bar", "FOO.BAR"
  496. and "FoO.bAr" match.
  497. </para>
  498. </listitem>
  499. </itemizedlist>
  500. </para>
  501. <programlisting>
  502. if (uri=~"^sip:(.+@)?(192\.168\.0\.10|(sip\.)?foo\.bar)([:;\?].*)?$")
  503. log(1, "yes, it is a request for our domain");
  504. break;
  505. };
  506. </programlisting>
  507. </example>
  508. </para>
  509. </section> <!-- domain matching -->
  510. <section id="numberingplans">
  511. <title>Numbering Plans</title>
  512. <para>
  513. Other use of URI matching is implementation of dialing
  514. plans. A typical task when designing a dialing plan for SIP networks
  515. is to distinguish between "pure-IP" and PSTN destinations.
  516. IP users typically have either alphanumerical or numerical
  517. usernames. The numerical usernames are convenient for PSTN
  518. callers who can only
  519. use numeric keypads. Next-hop destination of IP users is looked up dynamically
  520. using user location database. On the other hand, PSTN destinations are
  521. always indicated by numerical usernames. Requests to PSTN are statically
  522. forwarded to well-known PSTN gateways.
  523. </para>
  524. <example>
  525. <title>A simple Numbering Plan</title>
  526. <para>
  527. This example shows a simple dialing plan which reserves
  528. dialing prefix "8" for IP users, other numbers
  529. are used for PSTN destinations and all other non-numerical
  530. usernames are used for IP users.
  531. </para>
  532. <programlisting>
  533. # is it a PSTN destination? (is username numerical and does not begin with 8?)
  534. if (uri=~"^sip:[0-79][0-9]*@") { # ... forward to gateways then;
  535. # check first to which PSTN destination the requests goes;
  536. # if it is US (prefix "1"), use the gateway 192.168.0.1...
  537. if (uri=~"^sip:1") {
  538. # strip the leading "1"
  539. strip(1);
  540. forward(192.168.0.1, 5060);
  541. } else {
  542. # ... use the gateway 10.0.0.1 for all other destinations
  543. forward(10.0.0.1, 5060);
  544. }
  545. break;
  546. } else {
  547. # it is an IP destination -- try to lookup it up in user location DB
  548. if (!lookup("location")) {
  549. # bad luck ... user off-line
  550. sl_send_reply("404", "Not Found");
  551. break;
  552. }
  553. # user on-line...forward to his current destination
  554. forward(uri:host,uri:port);
  555. }
  556. </programlisting>
  557. </example>
  558. </section> <!-- numbering plans -->
  559. </section>
  560. </section> <!-- conditional statements -->
  561. <section id="urirewriting">
  562. <title>Request URI Rewriting</title>
  563. <para>
  564. The ability to give users and services a unique name using URI
  565. is a powerful tool. It allows users to advertise how to reach
  566. them, to state to whom they wish to communicate and what services
  567. they wish to use.
  568. Thus, the ability to change URIs is very important and is
  569. used for implementation of many services.
  570. "Unconditional forwarding" from user "boss" to user
  571. "secretary" is a typical example of application relying
  572. on change of URI address.
  573. </para>
  574. <para>
  575. <application>ser</application> has the ability
  576. to change request URI in many ways.
  577. A script can use any of the following
  578. built-in actions to change request URI or a part of it:
  579. <command>rewriteuri</command>,
  580. <command>rewritehost</command>,
  581. <command>rewritehostport</command>,
  582. <command>rewriteuser</command>,
  583. <command>rewriteuserpass</command> and
  584. <command>rewriteport</command>.
  585. When later in the script
  586. a forwarding action is encountered, the action forwards
  587. the request to address in the rewritten URI.
  588. <example>
  589. <title>Rewriting URIs</title>
  590. <programlisting>
  591. if (uri=~"[email protected]") {
  592. rewriteuri("sip:[email protected]")
  593. # forward statelessly to the destination in current URI, i.e.,
  594. # to sip:[email protected]:5060
  595. forward( uri:host, uri:port);
  596. }
  597. </programlisting>
  598. </example>
  599. </para>
  600. <para>Two more built-in URI-rewriting commands are of special importance
  601. for implementation of dialing plans and manipulation of dialing
  602. prefixes. <command>prefix(s)
  603. </command>, inserts
  604. a string "s" in front of SIP address and
  605. <command>strip(n)</command> takes
  606. away the first "n" characters of a SIP address.
  607. See <xref linkend="urirewritingexamples"/> for examples of use of
  608. built-in URI-rewriting actions.
  609. </para>
  610. <para>
  611. Commands exported by external modules can change URI too
  612. and many do so.
  613. The most important application is changing URI using the
  614. user location database. The command
  615. <command>lookup(table)</command> looks up current
  616. user's location and rewrites user's address with it.
  617. If there is no registered contact, the command returns a negative value.
  618. <example id="rewriteuri">
  619. <title>Rewriting URIs Using User Location Database</title>
  620. <programlisting>
  621. # store user location if a REGISTER appears
  622. if (method=="REGISTER") {
  623. save("mydomain1");
  624. } else {
  625. # try to use the previously registered contacts to
  626. # determine next hop
  627. if(lookup("mydomain1")) {
  628. # if found, forward there...
  629. t_relay();
  630. } else {
  631. # ... if no contact on-line, tell it upstream
  632. sl_send_reply("404", "Not Found" );
  633. };
  634. };
  635. </programlisting>
  636. </example>
  637. </para>
  638. <para>
  639. External applications can be used to rewrite URI too.
  640. The "exec" module provides script actions, which start external programs
  641. and read new URI value from their output. <command>exec_dset</command>
  642. both calls an external program, passes SIP request elements to it, waits until it completes,
  643. and eventually rewrites current destination set with its output.
  644. </para>
  645. <para>
  646. It is important to realize that <application>ser</application>
  647. operates over <emphasis>current URI</emphasis> all the time. If an
  648. original URI is rewritten by a new one, the original will will be
  649. forgotten and the new one will be used in any further
  650. processing. In particular, the uri matching operand and the user
  651. location action <command>lookup</command> always take current URI
  652. as input, regardless what the original URI was.
  653. </para>
  654. <para>
  655. <xref linkend="urirewritingexamples"/> shows how URI-rewriting actions affect
  656. an example URI, sip:[email protected]:6060.
  657. <table id="urirewritingexamples">
  658. <title>URI-rewriting Using Built-In Actions</title>
  659. <tgroup cols="2">
  660. <thead>
  661. <row>
  662. <entry>
  663. Example Action
  664. </entry>
  665. <entry>
  666. Resulting URI
  667. </entry>
  668. </row>
  669. </thead>
  670. <tbody>
  671. <row>
  672. <entry>
  673. <command>rewritehost("192.168.0.10")</command> rewrites
  674. the hostname in URI, other parts (including port number) remain unaffected.
  675. </entry>
  676. <entry>
  677. sip:[email protected]:6060
  678. </entry>
  679. </row>
  680. <row>
  681. <entry>
  682. <command>rewriteuri("sip:[email protected]");</command> rewrites
  683. the whole URI completely.
  684. </entry>
  685. <entry>
  686. sip:[email protected]
  687. </entry>
  688. </row>
  689. <row>
  690. <entry>
  691. <command>rewritehostport("192.168.0.10:3040")</command>rewrites
  692. both hostname and port number in URI.
  693. </entry>
  694. <entry>
  695. sip:[email protected]:3040
  696. </entry>
  697. </row>
  698. <row>
  699. <entry>
  700. <command>rewriteuser("alice")</command> rewrites user part of URI.
  701. </entry>
  702. <entry>
  703. sip:[email protected]:6060
  704. </entry>
  705. </row>
  706. <row>
  707. <entry>
  708. <command>rewriteuserpass("alice:pw")</command> replaces the pair
  709. user:password in URI with a new value. Rewriting password in URI is of historical
  710. meaning though, since basic password has been replaced with digest authentication.
  711. </entry>
  712. <entry>
  713. sip:alice:[email protected]:6060
  714. </entry>
  715. </row>
  716. <row>
  717. <entry>
  718. <command>rewriteport("1234")</command> replaces port number in URI
  719. </entry>
  720. <entry>
  721. sip:[email protected]:1234
  722. </entry>
  723. </row>
  724. <row>
  725. <entry>
  726. <command>prefix("9")</command> inserts a string ahead of user part of URI
  727. </entry>
  728. <entry>
  729. sip:[email protected]:6060
  730. </entry>
  731. </row>
  732. <row>
  733. <entry>
  734. <command>strip(2)</command> removes leading characters from user part of URI
  735. </entry>
  736. <entry>
  737. sip:[email protected]:6060
  738. </entry>
  739. </row>
  740. </tbody>
  741. </tgroup>
  742. </table>
  743. </para>
  744. <para>
  745. You can verify whether you understood URI processing by
  746. looking at the following example. It rewrites URI
  747. several times. The question is what is the final URI to which
  748. the script fill forward any incoming request.
  749. <example>
  750. <title>URI-rewriting Exercise</title>
  751. <programlisting>
  752. exec_dset("echo sip:[email protected]; echo > /dev/null");
  753. strip(2);
  754. if (uri=~"^sip:2") {
  755. prefix("0");
  756. } else {
  757. prefix("1");
  758. };
  759. forward(uri:host, uri:port);
  760. </programlisting>
  761. </example>
  762. </para>
  763. <para>
  764. The correct answer is the resulting URI will be
  765. "sip:[email protected]". <command>exec_dset</command>
  766. rewrites original URI to "sip:[email protected]",
  767. <command>strip(2)</command> takes
  768. two leading characters from username away resulting
  769. in "[email protected]", the condition does not match
  770. because URI does not begin with "2" any more,
  771. so the prefix "1" is inserted.
  772. </para>
  773. </section> <!-- URI rewriting -->
  774. <section>
  775. <title>Destination Set</title>
  776. <para>
  777. Whereas needs of many scenarios can by accommodated by maintaining
  778. a single request URI, some scenarios are better served by
  779. multiple URIs. Consider for example a user with address
  780. [email protected]. The user wishes to be reachable at his
  781. home phone, office phone, cell phone, softphone, etc.
  782. However, he still wishes to maintain a single public address
  783. on his business card.
  784. </para>
  785. <para>
  786. To enable such scenarios, <application>ser</application>
  787. allows translation of a single request URI into multiple
  788. outgoing URIs. The ability to forward a request to multiple
  789. destinations is known as <emphasis>forking</emphasis>
  790. in SIP language. All outgoing URIs (in trivial case one of them)
  791. are called <emphasis>destination set</emphasis>. The destination
  792. set always includes one default URI, to which additional URIs
  793. can be appended. Maximum size of a destination set is limited by
  794. a compile-time constant, MAX_BRANCHES,
  795. in <filename>config.h</filename>.
  796. </para>
  797. <para>
  798. Some actions are designed for use with a single URI whereas
  799. other actions work with the whole destination set.
  800. </para>
  801. <para>
  802. Actions which are currently available for creating the destination
  803. set are <command>lookup</command> from usrloc module and
  804. <command>exec_dset</command> from exec module.
  805. <command>lookup</command> fills in the destination
  806. set with user contact's registered previously with REGISTER
  807. requests. The <command>exec</command> actions
  808. fill in the destination set with output of an external program.
  809. In both cases, current destination set is completely rewritten.
  810. New URIs can be appended to destination set by a call to the built-in
  811. action <command>append_branch(uri)</command>.
  812. </para>
  813. <para>
  814. Currently supported features which utilize destination sets
  815. are <emphasis>forking</emphasis> and <emphasis>redirection</emphasis>.
  816. Action <command>t_relay</command> (TM module) for stateful
  817. forwarding supports forking. If called with a non-trivial destination
  818. set, <command>t_relay</command> forks
  819. incoming request to all URIs in current destination set.
  820. See <xref linkend="rewriteuri"/>. If a user
  821. previously registered from three locations, the destination set is filled with
  822. all of them by <command>lookup</command> and the <command>t_relay</command>
  823. command forwards the incoming request to all these destinations.
  824. Eventually, all user's phone will be ringing in parallel.
  825. </para>
  826. <para>
  827. SIP redirection is another feature which leverages destination sets.
  828. It is a very light-weighted method to establish communication
  829. between two parties with minimum burden put on the server. In
  830. <application>ser</application>, the action <command>sl_send_reply</command>
  831. (SL module) is used for this purpose. This action
  832. allows to generate replies to SIP requests without keeping
  833. any state. If the status code passed to the action is 3xx,
  834. the current destination set is printed in reply's Contact header
  835. fields. Such a reply instructs the originating client to
  836. retry at these addresses. (See <xref linkend="redirectexample"/>).
  837. </para>
  838. <para>
  839. Most other <application>ser</application> actions ignore destination
  840. sets: they either do not relate to URI processing (<command moreinfo="none">
  841. log</command>, for example) or they work only with the default URI.
  842. All URI-rewriting functions such as
  843. <command moreinfo="none">rewriteuri</command> belong in this
  844. category. URI-comparison operands only refer to the first URI
  845. (see <xref linkend="operators"/>). Also, the built-in action
  846. for stateless forwarding, <command>forward</command> works only
  847. with the default URI and ignores rest of the destination set. The reason
  848. is a proxy server willing to fork must guarantee that the burden
  849. of processing multiple replies is not put unexpectedly on upstream
  850. client. This is only achievable with stateful processing.
  851. Forking cannot be used along with stateless <command>forward</command>,
  852. which thus only processes one URI out of the whole destination set.
  853. </para>
  854. </section> <!-- Destination Set -->
  855. <section>
  856. <title>User Location</title>
  857. <para>
  858. Mobility is a key feature of SIP. Users are able to use one
  859. one or more SIP devices and be reachable at them. Incoming requests
  860. for users are forwarded to all user's devices in use. The key
  861. concept is that of soft-state registration. Users can
  862. -- if in possession of valid credentials -- link SIP
  863. devices to their e-mail like address of record. Their SIP devices
  864. do so using a REGISTER request, as in <xref linkend="register"/>.
  865. The request creates a binding between the public address of
  866. record (To header field) and SIP device's current address
  867. (Contact header field).
  868. <example id="register">
  869. <title>REGISTER Request</title>
  870. <programlisting>
  871. REGISTER sip:192.168.2.16 SIP/2.0
  872. Via: SIP/2.0/UDP 192.168.2.16;branch=z9hG4bKd5e5.5a9947e4.0
  873. Via: SIP/2.0/UDP 192.168.2.33:5060
  874. From: sip:[email protected]
  875. To: sip:[email protected]
  876. Call-ID: [email protected]
  877. Date: Wed, 29 Jan 2003 18:13:15 GMT
  878. CSeq: 101 REGISTER
  879. User-Agent: CSCO/4
  880. Contact: sip:[email protected]:5060
  881. Content-Length: 0
  882. Expires: 600
  883. </programlisting>
  884. </example>
  885. Similar requests can be used to query all user's current contacts or to
  886. delete them. All Contacts have certain time to live, when the time expires,
  887. contact is removed and no longer used for processing of incoming requests.
  888. </para>
  889. <para>
  890. <application>ser</application> is built to do both: update
  891. user location database from received REGISTER requests and look-up these
  892. contacts when inbound requests for a user arrive. To achieve high performance,
  893. the user location table is stored in memory. In regular intervals
  894. (usrloc module's parameter <varname>timer_interval</varname> determines
  895. their length), all changes to the in-memory table are backed up in
  896. <application>mysql</application> database to achieve
  897. persistence across server reboots. Administrators or application writers
  898. can lookup list of current user's contacts stored in memory using the
  899. <application>serctl</application> tool (see <xref linkend="serctl"/>).
  900. <example>
  901. <title>Use of <application>serctl</application> Tool to Query User Location</title>
  902. <screen>
  903. <![CDATA[
  904. [jiri@fox jiri]$ sc ul show jiri
  905. <sip:[email protected]>;q=0.00;expires=456
  906. <sip:[email protected]>;q=0.00;expires=36000
  907. ]]>
  908. </screen>
  909. </example>
  910. </para>
  911. <para>
  912. Building user location in <application>ser</application> scripts is
  913. quite easy. One first needs to determine whether a request is for served domain,
  914. as described in <xref linkend="domainmatching"/>. If that is the case, the script
  915. needs to distinguish between REGISTER requests, that update user location table,
  916. and all other requests for which next hop is determined from the table. The
  917. <command>save</command> action is used to update user location
  918. (i.e., it writes to it). The <command>lookup</command> actions
  919. reads from the user location table and fills in destination set with current
  920. user's contacts.
  921. <example>
  922. <title>Use of User Location Actions</title>
  923. <programlisting>
  924. # is the request for my domain ?
  925. if (uri==myself) {
  926. if (method=="REGISTER") { # REGISTERs are used to update
  927. save("location");
  928. break; # that's it, we saved the contacts, exit now
  929. } else {
  930. if (!lookup("location") { # no registered contact
  931. sl_send_reply("404", "Not Found");
  932. break;
  933. }
  934. # ok -- there are some contacts for the user; forward
  935. # the incoming request to all of them
  936. t_relay();
  937. };
  938. };
  939. </programlisting>
  940. </example>
  941. </para>
  942. <para>
  943. Note that we used the action for stateful forwarding,
  944. <command>t_relay</command>. That's because
  945. stateful forwarding allows to fork an incoming request to
  946. multiple destinations. If we used stateless forwarding,
  947. the request would be forwarded only to one uri out of
  948. all user's contacts.
  949. </para>
  950. </section> <!-- User Location -->
  951. <section>
  952. <title>External Modules</title>
  953. <para>
  954. <application>ser</application> provides the ability to link the server with external
  955. third-party shared libraries. Lot of functionality which is
  956. included in the <application>ser</application> distribution is actually located in
  957. modules to keep the server "core" compact and clean.
  958. Among others, there are modules for checking max_forwards
  959. value in SIP requests (maxfwd), transactional processing (tm),
  960. record routing (rr), accounting (acc), authentication (auth),
  961. SMS gateway (sms), replying requests (sl), user location
  962. (usrloc, registrar) and more.
  963. </para>
  964. <para>
  965. In order to utilize new actions exported by a module,
  966. ser must first load it. To load a module, the directive
  967. <command>loadmodule "filename"</command>
  968. must be included in beginning of
  969. a <application>ser</application> script file.
  970. </para>
  971. <example>
  972. <title>Using Modules</title>
  973. <para>
  974. This example shows how a script instructs
  975. <application>ser</application> to
  976. load a module and use actions exported by it.
  977. Particularly, the sl module exports an action
  978. <command>sl_send_reply</command> which makes
  979. <application>ser</application> act as a stateless
  980. user agent and reply all incoming requests with 404.
  981. </para>
  982. <programlisting>
  983. # first of all, load the module!
  984. loadmodule "/usr/lib/ser/modules/sl.so
  985. route{
  986. # reply all requests with 404
  987. sl_send_reply("404", "I am so sorry -- user not found");
  988. }
  989. </programlisting>
  990. </example>
  991. <note>
  992. <para>
  993. Note that unlike with core commands, all actions exported by
  994. modules must have parameters enclosed in quotation marks in
  995. current version of <application>ser</application>. In the following example,
  996. the built-in action <command>forward</command>
  997. for stateless forwarding takes IP address and port numbers as
  998. parameters without quotation marks whereas a module action
  999. <command>t_relay</command> for stateful
  1000. forwarding takes parameters enclosed in quotation marks.
  1001. <example>
  1002. <title>Parameters in built-in and exported
  1003. actions</title>
  1004. <programlisting>
  1005. # built-in action doesn't enclose IP addresses and port numbers
  1006. # in quotation marks
  1007. forward(192.168.99.100, 5060);
  1008. # module-exported functions enclose all parameters in quotation
  1009. # marks
  1010. t_relay_to_udp("192.168.99.100", "5060");
  1011. </programlisting>
  1012. </example>
  1013. </para>
  1014. </note>
  1015. <para>
  1016. Many modules also allow users to change the way how they work using
  1017. predefined parameters. For example, the authentication module needs
  1018. to know location of MySQL database which contains users' security
  1019. credentials. How module parameters are set using the
  1020. <command>modparam</command> directive is shown in <xref
  1021. linkend="moduleparameters"/>. <command>modparam</command> always
  1022. contains identification of module, parameter name and parameter
  1023. value. Description of parameters available in modules is available
  1024. in module documentation.
  1025. </para>
  1026. <para>
  1027. Yet another thing to notice in this example is module
  1028. dependency. Modules may depend on each other. For example, the
  1029. authentication modules leverages the mysql module for accessing
  1030. mysql databases and sl module for generating authentication
  1031. challenges. We recommend that modules are loaded in dependency
  1032. order to avoid ambiguous server behavior. </para>
  1033. <para>
  1034. <example id="moduleparameters">
  1035. <title>Module Parameters</title>
  1036. <programlisting>
  1037. # ------------------ module loading ----------------------------------
  1038. # load first modules on which 'auth' module depends;
  1039. # sl is used for sending challenges, mysql for storage
  1040. # of user credentials
  1041. loadmodule "modules/sl/sl.so"
  1042. loadmodule "modules/mysql/mysql.so"
  1043. loadmodule "modules/auth/auth.so"
  1044. # ------------------ module parameters -------------------------------
  1045. # tell the auth module the access data for SQL database:
  1046. # username, password, hostname and database name
  1047. modparam("auth", "db_url","mysql://ser:secret@dbhost/ser")
  1048. # ------------------------- request routing logic -------------------
  1049. # authenticate all requests prior to forwarding them
  1050. route{
  1051. if (!proxy_authorize("foo.bar" /* realm */,
  1052. "subscriber" /* table name */ )) {
  1053. proxy_challenge("foo.bar", "0");
  1054. break;
  1055. };
  1056. forward(192.168.0.10,5060);
  1057. }
  1058. </programlisting>
  1059. </example>
  1060. </para>
  1061. </section>
  1062. <section id="writing_scripts">
  1063. <title>Writing Scripts</title>
  1064. <para>
  1065. This section demonstrates simple examples
  1066. how to configure server's behavior using the
  1067. <application>ser</application>
  1068. request routing language. All configuration scripts follow the
  1069. <application>ser</application> language
  1070. syntax, which dictates the following section ordering:
  1071. <itemizedlist>
  1072. <listitem>
  1073. <para>
  1074. <emphasis>global configuration parameters</emphasis> --
  1075. these value affect behavior of the server such as port
  1076. number at which it listens, number of spawned children
  1077. processes, and log-level. See <xref
  1078. linkend="coreoptions"/> for a list of available options.
  1079. </para>
  1080. </listitem>
  1081. <listitem>
  1082. <para>
  1083. <emphasis>module loading</emphasis> -- these statements
  1084. link external modules, such as transaction management
  1085. (tm) or stateless UA server (sl) dynamically. See
  1086. <xref linkend="modulereference"/> for a list of modules
  1087. included in <application>ser</application>
  1088. distribution.
  1089. </para>
  1090. <note>
  1091. <para>
  1092. If modules depend on each other, than the depending
  1093. modules must be loaded after modules on which they
  1094. depend. We recommend to load first modules
  1095. <command>tm</command> and <command>sl</command>
  1096. because many other modules (authentication, user
  1097. location, accounting, etc.) depend on these.
  1098. </para>
  1099. </note>
  1100. </listitem>
  1101. <listitem>
  1102. <para>
  1103. <emphasis>module-specific parameters</emphasis> -- determine
  1104. how modules behave; for example, it is possible to configure
  1105. database to be used by authentication module.
  1106. </para>
  1107. </listitem>
  1108. <listitem>
  1109. <para>
  1110. one or more <emphasis>route blocks</emphasis> containing the
  1111. request processing logic, which includes built-in actions
  1112. as well as actions exported by modules. See <xref linkend="builtinref"/>
  1113. for a list of built-in actions.
  1114. </para>
  1115. </listitem>
  1116. <listitem>
  1117. <para>
  1118. optionally, if modules supporting reply
  1119. processing (currently only TM) are loaded,
  1120. one or more <emphasis>failure_route blocks</emphasis> containing
  1121. logic triggered by received replies. Restrictions on use of
  1122. actions within <command>failure_route</command>
  1123. blocks apply -- see <xref linkend="builtinref"/> for more
  1124. information.
  1125. </para>
  1126. </listitem>
  1127. </itemizedlist>
  1128. </para>
  1129. <section id="defaultscript">
  1130. <title>Default Configuration Script</title>
  1131. <para>
  1132. The configuration script, <filename>ser.cfg</filename>,
  1133. is a part of every <application>ser</application>
  1134. distribution and defines default behavior. It allows users
  1135. to register with the server and have requests proxied to each
  1136. other.
  1137. </para>
  1138. <para>
  1139. After performing
  1140. routine checks, the script looks whether incoming request is for
  1141. served domain. If so and the request is "REGISTER", <application>ser</application>
  1142. acts as SIP registrar and updates database of user's contacts.
  1143. Optionally, it verifies user's identity first to avoid
  1144. unauthorized contact manipulation.
  1145. </para>
  1146. <para>
  1147. Non-REGISTER requests for served domains are then processed using
  1148. user location database. If a contact is found for requested URI,
  1149. script execution proceeds to stateful forwarding, a negative 404
  1150. reply is generated otherwise. Requests outside served domain
  1151. are always statefully forwarded.
  1152. </para>
  1153. <para>
  1154. Note that this simple script features several limitations:
  1155. <itemizedlist>
  1156. <listitem>
  1157. <para>
  1158. By default, authentication is turned off to avoid
  1159. dependency on mysql. Unless it it turned on, anyone
  1160. can register using any name and "steal" someone else's
  1161. calls.
  1162. </para>
  1163. </listitem>
  1164. <listitem>
  1165. <para>
  1166. Even it authentication is turned on, there is no relationship
  1167. between authentication username and address of record. That
  1168. means that for example a user authenticating himself correctly
  1169. with "john.doe" id may register contacts for "gw.bush".
  1170. Site policy may wish to mandate authentication id to be equal
  1171. to username claimed in To header field. <action>check_to</action>
  1172. action from auth module can be used to enforce such a policy.
  1173. </para>
  1174. </listitem>
  1175. <listitem>
  1176. <para>
  1177. There is no dialing plan implemented. All users are supposed to
  1178. be reachable via user location database. See <xref linkend="numberingplans"/>
  1179. for more information.
  1180. </para>
  1181. </listitem>
  1182. <listitem>
  1183. <para>
  1184. The script assumes users will be using server's name as a part of
  1185. their address of record. If users wish to use another name (domain
  1186. name for example), this must be set using the <varname>alias</varname>
  1187. options. See <xref linkend="domainmatching"/> for more information.
  1188. </para>
  1189. </listitem>
  1190. <listitem>
  1191. <para>
  1192. If authentication is turned on by uncommenting related configuration
  1193. options, clear-text user passwords will by assumed in back-end database.
  1194. </para>
  1195. </listitem>
  1196. </itemizedlist>
  1197. </para>
  1198. <example>
  1199. <title>Default Configuration Script</title>
  1200. <programlisting>
  1201. <xi:include href="../../etc/ser.cfg" parse="text"/>
  1202. </programlisting>
  1203. </example>
  1204. </section>
  1205. <section id="statefulua">
  1206. <title>Stateful User Agent Server</title>
  1207. <para>
  1208. This examples shows how to make ser act as a stateful user
  1209. agent (UA). Ability to act as as a stateful UA is essential
  1210. to many applications which terminate a SIP path. These
  1211. applications wish to focus on their added value. They
  1212. do not wish to be involved in all SIP gory details, such
  1213. as request and reply retransmission, reply formatting, etc.
  1214. For example, we use the UA functionality to shield
  1215. SMS gateway and instant message store from SIP transactional
  1216. processing.
  1217. The simple example bellow issues a log report on receipt
  1218. of a new transaction.
  1219. If we did not use a stateful UA, every single request retransmission
  1220. would cause the application to be re-executed which would result in
  1221. duplicated SMS messages, instant message in message store or
  1222. log reports.
  1223. </para>
  1224. <para>
  1225. The most important actions are <command> t_newtran</command>
  1226. and <command> t_reply</command>. <command>
  1227. t_newtran</command> shields subsequent code from
  1228. retransmissions. It returns success and continues when a new
  1229. request arrived. It exits current route block immediately on
  1230. receipt of a retransmission. It only returns a negative value
  1231. when a serious error, such as lack of memory, occurs.
  1232. </para>
  1233. <para>
  1234. <command>t_reply</command> generates
  1235. a reply for a request. It generates the reply statefully,
  1236. i.e., it is kept for future retransmissions in memory.
  1237. </para>
  1238. <note>
  1239. <para>
  1240. Applications that do not need stateful processing
  1241. may act as stateless UA Server too. They just use
  1242. the <command>sl_send_reply</command> action to
  1243. send replies to requests without keeping any
  1244. state. The benefit is memory cannot run out,
  1245. the drawback is that each retransmission needs to
  1246. be processed as a new request. An example of use
  1247. of a stateless server is shown in
  1248. <xref linkend="redirectserver"/> and
  1249. <xref linkend="executingscript"/>.
  1250. </para>
  1251. </note>
  1252. <example>
  1253. <title>Stateful UA Server</title>
  1254. <programlisting format="linespecific">
  1255. <xi:include href="../../examples/uas.cfg" parse="text"/>
  1256. </programlisting>
  1257. </example>
  1258. </section> <!-- Stateful UAS -->
  1259. <section id="redirectserver">
  1260. <title>Redirect Server</title>
  1261. <para>
  1262. The redirect example shows how to redirect a request
  1263. to multiple destination using 3xx reply. Redirecting
  1264. requests as opposed to proxying them is essential to
  1265. various scalability scenarios. Once a message is
  1266. redirected, <application>ser</application>
  1267. discards all related state and is no more involved
  1268. in subsequent SIP transactions (unless the redirection
  1269. addresses point to the same server again).
  1270. </para>
  1271. <para>
  1272. The key <application>ser</application> actions in this example
  1273. are <command>append_branch</command> and
  1274. <command>sl_send_reply</command> (sl module).
  1275. </para>
  1276. <para>
  1277. <command>append_branch</command> adds
  1278. a new item to the destination set. The destinations set always
  1279. includes the current URI and may be enhanced up to
  1280. <constant>MAX_BRANCHES</constant> items.
  1281. <command>sl_send_reply</command> command,
  1282. if passed SIP reply code 3xx, takes all values in current
  1283. destination set and adds them to Contact header field in
  1284. the reply being sent.
  1285. </para>
  1286. <example id="redirectexample">
  1287. <title>Redirect Server</title>
  1288. <programlisting>
  1289. <xi:include href="../../examples/redirect.cfg" parse="text"/>
  1290. </programlisting>
  1291. </example>
  1292. </section> <!-- redirect server-->
  1293. <section id="executingscript">
  1294. <title>Executing External Script</title>
  1295. <para>
  1296. Like in the previous example, we show how to
  1297. make <application>ser</application> act as a redirect server. The difference is
  1298. that we do not use redirection addresses hardwired in
  1299. <application>ser</application> script but
  1300. get them from external shell commands. We also use
  1301. ser's ability to execute shell commands to log
  1302. source IP address of incoming SIP requests.
  1303. </para>
  1304. <para>
  1305. The new commands introduced in this example are
  1306. <command>exec_msg</command> and
  1307. <command>exec_dset</command>.
  1308. <command>exec_msg</command> takes
  1309. current requests, starts an external command, and
  1310. passes the requests to the command's standard input.
  1311. It also passes request's source IP address in
  1312. environment variable named <constant>SRCIP</constant>.
  1313. </para>
  1314. <para>
  1315. <command>exec_dset</command> serves for URI rewriting by
  1316. external applications. The <command>exec_dset</command> action
  1317. passes current URI to the called external program, and rewrites
  1318. current destination set with the program's output. An example
  1319. use would be an implementation of a Least-Cost-Router, software
  1320. which returns URI of the cheapest PSTN provider for a given
  1321. destination based on some pricing tables. <xref
  1322. linkend="execscript"/> is much easier: it prints fixed URIs on
  1323. its output using shell script <command>echo</command> command.
  1324. </para>
  1325. <note>
  1326. <para>
  1327. This script works statelessly -- it uses this action for
  1328. stateless replying, <command>sl_send_reply</command>. No
  1329. transaction is kept in memory and each request
  1330. retransmission is processed as a brand-new request. That
  1331. may be a particular concern if the server logic
  1332. (<command>exec</command> actions in this example) is too
  1333. expensive. See <xref linkend="statefulua"/> for instructions
  1334. on how to make server logic stateful, so that
  1335. retransmissions are absorbed and do not cause re-execution
  1336. of the logic.
  1337. </para>
  1338. </note>
  1339. <example id="execscript">
  1340. <title>Executing External Script</title>
  1341. <programlisting>
  1342. <xi:include href="../../examples/exec.cfg" parse="text"/>
  1343. </programlisting>
  1344. </example>
  1345. </section> <!-- exec example -->
  1346. <section id="replyprocessingsection">
  1347. <title>On-Reply Processing (Forward on Unavailable)</title>
  1348. <para>
  1349. Many services depend on status of messages relayed
  1350. downstream: <emphasis>forward on busy</emphasis> and
  1351. <emphasis>forward on no reply</emphasis> to name the
  1352. most well-known ones. To support implementation of
  1353. such services, <application>ser</application>
  1354. allows to return to request processing when request
  1355. forwarding failed. When a request is reprocessed,
  1356. new request branches may be initiated or the transaction
  1357. can be completed at discretion of script writer.
  1358. </para>
  1359. <para>
  1360. The primitives used are <command>t_on_failure(r)</command>
  1361. and <command>failure_route[r]{}.</command> If
  1362. <command>t_on_failure</command> is called before
  1363. a request is statefully forwarded and a forwarding failure occurs,
  1364. <application>ser</application>
  1365. will return to request processing in a <command>failure_route</command>
  1366. block. Failures include receipt of a SIP error
  1367. (status code >= 300 ) from downstream or not receiving
  1368. any final reply within final response period.
  1369. </para>
  1370. <para>
  1371. The length of the timer is governed by parameters of the
  1372. tm module. <varname>fr_timer</varname> is the length of
  1373. timer set for non-INVITE transactions and INVITE transactions
  1374. for which no provisional response is received. If a timer
  1375. hits, it indicates that a downstream server is unresponsive.
  1376. <varname>fr_inv_timer</varname> governs time to wait for
  1377. a final reply for an INVITE. It is typically longer than
  1378. <varname>fr_timer</varname> because final reply may take
  1379. long time until callee (finds a mobile phone in a pocket and)
  1380. answers the call.
  1381. </para>
  1382. <para>
  1383. In <xref linkend="replyprocessing"/>,
  1384. <command>failure_route[1]</command> is set to be entered on
  1385. error using the <command>t_on_failure(1)</command>
  1386. action. Within this reply block,
  1387. <application>ser</application> is instructed to initiate a
  1388. new branch and try to reach called party at another
  1389. destination (sip:[email protected]). To deal with the case
  1390. when neither the alternate destination succeeds,
  1391. <application>t_on_failure</application> is set again. If
  1392. the case really occurs, <command>failure_route[2]</command>
  1393. is entered and a last resort destination
  1394. (sip:[email protected]) is tried.
  1395. </para>
  1396. <example id="replyprocessing">
  1397. <title>On-Reply Processing</title>
  1398. <programlisting>
  1399. <xi:include href="../../examples/onr.cfg" parse="text"/>
  1400. </programlisting>
  1401. </example>
  1402. </section> <!-- reply processing -->
  1403. </section> <!-- examples -->
  1404. </section>