From 7bbed87c271dc334da4642a58f0048b90cfd3051 Mon Sep 17 00:00:00 2001 From: Sam Willcocks Date: Wed, 6 Jul 2022 01:29:27 +0100 Subject: [PATCH] Add initial SIP UAS. It's capable of responding to calls and hanging up after a few seconds. Very rude. --- .envrc | 1 + .gitignore | 1 + Cargo.lock | 890 ++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 16 + README.md | 18 +- flake.lock | 86 ++++- flake.nix | 47 ++- pjsip/default.nix | 3 +- src/main.rs | 296 +++++++++++++++ 9 files changed, 1345 insertions(+), 13 deletions(-) create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.envrc b/.envrc index 3550a30..b78e30e 100644 --- a/.envrc +++ b/.envrc @@ -1 +1,2 @@ use flake +export RUST_LOG=info diff --git a/.gitignore b/.gitignore index 92b2793..77adcb1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .direnv +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..185928c --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,890 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" +dependencies = [ + "memchr", +] + +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi", +] + +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "block-buffer" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" +dependencies = [ + "generic-array", +] + +[[package]] +name = "bstr" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223" +dependencies = [ + "lazy_static", + "memchr", + "regex-automata", +] + +[[package]] +name = "bytes" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "chrono" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" +dependencies = [ + "libc", + "num-integer", + "num-traits", + "time", + "winapi", +] + +[[package]] +name = "cpufeatures" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59a6001667ab124aebae2a495118e11d30984c3a653e99d86d58971708cf5e4b" +dependencies = [ + "libc", +] + +[[package]] +name = "darling" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a01d95850c592940db9b8194bc39f4bc0e89dee5c4265e4b1807c34a9aba453c" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "859d65a907b6852c9361e3185c862aae7fafd2887876799fa55f5f99dc40d610" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c972679f83bdf9c42bd905396b6c3588a843a17f0f16dfcfa3e2c5d57441835" +dependencies = [ + "darling_core", + "quote", + "syn", +] + +[[package]] +name = "digest" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +dependencies = [ + "generic-array", +] + +[[package]] +name = "discortp" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "524b9439c09174aede2c88d58cfc6b83575b06569d1af4d07562f76595b2896b" +dependencies = [ + "pnet_macros", + "pnet_macros_support", +] + +[[package]] +name = "discosip" +version = "0.1.0" +dependencies = [ + "bytes", + "discortp", + "futures", + "rsip", + "sdp-rs", + "tokio", + "tokio-stream", + "tokio-util", + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "futures" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f73fe65f54d1e12b726f517d3e2135ca3125a437b6d998caf1962961f7172d9e" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3083ce4b914124575708913bca19bfe887522d6e2e6d0952943f5eac4a74010" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c09fd04b7e4073ac7156a9539b57a484a8ea920f79c7c675d05d289ab6110d3" + +[[package]] +name = "futures-executor" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9420b90cfa29e327d0429f19be13e7ddb68fa1cccb09d65e5706b8c7a749b8a6" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc4045962a5a5e935ee2fdedaa4e08284547402885ab326734432bed5d12966b" + +[[package]] +name = "futures-macro" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33c1e13800337f4d4d7a316bf45a567dbcb6ffe087f16424852d97e97a91f512" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21163e139fa306126e6eedaf49ecdb4588f939600f0b1e770f4205ee4b7fa868" + +[[package]] +name = "futures-task" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c66a976bf5909d801bbef33416c41372779507e7a6b3a5e25e4749c58f776a" + +[[package]] +name = "futures-util" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8b7abd5d659d9b90c8cba917f6ec750a74e2dc23902ef9cd4cc8c8b22e6036a" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "generic-array" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd48d33ec7f05fbfa152300fdad764757cbded343c1aa1cff2fbaf4134851803" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.11.0+wasi-snapshot-preview1", +] + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" + +[[package]] +name = "lock_api" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327fa5b6a6940e4699ec49a9beae1ea4845c6bab9314e4f84ac68742139d8c53" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "md-5" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b5a279bb9607f9f53c22d496eade00d138d1bdcccd07d74650387cf94942a15" +dependencies = [ + "block-buffer", + "digest", + "opaque-debug", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "mio" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57ee1c23c7c63b0c9250c339ffdc69255f110b298b901b9f6c82547b7b87caaf" +dependencies = [ + "libc", + "log", + "wasi 0.11.0+wasi-snapshot-preview1", + "windows-sys", +] + +[[package]] +name = "no-std-net" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43794a0ace135be66a25d3ae77d41b91615fb68ae937f904090203e81f755b65" + +[[package]] +name = "nom" +version = "7.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8903e5a29a317527874d0402f867152a3d21c908bb0b933e416c65e301d4c36" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + +[[package]] +name = "num_cpus" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "once_cell" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7709cef83f0c1f58f666e746a08b21e0085f7440fa6a29cc194d68aac97a4225" + +[[package]] +name = "opaque-debug" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" + +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09a279cbf25cb0757810394fbc1e359949b59e348145c643a939a525692e6929" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-sys", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "pnet_base" +version = "0.31.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9d3a993d49e5fd5d4d854d6999d4addca1f72d86c65adf224a36757161c02b6" +dependencies = [ + "no-std-net", +] + +[[package]] +name = "pnet_macros" +version = "0.31.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48dd52a5211fac27e7acb14cfc9f30ae16ae0e956b7b779c8214c74559cef4c3" +dependencies = [ + "proc-macro2", + "quote", + "regex", + "syn", +] + +[[package]] +name = "pnet_macros_support" +version = "0.31.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89de095dc7739349559913aed1ef6a11e73ceade4897dadc77c5e09de6740750" +dependencies = [ + "pnet_base", +] + +[[package]] +name = "proc-macro2" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd96a1e8ed2596c337f8eae5f24924ec83f5ad5ab21ea8e455d3566c69fbcaf7" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bcdf212e9776fbcb2d23ab029360416bb1706b1aea2d1a5ba002727cbcab804" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "redox_syscall" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62f25bc4c7e55e0b0b7a1d43fb893f4fa1361d0abe38b9ce4f323c2adfe6ef42" +dependencies = [ + "bitflags", +] + +[[package]] +name = "regex" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" + +[[package]] +name = "regex-syntax" +version = "0.6.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244" + +[[package]] +name = "rsip" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1690d2cc1b13c9ff67f48f59bd2b0bb9bb57e157b5cce2d0b27ccf8e11964c3" +dependencies = [ + "bstr", + "bytes", + "md-5", + "nom", + "rsip-derives", + "sha2", + "uuid", +] + +[[package]] +name = "rsip-derives" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c16c459d744ca5815a23876812dc1655db9ce25bffabc01962967848487eea" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + +[[package]] +name = "sdp-rs" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0663d1b6270f516dd0c5a9691de1c6b768b045f4094bd0a46887b8153b755a89" +dependencies = [ + "chrono", + "nom", + "vec1", +] + +[[package]] +name = "sha2" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d58a1e1bf39749807d89cf2d98ac2dfa0ff1cb3faa38fbb64dd88ac8013d800" +dependencies = [ + "block-buffer", + "cfg-if", + "cpufeatures", + "digest", + "opaque-debug", +] + +[[package]] +name = "sharded-slab" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0" +dependencies = [ + "libc", +] + +[[package]] +name = "slab" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb703cfe953bccee95685111adeedb76fabe4e97549a58d16f03ea7b9367bb32" + +[[package]] +name = "smallvec" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1" + +[[package]] +name = "socket2" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66d72b759436ae32898a2af0a14218dbf55efde3feeb170eb623637db85ee1e0" +dependencies = [ + "libc", + "winapi", +] + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "syn" +version = "1.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c50aef8a904de4c23c788f104b7dddc7d6f79c647c7c8ce4cc8f73eb0ca773dd" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thread_local" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180" +dependencies = [ + "once_cell", +] + +[[package]] +name = "time" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +dependencies = [ + "libc", + "wasi 0.10.0+wasi-snapshot-preview1", + "winapi", +] + +[[package]] +name = "tokio" +version = "1.19.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c51a52ed6686dd62c320f9b89299e9dfb46f730c7a48e635c19f21d116cb1439" +dependencies = [ + "bytes", + "libc", + "memchr", + "mio", + "num_cpus", + "once_cell", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "winapi", +] + +[[package]] +name = "tokio-macros" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9724f9a975fb987ef7a3cd9be0350edcbe130698af5b8f7a631e23d42d052484" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-stream" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df54d54117d6fdc4e4fea40fe1e4e566b3505700e148a6827e59b34b0d2600d9" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tokio-util" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc463cd8deddc3770d20f9852143d50bf6094e640b485cb2e189a2099085ff45" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", + "tracing", +] + +[[package]] +name = "tracing" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a400e31aa60b9d44a52a8ee0343b5b18566b03a8321e0d321f695cf56e940160" +dependencies = [ + "cfg-if", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11c75893af559bc8e10716548bdef5cb2b983f8e637db9d0e15126b61b484ee2" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b7358be39f2f274f322d2aaed611acc57f382e8eb1e5b48cb9ae30933495ce7" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922" +dependencies = [ + "lazy_static", + "log", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a713421342a5a666b7577783721d3117f1b69a393df803ee17bb73b1e122a59" +dependencies = [ + "ansi_term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", +] + +[[package]] +name = "typenum" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" + +[[package]] +name = "unicode-ident" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bd2fe26506023ed7b5e1e315add59d6f584c621d037f9368fea9cfb988f368c" + +[[package]] +name = "uuid" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + +[[package]] +name = "vec1" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fc1631c774f0f9570797191e01247cbefde789eebfbf128074cb934115a6133" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2" +dependencies = [ + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" + +[[package]] +name = "windows_i686_gnu" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" + +[[package]] +name = "windows_i686_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c18ea31 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "discosip" +version = "0.1.0" +edition = "2021" + +[dependencies] +bytes = "1.1.0" +discortp = "0.5.0" +futures = "0.3.21" +rsip = "0.4.0" +sdp-rs = "0.2.1" +tokio = { version = "1.19.2", features = ["full"] } +tokio-stream = "0.1.9" +tokio-util = { version = "0.7.3", features = ["net", "codec"] } +tracing = "0.1.35" +tracing-subscriber = "0.3.14" diff --git a/README.md b/README.md index dc96f1f..c6784b7 100644 --- a/README.md +++ b/README.md @@ -1 +1,17 @@ -This is discosip, PRJ16, an attempt to enable use of discord from a SIP phone. +# DiscoSIP + + +
+An attempt to enable use of discord from a SIP phone. + +```mermaid +flowchart LR; + ipphone(IP Phone/SIP softphone) + discosip(DiscoSIP) + discord(Discord Voice) + style discosip stroke:orange,stroke-width:3px + ipphone<-->|SIP|discosip + ipphone<-->|RTP|discosip + discosip<-->|Websocket|discord + discosip<-->|RTP, encrypted|discord +``` diff --git a/flake.lock b/flake.lock index a3bb57d..7475083 100644 --- a/flake.lock +++ b/flake.lock @@ -34,6 +34,39 @@ "type": "github" } }, + "flake-utils_2": { + "locked": { + "lastModified": 1656065134, + "narHash": "sha256-oc6E6ByIw3oJaIyc67maaFcnjYOz1mMcOtHxbEf9NwQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "bee6a7250dd1b01844a2de7e02e4df7d8a0a206c", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "naersk": { + "inputs": { + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1655042882, + "narHash": "sha256-9BX8Fuez5YJlN7cdPO63InoyBy7dm3VlJkkmTt6fS1A=", + "owner": "nix-community", + "repo": "naersk", + "rev": "cddffb5aa211f50c4b8750adbec0bbbdfb26bb9f", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "naersk", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1643381941, @@ -64,13 +97,64 @@ "type": "indirect" } }, + "nixpkgs_3": { + "locked": { + "lastModified": 1656490020, + "narHash": "sha256-xonV1ITvAtwtoM58Iaz77mrkkj3NQKvP2QOPYZNiNvs=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "23488b5815ef60b3084a874f71fdae2dff52e1f7", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "nixpkgs_4": { + "locked": { + "lastModified": 1656401090, + "narHash": "sha256-bUS2nfQsvTQW2z8SK7oEFSElbmoBahOPtbXPm0AL3I4=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "16de63fcc54e88b9a106a603038dd5dd2feb21eb", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, "root": { "inputs": { "devshell": "devshell", - "nixpkgs": "nixpkgs_2", + "naersk": "naersk", + "nixpkgs": "nixpkgs_3", + "rust-overlay": "rust-overlay", "utils": "utils" } }, + "rust-overlay": { + "inputs": { + "flake-utils": "flake-utils_2", + "nixpkgs": "nixpkgs_4" + }, + "locked": { + "lastModified": 1656644123, + "narHash": "sha256-xg7D9aYBdmzH08MH6DlEOJW1UR5DYaQkvvvhnuXEatc=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "8b4c5bef319198920fd03a916dd5f6600147358b", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, "utils": { "locked": { "lastModified": 1656065134, diff --git a/flake.nix b/flake.nix index 9a9fc3e..a991537 100644 --- a/flake.nix +++ b/flake.nix @@ -1,28 +1,55 @@ { - description = "A basic flake from samw"; + description = "SIP Discord interconnect"; + inputs = { utils.url = "github:numtide/flake-utils"; devshell.url = "github:numtide/devshell"; + naersk.url = "github:nix-community/naersk"; + rust-overlay.url = "github:oxalica/rust-overlay"; }; + outputs = { self, nixpkgs, utils, + naersk, devshell, + rust-overlay, }: utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs { inherit system; + overlays = [(import rust-overlay)]; + }; + rust = pkgs.rust-bin.stable.latest.default; + # Override naersk to use our chosen rust version from rust-overlay + naersk-lib = naersk.lib.${system}.override { + cargo = rust; + rustc = rust; + }; + in rec { + packages.default = naersk-lib.buildPackage { + pname = "discosip"; + root = ./.; + }; - overlays = [devshell.overlay]; - }; - in { - devShells.default = pkgs.devshell.mkShell { - packages = with pkgs; [ - ffmpeg - (callPackage ./pjsip {inherit (darwin.apple_sdk.frameworks) AppKit;}) - ]; - }; + apps.default = utils.lib.mkApp {drv = packages.default;}; + + # Provide a dev env with rust and rls + devShells.default = let + pkgs = import nixpkgs { + inherit system; + overlays = [devshell.overlay]; + }; + in + pkgs.devshell.mkShell { + packages = with pkgs; [ + (rust.override {extensions = ["rls"];}) + ffmpeg + (callPackage ./pjsip {inherit (darwin.apple_sdk.frameworks) AppKit;}) + opusTools + ]; + }; formatter = pkgs.alejandra; }); } diff --git a/pjsip/default.nix b/pjsip/default.nix index 4d5141e..ac4fb93 100644 --- a/pjsip/default.nix +++ b/pjsip/default.nix @@ -7,6 +7,7 @@ alsa-lib, AppKit, fetchpatch, + libopus, }: stdenv.mkDerivation rec { pname = "pjsip"; @@ -24,7 +25,7 @@ stdenv.mkDerivation rec { ]; buildInputs = - [openssl libsamplerate] + [openssl libsamplerate libopus] ++ lib.optional stdenv.isLinux alsa-lib ++ lib.optional stdenv.isDarwin AppKit; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..ff53310 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,296 @@ +use bytes::{Bytes, BytesMut}; +use futures::{sink::Sink, SinkExt}; +use rsip::common::method::Method as SipMethod; +use rsip::headers::header::Header as SipHeader; +use rsip::message::{request::Request, response::Response, SipMessage}; +use sdp_rs::lines::media::{MediaType, ProtoType}; +use sdp_rs::lines::{attribute::Rtpmap, Attribute}; +use sdp_rs::{MediaDescription, SessionDescription}; +use std::net::SocketAddr; +use std::str; +use std::time::Duration; +use tokio::net::UdpSocket; +use tokio::sync::mpsc; +use tokio::time::sleep; +use tokio_stream::StreamExt; +use tokio_util::{ + codec::{Decoder, Encoder}, + sync::{PollSendError, PollSender}, + udp::UdpFramed, +}; +use tracing::{event, instrument, Level}; + +const SIP_PORT: u16 = 5060; +const BIND_ADDR: &str = "0.0.0.0"; // for now + +struct SipCodec {} + +impl Decoder for SipCodec { + type Item = SipMessage; + type Error = std::io::Error; + + fn decode(&mut self, src: &mut BytesMut) -> Result, Self::Error> { + if src.is_empty() { + Ok(None) + } else { + // We're assuming we get an entire sip message at once here. + match SipMessage::try_from(src.as_ref()) { + Ok(msg) => { + src.clear(); + Ok(Some(msg)) + } + Err(e) => { + src.clear(); // Still clear the buf to be ready for the next packet + event!(Level::ERROR, error = %e, "Error decoding SIP message."); + Ok(None) + } + } + } + } +} + +impl Encoder for SipCodec { + type Error = std::io::Error; + + #[instrument( + level="debug", + skip(self, item, dst) + )] + fn encode(&mut self, item: SipMessage, dst: &mut BytesMut) -> Result<(), Self::Error> { + let stuff: Bytes = item.into(); + dst.reserve(stuff.len()); + dst.extend_from_slice(&stuff); + Ok(()) + } +} + +type StdErr = Result>; + +struct Server {} + +struct CurrentCall { + remote: SocketAddr, + call_id: String, + req_tx: mpsc::Sender, +} + +impl Server { + #[instrument] + async fn run_sip() -> StdErr<()> { + event!(Level::INFO, "Starting..."); + let socket = UdpSocket::bind(format!("{}:{}", BIND_ADDR, SIP_PORT)).await?; + let mut framed = UdpFramed::new(socket, SipCodec {}); + + // State for the current call. + // TODO: be able to handle multiple SIP sessions at once + let mut current_call: Option = None; + // Channel for SIP responses from call handlers + let (response_tx, mut response_rx) = mpsc::channel(10); + loop { + tokio::select! { + // We got a new response to send + Some((res, send_to)) = response_rx.recv() => { + event!(Level::INFO, remote=%send_to, "Sending response"); + framed.send((SipMessage::from(res), send_to)).await?; + } + Some(frame) = framed.next() => { + let (msg, remote) = frame?; + match msg { + SipMessage::Request(req) => { + // TODO: some kind of more general extraction of headers + let call_id = req.headers.iter().filter_map(|h| { + match h { + SipHeader::CallId(id) => Some(id.clone()), + _ => None, + } + }).next().ok_or("No CallId header")?.into(); + event!(Level::INFO, %remote, method=%req.method, %call_id, "Got request"); + match req.method { + SipMethod::Invite => { + // We got a new INVITE. Are we already in a call? + match ¤t_call { + None => { + // We're not in a call already, guess we are now... + event!( Level::INFO, %remote, "Starting call"); + let (req_tx, req_rx) = mpsc::channel(10); + current_call = Some(CurrentCall { remote, call_id, req_tx }); + // Jesus christ. Wrap the response sender to inject the + // remote's address. This means that `handle_call` + // doesn't need to be aware of the remote at all. + let res_tx = response_tx.clone(); + tokio::spawn(async move { + let res_tx = PollSender::new(res_tx).with::<_, _, _, PollSendError<_>>(|res| {futures::future::ready(Ok((res, remote)))}); + Self::handle_call(&req, res_tx, req_rx).await.unwrap(); + }); + } + Some(call) => { + // We are in a call. + if call_id != call.call_id { + // If this INVITE is from someone else, say we're + // busy. + event!( + Level::INFO, + %remote, + "Call in progress already, responding with 486 Busy Here", + ); + let mut res = Response::default(); + res.status_code = 486.into(); + response_tx.send((res, remote)).await?; + continue; + } else { + // If it's from the same call... ignore it and warn + // I guess? + event!( Level::WARN, %remote, %call_id, "Got INVITE for same CallID as the one we're currently in"); + } + + } + } + } + _ => { + if let Some(call) = ¤t_call { + if call.call_id == call_id { + // If this request is for the currently handled callId, + // route it to the call handler. + call.req_tx.send(req).await?; + } + event!(Level::WARN, %remote, %call_id, "Ignoring request with different Call ID to the one we're handling"); + } else { + event!(Level::WARN, %remote, %call_id, method=%req.method, "Not in a call, ignoring non-INVITE request."); + } + } + } + } + SipMessage::Response(_res) => { + event!( Level::INFO, %remote, "Got SIP response"); + } + }; + } + } + } + } + + // Check that the media description is something we can handle, returning the selected format + #[instrument] + fn select_format(md: &MediaDescription) -> StdErr<(Rtpmap, Option<&str>)> { + if md.media.media != MediaType::Audio || md.media.proto != ProtoType::RtpAvp { + return Err("Media is not audio over RTP/AVP".into()); + } else if md.media.num_of_ports != None && md.media.num_of_ports != Some(1) { + return Err(format!("Media has {:?} ports, want 1", md.media.num_of_ports).into()); + } + md.attributes + .iter() + .filter_map(|a| match a { + Attribute::Rtpmap(r) => { + //TODO: likely need to be smarter here in case of multiple variants of opus + if r.encoding_name == "opus" { + let prefix = format!("{} ", r.payload_type); + // Find the matching fmtp if there is one + let fmtp = md.attributes.iter().filter_map(|a| match a{ + Attribute::Other(fmt, Some(params)) if fmt == "fmtp" => { + params.strip_prefix(&prefix) + }, + _ => None + }).next(); + Some((r.clone(), fmtp)) + } else { + None + } + } + _ => None, + }) + .next() + .ok_or("No accepted codecs found".into()) + } + + // Handle a call + #[instrument( + level = "info", + skip(invite, responses, requests) + fields() + )] + async fn handle_call + std::marker::Unpin>( + invite: &Request, + mut responses: T, + mut requests: mpsc::Receiver, + ) -> StdErr<()> { + let mut base_res = Response::default(); + // Copy headers from the invite + base_res.headers.extend( + invite + .headers + .iter() + .filter(|h| { + matches!( + h, + SipHeader::CSeq(_) + | SipHeader::CallId(_) + | SipHeader::From(_) + | SipHeader::To(_) + | SipHeader::Via(_) + ) + }) + .cloned() + .collect(), + ); + // 100 trying... + let mut res = base_res.clone(); + res.status_code = 100.into(); + responses.send(res).await; + + let body = str::from_utf8(invite.body().as_ref())?; + let sdp = SessionDescription::try_from(body)?; + let (md, rtpmap, fmtp) = sdp + .media_descriptions + .iter() + .filter_map(|md| match Self::select_format(md) { + Ok((rtpmap, fmtp)) => Some((md, rtpmap, fmtp)), + Err(e) => { + event!(Level::INFO, why=e, media_description=%md, "Rejected media description."); + None + } + }) + .next() + .ok_or("No supported media descriptions")?; + event!(Level::INFO, media_desc=%md, %rtpmap, ?fmtp, "Selected rtpmap/codec"); + + // Setup RTP. Bind to an os-allocated port + let socket = UdpSocket::bind(format!("{}:0", BIND_ADDR)).await?; + let rtp_port = socket.local_addr()?.port(); + event!(Level::INFO, rtp_port, "Bound RTP port"); + let mut framed = UdpFramed::new(socket, SipCodec {}); + + loop { + tokio::select! { + Some(req) = requests.recv() => { + event!(Level::INFO, ?req, "Got request"); + match req.method { + SipMethod::Bye => { + event!(Level::INFO, "Remote has hung up."); + //TODO: Cancel RTP streams and clean up + break + }, + _ => {event!(Level::WARN, method=%req.method, "Call handler got unimplemented SIP method!");} + } + + }, + _ = sleep(Duration::from_millis(3000)) => { + // Reject call after 3000 seconds + let mut res = base_res.clone(); + res.status_code = 603.into(); + responses.send(res).await; + break + } + } + } + + event!(Level::INFO, "Call handler loop done."); + + Ok(()) + } +} + +#[tokio::main] +async fn main() -> Result<(), Box> { + tracing_subscriber::fmt::init(); + Server::run_sip().await +}