This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
kamailio:k43-async-sip-routing-nodejs [2015/07/28 09:23] – created admin | kamailio:k43-async-sip-routing-nodejs [2015/09/07 11:36] (current) – [Async SIP Routing with Kamailio and Node.js] admin | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Async SIP Routing with Kamailio and Node.js ====== | ====== Async SIP Routing with Kamailio and Node.js ====== | ||
+ | |||
+ | Node.js-based external routing decision engine for Kamailio. | ||
+ | |||
+ | ===== Overview ===== | ||
The aim is to show how to leverage [[http:// | The aim is to show how to leverage [[http:// | ||
Line 5: | Line 9: | ||
The [[http:// | The [[http:// | ||
+ | {{ : | ||
+ | ===== SIP Routing ===== | ||
+ | |||
+ | The routing JSON document format specified by the [[http:// | ||
+ | |||
+ | Serial and parallel forking are the basic mechanisms for forwarding SIP traffic: | ||
+ | |||
+ | * serial forking - try several destinations one after the other, waiting for a final response on each try, stopping when one answered with 200ok or there is no other destination to try | ||
+ | * parallel forking - try several destinations at once, stopping when one answered with 200ok and cancelling the other active branches, or when all branches responded with a negative response. | ||
+ | |||
+ | Leveraging the two routing mechanisms, one can implement various routing engine such as load balancer, least cost routing, group hunting, call forwarding (on no answer, busy, etc.). Kamailio has dedicated modules (e.g., dispatcher, lcr, ...) to provide those features, but sometimes the logic behind deciding what is the best route to follow might not match existing components. Writing an application in nodejs might be faster or better integrated in the overall environment (e.g., when dealing with multiple nodes), than writing a new module in C. Obviously, the code written in C is always expected to run faster. | ||
+ | |||
+ | The tutorial here is aiming to offer a basic example, which should help building more complex external applications for deciding the SIP routing to be performed by Kamailio. | ||
+ | |||
+ | ==== RTJSON Routing Document ==== | ||
+ | |||
+ | The structure for RTJSON routing document is detailed at: | ||
+ | |||
+ | * [[http:// | ||
+ | |||
+ | Next is a partial example, with the attributes for one destination: | ||
+ | |||
+ | <code javascript> | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | [ | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | }, | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | }, | ||
+ | " | ||
+ | }, | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | }, | ||
+ | ... | ||
+ | ] | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The **routing** field specify if Kamailio has to do serial or parallel forking. After that, the relevant routing information is an array of destinations stored in **routes** field. | ||
+ | |||
+ | Each destination corresponds to a SIP branch that is going to be created by Kamailio. For each branch can be specified values to set: | ||
+ | |||
+ | * request uri | ||
+ | * outbound proxy address (dst uri) | ||
+ | * display name and uri for From/To headers | ||
+ | * extra headers | ||
+ | * retransmission and ringing timeouts | ||
+ | * local socket or the path to be followed | ||
+ | * branch flags | ||
+ | |||
+ | The example above results in: the request will be routed to sip: | ||
+ | |||
+ | ==== EVAPI Processing ==== | ||
+ | |||
+ | EVAPI is sort of generic framework to push events to external application from inside kamailio.cfg via TCP connections. It has the capability of suspending the processing of the SIP request until there is a response from the external application. By suspending the SIP request, other SIP messages can be processes, thus not blocking Kamailio while waiting for event response. This asynchronous mechanism enables high throughput for SIP routing. | ||
+ | ===== Presentations ===== | ||
+ | |||
+ | The presentation at Cluecon 2015 by Daniel-Constantin Mierla includes content about this tutorial: | ||
+ | |||
+ | * [[http:// | ||
+ | * [[http:// | ||
===== Kamailio Config File ===== | ===== Kamailio Config File ===== | ||
Line 21: | Line 101: | ||
* add the routing blocks specific for evapi module | * add the routing blocks specific for evapi module | ||
* use jansson to investigate returned JSON document and take decision of what to do | * use jansson to investigate returned JSON document and take decision of what to do | ||
+ | |||
+ | ==== Config Diff ==== | ||
The diff of the config file comparing with the default one: | The diff of the config file comparing with the default one: | ||
Line 141: | Line 223: | ||
</ | </ | ||
+ | |||
+ | ==== Full Config ==== | ||
+ | |||
+ | The full config file is: | ||
+ | |||
+ | <code c kamailio.cfg> | ||
+ | #!KAMAILIO | ||
+ | # | ||
+ | # Kamailio (OpenSER) SIP Server v4.3 - default configuration script | ||
+ | # - web: http:// | ||
+ | # - git: http:// | ||
+ | # | ||
+ | # Direct your questions about this file to: < | ||
+ | # | ||
+ | # Refer to the Core CookBook at http:// | ||
+ | # for an explanation of possible statements, functions and parameters. | ||
+ | # | ||
+ | # Several features can be enabled using '# | ||
+ | # | ||
+ | # *** To run in debug mode: | ||
+ | # - define WITH_DEBUG | ||
+ | # | ||
+ | # *** To enable mysql: | ||
+ | # - define WITH_MYSQL | ||
+ | # | ||
+ | # *** To enable authentication execute: | ||
+ | # - enable mysql | ||
+ | # - define WITH_AUTH | ||
+ | # - add users using ' | ||
+ | # | ||
+ | # *** To enable IP authentication execute: | ||
+ | # - enable mysql | ||
+ | # - enable authentication | ||
+ | # - define WITH_IPAUTH | ||
+ | # - add IP addresses with group id ' | ||
+ | # | ||
+ | # *** To enable persistent user location execute: | ||
+ | # - enable mysql | ||
+ | # - define WITH_USRLOCDB | ||
+ | # | ||
+ | # *** To enable nat traversal execute: | ||
+ | # - define WITH_NAT | ||
+ | # - install RTPProxy: http:// | ||
+ | # - start RTPProxy: | ||
+ | # rtpproxy -l _your_public_ip_ -s udp: | ||
+ | # - option for NAT SIP OPTIONS keepalives: WITH_NATSIPPING | ||
+ | # | ||
+ | # *** To enable TLS support execute: | ||
+ | # - adjust CFGDIR/ | ||
+ | # - define WITH_TLS | ||
+ | # | ||
+ | # *** To enhance accounting execute: | ||
+ | # - enable mysql | ||
+ | # - define WITH_ACCDB | ||
+ | # - add following columns to database | ||
+ | #!ifdef ACCDB_COMMENT | ||
+ | ALTER TABLE acc ADD COLUMN src_user VARCHAR(64) NOT NULL DEFAULT ''; | ||
+ | ALTER TABLE acc ADD COLUMN src_domain VARCHAR(128) NOT NULL DEFAULT ''; | ||
+ | ALTER TABLE acc ADD COLUMN src_ip varchar(64) NOT NULL default ''; | ||
+ | ALTER TABLE acc ADD COLUMN dst_ouser VARCHAR(64) NOT NULL DEFAULT ''; | ||
+ | ALTER TABLE acc ADD COLUMN dst_user VARCHAR(64) NOT NULL DEFAULT ''; | ||
+ | ALTER TABLE acc ADD COLUMN dst_domain VARCHAR(128) NOT NULL DEFAULT ''; | ||
+ | ALTER TABLE missed_calls ADD COLUMN src_user VARCHAR(64) NOT NULL DEFAULT ''; | ||
+ | ALTER TABLE missed_calls ADD COLUMN src_domain VARCHAR(128) NOT NULL DEFAULT ''; | ||
+ | ALTER TABLE missed_calls ADD COLUMN src_ip varchar(64) NOT NULL default ''; | ||
+ | ALTER TABLE missed_calls ADD COLUMN dst_ouser VARCHAR(64) NOT NULL DEFAULT ''; | ||
+ | ALTER TABLE missed_calls ADD COLUMN dst_user VARCHAR(64) NOT NULL DEFAULT ''; | ||
+ | ALTER TABLE missed_calls ADD COLUMN dst_domain VARCHAR(128) NOT NULL DEFAULT ''; | ||
+ | #!endif | ||
+ | |||
+ | ####### Include Local Config If Exists ######### | ||
+ | import_file " | ||
+ | |||
+ | ####### Defined Values ######### | ||
+ | |||
+ | # *** Value defines - IDs used later in config | ||
+ | #!ifdef WITH_MYSQL | ||
+ | # - database URL - used to connect to database server by modules such | ||
+ | # as: auth_db, acc, usrloc, a.s.o. | ||
+ | #!ifndef DBURL | ||
+ | #!define DBURL " | ||
+ | #!endif | ||
+ | #!endif | ||
+ | #!define MULTIDOMAIN 0 | ||
+ | |||
+ | # - flags | ||
+ | # FLT_ - per transaction (message) flags | ||
+ | # FLB_ - per branch flags | ||
+ | #!define FLT_ACC 1 | ||
+ | #!define FLT_ACCMISSED 2 | ||
+ | #!define FLT_ACCFAILED 3 | ||
+ | #!define FLT_NATS 5 | ||
+ | |||
+ | #!define FLB_NATB 6 | ||
+ | #!define FLB_NATSIPPING 7 | ||
+ | |||
+ | ####### Global Parameters ######### | ||
+ | |||
+ | ### LOG Levels: 3=DBG, 2=INFO, 1=NOTICE, 0=WARN, -1=ERR | ||
+ | #!ifdef WITH_DEBUG | ||
+ | debug=4 | ||
+ | log_stderror=yes | ||
+ | #!else | ||
+ | debug=2 | ||
+ | log_stderror=no | ||
+ | #!endif | ||
+ | |||
+ | memdbg=5 | ||
+ | memlog=5 | ||
+ | |||
+ | log_facility=LOG_LOCAL0 | ||
+ | |||
+ | fork=yes | ||
+ | children=4 | ||
+ | |||
+ | /* uncomment the next line to disable TCP (default on) */ | ||
+ | # | ||
+ | |||
+ | /* uncomment the next line to disable the auto discovery of local aliases | ||
+ | based on reverse DNS on IPs (default on) */ | ||
+ | # | ||
+ | |||
+ | /* add local domain aliases */ | ||
+ | # | ||
+ | |||
+ | /* uncomment and configure the following line if you want Kamailio to | ||
+ | bind on a specific interface/ | ||
+ | # | ||
+ | |||
+ | /* port to listen to | ||
+ | * - can be specified more than once if needed to listen on many ports */ | ||
+ | port=5060 | ||
+ | |||
+ | #!ifdef WITH_TLS | ||
+ | enable_tls=yes | ||
+ | #!endif | ||
+ | |||
+ | # life time of TCP connection when there is no traffic | ||
+ | # - a bit higher than registration expires to cope with UA behind NAT | ||
+ | tcp_connection_lifetime=3605 | ||
+ | |||
+ | ####### Modules Section ######## | ||
+ | |||
+ | # set paths to location of modules (to sources or installation folders) | ||
+ | #!ifdef WITH_SRCPATH | ||
+ | mpath=" | ||
+ | #!else | ||
+ | mpath="/ | ||
+ | #!endif | ||
+ | |||
+ | #!ifdef WITH_MYSQL | ||
+ | loadmodule " | ||
+ | #!endif | ||
+ | |||
+ | loadmodule " | ||
+ | loadmodule " | ||
+ | loadmodule " | ||
+ | loadmodule " | ||
+ | loadmodule " | ||
+ | loadmodule " | ||
+ | loadmodule " | ||
+ | loadmodule " | ||
+ | loadmodule " | ||
+ | loadmodule " | ||
+ | loadmodule " | ||
+ | loadmodule " | ||
+ | loadmodule " | ||
+ | loadmodule " | ||
+ | loadmodule " | ||
+ | loadmodule " | ||
+ | loadmodule " | ||
+ | loadmodule " | ||
+ | loadmodule " | ||
+ | |||
+ | #!ifdef WITH_AUTH | ||
+ | loadmodule " | ||
+ | loadmodule " | ||
+ | #!ifdef WITH_IPAUTH | ||
+ | loadmodule " | ||
+ | #!endif | ||
+ | #!endif | ||
+ | |||
+ | #!ifdef WITH_NAT | ||
+ | loadmodule " | ||
+ | loadmodule " | ||
+ | #!endif | ||
+ | |||
+ | #!ifdef WITH_TLS | ||
+ | loadmodule " | ||
+ | #!endif | ||
+ | |||
+ | #!ifdef WITH_DEBUG | ||
+ | loadmodule " | ||
+ | #!endif | ||
+ | |||
+ | loadmodule " | ||
+ | loadmodule " | ||
+ | loadmodule " | ||
+ | |||
+ | # ----------------- setting module-specific parameters --------------- | ||
+ | |||
+ | |||
+ | # ----- mi_fifo params ----- | ||
+ | # | ||
+ | |||
+ | # ----- ctl params ----- | ||
+ | # | ||
+ | |||
+ | # ----- tm params ----- | ||
+ | # auto-discard branches from previous serial forking leg | ||
+ | modparam(" | ||
+ | # default retransmission timeout: 30sec | ||
+ | modparam(" | ||
+ | # default invite retransmission timeout after 1xx: 120sec | ||
+ | modparam(" | ||
+ | |||
+ | |||
+ | # ----- rr params ----- | ||
+ | # add value to ;lr param to cope with most of the UAs | ||
+ | modparam(" | ||
+ | # do not append from tag to the RR (no need for this script) | ||
+ | modparam(" | ||
+ | |||
+ | |||
+ | # ----- registrar params ----- | ||
+ | modparam(" | ||
+ | /* uncomment the next line to disable parallel forking via location */ | ||
+ | # modparam(" | ||
+ | /* uncomment the next line not to allow more than 10 contacts per AOR */ | ||
+ | # | ||
+ | # max value for expires of registrations | ||
+ | modparam(" | ||
+ | # set it to 1 to enable GRUU | ||
+ | modparam(" | ||
+ | |||
+ | |||
+ | # ----- acc params ----- | ||
+ | /* what special events should be accounted ? */ | ||
+ | modparam(" | ||
+ | modparam(" | ||
+ | modparam(" | ||
+ | /* by default ww do not adjust the direct of the sequential requests. | ||
+ | if you enable this parameter, be sure the enable " | ||
+ | in " | ||
+ | modparam(" | ||
+ | /* account triggers (flags) */ | ||
+ | modparam(" | ||
+ | modparam(" | ||
+ | modparam(" | ||
+ | " | ||
+ | " | ||
+ | modparam(" | ||
+ | /* enhanced DB accounting */ | ||
+ | #!ifdef WITH_ACCDB | ||
+ | modparam(" | ||
+ | modparam(" | ||
+ | modparam(" | ||
+ | modparam(" | ||
+ | " | ||
+ | " | ||
+ | #!endif | ||
+ | |||
+ | |||
+ | # ----- usrloc params ----- | ||
+ | /* enable DB persistency for location entries */ | ||
+ | #!ifdef WITH_USRLOCDB | ||
+ | modparam(" | ||
+ | modparam(" | ||
+ | modparam(" | ||
+ | #!endif | ||
+ | |||
+ | |||
+ | # ----- auth_db params ----- | ||
+ | #!ifdef WITH_AUTH | ||
+ | modparam(" | ||
+ | modparam(" | ||
+ | modparam(" | ||
+ | modparam(" | ||
+ | modparam(" | ||
+ | |||
+ | # ----- permissions params ----- | ||
+ | #!ifdef WITH_IPAUTH | ||
+ | modparam(" | ||
+ | modparam(" | ||
+ | #!endif | ||
+ | |||
+ | #!endif | ||
+ | |||
+ | |||
+ | #!ifdef WITH_NAT | ||
+ | # ----- rtpproxy params ----- | ||
+ | modparam(" | ||
+ | |||
+ | # ----- nathelper params ----- | ||
+ | modparam(" | ||
+ | modparam(" | ||
+ | modparam(" | ||
+ | modparam(" | ||
+ | |||
+ | # params needed for NAT traversal in other modules | ||
+ | modparam(" | ||
+ | modparam(" | ||
+ | #!endif | ||
+ | |||
+ | |||
+ | #!ifdef WITH_TLS | ||
+ | # ----- tls params ----- | ||
+ | modparam(" | ||
+ | #!endif | ||
+ | |||
+ | #!ifdef WITH_DEBUG | ||
+ | # ----- debugger params ----- | ||
+ | modparam(" | ||
+ | #!endif | ||
+ | |||
+ | |||
+ | # ----- evapi params ----- | ||
+ | modparam(" | ||
+ | |||
+ | ####### Routing Logic ######## | ||
+ | |||
+ | |||
+ | # Main SIP request routing logic | ||
+ | # - processing of any incoming SIP request starts with this route | ||
+ | # - note: this is the same as route { ... } | ||
+ | request_route { | ||
+ | |||
+ | sl_send_reply(" | ||
+ | |||
+ | # per request initial checks | ||
+ | route(REQINIT); | ||
+ | |||
+ | # NAT detection | ||
+ | route(NATDETECT); | ||
+ | |||
+ | # CANCEL processing | ||
+ | if (is_method(" | ||
+ | if (t_check_trans()) { | ||
+ | route(RELAY); | ||
+ | } | ||
+ | exit; | ||
+ | } | ||
+ | |||
+ | # handle requests within SIP dialogs | ||
+ | route(WITHINDLG); | ||
+ | |||
+ | ### only initial requests (no To tag) | ||
+ | |||
+ | # handle retransmissions | ||
+ | if(t_precheck_trans()) { | ||
+ | t_check_trans(); | ||
+ | exit; | ||
+ | } | ||
+ | t_check_trans(); | ||
+ | |||
+ | # authentication | ||
+ | route(AUTH); | ||
+ | |||
+ | # record routing for dialog forming requests (in case they are routed) | ||
+ | # - remove preloaded route headers | ||
+ | remove_hf(" | ||
+ | if (is_method(" | ||
+ | record_route(); | ||
+ | |||
+ | # account only INVITEs | ||
+ | if (is_method(" | ||
+ | setflag(FLT_ACC); | ||
+ | } | ||
+ | |||
+ | # dispatch requests to foreign domains | ||
+ | route(SIPOUT); | ||
+ | |||
+ | ### requests for my local domains | ||
+ | |||
+ | # handle registrations | ||
+ | route(REGISTRAR); | ||
+ | |||
+ | route(TOEVAPI); | ||
+ | exit; | ||
+ | |||
+ | if ($rU==$null) { | ||
+ | # request with no Username in RURI | ||
+ | sl_send_reply(" | ||
+ | exit; | ||
+ | } | ||
+ | |||
+ | # user location service | ||
+ | route(LOCATION); | ||
+ | } | ||
+ | |||
+ | |||
+ | route[RELAY] { | ||
+ | # enable additional event routes for forwarded requests | ||
+ | # - serial forking, RTP relaying handling, a.s.o. | ||
+ | if (is_method(" | ||
+ | if(!t_is_set(" | ||
+ | } | ||
+ | if (is_method(" | ||
+ | if(!t_is_set(" | ||
+ | } | ||
+ | if (is_method(" | ||
+ | if(!t_is_set(" | ||
+ | } | ||
+ | |||
+ | if (!t_relay()) { | ||
+ | sl_reply_error(); | ||
+ | } | ||
+ | exit; | ||
+ | } | ||
+ | |||
+ | # Per SIP request initial checks | ||
+ | route[REQINIT] { | ||
+ | #!ifdef WITH_ANTIFLOOD | ||
+ | # flood dection from same IP and traffic ban for a while | ||
+ | # be sure you exclude checking trusted peers, such as pstn gateways | ||
+ | # - local host excluded (e.g., loop to self) | ||
+ | if(src_ip!=myself) { | ||
+ | if($sht(ipban=> | ||
+ | # ip is already blocked | ||
+ | xdbg(" | ||
+ | exit; | ||
+ | } | ||
+ | if (!pike_check_req()) { | ||
+ | xlog(" | ||
+ | $sht(ipban=> | ||
+ | exit; | ||
+ | } | ||
+ | } | ||
+ | if($ua =~ " | ||
+ | sl_send_reply(" | ||
+ | exit; | ||
+ | } | ||
+ | #!endif | ||
+ | |||
+ | if (!mf_process_maxfwd_header(" | ||
+ | sl_send_reply(" | ||
+ | exit; | ||
+ | } | ||
+ | |||
+ | if(is_method(" | ||
+ | sl_send_reply(" | ||
+ | exit; | ||
+ | } | ||
+ | |||
+ | if(!sanity_check(" | ||
+ | xlog(" | ||
+ | exit; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | # Handle requests within SIP dialogs | ||
+ | route[WITHINDLG] { | ||
+ | if (!has_totag()) return; | ||
+ | |||
+ | # sequential request withing a dialog should | ||
+ | # take the path determined by record-routing | ||
+ | if (loose_route()) { | ||
+ | route(DLGURI); | ||
+ | if (is_method(" | ||
+ | setflag(FLT_ACC); | ||
+ | setflag(FLT_ACCFAILED); | ||
+ | } | ||
+ | else if ( is_method(" | ||
+ | # ACK is forwarded statelessy | ||
+ | route(NATMANAGE); | ||
+ | } | ||
+ | else if ( is_method(" | ||
+ | # Add Record-Route for in-dialog NOTIFY as per RFC 6665. | ||
+ | record_route(); | ||
+ | } | ||
+ | route(RELAY); | ||
+ | exit; | ||
+ | } | ||
+ | if ( is_method(" | ||
+ | if ( t_check_trans() ) { | ||
+ | # no loose-route, | ||
+ | # must be an ACK after a 487 | ||
+ | # or e.g. 404 from upstream server | ||
+ | route(RELAY); | ||
+ | exit; | ||
+ | } else { | ||
+ | # ACK without matching transaction ... ignore and discard | ||
+ | exit; | ||
+ | } | ||
+ | } | ||
+ | sl_send_reply(" | ||
+ | exit; | ||
+ | } | ||
+ | |||
+ | # Handle SIP registrations | ||
+ | route[REGISTRAR] { | ||
+ | if (!is_method(" | ||
+ | if(isflagset(FLT_NATS)) { | ||
+ | setbflag(FLB_NATB); | ||
+ | #!ifdef WITH_NATSIPPING | ||
+ | # do SIP NAT pinging | ||
+ | setbflag(FLB_NATSIPPING); | ||
+ | #!endif | ||
+ | } | ||
+ | if (!save(" | ||
+ | sl_reply_error(); | ||
+ | |||
+ | exit; | ||
+ | } | ||
+ | |||
+ | # User location service | ||
+ | route[LOCATION] { | ||
+ | if (!lookup(" | ||
+ | $var(rc) = $rc; | ||
+ | t_newtran(); | ||
+ | switch ($var(rc)) { | ||
+ | case -1: | ||
+ | case -3: | ||
+ | send_reply(" | ||
+ | exit; | ||
+ | case -2: | ||
+ | send_reply(" | ||
+ | exit; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | # when routing via usrloc, log the missed calls also | ||
+ | if (is_method(" | ||
+ | setflag(FLT_ACCMISSED); | ||
+ | } | ||
+ | |||
+ | route(RELAY); | ||
+ | exit; | ||
+ | } | ||
+ | |||
+ | |||
+ | # IP authorization and user uthentication | ||
+ | route[AUTH] { | ||
+ | #!ifdef WITH_AUTH | ||
+ | |||
+ | #!ifdef WITH_IPAUTH | ||
+ | if((!is_method(" | ||
+ | # source IP allowed | ||
+ | return; | ||
+ | } | ||
+ | #!endif | ||
+ | |||
+ | if (is_method(" | ||
+ | # authenticate requests | ||
+ | if (!auth_check(" | ||
+ | auth_challenge(" | ||
+ | exit; | ||
+ | } | ||
+ | # user authenticated - remove auth header | ||
+ | if(!is_method(" | ||
+ | consume_credentials(); | ||
+ | } | ||
+ | # if caller is not local subscriber, then check if it calls | ||
+ | # a local destination, | ||
+ | if (from_uri!=myself && uri!=myself) { | ||
+ | sl_send_reply(" | ||
+ | exit; | ||
+ | } | ||
+ | |||
+ | #!endif | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | # Caller NAT detection | ||
+ | route[NATDETECT] { | ||
+ | #!ifdef WITH_NAT | ||
+ | force_rport(); | ||
+ | if (nat_uac_test(" | ||
+ | if (is_method(" | ||
+ | fix_nated_register(); | ||
+ | } else { | ||
+ | if(is_first_hop()) | ||
+ | set_contact_alias(); | ||
+ | } | ||
+ | setflag(FLT_NATS); | ||
+ | } | ||
+ | #!endif | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | # RTPProxy control | ||
+ | route[NATMANAGE] { | ||
+ | #!ifdef WITH_NAT | ||
+ | if (is_request()) { | ||
+ | if(has_totag()) { | ||
+ | if(check_route_param(" | ||
+ | setbflag(FLB_NATB); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | if (!(isflagset(FLT_NATS) || isbflagset(FLB_NATB))) | ||
+ | return; | ||
+ | |||
+ | rtpproxy_manage(" | ||
+ | |||
+ | if (is_request()) { | ||
+ | if (!has_totag()) { | ||
+ | if(t_is_branch_route()) { | ||
+ | add_rr_param("; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | if (is_reply()) { | ||
+ | if(isbflagset(FLB_NATB)) { | ||
+ | set_contact_alias(); | ||
+ | } | ||
+ | } | ||
+ | #!endif | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | # URI update for dialog requests | ||
+ | route[DLGURI] { | ||
+ | #!ifdef WITH_NAT | ||
+ | if(!isdsturiset()) { | ||
+ | handle_ruri_alias(); | ||
+ | } | ||
+ | #!endif | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | # Routing to foreign domains | ||
+ | route[SIPOUT] { | ||
+ | if (uri==myself) return; | ||
+ | |||
+ | append_hf(" | ||
+ | route(RELAY); | ||
+ | exit; | ||
+ | } | ||
+ | |||
+ | # Manage outgoing branches | ||
+ | branch_route[MANAGE_BRANCH] { | ||
+ | xdbg(" | ||
+ | rtjson_update_branch(); | ||
+ | route(NATMANAGE); | ||
+ | } | ||
+ | |||
+ | # Manage incoming replies | ||
+ | onreply_route[MANAGE_REPLY] { | ||
+ | xdbg(" | ||
+ | if(status=~" | ||
+ | route(NATMANAGE); | ||
+ | } | ||
+ | |||
+ | # Manage failure routing cases | ||
+ | failure_route[MANAGE_FAILURE] { | ||
+ | route(NATMANAGE); | ||
+ | |||
+ | if (t_is_canceled()) { | ||
+ | exit; | ||
+ | } | ||
+ | |||
+ | if(rtjson_next_route()) { | ||
+ | t_on_branch(" | ||
+ | t_on_failure(" | ||
+ | route(RELAY); | ||
+ | exit; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | event_route[evapi: | ||
+ | xlog(" | ||
+ | if($evapi(srcaddr)!=" | ||
+ | evapi_close(); | ||
+ | exit; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | event_route[evapi: | ||
+ | xlog(" | ||
+ | } | ||
+ | |||
+ | event_route[evapi: | ||
+ | xlog(" | ||
+ | if($evapi(msg)=~" | ||
+ | jansson_get(" | ||
+ | jansson_get(" | ||
+ | $var(evmsg) = $evapi(msg); | ||
+ | xlog(" | ||
+ | t_continue(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | route[EVAPIRESPONSE] { | ||
+ | xlog(" | ||
+ | # | ||
+ | # | ||
+ | # exit; | ||
+ | #} | ||
+ | jansson_get(" | ||
+ | if($var(routing)==" | ||
+ | route(LOCATION); | ||
+ | exit; | ||
+ | } | ||
+ | rtjson_init_routes(" | ||
+ | rtjson_push_routes(); | ||
+ | t_on_branch(" | ||
+ | t_on_failure(" | ||
+ | route(RELAY); | ||
+ | exit; | ||
+ | } | ||
+ | |||
+ | route[TOEVAPI] { | ||
+ | evapi_async_relay(" | ||
+ | " \" | ||
+ | " \" | ||
+ | |||
+ | xlog(" | ||
+ | exit; | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== Node.js Application ===== | ||
+ | |||
+ | The installation of node.js is documented on the project website: | ||
+ | |||
+ | * http:// | ||
+ | |||
+ | A simple sample application is shown next example. It connects to Kamailio' | ||
+ | |||
+ | To read more about nestring format, see: | ||
+ | |||
+ | * https:// | ||
+ | |||
+ | Node.js application: | ||
+ | |||
+ | <code javascript kanapi.js> | ||
+ | var net = require(' | ||
+ | |||
+ | var HOST = ' | ||
+ | var PORT = 8448; | ||
+ | |||
+ | var client = new net.Socket(); | ||
+ | |||
+ | client.connect(PORT, | ||
+ | |||
+ | console.log(' | ||
+ | |||
+ | }); | ||
+ | |||
+ | client.on(' | ||
+ | console.log(' | ||
+ | // Write a message to the socket as soon as the client is connected, the server will receive it as message from the client | ||
+ | // client.write(' | ||
+ | kanapi_send_response(' | ||
+ | }); | ||
+ | |||
+ | client.on(' | ||
+ | if(e.code == ' | ||
+ | console.log(' | ||
+ | |||
+ | setTimeout(function() { | ||
+ | client.connect(PORT, | ||
+ | // console.log(' | ||
+ | }); | ||
+ | }, 4000); | ||
+ | |||
+ | console.log(' | ||
+ | |||
+ | } | ||
+ | }); | ||
+ | |||
+ | // Add a ' | ||
+ | // data is what the server sent to this socket | ||
+ | client.on(' | ||
+ | |||
+ | console.log(' | ||
+ | kanapi_handle_request(data); | ||
+ | |||
+ | // Close the client socket completely | ||
+ | // client.destroy(); | ||
+ | |||
+ | }); | ||
+ | |||
+ | // Add a ' | ||
+ | client.on(' | ||
+ | console.log(' | ||
+ | }); | ||
+ | |||
+ | function kanapi_handle_request(data) { | ||
+ | var size, strings, jdoc; | ||
+ | var re = / | ||
+ | var response = {}; | ||
+ | |||
+ | size = 0; | ||
+ | strings = []; | ||
+ | |||
+ | data = new Buffer(data || '', | ||
+ | |||
+ | for(var i = 0; i < data.length; | ||
+ | var offset, c; | ||
+ | |||
+ | c = data[i]; | ||
+ | |||
+ | if (c === 58) { | ||
+ | offset = i + 1; | ||
+ | size = parseInt(data.toString(null, | ||
+ | strings.push(data.slice(offset, | ||
+ | i += size + 1; | ||
+ | size += offset + 1; | ||
+ | continue; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | console.log(' | ||
+ | |||
+ | jdoc = JSON.parse(strings); | ||
+ | |||
+ | console.log(' | ||
+ | if(jdoc.event != ' | ||
+ | return kanapi_send_response(' | ||
+ | } | ||
+ | |||
+ | console.log(' | ||
+ | console.log(' | ||
+ | | ||
+ | response.version=" | ||
+ | response.xtra = {}; | ||
+ | response.xtra.tindex=jdoc.tindex; | ||
+ | response.xtra.tlabel=jdoc.tlabel; | ||
+ | |||
+ | if(re.test(jdoc.callee)) { | ||
+ | // e.164 number - send to gateway | ||
+ | response.routing = " | ||
+ | // | ||
+ | response.routes = []; | ||
+ | response.routes[0] = {}; | ||
+ | response.routes[0].uri = " | ||
+ | response.routes[0].headers = {}; | ||
+ | response.routes[0].headers.extra = " | ||
+ | response.routes[1] = {}; | ||
+ | response.routes[1].uri = " | ||
+ | response.routes[1].headers = {}; | ||
+ | response.routes[1].headers.extra = " | ||
+ | } else { | ||
+ | // expect local extension - send to location | ||
+ | response.routing = " | ||
+ | } | ||
+ | return kanapi_send_response(JSON.stringify(response)); | ||
+ | } | ||
+ | |||
+ | function kanapi_send_response(data) { | ||
+ | var buffer; | ||
+ | |||
+ | data = new Buffer(data || '', | ||
+ | |||
+ | buffer = Buffer.concat([new Buffer(data.length + ':', | ||
+ | |||
+ | console.log(' | ||
+ | |||
+ | client.write(buffer.toString(' | ||
+ | return buffer; | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== Resources ===== | ||
+ | |||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[https:// |