Asterisk - The Open Source Telephony Project GIT-master-27fb039
Loading...
Searching...
No Matches
main/endpoints.c
Go to the documentation of this file.
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"
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
49static struct ao2_container *endpoints;
50
52
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 */
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 */
68 /*! Topic for this endpoint's messages */
70 /*! ast_str_container of channels associated with this endpoint */
72 /*! Forwarding subscription from an endpoint to its tech endpoint */
74};
75
78
80{
81 struct ast_endpoint *endpoint = ao2_find(endpoints, id, OBJ_KEY);
82
83 if (!endpoint) {
84 endpoint = ao2_find(tech_endpoints, id, OBJ_KEY);
85 }
86
87 return endpoint;
88}
89
91{
92 if (!endpoint) {
94 }
95 return stasis_cp_single_topic(endpoint->topics);
96}
97
99{
100 if (!endpoint) {
102 }
103 return stasis_cp_single_topic_cached(endpoint->topics);
104}
105
107{
108 switch (state) {
110 return "unknown";
112 return "offline";
114 return "online";
115 }
116 return "?";
117}
118
119static void endpoint_publish_snapshot(struct ast_endpoint *endpoint)
120{
121 RAII_VAR(struct ast_endpoint_snapshot *, snapshot, NULL, ao2_cleanup);
123
124 ast_assert(endpoint != NULL);
125 ast_assert(endpoint->topics != NULL);
126
128 return;
129 }
130
131 snapshot = ast_endpoint_snapshot_create(endpoint);
132 if (!snapshot) {
133 return;
134 }
136 if (!message) {
137 return;
138 }
140}
141
142static void endpoint_dtor(void *obj)
143{
144 struct ast_endpoint *endpoint = obj;
145
147 endpoint->topics = NULL;
148
149 ao2_cleanup(endpoint->channel_ids);
150 endpoint->channel_ids = NULL;
151
153}
154
155
157 struct ast_channel *chan)
158{
159 ast_assert(chan != NULL);
160 ast_assert(endpoint != NULL);
162
163 ast_channel_forward_endpoint(chan, endpoint);
164
165 ao2_lock(endpoint);
167 ao2_unlock(endpoint);
168
170
171 return 0;
172}
173
175 struct ast_channel *chan)
176{
177 ast_assert(chan != NULL);
178 ast_assert(endpoint != NULL);
180
181 ao2_lock(endpoint);
183 ao2_unlock(endpoint);
184
186
187 return 0;
188}
189
190static struct ast_endpoint *endpoint_internal_create(const char *tech, const char *resource)
191{
192 RAII_VAR(struct ast_endpoint *, endpoint, NULL, ao2_cleanup);
193 RAII_VAR(struct ast_endpoint *, tech_endpoint, NULL, ao2_cleanup);
194
195 /* Get/create the technology endpoint */
197 tech_endpoint = ao2_find(tech_endpoints, tech, OBJ_KEY);
198 if (!tech_endpoint) {
199 tech_endpoint = endpoint_internal_create(tech, NULL);
200 if (!tech_endpoint) {
201 return NULL;
202 }
203 }
204 }
205
206 endpoint = ao2_alloc(sizeof(*endpoint), endpoint_dtor);
207 if (!endpoint) {
208 return NULL;
209 }
210
211 endpoint->max_channels = -1;
212 endpoint->state = AST_ENDPOINT_UNKNOWN;
213
214 if (ast_string_field_init(endpoint, 80) != 0) {
215 return NULL;
216 }
217 ast_string_field_set(endpoint, tech, tech);
219 ast_string_field_build(endpoint, id, "%s%s%s",
220 tech,
221 !ast_strlen_zero(resource) ? "/" : "",
222 S_OR(resource, ""));
223
224 /* All access to channel_ids should be covered by the endpoint's
225 * lock; no extra lock needed. */
226 endpoint->channel_ids = ast_str_container_alloc_options(
228 if (!endpoint->channel_ids) {
229 return NULL;
230 }
231
233 char *topic_name;
234 int ret;
235
236 ret = ast_asprintf(&topic_name, "endpoint:%s", endpoint->id);
237 if (ret < 0) {
238 return NULL;
239 }
240
242 topic_name);
243 ast_free(topic_name);
244 if (!endpoint->topics) {
245 return NULL;
246 }
249
250 endpoint->tech_forward = stasis_forward_all(stasis_cp_single_topic(endpoint->topics),
251 stasis_cp_single_topic(tech_endpoint->topics));
252
254 ao2_link(endpoints, endpoint);
255 } else {
256 char *topic_name;
257 int ret;
258
259 ret = ast_asprintf(&topic_name, "endpoint:%s", endpoint->id);
260 if (ret < 0) {
261 return NULL;
262 }
263
265 topic_name);
266 ast_free(topic_name);
267 if (!endpoint->topics) {
268 return NULL;
269 }
272
273 ao2_link(tech_endpoints, endpoint);
274 }
275
276 ao2_ref(endpoint, +1);
277 return endpoint;
278}
279
280struct ast_endpoint *ast_endpoint_create(const char *tech, const char *resource)
281{
282 if (ast_strlen_zero(tech)) {
283 ast_log(LOG_ERROR, "Endpoint tech cannot be empty\n");
284 return NULL;
285 }
286
288 ast_log(LOG_ERROR, "Endpoint resource cannot be empty\n");
289 return NULL;
290 }
291
293}
294
296{
297 RAII_VAR(struct ast_endpoint_snapshot *, snapshot, NULL, ao2_cleanup);
298
300 return NULL;
301 }
302
303 snapshot = ast_endpoint_snapshot_create(endpoint);
304 if (!snapshot) {
305 return NULL;
306 }
307
309}
310
312{
313 RAII_VAR(struct stasis_message *, clear_msg, NULL, ao2_cleanup);
314
315 if (endpoint == NULL) {
316 return;
317 }
318
319 ao2_unlink(endpoints, endpoint);
320 endpoint->tech_forward = stasis_forward_cancel(endpoint->tech_forward);
321
322 clear_msg = create_endpoint_snapshot_message(endpoint);
323 if (clear_msg) {
326 if (message) {
328 }
329 }
330}
331
332const char *ast_endpoint_get_tech(const struct ast_endpoint *endpoint)
333{
334 if (!endpoint) {
335 return NULL;
336 }
337 return endpoint->tech;
338}
339
340const char *ast_endpoint_get_resource(const struct ast_endpoint *endpoint)
341{
342 if (!endpoint) {
343 return NULL;
344 }
345 return endpoint->resource;
346}
347
348const char *ast_endpoint_get_id(const struct ast_endpoint *endpoint)
349{
350 if (!endpoint) {
351 return NULL;
352 }
353 return endpoint->id;
354}
355
357{
358 if (!endpoint) {
360 }
361 return endpoint->state;
362}
363
366{
367 ast_assert(endpoint != NULL);
369
370 ao2_lock(endpoint);
371 endpoint->state = state;
372 ao2_unlock(endpoint);
374}
375
377 int max_channels)
378{
379 ast_assert(endpoint != NULL);
381
382 ao2_lock(endpoint);
383 endpoint->max_channels = max_channels;
384 ao2_unlock(endpoint);
386}
387
388static void endpoint_snapshot_dtor(void *obj)
389{
390 struct ast_endpoint_snapshot *snapshot = obj;
391 int channel;
392
393 ast_assert(snapshot != NULL);
394
395 for (channel = 0; channel < snapshot->num_channels; channel++) {
396 ao2_ref(snapshot->channel_ids[channel], -1);
397 }
398
400}
401
403 struct ast_endpoint *endpoint)
404{
405 struct ast_endpoint_snapshot *snapshot;
406 int channel_count;
407 struct ao2_iterator i;
408 void *obj;
409 SCOPED_AO2LOCK(lock, endpoint);
410
411 ast_assert(endpoint != NULL);
413
414 channel_count = ao2_container_count(endpoint->channel_ids);
415
416 snapshot = ao2_alloc_options(
417 sizeof(*snapshot) + channel_count * sizeof(char *),
420
421 if (!snapshot || ast_string_field_init(snapshot, 80) != 0) {
422 ao2_cleanup(snapshot);
423 return NULL;
424 }
425
426 ast_string_field_build(snapshot, id, "%s/%s", endpoint->tech,
427 endpoint->resource);
428 ast_string_field_set(snapshot, tech, endpoint->tech);
429 ast_string_field_set(snapshot, resource, endpoint->resource);
430
431 snapshot->state = endpoint->state;
432 snapshot->max_channels = endpoint->max_channels;
433
434 i = ao2_iterator_init(endpoint->channel_ids, 0);
435 while ((obj = ao2_iterator_next(&i))) {
436 /* The reference is kept so the channel id does not go away until the snapshot is gone */
437 snapshot->channel_ids[snapshot->num_channels++] = obj;
438 }
440
441 return snapshot;
442}
443
444static void endpoint_cleanup(void)
445{
447 endpoints = NULL;
448
451}
452
454{
456
458 ast_endpoint_hash_fn, NULL, ast_endpoint_cmp_fn);
459 if (!endpoints) {
460 return -1;
461 }
462
464 TECH_ENDPOINT_BUCKETS, ast_endpoint_hash_fn, NULL, ast_endpoint_cmp_fn);
465 if (!tech_endpoints) {
466 return -1;
467 }
468
469 return 0;
470}
Prototypes for public functions only of internal interest,.
ast_mutex_t lock
Definition app_sla.c:337
Asterisk main include file. File version handling, generic pbx functions.
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition clicompat.c:19
#define ast_free(a)
Definition astmm.h:180
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
Definition astmm.h:267
#define ast_log
Definition astobj2.c:42
#define ao2_iterator_next(iter)
Definition astobj2.h:1911
#define ao2_link(container, obj)
Add an object to a container.
Definition astobj2.h:1532
#define OBJ_KEY
Definition astobj2.h:1151
@ AO2_ALLOC_OPT_LOCK_NOLOCK
Definition astobj2.h:367
@ AO2_ALLOC_OPT_LOCK_MUTEX
Definition astobj2.h:363
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
#define AO2_STRING_FIELD_CMP_FN(stype, field)
Creates a compare function for a structure string field.
Definition astobj2.h:2048
#define ao2_cleanup(obj)
Definition astobj2.h:1934
#define ao2_unlink(container, obj)
Remove an object from a container.
Definition astobj2.h:1578
#define ao2_find(container, arg, flags)
Definition astobj2.h:1736
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
#define ao2_unlock(a)
Definition astobj2.h:729
#define ao2_lock(a)
Definition astobj2.h:717
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition astobj2.h:459
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition astobj2.h:404
#define AO2_STRING_FIELD_HASH_FN(stype, field)
Creates a hash function for a structure string field.
Definition astobj2.h:2032
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
#define ao2_alloc(data_size, destructor_fn)
Definition astobj2.h:409
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Allocate and initialize a hash container with the desired number of buckets.
Definition astobj2.h:1303
const char * ast_channel_uniqueid(const struct ast_channel *chan)
int ast_channel_forward_endpoint(struct ast_channel *chan, struct ast_endpoint *endpoint)
Forward channel stasis messages to the given endpoint.
Endpoint abstractions.
ast_endpoint_state
Valid states for an endpoint.
Definition endpoints.h:51
@ AST_ENDPOINT_OFFLINE
Definition endpoints.h:55
@ AST_ENDPOINT_ONLINE
Definition endpoints.h:57
@ AST_ENDPOINT_UNKNOWN
Definition endpoints.h:53
struct stasis_topic * ast_endpoint_topic_cached(struct ast_endpoint *endpoint)
Returns the topic for a specific endpoint.
struct stasis_topic * ast_endpoint_topic_all(void)
Topic for all endpoint related messages.
struct stasis_message_type * ast_endpoint_snapshot_type(void)
Message type for ast_endpoint_snapshot.
struct stasis_topic * ast_endpoint_topic(struct ast_endpoint *endpoint)
Returns the topic for a specific endpoint.
struct stasis_topic * ast_endpoint_topic_all_cached(void)
Cached topic for all endpoint related messages.
struct stasis_cp_all * ast_endpoint_cache_all(void)
struct ast_endpoint_snapshot * ast_endpoint_snapshot_create(struct ast_endpoint *endpoint)
Create a snapshot of an endpoint.
#define LOG_ERROR
#define SCOPED_AO2LOCK(varname, obj)
scoped lock specialization for ao2 mutexes.
Definition lock.h:611
void ast_endpoint_set_state(struct ast_endpoint *endpoint, enum ast_endpoint_state state)
Updates the state of the given endpoint.
static struct ao2_container * tech_endpoints
static struct ao2_container * endpoints
#define ENDPOINT_CHANNEL_BUCKETS
struct ast_endpoint * ast_endpoint_find_by_id(const char *id)
Finds the endpoint with the given tech[/resource] id.
static struct stasis_message * create_endpoint_snapshot_message(struct ast_endpoint *endpoint)
#define ENDPOINT_BUCKETS
static void endpoint_dtor(void *obj)
const char * ast_endpoint_get_tech(const struct ast_endpoint *endpoint)
Gets the technology of the given endpoint.
void ast_endpoint_shutdown(struct ast_endpoint *endpoint)
Shutsdown an ast_endpoint.
static struct ast_endpoint * endpoint_internal_create(const char *tech, const char *resource)
static void endpoint_publish_snapshot(struct ast_endpoint *endpoint)
void ast_endpoint_set_max_channels(struct ast_endpoint *endpoint, int max_channels)
Updates the maximum number of channels an endpoint supports.
int ast_endpoint_add_channel(struct ast_endpoint *endpoint, struct ast_channel *chan)
Adds a channel to the given endpoint.
enum ast_endpoint_state ast_endpoint_get_state(const struct ast_endpoint *endpoint)
Gets the state of the given endpoint.
static void endpoint_cleanup(void)
int ast_endpoint_init(void)
Endpoint support initialization.
const char * ast_endpoint_get_resource(const struct ast_endpoint *endpoint)
Gets the resource name of the given endpoint.
#define TECH_ENDPOINT_BUCKETS
const char * ast_endpoint_state_to_string(enum ast_endpoint_state state)
Returns a string representation of the given endpoint state.
struct ast_endpoint * ast_endpoint_create(const char *tech, const char *resource)
Create an endpoint struct.
const char * ast_endpoint_get_id(const struct ast_endpoint *endpoint)
Gets the tech/resource id of the given endpoint.
static void endpoint_snapshot_dtor(void *obj)
int ast_endpoint_remove_channel(struct ast_endpoint *endpoint, struct ast_channel *chan)
Removes a channel from the given endpoint.
#define NULL
Definition resample.c:96
Stasis Message Bus API. See Stasis Message Bus API for detailed documentation.
struct stasis_message * stasis_cache_clear_create(struct stasis_message *message)
A message which instructs the caching topic to remove an entry from its cache.
@ STASIS_SUBSCRIPTION_FILTER_SELECTIVE
Definition stasis.h:297
struct stasis_forward * stasis_forward_cancel(struct stasis_forward *forward)
Definition stasis.c:1615
struct stasis_message * stasis_message_create(struct stasis_message_type *type, void *data)
Create a new message.
struct stasis_forward * stasis_forward_all(struct stasis_topic *from_topic, struct stasis_topic *to_topic)
Create a subscription which forwards all messages from one topic to another.
Definition stasis.c:1645
void stasis_publish(struct stasis_topic *topic, struct stasis_message *message)
Publish a message to a topic's subscribers.
Definition stasis.c:1578
struct stasis_topic * stasis_cp_single_topic(struct stasis_cp_single *one)
Get the topic for this instance.
struct stasis_topic * stasis_cp_single_topic_cached(struct stasis_cp_single *one)
Get the caching topic for this instance.
int stasis_cp_single_set_filter(struct stasis_cp_single *one, enum stasis_subscription_message_filter filter)
Set the message type filtering level on a cache.
struct stasis_cp_single * stasis_cp_sink_create(struct stasis_cp_all *all, const char *name)
Create a sink in the cache pattern.
struct stasis_cp_single * stasis_cp_single_create(struct stasis_cp_all *all, const char *name)
Create the 'one' side of the cache pattern.
int stasis_cp_single_accept_message_type(struct stasis_cp_single *one, struct stasis_message_type *type)
Indicate to an instance that we are interested in a message type.
void stasis_cp_single_unsubscribe(struct stasis_cp_single *one)
Stops caching and forwarding messages.
Endpoint abstractions.
#define AST_DECLARE_STRING_FIELDS(field_list)
Declare the fields needed in a structure.
#define AST_STRING_FIELD(name)
Declare a string field.
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
#define ast_string_field_build(x, field, fmt, args...)
Set a field to a complex (built) value.
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
Definition strings.h:80
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition strings.h:65
void ast_str_container_remove(struct ao2_container *str_container, const char *remove)
Removes a string from a string container allocated by ast_str_container_alloc.
Definition strings.c:221
struct ao2_container * ast_str_container_alloc_options(enum ao2_alloc_opts opts, int buckets)
Allocates a hash container for bare strings.
Definition strings.c:200
int ast_str_container_add(struct ao2_container *str_container, const char *add)
Adds a string to a string container allocated by ast_str_container_alloc.
Definition strings.c:205
Generic container type.
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition astobj2.h:1821
Main Channel structure associated with a channel.
A snapshot of an endpoint's state.
enum ast_endpoint_state state
struct ao2_container * channel_ids
struct stasis_cp_single * topics
const ast_string_field tech
enum ast_endpoint_state state
struct stasis_forward * tech_forward
const ast_string_field id
int max_channels
Max channels for this endpoint. -1 means unlimited or unknown.
const ast_string_field resource
Forwarding information.
Definition stasis.c:1598
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition utils.h:981
#define ast_assert(a)
Definition utils.h:779