Add AMI registration events for PJSIP outbound registration attempts
[asterisk/asterisk.git] / res / res_pjsip_outbound_registration.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*** MODULEINFO
20         <depend>pjproject</depend>
21         <depend>res_pjsip</depend>
22         <support_level>core</support_level>
23  ***/
24
25 #include "asterisk.h"
26
27 #include <pjsip.h>
28 #include <pjsip_ua.h>
29
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"
35
36 /*** DOCUMENTATION
37         <configInfo name="res_pjsip_outbound_registration" language="en_US">
38                 <synopsis>SIP resource for outbound registrations</synopsis>
39                 <description><para>
40                         <emphasis>Outbound Registration</emphasis>
41                         </para>
42                         <para>This module allows <literal>res_pjsip</literal> to register to other SIP servers.</para>
43                 </description>
44                 <configFile name="pjsip.conf">
45                         <configObject name="registration">
46                                 <synopsis>The configuration for outbound registration</synopsis>
47                                 <description><para>
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>.
51                                 </para></description>
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>
57                                 </configOption>
58                                 <configOption name="client_uri">
59                                         <synopsis>Client SIP URI used when attemping outbound registration</synopsis>
60                                 </configOption>
61                                 <configOption name="contact_user">
62                                         <synopsis>Contact User to use in request</synopsis>
63                                 </configOption>
64                                 <configOption name="expiration" default="3600">
65                                         <synopsis>Expiration time for registrations in seconds</synopsis>
66                                 </configOption>
67                                 <configOption name="max_retries" default="10">
68                                         <synopsis>Maximum number of registration attempts.</synopsis>
69                                 </configOption>
70                                 <configOption name="outbound_auth" default="">
71                                         <synopsis>Authentication object to be used for outbound registrations.</synopsis>
72                                 </configOption>
73                                 <configOption name="outbound_proxy" default="">
74                                         <synopsis>Outbound Proxy used to send registrations</synopsis>
75                                 </configOption>
76                                 <configOption name="retry_interval" default="60">
77                                         <synopsis>Interval in seconds between retries if outbound registration is unsuccessful</synopsis>
78                                 </configOption>
79                                 <configOption name="server_uri">
80                                         <synopsis>SIP URI of the server to register against</synopsis>
81                                 </configOption>
82                                 <configOption name="transport">
83                                         <synopsis>Transport used for outbound authentication</synopsis>
84                                         <description>
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>
87                                         </description>
88                                 </configOption>
89                                 <configOption name="type">
90                                         <synopsis>Must be of type 'registration'.</synopsis>
91                                 </configOption>
92                         </configObject>
93                 </configFile>
94         </configInfo>
95         <manager name="PJSIPUnregister" language="en_US">
96                 <synopsis>
97                         Unregister an outbound registration.
98                 </synopsis>
99                 <syntax>
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>
103                         </parameter>
104                 </syntax>
105         </manager>
106  ***/
107
108 /*! \brief Amount of buffer time (in seconds) before expiration that we re-register at */
109 #define REREGISTER_BUFFER_TIME 10
110
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,
123 };
124
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",
131 };
132
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 */
138         pjsip_regc *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;
157 };
158
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;
163 };
164
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);
181         );
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;
196 };
197
198 /*! \brief Helper function which cancels the timer on a client */
199 static void cancel_registration(struct sip_outbound_registration_client_state *client_state)
200 {
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);
204         }
205 }
206
207 /*! \brief Callback function for registering */
208 static int handle_client_registration(void *data)
209 {
210         RAII_VAR(struct sip_outbound_registration_client_state *, client_state, data, ao2_cleanup);
211         pjsip_tx_data *tdata;
212
213         cancel_registration(client_state);
214
215         if ((client_state->status == SIP_REGISTRATION_STOPPED) ||
216                 (pjsip_regc_register(client_state->client, PJ_FALSE, &tdata) != PJ_SUCCESS)) {
217                 return 0;
218         }
219
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);
225         }
226
227         return 0;
228 }
229
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)
232 {
233         struct sip_outbound_registration_client_state *client_state = entry->user_data;
234
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);
239         }
240
241         entry->id = 0;
242 }
243
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)
246 {
247         pj_time_val delay = { .sec = seconds, };
248
249         cancel_registration(client_state);
250
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);
255         }
256 }
257
258 /*! \brief Callback function for unregistering (potentially) and destroying state */
259 static int handle_client_state_destruction(void *data)
260 {
261         RAII_VAR(struct sip_outbound_registration_client_state *, client_state, data, ao2_cleanup);
262         pjsip_regc_info info;
263
264         cancel_registration(client_state);
265
266         pjsip_regc_get_info(client_state->client, &info);
267
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;
271                 return 0;
272         }
273
274         if (client_state->status != SIP_REGISTRATION_UNREGISTERED && client_state->status != SIP_REGISTRATION_REJECTED_PERMANENT) {
275                 pjsip_tx_data *tdata;
276
277                 if (pjsip_regc_unregister(client_state->client, &tdata) == PJ_SUCCESS) {
278                         pjsip_regc_send(client_state->client, tdata);
279                 }
280         }
281
282         pjsip_regc_destroy(client_state->client);
283
284         client_state->status = SIP_REGISTRATION_STOPPED;
285         ast_sip_auth_array_destroy(&client_state->outbound_auths);
286
287         return 0;
288 }
289
290 /*! \brief Structure for registration response */
291 struct registration_response {
292         /*! \brief Response code for the registration attempt */
293         int code;
294         /*! \brief Expiration time for registration */
295         int expiration;
296         /*! \brief Retry-After value */
297         int retry_after;
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;
304 };
305
306 /*! \brief Registration response structure destructor */
307 static void registration_response_destroy(void *obj)
308 {
309         struct registration_response *response = obj;
310
311         if (response->rdata) {
312                 pjsip_rx_data_free_cloned(response->rdata);
313         }
314
315         ao2_cleanup(response->client_state);
316 }
317
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)
321 {
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)) {
332                 return 1;
333         } else {
334                 return 0;
335         }
336 }
337
338 /*! \brief Callback function for handling a response to a registration attempt */
339 static int handle_registration_response(void *data)
340 {
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];
344
345         if (response->client_state->status == SIP_REGISTRATION_STOPPED) {
346                 return 0;
347         }
348
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));
352
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);
360                         }
361                         return 0;
362                 }
363                 /* Otherwise, fall through so the failure is processed appropriately */
364         }
365
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);
383                 } else {
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);
390                 }
391         } else {
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);
396         }
397
398         ast_system_publish_registry("PJSIP", client_uri, server_uri, sip_outbound_registration_status_str[response->client_state->status], NULL);
399
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);
403         }
404
405         return 0;
406 }
407
408 /*! \brief Callback function for outbound registration client */
409 static void sip_outbound_registration_response_cb(struct pjsip_regc_cbparam *param)
410 {
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);
413
414         response->code = param->code;
415         response->expiration = param->expiration;
416         response->client_state = client_state;
417         ao2_ref(response->client_state, +1);
418
419         if (param->rdata) {
420                 struct pjsip_retry_after_hdr *retry_after = pjsip_msg_find_hdr(param->rdata->msg_info.msg, PJSIP_H_RETRY_AFTER, NULL);
421
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);
425         }
426
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);
430         }
431 }
432
433 /*! \brief Destructor function for registration state */
434 static void sip_outbound_registration_state_destroy(void *obj)
435 {
436         struct sip_outbound_registration_state *state = obj;
437
438         if (!state->client_state) {
439                 return;
440         }
441
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);
445         }
446 }
447
448 /*! \brief Destructor function for client registration state */
449 static void sip_outbound_registration_client_state_destroy(void *obj)
450 {
451         struct sip_outbound_registration_client_state *client_state = obj;
452
453         ast_taskprocessor_unreference(client_state->serializer);
454 }
455
456 /*! \brief Allocator function for registration state */
457 static struct sip_outbound_registration_state *sip_outbound_registration_state_alloc(void)
458 {
459         struct sip_outbound_registration_state *state = ao2_alloc(sizeof(*state), sip_outbound_registration_state_destroy);
460
461         if (!state || !(state->client_state = ao2_alloc(sizeof(*state->client_state), sip_outbound_registration_client_state_destroy))) {
462                 ao2_cleanup(state);
463                 return NULL;
464         }
465
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);
471                 ao2_cleanup(state);
472                 return NULL;
473         }
474
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;
478
479         return state;
480 }
481
482 /*! \brief Destructor function for registration information */
483 static void sip_outbound_registration_destroy(void *obj)
484 {
485         struct sip_outbound_registration *registration = obj;
486
487         ao2_cleanup(registration->state);
488         ast_sip_auth_array_destroy(&registration->outbound_auths);
489
490         ast_string_field_free_memory(registration);
491 }
492
493 /*! \brief Allocator function for registration information */
494 static void *sip_outbound_registration_alloc(const char *name)
495 {
496         struct sip_outbound_registration *registration = ast_sorcery_generic_alloc(sizeof(*registration), sip_outbound_registration_destroy);
497
498         if (!registration || ast_string_field_init(registration, 256)) {
499                 ao2_cleanup(registration);
500                 return NULL;
501         }
502
503         return registration;
504 }
505
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)
508 {
509         pj_str_t tmp, local_addr;
510         pjsip_uri *uri;
511         pjsip_sip_uri *sip_uri;
512         pjsip_transport_type_e type = PJSIP_TRANSPORT_UNSPECIFIED;
513         int local_port;
514
515         pj_strdup_with_null(pool, &tmp, target);
516
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))) {
519                 return -1;
520         }
521
522         sip_uri = pjsip_uri_get_uri(uri);
523
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;
528         } else {
529                 type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
530         }
531
532         if (type == PJSIP_TRANSPORT_UNSPECIFIED) {
533                 return -1;
534         }
535
536         if (pj_strchr(&sip_uri->host, ':')) {
537                 type = (pjsip_transport_type_e)(((int)type) + PJSIP_TRANSPORT_IPV6);
538         }
539
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) {
542                 return -1;
543         }
544
545         if (!pj_strchr(&sip_uri->host, ':') && pj_strchr(&local_addr, ':')) {
546                 type = (pjsip_transport_type_e)(((int)type) + PJSIP_TRANSPORT_IPV6);
547         }
548
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",
553                                       user,
554                                       (type & PJSIP_TRANSPORT_IPV6) ? "[" : "",
555                                       (int)local_addr.slen,
556                                       local_addr.ptr,
557                                       (type & PJSIP_TRANSPORT_IPV6) ? "]" : "",
558                                       local_port,
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) : "");
561
562         return 0;
563 }
564
565 /*!
566  * \internal
567  * \brief Check if a registration can be reused
568  *
569  * This checks if the existing outbound registration's configuration differs from a newly-applied
570  * outbound registration to see if the applied one.
571  *
572  * \param existing The pre-existing outbound registration
573  * \param applied The newly-created registration
574  */
575 static int can_reuse_registration(struct sip_outbound_registration *existing, struct sip_outbound_registration *applied)
576 {
577         int i;
578
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) {
583                 return 0;
584         }
585
586         for (i = 0; i < existing->num_outbound_auths; ++i) {
587                 if (strcmp(existing->outbound_auths.names[i], applied->outbound_auths.names[i])) {
588                         return 0;
589                 }
590         }
591
592         return 1;
593 }
594
595 /*! \brief Helper function that allocates a pjsip registration client and configures it */
596 static int sip_outbound_registration_regc_alloc(void *data)
597 {
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, };
601
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);
604
605                 if (!transport || !transport->state) {
606                         return -1;
607                 }
608
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;
615                 } else {
616                         return -1;
617                 }
618         }
619
620         pjsip_regc_set_transport(registration->state->client_state->client, &selector);
621
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 };
625                 pj_str_t tmp;
626
627                 pj_list_init(&route_set);
628
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))) {
631                         return -1;
632                 }
633                 pj_list_push_back(&route_set, route);
634
635                 pjsip_regc_set_route_set(registration->state->client_state->client, &route_set);
636         }
637
638         pj_cstr(&server_uri, registration->server_uri);
639
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)) {
641                 return -1;
642         }
643
644         pj_cstr(&client_uri, registration->client_uri);
645
646         if (pjsip_regc_init(registration->state->client_state->client, &server_uri, &client_uri, &client_uri, 1, &contact_uri, registration->expiration) != PJ_SUCCESS) {
647                 return -1;
648         }
649
650         return 0;
651 }
652
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)
655 {
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;
658
659         if (!existing) {
660                 /* If no existing registration exists we can just start fresh easily */
661                 applied->state = sip_outbound_registration_state_alloc();
662         } else {
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);
667                         return 0;
668                 }
669                 applied->state = sip_outbound_registration_state_alloc();
670         }
671
672         if (!applied->state) {
673                 return -1;
674         }
675
676         return ast_sip_push_task_synchronous(NULL, sip_outbound_registration_regc_alloc, applied);
677 }
678
679 /*! \brief Helper function which performs a single registration */
680 static int sip_outbound_registration_perform(void *data)
681 {
682         RAII_VAR(struct sip_outbound_registration *, registration, data, ao2_cleanup);
683         size_t i;
684
685         /* Just in case the client state is being reused for this registration, free the auth information */
686         ast_sip_auth_array_destroy(&registration->state->client_state->outbound_auths);
687
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]);
691         }
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;
696
697         pjsip_regc_update_expires(registration->state->client_state->client, registration->expiration);
698
699         schedule_registration(registration->state->client_state, (ast_random() % 10) + 1);
700
701         return 0;
702 }
703
704 /*! \brief Helper function which performs all registrations */
705 static void sip_outbound_registration_perform_all(void)
706 {
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;
710
711         if (!registrations) {
712                 return;
713         }
714
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);
720                 }
721         }
722         ao2_iterator_destroy(&i);
723 }
724
725 static int outbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
726 {
727         struct sip_outbound_registration *registration = obj;
728
729         return ast_sip_auth_array_init(&registration->outbound_auths, var->value);
730 }
731
732 static struct sip_outbound_registration *retrieve_registration(const char *registration_name)
733 {
734         return ast_sorcery_retrieve_by_id(
735                 ast_sip_get_sorcery(),
736                 "registration",
737                 registration_name);
738 }
739
740 static int unregister_task(void *obj)
741 {
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;
745
746         if (pjsip_regc_unregister(client, &tdata) != PJ_SUCCESS) {
747                 return 0;
748         }
749
750         ao2_ref(registration->state->client_state, +1);
751         if (pjsip_regc_send(client, tdata) != PJ_SUCCESS) {
752                 ao2_cleanup(registration->state->client_state);
753         }
754
755         return 0;
756 }
757
758 static int queue_unregister(struct sip_outbound_registration *registration)
759 {
760         ao2_ref(registration, +1);
761         if (ast_sip_push_task(registration->state->client_state->serializer, unregister_task, registration)) {
762                 ao2_cleanup(registration);
763                 return -1;
764         }
765         return 0;
766 }
767
768 static char *cli_complete_registration(const char *line, const char *word,
769 int pos, int state)
770 {
771         char *result = NULL;
772         int wordlen;
773         int which = 0;
774         struct sip_outbound_registration *registration;
775         RAII_VAR(struct ao2_container *, registrations, NULL, ao2_cleanup);
776         struct ao2_iterator i;
777
778         if (pos != 3) {
779                 return NULL;
780         }
781
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) {
786                 return NULL;
787         }
788
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);
794                 }
795
796                 ao2_cleanup(registration);
797                 if (result) {
798                         break;
799                 }
800         }
801         ao2_iterator_destroy(&i);
802         return result;
803 }
804
805 static char *cli_unregister(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
806 {
807         RAII_VAR(struct sip_outbound_registration *, registration, NULL, ao2_cleanup);
808         const char *registration_name;
809
810         switch (cmd) {
811         case CLI_INIT:
812                 e->command = "pjsip send unregister";
813                 e->usage =
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";
818                 return NULL;
819         case CLI_GENERATE:
820                 return cli_complete_registration(a->line, a->word, a->pos, a->n);
821         }
822
823         if (a->argc != 4) {
824                 return CLI_SHOWUSAGE;
825         }
826
827         registration_name = a->argv[3];
828
829         registration = retrieve_registration(registration_name);
830         if (!registration) {
831                 ast_cli(a->fd, "Unable to retrieve registration %s\n", registration_name);
832                 return CLI_FAILURE;
833         }
834
835         if (queue_unregister(registration)) {
836                 ast_cli(a->fd, "Failed to queue unregistration");
837                 return 0;
838         }
839
840         return CLI_SUCCESS;
841 }
842
843 static int ami_unregister(struct mansession *s, const struct message *m)
844 {
845         const char *registration_name = astman_get_header(m, "Registration");
846         RAII_VAR(struct sip_outbound_registration *, registration, NULL, ao2_cleanup);
847
848         if (ast_strlen_zero(registration_name)) {
849                 astman_send_error(s, m, "Registration parameter missing.");
850                 return 0;
851         }
852
853         registration = retrieve_registration(registration_name);
854         if (!registration) {
855                 astman_send_error(s, m, "Unable to retrieve registration entry\n");
856                 return 0;
857         }
858
859
860         if (queue_unregister(registration)) {
861                 astman_send_ack(s, m, "Failed to queue unregistration");
862                 return 0;
863         }
864
865         astman_send_ack(s, m, "Unregistration sent");
866         return 0;
867 }
868
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")
871 };
872
873 static int load_module(void)
874 {
875         ast_sorcery_apply_default(ast_sip_get_sorcery(), "registration", "config", "pjsip.conf,criteria=type=registration");
876
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;
879         }
880
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();
894
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;
898 }
899
900 static int reload_module(void)
901 {
902         ast_sorcery_reload_object(ast_sip_get_sorcery(), "registration");
903         sip_outbound_registration_perform_all();
904         return 0;
905 }
906
907 static int unload_module(void)
908 {
909         ast_cli_unregister_multiple(cli_outbound_registration, ARRAY_LEN(cli_outbound_registration));
910         ast_manager_unregister("PJSIPUnregister");
911         return 0;
912 }
913
914 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Outbound Registration Support",
915                 .load = load_module,
916                 .reload = reload_module,
917                 .unload = unload_module,
918                 .load_pri = AST_MODPRI_APP_DEPEND,
919                );