README 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. Regex Module
  2. Iñaki Baz Castillo
  3. <[email protected]>
  4. Edited by
  5. Iñaki Baz Castillo
  6. <[email protected]>
  7. Copyright © 2009 Iñaki Baz Castillo
  8. __________________________________________________________________
  9. Table of Contents
  10. 1. Admin Guide
  11. 1. Overview
  12. 2. Dependencies
  13. 2.1. Kamailio Modules
  14. 2.2. External Libraries or Applications
  15. 3. Parameters
  16. 3.1. file (string)
  17. 3.2. max_groups (int)
  18. 3.3. group_max_size (int)
  19. 3.4. pcre_caseless (int)
  20. 3.5. pcre_multiline (int)
  21. 3.6. pcre_dotall (int)
  22. 3.7. pcre_extended (int)
  23. 4. Functions
  24. 4.1. pcre_match (string, pcre_regex)
  25. 4.2. pcre_match_group (string [, group])
  26. 5. MI Commands
  27. 5.1. regex_reload
  28. 6. Installation and Running
  29. 6.1. File format
  30. List of Examples
  31. 1.1. Set file parameter
  32. 1.2. Set max_groups parameter
  33. 1.3. Set group_max_size parameter
  34. 1.4. Set pcre_caseless parameter
  35. 1.5. Set pcre_multiline parameter
  36. 1.6. Set pcre_dotall parameter
  37. 1.7. Set pcre_extended parameter
  38. 1.8. pcre_match usage (forcing case insensitive)
  39. 1.9. pcre_match usage (using "end of line" symbol)
  40. 1.10. pcre_match_group usage
  41. 1.11. pcre_match_group usage (using a pseudo-variable as group)
  42. 1.12. regex file
  43. 1.13. Using with pua_usrloc
  44. 1.14. Incorrect groups file
  45. Chapter 1. Admin Guide
  46. Table of Contents
  47. 1. Overview
  48. 2. Dependencies
  49. 2.1. Kamailio Modules
  50. 2.2. External Libraries or Applications
  51. 3. Parameters
  52. 3.1. file (string)
  53. 3.2. max_groups (int)
  54. 3.3. group_max_size (int)
  55. 3.4. pcre_caseless (int)
  56. 3.5. pcre_multiline (int)
  57. 3.6. pcre_dotall (int)
  58. 3.7. pcre_extended (int)
  59. 4. Functions
  60. 4.1. pcre_match (string, pcre_regex)
  61. 4.2. pcre_match_group (string [, group])
  62. 5. MI Commands
  63. 5.1. regex_reload
  64. 6. Installation and Running
  65. 6.1. File format
  66. 1. Overview
  67. This module offers matching operations using regular expressions based
  68. on the powerful PCRE library.
  69. A text file containing regular expressions categorized in groups is
  70. compiled when the module is loaded, the resulting PCRE objects are
  71. stored in an array. A function to match a string or pseudo-variable
  72. against any of these groups is provided. The text file can be modified
  73. and reloaded at any time via a MI command. The module also offers a
  74. function to perform a PCRE matching operation against a regular
  75. expression provided as function parameter.
  76. For a detailed list of PCRE features read the man page of the library.
  77. 2. Dependencies
  78. 2.1. Kamailio Modules
  79. 2.2. External Libraries or Applications
  80. 2.1. Kamailio Modules
  81. The following modules must be loaded before this module:
  82. * No dependencies on other Kamailio modules.
  83. 2.2. External Libraries or Applications
  84. The following libraries or applications must be installed before
  85. running Kamailio with this module loaded:
  86. * libpcre - the libraries of PCRE.
  87. 3. Parameters
  88. 3.1. file (string)
  89. 3.2. max_groups (int)
  90. 3.3. group_max_size (int)
  91. 3.4. pcre_caseless (int)
  92. 3.5. pcre_multiline (int)
  93. 3.6. pcre_dotall (int)
  94. 3.7. pcre_extended (int)
  95. 3.1. file (string)
  96. Text file containing the regular expression groups. It must be set in
  97. order to enable the group matching function.
  98. Default value is "NULL".
  99. Example 1.1. Set file parameter
  100. ...
  101. modparam("regex", "file", "/etc/kamailio/regex_groups")
  102. ...
  103. 3.2. max_groups (int)
  104. Max number of regular expression groups in the text file.
  105. Default value is "20".
  106. Example 1.2. Set max_groups parameter
  107. ...
  108. modparam("regex", "max_groups", 40)
  109. ...
  110. 3.3. group_max_size (int)
  111. Max content size of a group in the text file.
  112. Default value is "8192".
  113. Example 1.3. Set group_max_size parameter
  114. ...
  115. modparam("regex", "group_max_size", 16384)
  116. ...
  117. 3.4. pcre_caseless (int)
  118. If this options is set, matching is done caseless. It is equivalent to
  119. Perl's /i option, and it can be changed within a pattern by a (?i) or
  120. (?-i) option setting.
  121. Default value is "0".
  122. Example 1.4. Set pcre_caseless parameter
  123. ...
  124. modparam("regex", "pcre_caseless", 1)
  125. ...
  126. 3.5. pcre_multiline (int)
  127. By default, PCRE treats the subject string as consisting of a single
  128. line of characters (even if it actually contains newlines). The "start
  129. of line" metacharacter (^) matches only at the start of the string,
  130. while the "end of line" metacharacter ($) matches only at the end of
  131. the string, or before a terminating newline.
  132. When this option is set, the "start of line" and "end of line"
  133. constructs match immediately following or immediately before internal
  134. newlines in the subject string, respectively, as well as at the very
  135. start and end. This is equivalent to Perl's /m option, and it can be
  136. changed within a pattern by a (?m) or (?-m) option setting. If there
  137. are no newlines in a subject string, or no occurrences of ^ or $ in a
  138. pattern, setting this option has no effect.
  139. Default value is "0".
  140. Example 1.5. Set pcre_multiline parameter
  141. ...
  142. modparam("regex", "pcre_multiline", 1)
  143. ...
  144. 3.6. pcre_dotall (int)
  145. If this option is set, a dot metacharater in the pattern matches all
  146. characters, including those that indicate newline. Without it, a dot
  147. does not match when the current position is at a newline. This option
  148. is equivalent to Perl's /s option, and it can be changed within a
  149. pattern by a (?s) or (?-s) option setting.
  150. Default value is "0".
  151. Example 1.6. Set pcre_dotall parameter
  152. ...
  153. modparam("regex", "pcre_dotall", 1)
  154. ...
  155. 3.7. pcre_extended (int)
  156. If this option is set, whitespace data characters in the pattern are
  157. totally ignored except when escaped or inside a character class.
  158. Whitespace does not include the VT character (code 11). In addition,
  159. characters between an unescaped # outside a character class and the
  160. next newline, inclusive, are also ignored. This is equivalent to Perl's
  161. /x option, and it can be changed within a pattern by a (?x) or (?-x)
  162. option setting.
  163. Default value is "0".
  164. Example 1.7. Set pcre_extended parameter
  165. ...
  166. modparam("regex", "pcre_extended", 1)
  167. ...
  168. 4. Functions
  169. 4.1. pcre_match (string, pcre_regex)
  170. 4.2. pcre_match_group (string [, group])
  171. 4.1. pcre_match (string, pcre_regex)
  172. Matches the given string parameter against the regular expression
  173. pcre_regex, which is compiled in runtime into a PCRE object. Returns
  174. TRUE if it matches, FALSE otherwise.
  175. Meaning of the parameters is as follows:
  176. * string - String or pseudo-variable to compare.
  177. * pcre_regex - Regular expression to be compiled in a PCRE object. It
  178. can be a string or pseudo-variable.
  179. NOTE: To use the "end of line" symbol '$' in the pcre_regex parameter
  180. use '$$'.
  181. This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
  182. ONREPLY_ROUTE, BRANCH_ROUTE and LOCAL_ROUTE.
  183. Example 1.8. pcre_match usage (forcing case insensitive)
  184. ...
  185. if (pcre_match("$ua", "(?i)^twinkle")) {
  186. xlog("L_INFO", "User-Agent matches\n");
  187. }
  188. ...
  189. Example 1.9. pcre_match usage (using "end of line" symbol)
  190. ...
  191. if (pcre_match("$rU", "^user[1234]$$")) { # Will be converted to "^user[1234]$"
  192. xlog("L_INFO", "RURI username matches\n");
  193. }
  194. ...
  195. 4.2. pcre_match_group (string [, group])
  196. Tries to match the given string against a specific group in the text
  197. file (see Section 6.1, "File format"). Returns TRUE if it matches,
  198. FALSE otherwise.
  199. Meaning of the parameters is as follows:
  200. * string - String or pseudo-variable to compare.
  201. * group - Number of group to use in the operation. If not specified
  202. then 0 (the first group) is used. A pseudo-variable containing an
  203. integer can also be used.
  204. This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
  205. ONREPLY_ROUTE, BRANCH_ROUTE and LOCAL_ROUTE.
  206. Example 1.10. pcre_match_group usage
  207. ...
  208. if (pcre_match_group("$rU", "2")) {
  209. xlog("L_INFO", "RURI username matches group 2\n");
  210. }
  211. ...
  212. Example 1.11. pcre_match_group usage (using a pseudo-variable as group)
  213. ...
  214. $avp(i:10) = 5; # Maybe got from a DB query.
  215. if (pcre_match_group("$ua", "$avp(i:10)")) {
  216. xlog("L_INFO", "User-Agent matches group 5\n");
  217. }
  218. ...
  219. 5. MI Commands
  220. 5.1. regex_reload
  221. 5.1. regex_reload
  222. Causes regex module to re-read the content of the text file and
  223. re-compile the regular expressions. The number of groups in the file
  224. can be modified safely.
  225. Name: regex_reload
  226. Parameters: none
  227. MI FIFO Command Format:
  228. :regex_reload:_reply_fifo_file_
  229. _empty_line_
  230. 6. Installation and Running
  231. 6.1. File format
  232. 6.1. File format
  233. The file contains regular expressions categorized in groups. Each group
  234. starts with "[number]" line. Lines starting by space, tab, CR, LF or #
  235. (comments) are ignored. Each regular expression must take up just one
  236. line, this means that a regular expression can't be splitted in various
  237. lines.
  238. An example of the file format would be the following:
  239. Example 1.12. regex file
  240. ### List of User-Agents publishing presence status
  241. [0]
  242. # Softphones
  243. ^Twinkle/1
  244. ^X-Lite
  245. ^eyeBeam
  246. ^Bria
  247. ^SIP Communicator
  248. ^Linphone
  249. # Deskphones
  250. ^Snom
  251. # Others
  252. ^SIPp
  253. ^PJSUA
  254. ### Blacklisted source IP's
  255. [1]
  256. ^190\.232\.250\.226$
  257. ^122\.5\.27\.125$
  258. ^86\.92\.112\.
  259. ### Free PSTN destinations in Spain
  260. [2]
  261. ^1\d{3}$
  262. ^((\+|00)34)?900\d{6}$
  263. The module compiles the text above to the following regular
  264. expressions:
  265. group 0: ((^Twinkle/1)|(^X-Lite)|(^eyeBeam)|(^Bria)|(^SIP Communicator)|
  266. (^Linphone)|(^Snom)|(^SIPp)|(^PJSUA))
  267. group 1: ((^190\.232\.250\.226$)|(^122\.5\.27\.125$)|(^86\.92\.112\.))
  268. group 2: ((^1\d{3}$)|(^((\+|00)34)?900\d{6}$))
  269. The first group can be used to avoid auto-generated PUBLISH (pua_usrloc
  270. module) for UA's already supporting presence:
  271. Example 1.13. Using with pua_usrloc
  272. route[REGISTER] {
  273. if (! pcre_match_group("$ua", "0")) {
  274. xlog("L_INFO", "Auto-generated PUBLISH for $fu ($ua)\n");
  275. pua_set_publish();
  276. }
  277. save("location");
  278. exit;
  279. }
  280. NOTE: It's important to understand that the numbers in each group
  281. header ([number]) must start by 0. If not, the real group number will
  282. not match the number appearing in the file. For example, the following
  283. text file:
  284. Example 1.14. Incorrect groups file
  285. [1]
  286. ^aaa
  287. ^bbb
  288. [2]
  289. ^ccc
  290. ^ddd
  291. will generate the following regular expressions:
  292. group 0: ((^aaa)|(^bbb))
  293. group 1: ((^ccc)|(^ddd))
  294. Note that the real index doesn't match the group number in the file.
  295. This is, compiled group 0 always points to the first group in the file,
  296. regardless of its number in the file. In fact, the group number
  297. appearing in the file is used for nothing but for delimiting different
  298. groups.
  299. NOTE: A line containing a regular expression cannot start by '[' since
  300. it would be treated as a new group. The same for lines starting by
  301. space, tab, or '#' (they would be ignored by the parser). As a
  302. workaround, using brackets would work:
  303. [0]
  304. ([0-9]{9})
  305. ( #abcde)
  306. ( qwerty)