Asterisk - The Open Source Telephony Project GIT-master-4f2b068
Loading...
Searching...
No Matches
res_pjsip_redirect.h
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2025, Commend International
5 *
6 * Maximilian Fridrich <m.fridrich@commend.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19#ifndef _RES_PJSIP_REDIRECT_H
20#define _RES_PJSIP_REDIRECT_H
21
22#include <pjsip.h>
23
24/*!
25 * \brief Maximum number of redirect hops allowed
26 */
27#define AST_SIP_MAX_REDIRECT_HOPS 5
28
29/*!
30 * \brief Maximum number of redirect contacts to process
31 */
32#define AST_SIP_MAX_REDIRECT_CONTACTS 20
33
34/*!
35 * \brief Opaque structure for redirect state
36 *
37 * This structure encapsulates all state needed for handling
38 * SIP 3xx redirects, including visited URIs for loop detection,
39 * pending contacts for retry logic, and hop counting.
40 */
42
43/*!
44 * \brief Create a new redirect state
45 *
46 * \param endpoint The SIP endpoint
47 * \param initial_uri The initial URI being contacted (for loop detection)
48 *
49 * \retval NULL on failure
50 * \retval A new redirect state on success
51 *
52 * \note The caller must call ast_sip_redirect_state_destroy() when done
53 */
56 const char *initial_uri);
57
58/*!
59 * \brief Check if redirect should be followed based on endpoint configuration
60 *
61 * \param endpoint The SIP endpoint
62 * \param rdata The redirect response data containing the 3xx response
63 *
64 * \retval 0 if redirect should not be followed
65 * \retval 1 if redirect should be followed
66 *
67 * \note This checks if the status code is 3xx and if the SIP method
68 * (extracted from the CSeq header) is allowed to follow redirects
69 * based on the endpoint's follow_redirect_methods configuration
70 */
71int ast_sip_should_redirect(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata);
72
73/*!
74 * \brief Parse a 3xx redirect response and extract contacts
75 *
76 * This function parses all Contact headers from a 3xx response,
77 * extracts q-values, sorts contacts by priority (highest q-value first),
78 * and filters out URIs that would create loops.
79 *
80 * \param rdata The redirect response data
81 * \param state The redirect state
82 *
83 * \retval -1 on failure (hop limit reached, no valid contacts, etc.)
84 * \retval 0 on success (at least one valid contact available)
85 *
86 * \note After calling this, use ast_sip_redirect_get_next_uri() to retrieve URIs
87 */
88int ast_sip_redirect_parse_3xx(pjsip_rx_data *rdata, struct ast_sip_redirect_state *state);
89
90/*!
91 * \brief Get the next redirect URI to try
92 *
93 * This function returns the next contact URI from the redirect response,
94 * ordered by q-value (highest first). It also marks the URI as visited
95 * to prevent loops on subsequent redirects.
96 *
97 * \param state The redirect state
98 * \param uri_out Pointer to store the URI string (caller must free)
99 *
100 * \retval -1 if no more URIs available
101 * \retval 0 on success
102 *
103 * \note The caller must ast_free() the returned URI string
104 */
106
107/*!
108 * \brief Check if a URI would create a redirect loop
109 *
110 * \param state The redirect state
111 * \param uri The URI to check
112 *
113 * \retval 0 if URI is safe (not visited)
114 * \retval 1 if URI would create a loop (already visited)
115 */
116int ast_sip_redirect_check_loop(const struct ast_sip_redirect_state *state, const char *uri);
117
118/*!
119 * \brief Get the current hop count
120 *
121 * \param state The redirect state
122 *
123 * \return The current hop count
124 */
126
127/*!
128 * \brief Get the endpoint from the redirect state
129 *
130 * \param state The redirect state
131 *
132 * \return The endpoint (borrowed reference, do not cleanup)
133 */
135
136/*!
137 * \brief Destroy a redirect state and free all resources
138 *
139 * \param state The redirect state to destroy
140 */
142
143#endif /* _RES_PJSIP_REDIRECT_H */
int ast_sip_should_redirect(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata)
Check if redirect should be followed based on endpoint configuration.
Definition redirect.c:140
int ast_sip_redirect_parse_3xx(pjsip_rx_data *rdata, struct ast_sip_redirect_state *state)
Parse a 3xx redirect response and extract contacts.
Definition redirect.c:338
int ast_sip_redirect_check_loop(const struct ast_sip_redirect_state *state, const char *uri)
Check if a URI would create a redirect loop.
Definition redirect.c:429
int ast_sip_redirect_get_next_uri(struct ast_sip_redirect_state *state, char **uri_out)
Get the next redirect URI to try.
Definition redirect.c:398
struct ast_sip_endpoint * ast_sip_redirect_get_endpoint(const struct ast_sip_redirect_state *state)
Get the endpoint from the redirect state.
Definition redirect.c:439
int ast_sip_redirect_get_hop_count(const struct ast_sip_redirect_state *state)
Get the current hop count.
Definition redirect.c:434
void ast_sip_redirect_state_destroy(struct ast_sip_redirect_state *state)
Destroy a redirect state and free all resources.
Definition redirect.c:444
struct ast_sip_redirect_state * ast_sip_redirect_state_create(struct ast_sip_endpoint *endpoint, const char *initial_uri)
Create a new redirect state.
Definition redirect.c:59
An entity with which Asterisk communicates.
Definition res_pjsip.h:1061
Redirect state structure.
Definition redirect.c:52
struct ast_sip_endpoint * endpoint
Definition redirect.c:53