5
import org.siprouter.*;
6
import org.siprouter.NativeInterface.*;
8
//import org.siprouter.CoreMethods.*;
10
public class Kamailio extends NativeMethods
14
System.load("/opt/kamailio/lib/kamailio/modules/app_java.so");
17
/* Constructor. Do not remove !!! */
23
public int child_init(int rank)
28
public int TestMethod()
32
boolean boolstate = false;
35
LM_INFO(String.format("Msg Type: %s\n", SipMsg.getMsgType()));
37
IPPair src = SipMsg.getSrcAddress();
40
LM_INFO(String.format("src address=%s, src port=%d\n", src.ip, src.port));
44
LM_ERR("IPPair src is null!");
47
IPPair dst = SipMsg.getDstAddress();
50
LM_INFO(String.format("dst address=%s, dst port=%d\n", dst.ip, dst.port));
54
LM_ERR("IPPair dst is null!");
57
LM_INFO(String.format("buffer:\n%s\n", SipMsg.getBuffer().trim()));
59
SipMsg msg = SipMsg.ParseSipMsg();
63
LM_INFO(String.format("\tid=%d\n", msg.id));
64
LM_INFO(String.format("\tpid=%d\n", msg.pid));
65
LM_INFO(String.format("\teoh='%s'\n", msg.eoh));
66
LM_INFO(String.format("\tunparsed='%s'\n", msg.unparsed));
67
LM_INFO(String.format("\tbuf='%s'\n", msg.buf));
68
LM_INFO(String.format("\tlen=%d\n", msg.len));
69
LM_INFO(String.format("\tnew_uri='%s'\n", msg.new_uri));
70
LM_INFO(String.format("\tdst_uri='%s'\n", msg.dst_uri));
71
LM_INFO(String.format("\tparsed_uri_ok=%d\n", msg.parsed_uri_ok));
72
LM_INFO(String.format("\tparsed_orig_ruri_ok=%d\n", msg.parsed_orig_ruri_ok));
73
LM_INFO(String.format("\tadd_to_branch_s='%s'\n", msg.add_to_branch_s));
74
LM_INFO(String.format("\tadd_to_branch_len=%d\n", msg.add_to_branch_len));
75
LM_INFO(String.format("\thash_index=%d\n", msg.hash_index));
76
LM_INFO(String.format("\tmsg_flags=%d\n", msg.msg_flags));
77
LM_INFO(String.format("\tset_global_address='%s'\n", msg.set_global_address));
78
LM_INFO(String.format("\tset_global_port='%s'\n", msg.set_global_port));
82
LM_ERR("SipMsg msg is null!\n");
86
// retval = KamExec("append_hf", "P-hint: VOICEMAIL\r\n");
88
// KamExec("sl_send_reply", "404", "Not relaying");
90
// retval = KamExec("is_method", "INVITE|SUBSCRIBE");
92
// LM_INFO(String.format("return value: %d\n", retval));
97
// retval = CoreMethods.rewriteuri("sip:0002@192.168.254.99:5060");
100
// retval = KamExec("rewriteuri", "sip:0002@192.168.254.99:5060");
101
// retval = KamExec("sl_send_reply", "404", "relaying failed");
103
// LM_INFO(String.format("return value: %d\n", retval));
106
// retval = CoreMethods.add_local_rport();
107
// retval = CoreMethods.drop();
108
// retval = CoreMethods.force_rport();
109
// retval = CoreMethods.force_send_socket("192.168.254.9", 50349);
110
// retval = CoreMethods.forward("198.61.206.9", 5060);
111
// retval = CoreMethods.forward();
114
// CoreMethods.setflag(3);
116
// boolstate = CoreMethods.isflagset(3);
117
// LM_INFO("return state: " + boolstate + "\n");
119
// retval = CoreMethods.revert_uri();
121
// retval = CoreMethods.route("NATDETECT");
122
// retval = CoreMethods.route("5");
124
retval = KamExec("is_method", "INVITE");
126
LM_INFO(String.format("return value: %d\n", retval));
134
public static final int FLT_ACC = 1;
135
public static final int FLT_ACCMISSED = 2;
136
public static final int FLT_ACCFAILED = 3;
137
public static final int FLT_NATS = 5;
138
public static final int FLB_NATB = 6;
139
public static final int FLB_NATSIPPING = 7;
150
/// request_route ///
151
public int request_route()
154
// per request initial checks
155
CoreMethods.route("REQINIT");
158
CoreMethods.route("NATDETECT");
161
if (WrappedMethods.is_method("CANCEL"))
163
if (WrappedMethods.t_check_trans())
165
WrappedMethods.t_relay();
174
// handle requests within SIP dialogs
175
CoreMethods.route("WITHINDLG");
177
//### only initial requests (no To tag)
178
WrappedMethods.t_check_trans();
181
CoreMethods.route("AUTH");
183
// record routing for dialog forming requests (in case they are routed)
184
// - remove preloaded route headers
185
WrappedMethods.remove_hf("Route");
186
if (WrappedMethods.is_method("INVITE|SUBSCRIBE"))
188
WrappedMethods.record_route();
191
// account only INVITEs
192
if (WrappedMethods.is_method("INVITE"))
194
CoreMethods.setflag(FLT_ACC); // do accounting
197
// dispatch requests to foreign domains
198
CoreMethods.route("SIPOUT");
200
// ### requests for my local domains
202
// handle presence related requests
203
CoreMethods.route("PRESENCE");
205
String ruri = SipMsg.getRURI();
206
if (ruri == null || ruri.length() <= 0)
208
// request with no Username in RURI
209
WrappedMethods.sl_send_reply("484", "Address Incomplete");
213
// dispatch destinations to PSTN
214
CoreMethods.route("PSTN");
216
// user location service
217
CoreMethods.route("LOCATION");
219
CoreMethods.route("RELAY");
233
public int Route_REQINIT()
238
public int Route_NATDETECT()
243
public int Route_WITHINDLG()
245
if (WrappedMethods.has_totag())
247
// sequential request withing a dialog should
248
// take the path determined by record-routing
250
if (WrappedMethods.loose_route())
252
CoreMethods.route("DLGURI");
254
if (WrappedMethods.is_method("BYE"))
256
CoreMethods.setflag(FLT_ACC); // do accounting ...
257
CoreMethods.setflag(FLT_ACCFAILED); // ... even if the transaction fails
259
else if (WrappedMethods.is_method("ACK"))
261
// ACK is forwarded statelessy
262
CoreMethods.route("NATMANAGE");
264
else if (WrappedMethods.is_method("NOTIFY"))
266
// Add Record-Route for in-dialog NOTIFY as per RFC 6665.
267
WrappedMethods.record_route();
270
CoreMethods.route("RELAY");
274
/* // this block would not work -- 'uri' and 'myself' aren't implemented yet //
275
if (is_method("SUBSCRIBE") && uri == myself) {
276
# in-dialog subscribe requests
281
if (WrappedMethods.is_method("ACK"))
283
if (WrappedMethods.t_check_trans())
285
// no loose-route, but stateful ACK;
286
// must be an ACK after a 487
287
// or e.g. 404 from upstream server
288
WrappedMethods.t_relay();
292
// ACK without matching transaction ... ignore and discard
296
WrappedMethods.sl_send_reply("404", "Not here");
305
public int Route_AUTH()
310
public int Route_SIPOUT()
315
public int Route_PRESENCE()
320
public int Route_PSTN()
325
public int Route_LOCATION()
330
public int Route_RELAY()
332
// enable additional event routes for forwarded requests
333
// - serial forking, RTP relaying handling, a.s.o.
334
if (WrappedMethods.is_method("INVITE|SUBSCRIBE"))
336
//// WrappedMethods.t_on_branch("MANAGE_BRANCH");
337
//// WrappedMethods.t_on_reply("MANAGE_REPLY");
340
if (WrappedMethods.is_method("INVITE"))
342
//// WrappedMethods.t_on_failure("MANAGE_FAILURE");
345
if (!WrappedMethods.t_relay())
347
WrappedMethods.sl_reply_error();
353
public int Route_REGISTRAR()
355
if (WrappedMethods.is_method("REGISTER"))
357
if (CoreMethods.isflagset(FLT_NATS))
359
CoreMethods.setflag(FLB_NATB);
360
// uncomment next line to do SIP NAT pinging
361
// CoreMethods.setflag(FLB_NATSIPPING);
364
if (!WrappedMethods.save("location"))
366
WrappedMethods.sl_reply_error();