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