stasis: Add internal filtering of messages.
[asterisk/asterisk.git] / main / endpoints.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * David M. Lee, II <dlee@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Asterisk endpoint API.
22  *
23  * \author David M. Lee, II <dlee@digium.com>
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 #include "asterisk/astobj2.h"
33 #include "asterisk/endpoints.h"
34 #include "asterisk/stasis.h"
35 #include "asterisk/stasis_channels.h"
36 #include "asterisk/stasis_endpoints.h"
37 #include "asterisk/stasis_message_router.h"
38 #include "asterisk/stringfields.h"
39 #include "asterisk/_private.h"
40
41 /*! Buckets for endpoint->channel mappings. Keep it prime! */
42 #define ENDPOINT_CHANNEL_BUCKETS 127
43
44 /*! Buckets for endpoint hash. Keep it prime! */
45 #define ENDPOINT_BUCKETS 127
46
47 /*! Buckets for technology endpoints. */
48 #define TECH_ENDPOINT_BUCKETS 11
49
50 static struct ao2_container *endpoints;
51
52 static struct ao2_container *tech_endpoints;
53
54 struct ast_endpoint {
55         AST_DECLARE_STRING_FIELDS(
56                 AST_STRING_FIELD(tech); /*!< Technology (SIP, IAX2, etc.). */
57                 AST_STRING_FIELD(resource);     /*!< Name, unique to the tech. */
58                 AST_STRING_FIELD(id);   /*!< tech/resource id */
59                 );
60         /*! Endpoint's current state */
61         enum ast_endpoint_state state;
62         /*!
63          * \brief Max channels for this endpoint. -1 means unlimited or unknown.
64          *
65          * Note that this simply documents the limits of an endpoint, and does
66          * nothing to try to enforce the limit.
67          */
68         int max_channels;
69         /*! Topic for this endpoint's messages */
70         struct stasis_cp_single *topics;
71         /*! Router for handling this endpoint's messages */
72         struct stasis_message_router *router;
73         /*! ast_str_container of channels associated with this endpoint */
74         struct ao2_container *channel_ids;
75         /*! Forwarding subscription from an endpoint to its tech endpoint */
76         struct stasis_forward *tech_forward;
77 };
78
79 AO2_STRING_FIELD_HASH_FN(ast_endpoint, id)
80 AO2_STRING_FIELD_CMP_FN(ast_endpoint, id)
81
82 struct ast_endpoint *ast_endpoint_find_by_id(const char *id)
83 {
84         struct ast_endpoint *endpoint = ao2_find(endpoints, id, OBJ_KEY);
85
86         if (!endpoint) {
87                 endpoint = ao2_find(tech_endpoints, id, OBJ_KEY);
88         }
89
90         return endpoint;
91 }
92
93 struct stasis_topic *ast_endpoint_topic(struct ast_endpoint *endpoint)
94 {
95         if (!endpoint) {
96                 return ast_endpoint_topic_all();
97         }
98         return stasis_cp_single_topic(endpoint->topics);
99 }
100
101 struct stasis_topic *ast_endpoint_topic_cached(struct ast_endpoint *endpoint)
102 {
103         if (!endpoint) {
104                 return ast_endpoint_topic_all_cached();
105         }
106         return stasis_cp_single_topic_cached(endpoint->topics);
107 }
108
109 const char *ast_endpoint_state_to_string(enum ast_endpoint_state state)
110 {
111         switch (state) {
112         case AST_ENDPOINT_UNKNOWN:
113                 return "unknown";
114         case AST_ENDPOINT_OFFLINE:
115                 return "offline";
116         case AST_ENDPOINT_ONLINE:
117                 return "online";
118         }
119         return "?";
120 }
121
122 static void endpoint_publish_snapshot(struct ast_endpoint *endpoint)
123 {
124         RAII_VAR(struct ast_endpoint_snapshot *, snapshot, NULL, ao2_cleanup);
125         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
126
127         ast_assert(endpoint != NULL);
128         ast_assert(endpoint->topics != NULL);
129
130         if (!ast_endpoint_snapshot_type()) {
131                 return;
132         }
133
134         snapshot = ast_endpoint_snapshot_create(endpoint);
135         if (!snapshot) {
136                 return;
137         }
138         message = stasis_message_create(ast_endpoint_snapshot_type(), snapshot);
139         if (!message) {
140                 return;
141         }
142         stasis_publish(ast_endpoint_topic(endpoint), message);
143 }
144
145 static void endpoint_dtor(void *obj)
146 {
147         struct ast_endpoint *endpoint = obj;
148
149         /* The router should be shut down already */
150         ast_assert(stasis_message_router_is_done(endpoint->router));
151         ao2_cleanup(endpoint->router);
152         endpoint->router = NULL;
153
154         stasis_cp_single_unsubscribe(endpoint->topics);
155         endpoint->topics = NULL;
156
157         ao2_cleanup(endpoint->channel_ids);
158         endpoint->channel_ids = NULL;
159
160         ast_string_field_free_memory(endpoint);
161 }
162
163
164 int ast_endpoint_add_channel(struct ast_endpoint *endpoint,
165         struct ast_channel *chan)
166 {
167         ast_assert(chan != NULL);
168         ast_assert(endpoint != NULL);
169         ast_assert(!ast_strlen_zero(endpoint->resource));
170
171         ast_channel_forward_endpoint(chan, endpoint);
172
173         ao2_lock(endpoint);
174         ast_str_container_add(endpoint->channel_ids, ast_channel_uniqueid(chan));
175         ao2_unlock(endpoint);
176
177         endpoint_publish_snapshot(endpoint);
178
179         return 0;
180 }
181
182 /*! \brief Handler for channel snapshot cache clears */
183 static void endpoint_cache_clear(void *data,
184         struct stasis_subscription *sub,
185         struct stasis_message *message)
186 {
187         struct ast_endpoint *endpoint = data;
188         struct stasis_message *clear_msg = stasis_message_data(message);
189         struct ast_channel_snapshot *clear_snapshot;
190
191         if (stasis_message_type(clear_msg) != ast_channel_snapshot_type()) {
192                 return;
193         }
194
195         clear_snapshot = stasis_message_data(clear_msg);
196
197         ast_assert(endpoint != NULL);
198
199         ao2_lock(endpoint);
200         ast_str_container_remove(endpoint->channel_ids, clear_snapshot->uniqueid);
201         ao2_unlock(endpoint);
202         endpoint_publish_snapshot(endpoint);
203 }
204
205 static void endpoint_subscription_change(void *data,
206         struct stasis_subscription *sub,
207         struct stasis_message *message)
208 {
209         struct stasis_endpoint *endpoint = data;
210
211         if (stasis_subscription_final_message(sub, message)) {
212                 ao2_cleanup(endpoint);
213         }
214 }
215
216 static struct ast_endpoint *endpoint_internal_create(const char *tech, const char *resource)
217 {
218         RAII_VAR(struct ast_endpoint *, endpoint, NULL, ao2_cleanup);
219         RAII_VAR(struct ast_endpoint *, tech_endpoint, NULL, ao2_cleanup);
220         int r = 0;
221
222         /* Get/create the technology endpoint */
223         if (!ast_strlen_zero(resource)) {
224                 tech_endpoint = ao2_find(tech_endpoints, tech, OBJ_KEY);
225                 if (!tech_endpoint) {
226                         tech_endpoint = endpoint_internal_create(tech, NULL);
227                         if (!tech_endpoint) {
228                                 return NULL;
229                         }
230                 }
231         }
232
233         endpoint = ao2_alloc(sizeof(*endpoint), endpoint_dtor);
234         if (!endpoint) {
235                 return NULL;
236         }
237
238         endpoint->max_channels = -1;
239         endpoint->state = AST_ENDPOINT_UNKNOWN;
240
241         if (ast_string_field_init(endpoint, 80) != 0) {
242                 return NULL;
243         }
244         ast_string_field_set(endpoint, tech, tech);
245         ast_string_field_set(endpoint, resource, S_OR(resource, ""));
246         ast_string_field_build(endpoint, id, "%s%s%s",
247                 tech,
248                 !ast_strlen_zero(resource) ? "/" : "",
249                 S_OR(resource, ""));
250
251         /* All access to channel_ids should be covered by the endpoint's
252          * lock; no extra lock needed. */
253         endpoint->channel_ids = ast_str_container_alloc_options(
254                 AO2_ALLOC_OPT_LOCK_NOLOCK, ENDPOINT_CHANNEL_BUCKETS);
255         if (!endpoint->channel_ids) {
256                 return NULL;
257         }
258
259         if (!ast_strlen_zero(resource)) {
260
261                 endpoint->topics = stasis_cp_single_create(ast_endpoint_cache_all(),
262                         endpoint->id);
263                 if (!endpoint->topics) {
264                         return NULL;
265                 }
266                 stasis_cp_single_accept_message_type(endpoint->topics, ast_endpoint_snapshot_type());
267                 stasis_cp_single_set_filter(endpoint->topics, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
268
269                 endpoint->router = stasis_message_router_create_pool(ast_endpoint_topic(endpoint));
270                 if (!endpoint->router) {
271                         return NULL;
272                 }
273                 r |= stasis_message_router_add(endpoint->router,
274                         stasis_cache_clear_type(), endpoint_cache_clear,
275                         endpoint);
276                 r |= stasis_message_router_add(endpoint->router,
277                         stasis_subscription_change_type(), endpoint_subscription_change,
278                         endpoint);
279                 if (r) {
280                         return NULL;
281                 }
282
283                 endpoint->tech_forward = stasis_forward_all(stasis_cp_single_topic(endpoint->topics),
284                         stasis_cp_single_topic(tech_endpoint->topics));
285
286                 endpoint_publish_snapshot(endpoint);
287                 ao2_link(endpoints, endpoint);
288         } else {
289                 endpoint->topics = stasis_cp_sink_create(ast_endpoint_cache_all(),
290                         endpoint->id);
291                 if (!endpoint->topics) {
292                         return NULL;
293                 }
294                 stasis_cp_single_accept_message_type(endpoint->topics, ast_endpoint_snapshot_type());
295                 stasis_cp_single_set_filter(endpoint->topics, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
296
297                 ao2_link(tech_endpoints, endpoint);
298         }
299
300         ao2_ref(endpoint, +1);
301         return endpoint;
302 }
303
304 struct ast_endpoint *ast_endpoint_create(const char *tech, const char *resource)
305 {
306         if (ast_strlen_zero(tech)) {
307                 ast_log(LOG_ERROR, "Endpoint tech cannot be empty\n");
308                 return NULL;
309         }
310
311         if (ast_strlen_zero(resource)) {
312                 ast_log(LOG_ERROR, "Endpoint resource cannot be empty\n");
313                 return NULL;
314         }
315
316         return endpoint_internal_create(tech, resource);
317 }
318
319 static struct stasis_message *create_endpoint_snapshot_message(struct ast_endpoint *endpoint)
320 {
321         RAII_VAR(struct ast_endpoint_snapshot *, snapshot, NULL, ao2_cleanup);
322
323         if (!ast_endpoint_snapshot_type()) {
324                 return NULL;
325         }
326
327         snapshot = ast_endpoint_snapshot_create(endpoint);
328         if (!snapshot) {
329                 return NULL;
330         }
331
332         return stasis_message_create(ast_endpoint_snapshot_type(), snapshot);
333 }
334
335 void ast_endpoint_shutdown(struct ast_endpoint *endpoint)
336 {
337         RAII_VAR(struct stasis_message *, clear_msg, NULL, ao2_cleanup);
338
339         if (endpoint == NULL) {
340                 return;
341         }
342
343         ao2_unlink(endpoints, endpoint);
344         endpoint->tech_forward = stasis_forward_cancel(endpoint->tech_forward);
345
346         clear_msg = create_endpoint_snapshot_message(endpoint);
347         if (clear_msg) {
348                 RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
349                 message = stasis_cache_clear_create(clear_msg);
350                 if (message) {
351                         stasis_publish(ast_endpoint_topic(endpoint), message);
352                 }
353         }
354
355         /* Bump refcount to hold on to the router */
356         ao2_ref(endpoint->router, +1);
357         stasis_message_router_unsubscribe(endpoint->router);
358 }
359
360 const char *ast_endpoint_get_tech(const struct ast_endpoint *endpoint)
361 {
362         if (!endpoint) {
363                 return NULL;
364         }
365         return endpoint->tech;
366 }
367
368 const char *ast_endpoint_get_resource(const struct ast_endpoint *endpoint)
369 {
370         if (!endpoint) {
371                 return NULL;
372         }
373         return endpoint->resource;
374 }
375
376 const char *ast_endpoint_get_id(const struct ast_endpoint *endpoint)
377 {
378         if (!endpoint) {
379                 return NULL;
380         }
381         return endpoint->id;
382 }
383
384 enum ast_endpoint_state ast_endpoint_get_state(const struct ast_endpoint *endpoint)
385 {
386         if (!endpoint) {
387                 return AST_ENDPOINT_UNKNOWN;
388         }
389         return endpoint->state;
390 }
391
392 void ast_endpoint_set_state(struct ast_endpoint *endpoint,
393         enum ast_endpoint_state state)
394 {
395         ast_assert(endpoint != NULL);
396         ast_assert(!ast_strlen_zero(endpoint->resource));
397
398         ao2_lock(endpoint);
399         endpoint->state = state;
400         ao2_unlock(endpoint);
401         endpoint_publish_snapshot(endpoint);
402 }
403
404 void ast_endpoint_set_max_channels(struct ast_endpoint *endpoint,
405         int max_channels)
406 {
407         ast_assert(endpoint != NULL);
408         ast_assert(!ast_strlen_zero(endpoint->resource));
409
410         ao2_lock(endpoint);
411         endpoint->max_channels = max_channels;
412         ao2_unlock(endpoint);
413         endpoint_publish_snapshot(endpoint);
414 }
415
416 static void endpoint_snapshot_dtor(void *obj)
417 {
418         struct ast_endpoint_snapshot *snapshot = obj;
419         int channel;
420
421         ast_assert(snapshot != NULL);
422
423         for (channel = 0; channel < snapshot->num_channels; channel++) {
424                 ao2_ref(snapshot->channel_ids[channel], -1);
425         }
426
427         ast_string_field_free_memory(snapshot);
428 }
429
430 struct ast_endpoint_snapshot *ast_endpoint_snapshot_create(
431         struct ast_endpoint *endpoint)
432 {
433         struct ast_endpoint_snapshot *snapshot;
434         int channel_count;
435         struct ao2_iterator i;
436         void *obj;
437         SCOPED_AO2LOCK(lock, endpoint);
438
439         ast_assert(endpoint != NULL);
440         ast_assert(!ast_strlen_zero(endpoint->resource));
441
442         channel_count = ao2_container_count(endpoint->channel_ids);
443
444         snapshot = ao2_alloc_options(
445                 sizeof(*snapshot) + channel_count * sizeof(char *),
446                 endpoint_snapshot_dtor,
447                 AO2_ALLOC_OPT_LOCK_NOLOCK);
448
449         if (!snapshot || ast_string_field_init(snapshot, 80) != 0) {
450                 ao2_cleanup(snapshot);
451                 return NULL;
452         }
453
454         ast_string_field_build(snapshot, id, "%s/%s", endpoint->tech,
455                 endpoint->resource);
456         ast_string_field_set(snapshot, tech, endpoint->tech);
457         ast_string_field_set(snapshot, resource, endpoint->resource);
458
459         snapshot->state = endpoint->state;
460         snapshot->max_channels = endpoint->max_channels;
461
462         i = ao2_iterator_init(endpoint->channel_ids, 0);
463         while ((obj = ao2_iterator_next(&i))) {
464                 /* The reference is kept so the channel id does not go away until the snapshot is gone */
465                 snapshot->channel_ids[snapshot->num_channels++] = obj;
466         }
467         ao2_iterator_destroy(&i);
468
469         return snapshot;
470 }
471
472 static void endpoint_cleanup(void)
473 {
474         ao2_cleanup(endpoints);
475         endpoints = NULL;
476
477         ao2_cleanup(tech_endpoints);
478         tech_endpoints = NULL;
479 }
480
481 int ast_endpoint_init(void)
482 {
483         ast_register_cleanup(endpoint_cleanup);
484
485         endpoints = ao2_container_alloc(ENDPOINT_BUCKETS, ast_endpoint_hash_fn,
486                 ast_endpoint_cmp_fn);
487         if (!endpoints) {
488                 return -1;
489         }
490
491         tech_endpoints = ao2_container_alloc(TECH_ENDPOINT_BUCKETS, ast_endpoint_hash_fn,
492                 ast_endpoint_cmp_fn);
493         if (!tech_endpoints) {
494                 return -1;
495         }
496
497         return 0;
498 }