Debian Codegraph @

res/res_pjsip_exten_state.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013, Digium, Inc.
5 *
6 * Kevin Harwell <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*** MODULEINFO
20 <depend>pjproject</depend>
21 <depend>res_pjsip</depend>
22 <depend>res_pjsip_pubsub</depend>
23 <depend>res_pjsip_outbound_publish</depend>
24 <support_level>core</support_level>
25 ***/
26
27 #include "asterisk.h"
28
29 #include <regex.h>
30
31 #include <pjsip.h>
32 #include <pjsip_simple.h>
33 #include <pjlib.h>
34
35 #include "asterisk/res_pjsip.h"
36 #include "asterisk/res_pjsip_outbound_publish.h"
37 #include "asterisk/res_pjsip_pubsub.h"
38 #include "asterisk/res_pjsip_body_generator_types.h"
39 #include "asterisk/module.h"
40 #include "asterisk/logger.h"
41 #include "asterisk/astobj2.h"
42 #include "asterisk/sorcery.h"
43 #include "asterisk/app.h"
44 #include "asterisk/taskprocessor.h"
45
46 #define BODY_SIZE 1024
47 #define EVENT_TYPE_SIZE 50
48
49 /*!
50 * \brief The number of buckets to use for storing publishers
51 */
52 #define PUBLISHER_BUCKETS 31
53
54 /*!
55 * \brief Container of active outbound extension state publishers
56 */
57 static struct ao2_container *publishers;
58
59 /*! Serializer for outbound extension state publishing. */
60 static struct ast_taskprocessor *publish_exten_state_serializer;
61
62 /*!
63 * \brief A subscription for extension state
64 *
65 * This structure acts as the owner for the underlying SIP subscription. It
66 * also keeps a pointer to an associated "provider" so when a state changes
67 * a notify data creator is quickly accessible.
68 */
69 struct exten_state_subscription {
70 /*! Watcher id when registering for extension state changes */
71 int id;
72 /*! The SIP subscription */
73 struct ast_sip_subscription *sip_sub;
74 /*! The serializer to use for notifications */
75 struct ast_taskprocessor *serializer;
76 /*! Context in which subscription looks for updates */
77 char context[AST_MAX_CONTEXT];
78 /*! Extension within the context to receive updates from */
79 char exten[AST_MAX_EXTENSION];
80 /*! The subscription's user agent */
81 char *user_agent;
82 /*! The last known extension state */
83 enum ast_extension_states last_exten_state;
84 /*! The last known presence state */
85 enum ast_presence_state last_presence_state;
86 };
87
88 /*!
89 * \brief An extension state publisher
90 *
91 */
92 struct exten_state_publisher {
93 /*! Regular expression for context filtering */
94 regex_t context_regex;
95 /*! Regular expression for extension filtering */
96 regex_t exten_regex;
97 /*! Publish client to use for sending publish messages */
98 struct ast_sip_outbound_publish_client *client;
99 /*! Datastores container to hold persistent information */
100 struct ao2_container *datastores;
101 /*! Whether context filtering is active */
102 unsigned int context_filter;
103 /*! Whether extension filtering is active */
104 unsigned int exten_filter;
105 /*! The body type to use for this publisher - stored after the name */
106 char *body_type;
107 /*! The body subtype to use for this publisher - stored after the body type */
108 char *body_subtype;
109 /*! The name of this publisher */
110 char name[0];
111 };
112
113 #define DEFAULT_PRESENCE_BODY "application/pidf+xml"
114 #define DEFAULT_DIALOG_BODY "application/dialog-info+xml"
115
116 static void subscription_shutdown(struct ast_sip_subscription *sub);
117 static int new_subscribe(struct ast_sip_endpoint *endpoint, const char *resource);
118 static int subscription_established(struct ast_sip_subscription *sub);
119 static void *get_notify_data(struct ast_sip_subscription *sub);
120 static int get_resource_display_name(struct ast_sip_endpoint *endpoint, const char *resource, char *display_name, int display_name_size);
121 static void to_ami(struct ast_sip_subscription *sub,
122 struct ast_str **buf);
123 static int publisher_start(struct ast_sip_outbound_publish *configuration,
124 struct ast_sip_outbound_publish_client *client);
125 static int publisher_stop(struct ast_sip_outbound_publish_client *client);
126
127 struct ast_sip_notifier presence_notifier = {
128 .default_accept = DEFAULT_PRESENCE_BODY,
129 .new_subscribe = new_subscribe,
130 .subscription_established = subscription_established,
131 .get_notify_data = get_notify_data,
132 .get_resource_display_name = get_resource_display_name,
133 };
134
135 struct ast_sip_notifier dialog_notifier = {
136 .default_accept = DEFAULT_DIALOG_BODY,
137 .new_subscribe = new_subscribe,
138 .subscription_established = subscription_established,
139 .get_notify_data = get_notify_data,
140 .get_resource_display_name = get_resource_display_name,
141 };
142
143 struct ast_sip_subscription_handler presence_handler = {
144 .event_name = "presence",
145 .body_type = AST_SIP_EXTEN_STATE_DATA,
146 .accept = { DEFAULT_PRESENCE_BODY, },
147 .subscription_shutdown = subscription_shutdown,
148 .to_ami = to_ami,
149 .notifier = &presence_notifier,
150 };
151
152 struct ast_sip_event_publisher_handler presence_publisher = {
153 .event_name = "presence",
154 .start_publishing = publisher_start,
155 .stop_publishing = publisher_stop,
156 };
157
158 struct ast_sip_subscription_handler dialog_handler = {
159 .event_name = "dialog",
160 .body_type = AST_SIP_EXTEN_STATE_DATA,
161 .accept = { DEFAULT_DIALOG_BODY, },
162 .subscription_shutdown = subscription_shutdown,
163 .to_ami = to_ami,
164 .notifier = &dialog_notifier,
165 };
166
167 struct ast_sip_event_publisher_handler dialog_publisher = {
168 .event_name = "dialog",
169 .start_publishing = publisher_start,
170 .stop_publishing = publisher_stop,
171 };
172
173 static void exten_state_subscription_destructor(void *obj)
174 {
175 struct exten_state_subscription *sub = obj;
176
177 ast_free(sub->user_agent);
178 ast_sip_subscription_destroy(sub->sip_sub);
179 ast_taskprocessor_unreference(sub->serializer);
180 }
181
182 static char *get_user_agent(const struct ast_sip_subscription *sip_sub)
183 {
184 size_t size;
185 char *user_agent = NULL;
186 pjsip_user_agent_hdr *user_agent_hdr = ast_sip_subscription_get_header(
187 sip_sub, "User-Agent");
188
189 if (!user_agent_hdr) {
190 return NULL;
191 }
192
193 size = pj_strlen(&user_agent_hdr->hvalue) + 1;
194 user_agent = ast_malloc(size);
195 ast_copy_pj_str(user_agent, &user_agent_hdr->hvalue, size);
196 return ast_str_to_lower(user_agent);
197 }
198
199 /*!
200 * \internal
201 * \brief Initialize the last extension state to something outside
202 * its usual states.
203 */
204 #define INITIAL_LAST_EXTEN_STATE -3
205
206 /*!
207 * \internal
208 * \brief Allocates an exten_state_subscription object.
209 *
210 * Creates the underlying SIP subscription for the given request. First makes
211 * sure that there are registered handler and provider objects available.
212 */
213 static struct exten_state_subscription *exten_state_subscription_alloc(
214 struct ast_sip_subscription *sip_sub, struct ast_sip_endpoint *endpoint)
215 {
216 struct exten_state_subscription * exten_state_sub;
217
218 exten_state_sub = ao2_alloc(sizeof(*exten_state_sub), exten_state_subscription_destructor);
219 if (!exten_state_sub) {
220 return NULL;
221 }
222
223 exten_state_sub->sip_sub = sip_sub;
224
225 /* We keep our own reference to the serializer as there is no guarantee in state_changed
226 * that the subscription tree is still valid when it is called. This can occur when
227 * the subscription is terminated at around the same time as the state_changed
228 * callback is invoked.
229 */
230 exten_state_sub->serializer = ao2_bump(ast_sip_subscription_get_serializer(sip_sub));
231 exten_state_sub->last_exten_state = INITIAL_LAST_EXTEN_STATE;
232 exten_state_sub->last_presence_state = AST_PRESENCE_NOT_SET;
233 exten_state_sub->user_agent = get_user_agent(sip_sub);
234 return exten_state_sub;
235 }
236
237 struct notify_task_data {
238 struct ast_sip_exten_state_data exten_state_data;
239 struct exten_state_subscription *exten_state_sub;
240 int terminate;
241 };
242
243 static void notify_task_data_destructor(void *obj)
244 {
245 struct notify_task_data *task_data = obj;
246
247 ao2_ref(task_data->exten_state_sub, -1);
248 ao2_cleanup(task_data->exten_state_data.device_state_info);
249 ast_free(task_data->exten_state_data.presence_subtype);
250 ast_free(task_data->exten_state_data.presence_message);
251 ast_free(task_data->exten_state_data.user_agent);
252 }
253
254 static struct notify_task_data *alloc_notify_task_data(const char *exten,
255 struct exten_state_subscription *exten_state_sub,
256 struct ast_state_cb_info *info)
257 {
258 struct notify_task_data *task_data =
259 ao2_alloc(sizeof(*task_data), notify_task_data_destructor);
260
261 if (!task_data) {
262 ast_log(LOG_WARNING, "Unable to create notify task data\n");
263 return NULL;
264 }
265
266 task_data->exten_state_sub = exten_state_sub;
267 task_data->exten_state_sub->last_exten_state = info->exten_state;
268 task_data->exten_state_sub->last_presence_state = info->presence_state;
269 ao2_ref(task_data->exten_state_sub, +1);
270
271 task_data->exten_state_data.exten = exten_state_sub->exten;
272 task_data->exten_state_data.exten_state = info->exten_state;
273 task_data->exten_state_data.presence_state = info->presence_state;
274 task_data->exten_state_data.presence_subtype = ast_strdup(info->presence_subtype);
275 task_data->exten_state_data.presence_message = ast_strdup(info->presence_message);
276 task_data->exten_state_data.user_agent = ast_strdup(exten_state_sub->user_agent);
277 task_data->exten_state_data.device_state_info = ao2_bump(info->device_state_info);
278 task_data->exten_state_data.sub = exten_state_sub->sip_sub;
279 task_data->exten_state_data.datastores = ast_sip_subscription_get_datastores(exten_state_sub->sip_sub);
280
281 if ((info->exten_state == AST_EXTENSION_DEACTIVATED) ||
282 (info->exten_state == AST_EXTENSION_REMOVED)) {
283 ast_verb(2, "Watcher for hint %s %s\n", exten, info->exten_state
284 == AST_EXTENSION_REMOVED ? "removed" : "deactivated");
285 task_data->terminate = 1;
286 }
287
288 return task_data;
289 }
290
291 static int notify_task(void *obj)
292 {
293 RAII_VAR(struct notify_task_data *, task_data, obj, ao2_cleanup);
294 struct ast_sip_body_data data = {
295 .body_type = AST_SIP_EXTEN_STATE_DATA,
296 .body_data = &task_data->exten_state_data,
297 };
298
299 /* The subscription was terminated while notify_task was in queue.
300 Terminated subscriptions are no longer associated with a valid tree, and sending
301 * NOTIFY messages on a subscription which has already been terminated won't work.
302 */
303 if (ast_sip_subscription_is_terminated(task_data->exten_state_sub->sip_sub)) {
304 return 0;
305 }
306
307 /* All access to the subscription must occur within a task executed within its serializer */
308 ast_sip_subscription_get_local_uri(task_data->exten_state_sub->sip_sub,
309 task_data->exten_state_data.local, sizeof(task_data->exten_state_data.local));
310 ast_sip_subscription_get_remote_uri(task_data->exten_state_sub->sip_sub,
311 task_data->exten_state_data.remote, sizeof(task_data->exten_state_data.remote));
312
313 /* Pool allocation has to happen here so that we allocate within a PJLIB thread */
314 task_data->exten_state_data.pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
315 "exten_state", 1024, 1024);
316 if (!task_data->exten_state_data.pool) {
317 return -1;
318 }
319
320 task_data->exten_state_data.sub = task_data->exten_state_sub->sip_sub;
321 task_data->exten_state_data.datastores = ast_sip_subscription_get_datastores(task_data->exten_state_sub->sip_sub);
322
323 ast_sip_subscription_notify(task_data->exten_state_sub->sip_sub, &data,
324 task_data->terminate);
325
326 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(),
327 task_data->exten_state_data.pool);
328 return 0;
329 }
330
331 /*!
332 * \internal
333 * \brief Callback for exten/device state changes.
334 *
335 * Upon state change, send the appropriate notification to the subscriber.
336 */
337 static int state_changed(const char *context, const char *exten,
338 struct ast_state_cb_info *info, void *data)
339 {
340 struct notify_task_data *task_data;
341 struct exten_state_subscription *exten_state_sub = data;
342
343 /* Terminated subscriptions are no longer associated with a valid tree.
344 * Do not queue notify_task.
345 */
346 if (ast_sip_subscription_is_terminated(exten_state_sub->sip_sub)) {
347 return 0;
348 }
349
350 if (!(task_data = alloc_notify_task_data(exten, exten_state_sub, info))) {
351 return -1;
352 }
353
354 /* safe to push this async since we copy the data from info and
355 add a ref for the device state info */
356 if (ast_sip_push_task(task_data->exten_state_sub->serializer, notify_task,
357 task_data)) {
358 ao2_cleanup(task_data);
359 return -1;
360 }
361 return 0;
362 }
363
364 static void state_changed_destroy(int id, void *data)
365 {
366 struct exten_state_subscription *exten_state_sub = data;
367 ao2_cleanup(exten_state_sub);
368 }
369
370 static struct ast_datastore_info ds_info = { };
371 static const char ds_name[] = "exten state datastore";
372
373 /*!
374 * \internal
375 * \brief Add a datastore for exten exten_state_subscription.
376 *
377 * Adds the exten_state_subscription wrapper object to a datastore so it can be retrieved
378 * later based upon its association with the ast_sip_subscription.
379 */
380 static int add_datastore(struct exten_state_subscription *exten_state_sub)
381 {
382 RAII_VAR(struct ast_datastore *, datastore,
383 ast_sip_subscription_alloc_datastore(&ds_info, ds_name), ao2_cleanup);
384
385 if (!datastore) {
386 return -1;
387 }
388
389 datastore->data = exten_state_sub;
390 ast_sip_subscription_add_datastore(exten_state_sub->sip_sub, datastore);
391 ao2_ref(exten_state_sub, +1);
392 return 0;
393 }
394
395 /*!
396 * \internal
397 * \brief Get the exten_state_subscription object associated with the given
398 * ast_sip_subscription in the datastore.
399 */
400 static struct exten_state_subscription *get_exten_state_sub(
401 struct ast_sip_subscription *sub)
402 {
403 RAII_VAR(struct ast_datastore *, datastore,
404 ast_sip_subscription_get_datastore(sub, ds_name), ao2_cleanup);
405
406 return datastore ? datastore->data : NULL;
407 }
408
409 static void subscription_shutdown(struct ast_sip_subscription *sub)
410 {
411 struct exten_state_subscription *exten_state_sub = get_exten_state_sub(sub);
412
413 if (!exten_state_sub) {
414 return;
415 }
416
417 ast_extension_state_del(exten_state_sub->id, state_changed);
418 ast_sip_subscription_remove_datastore(exten_state_sub->sip_sub, ds_name);
419 /* remove data store reference */
420 ao2_cleanup(exten_state_sub);
421 }
422
423 static int new_subscribe(struct ast_sip_endpoint *endpoint,
424 const char *resource)
425 {
426 const char *context = S_OR(endpoint->subscription.context, endpoint->context);
427
428 if (!ast_exists_extension(NULL, context, resource, PRIORITY_HINT, NULL)) {
429 ast_log(LOG_NOTICE, "Endpoint '%s' state subscription failed: "
430 "Extension '%s' does not exist in context '%s' or has no associated hint\n",
431 ast_sorcery_object_get_id(endpoint), resource, context);
432 return 404;
433 }
434
435 return 200;
436 }
437
438 static int get_resource_display_name(struct ast_sip_endpoint *endpoint,
439 const char *resource, char *display_name, int display_name_size)
440 {
441 const char *context;
442
443 if (!endpoint || ast_strlen_zero(resource) || !display_name || display_name_size <= 0) {
444 return -1;
445 }
446
447 context = S_OR(endpoint->subscription.context, endpoint->context);
448
449 if (!ast_get_hint(NULL, 0, display_name, display_name_size, NULL, context, resource)) {
450 ast_log(LOG_NOTICE, "Endpoint '%s': "
451 "Extension '%s' does not exist in context '%s' or has no associated hint\n",
452 ast_sorcery_object_get_id(endpoint), resource, context);
453 return -1;
454 }
455
456 return 0;
457 }
458
459 static int subscription_established(struct ast_sip_subscription *sip_sub)
460 {
461 struct ast_sip_endpoint *endpoint = ast_sip_subscription_get_endpoint(sip_sub);
462 const char *resource = ast_sip_subscription_get_resource_name(sip_sub);
463 struct exten_state_subscription *exten_state_sub;
464
465 if (!(exten_state_sub = exten_state_subscription_alloc(sip_sub, endpoint))) {
466 ao2_cleanup(endpoint);
467 return -1;
468 }
469
470 ast_copy_string(exten_state_sub->context,
471 S_OR(endpoint->subscription.context, endpoint->context),
472 sizeof(exten_state_sub->context));
473 ast_copy_string(exten_state_sub->exten, resource, sizeof(exten_state_sub->exten));
474
475 if ((exten_state_sub->id = ast_extension_state_add_destroy_extended(
476 exten_state_sub->context, exten_state_sub->exten,
477 state_changed, state_changed_destroy, exten_state_sub)) < 0) {
478 ast_log(LOG_WARNING, "Unable to subscribe endpoint '%s' to extension '%s@%s'\n",
479 ast_sorcery_object_get_id(endpoint), exten_state_sub->exten,
480 exten_state_sub->context);
481 ao2_cleanup(endpoint);
482 ao2_cleanup(exten_state_sub);
483 return -1;
484 }
485
486 /* Go ahead and cleanup the endpoint since we don't need it anymore */
487 ao2_cleanup(endpoint);
488
489 /* bump the ref since ast_extension_state_add holds a reference */
490 ao2_ref(exten_state_sub, +1);
491
492 if (add_datastore(exten_state_sub)) {
493 ast_log(LOG_WARNING, "Unable to add to subscription datastore.\n");
494 ao2_cleanup(exten_state_sub);
495 return -1;
496 }
497
498 ao2_cleanup(exten_state_sub);
499 return 0;
500 }
501
502 static void exten_state_data_destructor(void *obj)
503 {
504 struct ast_sip_exten_state_data *exten_state_data = obj;
505
506 ao2_cleanup(exten_state_data->device_state_info);
507 ast_free(exten_state_data->presence_subtype);
508 ast_free(exten_state_data->presence_message);
509 if (exten_state_data->pool) {
510 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), exten_state_data->pool);
511 }
512 }
513
514 static struct ast_sip_exten_state_data *exten_state_data_alloc(struct ast_sip_subscription *sip_sub,
515 struct exten_state_subscription *exten_state_sub)
516 {
517 struct ast_sip_exten_state_data *exten_state_data;
518 char *subtype = NULL;
519 char *message = NULL;
520 int presence_state;
521
522 exten_state_data = ao2_alloc(sizeof(*exten_state_data), exten_state_data_destructor);
523 if (!exten_state_data) {
524 return NULL;
525 }
526
527 exten_state_data->exten = exten_state_sub->exten;
528 presence_state = ast_hint_presence_state(NULL, exten_state_sub->context, exten_state_sub->exten, &subtype, &message);
529 if (presence_state == -1 || presence_state == AST_PRESENCE_INVALID) {
530 ao2_cleanup(exten_state_data);
531 return NULL;
532 }
533 exten_state_data->presence_state = presence_state;
534 exten_state_data->presence_subtype = subtype;
535 exten_state_data->presence_message = message;
536 exten_state_data->user_agent = exten_state_sub->user_agent;
537 ast_sip_subscription_get_local_uri(sip_sub, exten_state_data->local,
538 sizeof(exten_state_data->local));
539 ast_sip_subscription_get_remote_uri(sip_sub, exten_state_data->remote,
540 sizeof(exten_state_data->remote));
541 exten_state_data->sub = sip_sub;
542 exten_state_data->datastores = ast_sip_subscription_get_datastores(sip_sub);
543
544 exten_state_data->exten_state = ast_extension_state_extended(
545 NULL, exten_state_sub->context, exten_state_sub->exten,
546 &exten_state_data->device_state_info);
547 if (exten_state_data->exten_state < 0) {
548 ao2_cleanup(exten_state_data);
549 return NULL;
550 }
551
552 exten_state_data->pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(),
553 "exten_state", 1024, 1024);
554 if (!exten_state_data->pool) {
555 ao2_cleanup(exten_state_data);
556 return NULL;
557 }
558
559 return exten_state_data;
560 }
561
562 static void *get_notify_data(struct ast_sip_subscription *sub)
563 {
564 struct exten_state_subscription *exten_state_sub;
565
566 exten_state_sub = get_exten_state_sub(sub);
567 if (!exten_state_sub) {
568 return NULL;
569 }
570
571 return exten_state_data_alloc(sub, exten_state_sub);
572 }
573
574 static void to_ami(struct ast_sip_subscription *sub,
575 struct ast_str **buf)
576 {
577 struct exten_state_subscription *exten_state_sub =
578 get_exten_state_sub(sub);
579
580 if (!exten_state_sub) {
581 return;
582 }
583
584 ast_str_append(buf, 0, "SubscriptionType: extension_state\r\n"
585 "Extension: %s\r\nExtensionStates: %s\r\n",
586 exten_state_sub->exten, ast_extension_state2str(
587 exten_state_sub->last_exten_state));
588 }
589
590 struct exten_state_pub_data {
591 /*! Publishers needing state update */
592 AST_VECTOR(name, struct exten_state_publisher *) pubs;
593 /*! Body generator state data */
594 struct ast_sip_exten_state_data exten_state_data;
595 };
596
597 static void exten_state_pub_data_destroy(struct exten_state_pub_data *doomed)
598 {
599 if (!doomed) {
600 return;
601 }
602
603 ast_free((void *) doomed->exten_state_data.exten);
604 ast_free(doomed->exten_state_data.presence_subtype);
605 ast_free(doomed->exten_state_data.presence_message);
606 ao2_cleanup(doomed->exten_state_data.device_state_info);
607
608 AST_VECTOR_CALLBACK_VOID(&doomed->pubs, ao2_ref, -1);
609 AST_VECTOR_FREE(&doomed->pubs);
610
611 ast_free(doomed);
612 }
613
614 static struct exten_state_pub_data *exten_state_pub_data_alloc(const char *exten, struct ast_state_cb_info *info)
615 {
616 struct exten_state_pub_data *pub_data;
617
618 pub_data = ast_calloc(1, sizeof(*pub_data));
619 if (!pub_data) {
620 return NULL;
621 }
622
623 if (AST_VECTOR_INIT(&pub_data->pubs, ao2_container_count(publishers))) {
624 exten_state_pub_data_destroy(pub_data);
625 return NULL;
626 }
627
628 /* Save off currently known information for the body generators. */
629 pub_data->exten_state_data.exten = ast_strdup(exten);
630 pub_data->exten_state_data.exten_state = info->exten_state;
631 pub_data->exten_state_data.presence_state = info->presence_state;
632 pub_data->exten_state_data.presence_subtype = ast_strdup(info->presence_subtype);
633 pub_data->exten_state_data.presence_message = ast_strdup(info->presence_message);
634 pub_data->exten_state_data.device_state_info = ao2_bump(info->device_state_info);
635 if (!pub_data->exten_state_data.exten
636 || !pub_data->exten_state_data.presence_subtype
637 || !pub_data->exten_state_data.presence_message) {
638 exten_state_pub_data_destroy(pub_data);
639 return NULL;
640 }
641 return pub_data;
642 }
643
644 /*!
645 * \internal
646 * \brief Create exten state PUBLISH messages under PJSIP thread.
647 * \since 14.0.0
648 *
649 * \return 0
650 */
651 static int exten_state_publisher_cb(void *data)
652 {
653 struct exten_state_pub_data *pub_data = data;
654 struct exten_state_publisher *publisher;
655 size_t idx;
656 struct ast_str *body_text;
657 pj_pool_t *pool;
658 struct ast_sip_body_data gen_data = {
659 .body_type = AST_SIP_EXTEN_STATE_DATA,
660 .body_data = &pub_data->exten_state_data,
661 };
662 struct ast_sip_body body;
663
664 body_text = ast_str_create(64);
665 if (!body_text) {
666 exten_state_pub_data_destroy(pub_data);
667 return 0;
668 }
669
670 /* Need a PJSIP memory pool to generate the bodies. */
671 pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "pub_state_body",
672 1024, 1024);
673 if (!pool) {
674 ast_log(LOG_WARNING, "Exten state publishing unable to create memory pool\n");
675 exten_state_pub_data_destroy(pub_data);
676 ast_free(body_text);
677 return 0;
678 }
679 pub_data->exten_state_data.pool = pool;
680
681 for (idx = 0; idx < AST_VECTOR_SIZE(&pub_data->pubs); ++idx) {
682 const char *uri;
683 int res;
684
685 publisher = AST_VECTOR_GET(&pub_data->pubs, idx);
686
687 uri = ast_sip_publish_client_get_user_from_uri(publisher->client, pub_data->exten_state_data.exten,
688 pub_data->exten_state_data.local, sizeof(pub_data->exten_state_data.local));
689 if (ast_strlen_zero(uri)) {
690 ast_log(LOG_WARNING, "PUBLISH client '%s' has no from_uri or server_uri defined.\n",
691 publisher->name);
692 continue;
693 }
694
695 uri = ast_sip_publish_client_get_user_to_uri(publisher->client, pub_data->exten_state_data.exten,
696 pub_data->exten_state_data.remote, sizeof(pub_data->exten_state_data.remote));
697 if (ast_strlen_zero(uri)) {
698 ast_log(LOG_WARNING, "PUBLISH client '%s' has no to_uri or server_uri defined.\n",
699 publisher->name);
700 continue;
701 }
702
703 pub_data->exten_state_data.datastores = publisher->datastores;
704
705 res = ast_sip_pubsub_generate_body_content(publisher->body_type,
706 publisher->body_subtype, &gen_data, &body_text);
707 pj_pool_reset(pool);
708 if (res) {
709 ast_log(LOG_WARNING,
710 "PUBLISH client '%s' unable to generate %s/%s PUBLISH body.\n",
711 publisher->name, publisher->body_type, publisher->body_subtype);
712 continue;
713 }
714
715 body.type = publisher->body_type;
716 body.subtype = publisher->body_subtype;
717 body.body_text = ast_str_buffer(body_text);
718 ast_sip_publish_client_user_send(publisher->client, pub_data->exten_state_data.exten, &body);
719 }
720
721 pjsip_endpt_release_pool(ast_sip_get_pjsip_endpoint(), pool);
722
723 ast_free(body_text);
724 exten_state_pub_data_destroy(pub_data);
725 return 0;
726 }
727
728 /*!
729 * \brief Global extension state callback function
730 */
731 static int exten_state_publisher_state_cb(const char *context, const char *exten, struct ast_state_cb_info *info, void *data)
732 {
733 struct ao2_iterator publisher_iter;
734 struct exten_state_publisher *publisher;
735 struct exten_state_pub_data *pub_data = NULL;
736
737 ast_debug(5, "Exten state publisher: %s@%s Reason:%s State:%s Presence:%s Subtype:'%s' Message:'%s'\n",
738 exten, context,
739 info->reason == AST_HINT_UPDATE_DEVICE
740 ? "Device"
741 : info->reason == AST_HINT_UPDATE_PRESENCE
742 ? "Presence"
743 : "Unknown",
744 ast_extension_state2str(info->exten_state),
745 ast_presence_state2str(info->presence_state),
746 S_OR(info->presence_subtype, ""),
747 S_OR(info->presence_message, ""));
748 publisher_iter = ao2_iterator_init(publishers, 0);
749 for (; (publisher = ao2_iterator_next(&publisher_iter)); ao2_ref(publisher, -1)) {
750 if ((publisher->context_filter && regexec(&publisher->context_regex, context, 0, NULL, 0)) ||
751 (publisher->exten_filter && regexec(&publisher->exten_regex, exten, 0, NULL, 0))) {
752 continue;
753 }
754
755 if (!pub_data) {
756 pub_data = exten_state_pub_data_alloc(exten, info);
757 if (!pub_data) {
758 ao2_ref(publisher, -1);
759 break;
760 }
761 }
762
763 ao2_ref(publisher, +1);
764 if (AST_VECTOR_APPEND(&pub_data->pubs, publisher)) {
765 ao2_ref(publisher, -1);
766 } else {
767 ast_debug(5, "'%s' will publish exten state\n", publisher->name);
768 }
769 }
770 ao2_iterator_destroy(&publisher_iter);
771
772 if (pub_data
773 && ast_sip_push_task(publish_exten_state_serializer, exten_state_publisher_cb,
774 pub_data)) {
775 exten_state_pub_data_destroy(pub_data);
776 }
777
778 return 0;
779 }
780
781 /*!
782 * \brief Hashing function for extension state publisher
783 */
784 static int exten_state_publisher_hash(const void *obj, const int flags)
785 {
786 const struct exten_state_publisher *object;
787 const char *key;
788
789 switch (flags & OBJ_SEARCH_MASK) {
790 case OBJ_SEARCH_KEY:
791 key = obj;
792 break;
793 case OBJ_SEARCH_OBJECT:
794 object = obj;
795 key = object->name;
796 break;
797 default:
798 ast_assert(0);
799 return 0;
800 }
801 return ast_str_hash(key);
802 }
803
804 /*!
805 * \brief Comparator function for extension state publisher
806 */
807 static int exten_state_publisher_cmp(void *obj, void *arg, int flags)
808 {
809 const struct exten_state_publisher *object_left = obj;
810 const struct exten_state_publisher *object_right = arg;
811 const char *right_key = arg;
812 int cmp;
813
814 switch (flags & OBJ_SEARCH_MASK) {
815 case OBJ_SEARCH_OBJECT:
816 right_key = object_right->name;
817 /* Fall through */
818 case OBJ_SEARCH_KEY:
819 cmp = strcmp(object_left->name, right_key);
820 break;
821 case OBJ_SEARCH_PARTIAL_KEY:
822 /* Not supported by container. */
823 ast_assert(0);
824 return 0;
825 default:
826 cmp = 0;
827 break;
828 }
829 if (cmp) {
830 return 0;
831 }
832 return CMP_MATCH;
833 }
834
835 /*!
836 * \brief Destructor for extension state publisher
837 */
838 static void exten_state_publisher_destroy(void *obj)
839 {
840 struct exten_state_publisher *publisher = obj;
841
842 if (publisher->context_filter) {
843 regfree(&publisher->context_regex);
844 }
845
846 if (publisher->exten_filter) {
847 regfree(&publisher->exten_regex);
848 }
849
850 ao2_cleanup(publisher->client);
851 ao2_cleanup(publisher->datastores);
852 }
853
854 static int build_regex(regex_t *regex, const char *text)
855 {
856 int res;
857
858 if ((res = regcomp(regex, text, REG_EXTENDED | REG_ICASE | REG_NOSUB))) {
859 size_t len = regerror(res, regex, NULL, 0);
860 char buf[len];
861 regerror(res, regex, buf, len);
862 ast_log(LOG_ERROR, "Could not compile regex '%s': %s\n", text, buf);
863 return -1;
864 }
865
866 return 0;
867 }
868
869 static int publisher_start(struct ast_sip_outbound_publish *configuration, struct ast_sip_outbound_publish_client *client)
870 {
871 struct exten_state_publisher *publisher;
872 size_t name_size;
873 size_t body_type_size;
874 size_t body_subtype_size;
875 char *body_subtype;
876 const char *body_full;
877 const char *body_type;
878 const char *name;
879 const char *context;
880 const char *exten;
881
882 name = ast_sorcery_object_get_id(configuration);
883
884 body_full = ast_sorcery_object_get_extended(configuration, "body");
885 if (ast_strlen_zero(body_full)) {
886 ast_log(LOG_ERROR, "Outbound extension state publisher '%s': Body not set\n",
887 name);
888 return -1;
889 }
890
891 body_subtype = ast_strdupa(body_full);
892 body_type = strsep(&body_subtype, "/");
893 if (ast_strlen_zero(body_type) || ast_strlen_zero(body_subtype)) {
894 ast_log(LOG_ERROR, "Outbound extension state publisher '%s': Body '%s' missing type or subtype\n",
895 name, body_full);
896 return -1;
897 }
898
899 if (!ast_sip_pubsub_is_body_generator_registered(body_type, body_subtype)) {
900 ast_log(LOG_ERROR, "Outbound extension state publisher '%s': '%s' body generator not registered\n",
901 name, body_full);
902 return -1;
903 }
904
905 name_size = strlen(name) + 1;
906 body_type_size = strlen(body_type) + 1;
907 body_subtype_size = strlen(body_subtype) + 1;
908
909 publisher = ao2_alloc_options(
910 sizeof(*publisher) + name_size + body_type_size + body_subtype_size,
911 exten_state_publisher_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
912 if (!publisher) {
913 return -1;
914 }
915
916 ast_copy_string(publisher->name, name, name_size);
917 publisher->body_type = publisher->name + name_size;
918 ast_copy_string(publisher->body_type, body_type, body_type_size);
919 publisher->body_subtype = publisher->body_type + body_type_size;
920 ast_copy_string(publisher->body_subtype, body_subtype, body_subtype_size);
921
922 context = ast_sorcery_object_get_extended(configuration, "context");
923 if (!ast_strlen_zero(context)) {
924 if (build_regex(&publisher->context_regex, context)) {
925 ast_log(LOG_ERROR, "Outbound extension state publisher '%s': Could not build context filter '%s'\n",
926 name, context);
927 ao2_ref(publisher, -1);
928 return -1;
929 }
930
931 publisher->context_filter = 1;
932 }
933
934 exten = ast_sorcery_object_get_extended(configuration, "exten");
935 if (!ast_strlen_zero(exten)) {
936 if (build_regex(&publisher->exten_regex, exten)) {
937 ast_log(LOG_ERROR, "Outbound extension state publisher '%s': Could not build exten filter '%s'\n",
938 name, exten);
939 ao2_ref(publisher, -1);
940 return -1;
941 }
942
943 publisher->exten_filter = 1;
944 }
945
946 publisher->datastores = ast_datastores_alloc();
947 if (!publisher->datastores) {
948 ast_log(LOG_ERROR, "Outbound extension state publisher '%s': Could not create datastores container\n",
949 name);
950 ao2_ref(publisher, -1);
951 return -1;
952 }
953
954 publisher->client = ao2_bump(client);
955
956 ao2_lock(publishers);
957 if (!ao2_container_count(publishers)) {
958 ast_extension_state_add(NULL, NULL, exten_state_publisher_state_cb, NULL);
959 }
960 ao2_link_flags(publishers, publisher, OBJ_NOLOCK);
961 ao2_unlock(publishers);
962
963 ao2_ref(publisher, -1);
964
965 return 0;
966 }
967
968 static int publisher_stop(struct ast_sip_outbound_publish_client *client)
969 {
970 ao2_find(publishers, ast_sorcery_object_get_id(client), OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
971 return 0;
972 }
973
974 static int unload_module(void)
975 {
976 /*
977 * pjsip_evsub_register_pkg is called by ast_sip_register_subscription_handler
978 * but there is no corresponding unregister function, so unloading
979 * a module does not remove the event package. If this module is ever
980 * loaded again, then pjproject will assert and cause a crash.
981 * For that reason, we must only be allowed to unload when
982 * asterisk is shutting down. If a pjsip_evsub_unregister_pkg
983 * API is added in the future then we should go back to unloading
984 * the module as intended.
985 */
986
987 if (ast_shutdown_final()) {
988 ast_sip_unregister_event_publisher_handler(&dialog_publisher);
989 ast_sip_unregister_subscription_handler(&dialog_handler);
990 ast_sip_unregister_event_publisher_handler(&presence_publisher);
991 ast_sip_unregister_subscription_handler(&presence_handler);
992
993 ast_extension_state_del(0, exten_state_publisher_state_cb);
994
995 ast_taskprocessor_unreference(publish_exten_state_serializer);
996 publish_exten_state_serializer = NULL;
997
998 ao2_cleanup(publishers);
999 publishers = NULL;
1000
1001 return 0;
1002 } else {
1003 return -1;
1004 }
1005 }
1006
1007 static int load_module(void)
1008 {
1009 publishers = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
1010 PUBLISHER_BUCKETS, exten_state_publisher_hash, NULL, exten_state_publisher_cmp);
1011 if (!publishers) {
1012 ast_log(LOG_WARNING, "Unable to create container to store extension state publishers\n");
1013 return AST_MODULE_LOAD_DECLINE;
1014 }
1015
1016 publish_exten_state_serializer = ast_sip_create_serializer("pjsip/exten_state");
1017 if (!publish_exten_state_serializer) {
1018 unload_module();
1019 return AST_MODULE_LOAD_DECLINE;
1020 }
1021
1022 if (ast_sip_register_subscription_handler(&presence_handler)) {
1023 ast_log(LOG_WARNING, "Unable to register subscription handler %s\n",
1024 presence_handler.event_name);
1025 unload_module();
1026 return AST_MODULE_LOAD_DECLINE;
1027 }
1028
1029 if (ast_sip_register_event_publisher_handler(&presence_publisher)) {
1030 ast_log(LOG_WARNING, "Unable to register presence publisher %s\n",
1031 presence_publisher.event_name);
1032 unload_module();
1033 return AST_MODULE_LOAD_DECLINE;
1034 }
1035
1036 if (ast_sip_register_subscription_handler(&dialog_handler)) {
1037 ast_log(LOG_WARNING, "Unable to register subscription handler %s\n",
1038 dialog_handler.event_name);
1039 unload_module();
1040 return AST_MODULE_LOAD_DECLINE;
1041 }
1042
1043 if (ast_sip_register_event_publisher_handler(&dialog_publisher)) {
1044 ast_log(LOG_WARNING, "Unable to register presence publisher %s\n",
1045 dialog_publisher.event_name);
1046 unload_module();
1047 return AST_MODULE_LOAD_DECLINE;
1048 }
1049
1050 return AST_MODULE_LOAD_SUCCESS;
1051 }
1052
1053 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Extension State Notifications",
1054 .support_level = AST_MODULE_SUPPORT_CORE,
1055 .load = load_module,
1056 .unload = unload_module,
1057 .load_pri = AST_MODPRI_CHANNEL_DEPEND + 5,
1058 .requires = "res_pjsip,res_pjsip_pubsub,res_pjsip_outbound_publish",
1059 );