/
channel.c
11131 lines (9813 loc) · 342 KB
/
channel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 1999 - 2006, Digium, Inc.
*
* Mark Spencer <markster@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief Channel Management
*
* \author Mark Spencer <markster@digium.com>
*/
/*** MODULEINFO
<support_level>core</support_level>
***/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision: 378459 $")
#include "asterisk/_private.h"
#include <sys/time.h>
#include <signal.h>
#include <math.h>
#include "asterisk/paths.h" /* use ast_config_AST_SYSTEM_NAME */
#include "asterisk/pbx.h"
#include "asterisk/frame.h"
#include "asterisk/mod_format.h"
#include "asterisk/sched.h"
#include "asterisk/channel.h"
#include "asterisk/musiconhold.h"
#include "asterisk/say.h"
#include "asterisk/file.h"
#include "asterisk/cli.h"
#include "asterisk/translate.h"
#include "asterisk/manager.h"
#include "asterisk/cel.h"
#include "asterisk/chanvars.h"
#include "asterisk/linkedlists.h"
#include "asterisk/indications.h"
#include "asterisk/monitor.h"
#include "asterisk/causes.h"
#include "asterisk/callerid.h"
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/app.h"
#include "asterisk/transcap.h"
#include "asterisk/devicestate.h"
#include "asterisk/threadstorage.h"
#include "asterisk/slinfactory.h"
#include "asterisk/audiohook.h"
#include "asterisk/framehook.h"
#include "asterisk/timing.h"
#include "asterisk/autochan.h"
#include "asterisk/stringfields.h"
#include "asterisk/global_datastores.h"
#include "asterisk/data.h"
#include "asterisk/channel_internal.h"
#include "asterisk/features.h"
#include "asterisk/test.h"
/*** DOCUMENTATION
***/
#ifdef HAVE_EPOLL
#include <sys/epoll.h>
#endif
#if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
#if defined(HAVE_PRI)
#include "libpri.h"
#endif /* defined(HAVE_PRI) */
#endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
struct ast_epoll_data {
struct ast_channel *chan;
int which;
};
/* uncomment if you have problems with 'monitoring' synchronized files */
#if 0
#define MONITOR_CONSTANT_DELAY
#define MONITOR_DELAY 150 * 8 /*!< 150 ms of MONITORING DELAY */
#endif
/*! \brief Prevent new channel allocation if shutting down. */
static int shutting_down;
static int uniqueint;
static int chancount;
unsigned long global_fin, global_fout;
AST_THREADSTORAGE(state2str_threadbuf);
#define STATE2STR_BUFSIZE 32
/*! Default amount of time to use when emulating a digit as a begin and end
* 100ms */
#define AST_DEFAULT_EMULATE_DTMF_DURATION 100
/*! Minimum amount of time between the end of the last digit and the beginning
* of a new one - 45ms */
#define AST_MIN_DTMF_GAP 45
/*! \brief List of channel drivers */
struct chanlist {
const struct ast_channel_tech *tech;
AST_LIST_ENTRY(chanlist) list;
};
#ifdef CHANNEL_TRACE
/*! \brief Structure to hold channel context backtrace data */
struct ast_chan_trace_data {
int enabled;
AST_LIST_HEAD_NOLOCK(, ast_chan_trace) trace;
};
/*! \brief Structure to save contexts where an ast_chan has been into */
struct ast_chan_trace {
char context[AST_MAX_CONTEXT];
char exten[AST_MAX_EXTENSION];
int priority;
AST_LIST_ENTRY(ast_chan_trace) entry;
};
#endif
/*! \brief the list of registered channel types */
static AST_RWLIST_HEAD_STATIC(backends, chanlist);
#ifdef LOW_MEMORY
#define NUM_CHANNEL_BUCKETS 61
#else
#define NUM_CHANNEL_BUCKETS 1567
#endif
/*! \brief All active channels on the system */
static struct ao2_container *channels;
/*! \brief map AST_CAUSE's to readable string representations
*
* \ref causes.h
*/
struct causes_map {
int cause;
const char *name;
const char *desc;
};
static const struct causes_map causes[] = {
{ AST_CAUSE_UNALLOCATED, "UNALLOCATED", "Unallocated (unassigned) number" },
{ AST_CAUSE_NO_ROUTE_TRANSIT_NET, "NO_ROUTE_TRANSIT_NET", "No route to specified transmit network" },
{ AST_CAUSE_NO_ROUTE_DESTINATION, "NO_ROUTE_DESTINATION", "No route to destination" },
{ AST_CAUSE_MISDIALLED_TRUNK_PREFIX, "MISDIALLED_TRUNK_PREFIX", "Misdialed trunk prefix" },
{ AST_CAUSE_CHANNEL_UNACCEPTABLE, "CHANNEL_UNACCEPTABLE", "Channel unacceptable" },
{ AST_CAUSE_CALL_AWARDED_DELIVERED, "CALL_AWARDED_DELIVERED", "Call awarded and being delivered in an established channel" },
{ AST_CAUSE_PRE_EMPTED, "PRE_EMPTED", "Pre-empted" },
{ AST_CAUSE_NUMBER_PORTED_NOT_HERE, "NUMBER_PORTED_NOT_HERE", "Number ported elsewhere" },
{ AST_CAUSE_NORMAL_CLEARING, "NORMAL_CLEARING", "Normal Clearing" },
{ AST_CAUSE_USER_BUSY, "USER_BUSY", "User busy" },
{ AST_CAUSE_NO_USER_RESPONSE, "NO_USER_RESPONSE", "No user responding" },
{ AST_CAUSE_NO_ANSWER, "NO_ANSWER", "User alerting, no answer" },
{ AST_CAUSE_SUBSCRIBER_ABSENT, "SUBSCRIBER_ABSENT", "Subscriber absent" },
{ AST_CAUSE_CALL_REJECTED, "CALL_REJECTED", "Call Rejected" },
{ AST_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED", "Number changed" },
{ AST_CAUSE_REDIRECTED_TO_NEW_DESTINATION, "REDIRECTED_TO_NEW_DESTINATION", "Redirected to new destination" },
{ AST_CAUSE_ANSWERED_ELSEWHERE, "ANSWERED_ELSEWHERE", "Answered elsewhere" },
{ AST_CAUSE_DESTINATION_OUT_OF_ORDER, "DESTINATION_OUT_OF_ORDER", "Destination out of order" },
{ AST_CAUSE_INVALID_NUMBER_FORMAT, "INVALID_NUMBER_FORMAT", "Invalid number format" },
{ AST_CAUSE_FACILITY_REJECTED, "FACILITY_REJECTED", "Facility rejected" },
{ AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY, "RESPONSE_TO_STATUS_ENQUIRY", "Response to STATus ENQuiry" },
{ AST_CAUSE_NORMAL_UNSPECIFIED, "NORMAL_UNSPECIFIED", "Normal, unspecified" },
{ AST_CAUSE_NORMAL_CIRCUIT_CONGESTION, "NORMAL_CIRCUIT_CONGESTION", "Circuit/channel congestion" },
{ AST_CAUSE_NETWORK_OUT_OF_ORDER, "NETWORK_OUT_OF_ORDER", "Network out of order" },
{ AST_CAUSE_NORMAL_TEMPORARY_FAILURE, "NORMAL_TEMPORARY_FAILURE", "Temporary failure" },
{ AST_CAUSE_SWITCH_CONGESTION, "SWITCH_CONGESTION", "Switching equipment congestion" },
{ AST_CAUSE_ACCESS_INFO_DISCARDED, "ACCESS_INFO_DISCARDED", "Access information discarded" },
{ AST_CAUSE_REQUESTED_CHAN_UNAVAIL, "REQUESTED_CHAN_UNAVAIL", "Requested channel not available" },
{ AST_CAUSE_FACILITY_NOT_SUBSCRIBED, "FACILITY_NOT_SUBSCRIBED", "Facility not subscribed" },
{ AST_CAUSE_OUTGOING_CALL_BARRED, "OUTGOING_CALL_BARRED", "Outgoing call barred" },
{ AST_CAUSE_INCOMING_CALL_BARRED, "INCOMING_CALL_BARRED", "Incoming call barred" },
{ AST_CAUSE_BEARERCAPABILITY_NOTAUTH, "BEARERCAPABILITY_NOTAUTH", "Bearer capability not authorized" },
{ AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, "BEARERCAPABILITY_NOTAVAIL", "Bearer capability not available" },
{ AST_CAUSE_BEARERCAPABILITY_NOTIMPL, "BEARERCAPABILITY_NOTIMPL", "Bearer capability not implemented" },
{ AST_CAUSE_CHAN_NOT_IMPLEMENTED, "CHAN_NOT_IMPLEMENTED", "Channel not implemented" },
{ AST_CAUSE_FACILITY_NOT_IMPLEMENTED, "FACILITY_NOT_IMPLEMENTED", "Facility not implemented" },
{ AST_CAUSE_INVALID_CALL_REFERENCE, "INVALID_CALL_REFERENCE", "Invalid call reference value" },
{ AST_CAUSE_INCOMPATIBLE_DESTINATION, "INCOMPATIBLE_DESTINATION", "Incompatible destination" },
{ AST_CAUSE_INVALID_MSG_UNSPECIFIED, "INVALID_MSG_UNSPECIFIED", "Invalid message unspecified" },
{ AST_CAUSE_MANDATORY_IE_MISSING, "MANDATORY_IE_MISSING", "Mandatory information element is missing" },
{ AST_CAUSE_MESSAGE_TYPE_NONEXIST, "MESSAGE_TYPE_NONEXIST", "Message type nonexist." },
{ AST_CAUSE_WRONG_MESSAGE, "WRONG_MESSAGE", "Wrong message" },
{ AST_CAUSE_IE_NONEXIST, "IE_NONEXIST", "Info. element nonexist or not implemented" },
{ AST_CAUSE_INVALID_IE_CONTENTS, "INVALID_IE_CONTENTS", "Invalid information element contents" },
{ AST_CAUSE_WRONG_CALL_STATE, "WRONG_CALL_STATE", "Message not compatible with call state" },
{ AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE, "RECOVERY_ON_TIMER_EXPIRE", "Recover on timer expiry" },
{ AST_CAUSE_MANDATORY_IE_LENGTH_ERROR, "MANDATORY_IE_LENGTH_ERROR", "Mandatory IE length error" },
{ AST_CAUSE_PROTOCOL_ERROR, "PROTOCOL_ERROR", "Protocol error, unspecified" },
{ AST_CAUSE_INTERWORKING, "INTERWORKING", "Interworking, unspecified" },
};
struct ast_variable *ast_channeltype_list(void)
{
struct chanlist *cl;
struct ast_variable *var = NULL, *prev = NULL;
AST_RWLIST_RDLOCK(&backends);
AST_RWLIST_TRAVERSE(&backends, cl, list) {
if (prev) {
if ((prev->next = ast_variable_new(cl->tech->type, cl->tech->description, "")))
prev = prev->next;
} else {
var = ast_variable_new(cl->tech->type, cl->tech->description, "");
prev = var;
}
}
AST_RWLIST_UNLOCK(&backends);
return var;
}
#if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
static const char *party_number_ton2str(int ton)
{
#if defined(HAVE_PRI)
switch ((ton >> 4) & 0x07) {
case PRI_TON_INTERNATIONAL:
return "International";
case PRI_TON_NATIONAL:
return "National";
case PRI_TON_NET_SPECIFIC:
return "Network Specific";
case PRI_TON_SUBSCRIBER:
return "Subscriber";
case PRI_TON_ABBREVIATED:
return "Abbreviated";
case PRI_TON_RESERVED:
return "Reserved";
case PRI_TON_UNKNOWN:
default:
break;
}
#endif /* defined(HAVE_PRI) */
return "Unknown";
}
#endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
#if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
static const char *party_number_plan2str(int plan)
{
#if defined(HAVE_PRI)
switch (plan & 0x0F) {
default:
case PRI_NPI_UNKNOWN:
break;
case PRI_NPI_E163_E164:
return "Public (E.163/E.164)";
case PRI_NPI_X121:
return "Data (X.121)";
case PRI_NPI_F69:
return "Telex (F.69)";
case PRI_NPI_NATIONAL:
return "National Standard";
case PRI_NPI_PRIVATE:
return "Private";
case PRI_NPI_RESERVED:
return "Reserved";
}
#endif /* defined(HAVE_PRI) */
return "Unknown";
}
#endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
/*! \brief Show channel types - CLI command */
static char *handle_cli_core_show_channeltypes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
#define FORMAT "%-10.10s %-40.40s %-12.12s %-12.12s %-12.12s\n"
struct chanlist *cl;
int count_chan = 0;
switch (cmd) {
case CLI_INIT:
e->command = "core show channeltypes";
e->usage =
"Usage: core show channeltypes\n"
" Lists available channel types registered in your\n"
" Asterisk server.\n";
return NULL;
case CLI_GENERATE:
return NULL;
}
if (a->argc != 3)
return CLI_SHOWUSAGE;
ast_cli(a->fd, FORMAT, "Type", "Description", "Devicestate", "Indications", "Transfer");
ast_cli(a->fd, FORMAT, "----------", "-----------", "-----------", "-----------", "--------");
AST_RWLIST_RDLOCK(&backends);
AST_RWLIST_TRAVERSE(&backends, cl, list) {
ast_cli(a->fd, FORMAT, cl->tech->type, cl->tech->description,
(cl->tech->devicestate) ? "yes" : "no",
(cl->tech->indicate) ? "yes" : "no",
(cl->tech->transfer) ? "yes" : "no");
count_chan++;
}
AST_RWLIST_UNLOCK(&backends);
ast_cli(a->fd, "----------\n%d channel drivers registered.\n", count_chan);
return CLI_SUCCESS;
#undef FORMAT
}
static char *complete_channeltypes(struct ast_cli_args *a)
{
struct chanlist *cl;
int which = 0;
int wordlen;
char *ret = NULL;
if (a->pos != 3)
return NULL;
wordlen = strlen(a->word);
AST_RWLIST_RDLOCK(&backends);
AST_RWLIST_TRAVERSE(&backends, cl, list) {
if (!strncasecmp(a->word, cl->tech->type, wordlen) && ++which > a->n) {
ret = ast_strdup(cl->tech->type);
break;
}
}
AST_RWLIST_UNLOCK(&backends);
return ret;
}
/*! \brief Show details about a channel driver - CLI command */
static char *handle_cli_core_show_channeltype(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
struct chanlist *cl = NULL;
char buf[512];
switch (cmd) {
case CLI_INIT:
e->command = "core show channeltype";
e->usage =
"Usage: core show channeltype <name>\n"
" Show details about the specified channel type, <name>.\n";
return NULL;
case CLI_GENERATE:
return complete_channeltypes(a);
}
if (a->argc != 4)
return CLI_SHOWUSAGE;
AST_RWLIST_RDLOCK(&backends);
AST_RWLIST_TRAVERSE(&backends, cl, list) {
if (!strncasecmp(cl->tech->type, a->argv[3], strlen(cl->tech->type)))
break;
}
if (!cl) {
ast_cli(a->fd, "\n%s is not a registered channel driver.\n", a->argv[3]);
AST_RWLIST_UNLOCK(&backends);
return CLI_FAILURE;
}
ast_cli(a->fd,
"-- Info about channel driver: %s --\n"
" Device State: %s\n"
" Indication: %s\n"
" Transfer : %s\n"
" Capabilities: %s\n"
" Digit Begin: %s\n"
" Digit End: %s\n"
" Send HTML : %s\n"
" Image Support: %s\n"
" Text Support: %s\n",
cl->tech->type,
(cl->tech->devicestate) ? "yes" : "no",
(cl->tech->indicate) ? "yes" : "no",
(cl->tech->transfer) ? "yes" : "no",
ast_getformatname_multiple(buf, sizeof(buf), cl->tech->capabilities),
(cl->tech->send_digit_begin) ? "yes" : "no",
(cl->tech->send_digit_end) ? "yes" : "no",
(cl->tech->send_html) ? "yes" : "no",
(cl->tech->send_image) ? "yes" : "no",
(cl->tech->send_text) ? "yes" : "no"
);
AST_RWLIST_UNLOCK(&backends);
return CLI_SUCCESS;
}
static struct ast_cli_entry cli_channel[] = {
AST_CLI_DEFINE(handle_cli_core_show_channeltypes, "List available channel types"),
AST_CLI_DEFINE(handle_cli_core_show_channeltype, "Give more details on that channel type")
};
static struct ast_frame *kill_read(struct ast_channel *chan)
{
/* Hangup channel. */
return NULL;
}
static struct ast_frame *kill_exception(struct ast_channel *chan)
{
/* Hangup channel. */
return NULL;
}
static int kill_write(struct ast_channel *chan, struct ast_frame *frame)
{
/* Hangup channel. */
return -1;
}
static int kill_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
{
/* No problem fixing up the channel. */
return 0;
}
static int kill_hangup(struct ast_channel *chan)
{
ast_channel_tech_pvt_set(chan, NULL);
return 0;
}
/*!
* \brief Kill the channel channel driver technology descriptor.
*
* \details
* The purpose of this channel technology is to encourage the
* channel to hangup as quickly as possible.
*
* \note Used by DTMF atxfer and zombie channels.
*/
const struct ast_channel_tech ast_kill_tech = {
.type = "Kill",
.description = "Kill channel (should not see this)",
.read = kill_read,
.exception = kill_exception,
.write = kill_write,
.fixup = kill_fixup,
.hangup = kill_hangup,
};
#ifdef CHANNEL_TRACE
/*! \brief Destructor for the channel trace datastore */
static void ast_chan_trace_destroy_cb(void *data)
{
struct ast_chan_trace *trace;
struct ast_chan_trace_data *traced = data;
while ((trace = AST_LIST_REMOVE_HEAD(&traced->trace, entry))) {
ast_free(trace);
}
ast_free(traced);
}
/*! \brief Datastore to put the linked list of ast_chan_trace and trace status */
static const struct ast_datastore_info ast_chan_trace_datastore_info = {
.type = "ChanTrace",
.destroy = ast_chan_trace_destroy_cb
};
/*! \brief Put the channel backtrace in a string */
int ast_channel_trace_serialize(struct ast_channel *chan, struct ast_str **buf)
{
int total = 0;
struct ast_chan_trace *trace;
struct ast_chan_trace_data *traced;
struct ast_datastore *store;
ast_channel_lock(chan);
store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
if (!store) {
ast_channel_unlock(chan);
return total;
}
traced = store->data;
ast_str_reset(*buf);
AST_LIST_TRAVERSE(&traced->trace, trace, entry) {
if (ast_str_append(buf, 0, "[%d] => %s, %s, %d\n", total, trace->context, trace->exten, trace->priority) < 0) {
ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
total = -1;
break;
}
total++;
}
ast_channel_unlock(chan);
return total;
}
/* !\brief Whether or not context tracing is enabled */
int ast_channel_trace_is_enabled(struct ast_channel *chan)
{
struct ast_datastore *store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
if (!store)
return 0;
return ((struct ast_chan_trace_data *)store->data)->enabled;
}
/*! \brief Update the context backtrace data if tracing is enabled */
static int ast_channel_trace_data_update(struct ast_channel *chan, struct ast_chan_trace_data *traced)
{
struct ast_chan_trace *trace;
if (!traced->enabled)
return 0;
/* If the last saved context does not match the current one
OR we have not saved any context so far, then save the current context */
if ((!AST_LIST_EMPTY(&traced->trace) && strcasecmp(AST_LIST_FIRST(&traced->trace)->context, ast_channel_context(chan))) ||
(AST_LIST_EMPTY(&traced->trace))) {
/* Just do some debug logging */
if (AST_LIST_EMPTY(&traced->trace))
ast_debug(1, "Setting initial trace context to %s\n", ast_channel_context(chan));
else
ast_debug(1, "Changing trace context from %s to %s\n", AST_LIST_FIRST(&traced->trace)->context, ast_channel_context(chan));
/* alloc or bail out */
trace = ast_malloc(sizeof(*trace));
if (!trace)
return -1;
/* save the current location and store it in the trace list */
ast_copy_string(trace->context, ast_channel_context(chan), sizeof(trace->context));
ast_copy_string(trace->exten, ast_channel_exten(chan), sizeof(trace->exten));
trace->priority = ast_channel_priority(chan);
AST_LIST_INSERT_HEAD(&traced->trace, trace, entry);
}
return 0;
}
/*! \brief Update the context backtrace if tracing is enabled */
int ast_channel_trace_update(struct ast_channel *chan)
{
struct ast_datastore *store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
if (!store)
return 0;
return ast_channel_trace_data_update(chan, store->data);
}
/*! \brief Enable context tracing in the channel */
int ast_channel_trace_enable(struct ast_channel *chan)
{
struct ast_datastore *store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
struct ast_chan_trace_data *traced;
if (!store) {
store = ast_datastore_alloc(&ast_chan_trace_datastore_info, "ChanTrace");
if (!store)
return -1;
traced = ast_calloc(1, sizeof(*traced));
if (!traced) {
ast_datastore_free(store);
return -1;
}
store->data = traced;
AST_LIST_HEAD_INIT_NOLOCK(&traced->trace);
ast_channel_datastore_add(chan, store);
}
((struct ast_chan_trace_data *)store->data)->enabled = 1;
ast_channel_trace_data_update(chan, store->data);
return 0;
}
/*! \brief Disable context tracing in the channel */
int ast_channel_trace_disable(struct ast_channel *chan)
{
struct ast_datastore *store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
if (!store)
return 0;
((struct ast_chan_trace_data *)store->data)->enabled = 0;
return 0;
}
#endif /* CHANNEL_TRACE */
/*! \brief Checks to see if a channel is needing hang up */
int ast_check_hangup(struct ast_channel *chan)
{
if (ast_channel_softhangup_internal_flag(chan)) /* yes if soft hangup flag set */
return 1;
if (ast_tvzero(*ast_channel_whentohangup(chan))) /* no if no hangup scheduled */
return 0;
if (ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()) > 0) /* no if hangup time has not come yet. */
return 0;
ast_debug(4, "Hangup time has come: %" PRIi64 "\n", ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()));
ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(chan));
ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT); /* record event */
return 1;
}
int ast_check_hangup_locked(struct ast_channel *chan)
{
int res;
ast_channel_lock(chan);
res = ast_check_hangup(chan);
ast_channel_unlock(chan);
return res;
}
void ast_channel_softhangup_withcause_locked(struct ast_channel *chan, int causecode)
{
ast_channel_lock(chan);
if (causecode > 0) {
ast_debug(1, "Setting hangupcause of channel %s to %d (is %d now)\n",
ast_channel_name(chan), causecode, ast_channel_hangupcause(chan));
ast_channel_hangupcause_set(chan, causecode);
}
ast_softhangup_nolock(chan, AST_SOFTHANGUP_EXPLICIT);
ast_channel_unlock(chan);
}
static int ast_channel_softhangup_cb(void *obj, void *arg, int flags)
{
struct ast_channel *chan = obj;
ast_softhangup(chan, AST_SOFTHANGUP_SHUTDOWN);
return 0;
}
void ast_begin_shutdown(int hangup)
{
shutting_down = 1;
if (hangup) {
ao2_callback(channels, OBJ_NODATA | OBJ_MULTIPLE, ast_channel_softhangup_cb, NULL);
}
}
/*! \brief returns number of active/allocated channels */
int ast_active_channels(void)
{
return channels ? ao2_container_count(channels) : 0;
}
int ast_undestroyed_channels(void)
{
return ast_atomic_fetchadd_int(&chancount, 0);
}
/*! \brief Cancel a shutdown in progress */
void ast_cancel_shutdown(void)
{
shutting_down = 0;
}
/*! \brief Returns non-zero if Asterisk is being shut down */
int ast_shutting_down(void)
{
return shutting_down;
}
/*! \brief Set when to hangup channel */
void ast_channel_setwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
{
if (ast_tvzero(offset)) {
ast_channel_whentohangup_set(chan, &offset);
} else {
struct timeval tv = ast_tvadd(offset, ast_tvnow());
ast_channel_whentohangup_set(chan, &tv);
}
ast_queue_frame(chan, &ast_null_frame);
return;
}
void ast_channel_setwhentohangup(struct ast_channel *chan, time_t offset)
{
struct timeval when = { offset, };
ast_channel_setwhentohangup_tv(chan, when);
}
/*! \brief Compare a offset with when to hangup channel */
int ast_channel_cmpwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
{
struct timeval whentohangup;
if (ast_tvzero(*ast_channel_whentohangup(chan)))
return ast_tvzero(offset) ? 0 : -1;
if (ast_tvzero(offset))
return 1;
whentohangup = ast_tvadd(offset, ast_tvnow());
return ast_tvdiff_ms(whentohangup, *ast_channel_whentohangup(chan));
}
int ast_channel_cmpwhentohangup(struct ast_channel *chan, time_t offset)
{
struct timeval when = { offset, };
return ast_channel_cmpwhentohangup_tv(chan, when);
}
/*! \brief Register a new telephony channel in Asterisk */
int ast_channel_register(const struct ast_channel_tech *tech)
{
struct chanlist *chan;
AST_RWLIST_WRLOCK(&backends);
AST_RWLIST_TRAVERSE(&backends, chan, list) {
if (!strcasecmp(tech->type, chan->tech->type)) {
ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", tech->type);
AST_RWLIST_UNLOCK(&backends);
return -1;
}
}
if (!(chan = ast_calloc(1, sizeof(*chan)))) {
AST_RWLIST_UNLOCK(&backends);
return -1;
}
chan->tech = tech;
AST_RWLIST_INSERT_HEAD(&backends, chan, list);
ast_debug(1, "Registered handler for '%s' (%s)\n", chan->tech->type, chan->tech->description);
ast_verb(2, "Registered channel type '%s' (%s)\n", chan->tech->type, chan->tech->description);
AST_RWLIST_UNLOCK(&backends);
return 0;
}
/*! \brief Unregister channel driver */
void ast_channel_unregister(const struct ast_channel_tech *tech)
{
struct chanlist *chan;
ast_debug(1, "Unregistering channel type '%s'\n", tech->type);
AST_RWLIST_WRLOCK(&backends);
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&backends, chan, list) {
if (chan->tech == tech) {
AST_LIST_REMOVE_CURRENT(list);
ast_free(chan);
ast_verb(2, "Unregistered channel type '%s'\n", tech->type);
break;
}
}
AST_LIST_TRAVERSE_SAFE_END;
AST_RWLIST_UNLOCK(&backends);
}
/*! \brief Get handle to channel driver based on name */
const struct ast_channel_tech *ast_get_channel_tech(const char *name)
{
struct chanlist *chanls;
const struct ast_channel_tech *ret = NULL;
AST_RWLIST_RDLOCK(&backends);
AST_RWLIST_TRAVERSE(&backends, chanls, list) {
if (!strcasecmp(name, chanls->tech->type)) {
ret = chanls->tech;
break;
}
}
AST_RWLIST_UNLOCK(&backends);
return ret;
}
/*! \brief Gives the string form of a given hangup cause */
const char *ast_cause2str(int cause)
{
int x;
for (x = 0; x < ARRAY_LEN(causes); x++) {
if (causes[x].cause == cause)
return causes[x].desc;
}
return "Unknown";
}
/*! \brief Convert a symbolic hangup cause to number */
int ast_str2cause(const char *name)
{
int x;
for (x = 0; x < ARRAY_LEN(causes); x++)
if (!strncasecmp(causes[x].name, name, strlen(causes[x].name)))
return causes[x].cause;
return -1;
}
/*! \brief Gives the string form of a given channel state.
\note This function is not reentrant.
*/
const char *ast_state2str(enum ast_channel_state state)
{
char *buf;
switch (state) {
case AST_STATE_DOWN:
return "Down";
case AST_STATE_RESERVED:
return "Rsrvd";
case AST_STATE_OFFHOOK:
return "OffHook";
case AST_STATE_DIALING:
return "Dialing";
case AST_STATE_RING:
return "Ring";
case AST_STATE_RINGING:
return "Ringing";
case AST_STATE_UP:
return "Up";
case AST_STATE_BUSY:
return "Busy";
case AST_STATE_DIALING_OFFHOOK:
return "Dialing Offhook";
case AST_STATE_PRERING:
return "Pre-ring";
default:
if (!(buf = ast_threadstorage_get(&state2str_threadbuf, STATE2STR_BUFSIZE)))
return "Unknown";
snprintf(buf, STATE2STR_BUFSIZE, "Unknown (%d)", state);
return buf;
}
}
/*! \brief Gives the string form of a given transfer capability */
char *ast_transfercapability2str(int transfercapability)
{
switch (transfercapability) {
case AST_TRANS_CAP_SPEECH:
return "SPEECH";
case AST_TRANS_CAP_DIGITAL:
return "DIGITAL";
case AST_TRANS_CAP_RESTRICTED_DIGITAL:
return "RESTRICTED_DIGITAL";
case AST_TRANS_CAP_3_1K_AUDIO:
return "3K1AUDIO";
case AST_TRANS_CAP_DIGITAL_W_TONES:
return "DIGITAL_W_TONES";
case AST_TRANS_CAP_VIDEO:
return "VIDEO";
default:
return "UNKNOWN";
}
}
/*! \brief Pick the best audio codec */
struct ast_format *ast_best_codec(struct ast_format_cap *cap, struct ast_format *result)
{
/* This just our opinion, expressed in code. We are asked to choose
the best codec to use, given no information */
static const enum ast_format_id prefs[] =
{
/*! Okay, ulaw is used by all telephony equipment, so start with it */
AST_FORMAT_ULAW,
/*! Unless of course, you're a silly European, so then prefer ALAW */
AST_FORMAT_ALAW,
AST_FORMAT_G719,
AST_FORMAT_SIREN14,
AST_FORMAT_SIREN7,
AST_FORMAT_TESTLAW,
/*! G.722 is better then all below, but not as common as the above... so give ulaw and alaw priority */
AST_FORMAT_G722,
/*! Okay, well, signed linear is easy to translate into other stuff */
AST_FORMAT_SLINEAR192,
AST_FORMAT_SLINEAR96,
AST_FORMAT_SLINEAR48,
AST_FORMAT_SLINEAR44,
AST_FORMAT_SLINEAR32,
AST_FORMAT_SLINEAR24,
AST_FORMAT_SLINEAR16,
AST_FORMAT_SLINEAR12,
AST_FORMAT_SLINEAR,
/*! G.726 is standard ADPCM, in RFC3551 packing order */
AST_FORMAT_G726,
/*! G.726 is standard ADPCM, in AAL2 packing order */
AST_FORMAT_G726_AAL2,
/*! ADPCM has great sound quality and is still pretty easy to translate */
AST_FORMAT_ADPCM,
/*! Okay, we're down to vocoders now, so pick GSM because it's small and easier to
translate and sounds pretty good */
AST_FORMAT_GSM,
/*! iLBC is not too bad */
AST_FORMAT_ILBC,
/*! Speex is free, but computationally more expensive than GSM */
AST_FORMAT_SPEEX32,
AST_FORMAT_SPEEX16,
AST_FORMAT_SPEEX,
/*! SILK is pretty awesome. */
AST_FORMAT_SILK,
/*! CELT supports crazy high sample rates */
AST_FORMAT_CELT,
/*! Ick, LPC10 sounds terrible, but at least we have code for it, if you're tacky enough
to use it */
AST_FORMAT_LPC10,
/*! G.729a is faster than 723 and slightly less expensive */
AST_FORMAT_G729A,
/*! Down to G.723.1 which is proprietary but at least designed for voice */
AST_FORMAT_G723_1,
//TODO how to handle custom formats
};
char buf[512];
int x;
/* Find the first preferred codec in the format given */
for (x = 0; x < ARRAY_LEN(prefs); x++) {
if (ast_format_cap_best_byid(cap, prefs[x], result)) {
return result;
}
}
ast_format_clear(result);
ast_log(LOG_WARNING, "Don't know any of %s formats\n", ast_getformatname_multiple(buf, sizeof(buf), cap));
return NULL;
}
static const struct ast_channel_tech null_tech = {
.type = "NULL",
.description = "Null channel (should not see this)",
};
static void ast_channel_destructor(void *obj);
static void ast_dummy_channel_destructor(void *obj);
/*! \brief Create a new channel structure */
static struct ast_channel * attribute_malloc __attribute__((format(printf, 13, 0)))
__ast_channel_alloc_ap(int needqueue, int state, const char *cid_num, const char *cid_name,
const char *acctcode, const char *exten, const char *context,
const char *linkedid, const int amaflag, const char *file, int line,
const char *function, const char *name_fmt, va_list ap)
{
struct ast_channel *tmp;
struct varshead *headp;
char *tech = "", *tech2 = NULL;
struct ast_format_cap *nativeformats;
struct ast_sched_context *schedctx;
struct ast_timer *timer;
struct timeval now;
/* If shutting down, don't allocate any new channels */
if (ast_shutting_down()) {
ast_log(LOG_WARNING, "Channel allocation failed: Refusing due to active shutdown\n");
return NULL;
}
if (!(tmp = ast_channel_internal_alloc(ast_channel_destructor))) {
/* Channel structure allocation failure. */
return NULL;
}
if (!(nativeformats = ast_format_cap_alloc())) {
ao2_ref(tmp, -1);
/* format capabilities structure allocation failure */
return NULL;
}
ast_channel_nativeformats_set(tmp, nativeformats);
/*
* Init file descriptors to unopened state so
* the destructor can know not to close them.
*/
ast_channel_timingfd_set(tmp, -1);
ast_channel_internal_alertpipe_clear(tmp);
ast_channel_internal_fd_clear_all(tmp);
#ifdef HAVE_EPOLL
ast_channel_epfd_set(tmp, epoll_create(25));
#endif
if (!(schedctx = ast_sched_context_create())) {
ast_log(LOG_WARNING, "Channel allocation failed: Unable to create schedule context\n");