res_pjsip_outbound_registration.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*** MODULEINFO
  19. <depend>pjproject</depend>
  20. <depend>res_pjsip</depend>
  21. <support_level>core</support_level>
  22. ***/
  23. #include "asterisk.h"
  24. #include <pjsip.h>
  25. #include <pjsip_ua.h>
  26. #include "asterisk/res_pjsip.h"
  27. #include "asterisk/res_pjsip_cli.h"
  28. #include "asterisk/module.h"
  29. #include "asterisk/taskprocessor.h"
  30. #include "asterisk/cli.h"
  31. #include "asterisk/stasis_system.h"
  32. #include "res_pjsip/include/res_pjsip_private.h"
  33. /*** DOCUMENTATION
  34. <configInfo name="res_pjsip_outbound_registration" language="en_US">
  35. <synopsis>SIP resource for outbound registrations</synopsis>
  36. <description><para>
  37. <emphasis>Outbound Registration</emphasis>
  38. </para>
  39. <para>This module allows <literal>res_pjsip</literal> to register to other SIP servers.</para>
  40. </description>
  41. <configFile name="pjsip.conf">
  42. <configObject name="registration">
  43. <synopsis>The configuration for outbound registration</synopsis>
  44. <description><para>
  45. Registration is <emphasis>COMPLETELY</emphasis> separate from the rest of
  46. <literal>pjsip.conf</literal>. A minimal configuration consists of
  47. setting a <literal>server_uri</literal> and a <literal>client_uri</literal>.
  48. </para></description>
  49. <configOption name="auth_rejection_permanent" default="yes">
  50. <synopsis>Determines whether failed authentication challenges are treated
  51. as permanent failures.</synopsis>
  52. <description><para>If this option is enabled and an authentication challenge fails,
  53. registration will not be attempted again until the configuration is reloaded.</para></description>
  54. </configOption>
  55. <configOption name="client_uri">
  56. <synopsis>Client SIP URI used when attemping outbound registration</synopsis>
  57. <description><para>
  58. This is the address-of-record for the outbound registration (i.e. the URI in
  59. the To header of the REGISTER).</para>
  60. <para>For registration with an ITSP, the client SIP URI may need to consist of
  61. an account name or number and the provider's hostname for their registrar, e.g.
  62. client_uri=1234567890@example.com. This may differ between providers.</para>
  63. <para>For registration to generic registrars, the client SIP URI will depend
  64. on networking specifics and configuration of the registrar.
  65. </para></description>
  66. </configOption>
  67. <configOption name="contact_user">
  68. <synopsis>Contact User to use in request</synopsis>
  69. </configOption>
  70. <configOption name="expiration" default="3600">
  71. <synopsis>Expiration time for registrations in seconds</synopsis>
  72. </configOption>
  73. <configOption name="max_retries" default="10">
  74. <synopsis>Maximum number of registration attempts.</synopsis>
  75. </configOption>
  76. <configOption name="outbound_auth" default="">
  77. <synopsis>Authentication object to be used for outbound registrations.</synopsis>
  78. </configOption>
  79. <configOption name="outbound_proxy" default="">
  80. <synopsis>Outbound Proxy used to send registrations</synopsis>
  81. </configOption>
  82. <configOption name="retry_interval" default="60">
  83. <synopsis>Interval in seconds between retries if outbound registration is unsuccessful</synopsis>
  84. </configOption>
  85. <configOption name="forbidden_retry_interval" default="0">
  86. <synopsis>Interval used when receiving a 403 Forbidden response.</synopsis>
  87. <description><para>
  88. If a 403 Forbidden is received, chan_pjsip will wait
  89. <replaceable>forbidden_retry_interval</replaceable> seconds before
  90. attempting registration again. If 0 is specified, chan_pjsip will not
  91. retry after receiving a 403 Forbidden response. Setting this to a non-zero
  92. value goes against a "SHOULD NOT" in RFC3261, but can be used to work around
  93. buggy registrars.
  94. </para></description>
  95. </configOption>
  96. <configOption name="server_uri">
  97. <synopsis>SIP URI of the server to register against</synopsis>
  98. <description><para>
  99. This is the URI at which to find the registrar to send the outbound REGISTER. This URI
  100. is used as the request URI of the outbound REGISTER request from Asterisk.</para>
  101. <para>For registration with an ITSP, the setting may often be just the domain of
  102. the registrar, e.g. sip:sip.example.com.
  103. </para></description>
  104. </configOption>
  105. <configOption name="transport">
  106. <synopsis>Transport used for outbound authentication</synopsis>
  107. <description>
  108. <note><para>A <replaceable>transport</replaceable> configured in
  109. <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>
  110. </description>
  111. </configOption>
  112. <configOption name="type">
  113. <synopsis>Must be of type 'registration'.</synopsis>
  114. </configOption>
  115. <configOption name="support_path">
  116. <synopsis>Enables Path support for outbound REGISTER requests.</synopsis>
  117. <description><para>
  118. When this option is enabled, outbound REGISTER requests will advertise
  119. support for Path headers so that intervening proxies can add to the Path
  120. header as necessary.
  121. </para></description>
  122. </configOption>
  123. </configObject>
  124. </configFile>
  125. </configInfo>
  126. <manager name="PJSIPUnregister" language="en_US">
  127. <synopsis>
  128. Unregister an outbound registration.
  129. </synopsis>
  130. <syntax>
  131. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  132. <parameter name="Registration" required="true">
  133. <para>The outbound registration to unregister.</para>
  134. </parameter>
  135. </syntax>
  136. </manager>
  137. <manager name="PJSIPShowRegistrationsOutbound" language="en_US">
  138. <synopsis>
  139. Lists PJSIP outbound registrations.
  140. </synopsis>
  141. <syntax />
  142. <description>
  143. <para>
  144. In response <literal>OutboundRegistrationDetail</literal> events showing configuration and status
  145. information are raised for each outbound registration object. <literal>AuthDetail</literal>
  146. events are raised for each associated auth object as well. Once all events are completed an
  147. <literal>OutboundRegistrationDetailComplete</literal> is issued.
  148. </para>
  149. </description>
  150. </manager>
  151. ***/
  152. /*! \brief Amount of buffer time (in seconds) before expiration that we re-register at */
  153. #define REREGISTER_BUFFER_TIME 10
  154. /*! \brief Various states that an outbound registration may be in */
  155. enum sip_outbound_registration_status {
  156. /*! \brief Currently unregistered */
  157. SIP_REGISTRATION_UNREGISTERED = 0,
  158. /*! \brief Registered, yay! */
  159. SIP_REGISTRATION_REGISTERED,
  160. /*! \brief Registration was rejected, but response was temporal */
  161. SIP_REGISTRATION_REJECTED_TEMPORARY,
  162. /*! \brief Registration was rejected, permanently */
  163. SIP_REGISTRATION_REJECTED_PERMANENT,
  164. /*! \brief Registration has been stopped */
  165. SIP_REGISTRATION_STOPPED,
  166. };
  167. static const char *sip_outbound_registration_status_str[] = {
  168. [SIP_REGISTRATION_UNREGISTERED] = "Unregistered",
  169. [SIP_REGISTRATION_REGISTERED] = "Registered",
  170. [SIP_REGISTRATION_REJECTED_TEMPORARY] = "Rejected",
  171. [SIP_REGISTRATION_REJECTED_PERMANENT] = "Rejected",
  172. [SIP_REGISTRATION_STOPPED] = "Stopped",
  173. };
  174. /*! \brief Outbound registration client state information (persists for lifetime of regc) */
  175. struct sip_outbound_registration_client_state {
  176. /*! \brief Current status of this registration */
  177. enum sip_outbound_registration_status status;
  178. /*! \brief Outbound registration client */
  179. pjsip_regc *client;
  180. /*! \brief Timer entry for retrying on temporal responses */
  181. pj_timer_entry timer;
  182. /*! \brief Current number of retries */
  183. unsigned int retries;
  184. /*! \brief Maximum number of retries permitted */
  185. unsigned int max_retries;
  186. /*! \brief Interval at which retries should occur for temporal responses */
  187. unsigned int retry_interval;
  188. /*! \brief Interval at which retries should occur for permanent responses */
  189. unsigned int forbidden_retry_interval;
  190. /*! \brief Treat authentication challenges that we cannot handle as permanent failures */
  191. unsigned int auth_rejection_permanent;
  192. /*! \brief Determines whether SIP Path support should be advertised */
  193. unsigned int support_path;
  194. /*! \brief Serializer for stuff and things */
  195. struct ast_taskprocessor *serializer;
  196. /*! \brief Configured authentication credentials */
  197. struct ast_sip_auth_vector outbound_auths;
  198. /*! \brief Registration should be destroyed after completion of transaction */
  199. unsigned int destroy:1;
  200. };
  201. /*! \brief Outbound registration state information (persists for lifetime that registration should exist) */
  202. struct sip_outbound_registration_state {
  203. /*! \brief Client state information */
  204. struct sip_outbound_registration_client_state *client_state;
  205. };
  206. /*! \brief Outbound registration information */
  207. struct sip_outbound_registration {
  208. /*! \brief Sorcery object details */
  209. SORCERY_OBJECT(details);
  210. /*! \brief Stringfields */
  211. AST_DECLARE_STRING_FIELDS(
  212. /*! \brief URI for the registrar */
  213. AST_STRING_FIELD(server_uri);
  214. /*! \brief URI for the AOR */
  215. AST_STRING_FIELD(client_uri);
  216. /*! \brief Optional user for contact header */
  217. AST_STRING_FIELD(contact_user);
  218. /*! \brief Explicit transport to use for registration */
  219. AST_STRING_FIELD(transport);
  220. /*! \brief Outbound proxy to use */
  221. AST_STRING_FIELD(outbound_proxy);
  222. );
  223. /*! \brief Requested expiration time */
  224. unsigned int expiration;
  225. /*! \brief Interval at which retries should occur for temporal responses */
  226. unsigned int retry_interval;
  227. /*! \brief Interval at which retries should occur for permanent responses */
  228. unsigned int forbidden_retry_interval;
  229. /*! \brief Treat authentication challenges that we cannot handle as permanent failures */
  230. unsigned int auth_rejection_permanent;
  231. /*! \brief Maximum number of retries permitted */
  232. unsigned int max_retries;
  233. /*! \brief Outbound registration state */
  234. struct sip_outbound_registration_state *state;
  235. /*! \brief Configured authentication credentials */
  236. struct ast_sip_auth_vector outbound_auths;
  237. /*! \brief Whether Path support is enabled */
  238. unsigned int support_path;
  239. };
  240. /*! \brief Helper function which cancels the timer on a client */
  241. static void cancel_registration(struct sip_outbound_registration_client_state *client_state)
  242. {
  243. if (pj_timer_heap_cancel(pjsip_endpt_get_timer_heap(ast_sip_get_pjsip_endpoint()), &client_state->timer)) {
  244. /* The timer was successfully cancelled, drop the refcount of client_state */
  245. ao2_ref(client_state, -1);
  246. }
  247. }
  248. static pj_str_t PATH_NAME = { "path", 4 };
  249. /*! \brief Callback function for registering */
  250. static int handle_client_registration(void *data)
  251. {
  252. RAII_VAR(struct sip_outbound_registration_client_state *, client_state, data, ao2_cleanup);
  253. pjsip_tx_data *tdata;
  254. pjsip_regc_info info;
  255. char server_uri[PJSIP_MAX_URL_SIZE], client_uri[PJSIP_MAX_URL_SIZE];
  256. cancel_registration(client_state);
  257. if ((client_state->status == SIP_REGISTRATION_STOPPED) ||
  258. (pjsip_regc_register(client_state->client, PJ_FALSE, &tdata) != PJ_SUCCESS)) {
  259. return 0;
  260. }
  261. pjsip_regc_get_info(client_state->client, &info);
  262. ast_copy_pj_str(server_uri, &info.server_uri, sizeof(server_uri));
  263. ast_copy_pj_str(client_uri, &info.client_uri, sizeof(client_uri));
  264. ast_debug(3, "REGISTER attempt %u to '%s' with client '%s'\n",
  265. client_state->retries + 1, server_uri, client_uri);
  266. if (client_state->support_path) {
  267. pjsip_supported_hdr *hdr;
  268. hdr = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_SUPPORTED, NULL);
  269. if (!hdr) {
  270. /* insert a new Supported header */
  271. hdr = pjsip_supported_hdr_create(tdata->pool);
  272. if (!hdr) {
  273. return -1;
  274. }
  275. pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *)hdr);
  276. }
  277. /* add on to the existing Supported header */
  278. pj_strassign(&hdr->values[hdr->count++], &PATH_NAME);
  279. }
  280. /* Due to the registration the callback may now get called, so bump the ref count */
  281. ao2_ref(client_state, +1);
  282. if (pjsip_regc_send(client_state->client, tdata) != PJ_SUCCESS) {
  283. ao2_ref(client_state, -1);
  284. }
  285. return 0;
  286. }
  287. /*! \brief Timer callback function, used just for registrations */
  288. static void sip_outbound_registration_timer_cb(pj_timer_heap_t *timer_heap, struct pj_timer_entry *entry)
  289. {
  290. struct sip_outbound_registration_client_state *client_state = entry->user_data;
  291. ao2_ref(client_state, +1);
  292. if (ast_sip_push_task(client_state->serializer, handle_client_registration, client_state)) {
  293. ast_log(LOG_WARNING, "Failed to pass outbound registration to threadpool\n");
  294. ao2_ref(client_state, -1);
  295. }
  296. entry->id = 0;
  297. }
  298. /*! \brief Helper function which sets up the timer to re-register in a specific amount of time */
  299. static void schedule_registration(struct sip_outbound_registration_client_state *client_state, unsigned int seconds)
  300. {
  301. pj_time_val delay = { .sec = seconds, };
  302. cancel_registration(client_state);
  303. ao2_ref(client_state, +1);
  304. if (pjsip_endpt_schedule_timer(ast_sip_get_pjsip_endpoint(), &client_state->timer, &delay) != PJ_SUCCESS) {
  305. ast_log(LOG_WARNING, "Failed to pass timed registration to scheduler\n");
  306. ao2_ref(client_state, -1);
  307. }
  308. }
  309. /*! \brief Callback function for unregistering (potentially) and destroying state */
  310. static int handle_client_state_destruction(void *data)
  311. {
  312. RAII_VAR(struct sip_outbound_registration_client_state *, client_state, data, ao2_cleanup);
  313. cancel_registration(client_state);
  314. if (client_state->client) {
  315. pjsip_regc_info info;
  316. pjsip_regc_get_info(client_state->client, &info);
  317. if (info.is_busy == PJ_TRUE) {
  318. /* If a client transaction is in progress we defer until it is complete */
  319. client_state->destroy = 1;
  320. return 0;
  321. }
  322. if (client_state->status != SIP_REGISTRATION_UNREGISTERED && client_state->status != SIP_REGISTRATION_REJECTED_PERMANENT) {
  323. pjsip_tx_data *tdata;
  324. if (pjsip_regc_unregister(client_state->client, &tdata) == PJ_SUCCESS) {
  325. pjsip_regc_send(client_state->client, tdata);
  326. }
  327. }
  328. pjsip_regc_destroy(client_state->client);
  329. }
  330. client_state->status = SIP_REGISTRATION_STOPPED;
  331. ast_sip_auth_vector_destroy(&client_state->outbound_auths);
  332. return 0;
  333. }
  334. /*! \brief Structure for registration response */
  335. struct registration_response {
  336. /*! \brief Response code for the registration attempt */
  337. int code;
  338. /*! \brief Expiration time for registration */
  339. int expiration;
  340. /*! \brief Retry-After value */
  341. int retry_after;
  342. /*! \brief Outbound registration client state */
  343. struct sip_outbound_registration_client_state *client_state;
  344. /*! \brief The response message */
  345. pjsip_rx_data *rdata;
  346. /*! \brief The response transaction */
  347. pjsip_transaction *tsx;
  348. };
  349. /*! \brief Registration response structure destructor */
  350. static void registration_response_destroy(void *obj)
  351. {
  352. struct registration_response *response = obj;
  353. if (response->rdata) {
  354. pjsip_rx_data_free_cloned(response->rdata);
  355. }
  356. ao2_cleanup(response->client_state);
  357. }
  358. /* \brief Helper funtion which determines if a response code is temporal or not */
  359. static int sip_outbound_registration_is_temporal(unsigned int code,
  360. struct sip_outbound_registration_client_state *client_state)
  361. {
  362. /* Shamelessly taken from pjsua */
  363. if (code == PJSIP_SC_REQUEST_TIMEOUT ||
  364. code == PJSIP_SC_INTERNAL_SERVER_ERROR ||
  365. code == PJSIP_SC_BAD_GATEWAY ||
  366. code == PJSIP_SC_SERVICE_UNAVAILABLE ||
  367. code == PJSIP_SC_SERVER_TIMEOUT ||
  368. ((code == PJSIP_SC_UNAUTHORIZED ||
  369. code == PJSIP_SC_PROXY_AUTHENTICATION_REQUIRED) &&
  370. !client_state->auth_rejection_permanent) ||
  371. PJSIP_IS_STATUS_IN_CLASS(code, 600)) {
  372. return 1;
  373. } else {
  374. return 0;
  375. }
  376. }
  377. static void schedule_retry(struct registration_response *response, unsigned int interval,
  378. const char *server_uri, const char *client_uri)
  379. {
  380. response->client_state->status = SIP_REGISTRATION_REJECTED_TEMPORARY;
  381. schedule_registration(response->client_state, interval);
  382. if (response->rdata) {
  383. ast_log(LOG_WARNING, "Temporal response '%d' received from '%s' on "
  384. "registration attempt to '%s', retrying in '%u'\n",
  385. response->code, server_uri, client_uri, interval);
  386. } else {
  387. ast_log(LOG_WARNING, "No response received from '%s' on "
  388. "registration attempt to '%s', retrying in '%u'\n",
  389. server_uri, client_uri, interval);
  390. }
  391. }
  392. /*! \brief Callback function for handling a response to a registration attempt */
  393. static int handle_registration_response(void *data)
  394. {
  395. RAII_VAR(struct registration_response *, response, data, ao2_cleanup);
  396. pjsip_regc_info info;
  397. char server_uri[PJSIP_MAX_URL_SIZE], client_uri[PJSIP_MAX_URL_SIZE];
  398. if (response->client_state->status == SIP_REGISTRATION_STOPPED) {
  399. return 0;
  400. }
  401. pjsip_regc_get_info(response->client_state->client, &info);
  402. ast_copy_pj_str(server_uri, &info.server_uri, sizeof(server_uri));
  403. ast_copy_pj_str(client_uri, &info.client_uri, sizeof(client_uri));
  404. if (response->code == 401 || response->code == 407) {
  405. pjsip_tx_data *tdata;
  406. if (!ast_sip_create_request_with_auth(&response->client_state->outbound_auths,
  407. response->rdata, response->tsx, &tdata)) {
  408. ao2_ref(response->client_state, +1);
  409. if (pjsip_regc_send(response->client_state->client, tdata) != PJ_SUCCESS) {
  410. ao2_cleanup(response->client_state);
  411. }
  412. return 0;
  413. }
  414. /* Otherwise, fall through so the failure is processed appropriately */
  415. }
  416. if (PJSIP_IS_STATUS_IN_CLASS(response->code, 200)) {
  417. /* If the registration went fine simply reschedule registration for the future */
  418. ast_debug(1, "Outbound registration to '%s' with client '%s' successful\n", server_uri, client_uri);
  419. response->client_state->status = SIP_REGISTRATION_REGISTERED;
  420. response->client_state->retries = 0;
  421. schedule_registration(response->client_state, response->expiration - REREGISTER_BUFFER_TIME);
  422. } else if (response->retry_after) {
  423. /* If we have been instructed to retry after a period of time, schedule it as such */
  424. schedule_retry(response, response->retry_after, server_uri, client_uri);
  425. } else if (response->client_state->retry_interval && sip_outbound_registration_is_temporal(response->code, response->client_state)) {
  426. if (response->client_state->retries == response->client_state->max_retries) {
  427. /* If we received enough temporal responses to exceed our maximum give up permanently */
  428. response->client_state->status = SIP_REGISTRATION_REJECTED_PERMANENT;
  429. ast_log(LOG_WARNING, "Maximum retries reached when attempting outbound registration to '%s' with client '%s', stopping registration attempt\n",
  430. server_uri, client_uri);
  431. } else {
  432. /* On the other hand if we can still try some more do so */
  433. response->client_state->retries++;
  434. schedule_retry(response, response->client_state->retry_interval, server_uri, client_uri);
  435. }
  436. } else {
  437. if (response->code == 403
  438. && response->client_state->forbidden_retry_interval
  439. && response->client_state->retries < response->client_state->max_retries) {
  440. /* A forbidden response retry interval is configured and there are retries remaining */
  441. response->client_state->status = SIP_REGISTRATION_REJECTED_TEMPORARY;
  442. response->client_state->retries++;
  443. schedule_registration(response->client_state, response->client_state->forbidden_retry_interval);
  444. ast_log(LOG_WARNING, "403 Forbidden fatal response received from '%s' on registration attempt to '%s', retrying in '%u' seconds\n",
  445. server_uri, client_uri, response->client_state->forbidden_retry_interval);
  446. } else {
  447. /* Finally if there's no hope of registering give up */
  448. response->client_state->status = SIP_REGISTRATION_REJECTED_PERMANENT;
  449. if (response->rdata) {
  450. ast_log(LOG_WARNING, "Fatal response '%d' received from '%s' on registration attempt to '%s', stopping outbound registration\n",
  451. response->code, server_uri, client_uri);
  452. } else {
  453. ast_log(LOG_WARNING, "Fatal registration attempt to '%s', stopping outbound registration\n", client_uri);
  454. }
  455. }
  456. }
  457. ast_system_publish_registry("PJSIP", client_uri, server_uri, sip_outbound_registration_status_str[response->client_state->status], NULL);
  458. /* If deferred destruction is in use see if we need to destroy now */
  459. if (response->client_state->destroy) {
  460. handle_client_state_destruction(response->client_state);
  461. }
  462. return 0;
  463. }
  464. /*! \brief Callback function for outbound registration client */
  465. static void sip_outbound_registration_response_cb(struct pjsip_regc_cbparam *param)
  466. {
  467. RAII_VAR(struct sip_outbound_registration_client_state *, client_state, param->token, ao2_cleanup);
  468. struct registration_response *response = ao2_alloc(sizeof(*response), registration_response_destroy);
  469. response->code = param->code;
  470. response->expiration = param->expiration;
  471. response->client_state = client_state;
  472. ao2_ref(response->client_state, +1);
  473. if (param->rdata) {
  474. struct pjsip_retry_after_hdr *retry_after = pjsip_msg_find_hdr(param->rdata->msg_info.msg, PJSIP_H_RETRY_AFTER, NULL);
  475. response->retry_after = retry_after ? retry_after->ivalue : 0;
  476. response->tsx = pjsip_rdata_get_tsx(param->rdata);
  477. pjsip_rx_data_clone(param->rdata, 0, &response->rdata);
  478. }
  479. if (ast_sip_push_task(client_state->serializer, handle_registration_response, response)) {
  480. ast_log(LOG_WARNING, "Failed to pass incoming registration response to threadpool\n");
  481. ao2_cleanup(response);
  482. }
  483. }
  484. /*! \brief Destructor function for registration state */
  485. static void sip_outbound_registration_state_destroy(void *obj)
  486. {
  487. struct sip_outbound_registration_state *state = obj;
  488. if (!state->client_state) {
  489. return;
  490. }
  491. if (state->client_state->serializer && ast_sip_push_task(state->client_state->serializer, handle_client_state_destruction, state->client_state)) {
  492. ast_log(LOG_WARNING, "Failed to pass outbound registration client destruction to threadpool\n");
  493. ao2_ref(state->client_state, -1);
  494. }
  495. }
  496. /*! \brief Destructor function for client registration state */
  497. static void sip_outbound_registration_client_state_destroy(void *obj)
  498. {
  499. struct sip_outbound_registration_client_state *client_state = obj;
  500. ast_taskprocessor_unreference(client_state->serializer);
  501. }
  502. /*! \brief Allocator function for registration state */
  503. static struct sip_outbound_registration_state *sip_outbound_registration_state_alloc(void)
  504. {
  505. struct sip_outbound_registration_state *state = ao2_alloc(sizeof(*state), sip_outbound_registration_state_destroy);
  506. if (!state || !(state->client_state = ao2_alloc(sizeof(*state->client_state), sip_outbound_registration_client_state_destroy))) {
  507. ao2_cleanup(state);
  508. return NULL;
  509. }
  510. if (!(state->client_state->serializer = ast_sip_create_serializer())) {
  511. ao2_cleanup(state->client_state);
  512. ao2_cleanup(state);
  513. return NULL;
  514. }
  515. state->client_state->status = SIP_REGISTRATION_UNREGISTERED;
  516. state->client_state->timer.user_data = state->client_state;
  517. state->client_state->timer.cb = sip_outbound_registration_timer_cb;
  518. return state;
  519. }
  520. /*! \brief Destructor function for registration information */
  521. static void sip_outbound_registration_destroy(void *obj)
  522. {
  523. struct sip_outbound_registration *registration = obj;
  524. ao2_cleanup(registration->state);
  525. ast_sip_auth_vector_destroy(&registration->outbound_auths);
  526. ast_string_field_free_memory(registration);
  527. }
  528. /*! \brief Allocator function for registration information */
  529. static void *sip_outbound_registration_alloc(const char *name)
  530. {
  531. struct sip_outbound_registration *registration = ast_sorcery_generic_alloc(sizeof(*registration), sip_outbound_registration_destroy);
  532. if (!registration || ast_string_field_init(registration, 256)) {
  533. ao2_cleanup(registration);
  534. return NULL;
  535. }
  536. return registration;
  537. }
  538. /*! \brief Helper function which populates a pj_str_t with a contact header */
  539. 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)
  540. {
  541. pj_str_t tmp, local_addr;
  542. pjsip_uri *uri;
  543. pjsip_sip_uri *sip_uri;
  544. pjsip_transport_type_e type = PJSIP_TRANSPORT_UNSPECIFIED;
  545. int local_port;
  546. pj_strdup_with_null(pool, &tmp, target);
  547. if (!(uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0)) ||
  548. (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))) {
  549. return -1;
  550. }
  551. sip_uri = pjsip_uri_get_uri(uri);
  552. if (PJSIP_URI_SCHEME_IS_SIPS(sip_uri)) {
  553. type = PJSIP_TRANSPORT_TLS;
  554. } else if (!sip_uri->transport_param.slen) {
  555. type = PJSIP_TRANSPORT_UDP;
  556. } else {
  557. type = pjsip_transport_get_type_from_name(&sip_uri->transport_param);
  558. }
  559. if (type == PJSIP_TRANSPORT_UNSPECIFIED) {
  560. return -1;
  561. }
  562. if (pj_strchr(&sip_uri->host, ':')) {
  563. type = (pjsip_transport_type_e)(((int)type) + PJSIP_TRANSPORT_IPV6);
  564. }
  565. if (pjsip_tpmgr_find_local_addr(pjsip_endpt_get_tpmgr(ast_sip_get_pjsip_endpoint()), pool, type, selector,
  566. &local_addr, &local_port) != PJ_SUCCESS) {
  567. return -1;
  568. }
  569. if (!pj_strchr(&sip_uri->host, ':') && pj_strchr(&local_addr, ':')) {
  570. type = (pjsip_transport_type_e)(((int)type) + PJSIP_TRANSPORT_IPV6);
  571. }
  572. contact->ptr = pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE);
  573. contact->slen = pj_ansi_snprintf(contact->ptr, PJSIP_MAX_URL_SIZE,
  574. "<%s:%s@%s%.*s%s:%d%s%s>",
  575. (pjsip_transport_get_flag_from_type(type) & PJSIP_TRANSPORT_SECURE) ? "sips" : "sip",
  576. user,
  577. (type & PJSIP_TRANSPORT_IPV6) ? "[" : "",
  578. (int)local_addr.slen,
  579. local_addr.ptr,
  580. (type & PJSIP_TRANSPORT_IPV6) ? "]" : "",
  581. local_port,
  582. (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? ";transport=" : "",
  583. (type != PJSIP_TRANSPORT_UDP && type != PJSIP_TRANSPORT_UDP6) ? pjsip_transport_get_type_name(type) : "");
  584. return 0;
  585. }
  586. /*!
  587. * \internal
  588. * \brief Check if a registration can be reused
  589. *
  590. * This checks if the existing outbound registration's configuration differs from a newly-applied
  591. * outbound registration to see if the applied one.
  592. *
  593. * \param existing The pre-existing outbound registration
  594. * \param applied The newly-created registration
  595. */
  596. static int can_reuse_registration(struct sip_outbound_registration *existing, struct sip_outbound_registration *applied)
  597. {
  598. int i;
  599. if (strcmp(existing->server_uri, applied->server_uri) || strcmp(existing->client_uri, applied->client_uri) ||
  600. strcmp(existing->transport, applied->transport) || strcmp(existing->contact_user, applied->contact_user) ||
  601. strcmp(existing->outbound_proxy, applied->outbound_proxy) ||
  602. AST_VECTOR_SIZE(&existing->outbound_auths) != AST_VECTOR_SIZE(&applied->outbound_auths) ||
  603. existing->auth_rejection_permanent != applied->auth_rejection_permanent) {
  604. return 0;
  605. }
  606. for (i = 0; i < AST_VECTOR_SIZE(&existing->outbound_auths); ++i) {
  607. if (strcmp(AST_VECTOR_GET(&existing->outbound_auths, i), AST_VECTOR_GET(&applied->outbound_auths, i))) {
  608. return 0;
  609. }
  610. }
  611. return 1;
  612. }
  613. /*! \brief Helper function that allocates a pjsip registration client and configures it */
  614. static int sip_outbound_registration_regc_alloc(void *data)
  615. {
  616. struct sip_outbound_registration *registration = data;
  617. pj_pool_t *pool;
  618. pj_str_t tmp;
  619. pjsip_uri *uri;
  620. pj_str_t server_uri, client_uri, contact_uri;
  621. pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
  622. pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "URI Validation", 256, 256);
  623. if (!pool) {
  624. ast_log(LOG_ERROR, "Could not create pool for URI validation on outbound registration '%s'\n",
  625. ast_sorcery_object_get_id(registration));
  626. return -1;
  627. }
  628. pj_strdup2_with_null(pool, &tmp, registration->server_uri);
  629. uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
  630. if (!uri) {
  631. ast_log(LOG_ERROR, "Invalid server URI '%s' specified on outbound registration '%s'\n",
  632. registration->server_uri, ast_sorcery_object_get_id(registration));
  633. pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
  634. return -1;
  635. }
  636. pj_strdup2_with_null(pool, &tmp, registration->client_uri);
  637. uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
  638. if (!uri) {
  639. ast_log(LOG_ERROR, "Invalid client URI '%s' specified on outbound registration '%s'\n",
  640. registration->client_uri, ast_sorcery_object_get_id(registration));
  641. pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
  642. return -1;
  643. }
  644. pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
  645. if (!ast_strlen_zero(registration->transport)) {
  646. RAII_VAR(struct ast_sip_transport *, transport, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport", registration->transport), ao2_cleanup);
  647. if (!transport || !transport->state) {
  648. ast_log(LOG_ERROR, "Unable to retrieve PJSIP transport '%s' "
  649. " for outbound registration", registration->transport);
  650. return -1;
  651. }
  652. if (transport->state->transport) {
  653. selector.type = PJSIP_TPSELECTOR_TRANSPORT;
  654. selector.u.transport = transport->state->transport;
  655. } else if (transport->state->factory) {
  656. selector.type = PJSIP_TPSELECTOR_LISTENER;
  657. selector.u.listener = transport->state->factory;
  658. } else {
  659. return -1;
  660. }
  661. }
  662. if (!registration->state->client_state->client &&
  663. pjsip_regc_create(ast_sip_get_pjsip_endpoint(), registration->state->client_state, sip_outbound_registration_response_cb,
  664. &registration->state->client_state->client) != PJ_SUCCESS) {
  665. return -1;
  666. }
  667. pjsip_regc_set_transport(registration->state->client_state->client, &selector);
  668. if (!ast_strlen_zero(registration->outbound_proxy)) {
  669. pjsip_route_hdr route_set, *route;
  670. static const pj_str_t ROUTE_HNAME = { "Route", 5 };
  671. pj_str_t tmp;
  672. pj_list_init(&route_set);
  673. pj_strdup2_with_null(pjsip_regc_get_pool(registration->state->client_state->client), &tmp, registration->outbound_proxy);
  674. if (!(route = pjsip_parse_hdr(pjsip_regc_get_pool(registration->state->client_state->client), &ROUTE_HNAME, tmp.ptr, tmp.slen, NULL))) {
  675. return -1;
  676. }
  677. pj_list_insert_nodes_before(&route_set, route);
  678. pjsip_regc_set_route_set(registration->state->client_state->client, &route_set);
  679. }
  680. pj_cstr(&server_uri, registration->server_uri);
  681. if (sip_dialog_create_contact(pjsip_regc_get_pool(registration->state->client_state->client), &contact_uri, S_OR(registration->contact_user, "s"), &server_uri, &selector)) {
  682. return -1;
  683. }
  684. pj_cstr(&client_uri, registration->client_uri);
  685. if (pjsip_regc_init(registration->state->client_state->client, &server_uri, &client_uri, &client_uri, 1, &contact_uri, registration->expiration) != PJ_SUCCESS) {
  686. return -1;
  687. }
  688. return 0;
  689. }
  690. /*! \brief Apply function which finds or allocates a state structure */
  691. static int sip_outbound_registration_apply(const struct ast_sorcery *sorcery, void *obj)
  692. {
  693. RAII_VAR(struct sip_outbound_registration *, existing, ast_sorcery_retrieve_by_id(sorcery, "registration", ast_sorcery_object_get_id(obj)), ao2_cleanup);
  694. struct sip_outbound_registration *applied = obj;
  695. if (ast_strlen_zero(applied->server_uri)) {
  696. ast_log(LOG_ERROR, "No server URI specified on outbound registration '%s'",
  697. ast_sorcery_object_get_id(applied));
  698. return -1;
  699. } else if (ast_strlen_zero(applied->client_uri)) {
  700. ast_log(LOG_ERROR, "No client URI specified on outbound registration '%s'\n",
  701. ast_sorcery_object_get_id(applied));
  702. return -1;
  703. }
  704. if (!existing) {
  705. /* If no existing registration exists we can just start fresh easily */
  706. applied->state = sip_outbound_registration_state_alloc();
  707. } else {
  708. /* If there is an existing registration things are more complicated, we can immediately reuse this state if most stuff remains unchanged */
  709. if (can_reuse_registration(existing, applied)) {
  710. applied->state = existing->state;
  711. ao2_ref(applied->state, +1);
  712. return 0;
  713. }
  714. applied->state = sip_outbound_registration_state_alloc();
  715. }
  716. if (!applied->state) {
  717. return -1;
  718. }
  719. return ast_sip_push_task_synchronous(NULL, sip_outbound_registration_regc_alloc, applied);
  720. }
  721. /*! \brief Helper function which performs a single registration */
  722. static int sip_outbound_registration_perform(void *data)
  723. {
  724. RAII_VAR(struct sip_outbound_registration *, registration, data, ao2_cleanup);
  725. size_t i;
  726. /* Just in case the client state is being reused for this registration, free the auth information */
  727. ast_sip_auth_vector_destroy(&registration->state->client_state->outbound_auths);
  728. AST_VECTOR_INIT(&registration->state->client_state->outbound_auths, AST_VECTOR_SIZE(&registration->outbound_auths));
  729. for (i = 0; i < AST_VECTOR_SIZE(&registration->outbound_auths); ++i) {
  730. const char *name = ast_strdup(AST_VECTOR_GET(&registration->outbound_auths, i));
  731. AST_VECTOR_APPEND(&registration->state->client_state->outbound_auths, name);
  732. }
  733. registration->state->client_state->retry_interval = registration->retry_interval;
  734. registration->state->client_state->forbidden_retry_interval = registration->forbidden_retry_interval;
  735. registration->state->client_state->max_retries = registration->max_retries;
  736. registration->state->client_state->retries = 0;
  737. registration->state->client_state->support_path = registration->support_path;
  738. registration->state->client_state->auth_rejection_permanent = registration->auth_rejection_permanent;
  739. pjsip_regc_update_expires(registration->state->client_state->client, registration->expiration);
  740. schedule_registration(registration->state->client_state, (ast_random() % 10) + 1);
  741. return 0;
  742. }
  743. /*! \brief Helper function which performs all registrations */
  744. static void sip_outbound_registration_perform_all(void)
  745. {
  746. RAII_VAR(struct ao2_container *, registrations, ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "registration", AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL), ao2_cleanup);
  747. struct ao2_iterator i;
  748. struct sip_outbound_registration *registration;
  749. if (!registrations) {
  750. return;
  751. }
  752. i = ao2_iterator_init(registrations, 0);
  753. while ((registration = ao2_iterator_next(&i))) {
  754. if (ast_sip_push_task(registration->state->client_state->serializer, sip_outbound_registration_perform, registration)) {
  755. ast_log(LOG_ERROR, "Failed to perform outbound registration on '%s'\n", ast_sorcery_object_get_id(registration));
  756. ao2_ref(registration, -1);
  757. }
  758. }
  759. ao2_iterator_destroy(&i);
  760. }
  761. static int outbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  762. {
  763. struct sip_outbound_registration *registration = obj;
  764. return ast_sip_auth_vector_init(&registration->outbound_auths, var->value);
  765. }
  766. static int outbound_auths_to_str(const void *obj, const intptr_t *args, char **buf)
  767. {
  768. const struct sip_outbound_registration *registration = obj;
  769. return ast_sip_auths_to_str(&registration->outbound_auths, buf);
  770. }
  771. static int outbound_auths_to_var_list(const void *obj, struct ast_variable **fields)
  772. {
  773. const struct sip_outbound_registration *registration = obj;
  774. int i;
  775. struct ast_variable *head = NULL;
  776. for (i = 0; i < AST_VECTOR_SIZE(&registration->outbound_auths) ; i++) {
  777. ast_variable_list_append(&head, ast_variable_new("outbound_auth",
  778. AST_VECTOR_GET(&registration->outbound_auths, i), ""));
  779. }
  780. if (head) {
  781. *fields = head;
  782. }
  783. return 0;
  784. }
  785. static struct sip_outbound_registration *retrieve_registration(const char *registration_name)
  786. {
  787. return ast_sorcery_retrieve_by_id(
  788. ast_sip_get_sorcery(),
  789. "registration",
  790. registration_name);
  791. }
  792. static int unregister_task(void *obj)
  793. {
  794. RAII_VAR(struct sip_outbound_registration*, registration, obj, ao2_cleanup);
  795. struct pjsip_regc *client = registration->state->client_state->client;
  796. pjsip_tx_data *tdata;
  797. if (pjsip_regc_unregister(client, &tdata) != PJ_SUCCESS) {
  798. return 0;
  799. }
  800. ao2_ref(registration->state->client_state, +1);
  801. if (pjsip_regc_send(client, tdata) != PJ_SUCCESS) {
  802. ao2_cleanup(registration->state->client_state);
  803. }
  804. return 0;
  805. }
  806. static int queue_unregister(struct sip_outbound_registration *registration)
  807. {
  808. ao2_ref(registration, +1);
  809. if (ast_sip_push_task(registration->state->client_state->serializer, unregister_task, registration)) {
  810. ao2_cleanup(registration);
  811. return -1;
  812. }
  813. return 0;
  814. }
  815. static char *cli_complete_registration(const char *line, const char *word,
  816. int pos, int state)
  817. {
  818. char *result = NULL;
  819. int wordlen;
  820. int which = 0;
  821. struct sip_outbound_registration *registration;
  822. RAII_VAR(struct ao2_container *, registrations, NULL, ao2_cleanup);
  823. struct ao2_iterator i;
  824. if (pos != 3) {
  825. return NULL;
  826. }
  827. wordlen = strlen(word);
  828. registrations = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "registration",
  829. AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
  830. if (!registrations) {
  831. return NULL;
  832. }
  833. i = ao2_iterator_init(registrations, 0);
  834. while ((registration = ao2_iterator_next(&i))) {
  835. const char *name = ast_sorcery_object_get_id(registration);
  836. if (!strncasecmp(word, name, wordlen) && ++which > state) {
  837. result = ast_strdup(name);
  838. }
  839. ao2_cleanup(registration);
  840. if (result) {
  841. break;
  842. }
  843. }
  844. ao2_iterator_destroy(&i);
  845. return result;
  846. }
  847. static char *cli_unregister(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  848. {
  849. RAII_VAR(struct sip_outbound_registration *, registration, NULL, ao2_cleanup);
  850. const char *registration_name;
  851. switch (cmd) {
  852. case CLI_INIT:
  853. e->command = "pjsip send unregister";
  854. e->usage =
  855. "Usage: pjsip send unregister <registration>\n"
  856. " Send a SIP REGISTER request to the specified outbound "
  857. "registration with an expiration of 0. This will cause the contact "
  858. "added by this registration to be removed on the remote system.\n";
  859. return NULL;
  860. case CLI_GENERATE:
  861. return cli_complete_registration(a->line, a->word, a->pos, a->n);
  862. }
  863. if (a->argc != 4) {
  864. return CLI_SHOWUSAGE;
  865. }
  866. registration_name = a->argv[3];
  867. registration = retrieve_registration(registration_name);
  868. if (!registration) {
  869. ast_cli(a->fd, "Unable to retrieve registration %s\n", registration_name);
  870. return CLI_FAILURE;
  871. }
  872. if (queue_unregister(registration)) {
  873. ast_cli(a->fd, "Failed to queue unregistration");
  874. return 0;
  875. }
  876. return CLI_SUCCESS;
  877. }
  878. static int ami_unregister(struct mansession *s, const struct message *m)
  879. {
  880. const char *registration_name = astman_get_header(m, "Registration");
  881. RAII_VAR(struct sip_outbound_registration *, registration, NULL, ao2_cleanup);
  882. if (ast_strlen_zero(registration_name)) {
  883. astman_send_error(s, m, "Registration parameter missing.");
  884. return 0;
  885. }
  886. registration = retrieve_registration(registration_name);
  887. if (!registration) {
  888. astman_send_error(s, m, "Unable to retrieve registration entry\n");
  889. return 0;
  890. }
  891. if (queue_unregister(registration)) {
  892. astman_send_ack(s, m, "Failed to queue unregistration");
  893. return 0;
  894. }
  895. astman_send_ack(s, m, "Unregistration sent");
  896. return 0;
  897. }
  898. struct sip_ami_outbound {
  899. struct ast_sip_ami *ami;
  900. int registered;
  901. int not_registered;
  902. struct sip_outbound_registration *registration;
  903. };
  904. static int ami_outbound_registration_task(void *obj)
  905. {
  906. struct sip_ami_outbound *ami = obj;
  907. RAII_VAR(struct ast_str *, buf,
  908. ast_sip_create_ami_event("OutboundRegistrationDetail", ami->ami), ast_free);
  909. if (!buf) {
  910. return -1;
  911. }
  912. ast_sip_sorcery_object_to_ami(ami->registration, &buf);
  913. if (ami->registration->state) {
  914. pjsip_regc_info info;
  915. if (ami->registration->state->client_state->status ==
  916. SIP_REGISTRATION_REGISTERED) {
  917. ++ami->registered;
  918. } else {
  919. ++ami->not_registered;
  920. }
  921. ast_str_append(&buf, 0, "Status: %s%s",
  922. sip_outbound_registration_status_str[
  923. ami->registration->state->client_state->status], "\r\n");
  924. pjsip_regc_get_info(ami->registration->state->client_state->client, &info);
  925. ast_str_append(&buf, 0, "NextReg: %d%s", info.next_reg, "\r\n");
  926. }
  927. astman_append(ami->ami->s, "%s\r\n", ast_str_buffer(buf));
  928. return ast_sip_format_auths_ami(&ami->registration->outbound_auths, ami->ami);
  929. }
  930. static int ami_outbound_registration_detail(void *obj, void *arg, int flags)
  931. {
  932. struct sip_ami_outbound *ami = arg;
  933. ami->registration = obj;
  934. return ast_sip_push_task_synchronous(
  935. NULL, ami_outbound_registration_task, ami);
  936. }
  937. static int ami_show_outbound_registrations(struct mansession *s,
  938. const struct message *m)
  939. {
  940. struct ast_sip_ami ami = { .s = s, .m = m, .action_id = astman_get_header(m, "ActionID"), };
  941. struct sip_ami_outbound ami_outbound = { .ami = &ami };
  942. RAII_VAR(struct ao2_container *, regs, ast_sorcery_retrieve_by_fields(
  943. ast_sip_get_sorcery(), "registration", AST_RETRIEVE_FLAG_MULTIPLE |
  944. AST_RETRIEVE_FLAG_ALL, NULL), ao2_cleanup);
  945. if (!regs) {
  946. astman_send_error(s, m, "Unable to retreive "
  947. "outbound registrations\n");
  948. return -1;
  949. }
  950. astman_send_listack(s, m, "Following are Events for each Outbound "
  951. "registration", "start");
  952. ao2_callback(regs, OBJ_NODATA, ami_outbound_registration_detail, &ami_outbound);
  953. astman_append(s, "Event: OutboundRegistrationDetailComplete\r\n");
  954. if (!ast_strlen_zero(ami.action_id)) {
  955. astman_append(s, "ActionID: %s\r\n", ami.action_id);
  956. }
  957. astman_append(s, "EventList: Complete\r\n"
  958. "Registered: %d\r\n"
  959. "NotRegistered: %d\r\n\r\n",
  960. ami_outbound.registered,
  961. ami_outbound.not_registered);
  962. return 0;
  963. }
  964. static struct ao2_container *cli_get_container(void)
  965. {
  966. RAII_VAR(struct ao2_container *, container, NULL, ao2_cleanup);
  967. struct ao2_container *s_container;
  968. container = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "registration",
  969. AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
  970. if (!container) {
  971. return NULL;
  972. }
  973. s_container = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
  974. ast_sorcery_object_id_sort, ast_sorcery_object_id_compare);
  975. if (!s_container) {
  976. return NULL;
  977. }
  978. if (ao2_container_dup(s_container, container, 0)) {
  979. ao2_ref(s_container, -1);
  980. return NULL;
  981. }
  982. return s_container;
  983. }
  984. static int cli_iterator(void *container, ao2_callback_fn callback, void *args)
  985. {
  986. ao2_callback(container, OBJ_NODATA, callback, args);
  987. return 0;
  988. }
  989. static void *cli_retrieve_by_id(const char *id)
  990. {
  991. return ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "registration", id);
  992. }
  993. static int cli_print_header(void *obj, void *arg, int flags)
  994. {
  995. struct ast_sip_cli_context *context = arg;
  996. ast_assert(context->output_buffer != NULL);
  997. ast_str_append(&context->output_buffer, 0,
  998. " <Registration/ServerURI..............................> <Auth..........> <Status.......>\n");
  999. return 0;
  1000. }
  1001. static int cli_print_body(void *obj, void *arg, int flags)
  1002. {
  1003. struct sip_outbound_registration *registration = obj;
  1004. struct ast_sip_cli_context *context = arg;
  1005. const char *id = ast_sorcery_object_get_id(registration);
  1006. #define REGISTRATION_URI_FIELD_LEN 53
  1007. ast_assert(context->output_buffer != NULL);
  1008. ast_str_append(&context->output_buffer, 0, " %-s/%-*.*s %-16s %-16s\n",
  1009. id,
  1010. (int) (REGISTRATION_URI_FIELD_LEN - strlen(id)),
  1011. (int) (REGISTRATION_URI_FIELD_LEN - strlen(id)),
  1012. registration->server_uri,
  1013. AST_VECTOR_SIZE(&registration->outbound_auths)
  1014. ? AST_VECTOR_GET(&registration->outbound_auths, 0)
  1015. : "n/a",
  1016. sip_outbound_registration_status_str[registration->state->client_state->status]);
  1017. if (context->show_details
  1018. || (context->show_details_only_level_0 && context->indent_level == 0)) {
  1019. ast_str_append(&context->output_buffer, 0, "\n");
  1020. ast_sip_cli_print_sorcery_objectset(registration, context, 0);
  1021. }
  1022. return 0;
  1023. }
  1024. /*
  1025. * A function pointer to callback needs to be within the
  1026. * module in order to avoid problems with an undefined
  1027. * symbol when the module is loaded.
  1028. */
  1029. static char *my_cli_traverse_objects(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1030. {
  1031. return ast_sip_cli_traverse_objects(e, cmd, a);
  1032. }
  1033. static struct ast_cli_entry cli_outbound_registration[] = {
  1034. AST_CLI_DEFINE(cli_unregister, "Send a REGISTER request to an outbound registration target with a expiration of 0"),
  1035. AST_CLI_DEFINE(my_cli_traverse_objects, "List PJSIP Registrations",
  1036. .command = "pjsip list registrations",
  1037. .usage = "Usage: pjsip list registrations\n"
  1038. " List the configured PJSIP Registrations\n"),
  1039. AST_CLI_DEFINE(my_cli_traverse_objects, "Show PJSIP Registrations",
  1040. .command = "pjsip show registrations",
  1041. .usage = "Usage: pjsip show registrations\n"
  1042. " Show the configured PJSIP Registrations\n"),
  1043. AST_CLI_DEFINE(my_cli_traverse_objects, "Show PJSIP Registration",
  1044. .command = "pjsip show registration",
  1045. .usage = "Usage: pjsip show registration <id>\n"
  1046. " Show the configured PJSIP Registration\n"),
  1047. };
  1048. static struct ast_sip_cli_formatter_entry *cli_formatter;
  1049. static int load_module(void)
  1050. {
  1051. CHECK_PJSIP_MODULE_LOADED();
  1052. ast_sorcery_apply_default(ast_sip_get_sorcery(), "registration", "config", "pjsip.conf,criteria=type=registration");
  1053. if (ast_sorcery_object_register(ast_sip_get_sorcery(), "registration", sip_outbound_registration_alloc, NULL, sip_outbound_registration_apply)) {
  1054. return AST_MODULE_LOAD_DECLINE;
  1055. }
  1056. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "type", "", OPT_NOOP_T, 0, 0);
  1057. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "server_uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, server_uri));
  1058. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "client_uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, client_uri));
  1059. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "contact_user", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, contact_user));
  1060. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, transport));
  1061. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct sip_outbound_registration, outbound_proxy));
  1062. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "expiration", "3600", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, expiration));
  1063. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "retry_interval", "60", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, retry_interval));
  1064. 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));
  1065. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "max_retries", "10", OPT_UINT_T, 0, FLDSET(struct sip_outbound_registration, max_retries));
  1066. 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));
  1067. 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);
  1068. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "registration", "support_path", "no", OPT_BOOL_T, 1, FLDSET(struct sip_outbound_registration, support_path));
  1069. ast_sorcery_reload_object(ast_sip_get_sorcery(), "registration");
  1070. sip_outbound_registration_perform_all();
  1071. ast_manager_register_xml("PJSIPUnregister", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, ami_unregister);
  1072. ast_manager_register_xml("PJSIPShowRegistrationsOutbound", EVENT_FLAG_SYSTEM | EVENT_FLAG_REPORTING, ami_show_outbound_registrations);
  1073. cli_formatter = ao2_alloc(sizeof(struct ast_sip_cli_formatter_entry), NULL);
  1074. if (!cli_formatter) {
  1075. ast_log(LOG_ERROR, "Unable to allocate memory for cli formatter\n");
  1076. return -1;
  1077. }
  1078. cli_formatter->name = "registration";
  1079. cli_formatter->print_header = cli_print_header;
  1080. cli_formatter->print_body = cli_print_body;
  1081. cli_formatter->get_container = cli_get_container;
  1082. cli_formatter->iterate = cli_iterator;
  1083. cli_formatter->get_id = ast_sorcery_object_get_id;
  1084. cli_formatter->retrieve_by_id = cli_retrieve_by_id;
  1085. ast_sip_register_cli_formatter(cli_formatter);
  1086. ast_cli_register_multiple(cli_outbound_registration, ARRAY_LEN(cli_outbound_registration));
  1087. return AST_MODULE_LOAD_SUCCESS;
  1088. }
  1089. static int reload_module(void)
  1090. {
  1091. ast_sorcery_reload_object(ast_sip_get_sorcery(), "registration");
  1092. sip_outbound_registration_perform_all();
  1093. return 0;
  1094. }
  1095. static int unload_module(void)
  1096. {
  1097. ast_cli_unregister_multiple(cli_outbound_registration, ARRAY_LEN(cli_outbound_registration));
  1098. ast_sip_unregister_cli_formatter(cli_formatter);
  1099. ast_manager_unregister("PJSIPShowRegistrationsOutbound");
  1100. ast_manager_unregister("PJSIPUnregister");
  1101. return 0;
  1102. }
  1103. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Outbound Registration Support",
  1104. .support_level = AST_MODULE_SUPPORT_CORE,
  1105. .load = load_module,
  1106. .reload = reload_module,
  1107. .unload = unload_module,
  1108. .load_pri = AST_MODPRI_APP_DEPEND,
  1109. );