File: pri.c

package info (click to toggle)
libpri 1.4.11.3-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,548 kB
  • ctags: 2,921
  • sloc: ansic: 25,580; makefile: 163; sh: 71
file content (1621 lines) | stat: -rw-r--r-- 44,090 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
/*
 * 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"
#include "pri_q921.h"
#include "pri_q931.h"

#define PRI_BIT(a_bit)		(1UL << (a_bit))
#define PRI_ALL_SWITCHES	0xFFFFFFFF

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 },
	{ "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 },
	{ "T313",           PRI_TIMER_T313,             PRI_ALL_SWITCHES },
	{ "T314",           PRI_TIMER_T314,             PRI_ALL_SWITCHES },
	{ "T316",           PRI_TIMER_T316,             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 },
/* *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 == 1)
		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_T202] = 10 * 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_T305] = 30 * 1000;	/* Wait for DISCONNECT acknowledge */
	ctrl->timers[PRI_TIMER_T308] = 4 * 1000;	/* Wait for RELEASE acknowledge */
	ctrl->timers[PRI_TIMER_T313] = 4 * 1000;	/* Wait for CONNECT acknowledge, CPE side only */
	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_T303] = 4 * 1000;			/* Length between SETUP retransmissions and timeout */
	ctrl->timers[PRI_TIMER_T309] = 6000;		/* Time to wait before clearing calls in case of D-channel transient event.  Q.931 specifies 6-90 seconds */

	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. */

	/* Set any switch specific override default values */
	switch (switchtype) {
	default:
		break;
	}
}

int pri_set_timer(struct pri *pri, int timer, int value)
{
	if (timer < 0 || timer > PRI_MAX_TIMERS || value < 0)
		return -1;

	pri->timers[timer] = value;
	return 0;
}

int pri_get_timer(struct pri *pri, int timer)
{
	if (timer < 0 || timer > PRI_MAX_TIMERS)
		return -1;
	return pri->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;
}

void __pri_free_tei(struct pri * p)
{
	if (p) {
		struct q931_call *call;

		call = p->dummy_call;
		if (call) {
			pri_schedule_del(call->pri, call->retranstimer);
			pri_call_apdu_queue_cleanup(call);
		}
		free(p->msg_line);
		free(p);
	}
}

struct pri *__pri_new_tei(int fd, int node, int switchtype, struct pri *master, pri_io_cb rd, pri_io_cb wr, void *userdata, int tei, int bri)
{
	struct d_ctrl_dummy *dummy_ctrl;
	struct pri *p;

	switch (switchtype) {
	case PRI_SWITCH_GR303_EOC:
	case PRI_SWITCH_GR303_TMC:
	case PRI_SWITCH_GR303_TMC_SWITCHING:
	case PRI_SWITCH_GR303_EOC_PATH:
		p = calloc(1, sizeof(*p));
		if (!p) {
			return NULL;
		}
		dummy_ctrl = NULL;
		break;
	default:
		dummy_ctrl = calloc(1, sizeof(*dummy_ctrl));
		if (!dummy_ctrl) {
			return NULL;
		}
		p = &dummy_ctrl->ctrl;
		break;
	}
	if (!master) {
		/* This is the master record. */
		p->msg_line = calloc(1, sizeof(*p->msg_line));
		if (!p->msg_line) {
			free(p);
			return NULL;
		}
	}

	p->bri = bri;
	p->fd = fd;
	p->read_func = rd;
	p->write_func = wr;
	p->userdata = userdata;
	p->localtype = node;
	p->switchtype = switchtype;
	p->cref = 1;
	p->sapi = (tei == Q921_TEI_GROUP) ? Q921_SAPI_LAYER2_MANAGEMENT : Q921_SAPI_CALL_CTRL;
	p->tei = tei;
	p->nsf = PRI_NSF_NONE;
	p->protodisc = Q931_PROTOCOL_DISCRIMINATOR;
	p->master = master;
	p->callpool = &p->localpool;
	pri_default_timers(p, switchtype);
	if (master) {
		pri_set_debug(p, master->debug);
		pri_set_inbanddisconnect(p, master->acceptinbanddisconnect);
		if (master->sendfacility)
			pri_facility_enable(p);
	}
#ifdef LIBPRI_COUNTERS
	p->q921_rxcount = 0;
	p->q921_txcount = 0;
	p->q931_rxcount = 0;
	p->q931_txcount = 0;
#endif
	if (dummy_ctrl) {
		/* Initialize the dummy call reference call record. */
		dummy_ctrl->ctrl.dummy_call = &dummy_ctrl->dummy_call;
		q931_init_call_record(&dummy_ctrl->ctrl, dummy_ctrl->ctrl.dummy_call,
			Q931_DUMMY_CALL_REFERENCE);
	}
	switch (switchtype) {
	case PRI_SWITCH_GR303_EOC:
		p->protodisc = GR303_PROTOCOL_DISCRIMINATOR;
		p->sapi = Q921_SAPI_GR303_EOC;
		p->tei = Q921_TEI_GR303_EOC_OPS;
		p->subchannel = __pri_new_tei(-1, node, PRI_SWITCH_GR303_EOC_PATH, p, NULL, NULL, NULL, Q921_TEI_GR303_EOC_PATH, 0);
		if (!p->subchannel) {
			free(p);
			p = NULL;
		}
		break;
	case PRI_SWITCH_GR303_TMC:
		p->protodisc = GR303_PROTOCOL_DISCRIMINATOR;
		p->sapi = Q921_SAPI_GR303_TMC_CALLPROC;
		p->tei = Q921_TEI_GR303_TMC_CALLPROC;
		p->subchannel = __pri_new_tei(-1, node, PRI_SWITCH_GR303_TMC_SWITCHING, p, NULL, NULL, NULL, Q921_TEI_GR303_TMC_SWITCHING, 0);
		if (!p->subchannel) {
			free(p);
			p = NULL;
		}
		break;
	case PRI_SWITCH_GR303_TMC_SWITCHING:
		p->protodisc = GR303_PROTOCOL_DISCRIMINATOR;
		p->sapi = Q921_SAPI_GR303_TMC_SWITCHING;
		p->tei = Q921_TEI_GR303_TMC_SWITCHING;
		break;
	case PRI_SWITCH_GR303_EOC_PATH:
		p->protodisc = GR303_PROTOCOL_DISCRIMINATOR;
		p->sapi = Q921_SAPI_GR303_EOC;
		p->tei = Q921_TEI_GR303_EOC_PATH;
		break;
	default:
		break;
	}

	if (p->tei == Q921_TEI_GROUP && p->sapi == Q921_SAPI_LAYER2_MANAGEMENT && p->localtype == PRI_CPE) {
		p->subchannel = __pri_new_tei(-1, p->localtype, p->switchtype, p, NULL, NULL, NULL, Q921_TEI_PRI, 1);
		if (!p->subchannel) {
			free(p);
			return NULL;
		}
	} else
		q921_start(p);
	
	return p;
}

void pri_call_set_useruser(q931_call *c, const char *userchars)
{
	if (userchars)
		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_new_tei(fd, nodetype, switchtype, NULL, __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_new_tei(fd, nodetype, switchtype, NULL, __pri_read, __pri_write, NULL, Q921_TEI_PRI, 1);
	else
		return __pri_new_tei(fd, nodetype, switchtype, NULL, __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_new_tei(fd, nodetype, switchtype, NULL, io_read, io_write, userdata, Q921_TEI_PRI, 0);
}

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,       "D-Channel Up" },
		{ PRI_EVENT_DCHAN_DOWN,     "D-channel Down" },
		{ PRI_EVENT_RESTART,        "Restart channel" },
		{ PRI_EVENT_CONFIG_ERR,     "Configuration Error" },
		{ PRI_EVENT_RING,           "Ring" },
		{ PRI_EVENT_HANGUP,         "Hangup" },
		{ PRI_EVENT_RINGING,        "Ringing" },
		{ PRI_EVENT_ANSWER,         "Answer" },
		{ PRI_EVENT_HANGUP_ACK,     "Hangup ACK" },
		{ PRI_EVENT_RESTART_ACK,    "Restart ACK" },
		{ PRI_EVENT_FACILITY,       "Facility" },
		{ PRI_EVENT_INFO_RECEIVED,  "Info Received" },
		{ PRI_EVENT_PROCEEDING,     "Proceeding" },
		{ PRI_EVENT_SETUP_ACK,      "Setup ACK" },
		{ PRI_EVENT_HANGUP_REQ,     "Hangup Req" },
		{ PRI_EVENT_NOTIFY,         "Notify" },
		{ PRI_EVENT_PROGRESS,       "Progress" },
		{ PRI_EVENT_KEYPAD_DIGIT,   "Keypad Digit" },
		{ PRI_EVENT_SERVICE,        "Service" },
		{ PRI_EVENT_SERVICE_ACK,    "Service ACK" },
		{ PRI_EVENT_HOLD,           "Hold" },
		{ PRI_EVENT_HOLD_ACK,       "Hold Ack" },
		{ PRI_EVENT_HOLD_REJ,       "Hold Rej" },
		{ PRI_EVENT_RETRIEVE,       "Retrieve" },
		{ PRI_EVENT_RETRIEVE_ACK,   "Retrieve ACK" },
		{ PRI_EVENT_RETRIEVE_REJ,   "Retrieve Rej" },
/* *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;
	if (pri->subchannel)
		pri_set_debug(pri->subchannel, debug);
}

int pri_get_debug(struct pri *pri)
{
	if (!pri)
		return -1;
	if (pri->subchannel)
		return pri_get_debug(pri->subchannel);
	return pri->debug;
}

void pri_facility_enable(struct pri *pri)
{
	if (!pri)
		return;
	pri->sendfacility = 1;
	if (pri->subchannel)
		pri_facility_enable(pri->subchannel);
	return;
}

int pri_acknowledge(struct pri *pri, q931_call *call, int channel, int info)
{
	if (!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 || !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 || !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 || !call)
		return -1;

	return q931_call_progress(pri, call, channel, info);
}

int pri_information(struct pri *pri, q931_call *call, char digit)
{
	if (!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 || !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 || !call)
		return -1;
	return q931_notify(pri, call, channel, info);
}

void pri_destroycall(struct pri *pri, q931_call *call)
{
	if (pri && call)
		q931_destroycall(pri, call);
	return;
}

int pri_need_more_info(struct pri *pri, q931_call *call, int channel, int nonisdn)
{
	if (!pri || !call)
		return -1;
	return q931_setup_ack(pri, call, channel, nonisdn);
}

int pri_answer(struct pri *pri, q931_call *call, int channel, int nonisdn)
{
	if (!pri || !call)
		return -1;
	return q931_connect(pri, call, channel, nonisdn);
}

/*!
 * \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;
		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
 *
 * \return Nothing
 */
void pri_copy_party_number_to_q931(struct q931_party_number *q931_number, const struct pri_party_number *pri_number)
{
	q931_party_number_init(q931_number);
	if (pri_number->valid) {
		q931_number->valid = 1;
		q931_number->presentation = pri_number->presentation;
		q931_number->plan = pri_number->plan;
		libpri_copy_string(q931_number->str, pri_number->str, sizeof(q931_number->str));
	}
}

/*!
 * \brief Copy the PRI party subaddress to the Q.931 party subaddress structure.
 *
 * \param q931_subaddress Q.931 party subaddress structure
 * \param pri_subaddress PRI party subaddress structure
 *
 * \return Nothing
 */
void pri_copy_party_subaddress_to_q931(struct q931_party_subaddress *q931_subaddress, const struct pri_party_subaddress *pri_subaddress)
{
	int length;
	int maxlen = sizeof(q931_subaddress->data) - 1;

	q931_party_subaddress_init(q931_subaddress);

	if (!pri_subaddress->valid) {
		return;
	}

	q931_subaddress->valid = 1;
	q931_subaddress->type = pri_subaddress->type;

	length = pri_subaddress->length;
	if (length > maxlen){
		length = maxlen;
	} else {
		q931_subaddress->odd_even_indicator = pri_subaddress->odd_even_indicator;
	}
	q931_subaddress->length = length;
	memcpy(q931_subaddress->data, pri_subaddress->data, length);
	q931_subaddress->data[length] = '\0';
}

/*!
 * \brief Copy the PRI party id to the Q.931 party id structure.
 *
 * \param q931_id Q.931 party id structure
 * \param pri_id PRI party id structure
 *
 * \return Nothing
 */
void pri_copy_party_id_to_q931(struct q931_party_id *q931_id, const struct pri_party_id *pri_id)
{
	pri_copy_party_name_to_q931(&q931_id->name, &pri_id->name);
	pri_copy_party_number_to_q931(&q931_id->number, &pri_id->number);
	pri_copy_party_subaddress_to_q931(&q931_id->subaddress, &pri_id->subaddress);
}

int pri_connected_line_update(struct pri *ctrl, q931_call *call, const struct pri_party_connected_line *connected)
{
	struct q931_party_id party_id;
	unsigned idx;
	struct q931_call *subcall;

	if (!ctrl || !call) {
		return -1;
	}

	pri_copy_party_id_to_q931(&party_id, &connected->id);
	q931_party_id_fixup(ctrl, &party_id);
	if (!q931_party_id_cmp(&party_id, &call->local_id)) {
		/* The local party information did not change so do nothing. */
		return 0;
	}
	call->local_id = party_id;

	/* Update all subcalls with new local_id. */
	if (call->outboundbroadcast && call->master_call == call) {
		for (idx = 0; idx < Q931_MAX_TEI; ++idx) {
			subcall = call->subcalls[idx];
			if (subcall) {
				subcall->local_id = party_id;
			}
		}
	}

	switch (call->ourcallstate) {
	case Q931_CALL_STATE_CALL_INITIATED:
	case Q931_CALL_STATE_OVERLAP_SENDING:
	case Q931_CALL_STATE_OUTGOING_CALL_PROCEEDING:
	case Q931_CALL_STATE_CALL_DELIVERED:
		/*
		 * The local party transferred to someone else before
		 * the remote end answered.
		 */
	case Q931_CALL_STATE_ACTIVE:
		switch (ctrl->switchtype) {
		case PRI_SWITCH_EUROISDN_E1:
		case PRI_SWITCH_EUROISDN_T1:
			if (PTMP_MODE(ctrl)) {
				/* PTMP mode */
				q931_notify_redirection(ctrl, call, PRI_NOTIFY_TRANSFER_ACTIVE,
					&call->local_id.number);
			} else {
				/* PTP mode */
				/* Immediately send EctInform APDU, callStatus=answered(0) */
				send_call_transfer_complete(ctrl, call, 0);
			}
			break;
		case PRI_SWITCH_QSIG:
			/* Immediately send CallTransferComplete APDU, callStatus=answered(0) */
			send_call_transfer_complete(ctrl, call, 0);
			break;
		default:
			break;
		}
		break;
	default:
		/* Just save the data for further developments. */
		break;
	}

	return 0;
}

int pri_redirecting_update(struct pri *ctrl, q931_call *call, const struct pri_party_redirecting *redirecting)
{
	unsigned idx;
	struct q931_call *subcall;

	if (!ctrl || !call) {
		return -1;
	}

	/* Save redirecting.to information and reason. */
	pri_copy_party_id_to_q931(&call->redirecting.to, &redirecting->to);
	q931_party_id_fixup(ctrl, &call->redirecting.to);
	call->redirecting.reason = redirecting->reason;

	/*
	 * Update all subcalls with new redirecting.to information and reason.
	 * I do not think we will ever have any subcalls when this data is relevant,
	 * but update it just in case.
	 */
	if (call->outboundbroadcast && call->master_call == call) {
		for (idx = 0; idx < Q931_MAX_TEI; ++idx) {
			subcall = call->subcalls[idx];
			if (subcall) {
				subcall->redirecting.to = call->redirecting.to;
				subcall->redirecting.reason = redirecting->reason;
			}
		}
	}

	switch (call->ourcallstate) {
	case Q931_CALL_STATE_NULL:
		/* Save the remaining redirecting information before we place a call. */
		pri_copy_party_id_to_q931(&call->redirecting.from, &redirecting->from);
		q931_party_id_fixup(ctrl, &call->redirecting.from);
		pri_copy_party_id_to_q931(&call->redirecting.orig_called, &redirecting->orig_called);
		q931_party_id_fixup(ctrl, &call->redirecting.orig_called);
		call->redirecting.orig_reason = redirecting->orig_reason;
		if (redirecting->count <= 0) {
			if (call->redirecting.from.number.valid) {
				/*
				 * We are redirecting with an unknown count
				 * so assume the count is one.
				 */
				call->redirecting.count = 1;
			} else {
				call->redirecting.count = 0;
			}
		} else if (redirecting->count < PRI_MAX_REDIRECTS) {
			call->redirecting.count = redirecting->count;
		} else {
			call->redirecting.count = PRI_MAX_REDIRECTS;
		}
		break;
	case Q931_CALL_STATE_OVERLAP_RECEIVING:
	case Q931_CALL_STATE_INCOMING_CALL_PROCEEDING:
	case Q931_CALL_STATE_CALL_RECEIVED:
		/* This is an incoming call that has not connected yet. */
		if (!call->redirecting.to.number.valid) {
			/* Not being redirected toward valid number data. Ignore. */
			break;
		}

		switch (ctrl->switchtype) {
		case PRI_SWITCH_EUROISDN_E1:
		case PRI_SWITCH_EUROISDN_T1:
			if (PTMP_MODE(ctrl)) {
				/* PTMP mode */
				q931_notify_redirection(ctrl, call, PRI_NOTIFY_CALL_DIVERTING,
					&call->redirecting.to.number);
				break;
			}
			/* PTP mode - same behaviour as Q.SIG */
			/* fall through */
		case PRI_SWITCH_QSIG:
			if (call->redirecting.state != Q931_REDIRECTING_STATE_PENDING_TX_DIV_LEG_3
				|| strcmp(call->redirecting.to.number.str, call->called.number.str) != 0) {
				/* immediately send divertingLegInformation1 APDU */
				if (rose_diverting_leg_information1_encode(ctrl, call)
					|| q931_facility(ctrl, call)) {
					pri_message(ctrl,
						"Could not schedule facility message for divertingLegInfo1\n");
				}
			}
			call->redirecting.state = Q931_REDIRECTING_STATE_IDLE;

			/* immediately send divertingLegInformation3 APDU */
			if (rose_diverting_leg_information3_encode(ctrl, call, Q931_FACILITY)
				|| q931_facility(ctrl, call)) {
				pri_message(ctrl,
					"Could not schedule facility message for divertingLegInfo3\n");
			}
			break;
		default:
			break;
		}
		break;
	default:
		pri_message(ctrl, "Ignored redirecting update because call in state %s(%d).\n",
			q931_call_state_str(call->ourcallstate), call->ourcallstate);
		break;
	}

	return 0;
}

#if 0
/* deprecated routines, use pri_hangup */
int pri_release(struct pri *pri, q931_call *call, int cause)
{
	if (!pri || !call)
		return -1;
	return q931_release(pri, call, cause);
}

int pri_disconnect(struct pri *pri, q931_call *call, int cause)
{
	if (!pri || !call)
		return -1;
	return q931_disconnect(pri, call, cause);
}
#endif

int pri_channel_bridge(q931_call *call1, q931_call *call2)
{
	if (!call1 || !call2)
		return -1;

	/* Make sure we have compatible switchtypes */
	if (call1->pri->switchtype != call2->pri->switchtype)
		return -1;

	/* Check for bearer capability */
	if (call1->transcapability != call2->transcapability)
		return -1;

	/* Check to see if we're on the same PRI */
	if (call1->pri != call2->pri)
		return -1;
	
	switch (call1->pri->switchtype) {
		case PRI_SWITCH_NI2:
		case PRI_SWITCH_LUCENT5E:
		case PRI_SWITCH_ATT4ESS:
			if (eect_initiate_transfer(call1->pri, call1, call2))
				return -1;
			else
				return 0;
			break;
		case PRI_SWITCH_DMS100:
			if (rlt_initiate_transfer(call1->pri, call1, call2))
				return -1;
			else
				return 0;
			break;
		case PRI_SWITCH_QSIG:
			call1->bridged_call = call2;
			call2->bridged_call = call1;
			if (anfpr_initiate_transfer(call1->pri, call1, call2))
				return -1;
			else
				return 0;
			break;
		default:
			return -1;
	}
}

void pri_hangup_fix_enable(struct pri *ctrl, int enable)
{
	if (ctrl) {
		ctrl = PRI_MASTER(ctrl);
		ctrl->hangup_fix_enabled = enable ? 1 : 0;
	}
}

int pri_hangup(struct pri *pri, q931_call *call, int cause)
{
	if (!pri || !call)
		return -1;
	if (cause == -1)
		/* normal clear cause */
		cause = PRI_CAUSE_NORMAL_CLEARING;
	return q931_hangup(pri, call, cause);
}

int pri_reset(struct pri *pri, int channel)
{
	if (!pri)
		return -1;
	return q931_restart(pri, channel);
}

int pri_maintenance_service(struct pri *pri, int span, int channel, int changestatus)
{
	if (!pri) {
		return -1;
	}
	return maintenance_service(pri, span, channel, changestatus);
}

q931_call *pri_new_call(struct pri *pri)
{
	if (!pri)
		return NULL;
	return q931_new_call(pri);
}

int pri_is_dummy_call(q931_call *call)
{
	if (!call) {
		return 0;
	}
	return q931_is_dummy_call(call);
}

void pri_dump_event(struct pri *pri, pri_event *e)
{
	if (!pri || !e)
		return;
	pri_message(pri, "Event type: %s (%d)\n", pri_event2str(e->gen.e), e->gen.e);
	switch(e->gen.e) {
	case PRI_EVENT_DCHAN_UP:
	case PRI_EVENT_DCHAN_DOWN:
		break;
	case PRI_EVENT_CONFIG_ERR:
		pri_message(pri, "Error: %s", e->err.err);
		break;
	case PRI_EVENT_RESTART:
		pri_message(pri, "Restart on channel %d\n", e->restart.channel);
	case PRI_EVENT_RING:
		pri_message(pri, "Calling number: %s (%s, %s)\n", e->ring.callingnum, pri_plan2str(e->ring.callingplan), pri_pres2str(e->ring.callingpres));
		pri_message(pri, "Called number: %s (%s)\n", e->ring.callednum, pri_plan2str(e->ring.calledplan));
		pri_message(pri, "Channel: %d (%s) Reference number: %d\n", e->ring.channel, e->ring.flexible ? "Flexible" : "Not Flexible", e->ring.cref);
		break;
	case PRI_EVENT_HANGUP:
		pri_message(pri, "Hangup, reference number: %d, reason: %s\n", e->hangup.cref, pri_cause2str(e->hangup.cause));
		break;
	default:
		pri_message(pri, "Don't know how to dump events of type %d\n", e->gen.e);
	}
}

static void pri_sr_init(struct pri_sr *req)
{
	memset(req, 0, sizeof(struct pri_sr));
	q931_party_redirecting_init(&req->redirecting);
	q931_party_id_init(&req->caller);
	q931_party_address_init(&req->called);
	req->reversecharge = PRI_REVERSECHARGE_NONE;
}

int pri_sr_set_connection_call_independent(struct pri_sr *req)
{
	if (!req)
		return -1;

	req->cis_call = 1; /* have to set cis_call for all those pesky IEs we need to setup */
	req->cis_auto_disconnect = 1;
	return 0;
}

int pri_sr_set_no_channel_call(struct pri_sr *req)
{
	if (!req) {
		return -1;
	}

	req->cis_call = 1;
	return 0;
}

/* Don't call any other pri functions on this */
int pri_mwi_activate(struct pri *pri, q931_call *c, char *caller, int callerplan, char *callername, int callerpres, char *called,
					int calledplan)
{
	struct pri_sr req;
	if (!pri || !c)
		return -1;

	pri_sr_init(&req);
	pri_sr_set_connection_call_independent(&req);
	pri_sr_set_caller(&req, caller, callername, callerplan, callerpres);
	pri_sr_set_called(&req, called, calledplan, 0);

	if (mwi_message_send(pri, c, &req, 1) < 0) {
		pri_message(pri, "Unable to send MWI activate message\n");
		return -1;
	}
	/* Do more stuff when we figure out that the CISC stuff works */
	return q931_setup(pri, c, &req);
}

int pri_mwi_deactivate(struct pri *pri, q931_call *c, char *caller, int callerplan, char *callername, int callerpres, char *called,
					int calledplan)
{
	struct pri_sr req;
	if (!pri || !c)
		return -1;

	pri_sr_init(&req);
	pri_sr_set_connection_call_independent(&req);
	pri_sr_set_caller(&req, caller, callername, callerplan, callerpres);
	pri_sr_set_called(&req, called, calledplan, 0);

	if(mwi_message_send(pri, c, &req, 0) < 0) {
		pri_message(pri, "Unable to send MWI deactivate message\n");
		return -1;
	}

	return q931_setup(pri, c, &req);
}
	
int pri_setup(struct pri *pri, q931_call *c, struct pri_sr *req)
{
	if (!pri || !c)
		return -1;

	return q931_setup(pri, c, req);
}

int pri_call(struct pri *pri, q931_call *c, int transmode, int channel, int exclusive, 
					int nonisdn, char *caller, int callerplan, char *callername, int callerpres, char *called,
					int calledplan, int ulayer1)
{
	struct pri_sr req;
	if (!pri || !c)
		return -1;
	pri_sr_init(&req);
	pri_sr_set_caller(&req, caller, callername, callerplan, callerpres);
	pri_sr_set_called(&req, called, calledplan, 0);
	req.transmode = transmode;
	req.channel = channel;
	req.exclusive = exclusive;
	req.nonisdn =  nonisdn;
	req.userl1 = ulayer1;
	return q931_setup(pri, c, &req);
}	

static void (*__pri_error)(struct pri *pri, char *stuff);
static void (*__pri_message)(struct pri *pri, char *stuff);

void pri_set_message(void (*func)(struct pri *pri, char *stuff))
{
	__pri_message = func;
}

void pri_set_error(void (*func)(struct pri *pri, char *stuff))
{
	__pri_error = func;
}

static void pri_old_message(struct pri *ctrl, const char *fmt, va_list *ap)
{
	char tmp[1024];

	vsnprintf(tmp, sizeof(tmp), fmt, *ap);
	if (__pri_message)
		__pri_message(ctrl, tmp);
	else
		fputs(tmp, stdout);
}

void pri_message(struct pri *ctrl, const char *fmt, ...)
{
	int added_length;
	va_list ap;

	if (ctrl) {
		ctrl = PRI_MASTER(ctrl);
	}
	if (!ctrl || !ctrl->msg_line) {
		/* Just have to do it the old way. */
		va_start(ap, fmt);
		pri_old_message(ctrl, fmt, &ap);
		va_end(ap);
		return;
	}

	va_start(ap, fmt);
	added_length = vsnprintf(ctrl->msg_line->str + ctrl->msg_line->length,
		sizeof(ctrl->msg_line->str) - ctrl->msg_line->length, fmt, ap);
	va_end(ap);
	if (added_length < 0
		|| sizeof(ctrl->msg_line->str) <= ctrl->msg_line->length + added_length) {
		static char truncated_output[] =
			"v-- Error building output or output was truncated. (Next line) --v\n";

		/*
		 * This clause should never need to run because the
		 * output line accumulation buffer is quite large.
		 */

		/* vsnprintf() error or output string was truncated. */
		if (__pri_message) {
			__pri_message(ctrl, truncated_output);
		} else {
			fputs(truncated_output, stdout);
		}

		/* Add a terminating '\n' to force a flush of the line. */
		ctrl->msg_line->length = strlen(ctrl->msg_line->str);
		if (ctrl->msg_line->length) {
			ctrl->msg_line->str[ctrl->msg_line->length - 1] = '\n';
		} else {
			ctrl->msg_line->str[0] = '\n';
			ctrl->msg_line->str[1] = '\0';
		}
	} else {
		ctrl->msg_line->length += added_length;
	}

	if (ctrl->msg_line->length
		&& ctrl->msg_line->str[ctrl->msg_line->length - 1] == '\n') {
		/* The accumulated output line was terminated so send it out. */
		ctrl->msg_line->length = 0;
		if (__pri_message) {
			__pri_message(ctrl, ctrl->msg_line->str);
		} else {
			fputs(ctrl->msg_line->str, stdout);
		}
	}
}

void pri_error(struct pri *pri, const char *fmt, ...)
{
	char tmp[1024];
	va_list ap;
	va_start(ap, fmt);
	vsnprintf(tmp, sizeof(tmp), fmt, ap);
	va_end(ap);
	if (__pri_error)
		__pri_error(pri ? PRI_MASTER(pri) : NULL, tmp);
	else
		fputs(tmp, stderr);
}

/* Set overlap mode */
void pri_set_overlapdial(struct pri *pri,int state)
{
	if (pri) {
		pri->overlapdial = state ? 1 : 0;
	}
}

void pri_set_chan_mapping_logical(struct pri *pri, int state)
{
	if (pri && pri->switchtype == PRI_SWITCH_QSIG) {
		pri->chan_mapping_logical = state ? 1 : 0;
	}
}

void pri_set_inbanddisconnect(struct pri *pri, unsigned int enable)
{
	if (pri) {
		pri->acceptinbanddisconnect = (enable != 0);
	}
}

int pri_fd(struct pri *pri)
{
	return pri->fd;
}

/*!
 * \internal
 * \brief Append snprintf output to the given buffer.
 *
 * \param buf Buffer currently filling.
 * \param buf_used Offset into buffer where to put new stuff.
 * \param buf_size Actual buffer size of buf.
 * \param format printf format string.
 *
 * \return Total buffer space used.
 */
static size_t pri_snprintf(char *buf, size_t buf_used, size_t buf_size, const char *format, ...) __attribute__((format(printf, 4, 5)));
static size_t pri_snprintf(char *buf, size_t buf_used, size_t buf_size, const char *format, ...)
{
	va_list args;

	if (buf_used < buf_size) {
		va_start(args, format);
		buf_used += vsnprintf(buf + buf_used, buf_size - buf_used, format, args);
		va_end(args);
	}
	if (buf_size < buf_used) {
		buf_used = buf_size + 1;
	}
	return buf_used;
}

char *pri_dump_info_str(struct pri *ctrl)
{
	char *buf;
	size_t buf_size;
	size_t used;
#ifdef LIBPRI_COUNTERS
	struct q921_frame *f;
	unsigned q921outstanding;
#endif
	unsigned idx;
	unsigned long switch_bit;

	if (!ctrl) {
		return NULL;
	}

	buf_size = 4096;	/* This should be bigger than we will ever need. */
	buf = malloc(buf_size);
	if (!buf) {
		return NULL;
	}

	/* Might be nice to format these a little better */
	used = 0;
	used = pri_snprintf(buf, used, buf_size, "Switchtype: %s\n",
		pri_switch2str(ctrl->switchtype));
	used = pri_snprintf(buf, used, buf_size, "Type: %s\n", pri_node2str(ctrl->localtype));
#ifdef LIBPRI_COUNTERS
	/* Remember that Q921 Counters include Q931 packets (and any retransmissions) */
	used = pri_snprintf(buf, used, buf_size, "Q931 RX: %d\n", ctrl->q931_rxcount);
	used = pri_snprintf(buf, used, buf_size, "Q931 TX: %d\n", ctrl->q931_txcount);
	used = pri_snprintf(buf, used, buf_size, "Q921 RX: %d\n", ctrl->q921_rxcount);
	used = pri_snprintf(buf, used, buf_size, "Q921 TX: %d\n", ctrl->q921_txcount);
	q921outstanding = 0;
	f = ctrl->txqueue;
	while (f) {
		q921outstanding++;
		f = f->next;
	}
	used = pri_snprintf(buf, used, buf_size, "Q921 Outstanding: %u\n", q921outstanding);
#endif
#if 0
	used = pri_snprintf(buf, used, buf_size, "Window Length: %d/%d\n",
		ctrl->timers[PRI_TIMER_K], ctrl->window);
	used = pri_snprintf(buf, used, buf_size, "Sentrej: %d\n", ctrl->sentrej);
	used = pri_snprintf(buf, used, buf_size, "SolicitFbit: %d\n", ctrl->solicitfbit);
	used = pri_snprintf(buf, used, buf_size, "Retrans: %d\n", ctrl->retrans);
	used = pri_snprintf(buf, used, buf_size, "Busy: %d\n", ctrl->busy);
#endif
	used = pri_snprintf(buf, used, buf_size, "Overlap Dial: %d\n", ctrl->overlapdial);
	used = pri_snprintf(buf, used, buf_size, "Logical Channel Mapping: %d\n",
		ctrl->chan_mapping_logical);
	used = pri_snprintf(buf, used, buf_size, "Timer and counter settings:\n");
	switch_bit = PRI_BIT(ctrl->switchtype);
	for (idx = 0; idx < ARRAY_LEN(pri_timer); ++idx) {
		if (pri_timer[idx].used_by & switch_bit) {
			enum PRI_TIMERS_AND_COUNTERS tmr;

			tmr = pri_timer[idx].number;
			if (0 <= ctrl->timers[tmr] || tmr == PRI_TIMER_T309) {
				used = pri_snprintf(buf, used, buf_size, "  %s: %d\n",
					pri_timer[idx].name, ctrl->timers[tmr]);
			}
		}
	}

	if (buf_size < used) {
		pri_message(ctrl,
			"pri_dump_info_str(): Produced output exceeded buffer capacity. (Truncated)\n");
	}
	return buf;
}

int pri_get_crv(struct pri *pri, q931_call *call, int *callmode)
{
	return q931_call_getcrv(pri, call, callmode);
}

int pri_set_crv(struct pri *pri, q931_call *call, int crv, int callmode)
{
	return q931_call_setcrv(pri, call, crv, callmode);
}

void pri_enslave(struct pri *master, struct pri *slave)
{
	if (master && slave)
		slave->callpool = &master->localpool;
}

struct pri_sr *pri_sr_new(void)
{
	struct pri_sr *req;
	req = malloc(sizeof(*req));
	if (req) 
		pri_sr_init(req);
	return req;
}

void pri_sr_free(struct pri_sr *sr)
{
	free(sr);
}

int pri_sr_set_channel(struct pri_sr *sr, int channel, int exclusive, int nonisdn)
{
	sr->channel = channel;
	sr->exclusive = exclusive;
	sr->nonisdn = nonisdn;
	return 0;
}

int pri_sr_set_bearer(struct pri_sr *sr, int transmode, int userl1)
{
	sr->transmode = transmode;
	sr->userl1 = userl1;
	return 0;
}

int pri_sr_set_called(struct pri_sr *sr, char *called, int calledplan, int numcomplete)
{
	q931_party_address_init(&sr->called);
	if (called) {
		sr->called.number.valid = 1;
		sr->called.number.plan = calledplan;
		libpri_copy_string(sr->called.number.str, called, sizeof(sr->called.number.str));
	}
	sr->numcomplete = numcomplete;
	return 0;
}

void pri_sr_set_called_subaddress(struct pri_sr *sr, const struct pri_party_subaddress *subaddress)
{
	pri_copy_party_subaddress_to_q931(&sr->called.subaddress, subaddress);
}

int pri_sr_set_caller(struct pri_sr *sr, char *caller, char *callername, int callerplan, int callerpres)
{
	q931_party_id_init(&sr->caller);
	if (caller) {
		sr->caller.number.valid = 1;
		sr->caller.number.presentation = callerpres;
		sr->caller.number.plan = callerplan;
		libpri_copy_string(sr->caller.number.str, caller, sizeof(sr->caller.number.str));

		if (callername) {
			sr->caller.name.valid = 1;
			sr->caller.name.presentation = callerpres;
			sr->caller.name.char_set = PRI_CHAR_SET_ISO8859_1;
			libpri_copy_string(sr->caller.name.str, callername,
				sizeof(sr->caller.name.str));
		}
	}
	return 0;
}

void pri_sr_set_caller_subaddress(struct pri_sr *sr, const struct pri_party_subaddress *subaddress)
{
	pri_copy_party_subaddress_to_q931(&sr->caller.subaddress, subaddress);
}

void pri_sr_set_caller_party(struct pri_sr *sr, const struct pri_party_id *caller)
{
	pri_copy_party_id_to_q931(&sr->caller, caller);
}

int pri_sr_set_redirecting(struct pri_sr *sr, char *num, int plan, int pres, int reason)
{
	q931_party_redirecting_init(&sr->redirecting);
	if (num && num[0]) {
		sr->redirecting.from.number.valid = 1;
		sr->redirecting.from.number.presentation = pres;
		sr->redirecting.from.number.plan = plan;
		libpri_copy_string(sr->redirecting.from.number.str, num,
			sizeof(sr->redirecting.from.number.str));

		sr->redirecting.count = 1;
		sr->redirecting.reason = reason;
	}
	return 0;
}

void pri_sr_set_redirecting_parties(struct pri_sr *sr, const struct pri_party_redirecting *redirecting)
{
	pri_copy_party_id_to_q931(&sr->redirecting.from, &redirecting->from);
	pri_copy_party_id_to_q931(&sr->redirecting.to, &redirecting->to);
	pri_copy_party_id_to_q931(&sr->redirecting.orig_called, &redirecting->orig_called);
	sr->redirecting.orig_reason = redirecting->orig_reason;
	sr->redirecting.reason = redirecting->reason;
	if (redirecting->count <= 0) {
		if (sr->redirecting.from.number.valid) {
			/*
			 * We are redirecting with an unknown count
			 * so assume the count is one.
			 */
			sr->redirecting.count = 1;
		} else {
			sr->redirecting.count = 0;
		}
	} else if (redirecting->count < PRI_MAX_REDIRECTS) {
		sr->redirecting.count = redirecting->count;
	} else {
		sr->redirecting.count = PRI_MAX_REDIRECTS;
	}
}

void pri_sr_set_reversecharge(struct pri_sr *sr, int requested)
{
	sr->reversecharge = requested;
}

void pri_sr_set_keypad_digits(struct pri_sr *sr, const char *keypad_digits)
{
	sr->keypad_digits = keypad_digits;
}

void pri_hold_enable(struct pri *ctrl, int enable)
{
	if (ctrl) {
		ctrl = PRI_MASTER(ctrl);
		ctrl->hold_support = enable ? 1 : 0;
	}
}

int pri_hold(struct pri *ctrl, q931_call *call)
{
	if (!ctrl || !call) {
		return -1;
	}
	return q931_send_hold(ctrl, call);
}

int pri_hold_ack(struct pri *ctrl, q931_call *call)
{
	if (!ctrl || !call) {
		return -1;
	}
	return q931_send_hold_ack(ctrl, call);
}

int pri_hold_rej(struct pri *ctrl, q931_call *call, int cause)
{
	if (!ctrl || !call) {
		return -1;
	}
	return q931_send_hold_rej(ctrl, call, cause);
}

int pri_retrieve(struct pri *ctrl, q931_call *call, int channel)
{
	if (!ctrl || !call) {
		return -1;
	}
	return q931_send_retrieve(ctrl, call, channel);
}

int pri_retrieve_ack(struct pri *ctrl, q931_call *call, int channel)
{
	if (!ctrl || !call) {
		return -1;
	}
	return q931_send_retrieve_ack(ctrl, call, channel);
}

int pri_retrieve_rej(struct pri *ctrl, q931_call *call, int cause)
{
	if (!ctrl || !call) {
		return -1;
	}
	return q931_send_retrieve_rej(ctrl, call, cause);
}

int pri_callrerouting_facility(struct pri *pri, q931_call *call, const char *dest, const char* original, const char* reason)
{
	if (!pri || !call || !dest)
		return -1;

	return qsig_cf_callrerouting(pri, call, dest, original, reason);
}

void pri_reroute_enable(struct pri *ctrl, int enable)
{
	if (ctrl) {
		ctrl = PRI_MASTER(ctrl);
		ctrl->deflection_support = enable ? 1 : 0;
	}
}

int pri_reroute_call(struct pri *ctrl, q931_call *call, const struct pri_party_id *caller, const struct pri_party_redirecting *deflection, int subscription_option)
{
	const struct q931_party_id *caller_id;
	struct q931_party_id local_caller;
	struct q931_party_redirecting reroute;

	if (!ctrl || !call || !deflection) {
		return -1;
	}

	if (caller) {
		/* Convert the caller update information. */
		pri_copy_party_id_to_q931(&local_caller, caller);
		q931_party_id_fixup(ctrl, &local_caller);
		caller_id = &local_caller;
	} else {
		caller_id = NULL;
	}

	/* Convert the deflection information. */
	q931_party_redirecting_init(&reroute);
	pri_copy_party_id_to_q931(&reroute.from, &deflection->from);
	q931_party_id_fixup(ctrl, &reroute.from);
	pri_copy_party_id_to_q931(&reroute.to, &deflection->to);
	q931_party_id_fixup(ctrl, &reroute.to);
	pri_copy_party_id_to_q931(&reroute.orig_called, &deflection->orig_called);
	q931_party_id_fixup(ctrl, &reroute.orig_called);
	reroute.reason = deflection->reason;
	reroute.orig_reason = deflection->orig_reason;
	if (deflection->count <= 0) {
		/*
		 * We are deflecting with an unknown count
		 * so assume the count is one.
		 */
		reroute.count = 1;
	} else if (deflection->count < PRI_MAX_REDIRECTS) {
		reroute.count = deflection->count;
	} else {
		reroute.count = PRI_MAX_REDIRECTS;
	}

	return send_reroute_request(ctrl, call, caller_id, &reroute, subscription_option);
}