f1608f3a0018f511c58dca057f7c2737cc9f5ec3
[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_default(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
267                 endpoint->router = stasis_message_router_create_pool(ast_endpoint_topic(endpoint));
268                 if (!endpoint->router) {
269                         return NULL;
270                 }
271                 r |= stasis_message_router_add(endpoint->router,
272                         stasis_cache_clear_type(), endpoint_cache_clear,
273                         endpoint);
274                 r |= stasis_message_router_set_default(endpoint->router,
275                         endpoint_default, endpoint);
276                 if (r) {
277                         return NULL;
278                 }
279
280                 endpoint->tech_forward = stasis_forward_all(stasis_cp_single_topic(endpoint->topics),
281                         stasis_cp_single_topic(tech_endpoint->topics));
282
283                 endpoint_publish_snapshot(endpoint);
284                 ao2_link(endpoints, endpoint);
285         } else {
286                 endpoint->topics = stasis_cp_sink_create(ast_endpoint_cache_all(),
287                         endpoint->id);
288                 if (!endpoint->topics) {
289                         return NULL;
290                 }
291
292                 ao2_link(tech_endpoints, endpoint);
293         }
294
295         ao2_ref(endpoint, +1);
296         return endpoint;
297 }
298
299 struct ast_endpoint *ast_endpoint_create(const char *tech, const char *resource)
300 {
301         if (ast_strlen_zero(tech)) {
302                 ast_log(LOG_ERROR, "Endpoint tech cannot be empty\n");
303                 return NULL;
304         }
305
306         if (ast_strlen_zero(resource)) {
307                 ast_log(LOG_ERROR, "Endpoint resource cannot be empty\n");
308                 return NULL;
309         }
310
311         return endpoint_internal_create(tech, resource);
312 }
313
314 static struct stasis_message *create_endpoint_snapshot_message(struct ast_endpoint *endpoint)
315 {
316         RAII_VAR(struct ast_endpoint_snapshot *, snapshot, NULL, ao2_cleanup);
317
318         if (!ast_endpoint_snapshot_type()) {
319                 return NULL;
320         }
321
322         snapshot = ast_endpoint_snapshot_create(endpoint);
323         if (!snapshot) {
324                 return NULL;
325         }
326
327         return stasis_message_create(ast_endpoint_snapshot_type(), snapshot);
328 }
329
330 void ast_endpoint_shutdown(struct ast_endpoint *endpoint)
331 {
332         RAII_VAR(struct stasis_message *, clear_msg, NULL, ao2_cleanup);
333
334         if (endpoint == NULL) {
335                 return;
336         }
337
338         ao2_unlink(endpoints, endpoint);
339         endpoint->tech_forward = stasis_forward_cancel(endpoint->tech_forward);
340
341         clear_msg = create_endpoint_snapshot_message(endpoint);
342         if (clear_msg) {
343                 RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
344                 message = stasis_cache_clear_create(clear_msg);
345                 if (message) {
346                         stasis_publish(ast_endpoint_topic(endpoint), message);
347                 }
348         }
349
350         /* Bump refcount to hold on to the router */
351         ao2_ref(endpoint->router, +1);
352         stasis_message_router_unsubscribe(endpoint->router);
353 }
354
355 const char *ast_endpoint_get_tech(const struct ast_endpoint *endpoint)
356 {
357         if (!endpoint) {
358                 return NULL;
359         }
360         return endpoint->tech;
361 }
362
363 const char *ast_endpoint_get_resource(const struct ast_endpoint *endpoint)
364 {
365         if (!endpoint) {
366                 return NULL;
367         }
368         return endpoint->resource;
369 }
370
371 const char *ast_endpoint_get_id(const struct ast_endpoint *endpoint)
372 {
373         if (!endpoint) {
374                 return NULL;
375         }
376         return endpoint->id;
377 }
378
379 enum ast_endpoint_state ast_endpoint_get_state(const struct ast_endpoint *endpoint)
380 {
381         if (!endpoint) {
382                 return AST_ENDPOINT_UNKNOWN;
383         }
384         return endpoint->state;
385 }
386
387 void ast_endpoint_set_state(struct ast_endpoint *endpoint,
388         enum ast_endpoint_state state)
389 {
390         ast_assert(endpoint != NULL);
391         ast_assert(!ast_strlen_zero(endpoint->resource));
392
393         ao2_lock(endpoint);
394         endpoint->state = state;
395         ao2_unlock(endpoint);
396         endpoint_publish_snapshot(endpoint);
397 }
398
399 void ast_endpoint_set_max_channels(struct ast_endpoint *endpoint,
400         int max_channels)
401 {
402         ast_assert(endpoint != NULL);
403         ast_assert(!ast_strlen_zero(endpoint->resource));
404
405         ao2_lock(endpoint);
406         endpoint->max_channels = max_channels;
407         ao2_unlock(endpoint);
408         endpoint_publish_snapshot(endpoint);
409 }
410
411 static void endpoint_snapshot_dtor(void *obj)
412 {
413         struct ast_endpoint_snapshot *snapshot = obj;
414         int channel;
415
416         ast_assert(snapshot != NULL);
417
418         for (channel = 0; channel < snapshot->num_channels; channel++) {
419                 ao2_ref(snapshot->channel_ids[channel], -1);
420         }
421
422         ast_string_field_free_memory(snapshot);
423 }
424
425 struct ast_endpoint_snapshot *ast_endpoint_snapshot_create(
426         struct ast_endpoint *endpoint)
427 {
428         struct ast_endpoint_snapshot *snapshot;
429         int channel_count;
430         struct ao2_iterator i;
431         void *obj;
432         SCOPED_AO2LOCK(lock, endpoint);
433
434         ast_assert(endpoint != NULL);
435         ast_assert(!ast_strlen_zero(endpoint->resource));
436
437         channel_count = ao2_container_count(endpoint->channel_ids);
438
439         snapshot = ao2_alloc_options(
440                 sizeof(*snapshot) + channel_count * sizeof(char *),
441                 endpoint_snapshot_dtor,
442                 AO2_ALLOC_OPT_LOCK_NOLOCK);
443
444         if (!snapshot || ast_string_field_init(snapshot, 80) != 0) {
445                 ao2_cleanup(snapshot);
446                 return NULL;
447         }
448
449         ast_string_field_build(snapshot, id, "%s/%s", endpoint->tech,
450                 endpoint->resource);
451         ast_string_field_set(snapshot, tech, endpoint->tech);
452         ast_string_field_set(snapshot, resource, endpoint->resource);
453
454         snapshot->state = endpoint->state;
455         snapshot->max_channels = endpoint->max_channels;
456
457         i = ao2_iterator_init(endpoint->channel_ids, 0);
458         while ((obj = ao2_iterator_next(&i))) {
459                 /* The reference is kept so the channel id does not go away until the snapshot is gone */
460                 snapshot->channel_ids[snapshot->num_channels++] = obj;
461         }
462         ao2_iterator_destroy(&i);
463
464         return snapshot;
465 }
466
467 static void endpoint_cleanup(void)
468 {
469         ao2_cleanup(endpoints);
470         endpoints = NULL;
471
472         ao2_cleanup(tech_endpoints);
473         tech_endpoints = NULL;
474 }
475
476 int ast_endpoint_init(void)
477 {
478         ast_register_cleanup(endpoint_cleanup);
479
480         endpoints = ao2_container_alloc(ENDPOINT_BUCKETS, ast_endpoint_hash_fn,
481                 ast_endpoint_cmp_fn);
482         if (!endpoints) {
483                 return -1;
484         }
485
486         tech_endpoints = ao2_container_alloc(TECH_ENDPOINT_BUCKETS, ast_endpoint_hash_fn,
487                 ast_endpoint_cmp_fn);
488         if (!tech_endpoints) {
489                 return -1;
490         }
491
492         return 0;
493 }