- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,837 @@ |
1 |
+app_java Module |
|
2 |
+ |
|
3 |
+Konstantin Mosesov |
|
4 |
+ |
|
5 |
+Edited by |
|
6 |
+ |
|
7 |
+Konstantin Mosesov |
|
8 |
+ |
|
9 |
+ <konstantinm@voipgroup.org.ua> |
|
10 |
+ |
|
11 |
+ Copyright © 2013 Konstantin Mosesov |
|
12 |
+ __________________________________________________________________ |
|
13 |
+ |
|
14 |
+ Table of Contents |
|
15 |
+ |
|
16 |
+ 1. Admin Guide |
|
17 |
+ |
|
18 |
+ 1. Overview |
|
19 |
+ 2. Dependencies |
|
20 |
+ |
|
21 |
+ 2.1. Kamailio Modules |
|
22 |
+ 2.2. External Libraries or Applications |
|
23 |
+ |
|
24 |
+ 3. Parameters |
|
25 |
+ |
|
26 |
+ 3.1. class_name (string) |
|
27 |
+ 3.2. child_init_method (string) |
|
28 |
+ 3.3. java_options (string) |
|
29 |
+ 3.4. force_cmd_exec (int) |
|
30 |
+ |
|
31 |
+ 4. Functions |
|
32 |
+ |
|
33 |
+ 4.0. Common requirements. |
|
34 |
+ 4.1. java_method_exec(method, method_signature, [param1[, param2]]) |
|
35 |
+ 4.2. java_s_method_exec(method, method_signature, [param1[, param2]]) |
|
36 |
+ 4.3. java_staticmethod_exec(method, method_signature, [param1[, param2]]) |
|
37 |
+ 4.4. java_s_staticmethod_exec(method, method_signature, [param1[, param2]]) |
|
38 |
+ |
|
39 |
+ 5. Java module API |
|
40 |
+ |
|
41 |
+ 5.0. Minimal program skeleton and structure of package org.siprouter |
|
42 |
+ 5.0.1. Minimal program skeleton |
|
43 |
+ 5.0.2. Structure of package org.siprouter |
|
44 |
+ 5.1. abstract class NativeMethods |
|
45 |
+ 5.1.1. General logging |
|
46 |
+ 5.1.1.0. Logging levels and facilities |
|
47 |
+ 5.1.1.1. native void LM_GEN1(int logLevel, String s); |
|
48 |
+ 5.1.1.2. native void LM_GEN2(int logFacility, int logLevel, String s); |
|
49 |
+ 5.1.1.3. native void LM_ALERT(String s); |
|
50 |
+ 5.1.1.4. native void LM_CRIT(String s); |
|
51 |
+ 5.1.1.5. native void LM_WARN(String s); |
|
52 |
+ 5.1.1.6. native void LM_NOTICE(String s); |
|
53 |
+ 5.1.1.7. native void LM_ERR(String s); |
|
54 |
+ 5.1.1.8. native void LM_INFO(String s); |
|
55 |
+ 5.1.1.9. native void LM_DBG(String s); |
|
56 |
+ 5.1.2. Execution of kamailio commands |
|
57 |
+ 5.1.2.1. static native int KamExec(String fname, String... params); |
|
58 |
+ 5.1.2.2. static native int SetURI(String ruri); |
|
59 |
+ 5.1.2.3. static native int RewriteURI(String ruri); |
|
60 |
+ |
|
61 |
+ 5.2. class IPPair. |
|
62 |
+ 5.2.1. final String ip; |
|
63 |
+ 5.2.2. final int port; |
|
64 |
+ 5.3. abstract class SipMsg |
|
65 |
+ 5.3.0. Internal structure understanding. |
|
66 |
+ 5.3.1. int id; |
|
67 |
+ 5.3.2. int pid; |
|
68 |
+ 5.3.3. String eoh; |
|
69 |
+ 5.3.4. String unparsed; |
|
70 |
+ 5.3.5. String buf; |
|
71 |
+ 5.3.6. int len; |
|
72 |
+ 5.3.7. String new_uri; |
|
73 |
+ 5.3.8. String dst_uri; |
|
74 |
+ 5.3.9. int parsed_uri_ok; |
|
75 |
+ 5.3.10. int parsed_orig_ruri_ok; |
|
76 |
+ 5.3.11. String add_to_branch_s; |
|
77 |
+ 5.3.12. int add_to_branch_len; |
|
78 |
+ 5.3.13. int hash_index; |
|
79 |
+ 5.3.14. int msg_flags; |
|
80 |
+ 5.3.15. static native SipMsg ParseSipMsg(); |
|
81 |
+ 5.3.16. static native String getMsgType(); |
|
82 |
+ 5.3.17. static native String getRURI(); |
|
83 |
+ 5.3.18. static native IPPair getSrcAddress(); |
|
84 |
+ 5.3.19. static native IPPair getDstAddress(); |
|
85 |
+ 5.3.20. static native String getBuffer(); |
|
86 |
+ 5.4. interface NativeInterface |
|
87 |
+ 5.4.1. abstract class Ranks |
|
88 |
+ 5.4.1.1. static final int PROC_MAIN |
|
89 |
+ 5.4.1.2. static final int PROC_TIMER |
|
90 |
+ 5.4.1.3. static final int PROC_RPC |
|
91 |
+ 5.4.1.4. static final int PROC_FIFO |
|
92 |
+ 5.4.1.5. static final int PROC_TCP_MAIN |
|
93 |
+ 5.4.1.6. static final int PROC_UNIXSOCK |
|
94 |
+ 5.4.1.7. static final int PROC_ATTENDANT |
|
95 |
+ 5.4.1.8. static final int PROC_INIT |
|
96 |
+ 5.4.1.9. static final int PROC_NOCHLDINIT |
|
97 |
+ 5.4.1.10. static final int PROC_SIPINIT |
|
98 |
+ 5.4.1.11. static final int PROC_SIPRPC |
|
99 |
+ 5.4.1.12. static final int PROC_MIN |
|
100 |
+ 5.4.2. abstract class LogParams |
|
101 |
+ 5.4.2.0. Loggigng params |
|
102 |
+ 5.4.2.1. static final int L_ALERT |
|
103 |
+ 5.4.2.2. static final int L_BUG |
|
104 |
+ 5.4.2.3. static final int L_CRIT2 |
|
105 |
+ 5.4.2.4. static final int L_CRIT |
|
106 |
+ 5.4.2.5. static final int L_ERR |
|
107 |
+ 5.4.2.6. static final int L_WARN |
|
108 |
+ 5.4.2.7. static final int L_NOTICE |
|
109 |
+ 5.4.2.8. static final int L_INFO |
|
110 |
+ 5.4.2.9. static final int L_DBG |
|
111 |
+ 5.4.2.10. static final int DEFAULT_FACILITY |
|
112 |
+ 5.5. Examples of usage Java API |
|
113 |
+ |
|
114 |
+ List of Examples |
|
115 |
+ |
|
116 |
+ 1.1. Set class_name parameter |
|
117 |
+ 1.2. Set child_init_method parameter |
|
118 |
+ 1.3. Set java_options parameter |
|
119 |
+ 1.4. Set java_options parameter (live configuration) |
|
120 |
+ 1.5. Set java_options parameter (verbose configuration) |
|
121 |
+ 1.6. Set java_options parameter (debug configuration) |
|
122 |
+ |
|
123 |
+ 2.0. Example of usage signatures. |
|
124 |
+ 2.1. Example of usage java_method_exec() |
|
125 |
+ 2.2. Example of usage java_s_method_exec() |
|
126 |
+ 2.3. Example of usage java_staticmethod_exec() |
|
127 |
+ 3.4. Example of usage java_s_staticmethod_exec() |
|
128 |
+ |
|
129 |
+Chapter 1. Admin Guide |
|
130 |
+ |
|
131 |
+ Table of Contents |
|
132 |
+ |
|
133 |
+ 1. Overview |
|
134 |
+ 2. Dependencies |
|
135 |
+ |
|
136 |
+ 2.1. Kamailio Modules |
|
137 |
+ 2.2. External Libraries or Applications |
|
138 |
+ |
|
139 |
+ 3. Parameters |
|
140 |
+ |
|
141 |
+ 3.1. class_name (string) |
|
142 |
+ 3.2. child_init_method (string) |
|
143 |
+ 3.3. java_options (string) |
|
144 |
+ 3.4. force_cmd_exec (int) |
|
145 |
+ |
|
146 |
+ 4. Functions |
|
147 |
+ |
|
148 |
+ 4.0. Common requirements. |
|
149 |
+ 4.1. java_method_exec(method, method_signature, [param1[, param2]]) - class method |
|
150 |
+ 4.2. java_s_method_exec(method, method_signature, [param1[, param2]]) - class synchronized method |
|
151 |
+ 4.3. java_staticmethod_exec(method, method_signature, [param1[, param2]]) - static method |
|
152 |
+ 4.4. java_s_staticmethod_exec(method, method_signature, [param1[, param2]]) - static synchronized method |
|
153 |
+ |
|
154 |
+1. Overview |
|
155 |
+ |
|
156 |
+ This module allows executing Java compiled classes from config file, exporting |
|
157 |
+ functions to access the SIP message from Java using Java Native Interface (JNI). |
|
158 |
+ |
|
159 |
+2. Dependencies |
|
160 |
+ |
|
161 |
+ 2.1. Kamailio Modules |
|
162 |
+ 2.2. External Libraries or Applications |
|
163 |
+ 2.3. Java runtime |
|
164 |
+ |
|
165 |
+2.1. Kamailio Modules |
|
166 |
+ |
|
167 |
+ The following modules must be loaded before this module: |
|
168 |
+ * none. |
|
169 |
+ |
|
170 |
+2.2. External Libraries or Applications |
|
171 |
+ |
|
172 |
+ Legend: |
|
173 |
+ * - Runtime library, required to launch |
|
174 |
+ R - Required |
|
175 |
+ O - Optional |
|
176 |
+ D - Development (for building-rebuilding java) |
|
177 |
+ P - Dependance of package |
|
178 |
+ |
|
179 |
+ The following libraries or applications must be installed before |
|
180 |
+ running Kamailio with this module loaded: |
|
181 |
+ [*] java-common - Base of all Java packages |
|
182 |
+ [*] default-jre - Standard Java or Java compatible Runtime |
|
183 |
+ [*] gcj-jre - Java runtime environment using GIJ/classpath |
|
184 |
+ [*] libgcj12 (>=12) - Java runtime library for use with gcj |
|
185 |
+ [DR] ant - Java based build tool like make |
|
186 |
+ [DO] ant-contrib - collection of tasks, types and other tools for Apache Ant |
|
187 |
+ [DPO] ant-gcj - Java based build tool like make (GCJ) |
|
188 |
+ [DPO] ant-optional - Java based build tool like make - optional libraries |
|
189 |
+ [DPO] ant-optional-gcj - Java based build tool like make - optional libraries (GCJ) |
|
190 |
+ [DR] default-jdk - Standard Java or Java compatible Development Kit |
|
191 |
+ [DR] gcj-jdk - gcj and classpath development tools for Java(TM) |
|
192 |
+ [DR] libgcj13-dev (>=12) - Java development headers for use with gcj |
|
193 |
+ [DO] jdk - JDK Development Kit (either oracle jdk or openjdk) |
|
194 |
+ |
|
195 |
+ The following libraries or applications must be compiled before |
|
196 |
+ running Kamailio with this module loaded: |
|
197 |
+ [*] <class_name>.class |
|
198 |
+ [*] kamailio.jar |
|
199 |
+ |
|
200 |
+2.3. Java runtime |
|
201 |
+ |
|
202 |
+ Java runtime library (JRE or JDK) is required to use this module. |
|
203 |
+ |
|
204 |
+3. Parameters |
|
205 |
+ |
|
206 |
+ 3.1. class_name (string) |
|
207 |
+ 3.2. child_init_method (string) |
|
208 |
+ 3.3. java_options (string) |
|
209 |
+ 3.4. force_cmd_exec (int) |
|
210 |
+ |
|
211 |
+3.1. class_name (string) |
|
212 |
+ |
|
213 |
+ The class name should have the same compiled file name. |
|
214 |
+ If the value is "Kamailio", then the compiled file should be named as "Kamailio.class". |
|
215 |
+ |
|
216 |
+ Default value is “Kamailio”. |
|
217 |
+ |
|
218 |
+ Example 1.1. Set class_name parameter |
|
219 |
+... |
|
220 |
+modparam("app_java", "class_name", "Kamailio") |
|
221 |
+... |
|
222 |
+ |
|
223 |
+3.2. child_init_method (string) |
|
224 |
+ |
|
225 |
+ TBD. |
|
226 |
+ |
|
227 |
+ Default value is “child_init”. |
|
228 |
+ |
|
229 |
+ Example 1.2. Set child_init_method parameter |
|
230 |
+... |
|
231 |
+modparam("app_java", "child_init_method", "my_mod_init") |
|
232 |
+... |
|
233 |
+ |
|
234 |
+3.3. java_options (string) |
|
235 |
+ |
|
236 |
+ Java options for Java Virtual Machine. |
|
237 |
+ For more info see: http://docs.oracle.com/javase/6/docs/technotes/tools/windows/java.html |
|
238 |
+ |
|
239 |
+ Default value is “-Djava.compiler=NONE”. |
|
240 |
+ |
|
241 |
+ Example 1.3. Set java_options parameter |
|
242 |
+... |
|
243 |
+modparam("app_java", "java_options", "-Djava.compiler=NONE") |
|
244 |
+... |
|
245 |
+ |
|
246 |
+ Example 1.4. Set java_options parameter (live configuration) |
|
247 |
+... |
|
248 |
+modparam("app_java", "java_options", "-Djava.compiler=NONE -Djava.class.path=/path/to/kamailio/modules:/path/to/<class_name>_file_directory:/path/to/kamailio.jar") |
|
249 |
+... |
|
250 |
+ |
|
251 |
+ Example 1.5. Set java_options parameter (verbose configuration) |
|
252 |
+... |
|
253 |
+modparam("app_java", "java_options", "-verbose:gc,class,jni -Djava.compiler=NONE -Djava.class.path=/path/to/kamailio/modules:/path/to/class_name_file_directory:/path/to/kamailio.jar") |
|
254 |
+... |
|
255 |
+ |
|
256 |
+ Example 1.6. Set java_options parameter (debug configuration) |
|
257 |
+... |
|
258 |
+modparam("app_java", "java_options", "-Xdebug -verbose:gc,class,jni -Djava.compiler=NONE -Djava.class.path=/path/to/kamailio/modules:/path/to/class_name_file_directory:/path/to/kamailio.jar") |
|
259 |
+... |
|
260 |
+ |
|
261 |
+3.4. force_cmd_exec (int) |
|
262 |
+ |
|
263 |
+ This parameter forces execution a kamailio comnmand with java native method KamExec_raw. |
|
264 |
+ Note: may cause a memory leaks if used from embedded languages. |
|
265 |
+ |
|
266 |
+ Default value is 0 (off). |
|
267 |
+ |
|
268 |
+4. Functions |
|
269 |
+ |
|
270 |
+ 4.0. Common requirements. |
|
271 |
+ 4.1. java_method_exec(method, method_signature, [param1[, param2]]) |
|
272 |
+ 4.2. java_s_method_exec(method, method_signature, [param1[, param2]]) |
|
273 |
+ 4.3. java_staticmethod_exec(method, method_signature, [param1[, param2]]) |
|
274 |
+ 4.4. java_s_staticmethod_exec(method, method_signature, [param1[, param2]]) |
|
275 |
+ |
|
276 |
+4.0. Common requirements. |
|
277 |
+ |
|
278 |
+ Each function has a required parameter “method_signature”. |
|
279 |
+ For more info see: http://www.rgagnon.com/javadetails/java-0286.html |
|
280 |
+ There are two parts to the signature. The first part is enclosed within the parentheses and represents the method's arguments. |
|
281 |
+ The second portion follows the closing parenthesis and represents the return type. The mapping between the Java type and C type is |
|
282 |
+ Type Chararacter |
|
283 |
+ boolean Z |
|
284 |
+ byte B |
|
285 |
+ char C |
|
286 |
+ double D |
|
287 |
+ float F |
|
288 |
+ int I |
|
289 |
+ long J |
|
290 |
+ object L |
|
291 |
+ short S |
|
292 |
+ void V |
|
293 |
+ array [ |
|
294 |
+ Note that to specify an object, the "L" is followed by the object's class name and ends with a semi-colon, ';' . |
|
295 |
+ |
|
296 |
+ app_java supports the following signatures |
|
297 |
+ primitives: Z,B,C,D,F,I,J,L,S,V |
|
298 |
+ objects: |
|
299 |
+ Ljava/lang/Boolean; |
|
300 |
+ Ljava/lang/Byte; |
|
301 |
+ Ljava/lang/Character; |
|
302 |
+ Ljava/lang/Double; |
|
303 |
+ Ljava/lang/Float; |
|
304 |
+ Ljava/lang/Integer; |
|
305 |
+ Ljava/lang/Long; |
|
306 |
+ Ljava/lang/Short; |
|
307 |
+ Ljava/lang/String; |
|
308 |
+ NULL parameter: V |
|
309 |
+ |
|
310 |
+ Each parameter passed to function will be cast according to given signature. |
|
311 |
+ |
|
312 |
+ Example 2.0. Example of usage signatures. |
|
313 |
+ 0. Equivalent of java prototype: public int ExampleMethod(); |
|
314 |
+... |
|
315 |
+java_method_exec("ExampleMethod", "V"); |
|
316 |
+... |
|
317 |
+ |
|
318 |
+ 1. Equivalent of java prototype: public int ExampleMethod(String param1, int param2); |
|
319 |
+... |
|
320 |
+java_method_exec("ExampleMethod", "Ljava/lang/String;I", "Hello world", "5"); |
|
321 |
+... |
|
322 |
+ In the above scenario parameter 2 ("5") will be cast to integer representation. |
|
323 |
+ |
|
324 |
+ 2. Equivalent of java prototype: public int ExampleMethod(boolean param1, byte param2); |
|
325 |
+... |
|
326 |
+java_method_exec("ExampleMethod", "ZB", "true", "0x05"); |
|
327 |
+... |
|
328 |
+ In the above scenario parameter 1 ("true") will be cast to boolean representation. |
|
329 |
+ |
|
330 |
+ Parameters are optional, ommitting a parameter meant the passed value is NULL. |
|
331 |
+ Parameters count should be exactly the same as signature count. |
|
332 |
+ Note, you shall use a correct signature, e.g. the following examples of combinations are invalid: |
|
333 |
+ *) java_method_exec("ExampleMethod", "ZI", "False"); |
|
334 |
+ *) java_method_exec("ExampleMethod", "VI", "", "5"); |
|
335 |
+ *) java_method_exec("ExampleMethod", "LI", "something", "5"); |
|
336 |
+ |
|
337 |
+ |
|
338 |
+4.1. java_method_exec(method, method_signature, [param1[, param2]]) |
|
339 |
+ |
|
340 |
+ Executes a java method “method”. Parameter “method_signature” is required (see 4.0). |
|
341 |
+ |
|
342 |
+ Example 2.1. java_method_exec usage |
|
343 |
+... |
|
344 |
+# Equivalent of java prototype: public int ExampleMethod(); |
|
345 |
+java_method_exec("ExampleMethod", "V"); |
|
346 |
+ |
|
347 |
+# Equivalent of java prototype: public int ExampleMethod(String SipMessageBuffer, int SipMessageLenght); |
|
348 |
+java_method_exec("ExampleMethod", "Ljava/lang/String;I", "$mb", "$ml"); |
|
349 |
+... |
|
350 |
+ |
|
351 |
+ |
|
352 |
+4.2. java_s_method_exec(method, method_signature, [param1[, param2]]) |
|
353 |
+ |
|
354 |
+ Executes a java synchronized method “method”. Parameter “method_signature” is required (see 4.0). |
|
355 |
+ See a more info about to synchronization: http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html |
|
356 |
+ |
|
357 |
+ Example 2.2. java_s_method_exec usage |
|
358 |
+... |
|
359 |
+# Equivalent of java prototype: public synchronized int ExampleMethod(); |
|
360 |
+java_s_method_exec("ExampleMethod", "V"); |
|
361 |
+ |
|
362 |
+# Equivalent of java prototype: public synchronized int ExampleMethod(String SipMessageBuffer, int SipMessageLenght); |
|
363 |
+java_s_method_exec("ExampleMethod", "Ljava/lang/String;I", "$mb", "$ml"); |
|
364 |
+... |
|
365 |
+ |
|
366 |
+ |
|
367 |
+4.3. java_staticmethod_exec(method, method_signature, [param1[, param2]]) |
|
368 |
+ |
|
369 |
+ Executes a java static method “method”. Parameter “method_signature” is required (see 4.0). |
|
370 |
+ |
|
371 |
+ Example 2.3. java_staticmethod_exec usage |
|
372 |
+... |
|
373 |
+# Equivalent of java prototype: public static int ExampleMethod(); |
|
374 |
+java_staticmethod_exec("ExampleMethod", "V"); |
|
375 |
+ |
|
376 |
+# Equivalent of java prototype: public static int ExampleMethod(String SipMessageBuffer, int SipMessageLenght); |
|
377 |
+java_staticmethod_exec("ExampleMethod", "Ljava/lang/String;I", "$mb", "$ml"); |
|
378 |
+... |
|
379 |
+ |
|
380 |
+ |
|
381 |
+4.4. java_s_staticmethod_exec(method, method_signature, [param1[, param2]]) |
|
382 |
+ |
|
383 |
+ Executes a java synchronized static method “method”. Parameter “method_signature” is required (see 4.0). |
|
384 |
+ See a more info about to synchronization: http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html |
|
385 |
+ |
|
386 |
+ Example 2.4. java_s_staticmethod_exec usage |
|
387 |
+... |
|
388 |
+# Equivalent of java prototype: public static synchronized int ExampleMethod(); |
|
389 |
+java_s_staticmethod_exec("ExampleMethod", "V"); |
|
390 |
+ |
|
391 |
+# Equivalent of java prototype: public static synchronized int ExampleMethod(String SipMessageBuffer, int SipMessageLenght); |
|
392 |
+java_s_staticmethod_exec("ExampleMethod", "Ljava/lang/String;I", "$mb", "$ml"); |
|
393 |
+... |
|
394 |
+ |
|
395 |
+ |
|
396 |
+5. Java module API |
|
397 |
+ |
|
398 |
+ |
|
399 |
+5.0. Minimal program skeleton and structure of package org.siprouter |
|
400 |
+ |
|
401 |
+ 5.0.1. Minimal program skeleton |
|
402 |
+ |
|
403 |
+... |
|
404 |
+import org.siprouter.*; |
|
405 |
+import org.siprouter.NativeInterface.*; |
|
406 |
+ |
|
407 |
+public class Kamailio extends NativeMethods |
|
408 |
+{ |
|
409 |
+ /* Here you should specify a full path to app_java.so */ |
|
410 |
+ static |
|
411 |
+ { |
|
412 |
+ System.load("/opt/kamailio/lib/kamailio/modules/app_java.so"); |
|
413 |
+ } |
|
414 |
+ |
|
415 |
+ /* Constructor. Do not remove !!! */ |
|
416 |
+ public Kamailio() |
|
417 |
+ { |
|
418 |
+ } |
|
419 |
+ |
|
420 |
+ /* |
|
421 |
+ This method should be executed for each children process, immediately after forking. |
|
422 |
+ Required. Do not remove !!! |
|
423 |
+ */ |
|
424 |
+ public int child_init(int rank) |
|
425 |
+ { |
|
426 |
+ return 1; |
|
427 |
+ } |
|
428 |
+} |
|
429 |
+... |
|
430 |
+ |
|
431 |
+ 5.0.2. Structure of package org.siprouter |
|
432 |
+ |
|
433 |
+ org.siprouter ---*---*--*-------> class NativeMethods |
|
434 |
+ | | +--> class IPPair |
|
435 |
+ | +--> class SipMsg |
|
436 |
+ +--> interface NativeInterface |
|
437 |
+ | |
|
438 |
+ +--> class Ranks |
|
439 |
+ +--> class LogParams |
|
440 |
+ |
|
441 |
+ |
|
442 |
+5.1. abstract class NativeMethods |
|
443 |
+ |
|
444 |
+ 5.1.1. General logging |
|
445 |
+ |
|
446 |
+ 5.1.1.0. Log levels and facilities |
|
447 |
+ |
|
448 |
+ Log levels: |
|
449 |
+ L_ALERT = -5 |
|
450 |
+ L_BUG = -4 |
|
451 |
+ L_CRIT2 = -3 |
|
452 |
+ L_CRIT = -2 |
|
453 |
+ L_ERR = -1 |
|
454 |
+ L_WARN = 0 |
|
455 |
+ L_NOTICE = 1 |
|
456 |
+ L_INFO = 2 |
|
457 |
+ L_DBG = 3 |
|
458 |
+ |
|
459 |
+ Log facilities (see man syslog(3)): |
|
460 |
+ DEFAULT_FACILITY = 0 (LOG_KERN (Linux)) |
|
461 |
+ |
|
462 |
+ 1. Example of usage log levels and facilities: |
|
463 |
+ LM_GEN1(LogParams.L_BUG, "Hello World!\n"); |
|
464 |
+ produces (example of output): |
|
465 |
+ 0(3003) BUG: app_java [java_native_methods.c:255]: Hello World! |
|
466 |
+ |
|
467 |
+ 2. Example of usage log levels and facilities: |
|
468 |
+ LM_GEN2(LogParams.DEFAULT_FACILITY, LogParams.L_WARN, "Hello World!\n"); |
|
469 |
+ produces (example of output): |
|
470 |
+ 2(3147) WARNING: app_java [java_native_methods.c:279]: Hello World! |
|
471 |
+ |
|
472 |
+ |
|
473 |
+ 5.1.1.1. native void LM_GEN1(int logLevel, String s); |
|
474 |
+ 5.1.1.2. native void LM_GEN2(int logFacility, int logLevel, String s); |
|
475 |
+ 5.1.1.3. native void LM_ALERT(String s); |
|
476 |
+ 5.1.1.4. native void LM_CRIT(String s); |
|
477 |
+ 5.1.1.5. native void LM_WARN(String s); |
|
478 |
+ 5.1.1.6. native void LM_NOTICE(String s); |
|
479 |
+ 5.1.1.7. native void LM_ERR(String s); |
|
480 |
+ 5.1.1.8. native void LM_INFO(String s); |
|
481 |
+ 5.1.1.9. native void LM_DBG(String s); |
|
482 |
+ |
|
483 |
+ 5.1.2. Execution of kamailio commands |
|
484 |
+ |
|
485 |
+ 5.1.2.1. static native int KamExec(String fname, String... params); |
|
486 |
+ |
|
487 |
+ Executes a kamailio command. |
|
488 |
+ |
|
489 |
+ Parameter 'fname' - Required. Kamailio function name. |
|
490 |
+ Parameter 'params' - An array of string parameters. Note, this method allows up to 6 params, |
|
491 |
+ it will ignore all parameters if more than 6. |
|
492 |
+ |
|
493 |
+ Returns: |
|
494 |
+ 1 - command was successfully executed. |
|
495 |
+ -1 - execution of command was failed. |
|
496 |
+ |
|
497 |
+ 5.1.2.2. static native int SetURI(String ruri); |
|
498 |
+ |
|
499 |
+ Rewrites the request URI. |
|
500 |
+ |
|
501 |
+ Returns: |
|
502 |
+ 1 - Ok |
|
503 |
+ -1 - Failed |
|
504 |
+ |
|
505 |
+ Online reference: http://www.kamailio.org/wiki/cookbooks/3.3.x/core#rewriteuri |
|
506 |
+ |
|
507 |
+ 5.1.2.3. static native int RewriteURI(String ruri); |
|
508 |
+ |
|
509 |
+ Rewrites the request URI. |
|
510 |
+ Alias method: SetURI |
|
511 |
+ |
|
512 |
+ Returns: |
|
513 |
+ 1 - Ok |
|
514 |
+ -1 - Failed |
|
515 |
+ |
|
516 |
+ Online reference: http://www.kamailio.org/wiki/cookbooks/3.3.x/core#rewriteuri |
|
517 |
+ |
|
518 |
+ |
|
519 |
+ |
|
520 |
+5.2. class IPPair. |
|
521 |
+ |
|
522 |
+ Represents an IP-address pair (IP, Port) |
|
523 |
+ |
|
524 |
+ 5.2.1. final String ip; |
|
525 |
+ |
|
526 |
+ IP Address |
|
527 |
+ |
|
528 |
+ 5.2.2. final int port; |
|
529 |
+ |
|
530 |
+ Port |
|
531 |
+ |
|
532 |
+ |
|
533 |
+5.3. abstract class SipMsg |
|
534 |
+ |
|
535 |
+ 5.3.0. Internal structure understanding. |
|
536 |
+ |
|
537 |
+ The class SipMsg is partially incapsulating kamailio's struct sip_msg. |
|
538 |
+ References: parser/msg_parser.h, parser/parse_fline.h |
|
539 |
+ |
|
540 |
+ 5.3.1. int id; |
|
541 |
+ |
|
542 |
+ Message id, unique/process |
|
543 |
+ |
|
544 |
+ 5.3.2. int pid; |
|
545 |
+ |
|
546 |
+ Process ID |
|
547 |
+ |
|
548 |
+ 5.3.3. String eoh; |
|
549 |
+ |
|
550 |
+ Pointer to the end of header (if found) or null |
|
551 |
+ |
|
552 |
+ 5.3.4. String unparsed; |
|
553 |
+ |
|
554 |
+ Here we stopped parsing |
|
555 |
+ |
|
556 |
+ 5.3.5. String buf; |
|
557 |
+ |
|
558 |
+ Scratch pad, holds a modified message, via, etc. point into it. |
|
559 |
+ |
|
560 |
+ 5.3.6. int len; |
|
561 |
+ |
|
562 |
+ Message len (orig) |
|
563 |
+ |
|
564 |
+ 5.3.7. String new_uri; |
|
565 |
+ |
|
566 |
+ Changed first line uri, when you change this |
|
567 |
+ |
|
568 |
+ 5.3.8. String dst_uri; |
|
569 |
+ |
|
570 |
+ Destination URI, must be forwarded to this URI if dst_url lenght != 0 |
|
571 |
+ |
|
572 |
+ 5.3.9. int parsed_uri_ok; |
|
573 |
+ |
|
574 |
+ 1 if parsed_orig_uri is valid, 0 if not, set if to 0 if you modify the uri (e.g change new_uri) |
|
575 |
+ |
|
576 |
+ 5.3.10. int parsed_orig_ruri_ok; |
|
577 |
+ |
|
578 |
+ 1 if parsed_orig_uri is valid, 0 if not, set if to 0 if you modify the uri (e.g change new_uri) |
|
579 |
+ |
|
580 |
+ 5.3.11. String add_to_branch_s; |
|
581 |
+ |
|
582 |
+ Whatever whoever want to append to branch comes here |
|
583 |
+ |
|
584 |
+ 5.3.12. int add_to_branch_len; |
|
585 |
+ |
|
586 |
+ Lenght of add_to_branch_s |
|
587 |
+ |
|
588 |
+ 5.3.13. int hash_index; |
|
589 |
+ |
|
590 |
+ Index to TM hash table; stored in core to avoid unnecessary calculations |
|
591 |
+ |
|
592 |
+ 5.3.14. int msg_flags; |
|
593 |
+ |
|
594 |
+ Flags used by core. Allows to set various flags on the message; may be used |
|
595 |
+ for simple inter-module communication or remembering processing state reache. |
|
596 |
+ |
|
597 |
+ 5.3.15. static native SipMsg ParseSipMsg(); |
|
598 |
+ |
|
599 |
+ This method is using to get an instance of class SipMsg and populate a properties |
|
600 |
+ of this class with a fields specified at 5.3.1 - 5.3.14. |
|
601 |
+ |
|
602 |
+ 5.3.16. static native String getMsgType(); |
|
603 |
+ |
|
604 |
+ Gets a message type. Return value: |
|
605 |
+ 'SIP_REQUEST' - if message is request |
|
606 |
+ 'SIP_REPLY' - if message is reply |
|
607 |
+ 'SIP_INVALID' - if invalid message |
|
608 |
+ |
|
609 |
+ 5.3.17. static native String getRURI(); |
|
610 |
+ |
|
611 |
+ Gets a request URI (RURI). |
|
612 |
+ |
|
613 |
+ 5.3.18. static native IPPair getSrcAddress(); |
|
614 |
+ |
|
615 |
+ Gets a source IP address and port. |
|
616 |
+ |
|
617 |
+ 5.3.19. static native IPPair getDstAddress(); |
|
618 |
+ |
|
619 |
+ Gets a destination IP address and port. |
|
620 |
+ |
|
621 |
+ 5.3.20. static native String getBuffer(); |
|
622 |
+ |
|
623 |
+ Gets a message buffer. |
|
624 |
+ |
|
625 |
+ |
|
626 |
+5.4. interface NativeInterface |
|
627 |
+ |
|
628 |
+ 5.4.1. abstract class Ranks |
|
629 |
+ |
|
630 |
+ 5.4.1.1. static final int PROC_MAIN |
|
631 |
+ |
|
632 |
+ Main ser process |
|
633 |
+ |
|
634 |
+ 5.4.1.2. static final int PROC_TIMER |
|
635 |
+ |
|
636 |
+ Timer attendant process |
|
637 |
+ |
|
638 |
+ 5.4.1.3. static final int PROC_RPC |
|
639 |
+ |
|
640 |
+ RPC type process |
|
641 |
+ |
|
642 |
+ 5.4.1.4. static final int PROC_FIFO |
|
643 |
+ |
|
644 |
+ FIFO attendant process. |
|
645 |
+ Alias to PROC_RPC. |
|
646 |
+ |
|
647 |
+ 5.4.1.5. static final int PROC_TCP_MAIN |
|
648 |
+ |
|
649 |
+ TCP main process |
|
650 |
+ |
|
651 |
+ 5.4.1.6. static final int PROC_UNIXSOCK |
|
652 |
+ |
|
653 |
+ Unix socket server |
|
654 |
+ |
|
655 |
+ 5.4.1.7. static final int PROC_ATTENDANT |
|
656 |
+ |
|
657 |
+ Main "attendant process |
|
658 |
+ |
|
659 |
+ 5.4.1.8. static final int PROC_INIT |
|
660 |
+ |
|
661 |
+ Special rank, the context is the main ser process, but this is |
|
662 |
+ guaranteed to be executed before any rocess is forked, so it |
|
663 |
+ can be used to setup shared variables that depend on some |
|
664 |
+ after mod_init available information (e.g. total number of processes). |
|
665 |
+ @warning child_init(PROC_MAIN) is again called in the same process (main) |
|
666 |
+ (before tcp), so make sure you don't init things twice, |
|
667 |
+ both in PROC_MAIN and PROC_INT |
|
668 |
+ |
|
669 |
+ 5.4.1.9. static final int PROC_NOCHLDINIT |
|
670 |
+ |
|
671 |
+ no child init functions will be called if this rank is used in fork_process() |
|
672 |
+ |
|
673 |
+ 5.4.1.10. static final int PROC_SIPINIT |
|
674 |
+ |
|
675 |
+ First SIP worker - some modules do special processing in this child, |
|
676 |
+ like loading db data |
|
677 |
+ |
|
678 |
+ 5.4.1.11. static final int PROC_SIPRPC |
|
679 |
+ |
|
680 |
+ Used to init RPC worker as SIP commands handler. |
|
681 |
+ Don't do any special processing in the child init with this rank - |
|
682 |
+ just bare child initialization |
|
683 |
+ |
|
684 |
+ 5.4.1.12. static final int PROC_MIN |
|
685 |
+ |
|
686 |
+ Minimum process rank. |
|
687 |
+ Alias to PROC_NOCHLDINIT. |
|
688 |
+ |
|
689 |
+ 5.4.2. abstract class LogParams |
|
690 |
+ 5.4.2.0. Loggigng params |
|
691 |
+ 5.4.2.1. static final int L_ALERT |
|
692 |
+ 5.4.2.2. static final int L_BUG |
|
693 |
+ 5.4.2.3. static final int L_CRIT2 |
|
694 |
+ 5.4.2.4. static final int L_CRIT |
|
695 |
+ 5.4.2.5. static final int L_ERR |
|
696 |
+ 5.4.2.6. static final int L_WARN |
|
697 |
+ 5.4.2.7. static final int L_NOTICE |
|
698 |
+ 5.4.2.8. static final int L_INFO |
|
699 |
+ 5.4.2.9. static final int L_DBG |
|
700 |
+ 5.4.2.10. static final int DEFAULT_FACILITY |
|
701 |
+ |
|
702 |
+ |
|
703 |
+5.5. Examples of usage Java API |
|
704 |
+ |
|
705 |
+... |
|
706 |
+ |
|
707 |
+import java.lang.*; |
|
708 |
+import java.io.*; |
|
709 |
+ |
|
710 |
+import org.siprouter.*; |
|
711 |
+import org.siprouter.NativeInterface.*; |
|
712 |
+ |
|
713 |
+public class Kamailio extends NativeMethods |
|
714 |
+{ |
|
715 |
+ static |
|
716 |
+ { |
|
717 |
+ System.load("/opt/kamailio/lib/kamailio/modules/app_java.so"); |
|
718 |
+ } |
|
719 |
+ |
|
720 |
+ /* Constructor. Do not remove !!! */ |
|
721 |
+ public Kamailio() |
|
722 |
+ { |
|
723 |
+ } |
|
724 |
+ |
|
725 |
+ |
|
726 |
+ public int child_init(int rank) |
|
727 |
+ { |
|
728 |
+ switch (rank) |
|
729 |
+ { |
|
730 |
+ case Ranks.PROC_MAIN: |
|
731 |
+ LM_INFO("We're at PROC_MAIN\n"); |
|
732 |
+ break; |
|
733 |
+ case Ranks.PROC_TIMER: |
|
734 |
+ LM_INFO("We're at PROC_TIMER\n"); |
|
735 |
+ break; |
|
736 |
+ case Ranks.PROC_RPC: |
|
737 |
+ LM_INFO("We're at PROC_RPC/PROC_FIFO\n"); |
|
738 |
+ break; |
|
739 |
+ case Ranks.PROC_TCP_MAIN: |
|
740 |
+ LM_INFO("We're at PROC_TCP_MAIN\n"); |
|
741 |
+ break; |
|
742 |
+ case Ranks.PROC_UNIXSOCK: |
|
743 |
+ LM_INFO("We're at PROC_UNIXSOCK\n"); |
|
744 |
+ break; |
|
745 |
+ case Ranks.PROC_ATTENDANT: |
|
746 |
+ LM_INFO("We're at PROC_ATTENDANT\n"); |
|
747 |
+ break; |
|
748 |
+ case Ranks.PROC_INIT: |
|
749 |
+ LM_INFO("We're at PROC_INIT\n"); |
|
750 |
+ break; |
|
751 |
+ case Ranks.PROC_NOCHLDINIT: |
|
752 |
+ LM_INFO("We're at PROC_NOCHLDINIT/PROC_MIN\n"); |
|
753 |
+ break; |
|
754 |
+ case Ranks.PROC_SIPINIT: |
|
755 |
+ LM_INFO("We're at PROC_SIPINIT\n"); |
|
756 |
+ break; |
|
757 |
+ case Ranks.PROC_SIPRPC: |
|
758 |
+ LM_INFO("We're at PROC_SIPRPC\n"); |
|
759 |
+ break; |
|
760 |
+ } |
|
761 |
+ |
|
762 |
+ return 1; |
|
763 |
+ } |
|
764 |
+ |
|
765 |
+ public int TestMethod() |
|
766 |
+ { |
|
767 |
+ |
|
768 |
+ LM_INFO(String.format("Msg Type: %s\n", SipMsg.getMsgType())); |
|
769 |
+ |
|
770 |
+ IPPair src = SipMsg.getSrcAddress(); |
|
771 |
+ if (src != null) |
|
772 |
+ { |
|
773 |
+ LM_INFO(String.format("src address=%s, src port=%d\n", src.ip, src.port)); |
|
774 |
+ } |
|
775 |
+ else |
|
776 |
+ { |
|
777 |
+ LM_ERR("IPPair src is null!"); |
|
778 |
+ } |
|
779 |
+ |
|
780 |
+ IPPair dst = SipMsg.getDstAddress(); |
|
781 |
+ if (dst != null) |
|
782 |
+ { |
|
783 |
+ LM_INFO(String.format("dst address=%s, dst port=%d\n", dst.ip, dst.port)); |
|
784 |
+ } |
|
785 |
+ else |
|
786 |
+ { |
|
787 |
+ LM_ERR("IPPair dst is null!"); |
|
788 |
+ } |
|
789 |
+ |
|
790 |
+ LM_INFO(String.format("buffer:\n%s\n", SipMsg.getBuffer().trim())); |
|
791 |
+ |
|
792 |
+ SipMsg msg = SipMsg.ParseSipMsg(); |
|
793 |
+ if (msg != null) |
|
794 |
+ { |
|
795 |
+ LM_INFO("msg:\n"); |
|
796 |
+ LM_INFO(String.format("\tid=%d\n", msg.id)); |
|
797 |
+ LM_INFO(String.format("\tpid=%d\n", msg.pid)); |
|
798 |
+ LM_INFO(String.format("\teoh='%s'\n", msg.eoh)); |
|
799 |
+ LM_INFO(String.format("\tunparsed='%s'\n", msg.unparsed)); |
|
800 |
+ LM_INFO(String.format("\tbuf='%s'\n", msg.buf)); |
|
801 |
+ LM_INFO(String.format("\tlen=%d\n", msg.len)); |
|
802 |
+ LM_INFO(String.format("\tnew_uri='%s'\n", msg.new_uri)); |
|
803 |
+ LM_INFO(String.format("\tdst_uri='%s'\n", msg.dst_uri)); |
|
804 |
+ LM_INFO(String.format("\tparsed_uri_ok=%d\n", msg.parsed_uri_ok)); |
|
805 |
+ LM_INFO(String.format("\tparsed_orig_ruri_ok=%d\n", msg.parsed_orig_ruri_ok)); |
|
806 |
+ LM_INFO(String.format("\tadd_to_branch_s='%s'\n", msg.add_to_branch_s)); |
|
807 |
+ LM_INFO(String.format("\tadd_to_branch_len=%d\n", msg.add_to_branch_len)); |
|
808 |
+ LM_INFO(String.format("\thash_index=%d\n", msg.hash_index)); |
|
809 |
+ LM_INFO(String.format("\tmsg_flags=%d\n", msg.msg_flags)); |
|
810 |
+ LM_INFO(String.format("\tset_global_address='%s'\n", msg.set_global_address)); |
|
811 |
+ LM_INFO(String.format("\tset_global_port='%s'\n", msg.set_global_port)); |
|
812 |
+ } |
|
813 |
+ else |
|
814 |
+ { |
|
815 |
+ LM_ERR("SipMsg msg is null!\n"); |
|
816 |
+ } |
|
817 |
+ |
|
818 |
+ return 1; |
|
819 |
+ } |
|
820 |
+} |
|
821 |
+ |
|
822 |
+... |
|
823 |
+ |
|
824 |
+ |
|
825 |
+ |
|
826 |
+ |
|
827 |
+ |
|
828 |
+ |
|
829 |
+ |
|
830 |
+ |
|
831 |
+ |
|
832 |
+ |
|
833 |
+ |
|
834 |
+ |
|
835 |
+ |
|
836 |
+ |
|
837 |
+ |