ftth_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 and TCP 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 ftth_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 ftth_rsipstack::dialog::dialog_layer::DialogLayer;
74//! use ftth_rsipstack::dialog::invitation::InviteOption;
75//! use ftth_rsipstack::transaction::endpoint::EndpointInner;
76//! use std::sync::Arc;
77//!
78//! # async fn example() -> ftth_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//!     destination: None,
92//!     offer: Some(sdp_body),
93//!     credential: None,
94//!     headers: None,
95//! };
96//!
97//! let (dialog, response) = dialog_layer.do_invite(invite_option, state_sender).await?;
98//! # Ok(())
99//! # }
100//! ```
101//!
102//! ## Core Components
103//!
104//! ### Transport Layer
105//!
106//! The transport layer handles network communication across different protocols:
107//!
108//! * [`SipConnection`](transport::SipConnection) - Abstraction over transport protocols
109//! * [`SipAddr`](transport::SipAddr) - SIP addressing with transport information
110//! * [`TransportLayer`](transport::TransportLayer) - Transport management
111//!
112//! ### Transaction Layer
113//!
114//! The transaction layer provides reliable message delivery:
115//!
116//! * [`Transaction`](transaction::transaction::Transaction) - SIP transaction implementation
117//! * [`Endpoint`](transaction::Endpoint) - SIP endpoint for transaction management
118//! * [`TransactionState`](transaction::TransactionState) - Transaction state machine
119//!
120//! ### Dialog Layer
121//!
122//! The dialog layer manages SIP dialogs and sessions:
123//!
124//! * [`Dialog`](dialog::dialog::Dialog) - SIP dialog representation
125//! * [`DialogId`](dialog::DialogId) - Dialog identification
126//! * [`DialogState`](dialog::dialog::DialogState) - Dialog state management
127//!
128//! ## Error Handling
129//!
130//! The stack uses a comprehensive error type that covers all layers:
131//!
132//! ```rust
133//! use ftth_rsipstack::{Result, Error};
134//!
135//! fn handle_sip_error(error: Error) {
136//!     match error {
137//!         Error::TransportLayerError(msg, addr) => {
138//!             eprintln!("Transport error at {msg}: {addr}");
139//!         },
140//!         Error::TransactionError(msg, key) => {
141//!             eprintln!("Transaction error {msg}: {key}");
142//!         },
143//!         Error::DialogError(msg, id, code) => {
144//!             eprintln!("Dialog error {msg}: {id} (Status code: {code})");
145//!         },
146//!         _ => eprintln!("Other error: {}", error),
147//!     }
148//! }
149//! ```
150//!
151//! ## Configuration
152//!
153//! The stack can be configured for different use cases:
154//!
155//! ### Basic UDP Server
156//!
157//! ```rust,no_run
158//! use ftth_rsipstack::EndpointBuilder;
159//! use ftth_rsipstack::transport::{TransportLayer, udp::UdpConnection};
160//! use tokio_util::sync::CancellationToken;
161//!
162//! # async fn example() -> ftth_rsipstack::Result<()> {
163//! # let cancel_token = CancellationToken::new();
164//! let transport_layer = TransportLayer::new(cancel_token.child_token());
165//! let udp_conn = UdpConnection::create_connection("0.0.0.0:5060".parse()?, None, Some(cancel_token.child_token())).await?;
166//! transport_layer.add_transport(udp_conn.into());
167//!
168//! let endpoint = EndpointBuilder::new()
169//!     .with_transport_layer(transport_layer)
170//!     .build();
171//! # Ok(())
172//! # }
173//! ```
174//!
175//! ## Standards Compliance
176//!
177//! RSIPStack implements the following RFCs:
178//!
179//! * **RFC 3261** - SIP: Session Initiation Protocol (core specification)
180//! * **RFC 3581** - Symmetric Response Routing (rport)
181//! * **RFC 6026** - Correct Transaction Handling for 2xx Responses to INVITE
182//!
183//! ## Performance
184//!
185//! The stack is designed for high performance:
186//!
187//! * **Zero-copy parsing** where possible
188//! * **Async I/O** with Tokio for scalability
189//! * **Efficient timer management** for large numbers of transactions
190//! * **Memory-safe** with Rust's ownership system
191//!
192//! ## Testing
193//!
194//! Comprehensive test suite covering:
195//!
196//! * Unit tests for all components
197//! * Integration tests for protocol compliance
198//! * Performance benchmarks
199//! * Interoperability testing
200//!
201//! ## Examples
202//!
203//! See the `examples/` directory for complete working examples:
204//!
205//! * Simple SIP client
206//! * SIP proxy server
207//! * Load testing tools
208
209pub use ftth_rsip as rsip;
210pub type Result<T> = std::result::Result<T, crate::error::Error>;
211pub use crate::error::Error;
212pub mod dialog;
213pub mod error;
214pub mod transaction;
215pub mod transport;
216pub use transaction::EndpointBuilder;
217pub mod rsip_ext;
218
219pub const VERSION: &str = concat!("rsipstack/", env!("CARGO_PKG_VERSION"));