2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
20 <depend>pjproject</depend>
21 <depend>res_pjsip</depend>
22 <support_level>core</support_level>
30 #include "asterisk/res_pjsip.h"
31 #include "asterisk/module.h"
32 #include "asterisk/taskprocessor.h"
33 #include "asterisk/cli.h"
34 #include "asterisk/stasis_system.h"
37 <configInfo name="res_pjsip_outbound_registration" language="en_US">
38 <synopsis>SIP resource for outbound registrations</synopsis>
40 <emphasis>Outbound Registration</emphasis>
42 <para>This module allows <literal>res_pjsip</literal> to register to other SIP servers.</para>
44 <configFile name="pjsip.conf">
45 <configObject name="registration">
46 <synopsis>The configuration for outbound registration</synopsis>
48 Registration is <emphasis>COMPLETELY</emphasis> separate from the rest of
49 <literal>pjsip.conf</literal>. A minimal configuration consists of
50 setting a <literal>server_uri</literal> and a <literal>client_uri</literal>.
52 <configOption name="auth_rejection_permanent" default="yes">
53 <synopsis>Determines whether failed authentication challenges are treated
54 as permanent failures.</synopsis>
55 <description><para>If this option is enabled and an authentication challenge fails,
56 registration will not be attempted again until the configuration is reloaded.</para></description>
58 <configOption name="client_uri">
59 <synopsis>Client SIP URI used when attemping outbound registration</synopsis>
61 <configOption name="contact_user">
62 <synopsis>Contact User to use in request</synopsis>
64 <configOption name="expiration" default="3600">
65 <synopsis>Expiration time for registrations in seconds</synopsis>
67 <configOption name="max_retries" default="10">
68 <synopsis>Maximum number of registration attempts.</synopsis>
70 <configOption name="outbound_auth" default="">
71 <synopsis>Authentication object to be used for outbound registrations.</synopsis>
73 <configOption name="outbound_proxy" default="">
74 <synopsis>Outbound Proxy used to send registrations</synopsis>
76 <configOption name="retry_interval" default="60">
77 <synopsis>Interval in seconds between retries if outbound registration is unsuccessful</synopsis>
79 <configOption name="server_uri">
80 <synopsis>SIP URI of the server to register against</synopsis>
82 <configOption name="transport">
83 <synopsis>Transport used for outbound authentication</synopsis>
85 <note><para>A <replaceable>transport</replaceable> configured in
86 <literal>pjsip.conf</literal>. As with other <literal>res_pjsip</literal> modules, this will use the first available transport of the appropriate type if unconfigured.</para></note>
89 <configOption name="type">
90 <synopsis>Must be of type 'registration'.</synopsis>
95 <manager name="PJSIPUnregister" language="en_US">
97 Unregister an outbound registration.
100 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
101 <parameter name="Registration" required="true">
102 <para>The outbound registration to unregister.</para>
108 /*! \brief Amount of buffer time (in seconds) before expiration that we re-register at */
109 #define REREGISTER_BUFFER_TIME 10
111 /*! \brief Various states that an outbound registration may be in */
112 enum sip_outbound_registration_status {
113 /*! \brief Currently unregistered */
114 SIP_REGISTRATION_UNREGISTERED = 0,
115 /*! \brief Registered, yay! */
116 SIP_REGISTRATION_REGISTERED,
117 /*! \brief Registration was rejected, but response was temporal */
118 SIP_REGISTRATION_REJECTED_TEMPORARY,
119 /*! \brief Registration was rejected, permanently */
120 SIP_REGISTRATION_REJECTED_PERMANENT,
121 /*! \brief Registration has been stopped */
122 SIP_REGISTRATION_STOPPED,
125 static const char *sip_outbound_registration_status_str[] = {
126 [SIP_REGISTRATION_UNREGISTERED] = "Unregistered",
127 [SIP_REGISTRATION_REGISTERED] = "Registered",
128 [SIP_REGISTRATION_REJECTED_TEMPORARY] = "Rejected",
129 [SIP_REGISTRATION_REJECTED_PERMANENT] = "Rejected",
130 [SIP_REGISTRATION_STOPPED] = "Stopped",
133 /*! \brief Outbound registration client state information (persists for lifetime of regc) */
134 struct sip_outbound_registration_client_state {
135 /*! \brief Current status of this registration */
136 enum sip_outbound_registration_status status;
137 /*! \brief Outbound registration client */
139 /*! \brief Timer entry for retrying on temporal responses */
140 pj_timer_entry timer;
141 /*! \brief Current number of retries */
142 unsigned int retries;
143 /*! \brief Maximum number of retries permitted */
144 unsigned int max_retries;
145 /*! \brief Interval at which retries should occur for temporal responses */
146 unsigned int retry_interval;
147 /*! \brief Treat authentication challenges that we cannot handle as permanent failures */
148 unsigned int auth_rejection_permanent;
149 /*! \brief Serializer for stuff and things */
150 struct ast_taskprocessor *serializer;
151 /*! \brief Configured authentication credentials */
152 struct ast_sip_auth_array outbound_auths;
153 /*! \brief Number of configured auths */
154 size_t num_outbound_auths;
155 /*! \brief Registration should be destroyed after completion of transaction */
156 unsigned int destroy:1;
159 /*! \brief Outbound registration state information (persists for lifetime that registration should exist) */
160 struct sip_outbound_registration_state {
161 /*! \brief Client state information */
162 struct sip_outbound_registration_client_state *client_state;
165 /*! \brief Outbound registration information */
166 struct sip_outbound_registration {
167 /*! \brief Sorcery object details */
168 SORCERY_OBJECT(details);
169 /*! \brief Stringfields */
170 AST_DECLARE_STRING_FIELDS(
171 /*! \brief URI for the registrar */
172 AST_STRING_FIELD(server_uri);
173 /*! \brief URI for the AOR */
174 AST_STRING_FIELD(client_uri);
175 /*! \brief Optional user for contact header */
176 AST_STRING_FIELD(contact_user);
177 /*! \brief Explicit transport to use for registration */
178 AST_STRING_FIELD(transport);
179 /*! \brief Outbound proxy to use */
180 AST_STRING_FIELD(outbound_proxy);
182 /*! \brief Requested expiration time */
183 unsigned int expiration;
184 /*! \brief Interval at which retries should occur for temporal responses */
185 unsigned int retry_interval;
186 /*! \brief Treat authentication challenges that we cannot handle as permanent failures */
187 unsigned int auth_rejection_permanent;
188 /*! \brief Maximum number of retries permitted */
189 unsigned int max_retries;
190 /*! \brief Outbound registration state */
191 struct sip_outbound_registration_state *state;
192 /*! \brief Configured authentication credentials */
193 struct ast_sip_auth_array outbound_auths;
194 /*! \brief Number of configured auths */
195 size_t num_outbound_auths;
198 /*! \brief Helper function which cancels the timer on a client */
199 static void cancel_registration(struct sip_outbound_registration_client_state *client_state)
201 if (pj_timer_heap_cancel(pjsip_endpt_get_timer_heap(ast_sip_get_pjsip_endpoint()), &client_state->timer)) {
202 /* The timer was successfully cancelled, drop the refcount of client_state */
203 ao2_ref(client_state, -1);
207 /*! \brief Callback function for registering */
208 static int handle_client_registration(void *data)
210 RAII_VAR(struct sip_outbound_registration_client_state *, client_state, data, ao2_cleanup);
211 pjsip_tx_data *tdata;
213 cancel_registration(client_state);
215 if ((client_state->status == SIP_REGISTRATION_STOPPED) ||
216 (pjsip_regc_register(client_state->client, PJ_FALSE, &tdata) != PJ_SUCCESS)) {
220 /* Due to the registration the callback may now get called, so bump the ref count */
221 ao2_ref(client_state, +1);
222 if (pjsip_regc_send(client_state->client, tdata) != PJ_SUCCESS) {
223 ao2_ref(client_state, -1);
224 pjsip_tx_data_dec_ref(tdata);
230 /*! \brief Timer callback function, used just for registrations */
231 static void sip_outbound_registration_timer_cb(pj_timer_heap_t *timer_heap, struct pj_timer_entry *entry)
233 struct sip_outbound_registration_client_state *client_state = entry->user_data;
235 ao2_ref(client_state, +1);
236 if (ast_sip_push_task(client_state->serializer, handle_client_registration, client_state)) {
237 ast_log(LOG_WARNING, "Failed to pass outbound registration to threadpool\n");
238 ao2_ref(client_state, -1);
244 /*! \brief Helper function which sets up the timer to re-register in a specific amount of time */
245 static void schedule_registration(struct sip_outbound_registration_client_state *client_state, unsigned int seconds)
247 pj_time_val delay = { .sec = seconds, };
249 cancel_registration(client_state);
251 ao2_ref(client_state, +1);
252 if (pjsip_endpt_schedule_timer(ast_sip_get_pjsip_endpoint(), &client_state->timer, &delay) != PJ_SUCCESS) {
253 ast_log(LOG_WARNING, "Failed to pass timed registration to scheduler\n");
254 ao2_ref(client_state, -1);
258 /*! \brief Callback function for unregistering (potentially) and destroying state */
259 static int handle_client_state_destruction(void *data)
261 RAII_VAR(struct sip_outbound_registration_client_state *, client_state, data, ao2_cleanup);
262 pjsip_regc_info info;
264 cancel_registration(client_state);
266 pjsip_regc_get_info(client_state->client, &info);
268 if (info.is_busy == PJ_TRUE) {
269 /* If a client transaction is in progress we defer until it is complete */
270 client_state->destroy = 1;
274 if (client_state->status != SIP_REGISTRATION_UNREGISTERED && client_state->status != SIP_REGISTRATION_REJECTED_PERMANENT) {
275 pjsip_tx_data *tdata;
277 if (pjsip_regc_unregister(client_state->client, &tdata) == PJ_SUCCESS) {
278 pjsip_regc_send(client_state->client, tdata);
282 pjsip_regc_destroy(client_state->client);
284 client_state->status = SIP_REGISTRATION_STOPPED;
285 ast_sip_auth_array_destroy(&client_state->outbound_auths);
290 /*! \brief Structure for registration response */
291 struct registration_response {
292 /*! \brief Response code for the registration attempt */
294 /*! \brief Expiration time for registration */
296 /*! \brief Retry-After value */
298 /*! \brief Outbound registration client state */
299 struct sip_outbound_registration_client_state *client_state;
300 /*! \brief The response message */
301 pjsip_rx_data *rdata;
302 /*! \brief The response transaction */
303 pjsip_transaction *tsx;
306 /*! \brief Registration response structure destructor */
307 static void registration_response_destroy(void *obj)
309 struct registration_response *response = obj;
311 if (response->rdata) {
312 pjsip_rx_data_free_cloned(response->rdata);
315 ao2_cleanup(response->client_state);
318 /* \brief Helper funtion which determines if a response code is temporal or not */
319 static int sip_outbound_registration_is_temporal(unsigned int code,
320 struct sip_outbound_registration_client_state *client_state)
322 /* Shamelessly taken from pjsua */
323 if (code == PJSIP_SC_REQUEST_TIMEOUT ||
324 code == PJSIP_SC_INTERNAL_SERVER_ERROR ||
325 code == PJSIP_SC_BAD_GATEWAY ||
326 code == PJSIP_SC_SERVICE_UNAVAILABLE ||
327 code == PJSIP_SC_SERVER_TIMEOUT ||
328 ((code == PJSIP_SC_UNAUTHORIZED ||
329 code == PJSIP_SC_PROXY_AUTHENTICATION_REQUIRED) &&
330 !client_state->auth_rejection_permanent) ||
331 PJSIP_IS_STATUS_IN_CLASS(code, 600)) {
338 /*! \brief Callback function for handling a response to a registration attempt */
339 static int handle_registration_response(void *data)
341 RAII_VAR(struct registration_response *, response, data, ao2_cleanup);
342 pjsip_regc_info info;
343 char server_uri[PJSIP_MAX_URL_SIZE], client_uri[PJSIP_MAX_URL_SIZE];
345 if (response->client_state->status == SIP_REGISTRATION_STOPPED) {
349 pjsip_regc_get_info(response->client_state->client, &info);
350 ast_copy_pj_str(server_uri, &info.server_uri, sizeof(server_uri));
351 ast_copy_pj_str(client_uri, &info.client_uri, sizeof(client_uri));
353 if (response->code == 401 || response->code == 407) {
354 pjsip_tx_data *tdata;
355 if (!ast_sip_create_request_with_auth(&response->client_state->outbound_auths,
356 response->rdata, response->tsx, &tdata)) {
357 ao2_ref(response->client_state, +1);
358 if (pjsip_regc_send(response->client_state->client, tdata) != PJ_SUCCESS) {
359 ao2_cleanup(response->client_state);
363 /* Otherwise, fall through so the failure is processed appropriately */
366 if (PJSIP_IS_STATUS_IN_CLASS(response->code, 200)) {
367 /* If the registration went fine simply reschedule registration for the future */
368 response->client_state->status = SIP_REGISTRATION_REGISTERED;
369 response->client_state->retries = 0;
370 schedule_registration(response->client_state, response->expiration - REREGISTER_BUFFER_TIME);
371 } else if (response->retry_after) {
372 /* If we have been instructed to retry after a period of time, schedule it as such */
373 response->client_state->status = SIP_REGISTRATION_REJECTED_TEMPORARY;
374 schedule_registration(response->client_state, response->retry_after);
375 ast_log(LOG_WARNING, "Temporal response '%d' received from '%s' on registration attempt to '%s', instructed to retry in '%d'\n",
376 response->code, server_uri, client_uri, response->retry_after);
377 } else if (response->client_state->retry_interval && sip_outbound_registration_is_temporal(response->code, response->client_state)) {
378 if (response->client_state->retries == response->client_state->max_retries) {
379 /* If we received enough temporal responses to exceed our maximum give up permanently */
380 response->client_state->status = SIP_REGISTRATION_REJECTED_PERMANENT;
381 ast_log(LOG_WARNING, "Maximum retries reached when attempting outbound registration to '%s' with client '%s', stopping registration attempt\n",
382 server_uri, client_uri);
384 /* On the other hand if we can still try some more do so */
385 response->client_state->status = SIP_REGISTRATION_REJECTED_TEMPORARY;
386 response->client_state->retries++;
387 schedule_registration(response->client_state, response->client_state->retry_interval);
388 ast_log(LOG_WARNING, "Temporal response '%d' received from '%s' on registration attempt to '%s', retrying in '%d' seconds\n",
389 response->code, server_uri, client_uri, response->client_state->retry_interval);
392 /* Finally if there's no hope of registering give up */
393 response->client_state->status = SIP_REGISTRATION_REJECTED_PERMANENT;
394 ast_log(LOG_WARNING, "Fatal response '%d' received from '%s' on registration attempt to '%s', stopping outbound registration\n",
395 response->code, server_uri, client_uri);
398 ast_system_publish_registry("PJSIP", client_uri, server_uri, sip_outbound_registration_status_str[response->client_state->status], NULL);
400 /* If deferred destruction is in use see if we need to destroy now */
401 if (response->client_state->destroy) {
402 handle_client_state_destruction(response->client_state);
408 /*! \brief Callback function for outbound registration client */
409 static void sip_outbound_registration_response_cb(struct pjsip_regc_cbparam *param)
411 RAII_VAR(struct sip_outbound_registration_client_state *, client_state, param->token, ao2_cleanup);
412 struct registration_response *response = ao2_alloc(sizeof(*response), registration_response_destroy);
414 response->code = param->code;
415 response->expiration = param->expiration;
416 response->client_state = client_state;
417 ao2_ref(response->client_state, +1);
420 struct pjsip_retry_after_hdr *retry_after = pjsip_msg_find_hdr(param->rdata->msg_info.msg, PJSIP_H_RETRY_AFTER, NULL);
422 response->retry_after = retry_after ? retry_after->ivalue : 0;
423 response->tsx = pjsip_rdata_get_tsx(param->rdata);
424 pjsip_rx_data_clone(param->rdata, 0, &response->rdata);
427 if (ast_sip_push_task(client_state->serializer, handle_registration_response, response)) {
428 ast_log(LOG_WARNING, "Failed to pass incoming registration response to threadpool\n");
429 ao2_cleanup(response);
433 /*! \brief Destructor function for registration state */
434 static void sip_outbound_registration_state_destroy(void *obj)
436 struct sip_outbound_registration_state *state = obj;
438 if (!state->client_state) {
442 if (state->client_state->serializer && ast_sip_push_task(state->client_state->serializer, handle_client_state_destruction, state->client_state)) {
443 ast_log(LOG_WARNING, "Failed to pass outbound registration client destruction to threadpool\n");
444 ao2_ref(state->client_state, -1);
448 /*! \brief Destructor function for client registration state */
449 static void sip_outbound_registration_client_state_destroy(void *obj)
451 struct sip_outbound_registration_client_state *client_state = obj;
453 ast_taskprocessor_unreference(client_state->serializer);
456 /*! \brief Allocator function for registration state */
457 static struct sip_outbound_registration_state *sip_outbound_registration_state_alloc(void)
459 struct sip_outbound_registration_state *state = ao2_alloc(sizeof(*state), sip_outbound_registration_state_destroy);
461 if (!state || !(state->client_state = ao2_alloc(sizeof(*state->client_state), sip_outbound_registration_client_state_destroy))) {
466 if ((pjsip_regc_create(ast_sip_get_pjsip_endpoint(), state->client_state, sip_outbound_registration_response_cb, &state->client_state->client) != PJ_SUCCESS) ||
467 !(state->client_state->serializer = ast_sip_create_serializer())) {
468 /* This is on purpose, normal operation will have it be deallocated within the serializer */
469 pjsip_regc_destroy(state->client_state->client);
470 ao2_cleanup(state->client_state);
475 state->client_state->status = SIP_REGISTRATION_UNREGISTERED;
476 state->client_state->timer.user_data = state->client_state;
477 state->client_state->timer.cb = sip_outbound_registration_timer_cb;
482 /*! \brief Destructor function for registration information */
483 static void sip_outbound_registration_destroy(void *obj)
485 struct sip_outbound_registration *registration = obj;
487 ao2_cleanup(registration->state);
488 ast_sip_auth_array_destroy(®istration->outbound_auths);
490 ast_string_field_free_memory(registration);
493 /*! \brief Allocator function for registration information */
494 static void *sip_outbound_registration_alloc(const char *name)
496 struct sip_outbound_registration *registration = ast_sorcery_generic_alloc(sizeof(*registration), sip_outbound_registration_destroy);
498 if (!registration || ast_string_field_init(registration, 256)) {
499 ao2_cleanup(registration);
506 /*! \brief Helper function which populates a pj_str_t with a contact header */
507 static int sip_dialog_create_contact(pj_pool_t *pool, pj_str_t *contact, const char *user, const pj_str_t *target, pjsip_tpselector *selector)
509 pj_str_t tmp, local_addr;
511 pjsip_sip_uri *sip_uri;
512 pjsip_transport_type_e type = PJSIP_TRANSPORT_UNSPECIFIED;
515 pj_strdup_with_null(pool, &tmp, target);
517 if (!(uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0)) ||
518 (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))) {
522 sip_uri = pjsip_uri_get_uri(uri);
524 if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri)) {
525 type = PJSIP_TRANSPORT_TLS;
526 } else if (!sip_uri->transport_param.slen) {
527 type = PJSIP_TRANSPORT_UDP;
529 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
532 if (type == PJSIP_TRANSPORT_UNSPECIFIED) {
536 if (pj_strchr(&sip_uri->host, ':')) {
537 type = (pjsip_transport_type_e)(((int)type) + PJSIP_TRANSPORT_IPV6);
540 if (pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()), pool, type, selector,
541 &local_addr, &local_port) != PJ_SUCCESS) {
545 if (!pj_strchr(&sip_uri->host, ':') && pj_strchr(&local_addr, ':')) {
546 type = (pjsip_transport_type_e)(((int)type) + PJSIP_TRANSPORT_IPV6);
549 contact->ptr = pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
550 contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
551 "<%s:%s@%s%.*s%s:%d%s%s>",
552 (pjsip_transport_get_flag_from_type(type) & PJSIP_TRANSPORT_SECURE) ? "sips" : "sip",
554 (type & PJSIP_TRANSPORT_IPV6) ? "[" : "",
555 (int)local_addr.slen,
557 (type & PJSIP_TRANSPORT_IPV6) ? "]" : "",
559 (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? ";transport=" : "",
560 (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? pjsip_transport_get_type_name(type) : "");
567 * \brief Check if a registration can be reused
569 * This checks if the existing outbound registration's configuration differs from a newly-applied
570 * outbound registration to see if the applied one.
572 * \param existing The pre-existing outbound registration
573 * \param applied The newly-created registration
575 static int can_reuse_registration(struct sip_outbound_registration *existing, struct sip_outbound_registration *applied)
579 if (strcmp(existing->server_uri, applied->server_uri) || strcmp(existing->client_uri, applied->client_uri) ||
580 strcmp(existing->transport, applied->transport) || strcmp(existing->contact_user, applied->contact_user) ||
581 strcmp(existing->outbound_proxy, applied->outbound_proxy) || existing->num_outbound_auths != applied->num_outbound_auths ||
582 existing->auth_rejection_permanent != applied->auth_rejection_permanent) {
586 for (i = 0; i < existing->num_outbound_auths; ++i) {
587 if (strcmp(existing->outbound_auths.names[i], applied->outbound_auths.names[i])) {
595 /*! \brief Helper function that allocates a pjsip registration client and configures it */
596 static int sip_outbound_registration_regc_alloc(void *data)
598 struct sip_outbound_registration *registration = data;
599 pj_str_t server_uri, client_uri, contact_uri;
600 pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
602 if (!ast_strlen_zero(registration->transport)) {
603 RAII_VAR(struct ast_sip_transport *, transport, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport", registration->transport), ao2_cleanup);
605 if (!transport || !transport->state) {
609 if (transport->state->transport) {
610 selector.type = PJSIP_TPSELECTOR_TRANSPORT;
611 selector.u.transport = transport->state->transport;
612 } else if (transport->state->factory) {
613 selector.type = PJSIP_TPSELECTOR_LISTENER;
614 selector.u.listener = transport->state->factory;
620 pjsip_regc_set_transport(registration->state->client_state->client, &selector);
622 if (!ast_strlen_zero(registration->outbound_proxy)) {
623 pjsip_route_hdr route_set, *route;
624 static const pj_str_t ROUTE_HNAME = { "Route", 5 };
627 pj_list_init(&route_set);
629 pj_strdup2_with_null(pjsip_regc_get_pool(registration->state->client_state->client), &tmp, registration->outbound_proxy);
630 if (!(route = pjsip_parse_hdr(pjsip_regc_get_pool(registration->state->client_state->client), &ROUTE_HNAME, tmp.ptr, tmp.slen, NULL))) {
633 pj_list_push_back(&route_set, route);
635 pjsip_regc_set_route_set(registration->state->client_state->client, &route_set);
638 pj_cstr(&server_uri, registration->server_uri);
640 if (sip_dialog_create_contact(pjsip_regc_get_pool(registration->state->client_state->client), &contact_uri, S_OR(registration->contact_user, "s"), &server_uri, &selector)) {
644 pj_cstr(&client_uri, registration->client_uri);
646 if (pjsip_regc_init(registration->state->client_state->client, &server_uri, &client_uri, &client_uri, 1, &contact_uri, registration->expiration) != PJ_SUCCESS) {
653 /*! \brief Apply function which finds or allocates a state structure */
654 static int sip_outbound_registration_apply(const struct ast_sorcery *sorcery, void *obj)
656 RAII_VAR(struct sip_outbound_registration *, existing, ast_sorcery_retrieve_by_id(sorcery, "registration", ast_sorcery_object_get_id(obj)), ao2_cleanup);
657 struct sip_outbound_registration *applied = obj;
660 /* If no existing registration exists we can just start fresh easily */
661 applied->state = sip_outbound_registration_state_alloc();
663 /* If there is an existing registration things are more complicated, we can immediately reuse this state if most stuff remains unchanged */
664 if (can_reuse_registration(existing, applied)) {
665 applied->state = existing->state;
666 ao2_ref(applied->state, +1);
669 applied->state = sip_outbound_registration_state_alloc();
672 if (!applied->state) {
676 return ast_sip_push_task_synchronous(NULL, sip_outbound_registration_regc_alloc, applied);
679 /*! \brief Helper function which performs a single registration */
680 static int sip_outbound_registration_perform(void *data)
682 RAII_VAR(struct sip_outbound_registration *, registration, data, ao2_cleanup);
685 /* Just in case the client state is being reused for this registration, free the auth information */
686 ast_sip_auth_array_destroy(®istration->state->client_state->outbound_auths);
688 registration->state->client_state->outbound_auths.names = ast_calloc(registration->outbound_auths.num, sizeof(char *));
689 for (i = 0; i < registration->outbound_auths.num; ++i) {
690 registration->state->client_state->outbound_auths.names[i] = ast_strdup(registration->outbound_auths.names[i]);
692 registration->state->client_state->outbound_auths.num = registration->outbound_auths.num;
693 registration->state->client_state->retry_interval = registration->retry_interval;
694 registration->state->client_state->max_retries = registration->max_retries;
695 registration->state->client_state->retries = 0;
697 pjsip_regc_update_expires(registration->state->client_state->client, registration->expiration);
699 schedule_registration(registration->state->client_state, (ast_random() % 10) + 1);
704 /*! \brief Helper function which performs all registrations */
705 static void sip_outbound_registration_perform_all(void)
707 RAII_VAR(struct ao2_container *, registrations, ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "registration", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL), ao2_cleanup);
708 struct ao2_iterator i;
709 struct sip_outbound_registration *registration;
711 if (!registrations) {
715 i = ao2_iterator_init(registrations, 0);
716 while ((registration = ao2_iterator_next(&i))) {
717 if (ast_sip_push_task(registration->state->client_state->serializer, sip_outbound_registration_perform, registration)) {
718 ast_log(LOG_ERROR, "Failed to perform outbound registration on '%s'\n", ast_sorcery_object_get_id(registration));
719 ao2_ref(registration, -1);
722 ao2_iterator_destroy(&i);
725 static int outbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
727 struct sip_outbound_registration *registration = obj;
729 return ast_sip_auth_array_init(®istration->outbound_auths, var->value);
732 static struct sip_outbound_registration *retrieve_registration(const char *registration_name)
734 return ast_sorcery_retrieve_by_id(
735 ast_sip_get_sorcery(),
740 static int unregister_task(void *obj)
742 RAII_VAR(struct sip_outbound_registration*, registration, obj, ao2_cleanup);
743 struct pjsip_regc *client = registration->state->client_state->client;
744 pjsip_tx_data *tdata;
746 if (pjsip_regc_unregister(client, &tdata) != PJ_SUCCESS) {
750 ao2_ref(registration->state->client_state, +1);
751 if (pjsip_regc_send(client, tdata) != PJ_SUCCESS) {
752 ao2_cleanup(registration->state->client_state);
758 static int queue_unregister(struct sip_outbound_registration *registration)
760 ao2_ref(registration, +1);
761 if (ast_sip_push_task(registration->state->client_state->serializer, unregister_task, registration)) {
762 ao2_cleanup(registration);
768 static char *cli_complete_registration(const char *line, const char *word,
774 struct sip_outbound_registration *registration;
775 RAII_VAR(struct ao2_container *, registrations, NULL, ao2_cleanup);
776 struct ao2_iterator i;
782 wordlen = strlen(word);
783 registrations = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "registration",
784 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
785 if (!registrations) {
789 i = ao2_iterator_init(registrations, 0);
790 while ((registration = ao2_iterator_next(&i))) {
791 const char *name = ast_sorcery_object_get_id(registration);
792 if (!strncasecmp(word, name, wordlen) && ++which > state) {
793 result = ast_strdup(name);
796 ao2_cleanup(registration);
801 ao2_iterator_destroy(&i);
805 static char *cli_unregister(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
807 RAII_VAR(struct sip_outbound_registration *, registration, NULL, ao2_cleanup);
808 const char *registration_name;
812 e->command = "pjsip send unregister";
814 "Usage: pjsip send unregister <registration>\n"
815 " Send a SIP REGISTER request to the specified outbound "
816 "registration with an expiration of 0. This will cause the contact "
817 "added by this registration to be removed on the remote system.\n";
820 return cli_complete_registration(a->line, a->word, a->pos, a->n);
824 return CLI_SHOWUSAGE;
827 registration_name = a->argv[3];
829 registration = retrieve_registration(registration_name);
831 ast_cli(a->fd, "Unable to retrieve registration %s\n", registration_name);
835 if (queue_unregister(registration)) {
836 ast_cli(a->fd, "Failed to queue unregistration");
843 static int ami_unregister(struct mansession *s, const struct message *m)
845 const char *registration_name = astman_get_header(m, "Registration");
846 RAII_VAR(struct sip_outbound_registration *, registration, NULL, ao2_cleanup);
848 if (ast_strlen_zero(registration_name)) {
849 astman_send_error(s, m, "Registration parameter missing.");
853 registration = retrieve_registration(registration_name);
855 astman_send_error(s, m, "Unable to retrieve registration entry\n");
860 if (queue_unregister(registration)) {
861 astman_send_ack(s, m, "Failed to queue unregistration");
865 astman_send_ack(s, m, "Unregistration sent");
869 static struct ast_cli_entry cli_outbound_registration[] = {
870 AST_CLI_DEFINE(cli_unregister, "Send a REGISTER request to an outbound registration target with a expiration of 0")
873 static int load_module(void)
875 ast_sorcery_apply_default(ast_sip_get_sorcery(), "registration", "config", "pjsip.conf,criteria=type=registration");
877 if (ast_sorcery_object_register(ast_sip_get_sorcery(), "registration", sip_outbound_registration_alloc, NULL, sip_outbound_registration_apply)) {
878 return AST_MODULE_LOAD_DECLINE;
881 ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "type", "", OPT_NOOP_T, 0, 0);
882 ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "server_uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, server_uri));
883 ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "client_uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, client_uri));
884 ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "contact_user", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, contact_user));
885 ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, transport));
886 ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, outbound_proxy));
887 ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "expiration", "3600", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, expiration));
888 ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "retry_interval", "60", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, retry_interval));
889 ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "max_retries", "10", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, max_retries));
890 ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "auth_rejection_permanent", "yes", OPT_BOOL_T, 1, FLDSET(struct sip_outbound_registration, auth_rejection_permanent));
891 ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "registration", "outbound_auth", "", outbound_auth_handler, NULL, 0, 0);
892 ast_sorcery_reload_object(ast_sip_get_sorcery(), "registration");
893 sip_outbound_registration_perform_all();
895 ast_cli_register_multiple(cli_outbound_registration, ARRAY_LEN(cli_outbound_registration));
896 ast_manager_register_xml("PJSIPUnregister", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, ami_unregister);
897 return AST_MODULE_LOAD_SUCCESS;
900 static int reload_module(void)
902 ast_sorcery_reload_object(ast_sip_get_sorcery(), "registration");
903 sip_outbound_registration_perform_all();
907 static int unload_module(void)
909 ast_cli_unregister_multiple(cli_outbound_registration, ARRAY_LEN(cli_outbound_registration));
910 ast_manager_unregister("PJSIPUnregister");
914 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Outbound Registration Support",
916 .reload = reload_module,
917 .unload = unload_module,
918 .load_pri = AST_MODPRI_APP_DEPEND,