Skip to main content

rsipstack/
lib.rs

1// A SIP stack in Rust
2
3#![allow(
4    clippy::result_large_err,
5    clippy::too_many_arguments,
6    clippy::module_inception,
7    clippy::wrong_self_convention,
8    clippy::large_enum_variant
9)]
10// The legacy ServerInviteDialog / ClientInviteDialog wrappers are retained
11// (deprecated) only for compatibility; internal code and the From/TryFrom
12// conversions keep using them during the transition to InviteDialog.
13#![allow(deprecated)]
14
15//! # RSIPStack - A SIP Stack Implementation in Rust
16//!
17//! RSIPStack is a comprehensive Session Initiation Protocol (SIP) implementation
18//! written in Rust. It provides a complete SIP stack with support for multiple
19//! transport protocols, transaction management, dialog handling, and more.
20//!
21//! ## Features
22//!
23//! * **Complete SIP Implementation** - Full RFC 3261 compliance
24//! * **Multiple Transports** - UDP, TCP, TLS, WebSocket support
25//! * **Transaction Layer** - Automatic retransmissions and timer management
26//! * **Dialog Management** - Full dialog state machine implementation
27//! * **Async/Await Support** - Built on Tokio for high performance
28//! * **Type Safety** - Leverages Rust's type system for protocol correctness
29//! * **Extensible** - Modular design for easy customization
30//!
31//! ## Architecture
32//!
33//! The stack is organized into several layers following the SIP specification:
34//!
35//! ```text
36//! ┌─────────────────────────────────────┐
37//! │           Application Layer         │
38//! ├─────────────────────────────────────┤
39//! │           Dialog Layer              │
40//! ├─────────────────────────────────────┤
41//! │         Transaction Layer           │
42//! ├─────────────────────────────────────┤
43//! │          Transport Layer            │
44//! └─────────────────────────────────────┘
45//! ```
46//!
47//! ## Quick Start
48//!
49//! ### Creating a SIP Endpoint
50//!
51//! ```rust,no_run
52//! use rsipstack::EndpointBuilder;
53//! use tokio_util::sync::CancellationToken;
54//!
55//! #[tokio::main]
56//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
57//!     // Create a SIP endpoint
58//!     let endpoint = EndpointBuilder::new()
59//!         .with_user_agent("MyApp/1.0")
60//!         .build();
61//!
62//!     // Get incoming transactions
63//!     let mut incoming = endpoint.incoming_transactions().expect("incoming_transactions");
64//!
65//!     // Start the endpoint (in production, you'd run this in a separate task)
66//!     // let endpoint_inner = endpoint.inner.clone();
67//!     // tokio::spawn(async move {
68//!     //     endpoint_inner.serve().await.ok();
69//!     // });
70//!
71//!     // Process incoming requests
72//!     while let Some(transaction) = incoming.recv().await {
73//!         // Handle the transaction
74//!         println!("Received: {}", transaction.original.method);
75//!         break; // Exit for example
76//!     }
77//!
78//!     Ok(())
79//! }
80//! ```
81//!
82//! ### Sending SIP Requests
83//!
84//! ```rust,no_run
85//! use rsipstack::dialog::dialog_layer::DialogLayer;
86//! use rsipstack::dialog::invitation::InviteOption;
87//! use rsipstack::transaction::endpoint::EndpointInner;
88//! use std::sync::Arc;
89//!
90//! # async fn example() -> rsipstack::Result<()> {
91//! # let endpoint: Arc<EndpointInner> = todo!();
92//! # let state_sender = todo!();
93//! # let sdp_body = vec![];
94//! // Create a dialog layer
95//! let dialog_layer = DialogLayer::new(endpoint.clone());
96//!
97//! // Send an INVITE
98//! let invite_option = InviteOption {
99//!     caller: rsipstack::sip::Uri::try_from("sip:alice@example.com")?,
100//!     callee: rsipstack::sip::Uri::try_from("sip:bob@example.com")?,
101//!     contact: rsipstack::sip::Uri::try_from("sip:alice@myhost.com:5060")?,
102//!     content_type: Some("application/sdp".to_string()),
103//!     offer: Some(sdp_body),
104//!     ..Default::default()
105//! };
106//!
107//! let (dialog, response) = dialog_layer.do_invite(invite_option, state_sender).await?;
108//! # Ok(())
109//! # }
110//! ```
111//!
112//! ## Core Components
113//!
114//! ### Transport Layer
115//!
116//! The transport layer handles network communication across different protocols:
117//!
118//! * [`SipConnection`](transport::SipConnection) - Abstraction over transport protocols
119//! * [`SipAddr`](transport::SipAddr) - SIP addressing with transport information
120//! * [`TransportLayer`](transport::TransportLayer) - Transport management
121//!
122//! ### Transaction Layer
123//!
124//! The transaction layer provides reliable message delivery:
125//!
126//! * [`Transaction`](transaction::transaction::Transaction) - SIP transaction implementation
127//! * [`Endpoint`](transaction::Endpoint) - SIP endpoint for transaction management
128//! * [`TransactionState`](transaction::TransactionState) - Transaction state machine
129//!
130//! ### Dialog Layer
131//!
132//! The dialog layer manages SIP dialogs and sessions:
133//!
134//! * [`Dialog`](dialog::dialog::Dialog) - SIP dialog representation
135//! * [`DialogId`](dialog::DialogId) - Dialog identification
136//! * [`DialogState`](dialog::dialog::DialogState) - Dialog state management
137//!
138//! ## Error Handling
139//!
140//! The stack uses a comprehensive error type that covers all layers:
141//!
142//! ```rust
143//! use rsipstack::{Result, Error};
144//!
145//! fn handle_sip_error(error: Error) {
146//!     match error {
147//!         Error::TransportLayerError(msg, addr) => {
148//!             eprintln!("Transport error at {msg}: {addr}");
149//!         },
150//!         Error::TransactionError(msg, key) => {
151//!             eprintln!("Transaction error {msg}: {key}");
152//!         },
153//!         Error::DialogError(msg, id, code) => {
154//!             eprintln!("Dialog error {msg}: {id} (Status code: {code})");
155//!         },
156//!         _ => eprintln!("Other error: {}", error),
157//!     }
158//! }
159//! ```
160//!
161//! ## Configuration
162//!
163//! The stack can be configured for different use cases:
164//!
165//! ### Basic UDP Server
166//!
167//! ```rust,no_run
168//! use rsipstack::EndpointBuilder;
169//! use rsipstack::transport::{TransportLayer, udp::UdpConnection};
170//! use tokio_util::sync::CancellationToken;
171//!
172//! # async fn example() -> rsipstack::Result<()> {
173//! # let cancel_token = CancellationToken::new();
174//! let transport_layer = TransportLayer::new(cancel_token.child_token());
175//! let udp_conn = UdpConnection::create_connection("0.0.0.0:5060".parse()?, None, Some(cancel_token.child_token())).await?;
176//! transport_layer.add_transport(udp_conn.into());
177//!
178//! let endpoint = EndpointBuilder::new()
179//!     .with_transport_layer(transport_layer)
180//!     .build();
181//! # Ok(())
182//! # }
183//! ```
184//!
185//! ### Secure TLS Server
186//!
187//! ```rust,no_run
188//! #[cfg(feature = "rustls")]
189//! use rsipstack::transport::tls::{TlsConnection, TlsConfig};
190//! use rsipstack::transport::TransportLayer;
191//!
192//! # async fn example() -> rsipstack::Result<()> {
193//! # let cert_pem = vec![];
194//! # let key_pem = vec![];
195//! # let transport_layer: TransportLayer = todo!();
196//! // Configure TLS transport
197//! let tls_config = TlsConfig {
198//!     cert: Some(cert_pem),
199//!     key: Some(key_pem),
200//!     ..Default::default()
201//! };
202//!
203//! // TLS connections would be created using the TLS configuration
204//! // let tls_conn = TlsConnection::serve_listener(...).await?;
205//! # Ok(())
206//! # }
207//! ```
208//!
209//! ## Standards Compliance
210//!
211//! RSIPStack implements the following RFCs:
212//!
213//! * **RFC 3261** - SIP: Session Initiation Protocol (core specification)
214//! * **RFC 3581** - Symmetric Response Routing (rport)
215//! * **RFC 6026** - Correct Transaction Handling for 2xx Responses to INVITE
216//!
217//! ## Performance
218//!
219//! The stack is designed for high performance:
220//!
221//! * **Zero-copy parsing** where possible
222//! * **Async I/O** with Tokio for scalability
223//! * **Efficient timer management** for large numbers of transactions
224//! * **Memory-safe** with Rust's ownership system
225//!
226//! ## Testing
227//!
228//! Comprehensive test suite covering:
229//!
230//! * Unit tests for all components
231//! * Integration tests for protocol compliance
232//! * Performance benchmarks
233//! * Interoperability testing
234//!
235//! ## Examples
236//!
237//! See the `examples/` directory for complete working examples:
238//!
239//! * Simple SIP client
240//! * SIP proxy server
241//! * WebSocket SIP gateway
242//! * Load testing tools
243
244pub type Result<T> = std::result::Result<T, crate::error::Error>;
245pub use crate::error::Error;
246pub mod dialog;
247pub mod error;
248pub mod resolver;
249pub mod transaction;
250pub mod transport;
251pub use transaction::EndpointBuilder;
252pub mod sip;
253pub use sip as rsip;
254
255pub const VERSION: &str = concat!("rsipstack/", env!("CARGO_PKG_VERSION"));