Skip to content

Instantly share code, notes, and snippets.

@ara4n
Created June 1, 2015 21:55
Show Gist options
  • Save ara4n/219424cad17b4f99b9a7 to your computer and use it in GitHub Desktop.
Save ara4n/219424cad17b4f99b9a7 to your computer and use it in GitHub Desktop.
WIP diff against kamailio-basic.cfg for SIP->Matrix bridging
--- kamailio-basic.cfg 2015-05-19 14:45:13.000000000 +0100
+++ kamailio-matrix.cfg 2015-06-01 22:53:52.000000000 +0100
@@ -1,6 +1,7 @@
#!KAMAILIO
#
-# Kamailio (OpenSER) SIP Server v4.2 - default configuration script
+# Kamailio (OpenSER) SIP Server v4.2 - default configuration script for Matrix
+# forked off kamailio-basic.cfg
# - web: http://www.kamailio.org
# - git: http://sip-router.org
#
@@ -107,6 +108,8 @@
fork=yes
children=4
+pv_buffer_size=8192
+
/* uncomment the next line to disable TCP (default on) */
#disable_tcp=yes
@@ -121,6 +124,11 @@
bind on a specific interface/port/proto (default bind on all available) */
#listen=udp:10.0.0.10:5060
+# http listener
+listen=udp:0.0.0.0:5060
+listen=tcp:0.0.0.0:5060
+listen=tcp:127.0.0.1:8090
+
/* port to listen to
* - can be specified more than once if needed to listen on many ports */
port=5060
@@ -133,6 +141,8 @@
# - a bit higher than registration expires to cope with UA behind NAT
tcp_connection_lifetime=3605
+tcp_accept_no_cl=yes
+
####### Modules Section ########
# set paths to location of modules (to sources or installation folders)
@@ -159,12 +169,15 @@
loadmodule "registrar.so"
loadmodule "textops.so"
loadmodule "siputils.so"
+loadmodule "utils.so"
loadmodule "xlog.so"
loadmodule "sanity.so"
loadmodule "ctl.so"
loadmodule "cfg_rpc.so"
loadmodule "mi_rpc.so"
loadmodule "acc.so"
+#loadmodule "sdpops.so"
+loadmodule "textopsx.so"
#!ifdef WITH_AUTH
loadmodule "auth.so"
@@ -187,6 +200,8 @@
loadmodule "debugger.so"
#!endif
+loadmodule "xhttp.so"
+
# ----------------- setting module-specific parameters ---------------
@@ -338,6 +353,12 @@
# authentication
route(AUTH);
+ # hack for stripping out non-SAVPF media lines from Jitsi
+ #sdp_remove_transport("UDP/TLS/RTP/SAVP");
+ #if (msg_apply_changes()) {
+ # xlog("successfully applied new body");
+ #}
+
# record routing for dialog forming requests (in case they are routed)
# - remove preloaded route headers
remove_hf("Route");
@@ -363,8 +384,11 @@
exit;
}
+ # dispatch requests to Matrix
+ route(RELAY);
+
# user location service
- route(LOCATION);
+ #route(LOCATION);
}
@@ -381,9 +405,34 @@
if(!t_is_set("failure_route")) t_on_failure("MANAGE_FAILURE");
}
- if (!t_relay()) {
- sl_reply_error();
+ # relay to Matrix rather than to SIP
+
+# if (!t_relay()) {
+# sl_reply_error();
+# }
+
+ if (is_method("INVITE")) {
+
+ if (t_newtran()) {
+ xlog("new tran\n");
+ }
+
+ xlog("sdp before transform: $rb\n");
+
+ $var(crlf) = "\r\n";
+ $var(escaped_crlf) = "\\r\\n";
+ $var(offer) = $(rb{s.replace,$var(crlf),$var(escaped_crlf)});
+
+ xlog("escaped sdp offer: $var(offer)\n");
+
+ http_query("http://localhost:8008/_matrix/client/api/v1/rooms/!gkuMZwqNMPQyCVqCqT:echo-matrix/send/m.call.invite?access_token=kamailio", \
+ "{ \"call_id\": \"$ci\", \"lifetime\": 30000, \"offer\": { \"sdp\" : \"$var(offer)\", \"type\": \"offer\" }, \"version\": 0 }", "$var(result)");
+
+ if (t_suspend()) {
+ xlog("suspend tran\n");
+ }
}
+
exit;
}
@@ -508,7 +557,7 @@
}
-# IP authorization and user uthentication
+# IP authorization and user authentication
route[AUTH] {
#!ifdef WITH_AUTH
@@ -521,10 +570,10 @@
if (is_method("REGISTER") || from_uri==myself) {
# authenticate requests
- if (!auth_check("$fd", "subscriber", "1")) {
- auth_challenge("$fd", "0");
- exit;
- }
+# if (!auth_check("$fd", "subscriber", "1")) {
+# auth_challenge("$fd", "0");
+# exit;
+# }
# user authenticated - remove auth header
if(!is_method("REGISTER|PUBLISH"))
consume_credentials();
@@ -628,3 +677,86 @@
exit;
}
}
+
+# Matrix integration:
+# Matrix->SIP call (must be offerful)
+#
+# -> INVITE
+# <- 100
+# create room inviting other party (or reuse existing one if it available)
+# -> m.call.invite
+# <- m.call.answer
+# <- 200 OK
+# -> ACK
+#
+# -> BYE
+# m.call.hangup
+# <- 200 OK
+#
+# or
+#
+# <- m.call.hangup
+# -> BYE
+# <- 200 OK
+#
+# -------------------------
+#
+# SIP->Matrix call
+#
+# -> Either or remote Matrix user already has a room which the AS is participating in, or they create a room and invite in user, causing a query on the user's existence. Then:
+# -> m.call.invite
+# -> INVITE
+# <- 200
+# <- m.call.answer
+# -> ACK
+#
+# ...and then BYE as before.
+#
+# -------------------------
+#
+# XXX: need a way to properly map SIP error codes
+# XXX: need a way to handle offerless invites
+# XXX: need a way to handle trickle ICE
+
+# Handling HTTP events
+event_route[xhttp:request] {
+ $var(xhttp_root) = $(hu{s.substr,0,14});
+ if ($var(xhttp_root) == "/transactions/") {
+ xlog("Received Matrix transaction $si:$sp: $rm $hu: $rb\n");
+
+ # TODO: handle Matrix->SIP calls
+ # TODO: make this work at all...
+
+ # we could match the call either by room (and thus to sender and then dialog)
+ # or via call_id (and thus to dialog). We do the latter.
+
+ # if message is m.call.answer, then respond to dialog
+ if (search_body("\"type\": \"m.call.answer\"")) {
+ # TODO: use json.so rather than regexps
+ # XXX: untested
+
+ $var(sdp) = $(rb{re.subst,/^.*"sdp": "(.*)".*$/\1/s});
+ # TODO: unescape \r\n's
+ xlog("extracted sdp: $var(sdp)");
+
+ $var(call_id) = $(rb{re.subst,/^.*"call_id": "(.*)".*$/\1/s});
+ xlog("extracted call_id: $var(call_id)");
+
+ t_reply_callid("$var(call_id)", "1", "200", "OK");
+ }
+
+ # if message is m.call.hangup, then terminate dialog - either CANCEL or BYE
+ if (search_body("\"type\": \"m.call.hangup\"")) {
+ # XXX: untested
+
+ $var(call_id) = $(rb{re.subst,/^.*"call_id": "(.*)".*$/\1/s});
+ xlog("extracted call_id: $var(call_id)");
+
+ # need to somehow synthesise a CANCEL or BYE for this call
+ # dlg_bye("caller");
+ }
+ }
+ xhttp_reply("200", "OK", "application/json",
+ "{}");
+}
+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment