-
Notifications
You must be signed in to change notification settings - Fork 14
/
pri.c
2285 lines (2059 loc) · 62.9 KB
/
pri.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
/*
* libpri: An implementation of Primary Rate ISDN
*
* Written by Mark Spencer <markster@digium.com>
*
* Copyright (C) 2001-2005, Digium, Inc.
* All Rights Reserved.
*/
/*
* 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 as published by the
* Free Software Foundation. See the LICENSE file included with
* this program for more details.
*
* In addition, when this program is distributed with Asterisk in
* any form that would qualify as a 'combined work' or as a
* 'derivative work' (but not mere aggregation), you can redistribute
* and/or modify the combination under the terms of the license
* provided with that copy of Asterisk, instead of the license
* terms granted here.
*/
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/select.h>
#include <stdarg.h>
#include "compat.h"
#include "libpri.h"
#include "pri_internal.h"
#include "pri_facility.h"
#define PRI_BIT(a_bit) (1UL << (a_bit))
#define PRI_ALL_SWITCHES 0xFFFFFFFF
#define PRI_ETSI_SWITCHES (PRI_BIT(PRI_SWITCH_EUROISDN_E1) | PRI_BIT(PRI_SWITCH_EUROISDN_T1))
struct pri_timer_table {
const char *name;
enum PRI_TIMERS_AND_COUNTERS number;
unsigned long used_by;
};
/*!
* \note Sort the timer table entries in the order of the timer name so
* pri_dump_info_str() can display them in a consistent order.
*/
static const struct pri_timer_table pri_timer[] = {
/* *INDENT-OFF* */
/* timer name timer number used by switches */
{ "N200", PRI_TIMER_N200, PRI_ALL_SWITCHES },
{ "N201", PRI_TIMER_N201, PRI_ALL_SWITCHES },
{ "N202", PRI_TIMER_N202, PRI_ALL_SWITCHES },
{ "K", PRI_TIMER_K, PRI_ALL_SWITCHES },
{ "T200", PRI_TIMER_T200, PRI_ALL_SWITCHES },
{ "T201", PRI_TIMER_T201, PRI_ALL_SWITCHES },
{ "T202", PRI_TIMER_T202, PRI_ALL_SWITCHES },
{ "T203", PRI_TIMER_T203, PRI_ALL_SWITCHES },
{ "T300", PRI_TIMER_T300, PRI_ALL_SWITCHES },
{ "T301", PRI_TIMER_T301, PRI_ALL_SWITCHES },
{ "T302", PRI_TIMER_T302, PRI_ALL_SWITCHES },
{ "T303", PRI_TIMER_T303, PRI_ALL_SWITCHES },
{ "T304", PRI_TIMER_T304, PRI_ALL_SWITCHES },
{ "T305", PRI_TIMER_T305, PRI_ALL_SWITCHES },
{ "T306", PRI_TIMER_T306, PRI_ALL_SWITCHES },
{ "T307", PRI_TIMER_T307, PRI_ALL_SWITCHES },
{ "T308", PRI_TIMER_T308, PRI_ALL_SWITCHES },
{ "T309", PRI_TIMER_T309, PRI_ALL_SWITCHES },
{ "T310", PRI_TIMER_T310, PRI_ALL_SWITCHES },
{ "T312", PRI_TIMER_T312, PRI_ALL_SWITCHES },
{ "T313", PRI_TIMER_T313, PRI_ALL_SWITCHES },
{ "T314", PRI_TIMER_T314, PRI_ALL_SWITCHES },
{ "T316", PRI_TIMER_T316, PRI_ALL_SWITCHES },
{ "N316", PRI_TIMER_N316, PRI_ALL_SWITCHES },
{ "T317", PRI_TIMER_T317, PRI_ALL_SWITCHES },
{ "T318", PRI_TIMER_T318, PRI_ALL_SWITCHES },
{ "T319", PRI_TIMER_T319, PRI_ALL_SWITCHES },
{ "T320", PRI_TIMER_T320, PRI_ALL_SWITCHES },
{ "T321", PRI_TIMER_T321, PRI_ALL_SWITCHES },
{ "T322", PRI_TIMER_T322, PRI_ALL_SWITCHES },
{ "T-HOLD", PRI_TIMER_T_HOLD, PRI_ALL_SWITCHES },
{ "T-RETRIEVE", PRI_TIMER_T_RETRIEVE, PRI_ALL_SWITCHES },
{ "T-RESPONSE", PRI_TIMER_T_RESPONSE, PRI_ALL_SWITCHES },
{ "T-STATUS", PRI_TIMER_T_STATUS, PRI_ETSI_SWITCHES },
{ "T-ACTIVATE", PRI_TIMER_T_ACTIVATE, PRI_ETSI_SWITCHES },
{ "T-DEACTIVATE", PRI_TIMER_T_DEACTIVATE, PRI_ETSI_SWITCHES },
{ "T-INTERROGATE", PRI_TIMER_T_INTERROGATE, PRI_ETSI_SWITCHES },
{ "T-RETENTION", PRI_TIMER_T_RETENTION, PRI_ETSI_SWITCHES | PRI_BIT(PRI_SWITCH_QSIG) },
{ "T-CCBS1", PRI_TIMER_T_CCBS1, PRI_ETSI_SWITCHES },
{ "T-CCBS2", PRI_TIMER_T_CCBS2, PRI_ETSI_SWITCHES },
{ "T-CCBS3", PRI_TIMER_T_CCBS3, PRI_ETSI_SWITCHES },
{ "T-CCBS4", PRI_TIMER_T_CCBS4, PRI_ETSI_SWITCHES },
{ "T-CCBS5", PRI_TIMER_T_CCBS5, PRI_ETSI_SWITCHES },
{ "T-CCBS6", PRI_TIMER_T_CCBS6, PRI_ETSI_SWITCHES },
{ "T-CCNR2", PRI_TIMER_T_CCNR2, PRI_ETSI_SWITCHES },
{ "T-CCNR5", PRI_TIMER_T_CCNR5, PRI_ETSI_SWITCHES },
{ "T-CCNR6", PRI_TIMER_T_CCNR6, PRI_ETSI_SWITCHES },
{ "CC-T1", PRI_TIMER_QSIG_CC_T1, PRI_BIT(PRI_SWITCH_QSIG) },
{ "CCBS-T2", PRI_TIMER_QSIG_CCBS_T2, PRI_BIT(PRI_SWITCH_QSIG) },
{ "CCNR-T2", PRI_TIMER_QSIG_CCNR_T2, PRI_BIT(PRI_SWITCH_QSIG) },
{ "CC-T3", PRI_TIMER_QSIG_CC_T3, PRI_BIT(PRI_SWITCH_QSIG) },
#if defined(QSIG_PATH_RESERVATION_SUPPORT)
{ "CC-T4", PRI_TIMER_QSIG_CC_T4, PRI_BIT(PRI_SWITCH_QSIG) },
#endif /* defined(QSIG_PATH_RESERVATION_SUPPORT) */
/* *INDENT-ON* */
};
char *pri_node2str(int node)
{
switch(node) {
case PRI_UNKNOWN:
return "Unknown node type";
case PRI_NETWORK:
return "Network";
case PRI_CPE:
return "CPE";
default:
return "Invalid value";
}
}
char *pri_switch2str(int sw)
{
switch(sw) {
case PRI_SWITCH_NI2:
return "National ISDN";
case PRI_SWITCH_DMS100:
return "Nortel DMS100";
case PRI_SWITCH_LUCENT5E:
return "Lucent 5E";
case PRI_SWITCH_ATT4ESS:
return "AT&T 4ESS";
case PRI_SWITCH_NI1:
return "National ISDN 1";
case PRI_SWITCH_EUROISDN_E1:
return "EuroISDN";
case PRI_SWITCH_GR303_EOC:
return "GR303 EOC";
case PRI_SWITCH_GR303_TMC:
return "GR303 TMC";
case PRI_SWITCH_QSIG:
return "Q.SIG switch";
default:
return "Unknown switchtype";
}
}
static void pri_default_timers(struct pri *ctrl, int switchtype)
{
unsigned idx;
/* Initialize all timers/counters to unsupported/disabled. */
for (idx = 0; idx < PRI_MAX_TIMERS; ++idx) {
ctrl->timers[idx] = -1;
}
/* Set timer values to standard defaults. Time is in ms. */
ctrl->timers[PRI_TIMER_N200] = 3; /* Max numer of Q.921 retransmissions */
ctrl->timers[PRI_TIMER_N202] = 3; /* Max numer of transmissions of the TEI identity request message */
if (ctrl->bri) {
ctrl->timers[PRI_TIMER_K] = 1; /* Max number of outstanding I-frames */
} else {
ctrl->timers[PRI_TIMER_K] = 7; /* Max number of outstanding I-frames */
}
ctrl->timers[PRI_TIMER_T200] = 1000; /* Time between SABME's */
ctrl->timers[PRI_TIMER_T201] = ctrl->timers[PRI_TIMER_T200];/* Time between TEI Identity Checks (Default same as T200) */
ctrl->timers[PRI_TIMER_T202] = 2 * 1000; /* Min time between transmission of TEI Identity request messages */
ctrl->timers[PRI_TIMER_T203] = 10 * 1000; /* Max time without exchanging packets */
ctrl->timers[PRI_TIMER_T303] = 4 * 1000; /* Length between SETUP retransmissions and timeout */
ctrl->timers[PRI_TIMER_T305] = 30 * 1000; /* Wait for DISCONNECT acknowledge */
ctrl->timers[PRI_TIMER_T308] = 4 * 1000; /* Wait for RELEASE acknowledge */
ctrl->timers[PRI_TIMER_T309] = 6 * 1000; /* Time to wait before clearing calls in case of D-channel transient event. Q.931 specifies 6-90 seconds */
ctrl->timers[PRI_TIMER_T312] = (4 + 2) * 1000;/* Supervise broadcast SETUP message call reference retention. T303 + 2 seconds */
ctrl->timers[PRI_TIMER_T313] = 4 * 1000; /* Wait for CONNECT acknowledge, CPE side only */
#if 0 /* Default disable the T316 timer otherwise the user cannot disable it. */
ctrl->timers[PRI_TIMER_T316] = 2 * 60 * 1000; /* RESTART retransmit timer */
#endif
ctrl->timers[PRI_TIMER_N316] = 2; /* Send RESTART this many times before giving up. */
ctrl->timers[PRI_TIMER_TM20] = 2500; /* Max time awaiting XID response - Q.921 Appendix IV */
ctrl->timers[PRI_TIMER_NM20] = 3; /* Number of XID retransmits - Q.921 Appendix IV */
ctrl->timers[PRI_TIMER_T_HOLD] = 4 * 1000; /* Wait for HOLD request response. */
ctrl->timers[PRI_TIMER_T_RETRIEVE] = 4 * 1000;/* Wait for RETRIEVE request response. */
ctrl->timers[PRI_TIMER_T_RESPONSE] = 4 * 1000; /* Maximum time to wait for a typical APDU response. */
/* ETSI timers */
ctrl->timers[PRI_TIMER_T_STATUS] = 4 * 1000; /* Max time to wait for all replies to check for compatible terminals */
ctrl->timers[PRI_TIMER_T_ACTIVATE] = 10 * 1000; /* Request supervision timeout. */
ctrl->timers[PRI_TIMER_T_DEACTIVATE] = 4 * 1000;/* Deactivate supervision timeout. */
ctrl->timers[PRI_TIMER_T_INTERROGATE] = 4 * 1000;/* Interrogation supervision timeout. */
/* ETSI call-completion timers */
ctrl->timers[PRI_TIMER_T_RETENTION] = 30 * 1000;/* Max time to wait for user A to activate call-completion. */
ctrl->timers[PRI_TIMER_T_CCBS1] = 4 * 1000; /* T-STATUS timer equivalent for CC user A status. */
ctrl->timers[PRI_TIMER_T_CCBS2] = 45 * 60 * 1000;/* Max time the CCBS service will be active */
ctrl->timers[PRI_TIMER_T_CCBS3] = 20 * 1000; /* Max time to wait for user A to respond to user B availability. */
ctrl->timers[PRI_TIMER_T_CCBS4] = 5 * 1000; /* CC user B guard time before sending CC recall indication. */
ctrl->timers[PRI_TIMER_T_CCBS5] = 60 * 60 * 1000;/* Network B CCBS supervision timeout. */
ctrl->timers[PRI_TIMER_T_CCBS6] = 60 * 60 * 1000;/* Network A CCBS supervision timeout. */
ctrl->timers[PRI_TIMER_T_CCNR2] = 180 * 60 * 1000;/* Max time the CCNR service will be active */
ctrl->timers[PRI_TIMER_T_CCNR5] = 195 * 60 * 1000;/* Network B CCNR supervision timeout. */
ctrl->timers[PRI_TIMER_T_CCNR6] = 195 * 60 * 1000;/* Network A CCNR supervision timeout. */
/* Q.SIG call-completion timers */
ctrl->timers[PRI_TIMER_QSIG_CC_T1] = 30 * 1000;/* CC request supervision timeout. */
ctrl->timers[PRI_TIMER_QSIG_CCBS_T2] = 60 * 60 * 1000;/* CCBS supervision timeout. */
ctrl->timers[PRI_TIMER_QSIG_CCNR_T2] = 195 * 60 * 1000;/* CCNR supervision timeout. */
ctrl->timers[PRI_TIMER_QSIG_CC_T3] = 30 * 1000;/* Max time to wait for user A to respond to user B availability. */
#if defined(QSIG_PATH_RESERVATION_SUPPORT)
ctrl->timers[PRI_TIMER_QSIG_CC_T4] = 40 * 1000;/* Path reservation supervision timeout. */
#endif /* defined(QSIG_PATH_RESERVATION_SUPPORT) */
/* Set any switch specific override default values */
switch (switchtype) {
default:
break;
}
}
int pri_set_timer(struct pri *ctrl, int timer, int value)
{
if (!ctrl || timer < 0 || PRI_MAX_TIMERS <= timer || value < 0) {
return -1;
}
ctrl->timers[timer] = value;
return 0;
}
int pri_get_timer(struct pri *ctrl, int timer)
{
if (!ctrl || timer < 0 || PRI_MAX_TIMERS <= timer) {
return -1;
}
return ctrl->timers[timer];
}
int pri_set_service_message_support(struct pri *pri, int supportflag)
{
if (!pri) {
return -1;
}
pri->service_message_support = supportflag ? 1 : 0;
return 0;
}
int pri_timer2idx(const char *timer_name)
{
unsigned idx;
enum PRI_TIMERS_AND_COUNTERS timer_number;
timer_number = -1;
for (idx = 0; idx < ARRAY_LEN(pri_timer); ++idx) {
if (!strcasecmp(timer_name, pri_timer[idx].name)) {
timer_number = pri_timer[idx].number;
break;
}
}
return timer_number;
}
static int __pri_read(struct pri *pri, void *buf, int buflen)
{
int res = read(pri->fd, buf, buflen);
if (res < 0) {
if (errno != EAGAIN)
pri_error(pri, "Read on %d failed: %s\n", pri->fd, strerror(errno));
return 0;
}
return res;
}
static int __pri_write(struct pri *pri, void *buf, int buflen)
{
int res = write(pri->fd, buf, buflen);
if (res < 0) {
if (errno != EAGAIN)
pri_error(pri, "Write to %d failed: %s\n", pri->fd, strerror(errno));
return 0;
}
return res;
}
/*!
* \internal
* \brief Determine the default layer 2 persistence option.
*
* \param ctrl D channel controller.
*
* \return Default layer 2 persistence option. (legacy behaviour default)
*/
static enum pri_layer2_persistence pri_l2_persistence_option_default(struct pri *ctrl)
{
enum pri_layer2_persistence persistence;
if (PTMP_MODE(ctrl)) {
persistence = PRI_L2_PERSISTENCE_LEAVE_DOWN;
} else {
persistence = PRI_L2_PERSISTENCE_KEEP_UP;
}
return persistence;
}
/*!
* \internal
* \brief Determine the default display text send options.
*
* \param ctrl D channel controller.
*
* \return Default display text send options. (legacy behaviour defaults)
*/
static unsigned long pri_display_options_send_default(struct pri *ctrl)
{
unsigned long flags;
switch (ctrl->switchtype) {
case PRI_SWITCH_QSIG:
flags = PRI_DISPLAY_OPTION_BLOCK;
break;
case PRI_SWITCH_EUROISDN_E1:
case PRI_SWITCH_EUROISDN_T1:
if (ctrl->localtype == PRI_CPE) {
flags = PRI_DISPLAY_OPTION_BLOCK;
break;
}
flags = PRI_DISPLAY_OPTION_NAME_INITIAL;
break;
default:
flags = PRI_DISPLAY_OPTION_NAME_INITIAL;
break;
}
return flags;
}
/*!
* \internal
* \brief Determine the default display text receive options.
*
* \param ctrl D channel controller.
*
* \return Default display text receive options. (legacy behaviour defaults)
*/
static unsigned long pri_display_options_receive_default(struct pri *ctrl)
{
unsigned long flags;
switch (ctrl->switchtype) {
case PRI_SWITCH_QSIG:
flags = PRI_DISPLAY_OPTION_BLOCK;
break;
default:
flags = PRI_DISPLAY_OPTION_NAME_INITIAL;
break;
}
return flags;
}
/*!
* \internal
* \brief Determine the default date/time send option default.
*
* \param ctrl D channel controller.
*
* \return Default date/time send option.
*/
static int pri_date_time_send_default(struct pri *ctrl)
{
int date_time_send;
if (BRI_NT_PTMP(ctrl)) {
date_time_send = PRI_DATE_TIME_SEND_DATE_HHMM;
} else {
date_time_send = PRI_DATE_TIME_SEND_NO;
}
return date_time_send;
}
/*!
* \brief Destroy the given link.
*
* \param link Q.921 link to destroy.
*
* \return Nothing
*/
void pri_link_destroy(struct q921_link *link)
{
if (link) {
struct q931_call *call;
call = link->dummy_call;
if (call) {
pri_schedule_del(call->pri, call->retranstimer);
call->retranstimer = 0;
pri_call_apdu_queue_cleanup(call);
}
free(link);
}
}
/*!
* \internal
* \brief Initialize the layer 2 link structure.
*
* \param ctrl D channel controller.
* \param link Q.921 link to initialize.
* \param sapi SAPI new link is to use.
* \param tei TEI new link is to use.
*
* \note It is assumed that the link has already been memset to zero.
*
* \return Nothing
*/
static void pri_link_init(struct pri *ctrl, struct q921_link *link, int sapi, int tei)
{
link->ctrl = ctrl;
link->sapi = sapi;
link->tei = tei;
}
/*!
* \brief Create a new layer 2 link.
*
* \param ctrl D channel controller.
* \param sapi SAPI new link is to use.
* \param tei TEI new link is to use.
*
* \retval link on success.
* \retval NULL on error.
*/
struct q921_link *pri_link_new(struct pri *ctrl, int sapi, int tei)
{
struct link_dummy *dummy_link;
struct q921_link *link;
switch (ctrl->switchtype) {
case PRI_SWITCH_GR303_EOC:
case PRI_SWITCH_GR303_TMC:
link = calloc(1, sizeof(*link));
if (!link) {
return NULL;
}
dummy_link = NULL;
break;
default:
dummy_link = calloc(1, sizeof(*dummy_link));
if (!dummy_link) {
return NULL;
}
link = &dummy_link->link;
break;
}
pri_link_init(ctrl, link, sapi, tei);
if (dummy_link) {
/* Initialize the dummy call reference call record. */
link->dummy_call = &dummy_link->dummy_call;
q931_init_call_record(link, link->dummy_call, Q931_DUMMY_CALL_REFERENCE);
}
q921_start(link);
return link;
}
/*!
* \internal
* \brief Destroy the given D channel controller.
*
* \param ctrl D channel control to destroy.
*
* \return Nothing
*/
static void pri_ctrl_destroy(struct pri *ctrl)
{
if (ctrl) {
struct q931_call *call;
if (ctrl->link.tei == Q921_TEI_GROUP
&& ctrl->link.sapi == Q921_SAPI_LAYER2_MANAGEMENT
&& ctrl->localtype == PRI_CPE) {
/* This dummy call was borrowed from the specific TEI link. */
call = NULL;
} else {
call = ctrl->link.dummy_call;
}
if (call) {
pri_schedule_del(call->pri, call->retranstimer);
call->retranstimer = 0;
pri_call_apdu_queue_cleanup(call);
}
free(ctrl->msg_line);
free(ctrl->sched.timer);
free(ctrl);
}
}
/*!
* \internal
* \brief Create a new D channel control structure.
*
* \param fd D channel file descriptor if no callback functions supplied.
* \param node Switch NET/CPE type
* \param switchtype ISDN switch type
* \param rd D channel read callback function
* \param wr D channel write callback function
* \param userdata Callback function parameter
* \param tei TEI new link is to use.
* \param bri TRUE if interface is BRI
*
* \retval ctrl on success.
* \retval NULL on error.
*/
static struct pri *pri_ctrl_new(int fd, int node, int switchtype, pri_io_cb rd, pri_io_cb wr, void *userdata, int tei, int bri)
{
int create_dummy_call;
struct d_ctrl_dummy *dummy_ctrl;
struct pri *ctrl;
switch (switchtype) {
case PRI_SWITCH_GR303_EOC:
case PRI_SWITCH_GR303_TMC:
create_dummy_call = 0;
break;
default:
if (bri && node == PRI_CPE && tei == Q921_TEI_GROUP) {
/*
* BRI TE PTMP will not use its own group dummy call record. It
* will use the specific TEI dummy call instead.
*/
create_dummy_call = 0;
} else {
create_dummy_call = 1;
}
break;
}
if (create_dummy_call) {
dummy_ctrl = calloc(1, sizeof(*dummy_ctrl));
if (!dummy_ctrl) {
return NULL;
}
ctrl = &dummy_ctrl->ctrl;
} else {
ctrl = calloc(1, sizeof(*ctrl));
if (!ctrl) {
return NULL;
}
dummy_ctrl = NULL;
}
ctrl->msg_line = calloc(1, sizeof(*ctrl->msg_line));
if (!ctrl->msg_line) {
free(ctrl);
return NULL;
}
ctrl->bri = bri;
ctrl->fd = fd;
ctrl->read_func = rd;
ctrl->write_func = wr;
ctrl->userdata = userdata;
ctrl->localtype = node;
ctrl->switchtype = switchtype;
ctrl->cref = 1;
ctrl->nsf = PRI_NSF_NONE;
ctrl->callpool = &ctrl->localpool;
pri_default_timers(ctrl, switchtype);
ctrl->q921_rxcount = 0;
ctrl->q921_txcount = 0;
ctrl->q931_rxcount = 0;
ctrl->q931_txcount = 0;
ctrl->l2_persistence = pri_l2_persistence_option_default(ctrl);
ctrl->display_flags.send = pri_display_options_send_default(ctrl);
ctrl->display_flags.receive = pri_display_options_receive_default(ctrl);
switch (switchtype) {
case PRI_SWITCH_GR303_EOC:
ctrl->protodisc = GR303_PROTOCOL_DISCRIMINATOR;
pri_link_init(ctrl, &ctrl->link, Q921_SAPI_GR303_EOC, Q921_TEI_GR303_EOC_OPS);
ctrl->link.next = pri_link_new(ctrl, Q921_SAPI_GR303_EOC, Q921_TEI_GR303_EOC_PATH);
if (!ctrl->link.next) {
pri_ctrl_destroy(ctrl);
return NULL;
}
break;
case PRI_SWITCH_GR303_TMC:
ctrl->protodisc = GR303_PROTOCOL_DISCRIMINATOR;
pri_link_init(ctrl, &ctrl->link, Q921_SAPI_GR303_TMC_CALLPROC, Q921_TEI_GR303_TMC_CALLPROC);
ctrl->link.next = pri_link_new(ctrl, Q921_SAPI_GR303_TMC_SWITCHING, Q921_TEI_GR303_TMC_SWITCHING);
if (!ctrl->link.next) {
pri_ctrl_destroy(ctrl);
return NULL;
}
break;
default:
ctrl->protodisc = Q931_PROTOCOL_DISCRIMINATOR;
pri_link_init(ctrl, &ctrl->link,
(tei == Q921_TEI_GROUP) ? Q921_SAPI_LAYER2_MANAGEMENT : Q921_SAPI_CALL_CTRL,
tei);
break;
}
ctrl->date_time_send = pri_date_time_send_default(ctrl);
if (dummy_ctrl) {
/* Initialize the dummy call reference call record. */
ctrl->link.dummy_call = &dummy_ctrl->dummy_call;
q931_init_call_record(&ctrl->link, ctrl->link.dummy_call,
Q931_DUMMY_CALL_REFERENCE);
}
if (ctrl->link.tei == Q921_TEI_GROUP && ctrl->link.sapi == Q921_SAPI_LAYER2_MANAGEMENT
&& ctrl->localtype == PRI_CPE) {
ctrl->link.next = pri_link_new(ctrl, Q921_SAPI_CALL_CTRL, Q921_TEI_PRI);
if (!ctrl->link.next) {
pri_ctrl_destroy(ctrl);
return NULL;
}
/*
* Make the group link use the just created specific TEI link
* dummy call instead. It makes no sense for TE PTMP interfaces
* to broadcast messages on the dummy call or to broadcast any
* messages for that matter.
*/
ctrl->link.dummy_call = ctrl->link.next->dummy_call;
} else {
q921_start(&ctrl->link);
}
return ctrl;
}
void pri_call_set_useruser(q931_call *c, const char *userchars)
{
/*
* There is a slight risk here if c is actually stale. However,
* if it is stale then it is better to catch it here than to
* write with it.
*/
if (!userchars || !pri_is_call_valid(NULL, c)) {
return;
}
libpri_copy_string(c->useruserinfo, userchars, sizeof(c->useruserinfo));
}
void pri_sr_set_useruser(struct pri_sr *sr, const char *userchars)
{
sr->useruserinfo = userchars;
}
int pri_restart(struct pri *pri)
{
/* pri_restart() is no longer needed since the Q.921 rewrite. */
#if 0
/* Restart Q.921 layer */
if (pri) {
q921_reset(pri, 1);
q921_start(pri, pri->localtype == PRI_CPE);
}
#endif
return 0;
}
struct pri *pri_new(int fd, int nodetype, int switchtype)
{
return pri_ctrl_new(fd, nodetype, switchtype, __pri_read, __pri_write, NULL, Q921_TEI_PRI, 0);
}
struct pri *pri_new_bri(int fd, int ptpmode, int nodetype, int switchtype)
{
if (ptpmode)
return pri_ctrl_new(fd, nodetype, switchtype, __pri_read, __pri_write, NULL, Q921_TEI_PRI, 1);
else
return pri_ctrl_new(fd, nodetype, switchtype, __pri_read, __pri_write, NULL, Q921_TEI_GROUP, 1);
}
struct pri *pri_new_cb(int fd, int nodetype, int switchtype, pri_io_cb io_read, pri_io_cb io_write, void *userdata)
{
if (!io_read)
io_read = __pri_read;
if (!io_write)
io_write = __pri_write;
return pri_ctrl_new(fd, nodetype, switchtype, io_read, io_write, userdata, Q921_TEI_PRI, 0);
}
struct pri *pri_new_bri_cb(int fd, int ptpmode, int nodetype, int switchtype, pri_io_cb io_read, pri_io_cb io_write, void *userdata)
{
if (!io_read) {
io_read = __pri_read;
}
if (!io_write) {
io_write = __pri_write;
}
if (ptpmode) {
return pri_ctrl_new(fd, nodetype, switchtype, io_read, io_write, userdata, Q921_TEI_PRI, 1);
} else {
return pri_ctrl_new(fd, nodetype, switchtype, io_read, io_write, userdata, Q921_TEI_GROUP, 1);
}
}
void *pri_get_userdata(struct pri *pri)
{
return pri ? pri->userdata : NULL;
}
void pri_set_userdata(struct pri *pri, void *userdata)
{
if (pri)
pri->userdata = userdata;
}
void pri_set_nsf(struct pri *pri, int nsf)
{
if (pri)
pri->nsf = nsf;
}
char *pri_event2str(int id)
{
unsigned idx;
struct {
int id;
char *name;
} events[] = {
/* *INDENT-OFF* */
{ PRI_EVENT_DCHAN_UP, "PRI_EVENT_DCHAN_UP" },
{ PRI_EVENT_DCHAN_DOWN, "PRI_EVENT_DCHAN_DOWN" },
{ PRI_EVENT_RESTART, "PRI_EVENT_RESTART" },
{ PRI_EVENT_CONFIG_ERR, "PRI_EVENT_CONFIG_ERR" },
{ PRI_EVENT_RING, "PRI_EVENT_RING" },
{ PRI_EVENT_HANGUP, "PRI_EVENT_HANGUP" },
{ PRI_EVENT_RINGING, "PRI_EVENT_RINGING" },
{ PRI_EVENT_ANSWER, "PRI_EVENT_ANSWER" },
{ PRI_EVENT_HANGUP_ACK, "PRI_EVENT_HANGUP_ACK" },
{ PRI_EVENT_RESTART_ACK, "PRI_EVENT_RESTART_ACK" },
{ PRI_EVENT_FACILITY, "PRI_EVENT_FACILITY" },
{ PRI_EVENT_INFO_RECEIVED, "PRI_EVENT_INFO_RECEIVED" },
{ PRI_EVENT_PROCEEDING, "PRI_EVENT_PROCEEDING" },
{ PRI_EVENT_SETUP_ACK, "PRI_EVENT_SETUP_ACK" },
{ PRI_EVENT_HANGUP_REQ, "PRI_EVENT_HANGUP_REQ" },
{ PRI_EVENT_NOTIFY, "PRI_EVENT_NOTIFY" },
{ PRI_EVENT_PROGRESS, "PRI_EVENT_PROGRESS" },
{ PRI_EVENT_KEYPAD_DIGIT, "PRI_EVENT_KEYPAD_DIGIT" },
{ PRI_EVENT_SERVICE, "PRI_EVENT_SERVICE" },
{ PRI_EVENT_SERVICE_ACK, "PRI_EVENT_SERVICE_ACK" },
{ PRI_EVENT_HOLD, "PRI_EVENT_HOLD" },
{ PRI_EVENT_HOLD_ACK, "PRI_EVENT_HOLD_ACK" },
{ PRI_EVENT_HOLD_REJ, "PRI_EVENT_HOLD_REJ" },
{ PRI_EVENT_RETRIEVE, "PRI_EVENT_RETRIEVE" },
{ PRI_EVENT_RETRIEVE_ACK, "PRI_EVENT_RETRIEVE_ACK" },
{ PRI_EVENT_RETRIEVE_REJ, "PRI_EVENT_RETRIEVE_REJ" },
{ PRI_EVENT_CONNECT_ACK, "PRI_EVENT_CONNECT_ACK" },
/* *INDENT-ON* */
};
for (idx = 0; idx < ARRAY_LEN(events); ++idx) {
if (events[idx].id == id) {
return events[idx].name;
}
}
return "Unknown Event";
}
pri_event *pri_check_event(struct pri *pri)
{
char buf[1024];
int res;
pri_event *e;
res = pri->read_func ? pri->read_func(pri, buf, sizeof(buf)) : 0;
if (!res)
return NULL;
/* Receive the q921 packet */
e = q921_receive(pri, (q921_h *)buf, res);
return e;
}
static int wait_pri(struct pri *pri)
{
struct timeval *tv, real;
fd_set fds;
int res;
FD_ZERO(&fds);
FD_SET(pri->fd, &fds);
tv = pri_schedule_next(pri);
if (tv) {
gettimeofday(&real, NULL);
real.tv_sec = tv->tv_sec - real.tv_sec;
real.tv_usec = tv->tv_usec - real.tv_usec;
if (real.tv_usec < 0) {
real.tv_usec += 1000000;
real.tv_sec -= 1;
}
if (real.tv_sec < 0) {
real.tv_sec = 0;
real.tv_usec = 0;
}
}
res = select(pri->fd + 1, &fds, NULL, NULL, tv ? &real : tv);
if (res < 0)
return -1;
return res;
}
pri_event *pri_mkerror(struct pri *pri, char *errstr)
{
/* Return a configuration error */
pri->ev.err.e = PRI_EVENT_CONFIG_ERR;
libpri_copy_string(pri->ev.err.err, errstr, sizeof(pri->ev.err.err));
return &pri->ev;
}
pri_event *pri_dchannel_run(struct pri *pri, int block)
{
pri_event *e;
int res;
if (!pri)
return NULL;
if (block) {
do {
e = NULL;
res = wait_pri(pri);
/* Check for error / interruption */
if (res < 0)
return NULL;
if (!res)
e = pri_schedule_run(pri);
else
e = pri_check_event(pri);
} while(!e);
} else {
e = pri_check_event(pri);
return e;
}
return e;
}
void pri_set_debug(struct pri *pri, int debug)
{
if (!pri)
return;
pri->debug = debug;
}
int pri_get_debug(struct pri *pri)
{
if (!pri)
return -1;
return pri->debug;
}
void pri_facility_enable(struct pri *pri)
{
if (!pri)
return;
pri->sendfacility = 1;
}
int pri_acknowledge(struct pri *pri, q931_call *call, int channel, int info)
{
if (!pri || !pri_is_call_valid(pri, call)) {
return -1;
}
return q931_alerting(pri, call, channel, info);
}
int pri_proceeding(struct pri *pri, q931_call *call, int channel, int info)
{
if (!pri || !pri_is_call_valid(pri, call)) {
return -1;
}
return q931_call_proceeding(pri, call, channel, info);
}
int pri_progress_with_cause(struct pri *pri, q931_call *call, int channel, int info, int cause)
{
if (!pri || !pri_is_call_valid(pri, call)) {
return -1;
}
return q931_call_progress_with_cause(pri, call, channel, info, cause);
}
int pri_progress(struct pri *pri, q931_call *call, int channel, int info)
{
if (!pri || !pri_is_call_valid(pri, call)) {
return -1;
}
return q931_call_progress(pri, call, channel, info);
}
int pri_information(struct pri *pri, q931_call *call, char digit)
{
if (!pri || !pri_is_call_valid(pri, call)) {
return -1;
}
return q931_information(pri, call, digit);
}
int pri_keypad_facility(struct pri *pri, q931_call *call, const char *digits)
{
if (!pri || !pri_is_call_valid(pri, call) || !digits || !digits[0]) {
return -1;
}
return q931_keypad_facility(pri, call, digits);
}
int pri_notify(struct pri *pri, q931_call *call, int channel, int info)
{
if (!pri || !pri_is_call_valid(pri, call)) {
return -1;
}
return q931_notify(pri, call, channel, info);
}
void pri_destroycall(struct pri *pri, q931_call *call)
{
if (pri && pri_is_call_valid(pri, call)) {
q931_destroycall(pri, call);
}
}
int pri_need_more_info(struct pri *pri, q931_call *call, int channel, int nonisdn)
{
if (!pri || !pri_is_call_valid(pri, call)) {
return -1;
}
return q931_setup_ack(pri, call, channel, nonisdn, 0);
}
int pri_setup_ack(struct pri *ctrl, q931_call *call, int channel, int nonisdn, int inband)
{
if (!ctrl || !pri_is_call_valid(ctrl, call)) {
return -1;
}
return q931_setup_ack(ctrl, call, channel, nonisdn, inband);
}
int pri_answer(struct pri *pri, q931_call *call, int channel, int nonisdn)
{
if (!pri || !pri_is_call_valid(pri, call)) {
return -1;
}
return q931_connect(pri, call, channel, nonisdn);
}
int pri_connect_ack(struct pri *ctrl, q931_call *call, int channel)
{
if (!ctrl || !pri_is_call_valid(ctrl, call)) {
return -1;
}
return q931_connect_acknowledge(ctrl, call, channel);
}
void pri_connect_ack_enable(struct pri *ctrl, int enable)
{
if (ctrl) {
ctrl->manual_connect_ack = enable ? 1 : 0;
}
}
/*!
* \brief Copy the PRI party name to the Q.931 party name structure.
*
* \param q931_name Q.931 party name structure
* \param pri_name PRI party name structure
*
* \return Nothing
*/
void pri_copy_party_name_to_q931(struct q931_party_name *q931_name, const struct pri_party_name *pri_name)
{
q931_party_name_init(q931_name);
if (pri_name->valid) {
q931_name->valid = 1;
q931_name->presentation = pri_name->presentation & PRI_PRES_RESTRICTION;
q931_name->char_set = pri_name->char_set;
libpri_copy_string(q931_name->str, pri_name->str, sizeof(q931_name->str));
}
}
/*!
* \brief Copy the PRI party number to the Q.931 party number structure.
*
* \param q931_number Q.931 party number structure
* \param pri_number PRI party number structure
*