Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Enumerations | Functions | Variables
pjsip_message_filter.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 "include/res_pjsip_private.h"
#include "asterisk/module.h"
Include dependency graph for pjsip_message_filter.c:

Go to the source code of this file.

Data Structures

struct  filter_message_restrictions
 Outgoing message modification restrictions. More...
 

Macros

#define FUNC_ATTRS   __attribute__ ((noinline))
 
#define is_sip_uri(uri)   (PJSIP_URI_SCHEME_IS_SIP(uri) || PJSIP_URI_SCHEME_IS_SIPS(uri))
 
#define MOD_DATA_RESTRICTIONS   "restrictions"
 

Enumerations

enum  uri_type { URI_TYPE_REQUEST = -1, URI_TYPE_TO = PJSIP_H_TO, URI_TYPE_FROM = PJSIP_H_FROM, URI_TYPE_CONTACT = PJSIP_H_CONTACT }
 

Functions

void ast_res_pjsip_cleanup_message_filter (void)
 
int ast_res_pjsip_init_message_filter (void)
 
static pj_bool_t filter_on_rx_message (pjsip_rx_data *rdata)
 
static pj_status_t filter_on_tx_message (pjsip_tx_data *tdata)
 
static void filter_outgoing_message (struct ast_sip_endpoint *endpoint, struct ast_sip_contact *contact, struct pjsip_tx_data *tdata)
 Callback invoked on non-session outgoing messages. More...
 
static void filter_session_outgoing_message (struct ast_sip_session *session, struct pjsip_tx_data *tdata)
 Callback invoked on session outgoing messages. More...
 
static struct filter_message_restrictionsget_restrictions (pjsip_tx_data *tdata)
 Helper function to get (or allocate if not already present) restrictions on a message. More...
 
static pjsip_transport * get_udp_transport (pj_str_t *address, int port)
 Helper function which returns a UDP transport bound to the given address and port. More...
 
static int is_bound_any (pjsip_transport *transport)
 Helper function which determines if a transport is bound to any. More...
 
static int multihomed_rewrite_sdp (struct pjmedia_sdp_session *sdp)
 Helper function which determines if the address within SDP should be rewritten. More...
 
static pj_bool_t on_rx_process_symmetric_transport (pjsip_rx_data *rdata)
 
static pj_bool_t on_rx_process_uris (pjsip_rx_data *rdata)
 
static void print_sanitize_debug (char *msg, pjsip_uri_context_e context, pjsip_sip_uri *uri)
 
static void print_uri_debug (enum uri_type ut, pjsip_rx_data *rdata, pjsip_hdr *hdr)
 
static void remove_x_ast_params (pjsip_uri *header_uri)
 
static void FUNC_ATTRS sanitize_tdata (pjsip_tx_data *tdata)
 

Variables

static pjsip_module filter_module_transport
 
static pjsip_module filter_module_tsx
 
static struct ast_sip_session_supplement filter_session_supplement
 PJSIP Session Supplement for tagging messages with restrictions. More...
 
static struct ast_sip_supplement filter_supplement
 PJSIP Supplement for tagging messages with restrictions. More...
 

Macro Definition Documentation

◆ FUNC_ATTRS

#define FUNC_ATTRS   __attribute__ ((noinline))

Definition at line 180 of file pjsip_message_filter.c.

◆ is_sip_uri

#define is_sip_uri (   uri)    (PJSIP_URI_SCHEME_IS_SIP(uri) || PJSIP_URI_SCHEME_IS_SIPS(uri))

Definition at line 163 of file pjsip_message_filter.c.

Referenced by on_rx_process_uris(), and sanitize_tdata().

◆ MOD_DATA_RESTRICTIONS

#define MOD_DATA_RESTRICTIONS   "restrictions"

Definition at line 29 of file pjsip_message_filter.c.

Referenced by filter_on_tx_message(), and get_restrictions().

Enumeration Type Documentation

◆ uri_type

enum uri_type
Enumerator
URI_TYPE_REQUEST 
URI_TYPE_TO 
URI_TYPE_FROM 
URI_TYPE_CONTACT 

Definition at line 358 of file pjsip_message_filter.c.

358  {
359  URI_TYPE_REQUEST = -1,
360  URI_TYPE_TO = PJSIP_H_TO,
361  URI_TYPE_FROM = PJSIP_H_FROM,
362  URI_TYPE_CONTACT = PJSIP_H_CONTACT,
363 };

Function Documentation

◆ ast_res_pjsip_cleanup_message_filter()

void ast_res_pjsip_cleanup_message_filter ( void  )

Definition at line 565 of file pjsip_message_filter.c.

References ast_sip_session_unregister_supplement(), ast_sip_unregister_service(), ast_sip_unregister_supplement(), filter_module_transport, and filter_module_tsx.

Referenced by ast_res_pjsip_init_message_filter(), and unload_pjsip().

566 {
571 }
static pjsip_module filter_module_transport
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
static pjsip_module filter_module_tsx
static struct ast_sip_supplement filter_supplement
PJSIP Supplement for tagging messages with restrictions.
static struct ast_sip_session_supplement filter_session_supplement
PJSIP Session Supplement for tagging messages with restrictions.
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
void ast_sip_unregister_service(pjsip_module *module)
Definition: res_pjsip.c:3331

◆ ast_res_pjsip_init_message_filter()

int ast_res_pjsip_init_message_filter ( void  )

Definition at line 573 of file pjsip_message_filter.c.

References ast_log, ast_res_pjsip_cleanup_message_filter(), ast_sip_register_service(), ast_sip_register_supplement(), ast_sip_session_register_supplement, filter_module_transport, filter_module_tsx, and LOG_ERROR.

Referenced by load_module().

574 {
577 
579  ast_log(LOG_ERROR, "Could not register message filter module for incoming and outgoing requests\n");
581  return -1;
582  }
583 
585  ast_log(LOG_ERROR, "Could not register message filter module for incoming and outgoing requests\n");
587  return -1;
588  }
589 
590  return 0;
591 }
static pjsip_module filter_module_transport
static pjsip_module filter_module_tsx
#define ast_log
Definition: astobj2.c:42
static struct ast_sip_supplement filter_supplement
PJSIP Supplement for tagging messages with restrictions.
void ast_res_pjsip_cleanup_message_filter(void)
int ast_sip_register_service(pjsip_module *module)
Register a SIP service in Asterisk.
Definition: res_pjsip.c:3315
static struct ast_sip_session_supplement filter_session_supplement
PJSIP Session Supplement for tagging messages with restrictions.
void ast_sip_register_supplement(struct ast_sip_supplement *supplement)
Register a supplement to SIP out of dialog processing.
Definition: res_pjsip.c:4511
#define LOG_ERROR
Definition: logger.h:285
#define ast_sip_session_register_supplement(supplement)

◆ filter_on_rx_message()

static pj_bool_t filter_on_rx_message ( pjsip_rx_data *  rdata)
static

Definition at line 548 of file pjsip_message_filter.c.

References on_rx_process_symmetric_transport(), and on_rx_process_uris().

549 {
550  pj_bool_t rc;
551 
552  rc = on_rx_process_uris(rdata);
553  if (rc == PJ_TRUE) {
554  return rc;
555  }
556 
558  if (rc == PJ_TRUE) {
559  return rc;
560  }
561 
562  return PJ_FALSE;
563 }
static pj_bool_t on_rx_process_uris(pjsip_rx_data *rdata)
static pj_bool_t on_rx_process_symmetric_transport(pjsip_rx_data *rdata)

◆ filter_on_tx_message()

static pj_status_t filter_on_tx_message ( pjsip_tx_data *  tdata)
static

Definition at line 225 of file pjsip_message_filter.c.

References ast_debug, ast_sip_get_pjsip_endpoint(), ast_sip_is_content_type(), ast_sip_mod_data_get, filter_message_restrictions::disallow_from_domain_modification, filter_module_transport, get_udp_transport(), is_bound_any(), MOD_DATA_RESTRICTIONS, multihomed_rewrite_sdp(), NULL, and sanitize_tdata().

226 {
227  struct filter_message_restrictions *restrictions =
229  pjsip_tpmgr_fla2_param prm;
230  pjsip_cseq_hdr *cseq;
231  pjsip_via_hdr *via;
232  pjsip_fromto_hdr *from;
233  pjsip_tpselector sel;
234 
235  sanitize_tdata(tdata);
236 
237  /* Use the destination information to determine what local interface this message will go out on */
238  pjsip_tpmgr_fla2_param_default(&prm);
239  prm.tp_type = tdata->tp_info.transport->key.type;
240  pj_strset2(&prm.dst_host, tdata->tp_info.dst_name);
241  prm.local_if = PJ_TRUE;
242 
243  if ((tdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP) &&
244  (tdata->tp_info.transport->key.type != PJSIP_TRANSPORT_UDP6)) {
245  sel.type = PJSIP_TPSELECTOR_LISTENER;
246  sel.u.listener = tdata->tp_info.transport->factory;
247  prm.tp_sel = &sel;
248  }
249 
250  /* If we can't get the local address use best effort and let it pass */
251  if (pjsip_tpmgr_find_local_addr2(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()), tdata->pool, &prm) != PJ_SUCCESS) {
252  return PJ_SUCCESS;
253  }
254 
255  /* For UDP we can have multiple transports so the port needs to be maintained */
256  if (tdata->tp_info.transport->key.type == PJSIP_TRANSPORT_UDP ||
257  tdata->tp_info.transport->key.type == PJSIP_TRANSPORT_UDP6) {
258  prm.ret_port = tdata->tp_info.transport->local_name.port;
259  }
260 
261  /* If the IP source differs from the existing transport see if we need to update it */
262  if (pj_strcmp(&prm.ret_addr, &tdata->tp_info.transport->local_name.host)) {
263 
264  /* If the transport it is going out on is different reflect it in the message */
265  if (tdata->tp_info.transport->key.type == PJSIP_TRANSPORT_UDP ||
266  tdata->tp_info.transport->key.type == PJSIP_TRANSPORT_UDP6) {
267  pjsip_transport *transport;
268 
269  transport = get_udp_transport(&prm.ret_addr, prm.ret_port);
270 
271  if (transport) {
272  tdata->tp_info.transport = transport;
273  }
274  }
275 
276  /* If the chosen transport is not bound to any we can't use the source address as it won't get back to us */
277  if (!is_bound_any(tdata->tp_info.transport)) {
278  pj_strassign(&prm.ret_addr, &tdata->tp_info.transport->local_name.host);
279  }
280  } else {
281  /* The transport chosen will deliver this but ensure it is updated with the right information */
282  pj_strassign(&prm.ret_addr, &tdata->tp_info.transport->local_name.host);
283  }
284 
285  /* If the message needs to be updated with new address do so */
286  if (tdata->msg->type == PJSIP_REQUEST_MSG || !(cseq = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ, NULL)) ||
287  pj_strcmp2(&cseq->method.name, "REGISTER")) {
288  pjsip_contact_hdr *contact = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CONTACT, NULL);
289  if (contact && (PJSIP_URI_SCHEME_IS_SIP(contact->uri) || PJSIP_URI_SCHEME_IS_SIPS(contact->uri))
290  && !(tdata->msg->type == PJSIP_RESPONSE_MSG && tdata->msg->line.status.code / 100 == 3)) {
291  pjsip_sip_uri *uri = pjsip_uri_get_uri(contact->uri);
292 
293  /* prm.ret_addr is allocated from the tdata pool OR the transport so it is perfectly fine to just do an assignment like this */
294  pj_strassign(&uri->host, &prm.ret_addr);
295  uri->port = prm.ret_port;
296  ast_debug(5, "Re-wrote Contact URI host/port to %.*s:%d (this may be re-written again later)\n",
297  (int)pj_strlen(&uri->host), pj_strbuf(&uri->host), uri->port);
298 
299  if (tdata->tp_info.transport->key.type == PJSIP_TRANSPORT_UDP ||
300  tdata->tp_info.transport->key.type == PJSIP_TRANSPORT_UDP6) {
301  uri->transport_param.slen = 0;
302  } else {
303  pj_strdup2(tdata->pool, &uri->transport_param, pjsip_transport_get_type_name(tdata->tp_info.transport->key.type));
304  }
305 
306  pjsip_tx_data_invalidate_msg(tdata);
307  }
308  }
309 
310  if (tdata->msg->type == PJSIP_REQUEST_MSG && (via = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL))) {
311  pj_strassign(&via->sent_by.host, &prm.ret_addr);
312  via->sent_by.port = prm.ret_port;
313 
314  pjsip_tx_data_invalidate_msg(tdata);
315  }
316 
317  if (tdata->msg->type == PJSIP_REQUEST_MSG && (from = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_FROM, NULL)) &&
318  (restrictions && !restrictions->disallow_from_domain_modification)) {
319  pjsip_name_addr *id_name_addr = (pjsip_name_addr *)from->uri;
320  pjsip_sip_uri *uri = pjsip_uri_get_uri(id_name_addr);
321  pj_sockaddr ip;
322 
323  if (pj_strcmp2(&uri->host, "localhost") && pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &uri->host, &ip) == PJ_SUCCESS) {
324  pj_strassign(&uri->host, &prm.ret_addr);
325  pjsip_tx_data_invalidate_msg(tdata);
326  }
327  }
328 
329  /* Update the SDP if it is present */
330  if (tdata->msg->body && ast_sip_is_content_type(&tdata->msg->body->content_type, "application", "sdp") &&
331  multihomed_rewrite_sdp(tdata->msg->body->data)) {
332  struct pjmedia_sdp_session *sdp = tdata->msg->body->data;
333  static const pj_str_t STR_IP4 = { "IP4", 3 };
334  static const pj_str_t STR_IP6 = { "IP6", 3 };
335  pj_str_t STR_IP;
336  int stream;
337 
338  STR_IP = tdata->tp_info.transport->key.type & PJSIP_TRANSPORT_IPV6 ? STR_IP6 : STR_IP4;
339 
340  pj_strassign(&sdp->origin.addr, &prm.ret_addr);
341  sdp->origin.addr_type = STR_IP;
342  pj_strassign(&sdp->conn->addr, &prm.ret_addr);
343  sdp->conn->addr_type = STR_IP;
344 
345  for (stream = 0; stream < sdp->media_count; ++stream) {
346  if (sdp->media[stream]->conn) {
347  pj_strassign(&sdp->media[stream]->conn->addr, &prm.ret_addr);
348  sdp->media[stream]->conn->addr_type = STR_IP;
349  }
350  }
351 
352  pjsip_tx_data_invalidate_msg(tdata);
353  }
354 
355  return PJ_SUCCESS;
356 }
static pjsip_module filter_module_transport
static int is_bound_any(pjsip_transport *transport)
Helper function which determines if a transport is bound to any.
#define NULL
Definition: resample.c:96
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_sip_mod_data_get(mod_data, id, key)
Using the dictionary stored in mod_data array at a given id, retrieve the value associated with the g...
Definition: res_pjsip.h:2638
int ast_sip_is_content_type(pjsip_media_type *content_type, char *type, char *subtype)
Checks if the given content type matches type/subtype.
Definition: res_pjsip.c:5259
static void FUNC_ATTRS sanitize_tdata(pjsip_tx_data *tdata)
static pjsip_transport * get_udp_transport(pj_str_t *address, int port)
Helper function which returns a UDP transport bound to the given address and port.
#define MOD_DATA_RESTRICTIONS
unsigned int disallow_from_domain_modification
Disallow modification of the From domain.
pjsip_endpoint * ast_sip_get_pjsip_endpoint(void)
Get a pointer to the PJSIP endpoint.
Definition: res_pjsip.c:3718
static int multihomed_rewrite_sdp(struct pjmedia_sdp_session *sdp)
Helper function which determines if the address within SDP should be rewritten.
Outgoing message modification restrictions.

◆ filter_outgoing_message()

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

Callback invoked on non-session outgoing messages.

Definition at line 72 of file pjsip_message_filter.c.

References ast_strlen_zero, filter_message_restrictions::disallow_from_domain_modification, ast_sip_endpoint::fromdomain, and get_restrictions().

73 {
74  struct filter_message_restrictions *restrictions = get_restrictions(tdata);
75 
77 }
#define ast_strlen_zero(foo)
Definition: strings.h:52
unsigned int disallow_from_domain_modification
Disallow modification of the From domain.
static struct filter_message_restrictions * get_restrictions(pjsip_tx_data *tdata)
Helper function to get (or allocate if not already present) restrictions on a message.
Outgoing message modification restrictions.
const ast_string_field fromdomain
Definition: res_pjsip.h:831

◆ filter_session_outgoing_message()

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

Callback invoked on session outgoing messages.

Definition at line 87 of file pjsip_message_filter.c.

References ast_strlen_zero, filter_message_restrictions::disallow_from_domain_modification, ast_sip_session::endpoint, ast_sip_endpoint::fromdomain, and get_restrictions().

88 {
89  struct filter_message_restrictions *restrictions = get_restrictions(tdata);
90 
92 }
struct ast_sip_endpoint * endpoint
#define ast_strlen_zero(foo)
Definition: strings.h:52
unsigned int disallow_from_domain_modification
Disallow modification of the From domain.
static struct filter_message_restrictions * get_restrictions(pjsip_tx_data *tdata)
Helper function to get (or allocate if not already present) restrictions on a message.
Outgoing message modification restrictions.
const ast_string_field fromdomain
Definition: res_pjsip.h:831

◆ get_restrictions()

static struct filter_message_restrictions* get_restrictions ( pjsip_tx_data *  tdata)
static

Helper function to get (or allocate if not already present) restrictions on a message.

Definition at line 56 of file pjsip_message_filter.c.

References ast_sip_mod_data_get, ast_sip_mod_data_set, filter_module_tsx, and MOD_DATA_RESTRICTIONS.

Referenced by filter_outgoing_message(), and filter_session_outgoing_message().

57 {
58  struct filter_message_restrictions *restrictions;
59 
60  restrictions = ast_sip_mod_data_get(tdata->mod_data, filter_module_tsx.id, MOD_DATA_RESTRICTIONS);
61  if (restrictions) {
62  return restrictions;
63  }
64 
65  restrictions = PJ_POOL_ALLOC_T(tdata->pool, struct filter_message_restrictions);
66  ast_sip_mod_data_set(tdata->pool, tdata->mod_data, filter_module_tsx.id, MOD_DATA_RESTRICTIONS, restrictions);
67 
68  return restrictions;
69 }
static pjsip_module filter_module_tsx
#define ast_sip_mod_data_set(pool, mod_data, id, key, val)
Utilizing a mod_data array for a given id, set the value associated with the given key...
Definition: res_pjsip.h:2670
#define ast_sip_mod_data_get(mod_data, id, key)
Using the dictionary stored in mod_data array at a given id, retrieve the value associated with the g...
Definition: res_pjsip.h:2638
#define MOD_DATA_RESTRICTIONS
Outgoing message modification restrictions.

◆ get_udp_transport()

static pjsip_transport* get_udp_transport ( pj_str_t *  address,
int  port 
)
static

Helper function which returns a UDP transport bound to the given address and port.

Definition at line 102 of file pjsip_message_filter.c.

References ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_ref, ast_sip_get_transport_states(), AST_TRANSPORT_UDP, ast_sip_transport_state::flow, NULL, ast_sip_transport_state::transport, transport_states, and ast_sip_transport_state::type.

Referenced by filter_on_tx_message().

103 {
105  struct ast_sip_transport_state *transport_state;
106  struct ao2_iterator iter;
107  pjsip_transport *sip_transport = NULL;
108 
109  if (!transport_states) {
110  return NULL;
111  }
112 
113  for (iter = ao2_iterator_init(transport_states, 0); (transport_state = ao2_iterator_next(&iter)); ao2_ref(transport_state, -1)) {
114  if (!transport_state->flow &&
115  transport_state->type == AST_TRANSPORT_UDP &&
116  !pj_strcmp(&transport_state->transport->local_name.host, address) &&
117  transport_state->transport->local_name.port == port) {
118  sip_transport = transport_state->transport;
119  ao2_ref(transport_state, -1);
120  break;
121  }
122  }
123  ao2_iterator_destroy(&iter);
124 
125  ao2_ref(transport_states, -1);
126 
127  return sip_transport;
128 }
enum ast_transport type
Definition: res_pjsip.h:101
char * address
Definition: f2c.h:59
static struct ao2_container * transport_states
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
#define NULL
Definition: resample.c:96
Structure for SIP transport information.
Definition: res_pjsip.h:87
struct pjsip_transport * transport
Transport itself.
Definition: res_pjsip.h:89
#define ao2_ref(o, delta)
Definition: astobj2.h:464
struct ao2_container * ast_sip_get_transport_states(void)
Retrieves all transport states.
#define ao2_iterator_next(iter)
Definition: astobj2.h:1933
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1841
Generic container type.
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.

◆ is_bound_any()

static int is_bound_any ( pjsip_transport *  transport)
static

Helper function which determines if a transport is bound to any.

Definition at line 131 of file pjsip_message_filter.c.

Referenced by filter_on_tx_message().

132 {
133  pj_uint32_t loop6[4] = {0, 0, 0, 0};
134 
135  if ((transport->local_addr.addr.sa_family == pj_AF_INET() &&
136  transport->local_addr.ipv4.sin_addr.s_addr == PJ_INADDR_ANY) ||
137  (transport->local_addr.addr.sa_family == pj_AF_INET6() &&
138  !pj_memcmp(&transport->local_addr.ipv6.sin6_addr, loop6, sizeof(loop6)))) {
139  return 1;
140  }
141 
142  return 0;
143 }

◆ multihomed_rewrite_sdp()

static int multihomed_rewrite_sdp ( struct pjmedia_sdp_session *  sdp)
static

Helper function which determines if the address within SDP should be rewritten.

Definition at line 146 of file pjsip_message_filter.c.

References ast_sip_get_host_ip_string().

Referenced by filter_on_tx_message().

147 {
148  if (!sdp->conn) {
149  return 0;
150  }
151 
152  /* If the host address is used in the SDP replace it with the address of what this is going out on */
153  if ((!pj_strcmp2(&sdp->conn->addr_type, "IP4") && !pj_strcmp2(&sdp->conn->addr,
154  ast_sip_get_host_ip_string(pj_AF_INET()))) ||
155  (!pj_strcmp2(&sdp->conn->addr_type, "IP6") && !pj_strcmp2(&sdp->conn->addr,
156  ast_sip_get_host_ip_string(pj_AF_INET6())))) {
157  return 1;
158  }
159 
160  return 0;
161 }
const char * ast_sip_get_host_ip_string(int af)
Retrieve the local host address in string form.
Definition: res_pjsip.c:5473

◆ on_rx_process_symmetric_transport()

static pj_bool_t on_rx_process_symmetric_transport ( pjsip_rx_data *  rdata)
static

Definition at line 505 of file pjsip_message_filter.c.

References ao2_cleanup, ast_begins_with(), ast_debug, ast_sip_get_sorcery(), AST_SIP_X_AST_TXP, AST_SIP_X_AST_TXP_LEN, ast_sorcery_retrieve_by_id(), NULL, and ast_sip_transport::symmetric_transport.

Referenced by filter_on_rx_message().

506 {
507  pjsip_contact_hdr *contact;
508  pjsip_sip_uri *uri;
509  const char *transport_id;
510  struct ast_sip_transport *transport;
511  pjsip_param *x_transport;
512 
513  if (rdata->msg_info.msg->type != PJSIP_REQUEST_MSG) {
514  return PJ_FALSE;
515  }
516 
517  contact = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, NULL);
518  if (!(contact && contact->uri
519  && ast_begins_with(rdata->tp_info.transport->info, AST_SIP_X_AST_TXP ":"))) {
520  return PJ_FALSE;
521  }
522 
523  uri = pjsip_uri_get_uri(contact->uri);
524 
525  transport_id = rdata->tp_info.transport->info + AST_SIP_X_AST_TXP_LEN + 1;
526  transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport", transport_id);
527 
528  if (!(transport && transport->symmetric_transport)) {
529  ao2_cleanup(transport);
530  return PJ_FALSE;
531  }
532  ao2_cleanup(transport);
533 
534  x_transport = PJ_POOL_ALLOC_T(rdata->tp_info.pool, pjsip_param);
535  x_transport->name = pj_strdup3(rdata->tp_info.pool, AST_SIP_X_AST_TXP);
536  x_transport->value = pj_strdup3(rdata->tp_info.pool, transport_id);
537 
538  pj_list_insert_before(&uri->other_param, x_transport);
539 
540  ast_debug(1, "Set transport '%s' on %.*s from %.*s:%d\n", transport_id,
541  (int)rdata->msg_info.msg->line.req.method.name.slen,
542  rdata->msg_info.msg->line.req.method.name.ptr,
543  (int)uri->host.slen, uri->host.ptr, uri->port);
544 
545  return PJ_FALSE;
546 }
#define NULL
Definition: resample.c:96
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
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define AST_SIP_X_AST_TXP_LEN
Definition: res_pjsip.h:910
Transport to bind to.
Definition: res_pjsip.h:171
struct ast_sorcery * ast_sip_get_sorcery(void)
Get a pointer to the SIP sorcery structure.
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define AST_SIP_X_AST_TXP
Definition: res_pjsip.h:909
static int force_inline attribute_pure ast_begins_with(const char *str, const char *prefix)
Definition: strings.h:94

◆ on_rx_process_uris()

static pj_bool_t on_rx_process_uris ( pjsip_rx_data *  rdata)
static

Definition at line 446 of file pjsip_message_filter.c.

References ast_sip_get_pjsip_endpoint(), is_sip_uri, NULL, print_uri_debug(), remove_x_ast_params(), URI_TYPE_CONTACT, URI_TYPE_FROM, URI_TYPE_REQUEST, and URI_TYPE_TO.

Referenced by filter_on_rx_message().

447 {
448  pjsip_contact_hdr *contact = NULL;
449 
450  if (rdata->msg_info.msg->type != PJSIP_REQUEST_MSG) {
451  return PJ_FALSE;
452  }
453 
454  if (!is_sip_uri(rdata->msg_info.msg->line.req.uri)) {
456  pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata,
457  PJSIP_SC_UNSUPPORTED_URI_SCHEME, NULL, NULL, NULL);
458  return PJ_TRUE;
459  }
460  remove_x_ast_params(rdata->msg_info.msg->line.req.uri);
461 
462  if (!is_sip_uri(rdata->msg_info.from->uri)) {
463  print_uri_debug(URI_TYPE_FROM, rdata, (pjsip_hdr *)rdata->msg_info.from);
464  pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata,
465  PJSIP_SC_UNSUPPORTED_URI_SCHEME, NULL, NULL, NULL);
466  return PJ_TRUE;
467  }
468  remove_x_ast_params(rdata->msg_info.from->uri);
469 
470  if (!is_sip_uri(rdata->msg_info.to->uri)) {
471  print_uri_debug(URI_TYPE_TO, rdata, (pjsip_hdr *)rdata->msg_info.to);
472  pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata,
473  PJSIP_SC_UNSUPPORTED_URI_SCHEME, NULL, NULL, NULL);
474  return PJ_TRUE;
475  }
476  remove_x_ast_params(rdata->msg_info.to->uri);
477 
478  contact = (pjsip_contact_hdr *) pjsip_msg_find_hdr(
479  rdata->msg_info.msg, PJSIP_H_CONTACT, NULL);
480 
481  if (!contact && pjsip_method_creates_dialog(&rdata->msg_info.msg->line.req.method)) {
482  /* A contact header is required for dialog creating methods */
483  static const pj_str_t missing_contact = { "Missing Contact header", 22 };
484  pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 400,
485  &missing_contact, NULL, NULL);
486  return PJ_TRUE;
487  }
488 
489  while (contact) {
490  if (!contact->star && !is_sip_uri(contact->uri)) {
491  print_uri_debug(URI_TYPE_CONTACT, rdata, (pjsip_hdr *)contact);
492  pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata,
493  PJSIP_SC_UNSUPPORTED_URI_SCHEME, NULL, NULL, NULL);
494  return PJ_TRUE;
495  }
496  remove_x_ast_params(contact->uri);
497 
498  contact = (pjsip_contact_hdr *) pjsip_msg_find_hdr(
499  rdata->msg_info.msg, PJSIP_H_CONTACT, contact->next);
500  }
501 
502  return PJ_FALSE;
503 }
static void print_uri_debug(enum uri_type ut, pjsip_rx_data *rdata, pjsip_hdr *hdr)
#define NULL
Definition: resample.c:96
#define is_sip_uri(uri)
static void remove_x_ast_params(pjsip_uri *header_uri)
pjsip_endpoint * ast_sip_get_pjsip_endpoint(void)
Get a pointer to the PJSIP endpoint.
Definition: res_pjsip.c:3718

◆ print_sanitize_debug()

static void print_sanitize_debug ( char *  msg,
pjsip_uri_context_e  context,
pjsip_sip_uri *  uri 
)
static

Definition at line 166 of file pjsip_message_filter.c.

References ast_debug.

Referenced by sanitize_tdata().

167 {
168 #ifdef AST_DEVMODE
169  char hdrbuf[512];
170  int hdrbuf_len;
171 
172  hdrbuf_len = pjsip_uri_print(context, uri, hdrbuf, 512);
173  hdrbuf[hdrbuf_len] = '\0';
174  ast_debug(2, "%s: %s\n", msg, hdrbuf);
175 #endif
176 }
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116

◆ print_uri_debug()

static void print_uri_debug ( enum uri_type  ut,
pjsip_rx_data *  rdata,
pjsip_hdr *  hdr 
)
static

Definition at line 365 of file pjsip_message_filter.c.

References ast_debug, ast_strdupa, context, NULL, and URI_TYPE_REQUEST.

Referenced by on_rx_process_uris().

366 {
367 #ifdef AST_DEVMODE
368  pjsip_uri *local_uri = NULL;
369  char hdrbuf[512];
370  int hdrbuf_len;
371  char *request_uri;
372  pjsip_uri_context_e context = PJSIP_URI_IN_OTHER;
373  char header_name[32];
374 
375  switch (ut) {
376  case(URI_TYPE_REQUEST):
377  context = PJSIP_URI_IN_REQ_URI;
378  strcpy(header_name, "Request"); /* Safe */
379  local_uri = rdata->msg_info.msg->line.req.uri;
380  break;
381  case(PJSIP_H_FROM):
382  strcpy(header_name, "From"); /* Safe */
383  context = PJSIP_URI_IN_FROMTO_HDR;
384  local_uri = pjsip_uri_get_uri(((pjsip_from_hdr *)hdr)->uri);
385  break;
386  case(PJSIP_H_TO):
387  strcpy(header_name, "To"); /* Safe */
388  context = PJSIP_URI_IN_FROMTO_HDR;
389  local_uri = pjsip_uri_get_uri(((pjsip_to_hdr *)hdr)->uri);
390  break;
391  case(PJSIP_H_CONTACT):
392  strcpy(header_name, "Contact"); /* Safe */
393  context = PJSIP_URI_IN_CONTACT_HDR;
394  local_uri = pjsip_uri_get_uri(((pjsip_contact_hdr *)hdr)->uri);
395  break;
396  }
397 
398  hdrbuf_len = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, rdata->msg_info.msg->line.req.uri, hdrbuf, 512);
399  hdrbuf[hdrbuf_len] = '\0';
400  request_uri = ast_strdupa(hdrbuf);
401  hdrbuf_len = pjsip_uri_print(context, local_uri, hdrbuf, 512);
402  hdrbuf[hdrbuf_len] = '\0';
403 
404  ast_debug(2, "There was a non sip(s) URI scheme in %s URI '%s' for request '%*.*s %s'\n",
405  header_name, hdrbuf,
406  (int)rdata->msg_info.msg->line.req.method.name.slen,
407  (int)rdata->msg_info.msg->line.req.method.name.slen,
408  rdata->msg_info.msg->line.req.method.name.ptr, request_uri);
409 #endif
410 }
#define NULL
Definition: resample.c:96
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116

◆ remove_x_ast_params()

static void remove_x_ast_params ( pjsip_uri *  header_uri)
static

/internal

We want to make sure that any incoming requests don't already have x-ast-* parameters in any URIs or we may get confused if symmetric transport (x-ast-txp) or rewrite_contact (x-ast-orig-host) is used later on.

Definition at line 420 of file pjsip_message_filter.c.

Referenced by on_rx_process_uris().

420  {
421  pjsip_sip_uri *uri;
422  pjsip_param *param;
423 
424  if (!header_uri) {
425  return;
426  }
427 
428  uri = pjsip_uri_get_uri(header_uri);
429  if (!uri) {
430  return;
431  }
432 
433  param = uri->other_param.next;
434 
435  while (param != &uri->other_param) {
436  /* We need to save off 'next' because pj_list_erase will remove it */
437  pjsip_param *next = param->next;
438 
439  if (pj_strncmp2(&param->name, "x-ast-", 6) == 0) {
440  pj_list_erase(param);
441  }
442  param = next;
443  }
444 }

◆ sanitize_tdata()

static void FUNC_ATTRS sanitize_tdata ( pjsip_tx_data *  tdata)
static

Definition at line 185 of file pjsip_message_filter.c.

References AST_SIP_X_AST_TXP, AST_SIP_X_AST_TXP_LEN, is_sip_uri, and print_sanitize_debug().

Referenced by filter_on_tx_message().

186 {
187  static const pj_str_t x_name = { AST_SIP_X_AST_TXP, AST_SIP_X_AST_TXP_LEN };
188  pjsip_param *x_transport;
189  pjsip_sip_uri *uri;
190  pjsip_hdr *hdr;
191 
192  if (tdata->msg->type == PJSIP_REQUEST_MSG) {
193  if (is_sip_uri(tdata->msg->line.req.uri)) {
194  uri = pjsip_uri_get_uri(tdata->msg->line.req.uri);
195  print_sanitize_debug("Sanitizing Request", PJSIP_URI_IN_REQ_URI, uri);
196  while ((x_transport = pjsip_param_find(&uri->other_param, &x_name))) {
197  pj_list_erase(x_transport);
198  }
199  }
200  }
201 
202  for (hdr = tdata->msg->hdr.next; hdr != &tdata->msg->hdr; hdr = hdr->next) {
203  if (hdr->type == PJSIP_H_TO || hdr->type == PJSIP_H_FROM) {
204  if (is_sip_uri(((pjsip_fromto_hdr *) hdr)->uri)) {
205  uri = pjsip_uri_get_uri(((pjsip_fromto_hdr *) hdr)->uri);
206  print_sanitize_debug("Sanitizing From/To header", PJSIP_URI_IN_FROMTO_HDR, uri);
207  while ((x_transport = pjsip_param_find(&uri->other_param, &x_name))) {
208  pj_list_erase(x_transport);
209  }
210  }
211  } else if (hdr->type == PJSIP_H_CONTACT) {
212  if (!((pjsip_contact_hdr *) hdr)->star && is_sip_uri(((pjsip_contact_hdr *) hdr)->uri)) {
213  uri = pjsip_uri_get_uri(((pjsip_contact_hdr *) hdr)->uri);
214  print_sanitize_debug("Sanitizing Contact header", PJSIP_URI_IN_CONTACT_HDR, uri);
215  while ((x_transport = pjsip_param_find(&uri->other_param, &x_name))) {
216  pj_list_erase(x_transport);
217  }
218  }
219  }
220  }
221 
222  pjsip_tx_data_invalidate_msg(tdata);
223 }
static void print_sanitize_debug(char *msg, pjsip_uri_context_e context, pjsip_sip_uri *uri)
#define is_sip_uri(uri)
#define AST_SIP_X_AST_TXP_LEN
Definition: res_pjsip.h:910
#define AST_SIP_X_AST_TXP
Definition: res_pjsip.h:909

Variable Documentation

◆ filter_module_transport

pjsip_module filter_module_transport
static

◆ filter_module_tsx

pjsip_module filter_module_tsx
static

◆ filter_session_supplement

struct ast_sip_session_supplement filter_session_supplement
static
Initial value:
= {
.priority = 1,
.outgoing_request = filter_session_outgoing_message,
.outgoing_response = filter_session_outgoing_message,
}
static void filter_session_outgoing_message(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
Callback invoked on session outgoing messages.

PJSIP Session Supplement for tagging messages with restrictions.

Definition at line 95 of file pjsip_message_filter.c.

◆ filter_supplement

struct ast_sip_supplement filter_supplement
static
Initial value:
= {
.outgoing_request = filter_outgoing_message,
.outgoing_response = filter_outgoing_message,
}
static void filter_outgoing_message(struct ast_sip_endpoint *endpoint, struct ast_sip_contact *contact, struct pjsip_tx_data *tdata)
Callback invoked on non-session outgoing messages.

PJSIP Supplement for tagging messages with restrictions.

Definition at line 80 of file pjsip_message_filter.c.