endpoints: Fix failing unit tests from r419196
[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         stasis_cp_single_unsubscribe(endpoint->topics);
198         endpoint->topics = NULL;
199
200         ao2_cleanup(endpoint->channel_ids);
201         endpoint->channel_ids = NULL;
202
203         ast_string_field_free_memory(endpoint);
204 }
205
206
207 int ast_endpoint_add_channel(struct ast_endpoint *endpoint,
208         struct ast_channel *chan)
209 {
210         ast_assert(chan != NULL);
211         ast_assert(endpoint != NULL);
212         ast_assert(!ast_strlen_zero(endpoint->resource));
213
214         ast_channel_forward_endpoint(chan, endpoint);
215
216         ao2_lock(endpoint);
217         ast_str_container_add(endpoint->channel_ids, ast_channel_uniqueid(chan));
218         ao2_unlock(endpoint);
219
220         endpoint_publish_snapshot(endpoint);
221
222         return 0;
223 }
224
225 /*! \brief Handler for channel snapshot cache clears */
226 static void endpoint_cache_clear(void *data,
227         struct stasis_subscription *sub,
228         struct stasis_message *message)
229 {
230         struct ast_endpoint *endpoint = data;
231         struct stasis_message *clear_msg = stasis_message_data(message);
232         struct ast_channel_snapshot *clear_snapshot;
233
234         if (stasis_message_type(clear_msg) != ast_channel_snapshot_type()) {
235                 return;
236         }
237
238         clear_snapshot = stasis_message_data(clear_msg);
239
240         ast_assert(endpoint != NULL);
241
242         ao2_lock(endpoint);
243         ast_str_container_remove(endpoint->channel_ids, clear_snapshot->uniqueid);
244         ao2_unlock(endpoint);
245         endpoint_publish_snapshot(endpoint);
246 }
247
248 static void endpoint_default(void *data,
249         struct stasis_subscription *sub,
250         struct stasis_message *message)
251 {
252         struct stasis_endpoint *endpoint = data;
253
254         if (stasis_subscription_final_message(sub, message)) {
255                 ao2_cleanup(endpoint);
256         }
257 }
258
259 static struct ast_endpoint *endpoint_internal_create(const char *tech, const char *resource)
260 {
261         RAII_VAR(struct ast_endpoint *, endpoint, NULL, ao2_cleanup);
262         RAII_VAR(struct ast_endpoint *, tech_endpoint, NULL, ao2_cleanup);
263         int r = 0;
264
265         /* Get/create the technology endpoint */
266         if (!ast_strlen_zero(resource)) {
267                 tech_endpoint = ao2_find(tech_endpoints, tech, OBJ_KEY);
268                 if (!tech_endpoint) {
269                         tech_endpoint = endpoint_internal_create(tech, NULL);
270                         if (!tech_endpoint) {
271                                 return NULL;
272                         }
273                 }
274         }
275
276         endpoint = ao2_alloc(sizeof(*endpoint), endpoint_dtor);
277         if (!endpoint) {
278                 return NULL;
279         }
280
281         endpoint->max_channels = -1;
282         endpoint->state = AST_ENDPOINT_UNKNOWN;
283
284         if (ast_string_field_init(endpoint, 80) != 0) {
285                 return NULL;
286         }
287         ast_string_field_set(endpoint, tech, tech);
288         ast_string_field_set(endpoint, resource, S_OR(resource, ""));
289         ast_string_field_build(endpoint, id, "%s%s%s",
290                 tech,
291                 !ast_strlen_zero(resource) ? "/" : "",
292                 S_OR(resource, ""));
293
294         /* All access to channel_ids should be covered by the endpoint's
295          * lock; no extra lock needed. */
296         endpoint->channel_ids = ast_str_container_alloc_options(
297                 AO2_ALLOC_OPT_LOCK_NOLOCK, ENDPOINT_CHANNEL_BUCKETS);
298         if (!endpoint->channel_ids) {
299                 return NULL;
300         }
301
302         endpoint->topics = stasis_cp_single_create(ast_endpoint_cache_all(),
303                 endpoint->id);
304         if (!endpoint->topics) {
305                 return NULL;
306         }
307
308         if (!ast_strlen_zero(resource)) {
309                 endpoint->router = stasis_message_router_create(ast_endpoint_topic(endpoint));
310                 if (!endpoint->router) {
311                         return NULL;
312                 }
313                 r |= stasis_message_router_add(endpoint->router,
314                         stasis_cache_clear_type(), endpoint_cache_clear,
315                         endpoint);
316                 r |= stasis_message_router_set_default(endpoint->router,
317                         endpoint_default, endpoint);
318                 if (r) {
319                         return NULL;
320                 }
321
322                 endpoint->tech_forward = stasis_forward_all(stasis_cp_single_topic(endpoint->topics),
323                         stasis_cp_single_topic(tech_endpoint->topics));
324                 endpoint_publish_snapshot(endpoint);
325                 ao2_link(endpoints, endpoint);
326         } else {
327                 ao2_link(tech_endpoints, endpoint);
328         }
329
330         ao2_ref(endpoint, +1);
331         return endpoint;
332 }
333
334 struct ast_endpoint *ast_endpoint_create(const char *tech, const char *resource)
335 {
336         if (ast_strlen_zero(tech)) {
337                 ast_log(LOG_ERROR, "Endpoint tech cannot be empty\n");
338                 return NULL;
339         }
340
341         if (ast_strlen_zero(resource)) {
342                 ast_log(LOG_ERROR, "Endpoint resource cannot be empty\n");
343                 return NULL;
344         }
345
346         return endpoint_internal_create(tech, resource);
347 }
348
349 static struct stasis_message *create_endpoint_snapshot_message(struct ast_endpoint *endpoint)
350 {
351         RAII_VAR(struct ast_endpoint_snapshot *, snapshot, NULL, ao2_cleanup);
352         snapshot = ast_endpoint_snapshot_create(endpoint);
353         if (!snapshot) {
354                 return NULL;
355         }
356
357         return stasis_message_create(ast_endpoint_snapshot_type(), snapshot);
358 }
359
360 void ast_endpoint_shutdown(struct ast_endpoint *endpoint)
361 {
362         RAII_VAR(struct stasis_message *, clear_msg, NULL, ao2_cleanup);
363
364         if (endpoint == NULL) {
365                 return;
366         }
367
368         ao2_unlink(endpoints, endpoint);
369         endpoint->tech_forward = stasis_forward_cancel(endpoint->tech_forward);
370
371         clear_msg = create_endpoint_snapshot_message(endpoint);
372         if (clear_msg) {
373                 RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
374                 message = stasis_cache_clear_create(clear_msg);
375                 if (message) {
376                         stasis_publish(ast_endpoint_topic(endpoint), message);
377                 }
378         }
379
380         /* Bump refcount to hold on to the router */
381         ao2_ref(endpoint->router, +1);
382         stasis_message_router_unsubscribe(endpoint->router);
383 }
384
385 const char *ast_endpoint_get_tech(const struct ast_endpoint *endpoint)
386 {
387         if (!endpoint) {
388                 return NULL;
389         }
390         return endpoint->tech;
391 }
392
393 const char *ast_endpoint_get_resource(const struct ast_endpoint *endpoint)
394 {
395         if (!endpoint) {
396                 return NULL;
397         }
398         return endpoint->resource;
399 }
400
401 const char *ast_endpoint_get_id(const struct ast_endpoint *endpoint)
402 {
403         if (!endpoint) {
404                 return NULL;
405         }
406         return endpoint->id;
407 }
408
409 void ast_endpoint_set_state(struct ast_endpoint *endpoint,
410         enum ast_endpoint_state state)
411 {
412         ast_assert(endpoint != NULL);
413         ast_assert(!ast_strlen_zero(endpoint->resource));
414
415         ao2_lock(endpoint);
416         endpoint->state = state;
417         ao2_unlock(endpoint);
418         endpoint_publish_snapshot(endpoint);
419 }
420
421 void ast_endpoint_set_max_channels(struct ast_endpoint *endpoint,
422         int max_channels)
423 {
424         ast_assert(endpoint != NULL);
425         ast_assert(!ast_strlen_zero(endpoint->resource));
426
427         ao2_lock(endpoint);
428         endpoint->max_channels = max_channels;
429         ao2_unlock(endpoint);
430         endpoint_publish_snapshot(endpoint);
431 }
432
433 static void endpoint_snapshot_dtor(void *obj)
434 {
435         struct ast_endpoint_snapshot *snapshot = obj;
436         int channel;
437
438         ast_assert(snapshot != NULL);
439
440         for (channel = 0; channel < snapshot->num_channels; channel++) {
441                 ao2_ref(snapshot->channel_ids[channel], -1);
442         }
443
444         ast_string_field_free_memory(snapshot);
445 }
446
447 struct ast_endpoint_snapshot *ast_endpoint_snapshot_create(
448         struct ast_endpoint *endpoint)
449 {
450         RAII_VAR(struct ast_endpoint_snapshot *, snapshot, NULL, ao2_cleanup);
451         int channel_count;
452         struct ao2_iterator i;
453         void *obj;
454         SCOPED_AO2LOCK(lock, endpoint);
455
456         ast_assert(endpoint != NULL);
457         ast_assert(!ast_strlen_zero(endpoint->resource));
458
459         channel_count = ao2_container_count(endpoint->channel_ids);
460
461         snapshot = ao2_alloc(
462                 sizeof(*snapshot) + channel_count * sizeof(char *),
463                 endpoint_snapshot_dtor);
464
465         if (ast_string_field_init(snapshot, 80) != 0) {
466                 return NULL;
467         }
468
469         ast_string_field_build(snapshot, id, "%s/%s", endpoint->tech,
470                 endpoint->resource);
471         ast_string_field_set(snapshot, tech, endpoint->tech);
472         ast_string_field_set(snapshot, resource, endpoint->resource);
473
474         snapshot->state = endpoint->state;
475         snapshot->max_channels = endpoint->max_channels;
476
477         i = ao2_iterator_init(endpoint->channel_ids, 0);
478         while ((obj = ao2_iterator_next(&i))) {
479                 /* The reference is kept so the channel id does not go away until the snapshot is gone */
480                 snapshot->channel_ids[snapshot->num_channels++] = obj;
481         }
482         ao2_iterator_destroy(&i);
483
484         ao2_ref(snapshot, +1);
485         return snapshot;
486 }
487
488 static void endpoint_cleanup(void)
489 {
490         ao2_cleanup(endpoints);
491         endpoints = NULL;
492
493         ao2_cleanup(tech_endpoints);
494         tech_endpoints = NULL;
495 }
496
497 int ast_endpoint_init(void)
498 {
499         ast_register_cleanup(endpoint_cleanup);
500
501         endpoints = ao2_container_alloc(ENDPOINT_BUCKETS, endpoint_hash,
502                 endpoint_cmp);
503         if (!endpoints) {
504                 return -1;
505         }
506
507         tech_endpoints = ao2_container_alloc(TECH_ENDPOINT_BUCKETS, endpoint_hash,
508                 endpoint_cmp);
509         if (!tech_endpoints) {
510                 return -1;
511         }
512
513         return 0;
514 }