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