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