File: Kamailio.java

package info (click to toggle)
kamailio 4.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 56,100 kB
  • sloc: ansic: 552,832; xml: 166,484; sh: 8,659; makefile: 7,676; sql: 6,235; perl: 3,487; yacc: 3,428; python: 1,457; cpp: 1,219; php: 1,047; java: 449; pascal: 194; cs: 40; awk: 27
file content (388 lines) | stat: -rw-r--r-- 8,545 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388

import java.lang.*;
import java.io.*; 

import org.siprouter.*;
import org.siprouter.NativeInterface.*;

//import org.siprouter.CoreMethods.*;

public class Kamailio extends NativeMethods
{
	static
	{
	    System.load("/opt/kamailio/lib/kamailio/modules/app_java.so");
	}

	/* Constructor. Do not remove !!! */
	public Kamailio()
	{
	}


	public int child_init(int rank)
	{
	    return 1;
	}

	public int TestMethod()
	{

	    int retval = 0;
	    boolean boolstate = false;

/*
	    LM_INFO(String.format("Msg Type: %s\n", SipMsg.getMsgType()));

	    IPPair src = SipMsg.getSrcAddress();
	    if (src != null)
	    {
		LM_INFO(String.format("src address=%s, src port=%d\n", src.ip, src.port));
	    }
	    else
	    {
		LM_ERR("IPPair src is null!");
	    }

	    IPPair dst = SipMsg.getDstAddress();
	    if (dst != null)
	    {
		LM_INFO(String.format("dst address=%s, dst port=%d\n", dst.ip, dst.port));
	    }
	    else
	    {
		LM_ERR("IPPair dst is null!");
	    }

	    LM_INFO(String.format("buffer:\n%s\n", SipMsg.getBuffer().trim()));

	    SipMsg msg = SipMsg.ParseSipMsg();
	    if (msg != null)
	    {
		LM_INFO("msg:\n");
		LM_INFO(String.format("\tid=%d\n", msg.id));
		LM_INFO(String.format("\tpid=%d\n", msg.pid));
		LM_INFO(String.format("\teoh='%s'\n", msg.eoh));
		LM_INFO(String.format("\tunparsed='%s'\n", msg.unparsed));
		LM_INFO(String.format("\tbuf='%s'\n", msg.buf));
		LM_INFO(String.format("\tlen=%d\n", msg.len));
		LM_INFO(String.format("\tnew_uri='%s'\n", msg.new_uri));
		LM_INFO(String.format("\tdst_uri='%s'\n", msg.dst_uri));
		LM_INFO(String.format("\tparsed_uri_ok=%d\n", msg.parsed_uri_ok));
		LM_INFO(String.format("\tparsed_orig_ruri_ok=%d\n", msg.parsed_orig_ruri_ok));
		LM_INFO(String.format("\tadd_to_branch_s='%s'\n", msg.add_to_branch_s));
		LM_INFO(String.format("\tadd_to_branch_len=%d\n", msg.add_to_branch_len));
		LM_INFO(String.format("\thash_index=%d\n", msg.hash_index));
		LM_INFO(String.format("\tmsg_flags=%d\n", msg.msg_flags));
		LM_INFO(String.format("\tset_global_address='%s'\n", msg.set_global_address));
		LM_INFO(String.format("\tset_global_port='%s'\n", msg.set_global_port));
	    }
	    else
	    {
		LM_ERR("SipMsg msg is null!\n");
	    }

*/
//	    retval = KamExec("append_hf", "P-hint: VOICEMAIL\r\n");

//	    KamExec("sl_send_reply", "404", "Not relaying");

//	    retval = KamExec("is_method", "INVITE|SUBSCRIBE");

//	    LM_INFO(String.format("return value: %d\n", retval));




//	    retval = CoreMethods.rewriteuri("sip:0002@192.168.254.99:5060");


//	    retval = KamExec("rewriteuri", "sip:0002@192.168.254.99:5060");
//	    retval = KamExec("sl_send_reply", "404", "relaying failed");

//	    LM_INFO(String.format("return value: %d\n", retval));


//	    retval = CoreMethods.add_local_rport();
//	    retval = CoreMethods.drop();
//	    retval = CoreMethods.force_rport();
//	    retval = CoreMethods.force_send_socket("192.168.254.9", 50349);
//	    retval = CoreMethods.forward("198.61.206.9", 5060);
//	    retval = CoreMethods.forward();


//	    CoreMethods.setflag(3);

//	    boolstate = CoreMethods.isflagset(3);
//	    LM_INFO("return state: " + boolstate + "\n");

//	    retval = CoreMethods.revert_uri();
    
//	    retval = CoreMethods.route("NATDETECT");
//	    retval = CoreMethods.route("5");

	    retval = KamExec("is_method", "INVITE");

	    LM_INFO(String.format("return value: %d\n", retval));



	    return 1;
	}


	public static final int FLT_ACC		= 1;
	public static final int FLT_ACCMISSED	= 2;
	public static final int FLT_ACCFAILED	= 3;
	public static final int FLT_NATS	= 5;
	public static final int FLB_NATB	= 6;
	public static final int FLB_NATSIPPING	= 7;


	/// route ///
	public int route()
	{
	    return 1;
	}



	/// request_route ///
	public int request_route()
	{

	    // per request initial checks
	    CoreMethods.route("REQINIT");

	    // NAT detection
    	    CoreMethods.route("NATDETECT");

	    // CANCEL processing
    	    if (WrappedMethods.is_method("CANCEL"))
    	    {
                if (WrappedMethods.t_check_trans())
		{
                    WrappedMethods.t_relay();
		}

                return 1;
    	    }

/*


    	    // handle requests within SIP dialogs
    	    CoreMethods.route("WITHINDLG");

	    //### only initial requests (no To tag)
	    WrappedMethods.t_check_trans();

	    // authentication
	    CoreMethods.route("AUTH");

    	    // record routing for dialog forming requests (in case they are routed)
	    // - remove preloaded route headers
	    WrappedMethods.remove_hf("Route");
	    if (WrappedMethods.is_method("INVITE|SUBSCRIBE"))
	    {
                WrappedMethods.record_route();
	    }

	    // account only INVITEs
	    if (WrappedMethods.is_method("INVITE"))
    	    {
        	CoreMethods.setflag(FLT_ACC); // do accounting
	    }

	    // dispatch requests to foreign domains
	    CoreMethods.route("SIPOUT");

	    // ### requests for my local domains

	    // handle presence related requests
	    CoreMethods.route("PRESENCE");

	    String ruri = SipMsg.getRURI();
    	    if (ruri == null || ruri.length() <= 0)
    	    {
                // request with no Username in RURI
                WrappedMethods.sl_send_reply("484", "Address Incomplete");
                return 1;
    	    }

	    // dispatch destinations to PSTN
    	    CoreMethods.route("PSTN");

	    // user location service
	    CoreMethods.route("LOCATION");

	    CoreMethods.route("RELAY");

*/

	    return 1;
	}








	public int Route_REQINIT()
	{
	    return 1;
	}

	public int Route_NATDETECT()
	{
	    return 1;
	}

	public int Route_WITHINDLG()	
	{
	    if (WrappedMethods.has_totag())
	    {
		// sequential request withing a dialog should
                // take the path determined by record-routing

		if (WrappedMethods.loose_route())
		{
		    CoreMethods.route("DLGURI");

		    if (WrappedMethods.is_method("BYE"))
		    {
			CoreMethods.setflag(FLT_ACC);		// do accounting ...
			CoreMethods.setflag(FLT_ACCFAILED);	// ... even if the transaction fails
		    }
		    else if (WrappedMethods.is_method("ACK"))
		    {
			// ACK is forwarded statelessy
			CoreMethods.route("NATMANAGE");
		    }
		    else if (WrappedMethods.is_method("NOTIFY"))
		    {
			// Add Record-Route for in-dialog NOTIFY as per RFC 6665.
			WrappedMethods.record_route();
		    }

		    CoreMethods.route("RELAY");
		}
		else
		{
/* // this block would not work -- 'uri' and 'myself' aren't implemented yet //
                        if (is_method("SUBSCRIBE") && uri == myself) {
                                # in-dialog subscribe requests
                                route(PRESENCE);
                                exit;
                        }
*/
			if (WrappedMethods.is_method("ACK"))
			{
			    if (WrappedMethods.t_check_trans())
			    {
                                // no loose-route, but stateful ACK;
                                // must be an ACK after a 487
                                // or e.g. 404 from upstream server
                                WrappedMethods.t_relay();
			    }
			    else
			    {
				// ACK without matching transaction ... ignore and discard
				return 1;
			    }	
			}
			WrappedMethods.sl_send_reply("404", "Not here");
		}

	    }


	    return 1;
	}

	public int Route_AUTH()
	{
	    return 1;
	}

	public int Route_SIPOUT()
	{
	    return 1;
	}

	public int Route_PRESENCE()
	{
	    return 1;
	}

	public int Route_PSTN()
	{
	    return 1;
	}

	public int Route_LOCATION()
	{
	    return 1;
	}

	public int Route_RELAY()
	{
	    // enable additional event routes for forwarded requests
	    //  - serial forking, RTP relaying handling, a.s.o.
    	    if (WrappedMethods.is_method("INVITE|SUBSCRIBE"))
	    {
////                WrappedMethods.t_on_branch("MANAGE_BRANCH");
////                WrappedMethods.t_on_reply("MANAGE_REPLY");
    	    }
	    
	    if (WrappedMethods.is_method("INVITE"))
	    {
////                WrappedMethods.t_on_failure("MANAGE_FAILURE");
    	    }

    	    if (!WrappedMethods.t_relay())
	    {
                WrappedMethods.sl_reply_error();
	    }

	    return 1;
	}

	public int Route_REGISTRAR()
	{
	    if (WrappedMethods.is_method("REGISTER"))
	    {
		if (CoreMethods.isflagset(FLT_NATS))
		{
		    CoreMethods.setflag(FLB_NATB);
		    // uncomment next line to do SIP NAT pinging
		    // CoreMethods.setflag(FLB_NATSIPPING);
		}

		if (!WrappedMethods.save("location"))
		{
		    WrappedMethods.sl_reply_error();
		}
	    }

	    return 1;
	}

}