Debian Codegraph @

main/endpoints.c

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