diff options
author | metamuffin <metamuffin@disroot.org> | 2024-07-03 23:49:00 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-07-03 23:49:00 +0200 |
commit | 2c1977bbf97653bc3faae9d4cebcfb61c6cd347b (patch) | |
tree | 1e429bea320023b19b1043b204eb5783293190a5 | |
download | sip-rs-2c1977bbf97653bc3faae9d4cebcfb61c6cd347b.tar sip-rs-2c1977bbf97653bc3faae9d4cebcfb61c6cd347b.tar.bz2 sip-rs-2c1977bbf97653bc3faae9d4cebcfb61c6cd347b.tar.zst |
stuff
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Cargo.lock | 383 | ||||
-rw-r--r-- | Cargo.toml | 12 | ||||
-rw-r--r-- | src/headermap.rs | 30 | ||||
-rw-r--r-- | src/headers.rs | 36 | ||||
-rw-r--r-- | src/lib.rs | 7 | ||||
-rw-r--r-- | src/method.rs | 23 | ||||
-rw-r--r-- | src/request.rs | 21 | ||||
-rw-r--r-- | src/response.rs | 7 | ||||
-rw-r--r-- | src/status.rs | 63 | ||||
-rw-r--r-- | src/uri.rs | 12 |
11 files changed, 596 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0c92d86 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/src/bin/* diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..53b83fc --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,383 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "anstream" +version = "0.6.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" + +[[package]] +name = "anstyle-parse" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" +dependencies = [ + "anstyle", + "windows-sys", +] + +[[package]] +name = "anyhow" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "colorchoice" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" + +[[package]] +name = "env_filter" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b35839ba51819680ba087cd351788c9a3c476841207e0b8cee0b04722343b9" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" + +[[package]] +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.9.0-alpha.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d31e63ea85be51c423e52ba8f2e68a3efd53eed30203ee029dd09947333693e" +dependencies = [ + "rand_chacha", + "rand_core", + "zerocopy", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0-alpha.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78674ef918c19451dbd250f8201f8619b494f64c9aa6f3adb28fd8a0f1f6da46" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.9.0-alpha.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc89dffba8377c5ec847d12bb41492bda235dba31a25e8b695cd0fe6589eb8c9" +dependencies = [ + "getrandom", + "zerocopy", +] + +[[package]] +name = "regex" +version = "1.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" + +[[package]] +name = "sip" +version = "0.1.0" +dependencies = [ + "anyhow", + "base64", + "env_logger", + "log", + "nom", + "rand", +] + +[[package]] +name = "syn" +version = "2.0.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "901fa70d88b9d6c98022e23b4136f9f3e54e4662c3bc1bd1d84a42a9a0f0c1e9" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" + +[[package]] +name = "zerocopy" +version = "0.8.0-alpha.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db678a6ee512bd06adf35c35be471cae2f9c82a5aed2b5d15e03628c98bddd57" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.0-alpha.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "201585ea96d37ee69f2ac769925ca57160cef31acb137c16f38b02b76f4c1e62" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..66cf791 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "sip" +version = "0.1.0" +edition = "2021" + +[dependencies] +anyhow = "1.0.86" +log = "0.4.22" +env_logger = "0.11.3" +nom = "7.1.3" +rand = "0.9.0-alpha.1" +base64 = "0.22.1" diff --git a/src/headermap.rs b/src/headermap.rs new file mode 100644 index 0000000..8712df1 --- /dev/null +++ b/src/headermap.rs @@ -0,0 +1,30 @@ +use crate::headers::Header; +use anyhow::Result; +use std::fmt::Display; + +pub struct HeaderMap(pub Vec<(String, String)>); + +impl HeaderMap { + pub fn new() -> Self { + Self(vec![]) + } + pub fn add<H: Header>(mut self, h: H) -> Self { + self.0.push((H::NAME.to_string(), format!("{h}"))); + self + } + pub fn get<H: Header>(&self) -> Option<Result<H>> { + self.0 + .iter() + .find(|(k, _)| k == H::NAME) + .map(|(_, v)| H::from_str(v)) + } +} + +impl Display for HeaderMap { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + for (k, v) in &self.0 { + write!(f, "{k}: {v}\r\n")?; + } + Ok(()) + } +} diff --git a/src/headers.rs b/src/headers.rs new file mode 100644 index 0000000..d837ee5 --- /dev/null +++ b/src/headers.rs @@ -0,0 +1,36 @@ +use std::{fmt::Display, str::FromStr}; + +macro_rules! header { + ($hname:literal, struct $name:ident($type:ty)) => { + pub struct $name(pub $type); + impl Header for $name { + const NAME: &'static str = $hname; + } + impl Display for $name { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } + } + impl FromStr for $name { + type Err = anyhow::Error; + fn from_str(s: &str) -> Result<Self, Self::Err> { + Ok($name(<$type>::from_str(s)?)) + } + } + }; +} + +pub trait Header: FromStr<Err = anyhow::Error> + Display { + const NAME: &'static str; +} + +header!("Content-Length", struct ContentLength(usize)); +header!("Call-ID", struct CallID(String)); +header!("CSeq", struct CSeq(String)); +header!("Via", struct Via(String)); +header!("Contact", struct Contact(String)); +header!("Max-Forwards", struct MaxForwards(usize)); +header!("From", struct From(String)); +header!("To", struct To(String)); +header!("User-Agent", struct UserAgent(String)); +header!("Allow", struct Allow(String)); diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..a7dd227 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,7 @@ +pub mod headermap; +pub mod headers; +pub mod method; +pub mod request; +pub mod response; +pub mod status; +pub mod uri; diff --git a/src/method.rs b/src/method.rs new file mode 100644 index 0000000..5f8110a --- /dev/null +++ b/src/method.rs @@ -0,0 +1,23 @@ +use std::fmt::Display; + +pub enum Method { + Register, + Invite, + Ack, + Option, + Cancel, + Bye, +} + +impl Display for Method { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(match self { + Method::Register => "REGISTER", + Method::Invite => "INVITE", + Method::Ack => "ACK", + Method::Option => "OPTION", + Method::Cancel => "CANCEL", + Method::Bye => "BYE", + }) + } +} diff --git a/src/request.rs b/src/request.rs new file mode 100644 index 0000000..827dae1 --- /dev/null +++ b/src/request.rs @@ -0,0 +1,21 @@ +use crate::{headermap::HeaderMap, method::Method, uri::Uri}; +use std::fmt::Display; + +pub struct Request { + pub method: Method, + pub uri: Uri, + pub headers: HeaderMap, +} + +impl Display for Request { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let Self { + headers, + method, + uri, + } = self; + write!(f, "{method} {uri} SIP/2.0\r\n")?; + write!(f, "{headers}\r\n")?; + Ok(()) + } +} diff --git a/src/response.rs b/src/response.rs new file mode 100644 index 0000000..4403c6e --- /dev/null +++ b/src/response.rs @@ -0,0 +1,7 @@ +use crate::headermap::HeaderMap; + + +pub struct Reponse { + pub code: u16, + pub headers: HeaderMap, +} diff --git a/src/status.rs b/src/status.rs new file mode 100644 index 0000000..de9ea88 --- /dev/null +++ b/src/status.rs @@ -0,0 +1,63 @@ + +macro_rules! status_enum { + ($v:vis enum $name:ident { $($variant:ident = $value:literal),*, }) => { + $v enum $name { $($variant),*, Other(u16) } + impl $name { pub fn from_code(c: u16) -> Self { match c { $($value => Self::$variant),*, x => Self::Other(x) } } } + impl $name { pub fn to_code(&self) -> u16 { match self { $(Self::$variant => $value),*, Self::Other(x) => *x } } } + }; +} + +status_enum!( + pub enum Status { + Trying = 100, + Ringing = 180, + CallIsBeingForwarded = 181, + Queued = 182, + SessionProgress = 183, + Ok = 200, + MultipleChoices = 300, + MovedPermanently = 301, + MovedTemporarily = 302, + UseProxy = 305, + AlternativeService = 380, + BadRequest = 400, + Unauthorized = 401, + PaymentRequired = 402, + Forbidden = 403, + NotFound = 404, + MethodNotAllowed = 405, + NotAcceptable = 406, + ProxyAuthenticationRequired = 407, + RequestTimeout = 408, + Gone = 410, + RequestEntityTooLarge = 413, + RequestURITooLarge = 414, + UnsupportedMediaType = 415, + UnsupportedURIScheme = 416, + BadExtension = 420, + ExtensionRequired = 421, + IntervalTooBrief = 423, + TemporarilyNotAvailable = 480, + CallLegTransactionDoesNotExist = 481, + LoopDetected = 482, + TooManyHops = 483, + AddressIncomplete = 484, + Ambiguous = 485, + BusyHere = 486, + RequestTerminated = 487, + NotAcceptableHere = 488, + RequestPending = 491, + Undecipherable = 493, + InternalServerError = 500, + NotImplemented = 501, + BadGateway = 502, + ServiceUnavailable = 503, + ServerTimeout = 504, + SIPVersionNotSupported = 505, + MessageTooLarge = 513, + BusyEverywhere = 600, + Decline = 603, + DoesNotExistAnywhere = 604, + GlobalNotAcceptable = 606, + } +); diff --git a/src/uri.rs b/src/uri.rs new file mode 100644 index 0000000..2d77df2 --- /dev/null +++ b/src/uri.rs @@ -0,0 +1,12 @@ +use std::fmt::Display; + +pub struct Uri { + pub content: String, +} + +impl Display for Uri { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.content)?; + Ok(()) + } +} |