648deee59615af24bcf3bb5b0e17f6dba49d3edf
[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         <use type="module">res_statsd</use>
23         <support_level>core</support_level>
24  ***/
25
26 #include "asterisk.h"
27
28 #include <pjsip.h>
29 #include <pjsip_ua.h>
30
31 #include "asterisk/res_pjsip.h"
32 #include "asterisk/res_pjsip_cli.h"
33 #include "asterisk/module.h"
34 #include "asterisk/taskprocessor.h"
35 #include "asterisk/cli.h"
36 #include "asterisk/stasis_system.h"
37 #include "asterisk/threadstorage.h"
38 #include "asterisk/threadpool.h"
39 #include "asterisk/statsd.h"
40 #include "res_pjsip/include/res_pjsip_private.h"
41 #include "asterisk/vector.h"
42 #include "asterisk/pbx.h"
43
44 /*** DOCUMENTATION
45         <configInfo name="res_pjsip_outbound_registration" language="en_US">
46                 <synopsis>SIP resource for outbound registrations</synopsis>
47                 <description><para>
48                         <emphasis>Outbound Registration</emphasis>
49                         </para>
50                         <para>This module allows <literal>res_pjsip</literal> to register to other SIP servers.</para>
51                 </description>
52                 <configFile name="pjsip.conf">
53                         <configObject name="registration">
54                                 <synopsis>The configuration for outbound registration</synopsis>
55                                 <description><para>
56                                         Registration is <emphasis>COMPLETELY</emphasis> separate from the rest of
57                                         <literal>pjsip.conf</literal>. A minimal configuration consists of
58                                         setting a <literal>server_uri</literal> and a <literal>client_uri</literal>.
59                                 </para></description>
60                                 <configOption name="auth_rejection_permanent" default="yes">
61                                         <synopsis>Determines whether failed authentication challenges are treated
62                                         as permanent failures.</synopsis>
63                                         <description><para>If this option is enabled and an authentication challenge fails,
64                                         registration will not be attempted again until the configuration is reloaded.</para></description>
65                                 </configOption>
66                                 <configOption name="client_uri">
67                                         <synopsis>Client SIP URI used when attemping outbound registration</synopsis>
68                                         <description><para>
69                                                 This is the address-of-record for the outbound registration (i.e. the URI in
70                                                 the To header of the REGISTER).</para>
71                                                 <para>For registration with an ITSP, the client SIP URI may need to consist of
72                                                 an account name or number and the provider's hostname for their registrar, e.g.
73                                                 client_uri=1234567890@example.com. This may differ between providers.</para>
74                                                 <para>For registration to generic registrars, the client SIP URI will depend
75                                                 on networking specifics and configuration of the registrar.
76                                         </para></description>
77                                 </configOption>
78                                 <configOption name="contact_user">
79                                         <synopsis>Contact User to use in request</synopsis>
80                                 </configOption>
81                                 <configOption name="contact_header_params">
82                                         <synopsis>Header parameters to place in the Contact header</synopsis>
83                                 </configOption>
84                                 <configOption name="expiration" default="3600">
85                                         <synopsis>Expiration time for registrations in seconds</synopsis>
86                                 </configOption>
87                                 <configOption name="max_retries" default="10">
88                                         <synopsis>Maximum number of registration attempts.</synopsis>
89                                 </configOption>
90                                 <configOption name="outbound_auth" default="">
91                                         <synopsis>Authentication object(s) to be used for outbound registrations.</synopsis>
92                                         <description><para>
93                                                 This is a comma-delimited list of <replaceable>auth</replaceable>
94                                                 sections defined in <filename>pjsip.conf</filename> used to respond
95                                                 to outbound authentication challenges.</para>
96                                                 <note><para>
97                                                 Using the same auth section for inbound and outbound
98                                                 authentication is not recommended.  There is a difference in
99                                                 meaning for an empty realm setting between inbound and outbound
100                                                 authentication uses.  See the auth realm description for details.
101                                                 </para></note>
102                                         </description>
103                                 </configOption>
104                                 <configOption name="outbound_proxy" default="">
105                                         <synopsis>Full SIP URI of the outbound proxy used to send registrations</synopsis>
106                                 </configOption>
107                                 <configOption name="retry_interval" default="60">
108                                         <synopsis>Interval in seconds between retries if outbound registration is unsuccessful</synopsis>
109                                 </configOption>
110                                 <configOption name="forbidden_retry_interval" default="0">
111                                         <synopsis>Interval used when receiving a 403 Forbidden response.</synopsis>
112                                         <description><para>
113                                                 If a 403 Forbidden is received, chan_pjsip will wait
114                                                 <replaceable>forbidden_retry_interval</replaceable> seconds before
115                                                 attempting registration again. If 0 is specified, chan_pjsip will not
116                                                 retry after receiving a 403 Forbidden response. Setting this to a non-zero
117                                                 value goes against a "SHOULD NOT" in RFC3261, but can be used to work around
118                                                 buggy registrars.
119                                         </para></description>
120                                 </configOption>
121                                 <configOption name="fatal_retry_interval" default="0">
122                                         <synopsis>Interval used when receiving a Fatal response.</synopsis>
123                                         <description><para>
124                                                 If a fatal response is received, chan_pjsip will wait
125                                                 <replaceable>fatal_retry_interval</replaceable> seconds before
126                                                 attempting registration again. If 0 is specified, chan_pjsip will not
127                                                 retry after receiving a fatal (non-temporary 4xx, 5xx, 6xx) response.
128                                                 Setting this to a non-zero value may go against a "SHOULD NOT" in RFC3261,
129                                                 but can be used to work around buggy registrars.</para>
130                                                 <note><para>if also set the <replaceable>forbidden_retry_interval</replaceable>
131                                                 takes precedence over this one when a 403 is received.
132                                                 Also, if <replaceable>auth_rejection_permanent</replaceable> equals 'yes' then
133                                                 a 401 and 407 become subject to this retry interval.</para></note>
134                                         </description>
135                                 </configOption>
136                                 <configOption name="server_uri">
137                                         <synopsis>SIP URI of the server to register against</synopsis>
138                                         <description><para>
139                                                 This is the URI at which to find the registrar to send the outbound REGISTER. This URI
140                                                 is used as the request URI of the outbound REGISTER request from Asterisk.</para>
141                                                 <para>For registration with an ITSP, the setting may often be just the domain of
142                                                 the registrar, e.g. sip:sip.example.com.
143                                         </para></description>
144                                 </configOption>
145                                 <configOption name="transport">
146                                         <synopsis>Transport used for outbound authentication</synopsis>
147                                         <description>
148                                                 <note><para>A <replaceable>transport</replaceable> configured in
149                                                 <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>
150                                         </description>
151                                 </configOption>
152                                 <configOption name="line">
153                                         <synopsis>Whether to add a 'line' parameter to the Contact for inbound call matching</synopsis>
154                                         <description><para>
155                                                 When enabled this option will cause a 'line' parameter to be added to the Contact
156                                                 header placed into the outgoing registration request. If the remote server sends a call
157                                                 this line parameter will be used to establish a relationship to the outbound registration,
158                                                 ultimately causing the configured endpoint to be used.
159                                         </para></description>
160                                 </configOption>
161                                 <configOption name="endpoint">
162                                         <synopsis>Endpoint to use for incoming related calls</synopsis>
163                                         <description><para>
164                                                 When line support is enabled this configured endpoint name is used for incoming calls
165                                                 that are related to the outbound registration.
166                                         </para></description>
167                                 </configOption>
168                                 <configOption name="type">
169                                         <synopsis>Must be of type 'registration'.</synopsis>
170                                 </configOption>
171                                 <configOption name="support_path">
172                                         <synopsis>Enables advertising SIP Path support for outbound REGISTER requests.</synopsis>
173                                         <description><para>
174                                                 When this option is enabled, outbound REGISTER requests will advertise
175                                                 support for Path headers so that intervening proxies can add to the Path
176                                                 header as necessary.
177                                         </para></description>
178                                 </configOption>
179                                 <configOption name="support_outbound">
180                                         <synopsis>Enables advertising SIP Outbound support (RFC5626) for outbound REGISTER requests.</synopsis>
181                                 </configOption>
182                         </configObject>
183                 </configFile>
184         </configInfo>
185         <manager name="PJSIPUnregister" language="en_US">
186                 <synopsis>
187                         Unregister an outbound registration.
188                 </synopsis>
189                 <syntax>
190                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
191                         <parameter name="Registration" required="true">
192                                 <para>The outbound registration to unregister or '*all' to unregister them all.</para>
193                         </parameter>
194                 </syntax>
195                 <description>
196                         <para>
197                         Unregisters the specified (or all) outbound registration(s) and stops future registration attempts.
198                         Call PJSIPRegister to start registration and schedule re-registrations according to configuration.
199             </para>
200                 </description>
201         </manager>
202         <manager name="PJSIPRegister" language="en_US">
203                 <synopsis>
204                         Register an outbound registration.
205                 </synopsis>
206                 <syntax>
207                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
208                         <parameter name="Registration" required="true">
209                                 <para>The outbound registration to register or '*all' to register them all.</para>
210                         </parameter>
211                 </syntax>
212                 <description>
213                         <para>
214                         Unregisters the specified (or all) outbound registration(s) then starts registration and schedules re-registrations
215                         according to configuration.
216             </para>
217                 </description>
218         </manager>
219         <manager name="PJSIPShowRegistrationsOutbound" language="en_US">
220                 <synopsis>
221                         Lists PJSIP outbound registrations.
222                 </synopsis>
223                 <syntax />
224                 <description>
225                         <para>
226                         In response <literal>OutboundRegistrationDetail</literal> events showing configuration and status
227                         information are raised for each outbound registration object. <literal>AuthDetail</literal>
228                         events are raised for each associated auth object as well.  Once all events are completed an
229                         <literal>OutboundRegistrationDetailComplete</literal> is issued.
230                         </para>
231                 </description>
232         </manager>
233  ***/
234
235 /* forward declarations */
236 static int set_outbound_initial_authentication_credentials(pjsip_regc *regc,
237                 const struct ast_sip_auth_vector *auth_vector);
238
239 /*! \brief Some thread local storage used to determine if the running thread invoked the callback */
240 AST_THREADSTORAGE(register_callback_invoked);
241
242 /*! \brief Amount of buffer time (in seconds) before expiration that we re-register at */
243 #define REREGISTER_BUFFER_TIME 10
244
245 /*! \brief Size of the buffer for creating a unique string for the line */
246 #define LINE_PARAMETER_SIZE 8
247
248 /*! \brief Various states that an outbound registration may be in */
249 enum sip_outbound_registration_status {
250         /*! \brief Currently unregistered */
251         SIP_REGISTRATION_UNREGISTERED = 0,
252         /*! \brief Registered, yay! */
253         SIP_REGISTRATION_REGISTERED,
254         /*! \brief Registration was rejected, but response was temporal */
255         SIP_REGISTRATION_REJECTED_TEMPORARY,
256         /*! \brief Registration was rejected, permanently */
257         SIP_REGISTRATION_REJECTED_PERMANENT,
258         /*! \brief Registration is stopping. */
259         SIP_REGISTRATION_STOPPING,
260         /*! \brief Registration has been stopped */
261         SIP_REGISTRATION_STOPPED,
262 };
263
264 /*!
265  * \internal
266  * \brief Convert the internal registration state to an external status string.
267  * \since 13.5.0
268  *
269  * \param state Current outbound registration state.
270  *
271  * \return External registration status string.
272  */
273 static const char *sip_outbound_registration_status_str(enum sip_outbound_registration_status state)
274 {
275         const char *str;
276
277         str = "Unregistered";
278         switch (state) {
279         case SIP_REGISTRATION_STOPPING:
280         case SIP_REGISTRATION_STOPPED:
281         case SIP_REGISTRATION_UNREGISTERED:
282                 break;
283         case SIP_REGISTRATION_REGISTERED:
284                 str = "Registered";
285                 break;
286         case SIP_REGISTRATION_REJECTED_TEMPORARY:
287         case SIP_REGISTRATION_REJECTED_PERMANENT:
288                 str = "Rejected";
289                 break;
290         }
291         return str;
292 }
293
294 /*! \brief Outbound registration information */
295 struct sip_outbound_registration {
296         /*! \brief Sorcery object details */
297         SORCERY_OBJECT(details);
298         /*! \brief Stringfields */
299         AST_DECLARE_STRING_FIELDS(
300                 /*! \brief URI for the registrar */
301                 AST_STRING_FIELD(server_uri);
302                 /*! \brief URI for the AOR */
303                 AST_STRING_FIELD(client_uri);
304                 /*! \brief Optional user for contact header */
305                 AST_STRING_FIELD(contact_user);
306                 /*! \bried Optional header parameters for contact */
307                 AST_STRING_FIELD(contact_header_params);
308                 /*! \brief Explicit transport to use for registration */
309                 AST_STRING_FIELD(transport);
310                 /*! \brief Outbound proxy to use */
311                 AST_STRING_FIELD(outbound_proxy);
312                 /*! \brief Endpoint to use for related incoming calls */
313                 AST_STRING_FIELD(endpoint);
314         );
315         /*! \brief Requested expiration time */
316         unsigned int expiration;
317         /*! \brief Interval at which retries should occur for temporal responses */
318         unsigned int retry_interval;
319         /*! \brief Interval at which retries should occur for permanent responses */
320         unsigned int forbidden_retry_interval;
321         /*! \brief Interval at which retries should occur for all permanent responses */
322         unsigned int fatal_retry_interval;
323         /*! \brief Treat authentication challenges that we cannot handle as permanent failures */
324         unsigned int auth_rejection_permanent;
325         /*! \brief Maximum number of retries permitted */
326         unsigned int max_retries;
327         /*! \brief Whether to add a line parameter to the outbound Contact or not */
328         unsigned int line;
329         /*! \brief Configured authentication credentials */
330         struct ast_sip_auth_vector outbound_auths;
331         /*! \brief Whether Path support is enabled */
332         unsigned int support_path;
333         /*! \brief Whether Outbound support is enabled */
334         unsigned int support_outbound;
335 };
336
337 /*! \brief Outbound registration client state information (persists for lifetime of regc) */
338 struct sip_outbound_registration_client_state {
339         /*! \brief Current state of this registration */
340         enum sip_outbound_registration_status status;
341         /*!
342          * \brief Outbound registration client
343          * \note May only be accessed within the serializer thread
344          * because it might get destroyed and set to NULL for
345          * module unload.
346          */
347         pjsip_regc *client;
348         /*! \brief Timer entry for retrying on temporal responses */
349         pj_timer_entry timer;
350         /*! \brief Optional line parameter placed into Contact */
351         char line[LINE_PARAMETER_SIZE];
352         /*! \brief Current number of retries */
353         unsigned int retries;
354         /*! \brief Maximum number of retries permitted */
355         unsigned int max_retries;
356         /*! \brief Interval at which retries should occur for temporal responses */
357         unsigned int retry_interval;
358         /*! \brief Interval at which retries should occur for permanent responses */
359         unsigned int forbidden_retry_interval;
360         /*! \brief Interval at which retries should occur for all permanent responses */
361         unsigned int fatal_retry_interval;
362         /*! \brief Treat authentication challenges that we cannot handle as permanent failures */
363         unsigned int auth_rejection_permanent;
364         /*! \brief Determines whether SIP Path support should be advertised */
365         unsigned int support_path;
366         /*! \brief Determines whether SIP Outbound support should be advertised */
367         unsigned int support_outbound;
368         /*! CSeq number of last sent auth request. */
369         unsigned int auth_cseq;
370         /*! \brief Serializer for stuff and things */
371         struct ast_taskprocessor *serializer;
372         /*! \brief Configured authentication credentials */
373         struct ast_sip_auth_vector outbound_auths;
374         /*! \brief Registration should be destroyed after completion of transaction */
375         unsigned int destroy:1;
376         /*! \brief Non-zero if we have attempted sending a REGISTER with authentication */
377         unsigned int auth_attempted:1;
378         /*! \brief The name of the transport to be used for the registration */
379         char *transport_name;
380         /*! \brief The name of the registration sorcery object */
381         char *registration_name;
382 };
383
384 /*! \brief Outbound registration state information (persists for lifetime that registration should exist) */
385 struct sip_outbound_registration_state {
386         /*! \brief Outbound registration configuration object */
387         struct sip_outbound_registration *registration;
388         /*! \brief Client state information */
389         struct sip_outbound_registration_client_state *client_state;
390 };
391
392 /*! Time needs to be long enough for a transaction to timeout if nothing replies. */
393 #define MAX_UNLOAD_TIMEOUT_TIME         35      /* Seconds */
394
395 /*! Shutdown group to monitor sip_outbound_registration_client_state serializers. */
396 static struct ast_serializer_shutdown_group *shutdown_group;
397
398 /*! \brief Default number of state container buckets */
399 #define DEFAULT_STATE_BUCKETS 53
400 static AO2_GLOBAL_OBJ_STATIC(current_states);
401
402 /*! subscription id for network change events */
403 static struct stasis_subscription *network_change_sub;
404
405 /*! \brief hashing function for state objects */
406 static int registration_state_hash(const void *obj, const int flags)
407 {
408         const struct sip_outbound_registration_state *object;
409         const char *key;
410
411         switch (flags & OBJ_SEARCH_MASK) {
412         case OBJ_SEARCH_KEY:
413                 key = obj;
414                 break;
415         case OBJ_SEARCH_OBJECT:
416                 object = obj;
417                 key = ast_sorcery_object_get_id(object->registration);
418                 break;
419         default:
420                 ast_assert(0);
421                 return 0;
422         }
423         return ast_str_hash(key);
424 }
425
426 /*! \brief comparator function for state objects */
427 static int registration_state_cmp(void *obj, void *arg, int flags)
428 {
429         const struct sip_outbound_registration_state *object_left = obj;
430         const struct sip_outbound_registration_state *object_right = arg;
431         const char *right_key = arg;
432         int cmp;
433
434         switch (flags & OBJ_SEARCH_MASK) {
435         case OBJ_SEARCH_OBJECT:
436                 right_key = ast_sorcery_object_get_id(object_right->registration);
437                 /* Fall through */
438         case OBJ_SEARCH_KEY:
439                 cmp = strcmp(ast_sorcery_object_get_id(object_left->registration), right_key);
440                 break;
441         case OBJ_SEARCH_PARTIAL_KEY:
442                 /* Not supported by container. */
443                 ast_assert(0);
444                 return 0;
445         default:
446                 cmp = 0;
447                 break;
448         }
449         if (cmp) {
450                 return 0;
451         }
452         return CMP_MATCH;
453 }
454
455 static struct sip_outbound_registration_state *get_state(const char *id)
456 {
457         struct sip_outbound_registration_state *state = NULL;
458         struct ao2_container *states;
459
460         states = ao2_global_obj_ref(current_states);
461         if (states) {
462                 state = ao2_find(states, id, OBJ_SEARCH_KEY);
463                 ao2_ref(states, -1);
464         }
465         return state;
466 }
467
468 static struct ao2_container *get_registrations(void)
469 {
470         struct ao2_container *registrations = ast_sorcery_retrieve_by_fields(
471                 ast_sip_get_sorcery(), "registration",
472                 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
473
474         return registrations;
475 }
476
477 /*! \brief Callback function for matching an outbound registration based on line */
478 static int line_identify_relationship(void *obj, void *arg, int flags)
479 {
480         struct sip_outbound_registration_state *state = obj;
481         pjsip_param *line = arg;
482
483         return !pj_strcmp2(&line->value, state->client_state->line) ? CMP_MATCH : 0;
484 }
485
486 static struct pjsip_param *get_uri_option_line(const void *uri)
487 {
488         pjsip_sip_uri *pjuri;
489         static const pj_str_t LINE_STR = { "line", 4 };
490
491         if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri)) {
492                 return NULL;
493         }
494         pjuri = pjsip_uri_get_uri(uri);
495         return pjsip_param_find(&pjuri->other_param, &LINE_STR);
496 }
497
498 /*! \brief Endpoint identifier which uses the 'line' parameter to establish a relationship to an outgoing registration */
499 static struct ast_sip_endpoint *line_identify(pjsip_rx_data *rdata)
500 {
501         pjsip_param *line;
502         RAII_VAR(struct ao2_container *, states, NULL, ao2_cleanup);
503         RAII_VAR(struct sip_outbound_registration_state *, state, NULL, ao2_cleanup);
504
505         if (!(line = get_uri_option_line(rdata->msg_info.to->uri))
506                 && !(line = get_uri_option_line(rdata->msg_info.msg->line.req.uri))) {
507                 return NULL;
508         }
509
510         states = ao2_global_obj_ref(current_states);
511         if (!states) {
512                 return NULL;
513         }
514
515         state = ao2_callback(states, 0, line_identify_relationship, line);
516         if (!state || ast_strlen_zero(state->registration->endpoint)) {
517                 return NULL;
518         }
519
520         ast_debug(3, "Determined relationship to outbound registration '%s' based on line '%s', using configured endpoint '%s'\n",
521                 ast_sorcery_object_get_id(state->registration), state->client_state->line, state->registration->endpoint);
522
523         return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", state->registration->endpoint);
524 }
525
526 static struct ast_sip_endpoint_identifier line_identifier = {
527         .identify_endpoint = line_identify,
528 };
529
530 /*! \brief Helper function which cancels the timer on a client */
531 static void cancel_registration(struct sip_outbound_registration_client_state *client_state)
532 {
533         if (pj_timer_heap_cancel_if_active(pjsip_endpt_get_timer_heap(ast_sip_get_pjsip_endpoint()),
534                 &client_state->timer, client_state->timer.id)) {
535                 /* The timer was successfully cancelled, drop the refcount of client_state */
536                 ao2_ref(client_state, -1);
537         }
538 }
539
540 static pj_str_t PATH_NAME = { "path", 4 };
541 static pj_str_t OUTBOUND_NAME = { "outbound", 8 };
542
543 /*! \brief Helper function which sends a message and cleans up, if needed, on failure */
544 static pj_status_t registration_client_send(struct sip_outbound_registration_client_state *client_state,
545         pjsip_tx_data *tdata)
546 {
547         pj_status_t status;
548         int *callback_invoked;
549         pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
550
551         callback_invoked = ast_threadstorage_get(&register_callback_invoked, sizeof(int));
552         if (!callback_invoked) {
553                 pjsip_tx_data_dec_ref(tdata);
554                 return PJ_ENOMEM;
555         }
556         *callback_invoked = 0;
557
558         /* Due to the message going out the callback may now be invoked, so bump the count */
559         ao2_ref(client_state, +1);
560         /*
561          * Set the transport in case transports were reloaded.
562          * When pjproject removes the extraneous error messages produced,
563          * we can check status and only set the transport and resend if there was an error
564          */
565         ast_sip_set_tpselector_from_transport_name(client_state->transport_name, &selector);
566         pjsip_regc_set_transport(client_state->client, &selector);
567         ast_sip_tpselector_unref(&selector);
568
569         status = pjsip_regc_send(client_state->client, tdata);
570
571         /* If the attempt to send the message failed and the callback was not invoked we need to
572          * drop the reference we just added
573          */
574         if ((status != PJ_SUCCESS) && !(*callback_invoked)) {
575                 ao2_ref(client_state, -1);
576         }
577
578         return status;
579 }
580
581 /*! \brief Helper function to add string to Supported header */
582 static int add_to_supported_header(pjsip_tx_data *tdata, pj_str_t *name)
583 {
584         pjsip_supported_hdr *hdr;
585
586         hdr = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_SUPPORTED, NULL);
587         if (!hdr) {
588                 /* insert a new Supported header */
589                 hdr = pjsip_supported_hdr_create(tdata->pool);
590                 if (!hdr) {
591                         pjsip_tx_data_dec_ref(tdata);
592                         return 0;
593                 }
594
595                 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)hdr);
596         }
597
598         /* add on to the existing Supported header */
599         pj_strassign(&hdr->values[hdr->count++], name);
600
601         return 1;
602 }
603
604 /*! \brief Helper function to add configured supported headers */
605 static int add_configured_supported_headers(struct sip_outbound_registration_client_state *client_state, pjsip_tx_data *tdata)
606 {
607         if (client_state->support_path) {
608                 if (!add_to_supported_header(tdata, &PATH_NAME)) {
609                         return 0;
610                 }
611         }
612
613         if (client_state->support_outbound) {
614                 if (!add_to_supported_header(tdata, &OUTBOUND_NAME)) {
615                         return 0;
616                 }
617         }
618
619         return 1;
620 }
621
622 /*! \brief Callback function for registering */
623 static int handle_client_registration(void *data)
624 {
625         RAII_VAR(struct sip_outbound_registration_client_state *, client_state, data, ao2_cleanup);
626         pjsip_tx_data *tdata;
627
628         if (set_outbound_initial_authentication_credentials(client_state->client, &client_state->outbound_auths)) {
629                 ast_log(LOG_WARNING, "Failed to set initial authentication credentials\n");
630                 return -1;
631         }
632
633         if (client_state->status == SIP_REGISTRATION_STOPPED
634                 || pjsip_regc_register(client_state->client, PJ_FALSE, &tdata) != PJ_SUCCESS) {
635                 return 0;
636         }
637
638         if (DEBUG_ATLEAST(1)) {
639                 pjsip_regc_info info;
640
641                 pjsip_regc_get_info(client_state->client, &info);
642                 ast_log(LOG_DEBUG, "Outbound REGISTER attempt %u to '%.*s' with client '%.*s'\n",
643                         client_state->retries + 1,
644                         (int) info.server_uri.slen, info.server_uri.ptr,
645                         (int) info.client_uri.slen, info.client_uri.ptr);
646         }
647
648         if (!add_configured_supported_headers(client_state, tdata)) {
649                 ast_log(LOG_WARNING, "Failed to set supported headers\n");
650                 return -1;
651         }
652
653         registration_client_send(client_state, tdata);
654
655         return 0;
656 }
657
658 /*! \brief Timer callback function, used just for registrations */
659 static void sip_outbound_registration_timer_cb(pj_timer_heap_t *timer_heap, struct pj_timer_entry *entry)
660 {
661         struct sip_outbound_registration_client_state *client_state = entry->user_data;
662
663         entry->id = 0;
664
665         /*
666          * Transfer client_state reference to serializer task so the
667          * nominal path will not dec the client_state ref in this
668          * pjproject callback thread.
669          */
670         if (ast_sip_push_task(client_state->serializer, handle_client_registration, client_state)) {
671                 ast_log(LOG_WARNING, "Scheduled outbound registration could not be executed.\n");
672                 ao2_ref(client_state, -1);
673         }
674 }
675
676 /*! \brief Helper function which sets up the timer to re-register in a specific amount of time */
677 static void schedule_registration(struct sip_outbound_registration_client_state *client_state, unsigned int seconds)
678 {
679         pj_time_val delay = { .sec = seconds, };
680         pjsip_regc_info info;
681
682         cancel_registration(client_state);
683
684         pjsip_regc_get_info(client_state->client, &info);
685         ast_debug(1, "Scheduling outbound registration to server '%.*s' from client '%.*s' in %d seconds\n",
686                         (int) info.server_uri.slen, info.server_uri.ptr,
687                         (int) info.client_uri.slen, info.client_uri.ptr,
688                         seconds);
689
690         ao2_ref(client_state, +1);
691         if (pjsip_endpt_schedule_timer(ast_sip_get_pjsip_endpoint(), &client_state->timer, &delay) != PJ_SUCCESS) {
692                 ast_log(LOG_WARNING, "Failed to schedule registration to server '%.*s' from client '%.*s'\n",
693                                 (int) info.server_uri.slen, info.server_uri.ptr,
694                                 (int) info.client_uri.slen, info.client_uri.ptr);
695                 ao2_ref(client_state, -1);
696         }
697 }
698
699 static void update_client_state_status(struct sip_outbound_registration_client_state *client_state, enum sip_outbound_registration_status status)
700 {
701         const char *status_old;
702         const char *status_new;
703
704         if (client_state->status == status) {
705                 /* Status state did not change at all. */
706                 return;
707         }
708
709         status_old = sip_outbound_registration_status_str(client_state->status);
710         status_new = sip_outbound_registration_status_str(status);
711         client_state->status = status;
712
713         if (!strcmp(status_old, status_new)) {
714                 /*
715                  * The internal status state may have changed but the status
716                  * state we tell the world did not change at all.
717                  */
718                 return;
719         }
720
721         ast_statsd_log_string_va("PJSIP.registrations.state.%s", AST_STATSD_GAUGE, "-1", 1.0,
722                 status_old);
723         ast_statsd_log_string_va("PJSIP.registrations.state.%s", AST_STATSD_GAUGE, "+1", 1.0,
724                 status_new);
725 }
726
727 /*! \brief Callback function for unregistering (potentially) and destroying state */
728 static int handle_client_state_destruction(void *data)
729 {
730         struct sip_outbound_registration_client_state *client_state = data;
731
732         cancel_registration(client_state);
733
734         if (client_state->client) {
735                 pjsip_regc_info info;
736                 pjsip_tx_data *tdata;
737
738                 pjsip_regc_get_info(client_state->client, &info);
739
740                 if (info.is_busy == PJ_TRUE) {
741                         /* If a client transaction is in progress we defer until it is complete */
742                         ast_debug(1,
743                                 "Registration transaction is busy with server '%.*s' from client '%.*s'.\n",
744                                 (int) info.server_uri.slen, info.server_uri.ptr,
745                                 (int) info.client_uri.slen, info.client_uri.ptr);
746                         client_state->destroy = 1;
747                         ao2_ref(client_state, -1);
748                         return 0;
749                 }
750
751                 switch (client_state->status) {
752                 case SIP_REGISTRATION_UNREGISTERED:
753                         break;
754                 case SIP_REGISTRATION_REGISTERED:
755                         ast_debug(1,
756                                 "Trying to unregister with server '%.*s' from client '%.*s' before destruction.\n",
757                                 (int) info.server_uri.slen, info.server_uri.ptr,
758                                 (int) info.client_uri.slen, info.client_uri.ptr);
759
760                         update_client_state_status(client_state, SIP_REGISTRATION_STOPPING);
761                         client_state->destroy = 1;
762                         if (pjsip_regc_unregister(client_state->client, &tdata) == PJ_SUCCESS
763                                 && add_configured_supported_headers(client_state, tdata)
764                                 && registration_client_send(client_state, tdata) == PJ_SUCCESS) {
765                                 ao2_ref(client_state, -1);
766                                 return 0;
767                         }
768                         break;
769                 case SIP_REGISTRATION_REJECTED_TEMPORARY:
770                 case SIP_REGISTRATION_REJECTED_PERMANENT:
771                 case SIP_REGISTRATION_STOPPING:
772                 case SIP_REGISTRATION_STOPPED:
773                         break;
774                 }
775
776                 pjsip_regc_destroy(client_state->client);
777                 client_state->client = NULL;
778         }
779
780         update_client_state_status(client_state, SIP_REGISTRATION_STOPPED);
781         ast_sip_auth_vector_destroy(&client_state->outbound_auths);
782         ao2_ref(client_state, -1);
783
784         return 0;
785 }
786
787 /*! \brief Structure for registration response */
788 struct registration_response {
789         /*! \brief Response code for the registration attempt */
790         int code;
791         /*! \brief Expiration time for registration */
792         int expiration;
793         /*! \brief Retry-After value */
794         int retry_after;
795         /*! \brief Outbound registration client state */
796         struct sip_outbound_registration_client_state *client_state;
797         /*! \brief The response message */
798         pjsip_rx_data *rdata;
799         /*! \brief Request for which the response was received */
800         pjsip_tx_data *old_request;
801 };
802
803 /*! \brief Registration response structure destructor */
804 static void registration_response_destroy(void *obj)
805 {
806         struct registration_response *response = obj;
807
808         if (response->rdata) {
809                 pjsip_rx_data_free_cloned(response->rdata);
810         }
811
812         if (response->old_request) {
813                 pjsip_tx_data_dec_ref(response->old_request);
814         }
815
816         ao2_cleanup(response->client_state);
817 }
818
819 /*! \brief Helper function which determines if a response code is temporal or not */
820 static int sip_outbound_registration_is_temporal(unsigned int code,
821                 struct sip_outbound_registration_client_state *client_state)
822 {
823         /* Shamelessly taken from pjsua */
824         if (code == PJSIP_SC_REQUEST_TIMEOUT ||
825                 code == PJSIP_SC_INTERNAL_SERVER_ERROR ||
826                 code == PJSIP_SC_BAD_GATEWAY ||
827                 code == PJSIP_SC_SERVICE_UNAVAILABLE ||
828                 code == PJSIP_SC_SERVER_TIMEOUT ||
829                 ((code == PJSIP_SC_UNAUTHORIZED ||
830                   code == PJSIP_SC_PROXY_AUTHENTICATION_REQUIRED) &&
831                  !client_state->auth_rejection_permanent) ||
832                 PJSIP_IS_STATUS_IN_CLASS(code, 600)) {
833                 return 1;
834         } else {
835                 return 0;
836         }
837 }
838
839 static void schedule_retry(struct registration_response *response, unsigned int interval,
840                            const char *server_uri, const char *client_uri)
841 {
842         update_client_state_status(response->client_state, SIP_REGISTRATION_REJECTED_TEMPORARY);
843         schedule_registration(response->client_state, interval);
844
845         if (response->rdata) {
846                 ast_log(LOG_WARNING, "Temporal response '%d' received from '%s' on "
847                         "registration attempt to '%s', retrying in '%u'\n",
848                         response->code, server_uri, client_uri, interval);
849         } else {
850                 ast_log(LOG_WARNING, "No response received from '%s' on "
851                         "registration attempt to '%s', retrying in '%u'\n",
852                         server_uri, client_uri, interval);
853         }
854 }
855
856 static int reregister_immediately_cb(void *obj)
857 {
858         struct sip_outbound_registration_state *state = obj;
859
860         if (state->client_state->status != SIP_REGISTRATION_REGISTERED) {
861                 ao2_ref(state, -1);
862                 return 0;
863         }
864
865         if (DEBUG_ATLEAST(1)) {
866                 pjsip_regc_info info;
867
868                 pjsip_regc_get_info(state->client_state->client, &info);
869                 ast_log(LOG_DEBUG,
870                         "Outbound registration transport to server '%.*s' from client '%.*s' shutdown\n",
871                         (int) info.server_uri.slen, info.server_uri.ptr,
872                         (int) info.client_uri.slen, info.client_uri.ptr);
873         }
874
875         cancel_registration(state->client_state);
876
877         ao2_ref(state->client_state, +1);
878         handle_client_registration(state->client_state);
879
880         ao2_ref(state, -1);
881         return 0;
882 }
883
884 /*!
885  * \internal
886  * \brief The reliable transport we registered using has shutdown.
887  * \since 13.18.0
888  *
889  * \param obj What is needed to initiate a reregister attempt.
890  *
891  * \note Normally executed by the pjsip monitor thread.
892  *
893  * \return Nothing
894  */
895 static void registration_transport_shutdown_cb(void *obj)
896 {
897         const char *registration_name = obj;
898         struct sip_outbound_registration_state *state;
899
900         state = get_state(registration_name);
901         if (!state) {
902                 /* Registration no longer exists or shutting down. */
903                 return;
904         }
905         if (ast_sip_push_task(state->client_state->serializer, reregister_immediately_cb, state)) {
906                 ao2_ref(state, -1);
907         }
908 }
909
910 static int monitor_matcher(void *a, void *b)
911 {
912         char *ma = a;
913         char *mb = b;
914
915         return strcmp(ma, mb) == 0;
916 }
917
918 static void registration_transport_monitor_setup(pjsip_transport *transport, const char *registration_name)
919 {
920         char *monitor;
921
922         if (!PJSIP_TRANSPORT_IS_RELIABLE(transport)) {
923                 return;
924         }
925         monitor = ao2_alloc_options(strlen(registration_name) + 1, NULL,
926                 AO2_ALLOC_OPT_LOCK_NOLOCK);
927         if (!monitor) {
928                 return;
929         }
930         strcpy(monitor, registration_name);/* Safe */
931
932         /*
933          * We'll ignore if the transport has already been shutdown before we
934          * register the monitor.  We might get into a message spamming infinite
935          * loop of registration, shutdown, reregistration...
936          */
937         ast_sip_transport_monitor_register(transport, registration_transport_shutdown_cb,
938                 monitor);
939         ao2_ref(monitor, -1);
940 }
941
942 static void save_response_fields_to_transport(struct registration_response *response)
943 {
944         static const pj_str_t associated_uri_str = { "P-Associated-URI", 16 };
945         static const pj_str_t service_route_str = { "Service-Route", 13 };
946         pjsip_hdr *header = NULL;
947         pjsip_msg *msg = response->rdata->msg_info.msg;
948         struct ast_sip_service_route_vector *service_routes = NULL;
949
950         /* If no transport is specified then we can't update any */
951         if (ast_strlen_zero(response->client_state->transport_name)) {
952                 return;
953         }
954
955         ast_sip_transport_state_set_transport(response->client_state->transport_name, response->rdata->tp_info.transport);
956
957         while ((header = pjsip_msg_find_hdr_by_name(msg, &service_route_str, header ? header->next : NULL))) {
958                 char *service_route;
959                 size_t size;
960
961                 /* The below code takes the approach that if we can't store all service routes then we
962                  * store none at all. This gives a predictable failure condition instead of storing a
963                  * partial list and having partial route headers.
964                  */
965                 size = pj_strlen(&((pjsip_generic_string_hdr*)header)->hvalue) + 1;
966                 service_route = ast_malloc(size);
967                 if (!service_route) {
968                         if (service_routes) {
969                                 ast_sip_service_route_vector_destroy(service_routes);
970                                 service_routes = NULL;
971                         }
972                         break;
973                 }
974
975                 ast_copy_pj_str(service_route, &((pjsip_generic_string_hdr*)header)->hvalue, size);
976
977                 if (!service_routes) {
978                         service_routes = ast_sip_service_route_vector_alloc();
979                         if (!service_routes) {
980                                 ast_free(service_route);
981                                 break;
982                         }
983                 }
984
985                 if (AST_VECTOR_APPEND(service_routes, service_route)) {
986                         ast_free(service_route);
987                         ast_sip_service_route_vector_destroy(service_routes);
988                         service_routes = NULL;
989                         break;
990                 }
991         }
992
993         /* If any service routes were handled then store them on the transport */
994         if (service_routes) {
995                 ast_sip_transport_state_set_service_routes(response->client_state->transport_name, service_routes);
996         }
997
998         /* If an associated URI is present in the response we need to use it on any outgoing
999          * traffic on the transport.
1000          */
1001         header = pjsip_msg_find_hdr_by_name(msg, &associated_uri_str, NULL);
1002         if (header) {
1003                 char value[pj_strlen(&((pjsip_generic_string_hdr*)header)->hvalue) + 1];
1004
1005                 ast_copy_pj_str(value, &((pjsip_generic_string_hdr*)header)->hvalue, sizeof(value));
1006                 ast_sip_transport_state_set_preferred_identity(response->client_state->transport_name, value);
1007         }
1008 }
1009
1010
1011 /*! \brief Callback function for handling a response to a registration attempt */
1012 static int handle_registration_response(void *data)
1013 {
1014         struct registration_response *response = data;
1015         pjsip_regc_info info;
1016         char server_uri[PJSIP_MAX_URL_SIZE];
1017         char client_uri[PJSIP_MAX_URL_SIZE];
1018
1019         if (response->client_state->status == SIP_REGISTRATION_STOPPED) {
1020                 ao2_ref(response, -1);
1021                 return 0;
1022         }
1023
1024         pjsip_regc_get_info(response->client_state->client, &info);
1025         ast_copy_pj_str(server_uri, &info.server_uri, sizeof(server_uri));
1026         ast_copy_pj_str(client_uri, &info.client_uri, sizeof(client_uri));
1027
1028         ast_debug(1, "Processing REGISTER response %d from server '%s' for client '%s'\n",
1029                         response->code, server_uri, client_uri);
1030
1031         if ((response->code == 401 || response->code == 407)
1032                 && (!response->client_state->auth_attempted
1033                         || response->rdata->msg_info.cseq->cseq != response->client_state->auth_cseq)) {
1034                 int res;
1035                 pjsip_cseq_hdr *cseq_hdr;
1036                 pjsip_tx_data *tdata;
1037
1038                 if (!ast_sip_create_request_with_auth(&response->client_state->outbound_auths,
1039                                 response->rdata, response->old_request, &tdata)) {
1040                         response->client_state->auth_attempted = 1;
1041                         ast_debug(1, "Sending authenticated REGISTER to server '%s' from client '%s'\n",
1042                                         server_uri, client_uri);
1043                         pjsip_tx_data_add_ref(tdata);
1044                         res = registration_client_send(response->client_state, tdata);
1045
1046                         /* Save the cseq that actually got sent. */
1047                         cseq_hdr = (pjsip_cseq_hdr *) pjsip_msg_find_hdr(tdata->msg, PJSIP_H_CSEQ,
1048                                 NULL);
1049                         response->client_state->auth_cseq = cseq_hdr->cseq;
1050                         pjsip_tx_data_dec_ref(tdata);
1051                         if (res == PJ_SUCCESS) {
1052                                 ao2_ref(response, -1);
1053                                 return 0;
1054                         }
1055                 } else {
1056                         ast_log(LOG_WARNING, "Failed to create authenticated REGISTER request to server '%s' from client '%s'\n",
1057                                         server_uri, client_uri);
1058                 }
1059                 /* Otherwise, fall through so the failure is processed appropriately */
1060         }
1061
1062         response->client_state->auth_attempted = 0;
1063
1064         if (PJSIP_IS_STATUS_IN_CLASS(response->code, 200)) {
1065                 /* Check if this is in regards to registering or unregistering */
1066                 if (response->expiration) {
1067                         int next_registration_round;
1068
1069                         /* If the registration went fine simply reschedule registration for the future */
1070                         ast_debug(1, "Outbound registration to '%s' with client '%s' successful\n", server_uri, client_uri);
1071                         update_client_state_status(response->client_state, SIP_REGISTRATION_REGISTERED);
1072                         response->client_state->retries = 0;
1073                         next_registration_round = response->expiration - REREGISTER_BUFFER_TIME;
1074                         if (next_registration_round < 0) {
1075                                 /* Re-register immediately. */
1076                                 next_registration_round = 0;
1077                         }
1078                         schedule_registration(response->client_state, next_registration_round);
1079
1080                         /* See if we should monitor for transport shutdown */
1081                         registration_transport_monitor_setup(response->rdata->tp_info.transport,
1082                                 response->client_state->registration_name);
1083                 } else {
1084                         ast_debug(1, "Outbound unregistration to '%s' with client '%s' successful\n", server_uri, client_uri);
1085                         update_client_state_status(response->client_state, SIP_REGISTRATION_UNREGISTERED);
1086                         ast_sip_transport_monitor_unregister(response->rdata->tp_info.transport,
1087                                 registration_transport_shutdown_cb, response->client_state->registration_name,
1088                                 monitor_matcher);
1089                 }
1090
1091                 save_response_fields_to_transport(response);
1092         } else if (response->client_state->destroy) {
1093                 /* We need to deal with the pending destruction instead. */
1094         } else if (response->retry_after) {
1095                 /* If we have been instructed to retry after a period of time, schedule it as such */
1096                 schedule_retry(response, response->retry_after, server_uri, client_uri);
1097         } else if (response->client_state->retry_interval
1098                 && sip_outbound_registration_is_temporal(response->code, response->client_state)) {
1099                 if (response->client_state->retries == response->client_state->max_retries) {
1100                         /* If we received enough temporal responses to exceed our maximum give up permanently */
1101                         update_client_state_status(response->client_state, SIP_REGISTRATION_REJECTED_PERMANENT);
1102                         ast_log(LOG_WARNING, "Maximum retries reached when attempting outbound registration to '%s' with client '%s', stopping registration attempt\n",
1103                                 server_uri, client_uri);
1104                 } else {
1105                         /* On the other hand if we can still try some more do so */
1106                         response->client_state->retries++;
1107                         schedule_retry(response, response->client_state->retry_interval, server_uri, client_uri);
1108                 }
1109         } else {
1110                 if (response->code == 403
1111                         && response->client_state->forbidden_retry_interval
1112                         && response->client_state->retries < response->client_state->max_retries) {
1113                         /* A forbidden response retry interval is configured and there are retries remaining */
1114                         update_client_state_status(response->client_state, SIP_REGISTRATION_REJECTED_TEMPORARY);
1115                         response->client_state->retries++;
1116                         schedule_registration(response->client_state, response->client_state->forbidden_retry_interval);
1117                         ast_log(LOG_WARNING, "403 Forbidden fatal response received from '%s' on registration attempt to '%s', retrying in '%u' seconds\n",
1118                                 server_uri, client_uri, response->client_state->forbidden_retry_interval);
1119                 } else if (response->client_state->fatal_retry_interval
1120                            && response->client_state->retries < response->client_state->max_retries) {
1121                         /* Some kind of fatal failure response received, so retry according to configured interval */
1122                         update_client_state_status(response->client_state, SIP_REGISTRATION_REJECTED_TEMPORARY);
1123                         response->client_state->retries++;
1124                         schedule_registration(response->client_state, response->client_state->fatal_retry_interval);
1125                         ast_log(LOG_WARNING, "'%d' fatal response received from '%s' on registration attempt to '%s', retrying in '%u' seconds\n",
1126                                 response->code, server_uri, client_uri, response->client_state->fatal_retry_interval);
1127                 } else {
1128                         /* Finally if there's no hope of registering give up */
1129                         update_client_state_status(response->client_state, SIP_REGISTRATION_REJECTED_PERMANENT);
1130                         if (response->rdata) {
1131                                 ast_log(LOG_WARNING, "Fatal response '%d' received from '%s' on registration attempt to '%s', stopping outbound registration\n",
1132                                         response->code, server_uri, client_uri);
1133                         } else {
1134                                 ast_log(LOG_WARNING, "Fatal registration attempt to '%s', stopping outbound registration\n", client_uri);
1135                         }
1136                 }
1137         }
1138
1139         ast_system_publish_registry("PJSIP", client_uri, server_uri,
1140                 sip_outbound_registration_status_str(response->client_state->status), NULL);
1141
1142         if (response->client_state->destroy) {
1143                 /* We have a pending deferred destruction to complete now. */
1144                 ao2_ref(response->client_state, +1);
1145                 handle_client_state_destruction(response->client_state);
1146         }
1147
1148         ao2_ref(response, -1);
1149         return 0;
1150 }
1151
1152 /*! \brief Callback function for outbound registration client */
1153 static void sip_outbound_registration_response_cb(struct pjsip_regc_cbparam *param)
1154 {
1155         struct sip_outbound_registration_client_state *client_state = param->token;
1156         struct registration_response *response;
1157         int *callback_invoked;
1158
1159         callback_invoked = ast_threadstorage_get(&register_callback_invoked, sizeof(int));
1160
1161         ast_assert(callback_invoked != NULL);
1162         ast_assert(client_state != NULL);
1163
1164         *callback_invoked = 1;
1165
1166         response = ao2_alloc(sizeof(*response), registration_response_destroy);
1167         if (!response) {
1168                 ao2_ref(client_state, -1);
1169                 return;
1170         }
1171         response->code = param->code;
1172         response->expiration = param->expiration;
1173         /*
1174          * Transfer client_state reference to response so the
1175          * nominal path will not dec the client_state ref in this
1176          * pjproject callback thread.
1177          */
1178         response->client_state = client_state;
1179
1180         ast_debug(1, "Received REGISTER response %d(%.*s)\n",
1181                 param->code, (int) param->reason.slen, param->reason.ptr);
1182
1183         if (param->rdata) {
1184                 struct pjsip_retry_after_hdr *retry_after;
1185                 pjsip_transaction *tsx;
1186
1187                 retry_after = pjsip_msg_find_hdr(param->rdata->msg_info.msg, PJSIP_H_RETRY_AFTER,
1188                         NULL);
1189                 response->retry_after = retry_after ? retry_after->ivalue : 0;
1190                 tsx = pjsip_rdata_get_tsx(param->rdata);
1191                 response->old_request = tsx->last_tx;
1192                 pjsip_tx_data_add_ref(response->old_request);
1193                 pjsip_rx_data_clone(param->rdata, 0, &response->rdata);
1194         }
1195
1196         /*
1197          * Transfer response reference to serializer task so the
1198          * nominal path will not dec the response ref in this
1199          * pjproject callback thread.
1200          */
1201         if (ast_sip_push_task(client_state->serializer, handle_registration_response, response)) {
1202                 ast_log(LOG_WARNING, "Failed to pass incoming registration response to threadpool\n");
1203                 ao2_cleanup(response);
1204         }
1205 }
1206
1207 /*! \brief Destructor function for registration state */
1208 static void sip_outbound_registration_state_destroy(void *obj)
1209 {
1210         struct sip_outbound_registration_state *state = obj;
1211
1212         ast_debug(3, "Destroying registration state for registration to server '%s' from client '%s'\n",
1213                 state->registration ? state->registration->server_uri : "",
1214                 state->registration ? state->registration->client_uri : "");
1215         ao2_cleanup(state->registration);
1216
1217         if (!state->client_state) {
1218                 /* Nothing to do */
1219         } else if (!state->client_state->serializer) {
1220                 ao2_ref(state->client_state, -1);
1221         } else if (ast_sip_push_task(state->client_state->serializer,
1222                 handle_client_state_destruction, state->client_state)) {
1223                 ast_log(LOG_WARNING, "Failed to pass outbound registration client destruction to threadpool\n");
1224                 ao2_ref(state->client_state, -1);
1225         }
1226 }
1227
1228 /*! \brief Destructor function for client registration state */
1229 static void sip_outbound_registration_client_state_destroy(void *obj)
1230 {
1231         struct sip_outbound_registration_client_state *client_state = obj;
1232
1233         ast_statsd_log_string("PJSIP.registrations.count", AST_STATSD_GAUGE, "-1", 1.0);
1234         ast_statsd_log_string_va("PJSIP.registrations.state.%s", AST_STATSD_GAUGE, "-1", 1.0,
1235                 sip_outbound_registration_status_str(client_state->status));
1236
1237         ast_taskprocessor_unreference(client_state->serializer);
1238         ast_free(client_state->transport_name);
1239         ast_free(client_state->registration_name);
1240 }
1241
1242 /*! \brief Allocator function for registration state */
1243 static struct sip_outbound_registration_state *sip_outbound_registration_state_alloc(struct sip_outbound_registration *registration)
1244 {
1245         struct sip_outbound_registration_state *state;
1246         char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1];
1247
1248         state = ao2_alloc(sizeof(*state), sip_outbound_registration_state_destroy);
1249         if (!state) {
1250                 return NULL;
1251         }
1252         state->client_state = ao2_alloc(sizeof(*state->client_state),
1253                 sip_outbound_registration_client_state_destroy);
1254         if (!state->client_state) {
1255                 ao2_cleanup(state);
1256                 return NULL;
1257         }
1258
1259         state->client_state->status = SIP_REGISTRATION_UNREGISTERED;
1260         pj_timer_entry_init(&state->client_state->timer, 0, state->client_state,
1261                 sip_outbound_registration_timer_cb);
1262         state->client_state->transport_name = ast_strdup(registration->transport);
1263         state->client_state->registration_name =
1264                 ast_strdup(ast_sorcery_object_get_id(registration));
1265
1266         ast_statsd_log_string("PJSIP.registrations.count", AST_STATSD_GAUGE, "+1", 1.0);
1267         ast_statsd_log_string_va("PJSIP.registrations.state.%s", AST_STATSD_GAUGE, "+1", 1.0,
1268                 sip_outbound_registration_status_str(state->client_state->status));
1269
1270         if (!state->client_state->transport_name
1271                 || !state->client_state->registration_name) {
1272                 ao2_cleanup(state);
1273                 return NULL;
1274         }
1275
1276         /* Create name with seq number appended. */
1277         ast_taskprocessor_build_name(tps_name, sizeof(tps_name), "pjsip/outreg/%s",
1278                 ast_sorcery_object_get_id(registration));
1279
1280         state->client_state->serializer = ast_sip_create_serializer_group(tps_name,
1281                 shutdown_group);
1282         if (!state->client_state->serializer) {
1283                 ao2_cleanup(state);
1284                 return NULL;
1285         }
1286
1287         state->registration = ao2_bump(registration);
1288         return state;
1289 }
1290
1291 /*! \brief Destructor function for registration information */
1292 static void sip_outbound_registration_destroy(void *obj)
1293 {
1294         struct sip_outbound_registration *registration = obj;
1295
1296         ast_sip_auth_vector_destroy(&registration->outbound_auths);
1297
1298         ast_string_field_free_memory(registration);
1299 }
1300
1301 /*! \brief Allocator function for registration information */
1302 static void *sip_outbound_registration_alloc(const char *name)
1303 {
1304         struct sip_outbound_registration *registration;
1305
1306         registration = ast_sorcery_generic_alloc(sizeof(*registration),
1307                 sip_outbound_registration_destroy);
1308         if (!registration || ast_string_field_init(registration, 256)) {
1309                 ao2_cleanup(registration);
1310                 return NULL;
1311         }
1312
1313         return registration;
1314 }
1315
1316 /*! \brief Helper function which populates a pj_str_t with a contact header */
1317 static int sip_dialog_create_contact(pj_pool_t *pool, pj_str_t *contact, const char *user,
1318         const pj_str_t *target, pjsip_tpselector *selector, const char *line, const char *header_params)
1319 {
1320         pj_str_t tmp, local_addr;
1321         pjsip_uri *uri;
1322         pjsip_sip_uri *sip_uri;
1323         pjsip_transport_type_e type;
1324         int local_port;
1325
1326         pj_strdup_with_null(pool, &tmp, target);
1327
1328         if (!(uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0)) ||
1329             (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))) {
1330                 return -1;
1331         }
1332
1333         sip_uri = pjsip_uri_get_uri(uri);
1334
1335         type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
1336         if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri)) {
1337                 if (type == PJSIP_TRANSPORT_UNSPECIFIED
1338                         || !(pjsip_transport_get_flag_from_type(type) & PJSIP_TRANSPORT_SECURE)) {
1339                         type = PJSIP_TRANSPORT_TLS;
1340                 }
1341         } else if (!sip_uri->transport_param.slen) {
1342                 type = PJSIP_TRANSPORT_UDP;
1343         } else if (type == PJSIP_TRANSPORT_UNSPECIFIED) {
1344                 return -1;
1345         }
1346
1347         if (pj_strchr(&sip_uri->host, ':')) {
1348                 type |= PJSIP_TRANSPORT_IPV6;
1349         }
1350
1351         if (pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()),
1352                 pool, type, selector, &local_addr, &local_port) != PJ_SUCCESS) {
1353                 return -1;
1354         }
1355
1356         if (!pj_strchr(&sip_uri->host, ':') && pj_strchr(&local_addr, ':')) {
1357                 type |= PJSIP_TRANSPORT_IPV6;
1358         }
1359
1360         contact->ptr = pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
1361         contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
1362                 "<%s:%s@%s%.*s%s:%d%s%s%s%s>%s%s",
1363                 ((pjsip_transport_get_flag_from_type(type) & PJSIP_TRANSPORT_SECURE) && PJSIP_URI_SCHEME_IS_SIPS(uri)) ? "sips" : "sip",
1364                 user,
1365                 (type & PJSIP_TRANSPORT_IPV6) ? "[" : "",
1366                 (int)local_addr.slen,
1367                 local_addr.ptr,
1368                 (type & PJSIP_TRANSPORT_IPV6) ? "]" : "",
1369                 local_port,
1370                 (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? ";transport=" : "",
1371                 (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? pjsip_transport_get_type_name(type) : "",
1372                 !ast_strlen_zero(line) ? ";line=" : "",
1373                 S_OR(line, ""),
1374                 !ast_strlen_zero(header_params) ? ";" : "",
1375                 S_OR(header_params, ""));
1376
1377         return 0;
1378 }
1379
1380 /*!
1381  * \internal
1382  * \brief Check if a registration can be reused
1383  *
1384  * This checks if the existing outbound registration's configuration differs from a newly-applied
1385  * outbound registration to see if the applied one.
1386  *
1387  * \param existing The pre-existing outbound registration
1388  * \param applied The newly-created registration
1389  */
1390 static int can_reuse_registration(struct sip_outbound_registration *existing,
1391         struct sip_outbound_registration *applied)
1392 {
1393         int rc = 1;
1394         struct ast_sorcery *sorcery = ast_sip_get_sorcery();
1395         struct ast_variable *ve = ast_sorcery_objectset_create(sorcery, existing);
1396         struct ast_variable *va = ast_sorcery_objectset_create(sorcery, applied);
1397         struct ast_variable *vc = NULL;
1398
1399         if (ast_sorcery_changeset_create(ve, va, &vc) || vc != NULL) {
1400                 rc = 0;
1401                 ast_debug(4, "Registration '%s' changed.  Can't re-use.\n", ast_sorcery_object_get_id(existing));
1402         } else {
1403                 ast_debug(4, "Registration '%s' didn't change.  Can re-use\n", ast_sorcery_object_get_id(existing));
1404         }
1405
1406         ast_variables_destroy(ve);
1407         ast_variables_destroy(va);
1408         ast_variables_destroy(vc);
1409
1410         return rc;
1411 }
1412
1413 /* \brief Get google oauth2 access token using refresh token */
1414 static const char *fetch_google_access_token(const struct ast_sip_auth *auth)
1415 {
1416         char *cmd = NULL;
1417         const char *token;
1418         const char *url = "https://www.googleapis.com/oauth2/v3/token";
1419         char buf[4096];
1420         int res;
1421         struct ast_json_error error;
1422         struct ast_json *json;
1423
1424         /* set timeout to be shorter than default 180s (also checks func_curl is available) */
1425         if (ast_func_write(NULL, "CURLOPT(conntimeout)", "10")) {
1426                 ast_log(LOG_ERROR, "CURL is unavailable. This is required for Google OAuth 2.0 authentication. Please ensure it is loaded.\n");
1427                 return NULL;
1428         }
1429
1430         res = ast_asprintf(&cmd,
1431                 "CURL(%s,client_id=%s&client_secret=%s&refresh_token=%s&grant_type=refresh_token)",
1432                 url, auth->oauth_clientid, auth->oauth_secret, auth->refresh_token);
1433         if (res < 0) {
1434                 return NULL;
1435         }
1436
1437         ast_debug(2, "Performing Google OAuth 2.0 authentication using command: %s\n", cmd);
1438
1439         buf[0] = '\0';
1440         res = ast_func_read(NULL, cmd, buf, sizeof(buf));
1441         ast_free(cmd);
1442         if (res) {
1443                 ast_log(LOG_ERROR, "Could not retrieve Google OAuth 2.0 authentication\n");
1444                 return NULL;
1445         }
1446
1447         ast_debug(2, "Google OAuth 2.0 authentication returned: %s\n", buf);
1448
1449         json = ast_json_load_string(buf, &error);
1450         if (!json) {
1451                 ast_log(LOG_ERROR, "Could not parse Google OAuth 2.0 authentication: %d(%d) %s: '%s'\n",
1452                         error.line, error.column, error.text, buf);
1453                 return NULL;
1454         }
1455
1456         token = ast_json_string_get(ast_json_object_get(json, "access_token"));
1457         if (!token) {
1458                 ast_log(LOG_ERROR, "Could not find Google OAuth 2.0 access_token in: '%s'\n",
1459                         buf);
1460         }
1461         token = ast_strdup(token);
1462         ast_json_unref(json);
1463         return token;
1464 }
1465
1466 /*!
1467  * \internal
1468  * \brief Set pjsip registration context with any authentication credentials that need to be
1469  * sent in the initial registration request
1470  *
1471  * \param regc The pjsip registration context
1472  * \param auth_vector The vector of configured authentication credentials
1473  */
1474 static int set_outbound_initial_authentication_credentials(pjsip_regc *regc,
1475                 const struct ast_sip_auth_vector *auth_vector)
1476 {
1477         size_t auth_size = AST_VECTOR_SIZE(auth_vector);
1478         struct ast_sip_auth *auths[auth_size];
1479         const char *access_token;
1480         pjsip_cred_info auth_creds[1];
1481         pjsip_auth_clt_pref prefs;
1482         int res = 0;
1483         int idx;
1484
1485         memset(auths, 0, sizeof(auths));
1486         if (ast_sip_retrieve_auths(auth_vector, auths)) {
1487                 res = -1;
1488                 goto cleanup;
1489         }
1490
1491         for (idx = 0; idx < auth_size; ++idx) {
1492                 switch (auths[idx]->type) {
1493                 case AST_SIP_AUTH_TYPE_GOOGLE_OAUTH:
1494                         pj_cstr(&auth_creds[0].username, auths[idx]->auth_user);
1495                         pj_cstr(&auth_creds[0].scheme, "Bearer");
1496                         pj_cstr(&auth_creds[0].realm, auths[idx]->realm);
1497                         ast_debug(2, "Obtaining Google OAuth access token\n");
1498                         access_token = fetch_google_access_token(auths[idx]);
1499                         if (!access_token) {
1500                                 ast_log(LOG_WARNING, "Obtaining Google OAuth access token failed\n");
1501                                 access_token = auths[idx]->auth_pass;
1502                                 res = -1;
1503                         }
1504                         ast_debug(2, "Setting data to '%s'\n", access_token);
1505
1506                         pj_cstr(&auth_creds[0].data, access_token);
1507                         auth_creds[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
1508
1509                         pjsip_regc_set_credentials(regc, 1, auth_creds);
1510
1511                         /* for oauth, send auth without waiting for unauthorized response */
1512                         prefs.initial_auth = PJ_TRUE;
1513                         pj_cstr(&prefs.algorithm, "oauth");
1514                         pjsip_regc_set_prefs(regc, &prefs);
1515
1516                         if (access_token != auths[idx]->auth_pass) {
1517                                 ast_free((char *) access_token);
1518                         }
1519                         break;
1520                 default:
1521                         /* other cases handled after receiving auth rejection */
1522                         break;
1523                 }
1524         }
1525
1526 cleanup:
1527         ast_sip_cleanup_auths(auths, auth_size);
1528         return res;
1529 }
1530
1531 /*! \brief Helper function that allocates a pjsip registration client and configures it */
1532 static int sip_outbound_registration_regc_alloc(void *data)
1533 {
1534         struct sip_outbound_registration_state *state = data;
1535         RAII_VAR(struct sip_outbound_registration *, registration,
1536                  ao2_bump(state->registration), ao2_cleanup);
1537         pj_pool_t *pool;
1538         pj_str_t tmp;
1539         pjsip_uri *uri;
1540         pj_str_t server_uri, client_uri, contact_uri;
1541         pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
1542
1543         pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "URI Validation", 256, 256);
1544         if (!pool) {
1545                 ast_log(LOG_ERROR, "Could not create pool for URI validation on outbound registration '%s'\n",
1546                         ast_sorcery_object_get_id(registration));
1547                 return -1;
1548         }
1549
1550         pj_strdup2_with_null(pool, &tmp, registration->server_uri);
1551         uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
1552         if (!uri) {
1553                 ast_log(LOG_ERROR, "Invalid server URI '%s' specified on outbound registration '%s'\n",
1554                         registration->server_uri, ast_sorcery_object_get_id(registration));
1555                 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
1556                 return -1;
1557         }
1558
1559         pj_strdup2_with_null(pool, &tmp, registration->client_uri);
1560         uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
1561         if (!uri) {
1562                 ast_log(LOG_ERROR, "Invalid client URI '%s' specified on outbound registration '%s'\n",
1563                         registration->client_uri, ast_sorcery_object_get_id(registration));
1564                 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
1565                 return -1;
1566         }
1567
1568         if (!ast_strlen_zero(registration->outbound_proxy)) {
1569                 pj_strdup2_with_null(pool, &tmp, registration->outbound_proxy);
1570                 uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
1571                 if (!uri) {
1572                         ast_log(LOG_ERROR, "Invalid outbound proxy URI '%s' specified on outbound registration '%s'\n",
1573                                 registration->outbound_proxy, ast_sorcery_object_get_id(registration));
1574                         pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
1575                         return -1;
1576                 }
1577         }
1578
1579         pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
1580
1581
1582         ast_assert(state->client_state->client == NULL);
1583         if (pjsip_regc_create(ast_sip_get_pjsip_endpoint(), state->client_state,
1584                         sip_outbound_registration_response_cb,
1585                         &state->client_state->client) != PJ_SUCCESS) {
1586                 return -1;
1587         }
1588
1589         ast_sip_set_tpselector_from_transport_name(registration->transport, &selector);
1590         pjsip_regc_set_transport(state->client_state->client, &selector);
1591
1592         if (!ast_strlen_zero(registration->outbound_proxy)) {
1593                 pjsip_route_hdr route_set, *route;
1594                 static const pj_str_t ROUTE_HNAME = { "Route", 5 };
1595                 pj_str_t tmp;
1596
1597                 pj_list_init(&route_set);
1598
1599                 pj_strdup2_with_null(pjsip_regc_get_pool(state->client_state->client), &tmp,
1600                         registration->outbound_proxy);
1601                 route = pjsip_parse_hdr(pjsip_regc_get_pool(state->client_state->client),
1602                         &ROUTE_HNAME, tmp.ptr, tmp.slen, NULL);
1603                 if (!route) {
1604                         ast_sip_tpselector_unref(&selector);
1605                         return -1;
1606                 }
1607                 pj_list_insert_nodes_before(&route_set, route);
1608
1609                 pjsip_regc_set_route_set(state->client_state->client, &route_set);
1610         }
1611
1612         if (state->registration->line) {
1613                 ast_generate_random_string(state->client_state->line, sizeof(state->client_state->line));
1614         }
1615
1616         pj_cstr(&server_uri, registration->server_uri);
1617
1618         if (sip_dialog_create_contact(pjsip_regc_get_pool(state->client_state->client),
1619                 &contact_uri, S_OR(registration->contact_user, "s"), &server_uri, &selector,
1620                 state->client_state->line, registration->contact_header_params)) {
1621                 ast_sip_tpselector_unref(&selector);
1622                 return -1;
1623         }
1624
1625         ast_sip_tpselector_unref(&selector);
1626
1627         pj_cstr(&client_uri, registration->client_uri);
1628         if (pjsip_regc_init(state->client_state->client, &server_uri, &client_uri,
1629                 &client_uri, 1, &contact_uri, registration->expiration) != PJ_SUCCESS) {
1630                 return -1;
1631         }
1632
1633         return 0;
1634 }
1635
1636 /*! \brief Helper function which performs a single registration */
1637 static int sip_outbound_registration_perform(void *data)
1638 {
1639         struct sip_outbound_registration_state *state = data;
1640         struct sip_outbound_registration *registration = ao2_bump(state->registration);
1641         size_t i;
1642
1643         /* Just in case the client state is being reused for this registration, free the auth information */
1644         ast_sip_auth_vector_destroy(&state->client_state->outbound_auths);
1645
1646         AST_VECTOR_INIT(&state->client_state->outbound_auths, AST_VECTOR_SIZE(&registration->outbound_auths));
1647         for (i = 0; i < AST_VECTOR_SIZE(&registration->outbound_auths); ++i) {
1648                 char *name = ast_strdup(AST_VECTOR_GET(&registration->outbound_auths, i));
1649
1650                 if (name && AST_VECTOR_APPEND(&state->client_state->outbound_auths, name)) {
1651                         ast_free(name);
1652                 }
1653         }
1654         state->client_state->retry_interval = registration->retry_interval;
1655         state->client_state->forbidden_retry_interval = registration->forbidden_retry_interval;
1656         state->client_state->fatal_retry_interval = registration->fatal_retry_interval;
1657         state->client_state->max_retries = registration->max_retries;
1658         state->client_state->retries = 0;
1659         state->client_state->support_path = registration->support_path;
1660         state->client_state->support_outbound = registration->support_outbound;
1661         state->client_state->auth_rejection_permanent = registration->auth_rejection_permanent;
1662
1663         pjsip_regc_update_expires(state->client_state->client, registration->expiration);
1664
1665         schedule_registration(state->client_state, (ast_random() % 10) + 1);
1666
1667         ao2_ref(registration, -1);
1668         ao2_ref(state, -1);
1669         return 0;
1670 }
1671
1672 /*! \brief Apply function which finds or allocates a state structure */
1673 static int sip_outbound_registration_apply(const struct ast_sorcery *sorcery, void *obj)
1674 {
1675         RAII_VAR(struct ao2_container *, states, ao2_global_obj_ref(current_states), ao2_cleanup);
1676         RAII_VAR(struct sip_outbound_registration_state *, state, NULL, ao2_cleanup);
1677         RAII_VAR(struct sip_outbound_registration_state *, new_state, NULL, ao2_cleanup);
1678         struct sip_outbound_registration *applied = obj;
1679
1680         if (!states) {
1681                 /* Global container has gone.  Likely shutting down. */
1682                 return -1;
1683         }
1684         state = ao2_find(states, ast_sorcery_object_get_id(applied), OBJ_SEARCH_KEY);
1685
1686         ast_debug(4, "Applying configuration to outbound registration '%s'\n", ast_sorcery_object_get_id(applied));
1687
1688         if (ast_strlen_zero(applied->server_uri)) {
1689                 ast_log(LOG_ERROR, "No server URI specified on outbound registration '%s'\n",
1690                         ast_sorcery_object_get_id(applied));
1691                 return -1;
1692         } else if (ast_sip_validate_uri_length(applied->server_uri)) {
1693                         ast_log(LOG_ERROR, "Server URI or hostname length exceeds pjproject limit or is not a sip(s) uri: '%s'\n",
1694                                 ast_sorcery_object_get_id(applied));
1695                         return -1;
1696         } else if (ast_strlen_zero(applied->client_uri)) {
1697                 ast_log(LOG_ERROR, "No client URI specified on outbound registration '%s'\n",
1698                         ast_sorcery_object_get_id(applied));
1699                 return -1;
1700         } else if (ast_sip_validate_uri_length(applied->client_uri)) {
1701                         ast_log(LOG_ERROR, "Client URI or hostname length exceeds pjproject limit or is not a sip(s) uri: '%s'\n",
1702                                 ast_sorcery_object_get_id(applied));
1703                         return -1;
1704         } else if (applied->line && ast_strlen_zero(applied->endpoint)) {
1705                 ast_log(LOG_ERROR, "Line support has been enabled on outbound registration '%s' without providing an endpoint\n",
1706                         ast_sorcery_object_get_id(applied));
1707                 return -1;
1708         } else if (!ast_strlen_zero(applied->endpoint) && !applied->line) {
1709                 ast_log(LOG_ERROR, "An endpoint has been specified on outbound registration '%s' without enabling line support\n",
1710                         ast_sorcery_object_get_id(applied));
1711                 return -1;
1712         }
1713
1714         if (state && can_reuse_registration(state->registration, applied)) {
1715                 ast_debug(4,
1716                         "No change between old configuration and new configuration on outbound registration '%s'. Using previous state\n",
1717                         ast_sorcery_object_get_id(applied));
1718
1719                 /*
1720                  * This is OK to replace without relinking the state in the
1721                  * current_states container since state->registration and
1722                  * applied have the same key.
1723                  */
1724                 ao2_lock(states);
1725                 ao2_replace(state->registration, applied);
1726                 ao2_unlock(states);
1727                 return 0;
1728         }
1729
1730         if (!(new_state = sip_outbound_registration_state_alloc(applied))) {
1731                 return -1;
1732         }
1733
1734         if (ast_sip_push_task_wait_serializer(new_state->client_state->serializer,
1735                 sip_outbound_registration_regc_alloc, new_state)) {
1736                 return -1;
1737         }
1738
1739         if (ast_sip_push_task(new_state->client_state->serializer,
1740                               sip_outbound_registration_perform, ao2_bump(new_state))) {
1741                 ast_log(LOG_ERROR, "Failed to perform outbound registration on '%s'\n",
1742                         ast_sorcery_object_get_id(new_state->registration));
1743                 ao2_ref(new_state, -1);
1744                 return -1;
1745         }
1746
1747         ao2_lock(states);
1748         if (state) {
1749                 ao2_unlink(states, state);
1750         }
1751         ao2_link(states, new_state);
1752         ao2_unlock(states);
1753
1754         return 0;
1755 }
1756
1757 static int outbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
1758 {
1759         struct sip_outbound_registration *registration = obj;
1760
1761         return ast_sip_auth_vector_init(&registration->outbound_auths, var->value);
1762 }
1763
1764 static int outbound_auths_to_str(const void *obj, const intptr_t *args, char **buf)
1765 {
1766         const struct sip_outbound_registration *registration = obj;
1767
1768         return ast_sip_auths_to_str(&registration->outbound_auths, buf);
1769 }
1770
1771 static int outbound_auths_to_var_list(const void *obj, struct ast_variable **fields)
1772 {
1773         const struct sip_outbound_registration *registration = obj;
1774         int i;
1775         struct ast_variable *head = NULL;
1776
1777         for (i = 0; i < AST_VECTOR_SIZE(&registration->outbound_auths) ; i++) {
1778                 ast_variable_list_append(&head, ast_variable_new("outbound_auth",
1779                         AST_VECTOR_GET(&registration->outbound_auths, i), ""));
1780         }
1781
1782         if (head) {
1783                 *fields = head;
1784         }
1785
1786         return 0;
1787 }
1788
1789 static int unregister_task(void *obj)
1790 {
1791         struct sip_outbound_registration_state *state = obj;
1792         struct pjsip_regc *client = state->client_state->client;
1793         pjsip_tx_data *tdata;
1794         pjsip_regc_info info;
1795
1796         pjsip_regc_get_info(client, &info);
1797         ast_debug(1, "Unregistering contacts with server '%s' from client '%s'\n",
1798                 state->registration->server_uri, state->registration->client_uri);
1799
1800         cancel_registration(state->client_state);
1801
1802         if (pjsip_regc_unregister(client, &tdata) == PJ_SUCCESS
1803                 && add_configured_supported_headers(state->client_state, tdata)) {
1804                 registration_client_send(state->client_state, tdata);
1805         }
1806
1807         ao2_ref(state, -1);
1808         return 0;
1809 }
1810
1811 static int queue_unregister(struct sip_outbound_registration_state *state)
1812 {
1813         ao2_ref(state, +1);
1814         if (ast_sip_push_task(state->client_state->serializer, unregister_task, state)) {
1815                 ao2_ref(state, -1);
1816                 return -1;
1817         }
1818
1819         return 0;
1820 }
1821
1822 static int queue_register(struct sip_outbound_registration_state *state)
1823 {
1824         ao2_ref(state, +1);
1825         if (ast_sip_push_task(state->client_state->serializer, sip_outbound_registration_perform, state)) {
1826                 ao2_ref(state, -1);
1827                 return -1;
1828         }
1829
1830         return 0;
1831 }
1832
1833 static void unregister_all(void)
1834 {
1835         struct ao2_container *states;
1836
1837         states = ao2_global_obj_ref(current_states);
1838         if (!states) {
1839                 return;
1840         }
1841
1842         /* Clean out all the states and let sorcery handle recreating the registrations */
1843         ao2_callback(states, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
1844         ao2_ref(states, -1);
1845 }
1846
1847 static void reregister_all(void)
1848 {
1849         unregister_all();
1850         ast_sorcery_load_object(ast_sip_get_sorcery(), "registration");
1851 }
1852
1853 static char *cli_complete_registration(const char *line, const char *word,
1854                                        int pos, int state)
1855 {
1856         char *result = NULL;
1857         int wordlen;
1858         int which = 0;
1859         struct sip_outbound_registration *registration;
1860         struct ao2_container *registrations;
1861         struct ao2_iterator i;
1862
1863         if (pos != 3) {
1864                 return NULL;
1865         }
1866
1867         wordlen = strlen(word);
1868         if (wordlen == 0 && ++which > state) {
1869                 return ast_strdup("*all");
1870         }
1871
1872         registrations = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "registration",
1873                 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
1874         if (!registrations) {
1875                 return NULL;
1876         }
1877
1878         i = ao2_iterator_init(registrations, 0);
1879         while ((registration = ao2_iterator_next(&i))) {
1880                 const char *name = ast_sorcery_object_get_id(registration);
1881
1882                 if (!strncasecmp(word, name, wordlen) && ++which > state) {
1883                         result = ast_strdup(name);
1884                 }
1885
1886                 ao2_ref(registration, -1);
1887                 if (result) {
1888                         break;
1889                 }
1890         }
1891         ao2_iterator_destroy(&i);
1892
1893         ao2_ref(registrations, -1);
1894         return result;
1895 }
1896
1897 static char *cli_unregister(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1898 {
1899         struct sip_outbound_registration_state *state;
1900         const char *registration_name;
1901
1902         switch (cmd) {
1903         case CLI_INIT:
1904                 e->command = "pjsip send unregister";
1905                 e->usage =
1906                         "Usage: pjsip send unregister <registration> | *all\n"
1907                         "       Unregisters the specified (or all) outbound registration(s) "
1908                         "and stops future registration attempts.\n";
1909                 return NULL;
1910         case CLI_GENERATE:
1911                 return cli_complete_registration(a->line, a->word, a->pos, a->n);
1912         }
1913
1914         if (a->argc != 4) {
1915                 return CLI_SHOWUSAGE;
1916         }
1917
1918         registration_name = a->argv[3];
1919
1920         if (strcmp(registration_name, "*all") == 0) {
1921                 unregister_all();
1922                 ast_cli(a->fd, "Unregister all queued\n");
1923                 return CLI_SUCCESS;
1924         }
1925
1926         state = get_state(registration_name);
1927         if (!state) {
1928                 ast_cli(a->fd, "Unable to retrieve registration %s\n", registration_name);
1929                 return CLI_FAILURE;
1930         }
1931
1932         if (queue_unregister(state)) {
1933                 ast_cli(a->fd, "Failed to queue unregistration\n");
1934         }
1935
1936         ao2_ref(state, -1);
1937         return CLI_SUCCESS;
1938 }
1939
1940 static char *cli_register(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1941 {
1942         struct sip_outbound_registration_state *state;
1943         const char *registration_name;
1944
1945         switch (cmd) {
1946         case CLI_INIT:
1947                 e->command = "pjsip send register";
1948                 e->usage =
1949                         "Usage: pjsip send register <registration> | *all \n"
1950                         "       Unregisters the specified (or all) outbound "
1951                         "registration(s) then starts registration(s) and schedules re-registrations.\n";
1952                 return NULL;
1953         case CLI_GENERATE:
1954                 return cli_complete_registration(a->line, a->word, a->pos, a->n);
1955         }
1956
1957         if (a->argc != 4) {
1958                 return CLI_SHOWUSAGE;
1959         }
1960
1961         registration_name = a->argv[3];
1962
1963         if (strcmp(registration_name, "*all") == 0) {
1964                 reregister_all();
1965                 ast_cli(a->fd, "Re-register all queued\n");
1966                 return CLI_SUCCESS;
1967         }
1968
1969         state = get_state(registration_name);
1970         if (!state) {
1971                 ast_cli(a->fd, "Unable to retrieve registration %s\n", registration_name);
1972                 return CLI_FAILURE;
1973         }
1974
1975         /* We need to serialize the unregister and register so they need
1976          * to be queued as separate tasks.
1977          */
1978         if (queue_unregister(state)) {
1979                 ast_cli(a->fd, "Failed to queue unregistration\n");
1980         } else if (queue_register(state)) {
1981                 ast_cli(a->fd, "Failed to queue registration\n");
1982         }
1983
1984         ao2_ref(state, -1);
1985         return CLI_SUCCESS;
1986 }
1987
1988 static int ami_unregister(struct mansession *s, const struct message *m)
1989 {
1990         const char *registration_name = astman_get_header(m, "Registration");
1991         struct sip_outbound_registration_state *state;
1992
1993         if (ast_strlen_zero(registration_name)) {
1994                 astman_send_error(s, m, "Registration parameter missing.");
1995                 return 0;
1996         }
1997
1998         if (strcmp(registration_name, "*all") == 0) {
1999                 unregister_all();
2000                 astman_send_ack(s, m, "Unregistrations queued.");
2001                 return 0;
2002         }
2003
2004         state = get_state(registration_name);
2005         if (!state) {
2006                 astman_send_error(s, m, "Unable to retrieve registration entry\n");
2007                 return 0;
2008         }
2009
2010         if (queue_unregister(state)) {
2011                 astman_send_ack(s, m, "Failed to queue unregistration");
2012         } else {
2013                 astman_send_ack(s, m, "Unregistration sent");
2014         }
2015
2016         ao2_ref(state, -1);
2017         return 0;
2018 }
2019
2020 static int ami_register(struct mansession *s, const struct message *m)
2021 {
2022         const char *registration_name = astman_get_header(m, "Registration");
2023         struct sip_outbound_registration_state *state;
2024
2025         if (ast_strlen_zero(registration_name)) {
2026                 astman_send_error(s, m, "Registration parameter missing.");
2027                 return 0;
2028         }
2029
2030         if (strcmp(registration_name, "*all") == 0) {
2031                 reregister_all();
2032                 astman_send_ack(s, m, "Reregistrations queued.");
2033                 return 0;
2034         }
2035
2036         state = get_state(registration_name);
2037         if (!state) {
2038                 astman_send_error(s, m, "Unable to retrieve registration entry\n");
2039                 return 0;
2040         }
2041
2042         /* We need to serialize the unregister and register so they need
2043          * to be queued as separate tasks.
2044          */
2045         if (queue_unregister(state)) {
2046                 astman_send_ack(s, m, "Failed to queue unregistration");
2047         } else if (queue_register(state)) {
2048                 astman_send_ack(s, m, "Failed to queue unregistration");
2049         } else {
2050                 astman_send_ack(s, m, "Reregistration sent");
2051         }
2052
2053         ao2_ref(state, -1);
2054         return 0;
2055 }
2056
2057 struct sip_ami_outbound {
2058         struct ast_sip_ami *ami;
2059         int registered;
2060         int not_registered;
2061         struct sip_outbound_registration *registration;
2062 };
2063
2064 static int ami_outbound_registration_task(void *obj)
2065 {
2066         struct sip_ami_outbound *ami = obj;
2067         struct ast_str *buf;
2068         struct sip_outbound_registration_state *state;
2069
2070         buf = ast_sip_create_ami_event("OutboundRegistrationDetail", ami->ami);
2071         if (!buf) {
2072                 return -1;
2073         }
2074
2075         ast_sip_sorcery_object_to_ami(ami->registration, &buf);
2076
2077         if ((state = get_state(ast_sorcery_object_get_id(ami->registration)))) {
2078                 pjsip_regc_info info;
2079
2080                 if (state->client_state->status == SIP_REGISTRATION_REGISTERED) {
2081                         ++ami->registered;
2082                 } else {
2083                         ++ami->not_registered;
2084                 }
2085
2086                 ast_str_append(&buf, 0, "Status: %s\r\n",
2087                         sip_outbound_registration_status_str(state->client_state->status));
2088
2089                 pjsip_regc_get_info(state->client_state->client, &info);
2090                 ast_str_append(&buf, 0, "NextReg: %d\r\n", info.next_reg);
2091                 ao2_ref(state, -1);
2092         }
2093
2094         astman_append(ami->ami->s, "%s\r\n", ast_str_buffer(buf));
2095         ast_free(buf);
2096
2097         return ast_sip_format_auths_ami(&ami->registration->outbound_auths, ami->ami);
2098 }
2099
2100 static int ami_outbound_registration_detail(void *obj, void *arg, int flags)
2101 {
2102         struct sip_ami_outbound *ami = arg;
2103
2104         ami->registration = obj;
2105         return ast_sip_push_task_wait_servant(NULL, ami_outbound_registration_task, ami);
2106 }
2107
2108 static int ami_show_outbound_registrations(struct mansession *s,
2109                                            const struct message *m)
2110 {
2111         struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"), };
2112         struct sip_ami_outbound ami_outbound = { .ami = &ami };
2113         struct ao2_container *regs;
2114
2115         regs = get_registrations();
2116         if (!regs) {
2117                 astman_send_error(s, m, "Unable to retrieve "
2118                                   "outbound registrations\n");
2119                 return -1;
2120         }
2121
2122         astman_send_listack(s, m, "Following are Events for each Outbound registration",
2123                 "start");
2124
2125         ao2_callback(regs, OBJ_NODATA, ami_outbound_registration_detail, &ami_outbound);
2126
2127         astman_send_list_complete_start(s, m, "OutboundRegistrationDetailComplete",
2128                 ami_outbound.registered + ami_outbound.not_registered);
2129         astman_append(s,
2130                 "Registered: %d\r\n"
2131                 "NotRegistered: %d\r\n",
2132                 ami_outbound.registered,
2133                 ami_outbound.not_registered);
2134         astman_send_list_complete_end(s);
2135
2136         ao2_ref(regs, -1);
2137         return 0;
2138 }
2139
2140 static struct ao2_container *cli_get_container(const char *regex)
2141 {
2142         RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup);
2143         struct ao2_container *s_container;
2144
2145         container = ast_sorcery_retrieve_by_regex(ast_sip_get_sorcery(), "registration", regex);
2146         if (!container) {
2147                 return NULL;
2148         }
2149
2150         s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
2151                 ast_sorcery_object_id_sort, ast_sorcery_object_id_compare);
2152         if (!s_container) {
2153                 return NULL;
2154         }
2155
2156         if (ao2_container_dup(s_container, container, 0)) {
2157                 ao2_ref(s_container, -1);
2158                 return NULL;
2159         }
2160
2161         return s_container;
2162 }
2163
2164 static int cli_iterator(void *container, ao2_callback_fn callback, void *args)
2165 {
2166         ao2_callback(container, OBJ_NODATA, callback, args);
2167
2168         return 0;
2169 }
2170
2171 static void *cli_retrieve_by_id(const char *id)
2172 {
2173         struct ao2_container *states;
2174         void *obj = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "registration", id);
2175
2176         if (!obj) {
2177                 /* if the object no longer exists then remove its state  */
2178                 states = ao2_global_obj_ref(current_states);
2179                 if (states) {
2180                         ao2_find(states, id, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
2181                         ao2_ref(states, -1);
2182                 }
2183         }
2184
2185         return obj;
2186 }
2187
2188 static int cli_print_header(void *obj, void *arg, int flags)
2189 {
2190         struct ast_sip_cli_context *context = arg;
2191
2192         ast_assert(context->output_buffer != NULL);
2193
2194         ast_str_append(&context->output_buffer, 0,
2195                 " <Registration/ServerURI..............................>  <Auth..........>  <Status.......>\n");
2196
2197         return 0;
2198 }
2199
2200 static int cli_print_body(void *obj, void *arg, int flags)
2201 {
2202         struct sip_outbound_registration *registration = obj;
2203         struct ast_sip_cli_context *context = arg;
2204         const char *id = ast_sorcery_object_get_id(registration);
2205         struct sip_outbound_registration_state *state = get_state(id);
2206 #define REGISTRATION_URI_FIELD_LEN      53
2207
2208         ast_assert(context->output_buffer != NULL);
2209
2210         ast_str_append(&context->output_buffer, 0, " %-s/%-*.*s  %-16s  %-16s\n",
2211                 id,
2212                 (int) (REGISTRATION_URI_FIELD_LEN - strlen(id)),
2213                 (int) (REGISTRATION_URI_FIELD_LEN - strlen(id)),
2214                 registration->server_uri,
2215                 AST_VECTOR_SIZE(&registration->outbound_auths)
2216                         ? AST_VECTOR_GET(&registration->outbound_auths, 0)
2217                         : "n/a",
2218                 (state ? sip_outbound_registration_status_str(state->client_state->status) : "Unregistered"));
2219         ao2_cleanup(state);
2220
2221         if (context->show_details
2222                 || (context->show_details_only_level_0 && context->indent_level == 0)) {
2223                 ast_str_append(&context->output_buffer, 0, "\n");
2224                 ast_sip_cli_print_sorcery_objectset(registration, context, 0);
2225         }
2226
2227         return 0;
2228 }
2229
2230 /*
2231  * A function pointer to callback needs to be within the
2232  * module in order to avoid problems with an undefined
2233  * symbol when the module is loaded.
2234  */
2235 static char *my_cli_traverse_objects(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2236 {
2237         return ast_sip_cli_traverse_objects(e, cmd, a);
2238 }
2239
2240 static struct ast_cli_entry cli_outbound_registration[] = {
2241         AST_CLI_DEFINE(cli_unregister, "Unregisters outbound registration target"),
2242         AST_CLI_DEFINE(cli_register, "Registers an outbound registration target"),
2243         AST_CLI_DEFINE(my_cli_traverse_objects, "List PJSIP Registrations",
2244                 .command = "pjsip list registrations",
2245                 .usage = "Usage: pjsip list registrations [ like <pattern> ]\n"
2246                                 "       List the configured PJSIP Registrations\n"
2247                                 "       Optional regular expression pattern is used to filter the list.\n"),
2248         AST_CLI_DEFINE(my_cli_traverse_objects, "Show PJSIP Registrations",
2249                 .command = "pjsip show registrations",
2250                 .usage = "Usage: pjsip show registrations [ like <pattern> ]\n"
2251                                 "       Show the configured PJSIP Registrations\n"
2252                                 "       Optional regular expression pattern is used to filter the list.\n"),
2253         AST_CLI_DEFINE(my_cli_traverse_objects, "Show PJSIP Registration",
2254                 .command = "pjsip show registration",
2255                 .usage = "Usage: pjsip show registration <id>\n"
2256                                  "       Show the configured PJSIP Registration\n"),
2257 };
2258
2259 static struct ast_sip_cli_formatter_entry *cli_formatter;
2260
2261 static void auth_observer(const char *type)
2262 {
2263         struct sip_outbound_registration *registration;
2264         struct sip_outbound_registration_state *state;
2265         struct ao2_container *regs;
2266         const char *registration_id;
2267         struct ao2_iterator i;
2268
2269         ast_debug(4, "Auths updated. Checking for any outbound registrations that are in permanent rejected state so they can be retried\n");
2270
2271         regs = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "registration",
2272                 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
2273         if (!regs || ao2_container_count(regs) == 0) {
2274                 ao2_cleanup(regs);
2275                 return;
2276         }
2277
2278         i = ao2_iterator_init(regs, 0);
2279         for (; (registration = ao2_iterator_next(&i)); ao2_ref(registration, -1)) {
2280                 registration_id = ast_sorcery_object_get_id(registration);
2281                 state = get_state(registration_id);
2282                 if (state && state->client_state->status == SIP_REGISTRATION_REJECTED_PERMANENT) {
2283                         ast_debug(4, "Trying outbound registration '%s' again\n", registration_id);
2284
2285                         if (ast_sip_push_task(state->client_state->serializer,
2286                                               sip_outbound_registration_perform, ao2_bump(state))) {
2287                                 ast_log(LOG_ERROR, "Failed to perform outbound registration on '%s'\n", registration_id);
2288                                 ao2_ref(state, -1);
2289                         }
2290                 }
2291                 ao2_cleanup(state);
2292         }
2293         ao2_iterator_destroy(&i);
2294         ao2_cleanup(regs);
2295 }
2296
2297 static const struct ast_sorcery_observer observer_callbacks_auth = {
2298         .loaded = auth_observer,
2299 };
2300
2301 static int check_state(void *obj, void *arg, int flags)
2302 {
2303         struct sip_outbound_registration_state *state = obj;
2304         struct sip_outbound_registration *registration;
2305
2306         registration = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "registration",
2307                 ast_sorcery_object_get_id(state->registration));
2308         if (!registration) {
2309                 /* This is a dead registration */
2310                 return CMP_MATCH;
2311         }
2312
2313         ao2_ref(registration, -1);
2314         return 0;
2315 }
2316
2317 /*!
2318  * \internal
2319  * \brief Observer to purge dead registration states.
2320  *
2321  * \param name Module name owning the sorcery instance.
2322  * \param sorcery Instance being observed.
2323  * \param object_type Name of object being observed.
2324  * \param reloaded Non-zero if the object is being reloaded.
2325  *
2326  * \return Nothing
2327  */
2328 static void registration_loaded_observer(const char *name, const struct ast_sorcery *sorcery, const char *object_type, int reloaded)
2329 {
2330         struct ao2_container *states;
2331
2332         if (strcmp(object_type, "registration")) {
2333                 /* Not interested */
2334                 return;
2335         }
2336
2337         states = ao2_global_obj_ref(current_states);
2338         if (!states) {
2339                 /* Global container has gone.  Likely shutting down. */
2340                 return;
2341         }
2342
2343         /*
2344          * Refresh the current configured registrations. We don't need to hold
2345          * onto the objects, as the apply handler will cause their states to
2346          * be created appropriately.
2347          */
2348         ao2_cleanup(get_registrations());
2349
2350         /* Now to purge dead registrations. */
2351         ao2_callback(states, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, check_state, NULL);
2352         ao2_ref(states, -1);
2353 }
2354
2355 static const struct ast_sorcery_instance_observer observer_callbacks_registrations = {
2356         .object_type_loaded = registration_loaded_observer,
2357 };
2358
2359 static void registration_deleted_observer(const void *obj)
2360 {
2361         const struct sip_outbound_registration *registration = obj;
2362         struct ao2_container *states;
2363
2364         states = ao2_global_obj_ref(current_states);
2365         if (!states) {
2366                 /* Global container has gone.  Likely shutting down. */
2367                 return;
2368         }
2369
2370         ao2_find(states, ast_sorcery_object_get_id(registration), OBJ_UNLINK | OBJ_NODATA | OBJ_SEARCH_KEY);
2371
2372         ao2_ref(states, -1);
2373 }
2374
2375 static const struct ast_sorcery_observer registration_observer = {
2376         .deleted = registration_deleted_observer,
2377 };
2378
2379 static void network_change_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
2380 {
2381         /* This callback is only concerned with network change messages from the system topic. */
2382         if (stasis_message_type(message) != ast_network_change_type()) {
2383                 return;
2384         }
2385         ast_debug(3, "Received network change event\n");
2386
2387         reregister_all();
2388 }
2389
2390 static int unload_module(void)
2391 {
2392         int remaining;
2393
2394         network_change_sub = stasis_unsubscribe_and_join(network_change_sub);
2395
2396         ast_manager_unregister("PJSIPShowRegistrationsOutbound");
2397         ast_manager_unregister("PJSIPUnregister");
2398         ast_manager_unregister("PJSIPRegister");
2399
2400         ast_cli_unregister_multiple(cli_outbound_registration, ARRAY_LEN(cli_outbound_registration));
2401         ast_sip_unregister_cli_formatter(cli_formatter);
2402         cli_formatter = NULL;
2403
2404         ast_sip_unregister_endpoint_identifier(&line_identifier);
2405
2406         ast_sorcery_observer_remove(ast_sip_get_sorcery(), "auth", &observer_callbacks_auth);
2407         ast_sorcery_instance_observer_remove(ast_sip_get_sorcery(), &observer_callbacks_registrations);
2408
2409         ast_sorcery_object_unregister(ast_sip_get_sorcery(), "registration");
2410
2411         ao2_global_obj_release(current_states);
2412
2413         ast_sip_transport_monitor_unregister_all(registration_transport_shutdown_cb, NULL, NULL);
2414
2415         /* Wait for registration serializers to get destroyed. */
2416         ast_debug(2, "Waiting for registration transactions to complete for unload.\n");
2417         remaining = ast_serializer_shutdown_group_join(shutdown_group, MAX_UNLOAD_TIMEOUT_TIME);
2418         if (remaining) {
2419                 /*
2420                  * NOTE: We probably have a sip_outbound_registration_client_state
2421                  * ref leak if the remaining count cannot reach zero after a few
2422                  * minutes of trying to unload.
2423                  */
2424                 ast_log(LOG_WARNING, "Unload incomplete.  Could not stop %d outbound registrations.  Try again later.\n",
2425                         remaining);
2426                 return -1;
2427         }
2428
2429         ast_debug(2, "Successful shutdown.\n");
2430
2431         ao2_cleanup(shutdown_group);
2432         shutdown_group = NULL;
2433
2434         return 0;
2435 }
2436
2437 static int load_module(void)
2438 {
2439         struct ao2_container *new_states;
2440
2441         shutdown_group = ast_serializer_shutdown_group_alloc();
2442         if (!shutdown_group) {
2443                 return AST_MODULE_LOAD_DECLINE;
2444         }
2445
2446         /* Create outbound registration states container. */
2447         new_states = ao2_container_alloc(DEFAULT_STATE_BUCKETS,
2448                 registration_state_hash, registration_state_cmp);
2449         if (!new_states) {
2450                 ast_log(LOG_ERROR, "Unable to allocate registration states container\n");
2451                 unload_module();
2452                 return AST_MODULE_LOAD_DECLINE;
2453         }
2454         ao2_global_obj_replace_unref(current_states, new_states);
2455         ao2_ref(new_states, -1);
2456
2457         /*
2458          * Register sorcery object descriptions.
2459          */
2460         ast_sorcery_apply_config(ast_sip_get_sorcery(), "res_pjsip_outbound_registration");
2461         ast_sorcery_apply_default(ast_sip_get_sorcery(), "registration", "config", "pjsip.conf,criteria=type=registration");
2462
2463         if (ast_sorcery_object_register(ast_sip_get_sorcery(), "registration", sip_outbound_registration_alloc, NULL, sip_outbound_registration_apply)) {
2464                 unload_module();
2465                 return AST_MODULE_LOAD_DECLINE;
2466         }
2467
2468         ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "type", "", OPT_NOOP_T, 0, 0);
2469         ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "server_uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, server_uri));
2470         ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "client_uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, client_uri));
2471         ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "contact_user", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, contact_user));
2472         ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "contact_header_params", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, contact_header_params));
2473         ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, transport));
2474         ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, outbound_proxy));
2475         ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "expiration", "3600", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, expiration));
2476         ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "retry_interval", "60", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, retry_interval));
2477         ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "forbidden_retry_interval", "0", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, forbidden_retry_interval));
2478         ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "fatal_retry_interval", "0", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, fatal_retry_interval));
2479         ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "max_retries", "10", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, max_retries));
2480         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));
2481         ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "registration", "outbound_auth", "", outbound_auth_handler, outbound_auths_to_str, outbound_auths_to_var_list, 0, 0);
2482         ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "support_path", "no", OPT_BOOL_T, 1, FLDSET(struct sip_outbound_registration, support_path));
2483         ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "support_outbound", "no", OPT_YESNO_T, 1, FLDSET(struct sip_outbound_registration, support_outbound));
2484         ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "line", "no", OPT_BOOL_T, 1, FLDSET(struct sip_outbound_registration, line));
2485         ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "endpoint", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, endpoint));
2486
2487         /*
2488          * Register sorcery observers.
2489          */
2490         if (ast_sorcery_instance_observer_add(ast_sip_get_sorcery(),
2491                 &observer_callbacks_registrations)
2492                 || ast_sorcery_observer_add(ast_sip_get_sorcery(), "auth",
2493                         &observer_callbacks_auth)
2494                 || ast_sorcery_observer_add(ast_sip_get_sorcery(), "registration",
2495                         &registration_observer)) {
2496                 ast_log(LOG_ERROR, "Unable to register observers.\n");
2497                 unload_module();
2498                 return AST_MODULE_LOAD_DECLINE;
2499         }
2500
2501         /* Register how this module identifies endpoints. */
2502         ast_sip_register_endpoint_identifier(&line_identifier);
2503
2504         /* Register CLI commands. */
2505         cli_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
2506         if (!cli_formatter) {
2507                 ast_log(LOG_ERROR, "Unable to allocate memory for cli formatter\n");
2508                 unload_module();
2509                 return AST_MODULE_LOAD_DECLINE;
2510         }
2511         cli_formatter->name = "registration";
2512         cli_formatter->print_header = cli_print_header;
2513         cli_formatter->print_body = cli_print_body;
2514         cli_formatter->get_container = cli_get_container;
2515         cli_formatter->iterate = cli_iterator;
2516         cli_formatter->get_id = ast_sorcery_object_get_id;
2517         cli_formatter->retrieve_by_id = cli_retrieve_by_id;
2518         ast_sip_register_cli_formatter(cli_formatter);
2519         ast_cli_register_multiple(cli_outbound_registration, ARRAY_LEN(cli_outbound_registration));
2520
2521         /* Register AMI actions. */
2522         ast_manager_register_xml("PJSIPUnregister", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, ami_unregister);
2523         ast_manager_register_xml("PJSIPRegister", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, ami_register);
2524         ast_manager_register_xml("PJSIPShowRegistrationsOutbound", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, ami_show_outbound_registrations);
2525
2526         /* Clear any previous statsd gauges in case we weren't shutdown cleanly */
2527         ast_statsd_log("PJSIP.registrations.count", AST_STATSD_GAUGE, 0);
2528         ast_statsd_log("PJSIP.registrations.state.Registered", AST_STATSD_GAUGE, 0);
2529         ast_statsd_log("PJSIP.registrations.state.Unregistered", AST_STATSD_GAUGE, 0);
2530         ast_statsd_log("PJSIP.registrations.state.Rejected", AST_STATSD_GAUGE, 0);
2531
2532         /* Load configuration objects */
2533         ast_sorcery_load_object(ast_sip_get_sorcery(), "registration");
2534
2535         network_change_sub = stasis_subscribe(ast_system_topic(),
2536                 network_change_stasis_cb, NULL);
2537
2538         return AST_MODULE_LOAD_SUCCESS;
2539 }
2540
2541 static int reload_module(void)
2542 {
2543         ast_sorcery_reload_object(ast_sip_get_sorcery(), "registration");
2544         return 0;
2545 }
2546
2547 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Outbound Registration Support",
2548         .support_level = AST_MODULE_SUPPORT_CORE,
2549         .load = load_module,
2550         .reload = reload_module,
2551         .unload = unload_module,
2552         .load_pri = AST_MODPRI_APP_DEPEND,
2553         .requires = "res_pjsip",
2554         .optional_modules = "res_statsd",
2555 );