rsipstack/
lib.rs

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