Asterisk - The Open Source Telephony Project  18.5.0
Functions | Variables
res_pjsip_path.c File Reference
#include "asterisk.h"
#include <pjsip.h>
#include <pjsip_ua.h>
#include "asterisk/res_pjsip.h"
#include "asterisk/res_pjsip_session.h"
#include "asterisk/module.h"
#include "asterisk/strings.h"
Include dependency graph for res_pjsip_path.c:

Go to the source code of this file.

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static int add_supported (pjsip_tx_data *tdata)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static struct ast_sip_aorfind_aor (struct ast_sip_endpoint *endpoint, pjsip_uri *uri)
 
static int load_module (void)
 
static int path_get_string (pj_pool_t *pool, struct ast_sip_contact *contact, pj_str_t *path_str)
 Get the path string associated with this contact and tdata. More...
 
static void path_outgoing_request (struct ast_sip_endpoint *endpoint, struct ast_sip_contact *contact, pjsip_tx_data *tdata)
 
static void path_outgoing_response (struct ast_sip_endpoint *endpoint, struct ast_sip_contact *contact, pjsip_tx_data *tdata)
 
static void path_session_outgoing_request (struct ast_sip_session *session, pjsip_tx_data *tdata)
 
static void path_session_outgoing_response (struct ast_sip_session *session, pjsip_tx_data *tdata)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "PJSIP Path Header Support" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_APP_DEPEND, .requires = "res_pjsip,res_pjsip_session", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static const pj_str_t PATH_NAME = { "Path", 4 }
 
static struct ast_sip_session_supplement path_session_supplement
 
static struct ast_sip_supplement path_supplement
 
static pj_str_t PATH_SUPPORTED_NAME = { "path", 4 }
 

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 273 of file res_pjsip_path.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 273 of file res_pjsip_path.c.

◆ add_supported()

static int add_supported ( pjsip_tx_data *  tdata)
static

Definition at line 123 of file res_pjsip_path.c.

References NULL, and PATH_SUPPORTED_NAME.

Referenced by path_outgoing_request(), and path_outgoing_response().

124 {
125  pjsip_supported_hdr *hdr;
126  int i;
127 
128  hdr = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_SUPPORTED, NULL);
129  if (!hdr) {
130  /* insert a new Supported header */
131  hdr = pjsip_supported_hdr_create(tdata->pool);
132  if (!hdr) {
133  return -1;
134  }
135 
136  pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)hdr);
137  }
138 
139  /* Don't add the value if it's already there */
140  for (i = 0; i < hdr->count; ++i) {
141  if (pj_stricmp(&hdr->values[i], &PATH_SUPPORTED_NAME) == 0) {
142  return 0;
143  }
144  }
145 
146  if (hdr->count >= PJSIP_GENERIC_ARRAY_MAX_COUNT) {
147  return -1;
148  }
149 
150  /* add on to the existing Supported header */
151  pj_strassign(&hdr->values[hdr->count++], &PATH_SUPPORTED_NAME);
152 
153  return 0;
154 }
#define NULL
Definition: resample.c:96
static pj_str_t PATH_SUPPORTED_NAME

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 273 of file res_pjsip_path.c.

◆ find_aor()

static struct ast_sip_aor* find_aor ( struct ast_sip_endpoint endpoint,
pjsip_uri *  uri 
)
static

Definition at line 39 of file res_pjsip_path.c.

References ao2_cleanup, ast_sip_endpoint::aors, ast_alloca, ast_copy_pj_str(), ast_free, ast_sip_get_sorcery(), ast_sip_location_retrieve_aor(), AST_SIP_USER_OPTIONS_TRUNCATE_CHECK, ast_sorcery_retrieve_by_id(), ast_str_append(), ast_str_buffer(), ast_str_create, ast_str_set(), ast_strdupa, ast_strip(), ast_strlen_zero, ast_sip_domain_alias::domain, NULL, and strsep().

Referenced by path_outgoing_request(), and path_outgoing_response().

40 {
41  char *configured_aors, *aor_name;
42  pjsip_sip_uri *sip_uri;
43  char *domain_name;
44  char *username;
45  struct ast_str *id = NULL;
46 
47  if (ast_strlen_zero(endpoint->aors)) {
48  return NULL;
49  }
50 
51  sip_uri = pjsip_uri_get_uri(uri);
52  domain_name = ast_alloca(sip_uri->host.slen + 1);
53  ast_copy_pj_str(domain_name, &sip_uri->host, sip_uri->host.slen + 1);
54  username = ast_alloca(sip_uri->user.slen + 1);
55  ast_copy_pj_str(username, &sip_uri->user, sip_uri->user.slen + 1);
56 
57  /*
58  * We may want to match without any user options getting
59  * in the way.
60  */
62 
63  configured_aors = ast_strdupa(endpoint->aors);
64 
65  /* Iterate the configured AORs to see if the user or the user+domain match */
66  while ((aor_name = ast_strip(strsep(&configured_aors, ",")))) {
67  struct ast_sip_domain_alias *alias = NULL;
68 
69  if (ast_strlen_zero(aor_name)) {
70  continue;
71  }
72 
73  if (!strcmp(username, aor_name)) {
74  break;
75  }
76 
77  if (!id && !(id = ast_str_create(strlen(username) + sip_uri->host.slen + 2))) {
78  aor_name = NULL;
79  break;
80  }
81 
82  ast_str_set(&id, 0, "%s@", username);
83  if ((alias = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "domain_alias", domain_name))) {
84  ast_str_append(&id, 0, "%s", alias->domain);
85  ao2_cleanup(alias);
86  } else {
87  ast_str_append(&id, 0, "%s", domain_name);
88  }
89 
90  if (!strcmp(aor_name, ast_str_buffer(id))) {
91  break;
92  }
93  }
94  ast_free(id);
95 
96  if (ast_strlen_zero(aor_name)) {
97  return NULL;
98  }
99 
100  return ast_sip_location_retrieve_aor(aor_name);
101 }
#define AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(str)
Truncate the URI user field options string if enabled.
Definition: res_pjsip.h:3036
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1091
void ast_copy_pj_str(char *dest, const pj_str_t *src, size_t size)
Copy a pj_str_t into a standard character buffer.
Definition: res_pjsip.c:5240
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition: sorcery.c:1853
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1065
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Definition: strings.h:219
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
struct ast_sip_aor * ast_sip_location_retrieve_aor(const char *aor_name)
Retrieve a named AOR.
Definition: location.c:147
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:290
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
#define ast_free(a)
Definition: astmm.h:182
char * strsep(char **str, const char *delims)
const ast_string_field domain
Definition: res_pjsip.h:265
struct ast_sorcery * ast_sip_get_sorcery(void)
Get a pointer to the SIP sorcery structure.
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
const ast_string_field aors
Definition: res_pjsip.h:821
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620

◆ load_module()

static int load_module ( void  )
static

Definition at line 252 of file res_pjsip_path.c.

References AST_MODULE_LOAD_SUCCESS, ast_sip_register_supplement(), and ast_sip_session_register_supplement.

Referenced by unload_module().

253 {
256 
258 }
void ast_sip_register_supplement(struct ast_sip_supplement *supplement)
Register a supplement to SIP out of dialog processing.
Definition: res_pjsip.c:4511
static struct ast_sip_session_supplement path_session_supplement
#define ast_sip_session_register_supplement(supplement)
static struct ast_sip_supplement path_supplement

◆ path_get_string()

static int path_get_string ( pj_pool_t *  pool,
struct ast_sip_contact contact,
pj_str_t *  path_str 
)
static

Get the path string associated with this contact and tdata.

Parameters
endpointThe endpoint from which to pull associated path data
contact_uriThe URI identifying the associated contact
path_strThe place to store the retrieved path information
Return values
zeroon success
non-zeroon failure or no available path information

Definition at line 113 of file res_pjsip_path.c.

References ast_strlen_zero, and ast_sip_contact::path.

Referenced by path_outgoing_response().

114 {
115  if (!contact || ast_strlen_zero(contact->path)) {
116  return -1;
117  }
118 
119  *path_str = pj_strdup3(pool, contact->path);
120  return 0;
121 }
const ast_string_field path
Definition: res_pjsip.h:303
static pj_pool_t * pool
Global memory pool for configuration and timers.
#define ast_strlen_zero(foo)
Definition: strings.h:52

◆ path_outgoing_request()

static void path_outgoing_request ( struct ast_sip_endpoint endpoint,
struct ast_sip_contact contact,
pjsip_tx_data *  tdata 
)
static

Definition at line 165 of file res_pjsip_path.c.

References add_supported(), ao2_cleanup, ast_sip_set_outbound_proxy(), ast_strlen_zero, find_aor(), NULL, ast_sip_contact::path, and RAII_VAR.

Referenced by path_session_outgoing_request().

166 {
167  RAII_VAR(struct ast_sip_aor *, aor, NULL, ao2_cleanup);
168 
169  if (!endpoint) {
170  return;
171  }
172 
173  aor = find_aor(endpoint, tdata->msg->line.req.uri);
174  if (!aor || !aor->support_path) {
175  return;
176  }
177 
178  if (add_supported(tdata)) {
179  return;
180  }
181 
182  if (contact && !ast_strlen_zero(contact->path)) {
183  ast_sip_set_outbound_proxy(tdata, contact->path);
184  }
185 }
static struct ast_sip_aor * find_aor(struct ast_sip_endpoint *endpoint, pjsip_uri *uri)
A SIP address of record.
Definition: res_pjsip.h:361
static int add_supported(pjsip_tx_data *tdata)
const ast_string_field path
Definition: res_pjsip.h:303
int ast_sip_set_outbound_proxy(pjsip_tx_data *tdata, const char *proxy)
Set the outbound proxy for an outbound SIP message.
Definition: res_pjsip.c:5047
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
#define ao2_cleanup(obj)
Definition: astobj2.h:1958

◆ path_outgoing_response()

static void path_outgoing_response ( struct ast_sip_endpoint endpoint,
struct ast_sip_contact contact,
pjsip_tx_data *  tdata 
)
static

Definition at line 200 of file res_pjsip_path.c.

References add_supported(), ao2_cleanup, find_aor(), NULL, path_get_string(), PATH_NAME, and RAII_VAR.

Referenced by path_session_outgoing_response().

201 {
202  struct pjsip_status_line status = tdata->msg->line.status;
203  pj_str_t path_dup;
204  pjsip_generic_string_hdr *path_hdr;
205  pjsip_contact_hdr *contact_hdr;
206  RAII_VAR(struct ast_sip_aor *, aor, NULL, ao2_cleanup);
207  pjsip_cseq_hdr *cseq = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, NULL);
208  const pj_str_t REGISTER_METHOD = {"REGISTER", 8};
209 
210  if (!endpoint
211  || !pj_stristr(&REGISTER_METHOD, &cseq->method.name)
212  || !PJSIP_IS_STATUS_IN_CLASS(status.code, 200)) {
213  return;
214  }
215 
216  contact_hdr = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CONTACT, NULL);
217  if (!contact_hdr) {
218  return;
219  }
220 
221  aor = find_aor(endpoint, contact_hdr->uri);
222  if (!aor || !aor->support_path || add_supported(tdata)
223  || path_get_string(tdata->pool, contact, &path_dup)) {
224  return;
225  }
226 
227  path_hdr = pjsip_generic_string_hdr_create(tdata->pool, &PATH_NAME, &path_dup);
228  if (!path_hdr) {
229  return;
230  }
231 
232  pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)path_hdr);
233 }
static struct ast_sip_aor * find_aor(struct ast_sip_endpoint *endpoint, pjsip_uri *uri)
A SIP address of record.
Definition: res_pjsip.h:361
static int add_supported(pjsip_tx_data *tdata)
static int path_get_string(pj_pool_t *pool, struct ast_sip_contact *contact, pj_str_t *path_str)
Get the path string associated with this contact and tdata.
#define NULL
Definition: resample.c:96
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
static const pj_str_t PATH_NAME
jack_status_t status
Definition: app_jack.c:146

◆ path_session_outgoing_request()

static void path_session_outgoing_request ( struct ast_sip_session session,
pjsip_tx_data *  tdata 
)
static

Definition at line 187 of file res_pjsip_path.c.

References ast_sip_session::contact, ast_sip_session::endpoint, and path_outgoing_request().

188 {
189  path_outgoing_request(session->endpoint, session->contact, tdata);
190 }
struct ast_sip_endpoint * endpoint
static void path_outgoing_request(struct ast_sip_endpoint *endpoint, struct ast_sip_contact *contact, pjsip_tx_data *tdata)
struct ast_sip_contact * contact

◆ path_session_outgoing_response()

static void path_session_outgoing_response ( struct ast_sip_session session,
pjsip_tx_data *  tdata 
)
static

Definition at line 235 of file res_pjsip_path.c.

References ast_sip_session::contact, ast_sip_session::endpoint, and path_outgoing_response().

236 {
237  path_outgoing_response(session->endpoint, session->contact, tdata);
238 }
struct ast_sip_endpoint * endpoint
static void path_outgoing_response(struct ast_sip_endpoint *endpoint, struct ast_sip_contact *contact, pjsip_tx_data *tdata)
struct ast_sip_contact * contact

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 260 of file res_pjsip_path.c.

References AST_MODFLAG_LOAD_ORDER, AST_MODPRI_APP_DEPEND, AST_MODULE_INFO(), AST_MODULE_SUPPORT_CORE, ast_sip_session_unregister_supplement(), ast_sip_unregister_supplement(), ASTERISK_GPL_KEY, and load_module().

261 {
264  return 0;
265 }
void ast_sip_session_unregister_supplement(struct ast_sip_session_supplement *supplement)
Unregister a an supplement to SIP session processing.
Definition: pjsip_session.c:63
void ast_sip_unregister_supplement(struct ast_sip_supplement *supplement)
Unregister a an supplement to SIP out of dialog processing.
Definition: res_pjsip.c:4531
static struct ast_sip_session_supplement path_session_supplement
static struct ast_sip_supplement path_supplement

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "PJSIP Path Header Support" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_APP_DEPEND, .requires = "res_pjsip,res_pjsip_session", }
static

Definition at line 273 of file res_pjsip_path.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 273 of file res_pjsip_path.c.

◆ PATH_NAME

const pj_str_t PATH_NAME = { "Path", 4 }
static

Definition at line 36 of file res_pjsip_path.c.

Referenced by path_outgoing_response().

◆ path_session_supplement

struct ast_sip_session_supplement path_session_supplement
static
Initial value:
= {
.outgoing_request = path_session_outgoing_request,
.outgoing_response = path_session_outgoing_response,
}
static void path_session_outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata)
static void path_session_outgoing_response(struct ast_sip_session *session, pjsip_tx_data *tdata)

Definition at line 246 of file res_pjsip_path.c.

◆ path_supplement

struct ast_sip_supplement path_supplement
static
Initial value:
= {
.outgoing_request = path_outgoing_request,
.outgoing_response = path_outgoing_response,
}
static void path_outgoing_request(struct ast_sip_endpoint *endpoint, struct ast_sip_contact *contact, pjsip_tx_data *tdata)
static void path_outgoing_response(struct ast_sip_endpoint *endpoint, struct ast_sip_contact *contact, pjsip_tx_data *tdata)

Definition at line 240 of file res_pjsip_path.c.

◆ PATH_SUPPORTED_NAME

pj_str_t PATH_SUPPORTED_NAME = { "path", 4 }
static

Definition at line 37 of file res_pjsip_path.c.

Referenced by add_supported().