29 #define MOD_DATA_RESTRICTIONS "restrictions" 41 .name = {
"Message Filtering Transport", 27 },
43 .priority = PJSIP_MOD_PRIORITY_TRANSPORT_LAYER,
48 .name = {
"Message Filtering TSX", 21 },
50 .priority = PJSIP_MOD_PRIORITY_TSX_LAYER - 1,
107 pjsip_transport *sip_transport =
NULL;
109 if (!transport_states) {
114 if (!transport_state->
flow &&
116 !pj_strcmp(&transport_state->
transport->local_name.host, address) &&
117 transport_state->
transport->local_name.port == port) {
118 sip_transport = transport_state->
transport;
127 return sip_transport;
133 pj_uint32_t loop6[4] = {0, 0, 0, 0};
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)))) {
153 if ((!pj_strcmp2(&sdp->conn->addr_type,
"IP4") && !pj_strcmp2(&sdp->conn->addr,
155 (!pj_strcmp2(&sdp->conn->addr_type,
"IP6") && !pj_strcmp2(&sdp->conn->addr,
163 #define is_sip_uri(uri) \ 164 (PJSIP_URI_SCHEME_IS_SIP(uri) || PJSIP_URI_SCHEME_IS_SIPS(uri)) 172 hdrbuf_len = pjsip_uri_print(context, uri, hdrbuf, 512);
173 hdrbuf[hdrbuf_len] =
'\0';
180 #define FUNC_ATTRS __attribute__ ((noinline)) 188 pjsip_param *x_transport;
192 if (tdata->msg->type == PJSIP_REQUEST_MSG) {
194 uri = pjsip_uri_get_uri(tdata->msg->line.req.uri);
196 while ((x_transport = pjsip_param_find(&uri->other_param, &x_name))) {
197 pj_list_erase(x_transport);
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);
207 while ((x_transport = pjsip_param_find(&uri->other_param, &x_name))) {
208 pj_list_erase(x_transport);
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);
215 while ((x_transport = pjsip_param_find(&uri->other_param, &x_name))) {
216 pj_list_erase(x_transport);
222 pjsip_tx_data_invalidate_msg(tdata);
229 pjsip_tpmgr_fla2_param prm;
230 pjsip_cseq_hdr *cseq;
232 pjsip_fromto_hdr *from;
233 pjsip_tpselector sel;
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;
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;
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;
262 if (pj_strcmp(&prm.ret_addr, &tdata->tp_info.transport->local_name.host)) {
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;
272 tdata->tp_info.transport = transport;
278 pj_strassign(&prm.ret_addr, &tdata->tp_info.transport->local_name.host);
282 pj_strassign(&prm.ret_addr, &tdata->tp_info.transport->local_name.host);
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);
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);
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;
303 pj_strdup2(tdata->pool, &uri->transport_param, pjsip_transport_get_type_name(tdata->tp_info.transport->key.type));
306 pjsip_tx_data_invalidate_msg(tdata);
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;
314 pjsip_tx_data_invalidate_msg(tdata);
317 if (tdata->msg->type == PJSIP_REQUEST_MSG && (from = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_FROM,
NULL)) &&
319 pjsip_name_addr *id_name_addr = (pjsip_name_addr *)from->uri;
320 pjsip_sip_uri *uri = pjsip_uri_get_uri(id_name_addr);
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);
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 };
338 STR_IP = tdata->tp_info.transport->key.type & PJSIP_TRANSPORT_IPV6 ? STR_IP6 : STR_IP4;
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;
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;
352 pjsip_tx_data_invalidate_msg(tdata);
368 pjsip_uri *local_uri =
NULL;
372 pjsip_uri_context_e
context = PJSIP_URI_IN_OTHER;
373 char header_name[32];
377 context = PJSIP_URI_IN_REQ_URI;
378 strcpy(header_name,
"Request");
379 local_uri = rdata->msg_info.msg->line.req.uri;
382 strcpy(header_name,
"From");
383 context = PJSIP_URI_IN_FROMTO_HDR;
384 local_uri = pjsip_uri_get_uri(((pjsip_from_hdr *)hdr)->uri);
387 strcpy(header_name,
"To");
388 context = PJSIP_URI_IN_FROMTO_HDR;
389 local_uri = pjsip_uri_get_uri(((pjsip_to_hdr *)hdr)->uri);
391 case(PJSIP_H_CONTACT):
392 strcpy(header_name,
"Contact");
393 context = PJSIP_URI_IN_CONTACT_HDR;
394 local_uri = pjsip_uri_get_uri(((pjsip_contact_hdr *)hdr)->uri);
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';
401 hdrbuf_len = pjsip_uri_print(context, local_uri, hdrbuf, 512);
402 hdrbuf[hdrbuf_len] =
'\0';
404 ast_debug(2,
"There was a non sip(s) URI scheme in %s URI '%s' for request '%*.*s %s'\n",
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);
428 uri = pjsip_uri_get_uri(header_uri);
433 param = uri->other_param.next;
435 while (param != &uri->other_param) {
437 pjsip_param *next = param->next;
439 if (pj_strncmp2(¶m->name,
"x-ast-", 6) == 0) {
440 pj_list_erase(param);
450 if (rdata->msg_info.msg->type != PJSIP_REQUEST_MSG) {
454 if (!
is_sip_uri(rdata->msg_info.msg->line.req.uri)) {
478 contact = (pjsip_contact_hdr *) pjsip_msg_find_hdr(
479 rdata->msg_info.msg, PJSIP_H_CONTACT,
NULL);
481 if (!contact && pjsip_method_creates_dialog(&rdata->msg_info.msg->line.req.method)) {
483 static const pj_str_t missing_contact = {
"Missing Contact header", 22 };
490 if (!contact->star && !
is_sip_uri(contact->uri)) {
498 contact = (pjsip_contact_hdr *) pjsip_msg_find_hdr(
499 rdata->msg_info.msg, PJSIP_H_CONTACT, contact->next);
509 const char *transport_id;
511 pjsip_param *x_transport;
513 if (rdata->msg_info.msg->type != PJSIP_REQUEST_MSG) {
517 contact = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
NULL);
518 if (!(contact && contact->uri
523 uri = pjsip_uri_get_uri(contact->uri);
534 x_transport = PJ_POOL_ALLOC_T(rdata->tp_info.pool, pjsip_param);
536 x_transport->value = pj_strdup3(rdata->tp_info.pool, transport_id);
538 pj_list_insert_before(&uri->other_param, x_transport);
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);
579 ast_log(
LOG_ERROR,
"Could not register message filter module for incoming and outgoing requests\n");
585 ast_log(
LOG_ERROR,
"Could not register message filter module for incoming and outgoing requests\n");
static void print_uri_debug(enum uri_type ut, pjsip_rx_data *rdata, pjsip_hdr *hdr)
struct ast_sip_endpoint * endpoint
Asterisk main include file. File version handling, generic pbx functions.
static pj_status_t filter_on_tx_message(pjsip_tx_data *tdata)
static void filter_session_outgoing_message(struct ast_sip_session *session, struct pjsip_tx_data *tdata)
Callback invoked on session outgoing messages.
static void print_sanitize_debug(char *msg, pjsip_uri_context_e context, pjsip_sip_uri *uri)
static pj_bool_t on_rx_process_uris(pjsip_rx_data *rdata)
static pjsip_module filter_module_transport
static struct ao2_container * transport_states
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
static int is_bound_any(pjsip_transport *transport)
Helper function which determines if a transport is bound to any.
static pj_bool_t filter_on_rx_message(pjsip_rx_data *rdata)
void ast_sip_session_unregister_supplement(struct ast_sip_session_supplement *supplement)
Unregister a an supplement to SIP session processing.
static pjsip_module filter_module_tsx
A structure describing a SIP session.
#define ast_strlen_zero(foo)
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
const char * ast_sip_get_host_ip_string(int af)
Retrieve the local host address in string form.
#define ast_debug(level,...)
Log a DEBUG message.
static struct ast_sip_supplement filter_supplement
PJSIP Supplement for tagging messages with restrictions.
Structure for SIP transport information.
#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...
void ast_res_pjsip_cleanup_message_filter(void)
int ast_res_pjsip_init_message_filter(void)
struct pjsip_transport * transport
Transport itself.
static struct ast_mansession session
#define ao2_ref(o, delta)
#define ast_strdupa(s)
duplicate a string in memory from the stack
An entity with which Asterisk communicates.
int ast_sip_register_service(pjsip_module *module)
Register a SIP service in Asterisk.
#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...
int ast_sip_is_content_type(pjsip_media_type *content_type, char *type, char *subtype)
Checks if the given content type matches type/subtype.
static struct ast_sip_session_supplement filter_session_supplement
PJSIP Session Supplement for tagging messages with restrictions.
#define AST_SIP_X_AST_TXP_LEN
void ast_sip_register_supplement(struct ast_sip_supplement *supplement)
Register a supplement to SIP out of dialog processing.
struct ao2_container * ast_sip_get_transport_states(void)
Retrieves all transport states.
void ast_sip_unregister_supplement(struct ast_sip_supplement *supplement)
Unregister a an supplement to SIP out of dialog processing.
enum ast_sip_supplement_priority priority
#define ao2_iterator_next(iter)
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.
static pj_bool_t on_rx_process_symmetric_transport(pjsip_rx_data *rdata)
static void remove_x_ast_params(pjsip_uri *header_uri)
#define MOD_DATA_RESTRICTIONS
unsigned int disallow_from_domain_modification
Disallow modification of the From domain.
A supplement to SIP message processing.
pjsip_endpoint * ast_sip_get_pjsip_endpoint(void)
Get a pointer to the PJSIP endpoint.
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.
struct ast_sorcery * ast_sip_get_sorcery(void)
Get a pointer to the SIP sorcery structure.
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
A supplement to SIP message processing.
#define AST_SIP_X_AST_TXP
void ast_sip_unregister_service(pjsip_module *module)
static int force_inline attribute_pure ast_begins_with(const char *str, const char *prefix)
static int multihomed_rewrite_sdp(struct pjmedia_sdp_session *sdp)
Helper function which determines if the address within SDP should be rewritten.
static char context[AST_MAX_CONTEXT]
Asterisk module definitions.
Outgoing message modification restrictions.
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
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.
const ast_string_field fromdomain
#define ast_sip_session_register_supplement(supplement)
enum ast_sip_supplement_priority priority