res_pjsip_outbound_publish.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2014, 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_pjproject</depend>
  21. <depend>res_pjsip</depend>
  22. <support_level>core</support_level>
  23. ***/
  24. #include "asterisk.h"
  25. #include <pjsip.h>
  26. #include <pjsip_simple.h>
  27. #include "asterisk/res_pjsip.h"
  28. #include "asterisk/res_pjsip_outbound_publish.h"
  29. #include "asterisk/module.h"
  30. #include "asterisk/taskprocessor.h"
  31. #include "asterisk/threadpool.h"
  32. #include "asterisk/datastore.h"
  33. #include "res_pjsip/include/res_pjsip_private.h"
  34. /*** DOCUMENTATION
  35. <configInfo name="res_pjsip_outbound_publish" language="en_US">
  36. <synopsis>SIP resource for outbound publish</synopsis>
  37. <description><para>
  38. <emphasis>Outbound Publish</emphasis>
  39. </para>
  40. <para>This module allows <literal>res_pjsip</literal> to publish to other SIP servers.</para>
  41. </description>
  42. <configFile name="pjsip.conf">
  43. <configObject name="outbound-publish">
  44. <synopsis>The configuration for outbound publish</synopsis>
  45. <description><para>
  46. Publish is <emphasis>COMPLETELY</emphasis> separate from the rest of
  47. <literal>pjsip.conf</literal>. A minimal configuration consists of
  48. setting a <literal>server_uri</literal> and <literal>event</literal>.
  49. </para></description>
  50. <configOption name="expiration" default="3600">
  51. <synopsis>Expiration time for publications in seconds</synopsis>
  52. </configOption>
  53. <configOption name="outbound_auth" default="">
  54. <synopsis>Authentication object(s) to be used for outbound publishes.</synopsis>
  55. <description><para>
  56. This is a comma-delimited list of <replaceable>auth</replaceable>
  57. sections defined in <filename>pjsip.conf</filename> used to respond
  58. to outbound authentication challenges.</para>
  59. <note><para>
  60. Using the same auth section for inbound and outbound
  61. authentication is not recommended. There is a difference in
  62. meaning for an empty realm setting between inbound and outbound
  63. authentication uses. See the auth realm description for details.
  64. </para></note>
  65. </description>
  66. </configOption>
  67. <configOption name="outbound_proxy" default="">
  68. <synopsis>Full SIP URI of the outbound proxy used to send publishes</synopsis>
  69. </configOption>
  70. <configOption name="server_uri">
  71. <synopsis>SIP URI of the server and entity to publish to</synopsis>
  72. <description><para>
  73. This is the URI at which to find the entity and server to send the outbound PUBLISH to.
  74. This URI is used as the request URI of the outbound PUBLISH request from Asterisk.
  75. </para></description>
  76. </configOption>
  77. <configOption name="from_uri">
  78. <synopsis>SIP URI to use in the From header</synopsis>
  79. <description><para>
  80. This is the URI that will be placed into the From header of outgoing PUBLISH
  81. messages. If no URI is specified then the URI provided in <literal>server_uri</literal>
  82. will be used.
  83. </para></description>
  84. </configOption>
  85. <configOption name="to_uri">
  86. <synopsis>SIP URI to use in the To header</synopsis>
  87. <description><para>
  88. This is the URI that will be placed into the To header of outgoing PUBLISH
  89. messages. If no URI is specified then the URI provided in <literal>server_uri</literal>
  90. will be used.
  91. </para></description>
  92. </configOption>
  93. <configOption name="event" default="">
  94. <synopsis>Event type of the PUBLISH.</synopsis>
  95. </configOption>
  96. <configOption name="max_auth_attempts" default="5">
  97. <synopsis>Maximum number of authentication attempts before stopping the publication.</synopsis>
  98. </configOption>
  99. <configOption name="transport">
  100. <synopsis>Transport used for outbound publish</synopsis>
  101. <description>
  102. <note><para>A <replaceable>transport</replaceable> configured in
  103. <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>
  104. </description>
  105. </configOption>
  106. <configOption name="type">
  107. <synopsis>Must be of type 'outbound-publish'.</synopsis>
  108. </configOption>
  109. </configObject>
  110. </configFile>
  111. </configInfo>
  112. ***/
  113. /*! \brief Queued outbound publish message */
  114. struct sip_outbound_publish_message {
  115. /*! \brief Optional body */
  116. struct ast_sip_body body;
  117. /*! \brief Linked list information */
  118. AST_LIST_ENTRY(sip_outbound_publish_message) entry;
  119. /*! \brief Extra space for body contents */
  120. char body_contents[0];
  121. };
  122. /*! \brief Outbound publish information */
  123. struct ast_sip_outbound_publish {
  124. /*! \brief Sorcery object details */
  125. SORCERY_OBJECT(details);
  126. /*! \brief Stringfields */
  127. AST_DECLARE_STRING_FIELDS(
  128. /*! \brief URI for the entity and server */
  129. AST_STRING_FIELD(server_uri);
  130. /*! \brief URI for the From header */
  131. AST_STRING_FIELD(from_uri);
  132. /*! \brief URI for the To header */
  133. AST_STRING_FIELD(to_uri);
  134. /*! \brief Explicit transport to use for publish */
  135. AST_STRING_FIELD(transport);
  136. /*! \brief Outbound proxy to use */
  137. AST_STRING_FIELD(outbound_proxy);
  138. /*! \brief The event type to publish */
  139. AST_STRING_FIELD(event);
  140. );
  141. /*! \brief Requested expiration time */
  142. unsigned int expiration;
  143. /*! \brief Maximum number of auth attempts before stopping the publish client */
  144. unsigned int max_auth_attempts;
  145. /*! \brief Configured authentication credentials */
  146. struct ast_sip_auth_vector outbound_auths;
  147. };
  148. /*! \brief Outbound publish client state information (persists for lifetime that publish should exist) */
  149. struct ast_sip_outbound_publish_client {
  150. /*! \brief Underlying publish client */
  151. pjsip_publishc *client;
  152. /*! \brief Timer entry for refreshing publish */
  153. pj_timer_entry timer;
  154. /*! \brief Publisher datastores set up by handlers */
  155. struct ao2_container *datastores;
  156. /*! \brief The number of auth attempts done */
  157. unsigned int auth_attempts;
  158. /*! \brief Queue of outgoing publish messages to send*/
  159. AST_LIST_HEAD_NOLOCK(, sip_outbound_publish_message) queue;
  160. /*! \brief The message currently being sent */
  161. struct sip_outbound_publish_message *sending;
  162. /*! \brief Publish client has been fully started and event type informed */
  163. unsigned int started;
  164. /*! \brief Publish client should be destroyed */
  165. unsigned int destroy;
  166. /*! \brief Outbound publish information */
  167. struct ast_sip_outbound_publish *publish;
  168. /*! \brief The name of the transport to be used for the publish */
  169. char *transport_name;
  170. /*! \brief Serializer for stuff and things */
  171. struct ast_taskprocessor *serializer;
  172. };
  173. /*! \brief Outbound publish state information (persists for lifetime of a publish) */
  174. struct ast_sip_outbound_publish_state {
  175. /*! \brief Outbound publish client */
  176. struct ast_sip_outbound_publish_client *client;
  177. /* publish state id lookup key - same as publish configuration id */
  178. char id[0];
  179. };
  180. /*! Time needs to be long enough for a transaction to timeout if nothing replies. */
  181. #define MAX_UNLOAD_TIMEOUT_TIME 35 /* Seconds */
  182. /*! Shutdown group to monitor sip_outbound_registration_client_state serializers. */
  183. static struct ast_serializer_shutdown_group *shutdown_group;
  184. /*! \brief Default number of client state container buckets */
  185. #define DEFAULT_STATE_BUCKETS 31
  186. static AO2_GLOBAL_OBJ_STATIC(current_states);
  187. /*! \brief Used on [re]loads to hold new state data */
  188. static struct ao2_container *new_states;
  189. /*! \brief hashing function for state objects */
  190. static int outbound_publish_state_hash(const void *obj, const int flags)
  191. {
  192. const struct ast_sip_outbound_publish_state *object;
  193. const char *key;
  194. switch (flags & OBJ_SEARCH_MASK) {
  195. case OBJ_SEARCH_KEY:
  196. key = obj;
  197. break;
  198. case OBJ_SEARCH_OBJECT:
  199. object = obj;
  200. key = object->id;
  201. break;
  202. default:
  203. ast_assert(0);
  204. return 0;
  205. }
  206. return ast_str_hash(key);
  207. }
  208. /*! \brief comparator function for client objects */
  209. static int outbound_publish_state_cmp(void *obj, void *arg, int flags)
  210. {
  211. const struct ast_sip_outbound_publish_state *object_left = obj;
  212. const struct ast_sip_outbound_publish_state *object_right = arg;
  213. const char *right_key = arg;
  214. int cmp;
  215. switch (flags & OBJ_SEARCH_MASK) {
  216. case OBJ_SEARCH_OBJECT:
  217. right_key = object_right->id;
  218. /* Fall through */
  219. case OBJ_SEARCH_KEY:
  220. cmp = strcmp(object_left->id, right_key);
  221. break;
  222. case OBJ_SEARCH_PARTIAL_KEY:
  223. /* Not supported by container. */
  224. ast_assert(0);
  225. return 0;
  226. default:
  227. cmp = 0;
  228. break;
  229. }
  230. if (cmp) {
  231. return 0;
  232. }
  233. return CMP_MATCH;
  234. }
  235. static struct ao2_container *get_publishes_and_update_state(void)
  236. {
  237. struct ao2_container *container;
  238. container = ast_sorcery_retrieve_by_fields(
  239. ast_sip_get_sorcery(), "outbound-publish",
  240. AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
  241. if (!new_states) {
  242. return container;
  243. }
  244. ao2_global_obj_replace_unref(current_states, new_states);
  245. ao2_cleanup(new_states);
  246. new_states = NULL;
  247. return container;
  248. }
  249. AST_RWLIST_HEAD_STATIC(publisher_handlers, ast_sip_event_publisher_handler);
  250. static void sub_add_handler(struct ast_sip_event_publisher_handler *handler)
  251. {
  252. AST_RWLIST_INSERT_TAIL(&publisher_handlers, handler, next);
  253. ast_module_ref(ast_module_info->self);
  254. }
  255. static struct ast_sip_event_publisher_handler *find_publisher_handler_for_event_name(const char *event_name)
  256. {
  257. struct ast_sip_event_publisher_handler *iter;
  258. AST_RWLIST_TRAVERSE(&publisher_handlers, iter, next) {
  259. if (!strcmp(iter->event_name, event_name)) {
  260. break;
  261. }
  262. }
  263. return iter;
  264. }
  265. /*! \brief Helper function which cancels the refresh timer on a client */
  266. static void cancel_publish_refresh(struct ast_sip_outbound_publish_client *client)
  267. {
  268. if (pj_timer_heap_cancel_if_active(pjsip_endpt_get_timer_heap(ast_sip_get_pjsip_endpoint()),
  269. &client->timer, 0)) {
  270. /* The timer was successfully cancelled, drop the refcount of the client */
  271. ao2_ref(client, -1);
  272. }
  273. }
  274. /*! \brief Helper function which sets up the timer to send publication */
  275. static void schedule_publish_refresh(struct ast_sip_outbound_publish_client *client, int expiration)
  276. {
  277. struct ast_sip_outbound_publish *publish = ao2_bump(client->publish);
  278. pj_time_val delay = { .sec = 0, };
  279. cancel_publish_refresh(client);
  280. if (expiration > 0) {
  281. delay.sec = expiration - PJSIP_PUBLISHC_DELAY_BEFORE_REFRESH;
  282. }
  283. if (publish->expiration && ((delay.sec > publish->expiration) || !delay.sec)) {
  284. delay.sec = publish->expiration;
  285. }
  286. if (delay.sec < PJSIP_PUBLISHC_DELAY_BEFORE_REFRESH) {
  287. delay.sec = PJSIP_PUBLISHC_DELAY_BEFORE_REFRESH;
  288. }
  289. ao2_ref(client, +1);
  290. if (pjsip_endpt_schedule_timer(ast_sip_get_pjsip_endpoint(), &client->timer, &delay) != PJ_SUCCESS) {
  291. ast_log(LOG_WARNING, "Failed to pass timed publish refresh to scheduler\n");
  292. ao2_ref(client, -1);
  293. }
  294. ao2_ref(publish, -1);
  295. }
  296. /*! \brief Publish client timer callback function */
  297. static void sip_outbound_publish_timer_cb(pj_timer_heap_t *timer_heap, struct pj_timer_entry *entry)
  298. {
  299. struct ast_sip_outbound_publish_client *client = entry->user_data;
  300. ao2_lock(client);
  301. if (AST_LIST_EMPTY(&client->queue)) {
  302. /* If there are no outstanding messages send an empty PUBLISH message so our publication doesn't expire */
  303. ast_sip_publish_client_send(client, NULL);
  304. }
  305. ao2_unlock(client);
  306. ao2_ref(client, -1);
  307. }
  308. /*! \brief Task for cancelling a refresh timer */
  309. static int cancel_refresh_timer_task(void *data)
  310. {
  311. struct ast_sip_outbound_publish_client *client = data;
  312. cancel_publish_refresh(client);
  313. ao2_ref(client, -1);
  314. return 0;
  315. }
  316. /*! \brief Task for sending an unpublish */
  317. static int send_unpublish_task(void *data)
  318. {
  319. struct ast_sip_outbound_publish_client *client = data;
  320. pjsip_tx_data *tdata;
  321. if (pjsip_publishc_unpublish(client->client, &tdata) == PJ_SUCCESS) {
  322. if (!ast_strlen_zero(client->transport_name)) {
  323. pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
  324. ast_sip_set_tpselector_from_transport_name(client->transport_name, &selector);
  325. pjsip_tx_data_set_transport(tdata, &selector);
  326. }
  327. ast_sip_record_request_serializer(tdata);
  328. pjsip_publishc_send(client->client, tdata);
  329. }
  330. ao2_ref(client, -1);
  331. return 0;
  332. }
  333. /*! \brief Helper function which starts or stops publish clients when applicable */
  334. static void sip_outbound_publish_synchronize(struct ast_sip_event_publisher_handler *removed)
  335. {
  336. RAII_VAR(struct ao2_container *, publishes, get_publishes_and_update_state(), ao2_cleanup);
  337. struct ao2_container *states;
  338. struct ao2_iterator i;
  339. struct ast_sip_outbound_publish_state *state;
  340. if (!publishes) {
  341. return;
  342. }
  343. states = ao2_global_obj_ref(current_states);
  344. if (!states) {
  345. return;
  346. }
  347. i = ao2_iterator_init(states, 0);
  348. while ((state = ao2_iterator_next(&i))) {
  349. struct ast_sip_outbound_publish *publish = ao2_bump(state->client->publish);
  350. struct ast_sip_event_publisher_handler *handler = find_publisher_handler_for_event_name(publish->event);
  351. if (!state->client->started) {
  352. /* If the publisher client has not yet been started try to start it */
  353. if (!handler) {
  354. ast_debug(2, "Could not find handler for event '%s' for outbound publish client '%s'\n",
  355. publish->event, ast_sorcery_object_get_id(publish));
  356. } else if (handler->start_publishing(publish, state->client)) {
  357. ast_log(LOG_ERROR, "Failed to start outbound publish with event '%s' for client '%s'\n",
  358. publish->event, ast_sorcery_object_get_id(publish));
  359. } else {
  360. state->client->started = 1;
  361. }
  362. } else if (!handler && removed && !strcmp(publish->event, removed->event_name)) {
  363. /* If the publisher client has been started but it is going away stop it */
  364. removed->stop_publishing(state->client);
  365. state->client->started = 0;
  366. if (ast_sip_push_task(state->client->serializer, cancel_refresh_timer_task, ao2_bump(state->client))) {
  367. ast_log(LOG_WARNING, "Could not stop refresh timer on client '%s'\n",
  368. ast_sorcery_object_get_id(publish));
  369. ao2_ref(state->client, -1);
  370. }
  371. }
  372. ao2_ref(publish, -1);
  373. ao2_ref(state, -1);
  374. }
  375. ao2_iterator_destroy(&i);
  376. ao2_ref(states, -1);
  377. }
  378. static struct ast_sip_outbound_publish_state *sip_publish_state_get(const char *id)
  379. {
  380. struct ao2_container *states = ao2_global_obj_ref(current_states);
  381. struct ast_sip_outbound_publish_state *res;
  382. if (!states) {
  383. return NULL;
  384. }
  385. res = ao2_find(states, id, OBJ_SEARCH_KEY);
  386. ao2_ref(states, -1);
  387. return res;
  388. }
  389. struct ast_sip_outbound_publish_client *ast_sip_publish_client_get(const char *name)
  390. {
  391. struct ast_sip_outbound_publish_state *state = sip_publish_state_get(name);
  392. if (!state) {
  393. return NULL;
  394. }
  395. ao2_ref(state->client, +1);
  396. ao2_ref(state, -1);
  397. return state->client;
  398. }
  399. int ast_sip_register_event_publisher_handler(struct ast_sip_event_publisher_handler *handler)
  400. {
  401. struct ast_sip_event_publisher_handler *existing;
  402. SCOPED_LOCK(lock, &publisher_handlers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
  403. if (!handler->start_publishing || !handler->stop_publishing) {
  404. ast_log(LOG_ERROR, "Handler does not implement required callbacks. Cannot register\n");
  405. return -1;
  406. } else if (ast_strlen_zero(handler->event_name)) {
  407. ast_log(LOG_ERROR, "No event package specified for event publisher handler. Cannot register\n");
  408. return -1;
  409. }
  410. existing = find_publisher_handler_for_event_name(handler->event_name);
  411. if (existing) {
  412. ast_log(LOG_ERROR, "Unable to register event publisher handler for event %s. "
  413. "A handler is already registered\n", handler->event_name);
  414. return -1;
  415. }
  416. sub_add_handler(handler);
  417. sip_outbound_publish_synchronize(NULL);
  418. return 0;
  419. }
  420. void ast_sip_unregister_event_publisher_handler(struct ast_sip_event_publisher_handler *handler)
  421. {
  422. struct ast_sip_event_publisher_handler *iter;
  423. SCOPED_LOCK(lock, &publisher_handlers, AST_RWLIST_WRLOCK, AST_RWLIST_UNLOCK);
  424. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&publisher_handlers, iter, next) {
  425. if (handler == iter) {
  426. AST_RWLIST_REMOVE_CURRENT(next);
  427. ast_module_unref(ast_module_info->self);
  428. break;
  429. }
  430. }
  431. AST_RWLIST_TRAVERSE_SAFE_END;
  432. sip_outbound_publish_synchronize(handler);
  433. }
  434. /*! \brief Destructor function for publish information */
  435. static void sip_outbound_publish_destroy(void *obj)
  436. {
  437. struct ast_sip_outbound_publish *publish = obj;
  438. ast_sip_auth_vector_destroy(&publish->outbound_auths);
  439. ast_string_field_free_memory(publish);
  440. }
  441. /*! \brief Allocator function for publish information */
  442. static void *sip_outbound_publish_alloc(const char *name)
  443. {
  444. struct ast_sip_outbound_publish *publish = ast_sorcery_generic_alloc(sizeof(*publish),
  445. sip_outbound_publish_destroy);
  446. if (!publish || ast_string_field_init(publish, 256)) {
  447. ao2_cleanup(publish);
  448. return NULL;
  449. }
  450. return publish;
  451. }
  452. static void sip_outbound_publish_datastore_destroy(void *obj)
  453. {
  454. struct ast_datastore *datastore = obj;
  455. /* Using the destroy function (if present) destroy the data */
  456. if (datastore->info->destroy != NULL && datastore->data != NULL) {
  457. datastore->info->destroy(datastore->data);
  458. datastore->data = NULL;
  459. }
  460. ast_free((void *) datastore->uid);
  461. datastore->uid = NULL;
  462. }
  463. struct ast_datastore *ast_sip_publish_client_alloc_datastore(const struct ast_datastore_info *info, const char *uid)
  464. {
  465. RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
  466. const char *uid_ptr = uid;
  467. char uuid_buf[AST_UUID_STR_LEN];
  468. if (!info) {
  469. return NULL;
  470. }
  471. datastore = ao2_alloc(sizeof(*datastore), sip_outbound_publish_datastore_destroy);
  472. if (!datastore) {
  473. return NULL;
  474. }
  475. datastore->info = info;
  476. if (ast_strlen_zero(uid)) {
  477. /* They didn't provide an ID so we'll provide one ourself */
  478. uid_ptr = ast_uuid_generate_str(uuid_buf, sizeof(uuid_buf));
  479. }
  480. datastore->uid = ast_strdup(uid_ptr);
  481. if (!datastore->uid) {
  482. return NULL;
  483. }
  484. ao2_ref(datastore, +1);
  485. return datastore;
  486. }
  487. int ast_sip_publish_client_add_datastore(struct ast_sip_outbound_publish_client *client,
  488. struct ast_datastore *datastore)
  489. {
  490. ast_assert(datastore != NULL);
  491. ast_assert(datastore->info != NULL);
  492. ast_assert(!ast_strlen_zero(datastore->uid));
  493. if (!ao2_link(client->datastores, datastore)) {
  494. return -1;
  495. }
  496. return 0;
  497. }
  498. struct ast_datastore *ast_sip_publish_client_get_datastore(struct ast_sip_outbound_publish_client *client,
  499. const char *name)
  500. {
  501. return ao2_find(client->datastores, name, OBJ_SEARCH_KEY);
  502. }
  503. void ast_sip_publish_client_remove_datastore(struct ast_sip_outbound_publish_client *client,
  504. const char *name)
  505. {
  506. ao2_find(client->datastores, name, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
  507. }
  508. static int sip_publish_client_service_queue(void *data)
  509. {
  510. RAII_VAR(struct ast_sip_outbound_publish_client *, client, data, ao2_cleanup);
  511. SCOPED_AO2LOCK(lock, client);
  512. struct sip_outbound_publish_message *message;
  513. pjsip_tx_data *tdata;
  514. pj_status_t status;
  515. if (client->destroy || client->sending || !(message = AST_LIST_FIRST(&client->queue))) {
  516. return 0;
  517. }
  518. if (pjsip_publishc_publish(client->client, PJ_FALSE, &tdata) != PJ_SUCCESS) {
  519. goto fatal;
  520. }
  521. if (!ast_strlen_zero(message->body.type) && !ast_strlen_zero(message->body.subtype) &&
  522. ast_sip_add_body(tdata, &message->body)) {
  523. pjsip_tx_data_dec_ref(tdata);
  524. goto fatal;
  525. }
  526. if (!ast_strlen_zero(client->transport_name)) {
  527. pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
  528. ast_sip_set_tpselector_from_transport_name(client->transport_name, &selector);
  529. pjsip_tx_data_set_transport(tdata, &selector);
  530. }
  531. ast_sip_record_request_serializer(tdata);
  532. status = pjsip_publishc_send(client->client, tdata);
  533. if (status == PJ_EBUSY) {
  534. /* We attempted to send the message but something else got there first */
  535. goto service;
  536. } else if (status != PJ_SUCCESS) {
  537. goto fatal;
  538. }
  539. client->sending = message;
  540. return 0;
  541. fatal:
  542. AST_LIST_REMOVE_HEAD(&client->queue, entry);
  543. ast_free(message);
  544. service:
  545. if (ast_sip_push_task(client->serializer, sip_publish_client_service_queue, ao2_bump(client))) {
  546. ao2_ref(client, -1);
  547. }
  548. return -1;
  549. }
  550. int ast_sip_publish_client_send(struct ast_sip_outbound_publish_client *client,
  551. const struct ast_sip_body *body)
  552. {
  553. SCOPED_AO2LOCK(lock, client);
  554. struct sip_outbound_publish_message *message;
  555. size_t type_len = 0, subtype_len = 0, body_text_len = 0;
  556. int res;
  557. if (!client->client) {
  558. return -1;
  559. }
  560. /* If a body is present we need more space for the contents of it */
  561. if (body) {
  562. type_len = strlen(body->type) + 1;
  563. subtype_len = strlen(body->subtype) + 1;
  564. body_text_len = strlen(body->body_text) + 1;
  565. }
  566. message = ast_calloc(1, sizeof(*message) + type_len + subtype_len + body_text_len);
  567. if (!message) {
  568. return -1;
  569. }
  570. if (body) {
  571. char *dst = message->body_contents;
  572. message->body.type = strcpy(dst, body->type);
  573. dst += type_len;
  574. message->body.subtype = strcpy(dst, body->subtype);
  575. dst += subtype_len;
  576. message->body.body_text = strcpy(dst, body->body_text);
  577. }
  578. AST_LIST_INSERT_TAIL(&client->queue, message, entry);
  579. res = ast_sip_push_task(client->serializer, sip_publish_client_service_queue, ao2_bump(client));
  580. if (res) {
  581. ao2_ref(client, -1);
  582. }
  583. return res;
  584. }
  585. /*! \brief Destructor function for publish client */
  586. static void sip_outbound_publish_client_destroy(void *obj)
  587. {
  588. struct ast_sip_outbound_publish_client *client = obj;
  589. struct sip_outbound_publish_message *message;
  590. /* You might be tempted to think "the publish client isn't being destroyed" but it actually is - just elsewhere */
  591. while ((message = AST_LIST_REMOVE_HEAD(&client->queue, entry))) {
  592. ast_free(message);
  593. }
  594. ao2_cleanup(client->datastores);
  595. ao2_cleanup(client->publish);
  596. ast_free(client->transport_name);
  597. ast_taskprocessor_unreference(client->serializer);
  598. }
  599. static int explicit_publish_destroy(void *data)
  600. {
  601. struct ast_sip_outbound_publish_client *client = data;
  602. /*
  603. * If there is no pjsip publishing client then we obviously don't need
  604. * to destroy it. Also, the ref for the Asterisk publishing client that
  605. * pjsip had would not exist or should already be gone as well.
  606. */
  607. if (client->client) {
  608. pjsip_publishc_destroy(client->client);
  609. ao2_ref(client, -1);
  610. }
  611. return 0;
  612. }
  613. /*! \brief Helper function which cancels and un-publishes a no longer used client */
  614. static int cancel_and_unpublish(struct ast_sip_outbound_publish_client *client)
  615. {
  616. struct ast_sip_event_publisher_handler *handler;
  617. SCOPED_AO2LOCK(lock, client);
  618. if (!client->started) {
  619. /* If the client was never started, there's nothing to unpublish, so just
  620. * destroy the publication and remove its reference to the client.
  621. */
  622. ast_sip_push_task(client->serializer, explicit_publish_destroy, client);
  623. return 0;
  624. }
  625. handler = find_publisher_handler_for_event_name(client->publish->event);
  626. if (handler) {
  627. handler->stop_publishing(client);
  628. }
  629. client->started = 0;
  630. if (ast_sip_push_task(client->serializer, cancel_refresh_timer_task, ao2_bump(client))) {
  631. ast_log(LOG_WARNING, "Could not stop refresh timer on outbound publish '%s'\n",
  632. ast_sorcery_object_get_id(client->publish));
  633. ao2_ref(client, -1);
  634. }
  635. /* If nothing is being sent right now send the unpublish - the destroy will happen in the subsequent callback */
  636. if (!client->sending) {
  637. if (ast_sip_push_task(client->serializer, send_unpublish_task, ao2_bump(client))) {
  638. ast_log(LOG_WARNING, "Could not send unpublish message on outbound publish '%s'\n",
  639. ast_sorcery_object_get_id(client->publish));
  640. ao2_ref(client, -1);
  641. }
  642. }
  643. client->destroy = 1;
  644. return 0;
  645. }
  646. /*! \brief Destructor function for publish state */
  647. static void sip_outbound_publish_state_destroy(void *obj)
  648. {
  649. struct ast_sip_outbound_publish_state *state = obj;
  650. cancel_and_unpublish(state->client);
  651. ao2_cleanup(state->client);
  652. }
  653. /*!
  654. * \internal
  655. * \brief Check if a publish can be reused
  656. *
  657. * This checks if the existing outbound publish's configuration differs from a newly-applied
  658. * outbound publish.
  659. *
  660. * \param existing The pre-existing outbound publish
  661. * \param applied The newly-created publish
  662. */
  663. static int can_reuse_publish(struct ast_sip_outbound_publish *existing, struct ast_sip_outbound_publish *applied)
  664. {
  665. int i;
  666. if (strcmp(existing->server_uri, applied->server_uri) || strcmp(existing->from_uri, applied->from_uri) ||
  667. strcmp(existing->to_uri, applied->to_uri) || strcmp(existing->outbound_proxy, applied->outbound_proxy) ||
  668. strcmp(existing->event, applied->event) ||
  669. AST_VECTOR_SIZE(&existing->outbound_auths) != AST_VECTOR_SIZE(&applied->outbound_auths)) {
  670. return 0;
  671. }
  672. for (i = 0; i < AST_VECTOR_SIZE(&existing->outbound_auths); ++i) {
  673. if (strcmp(AST_VECTOR_GET(&existing->outbound_auths, i), AST_VECTOR_GET(&applied->outbound_auths, i))) {
  674. return 0;
  675. }
  676. }
  677. return 1;
  678. }
  679. static void sip_outbound_publish_callback(struct pjsip_publishc_cbparam *param);
  680. /*! \brief Helper function that allocates a pjsip publish client and configures it */
  681. static int sip_outbound_publish_client_alloc(void *data)
  682. {
  683. struct ast_sip_outbound_publish_client *client = data;
  684. RAII_VAR(struct ast_sip_outbound_publish *, publish, NULL, ao2_cleanup);
  685. pjsip_publishc_opt opt = {
  686. .queue_request = PJ_FALSE,
  687. };
  688. pj_str_t event, server_uri, to_uri, from_uri;
  689. pj_status_t status;
  690. if (client->client) {
  691. return 0;
  692. } else if (pjsip_publishc_create(ast_sip_get_pjsip_endpoint(), &opt, ao2_bump(client), sip_outbound_publish_callback,
  693. &client->client) != PJ_SUCCESS) {
  694. ao2_ref(client, -1);
  695. return -1;
  696. }
  697. publish = ao2_bump(client->publish);
  698. if (!ast_strlen_zero(publish->outbound_proxy)) {
  699. pjsip_route_hdr route_set, *route;
  700. static const pj_str_t ROUTE_HNAME = { "Route", 5 };
  701. pj_list_init(&route_set);
  702. if (!(route = pjsip_parse_hdr(pjsip_publishc_get_pool(client->client), &ROUTE_HNAME,
  703. (char*)publish->outbound_proxy, strlen(publish->outbound_proxy), NULL))) {
  704. pjsip_publishc_destroy(client->client);
  705. return -1;
  706. }
  707. pj_list_insert_nodes_before(&route_set, route);
  708. pjsip_publishc_set_route_set(client->client, &route_set);
  709. }
  710. pj_cstr(&event, publish->event);
  711. pj_cstr(&server_uri, publish->server_uri);
  712. pj_cstr(&to_uri, S_OR(publish->to_uri, publish->server_uri));
  713. pj_cstr(&from_uri, S_OR(publish->from_uri, publish->server_uri));
  714. status = pjsip_publishc_init(client->client, &event, &server_uri, &from_uri, &to_uri,
  715. publish->expiration);
  716. if (status == PJSIP_EINVALIDURI) {
  717. pj_pool_t *pool;
  718. pj_str_t tmp;
  719. pjsip_uri *uri;
  720. pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "URI Validation", 256, 256);
  721. if (!pool) {
  722. ast_log(LOG_ERROR, "Could not create pool for URI validation on outbound publish '%s'\n",
  723. ast_sorcery_object_get_id(publish));
  724. pjsip_publishc_destroy(client->client);
  725. return -1;
  726. }
  727. pj_strdup2_with_null(pool, &tmp, publish->server_uri);
  728. uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
  729. if (!uri) {
  730. ast_log(LOG_ERROR, "Invalid server URI '%s' specified on outbound publish '%s'\n",
  731. publish->server_uri, ast_sorcery_object_get_id(publish));
  732. }
  733. if (!ast_strlen_zero(publish->to_uri)) {
  734. pj_strdup2_with_null(pool, &tmp, publish->to_uri);
  735. uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
  736. if (!uri) {
  737. ast_log(LOG_ERROR, "Invalid to URI '%s' specified on outbound publish '%s'\n",
  738. publish->to_uri, ast_sorcery_object_get_id(publish));
  739. }
  740. }
  741. if (!ast_strlen_zero(publish->from_uri)) {
  742. pj_strdup2_with_null(pool, &tmp, publish->from_uri);
  743. uri = pjsip_parse_uri(pool, tmp.ptr, tmp.slen, 0);
  744. if (!uri) {
  745. ast_log(LOG_ERROR, "Invalid from URI '%s' specified on outbound publish '%s'\n",
  746. publish->from_uri, ast_sorcery_object_get_id(publish));
  747. }
  748. }
  749. pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
  750. pjsip_publishc_destroy(client->client);
  751. return -1;
  752. } else if (status != PJ_SUCCESS) {
  753. pjsip_publishc_destroy(client->client);
  754. return -1;
  755. }
  756. return 0;
  757. }
  758. /*! \brief Callback function for publish client responses */
  759. static void sip_outbound_publish_callback(struct pjsip_publishc_cbparam *param)
  760. {
  761. #define DESTROY_CLIENT() do { \
  762. pjsip_publishc_destroy(client->client); \
  763. client->client = NULL; \
  764. ao2_ref(client, -1); } while (0)
  765. RAII_VAR(struct ast_sip_outbound_publish_client *, client, ao2_bump(param->token), ao2_cleanup);
  766. RAII_VAR(struct ast_sip_outbound_publish *, publish, ao2_bump(client->publish), ao2_cleanup);
  767. SCOPED_AO2LOCK(lock, client);
  768. pjsip_tx_data *tdata;
  769. if (client->destroy) {
  770. if (client->sending) {
  771. client->sending = NULL;
  772. if (!ast_sip_push_task(client->serializer, send_unpublish_task, ao2_bump(client))) {
  773. return;
  774. }
  775. ast_log(LOG_WARNING, "Could not send unpublish message on outbound publish '%s'\n",
  776. ast_sorcery_object_get_id(publish));
  777. ao2_ref(client, -1);
  778. }
  779. /* Once the destroy is called this callback will not get called any longer, so drop the client ref */
  780. DESTROY_CLIENT();
  781. return;
  782. }
  783. if (param->code == 401 || param->code == 407) {
  784. if (!ast_sip_create_request_with_auth(&publish->outbound_auths,
  785. param->rdata, pjsip_rdata_get_tsx(param->rdata), &tdata)) {
  786. if (!ast_strlen_zero(client->transport_name)) {
  787. pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, };
  788. ast_sip_set_tpselector_from_transport_name(client->transport_name, &selector);
  789. pjsip_tx_data_set_transport(tdata, &selector);
  790. }
  791. ast_sip_record_request_serializer(tdata);
  792. pjsip_publishc_send(client->client, tdata);
  793. }
  794. client->auth_attempts++;
  795. if (client->auth_attempts == publish->max_auth_attempts) {
  796. DESTROY_CLIENT();
  797. ast_log(LOG_ERROR, "Reached maximum number of PUBLISH authentication attempts on outbound publish '%s'\n",
  798. ast_sorcery_object_get_id(publish));
  799. goto end;
  800. }
  801. return;
  802. }
  803. client->auth_attempts = 0;
  804. if (param->code == 412) {
  805. DESTROY_CLIENT();
  806. if (sip_outbound_publish_client_alloc(client)) {
  807. ast_log(LOG_ERROR, "Failed to create a new outbound publish client for '%s' on 412 response\n",
  808. ast_sorcery_object_get_id(publish));
  809. goto end;
  810. }
  811. /* Setting this to NULL will cause a new PUBLISH to get created and sent for the same underlying body */
  812. client->sending = NULL;
  813. } else if (param->code == 423) {
  814. /* Update the expiration with the new expiration time if available */
  815. pjsip_expires_hdr *expires;
  816. expires = pjsip_msg_find_hdr(param->rdata->msg_info.msg, PJSIP_H_MIN_EXPIRES, NULL);
  817. if (!expires || !expires->ivalue) {
  818. DESTROY_CLIENT();
  819. ast_log(LOG_ERROR, "Received 423 response on outbound publish '%s' without a Min-Expires header\n",
  820. ast_sorcery_object_get_id(publish));
  821. goto end;
  822. }
  823. pjsip_publishc_update_expires(client->client, expires->ivalue);
  824. client->sending = NULL;
  825. } else if (client->sending) {
  826. /* Remove the message currently being sent so that when the queue is serviced another will get sent */
  827. AST_LIST_REMOVE_HEAD(&client->queue, entry);
  828. ast_free(client->sending);
  829. client->sending = NULL;
  830. if (!param->rdata) {
  831. ast_log(LOG_NOTICE, "No response received for outbound publish '%s'\n",
  832. ast_sorcery_object_get_id(publish));
  833. }
  834. }
  835. if (AST_LIST_EMPTY(&client->queue)) {
  836. schedule_publish_refresh(client, param->expiration);
  837. }
  838. end:
  839. if (!client->client) {
  840. struct sip_outbound_publish_message *message;
  841. while ((message = AST_LIST_REMOVE_HEAD(&client->queue, entry))) {
  842. ast_free(message);
  843. }
  844. } else {
  845. if (ast_sip_push_task(client->serializer, sip_publish_client_service_queue, ao2_bump(client))) {
  846. ao2_ref(client, -1);
  847. }
  848. }
  849. }
  850. #define DATASTORE_BUCKETS 53
  851. static int datastore_hash(const void *obj, int flags)
  852. {
  853. const struct ast_datastore *datastore;
  854. const char *uid;
  855. switch (flags & OBJ_SEARCH_MASK) {
  856. case OBJ_SEARCH_KEY:
  857. uid = obj;
  858. break;
  859. case OBJ_SEARCH_OBJECT:
  860. datastore = obj;
  861. uid = datastore->uid;
  862. break;
  863. default:
  864. /* Hash can only work on something with a full key. */
  865. ast_assert(0);
  866. return 0;
  867. }
  868. return ast_str_hash(uid);
  869. }
  870. static int datastore_cmp(void *obj, void *arg, int flags)
  871. {
  872. const struct ast_datastore *object_left = obj;
  873. const struct ast_datastore *object_right = arg;
  874. const char *right_key = arg;
  875. int cmp;
  876. switch (flags & OBJ_SEARCH_MASK) {
  877. case OBJ_SEARCH_OBJECT:
  878. right_key = object_right->uid;
  879. /* Fall through */
  880. case OBJ_SEARCH_KEY:
  881. cmp = strcmp(object_left->uid, right_key);
  882. break;
  883. case OBJ_SEARCH_PARTIAL_KEY:
  884. cmp = strncmp(object_left->uid, right_key, strlen(right_key));
  885. break;
  886. default:
  887. /*
  888. * What arg points to is specific to this traversal callback
  889. * and has no special meaning to astobj2.
  890. */
  891. cmp = 0;
  892. break;
  893. }
  894. if (cmp) {
  895. return 0;
  896. }
  897. /*
  898. * At this point the traversal callback is identical to a sorted
  899. * container.
  900. */
  901. return CMP_MATCH;
  902. }
  903. /*! \brief Allocator function for publish client */
  904. static struct ast_sip_outbound_publish_state *sip_outbound_publish_state_alloc(
  905. struct ast_sip_outbound_publish *publish)
  906. {
  907. const char *id = ast_sorcery_object_get_id(publish);
  908. struct ast_sip_outbound_publish_state *state =
  909. ao2_alloc(sizeof(*state) + strlen(id) + 1, sip_outbound_publish_state_destroy);
  910. char tps_name[AST_TASKPROCESSOR_MAX_NAME + 1];
  911. if (!state) {
  912. return NULL;
  913. }
  914. state->client = ao2_alloc(sizeof(*state->client), sip_outbound_publish_client_destroy);
  915. if (!state->client) {
  916. ao2_ref(state, -1);
  917. return NULL;
  918. }
  919. state->client->datastores = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
  920. DATASTORE_BUCKETS, datastore_hash, NULL, datastore_cmp);
  921. if (!state->client->datastores) {
  922. ao2_ref(state, -1);
  923. return NULL;
  924. }
  925. /* Create name with seq number appended. */
  926. ast_taskprocessor_build_name(tps_name, sizeof(tps_name), "pjsip/outpub/%s",
  927. ast_sorcery_object_get_id(publish));
  928. state->client->serializer = ast_sip_create_serializer_group_named(tps_name,
  929. shutdown_group);
  930. if (!state->client->serializer) {
  931. ao2_ref(state, -1);
  932. return NULL;
  933. }
  934. pj_timer_entry_init(&state->client->timer, 0, state->client, sip_outbound_publish_timer_cb);
  935. state->client->transport_name = ast_strdup(publish->transport);
  936. state->client->publish = ao2_bump(publish);
  937. strcpy(state->id, id);
  938. return state;
  939. }
  940. static int initialize_publish_client(struct ast_sip_outbound_publish *publish,
  941. struct ast_sip_outbound_publish_state *state)
  942. {
  943. if (ast_sip_push_task_wait_serializer(state->client->serializer,
  944. sip_outbound_publish_client_alloc, state->client)) {
  945. ast_log(LOG_ERROR, "Unable to create client for outbound publish '%s'\n",
  946. ast_sorcery_object_get_id(publish));
  947. return -1;
  948. }
  949. return 0;
  950. }
  951. static int validate_publish_config(struct ast_sip_outbound_publish *publish)
  952. {
  953. if (ast_strlen_zero(publish->server_uri)) {
  954. ast_log(LOG_ERROR, "No server URI specified on outbound publish '%s'\n",
  955. ast_sorcery_object_get_id(publish));
  956. return -1;
  957. } else if (ast_sip_validate_uri_length(publish->server_uri)) {
  958. ast_log(LOG_ERROR, "Server URI or hostname length exceeds pjproject limit or is not a sip(s) uri: '%s' on outbound publish '%s'\n",
  959. publish->server_uri,
  960. ast_sorcery_object_get_id(publish));
  961. return -1;
  962. } else if (ast_strlen_zero(publish->event)) {
  963. ast_log(LOG_ERROR, "No event type specified for outbound publish '%s'\n",
  964. ast_sorcery_object_get_id(publish));
  965. return -1;
  966. } else if (!ast_strlen_zero(publish->from_uri)
  967. && ast_sip_validate_uri_length(publish->from_uri)) {
  968. ast_log(LOG_ERROR, "From URI or hostname length exceeds pjproject limit or is not a sip(s) uri: '%s' on outbound publish '%s'\n",
  969. publish->from_uri,
  970. ast_sorcery_object_get_id(publish));
  971. return -1;
  972. } else if (!ast_strlen_zero(publish->to_uri)
  973. && ast_sip_validate_uri_length(publish->to_uri)) {
  974. ast_log(LOG_ERROR, "To URI or hostname length exceeds pjproject limit or is not a sip(s) uri: '%s' on outbound publish '%s'\n",
  975. publish->to_uri,
  976. ast_sorcery_object_get_id(publish));
  977. return -1;
  978. }
  979. return 0;
  980. }
  981. static int current_state_reusable(struct ast_sip_outbound_publish *publish,
  982. struct ast_sip_outbound_publish_state *current_state)
  983. {
  984. struct ast_sip_outbound_publish *old_publish;
  985. if (!can_reuse_publish(current_state->client->publish, publish)) {
  986. /*
  987. * Something significant has changed in the configuration, so we are
  988. * unable to use the old state object. The current state needs to go
  989. * away and a new one needs to be created.
  990. */
  991. return 0;
  992. }
  993. /*
  994. * We can reuse the current state object so keep it, but swap out the
  995. * underlying publish object with the new one.
  996. */
  997. old_publish = current_state->client->publish;
  998. current_state->client->publish = publish;
  999. if (initialize_publish_client(publish, current_state)) {
  1000. /*
  1001. * If the state object fails to re-initialize then swap
  1002. * the old publish info back in.
  1003. */
  1004. current_state->client->publish = publish;
  1005. return -1;
  1006. }
  1007. /*
  1008. * Since we swapped out the publish object the new one needs a ref
  1009. * while the old one needs to go away.
  1010. */
  1011. ao2_ref(current_state->client->publish, +1);
  1012. ao2_cleanup(old_publish);
  1013. /* Tell the caller that the current state object should be used */
  1014. return 1;
  1015. }
  1016. /*! \brief Apply function which finds or allocates a state structure */
  1017. static int sip_outbound_publish_apply(const struct ast_sorcery *sorcery, void *obj)
  1018. {
  1019. #define ADD_TO_NEW_STATES(__obj) \
  1020. do { if (__obj) { \
  1021. ao2_link(new_states, __obj); \
  1022. ao2_ref(__obj, -1); } } while (0)
  1023. struct ast_sip_outbound_publish *applied = obj;
  1024. struct ast_sip_outbound_publish_state *current_state, *new_state;
  1025. int res;
  1026. /*
  1027. * New states are being loaded or reloaded. We'll need to add the new
  1028. * object if created/updated, or keep the old object if an error occurs.
  1029. */
  1030. if (!new_states) {
  1031. new_states = ao2_container_alloc_hash(
  1032. AO2_ALLOC_OPT_LOCK_NOLOCK, 0, DEFAULT_STATE_BUCKETS,
  1033. outbound_publish_state_hash, NULL, outbound_publish_state_cmp);
  1034. if (!new_states) {
  1035. ast_log(LOG_ERROR, "Unable to allocate new states container\n");
  1036. return -1;
  1037. }
  1038. }
  1039. /* If there is current state we'll want to maintain it if any errors occur */
  1040. current_state = sip_publish_state_get(ast_sorcery_object_get_id(applied));
  1041. if ((res = validate_publish_config(applied))) {
  1042. ADD_TO_NEW_STATES(current_state);
  1043. return res;
  1044. }
  1045. if (current_state && (res = current_state_reusable(applied, current_state))) {
  1046. /*
  1047. * The current state object was able to be reused, or an error
  1048. * occurred. Either way we keep the current state and be done.
  1049. */
  1050. ADD_TO_NEW_STATES(current_state);
  1051. return res == 1 ? 0 : -1;
  1052. }
  1053. /*
  1054. * No current state was found or it was unable to be reused. Either way
  1055. * we'll need to create a new state object.
  1056. */
  1057. new_state = sip_outbound_publish_state_alloc(applied);
  1058. if (!new_state) {
  1059. ast_log(LOG_ERROR, "Unable to create state for outbound publish '%s'\n",
  1060. ast_sorcery_object_get_id(applied));
  1061. ADD_TO_NEW_STATES(current_state);
  1062. return -1;
  1063. };
  1064. if (initialize_publish_client(applied, new_state)) {
  1065. ADD_TO_NEW_STATES(current_state);
  1066. ao2_ref(new_state, -1);
  1067. return -1;
  1068. }
  1069. ADD_TO_NEW_STATES(new_state);
  1070. ao2_cleanup(current_state);
  1071. return res;
  1072. }
  1073. static int outbound_auth_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  1074. {
  1075. struct ast_sip_outbound_publish *publish = obj;
  1076. return ast_sip_auth_vector_init(&publish->outbound_auths, var->value);
  1077. }
  1078. static int unload_module(void)
  1079. {
  1080. int remaining;
  1081. ast_sorcery_object_unregister(ast_sip_get_sorcery(), "outbound-publish");
  1082. ao2_global_obj_release(current_states);
  1083. /* Wait for publication serializers to get destroyed. */
  1084. ast_debug(2, "Waiting for publication to complete for unload.\n");
  1085. remaining = ast_serializer_shutdown_group_join(shutdown_group, MAX_UNLOAD_TIMEOUT_TIME);
  1086. if (remaining) {
  1087. ast_log(LOG_WARNING, "Unload incomplete. Could not stop %d outbound publications. Try again later.\n",
  1088. remaining);
  1089. return -1;
  1090. }
  1091. ast_debug(2, "Successful shutdown.\n");
  1092. ao2_cleanup(shutdown_group);
  1093. shutdown_group = NULL;
  1094. return 0;
  1095. }
  1096. static int load_module(void)
  1097. {
  1098. CHECK_PJSIP_MODULE_LOADED();
  1099. shutdown_group = ast_serializer_shutdown_group_alloc();
  1100. if (!shutdown_group) {
  1101. return AST_MODULE_LOAD_DECLINE;
  1102. }
  1103. ast_sorcery_apply_config(ast_sip_get_sorcery(), "res_pjsip_outbound_publish");
  1104. ast_sorcery_apply_default(ast_sip_get_sorcery(), "outbound-publish", "config", "pjsip.conf,criteria=type=outbound-publish");
  1105. if (ast_sorcery_object_register(ast_sip_get_sorcery(), "outbound-publish", sip_outbound_publish_alloc, NULL,
  1106. sip_outbound_publish_apply)) {
  1107. ast_log(LOG_ERROR, "Unable to register 'outbound-publish' type with sorcery\n");
  1108. unload_module();
  1109. return AST_MODULE_LOAD_DECLINE;
  1110. }
  1111. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "outbound-publish", "type", "", OPT_NOOP_T, 0, 0);
  1112. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "outbound-publish", "server_uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_outbound_publish, server_uri));
  1113. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "outbound-publish", "from_uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_outbound_publish, from_uri));
  1114. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "outbound-publish", "event", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_outbound_publish, event));
  1115. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "outbound-publish", "to_uri", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_outbound_publish, to_uri));
  1116. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "outbound-publish", "outbound_proxy", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_outbound_publish, outbound_proxy));
  1117. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "outbound-publish", "expiration", "3600", OPT_UINT_T, 0, FLDSET(struct ast_sip_outbound_publish, expiration));
  1118. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "outbound-publish", "max_auth_attempts", "5", OPT_UINT_T, 0, FLDSET(struct ast_sip_outbound_publish, max_auth_attempts));
  1119. ast_sorcery_object_field_register(ast_sip_get_sorcery(), "outbound-publish", "transport", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_sip_outbound_publish, transport));
  1120. ast_sorcery_object_field_register_custom(ast_sip_get_sorcery(), "outbound-publish", "outbound_auth", "", outbound_auth_handler, NULL, NULL, 0, 0);
  1121. ast_sorcery_reload_object(ast_sip_get_sorcery(), "outbound-publish");
  1122. AST_RWLIST_RDLOCK(&publisher_handlers);
  1123. sip_outbound_publish_synchronize(NULL);
  1124. AST_RWLIST_UNLOCK(&publisher_handlers);
  1125. pjsip_publishc_init_module(ast_sip_get_pjsip_endpoint());
  1126. return AST_MODULE_LOAD_SUCCESS;
  1127. }
  1128. static int reload_module(void)
  1129. {
  1130. ast_sorcery_reload_object(ast_sip_get_sorcery(), "outbound-publish");
  1131. AST_RWLIST_RDLOCK(&publisher_handlers);
  1132. sip_outbound_publish_synchronize(NULL);
  1133. AST_RWLIST_UNLOCK(&publisher_handlers);
  1134. return 0;
  1135. }
  1136. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "PJSIP Outbound Publish Support",
  1137. .support_level = AST_MODULE_SUPPORT_CORE,
  1138. .load = load_module,
  1139. .reload = reload_module,
  1140. .unload = unload_module,
  1141. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  1142. );