Debian Codegraph @

main/channel_internal_api.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2012, Digium, Inc.
5 *
6 * Mark Spencer <[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 Channel Accessor API
22 *
23 * This file is intended to be the only file that ever accesses the
24 * internals of an ast_channel. All other files should use the
25 * accessor functions defined here.
26 *
27 * \author Terry Wilson
28 */
29
30 /*** MODULEINFO
31 <support_level>core</support_level>
32 ***/
33
34 #include "asterisk.h"
35
36 #include <unistd.h>
37 #include <fcntl.h>
38
39 #include "asterisk/alertpipe.h"
40 #include "asterisk/paths.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/channel_internal.h"
43 #include "asterisk/endpoints.h"
44 #include "asterisk/indications.h"
45 #include "asterisk/stasis_channels.h"
46 #include "asterisk/stasis_endpoints.h"
47 #include "asterisk/stringfields.h"
48 #include "asterisk/stream.h"
49 #include "asterisk/test.h"
50 #include "asterisk/vector.h"
51 #include "channel_private.h"
52 #include "channelstorage.h"
53
54 /*! \brief The current channel storage driver */
55 const struct ast_channelstorage_driver *current_channel_storage_driver;
56 /*! \brief The current channel storage instance */
57 struct ast_channelstorage_instance *current_channel_storage_instance;
58
59 /*! \brief The monotonically increasing integer counter for channel uniqueids */
60 static int uniqueint;
61
62 /* ACCESSORS */
63
64 #define DEFINE_STRINGFIELD_SETTERS_FOR(field, assert_on_null) \
65 void ast_channel_##field##_set(struct ast_channel *chan, const char *value) \
66 { \
67 if ((assert_on_null)) ast_assert(!ast_strlen_zero(value)); \
68 if (!strcmp(value, chan->field)) return; \
69 ast_string_field_set(chan, field, value); \
70 } \
71 \
72 void ast_channel_##field##_build_va(struct ast_channel *chan, const char *fmt, va_list ap) \
73 { \
74 ast_string_field_build_va(chan, field, fmt, ap); \
75 } \
76 void ast_channel_##field##_build(struct ast_channel *chan, const char *fmt, ...) \
77 { \
78 va_list ap; \
79 va_start(ap, fmt); \
80 ast_channel_##field##_build_va(chan, fmt, ap); \
81 va_end(ap); \
82 }
83
84 #define DEFINE_STRINGFIELD_SETTERS_AND_INVALIDATE_FOR(field, publish, assert_on_null, invalidate) \
85 void ast_channel_##field##_set(struct ast_channel *chan, const char *value) \
86 { \
87 if ((assert_on_null)) ast_assert(!ast_strlen_zero(value)); \
88 if (!strcmp(value, chan->field)) return; \
89 ast_string_field_set(chan, field, value); \
90 ast_channel_snapshot_invalidate_segment(chan, invalidate); \
91 if (publish && ast_channel_internal_is_finalized(chan)) ast_channel_publish_snapshot(chan); \
92 } \
93 \
94 void ast_channel_##field##_build_va(struct ast_channel *chan, const char *fmt, va_list ap) \
95 { \
96 ast_string_field_build_va(chan, field, fmt, ap); \
97 ast_channel_snapshot_invalidate_segment(chan, invalidate); \
98 if (publish && ast_channel_internal_is_finalized(chan)) ast_channel_publish_snapshot(chan); \
99 } \
100 void ast_channel_##field##_build(struct ast_channel *chan, const char *fmt, ...) \
101 { \
102 va_list ap; \
103 va_start(ap, fmt); \
104 ast_channel_##field##_build_va(chan, fmt, ap); \
105 va_end(ap); \
106 }
107
108 DEFINE_STRINGFIELD_SETTERS_AND_INVALIDATE_FOR(language, 1, 0, AST_CHANNEL_SNAPSHOT_INVALIDATE_BASE);
109 DEFINE_STRINGFIELD_SETTERS_FOR(musicclass, 0);
110 DEFINE_STRINGFIELD_SETTERS_FOR(latest_musicclass, 0);
111 DEFINE_STRINGFIELD_SETTERS_AND_INVALIDATE_FOR(accountcode, 1, 0, AST_CHANNEL_SNAPSHOT_INVALIDATE_BASE);
112 DEFINE_STRINGFIELD_SETTERS_AND_INVALIDATE_FOR(peeraccount, 1, 0, AST_CHANNEL_SNAPSHOT_INVALIDATE_PEER);
113 DEFINE_STRINGFIELD_SETTERS_AND_INVALIDATE_FOR(userfield, 0, 0, AST_CHANNEL_SNAPSHOT_INVALIDATE_BASE);
114 DEFINE_STRINGFIELD_SETTERS_FOR(call_forward, 0);
115 DEFINE_STRINGFIELD_SETTERS_FOR(parkinglot, 0);
116 DEFINE_STRINGFIELD_SETTERS_AND_INVALIDATE_FOR(hangupsource, 0, 0, AST_CHANNEL_SNAPSHOT_INVALIDATE_HANGUP);
117 DEFINE_STRINGFIELD_SETTERS_FOR(dialcontext, 0);
118
119 #define DEFINE_STRINGFIELD_GETTER_FOR(field) const char *ast_channel_##field(const struct ast_channel *chan) \
120 { \
121 return chan->field; \
122 }
123
124 DEFINE_STRINGFIELD_GETTER_FOR(name);
125 DEFINE_STRINGFIELD_GETTER_FOR(language);
126 DEFINE_STRINGFIELD_GETTER_FOR(musicclass);
127 DEFINE_STRINGFIELD_GETTER_FOR(latest_musicclass);
128 DEFINE_STRINGFIELD_GETTER_FOR(accountcode);
129 DEFINE_STRINGFIELD_GETTER_FOR(peeraccount);
130 DEFINE_STRINGFIELD_GETTER_FOR(userfield);
131 DEFINE_STRINGFIELD_GETTER_FOR(call_forward);
132 DEFINE_STRINGFIELD_GETTER_FOR(parkinglot);
133 DEFINE_STRINGFIELD_GETTER_FOR(hangupsource);
134 DEFINE_STRINGFIELD_GETTER_FOR(dialcontext);
135
136 void ast_channel_name_set(struct ast_channel *chan, const char *value)
137 {
138 ast_assert(!ast_strlen_zero(value));
139 ast_assert(!chan->linked_in_container);
140 if (!strcmp(value, chan->name)) return;
141 ast_string_field_set(chan, name, value);
142 ast_channel_snapshot_invalidate_segment(chan, AST_CHANNEL_SNAPSHOT_INVALIDATE_BASE);
143 }
144
145 void ast_channel_name_build_va(struct ast_channel *chan, const char *fmt, va_list ap)
146 {
147 ast_assert(!chan->linked_in_container);
148 ast_string_field_build_va(chan, name, fmt, ap);
149 ast_channel_snapshot_invalidate_segment(chan, AST_CHANNEL_SNAPSHOT_INVALIDATE_BASE); \
150 }
151
152 void ast_channel_name_build(struct ast_channel *chan, const char *fmt, ...)
153 {
154 va_list ap;
155 ast_assert(!chan->linked_in_container);
156 va_start(ap, fmt);
157 ast_channel_name_build_va(chan, fmt, ap);
158 va_end(ap);
159 }
160
161 const char *ast_channel_uniqueid(const struct ast_channel *chan)
162 {
163 ast_assert(chan->uniqueid.unique_id[0] != '\0');
164 return chan->uniqueid.unique_id;
165 }
166
167 const char *ast_channel_linkedid(const struct ast_channel *chan)
168 {
169 ast_assert(chan->linkedid.unique_id[0] != '\0');
170 return chan->linkedid.unique_id;
171 }
172
173 const char *ast_channel_tenantid(const struct ast_channel *chan)
174 {
175 /* It's ok for tenantid to be empty, so no need to assert */
176 return chan->linkedid.tenant_id;
177 }
178
179 void ast_channel_tenantid_set(struct ast_channel *chan, const char *value)
180 {
181 if (ast_strlen_zero(value)) {
182 return;
183 }
184 ast_copy_string(chan->linkedid.tenant_id, value, sizeof(chan->linkedid.tenant_id));
185 ast_channel_snapshot_invalidate_segment(chan, AST_CHANNEL_SNAPSHOT_INVALIDATE_BASE);
186 }
187
188 const char *ast_channel_appl(const struct ast_channel *chan)
189 {
190 return chan->appl;
191 }
192 void ast_channel_appl_set(struct ast_channel *chan, const char *value)
193 {
194 chan->appl = value;
195 ast_channel_snapshot_invalidate_segment(chan, AST_CHANNEL_SNAPSHOT_INVALIDATE_DIALPLAN);
196 }
197 const char *ast_channel_blockproc(const struct ast_channel *chan)
198 {
199 return chan->blockproc;
200 }
201 void ast_channel_blockproc_set(struct ast_channel *chan, const char *value)
202 {
203 chan->blockproc = value;
204 }
205 const char *ast_channel_data(const struct ast_channel *chan)
206 {
207 return chan->data;
208 }
209 void ast_channel_data_set(struct ast_channel *chan, const char *value)
210 {
211 chan->data = value;
212 ast_channel_snapshot_invalidate_segment(chan, AST_CHANNEL_SNAPSHOT_INVALIDATE_DIALPLAN);
213 }
214
215 const char *ast_channel_context(const struct ast_channel *chan)
216 {
217 return chan->context;
218 }
219 const char *ast_channel_lastcontext(const struct ast_channel *chan)
220 {
221 return chan->lastcontext;
222 }
223 void ast_channel_context_set(struct ast_channel *chan, const char *value)
224 {
225 if (!*chan->lastcontext || strcmp(value, chan->context)) {
226 /* only copy to last context when it changes, unless it's empty to begin with */
227 ast_copy_string(chan->lastcontext, chan->context, sizeof(chan->lastcontext));
228 }
229 ast_copy_string(chan->context, value, sizeof(chan->context));
230 ast_channel_snapshot_invalidate_segment(chan, AST_CHANNEL_SNAPSHOT_INVALIDATE_DIALPLAN);
231 }
232 const char *ast_channel_exten(const struct ast_channel *chan)
233 {
234 return chan->exten;
235 }
236 const char *ast_channel_lastexten(const struct ast_channel *chan)
237 {
238 return chan->lastexten;
239 }
240 void ast_channel_exten_set(struct ast_channel *chan, const char *value)
241 {
242 if (!*chan->lastexten || strcmp(value, chan->exten)) {
243 /* only copy to last exten when it changes, unless it's empty to begin with */
244 ast_copy_string(chan->lastexten, chan->exten, sizeof(chan->lastexten));
245 }
246 ast_copy_string(chan->exten, value, sizeof(chan->exten));
247 ast_channel_snapshot_invalidate_segment(chan, AST_CHANNEL_SNAPSHOT_INVALIDATE_DIALPLAN);
248 }
249
250 char ast_channel_dtmf_digit_to_emulate(const struct ast_channel *chan)
251 {
252 return chan->dtmf_digit_to_emulate;
253 }
254 void ast_channel_dtmf_digit_to_emulate_set(struct ast_channel *chan, char value)
255 {
256 chan->dtmf_digit_to_emulate = value;
257 }
258
259 char ast_channel_sending_dtmf_digit(const struct ast_channel *chan)
260 {
261 return chan->sending_dtmf_digit;
262 }
263 void ast_channel_sending_dtmf_digit_set(struct ast_channel *chan, char value)
264 {
265 chan->sending_dtmf_digit = value;
266 }
267
268 struct timeval ast_channel_sending_dtmf_tv(const struct ast_channel *chan)
269 {
270 return chan->sending_dtmf_tv;
271 }
272 void ast_channel_sending_dtmf_tv_set(struct ast_channel *chan, struct timeval value)
273 {
274 chan->sending_dtmf_tv = value;
275 }
276
277 enum ama_flags ast_channel_amaflags(const struct ast_channel *chan)
278 {
279 return chan->amaflags;
280 }
281
282 void ast_channel_amaflags_set(struct ast_channel *chan, enum ama_flags value)
283 {
284 if (chan->amaflags == value) {
285 return;
286 }
287 chan->amaflags = value;
288 ast_channel_publish_snapshot(chan);
289 }
290 int ast_channel_fdno(const struct ast_channel *chan)
291 {
292 return chan->fdno;
293 }
294 void ast_channel_fdno_set(struct ast_channel *chan, int value)
295 {
296 chan->fdno = value;
297 }
298 int ast_channel_hangupcause(const struct ast_channel *chan)
299 {
300 return chan->hangupcause;
301 }
302 void ast_channel_hangupcause_set(struct ast_channel *chan, int value)
303 {
304 chan->hangupcause = value;
305 ast_channel_snapshot_invalidate_segment(chan, AST_CHANNEL_SNAPSHOT_INVALIDATE_HANGUP);
306 }
307 int ast_channel_tech_hangupcause(const struct ast_channel *chan)
308 {
309 return chan->tech_hangupcause;
310 }
311 void ast_channel_tech_hangupcause_set(struct ast_channel *chan, int value)
312 {
313 chan->tech_hangupcause = value;
314 ast_channel_snapshot_invalidate_segment(chan, AST_CHANNEL_SNAPSHOT_INVALIDATE_HANGUP);
315 }
316 int ast_channel_priority(const struct ast_channel *chan)
317 {
318 return chan->priority;
319 }
320 void ast_channel_priority_set(struct ast_channel *chan, int value)
321 {
322 chan->priority = value;
323 ast_channel_snapshot_invalidate_segment(chan, AST_CHANNEL_SNAPSHOT_INVALIDATE_DIALPLAN);
324 }
325 int ast_channel_rings(const struct ast_channel *chan)
326 {
327 return chan->rings;
328 }
329 void ast_channel_rings_set(struct ast_channel *chan, int value)
330 {
331 chan->rings = value;
332 }
333 int ast_channel_streamid(const struct ast_channel *chan)
334 {
335 return chan->streamid;
336 }
337 void ast_channel_streamid_set(struct ast_channel *chan, int value)
338 {
339 chan->streamid = value;
340 }
341 int ast_channel_timingfd(const struct ast_channel *chan)
342 {
343 return chan->timingfd;
344 }
345 void ast_channel_timingfd_set(struct ast_channel *chan, int value)
346 {
347 chan->timingfd = value;
348 }
349 int ast_channel_visible_indication(const struct ast_channel *chan)
350 {
351 return chan->visible_indication;
352 }
353 void ast_channel_visible_indication_set(struct ast_channel *chan, int value)
354 {
355 chan->visible_indication = value;
356 }
357 int ast_channel_hold_state(const struct ast_channel *chan)
358 {
359 return chan->hold_state;
360 }
361 void ast_channel_hold_state_set(struct ast_channel *chan, int value)
362 {
363 chan->hold_state = value;
364 }
365 int ast_channel_vstreamid(const struct ast_channel *chan)
366 {
367 return chan->vstreamid;
368 }
369 void ast_channel_vstreamid_set(struct ast_channel *chan, int value)
370 {
371 chan->vstreamid = value;
372 }
373 unsigned short ast_channel_transfercapability(const struct ast_channel *chan)
374 {
375 return chan->transfercapability;
376 }
377 void ast_channel_transfercapability_set(struct ast_channel *chan, unsigned short value)
378 {
379 chan->transfercapability = value;
380 }
381 unsigned int ast_channel_emulate_dtmf_duration(const struct ast_channel *chan)
382 {
383 return chan->emulate_dtmf_duration;
384 }
385 void ast_channel_emulate_dtmf_duration_set(struct ast_channel *chan, unsigned int value)
386 {
387 chan->emulate_dtmf_duration = value;
388 }
389 unsigned int ast_channel_fin(const struct ast_channel *chan)
390 {
391 return chan->fin;
392 }
393 void ast_channel_fin_set(struct ast_channel *chan, unsigned int value)
394 {
395 chan->fin = value;
396 }
397 unsigned int ast_channel_fout(const struct ast_channel *chan)
398 {
399 return chan->fout;
400 }
401 void ast_channel_fout_set(struct ast_channel *chan, unsigned int value)
402 {
403 chan->fout = value;
404 }
405 unsigned long ast_channel_insmpl(const struct ast_channel *chan)
406 {
407 return chan->insmpl;
408 }
409 void ast_channel_insmpl_set(struct ast_channel *chan, unsigned long value)
410 {
411 chan->insmpl = value;
412 }
413 unsigned long ast_channel_outsmpl(const struct ast_channel *chan)
414 {
415 return chan->outsmpl;
416 }
417 void ast_channel_outsmpl_set(struct ast_channel *chan, unsigned long value)
418 {
419 chan->outsmpl = value;
420 }
421 void *ast_channel_generatordata(const struct ast_channel *chan)
422 {
423 return chan->generatordata;
424 }
425 void ast_channel_generatordata_set(struct ast_channel *chan, void *value)
426 {
427 chan->generatordata = value;
428 }
429 void *ast_channel_music_state(const struct ast_channel *chan)
430 {
431 return chan->music_state;
432 }
433 void ast_channel_music_state_set(struct ast_channel *chan, void *value)
434 {
435 chan->music_state = value;
436 }
437 void *ast_channel_tech_pvt(const struct ast_channel *chan)
438 {
439 return chan->tech_pvt;
440 }
441 void ast_channel_tech_pvt_set(struct ast_channel *chan, void *value)
442 {
443 chan->tech_pvt = value;
444 if (value != NULL) {
445 ast_channel_snapshot_invalidate_segment(chan, AST_CHANNEL_SNAPSHOT_INVALIDATE_BASE);
446 }
447 }
448 void *ast_channel_timingdata(const struct ast_channel *chan)
449 {
450 return chan->timingdata;
451 }
452 void ast_channel_timingdata_set(struct ast_channel *chan, void *value)
453 {
454 chan->timingdata = value;
455 }
456 struct ast_audiohook_list *ast_channel_audiohooks(const struct ast_channel *chan)
457 {
458 return chan->audiohooks;
459 }
460 void ast_channel_audiohooks_set(struct ast_channel *chan, struct ast_audiohook_list *value)
461 {
462 chan->audiohooks = value;
463 }
464 struct ast_cdr *ast_channel_cdr(const struct ast_channel *chan)
465 {
466 return chan->cdr;
467 }
468 void ast_channel_cdr_set(struct ast_channel *chan, struct ast_cdr *value)
469 {
470 chan->cdr = value;
471 }
472 struct ast_channel *ast_channel_masq(const struct ast_channel *chan)
473 {
474 return chan->masq;
475 }
476 void ast_channel_masq_set(struct ast_channel *chan, struct ast_channel *value)
477 {
478 chan->masq = value;
479 }
480 struct ast_channel *ast_channel_masqr(const struct ast_channel *chan)
481 {
482 return chan->masqr;
483 }
484 void ast_channel_masqr_set(struct ast_channel *chan, struct ast_channel *value)
485 {
486 chan->masqr = value;
487 }
488 struct ast_filestream *ast_channel_stream(const struct ast_channel *chan)
489 {
490 return chan->stream;
491 }
492 void ast_channel_stream_set(struct ast_channel *chan, struct ast_filestream *value)
493 {
494 chan->stream = value;
495 }
496 struct ast_filestream *ast_channel_vstream(const struct ast_channel *chan)
497 {
498 return chan->vstream;
499 }
500 void ast_channel_vstream_set(struct ast_channel *chan, struct ast_filestream *value)
501 {
502 chan->vstream = value;
503 }
504 struct ast_format_cap *ast_channel_nativeformats(const struct ast_channel *chan)
505 {
506 return chan->nativeformats;
507 }
508
509 static void channel_set_default_streams(struct ast_channel *chan)
510 {
511 enum ast_media_type type;
512
513 ast_assert(chan != NULL);
514
515 for (type = AST_MEDIA_TYPE_UNKNOWN; type < AST_MEDIA_TYPE_END; type++) {
516 if (chan->stream_topology) {
517 chan->default_streams[type] =
518 ast_stream_topology_get_first_stream_by_type(chan->stream_topology, type);
519 } else {
520 chan->default_streams[type] = NULL;
521 }
522 }
523 }
524
525 void ast_channel_internal_set_stream_topology(struct ast_channel *chan,
526 struct ast_stream_topology *topology)
527 {
528 ast_stream_topology_free(chan->stream_topology);
529 chan->stream_topology = topology;
530 channel_set_default_streams(chan);
531 }
532
533 void ast_channel_internal_set_stream_topology_change_source(
534 struct ast_channel *chan, void *change_source)
535 {
536 chan->stream_topology_change_source = change_source;
537 }
538
539 void *ast_channel_get_stream_topology_change_source(struct ast_channel *chan)
540 {
541 return chan->stream_topology_change_source;
542 }
543
544 void ast_channel_nativeformats_set(struct ast_channel *chan,
545 struct ast_format_cap *value)
546 {
547 SCOPE_ENTER(2, "%s: %sFormats: %s\n", S_OR(ast_channel_name(chan), "<initializing>"),
548 S_COR(ast_channel_is_multistream(chan), "Multistream", ""),
549 ast_str_tmp(128, ast_format_cap_get_names(value, &STR_TMP)));
550
551 ast_assert(chan != NULL);
552
553 ao2_replace(chan->nativeformats, value);
554
555 /* If chan->stream_topology is NULL, the channel is being destroyed
556 * and topology is destroyed.
557 */
558 if (!chan->stream_topology) {
559 SCOPE_EXIT_RTN("Channel is being initialized or destroyed\n");
560 }
561
562 if (!ast_channel_is_multistream(chan) || !value) {
563 struct ast_stream_topology *new_topology;
564
565 new_topology = ast_stream_topology_create_from_format_cap(value);
566 ast_channel_internal_set_stream_topology(chan, new_topology);
567 SCOPE_EXIT_RTN("New %stopology set\n", value ? "" : "empty ");
568 }
569 SCOPE_EXIT_RTN("Set native formats but not topology\n");
570 }
571
572 struct ast_framehook_list *ast_channel_framehooks(const struct ast_channel *chan)
573 {
574 return chan->framehooks;
575 }
576 void ast_channel_framehooks_set(struct ast_channel *chan, struct ast_framehook_list *value)
577 {
578 chan->framehooks = value;
579 }
580 struct ast_generator *ast_channel_generator(const struct ast_channel *chan)
581 {
582 return chan->generator;
583 }
584 void ast_channel_generator_set(struct ast_channel *chan, struct ast_generator *value)
585 {
586 chan->generator = value;
587 }
588 struct ast_pbx *ast_channel_pbx(const struct ast_channel *chan)
589 {
590 return chan->pbx;
591 }
592 void ast_channel_pbx_set(struct ast_channel *chan, struct ast_pbx *value)
593 {
594 chan->pbx = value;
595 }
596 struct ast_sched_context *ast_channel_sched(const struct ast_channel *chan)
597 {
598 return chan->sched;
599 }
600 void ast_channel_sched_set(struct ast_channel *chan, struct ast_sched_context *value)
601 {
602 chan->sched = value;
603 }
604 struct ast_timer *ast_channel_timer(const struct ast_channel *chan)
605 {
606 return chan->timer;
607 }
608 void ast_channel_timer_set(struct ast_channel *chan, struct ast_timer *value)
609 {
610 chan->timer = value;
611 }
612 struct ast_tone_zone *ast_channel_zone(const struct ast_channel *chan)
613 {
614 return chan->zone;
615 }
616 void ast_channel_zone_set(struct ast_channel *chan, struct ast_tone_zone *value)
617 {
618 chan->zone = value;
619 }
620 struct ast_trans_pvt *ast_channel_readtrans(const struct ast_channel *chan)
621 {
622 return chan->readtrans;
623 }
624 void ast_channel_readtrans_set(struct ast_channel *chan, struct ast_trans_pvt *value)
625 {
626 chan->readtrans = value;
627 }
628 struct ast_trans_pvt *ast_channel_writetrans(const struct ast_channel *chan)
629 {
630 return chan->writetrans;
631 }
632 void ast_channel_writetrans_set(struct ast_channel *chan, struct ast_trans_pvt *value)
633 {
634 chan->writetrans = value;
635 }
636 const struct ast_channel_tech *ast_channel_tech(const struct ast_channel *chan)
637 {
638 return chan->tech;
639 }
640 void ast_channel_tech_set(struct ast_channel *chan, const struct ast_channel_tech *value)
641 {
642 if (value->read_stream || value->write_stream) {
643 ast_assert(value->read_stream && value->write_stream);
644 }
645
646 chan->tech = value;
647 }
648 enum ast_channel_adsicpe ast_channel_adsicpe(const struct ast_channel *chan)
649 {
650 return chan->adsicpe;
651 }
652 void ast_channel_adsicpe_set(struct ast_channel *chan, enum ast_channel_adsicpe value)
653 {
654 chan->adsicpe = value;
655 }
656 enum ast_channel_state ast_channel_state(const struct ast_channel *chan)
657 {
658 return chan->state;
659 }
660 ast_callid ast_channel_callid(const struct ast_channel *chan)
661 {
662 return chan->callid;
663 }
664 void ast_channel_callid_set(struct ast_channel *chan, ast_callid callid)
665 {
666 char call_identifier_from[AST_CALLID_BUFFER_LENGTH];
667 char call_identifier_to[AST_CALLID_BUFFER_LENGTH];
668 call_identifier_from[0] = '\0';
669 ast_callid_strnprint(call_identifier_to, sizeof(call_identifier_to), callid);
670 if (chan->callid) {
671 ast_callid_strnprint(call_identifier_from, sizeof(call_identifier_from), chan->callid);
672 ast_debug(3, "Channel Call ID changing from %s to %s\n", call_identifier_from, call_identifier_to);
673 }
674
675 chan->callid = callid;
676
677 ast_test_suite_event_notify("CallIDChange",
678 "State: CallIDChange\r\n"
679 "Channel: %s\r\n"
680 "CallID: %s\r\n"
681 "PriorCallID: %s",
682 ast_channel_name(chan),
683 call_identifier_to,
684 call_identifier_from);
685 }
686
687 void ast_channel_state_set(struct ast_channel *chan, enum ast_channel_state value)
688 {
689 chan->state = value;
690 }
691 void ast_channel_set_oldwriteformat(struct ast_channel *chan, struct ast_format *format)
692 {
693 ao2_replace(chan->oldwriteformat, format);
694 }
695 void ast_channel_set_rawreadformat(struct ast_channel *chan, struct ast_format *format)
696 {
697 ao2_replace(chan->rawreadformat, format);
698 }
699 void ast_channel_set_rawwriteformat(struct ast_channel *chan, struct ast_format *format)
700 {
701 ao2_replace(chan->rawwriteformat, format);
702 }
703 void ast_channel_set_readformat(struct ast_channel *chan, struct ast_format *format)
704 {
705 ao2_replace(chan->readformat, format);
706 }
707 void ast_channel_set_writeformat(struct ast_channel *chan, struct ast_format *format)
708 {
709 ao2_replace(chan->writeformat, format);
710 }
711 struct ast_format *ast_channel_oldwriteformat(struct ast_channel *chan)
712 {
713 return chan->oldwriteformat;
714 }
715 struct ast_format *ast_channel_rawreadformat(struct ast_channel *chan)
716 {
717 return chan->rawreadformat;
718 }
719 struct ast_format *ast_channel_rawwriteformat(struct ast_channel *chan)
720 {
721 return chan->rawwriteformat;
722 }
723 struct ast_format *ast_channel_readformat(struct ast_channel *chan)
724 {
725 return chan->readformat;
726 }
727 struct ast_format *ast_channel_writeformat(struct ast_channel *chan)
728 {
729 return chan->writeformat;
730 }
731 struct ast_hangup_handler_list *ast_channel_hangup_handlers(struct ast_channel *chan)
732 {
733 return &chan->hangup_handlers;
734 }
735 struct ast_datastore_list *ast_channel_datastores(struct ast_channel *chan)
736 {
737 return &chan->datastores;
738 }
739 struct ast_autochan_list *ast_channel_autochans(struct ast_channel *chan)
740 {
741 return &chan->autochans;
742 }
743 struct ast_readq_list *ast_channel_readq(struct ast_channel *chan)
744 {
745 return &chan->readq;
746 }
747 struct ast_frame *ast_channel_dtmff(struct ast_channel *chan)
748 {
749 return &chan->dtmff;
750 }
751 struct ast_jb *ast_channel_jb(struct ast_channel *chan)
752 {
753 return &chan->jb;
754 }
755 struct ast_party_caller *ast_channel_caller(struct ast_channel *chan)
756 {
757 return &chan->caller;
758 }
759 struct ast_party_connected_line *ast_channel_connected(struct ast_channel *chan)
760 {
761 return &chan->connected;
762 }
763 struct ast_party_connected_line *ast_channel_connected_indicated(struct ast_channel *chan)
764 {
765 return &chan->connected_indicated;
766 }
767 struct ast_party_id ast_channel_connected_effective_id(struct ast_channel *chan)
768 {
769 return ast_party_id_merge(&chan->connected.id, &chan->connected.priv);
770 }
771 struct ast_party_dialed *ast_channel_dialed(struct ast_channel *chan)
772 {
773 return &chan->dialed;
774 }
775 struct ast_party_redirecting *ast_channel_redirecting(struct ast_channel *chan)
776 {
777 return &chan->redirecting;
778 }
779 struct ast_party_id ast_channel_redirecting_effective_orig(struct ast_channel *chan)
780 {
781 return ast_party_id_merge(&chan->redirecting.orig, &chan->redirecting.priv_orig);
782 }
783 struct ast_party_id ast_channel_redirecting_effective_from(struct ast_channel *chan)
784 {
785 return ast_party_id_merge(&chan->redirecting.from, &chan->redirecting.priv_from);
786 }
787 struct ast_party_id ast_channel_redirecting_effective_to(struct ast_channel *chan)
788 {
789 return ast_party_id_merge(&chan->redirecting.to, &chan->redirecting.priv_to);
790 }
791 struct timeval *ast_channel_dtmf_tv(struct ast_channel *chan)
792 {
793 return &chan->dtmf_tv;
794 }
795 struct timeval *ast_channel_whentohangup(struct ast_channel *chan)
796 {
797 return &chan->whentohangup;
798 }
799 struct varshead *ast_channel_varshead(struct ast_channel *chan)
800 {
801 return &chan->varshead;
802 }
803 void ast_channel_dtmff_set(struct ast_channel *chan, struct ast_frame *value)
804 {
805 chan->dtmff = *value;
806 }
807 void ast_channel_jb_set(struct ast_channel *chan, struct ast_jb *value)
808 {
809 chan->jb = *value;
810 }
811 void ast_channel_caller_set(struct ast_channel *chan, struct ast_party_caller *value)
812 {
813 chan->caller = *value;
814 ast_channel_snapshot_invalidate_segment(chan, AST_CHANNEL_SNAPSHOT_INVALIDATE_CALLER);
815 }
816 void ast_channel_connected_set(struct ast_channel *chan, struct ast_party_connected_line *value)
817 {
818 chan->connected = *value;
819 ast_channel_snapshot_invalidate_segment(chan, AST_CHANNEL_SNAPSHOT_INVALIDATE_CONNECTED);
820 }
821 void ast_channel_dialed_set(struct ast_channel *chan, struct ast_party_dialed *value)
822 {
823 chan->dialed = *value;
824 ast_channel_snapshot_invalidate_segment(chan, AST_CHANNEL_SNAPSHOT_INVALIDATE_CALLER);
825 }
826 void ast_channel_redirecting_set(struct ast_channel *chan, struct ast_party_redirecting *value)
827 {
828 chan->redirecting = *value;
829 ast_channel_snapshot_invalidate_segment(chan, AST_CHANNEL_SNAPSHOT_INVALIDATE_CALLER);
830 }
831 void ast_channel_dtmf_tv_set(struct ast_channel *chan, struct timeval *value)
832 {
833 chan->dtmf_tv = *value;
834 }
835 void ast_channel_whentohangup_set(struct ast_channel *chan, struct timeval *value)
836 {
837 chan->whentohangup = *value;
838 }
839 void ast_channel_varshead_set(struct ast_channel *chan, struct varshead *value)
840 {
841 chan->varshead = *value;
842 }
843 struct timeval ast_channel_creationtime(struct ast_channel *chan)
844 {
845 return chan->creationtime;
846 }
847 void ast_channel_creationtime_set(struct ast_channel *chan, struct timeval *value)
848 {
849 chan->creationtime = *value;
850 ast_channel_snapshot_invalidate_segment(chan, AST_CHANNEL_SNAPSHOT_INVALIDATE_BASE);
851 }
852
853 struct timeval ast_channel_answertime(struct ast_channel *chan)
854 {
855 return chan->answertime;
856 }
857
858 void ast_channel_answertime_set(struct ast_channel *chan, struct timeval *value)
859 {
860 chan->answertime = *value;
861 }
862
863 /* Evil softhangup accessors */
864 int ast_channel_softhangup_internal_flag(struct ast_channel *chan)
865 {
866 return chan->softhangup;
867 }
868 void ast_channel_softhangup_internal_flag_set(struct ast_channel *chan, int value)
869 {
870 chan->softhangup = value;
871 }
872 void ast_channel_softhangup_internal_flag_add(struct ast_channel *chan, int value)
873 {
874 chan->softhangup |= value;
875 }
876 void ast_channel_softhangup_internal_flag_clear(struct ast_channel *chan, int value)
877 {
878 chan ->softhangup &= ~value;
879 }
880
881 int ast_channel_unbridged_nolock(struct ast_channel *chan)
882 {
883 return chan->unbridged;
884 }
885
886 int ast_channel_unbridged(struct ast_channel *chan)
887 {
888 int res;
889 ast_channel_lock(chan);
890 res = ast_channel_unbridged_nolock(chan);
891 ast_channel_unlock(chan);
892 return res;
893 }
894
895 void ast_channel_set_unbridged_nolock(struct ast_channel *chan, int value)
896 {
897 chan->unbridged = !!value;
898 ast_queue_frame(chan, &ast_null_frame);
899 }
900
901 void ast_channel_set_unbridged(struct ast_channel *chan, int value)
902 {
903 ast_channel_lock(chan);
904 ast_channel_set_unbridged_nolock(chan, value);
905 ast_channel_unlock(chan);
906 }
907
908 int ast_channel_is_t38_active_nolock(struct ast_channel *chan)
909 {
910 return chan->is_t38_active;
911 }
912
913 int ast_channel_is_t38_active(struct ast_channel *chan)
914 {
915 int res;
916
917 ast_channel_lock(chan);
918 res = ast_channel_is_t38_active_nolock(chan);
919 ast_channel_unlock(chan);
920 return res;
921 }
922
923 void ast_channel_set_is_t38_active_nolock(struct ast_channel *chan, int is_t38_active)
924 {
925 chan->is_t38_active = !!is_t38_active;
926 }
927
928 void ast_channel_set_is_t38_active(struct ast_channel *chan, int is_t38_active)
929 {
930 ast_channel_lock(chan);
931 ast_channel_set_is_t38_active_nolock(chan, is_t38_active);
932 ast_channel_unlock(chan);
933 }
934
935 void ast_channel_callid_cleanup(struct ast_channel *chan)
936 {
937 chan->callid = 0;
938 }
939
940 /* Typedef accessors */
941 ast_group_t ast_channel_callgroup(const struct ast_channel *chan)
942 {
943 return chan->callgroup;
944 }
945 void ast_channel_callgroup_set(struct ast_channel *chan, ast_group_t value)
946 {
947 chan->callgroup = value;
948 }
949 ast_group_t ast_channel_pickupgroup(const struct ast_channel *chan)
950 {
951 return chan->pickupgroup;
952 }
953 void ast_channel_pickupgroup_set(struct ast_channel *chan, ast_group_t value)
954 {
955 chan->pickupgroup = value;
956 }
957 struct ast_namedgroups *ast_channel_named_callgroups(const struct ast_channel *chan)
958 {
959 return chan->named_callgroups;
960 }
961 void ast_channel_named_callgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
962 {
963 ast_unref_namedgroups(chan->named_callgroups);
964 chan->named_callgroups = ast_ref_namedgroups(value);
965 }
966 struct ast_namedgroups *ast_channel_named_pickupgroups(const struct ast_channel *chan)
967 {
968 return chan->named_pickupgroups;
969 }
970 void ast_channel_named_pickupgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
971 {
972 ast_unref_namedgroups(chan->named_pickupgroups);
973 chan->named_pickupgroups = ast_ref_namedgroups(value);
974 }
975
976 /* Alertpipe functions */
977 int ast_channel_alert_write(struct ast_channel *chan)
978 {
979 return ast_alertpipe_write(chan->alertpipe);
980 }
981
982 ast_alert_status_t ast_channel_internal_alert_flush(struct ast_channel *chan)
983 {
984 return ast_alertpipe_flush(chan->alertpipe);
985 }
986
987 ast_alert_status_t ast_channel_internal_alert_read(struct ast_channel *chan)
988 {
989 return ast_alertpipe_read(chan->alertpipe);
990 }
991
992 int ast_channel_alert_writable(struct ast_channel *chan)
993 {
994 return ast_alertpipe_writable(chan->alertpipe);
995 }
996
997 int ast_channel_internal_alert_readable(struct ast_channel *chan)
998 {
999 return ast_alertpipe_readable(chan->alertpipe);
1000 }
1001
1002 void ast_channel_internal_alertpipe_clear(struct ast_channel *chan)
1003 {
1004 ast_alertpipe_clear(chan->alertpipe);
1005 }
1006
1007 void ast_channel_internal_alertpipe_close(struct ast_channel *chan)
1008 {
1009 ast_alertpipe_close(chan->alertpipe);
1010 }
1011
1012 int ast_channel_internal_alertpipe_init(struct ast_channel *chan)
1013 {
1014 return ast_alertpipe_init(chan->alertpipe);
1015 }
1016
1017 int ast_channel_internal_alert_readfd(struct ast_channel *chan)
1018 {
1019 return ast_alertpipe_readfd(chan->alertpipe);
1020 }
1021
1022 void ast_channel_internal_alertpipe_swap(struct ast_channel *chan1, struct ast_channel *chan2)
1023 {
1024 ast_alertpipe_swap(chan1->alertpipe, chan2->alertpipe);
1025 }
1026
1027 /* file descriptor array accessors */
1028 void ast_channel_internal_fd_set(struct ast_channel *chan, int which, int value)
1029 {
1030 int pos;
1031
1032 /* This ensures that if the vector has to grow with unused positions they will be
1033 * initialized to -1.
1034 */
1035 for (pos = AST_VECTOR_SIZE(&chan->fds); pos < which; pos++) {
1036 AST_VECTOR_REPLACE(&chan->fds, pos, -1);
1037 }
1038
1039 AST_VECTOR_REPLACE(&chan->fds, which, value);
1040 }
1041 void ast_channel_internal_fd_clear(struct ast_channel *chan, int which)
1042 {
1043 if (which >= AST_VECTOR_SIZE(&chan->fds)) {
1044 return;
1045 }
1046
1047 AST_VECTOR_REPLACE(&chan->fds, which, -1);
1048 }
1049 void ast_channel_internal_fd_clear_all(struct ast_channel *chan)
1050 {
1051 AST_VECTOR_RESET(&chan->fds, AST_VECTOR_ELEM_CLEANUP_NOOP);
1052 }
1053 int ast_channel_fd(const struct ast_channel *chan, int which)
1054 {
1055 return (which >= AST_VECTOR_SIZE(&chan->fds)) ? -1 : AST_VECTOR_GET(&chan->fds, which);
1056 }
1057 int ast_channel_fd_isset(const struct ast_channel *chan, int which)
1058 {
1059 return ast_channel_fd(chan, which) > -1;
1060 }
1061
1062 int ast_channel_fd_count(const struct ast_channel *chan)
1063 {
1064 return AST_VECTOR_SIZE(&chan->fds);
1065 }
1066
1067 size_t ast_channel_internal_ari_reportable_vars_count(const struct ast_channel *chan)
1068 {
1069 return AST_VECTOR_SIZE(&chan->ari_report_vars);
1070 }
1071
1072 char *ast_channel_internal_ari_reportable_vars_get(
1073 const struct ast_channel *chan, size_t index)
1074 {
1075 return index < AST_VECTOR_SIZE(&chan->ari_report_vars)
1076 ? AST_VECTOR_GET(&chan->ari_report_vars, index)
1077 : NULL;
1078 }
1079
1080 int ast_channel_internal_ari_reportable_vars_append(struct ast_channel *chan,
1081 char *key)
1082 {
1083 return AST_VECTOR_APPEND(&chan->ari_report_vars, key);
1084 }
1085
1086 char *ast_channel_internal_ari_reportable_vars_remove(
1087 struct ast_channel *chan, size_t index)
1088 {
1089 if (index >= AST_VECTOR_SIZE(&chan->ari_report_vars)) {
1090 return NULL;
1091 }
1092
1093 return AST_VECTOR_REMOVE(&chan->ari_report_vars, index, 0);
1094 }
1095
1096 int ast_channel_fd_add(struct ast_channel *chan, int value)
1097 {
1098 int pos = AST_EXTENDED_FDS;
1099
1100 while (ast_channel_fd_isset(chan, pos)) {
1101 pos += 1;
1102 }
1103
1104 AST_VECTOR_REPLACE(&chan->fds, pos, value);
1105
1106 return pos;
1107 }
1108
1109 pthread_t ast_channel_blocker(const struct ast_channel *chan)
1110 {
1111 return chan->blocker;
1112 }
1113 void ast_channel_blocker_set(struct ast_channel *chan, pthread_t value)
1114 {
1115 chan->blocker = value;
1116 }
1117
1118 int ast_channel_blocker_tid(const struct ast_channel *chan)
1119 {
1120 return chan->blocker_tid;
1121 }
1122 void ast_channel_blocker_tid_set(struct ast_channel *chan, int value)
1123 {
1124 chan->blocker_tid = value;
1125 }
1126
1127 ast_timing_func_t ast_channel_timingfunc(const struct ast_channel *chan)
1128 {
1129 return chan->timingfunc;
1130 }
1131 void ast_channel_timingfunc_set(struct ast_channel *chan, ast_timing_func_t value)
1132 {
1133 chan->timingfunc = value;
1134 }
1135
1136 struct ast_bridge *ast_channel_internal_bridge(const struct ast_channel *chan)
1137 {
1138 return chan->bridge;
1139 }
1140 void ast_channel_internal_bridge_set(struct ast_channel *chan, struct ast_bridge *value)
1141 {
1142 chan->bridge = value;
1143 ast_channel_snapshot_invalidate_segment(chan, AST_CHANNEL_SNAPSHOT_INVALIDATE_BRIDGE);
1144 ast_channel_publish_snapshot(chan);
1145 }
1146
1147 struct ast_bridge_channel *ast_channel_internal_bridge_channel(const struct ast_channel *chan)
1148 {
1149 return chan->bridge_channel;
1150 }
1151 void ast_channel_internal_bridge_channel_set(struct ast_channel *chan, struct ast_bridge_channel *value)
1152 {
1153 chan->bridge_channel = value;
1154 }
1155
1156 struct ast_flags *ast_channel_flags(struct ast_channel *chan)
1157 {
1158 return &chan->flags;
1159 }
1160
1161 static int collect_names_cb(void *obj, void *arg, int flags)
1162 {
1163 struct ast_control_pvt_cause_code *cause_code = obj;
1164 struct ast_str **str = arg;
1165
1166 ast_str_append(str, 0, "%s%s", (ast_str_strlen(*str) ? "," : ""), cause_code->chan_name);
1167
1168 return 0;
1169 }
1170
1171 struct ao2_iterator ast_channel_dialed_causes_iterator(const struct ast_channel *chan)
1172 {
1173 return ao2_iterator_init(chan->dialed_causes, 0);
1174 }
1175
1176 struct ast_str *ast_channel_dialed_causes_channels(const struct ast_channel *chan)
1177 {
1178 struct ast_str *chanlist = ast_str_create(128);
1179
1180 if (!chanlist) {
1181 return NULL;
1182 }
1183
1184 ao2_callback(chan->dialed_causes, 0, collect_names_cb, &chanlist);
1185
1186 return chanlist;
1187 }
1188
1189 struct ast_control_pvt_cause_code *ast_channel_dialed_causes_find(const struct ast_channel *chan, const char *chan_name)
1190 {
1191 return ao2_find(chan->dialed_causes, chan_name, OBJ_KEY);
1192 }
1193
1194 int ast_channel_dialed_causes_add(const struct ast_channel *chan, const struct ast_control_pvt_cause_code *cause_code, int datalen)
1195 {
1196 struct ast_control_pvt_cause_code *ao2_cause_code;
1197 ao2_find(chan->dialed_causes, cause_code->chan_name, OBJ_KEY | OBJ_UNLINK | OBJ_NODATA);
1198 ao2_cause_code = ao2_alloc(datalen, NULL);
1199
1200 if (ao2_cause_code) {
1201 memcpy(ao2_cause_code, cause_code, datalen);
1202 ao2_link(chan->dialed_causes, ao2_cause_code);
1203 ao2_ref(ao2_cause_code, -1);
1204 return 0;
1205 } else {
1206 return -1;
1207 }
1208 }
1209
1210 void ast_channel_dialed_causes_clear(const struct ast_channel *chan)
1211 {
1212 ao2_callback(chan->dialed_causes, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, NULL, NULL);
1213 }
1214
1215 /*! \brief Hash function for pvt cause code frames */
1216 static int pvt_cause_hash_fn(const void *vpc, const int flags)
1217 {
1218 const struct ast_control_pvt_cause_code *pc = vpc;
1219 return ast_str_hash(ast_tech_to_upper(ast_strdupa(pc->chan_name)));
1220 }
1221
1222 /*! \brief Comparison function for pvt cause code frames */
1223 static int pvt_cause_cmp_fn(void *obj, void *vstr, int flags)
1224 {
1225 struct ast_control_pvt_cause_code *pc = obj;
1226 char *str = ast_tech_to_upper(ast_strdupa(vstr));
1227 char *pc_str = ast_tech_to_upper(ast_strdupa(pc->chan_name));
1228 return !strcmp(pc_str, str) ? CMP_MATCH | CMP_STOP : 0;
1229 }
1230
1231 #define DIALED_CAUSES_BUCKETS 37
1232
1233 struct ast_channel *__ast_channel_internal_alloc_with_initializers(void (*destructor)(void *obj), const struct ast_assigned_ids *assignedids,
1234 const struct ast_channel *requestor, const struct ast_channel_initializers *initializers, const char *file, int line, const char *function)
1235 {
1236 struct ast_channel *tmp;
1237
1238 tmp = __ao2_alloc(sizeof(*tmp), destructor,
1239 AO2_ALLOC_OPT_LOCK_MUTEX, "", file, line, function);
1240
1241 if (!tmp) {
1242 return NULL;
1243 }
1244
1245 if ((ast_string_field_init(tmp, 128))) {
1246 return ast_channel_unref(tmp);
1247 }
1248
1249 tmp->dialed_causes = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
1250 DIALED_CAUSES_BUCKETS, pvt_cause_hash_fn, NULL, pvt_cause_cmp_fn);
1251 if (!tmp->dialed_causes) {
1252 return ast_channel_unref(tmp);
1253 }
1254
1255 /* Check initializers validity here for early abort. Unfortunately, we can't do much here because
1256 * tenant ID is part of linked ID, which would overwrite it further down. */
1257 if (initializers) {
1258 if (initializers->version == 0) {
1259 ast_log(LOG_ERROR, "Channel initializers must have a non-zero version.\n");
1260 return ast_channel_unref(tmp);
1261 } else if (initializers->version != AST_CHANNEL_INITIALIZERS_VERSION) {
1262 ast_log(LOG_ERROR, "ABI mismatch for ast_channel_initializers. "
1263 "Please ensure all modules were compiled for "
1264 "this version of Asterisk.\n");
1265 return ast_channel_unref(tmp);
1266 }
1267 }
1268
1269 /* set the creation time in the uniqueid */
1270 tmp->uniqueid.creation_time = time(NULL);
1271 tmp->uniqueid.creation_unique = ast_atomic_fetchadd_int(&uniqueint, 1);
1272
1273 /* use provided id or default to historical {system-}time.# format */
1274 if (assignedids && !ast_strlen_zero(assignedids->uniqueid)) {
1275 ast_copy_string(tmp->uniqueid.unique_id, assignedids->uniqueid, sizeof(tmp->uniqueid.unique_id));
1276 } else if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME)) {
1277 snprintf(tmp->uniqueid.unique_id, sizeof(tmp->uniqueid.unique_id), "%li.%d",
1278 (long)(tmp->uniqueid.creation_time),
1279 tmp->uniqueid.creation_unique);
1280 } else {
1281 snprintf(tmp->uniqueid.unique_id, sizeof(tmp->uniqueid.unique_id), "%s-%li.%d",
1282 ast_config_AST_SYSTEM_NAME,
1283 (long)(tmp->uniqueid.creation_time),
1284 tmp->uniqueid.creation_unique);
1285 }
1286
1287 /* copy linked id from parent channel if known */
1288 if (requestor) {
1289 tmp->linkedid = requestor->linkedid;
1290 } else {
1291 tmp->linkedid = tmp->uniqueid;
1292 }
1293
1294 /* Things like tenant ID need to be set here, otherwise they would be overwritten by
1295 * things like inheriting linked ID above. */
1296 if (initializers) {
1297 ast_copy_string(tmp->linkedid.tenant_id, initializers->tenantid, sizeof(tmp->linkedid.tenant_id));
1298 }
1299
1300 AST_VECTOR_INIT(&tmp->fds, AST_MAX_FDS);
1301 AST_VECTOR_INIT(&tmp->ari_report_vars, 8);
1302
1303 /* Force all channel snapshot segments to be created on first use, so we don't have to check if
1304 * an old snapshot exists.
1305 */
1306 ast_set_flag(&tmp->snapshot_segment_flags, AST_FLAGS_ALL);
1307
1308 return tmp;
1309 }
1310
1311 struct ast_channel *__ast_channel_internal_alloc(void (*destructor)(void *obj), const struct ast_assigned_ids *assignedids,
1312 const struct ast_channel *requestor, const char *file, int line, const char *function)
1313 {
1314 return __ast_channel_internal_alloc_with_initializers(destructor, assignedids, requestor, NULL, file, line, function);
1315 }
1316
1317 struct ast_channel *ast_channel_internal_oldest_linkedid(struct ast_channel *a, struct ast_channel *b)
1318 {
1319 ast_assert(a->linkedid.creation_time != 0);
1320 ast_assert(b->linkedid.creation_time != 0);
1321
1322 if (a->linkedid.creation_time < b->linkedid.creation_time) {
1323 return a;
1324 }
1325 if (b->linkedid.creation_time < a->linkedid.creation_time) {
1326 return b;
1327 }
1328 if (a->linkedid.creation_unique < b->linkedid.creation_unique) {
1329 return a;
1330 }
1331 return b;
1332 }
1333
1334 void ast_channel_internal_copy_linkedid(struct ast_channel *dest, struct ast_channel *source)
1335 {
1336 if (dest->linkedid.creation_time == source->linkedid.creation_time
1337 && dest->linkedid.creation_unique == source->linkedid.creation_unique
1338 && !strcmp(dest->linkedid.unique_id, source->linkedid.unique_id)) {
1339 return;
1340 }
1341 dest->linkedid = source->linkedid;
1342 ast_channel_snapshot_invalidate_segment(dest, AST_CHANNEL_SNAPSHOT_INVALIDATE_PEER);
1343 ast_channel_publish_snapshot(dest);
1344 }
1345
1346 void ast_channel_internal_swap_uniqueid_and_linkedid(struct ast_channel *a, struct ast_channel *b)
1347 {
1348 struct ast_channel_id temp;
1349
1350 /* This operation is used as part of masquerading and so does not invalidate the peer
1351 * segment. This is due to the masquerade process invalidating all segments.
1352 */
1353
1354 /*
1355 * Since unique ids can be a key in the channel storage backend,
1356 * ensure that neither channel is linked in or the keys will be
1357 * invalid.
1358 */
1359 ast_assert(!a->linked_in_container && !b->linked_in_container);
1360
1361 temp = a->uniqueid;
1362 a->uniqueid = b->uniqueid;
1363 b->uniqueid = temp;
1364
1365 temp = a->linkedid;
1366 a->linkedid = b->linkedid;
1367 b->linkedid = temp;
1368 }
1369
1370 void ast_channel_internal_swap_topics(struct ast_channel *a, struct ast_channel *b)
1371 {
1372 struct stasis_topic *topic;
1373 struct stasis_forward *forward;
1374
1375 topic = a->topic;
1376 a->topic = b->topic;
1377 b->topic = topic;
1378
1379 forward = a->channel_forward;
1380 a->channel_forward = b->channel_forward;
1381 b->channel_forward = forward;
1382 }
1383
1384 void ast_channel_internal_swap_endpoint_forward(struct ast_channel *a, struct ast_channel *b)
1385 {
1386 struct stasis_forward *temp;
1387
1388 temp = a->endpoint_forward;
1389 a->endpoint_forward = b->endpoint_forward;
1390 b->endpoint_forward = temp;
1391 }
1392
1393 void ast_channel_internal_swap_snapshots(struct ast_channel *a, struct ast_channel *b)
1394 {
1395 struct ast_channel_snapshot *snapshot;
1396
1397 snapshot = a->snapshot;
1398 a->snapshot = b->snapshot;
1399 b->snapshot = snapshot;
1400 }
1401
1402 void ast_channel_internal_swap_endpoints(struct ast_channel *a, struct ast_channel *b)
1403 {
1404 struct ast_endpoint *endpoint;
1405
1406 endpoint = a->endpoint;
1407 a->endpoint = b->endpoint;
1408 b->endpoint = endpoint;
1409 }
1410
1411 void ast_channel_internal_set_fake_ids(struct ast_channel *chan, const char *uniqueid, const char *linkedid)
1412 {
1413 ast_copy_string(chan->uniqueid.unique_id, uniqueid, sizeof(chan->uniqueid.unique_id));
1414 ast_copy_string(chan->linkedid.unique_id, linkedid, sizeof(chan->linkedid.unique_id));
1415 }
1416
1417 void ast_channel_internal_cleanup(struct ast_channel *chan)
1418 {
1419 if (chan->dialed_causes) {
1420 ao2_t_ref(chan->dialed_causes, -1,
1421 "done with dialed causes since the channel is going away");
1422 chan->dialed_causes = NULL;
1423 }
1424
1425 ast_string_field_free_memory(chan);
1426
1427 chan->channel_forward = stasis_forward_cancel(chan->channel_forward);
1428 chan->endpoint_forward = stasis_forward_cancel(chan->endpoint_forward);
1429
1430 ao2_cleanup(chan->topic);
1431 chan->topic = NULL;
1432
1433 ast_channel_internal_set_stream_topology(chan, NULL);
1434
1435 AST_VECTOR_RESET(&chan->ari_report_vars, ast_free);
1436 AST_VECTOR_FREE(&chan->ari_report_vars);
1437 AST_VECTOR_FREE(&chan->fds);
1438 }
1439
1440 void ast_channel_internal_finalize(struct ast_channel *chan)
1441 {
1442 chan->finalized = 1;
1443 }
1444
1445 int ast_channel_internal_is_finalized(struct ast_channel *chan)
1446 {
1447 return chan->finalized;
1448 }
1449
1450 struct stasis_topic *ast_channel_topic(struct ast_channel *chan)
1451 {
1452 if (!chan) {
1453 return ast_channel_topic_all();
1454 }
1455
1456 return chan->topic;
1457 }
1458
1459 int ast_channel_forward_endpoint(struct ast_channel *chan,
1460 struct ast_endpoint *endpoint)
1461 {
1462 ast_assert(chan != NULL);
1463 ast_assert(endpoint != NULL);
1464
1465 chan->endpoint_forward =
1466 stasis_forward_all(ast_channel_topic(chan),
1467 ast_endpoint_topic(endpoint));
1468 if (!chan->endpoint_forward) {
1469 return -1;
1470 }
1471
1472 return 0;
1473 }
1474
1475 int ast_channel_internal_setup_topics(struct ast_channel *chan)
1476 {
1477 char *topic_name;
1478 int ret;
1479 ast_assert(chan->topic == NULL);
1480
1481 if (ast_strlen_zero(chan->uniqueid.unique_id)) {
1482 static int dummy_id;
1483 ret = ast_asprintf(&topic_name, "channel:dummy-%d", ast_atomic_fetchadd_int(&dummy_id, +1));
1484 } else {
1485 ret = ast_asprintf(&topic_name, "channel:%s", chan->uniqueid.unique_id);
1486 }
1487
1488 if (ret < 0) {
1489 return -1;
1490 }
1491
1492 chan->topic = stasis_topic_create(topic_name);
1493 ast_free(topic_name);
1494 if (!chan->topic) {
1495 return -1;
1496 }
1497
1498 chan->channel_forward = stasis_forward_all(ast_channel_topic(chan),
1499 ast_channel_topic_all());
1500 if (!chan->channel_forward) {
1501 ao2_ref(chan->topic, -1);
1502 chan->topic = NULL;
1503 return -1;
1504 }
1505
1506 return 0;
1507 }
1508
1509 AST_THREADSTORAGE(channel_errno);
1510
1511 void ast_channel_internal_errno_set(enum ast_channel_error error)
1512 {
1513 enum ast_channel_error *error_code = ast_threadstorage_get(&channel_errno, sizeof(*error_code));
1514 if (!error_code) {
1515 return;
1516 }
1517
1518 *error_code = error;
1519 }
1520
1521 enum ast_channel_error ast_channel_internal_errno(void)
1522 {
1523 enum ast_channel_error *error_code = ast_threadstorage_get(&channel_errno, sizeof(*error_code));
1524 if (!error_code) {
1525 return AST_CHANNEL_ERROR_UNKNOWN;
1526 }
1527
1528 return *error_code;
1529 }
1530
1531 struct ast_stream_topology *ast_channel_get_stream_topology(
1532 const struct ast_channel *chan)
1533 {
1534 ast_assert(chan != NULL);
1535
1536 return chan->stream_topology;
1537 }
1538
1539 struct ast_stream_topology *ast_channel_set_stream_topology(struct ast_channel *chan,
1540 struct ast_stream_topology *topology)
1541 {
1542 struct ast_stream_topology *new_topology;
1543 SCOPE_ENTER(1, "%s: %s\n", ast_channel_name(chan),
1544 ast_str_tmp(256, ast_stream_topology_to_str(topology, &STR_TMP)));
1545
1546 ast_assert(chan != NULL);
1547
1548 /* A non-MULTISTREAM channel can't manipulate topology directly */
1549 ast_assert(ast_channel_is_multistream(chan));
1550
1551 /* Unless the channel is being destroyed, we always want a topology on
1552 * it even if its empty.
1553 */
1554 if (!topology) {
1555 new_topology = ast_stream_topology_alloc();
1556 } else {
1557 new_topology = topology;
1558 }
1559
1560 if (new_topology) {
1561 ast_channel_internal_set_stream_topology(chan, new_topology);
1562 }
1563
1564 SCOPE_EXIT_RTN_VALUE(new_topology, "Used %s topology\n", topology ? "provided" : "empty");
1565 }
1566
1567 struct ast_stream *ast_channel_get_default_stream(struct ast_channel *chan,
1568 enum ast_media_type type)
1569 {
1570 ast_assert(chan != NULL);
1571 ast_assert(type < AST_MEDIA_TYPE_END);
1572
1573 return chan->default_streams[type];
1574 }
1575
1576 void ast_channel_internal_swap_stream_topology(struct ast_channel *chan1,
1577 struct ast_channel *chan2)
1578 {
1579 struct ast_stream_topology *tmp_topology;
1580
1581 ast_assert(chan1 != NULL && chan2 != NULL);
1582
1583 tmp_topology = chan1->stream_topology;
1584 chan1->stream_topology = chan2->stream_topology;
1585 chan2->stream_topology = tmp_topology;
1586
1587 channel_set_default_streams(chan1);
1588 channel_set_default_streams(chan2);
1589 }
1590
1591 int ast_channel_is_multistream(struct ast_channel *chan)
1592 {
1593 return (chan && chan->tech && chan->tech->read_stream && chan->tech->write_stream);
1594 }
1595
1596 struct ast_channel_snapshot *ast_channel_snapshot(const struct ast_channel *chan)
1597 {
1598 return chan->snapshot;
1599 }
1600
1601 void ast_channel_snapshot_set(struct ast_channel *chan, struct ast_channel_snapshot *snapshot)
1602 {
1603 ao2_cleanup(chan->snapshot);
1604 chan->snapshot = ao2_bump(snapshot);
1605 }
1606
1607 struct ast_flags *ast_channel_snapshot_segment_flags(struct ast_channel *chan)
1608 {
1609 return &chan->snapshot_segment_flags;
1610 }
1611
1612 struct ast_endpoint *ast_channel_endpoint(const struct ast_channel *chan)
1613 {
1614 return chan->endpoint;
1615 }
1616
1617 void ast_channel_endpoint_set(struct ast_channel *chan, struct ast_endpoint *endpoint)
1618 {
1619 if (chan->endpoint) {
1620 ast_endpoint_remove_channel(chan->endpoint, chan);
1621 ao2_ref(chan->endpoint, -1);
1622 }
1623
1624 chan->endpoint = ao2_bump(endpoint);
1625
1626 if (chan->endpoint) {
1627 ast_endpoint_add_channel(chan->endpoint, chan);
1628 }
1629 }