Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Functions | Variables
pjsip_distributor.c File Reference
#include "asterisk.h"
#include <pjsip.h>
#include "asterisk/res_pjsip.h"
#include "asterisk/acl.h"
#include "include/res_pjsip_private.h"
#include "asterisk/taskprocessor.h"
#include "asterisk/threadpool.h"
#include "asterisk/res_pjsip_cli.h"
Include dependency graph for pjsip_distributor.c:

Go to the source code of this file.

Data Structures

struct  distributor_dialog_data
 
struct  unidentified_request
 

Macros

#define DEFAULT_SUSPECTS_BUCKETS   53
 
#define DIALOG_ASSOCIATIONS_BUCKETS   251
 
#define DISTRIBUTOR_POOL_SIZE   31
 
#define MAX_REALM_LENGTH   40
 

Functions

static struct ast_sip_authalloc_artificial_auth (char *default_realm)
 
static AO2_GLOBAL_OBJ_STATIC (artificial_auth)
 
static void apply_acls (pjsip_rx_data *rdata)
 
static int apply_endpoint_acl (pjsip_rx_data *rdata, struct ast_sip_endpoint *endpoint)
 
static int apply_endpoint_contact_acl (pjsip_rx_data *rdata, struct ast_sip_endpoint *endpoint)
 
struct ast_sip_endpointast_pjsip_rdata_get_endpoint (pjsip_rx_data *rdata)
 Get the looked-up endpoint on an out-of dialog request or response. More...
 
void ast_sip_destroy_distributor (void)
 
struct ast_sip_endpointast_sip_dialog_get_endpoint (pjsip_dialog *dlg)
 Get the endpoint associated with this dialog. More...
 
void ast_sip_dialog_set_endpoint (pjsip_dialog *dlg, struct ast_sip_endpoint *endpoint)
 Set an endpoint on a SIP dialog so in-dialog requests do not undergo endpoint lookup. More...
 
void ast_sip_dialog_set_serializer (pjsip_dialog *dlg, struct ast_taskprocessor *serializer)
 Set a serializer on a SIP dialog so requests and responses are automatically serialized. More...
 
struct ast_sip_authast_sip_get_artificial_auth (void)
 Retrieves a reference to the artificial auth. More...
 
struct ast_sip_endpointast_sip_get_artificial_endpoint (void)
 Retrieves a reference to the artificial endpoint. More...
 
struct ast_taskprocessorast_sip_get_distributor_serializer (pjsip_rx_data *rdata)
 Determine the distributor serializer for the SIP message. More...
 
int ast_sip_initialize_distributor (void)
 
static pj_bool_t authenticate (pjsip_rx_data *rdata)
 
static int buf_hash (const char *pos, size_t len)
 
static int buf_hash_add (const char *pos, size_t len, int hash)
 
static void check_endpoint (pjsip_rx_data *rdata, struct unidentified_request *unid, const char *name)
 
static int clean_task (const void *data)
 
static struct ao2_containercli_unid_get_container (const char *regex)
 
static const char * cli_unid_get_id (const void *obj)
 
static int cli_unid_iterate (void *container, ao2_callback_fn callback, void *args)
 
static int cli_unid_print_body (void *obj, void *arg, int flags)
 
static int cli_unid_print_header (void *obj, void *arg, int flags)
 
static void * cli_unid_retrieve_by_id (const char *id)
 
static int create_artificial_auth (void)
 
static int create_artificial_endpoint (void)
 
static int dialog_associations_cmp (void *obj, void *arg, int flags)
 
static int dialog_associations_hash (const void *obj, int flags)
 
static int distribute (void *data)
 
static pj_bool_t distributor (pjsip_rx_data *rdata)
 
static int distributor_pool_setup (void)
 
static void distributor_pool_shutdown (void)
 
static pj_bool_t endpoint_lookup (pjsip_rx_data *rdata)
 
static int expire_requests (void *object, void *arg, int flags)
 
static int extract_contact_addr (pjsip_contact_hdr *contact, struct ast_sockaddr **addrs)
 
static pjsip_dialog * find_dialog (pjsip_rx_data *rdata)
 
static struct ast_taskprocessorfind_request_serializer (pjsip_rx_data *rdata)
 
static void global_loaded (const char *object_type)
 
static void log_failed_request (pjsip_rx_data *rdata, char *msg, unsigned int count, unsigned int period)
 
static int pjstr_hash (pj_str_t *str)
 
static int pjstr_hash_add (pj_str_t *str, int hash)
 
static int prune_task (const void *data)
 
static pj_status_t record_serializer (pjsip_tx_data *tdata)
 
static int suspects_compare (void *obj, void *arg, int flags)
 
static int suspects_hash (const void *obj, int flags)
 
static int suspects_sort (const void *obj, const void *arg, int flags)
 

Variables

static struct ast_sip_endpointartificial_endpoint = NULL
 
static pjsip_module auth_mod
 
static struct ast_cli_entry cli_commands []
 
static struct ao2_containerdialog_associations
 
static pjsip_module distributor_mod
 
static struct ast_taskprocessordistributor_pool [DISTRIBUTOR_POOL_SIZE]
 
static pjsip_module endpoint_mod
 
static struct ast_sorcery_observer global_observer
 Observer which is used to update our interval and default_realm when the global setting changes. More...
 
static enum ast_sip_taskprocessor_overload_trigger overload_trigger
 
struct ast_sched_contextprune_context
 
struct ast_sip_cli_formatter_entryunid_formatter
 
static unsigned int unidentified_count
 
static unsigned int unidentified_period
 
static unsigned int unidentified_prune_interval
 
static struct ao2_containerunidentified_requests
 
static int using_auth_username
 

Macro Definition Documentation

◆ DEFAULT_SUSPECTS_BUCKETS

#define DEFAULT_SUSPECTS_BUCKETS   53

Definition at line 47 of file pjsip_distributor.c.

Referenced by ast_sip_initialize_distributor().

◆ DIALOG_ASSOCIATIONS_BUCKETS

#define DIALOG_ASSOCIATIONS_BUCKETS   251

Definition at line 162 of file pjsip_distributor.c.

Referenced by ast_sip_initialize_distributor().

◆ DISTRIBUTOR_POOL_SIZE

#define DISTRIBUTOR_POOL_SIZE   31

Number of serializers in pool if one not otherwise known. (Best if prime number)

Definition at line 63 of file pjsip_distributor.c.

◆ MAX_REALM_LENGTH

#define MAX_REALM_LENGTH   40

Definition at line 45 of file pjsip_distributor.c.

Referenced by create_artificial_auth(), and global_loaded().

Function Documentation

◆ alloc_artificial_auth()

static struct ast_sip_auth* alloc_artificial_auth ( char *  default_realm)
static

Definition at line 594 of file pjsip_distributor.c.

References AO2_GLOBAL_OBJ_STATIC(), AST_SIP_AUTH_TYPE_ARTIFICIAL, ast_sip_get_sorcery(), ast_sorcery_alloc(), ast_string_field_set, ast_sip_auth::auth_pass, ast_sip_auth::auth_user, NULL, ast_sip_auth::realm, and ast_sip_auth::type.

Referenced by create_artificial_auth(), and global_loaded().

595 {
596  struct ast_sip_auth *fake_auth;
597 
598  fake_auth = ast_sorcery_alloc(ast_sip_get_sorcery(), SIP_SORCERY_AUTH_TYPE,
599  "artificial");
600  if (!fake_auth) {
601  return NULL;
602  }
603 
605  ast_string_field_set(fake_auth, auth_user, "");
606  ast_string_field_set(fake_auth, auth_pass, "");
607  fake_auth->type = AST_SIP_AUTH_TYPE_ARTIFICIAL;
608 
609  return fake_auth;
610 }
static char default_realm[MAX_REALM_LENGTH+1]
#define NULL
Definition: resample.c:96
const ast_string_field realm
Definition: res_pjsip.h:448
void * ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
Allocate an object.
Definition: sorcery.c:1744
const ast_string_field auth_pass
Definition: res_pjsip.h:452
enum ast_sip_auth_type type
Definition: res_pjsip.h:464
struct ast_sorcery * ast_sip_get_sorcery(void)
Get a pointer to the SIP sorcery structure.
const ast_string_field auth_user
Definition: res_pjsip.h:450
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:514

◆ AO2_GLOBAL_OBJ_STATIC()

static AO2_GLOBAL_OBJ_STATIC ( artificial_auth  )
static

Referenced by alloc_artificial_auth().

◆ apply_acls()

static void apply_acls ( pjsip_rx_data *  rdata)
static

Definition at line 701 of file pjsip_distributor.c.

References ao2_replace, apply_endpoint_acl(), apply_endpoint_contact_acl(), ast_debug, ast_sorcery_object_get_id(), and endpoint_mod.

Referenced by endpoint_lookup().

702 {
703  struct ast_sip_endpoint *endpoint;
704 
705  /* Is the endpoint allowed with the source or contact address? */
706  endpoint = rdata->endpt_info.mod_data[endpoint_mod.id];
707  if (endpoint != artificial_endpoint
708  && (apply_endpoint_acl(rdata, endpoint)
709  || apply_endpoint_contact_acl(rdata, endpoint))) {
710  ast_debug(1, "Endpoint '%s' not allowed by ACL\n",
711  ast_sorcery_object_get_id(endpoint));
712 
713  /* Replace the rdata endpoint with the artificial endpoint. */
714  ao2_replace(rdata->endpt_info.mod_data[endpoint_mod.id], artificial_endpoint);
715  }
716 }
static int apply_endpoint_contact_acl(pjsip_rx_data *rdata, struct ast_sip_endpoint *endpoint)
static pjsip_module endpoint_mod
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
static struct ast_sip_endpoint * artificial_endpoint
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312
static int apply_endpoint_acl(pjsip_rx_data *rdata, struct ast_sip_endpoint *endpoint)
An entity with which Asterisk communicates.
Definition: res_pjsip.h:812
#define ao2_replace(dst, src)
Definition: astobj2.h:517

◆ apply_endpoint_acl()

static int apply_endpoint_acl ( pjsip_rx_data *  rdata,
struct ast_sip_endpoint endpoint 
)
static

Definition at line 807 of file pjsip_distributor.c.

References ast_sip_endpoint::acl, ast_acl_list_is_empty(), ast_apply_acl(), AST_SENSE_ALLOW, ast_sip_report_failed_acl(), ast_sockaddr_parse(), ast_sockaddr_set_port, log_failed_request(), and PARSE_PORT_FORBID.

Referenced by apply_acls(), and check_endpoint().

808 {
809  struct ast_sockaddr addr;
810 
811  if (ast_acl_list_is_empty(endpoint->acl)) {
812  return 0;
813  }
814 
815  memset(&addr, 0, sizeof(addr));
816  ast_sockaddr_parse(&addr, rdata->pkt_info.src_name, PARSE_PORT_FORBID);
817  ast_sockaddr_set_port(&addr, rdata->pkt_info.src_port);
818 
819  if (ast_apply_acl(endpoint->acl, &addr, "SIP ACL: ") != AST_SENSE_ALLOW) {
820  log_failed_request(rdata, "Not match Endpoint ACL", 0, 0);
821  ast_sip_report_failed_acl(endpoint, rdata, "not_match_endpoint_acl");
822  return 1;
823  }
824  return 0;
825 }
int ast_sockaddr_parse(struct ast_sockaddr *addr, const char *str, int flags)
Parse an IPv4 or IPv6 address string.
Definition: netsock2.c:230
static void log_failed_request(pjsip_rx_data *rdata, char *msg, unsigned int count, unsigned int period)
Socket address structure.
Definition: netsock2.h:97
struct ast_acl_list * acl
Definition: res_pjsip.h:881
void ast_sip_report_failed_acl(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, const char *name)
Send a security event notification for when an ACL check fails.
enum ast_acl_sense ast_apply_acl(struct ast_acl_list *acl_list, const struct ast_sockaddr *addr, const char *purpose)
Apply a set of rules to a given IP address.
Definition: acl.c:800
int ast_acl_list_is_empty(struct ast_acl_list *acl_list)
Determines if an ACL is empty or if it contains entries.
Definition: acl.c:541
#define ast_sockaddr_set_port(addr, port)
Sets the port number of a socket address.
Definition: netsock2.h:537

◆ apply_endpoint_contact_acl()

static int apply_endpoint_contact_acl ( pjsip_rx_data *  rdata,
struct ast_sip_endpoint endpoint 
)
static

Definition at line 845 of file pjsip_distributor.c.

References ast_acl_list_is_empty(), ast_apply_acl(), ast_free, AST_SENSE_ALLOW, ast_sip_report_failed_acl(), ast_sip_endpoint::contact_acl, extract_contact_addr(), if(), and log_failed_request().

Referenced by apply_acls(), and check_endpoint().

846 {
847  int num_contact_addrs;
848  int forbidden = 0;
849  struct ast_sockaddr *contact_addrs;
850  int i;
851  pjsip_contact_hdr *contact = (pjsip_contact_hdr *)&rdata->msg_info.msg->hdr;
852 
853  if (ast_acl_list_is_empty(endpoint->contact_acl)) {
854  return 0;
855  }
856 
857  while ((contact = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, contact->next))) {
858  num_contact_addrs = extract_contact_addr(contact, &contact_addrs);
859  if (num_contact_addrs <= 0) {
860  continue;
861  }
862  for (i = 0; i < num_contact_addrs; ++i) {
863  if (ast_apply_acl(endpoint->contact_acl, &contact_addrs[i], "SIP Contact ACL: ") != AST_SENSE_ALLOW) {
864  log_failed_request(rdata, "Not match Endpoint Contact ACL", 0, 0);
865  ast_sip_report_failed_acl(endpoint, rdata, "not_match_endpoint_contact_acl");
866  forbidden = 1;
867  break;
868  }
869  }
870  ast_free(contact_addrs);
871  if (forbidden) {
872  /* No use checking other contacts if we already have failed ACL check */
873  break;
874  }
875  }
876 
877  return forbidden;
878 }
if(!yyg->yy_init)
Definition: ast_expr2f.c:868
static int extract_contact_addr(pjsip_contact_hdr *contact, struct ast_sockaddr **addrs)
static void log_failed_request(pjsip_rx_data *rdata, char *msg, unsigned int count, unsigned int period)
Socket address structure.
Definition: netsock2.h:97
void ast_sip_report_failed_acl(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, const char *name)
Send a security event notification for when an ACL check fails.
struct ast_acl_list * contact_acl
Definition: res_pjsip.h:883
enum ast_acl_sense ast_apply_acl(struct ast_acl_list *acl_list, const struct ast_sockaddr *addr, const char *purpose)
Apply a set of rules to a given IP address.
Definition: acl.c:800
int ast_acl_list_is_empty(struct ast_acl_list *acl_list)
Determines if an ACL is empty or if it contains entries.
Definition: acl.c:541
#define ast_free(a)
Definition: astmm.h:182

◆ ast_pjsip_rdata_get_endpoint()

struct ast_sip_endpoint* ast_pjsip_rdata_get_endpoint ( pjsip_rx_data *  rdata)

Get the looked-up endpoint on an out-of dialog request or response.

The function may ONLY be called on out-of-dialog requests or responses. For in-dialog requests and responses, it is required that the user of the dialog has the looked-up endpoint stored locally.

This function should never return NULL if the message is out-of-dialog. It will always return NULL if the message is in-dialog.

This function will increase the reference count of the returned endpoint by one. Release your reference using the ao2_ref function when finished.

Parameters
rdataOut-of-dialog request or response
Returns
The looked up endpoint

Definition at line 969 of file pjsip_distributor.c.

References ao2_ref, and endpoint_mod.

Referenced by authenticate(), handle_new_invite_request(), nat_on_rx_message(), options_on_rx_request(), pubsub_on_rx_mwi_notify_request(), pubsub_on_rx_publish_request(), pubsub_on_rx_subscribe_request(), registrar_on_rx_request(), rx_data_to_ast_msg(), send_options_response(), send_response(), and supplement_on_rx_request().

970 {
971  struct ast_sip_endpoint *endpoint = rdata->endpt_info.mod_data[endpoint_mod.id];
972  if (endpoint) {
973  ao2_ref(endpoint, +1);
974  }
975  return endpoint;
976 }
static pjsip_module endpoint_mod
#define ao2_ref(o, delta)
Definition: astobj2.h:464
An entity with which Asterisk communicates.
Definition: res_pjsip.h:812

◆ ast_sip_destroy_distributor()

void ast_sip_destroy_distributor ( void  )

Definition at line 1334 of file pjsip_distributor.c.

References ao2_cleanup, ao2_global_obj_release, ARRAY_LEN, ast_cli_unregister_multiple(), ast_sched_context_destroy(), ast_sip_get_sorcery(), ast_sip_unregister_cli_formatter(), ast_sip_unregister_service(), ast_sorcery_observer_remove(), auth_mod, distributor_mod, distributor_pool_shutdown(), and endpoint_mod.

Referenced by ast_sip_initialize_distributor(), and unload_pjsip().

1335 {
1338 
1342 
1343  ao2_global_obj_release(artificial_auth);
1345 
1347 
1348  if (prune_context) {
1350  }
1351 
1353 
1356 }
struct ast_sip_cli_formatter_entry * unid_formatter
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
static struct ao2_container * unidentified_requests
static struct ast_cli_entry cli_commands[]
static pjsip_module endpoint_mod
static pjsip_module auth_mod
static struct ao2_container * dialog_associations
static void distributor_pool_shutdown(void)
static struct ast_sip_endpoint * artificial_endpoint
int ast_sip_unregister_cli_formatter(struct ast_sip_cli_formatter_entry *formatter)
Unregisters a CLI formatter.
Definition: pjsip_cli.c:326
static pjsip_module distributor_mod
#define ao2_global_obj_release(holder)
Definition: astobj2.h:865
struct ast_sched_context * prune_context
void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
Remove an observer from a specific object type.
Definition: sorcery.c:2418
struct ast_sorcery * ast_sip_get_sorcery(void)
Get a pointer to the SIP sorcery structure.
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
void ast_sip_unregister_service(pjsip_module *module)
Definition: res_pjsip.c:3331
static struct ast_sorcery_observer global_observer
Observer which is used to update our interval and default_realm when the global setting changes...
void ast_sched_context_destroy(struct ast_sched_context *c)
destroys a schedule context
Definition: sched.c:269

◆ ast_sip_get_artificial_auth()

struct ast_sip_auth* ast_sip_get_artificial_auth ( void  )

Retrieves a reference to the artificial auth.

Return values
Theartificial auth

Definition at line 631 of file pjsip_distributor.c.

References ao2_global_obj_ref.

Referenced by digest_check_auth(), and global_loaded().

632 {
633  return ao2_global_obj_ref(artificial_auth);
634 }
#define ao2_global_obj_ref(holder)
Definition: astobj2.h:925

◆ ast_sip_get_artificial_endpoint()

struct ast_sip_endpoint* ast_sip_get_artificial_endpoint ( void  )

Retrieves a reference to the artificial endpoint.

Return values
Theartificial endpoint

Definition at line 654 of file pjsip_distributor.c.

References ao2_ref, and artificial_endpoint.

Referenced by digest_check_auth(), digest_requires_authentication(), endpoint_lookup(), and get_account_id().

655 {
657  return artificial_endpoint;
658 }
static struct ast_sip_endpoint * artificial_endpoint
#define ao2_ref(o, delta)
Definition: astobj2.h:464

◆ ast_sip_initialize_distributor()

int ast_sip_initialize_distributor ( void  )

Definition at line 1260 of file pjsip_distributor.c.

References AO2_ALLOC_OPT_LOCK_NOLOCK, AO2_ALLOC_OPT_LOCK_RWLOCK, ao2_alloc_options, ao2_container_alloc_hash, ARRAY_LEN, ast_cli_register_multiple, ast_log, ast_sched_context_create(), ast_sched_start_thread(), ast_sip_destroy_distributor(), ast_sip_get_sorcery(), ast_sip_register_cli_formatter(), ast_sip_register_service(), ast_sorcery_observer_add(), ast_sorcery_reload_object(), auth_mod, cli_unid_get_container(), cli_unid_get_id(), cli_unid_iterate(), cli_unid_print_body(), cli_unid_print_header(), cli_unid_retrieve_by_id(), create_artificial_auth(), create_artificial_endpoint(), DEFAULT_SUSPECTS_BUCKETS, DIALOG_ASSOCIATIONS_BUCKETS, dialog_associations_cmp(), dialog_associations_hash(), distributor_mod, distributor_pool_setup(), endpoint_mod, ast_sip_cli_formatter_entry::get_container, ast_sip_cli_formatter_entry::get_id, ast_sip_cli_formatter_entry::iterate, LOG_ERROR, ast_sip_cli_formatter_entry::name, NULL, ast_sip_cli_formatter_entry::print_body, ast_sip_cli_formatter_entry::print_header, ast_sip_cli_formatter_entry::retrieve_by_id, suspects_compare(), and suspects_hash().

Referenced by load_module().

1261 {
1264  if (!unidentified_requests) {
1265  return -1;
1266  }
1267 
1271  if (!dialog_associations) {
1273  return -1;
1274  }
1275 
1276  if (distributor_pool_setup()) {
1278  return -1;
1279  }
1280 
1282  if (!prune_context) {
1284  return -1;
1285  }
1286 
1289  return -1;
1290  }
1291 
1294 
1297  return -1;
1298  }
1299 
1302  return -1;
1303  }
1306  return -1;
1307  }
1310  return -1;
1311  }
1312 
1315  if (!unid_formatter) {
1317  ast_log(LOG_ERROR, "Unable to allocate memory for unid_formatter\n");
1318  return -1;
1319  }
1320  unid_formatter->name = "unidentified_request";
1328 
1330 
1331  return 0;
1332 }
int ast_sched_start_thread(struct ast_sched_context *con)
Start a thread for processing scheduler entries.
Definition: sched.c:195
static const char * cli_unid_get_id(const void *obj)
struct ao2_container *(* get_container)(const char *regex)
Definition: res_pjsip_cli.h:64
struct ast_sip_cli_formatter_entry * unid_formatter
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
Definition: res_pjsip_cli.h:52
static int create_artificial_endpoint(void)
static int create_artificial_auth(void)
static struct ao2_container * unidentified_requests
static int dialog_associations_hash(const void *obj, int flags)
static struct ao2_container * cli_unid_get_container(const char *regex)
int(* iterate)(void *container, ao2_callback_fn callback, void *args)
Definition: res_pjsip_cli.h:66
static struct ast_cli_entry cli_commands[]
static void * cli_unid_retrieve_by_id(const char *id)
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition: astobj2.h:406
#define NULL
Definition: resample.c:96
static pjsip_module endpoint_mod
static int dialog_associations_cmp(void *obj, void *arg, int flags)
static pjsip_module auth_mod
static int suspects_hash(const void *obj, int flags)
static struct ao2_container * dialog_associations
int ast_sip_register_cli_formatter(struct ast_sip_cli_formatter_entry *formatter)
Registers a CLI formatter.
Definition: pjsip_cli.c:310
void ast_sip_destroy_distributor(void)
#define ast_log
Definition: astobj2.c:42
void *(* retrieve_by_id)(const char *id)
Definition: res_pjsip_cli.h:68
int ast_sip_register_service(pjsip_module *module)
Register a SIP service in Asterisk.
Definition: res_pjsip.c:3315
int ast_sorcery_observer_add(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
Add an observer to a specific object type.
Definition: sorcery.c:2386
struct ast_sched_context * ast_sched_context_create(void)
Create a scheduler context.
Definition: sched.c:236
static int cli_unid_print_header(void *obj, void *arg, int flags)
static int cli_unid_iterate(void *container, ao2_callback_fn callback, void *args)
#define LOG_ERROR
Definition: logger.h:285
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Definition: astobj2.h:1310
static pjsip_module distributor_mod
static int suspects_compare(void *obj, void *arg, int flags)
struct ast_sched_context * prune_context
const char *(* get_id)(const void *obj)
Definition: res_pjsip_cli.h:70
static int distributor_pool_setup(void)
#define DEFAULT_SUSPECTS_BUCKETS
struct ast_sorcery * ast_sip_get_sorcery(void)
Get a pointer to the SIP sorcery structure.
static int cli_unid_print_body(void *obj, void *arg, int flags)
#define DIALOG_ASSOCIATIONS_BUCKETS
static struct ast_sorcery_observer global_observer
Observer which is used to update our interval and default_realm when the global setting changes...
ao2_callback_fn * print_header
Definition: res_pjsip_cli.h:60
ao2_callback_fn * print_body
Definition: res_pjsip_cli.h:62
const char * name
Definition: res_pjsip_cli.h:58
void ast_sorcery_reload_object(const struct ast_sorcery *sorcery, const char *type)
Inform any wizards of a specific object type to reload persistent objects.
Definition: sorcery.c:1442

◆ authenticate()

static pj_bool_t authenticate ( pjsip_rx_data *  rdata)
static

Definition at line 880 of file pjsip_distributor.c.

References ao2_cleanup, ao2_find, ao2_ref, ao2_unlink, ast_assert, ast_pjsip_rdata_get_endpoint(), AST_SIP_AUTHENTICATION_CHALLENGE, AST_SIP_AUTHENTICATION_ERROR, AST_SIP_AUTHENTICATION_FAILED, AST_SIP_AUTHENTICATION_SUCCESS, ast_sip_check_authentication(), ast_sip_get_pjsip_endpoint(), ast_sip_report_auth_challenge_sent(), ast_sip_report_auth_failed_challenge_response(), ast_sip_report_auth_success(), ast_sip_requires_authentication(), log_failed_request(), NULL, OBJ_SEARCH_KEY, and RAII_VAR.

881 {
883  int is_ack = rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD;
884 
885  ast_assert(endpoint != NULL);
886 
887  if (is_ack) {
888  return PJ_FALSE;
889  }
890 
891  if (ast_sip_requires_authentication(endpoint, rdata)) {
892  pjsip_tx_data *tdata;
893  struct unidentified_request *unid;
894 
895  pjsip_endpt_create_response(ast_sip_get_pjsip_endpoint(), rdata, 401, NULL, &tdata);
896  switch (ast_sip_check_authentication(endpoint, rdata, tdata)) {
898  /* Send the 401 we created for them */
899  ast_sip_report_auth_challenge_sent(endpoint, rdata, tdata);
900  if (pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL) != PJ_SUCCESS) {
901  pjsip_tx_data_dec_ref(tdata);
902  }
903  return PJ_TRUE;
905  /* See note in endpoint_lookup about not holding an unnecessary write lock */
906  unid = ao2_find(unidentified_requests, rdata->pkt_info.src_name, OBJ_SEARCH_KEY);
907  if (unid) {
909  ao2_ref(unid, -1);
910  }
911  ast_sip_report_auth_success(endpoint, rdata);
912  break;
914  log_failed_request(rdata, "Failed to authenticate", 0, 0);
916  if (pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL) != PJ_SUCCESS) {
917  pjsip_tx_data_dec_ref(tdata);
918  }
919  return PJ_TRUE;
921  log_failed_request(rdata, "Error to authenticate", 0, 0);
923  pjsip_tx_data_dec_ref(tdata);
924  pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
925  return PJ_TRUE;
926  }
927  pjsip_tx_data_dec_ref(tdata);
928  } else if (endpoint == artificial_endpoint) {
929  /* Uh. Oh. The artificial endpoint couldn't challenge so block the request. */
930  pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
931  return PJ_TRUE;
932  }
933 
934  return PJ_FALSE;
935 }
void ast_sip_report_auth_failed_challenge_response(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata)
Send a security event notification for when a challenge response has failed.
int ast_sip_requires_authentication(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata)
Determine if an incoming request requires authentication.
Definition: res_pjsip.c:3361
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
static struct ao2_container * unidentified_requests
#define ast_assert(a)
Definition: utils.h:695
#define NULL
Definition: resample.c:96
static void log_failed_request(pjsip_rx_data *rdata, char *msg, unsigned int count, unsigned int period)
#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
static struct ast_sip_endpoint * artificial_endpoint
#define ao2_ref(o, delta)
Definition: astobj2.h:464
An entity with which Asterisk communicates.
Definition: res_pjsip.h:812
#define ao2_unlink(container, obj)
Definition: astobj2.h:1598
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
pjsip_endpoint * ast_sip_get_pjsip_endpoint(void)
Get a pointer to the PJSIP endpoint.
Definition: res_pjsip.c:3718
struct ast_sip_endpoint * ast_pjsip_rdata_get_endpoint(pjsip_rx_data *rdata)
Get the looked-up endpoint on an out-of dialog request or response.
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
void ast_sip_report_auth_challenge_sent(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, pjsip_tx_data *tdata)
Send a security event notification for when an authentication challenge is sent.
void ast_sip_report_auth_success(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata)
Send a security event notification for when authentication succeeds.
enum ast_sip_check_auth_result ast_sip_check_authentication(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, pjsip_tx_data *tdata)
Method to determine authentication status of an incoming request.
Definition: res_pjsip.c:3377

◆ buf_hash()

static int buf_hash ( const char *  pos,
size_t  len 
)
static

Definition at line 210 of file pjsip_distributor.c.

References buf_hash_add().

Referenced by dialog_associations_hash().

211 {
212  return buf_hash_add(pos, len, 5381);
213 }
static int buf_hash_add(const char *pos, size_t len, int hash)
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)

◆ buf_hash_add()

static int buf_hash_add ( const char *  pos,
size_t  len,
int  hash 
)
static

Definition at line 184 of file pjsip_distributor.c.

Referenced by buf_hash(), and pjstr_hash_add().

185 {
186  while (len--) {
187  hash = hash * 33 ^ *pos++;
188  }
189 
190  return hash;
191 }
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)

◆ check_endpoint()

static void check_endpoint ( pjsip_rx_data *  rdata,
struct unidentified_request unid,
const char *  name 
)
static

Definition at line 683 of file pjsip_distributor.c.

References ao2_unlock, ao2_wrlock, apply_endpoint_acl(), apply_endpoint_contact_acl(), ast_sip_report_invalid_endpoint(), ast_tvdiff_ms(), ast_tvnow(), unidentified_request::count, unidentified_request::first_seen, log_failed_request(), unidentified_count, and unidentified_period.

Referenced by endpoint_lookup().

685 {
686  int64_t ms = ast_tvdiff_ms(ast_tvnow(), unid->first_seen);
687 
688  ao2_wrlock(unid);
689  unid->count++;
690 
691  if (ms < (unidentified_period * 1000) && unid->count >= unidentified_count) {
692  log_failed_request(rdata, "No matching endpoint found", unid->count, ms);
694  }
695  ao2_unlock(unid);
696 }
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
#define ao2_unlock(a)
Definition: astobj2.h:730
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:98
static void log_failed_request(pjsip_rx_data *rdata, char *msg, unsigned int count, unsigned int period)
#define ao2_wrlock(a)
Definition: astobj2.h:720
static unsigned int unidentified_period
void ast_sip_report_invalid_endpoint(const char *name, pjsip_rx_data *rdata)
Send a security event notification for when an invalid endpoint is requested.
static const char name[]
Definition: cdr_mysql.c:74
static unsigned int unidentified_count
struct timeval first_seen

◆ clean_task()

static int clean_task ( const void *  data)
static

Definition at line 1160 of file pjsip_distributor.c.

Referenced by global_loaded().

1161 {
1162  return 0;
1163 }

◆ cli_unid_get_container()

static struct ao2_container* cli_unid_get_container ( const char *  regex)
static

Definition at line 1051 of file pjsip_distributor.c.

References AO2_ALLOC_OPT_LOCK_NOLOCK, ao2_container_alloc_list, ao2_container_dup(), ao2_ref, NULL, suspects_compare(), and suspects_sort().

Referenced by ast_sip_initialize_distributor().

1052 {
1053  struct ao2_container *s_container;
1054 
1057  if (!s_container) {
1058  return NULL;
1059  }
1060 
1061  if (ao2_container_dup(s_container, unidentified_requests, 0)) {
1062  ao2_ref(s_container, -1);
1063  return NULL;
1064  }
1065 
1066  return s_container;
1067 }
static struct ao2_container * unidentified_requests
#define ao2_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn)
Definition: astobj2.h:1335
#define NULL
Definition: resample.c:96
static int suspects_sort(const void *obj, const void *arg, int flags)
#define ao2_ref(o, delta)
Definition: astobj2.h:464
int ao2_container_dup(struct ao2_container *dest, struct ao2_container *src, enum search_flags flags)
Copy all object references in the src container into the dest container.
static int suspects_compare(void *obj, void *arg, int flags)
Generic container type.

◆ cli_unid_get_id()

static const char* cli_unid_get_id ( const void *  obj)
static

Definition at line 1081 of file pjsip_distributor.c.

References unidentified_request::src_name.

Referenced by ast_sip_initialize_distributor().

1082 {
1083  const struct unidentified_request *unid = obj;
1084 
1085  return unid->src_name;
1086 }

◆ cli_unid_iterate()

static int cli_unid_iterate ( void *  container,
ao2_callback_fn  callback,
void *  args 
)
static

Definition at line 1069 of file pjsip_distributor.c.

References ao2_callback.

Referenced by ast_sip_initialize_distributor().

1070 {
1071  ao2_callback(container, 0, callback, args);
1072 
1073  return 0;
1074 }
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
const char * args
struct ao2_container * container
Definition: res_fax.c:502

◆ cli_unid_print_body()

static int cli_unid_print_body ( void *  obj,
void *  arg,
int  flags 
)
static

Definition at line 1105 of file pjsip_distributor.c.

References ast_assert, ast_str_append(), ast_tvdiff_ms(), ast_tvnow(), CLI_INDENT_TO_SPACES, CLI_LAST_TABSTOP, context, unidentified_request::count, unidentified_request::first_seen, ast_sip_cli_context::indent_level, NULL, ast_sip_cli_context::output_buffer, and unidentified_request::src_name.

Referenced by ast_sip_initialize_distributor().

1106 {
1107  struct unidentified_request *unid = obj;
1108  struct ast_sip_cli_context *context = arg;
1109  int indent;
1110  int flexwidth;
1111  int64_t ms = ast_tvdiff_ms(ast_tvnow(), unid->first_seen);
1112 
1113  ast_assert(context->output_buffer != NULL);
1114 
1115  indent = CLI_INDENT_TO_SPACES(context->indent_level);
1116  flexwidth = CLI_LAST_TABSTOP - 4;
1117 
1118  ast_str_append(&context->output_buffer, 0, "%*s: %-*.*s %7d %10.3f\n",
1119  indent,
1120  "Request",
1121  flexwidth, flexwidth,
1122  unid->src_name, unid->count, ms / 1000.0);
1123 
1124  return 0;
1125 }
struct ast_str * output_buffer
Definition: res_pjsip_cli.h:36
#define CLI_LAST_TABSTOP
Definition: res_pjsip_cli.h:27
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
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
#define ast_assert(a)
Definition: utils.h:695
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:98
#define NULL
Definition: resample.c:96
#define CLI_INDENT_TO_SPACES(x)
Definition: res_pjsip_cli.h:29
struct timeval first_seen
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116

◆ cli_unid_print_header()

static int cli_unid_print_header ( void *  obj,
void *  arg,
int  flags 
)
static

Definition at line 1088 of file pjsip_distributor.c.

References ao2_cleanup, ast_assert, ast_str_append(), CLI_HEADER_FILLER, CLI_INDENT_TO_SPACES, CLI_LAST_TABSTOP, context, ast_sip_cli_context::indent_level, NULL, ast_sip_cli_context::output_buffer, and RAII_VAR.

Referenced by ast_sip_initialize_distributor().

1089 {
1090  struct ast_sip_cli_context *context = arg;
1091  RAII_VAR(struct ast_sip_cli_formatter_entry *, formatter_entry, NULL, ao2_cleanup);
1092 
1093  int indent = CLI_INDENT_TO_SPACES(context->indent_level);
1094  int filler = CLI_LAST_TABSTOP - indent - 7;
1095 
1096  ast_assert(context->output_buffer != NULL);
1097 
1098  ast_str_append(&context->output_buffer, 0,
1099  "%*s: <IP Address%*.*s> <Count> <Age(sec)>\n",
1100  indent, "Request", filler, filler, CLI_HEADER_FILLER);
1101 
1102  return 0;
1103 }
struct ast_str * output_buffer
Definition: res_pjsip_cli.h:36
Definition: res_pjsip_cli.h:52
#define CLI_LAST_TABSTOP
Definition: res_pjsip_cli.h:27
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
#define ast_assert(a)
Definition: utils.h:695
#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 CLI_INDENT_TO_SPACES(x)
Definition: res_pjsip_cli.h:29
#define CLI_HEADER_FILLER
Definition: res_pjsip_cli.h:24
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116

◆ cli_unid_retrieve_by_id()

static void* cli_unid_retrieve_by_id ( const char *  id)
static

Definition at line 1076 of file pjsip_distributor.c.

References ao2_find, and OBJ_SEARCH_KEY.

Referenced by ast_sip_initialize_distributor().

1077 {
1079 }
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
static struct ao2_container * unidentified_requests
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756

◆ create_artificial_auth()

static int create_artificial_auth ( void  )
static

Definition at line 614 of file pjsip_distributor.c.

References alloc_artificial_auth(), ao2_global_obj_replace_unref, ao2_ref, ast_log, ast_sip_get_default_realm(), default_realm, LOG_ERROR, and MAX_REALM_LENGTH.

Referenced by ast_sip_initialize_distributor().

615 {
617  struct ast_sip_auth *fake_auth;
618 
619  ast_sip_get_default_realm(default_realm, sizeof(default_realm));
620  fake_auth = alloc_artificial_auth(default_realm);
621  if (!fake_auth) {
622  ast_log(LOG_ERROR, "Unable to create artificial auth\n");
623  return -1;
624  }
625 
626  ao2_global_obj_replace_unref(artificial_auth, fake_auth);
627  ao2_ref(fake_auth, -1);
628  return 0;
629 }
static char default_realm[MAX_REALM_LENGTH+1]
void ast_sip_get_default_realm(char *realm, size_t size)
Retrieve the global default realm.
#define ast_log
Definition: astobj2.c:42
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define LOG_ERROR
Definition: logger.h:285
#define ao2_global_obj_replace_unref(holder, obj)
Definition: astobj2.h:908
#define MAX_REALM_LENGTH
static struct ast_sip_auth * alloc_artificial_auth(char *default_realm)

◆ create_artificial_endpoint()

static int create_artificial_endpoint ( void  )
static

Definition at line 638 of file pjsip_distributor.c.

References ast_sip_get_sorcery(), ast_sorcery_alloc(), ast_strdup, AST_VECTOR_APPEND, AST_VECTOR_INIT, ast_sip_endpoint::inbound_auths, and NULL.

Referenced by ast_sip_initialize_distributor().

639 {
641  if (!artificial_endpoint) {
642  return -1;
643  }
644 
646  /* Pushing a bogus value into the vector will ensure that
647  * the proper size of the vector is returned. This value is
648  * not actually used anywhere
649  */
651  return 0;
652 }
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition: vector.h:256
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
#define NULL
Definition: resample.c:96
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
Definition: vector.h:113
static struct ast_sip_endpoint * artificial_endpoint
struct ast_sip_auth_vector inbound_auths
Definition: res_pjsip.h:853
void * ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
Allocate an object.
Definition: sorcery.c:1744
struct ast_sorcery * ast_sip_get_sorcery(void)
Get a pointer to the SIP sorcery structure.

◆ dialog_associations_cmp()

static int dialog_associations_cmp ( void *  obj,
void *  arg,
int  flags 
)
static

Definition at line 239 of file pjsip_distributor.c.

References ast_assert, CMP_MATCH, distributor_dialog_data::dlg, OBJ_SEARCH_KEY, OBJ_SEARCH_MASK, OBJ_SEARCH_OBJECT, and OBJ_SEARCH_PARTIAL_KEY.

Referenced by ast_sip_initialize_distributor().

240 {
241  const struct distributor_dialog_data *object_left = obj;
242  const struct distributor_dialog_data *object_right = arg;
243  const pjsip_dialog *right_key = arg;
244  int cmp = 0;
245 
246  switch (flags & OBJ_SEARCH_MASK) {
247  case OBJ_SEARCH_OBJECT:
248  right_key = object_right->dlg;
249  /* Fall through */
250  case OBJ_SEARCH_KEY:
251  if (object_left->dlg == right_key) {
252  cmp = CMP_MATCH;
253  }
254  break;
256  /* There is no such thing for this container. */
257  ast_assert(0);
258  break;
259  default:
260  cmp = 0;
261  break;
262  }
263  return cmp;
264 }
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
#define ast_assert(a)
Definition: utils.h:695
The arg parameter is a partial search key similar to OBJ_SEARCH_KEY.
Definition: astobj2.h:1120
The arg parameter is an object of the same type.
Definition: astobj2.h:1091
Search option field mask.
Definition: astobj2.h:1076

◆ dialog_associations_hash()

static int dialog_associations_hash ( const void *  obj,
int  flags 
)
static

Definition at line 215 of file pjsip_distributor.c.

References ast_assert, ast_str_hash_restrict(), buf, buf_hash(), OBJ_SEARCH_KEY, OBJ_SEARCH_MASK, and OBJ_SEARCH_OBJECT.

Referenced by ast_sip_initialize_distributor().

216 {
217  const struct distributor_dialog_data *object;
218  union {
219  const pjsip_dialog *dlg;
220  const char buf[sizeof(pjsip_dialog *)];
221  } key;
222 
223  switch (flags & OBJ_SEARCH_MASK) {
224  case OBJ_SEARCH_KEY:
225  key.dlg = obj;
226  break;
227  case OBJ_SEARCH_OBJECT:
228  object = obj;
229  key.dlg = object->dlg;
230  break;
231  default:
232  /* Hash can only work on something with a full key. */
233  ast_assert(0);
234  return 0;
235  }
236  return ast_str_hash_restrict(buf_hash(key.buf, sizeof(key.buf)));
237 }
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define ast_assert(a)
Definition: utils.h:695
static int buf_hash(const char *pos, size_t len)
The arg parameter is an object of the same type.
Definition: astobj2.h:1091
static force_inline int attribute_pure ast_str_hash_restrict(unsigned int hash)
Restrict hash value range.
Definition: strings.h:1193
Search option field mask.
Definition: astobj2.h:1076

◆ distribute()

static int distribute ( void *  data)
static

Definition at line 943 of file pjsip_distributor.c.

References ao2_cleanup, ast_sip_get_pjsip_endpoint(), distributor_mod, endpoint_mod, and NULL.

Referenced by distributor().

944 {
945  static pjsip_process_rdata_param param = {
946  .start_mod = &distributor_mod,
947  .idx_after_start = 1,
948  };
949  pj_bool_t handled = PJ_FALSE;
950  pjsip_rx_data *rdata = data;
951  int is_request = rdata->msg_info.msg->type == PJSIP_REQUEST_MSG;
952  int is_ack = is_request ? rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD : 0;
953  struct ast_sip_endpoint *endpoint;
954 
955  pjsip_endpt_process_rx_data(ast_sip_get_pjsip_endpoint(), rdata, &param, &handled);
956  if (!handled && is_request && !is_ack) {
957  pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 501, NULL, NULL, NULL);
958  }
959 
960  /* The endpoint_mod stores an endpoint reference in the mod_data of rdata. This
961  * is the only appropriate spot to actually decrement the reference.
962  */
963  endpoint = rdata->endpt_info.mod_data[endpoint_mod.id];
964  ao2_cleanup(endpoint);
965  pjsip_rx_data_free_cloned(rdata);
966  return 0;
967 }
#define NULL
Definition: resample.c:96
static pjsip_module endpoint_mod
An entity with which Asterisk communicates.
Definition: res_pjsip.h:812
static pjsip_module distributor_mod
pjsip_endpoint * ast_sip_get_pjsip_endpoint(void)
Get a pointer to the PJSIP endpoint.
Definition: res_pjsip.c:3718
#define ao2_cleanup(obj)
Definition: astobj2.h:1958

◆ distributor()

static pj_bool_t distributor ( pjsip_rx_data *  rdata)
static

Definition at line 485 of file pjsip_distributor.c.

References ao2_bump, ao2_cleanup, ao2_find, ao2_lock, ao2_unlock, ast_debug, AST_OPT_FLAG_FULLY_BOOTED, ast_options, ast_sip_get_distributor_serializer(), ast_sip_get_pjsip_endpoint(), ast_sip_push_task(), ast_taskprocessor_alert_get(), ast_taskprocessor_get_subsystem_alert(), ast_taskprocessor_name(), ast_taskprocessor_unreference(), ast_test_flag, distribute(), distributor_dialog_data::endpoint, endpoint_mod, find_dialog(), find_request_serializer(), NULL, OBJ_SEARCH_KEY, overload_trigger, distributor_dialog_data::serializer, TASKPROCESSOR_OVERLOAD_TRIGGER_GLOBAL, and TASKPROCESSOR_OVERLOAD_TRIGGER_PJSIP_ONLY.

486 {
487  pjsip_dialog *dlg;
488  struct distributor_dialog_data *dist = NULL;
490  pjsip_rx_data *clone;
491 
493  /*
494  * Ignore everything until we are fully booted. Let the
495  * peer retransmit messages until we are ready.
496  */
497  return PJ_TRUE;
498  }
499 
500  dlg = find_dialog(rdata);
501  if (dlg) {
502  ast_debug(3, "Searching for serializer associated with dialog %s for %s\n",
503  dlg->obj_name, pjsip_rx_data_get_info(rdata));
505  if (dist) {
506  ao2_lock(dist);
507  serializer = ao2_bump(dist->serializer);
508  ao2_unlock(dist);
509  if (serializer) {
510  ast_debug(3, "Found serializer %s associated with dialog %s\n",
511  ast_taskprocessor_name(serializer), dlg->obj_name);
512  }
513  }
514  }
515 
516  if (serializer) {
517  /* We have a serializer so we know where to send the message. */
518  } else if (rdata->msg_info.msg->type == PJSIP_RESPONSE_MSG) {
519  ast_debug(3, "No dialog serializer for %s. Using request transaction as basis.\n",
520  pjsip_rx_data_get_info(rdata));
521  serializer = find_request_serializer(rdata);
522  if (!serializer) {
523  /*
524  * Pick a serializer for the unmatched response.
525  * We couldn't determine what serializer originally
526  * sent the request or the serializer is gone.
527  */
528  serializer = ast_sip_get_distributor_serializer(rdata);
529  }
530  } else if (!pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_cancel_method)
531  || !pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_bye_method)) {
532  /* We have a BYE or CANCEL request without a serializer. */
533  pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata,
534  PJSIP_SC_CALL_TSX_DOES_NOT_EXIST, NULL, NULL, NULL);
535  ao2_cleanup(dist);
536  return PJ_TRUE;
537  } else {
542  /*
543  * When taskprocessors get backed up, there is a good chance that
544  * we are being overloaded and need to defer adding new work to
545  * the system. To defer the work we will ignore the request and
546  * rely on the peer's transport layer to retransmit the message.
547  * We usually work off the overload within a few seconds.
548  * If transport is non-UDP we send a 503 response instead.
549  */
550  switch (rdata->tp_info.transport->key.type) {
551  case PJSIP_TRANSPORT_UDP6:
552  case PJSIP_TRANSPORT_UDP:
553  ast_debug(3, "Taskprocessor overload alert: Ignoring '%s'.\n",
554  pjsip_rx_data_get_info(rdata));
555  break;
556  default:
557  ast_debug(3, "Taskprocessor overload on non-udp transport. Received:'%s'. "
558  "Responding with a 503.\n", pjsip_rx_data_get_info(rdata));
559  pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata,
560  PJSIP_SC_SERVICE_UNAVAILABLE, NULL, NULL, NULL);
561  break;
562  }
563  ao2_cleanup(dist);
564  return PJ_TRUE;
565  }
566 
567  /* Pick a serializer for the out-of-dialog request. */
568  serializer = ast_sip_get_distributor_serializer(rdata);
569  }
570 
571  if (pjsip_rx_data_clone(rdata, 0, &clone) != PJ_SUCCESS) {
572  ast_taskprocessor_unreference(serializer);
573  ao2_cleanup(dist);
574  return PJ_TRUE;
575  }
576 
577  if (dist) {
578  ao2_lock(dist);
579  clone->endpt_info.mod_data[endpoint_mod.id] = ao2_bump(dist->endpoint);
580  ao2_unlock(dist);
581  ao2_cleanup(dist);
582  }
583 
584  if (ast_sip_push_task(serializer, distribute, clone)) {
585  ao2_cleanup(clone->endpt_info.mod_data[endpoint_mod.id]);
586  pjsip_rx_data_free_cloned(clone);
587  }
588 
589  ast_taskprocessor_unreference(serializer);
590 
591  return PJ_TRUE;
592 }
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
#define ast_test_flag(p, flag)
Definition: utils.h:63
static struct ast_taskprocessor * find_request_serializer(pjsip_rx_data *rdata)
const char * ast_taskprocessor_name(struct ast_taskprocessor *tps)
Return the name of the taskprocessor singleton.
static int distribute(void *data)
#define ao2_unlock(a)
Definition: astobj2.h:730
#define NULL
Definition: resample.c:96
static pjsip_module endpoint_mod
static struct ao2_container * dialog_associations
#define ao2_bump(obj)
Definition: astobj2.h:491
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ao2_lock(a)
Definition: astobj2.h:718
struct ast_sip_endpoint * endpoint
int ast_sip_push_task(struct ast_taskprocessor *serializer, int(*sip_task)(void *), void *task_data)
Pushes a task to SIP servants.
Definition: res_pjsip.c:5138
unsigned int ast_taskprocessor_get_subsystem_alert(const char *subsystem)
Get the current taskprocessor high water alert count by sybsystem.
struct ast_taskprocessor * ast_sip_get_distributor_serializer(pjsip_rx_data *rdata)
Determine the distributor serializer for the SIP message.
struct ast_taskprocessor * serializer
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
pjsip_endpoint * ast_sip_get_pjsip_endpoint(void)
Get a pointer to the PJSIP endpoint.
Definition: res_pjsip.c:3718
struct ast_flags ast_options
Definition: options.c:61
A ast_taskprocessor structure is a singleton by name.
Definition: taskprocessor.c:69
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
void * ast_taskprocessor_unreference(struct ast_taskprocessor *tps)
Unreference the specified taskprocessor and its reference count will decrement.
unsigned int ast_taskprocessor_alert_get(void)
Get the current taskprocessor high water alert count.
static enum ast_sip_taskprocessor_overload_trigger overload_trigger
static pjsip_dialog * find_dialog(pjsip_rx_data *rdata)

◆ distributor_pool_setup()

static int distributor_pool_setup ( void  )
static

Definition at line 1243 of file pjsip_distributor.c.

References ARRAY_LEN, ast_sip_create_serializer(), ast_taskprocessor_build_name(), and AST_TASKPROCESSOR_MAX_NAME.

Referenced by ast_sip_initialize_distributor().

1244 {
1245  char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1];
1246  int idx;
1247 
1248  for (idx = 0; idx < ARRAY_LEN(distributor_pool); ++idx) {
1249  /* Create name with seq number appended. */
1250  ast_taskprocessor_build_name(tps_name, sizeof(tps_name), "pjsip/distributor");
1251 
1253  if (!distributor_pool[idx]) {
1254  return -1;
1255  }
1256  }
1257  return 0;
1258 }
void ast_taskprocessor_build_name(char *buf, unsigned int size, const char *format,...)
Build a taskprocessor name with a sequence number on the end.
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
#define AST_TASKPROCESSOR_MAX_NAME
Suggested maximum taskprocessor name length (less null terminator).
Definition: taskprocessor.h:60
struct ast_taskprocessor * ast_sip_create_serializer(const char *name)
Create a new serializer for SIP tasks.
Definition: res_pjsip.c:5133
static struct ast_taskprocessor * distributor_pool[DISTRIBUTOR_POOL_SIZE]

◆ distributor_pool_shutdown()

static void distributor_pool_shutdown ( void  )
static

Definition at line 1225 of file pjsip_distributor.c.

References ARRAY_LEN, ast_taskprocessor_unreference(), and NULL.

Referenced by ast_sip_destroy_distributor().

1226 {
1227  int idx;
1228 
1229  for (idx = 0; idx < ARRAY_LEN(distributor_pool); ++idx) {
1231  distributor_pool[idx] = NULL;
1232  }
1233 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
#define NULL
Definition: resample.c:96
static struct ast_taskprocessor * distributor_pool[DISTRIBUTOR_POOL_SIZE]
void * ast_taskprocessor_unreference(struct ast_taskprocessor *tps)
Unreference the specified taskprocessor and its reference count will decrement.

◆ endpoint_lookup()

static pj_bool_t endpoint_lookup ( pjsip_rx_data *  rdata)
static

Definition at line 718 of file pjsip_distributor.c.

References AO2_ALLOC_OPT_LOCK_RWLOCK, ao2_alloc_options, ao2_find, ao2_link_flags, ao2_ref, ao2_unlink, ao2_unlock, ao2_wrlock, apply_acls(), ast_copy_pj_str(), ast_sip_get_artificial_endpoint(), ast_sip_get_pjsip_endpoint(), ast_sip_identify_endpoint(), ast_sip_report_invalid_endpoint(), ast_tvnow(), AST_UUID_STR_LEN, check_endpoint(), unidentified_request::count, endpoint_mod, unidentified_request::first_seen, log_failed_request(), name, NULL, OBJ_NOLOCK, OBJ_SEARCH_KEY, unidentified_request::src_name, and using_auth_username.

Referenced by ast_sip_get_distributor_serializer().

719 {
720  struct ast_sip_endpoint *endpoint;
721  struct unidentified_request *unid;
722  int is_ack = rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD;
723 
724  endpoint = rdata->endpt_info.mod_data[endpoint_mod.id];
725  if (endpoint) {
726  /*
727  * ao2_find with OBJ_UNLINK always write locks the container before even searching
728  * for the object. Since the majority case is that the object won't be found, do
729  * the find without OBJ_UNLINK to prevent the unnecessary write lock, then unlink
730  * if needed.
731  */
732  unid = ao2_find(unidentified_requests, rdata->pkt_info.src_name, OBJ_SEARCH_KEY);
733  if (unid) {
735  ao2_ref(unid, -1);
736  }
737  apply_acls(rdata);
738  return PJ_FALSE;
739  }
740 
741  endpoint = ast_sip_identify_endpoint(rdata);
742  if (endpoint) {
743  unid = ao2_find(unidentified_requests, rdata->pkt_info.src_name, OBJ_SEARCH_KEY);
744  if (unid) {
746  ao2_ref(unid, -1);
747  }
748  }
749 
750  if (!endpoint) {
751  /* always use an artificial endpoint - per discussion no reason
752  to have "alwaysauthreject" as an option. It is felt using it
753  was a bug fix and it is not needed since we are not worried about
754  breaking old stuff and we really don't want to enable the discovery
755  of SIP accounts */
756  endpoint = ast_sip_get_artificial_endpoint();
757  }
758 
759  /* endpoint ref held by mod_data[] */
760  rdata->endpt_info.mod_data[endpoint_mod.id] = endpoint;
761 
762  if (endpoint == artificial_endpoint && !is_ack) {
763  char name[AST_UUID_STR_LEN] = "";
764  pjsip_uri *from = rdata->msg_info.from->uri;
765 
766  if (PJSIP_URI_SCHEME_IS_SIP(from) || PJSIP_URI_SCHEME_IS_SIPS(from)) {
767  pjsip_sip_uri *sip_from = pjsip_uri_get_uri(from);
768  ast_copy_pj_str(name, &sip_from->user, sizeof(name));
769  }
770 
771  unid = ao2_find(unidentified_requests, rdata->pkt_info.src_name, OBJ_SEARCH_KEY);
772  if (unid) {
773  check_endpoint(rdata, unid, name);
774  ao2_ref(unid, -1);
775  } else if (using_auth_username) {
777  /* Checking again with the write lock held allows us to eliminate the DUPS_REPLACE and sort_fn */
778  unid = ao2_find(unidentified_requests, rdata->pkt_info.src_name,
780  if (unid) {
781  check_endpoint(rdata, unid, name);
782  } else {
783  unid = ao2_alloc_options(sizeof(*unid) + strlen(rdata->pkt_info.src_name) + 1,
785  if (!unid) {
787  pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL);
788  return PJ_TRUE;
789  }
790  strcpy(unid->src_name, rdata->pkt_info.src_name); /* Safe */
791  unid->first_seen = ast_tvnow();
792  unid->count = 1;
794  }
795  ao2_ref(unid, -1);
797  } else {
798  log_failed_request(rdata, "No matching endpoint found", 0, 0);
799  ast_sip_report_invalid_endpoint(name, rdata);
800  }
801  }
802 
803  apply_acls(rdata);
804  return PJ_FALSE;
805 }
#define AST_UUID_STR_LEN
Definition: uuid.h:27
struct ast_sip_endpoint * ast_sip_get_artificial_endpoint(void)
Retrieves a reference to the artificial endpoint.
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
static void check_endpoint(pjsip_rx_data *rdata, struct unidentified_request *unid, const char *name)
static struct ao2_container * unidentified_requests
static int using_auth_username
Assume that the ao2_container is already locked.
Definition: astobj2.h:1067
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition: astobj2.h:406
#define ao2_link_flags(container, obj, flags)
Definition: astobj2.h:1572
#define ao2_unlock(a)
Definition: astobj2.h:730
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
static void log_failed_request(pjsip_rx_data *rdata, char *msg, unsigned int count, unsigned int period)
#define ao2_wrlock(a)
Definition: astobj2.h:720
static pjsip_module endpoint_mod
static struct ast_sip_endpoint * artificial_endpoint
#define ao2_ref(o, delta)
Definition: astobj2.h:464
struct ast_sip_endpoint * ast_sip_identify_endpoint(pjsip_rx_data *rdata)
Determine the endpoint that has sent a SIP message.
Definition: res_pjsip.c:3528
An entity with which Asterisk communicates.
Definition: res_pjsip.h:812
#define ao2_unlink(container, obj)
Definition: astobj2.h:1598
void ast_sip_report_invalid_endpoint(const char *name, pjsip_rx_data *rdata)
Send a security event notification for when an invalid endpoint is requested.
static const char name[]
Definition: cdr_mysql.c:74
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
pjsip_endpoint * ast_sip_get_pjsip_endpoint(void)
Get a pointer to the PJSIP endpoint.
Definition: res_pjsip.c:3718
static void apply_acls(pjsip_rx_data *rdata)
struct timeval first_seen

◆ expire_requests()

static int expire_requests ( void *  object,
void *  arg,
int  flags 
)
static

Definition at line 1136 of file pjsip_distributor.c.

References ast_tvdiff_ms(), ast_tvnow(), CMP_MATCH, and unidentified_request::first_seen.

Referenced by prune_task().

1137 {
1138  struct unidentified_request *unid = object;
1139  int *maxage = arg;
1140  int64_t ms = ast_tvdiff_ms(ast_tvnow(), unid->first_seen);
1141 
1142  if (ms > (*maxage) * 2 * 1000) {
1143  return CMP_MATCH;
1144  }
1145 
1146  return 0;
1147 }
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:98
struct timeval first_seen

◆ extract_contact_addr()

static int extract_contact_addr ( pjsip_contact_hdr *  contact,
struct ast_sockaddr **  addrs 
)
static

Definition at line 827 of file pjsip_distributor.c.

References AST_AF_UNSPEC, ast_copy_pj_str(), ast_sockaddr_resolve(), host, NULL, and PARSE_PORT_FORBID.

Referenced by apply_endpoint_contact_acl().

828 {
829  pjsip_sip_uri *sip_uri;
830  char host[256];
831 
832  if (!contact || contact->star) {
833  *addrs = NULL;
834  return 0;
835  }
836  if (!PJSIP_URI_SCHEME_IS_SIP(contact->uri) && !PJSIP_URI_SCHEME_IS_SIPS(contact->uri)) {
837  *addrs = NULL;
838  return 0;
839  }
840  sip_uri = pjsip_uri_get_uri(contact->uri);
841  ast_copy_pj_str(host, &sip_uri->host, sizeof(host));
843 }
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
static char host[256]
Definition: muted.c:77
int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str, int flags, int family)
Parses a string with an IPv4 or IPv6 address and place results into an array.
Definition: netsock2.c:280

◆ find_dialog()

static pjsip_dialog* find_dialog ( pjsip_rx_data *  rdata)
static

Definition at line 339 of file pjsip_distributor.c.

References ast_debug, and NULL.

Referenced by distributor().

340 {
341  pj_str_t tsx_key;
342  pjsip_transaction *tsx;
343  pjsip_dialog *dlg;
344  pj_str_t *local_tag;
345  pj_str_t *remote_tag;
346 
347  if (!rdata->msg_info.msg) {
348  return NULL;
349  }
350 
351  if (rdata->msg_info.msg->type == PJSIP_REQUEST_MSG) {
352  local_tag = &rdata->msg_info.to->tag;
353  remote_tag = &rdata->msg_info.from->tag;
354  } else {
355  local_tag = &rdata->msg_info.from->tag;
356  remote_tag = &rdata->msg_info.to->tag;
357  }
358 
359  /* We can only call the convenient method for
360  * 1) responses
361  * 2) non-CANCEL requests
362  * 3) CANCEL requests with a to-tag
363  */
364  if (rdata->msg_info.msg->type == PJSIP_RESPONSE_MSG ||
365  pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_cancel_method) ||
366  rdata->msg_info.to->tag.slen != 0) {
367  dlg = pjsip_ua_find_dialog(&rdata->msg_info.cid->id, local_tag,
368  remote_tag, PJ_FALSE);
369  if (dlg) {
370  return dlg;
371  }
372  }
373 
374  /*
375  * There may still be a matching dialog if this is
376  * 1) an incoming CANCEL request without a to-tag
377  * 2) an incoming response to a dialog-creating request.
378  */
379  if (rdata->msg_info.msg->type == PJSIP_REQUEST_MSG) {
380  /* CANCEL requests will need to match the INVITE we initially received. Any
381  * other request type will either have been matched already or is not in
382  * dialog
383  */
384  pjsip_tsx_create_key(rdata->tp_info.pool, &tsx_key, PJSIP_ROLE_UAS,
385  pjsip_get_invite_method(), rdata);
386  } else {
387  pjsip_tsx_create_key(rdata->tp_info.pool, &tsx_key, PJSIP_ROLE_UAC,
388  &rdata->msg_info.cseq->method, rdata);
389  }
390 
391  tsx = pjsip_tsx_layer_find_tsx(&tsx_key, PJ_TRUE);
392  if (!tsx) {
393  ast_debug(3, "Could not find matching transaction for %s\n",
394  pjsip_rx_data_get_info(rdata));
395  return NULL;
396  }
397 
398  dlg = pjsip_tsx_get_dlg(tsx);
399 
400 #ifdef HAVE_PJ_TRANSACTION_GRP_LOCK
401  pj_grp_lock_release(tsx->grp_lock);
402 #else
403  pj_mutex_unlock(tsx->mutex);
404 #endif
405 
406  return dlg;
407 }
#define NULL
Definition: resample.c:96
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452

◆ find_request_serializer()

static struct ast_taskprocessor* find_request_serializer ( pjsip_rx_data *  rdata)
static

Definition at line 112 of file pjsip_distributor.c.

References ast_debug, ast_strlen_zero, ast_taskprocessor_get(), distributor_mod, NULL, and TPS_REF_IF_EXISTS.

Referenced by distributor().

113 {
115  pj_str_t tsx_key;
116  pjsip_transaction *tsx;
117 
118  pjsip_tsx_create_key(rdata->tp_info.pool, &tsx_key, PJSIP_ROLE_UAC,
119  &rdata->msg_info.cseq->method, rdata);
120 
121  tsx = pjsip_tsx_layer_find_tsx(&tsx_key, PJ_TRUE);
122  if (!tsx) {
123  ast_debug(1, "Could not find transaction for %s.\n",
124  pjsip_rx_data_get_info(rdata));
125  return NULL;
126  }
127  ast_debug(3, "Found transaction %s for %s.\n",
128  tsx->obj_name, pjsip_rx_data_get_info(rdata));
129 
130  if (tsx->last_tx) {
131  const char *serializer_name;
132 
133  serializer_name = tsx->last_tx->mod_data[distributor_mod.id];
134  if (!ast_strlen_zero(serializer_name)) {
135  serializer = ast_taskprocessor_get(serializer_name, TPS_REF_IF_EXISTS);
136  if (serializer) {
137  ast_debug(3, "Found serializer %s on transaction %s\n",
138  serializer_name, tsx->obj_name);
139  }
140  }
141  }
142 
143 #ifdef HAVE_PJ_TRANSACTION_GRP_LOCK
144  pj_grp_lock_release(tsx->grp_lock);
145 #else
146  pj_mutex_unlock(tsx->mutex);
147 #endif
148 
149  return serializer;
150 }
struct ast_taskprocessor * ast_taskprocessor_get(const char *name, enum ast_tps_options create)
Get a reference to a taskprocessor with the specified name and create the taskprocessor if necessary...
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
static pjsip_module distributor_mod
return a reference to a taskprocessor ONLY if it already exists
Definition: taskprocessor.h:77
A ast_taskprocessor structure is a singleton by name.
Definition: taskprocessor.c:69

◆ global_loaded()

static void global_loaded ( const char *  object_type)
static

Definition at line 1165 of file pjsip_distributor.c.

References alloc_artificial_auth(), ao2_cleanup, ao2_global_obj_replace_unref, ast_free, ast_sched_add_variable(), ast_sched_clean_by_callback(), ast_sip_get_artificial_auth(), ast_sip_get_default_realm(), ast_sip_get_endpoint_identifier_order(), ast_sip_get_taskprocessor_overload_trigger(), ast_sip_get_unidentified_request_thresholds(), ast_strdupa, ast_strip(), clean_task(), default_realm, MAX_REALM_LENGTH, NULL, overload_trigger, prune_task(), ast_sip_auth::realm, strsep(), unidentified_count, unidentified_period, unidentified_prune_interval, and using_auth_username.

1166 {
1167  char default_realm[MAX_REALM_LENGTH + 1];
1168  struct ast_sip_auth *fake_auth;
1169  char *identifier_order;
1170 
1171  /* Update using_auth_username */
1172  identifier_order = ast_sip_get_endpoint_identifier_order();
1173  if (identifier_order) {
1174  char *identify_method;
1175  char *io_copy = ast_strdupa(identifier_order);
1176  int new_using = 0;
1177 
1178  ast_free(identifier_order);
1179  while ((identify_method = ast_strip(strsep(&io_copy, ",")))) {
1180  if (!strcmp(identify_method, "auth_username")) {
1181  new_using = 1;
1182  break;
1183  }
1184  }
1185  using_auth_username = new_using;
1186  }
1187 
1188  /* Update default_realm of artificial_auth */
1189  ast_sip_get_default_realm(default_realm, sizeof(default_realm));
1190  fake_auth = ast_sip_get_artificial_auth();
1191  if (!fake_auth || strcmp(fake_auth->realm, default_realm)) {
1192  ao2_cleanup(fake_auth);
1193 
1194  fake_auth = alloc_artificial_auth(default_realm);
1195  if (fake_auth) {
1196  ao2_global_obj_replace_unref(artificial_auth, fake_auth);
1197  }
1198  }
1199  ao2_cleanup(fake_auth);
1200 
1202 
1204 
1205  /* Clean out the old task, if any */
1207  /* Have to do something with the return value to shut up the stupid compiler. */
1209  return;
1210  }
1211 }
void ast_sched_clean_by_callback(struct ast_sched_context *con, ast_sched_cb match, ast_sched_cb cleanup_cb)
Clean all scheduled events with matching callback.
Definition: sched.c:407
static unsigned int unidentified_prune_interval
static char default_realm[MAX_REALM_LENGTH+1]
enum ast_sip_taskprocessor_overload_trigger ast_sip_get_taskprocessor_overload_trigger(void)
static int using_auth_username
int ast_sched_add_variable(struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data, int variable) attribute_warn_unused_result
Adds a scheduled event with rescheduling support.
Definition: sched.c:524
void ast_sip_get_default_realm(char *realm, size_t size)
Retrieve the global default realm.
#define NULL
Definition: resample.c:96
static unsigned int unidentified_period
static int clean_task(const void *data)
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
const ast_string_field realm
Definition: res_pjsip.h:448
struct ast_sched_context * prune_context
char * ast_sip_get_endpoint_identifier_order(void)
Retrieve the global endpoint_identifier_order setting.
static int prune_task(const void *data)
void ast_sip_get_unidentified_request_thresholds(unsigned int *count, unsigned int *period, unsigned int *prune_interval)
Retrieve the unidentified request security event thresholds.
#define ast_free(a)
Definition: astmm.h:182
static unsigned int unidentified_count
#define ao2_global_obj_replace_unref(holder, obj)
Definition: astobj2.h:908
char * strsep(char **str, const char *delims)
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define MAX_REALM_LENGTH
static enum ast_sip_taskprocessor_overload_trigger overload_trigger
static struct ast_sip_auth * alloc_artificial_auth(char *default_realm)
struct ast_sip_auth * ast_sip_get_artificial_auth(void)
Retrieves a reference to the artificial auth.

◆ log_failed_request()

static void log_failed_request ( pjsip_rx_data *  rdata,
char *  msg,
unsigned int  count,
unsigned int  period 
)
static

Definition at line 660 of file pjsip_distributor.c.

References ast_copy_pj_str(), ast_log, AST_SOCKADDR_BUFLEN, and LOG_NOTICE.

Referenced by apply_endpoint_acl(), apply_endpoint_contact_acl(), authenticate(), check_endpoint(), and endpoint_lookup().

661 {
662  char from_buf[PJSIP_MAX_URL_SIZE];
663  char callid_buf[PJSIP_MAX_URL_SIZE];
664  char method_buf[PJSIP_MAX_URL_SIZE];
665  char src_addr_buf[AST_SOCKADDR_BUFLEN];
666  pjsip_uri_print(PJSIP_URI_IN_FROMTO_HDR, rdata->msg_info.from->uri, from_buf, PJSIP_MAX_URL_SIZE);
667  ast_copy_pj_str(callid_buf, &rdata->msg_info.cid->id, PJSIP_MAX_URL_SIZE);
668  ast_copy_pj_str(method_buf, &rdata->msg_info.msg->line.req.method.name, PJSIP_MAX_URL_SIZE);
669  if (count) {
670  ast_log(LOG_NOTICE, "Request '%s' from '%s' failed for '%s' (callid: %s) - %s"
671  " after %u tries in %.3f ms\n",
672  method_buf, from_buf,
673  pj_sockaddr_print(&rdata->pkt_info.src_addr, src_addr_buf, sizeof(src_addr_buf), 3),
674  callid_buf, msg, count, period / 1000.0);
675  } else {
676  ast_log(LOG_NOTICE, "Request '%s' from '%s' failed for '%s' (callid: %s) - %s\n",
677  method_buf, from_buf,
678  pj_sockaddr_print(&rdata->pkt_info.src_addr, src_addr_buf, sizeof(src_addr_buf), 3),
679  callid_buf, msg);
680  }
681 }
#define AST_SOCKADDR_BUFLEN
Definition: netsock2.h:46
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 ast_log
Definition: astobj2.c:42
#define LOG_NOTICE
Definition: logger.h:263

◆ pjstr_hash()

static int pjstr_hash ( pj_str_t *  str)
static

Definition at line 443 of file pjsip_distributor.c.

References pjstr_hash_add().

Referenced by ast_sip_get_distributor_serializer().

444 {
445  return pjstr_hash_add(str, 5381);
446 }
const char * str
Definition: app_jack.c:147
static int pjstr_hash_add(pj_str_t *str, int hash)

◆ pjstr_hash_add()

static int pjstr_hash_add ( pj_str_t *  str,
int  hash 
)
static

Definition at line 426 of file pjsip_distributor.c.

References buf_hash_add().

Referenced by ast_sip_get_distributor_serializer(), and pjstr_hash().

427 {
428  return buf_hash_add(pj_strbuf(str), pj_strlen(str), hash);
429 }
static int buf_hash_add(const char *pos, size_t len, int hash)
const char * str
Definition: app_jack.c:147

◆ prune_task()

static int prune_task ( const void *  data)
static

Definition at line 1149 of file pjsip_distributor.c.

References ao2_callback, ast_sip_get_unidentified_request_thresholds(), expire_requests(), OBJ_MULTIPLE, OBJ_NODATA, OBJ_UNLINK, unidentified_count, unidentified_period, and unidentified_prune_interval.

Referenced by global_loaded().

1150 {
1151  unsigned int maxage;
1152 
1154  maxage = unidentified_period * 2;
1156 
1157  return unidentified_prune_interval * 1000;
1158 }
static int expire_requests(void *object, void *arg, int flags)
static unsigned int unidentified_prune_interval
static struct ao2_container * unidentified_requests
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
static unsigned int unidentified_period
void ast_sip_get_unidentified_request_thresholds(unsigned int *count, unsigned int *period, unsigned int *prune_interval)
Retrieve the unidentified request security event thresholds.
static unsigned int unidentified_count

◆ record_serializer()

static pj_status_t record_serializer ( pjsip_tx_data *  tdata)
static

Definition at line 77 of file pjsip_distributor.c.

References ast_strlen_zero, ast_taskprocessor_name(), ast_threadpool_serializer_get_current(), distributor_mod, and name.

78 {
80 
82  if (serializer) {
83  const char *name;
84 
85  name = ast_taskprocessor_name(serializer);
86  if (!ast_strlen_zero(name)
87  && (!tdata->mod_data[distributor_mod.id]
88  || strcmp(tdata->mod_data[distributor_mod.id], name))) {
89  char *tdata_name;
90 
91  /* The serializer in use changed. */
92  tdata_name = pj_pool_alloc(tdata->pool, strlen(name) + 1);
93  strcpy(tdata_name, name);/* Safe */
94 
95  tdata->mod_data[distributor_mod.id] = tdata_name;
96  }
97  }
98 
99  return PJ_SUCCESS;
100 }
const char * ast_taskprocessor_name(struct ast_taskprocessor *tps)
Return the name of the taskprocessor singleton.
struct ast_taskprocessor * ast_threadpool_serializer_get_current(void)
Get the threadpool serializer currently associated with this thread.
Definition: threadpool.c:1397
#define ast_strlen_zero(foo)
Definition: strings.h:52
static pjsip_module distributor_mod
static const char name[]
Definition: cdr_mysql.c:74
A ast_taskprocessor structure is a singleton by name.
Definition: taskprocessor.c:69

◆ suspects_compare()

static int suspects_compare ( void *  obj,
void *  arg,
int  flags 
)
static

Definition at line 1002 of file pjsip_distributor.c.

References CMP_MATCH, OBJ_SEARCH_KEY, OBJ_SEARCH_MASK, OBJ_SEARCH_OBJECT, OBJ_SEARCH_PARTIAL_KEY, and unidentified_request::src_name.

Referenced by ast_sip_initialize_distributor(), and cli_unid_get_container().

1003 {
1004  const struct unidentified_request *object_left = obj;
1005  const struct unidentified_request *object_right = arg;
1006  const char *right_key = arg;
1007  int cmp = 0;
1008 
1009  switch (flags & OBJ_SEARCH_MASK) {
1010  case OBJ_SEARCH_OBJECT:
1011  right_key = object_right->src_name;
1012  /* Fall through */
1013  case OBJ_SEARCH_KEY:
1014  if (strcmp(object_left->src_name, right_key) == 0) {
1015  cmp = CMP_MATCH;
1016  }
1017  break;
1019  if (strncmp(object_left->src_name, right_key, strlen(right_key)) == 0) {
1020  cmp = CMP_MATCH;
1021  }
1022  break;
1023  default:
1024  cmp = 0;
1025  break;
1026  }
1027  return cmp;
1028 }
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
The arg parameter is a partial search key similar to OBJ_SEARCH_KEY.
Definition: astobj2.h:1120
The arg parameter is an object of the same type.
Definition: astobj2.h:1091
Search option field mask.
Definition: astobj2.h:1076

◆ suspects_hash()

static int suspects_hash ( const void *  obj,
int  flags 
)
static

Definition at line 1030 of file pjsip_distributor.c.

References ast_assert, ast_str_hash(), OBJ_SEARCH_KEY, OBJ_SEARCH_MASK, and OBJ_SEARCH_OBJECT.

Referenced by ast_sip_initialize_distributor().

1031 {
1032  const struct unidentified_request *object;
1033  const char *key;
1034 
1035  switch (flags & OBJ_SEARCH_MASK) {
1036  case OBJ_SEARCH_KEY:
1037  key = obj;
1038  break;
1039  case OBJ_SEARCH_OBJECT:
1040  object = obj;
1041  key = object->src_name;
1042  break;
1043  default:
1044  /* Hash can only work on something with a full key. */
1045  ast_assert(0);
1046  return 0;
1047  }
1048  return ast_str_hash(key);
1049 }
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
#define ast_assert(a)
Definition: utils.h:695
The arg parameter is an object of the same type.
Definition: astobj2.h:1091
Search option field mask.
Definition: astobj2.h:1076
static force_inline int attribute_pure ast_str_hash(const char *str)
Compute a hash value on a string.
Definition: strings.h:1206

◆ suspects_sort()

static int suspects_sort ( const void *  obj,
const void *  arg,
int  flags 
)
static

Definition at line 978 of file pjsip_distributor.c.

References OBJ_SEARCH_KEY, OBJ_SEARCH_MASK, OBJ_SEARCH_OBJECT, OBJ_SEARCH_PARTIAL_KEY, and unidentified_request::src_name.

Referenced by cli_unid_get_container().

979 {
980  const struct unidentified_request *object_left = obj;
981  const struct unidentified_request *object_right = arg;
982  const char *right_key = arg;
983  int cmp;
984 
985  switch (flags & OBJ_SEARCH_MASK) {
986  case OBJ_SEARCH_OBJECT:
987  right_key = object_right->src_name;
988  /* Fall through */
989  case OBJ_SEARCH_KEY:
990  cmp = strcmp(object_left->src_name, right_key);
991  break;
993  cmp = strncmp(object_left->src_name, right_key, strlen(right_key));
994  break;
995  default:
996  cmp = 0;
997  break;
998  }
999  return cmp;
1000 }
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
The arg parameter is a partial search key similar to OBJ_SEARCH_KEY.
Definition: astobj2.h:1120
The arg parameter is an object of the same type.
Definition: astobj2.h:1091
Search option field mask.
Definition: astobj2.h:1076

Variable Documentation

◆ artificial_endpoint

struct ast_sip_endpoint* artificial_endpoint = NULL
static

Definition at line 636 of file pjsip_distributor.c.

Referenced by ast_sip_get_artificial_endpoint(), and digest_check_auth().

◆ auth_mod

pjsip_module auth_mod
static
Initial value:
= {
.name = {"Request Authenticator", 21},
.priority = PJSIP_MOD_PRIORITY_APPLICATION - 2,
.on_rx_request = authenticate,
}
static pj_bool_t authenticate(pjsip_rx_data *rdata)

Definition at line 937 of file pjsip_distributor.c.

Referenced by ast_sip_destroy_distributor(), and ast_sip_initialize_distributor().

◆ cli_commands

struct ast_cli_entry cli_commands[]
static

Definition at line 1127 of file pjsip_distributor.c.

◆ dialog_associations

struct ao2_container* dialog_associations
static

Definition at line 164 of file pjsip_distributor.c.

◆ distributor_mod

pjsip_module distributor_mod
static

◆ distributor_pool

struct ast_taskprocessor* distributor_pool[DISTRIBUTOR_POOL_SIZE]
static

Pool of serializers to use if not supplied.

Definition at line 66 of file pjsip_distributor.c.

◆ endpoint_mod

pjsip_module endpoint_mod
static
Initial value:
= {
.name = {"Endpoint Identifier", 19},
.priority = PJSIP_MOD_PRIORITY_TSX_LAYER - 3,
.on_rx_request = endpoint_lookup,
}
static pj_bool_t endpoint_lookup(pjsip_rx_data *rdata)

Definition at line 479 of file pjsip_distributor.c.

Referenced by apply_acls(), ast_pjsip_rdata_get_endpoint(), ast_sip_destroy_distributor(), ast_sip_initialize_distributor(), distribute(), distributor(), and endpoint_lookup().

◆ global_observer

struct ast_sorcery_observer global_observer
static
Initial value:
= {
.loaded = global_loaded,
}
static void global_loaded(const char *object_type)

Observer which is used to update our interval and default_realm when the global setting changes.

Definition at line 1214 of file pjsip_distributor.c.

◆ overload_trigger

enum ast_sip_taskprocessor_overload_trigger overload_trigger
static

Definition at line 54 of file pjsip_distributor.c.

Referenced by distributor(), and global_loaded().

◆ prune_context

struct ast_sched_context* prune_context

Definition at line 42 of file pjsip_distributor.c.

◆ unid_formatter

struct ast_sip_cli_formatter_entry* unid_formatter

Definition at line 1134 of file pjsip_distributor.c.

◆ unidentified_count

unsigned int unidentified_count
static

Definition at line 50 of file pjsip_distributor.c.

Referenced by check_endpoint(), global_loaded(), and prune_task().

◆ unidentified_period

unsigned int unidentified_period
static

Definition at line 51 of file pjsip_distributor.c.

Referenced by check_endpoint(), global_loaded(), and prune_task().

◆ unidentified_prune_interval

unsigned int unidentified_prune_interval
static

Definition at line 52 of file pjsip_distributor.c.

Referenced by global_loaded(), and prune_task().

◆ unidentified_requests

struct ao2_container* unidentified_requests
static

Definition at line 49 of file pjsip_distributor.c.

◆ using_auth_username

int using_auth_username
static

Definition at line 53 of file pjsip_distributor.c.

Referenced by endpoint_lookup(), and global_loaded().