diff options
Diffstat (limited to 'res')
-rw-r--r-- | res/res_pjsip.c | 28 | ||||
-rw-r--r-- | res/res_pjsip/location.c | 17 | ||||
-rw-r--r-- | res/res_pjsip/pjsip_configuration.c | 10 | ||||
-rw-r--r-- | res/res_pjsip/pjsip_options.c | 10 | ||||
-rw-r--r-- | res/res_pjsip_header_funcs.c | 9 | ||||
-rw-r--r-- | res/res_pjsip_registrar.c | 32 | ||||
-rw-r--r-- | res/res_rtp_multicast.c | 189 | ||||
-rw-r--r-- | res/res_rtp_multicast.exports.in | 6 |
8 files changed, 284 insertions, 17 deletions
diff --git a/res/res_pjsip.c b/res/res_pjsip.c index bebe941b5..8fc3c530e 100644 --- a/res/res_pjsip.c +++ b/res/res_pjsip.c @@ -1175,6 +1175,28 @@ Asterisk Server name on which SIP endpoint registered. </para></description> </configOption> + <configOption name="via_addr"> + <synopsis>IP-address of the last Via header from registration.</synopsis> + <description><para> + The last Via header should contain the address of UA which sent the request. + The IP-address of the last Via header is automatically stored based on data present + in incoming SIP REGISTER requests and is not intended to be configured manually. + </para></description> + </configOption> + <configOption name="via_port"> + <synopsis>IP-port of the last Via header from registration.</synopsis> + <description><para> + The IP-port of the last Via header is automatically stored based on data present + in incoming SIP REGISTER requests and is not intended to be configured manually. + </para></description> + </configOption> + <configOption name="call_id"> + <synopsis>Call-ID header from registration.</synopsis> + <description><para> + The Call-ID header is automatically stored based on data present + in incoming SIP REGISTER requests and is not intended to be configured manually. + </para></description> + </configOption> </configObject> <configObject name="aor"> <synopsis>The configuration for a location of an endpoint</synopsis> @@ -1967,6 +1989,12 @@ <parameter name="RegExpire"> <para>Absolute time that this contact is no longer valid after</para> </parameter> + <parameter name="ViaAddress"> + <para>IP address:port of the last Via header in REGISTER request</para> + </parameter> + <parameter name="CallID"> + <para>Content of the Call-ID header in REGISTER request</para> + </parameter> </syntax> </managerEventInstance> </managerEvent> diff --git a/res/res_pjsip/location.c b/res/res_pjsip/location.c index fd6db6edc..43e6ea40f 100644 --- a/res/res_pjsip/location.c +++ b/res/res_pjsip/location.c @@ -121,6 +121,8 @@ static void *contact_alloc(const char *name) } ast_string_field_init_extended(contact, reg_server); + ast_string_field_init_extended(contact, via_addr); + ast_string_field_init_extended(contact, call_id); /* Dynamic contacts are delimited with ";@" and static ones with "@@" */ if ((aor_separator = strstr(id, ";@")) || (aor_separator = strstr(id, "@@"))) { @@ -303,6 +305,7 @@ struct ast_sip_contact *ast_sip_location_retrieve_contact(const char *contact_na int ast_sip_location_add_contact_nolock(struct ast_sip_aor *aor, const char *uri, struct timeval expiration_time, const char *path_info, const char *user_agent, + const char *via_addr, int via_port, const char *call_id, struct ast_sip_endpoint *endpoint) { char name[MAX_OBJECT_FIELD * 2 + 3]; @@ -337,6 +340,15 @@ int ast_sip_location_add_contact_nolock(struct ast_sip_aor *aor, const char *uri ast_string_field_set(contact, reg_server, ast_config_AST_SYSTEM_NAME); } + if (!ast_strlen_zero(via_addr)) { + ast_string_field_set(contact, via_addr, via_addr); + } + contact->via_port = via_port; + + if (!ast_strlen_zero(call_id)) { + ast_string_field_set(contact, call_id, call_id); + } + contact->endpoint = ao2_bump(endpoint); return ast_sorcery_create(ast_sip_get_sorcery(), contact); @@ -344,6 +356,7 @@ int ast_sip_location_add_contact_nolock(struct ast_sip_aor *aor, const char *uri int ast_sip_location_add_contact(struct ast_sip_aor *aor, const char *uri, struct timeval expiration_time, const char *path_info, const char *user_agent, + const char *via_addr, int via_port, const char *call_id, struct ast_sip_endpoint *endpoint) { int res; @@ -356,6 +369,7 @@ int ast_sip_location_add_contact(struct ast_sip_aor *aor, const char *uri, ao2_wrlock(lock); res = ast_sip_location_add_contact_nolock(aor, uri, expiration_time, path_info, user_agent, + via_addr, via_port, call_id, endpoint); ao2_unlock(lock); ast_named_lock_put(lock); @@ -1120,6 +1134,9 @@ int ast_sip_initialize_sorcery_location(void) ast_sorcery_object_field_register(sorcery, "contact", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, outbound_proxy)); ast_sorcery_object_field_register(sorcery, "contact", "user_agent", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, user_agent)); ast_sorcery_object_field_register(sorcery, "contact", "reg_server", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, reg_server)); + ast_sorcery_object_field_register(sorcery, "contact", "via_addr", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, via_addr)); + ast_sorcery_object_field_register(sorcery, "contact", "via_port", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_contact, via_port)); + ast_sorcery_object_field_register(sorcery, "contact", "call_id", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_contact, call_id)); 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)); diff --git a/res/res_pjsip/pjsip_configuration.c b/res/res_pjsip/pjsip_configuration.c index 8b6fe61d8..3c4949573 100644 --- a/res/res_pjsip/pjsip_configuration.c +++ b/res/res_pjsip/pjsip_configuration.c @@ -131,7 +131,7 @@ static int persistent_endpoint_update_state(void *obj, void *arg, int flags) } } - ast_verb(1, "Endpoint %s is now Reachable\n", ast_endpoint_get_resource(endpoint)); + ast_verb(2, "Endpoint %s is now Reachable\n", ast_endpoint_get_resource(endpoint)); } else { ast_endpoint_set_state(endpoint, AST_ENDPOINT_OFFLINE); blob = ast_json_pack("{s: s}", "peer_status", "Unreachable"); @@ -144,7 +144,7 @@ static int persistent_endpoint_update_state(void *obj, void *arg, int flags) } } - ast_verb(1, "Endpoint %s is now Unreachable\n", ast_endpoint_get_resource(endpoint)); + ast_verb(2, "Endpoint %s is now Unreachable\n", ast_endpoint_get_resource(endpoint)); } ast_free(regcontext); @@ -173,7 +173,7 @@ static void persistent_endpoint_contact_created_observer(const void *object) contact_status->status = CREATED; - ast_verb(1, "Contact %s/%s has been created\n",contact->aor, contact->uri); + ast_verb(2, "Contact %s/%s has been created\n",contact->aor, contact->uri); ao2_callback(persistent_endpoints, OBJ_NODATA, persistent_endpoint_update_state, contact_status); ao2_cleanup(contact_status); @@ -192,7 +192,7 @@ static void persistent_endpoint_contact_deleted_observer(const void *object) return; } - ast_verb(1, "Contact %s/%s has been deleted\n", contact->aor, contact->uri); + ast_verb(2, "Contact %s/%s has been deleted\n", contact->aor, contact->uri); ast_statsd_log_string_va("PJSIP.contacts.states.%s", AST_STATSD_GAUGE, "-1", 1.0, ast_sip_get_contact_status_label(contact_status->status)); ast_statsd_log_string_va("PJSIP.contacts.states.%s", AST_STATSD_GAUGE, @@ -220,7 +220,7 @@ static void persistent_endpoint_contact_status_observer(const void *object) } if (contact_status->status != contact_status->last_status) { - ast_verb(1, "Contact %s/%s is now %s. RTT: %.3f msec\n", contact_status->aor, contact_status->uri, + ast_verb(3, "Contact %s/%s is now %s. RTT: %.3f msec\n", contact_status->aor, contact_status->uri, ast_sip_get_contact_status_label(contact_status->status), contact_status->rtt / 1000.0); diff --git a/res/res_pjsip/pjsip_options.c b/res/res_pjsip/pjsip_options.c index 62640fe4e..1114336bd 100644 --- a/res/res_pjsip/pjsip_options.c +++ b/res/res_pjsip/pjsip_options.c @@ -1156,6 +1156,16 @@ static int format_contact_status(void *obj, void *arg, int flags) ast_str_append(&buf, 0, "URI: %s\r\n", contact->uri); ast_str_append(&buf, 0, "UserAgent: %s\r\n", contact->user_agent); ast_str_append(&buf, 0, "RegExpire: %ld\r\n", contact->expiration_time.tv_sec); + if (!ast_strlen_zero(contact->via_addr)) { + ast_str_append(&buf, 0, "ViaAddress: %s", contact->via_addr); + if (contact->via_port) { + ast_str_append(&buf, 0, ":%d", contact->via_port); + } + ast_str_append(&buf, 0, "\r\n"); + } + if (!ast_strlen_zero(contact->call_id)) { + ast_str_append(&buf, 0, "CallID: %s\r\n", contact->call_id); + } ast_str_append(&buf, 0, "Status: %s\r\n", ast_sip_get_contact_status_label(status->status)); if (status->status == UNKNOWN) { ast_str_append(&buf, 0, "RoundtripUsec: N/A\r\n"); diff --git a/res/res_pjsip_header_funcs.c b/res/res_pjsip_header_funcs.c index 7d164b12a..648f1c860 100644 --- a/res/res_pjsip_header_funcs.c +++ b/res/res_pjsip_header_funcs.c @@ -39,7 +39,8 @@ /*** DOCUMENTATION <function name="PJSIP_HEADER" language="en_US"> <synopsis> - Gets, adds, updates or removes the specified SIP header from a PJSIP session. + Gets headers from an inbound PJSIP channel. Adds, updates or removes the + specified SIP header from an outbound PJSIP channel. </synopsis> <syntax> <parameter name="action" required="true"> @@ -75,6 +76,10 @@ </syntax> <description> + <para>PJSIP_HEADER allows you to read specific SIP headers from the inbound + PJSIP channel as well as write(add, update, remove) headers on the outbound + channel. One exception is that you can read headers that you have already + added on the outbound channel.</para> <para>Examples:</para> <para>;</para> <para>; Set 'somevar' to the value of the 'From' header.</para> @@ -120,7 +125,7 @@ <note><para>If you call PJSIP_HEADER in a normal dialplan context you'll be operating on the <emphasis>caller's (incoming)</emphasis> channel which - may not be what you want. To operate on the <emphasis>callee's (outgoing)</emphasis> + may not be what you want. To operate on the <emphasis>callee's (outgoing)</emphasis> channel call PJSIP_HEADER in a pre-dial handler. </para> <para>Example:</para> <para>;</para> diff --git a/res/res_pjsip_registrar.c b/res/res_pjsip_registrar.c index cbc33ab80..0e14ab786 100644 --- a/res/res_pjsip_registrar.c +++ b/res/res_pjsip_registrar.c @@ -447,6 +447,13 @@ static int rx_task_core(struct rx_task_data *task_data, struct ao2_container *co char *user_agent = NULL; pjsip_user_agent_hdr *user_agent_hdr; pjsip_expires_hdr *expires_hdr; + pjsip_via_hdr *via_hdr; + pjsip_via_hdr *via_hdr_last; + char *via_addr = NULL; + int via_port = 0; + pjsip_cid_hdr *call_id_hdr; + char *call_id = NULL; + size_t alloc_size; /* So we don't count static contacts against max_contacts we prune them out from the container */ ao2_callback(contacts, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, registrar_prune_static, NULL); @@ -484,11 +491,32 @@ static int rx_task_core(struct rx_task_data *task_data, struct ao2_container *co user_agent_hdr = pjsip_msg_find_hdr_by_name(task_data->rdata->msg_info.msg, &USER_AGENT, NULL); if (user_agent_hdr) { - size_t alloc_size = pj_strlen(&user_agent_hdr->hvalue) + 1; + alloc_size = pj_strlen(&user_agent_hdr->hvalue) + 1; user_agent = ast_alloca(alloc_size); ast_copy_pj_str(user_agent, &user_agent_hdr->hvalue, alloc_size); } + /* Find the first Via header */ + via_hdr = via_hdr_last = (pjsip_via_hdr*) pjsip_msg_find_hdr(task_data->rdata->msg_info.msg, PJSIP_H_VIA, NULL); + if (via_hdr) { + /* Find the last Via header */ + while ( (via_hdr = (pjsip_via_hdr*) pjsip_msg_find_hdr(task_data->rdata->msg_info.msg, + PJSIP_H_VIA, via_hdr->next)) != NULL) { + via_hdr_last = via_hdr; + } + alloc_size = pj_strlen(&via_hdr_last->sent_by.host) + 1; + via_addr = ast_alloca(alloc_size); + ast_copy_pj_str(via_addr, &via_hdr_last->sent_by.host, alloc_size); + via_port=via_hdr_last->sent_by.port; + } + + call_id_hdr = (pjsip_cid_hdr*) pjsip_msg_find_hdr(task_data->rdata->msg_info.msg, PJSIP_H_CALL_ID, NULL); + if (call_id_hdr) { + alloc_size = pj_strlen(&call_id_hdr->id) + 1; + call_id = ast_alloca(alloc_size); + ast_copy_pj_str(call_id, &call_id_hdr->id, alloc_size); + } + /* Iterate each provided Contact header and add, update, or delete */ while ((contact_hdr = pjsip_msg_find_hdr(task_data->rdata->msg_info.msg, PJSIP_H_CONTACT, contact_hdr ? contact_hdr->next : NULL))) { int expiration; @@ -520,7 +548,7 @@ static int rx_task_core(struct rx_task_data *task_data, struct ao2_container *co if (ast_sip_location_add_contact_nolock(task_data->aor, contact_uri, ast_tvadd(ast_tvnow(), ast_samp2tv(expiration, 1)), path_str ? ast_str_buffer(path_str) : NULL, - user_agent, task_data->endpoint)) { + user_agent, via_addr, via_port, call_id, task_data->endpoint)) { ast_log(LOG_ERROR, "Unable to bind contact '%s' to AOR '%s'\n", contact_uri, aor_name); continue; diff --git a/res/res_rtp_multicast.c b/res/res_rtp_multicast.c index 192f3d137..5c419d3e7 100644 --- a/res/res_rtp_multicast.c +++ b/res/res_rtp_multicast.c @@ -54,6 +54,8 @@ ASTERISK_REGISTER_FILE() #include "asterisk/module.h" #include "asterisk/rtp_engine.h" #include "asterisk/format_cache.h" +#include "asterisk/multicast_rtp.h" +#include "asterisk/app.h" /*! Command value used for Linksys paging to indicate we are starting */ #define LINKSYS_MCAST_STARTCMD 6 @@ -63,8 +65,10 @@ ASTERISK_REGISTER_FILE() /*! \brief Type of paging to do */ enum multicast_type { + /*! Type has not been set yet */ + MULTICAST_TYPE_UNSPECIFIED = 0, /*! Simple multicast enabled client/receiver paging like Snom and Barix uses */ - MULTICAST_TYPE_BASIC = 0, + MULTICAST_TYPE_BASIC, /*! More advanced Linksys type paging which requires a start and stop packet */ MULTICAST_TYPE_LINKSYS, }; @@ -95,6 +99,91 @@ struct multicast_rtp { struct timeval txcore; }; +enum { + OPT_CODEC = (1 << 0), + OPT_LOOP = (1 << 1), + OPT_TTL = (1 << 2), + OPT_IF = (1 << 3), +}; + +enum { + OPT_ARG_CODEC = 0, + OPT_ARG_LOOP, + OPT_ARG_TTL, + OPT_ARG_IF, + OPT_ARG_ARRAY_SIZE, +}; + +AST_APP_OPTIONS(multicast_rtp_options, BEGIN_OPTIONS + /*! Set the codec to be used for multicast RTP */ + AST_APP_OPTION_ARG('c', OPT_CODEC, OPT_ARG_CODEC), + /*! Set whether multicast RTP is looped back to the sender */ + AST_APP_OPTION_ARG('l', OPT_LOOP, OPT_ARG_LOOP), + /*! Set the hop count for multicast RTP */ + AST_APP_OPTION_ARG('t', OPT_TTL, OPT_ARG_TTL), + /*! Set the interface from which multicast RTP is sent */ + AST_APP_OPTION_ARG('i', OPT_IF, OPT_ARG_IF), +END_OPTIONS ); + +struct ast_multicast_rtp_options { + char *type; + char *options; + struct ast_format *fmt; + struct ast_flags opts; + char *opt_args[OPT_ARG_ARRAY_SIZE]; + /*! The type and options are stored in this buffer */ + char buf[0]; +}; + +struct ast_multicast_rtp_options *ast_multicast_rtp_create_options(const char *type, + const char *options) +{ + struct ast_multicast_rtp_options *mcast_options; + char *pos; + + mcast_options = ast_calloc(1, sizeof(*mcast_options) + + strlen(type) + + strlen(options) + 2); + if (!mcast_options) { + return NULL; + } + + pos = mcast_options->buf; + + /* Safe */ + strcpy(pos, type); + mcast_options->type = pos; + pos += strlen(type) + 1; + + /* Safe */ + strcpy(pos, options); + mcast_options->options = pos; + + if (ast_app_parse_options(multicast_rtp_options, &mcast_options->opts, + mcast_options->opt_args, mcast_options->options)) { + ast_log(LOG_WARNING, "Error parsing multicast RTP options\n"); + ast_multicast_rtp_free_options(mcast_options); + return NULL; + } + + return mcast_options; +} + +void ast_multicast_rtp_free_options(struct ast_multicast_rtp_options *mcast_options) +{ + ast_free(mcast_options); +} + +struct ast_format *ast_multicast_rtp_options_get_format(struct ast_multicast_rtp_options *mcast_options) +{ + if (ast_test_flag(&mcast_options->opts, OPT_CODEC) + && !ast_strlen_zero(mcast_options->opt_args[OPT_ARG_CODEC])) { + return ast_format_cache_get(mcast_options->opt_args[OPT_ARG_CODEC]); + } + + return NULL; +} + /* Forward Declarations */ static int multicast_rtp_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data); static int multicast_rtp_activate(struct ast_rtp_instance *instance); @@ -112,21 +201,93 @@ static struct ast_rtp_engine multicast_rtp_engine = { .read = multicast_rtp_read, }; +static int set_type(struct multicast_rtp *multicast, const char *type) +{ + if (!strcasecmp(type, "basic")) { + multicast->type = MULTICAST_TYPE_BASIC; + } else if (!strcasecmp(type, "linksys")) { + multicast->type = MULTICAST_TYPE_LINKSYS; + } else { + ast_log(LOG_WARNING, "Unrecognized multicast type '%s' specified.\n", type); + return -1; + } + + return 0; +} + +static void set_ttl(int sock, const char *ttl_str) +{ + int ttl; + + if (ast_strlen_zero(ttl_str)) { + return; + } + + ast_debug(3, "Setting multicast TTL to %s\n", ttl_str); + + if (sscanf(ttl_str, "%30d", &ttl) < 1) { + ast_log(LOG_WARNING, "Inavlid multicast ttl option '%s'\n", ttl_str); + return; + } + + if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) { + ast_log(LOG_WARNING, "Could not set multicast ttl to '%s': %s\n", + ttl_str, strerror(errno)); + } +} + +static void set_loop(int sock, const char *loop_str) +{ + unsigned char loop; + + if (ast_strlen_zero(loop_str)) { + return; + } + + ast_debug(3, "Setting multicast loop to %s\n", loop_str); + + if (sscanf(loop_str, "%30hhu", &loop) < 1) { + ast_log(LOG_WARNING, "Invalid multicast loop option '%s'\n", loop_str); + return; + } + + if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0) { + ast_log(LOG_WARNING, "Could not set multicast loop to '%s': %s\n", + loop_str, strerror(errno)); + } +} + +static void set_if(int sock, const char *if_str) +{ + struct in_addr iface; + + if (ast_strlen_zero(if_str)) { + return; + } + + ast_debug(3, "Setting multicast if to %s\n", if_str); + + if (!inet_aton(if_str, &iface)) { + ast_log(LOG_WARNING, "Cannot parse if option '%s'\n", if_str); + } + + if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, &iface, sizeof(iface)) < 0) { + ast_log(LOG_WARNING, "Could not set multicast if to '%s': %s\n", + if_str, strerror(errno)); + } +} + /*! \brief Function called to create a new multicast instance */ static int multicast_rtp_new(struct ast_rtp_instance *instance, struct ast_sched_context *sched, struct ast_sockaddr *addr, void *data) { struct multicast_rtp *multicast; - const char *type = data; + struct ast_multicast_rtp_options *mcast_options = data; if (!(multicast = ast_calloc(1, sizeof(*multicast)))) { return -1; } - if (!strcasecmp(type, "basic")) { - multicast->type = MULTICAST_TYPE_BASIC; - } else if (!strcasecmp(type, "linksys")) { - multicast->type = MULTICAST_TYPE_LINKSYS; - } else { + if (set_type(multicast, mcast_options->type)) { ast_free(multicast); return -1; } @@ -136,6 +297,18 @@ static int multicast_rtp_new(struct ast_rtp_instance *instance, struct ast_sched return -1; } + if (ast_test_flag(&mcast_options->opts, OPT_LOOP)) { + set_loop(multicast->socket, mcast_options->opt_args[OPT_ARG_LOOP]); + } + + if (ast_test_flag(&mcast_options->opts, OPT_TTL)) { + set_ttl(multicast->socket, mcast_options->opt_args[OPT_ARG_TTL]); + } + + if (ast_test_flag(&mcast_options->opts, OPT_IF)) { + set_if(multicast->socket, mcast_options->opt_args[OPT_ARG_IF]); + } + multicast->ssrc = ast_random(); ast_rtp_instance_set_data(instance, multicast); @@ -316,7 +489,7 @@ static int unload_module(void) return 0; } -AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Multicast RTP Engine", +AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Multicast RTP Engine", .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, diff --git a/res/res_rtp_multicast.exports.in b/res/res_rtp_multicast.exports.in new file mode 100644 index 000000000..995a1802e --- /dev/null +++ b/res/res_rtp_multicast.exports.in @@ -0,0 +1,6 @@ +{ + global: + LINKER_SYMBOL_PREFIXast_multicast_rtp*; + local: + *; +}; |