| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (C) 2013, Digium, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Joshua Colp <jcolp@digium.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * See http://www.asterisk.org for more information about
 | 
					
						
							|  |  |  |  * the Asterisk project. Please do not directly contact | 
					
						
							|  |  |  |  * any of the maintainers of this project for assistance; | 
					
						
							|  |  |  |  * the project provides a web site, mailing lists and IRC | 
					
						
							|  |  |  |  * channels for your use. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software, distributed under the terms of | 
					
						
							|  |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*** MODULEINFO
 | 
					
						
							| 
									
										
										
										
											2013-04-26 21:52:06 +00:00
										 |  |  | 	<depend>pjproject</depend> | 
					
						
							| 
									
										
										
										
											2013-07-30 18:14:50 +00:00
										 |  |  | 	<depend>res_pjsip</depend> | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	<support_level>core</support_level> | 
					
						
							|  |  |  |  ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <pjsip.h>
 | 
					
						
							|  |  |  | #include <pjsip_ua.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-30 18:14:50 +00:00
										 |  |  | #include "asterisk/res_pjsip.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | #include "asterisk/res_pjsip_cli.h"
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | #include "asterisk/module.h"
 | 
					
						
							|  |  |  | #include "asterisk/taskprocessor.h"
 | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | #include "asterisk/cli.h"
 | 
					
						
							| 
									
										
										
										
											2013-08-05 19:01:45 +00:00
										 |  |  | #include "asterisk/stasis_system.h"
 | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | #include "res_pjsip/include/res_pjsip_private.h"
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-19 17:45:42 +00:00
										 |  |  | /*** DOCUMENTATION
 | 
					
						
							| 
									
										
										
										
											2013-07-30 18:14:50 +00:00
										 |  |  | 	<configInfo name="res_pjsip_outbound_registration" language="en_US"> | 
					
						
							| 
									
										
										
										
											2013-05-19 17:45:42 +00:00
										 |  |  | 		<synopsis>SIP resource for outbound registrations</synopsis> | 
					
						
							|  |  |  | 		<description><para> | 
					
						
							|  |  |  | 			<emphasis>Outbound Registration</emphasis> | 
					
						
							|  |  |  | 			</para> | 
					
						
							| 
									
										
										
										
											2013-07-30 18:14:50 +00:00
										 |  |  | 			<para>This module allows <literal>res_pjsip</literal> to register to other SIP servers.</para> | 
					
						
							| 
									
										
										
										
											2013-05-19 17:45:42 +00:00
										 |  |  | 		</description> | 
					
						
							| 
									
										
										
										
											2013-07-30 18:14:50 +00:00
										 |  |  | 		<configFile name="pjsip.conf"> | 
					
						
							| 
									
										
										
										
											2013-05-19 17:45:42 +00:00
										 |  |  | 			<configObject name="registration"> | 
					
						
							|  |  |  | 				<synopsis>The configuration for outbound registration</synopsis> | 
					
						
							|  |  |  | 				<description><para> | 
					
						
							|  |  |  | 					Registration is <emphasis>COMPLETELY</emphasis> separate from the rest of | 
					
						
							| 
									
										
										
										
											2013-07-30 18:14:50 +00:00
										 |  |  | 					<literal>pjsip.conf</literal>. A minimal configuration consists of | 
					
						
							| 
									
										
										
										
											2013-05-19 17:45:42 +00:00
										 |  |  | 					setting a <literal>server_uri</literal>	and a <literal>client_uri</literal>. | 
					
						
							|  |  |  | 				</para></description> | 
					
						
							|  |  |  | 				<configOption name="auth_rejection_permanent" default="yes"> | 
					
						
							|  |  |  | 					<synopsis>Determines whether failed authentication challenges are treated | 
					
						
							|  |  |  | 					as permanent failures.</synopsis> | 
					
						
							|  |  |  | 					<description><para>If this option is enabled and an authentication challenge fails, | 
					
						
							|  |  |  | 					registration will not be attempted again until the configuration is reloaded.</para></description> | 
					
						
							|  |  |  | 				</configOption> | 
					
						
							|  |  |  | 				<configOption name="client_uri"> | 
					
						
							|  |  |  | 					<synopsis>Client SIP URI used when attemping outbound registration</synopsis> | 
					
						
							| 
									
										
										
										
											2013-09-05 14:10:45 +00:00
										 |  |  | 					<description><para> | 
					
						
							|  |  |  | 						This is the address-of-record for the outbound registration (i.e. the URI in | 
					
						
							|  |  |  | 						the To header of the REGISTER).</para> | 
					
						
							|  |  |  | 						<para>For registration with an ITSP, the client SIP URI may need to consist of | 
					
						
							|  |  |  | 						an account name or number and the provider's hostname for their registrar, e.g. | 
					
						
							|  |  |  | 						client_uri=1234567890@example.com. This may differ between providers.</para> | 
					
						
							|  |  |  | 						<para>For registration to generic registrars, the client SIP URI will depend | 
					
						
							|  |  |  | 						on networking specifics and configuration of the registrar. | 
					
						
							|  |  |  | 					</para></description> | 
					
						
							| 
									
										
										
										
											2013-05-19 17:45:42 +00:00
										 |  |  | 				</configOption> | 
					
						
							|  |  |  | 				<configOption name="contact_user"> | 
					
						
							|  |  |  | 					<synopsis>Contact User to use in request</synopsis> | 
					
						
							|  |  |  | 				</configOption> | 
					
						
							|  |  |  | 				<configOption name="expiration" default="3600"> | 
					
						
							|  |  |  | 					<synopsis>Expiration time for registrations in seconds</synopsis> | 
					
						
							|  |  |  | 				</configOption> | 
					
						
							|  |  |  | 				<configOption name="max_retries" default="10"> | 
					
						
							|  |  |  | 					<synopsis>Maximum number of registration attempts.</synopsis> | 
					
						
							|  |  |  | 				</configOption> | 
					
						
							|  |  |  | 				<configOption name="outbound_auth" default=""> | 
					
						
							|  |  |  | 					<synopsis>Authentication object to be used for outbound registrations.</synopsis> | 
					
						
							|  |  |  | 				</configOption> | 
					
						
							|  |  |  | 				<configOption name="outbound_proxy" default=""> | 
					
						
							|  |  |  | 					<synopsis>Outbound Proxy used to send registrations</synopsis> | 
					
						
							|  |  |  | 				</configOption> | 
					
						
							|  |  |  | 				<configOption name="retry_interval" default="60"> | 
					
						
							|  |  |  | 					<synopsis>Interval in seconds between retries if outbound registration is unsuccessful</synopsis> | 
					
						
							|  |  |  | 				</configOption> | 
					
						
							| 
									
										
										
										
											2013-09-30 15:57:11 +00:00
										 |  |  | 				<configOption name="forbidden_retry_interval" default="0"> | 
					
						
							|  |  |  | 					<synopsis>Interval used when receiving a 403 Forbidden response.</synopsis> | 
					
						
							|  |  |  | 					<description><para> | 
					
						
							|  |  |  | 						If a 403 Forbidden is received, chan_pjsip will wait | 
					
						
							|  |  |  | 						<replaceable>forbidden_retry_interval</replaceable> seconds before | 
					
						
							|  |  |  | 						attempting registration again. If 0 is specified, chan_pjsip will not | 
					
						
							|  |  |  | 						retry after receiving a 403 Forbidden response. Setting this to a non-zero | 
					
						
							|  |  |  | 						value goes against a "SHOULD NOT" in RFC3261, but can be used to work around | 
					
						
							|  |  |  | 						buggy registrars. | 
					
						
							|  |  |  | 					</para></description> | 
					
						
							|  |  |  | 				</configOption> | 
					
						
							| 
									
										
										
										
											2013-05-19 17:45:42 +00:00
										 |  |  | 				<configOption name="server_uri"> | 
					
						
							|  |  |  | 					<synopsis>SIP URI of the server to register against</synopsis> | 
					
						
							| 
									
										
										
										
											2013-09-05 14:10:45 +00:00
										 |  |  | 					<description><para> | 
					
						
							|  |  |  | 						This is the URI at which to find the registrar to send the outbound REGISTER. This URI | 
					
						
							|  |  |  | 						is used as the request URI of the outbound REGISTER request from Asterisk.</para> | 
					
						
							|  |  |  | 						<para>For registration with an ITSP, the setting may often be just the domain of | 
					
						
							|  |  |  | 						the registrar, e.g. sip:sip.example.com. | 
					
						
							|  |  |  | 					</para></description> | 
					
						
							| 
									
										
										
										
											2013-05-19 17:45:42 +00:00
										 |  |  | 				</configOption> | 
					
						
							|  |  |  | 				<configOption name="transport"> | 
					
						
							|  |  |  | 					<synopsis>Transport used for outbound authentication</synopsis> | 
					
						
							|  |  |  | 					<description> | 
					
						
							|  |  |  | 						<note><para>A <replaceable>transport</replaceable> configured in | 
					
						
							| 
									
										
										
										
											2013-07-30 18:14:50 +00:00
										 |  |  | 						<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> | 
					
						
							| 
									
										
										
										
											2013-05-19 17:45:42 +00:00
										 |  |  | 					</description> | 
					
						
							|  |  |  | 				</configOption> | 
					
						
							|  |  |  | 				<configOption name="type"> | 
					
						
							|  |  |  | 					<synopsis>Must be of type 'registration'.</synopsis> | 
					
						
							|  |  |  | 				</configOption> | 
					
						
							| 
									
										
										
										
											2014-01-15 13:16:10 +00:00
										 |  |  | 				<configOption name="support_path"> | 
					
						
							|  |  |  | 					<synopsis>Enables Path support for outbound REGISTER requests.</synopsis> | 
					
						
							|  |  |  | 					<description><para> | 
					
						
							|  |  |  | 						When this option is enabled, outbound REGISTER requests will advertise | 
					
						
							|  |  |  | 						support for Path headers so that intervening proxies can add to the Path | 
					
						
							|  |  |  | 						header as necessary. | 
					
						
							|  |  |  | 					</para></description> | 
					
						
							|  |  |  | 				</configOption> | 
					
						
							| 
									
										
										
										
											2013-05-19 17:45:42 +00:00
										 |  |  | 			</configObject> | 
					
						
							|  |  |  | 		</configFile> | 
					
						
							|  |  |  | 	</configInfo> | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | 	<manager name="PJSIPUnregister" language="en_US"> | 
					
						
							|  |  |  | 		<synopsis> | 
					
						
							|  |  |  | 			Unregister an outbound registration. | 
					
						
							|  |  |  | 		</synopsis> | 
					
						
							|  |  |  | 		<syntax> | 
					
						
							|  |  |  | 			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" /> | 
					
						
							|  |  |  | 			<parameter name="Registration" required="true"> | 
					
						
							|  |  |  | 				<para>The outbound registration to unregister.</para> | 
					
						
							|  |  |  | 			</parameter> | 
					
						
							|  |  |  | 		</syntax> | 
					
						
							| 
									
										
										
										
											2014-10-31 16:21:57 +00:00
										 |  |  | 		<description> | 
					
						
							|  |  |  | 			<para> | 
					
						
							| 
									
										
										
										
											2015-01-06 17:35:21 +00:00
										 |  |  | 			Unregisters the specified outbound registration and stops future registration attempts. | 
					
						
							|  |  |  | 			Call PJSIPRegister to start registration and schedule re-registrations according to configuration. | 
					
						
							|  |  |  |             </para> | 
					
						
							|  |  |  | 		</description> | 
					
						
							|  |  |  | 	</manager> | 
					
						
							|  |  |  | 	<manager name="PJSIPRegister" language="en_US"> | 
					
						
							|  |  |  | 		<synopsis> | 
					
						
							|  |  |  | 			Register an outbound registration. | 
					
						
							|  |  |  | 		</synopsis> | 
					
						
							|  |  |  | 		<syntax> | 
					
						
							|  |  |  | 			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" /> | 
					
						
							|  |  |  | 			<parameter name="Registration" required="true"> | 
					
						
							|  |  |  | 				<para>The outbound registration to register.</para> | 
					
						
							|  |  |  | 			</parameter> | 
					
						
							|  |  |  | 		</syntax> | 
					
						
							|  |  |  | 		<description> | 
					
						
							|  |  |  | 			<para> | 
					
						
							|  |  |  | 			Unregisters the specified outbound registration then starts registration and schedules re-registrations | 
					
						
							|  |  |  | 			according to configuration. | 
					
						
							|  |  |  | 			future registrations. | 
					
						
							|  |  |  |             </para> | 
					
						
							| 
									
										
										
										
											2014-10-31 16:21:57 +00:00
										 |  |  | 		</description> | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | 	</manager> | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | 	<manager name="PJSIPShowRegistrationsOutbound" language="en_US"> | 
					
						
							|  |  |  | 		<synopsis> | 
					
						
							|  |  |  | 			Lists PJSIP outbound registrations. | 
					
						
							|  |  |  | 		</synopsis> | 
					
						
							|  |  |  | 		<syntax /> | 
					
						
							|  |  |  | 		<description> | 
					
						
							|  |  |  | 			<para> | 
					
						
							|  |  |  | 			In response <literal>OutboundRegistrationDetail</literal> events showing configuration and status | 
					
						
							|  |  |  | 			information are raised for each outbound registration object. <literal>AuthDetail</literal> | 
					
						
							|  |  |  | 			events are raised for each associated auth object as well.  Once all events are completed an | 
					
						
							|  |  |  | 			<literal>OutboundRegistrationDetailComplete</literal> is issued. | 
					
						
							|  |  |  |                         </para> | 
					
						
							|  |  |  | 		</description> | 
					
						
							|  |  |  | 	</manager> | 
					
						
							| 
									
										
										
										
											2013-05-19 17:45:42 +00:00
										 |  |  |  ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | /*! \brief Amount of buffer time (in seconds) before expiration that we re-register at */ | 
					
						
							|  |  |  | #define REREGISTER_BUFFER_TIME 10
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | /*! \brief Size of the buffer for creating a unique string for the line */ | 
					
						
							|  |  |  | #define LINE_PARAMETER_SIZE 8
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | /*! \brief Various states that an outbound registration may be in */ | 
					
						
							|  |  |  | enum sip_outbound_registration_status { | 
					
						
							|  |  |  | 	/*! \brief Currently unregistered */ | 
					
						
							|  |  |  | 	SIP_REGISTRATION_UNREGISTERED = 0, | 
					
						
							|  |  |  | 	/*! \brief Registered, yay! */ | 
					
						
							|  |  |  | 	SIP_REGISTRATION_REGISTERED, | 
					
						
							|  |  |  | 	/*! \brief Registration was rejected, but response was temporal */ | 
					
						
							|  |  |  | 	SIP_REGISTRATION_REJECTED_TEMPORARY, | 
					
						
							|  |  |  | 	/*! \brief Registration was rejected, permanently */ | 
					
						
							|  |  |  | 	SIP_REGISTRATION_REJECTED_PERMANENT, | 
					
						
							|  |  |  | 	/*! \brief Registration has been stopped */ | 
					
						
							|  |  |  | 	SIP_REGISTRATION_STOPPED, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-05 19:01:45 +00:00
										 |  |  | static const char *sip_outbound_registration_status_str[] = { | 
					
						
							|  |  |  | 	[SIP_REGISTRATION_UNREGISTERED] = "Unregistered", | 
					
						
							|  |  |  | 	[SIP_REGISTRATION_REGISTERED] = "Registered", | 
					
						
							|  |  |  | 	[SIP_REGISTRATION_REJECTED_TEMPORARY] = "Rejected", | 
					
						
							|  |  |  | 	[SIP_REGISTRATION_REJECTED_PERMANENT] = "Rejected", | 
					
						
							|  |  |  | 	[SIP_REGISTRATION_STOPPED] = "Stopped", | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | /*! \brief Outbound registration information */ | 
					
						
							|  |  |  | struct sip_outbound_registration { | 
					
						
							|  |  |  | 	/*! \brief Sorcery object details */ | 
					
						
							|  |  |  | 	SORCERY_OBJECT(details); | 
					
						
							|  |  |  | 	/*! \brief Stringfields */ | 
					
						
							|  |  |  | 	AST_DECLARE_STRING_FIELDS( | 
					
						
							|  |  |  | 		/*! \brief URI for the registrar */ | 
					
						
							|  |  |  | 		AST_STRING_FIELD(server_uri); | 
					
						
							|  |  |  | 		/*! \brief URI for the AOR */ | 
					
						
							|  |  |  | 		AST_STRING_FIELD(client_uri); | 
					
						
							|  |  |  | 		/*! \brief Optional user for contact header */ | 
					
						
							|  |  |  | 		AST_STRING_FIELD(contact_user); | 
					
						
							|  |  |  | 		/*! \brief Explicit transport to use for registration */ | 
					
						
							|  |  |  | 		AST_STRING_FIELD(transport); | 
					
						
							|  |  |  | 		/*! \brief Outbound proxy to use */ | 
					
						
							|  |  |  | 		AST_STRING_FIELD(outbound_proxy); | 
					
						
							|  |  |  | 		/*! \brief Endpoint to use for related incoming calls */ | 
					
						
							|  |  |  | 		AST_STRING_FIELD(endpoint); | 
					
						
							|  |  |  | 	); | 
					
						
							|  |  |  | 	/*! \brief Requested expiration time */ | 
					
						
							|  |  |  | 	unsigned int expiration; | 
					
						
							|  |  |  | 	/*! \brief Interval at which retries should occur for temporal responses */ | 
					
						
							|  |  |  | 	unsigned int retry_interval; | 
					
						
							|  |  |  | 	/*! \brief Interval at which retries should occur for permanent responses */ | 
					
						
							|  |  |  | 	unsigned int forbidden_retry_interval; | 
					
						
							|  |  |  | 	/*! \brief Treat authentication challenges that we cannot handle as permanent failures */ | 
					
						
							|  |  |  | 	unsigned int auth_rejection_permanent; | 
					
						
							|  |  |  | 	/*! \brief Maximum number of retries permitted */ | 
					
						
							|  |  |  | 	unsigned int max_retries; | 
					
						
							|  |  |  | 	/*! \brief Whether to add a line parameter to the outbound Contact or not */ | 
					
						
							|  |  |  | 	unsigned int line; | 
					
						
							|  |  |  | 	/*! \brief Configured authentication credentials */ | 
					
						
							|  |  |  | 	struct ast_sip_auth_vector outbound_auths; | 
					
						
							|  |  |  | 	/*! \brief Whether Path support is enabled */ | 
					
						
							|  |  |  | 	unsigned int support_path; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | /*! \brief Outbound registration client state information (persists for lifetime of regc) */ | 
					
						
							|  |  |  | struct sip_outbound_registration_client_state { | 
					
						
							|  |  |  | 	/*! \brief Current status of this registration */ | 
					
						
							|  |  |  | 	enum sip_outbound_registration_status status; | 
					
						
							|  |  |  | 	/*! \brief Outbound registration client */ | 
					
						
							|  |  |  | 	pjsip_regc *client; | 
					
						
							|  |  |  | 	/*! \brief Timer entry for retrying on temporal responses */ | 
					
						
							|  |  |  | 	pj_timer_entry timer; | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	/*! \brief Optional line parameter placed into Contact */ | 
					
						
							|  |  |  | 	char line[LINE_PARAMETER_SIZE]; | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	/*! \brief Current number of retries */ | 
					
						
							|  |  |  | 	unsigned int retries; | 
					
						
							|  |  |  | 	/*! \brief Maximum number of retries permitted */ | 
					
						
							|  |  |  | 	unsigned int max_retries; | 
					
						
							|  |  |  | 	/*! \brief Interval at which retries should occur for temporal responses */ | 
					
						
							|  |  |  | 	unsigned int retry_interval; | 
					
						
							| 
									
										
										
										
											2013-09-30 15:57:11 +00:00
										 |  |  | 	/*! \brief Interval at which retries should occur for permanent responses */ | 
					
						
							|  |  |  | 	unsigned int forbidden_retry_interval; | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	/*! \brief Treat authentication challenges that we cannot handle as permanent failures */ | 
					
						
							|  |  |  | 	unsigned int auth_rejection_permanent; | 
					
						
							| 
									
										
										
										
											2014-01-15 13:16:10 +00:00
										 |  |  | 	/*! \brief Determines whether SIP Path support should be advertised */ | 
					
						
							|  |  |  | 	unsigned int support_path; | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	/*! \brief Serializer for stuff and things */ | 
					
						
							|  |  |  | 	struct ast_taskprocessor *serializer; | 
					
						
							|  |  |  | 	/*! \brief Configured authentication credentials */ | 
					
						
							| 
									
										
										
										
											2013-12-09 16:10:05 +00:00
										 |  |  | 	struct ast_sip_auth_vector outbound_auths; | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	/*! \brief Registration should be destroyed after completion of transaction */ | 
					
						
							|  |  |  | 	unsigned int destroy:1; | 
					
						
							| 
									
										
										
										
											2014-12-18 15:34:16 +00:00
										 |  |  | 	/*! \brief Non-zero if we have attempted sending a REGISTER with authentication */ | 
					
						
							|  |  |  | 	unsigned int auth_attempted:1; | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Outbound registration state information (persists for lifetime that registration should exist) */ | 
					
						
							|  |  |  | struct sip_outbound_registration_state { | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	/*! \brief Outbound registration configuration object */ | 
					
						
							|  |  |  | 	struct sip_outbound_registration *registration; | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	/*! \brief Client state information */ | 
					
						
							|  |  |  | 	struct sip_outbound_registration_client_state *client_state; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | /*! \brief Default number of state container buckets */ | 
					
						
							|  |  |  | #define DEFAULT_STATE_BUCKETS 53
 | 
					
						
							|  |  |  | static AO2_GLOBAL_OBJ_STATIC(current_states); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief hashing function for state objects */ | 
					
						
							|  |  |  | static int registration_state_hash(const void *obj, const int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	const struct sip_outbound_registration_state *object; | 
					
						
							|  |  |  | 	const char *key; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch (flags & OBJ_SEARCH_MASK) { | 
					
						
							|  |  |  | 	case OBJ_SEARCH_KEY: | 
					
						
							|  |  |  | 		key = obj; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case OBJ_SEARCH_OBJECT: | 
					
						
							|  |  |  | 		object = obj; | 
					
						
							|  |  |  | 		key = ast_sorcery_object_get_id(object->registration); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		ast_assert(0); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return ast_str_hash(key); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief comparator function for state objects */ | 
					
						
							|  |  |  | static int registration_state_cmp(void *obj, void *arg, int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	const struct sip_outbound_registration_state *object_left = obj; | 
					
						
							|  |  |  | 	const struct sip_outbound_registration_state *object_right = arg; | 
					
						
							|  |  |  | 	const char *right_key = arg; | 
					
						
							|  |  |  | 	int cmp; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch (flags & OBJ_SEARCH_MASK) { | 
					
						
							|  |  |  | 	case OBJ_SEARCH_OBJECT: | 
					
						
							|  |  |  | 		right_key = ast_sorcery_object_get_id(object_right->registration); | 
					
						
							|  |  |  | 		/* Fall through */ | 
					
						
							|  |  |  | 	case OBJ_SEARCH_KEY: | 
					
						
							|  |  |  | 		cmp = strcmp(ast_sorcery_object_get_id(object_left->registration), right_key); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case OBJ_SEARCH_PARTIAL_KEY: | 
					
						
							|  |  |  | 		/* Not supported by container. */ | 
					
						
							|  |  |  | 		ast_assert(0); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		cmp = 0; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (cmp) { | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return CMP_MATCH; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct sip_outbound_registration_state *get_state(const char *id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	RAII_VAR(struct ao2_container *, states, | 
					
						
							|  |  |  | 		 ao2_global_obj_ref(current_states), ao2_cleanup); | 
					
						
							|  |  |  | 	return states ? ao2_find(states, id, OBJ_SEARCH_KEY) : NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int registration_state_add(void *obj, void *arg, int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct sip_outbound_registration_state *state = | 
					
						
							|  |  |  | 		get_state(ast_sorcery_object_get_id(obj)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (state) { | 
					
						
							|  |  |  | 		ao2_link(arg, state); | 
					
						
							|  |  |  | 		ao2_ref(state, -1); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct ao2_container *get_registrations(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	RAII_VAR(struct ao2_container *, new_states, NULL, ao2_cleanup); | 
					
						
							|  |  |  | 	struct ao2_container *registrations = ast_sorcery_retrieve_by_fields( | 
					
						
							|  |  |  | 		ast_sip_get_sorcery(), "registration", | 
					
						
							|  |  |  | 		AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!(new_states = ao2_container_alloc(DEFAULT_STATE_BUCKETS, | 
					
						
							|  |  |  | 		      registration_state_hash, registration_state_cmp))) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Unable to allocate registration states container\n"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (registrations && ao2_container_count(registrations)) { | 
					
						
							|  |  |  | 		ao2_callback(registrations, OBJ_NODATA, registration_state_add, new_states); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ao2_global_obj_replace_unref(current_states, new_states); | 
					
						
							|  |  |  | 	return registrations; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Helper function which cancels the timer on a client */ | 
					
						
							|  |  |  | static void cancel_registration(struct sip_outbound_registration_client_state *client_state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (pj_timer_heap_cancel(pjsip_endpt_get_timer_heap(ast_sip_get_pjsip_endpoint()), &client_state->timer)) { | 
					
						
							|  |  |  | 		/* The timer was successfully cancelled, drop the refcount of client_state */ | 
					
						
							|  |  |  | 		ao2_ref(client_state, -1); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-15 13:16:10 +00:00
										 |  |  | static pj_str_t PATH_NAME = { "path", 4 }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | /*! \brief Callback function for registering */ | 
					
						
							|  |  |  | static int handle_client_registration(void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	RAII_VAR(struct sip_outbound_registration_client_state *, client_state, data, ao2_cleanup); | 
					
						
							|  |  |  | 	pjsip_tx_data *tdata; | 
					
						
							| 
									
										
										
										
											2013-09-04 14:32:25 +00:00
										 |  |  | 	pjsip_regc_info info; | 
					
						
							|  |  |  | 	char server_uri[PJSIP_MAX_URL_SIZE], client_uri[PJSIP_MAX_URL_SIZE]; | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	cancel_registration(client_state); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ((client_state->status == SIP_REGISTRATION_STOPPED) || | 
					
						
							|  |  |  | 		(pjsip_regc_register(client_state->client, PJ_FALSE, &tdata) != PJ_SUCCESS)) { | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-04 14:32:25 +00:00
										 |  |  | 	pjsip_regc_get_info(client_state->client, &info); | 
					
						
							|  |  |  | 	ast_copy_pj_str(server_uri, &info.server_uri, sizeof(server_uri)); | 
					
						
							|  |  |  | 	ast_copy_pj_str(client_uri, &info.client_uri, sizeof(client_uri)); | 
					
						
							| 
									
										
										
										
											2014-05-09 22:49:26 +00:00
										 |  |  | 	ast_debug(3, "REGISTER attempt %u to '%s' with client '%s'\n", | 
					
						
							| 
									
										
										
										
											2013-09-04 14:32:25 +00:00
										 |  |  | 		  client_state->retries + 1, server_uri, client_uri); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-15 13:16:10 +00:00
										 |  |  | 	if (client_state->support_path) { | 
					
						
							|  |  |  | 		pjsip_supported_hdr *hdr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		hdr = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_SUPPORTED, NULL); | 
					
						
							|  |  |  | 		if (!hdr) { | 
					
						
							|  |  |  | 			/* insert a new Supported header */ | 
					
						
							|  |  |  | 			hdr = pjsip_supported_hdr_create(tdata->pool); | 
					
						
							|  |  |  | 			if (!hdr) { | 
					
						
							|  |  |  | 				return -1; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)hdr); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* add on to the existing Supported header */ | 
					
						
							|  |  |  | 		pj_strassign(&hdr->values[hdr->count++], &PATH_NAME); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	/* Due to the registration the callback may now get called, so bump the ref count */ | 
					
						
							|  |  |  | 	ao2_ref(client_state, +1); | 
					
						
							|  |  |  | 	if (pjsip_regc_send(client_state->client, tdata) != PJ_SUCCESS) { | 
					
						
							|  |  |  | 		ao2_ref(client_state, -1); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Timer callback function, used just for registrations */ | 
					
						
							|  |  |  | static void sip_outbound_registration_timer_cb(pj_timer_heap_t *timer_heap, struct pj_timer_entry *entry) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | 	struct sip_outbound_registration_client_state *client_state = entry->user_data; | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ao2_ref(client_state, +1); | 
					
						
							|  |  |  | 	if (ast_sip_push_task(client_state->serializer, handle_client_registration, client_state)) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Failed to pass outbound registration to threadpool\n"); | 
					
						
							|  |  |  | 		ao2_ref(client_state, -1); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	entry->id = 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Helper function which sets up the timer to re-register in a specific amount of time */ | 
					
						
							|  |  |  | static void schedule_registration(struct sip_outbound_registration_client_state *client_state, unsigned int seconds) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	pj_time_val delay = { .sec = seconds, }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cancel_registration(client_state); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ao2_ref(client_state, +1); | 
					
						
							|  |  |  | 	if (pjsip_endpt_schedule_timer(ast_sip_get_pjsip_endpoint(), &client_state->timer, &delay) != PJ_SUCCESS) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Failed to pass timed registration to scheduler\n"); | 
					
						
							|  |  |  | 		ao2_ref(client_state, -1); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Callback function for unregistering (potentially) and destroying state */ | 
					
						
							|  |  |  | static int handle_client_state_destruction(void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	RAII_VAR(struct sip_outbound_registration_client_state *, client_state, data, ao2_cleanup); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cancel_registration(client_state); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-05 16:01:53 +00:00
										 |  |  | 	if (client_state->client) { | 
					
						
							|  |  |  | 		pjsip_regc_info info; | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-05 16:01:53 +00:00
										 |  |  | 		pjsip_regc_get_info(client_state->client, &info); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-05 16:01:53 +00:00
										 |  |  | 		if (info.is_busy == PJ_TRUE) { | 
					
						
							|  |  |  | 			/* If a client transaction is in progress we defer until it is complete */ | 
					
						
							|  |  |  | 			client_state->destroy = 1; | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (client_state->status != SIP_REGISTRATION_UNREGISTERED && client_state->status != SIP_REGISTRATION_REJECTED_PERMANENT) { | 
					
						
							|  |  |  | 			pjsip_tx_data *tdata; | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-05 16:01:53 +00:00
										 |  |  | 			if (pjsip_regc_unregister(client_state->client, &tdata) == PJ_SUCCESS) { | 
					
						
							|  |  |  | 				pjsip_regc_send(client_state->client, tdata); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-05 16:01:53 +00:00
										 |  |  | 		pjsip_regc_destroy(client_state->client); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	client_state->status = SIP_REGISTRATION_STOPPED; | 
					
						
							| 
									
										
										
										
											2013-12-09 16:10:05 +00:00
										 |  |  | 	ast_sip_auth_vector_destroy(&client_state->outbound_auths); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Structure for registration response */ | 
					
						
							|  |  |  | struct registration_response { | 
					
						
							|  |  |  | 	/*! \brief Response code for the registration attempt */ | 
					
						
							|  |  |  | 	int code; | 
					
						
							|  |  |  | 	/*! \brief Expiration time for registration */ | 
					
						
							|  |  |  | 	int expiration; | 
					
						
							|  |  |  | 	/*! \brief Retry-After value */ | 
					
						
							|  |  |  | 	int retry_after; | 
					
						
							|  |  |  | 	/*! \brief Outbound registration client state */ | 
					
						
							|  |  |  | 	struct sip_outbound_registration_client_state *client_state; | 
					
						
							|  |  |  | 	/*! \brief The response message */ | 
					
						
							|  |  |  | 	pjsip_rx_data *rdata; | 
					
						
							|  |  |  | 	/*! \brief The response transaction */ | 
					
						
							|  |  |  | 	pjsip_transaction *tsx; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Registration response structure destructor */ | 
					
						
							|  |  |  | static void registration_response_destroy(void *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct registration_response *response = obj; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-10 20:02:59 +00:00
										 |  |  | 	if (response->rdata) { | 
					
						
							|  |  |  | 		pjsip_rx_data_free_cloned(response->rdata); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	ao2_cleanup(response->client_state); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* \brief Helper funtion which determines if a response code is temporal or not */ | 
					
						
							|  |  |  | static int sip_outbound_registration_is_temporal(unsigned int code, | 
					
						
							|  |  |  | 		struct sip_outbound_registration_client_state *client_state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	/* Shamelessly taken from pjsua */ | 
					
						
							|  |  |  | 	if (code == PJSIP_SC_REQUEST_TIMEOUT || | 
					
						
							|  |  |  | 		code == PJSIP_SC_INTERNAL_SERVER_ERROR || | 
					
						
							|  |  |  | 		code == PJSIP_SC_BAD_GATEWAY || | 
					
						
							|  |  |  | 		code == PJSIP_SC_SERVICE_UNAVAILABLE || | 
					
						
							|  |  |  | 		code == PJSIP_SC_SERVER_TIMEOUT || | 
					
						
							|  |  |  | 		((code == PJSIP_SC_UNAUTHORIZED || | 
					
						
							|  |  |  | 		  code == PJSIP_SC_PROXY_AUTHENTICATION_REQUIRED) && | 
					
						
							|  |  |  | 		 !client_state->auth_rejection_permanent) || | 
					
						
							|  |  |  | 		PJSIP_IS_STATUS_IN_CLASS(code, 600)) { | 
					
						
							|  |  |  | 		return 1; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-14 15:54:06 +00:00
										 |  |  | static void schedule_retry(struct registration_response *response, unsigned int interval, | 
					
						
							|  |  |  | 			   const char *server_uri, const char *client_uri) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	response->client_state->status = SIP_REGISTRATION_REJECTED_TEMPORARY; | 
					
						
							|  |  |  | 	schedule_registration(response->client_state, interval); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (response->rdata) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Temporal response '%d' received from '%s' on " | 
					
						
							| 
									
										
										
										
											2014-05-09 22:49:26 +00:00
										 |  |  | 			"registration attempt to '%s', retrying in '%u'\n", | 
					
						
							| 
									
										
										
										
											2013-10-14 15:54:06 +00:00
										 |  |  | 			response->code, server_uri, client_uri, interval); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "No response received from '%s' on " | 
					
						
							| 
									
										
										
										
											2014-05-09 22:49:26 +00:00
										 |  |  | 			"registration attempt to '%s', retrying in '%u'\n", | 
					
						
							| 
									
										
										
										
											2013-10-14 15:54:06 +00:00
										 |  |  | 			server_uri, client_uri, interval); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | /*! \brief Callback function for handling a response to a registration attempt */ | 
					
						
							|  |  |  | static int handle_registration_response(void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	RAII_VAR(struct registration_response *, response, data, ao2_cleanup); | 
					
						
							|  |  |  | 	pjsip_regc_info info; | 
					
						
							|  |  |  | 	char server_uri[PJSIP_MAX_URL_SIZE], client_uri[PJSIP_MAX_URL_SIZE]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (response->client_state->status == SIP_REGISTRATION_STOPPED) { | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pjsip_regc_get_info(response->client_state->client, &info); | 
					
						
							|  |  |  | 	ast_copy_pj_str(server_uri, &info.server_uri, sizeof(server_uri)); | 
					
						
							|  |  |  | 	ast_copy_pj_str(client_uri, &info.client_uri, sizeof(client_uri)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-18 15:34:16 +00:00
										 |  |  | 	if (!response->client_state->auth_attempted && | 
					
						
							|  |  |  | 			(response->code == 401 || response->code == 407)) { | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 		pjsip_tx_data *tdata; | 
					
						
							| 
									
										
										
										
											2013-07-30 15:17:56 +00:00
										 |  |  | 		if (!ast_sip_create_request_with_auth(&response->client_state->outbound_auths, | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 				response->rdata, response->tsx, &tdata)) { | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | 			ao2_ref(response->client_state, +1); | 
					
						
							| 
									
										
										
										
											2014-12-18 15:34:16 +00:00
										 |  |  | 			response->client_state->auth_attempted = 1; | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | 			if (pjsip_regc_send(response->client_state->client, tdata) != PJ_SUCCESS) { | 
					
						
							| 
									
										
										
										
											2014-12-18 15:34:16 +00:00
										 |  |  | 				response->client_state->auth_attempted = 0; | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | 				ao2_cleanup(response->client_state); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 			return 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		/* Otherwise, fall through so the failure is processed appropriately */ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-18 15:34:16 +00:00
										 |  |  | 	response->client_state->auth_attempted = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	if (PJSIP_IS_STATUS_IN_CLASS(response->code, 200)) { | 
					
						
							| 
									
										
										
										
											2014-10-31 16:21:57 +00:00
										 |  |  | 		/* Check if this is in regards to registering or unregistering */ | 
					
						
							|  |  |  | 		if (response->expiration) { | 
					
						
							|  |  |  | 			/* If the registration went fine simply reschedule registration for the future */ | 
					
						
							|  |  |  | 			ast_debug(1, "Outbound registration to '%s' with client '%s' successful\n", server_uri, client_uri); | 
					
						
							|  |  |  | 			response->client_state->status = SIP_REGISTRATION_REGISTERED; | 
					
						
							|  |  |  | 			response->client_state->retries = 0; | 
					
						
							|  |  |  | 			schedule_registration(response->client_state, response->expiration - REREGISTER_BUFFER_TIME); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			ast_debug(1, "Outbound unregistration to '%s' with client '%s' successful\n", server_uri, client_uri); | 
					
						
							|  |  |  | 			response->client_state->status = SIP_REGISTRATION_UNREGISTERED; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	} else if (response->retry_after) { | 
					
						
							|  |  |  | 		/* If we have been instructed to retry after a period of time, schedule it as such */ | 
					
						
							| 
									
										
										
										
											2013-10-14 15:54:06 +00:00
										 |  |  | 		schedule_retry(response, response->retry_after, server_uri, client_uri); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	} else if (response->client_state->retry_interval && sip_outbound_registration_is_temporal(response->code, response->client_state)) { | 
					
						
							|  |  |  | 		if (response->client_state->retries == response->client_state->max_retries) { | 
					
						
							|  |  |  | 			/* If we received enough temporal responses to exceed our maximum give up permanently */ | 
					
						
							|  |  |  | 			response->client_state->status = SIP_REGISTRATION_REJECTED_PERMANENT; | 
					
						
							|  |  |  | 			ast_log(LOG_WARNING, "Maximum retries reached when attempting outbound registration to '%s' with client '%s', stopping registration attempt\n", | 
					
						
							|  |  |  | 				server_uri, client_uri); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			/* On the other hand if we can still try some more do so */ | 
					
						
							|  |  |  | 			response->client_state->retries++; | 
					
						
							| 
									
										
										
										
											2013-10-14 15:54:06 +00:00
										 |  |  | 			schedule_retry(response, response->client_state->retry_interval, server_uri, client_uri); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2013-09-30 15:57:11 +00:00
										 |  |  | 		if (response->code == 403 | 
					
						
							|  |  |  | 			&& response->client_state->forbidden_retry_interval | 
					
						
							|  |  |  | 			&& response->client_state->retries < response->client_state->max_retries) { | 
					
						
							|  |  |  | 			/* A forbidden response retry interval is configured and there are retries remaining */ | 
					
						
							|  |  |  | 			response->client_state->status = SIP_REGISTRATION_REJECTED_TEMPORARY; | 
					
						
							|  |  |  | 			response->client_state->retries++; | 
					
						
							|  |  |  | 			schedule_registration(response->client_state, response->client_state->forbidden_retry_interval); | 
					
						
							| 
									
										
										
										
											2014-05-09 22:49:26 +00:00
										 |  |  | 			ast_log(LOG_WARNING, "403 Forbidden fatal response received from '%s' on registration attempt to '%s', retrying in '%u' seconds\n", | 
					
						
							| 
									
										
										
										
											2013-09-30 15:57:11 +00:00
										 |  |  | 				server_uri, client_uri, response->client_state->forbidden_retry_interval); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			/* Finally if there's no hope of registering give up */ | 
					
						
							|  |  |  | 			response->client_state->status = SIP_REGISTRATION_REJECTED_PERMANENT; | 
					
						
							| 
									
										
										
										
											2013-10-14 15:54:06 +00:00
										 |  |  | 			if (response->rdata) { | 
					
						
							|  |  |  | 				ast_log(LOG_WARNING, "Fatal response '%d' received from '%s' on registration attempt to '%s', stopping outbound registration\n", | 
					
						
							|  |  |  | 					response->code, server_uri, client_uri); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				ast_log(LOG_WARNING, "Fatal registration attempt to '%s', stopping outbound registration\n", client_uri); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-09-30 15:57:11 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-05 19:01:45 +00:00
										 |  |  | 	ast_system_publish_registry("PJSIP", client_uri, server_uri, sip_outbound_registration_status_str[response->client_state->status], NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	/* If deferred destruction is in use see if we need to destroy now */ | 
					
						
							|  |  |  | 	if (response->client_state->destroy) { | 
					
						
							|  |  |  | 		handle_client_state_destruction(response->client_state); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Callback function for outbound registration client */ | 
					
						
							|  |  |  | static void sip_outbound_registration_response_cb(struct pjsip_regc_cbparam *param) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	RAII_VAR(struct sip_outbound_registration_client_state *, client_state, param->token, ao2_cleanup); | 
					
						
							|  |  |  | 	struct registration_response *response = ao2_alloc(sizeof(*response), registration_response_destroy); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	response->code = param->code; | 
					
						
							|  |  |  | 	response->expiration = param->expiration; | 
					
						
							|  |  |  | 	response->client_state = client_state; | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | 	ao2_ref(response->client_state, +1); | 
					
						
							| 
									
										
										
										
											2013-07-10 20:02:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (param->rdata) { | 
					
						
							|  |  |  | 		struct pjsip_retry_after_hdr *retry_after = pjsip_msg_find_hdr(param->rdata->msg_info.msg, PJSIP_H_RETRY_AFTER, NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		response->retry_after = retry_after ? retry_after->ivalue : 0; | 
					
						
							|  |  |  | 		response->tsx = pjsip_rdata_get_tsx(param->rdata); | 
					
						
							|  |  |  | 		pjsip_rx_data_clone(param->rdata, 0, &response->rdata); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	if (ast_sip_push_task(client_state->serializer, handle_registration_response, response)) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Failed to pass incoming registration response to threadpool\n"); | 
					
						
							|  |  |  | 		ao2_cleanup(response); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Destructor function for registration state */ | 
					
						
							|  |  |  | static void sip_outbound_registration_state_destroy(void *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct sip_outbound_registration_state *state = obj; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	ao2_cleanup(state->registration); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	if (!state->client_state) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (state->client_state->serializer && ast_sip_push_task(state->client_state->serializer, handle_client_state_destruction, state->client_state)) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Failed to pass outbound registration client destruction to threadpool\n"); | 
					
						
							|  |  |  | 		ao2_ref(state->client_state, -1); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Destructor function for client registration state */ | 
					
						
							|  |  |  | static void sip_outbound_registration_client_state_destroy(void *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct sip_outbound_registration_client_state *client_state = obj; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_taskprocessor_unreference(client_state->serializer); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Allocator function for registration state */ | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | static struct sip_outbound_registration_state *sip_outbound_registration_state_alloc(struct sip_outbound_registration *registration) | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct sip_outbound_registration_state *state = ao2_alloc(sizeof(*state), sip_outbound_registration_state_destroy); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!state || !(state->client_state = ao2_alloc(sizeof(*state->client_state), sip_outbound_registration_client_state_destroy))) { | 
					
						
							|  |  |  | 		ao2_cleanup(state); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-05 01:31:19 +00:00
										 |  |  | 	if (!(state->client_state->serializer = ast_sip_create_serializer())) { | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 		ao2_cleanup(state->client_state); | 
					
						
							|  |  |  | 		ao2_cleanup(state); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	state->client_state->status = SIP_REGISTRATION_UNREGISTERED; | 
					
						
							|  |  |  | 	state->client_state->timer.user_data = state->client_state; | 
					
						
							|  |  |  | 	state->client_state->timer.cb = sip_outbound_registration_timer_cb; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	state->registration = ao2_bump(registration); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	return state; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Destructor function for registration information */ | 
					
						
							|  |  |  | static void sip_outbound_registration_destroy(void *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct sip_outbound_registration *registration = obj; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 16:10:05 +00:00
										 |  |  | 	ast_sip_auth_vector_destroy(®istration->outbound_auths); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ast_string_field_free_memory(registration); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Allocator function for registration information */ | 
					
						
							|  |  |  | static void *sip_outbound_registration_alloc(const char *name) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-06-23 18:59:36 +00:00
										 |  |  | 	struct sip_outbound_registration *registration = ast_sorcery_generic_alloc(sizeof(*registration), sip_outbound_registration_destroy); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	if (!registration || ast_string_field_init(registration, 256)) { | 
					
						
							|  |  |  | 		ao2_cleanup(registration); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return registration; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*! \brief Helper function which populates a pj_str_t with a contact header */ | 
					
						
							|  |  |  | static int sip_dialog_create_contact(pj_pool_t *pool, pj_str_t *contact, const char *user, const pj_str_t *target, pjsip_tpselector *selector) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	pj_str_t tmp, local_addr; | 
					
						
							|  |  |  | 	pjsip_uri *uri; | 
					
						
							|  |  |  | 	pjsip_sip_uri *sip_uri; | 
					
						
							|  |  |  | 	pjsip_transport_type_e type = PJSIP_TRANSPORT_UNSPECIFIED; | 
					
						
							|  |  |  | 	int local_port; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pj_strdup_with_null(pool, &tmp, target); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!(uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0)) || | 
					
						
							|  |  |  | 	    (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))) { | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sip_uri = pjsip_uri_get_uri(uri); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri)) { | 
					
						
							|  |  |  | 		type = PJSIP_TRANSPORT_TLS; | 
					
						
							|  |  |  | 	} else if (!sip_uri->transport_param.slen) { | 
					
						
							|  |  |  | 		type = PJSIP_TRANSPORT_UDP; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		type = pjsip_transport_get_type_from_name(&sip_uri->transport_param); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (type == PJSIP_TRANSPORT_UNSPECIFIED) { | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (pj_strchr(&sip_uri->host, ':')) { | 
					
						
							|  |  |  | 		type = (pjsip_transport_type_e)(((int)type) + PJSIP_TRANSPORT_IPV6); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()), pool, type, selector, | 
					
						
							|  |  |  | 							      &local_addr, &local_port) != PJ_SUCCESS) { | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!pj_strchr(&sip_uri->host, ':') && pj_strchr(&local_addr, ':')) { | 
					
						
							|  |  |  | 		type = (pjsip_transport_type_e)(((int)type) + PJSIP_TRANSPORT_IPV6); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	contact->ptr = pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE); | 
					
						
							|  |  |  | 	contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE, | 
					
						
							|  |  |  | 				      "<%s:%s@%s%.*s%s:%d%s%s>", | 
					
						
							|  |  |  | 				      (pjsip_transport_get_flag_from_type(type) & PJSIP_TRANSPORT_SECURE) ? "sips" : "sip", | 
					
						
							|  |  |  | 				      user, | 
					
						
							|  |  |  | 				      (type & PJSIP_TRANSPORT_IPV6) ? "[" : "", | 
					
						
							|  |  |  | 				      (int)local_addr.slen, | 
					
						
							|  |  |  | 				      local_addr.ptr, | 
					
						
							|  |  |  | 				      (type & PJSIP_TRANSPORT_IPV6) ? "]" : "", | 
					
						
							|  |  |  | 				      local_port, | 
					
						
							|  |  |  | 				      (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? ";transport=" : "", | 
					
						
							|  |  |  | 				      (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? pjsip_transport_get_type_name(type) : ""); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*!
 | 
					
						
							|  |  |  |  * \internal | 
					
						
							|  |  |  |  * \brief Check if a registration can be reused | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This checks if the existing outbound registration's configuration differs from a newly-applied | 
					
						
							|  |  |  |  * outbound registration to see if the applied one. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \param existing The pre-existing outbound registration | 
					
						
							|  |  |  |  * \param applied The newly-created registration | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static int can_reuse_registration(struct sip_outbound_registration *existing, struct sip_outbound_registration *applied) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (strcmp(existing->server_uri, applied->server_uri) || strcmp(existing->client_uri, applied->client_uri) || | 
					
						
							|  |  |  | 		strcmp(existing->transport, applied->transport) || strcmp(existing->contact_user, applied->contact_user) || | 
					
						
							| 
									
										
										
										
											2013-12-09 16:10:05 +00:00
										 |  |  | 		strcmp(existing->outbound_proxy, applied->outbound_proxy) || | 
					
						
							|  |  |  | 		AST_VECTOR_SIZE(&existing->outbound_auths) != AST_VECTOR_SIZE(&applied->outbound_auths) || | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 		existing->auth_rejection_permanent != applied->auth_rejection_permanent) { | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 16:10:05 +00:00
										 |  |  | 	for (i = 0; i < AST_VECTOR_SIZE(&existing->outbound_auths); ++i) { | 
					
						
							|  |  |  | 		if (strcmp(AST_VECTOR_GET(&existing->outbound_auths, i), AST_VECTOR_GET(&applied->outbound_auths, i))) { | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 			return 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-09 11:05:48 +00:00
										 |  |  | /*! \brief Helper function that allocates a pjsip registration client and configures it */ | 
					
						
							|  |  |  | static int sip_outbound_registration_regc_alloc(void *data) | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	struct sip_outbound_registration_state *state = data; | 
					
						
							|  |  |  | 	RAII_VAR(struct sip_outbound_registration *, registration, | 
					
						
							|  |  |  | 		 ao2_bump(state->registration), ao2_cleanup); | 
					
						
							| 
									
										
										
										
											2014-01-03 17:10:23 +00:00
										 |  |  | 	pj_pool_t *pool; | 
					
						
							|  |  |  | 	pj_str_t tmp; | 
					
						
							|  |  |  | 	pjsip_uri *uri; | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	pj_str_t server_uri, client_uri, contact_uri; | 
					
						
							|  |  |  | 	pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-03 17:10:23 +00:00
										 |  |  | 	pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "URI Validation", 256, 256); | 
					
						
							|  |  |  | 	if (!pool) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Could not create pool for URI validation on outbound registration '%s'\n", | 
					
						
							|  |  |  | 			ast_sorcery_object_get_id(registration)); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pj_strdup2_with_null(pool, &tmp, registration->server_uri); | 
					
						
							|  |  |  | 	uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0); | 
					
						
							|  |  |  | 	if (!uri) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Invalid server URI '%s' specified on outbound registration '%s'\n", | 
					
						
							|  |  |  | 			registration->server_uri, ast_sorcery_object_get_id(registration)); | 
					
						
							|  |  |  | 		pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pj_strdup2_with_null(pool, &tmp, registration->client_uri); | 
					
						
							|  |  |  | 	uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0); | 
					
						
							|  |  |  | 	if (!uri) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Invalid client URI '%s' specified on outbound registration '%s'\n", | 
					
						
							|  |  |  | 			registration->client_uri, ast_sorcery_object_get_id(registration)); | 
					
						
							|  |  |  | 		pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-09 11:05:48 +00:00
										 |  |  | 	if (!ast_strlen_zero(registration->transport)) { | 
					
						
							|  |  |  | 		RAII_VAR(struct ast_sip_transport *, transport, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport", registration->transport), ao2_cleanup); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (!transport || !transport->state) { | 
					
						
							| 
									
										
										
										
											2013-08-30 19:55:56 +00:00
										 |  |  | 			ast_log(LOG_ERROR, "Unable to retrieve PJSIP transport '%s' " | 
					
						
							|  |  |  | 				" for outbound registration", registration->transport); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-22 14:03:22 +00:00
										 |  |  | 		if (transport->state->transport) { | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 			selector.type = PJSIP_TPSELECTOR_TRANSPORT; | 
					
						
							|  |  |  | 			selector.u.transport = transport->state->transport; | 
					
						
							| 
									
										
										
										
											2013-06-22 14:03:22 +00:00
										 |  |  | 		} else if (transport->state->factory) { | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 			selector.type = PJSIP_TPSELECTOR_LISTENER; | 
					
						
							|  |  |  | 			selector.u.listener = transport->state->factory; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	if (!state->client_state->client && | 
					
						
							|  |  |  | 		pjsip_regc_create(ast_sip_get_pjsip_endpoint(), state->client_state, sip_outbound_registration_response_cb, | 
					
						
							|  |  |  | 		&state->client_state->client) != PJ_SUCCESS) { | 
					
						
							| 
									
										
										
										
											2014-01-05 01:31:19 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	pjsip_regc_set_transport(state->client_state->client, &selector); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-09 11:05:48 +00:00
										 |  |  | 	if (!ast_strlen_zero(registration->outbound_proxy)) { | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 		pjsip_route_hdr route_set, *route; | 
					
						
							|  |  |  | 		static const pj_str_t ROUTE_HNAME = { "Route", 5 }; | 
					
						
							|  |  |  | 		pj_str_t tmp; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		pj_list_init(&route_set); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 		pj_strdup2_with_null(pjsip_regc_get_pool(state->client_state->client), &tmp, registration->outbound_proxy); | 
					
						
							|  |  |  | 		if (!(route = pjsip_parse_hdr(pjsip_regc_get_pool(state->client_state->client), &ROUTE_HNAME, tmp.ptr, tmp.slen, NULL))) { | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-01-21 17:15:34 +00:00
										 |  |  | 		pj_list_insert_nodes_before(&route_set, route); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 		pjsip_regc_set_route_set(state->client_state->client, &route_set); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-09 11:05:48 +00:00
										 |  |  | 	pj_cstr(&server_uri, registration->server_uri); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	if (sip_dialog_create_contact(pjsip_regc_get_pool(state->client_state->client), &contact_uri, S_OR(registration->contact_user, "s"), &server_uri, &selector)) { | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-09 11:05:48 +00:00
										 |  |  | 	pj_cstr(&client_uri, registration->client_uri); | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	if (pjsip_regc_init(state->client_state->client, &server_uri, &client_uri, &client_uri, 1, &contact_uri, registration->expiration) != PJ_SUCCESS) { | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | /*! \brief Helper function which performs a single registration */ | 
					
						
							|  |  |  | static int sip_outbound_registration_perform(void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	RAII_VAR(struct sip_outbound_registration_state *, state, data, ao2_cleanup); | 
					
						
							|  |  |  | 	RAII_VAR(struct sip_outbound_registration *, registration, ao2_bump(state->registration), ao2_cleanup); | 
					
						
							|  |  |  | 	size_t i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Just in case the client state is being reused for this registration, free the auth information */ | 
					
						
							|  |  |  | 	ast_sip_auth_vector_destroy(&state->client_state->outbound_auths); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	AST_VECTOR_INIT(&state->client_state->outbound_auths, AST_VECTOR_SIZE(®istration->outbound_auths)); | 
					
						
							|  |  |  | 	for (i = 0; i < AST_VECTOR_SIZE(®istration->outbound_auths); ++i) { | 
					
						
							|  |  |  | 		const char *name = ast_strdup(AST_VECTOR_GET(®istration->outbound_auths, i)); | 
					
						
							|  |  |  | 		AST_VECTOR_APPEND(&state->client_state->outbound_auths, name); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	state->client_state->retry_interval = registration->retry_interval; | 
					
						
							|  |  |  | 	state->client_state->forbidden_retry_interval = registration->forbidden_retry_interval; | 
					
						
							|  |  |  | 	state->client_state->max_retries = registration->max_retries; | 
					
						
							|  |  |  | 	state->client_state->retries = 0; | 
					
						
							|  |  |  | 	state->client_state->support_path = registration->support_path; | 
					
						
							|  |  |  | 	state->client_state->auth_rejection_permanent = registration->auth_rejection_permanent; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pjsip_regc_update_expires(state->client_state->client, registration->expiration); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	schedule_registration(state->client_state, (ast_random() % 10) + 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-09 11:05:48 +00:00
										 |  |  | /*! \brief Apply function which finds or allocates a state structure */ | 
					
						
							|  |  |  | static int sip_outbound_registration_apply(const struct ast_sorcery *sorcery, void *obj) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	RAII_VAR(struct ao2_container *, states, ao2_global_obj_ref(current_states), ao2_cleanup); | 
					
						
							|  |  |  | 	RAII_VAR(struct sip_outbound_registration_state *, state, | 
					
						
							|  |  |  | 		 ao2_find(states, ast_sorcery_object_get_id(obj), OBJ_SEARCH_KEY), ao2_cleanup); | 
					
						
							|  |  |  | 	RAII_VAR(struct sip_outbound_registration_state *, new_state, NULL, ao2_cleanup); | 
					
						
							| 
									
										
										
										
											2013-07-09 11:05:48 +00:00
										 |  |  | 	struct sip_outbound_registration *applied = obj; | 
					
						
							| 
									
										
										
										
											2013-12-31 20:27:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (ast_strlen_zero(applied->server_uri)) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "No server URI specified on outbound registration '%s'", | 
					
						
							|  |  |  | 			ast_sorcery_object_get_id(applied)); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} else if (ast_strlen_zero(applied->client_uri)) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "No client URI specified on outbound registration '%s'\n", | 
					
						
							|  |  |  | 			ast_sorcery_object_get_id(applied)); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	if (state && can_reuse_registration(state->registration, applied)) { | 
					
						
							|  |  |  | 		ao2_replace(state->registration, applied); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2013-07-09 11:05:48 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	if (!(new_state = sip_outbound_registration_state_alloc(applied))) { | 
					
						
							| 
									
										
										
										
											2013-07-09 11:05:48 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	if (ast_sip_push_task_synchronous(NULL, sip_outbound_registration_regc_alloc, new_state)) { | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-07-09 11:05:48 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	if (ast_sip_push_task(new_state->client_state->serializer, | 
					
						
							|  |  |  | 			      sip_outbound_registration_perform, ao2_bump(new_state))) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Failed to perform outbound registration on '%s'\n", | 
					
						
							|  |  |  | 			ast_sorcery_object_get_id(new_state->registration)); | 
					
						
							|  |  |  | 		ao2_ref(new_state, -1); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	ao2_lock(states); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	if (state) { | 
					
						
							|  |  |  | 		ao2_unlink(states, state); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	ao2_link(states, new_state); | 
					
						
							|  |  |  | 	ao2_unlock(states); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int outbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct sip_outbound_registration *registration = obj; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 16:10:05 +00:00
										 |  |  | 	return ast_sip_auth_vector_init(®istration->outbound_auths, var->value); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | static int outbound_auths_to_str(const void *obj, const intptr_t *args, char **buf) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	const struct sip_outbound_registration *registration = obj; | 
					
						
							| 
									
										
										
										
											2014-03-06 22:39:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | 	return ast_sip_auths_to_str(®istration->outbound_auths, buf); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-06 22:39:54 +00:00
										 |  |  | static int outbound_auths_to_var_list(const void *obj, struct ast_variable **fields) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	const struct sip_outbound_registration *registration = obj; | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 	struct ast_variable *head = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < AST_VECTOR_SIZE(®istration->outbound_auths) ; i++) { | 
					
						
							|  |  |  | 		ast_variable_list_append(&head, ast_variable_new("outbound_auth", | 
					
						
							|  |  |  | 			AST_VECTOR_GET(®istration->outbound_auths, i), "")); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (head) { | 
					
						
							|  |  |  | 		*fields = head; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | static int unregister_task(void *obj) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	RAII_VAR(struct sip_outbound_registration_state*, state, obj, ao2_cleanup); | 
					
						
							|  |  |  | 	struct pjsip_regc *client = state->client_state->client; | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | 	pjsip_tx_data *tdata; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-06 17:35:21 +00:00
										 |  |  | 	cancel_registration(state->client_state); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | 	if (pjsip_regc_unregister(client, &tdata) != PJ_SUCCESS) { | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	ao2_ref(state->client_state, +1); | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | 	if (pjsip_regc_send(client, tdata) != PJ_SUCCESS) { | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 		ao2_cleanup(state->client_state); | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | static int queue_unregister(struct sip_outbound_registration_state *state) | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	ao2_ref(state, +1); | 
					
						
							|  |  |  | 	if (ast_sip_push_task(state->client_state->serializer, unregister_task, state)) { | 
					
						
							|  |  |  | 		ao2_ref(state, -1); | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-01-06 17:35:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int queue_register(struct sip_outbound_registration_state *state) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ao2_ref(state, +1); | 
					
						
							|  |  |  | 	if (ast_sip_push_task(state->client_state->serializer, sip_outbound_registration_perform, state)) { | 
					
						
							|  |  |  | 		ao2_ref(state, -1); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *cli_complete_registration(const char *line, const char *word, | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 				       int pos, int state) | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	char *result = NULL; | 
					
						
							|  |  |  | 	int wordlen; | 
					
						
							|  |  |  | 	int which = 0; | 
					
						
							|  |  |  | 	struct sip_outbound_registration *registration; | 
					
						
							|  |  |  | 	RAII_VAR(struct ao2_container *, registrations, NULL, ao2_cleanup); | 
					
						
							|  |  |  | 	struct ao2_iterator i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (pos != 3) { | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	wordlen = strlen(word); | 
					
						
							|  |  |  | 	registrations = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "registration", | 
					
						
							|  |  |  | 		AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL); | 
					
						
							|  |  |  | 	if (!registrations) { | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	i = ao2_iterator_init(registrations, 0); | 
					
						
							|  |  |  | 	while ((registration = ao2_iterator_next(&i))) { | 
					
						
							|  |  |  | 		const char *name = ast_sorcery_object_get_id(registration); | 
					
						
							|  |  |  | 		if (!strncasecmp(word, name, wordlen) && ++which > state) { | 
					
						
							|  |  |  | 			result = ast_strdup(name); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ao2_cleanup(registration); | 
					
						
							|  |  |  | 		if (result) { | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ao2_iterator_destroy(&i); | 
					
						
							|  |  |  | 	return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *cli_unregister(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	RAII_VAR(struct sip_outbound_registration_state *, state, NULL, ao2_cleanup); | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | 	const char *registration_name; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch (cmd) { | 
					
						
							|  |  |  | 	case CLI_INIT: | 
					
						
							|  |  |  | 		e->command = "pjsip send unregister"; | 
					
						
							|  |  |  | 		e->usage = | 
					
						
							|  |  |  | 			"Usage: pjsip send unregister <registration>\n" | 
					
						
							| 
									
										
										
										
											2015-01-06 17:35:21 +00:00
										 |  |  | 			"       Unregisters the specified outbound registration and stops future registration attempts.\n"; | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	case CLI_GENERATE: | 
					
						
							|  |  |  | 		return cli_complete_registration(a->line, a->word, a->pos, a->n); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (a->argc != 4) { | 
					
						
							|  |  |  | 		return CLI_SHOWUSAGE; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	registration_name = a->argv[3]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	state = get_state(registration_name); | 
					
						
							|  |  |  | 	if (!state) { | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | 		ast_cli(a->fd, "Unable to retrieve registration %s\n", registration_name); | 
					
						
							|  |  |  | 		return CLI_FAILURE; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	if (queue_unregister(state)) { | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | 		ast_cli(a->fd, "Failed to queue unregistration"); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return CLI_SUCCESS; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-06 17:35:21 +00:00
										 |  |  | static char *cli_register(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	RAII_VAR(struct sip_outbound_registration_state *, state, NULL, ao2_cleanup); | 
					
						
							|  |  |  | 	const char *registration_name; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch (cmd) { | 
					
						
							|  |  |  | 	case CLI_INIT: | 
					
						
							|  |  |  | 		e->command = "pjsip send register"; | 
					
						
							|  |  |  | 		e->usage = | 
					
						
							|  |  |  | 			"Usage: pjsip send register <registration>\n" | 
					
						
							|  |  |  | 			"       Unregisters the specified outbound " | 
					
						
							|  |  |  | 			"registration then re-registers and re-schedules it.\n"; | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	case CLI_GENERATE: | 
					
						
							|  |  |  | 		return cli_complete_registration(a->line, a->word, a->pos, a->n); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (a->argc != 4) { | 
					
						
							|  |  |  | 		return CLI_SHOWUSAGE; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	registration_name = a->argv[3]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	state = get_state(registration_name); | 
					
						
							|  |  |  | 	if (!state) { | 
					
						
							|  |  |  | 		ast_cli(a->fd, "Unable to retrieve registration %s\n", registration_name); | 
					
						
							|  |  |  | 		return CLI_FAILURE; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* We need to serialize the unregister and register so they need
 | 
					
						
							|  |  |  | 	 * to be queued as separate tasks. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	if (queue_unregister(state)) { | 
					
						
							|  |  |  | 		ast_cli(a->fd, "Failed to queue unregistration"); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (queue_register(state)) { | 
					
						
							|  |  |  | 		ast_cli(a->fd, "Failed to queue registration"); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return CLI_SUCCESS; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | static int ami_unregister(struct mansession *s, const struct message *m) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	const char *registration_name = astman_get_header(m, "Registration"); | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	RAII_VAR(struct sip_outbound_registration_state *, state, NULL, ao2_cleanup); | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (ast_strlen_zero(registration_name)) { | 
					
						
							|  |  |  | 		astman_send_error(s, m, "Registration parameter missing."); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	state = get_state(registration_name); | 
					
						
							|  |  |  | 	if (!state) { | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | 		astman_send_error(s, m, "Unable to retrieve registration entry\n"); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	if (queue_unregister(state)) { | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | 		astman_send_ack(s, m, "Failed to queue unregistration"); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	astman_send_ack(s, m, "Unregistration sent"); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-06 17:35:21 +00:00
										 |  |  | static int ami_register(struct mansession *s, const struct message *m) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	const char *registration_name = astman_get_header(m, "Registration"); | 
					
						
							|  |  |  | 	RAII_VAR(struct sip_outbound_registration_state *, state, NULL, ao2_cleanup); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (ast_strlen_zero(registration_name)) { | 
					
						
							|  |  |  | 		astman_send_error(s, m, "Registration parameter missing."); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	state = get_state(registration_name); | 
					
						
							|  |  |  | 	if (!state) { | 
					
						
							|  |  |  | 		astman_send_error(s, m, "Unable to retrieve registration entry\n"); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* We need to serialize the unregister and register so they need
 | 
					
						
							|  |  |  | 	 * to be queued as separate tasks. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	if (queue_unregister(state)) { | 
					
						
							|  |  |  | 		astman_send_ack(s, m, "Failed to queue unregistration"); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (queue_register(state)) { | 
					
						
							|  |  |  | 		astman_send_ack(s, m, "Failed to queue unregistration"); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	astman_send_ack(s, m, "Reregistration sent"); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | struct sip_ami_outbound { | 
					
						
							|  |  |  | 	struct ast_sip_ami *ami; | 
					
						
							|  |  |  | 	int registered; | 
					
						
							|  |  |  | 	int not_registered; | 
					
						
							|  |  |  | 	struct sip_outbound_registration *registration; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int ami_outbound_registration_task(void *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct sip_ami_outbound *ami = obj; | 
					
						
							|  |  |  | 	RAII_VAR(struct ast_str *, buf, | 
					
						
							|  |  |  | 		 ast_sip_create_ami_event("OutboundRegistrationDetail", ami->ami), ast_free); | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	struct sip_outbound_registration_state *state; | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!buf) { | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_sip_sorcery_object_to_ami(ami->registration, &buf); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	if ((state = get_state(ast_sorcery_object_get_id(ami->registration)))) { | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | 		pjsip_regc_info info; | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 		if (state->client_state->status == SIP_REGISTRATION_REGISTERED) { | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | 			++ami->registered; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			++ami->not_registered; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ast_str_append(&buf, 0, "Status: %s%s", | 
					
						
							|  |  |  | 			       sip_outbound_registration_status_str[ | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 				       state->client_state->status], "\r\n"); | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 		pjsip_regc_get_info(state->client_state->client, &info); | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | 		ast_str_append(&buf, 0, "NextReg: %d%s", info.next_reg, "\r\n"); | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 		ao2_ref(state, -1); | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	astman_append(ami->ami->s, "%s\r\n", ast_str_buffer(buf)); | 
					
						
							|  |  |  | 	return ast_sip_format_auths_ami(&ami->registration->outbound_auths, ami->ami); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int ami_outbound_registration_detail(void *obj, void *arg, int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct sip_ami_outbound *ami = arg; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ami->registration = obj; | 
					
						
							|  |  |  | 	return ast_sip_push_task_synchronous( | 
					
						
							|  |  |  | 		NULL, ami_outbound_registration_task, ami); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int ami_show_outbound_registrations(struct mansession *s, | 
					
						
							|  |  |  | 					   const struct message *m) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-06-27 13:50:02 +00:00
										 |  |  | 	struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"), }; | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | 	struct sip_ami_outbound ami_outbound = { .ami = &ami }; | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	RAII_VAR(struct ao2_container *, regs, get_registrations(), ao2_cleanup); | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!regs) { | 
					
						
							|  |  |  | 		astman_send_error(s, m, "Unable to retreive " | 
					
						
							|  |  |  | 				  "outbound registrations\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	astman_send_listack(s, m, "Following are Events for each Outbound " | 
					
						
							|  |  |  | 			    "registration", "start"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ao2_callback(regs, OBJ_NODATA, ami_outbound_registration_detail, &ami_outbound); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-27 13:50:02 +00:00
										 |  |  | 	astman_append(s, "Event: OutboundRegistrationDetailComplete\r\n"); | 
					
						
							|  |  |  | 	if (!ast_strlen_zero(ami.action_id)) { | 
					
						
							|  |  |  | 		astman_append(s, "ActionID: %s\r\n", ami.action_id); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	astman_append(s, "EventList: Complete\r\n" | 
					
						
							| 
									
										
										
										
											2013-11-23 17:26:57 +00:00
										 |  |  | 		      "Registered: %d\r\n" | 
					
						
							|  |  |  | 		      "NotRegistered: %d\r\n\r\n", | 
					
						
							|  |  |  | 		      ami_outbound.registered, | 
					
						
							|  |  |  | 		      ami_outbound.not_registered); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | static struct ao2_container *cli_get_container(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	RAII_VAR(struct ao2_container *, container, get_registrations(), ao2_cleanup); | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | 	struct ao2_container *s_container; | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!container) { | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | 		ast_sorcery_object_id_sort, ast_sorcery_object_id_compare); | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 	if (!s_container) { | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (ao2_container_dup(s_container, container, 0)) { | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | 		ao2_ref(s_container, -1); | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 	return s_container; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | static int cli_iterator(void *container, ao2_callback_fn callback, void *args) | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | 	ao2_callback(container, OBJ_NODATA, callback, args); | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | static void *cli_retrieve_by_id(const char *id) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	struct ao2_container *states; | 
					
						
							|  |  |  | 	void *obj = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "registration", id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!obj) { | 
					
						
							|  |  |  | 		/* if the object no longer exists then remove its state  */ | 
					
						
							|  |  |  | 		ao2_find((states = ao2_global_obj_ref(current_states)), | 
					
						
							|  |  |  | 			 id, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA); | 
					
						
							|  |  |  | 		ao2_ref(states, -1); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return obj; | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | static int cli_print_header(void *obj, void *arg, int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct ast_sip_cli_context *context = arg; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | 	ast_assert(context->output_buffer != NULL); | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ast_str_append(&context->output_buffer, 0, | 
					
						
							|  |  |  | 		" <Registration/ServerURI..............................>  <Auth..........>  <Status.......>\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int cli_print_body(void *obj, void *arg, int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct sip_outbound_registration *registration = obj; | 
					
						
							|  |  |  | 	struct ast_sip_cli_context *context = arg; | 
					
						
							|  |  |  | 	const char *id = ast_sorcery_object_get_id(registration); | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	struct sip_outbound_registration_state *state = get_state(id); | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | #define REGISTRATION_URI_FIELD_LEN	53
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | 	ast_assert(context->output_buffer != NULL); | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ast_str_append(&context->output_buffer, 0, " %-s/%-*.*s  %-16s  %-16s\n", | 
					
						
							|  |  |  | 		id, | 
					
						
							|  |  |  | 		(int) (REGISTRATION_URI_FIELD_LEN - strlen(id)), | 
					
						
							|  |  |  | 		(int) (REGISTRATION_URI_FIELD_LEN - strlen(id)), | 
					
						
							|  |  |  | 		registration->server_uri, | 
					
						
							|  |  |  | 		AST_VECTOR_SIZE(®istration->outbound_auths) | 
					
						
							|  |  |  | 			? AST_VECTOR_GET(®istration->outbound_auths, 0) | 
					
						
							|  |  |  | 			: "n/a", | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 		sip_outbound_registration_status_str[state->client_state->status]); | 
					
						
							|  |  |  | 	ao2_ref(state, -1); | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (context->show_details | 
					
						
							|  |  |  | 		|| (context->show_details_only_level_0 && context->indent_level == 0)) { | 
					
						
							|  |  |  | 		ast_str_append(&context->output_buffer, 0, "\n"); | 
					
						
							|  |  |  | 		ast_sip_cli_print_sorcery_objectset(registration, context, 0); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * A function pointer to callback needs to be within the | 
					
						
							|  |  |  |  * module in order to avoid problems with an undefined | 
					
						
							|  |  |  |  * symbol when the module is loaded. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static char *my_cli_traverse_objects(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return ast_sip_cli_traverse_objects(e, cmd, a); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct ast_cli_entry cli_outbound_registration[] = { | 
					
						
							| 
									
										
										
										
											2015-01-06 17:35:21 +00:00
										 |  |  | 	AST_CLI_DEFINE(cli_unregister, "Unregisters outbound registration target"), | 
					
						
							|  |  |  | 	AST_CLI_DEFINE(cli_register, "Registers an outbound registration target"), | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 	AST_CLI_DEFINE(my_cli_traverse_objects, "List PJSIP Registrations", | 
					
						
							|  |  |  | 		.command = "pjsip list registrations", | 
					
						
							|  |  |  | 		.usage = "Usage: pjsip list registrations\n" | 
					
						
							|  |  |  | 				 "       List the configured PJSIP Registrations\n"), | 
					
						
							| 
									
										
										
										
											2014-02-20 21:12:02 +00:00
										 |  |  | 	AST_CLI_DEFINE(my_cli_traverse_objects, "Show PJSIP Registrations", | 
					
						
							|  |  |  | 		.command = "pjsip show registrations", | 
					
						
							|  |  |  | 		.usage = "Usage: pjsip show registrations\n" | 
					
						
							|  |  |  | 				 "       Show the configured PJSIP Registrations\n"), | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 	AST_CLI_DEFINE(my_cli_traverse_objects, "Show PJSIP Registration", | 
					
						
							|  |  |  | 		.command = "pjsip show registration", | 
					
						
							|  |  |  | 		.usage = "Usage: pjsip show registration <id>\n" | 
					
						
							|  |  |  | 				 "       Show the configured PJSIP Registration\n"), | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | static struct ast_sip_cli_formatter_entry *cli_formatter; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | static int unload_module(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ast_cli_unregister_multiple(cli_outbound_registration, ARRAY_LEN(cli_outbound_registration)); | 
					
						
							|  |  |  | 	ast_sip_unregister_cli_formatter(cli_formatter); | 
					
						
							|  |  |  | 	ast_manager_unregister("PJSIPShowRegistrationsOutbound"); | 
					
						
							|  |  |  | 	ast_manager_unregister("PJSIPUnregister"); | 
					
						
							| 
									
										
										
										
											2015-01-06 17:35:21 +00:00
										 |  |  | 	ast_manager_unregister("PJSIPRegister"); | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ao2_global_obj_release(current_states); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | static int load_module(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	struct ao2_container *registrations, *new_states; | 
					
						
							| 
									
										
										
										
											2014-10-16 14:35:00 +00:00
										 |  |  | 	CHECK_PJSIP_MODULE_LOADED(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	ast_sorcery_apply_config(ast_sip_get_sorcery(), "res_pjsip_outbound_registration"); | 
					
						
							| 
									
										
										
										
											2013-07-30 18:14:50 +00:00
										 |  |  | 	ast_sorcery_apply_default(ast_sip_get_sorcery(), "registration", "config", "pjsip.conf,criteria=type=registration"); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (ast_sorcery_object_register(ast_sip_get_sorcery(), "registration", sip_outbound_registration_alloc, NULL, sip_outbound_registration_apply)) { | 
					
						
							|  |  |  | 		return AST_MODULE_LOAD_DECLINE; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "type", "", OPT_NOOP_T, 0, 0); | 
					
						
							|  |  |  | 	ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "server_uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, server_uri)); | 
					
						
							|  |  |  | 	ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "client_uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, client_uri)); | 
					
						
							|  |  |  | 	ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "contact_user", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, contact_user)); | 
					
						
							|  |  |  | 	ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, transport)); | 
					
						
							|  |  |  | 	ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, outbound_proxy)); | 
					
						
							|  |  |  | 	ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "expiration", "3600", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, expiration)); | 
					
						
							|  |  |  | 	ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "retry_interval", "60", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, retry_interval)); | 
					
						
							| 
									
										
										
										
											2013-09-30 15:57:11 +00:00
										 |  |  | 	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)); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "max_retries", "10", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, max_retries)); | 
					
						
							|  |  |  | 	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)); | 
					
						
							| 
									
										
										
										
											2014-03-06 22:39:54 +00:00
										 |  |  | 	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); | 
					
						
							| 
									
										
										
										
											2014-01-15 13:16:10 +00:00
										 |  |  | 	ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "support_path", "no", OPT_BOOL_T, 1, FLDSET(struct sip_outbound_registration, support_path)); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-02 12:40:03 +00:00
										 |  |  | 	ast_manager_register_xml("PJSIPUnregister", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, ami_unregister); | 
					
						
							| 
									
										
										
										
											2015-01-06 17:35:21 +00:00
										 |  |  | 	ast_manager_register_xml("PJSIPRegister", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, ami_register); | 
					
						
							| 
									
										
										
										
											2014-06-27 13:50:02 +00:00
										 |  |  | 	ast_manager_register_xml("PJSIPShowRegistrationsOutbound", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, ami_show_outbound_registrations); | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	cli_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL); | 
					
						
							|  |  |  | 	if (!cli_formatter) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Unable to allocate memory for cli formatter\n"); | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 		unload_module(); | 
					
						
							| 
									
										
										
										
											2014-03-08 16:50:36 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	cli_formatter->name = "registration"; | 
					
						
							|  |  |  | 	cli_formatter->print_header = cli_print_header; | 
					
						
							|  |  |  | 	cli_formatter->print_body = cli_print_body; | 
					
						
							|  |  |  | 	cli_formatter->get_container = cli_get_container; | 
					
						
							|  |  |  | 	cli_formatter->iterate = cli_iterator; | 
					
						
							|  |  |  | 	cli_formatter->get_id = ast_sorcery_object_get_id; | 
					
						
							|  |  |  | 	cli_formatter->retrieve_by_id = cli_retrieve_by_id; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_sip_register_cli_formatter(cli_formatter); | 
					
						
							|  |  |  | 	ast_cli_register_multiple(cli_outbound_registration, ARRAY_LEN(cli_outbound_registration)); | 
					
						
							| 
									
										
										
										
											2014-02-06 17:55:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	if (!(new_states = ao2_container_alloc( | 
					
						
							|  |  |  | 		      DEFAULT_STATE_BUCKETS, registration_state_hash, registration_state_cmp))) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Unable to allocate registration states container\n"); | 
					
						
							|  |  |  | 		unload_module(); | 
					
						
							|  |  |  | 		return AST_MODULE_LOAD_FAILURE; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ao2_global_obj_replace_unref(current_states, new_states); | 
					
						
							|  |  |  | 	ao2_ref(new_states, -1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_sorcery_reload_object(ast_sip_get_sorcery(), "registration"); | 
					
						
							|  |  |  | 	if (!(registrations = get_registrations())) { | 
					
						
							|  |  |  | 		unload_module(); | 
					
						
							|  |  |  | 		return AST_MODULE_LOAD_FAILURE; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ao2_ref(registrations, -1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	return AST_MODULE_LOAD_SUCCESS; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int reload_module(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ast_sorcery_reload_object(ast_sip_get_sorcery(), "registration"); | 
					
						
							| 
									
										
										
										
											2014-11-13 22:03:00 +00:00
										 |  |  | 	ao2_cleanup(get_registrations()); | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-30 18:14:50 +00:00
										 |  |  | AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Outbound Registration Support", | 
					
						
							| 
									
										
										
										
											2014-07-25 16:47:17 +00:00
										 |  |  | 		.support_level = AST_MODULE_SUPPORT_CORE, | 
					
						
							| 
									
										
										
										
											2013-04-25 18:25:31 +00:00
										 |  |  | 		.load = load_module, | 
					
						
							|  |  |  | 		.reload = reload_module, | 
					
						
							|  |  |  | 		.unload = unload_module, | 
					
						
							|  |  |  | 		.load_pri = AST_MODPRI_APP_DEPEND, | 
					
						
							|  |  |  | 	       ); |