diff options
Diffstat (limited to 'res/res_sip')
-rw-r--r-- | res/res_sip/config_transport.c | 7 | ||||
-rw-r--r-- | res/res_sip/include/res_sip_private.h | 8 | ||||
-rw-r--r-- | res/res_sip/location.c | 66 | ||||
-rw-r--r-- | res/res_sip/security_events.c | 234 | ||||
-rw-r--r-- | res/res_sip/sip_configuration.c | 181 | ||||
-rw-r--r-- | res/res_sip/sip_distributor.c | 14 | ||||
-rw-r--r-- | res/res_sip/sip_options.c | 791 |
7 files changed, 1106 insertions, 195 deletions
diff --git a/res/res_sip/config_transport.c b/res/res_sip/config_transport.c index 0df8c66ad..1d60274b7 100644 --- a/res/res_sip/config_transport.c +++ b/res/res_sip/config_transport.c @@ -145,6 +145,8 @@ static int transport_apply(const struct ast_sorcery *sorcery, void *obj) transport->tls.password = pj_str((char*)transport->password); res = pjsip_tls_transport_start2(ast_sip_get_pjsip_endpoint(), &transport->tls, &transport->host, NULL, transport->async_operations, &transport->state->factory); + } else if ((transport->type == AST_TRANSPORT_WS) || (transport->type == AST_TRANSPORT_WSS)) { + res = PJ_SUCCESS; } if (res != PJ_SUCCESS) { @@ -168,8 +170,11 @@ static int transport_protocol_handler(const struct aco_option *opt, struct ast_v transport->type = AST_TRANSPORT_TCP; } else if (!strcasecmp(var->value, "tls")) { transport->type = AST_TRANSPORT_TLS; + } else if (!strcasecmp(var->value, "ws")) { + transport->type = AST_TRANSPORT_WS; + } else if (!strcasecmp(var->value, "wss")) { + transport->type = AST_TRANSPORT_WSS; } else { - /* TODO: Implement websockets */ return -1; } diff --git a/res/res_sip/include/res_sip_private.h b/res/res_sip/include/res_sip_private.h index 318510aae..3625bab31 100644 --- a/res/res_sip/include/res_sip_private.h +++ b/res/res_sip/include/res_sip_private.h @@ -40,6 +40,14 @@ int ast_res_sip_reload_configuration(void); int ast_res_sip_init_options_handling(int reload); /*! + * \brief Initialize transport storage for contacts. + * + * \retval 0 on success + * \retval other on failure + */ +int ast_res_sip_init_contact_transports(void); + +/*! * \brief Initialize outbound authentication support * * \retval 0 Success diff --git a/res/res_sip/location.c b/res/res_sip/location.c index 91521c813..d0b0a28c9 100644 --- a/res/res_sip/location.c +++ b/res/res_sip/location.c @@ -24,6 +24,10 @@ #include "asterisk/logger.h" #include "asterisk/astobj2.h" #include "asterisk/sorcery.h" +#include "include/res_sip_private.h" + +#define CONTACT_TRANSPORTS_BUCKETS 7 +static struct ao2_container *contact_transports; /*! \brief Destructor for AOR */ static void aor_destroy(void *obj) @@ -70,6 +74,48 @@ static void *contact_alloc(const char *name) return contact; } +/*! \brief Callback function for finding a contact_transport by URI */ +static int contact_transport_find_by_uri(void *obj, void *arg, int flags) +{ + struct ast_sip_contact_transport *ct = obj; + const char *contact_uri = arg; + + return (!strcmp(ct->uri, contact_uri)) ? CMP_MATCH | CMP_STOP : 0; +} + +/*! \brief Callback function for finding a contact_transport by transport */ +static int contact_transport_find_by_transport(void *obj, void *arg, int flags) +{ + struct ast_sip_contact_transport *ct = obj; + pjsip_transport *transport = arg; + + return (ct->transport == transport) ? CMP_MATCH | CMP_STOP : 0; +} + +void ast_sip_location_add_contact_transport(struct ast_sip_contact_transport *ct) +{ + ao2_link(contact_transports, ct); + + return; +} + +void ast_sip_location_delete_contact_transport(struct ast_sip_contact_transport *ct) +{ + ao2_unlink(contact_transports, ct); + + return; +} + +struct ast_sip_contact_transport *ast_sip_location_retrieve_contact_transport_by_uri(const char *contact_uri) +{ + return ao2_callback(contact_transports, 0, contact_transport_find_by_uri, (void *)contact_uri); +} + +struct ast_sip_contact_transport *ast_sip_location_retrieve_contact_transport_by_transport(pjsip_transport *transport) +{ + return ao2_callback(contact_transports, 0, contact_transport_find_by_transport, transport); +} + struct ast_sip_aor *ast_sip_location_retrieve_aor(const char *aor_name) { return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "aor", aor_name); @@ -189,6 +235,8 @@ int ast_sip_location_add_contact(struct ast_sip_aor *aor, const char *uri, struc ast_string_field_set(contact, uri, uri); contact->expiration_time = expiration_time; + contact->qualify_frequency = aor->qualify_frequency; + contact->authenticate_qualify = aor->authenticate_qualify; return ast_sorcery_create(ast_sip_get_sorcery(), contact); } @@ -248,11 +296,15 @@ int ast_sip_initialize_sorcery_location(struct ast_sorcery *sorcery) ast_sorcery_object_field_register(sorcery, "contact", "type", "", OPT_NOOP_T, 0, 0); ast_sorcery_object_field_register(sorcery, "contact", "uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, uri)); ast_sorcery_object_field_register_custom(sorcery, "contact", "expiration_time", "", expiration_str2struct, expiration_struct2str, 0, 0); + ast_sorcery_object_field_register(sorcery, "contact", "qualify_frequency", 0, OPT_UINT_T, + PARSE_IN_RANGE, FLDSET(struct ast_sip_contact, qualify_frequency), 0, 86400); ast_sorcery_object_field_register(sorcery, "aor", "type", "", OPT_NOOP_T, 0, 0); ast_sorcery_object_field_register(sorcery, "aor", "minimum_expiration", "60", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, minimum_expiration)); ast_sorcery_object_field_register(sorcery, "aor", "maximum_expiration", "7200", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, maximum_expiration)); ast_sorcery_object_field_register(sorcery, "aor", "default_expiration", "3600", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, default_expiration)); + ast_sorcery_object_field_register(sorcery, "aor", "qualify_frequency", 0, OPT_UINT_T, PARSE_IN_RANGE, FLDSET(struct ast_sip_aor, qualify_frequency), 0, 86400); + ast_sorcery_object_field_register(sorcery, "aor", "authenticate_qualify", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, authenticate_qualify)); ast_sorcery_object_field_register(sorcery, "aor", "max_contacts", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_aor, max_contacts)); ast_sorcery_object_field_register(sorcery, "aor", "remove_existing", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_aor, remove_existing)); ast_sorcery_object_field_register_custom(sorcery, "aor", "contact", "", permanent_uri_handler, NULL, 0, 0); @@ -260,3 +312,17 @@ int ast_sip_initialize_sorcery_location(struct ast_sorcery *sorcery) return 0; } + +int ast_res_sip_init_contact_transports(void) +{ + if (contact_transports) { + ao2_t_ref(contact_transports, -1, "Remove old contact transports"); + } + + contact_transports = ao2_t_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, CONTACT_TRANSPORTS_BUCKETS, NULL, NULL, "Create container for contact transports"); + if (!contact_transports) { + return -1; + } + + return 0; +} diff --git a/res/res_sip/security_events.c b/res/res_sip/security_events.c new file mode 100644 index 000000000..068e8551f --- /dev/null +++ b/res/res_sip/security_events.c @@ -0,0 +1,234 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 2013, Digium, Inc. + * + * Joshua Colp <jcolp@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! + * \file + * + * \brief Generate security events in the PJSIP channel + * + * \author Joshua Colp <jcolp@digium.com> + */ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include <pjsip.h> + +#include "asterisk/res_sip.h" +#include "asterisk/security_events.h" + +static int find_transport_in_use(void *obj, void *arg, int flags) +{ + struct ast_sip_transport *transport = obj; + pjsip_rx_data *rdata = arg; + + if ((transport->state->transport == rdata->tp_info.transport) || + (transport->state->factory && !pj_strcmp(&transport->state->factory->addr_name.host, &rdata->tp_info.transport->local_name.host) && + transport->state->factory->addr_name.port == rdata->tp_info.transport->local_name.port)) { + return CMP_MATCH | CMP_STOP; + } + + return 0; +} + +static enum ast_transport security_event_get_transport(pjsip_rx_data *rdata) +{ + RAII_VAR(struct ao2_container *, transports, NULL, ao2_cleanup); + RAII_VAR(struct ast_sip_transport *, transport, NULL, ao2_cleanup); + + /* It should be impossible for these to fail as the transport has to exist for the message to exist */ + transports = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "transport", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); + + ast_assert(transports != NULL); + + transport = ao2_callback(transports, 0, find_transport_in_use, rdata); + + ast_assert(transport != NULL); + + return transport->type; +} + +static void security_event_populate(pjsip_rx_data *rdata, char *call_id, size_t call_id_size, struct ast_sockaddr *local, struct ast_sockaddr *remote) +{ + char host[NI_MAXHOST]; + + ast_copy_pj_str(call_id, &rdata->msg_info.cid->id, call_id_size); + + ast_copy_pj_str(host, &rdata->tp_info.transport->local_name.host, sizeof(host)); + ast_sockaddr_parse(local, host, PARSE_PORT_FORBID); + ast_sockaddr_set_port(local, rdata->tp_info.transport->local_name.port); + + ast_sockaddr_parse(remote, rdata->pkt_info.src_name, PARSE_PORT_FORBID); + ast_sockaddr_set_port(remote, rdata->pkt_info.src_port); +} + +void ast_sip_report_invalid_endpoint(const char *name, pjsip_rx_data *rdata) +{ + enum ast_transport transport = security_event_get_transport(rdata); + char call_id[pj_strlen(&rdata->msg_info.cid->id) + 1]; + struct ast_sockaddr local, remote; + + struct ast_security_event_inval_acct_id inval_acct_id = { + .common.event_type = AST_SECURITY_EVENT_INVAL_ACCT_ID, + .common.version = AST_SECURITY_EVENT_INVAL_ACCT_ID_VERSION, + .common.service = "PJSIP", + .common.account_id = name, + .common.local_addr = { + .addr = &local, + .transport = transport, + }, + .common.remote_addr = { + .addr = &remote, + .transport = transport, + }, + .common.session_id = call_id, + }; + + security_event_populate(rdata, call_id, sizeof(call_id), &local, &remote); + + ast_security_event_report(AST_SEC_EVT(&inval_acct_id)); +} + +void ast_sip_report_failed_acl(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, const char *name) +{ + enum ast_transport transport = security_event_get_transport(rdata); + char call_id[pj_strlen(&rdata->msg_info.cid->id) + 1]; + struct ast_sockaddr local, remote; + + struct ast_security_event_failed_acl failed_acl_event = { + .common.event_type = AST_SECURITY_EVENT_FAILED_ACL, + .common.version = AST_SECURITY_EVENT_FAILED_ACL_VERSION, + .common.service = "PJSIP", + .common.account_id = ast_sorcery_object_get_id(endpoint), + .common.local_addr = { + .addr = &local, + .transport = transport, + }, + .common.remote_addr = { + .addr = &remote, + .transport = transport, + }, + .common.session_id = call_id, + .acl_name = name, + }; + + security_event_populate(rdata, call_id, sizeof(call_id), &local, &remote); + + ast_security_event_report(AST_SEC_EVT(&failed_acl_event)); +} + +void ast_sip_report_auth_failed_challenge_response(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata) +{ + pjsip_authorization_hdr *auth = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_AUTHORIZATION, NULL); + enum ast_transport transport = security_event_get_transport(rdata); + char call_id[pj_strlen(&rdata->msg_info.cid->id) + 1]; + char nonce[64] = "", response[256] = ""; + struct ast_sockaddr local, remote; + + struct ast_security_event_chal_resp_failed chal_resp_failed = { + .common.event_type = AST_SECURITY_EVENT_CHAL_RESP_FAILED, + .common.version = AST_SECURITY_EVENT_CHAL_RESP_FAILED_VERSION, + .common.service = "PJSIP", + .common.account_id = ast_sorcery_object_get_id(endpoint), + .common.local_addr = { + .addr = &local, + .transport = transport, + }, + .common.remote_addr = { + .addr = &remote, + .transport = transport, + }, + .common.session_id = call_id, + + .challenge = nonce, + .response = response, + .expected_response = "", + }; + + if (auth && !pj_strcmp2(&auth->scheme, "digest")) { + ast_copy_pj_str(nonce, &auth->credential.digest.nonce, sizeof(nonce)); + ast_copy_pj_str(response, &auth->credential.digest.response, sizeof(response)); + } + + security_event_populate(rdata, call_id, sizeof(call_id), &local, &remote); + + ast_security_event_report(AST_SEC_EVT(&chal_resp_failed)); +} + +void ast_sip_report_auth_success(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata) +{ + pjsip_authorization_hdr *auth = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_AUTHORIZATION, NULL); + enum ast_transport transport = security_event_get_transport(rdata); + char call_id[pj_strlen(&rdata->msg_info.cid->id) + 1]; + struct ast_sockaddr local, remote; + + struct ast_security_event_successful_auth successful_auth = { + .common.event_type = AST_SECURITY_EVENT_SUCCESSFUL_AUTH, + .common.version = AST_SECURITY_EVENT_SUCCESSFUL_AUTH_VERSION, + .common.service = "PJSIP", + .common.account_id = ast_sorcery_object_get_id(endpoint), + .common.local_addr = { + .addr = &local, + .transport = transport, + }, + .common.remote_addr = { + .addr = &remote, + .transport = transport, + }, + .common.session_id = call_id, + .using_password = auth ? (uint32_t *)1 : (uint32_t *)0, + }; + + security_event_populate(rdata, call_id, sizeof(call_id), &local, &remote); + + ast_security_event_report(AST_SEC_EVT(&successful_auth)); +} + +void ast_sip_report_auth_challenge_sent(struct ast_sip_endpoint *endpoint, pjsip_rx_data *rdata, pjsip_tx_data *tdata) +{ + pjsip_www_authenticate_hdr *auth = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_WWW_AUTHENTICATE, NULL); + enum ast_transport transport = security_event_get_transport(rdata); + char nonce[64] = "", call_id[pj_strlen(&rdata->msg_info.cid->id) + 1]; + struct ast_sockaddr local, remote; + + struct ast_security_event_chal_sent chal_sent = { + .common.event_type = AST_SECURITY_EVENT_CHAL_SENT, + .common.version = AST_SECURITY_EVENT_CHAL_SENT_VERSION, + .common.service = "PJSIP", + .common.account_id = ast_sorcery_object_get_id(endpoint), + .common.local_addr = { + .addr = &local, + .transport = transport, + }, + .common.remote_addr = { + .addr = &remote, + .transport = transport, + }, + .common.session_id = call_id, + .challenge = nonce, + }; + + if (auth && !pj_strcmp2(&auth->scheme, "digest")) { + ast_copy_pj_str(nonce, &auth->challenge.digest.nonce, sizeof(nonce)); + } + + security_event_populate(rdata, call_id, sizeof(call_id), &local, &remote); + + ast_security_event_report(AST_SEC_EVT(&chal_sent)); +} diff --git a/res/res_sip/sip_configuration.c b/res/res_sip/sip_configuration.c index 3488d527e..5864bdeec 100644 --- a/res/res_sip/sip_configuration.c +++ b/res/res_sip/sip_configuration.c @@ -134,8 +134,93 @@ static char *handle_cli_show_endpoints(struct ast_cli_entry *e, int cmd, struct return CLI_SUCCESS; } +static int show_contact(void *obj, void *arg, int flags) +{ + struct ast_sip_contact *contact = obj; + struct ast_cli_args *a = arg; + RAII_VAR(struct ast_sip_contact_status *, status, ast_sorcery_retrieve_by_id( + ast_sip_get_sorcery(), CONTACT_STATUS, + ast_sorcery_object_get_id(contact)), ao2_cleanup); + + ast_cli(a->fd, "\tContact %s:\n", contact->uri); + + if (!status) { + ast_cli(a->fd, "\tStatus not found!\n"); + return 0; + } + + ast_cli(a->fd, "\t\tavailable = %s\n", status->status ? "yes" : "no"); + + if (status->status) { + ast_cli(a->fd, "\t\tRTT = %lld microseconds\n", (long long)status->rtt); + } + + return 0; +} + +static void show_endpoint(struct ast_sip_endpoint *endpoint, struct ast_cli_args *a) +{ + char *aor_name, *aors; + + if (ast_strlen_zero(endpoint->aors)) { + return; + } + + aors = ast_strdupa(endpoint->aors); + + while ((aor_name = strsep(&aors, ","))) { + RAII_VAR(struct ast_sip_aor *, aor, + ast_sip_location_retrieve_aor(aor_name), ao2_cleanup); + RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup); + + if (!aor || !(contacts = ast_sip_location_retrieve_aor_contacts(aor))) { + continue; + } + + ast_cli(a->fd, "AOR %s:\n", ast_sorcery_object_get_id(aor)); + ao2_callback(contacts, OBJ_NODATA, show_contact, a); + } + + return; +} + +static char *cli_show_endpoint(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) +{ + RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup); + const char *endpoint_name; + + switch (cmd) { + case CLI_INIT: + e->command = "sip show endpoint"; + e->usage = + "Usage: sip show endpoint <endpoint>\n" + " Show the given SIP endpoint.\n"; + return NULL; + case CLI_GENERATE: + return NULL; + } + + if (a->argc != 4) { + return CLI_SHOWUSAGE; + } + + endpoint_name = a->argv[3]; + + if (!(endpoint = ast_sorcery_retrieve_by_id( + ast_sip_get_sorcery(), "endpoint", endpoint_name))) { + ast_cli(a->fd, "Unable to retrieve endpoint %s\n", endpoint_name); + return CLI_FAILURE; + } + + ast_cli(a->fd, "Endpoint %s:\n", endpoint_name); + show_endpoint(endpoint, a); + + return CLI_SUCCESS; +} + static struct ast_cli_entry cli_commands[] = { AST_CLI_DEFINE(handle_cli_show_endpoints, "Show SIP Endpoints"), + AST_CLI_DEFINE(cli_show_endpoint, "Show SIP Endpoint") }; static int dtmf_handler(const struct aco_option *opt, struct ast_variable *var, void *obj) @@ -291,6 +376,22 @@ static int direct_media_method_handler(const struct aco_option *opt, struct ast_ return 0; } +static int connected_line_method_handler(const struct aco_option *opt, struct ast_variable *var, void *obj) +{ + struct ast_sip_endpoint *endpoint = obj; + + if (!strcasecmp(var->value, "invite") || !strcasecmp(var->value, "reinvite")) { + endpoint->connected_line_method = AST_SIP_SESSION_REFRESH_METHOD_INVITE; + } else if (!strcasecmp(var->value, "update")) { + endpoint->connected_line_method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE; + } else { + ast_log(LOG_NOTICE, "Unrecognized option value %s for %s on endpoint %s\n", + var->value, var->name, ast_sorcery_object_get_id(endpoint)); + return -1; + } + return 0; +} + static int direct_media_glare_mitigation_handler(const struct aco_option *opt, struct ast_variable *var, void *obj) { struct ast_sip_endpoint *endpoint = obj; @@ -353,6 +454,65 @@ static int caller_id_tag_handler(const struct aco_option *opt, struct ast_variab return endpoint->id.tag ? 0 : -1; } +static int media_encryption_handler(const struct aco_option *opt, struct ast_variable *var, void *obj) +{ + struct ast_sip_endpoint *endpoint = obj; + + if (!strcasecmp("no", var->value)) { + endpoint->media_encryption = AST_SIP_MEDIA_ENCRYPT_NONE; + } else if (!strcasecmp("sdes", var->value)) { + endpoint->media_encryption = AST_SIP_MEDIA_ENCRYPT_SDES; + /*} else if (!strcasecmp("dtls", var->value)) { + endpoint->media_encryption = AST_SIP_MEDIA_ENCRYPT_DTLS;*/ + } else { + return -1; + } + + return 0; +} + +static int group_handler(const struct aco_option *opt, + struct ast_variable *var, void *obj) +{ + struct ast_sip_endpoint *endpoint = obj; + + if (!strncmp(var->name, "callgroup", 9)) { + if (!(endpoint->callgroup = ast_get_group(var->value))) { + return -1; + } + } else if (!strncmp(var->name, "pickupgroup", 11)) { + if (!(endpoint->pickupgroup = ast_get_group(var->value))) { + return -1; + } + } else { + return -1; + } + + return 0; +} + +static int named_groups_handler(const struct aco_option *opt, + struct ast_variable *var, void *obj) +{ + struct ast_sip_endpoint *endpoint = obj; + + if (!strncmp(var->name, "namedcallgroup", 14)) { + if (!(endpoint->named_callgroups = + ast_get_namedgroups(var->value))) { + return -1; + } + } else if (!strncmp(var->name, "namedpickupgroup", 16)) { + if (!(endpoint->named_pickupgroups = + ast_get_namedgroups(var->value))) { + return -1; + } + } else { + return -1; + } + + return 0; +} + static void *sip_nat_hook_alloc(const char *name) { return ao2_alloc(sizeof(struct ast_sip_nat_hook), NULL); @@ -450,7 +610,6 @@ int ast_res_sip_initialize_configuration(void) ast_sorcery_object_field_register(sip_sorcery, "endpoint", "context", "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, context)); ast_sorcery_object_field_register(sip_sorcery, "endpoint", "disallow", "", OPT_CODEC_T, 0, FLDSET(struct ast_sip_endpoint, prefs, codecs)); ast_sorcery_object_field_register(sip_sorcery, "endpoint", "allow", "", OPT_CODEC_T, 1, FLDSET(struct ast_sip_endpoint, prefs, codecs)); - ast_sorcery_object_field_register(sip_sorcery, "endpoint", "qualify_frequency", 0, OPT_UINT_T, PARSE_IN_RANGE, FLDSET(struct ast_sip_endpoint, qualify_frequency), 0, 86400); ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "dtmfmode", "rfc4733", dtmf_handler, NULL, 0, 0); ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_ipv6", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, rtp_ipv6)); ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtp_symmetric", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, rtp_symmetric)); @@ -472,6 +631,7 @@ int ast_res_sip_initialize_configuration(void) ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "identify_by", "username,location", ident_handler, NULL, 0, 0); ast_sorcery_object_field_register(sip_sorcery, "endpoint", "direct_media", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, direct_media)); ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "direct_media_method", "invite", direct_media_method_handler, NULL, 0, 0); + ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "connected_line_method", "invite", connected_line_method_handler, NULL, 0, 0); ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "direct_media_glare_mitigation", "none", direct_media_glare_mitigation_handler, NULL, 0, 0); ast_sorcery_object_field_register(sip_sorcery, "endpoint", "disable_direct_media_on_nat", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, disable_direct_media_on_nat)); ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callerid", "", caller_id_handler, NULL, 0, 0); @@ -481,8 +641,17 @@ int ast_res_sip_initialize_configuration(void) ast_sorcery_object_field_register(sip_sorcery, "endpoint", "trust_id_outbound", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, trust_id_outbound)); ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_pai", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, send_pai)); ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_rpid", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, send_rpid)); + ast_sorcery_object_field_register(sip_sorcery, "endpoint", "send_diversion", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, send_diversion)); ast_sorcery_object_field_register(sip_sorcery, "endpoint", "mailboxes", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_endpoint, mailboxes)); ast_sorcery_object_field_register(sip_sorcery, "endpoint", "aggregate_mwi", "yes", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, aggregate_mwi)); + ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "media_encryption", "no", media_encryption_handler, NULL, 0, 0); + ast_sorcery_object_field_register(sip_sorcery, "endpoint", "use_avpf", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, use_avpf)); + ast_sorcery_object_field_register(sip_sorcery, "endpoint", "one_touch_recording", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, one_touch_recording)); + ast_sorcery_object_field_register(sip_sorcery, "endpoint", "inband_progress", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, inband_progress)); + ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "callgroup", "", group_handler, NULL, 0, 0); + ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "pickupgroup", "", group_handler, NULL, 0, 0); + ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "namedcallgroup", "", named_groups_handler, NULL, 0, 0); + ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "namedpickupgroup", "", named_groups_handler, NULL, 0, 0); ast_sorcery_object_field_register(sip_sorcery, "endpoint", "devicestate_busy_at", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_endpoint, devicestate_busy_at)); if (ast_sip_initialize_sorcery_transport(sip_sorcery)) { @@ -499,6 +668,13 @@ int ast_res_sip_initialize_configuration(void) return -1; } + if (ast_sip_initialize_sorcery_qualify(sip_sorcery)) { + ast_log(LOG_ERROR, "Failed to register SIP qualify support with sorcery\n"); + ast_sorcery_unref(sip_sorcery); + sip_sorcery = NULL; + return -1; + } + ast_sorcery_observer_add(sip_sorcery, "contact", &state_contact_observer); if (ast_sip_initialize_sorcery_domain_alias(sip_sorcery)) { @@ -539,6 +715,8 @@ static void endpoint_destructor(void* obj) destroy_auths(endpoint->sip_inbound_auths, endpoint->num_inbound_auths); destroy_auths(endpoint->sip_outbound_auths, endpoint->num_outbound_auths); ast_party_id_free(&endpoint->id); + endpoint->named_callgroups = ast_unref_namedgroups(endpoint->named_callgroups); + endpoint->named_pickupgroups = ast_unref_namedgroups(endpoint->named_pickupgroups); ao2_cleanup(endpoint->persistent); } @@ -596,4 +774,3 @@ struct ast_sorcery *ast_sip_get_sorcery(void) { return sip_sorcery; } - diff --git a/res/res_sip/sip_distributor.c b/res/res_sip/sip_distributor.c index 766261089..db36b6182 100644 --- a/res/res_sip/sip_distributor.c +++ b/res/res_sip/sip_distributor.c @@ -140,11 +140,21 @@ static pj_bool_t endpoint_lookup(pjsip_rx_data *rdata) } if (!endpoint && !is_ack) { + char name[AST_UUID_STR_LEN] = ""; + pjsip_uri *from = rdata->msg_info.from->uri; + /* XXX When we do an alwaysauthreject-like option, we'll need to take that into account * for this response. Either that, or have a pseudo-endpoint to pass along so that authentication * will fail */ pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 403, NULL, NULL, NULL); + + if (PJSIP_URI_SCHEME_IS_SIP(from) || PJSIP_URI_SCHEME_IS_SIPS(from)) { + pjsip_sip_uri *sip_from = pjsip_uri_get_uri(from); + ast_copy_pj_str(name, &sip_from->user, sizeof(name)); + } + + ast_sip_report_invalid_endpoint(name, rdata); return PJ_TRUE; } rdata->endpt_info.mod_data[endpoint_mod.id] = endpoint; @@ -164,16 +174,20 @@ static pj_bool_t authenticate(pjsip_rx_data *rdata) switch (ast_sip_check_authentication(endpoint, rdata, tdata)) { case AST_SIP_AUTHENTICATION_CHALLENGE: /* Send the 401 we created for them */ + ast_sip_report_auth_challenge_sent(endpoint, rdata, tdata); pjsip_endpt_send_response2(ast_sip_get_pjsip_endpoint(), rdata, tdata, NULL, NULL); return PJ_TRUE; case AST_SIP_AUTHENTICATION_SUCCESS: + ast_sip_report_auth_success(endpoint, rdata); pjsip_tx_data_dec_ref(tdata); return PJ_FALSE; case AST_SIP_AUTHENTICATION_FAILED: + ast_sip_report_auth_failed_challenge_response(endpoint, rdata); pjsip_tx_data_dec_ref(tdata); pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 403, NULL, NULL, NULL); return PJ_TRUE; case AST_SIP_AUTHENTICATION_ERROR: + ast_sip_report_auth_failed_challenge_response(endpoint, rdata); pjsip_tx_data_dec_ref(tdata); pjsip_endpt_respond_stateless(ast_sip_get_pjsip_endpoint(), rdata, 500, NULL, NULL, NULL); return PJ_TRUE; diff --git a/res/res_sip/sip_options.c b/res/res_sip/sip_options.c index 5e3f8edca..4c8a9f6a7 100644 --- a/res/res_sip/sip_options.c +++ b/res/res_sip/sip_options.c @@ -1,8 +1,19 @@ /* - * sip_options.c + * Asterisk -- An open source telephony toolkit. * - * Created on: Jan 25, 2013 - * Author: mjordan + * Copyright (C) 2013, Digium, Inc. + * + * Matt Jordan <mjordan@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. */ #include "asterisk.h" @@ -16,41 +27,429 @@ #include "asterisk/pbx.h" #include "asterisk/astobj2.h" #include "asterisk/cli.h" +#include "asterisk/time.h" #include "include/res_sip_private.h" #define DEFAULT_LANGUAGE "en" #define DEFAULT_ENCODING "text/plain" #define QUALIFIED_BUCKETS 211 -/*! \brief Scheduling context for qualifies */ -static struct ast_sched_context *sched; /* XXX move this to registrar */ +static int qualify_contact(struct ast_sip_contact *contact); + +/*! + * \internal + * \brief Create a ast_sip_contact_status object. + */ +static void *contact_status_alloc(const char *name) +{ + struct ast_sip_contact_status *status = ao2_alloc_options( + sizeof(*status), NULL, AO2_ALLOC_OPT_LOCK_NOLOCK); + + if (!status) { + ast_log(LOG_ERROR, "Unable to allocate ast_sip_contact_status\n"); + return NULL; + } + + status->status = UNAVAILABLE; + + return status; +} + +/*! + * \internal + * \brief Retrieve a ast_sip_contact_status object from sorcery creating + * one if not found. + */ +static struct ast_sip_contact_status *find_or_create_contact_status(const struct ast_sip_contact *contact) +{ + struct ast_sip_contact_status *status = ast_sorcery_retrieve_by_id( + ast_sip_get_sorcery(), CONTACT_STATUS, + ast_sorcery_object_get_id(contact)); + + if (status) { + return status; + } + + if (!(status = ast_sorcery_alloc( + ast_sip_get_sorcery(), CONTACT_STATUS, + ast_sorcery_object_get_id(contact)))) { + + ast_log(LOG_ERROR, "Unable to create ast_sip_contact_status for contact %s\n", + contact->uri); + return NULL; + } + + if (ast_sorcery_create(ast_sip_get_sorcery(), status)) { + ast_log(LOG_ERROR, "Unable to persist ast_sip_contact_status for contact %s\n", + contact->uri); + return NULL; + } + + return status; +} + +/*! + * \internal + * \brief Update an ast_sip_contact_status's elements. + */ +static void update_contact_status(const struct ast_sip_contact *contact, + enum ast_sip_contact_status_type value) +{ + RAII_VAR(struct ast_sip_contact_status *, status, + find_or_create_contact_status(contact), ao2_cleanup); + + RAII_VAR(struct ast_sip_contact_status *, update, ast_sorcery_alloc( + ast_sip_get_sorcery(), CONTACT_STATUS, + ast_sorcery_object_get_id(status)), ao2_cleanup); + + if (!update) { + ast_log(LOG_ERROR, "Unable to create update ast_sip_contact_status for contact %s\n", + contact->uri); + return; + } + + update->status = value; + + /* if the contact is available calculate the rtt as + the diff between the last start time and "now" */ + update->rtt = update->status ? + ast_tvdiff_us(ast_tvnow(), status->rtt_start) : 0; + + update->rtt_start = ast_tv(0, 0); + + if (ast_sorcery_update(ast_sip_get_sorcery(), update)) { + ast_log(LOG_ERROR, "Unable to update ast_sip_contact_status for contact %s\n", + contact->uri); + } +} + +/*! + * \internal + * \brief Initialize the start time on a contact status so the round + * trip time can be calculated upon a valid response. + */ +static void init_start_time(const struct ast_sip_contact *contact) +{ + RAII_VAR(struct ast_sip_contact_status *, status, + find_or_create_contact_status(contact), ao2_cleanup); + + RAII_VAR(struct ast_sip_contact_status *, update, ast_sorcery_alloc( + ast_sip_get_sorcery(), CONTACT_STATUS, + ast_sorcery_object_get_id(status)), ao2_cleanup); + + if (!update) { + ast_log(LOG_ERROR, "Unable to create update ast_sip_contact_status for contact %s\n", + contact->uri); + return; + } + + update->rtt_start = ast_tvnow(); + + if (ast_sorcery_update(ast_sip_get_sorcery(), update)) { + ast_log(LOG_ERROR, "Unable to update ast_sip_contact_status for contact %s\n", + contact->uri); + } +} + +/*! + * \internal + * \brief For an endpoint try to match on a given contact. + */ +static int on_endpoint(void *obj, void *arg, int flags) +{ + struct ast_sip_endpoint *endpoint = obj; + char *aor_name, *aors; + + if (!arg || ast_strlen_zero(endpoint->aors)) { + return 0; + } + + aors = ast_strdupa(endpoint->aors); + + while ((aor_name = strsep(&aors, ","))) { + RAII_VAR(struct ast_sip_aor *, aor, + ast_sip_location_retrieve_aor(aor_name), ao2_cleanup); + RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup); + + if (!aor || !(contacts = ast_sip_location_retrieve_aor_contacts(aor))) { + continue; + } + + if (ao2_find(contacts, arg, OBJ_NODATA | OBJ_POINTER)) { + return CMP_MATCH; + } + } + + return 0; +} + +/*! + * \internal + * \brief Find endpoints associated with the given contact. + */ +static struct ao2_container *find_endpoints(struct ast_sip_contact *contact) +{ + RAII_VAR(struct ao2_container *, endpoints, + ast_res_sip_get_endpoints(), ao2_cleanup); + + return ao2_callback(endpoints, OBJ_MULTIPLE, on_endpoint, contact); +} + +/*! + * \internal + * \brief Receive an response to the qualify contact request. + */ +static void qualify_contact_cb(void *token, pjsip_event *e) +{ + RAII_VAR(struct ast_sip_contact *, contact, token, ao2_cleanup); + RAII_VAR(struct ao2_container *, endpoints, NULL, ao2_cleanup); + RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup); + + pjsip_transaction *tsx = e->body.tsx_state.tsx; + pjsip_rx_data *challenge = e->body.tsx_state.src.rdata; + pjsip_tx_data *tdata; + + switch(e->body.tsx_state.type) { + case PJSIP_EVENT_TRANSPORT_ERROR: + case PJSIP_EVENT_TIMER: + update_contact_status(contact, UNAVAILABLE); + return; + default: + break; + } + + if (!contact->authenticate_qualify || (tsx->status_code != 401 && + tsx->status_code != 407)) { + update_contact_status(contact, AVAILABLE); + return; + } + + /* try to find endpoints that are associated with the contact */ + if (!(endpoints = find_endpoints(contact))) { + ast_log(LOG_ERROR, "No endpoints found for contact %s, cannot authenticate", + contact->uri); + return; + } + + /* find "first" endpoint in order to authenticate - actually any + endpoint should do that matched on the contact */ + endpoint = ao2_callback(endpoints, 0, NULL, NULL); + + if (!ast_sip_create_request_with_auth(endpoint->sip_outbound_auths, + endpoint->num_outbound_auths, + challenge, tsx, &tdata)) { + pjsip_endpt_send_request(ast_sip_get_pjsip_endpoint(), tdata, + -1, NULL, NULL); + } +} + +/*! + * \internal + * \brief Attempt to qualify the contact + * + * \detail Sends a SIP OPTIONS request to the given contact in order to make + * sure that contact is available. + */ +static int qualify_contact(struct ast_sip_contact *contact) +{ + pjsip_tx_data *tdata; + + if (ast_sip_create_request("OPTIONS", NULL, NULL, contact->uri, &tdata)) { + ast_log(LOG_ERROR, "Unable to create request to qualify contact %s\n", + contact->uri); + return -1; + } + + init_start_time(contact); + + ao2_ref(contact, +1); + if (pjsip_endpt_send_request(ast_sip_get_pjsip_endpoint(), + tdata, -1, contact, qualify_contact_cb) != PJ_SUCCESS) { + pjsip_tx_data_dec_ref(tdata); + ast_log(LOG_ERROR, "Unable to send request to qualify contact %s\n", + contact->uri); + ao2_ref(contact, -1); + return -1; + } -struct ao2_container *scheduled_qualifies; + return 0; +} + +/*! + * \internal + * \brief Scheduling context for sending QUALIFY request at specified intervals. + */ +static struct ast_sched_context *sched; -struct qualify_info { - AST_DECLARE_STRING_FIELDS( - AST_STRING_FIELD(endpoint_id); - ); - char *scheduler_data; - int scheduler_id; +/*! + * \internal + * \brief Container to hold all actively scheduled qualifies. + */ +static struct ao2_container *sched_qualifies; + +/*! + * \internal + * \brief Structure to hold qualify contact scheduling information. + */ +struct sched_data { + /*! The scheduling id */ + int id; + /*! The the contact being checked */ + struct ast_sip_contact *contact; }; -static pj_bool_t options_module_start(void); -static pj_bool_t options_module_stop(void); -static pj_bool_t options_module_on_rx_request(pjsip_rx_data *rdata); -static pj_bool_t options_module_on_rx_response(pjsip_rx_data *rdata); +/*! + * \internal + * \brief Destroy the scheduled data and remove from scheduler. + */ +static void sched_data_destructor(void *obj) +{ + struct sched_data *data = obj; + ao2_cleanup(data->contact); +} +/*! + * \internal + * \brief Create the scheduling data object. + */ +static struct sched_data *sched_data_create(struct ast_sip_contact *contact) +{ + struct sched_data *data = ao2_alloc(sizeof(*data), sched_data_destructor); -static pjsip_module options_module = { - .name = {"Options Module", 14}, - .id = -1, - .priority = PJSIP_MOD_PRIORITY_APPLICATION, - .start = options_module_start, - .stop = options_module_stop, - .on_rx_request = options_module_on_rx_request, - .on_rx_response = options_module_on_rx_response, + if (!data) { + ast_log(LOG_ERROR, "Unable to create schedule qualify data\n"); + return NULL; + } + + data->contact = contact; + ao2_ref(data->contact, +1); + + return data; +} + +/*! + * \internal + * \brief Send a qualify contact request within a threaded task. + */ +static int qualify_contact_task(void *obj) +{ + RAII_VAR(struct ast_sip_contact *, contact, obj, ao2_cleanup); + return qualify_contact(contact); +} + +/*! + * \internal + * \brief Send a scheduled qualify contact request. + */ +static int qualify_contact_sched(const void *obj) +{ + struct sched_data *data = (struct sched_data *)obj; + + ao2_ref(data->contact, +1); + if (ast_sip_push_task(NULL, qualify_contact_task, data->contact)) { + ao2_ref(data->contact, -1); + ao2_cleanup(data); + return 0; + } + + return data->contact->qualify_frequency * 1000; +} + +/*! + * \internal + * \brief Set up a scheduled qualify contact check. + */ +static void schedule_qualify(struct ast_sip_contact *contact) +{ + RAII_VAR(struct sched_data *, data, sched_data_create(contact), ao2_cleanup); + + if (!data) { + return; + } + + ao2_ref(data, +1); + if ((data->id = ast_sched_add_variable( + sched, contact->qualify_frequency * 1000, + qualify_contact_sched, data, 1)) < 0) { + + ao2_ref(data, -1); + ast_log(LOG_ERROR, "Unable to schedule qualify for contact %s\n", + contact->uri); + return; + } + + ao2_link(sched_qualifies, data); +} + +/*! + * \internal + * \brief Remove the contact from the scheduler. + */ +static void unschedule_qualify(struct ast_sip_contact *contact) +{ + RAII_VAR(struct sched_data *, data, ao2_find( + sched_qualifies, contact, OBJ_UNLINK), ao2_cleanup); + + if (!data) { + return; + } + + AST_SCHED_DEL_UNREF(sched, data->id, ao2_cleanup(data)); +} + +/*! + * \internal + * \brief Qualify the given contact and set up scheduling if configured. + */ +static void qualify_and_schedule(struct ast_sip_contact *contact) +{ + unschedule_qualify(contact); + + if (contact->qualify_frequency) { + ao2_ref(contact, +1); + ast_sip_push_task(NULL, qualify_contact_task, contact); + + schedule_qualify(contact); + } +} + +/*! + * \internal + * \brief A new contact has been created make sure it is available. + */ +static void contact_created(const void *obj) +{ + qualify_and_schedule((struct ast_sip_contact *)obj); +} + +/*! + * \internal + * \brief A contact has been deleted remove status tracking. + */ +static void contact_deleted(const void *obj) +{ + struct ast_sip_contact *contact = (struct ast_sip_contact *)obj; + RAII_VAR(struct ast_sip_contact_status *, status, NULL, ao2_cleanup); + + unschedule_qualify(contact); + + if (!(status = ast_sorcery_retrieve_by_id( + ast_sip_get_sorcery(), CONTACT_STATUS, + ast_sorcery_object_get_id(contact)))) { + return; + } + + if (ast_sorcery_delete(ast_sip_get_sorcery(), status)) { + ast_log(LOG_ERROR, "Unable to delete ast_sip_contact_status for contact %s\n", + contact->uri); + } +} + +struct ast_sorcery_observer contact_observer = { + .created = contact_created, + .deleted = contact_deleted }; -static pj_bool_t options_module_start(void) +static pj_bool_t options_start(void) { if (!(sched = ast_sched_context_create()) || ast_sched_start_thread(sched)) { @@ -60,9 +459,11 @@ static pj_bool_t options_module_start(void) return PJ_SUCCESS; } -static pj_bool_t options_module_stop(void) +static pj_bool_t options_stop(void) { - ao2_t_ref(scheduled_qualifies, -1, "Remove scheduled qualifies on module stop"); + ast_sorcery_observer_remove(ast_sip_get_sorcery(), "contact", &contact_observer); + + ao2_t_ref(sched_qualifies, -1, "Remove scheduled qualifies on module stop"); if (sched) { ast_sched_context_destroy(sched); @@ -71,18 +472,20 @@ static pj_bool_t options_module_stop(void) return PJ_SUCCESS; } -static pj_status_t send_options_response(pjsip_rx_data *rdata, pjsip_dialog *pj_dlg, int code) +static pj_status_t send_options_response(pjsip_rx_data *rdata, int code) { pjsip_endpoint *endpt = ast_sip_get_pjsip_endpoint(); - pjsip_transaction *pj_trans = pjsip_rdata_get_tsx(rdata); + pjsip_dialog *dlg = pjsip_rdata_get_dlg(rdata); + pjsip_transaction *trans = pjsip_rdata_get_tsx(rdata); pjsip_tx_data *tdata; const pjsip_hdr *hdr; pjsip_response_addr res_addr; pj_status_t status; /* Make the response object */ - status = pjsip_endpt_create_response(endpt, rdata, code, NULL, &tdata); - if (status != PJ_SUCCESS) { + if ((status = pjsip_endpt_create_response( + endpt, rdata, code, NULL, &tdata) != PJ_SUCCESS)) { + ast_log(LOG_ERROR, "Unable to create response (%d)\n", status); return status; } @@ -106,262 +509,267 @@ static pj_status_t send_options_response(pjsip_rx_data *rdata, pjsip_dialog *pj_ ast_sip_add_header(tdata, "Accept-Encoding", DEFAULT_ENCODING); ast_sip_add_header(tdata, "Accept-Language", DEFAULT_LANGUAGE); - if (pj_dlg && pj_trans) { - status = pjsip_dlg_send_response(pj_dlg, pj_trans, tdata); + if (dlg && trans) { + status = pjsip_dlg_send_response(dlg, trans, tdata); } else { /* Get where to send request. */ - status = pjsip_get_response_addr(tdata->pool, rdata, &res_addr); - if (status != PJ_SUCCESS) { + if ((status = pjsip_get_response_addr( + tdata->pool, rdata, &res_addr)) != PJ_SUCCESS) { + ast_log(LOG_ERROR, "Unable to get response address (%d)\n", + status); + pjsip_tx_data_dec_ref(tdata); return status; } - status = pjsip_endpt_send_response(endpt, &res_addr, tdata, NULL, NULL); + status = pjsip_endpt_send_response(endpt, &res_addr, tdata, + NULL, NULL); + } + + if (status != PJ_SUCCESS) { + ast_log(LOG_ERROR, "Unable to send response (%d)\n", status); } return status; } -static pj_bool_t options_module_on_rx_request(pjsip_rx_data *rdata) +static pj_bool_t options_on_rx_request(pjsip_rx_data *rdata) { RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup); - pjsip_dialog *dlg = pjsip_rdata_get_dlg(rdata); pjsip_uri *ruri; pjsip_sip_uri *sip_ruri; char exten[AST_MAX_EXTENSION]; - if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, &pjsip_options_method)) { + if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, + &pjsip_options_method)) { + return PJ_FALSE; + } + + if (!(endpoint = ast_pjsip_rdata_get_endpoint(rdata))) { return PJ_FALSE; } - endpoint = ast_pjsip_rdata_get_endpoint(rdata); - ast_assert(endpoint != NULL); ruri = rdata->msg_info.msg->line.req.uri; if (!PJSIP_URI_SCHEME_IS_SIP(ruri) && !PJSIP_URI_SCHEME_IS_SIPS(ruri)) { - send_options_response(rdata, dlg, 416); + send_options_response(rdata, 416); return -1; } - + sip_ruri = pjsip_uri_get_uri(ruri); ast_copy_pj_str(exten, &sip_ruri->user, sizeof(exten)); if (ast_shutting_down()) { - send_options_response(rdata, dlg, 503); + send_options_response(rdata, 503); } else if (!ast_exists_extension(NULL, endpoint->context, exten, 1, NULL)) { - send_options_response(rdata, dlg, 404); + send_options_response(rdata, 404); } else { - send_options_response(rdata, dlg, 200); + send_options_response(rdata, 200); } return PJ_TRUE; } -static pj_bool_t options_module_on_rx_response(pjsip_rx_data *rdata) -{ +static pjsip_module options_module = { + .name = {"Options Module", 14}, + .id = -1, + .priority = PJSIP_MOD_PRIORITY_APPLICATION, + .start = options_start, + .stop = options_stop, + .on_rx_request = options_on_rx_request, +}; - return PJ_SUCCESS; +/*! + * \internal + * \brief Send qualify request to the given contact. + */ +static int cli_on_contact(void *obj, void *arg, int flags) +{ + struct ast_sip_contact *contact = obj; + struct ast_cli_args *a = arg; + ast_cli(a->fd, " contact %s\n", contact->uri); + qualify_contact(contact); + return 0; } -static int qualify_info_hash_fn(const void *obj, int flags) +/*! + * \internal + * \brief For an endpoint iterate over and qualify all aors/contacts + */ +static void cli_qualify_contacts(struct ast_cli_args *a, const char *endpoint_name, + struct ast_sip_endpoint *endpoint) { - const struct qualify_info *info = obj; - const char *endpoint_id = flags & OBJ_KEY ? obj : info->endpoint_id; + char *aor_name, *aors; - return ast_str_hash(endpoint_id); -} + if (ast_strlen_zero(endpoint->aors)) { + ast_cli(a->fd, "Endpoint %s has no AoR's configured\n", + endpoint_name); + return; + } -static int qualify_info_cmp_fn(void *obj, void *arg, int flags) -{ - struct qualify_info *left = obj; - struct qualify_info *right = arg; - const char *right_endpoint_id = flags & OBJ_KEY ? arg : right->endpoint_id; + aors = ast_strdupa(endpoint->aors); - return strcmp(left->endpoint_id, right_endpoint_id) ? 0 : CMP_MATCH | CMP_STOP; -} + while ((aor_name = strsep(&aors, ","))) { + RAII_VAR(struct ast_sip_aor *, aor, + ast_sip_location_retrieve_aor(aor_name), ao2_cleanup); + RAII_VAR(struct ao2_container *, contacts, NULL, ao2_cleanup); + if (!aor || !(contacts = ast_sip_location_retrieve_aor_contacts(aor))) { + continue; + } -static void qualify_info_destructor(void *obj) -{ - struct qualify_info *info = obj; - if (!info) { - return; - } - ast_string_field_free_memory(info); - /* Cancel the qualify */ - if (!AST_SCHED_DEL(sched, info->scheduler_id)) { - /* If we successfully deleted the qualify, we got it before it - * fired. We can safely delete the data that was passed to it. - * Otherwise, we're getting deleted while this is firing - don't - * touch that memory! - */ - ast_free(info->scheduler_data); + ast_cli(a->fd, "Sending qualify to endpoint %s", endpoint_name); + ao2_callback(contacts, OBJ_NODATA, cli_on_contact, a); } } -static struct qualify_info *create_qualify_info(struct ast_sip_endpoint *endpoint) +static char *cli_qualify(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) { - struct qualify_info *info; + RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup); + const char *endpoint_name; - info = ao2_alloc(sizeof(*info), qualify_info_destructor); - if (!info) { + switch (cmd) { + case CLI_INIT: + e->command = "sip qualify"; + e->usage = + "Usage: sip qualify <endpoint>\n" + " Send a SIP OPTIONS request to all contacts on the endpoint.\n"; + return NULL; + case CLI_GENERATE: return NULL; } - if (ast_string_field_init(info, 64)) { - ao2_ref(info, -1); - return NULL; + if (a->argc != 3) { + return CLI_SHOWUSAGE; } - ast_string_field_set(info, endpoint_id, ast_sorcery_object_get_id(endpoint)); - return info; + endpoint_name = a->argv[2]; + + if (!(endpoint = ast_sorcery_retrieve_by_id( + ast_sip_get_sorcery(), "endpoint", endpoint_name))) { + ast_cli(a->fd, "Unable to retrieve endpoint %s\n", endpoint_name); + return CLI_FAILURE; + } + + /* send a qualify for all contacts registered with the endpoint */ + cli_qualify_contacts(a, endpoint_name, endpoint); + + return CLI_SUCCESS; } -static int send_qualify_request(void *data) +static struct ast_cli_entry cli_options[] = { + AST_CLI_DEFINE(cli_qualify, "Send an OPTIONS request to a SIP endpoint") +}; + +static int sched_qualifies_hash_fn(const void *obj, int flags) { - struct ast_sip_endpoint *endpoint = data; - pjsip_tx_data *tdata; - /* YAY! Send an OPTIONS request. */ + const struct sched_data *data = obj; - ast_sip_create_request("OPTIONS", NULL, endpoint, NULL, &tdata); - ast_sip_send_request(tdata, NULL, endpoint); + return ast_str_hash(ast_sorcery_object_get_id(data->contact)); +} - ao2_cleanup(endpoint); - return 0; +static int sched_qualifies_cmp_fn(void *obj, void *arg, int flags) +{ + struct sched_data *data = obj; + + return !strcmp(ast_sorcery_object_get_id(data->contact), + ast_sorcery_object_get_id(arg)); } -static int qualify_endpoint_scheduler_cb(const void *data) +int ast_sip_initialize_sorcery_qualify(struct ast_sorcery *sorcery) { - RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup); - struct ast_sorcery *sorcery; - char *endpoint_id = (char *)data; + /* initialize sorcery ast_sip_contact_status resource */ + ast_sorcery_apply_default(sorcery, CONTACT_STATUS, "memory", NULL); - sorcery = ast_sip_get_sorcery(); - if (!sorcery) { - ast_free(endpoint_id); - return 0; + if (ast_sorcery_object_register(sorcery, CONTACT_STATUS, + contact_status_alloc, NULL, NULL)) { + ast_log(LOG_ERROR, "Unable to register ast_sip_contact_status in sorcery\n"); + return -1; } - endpoint = ast_sorcery_retrieve_by_id(sorcery, "endpoint", endpoint_id); - if (!endpoint) { - /* Whoops, endpoint went away */ - ast_free(endpoint_id); - return 0; - } + ast_sorcery_object_field_register(sorcery, CONTACT_STATUS, "rtt", "0", OPT_UINT_T, + 1, FLDSET(struct ast_sip_contact_status, status)); + ast_sorcery_object_field_register(sorcery, CONTACT_STATUS, "rtt", "0", OPT_UINT_T, + 1, FLDSET(struct ast_sip_contact_status, rtt)); - ast_sip_push_task(NULL, send_qualify_request, endpoint); + if (ast_sorcery_observer_add(sorcery, "contact", &contact_observer)) { + ast_log(LOG_WARNING, "Unable to add contact observer\n"); + return -1; + } - return 1; + return 0; } -static void schedule_qualifies(void) +static int qualify_and_schedule_cb(void *obj, void *arg, int flags) { - RAII_VAR(struct ao2_container *, endpoints, NULL, ao2_cleanup); - struct ao2_iterator it_endpoints; - struct ast_sip_endpoint *endpoint; - struct qualify_info *info; - char *endpoint_id; + struct ast_sip_contact *contact = obj; + struct ast_sip_aor *aor = arg; - endpoints = ast_res_sip_get_endpoints(); - if (!endpoints) { - return; - } + contact->qualify_frequency = aor->qualify_frequency; + qualify_and_schedule(contact); - it_endpoints = ao2_iterator_init(endpoints, 0); - while ((endpoint = ao2_iterator_next(&it_endpoints))) { - if (endpoint->qualify_frequency) { - /* XXX TODO: This really should only qualify registered peers, - * which means we need a registrar. We should check the - * registrar to see if this endpoint has registered and, if - * not, pass on it. - * - * Actually, all of this should just get moved into the registrar. - * Otherwise, the registar will have to kick this off when a - * new endpoint registers, so it just makes sense to have it - * all live there. - */ - info = create_qualify_info(endpoint); - if (!info) { - ao2_ref(endpoint, -1); - break; - } - endpoint_id = ast_strdup(info->endpoint_id); - if (!endpoint_id) { - ao2_t_ref(info, -1, "Dispose of info on off nominal"); - ao2_ref(endpoint, -1); - break; - } - info->scheduler_data = endpoint_id; - info->scheduler_id = ast_sched_add_variable(sched, endpoint->qualify_frequency * 1000, qualify_endpoint_scheduler_cb, endpoint_id, 1); - ao2_t_link(scheduled_qualifies, info, "Link scheduled qualify information into container"); - ao2_t_ref(info, -1, "Dispose of creation ref"); - } - ao2_t_ref(endpoint, -1, "Dispose of iterator ref"); - } - ao2_iterator_destroy(&it_endpoints); + return 0; } -static char *send_options(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) +/*! + * \internal + * \brief Qualify and schedule an endpoint's permanent contacts + * + * \detail For the given endpoint retrieve its list of aors, qualify all + * permanent contacts, and schedule for checks if configured. + */ +static int qualify_and_schedule_permanent_cb(void *obj, void *arg, int flags) { - RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup); - const char *endpoint_name; - pjsip_tx_data *tdata; + struct ast_sip_endpoint *endpoint = obj; + char *aor_name, *aors; - switch (cmd) { - case CLI_INIT: - e->command = "sip send options"; - e->usage = - "Usage: sip send options <endpoint>\n" - " Send a SIP OPTIONS request to the specified endpoint.\n"; - return NULL; - case CLI_GENERATE: - return NULL; + if (ast_strlen_zero(endpoint->aors)) { + return 0; } - if (a->argc != 4) { - return CLI_SHOWUSAGE; - } + aors = ast_strdupa(endpoint->aors); - endpoint_name = a->argv[3]; + while ((aor_name = strsep(&aors, ","))) { + RAII_VAR(struct ast_sip_aor *, aor, + ast_sip_location_retrieve_aor(aor_name), ao2_cleanup); - endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", endpoint_name); - if (!endpoint) { - ast_log(LOG_ERROR, "Unable to retrieve endpoint %s\n", endpoint_name); - return CLI_FAILURE; + if (!aor || !aor->permanent_contacts) { + continue; + } + ao2_callback(aor->permanent_contacts, OBJ_NODATA, qualify_and_schedule_cb, aor); } - if (ast_sip_create_request("OPTIONS", NULL, endpoint, NULL, &tdata)) { - ast_log(LOG_ERROR, "Unable to create OPTIONS request to endpoint %s\n", endpoint_name); - return CLI_FAILURE; - } + return 0; +} - if (ast_sip_send_request(tdata, NULL, endpoint)) { - ast_log(LOG_ERROR, "Unable to send OPTIONS request to endpoint %s\n", endpoint_name); - return CLI_FAILURE; - } +static void qualify_and_schedule_permanent(void) +{ + RAII_VAR(struct ao2_container *, endpoints, + ast_res_sip_get_endpoints(), ao2_cleanup); - return CLI_SUCCESS; + ao2_callback(endpoints, OBJ_NODATA, + qualify_and_schedule_permanent_cb, NULL); } -static struct ast_cli_entry cli_options[] = { - AST_CLI_DEFINE(send_options, "Send an OPTIONS requst to an arbitrary SIP URI"), -}; - int ast_res_sip_init_options_handling(int reload) { const pj_str_t STR_OPTIONS = { "OPTIONS", 7 }; - if (scheduled_qualifies) { - ao2_t_ref(scheduled_qualifies, -1, "Remove old scheduled qualifies"); + if (sched_qualifies) { + ao2_t_ref(sched_qualifies, -1, "Remove old scheduled qualifies"); } - scheduled_qualifies = ao2_t_container_alloc(QUALIFIED_BUCKETS, qualify_info_hash_fn, qualify_info_cmp_fn, "Create container for scheduled qualifies"); - if (!scheduled_qualifies) { + + if (!(sched_qualifies = ao2_t_container_alloc( + QUALIFIED_BUCKETS, sched_qualifies_hash_fn, sched_qualifies_cmp_fn, + "Create container for scheduled qualifies"))) { + return -1; } if (reload) { + qualify_and_schedule_permanent(); return 0; } if (pjsip_endpt_register_module(ast_sip_get_pjsip_endpoint(), &options_module) != PJ_SUCCESS) { - options_module_stop(); + options_stop(); return -1; } @@ -370,9 +778,8 @@ int ast_res_sip_init_options_handling(int reload) return -1; } + qualify_and_schedule_permanent(); ast_cli_register_multiple(cli_options, ARRAY_LEN(cli_options)); - schedule_qualifies(); - return 0; } |