Parrot.Sip.Branch (Parrot Platform v0.0.1-alpha.2)
Module for generating and working with SIP branch parameters for Via headers.
The branch parameter is a unique identifier used in the Via header of SIP messages to identify transactions. As specified in RFC 3261, the branch parameter in SIP requests must start with the magic cookie "z9hG4bK" to be compliant with RFC 3261.
The branch parameter is crucial for:
- Transaction identification and matching
- Loop detection in SIP networks
- Stateless processing of SIP messages
References:
- RFC 3261 Section 8.1.1.7: Via
- RFC 3261 Section 17.2.3: Matching Requests to Server Transactions
- RFC 3261 Section 20.40: Via Header Field
Summary
Functions
Ensures a branch parameter is RFC 3261 compliant by adding the magic cookie if it's missing.
Generates a unique branch parameter for a Via header. The branch parameter will start with the RFC 3261 magic cookie "z9hG4bK".
Generates a unique branch parameter for a Via header based on the given SIP message properties for loop detection.
Checks if a branch parameter is RFC 3261 compliant.
Checks if two branch parameters refer to the same transaction.
Extracts the transaction identifier part from a branch parameter. This is the part after the magic cookie.
Types
@type t() :: String.t()
Functions
Ensures a branch parameter is RFC 3261 compliant by adding the magic cookie if it's missing.
Examples
iex> Parrot.Sip.Branch.ensure_rfc3261_compliance("abc123")
"z9hG4bKabc123"
iex> Parrot.Sip.Branch.ensure_rfc3261_compliance("z9hG4bKabc123")
"z9hG4bKabc123"
@spec generate() :: t()
Generates a unique branch parameter for a Via header. The branch parameter will start with the RFC 3261 magic cookie "z9hG4bK".
Examples
iex> branch = Parrot.Sip.Branch.generate()
iex> String.starts_with?(branch, "z9hG4bK")
true
Generates a unique branch parameter for a Via header based on the given SIP message properties for loop detection.
This function creates a deterministic branch parameter based on key message properties, which helps with loop detection as described in RFC 3261 Section 16.6.
Parameters
- method: The SIP method (atom) of the request
- request_uri: The request URI as a string
- from_tag: The tag parameter from the From header
- to_tag: The tag parameter from the To header (may be nil)
- call_id: The Call-ID header value
Examples
iex> Parrot.Sip.Branch.generate_for_message(:invite, "sip:alice@example.com", "123", nil, "abc@example.com")
"z9hG4bK" <> _rest
Checks if a branch parameter is RFC 3261 compliant.
Examples
iex> Parrot.Sip.Branch.is_rfc3261_compliant?("z9hG4bKabc123")
true
iex> Parrot.Sip.Branch.is_rfc3261_compliant?("123456")
false
Checks if two branch parameters refer to the same transaction.
Examples
iex> Parrot.Sip.Branch.same_transaction?("z9hG4bKabc123", "z9hG4bKabc123")
true
iex> Parrot.Sip.Branch.same_transaction?("z9hG4bKabc123", "abc123")
true
iex> Parrot.Sip.Branch.same_transaction?("z9hG4bKabc123", "z9hG4bKdef456")
false
Extracts the transaction identifier part from a branch parameter. This is the part after the magic cookie.
Examples
iex> Parrot.Sip.Branch.transaction_id("z9hG4bKabc123")
"abc123"
iex> Parrot.Sip.Branch.transaction_id("abc123")
"abc123"