summaryrefslogtreecommitdiffstats
path: root/interface.c
blob: 57d7fe89bb442924fe8a68b8e0e7ee916deb20d0 (plain) (blame)
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
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
/*****************************************************************************\
**                                                                           **
** LCR                                                                       **
**                                                                           **
**---------------------------------------------------------------------------**
** Copyright: Andreas Eversberg                                              **
**                                                                           **
** reading interface.conf file and filling structure                         **
**                                                                           **
\*****************************************************************************/ 

#include "main.h"

struct interface *interface_first = NULL; /* first interface is current list */
struct interface *interface_newlist = NULL; /* first interface in new list */


/* set default out_channel */
void default_out_channel(struct interface_port *ifport)
{
	struct select_channel *selchannel, **selchannelp;

	selchannel = (struct select_channel *)MALLOC(sizeof(struct select_channel));
	memuse++;
	
	if (ifport->mISDNport->ntmode)
		selchannel->channel = CHANNEL_FREE;
	else
		selchannel->channel = CHANNEL_ANY;
	
	ifport->out_channel = selchannel;

	/* additional channel selection for multipoint NT ports */
	if (!ifport->mISDNport->ptp && ifport->mISDNport->ntmode) {
		selchannelp = &(selchannel->next);
		selchannel = (struct select_channel *)MALLOC(sizeof(struct select_channel));
		memuse++;
		selchannel->channel = CHANNEL_NO; // call waiting
		*selchannelp = selchannel;
	}
}


/* set default in_channel */
void default_in_channel(struct interface_port *ifport)
{
	struct select_channel *selchannel;

	selchannel = (struct select_channel *)MALLOC(sizeof(struct select_channel));
	memuse++;
	
	selchannel->channel = CHANNEL_FREE;
	
	ifport->in_channel = selchannel;
}


/* parse string for a positive number */
static int get_number(char *value)
{
	int val = 0;
	char text[10];

	val = atoi(value);
	
	SPRINT(text, "%d", val);

	if (!strcmp(value, text))
		return(val);

	return(-1);
}


/* remove element from buffer
 * and return pointer to next element in buffer */
static char *get_seperated(char *buffer)
{
	while(*buffer) {
		if (*buffer==',' || *buffer<=32) { /* seperate */
			*buffer++ = '\0';
			while((*buffer>'\0' && *buffer<=32) || *buffer==',')
				buffer++;
			return(buffer);
		}
		buffer++;
	}
	return(buffer);
}

/*
 * parameter processing
 */
static int inter_block(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	struct interface_port *ifport;

	/* port in chain ? */
	if (!interface->ifport) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects previous 'port' definition.\n", filename, line, parameter);
		return(-1);
	}
	/* goto end of chain */
	ifport = interface->ifport;
	while(ifport->next)
		ifport = ifport->next;
	/* add value */
	if (value[0]) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects no value.\n", filename, line, parameter);
		return(-1);
	}
	ifport->block = 1;
	return(0);
}
static int inter_extension(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	if (interface->external) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' not allowed, because interface is external interface.\n", filename, line, parameter);
		return(-1);
	}
	if (value[0]) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects no value.\n", filename, line, parameter);
		return(-1);
	}
	interface->extension = 1;
	return(0);
}
static int inter_extern(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	if (interface->extension) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' not allowed, because interface is an extension.\n", filename, line, parameter);
		return(-1);
	}
	if (value[0]) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects no value.\n", filename, line, parameter);
		return(-1);
	}
	interface->external = 1;
	return(0);
}
static int inter_ptp(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	struct interface_port *ifport;

	/* port in chain ? */
	if (!interface->ifport) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects previous 'port' definition.\n", filename, line, parameter);
		return(-1);
	}
	if (interface->ifport->ptmp) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' previously ptmp was given.\n", filename, line, parameter);
		return(-1);
	}
	/* goto end of chain */
	ifport = interface->ifport;
	while(ifport->next)
		ifport = ifport->next;
	/* add value */
	if (value[0]) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects no value.\n", filename, line, parameter);
		return(-1);
	}
	ifport->ptp = 1;
	return(0);
}
#if 0
static int inter_ptmp(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	struct interface_port *ifport;

	/* port in chain ? */
	if (!interface->ifport) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects previous 'port' definition.\n", filename, line, parameter);
		return(-1);
	}
	if (interface->ifport->ptp) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' previously ptp was given.\n", filename, line, parameter);
		return(-1);
	}
	/* goto end of chain */
	ifport = interface->ifport;
	while(ifport->next)
		ifport = ifport->next;
	/* add value */
	if (value[0]) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects no value.\n", filename, line, parameter);
		return(-1);
	}
	ifport->ptmp = 1;
	return(0);
}
#endif
static int inter_nt(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	struct interface_port *ifport;

	/* port in chain ? */
	if (!interface->ifport) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects previous 'port' definition.\n", filename, line, parameter);
		return(-1);
	}
	/* goto end of chain */
	ifport = interface->ifport;
	while(ifport->next)
		ifport = ifport->next;
	/* add value */
	if (value[0]) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects no value.\n", filename, line, parameter);
		return(-1);
	}
	ifport->nt = 1;
	return(0);
}
static int inter_tespecial(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	struct interface_port *ifport;

	/* port in chain ? */
	if (!interface->ifport) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects previous 'port' definition.\n", filename, line, parameter);
		return(-1);
	}
	/* goto end of chain */
	ifport = interface->ifport;
	while(ifport->next)
		ifport = ifport->next;
	/* add value */
	if (value[0]) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects no value.\n", filename, line, parameter);
		return(-1);
	}
	ifport->tespecial = 1;
	return(0);
}
static int inter_tones(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	if (!strcasecmp(value, "yes")) {
		interface->is_tones = IS_YES;
	} else
	if (!strcasecmp(value, "no")) {
		interface->is_tones = IS_NO;
	} else {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects value 'yes' or 'no'.\n", filename, line, parameter);
		return(-1);
	}
	return(0);
}
static int inter_earlyb(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	if (!strcasecmp(value, "yes")) {
		interface->is_earlyb = IS_YES;
	} else
	if (!strcasecmp(value, "no")) {
		interface->is_earlyb = IS_NO;
	} else {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects value 'yes' or 'no'.\n", filename, line, parameter);
		return(-1);
	}
	return(0);
}
static int inter_hunt(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	if (!strcasecmp(value, "linear")) {
		interface->hunt = HUNT_LINEAR;
	} else
	if (!strcasecmp(value, "roundrobin")) {
		interface->hunt = HUNT_ROUNDROBIN;
	} else {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects value 'linear' or 'roundrobin'.\n", filename, line, parameter);
		return(-1);
	}
	return(0);
}
static int inter_port(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	SPRINT(interface_error, "Error in %s (line %d): parameter '%s' is outdated.\nPlease use 'portnum' and decrease port number by 1! Ports are counted from 0 now.\n", filename, line, parameter);
	return(-1);
}
static int inter_portnum(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	struct interface_port *ifport, **ifportp;
	struct interface *searchif;
	int val;

	val = get_number(value);
	if (val == -1) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects one numeric value.\n", filename, line, parameter);
		return(-1);
	}
	/* check for port already assigned */
	searchif = interface_newlist;
	while(searchif) {
		ifport = searchif->ifport;
		while(ifport) {
			if (ifport->portnum == val) {
				SPRINT(interface_error, "Error in %s (line %d): port '%d' already used above.\n", filename, line, val);
				return(-1);
			}
			ifport = ifport->next;
		}
		searchif = searchif->next;
	}
	/* alloc port substructure */
	ifport = (struct interface_port *)MALLOC(sizeof(struct interface_port));
	memuse++;
	ifport->interface = interface;
	/* set value */
	ifport->portnum = val;
	/* tail port */
	ifportp = &interface->ifport;
	while(*ifportp)
		ifportp = &((*ifportp)->next);
	*ifportp = ifport;
	return(0);
}
static int inter_portname(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	struct interface_port *ifport, **ifportp;
	struct interface *searchif;

	/* goto end of chain */
	ifport = interface->ifport;
	if (ifport) {
		while(ifport->next)
			ifport = ifport->next;
	}

	/* check for port already assigned, but not for shared loop interface */
	searchif = interface_newlist;
	if (!!strcmp(value, options.loopback_lcr))
	{
		while(searchif) {
			ifport = searchif->ifport;
			while(ifport) {
				if (!strcasecmp(ifport->portname, value)) {
					SPRINT(interface_error, "Error in %s (line %d): port '%s' already used above.\n", filename, line, value);
					return(-1);
				}
				ifport = ifport->next;
			}
			searchif = searchif->next;
		}
	}
	/* alloc port substructure */
	ifport = (struct interface_port *)MALLOC(sizeof(struct interface_port));
	memuse++;
	ifport->interface = interface;
	/* set value */
	ifport->portnum = -1; // disable until resolved
	SCPY(ifport->portname, value);
	/* tail port */
	ifportp = &interface->ifport;
	while(*ifportp)
		ifportp = &((*ifportp)->next);
	*ifportp = ifport;
	return(0);
}
static int inter_l1hold(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	struct interface_port *ifport;

	/* port in chain ? */
	if (!interface->ifport) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects previous 'port' definition.\n", filename, line, parameter);
		return(-1);
	}
	/* goto end of chain */
	ifport = interface->ifport;
	while(ifport->next)
		ifport = ifport->next;
	if (!strcmp(value, "yes")) {
		ifport->l1hold = 1;
	} else
	if (!strcmp(value, "no")) {
		ifport->l1hold = 0;
	} else {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expecting parameter 'yes' or 'no'.\n", filename, line, parameter);
		return(-1);
	}
	return(0);
}
static int inter_l2hold(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	struct interface_port *ifport;

	/* port in chain ? */
	if (!interface->ifport) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects previous 'port' definition.\n", filename, line, parameter);
		return(-1);
	}
	/* goto end of chain */
	ifport = interface->ifport;
	while(ifport->next)
		ifport = ifport->next;
	if (!strcmp(value, "yes")) {
		ifport->l2hold = 1;
	} else
	if (!strcmp(value, "no")) {
		ifport->l2hold = -1;
	} else {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expecting parameter 'yes' or 'no'.\n", filename, line, parameter);
		return(-1);
	}
	return(0);
}
static int inter_channel_out(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	struct interface_port *ifport;
	struct select_channel *selchannel, **selchannelp;
	int val;
	char *p, *el;

	/* port in chain ? */
	if (!interface->ifport) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects previous 'port' definition.\n", filename, line, parameter);
		return(-1);
	}
	/* goto end of chain */
	ifport = interface->ifport;
	while(ifport->next)
		ifport = ifport->next;
	p = value;
	while(*p) {
		el = p;
		p = get_seperated(p);
		if (!strcasecmp(el, "force")) {
			ifport->channel_force = 1;
			if (ifport->out_channel) {
				SPRINT(interface_error, "Error in %s (line %d): value 'force' may only appear as first element in list.\n", filename, line);
				return(-1);
			}
		} else
		if (!strcasecmp(el, "any")) {
			val = CHANNEL_ANY;
			goto selchannel;
		} else
		if (!strcasecmp(el, "free")) {
			val = CHANNEL_FREE;
			goto selchannel;
		} else
		if (!strcasecmp(el, "no")) {
			val = CHANNEL_NO;
			goto selchannel;
		} else {
			val = get_number(el);
			if (val == -1) {
				SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects a comma seperated list of 'force', 'any', 'free', 'no' and any channel number.\n", filename, line, parameter);
				return(-1);
			}

			if (val<1 || val==16 || val>126) {
				SPRINT(interface_error, "Error in %s (line %d): channel '%d' out of range.\n", filename, line, val);
				return(-1);
			}
			selchannel:
			/* add to select-channel list */
			selchannel = (struct select_channel *)MALLOC(sizeof(struct select_channel));
			memuse++;
			/* set value */
			selchannel->channel = val;
			/* tail port */
			selchannelp = &ifport->out_channel;
			while(*selchannelp)
				selchannelp = &((*selchannelp)->next);
			*selchannelp = selchannel;
		}
	}
	return(0);
}
static int inter_channel_in(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	struct interface_port *ifport;
	struct select_channel *selchannel, **selchannelp;
	int val;
	char *p, *el;

	/* port in chain ? */
	if (!interface->ifport) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects previous 'port' definition.\n", filename, line, parameter);
		return(-1);
	}
	/* goto end of chain */
	ifport = interface->ifport;
	while(ifport->next)
		ifport = ifport->next;
	p = value;
	while(*p) {
		el = p;
		p = get_seperated(p);
		if (ifport->in_channel) if (ifport->in_channel->channel == CHANNEL_FREE) {
			SPRINT(interface_error, "Error in %s (line %d): parameter '%s' has values behind 'free' keyword. They has no effect.\n", filename, line, parameter);
				return(-1);
		}
		if (!strcasecmp(el, "free")) {
			val = CHANNEL_FREE;
			goto selchannel;
		} else {
			val = get_number(el);
			if (val == -1) {
				SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects a comma seperated list of channel numbers and 'free'.\n", filename, line, parameter);
				return(-1);
			}

			if (val<1 || val==16 || val>126) {
				SPRINT(interface_error, "Error in %s (line %d): channel '%d' out of range.\n", filename, line, val);
				return(-1);
			}
			selchannel:
			/* add to select-channel list */
			selchannel = (struct select_channel *)MALLOC(sizeof(struct select_channel));
			memuse++;
			/* set value */
			selchannel->channel = val;
			/* tail port */
			selchannelp = &ifport->in_channel;
			while(*selchannelp)
				selchannelp = &((*selchannelp)->next);
			*selchannelp = selchannel;
		}
	}
	return(0);
}
static int inter_timeouts(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	struct interface_port *ifport;
//	struct select_channel *selchannel, **selchannelp;
//	int val;
	char *p, *el;

	/* port in chain ? */
	if (!interface->ifport) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects previous 'port' definition.\n", filename, line, parameter);
		return(-1);
	}
	/* goto end of chain */
	ifport = interface->ifport;
	while(ifport->next)
		ifport = ifport->next;
	p = value;
	if (!*p) {
		nofive:
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects five timeout values.\n", filename, line, parameter);
		return(-1);
	}
	el = p;
	p = get_seperated(p);
	ifport->tout_setup = atoi(el);
	if (!*p)
		goto nofive;
	el = p;
	p = get_seperated(p);
	ifport->tout_dialing = atoi(el);
	if (!*p)
		goto nofive;
	el = p;
	p = get_seperated(p);
	ifport->tout_proceeding = atoi(el);
	if (!*p)
		goto nofive;
	el = p;
	p = get_seperated(p);
	ifport->tout_alerting = atoi(el);
	if (!*p)
		goto nofive;
	el = p;
	p = get_seperated(p);
	ifport->tout_disconnect = atoi(el);
	return(0);
}
static int inter_msn(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	struct interface_msn *ifmsn, **ifmsnp;
	char *p, *el;

	if (!value[0]) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects one MSN number or a list.\n", filename, line, parameter);
		return(-1);
	}
	if (interface->ifscreen_in) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' not allowed with 'screen_in' parameter.\n", filename, line, parameter);
		return(-1);
	}

	/* process list */
	p = value;
	while(*p) {
		el = p;
		p = get_seperated(p);
		/* add MSN to list */
		ifmsn = (struct interface_msn *)MALLOC(sizeof(struct interface_msn));
		memuse++;
		/* set value */
		SCPY(ifmsn->msn, el);
		/* tail port */
		ifmsnp = &interface->ifmsn;
		while(*ifmsnp)
			ifmsnp = &((*ifmsnp)->next);
		*ifmsnp = ifmsn;
	}
	return(0);
}
static int inter_screen(struct interface_screen **ifscreenp, struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	struct interface_screen *ifscreen;
	char *p, *el;

	if (!value[0]) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects old caller ID and new caller ID.\n", filename, line, parameter);
		return(-1);
	}
	/* add screen entry to list*/
	ifscreen = (struct interface_screen *)MALLOC(sizeof(struct interface_screen));
	memuse++;
	ifscreen->match_type = -1; /* unchecked */
	ifscreen->match_present = -1; /* unchecked */
	ifscreen->result_type = -1; /* unchanged */
	ifscreen->result_present = -1; /* unchanged */
	/* tail port */
	while(*ifscreenp)
		ifscreenp = &((*ifscreenp)->next);
	*ifscreenp = ifscreen;
//	printf("interface=%s\n", interface->name);
	/* get match */
	p = value;
	while(*p) {
		el = p;
		p = get_seperated(p);
		if (!strcasecmp(el, "unknown")) {
			if (ifscreen->match_type != -1) {
				typeerror:
				SPRINT(interface_error, "Error in %s (line %d): number type already set earlier.\n", filename, line, parameter);
				return(-1);
			}
			ifscreen->match_type = INFO_NTYPE_UNKNOWN;
		} else
		if (!strcasecmp(el, "subscriber")) {
			if (ifscreen->match_type != -1)
				goto typeerror;
			ifscreen->match_type = INFO_NTYPE_SUBSCRIBER;
		} else
		if (!strcasecmp(el, "national")) {
			if (ifscreen->match_type != -1)
				goto typeerror;
			ifscreen->match_type = INFO_NTYPE_NATIONAL;
		} else
		if (!strcasecmp(el, "international")) {
			if (ifscreen->match_type != -1)
				goto typeerror;
			ifscreen->match_type = INFO_NTYPE_INTERNATIONAL;
		} else
		if (!strcasecmp(el, "allowed")) {
			if (ifscreen->match_present != -1) {
				presenterror:
				SPRINT(interface_error, "Error in %s (line %d): presentation type already set earlier.\n", filename, line);
				return(-1);
			}
			ifscreen->match_present = INFO_PRESENT_ALLOWED;
		} else
		if (!strcasecmp(el, "restrict") || !strcasecmp(el, "restricted")) {
			if (ifscreen->match_present != -1)
				goto presenterror;
			ifscreen->match_present = INFO_PRESENT_RESTRICTED;
		} else {
			SCPY(ifscreen->match, el);
			/* check for % at the end */
			if (strchr(el, '%')) {
				if (strchr(el, '%') != el+strlen(el)-1) {
					SPRINT(interface_error, "Error in %s (line %d): %% joker found, but must at the end.\n", filename, line, parameter);
					return(-1);
				}
			}
			break;
		}
	}
	if (ifscreen->match[0] == '\0') {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects old caller ID.\n", filename, line, parameter);
		return(-1);
	}
	/* get result */
	while(*p) {
		el = p;
		p = get_seperated(p);
		if (!strcasecmp(el, "unknown")) {
			if (ifscreen->result_type != -1)
				goto typeerror;
			ifscreen->result_type = INFO_NTYPE_UNKNOWN;
		} else
		if (!strcasecmp(el, "subscriber")) {
			if (ifscreen->result_type != -1)
				goto typeerror;
			ifscreen->result_type = INFO_NTYPE_SUBSCRIBER;
		} else
		if (!strcasecmp(el, "national")) {
			if (ifscreen->result_type != -1)
				goto typeerror;
			ifscreen->result_type = INFO_NTYPE_NATIONAL;
		} else
		if (!strcasecmp(el, "international")) {
			if (ifscreen->result_type != -1)
				goto typeerror;
			ifscreen->result_type = INFO_NTYPE_INTERNATIONAL;
		} else
		if (!strcasecmp(el, "present") || !strcasecmp(el, "presented") || !strcasecmp(el, "allowed") || !strcasecmp(el, "allow")) {
			if (ifscreen->result_present != -1)
				goto presenterror;
			ifscreen->result_present = INFO_PRESENT_ALLOWED;
		} else
		if (!strcasecmp(el, "restrict") || !strcasecmp(el, "restricted") || !strcasecmp(el, "deny") || !strcasecmp(el, "denied")) {
			if (ifscreen->result_present != -1)
				goto presenterror;
			ifscreen->result_present = INFO_PRESENT_RESTRICTED;
		} else {
			SCPY(ifscreen->result, el);
			/* check for % at the end */
			if (strchr(el, '%')) {
				if (strchr(el, '%') != el+strlen(el)-1) {
					SPRINT(interface_error, "Error in %s (line %d): %% joker found, but must at the end.\n", filename, line, parameter);
					return(-1);
				}
			}
			break;
		}
	}
	if (ifscreen->result[0] == '\0') {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects new caller ID.\n", filename, line, parameter);
		return(-1);
	}
	return(0);
}
static int inter_screen_in(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	if (interface->ifmsn) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' not allowed with 'msn' parameter.\n", filename, line, parameter);
		return(-1);
	}

	return(inter_screen(&interface->ifscreen_in, interface, filename, line, parameter, value));
}
static int inter_screen_out(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	return(inter_screen(&interface->ifscreen_out, interface, filename, line, parameter, value));
}
static int inter_nodtmf(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	struct interface_port *ifport;

	/* port in chain ? */
	if (!interface->ifport) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects previous 'port' definition.\n", filename, line, parameter);
		return(-1);
	}
	/* goto end of chain */
	ifport = interface->ifport;
	while(ifport->next)
		ifport = ifport->next;
	ifport->nodtmf = 1;
	return(0);
}
static int inter_filter(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	char *p, *q;

	/* seperate parameter from filter */
	p = value;
	while(*p > 32)
		p++;
	if (*p) {
		*p++ = 0;
		while(*p > 0 && *p <= 32)
			p++;
	}

	if (!strcasecmp(value, "gain")) {
		q = p;
		while(*q > 32)
			q++;
		if (*q) {
			*q++ = 0;
			while(*q > 0 && *q <= 32)
				q++;
		}
		if (*p == 0 || *q == 0) {
			SPRINT(interface_error, "Error in %s (line %d): parameter '%s %s' expects two gain values.\n", filename, line, parameter, value);
			return(-1);
		}
		if (atoi(p)<-8 || atoi(p)>8 || atoi(q)<-8 || atoi(q)>8) {
			SPRINT(interface_error, "Error in %s (line %d): parameter '%s %s' gain values not in range. (-8...8)\n", filename, line, parameter, value);
			return(-1);
		}
		interface->tx_gain = atoi(p);
		interface->rx_gain = atoi(q);
	} else
	if (!strcasecmp(value, "pipeline")) {
		if (*p == 0) {
			SPRINT(interface_error, "Error in %s (line %d): parameter '%s %s' expects pipeline string.\n", filename, line, parameter, value);
			return(-1);
		}
		SCPY(interface->pipeline, p);
	} else
	if (!strcasecmp(value, "blowfish")) {
		unsigned char key[56];
		int l;
		
		if (!!strncmp(p, "0x", 2)) {
			SPRINT(interface_error, "Error in %s (line %d): parameter '%s %s' expects blowfish key starting with '0x'.\n", filename, line, parameter, value);
			return(-1);
		}
		p += 2;
		l = 0; 
		while(*p) {
			if (l == 56) {
				SPRINT(interface_error, "Error in %s (line %d): parameter '%s %s' key too long.\n", filename, line, parameter, value);
				return(-1);
			}
			if (*p >= '0' && *p <= '9')
				key[l] = (*p-'0')<<4;
			else if (*p >= 'a' && *p <= 'f')
				key[l] = (*p-'a'+10)<<4;
			else if (*p >= 'A' && *p <= 'F')
				key[l] = (*p-'A'+10)<<4;
			else {
				digout:
				SPRINT(interface_error, "Error in %s (line %d): parameter '%s %s' key has digits out of range. (0...9, a...f)\n", filename, line, parameter, value);
				return(-1);
			}
			p++;
			if (*p == 0) {
				SPRINT(interface_error, "Error in %s (line %d): parameter '%s %s' key must end on an 8 bit boundary (two character boundary).\n", filename, line, parameter, value);
				return(-1);
			}
			if (*p >= '0' && *p <= '9')
				key[l] = (*p-'0')<<4;
			else if (*p >= 'a' && *p <= 'f')
				key[l] = (*p-'a'+10)<<4;
			else if (*p >= 'A' && *p <= 'F')
				key[l] = (*p-'A'+10)<<4;
			else
				goto digout;
			p++;
			l++;
		}
		if (l < 4) {
			SPRINT(interface_error, "Error in %s (line %d): parameter '%s %s' key must be at least 4 bytes (8 characters).\n", filename, line, parameter, value);
			return(-1);
		}
		memcpy(interface->bf_key, key, l);
		interface->bf_len = l;
	} else {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' has unknown filter '%s'.\n", filename, line, parameter, value);
		return(-1);
	}
	return(0);
}
static int inter_dialmax(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	struct interface_port *ifport;

	/* port in chain ? */
	if (!interface->ifport) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects previous 'port' definition.\n", filename, line, parameter);
		return(-1);
	}
	/* goto end of chain */
	ifport = interface->ifport;
	while(ifport->next)
		ifport = ifport->next;
	ifport->dialmax = atoi(value);
	return(0);
}
static int inter_tones_dir(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	struct interface_port *ifport;

	/* port in chain ? */
	if (!interface->ifport) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects previous 'port' definition.\n", filename, line, parameter);
		return(-1);
	}
	/* goto end of chain */
	ifport = interface->ifport;
	while(ifport->next)
		ifport = ifport->next;
	SCPY(ifport->tones_dir, value);
	return(0);
}
static int inter_gsm(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	SPRINT(interface_error, "Error in %s (line %d): parameter '%s' is outdated.\nPlease use 'gsm-bs' for base station or 'gsm-ms' for mobile station interface!\n", filename, line, parameter);
	return(-1);
}
static int inter_gsm_bs(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
#ifndef WITH_GSM_BS
	SPRINT(interface_error, "Error in %s (line %d): GSM BS side not compiled in.\n", filename, line);
	return(-1);
#else
	struct interface_port *ifport;
	struct interface *searchif;

	searchif = interface_newlist;
	while(searchif) {
		ifport = searchif->ifport;
		while(ifport) {
			if (ifport->gsm_bs) {
				SPRINT(interface_error, "Error in %s (line %d): port '%s' already uses gsm BS side.\n", filename, line, ifport->portname);
				return(-1);
			}
			ifport = ifport->next;
		}
		searchif = searchif->next;
	}

	/* set portname */
	if (inter_portname(interface, filename, line, (char *)"portname", options.loopback_lcr))
		return(-1);

	/* goto end of chain again to set gsmflag */
	ifport = interface->ifport;
	while(ifport->next)
		ifport = ifport->next;
	ifport->gsm_bs = 1;

	return(0);
#endif
}
static int inter_gsm_ms(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
#ifndef WITH_GSM_MS
	SPRINT(interface_error, "Error in %s (line %d): GSM MS side not compiled in.\n", filename, line);
	return(-1);
#else
	struct interface_port *ifport, *searchifport;
	struct interface *searchif;

	/* set portname */
	if (inter_portname(interface, filename, line, (char *)"portname", options.loopback_lcr))
		return(-1);

	/* goto end of chain again to set gsmflag and socket */
	ifport = interface->ifport;
	while(ifport->next)
		ifport = ifport->next;
	ifport->gsm_ms = 1;

	/* copy values */
	if (!value || !value[0]) {
		SPRINT(interface_error, "Error in %s (line %d): Missing MS name and socket name.\n", filename, line);
		return(-1);
	}
	SCPY(ifport->gsm_ms_name, value);

	/* check if name is used multiple times */
	searchif = interface_newlist;
	while(searchif) {
		searchifport = searchif->ifport;
		while(searchifport) {
			if (searchifport != ifport 
			 && !strcmp(searchifport->gsm_ms_name, ifport->gsm_ms_name)) {
				SPRINT(interface_error, "Error in %s (line %d): mobile '%s' already uses the given MS name '%s', choose a different one.\n", filename, line, ifport->gsm_ms_name, searchifport->gsm_ms_name);
				return(-1);
			}
			searchifport = searchifport->next;
		}
		searchif = searchif->next;
	}

	return(0);
#endif
}
static int inter_nonotify(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	struct interface_port *ifport;

	/* port in chain ? */
	if (!interface->ifport) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects previous 'port' definition.\n", filename, line, parameter);
		return(-1);
	}
	/* goto end of chain */
	ifport = interface->ifport;
	while(ifport->next)
		ifport = ifport->next;
	ifport->nonotify = 1;
	return(0);
}
#ifdef WITH_SS5
static int inter_ss5(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	struct interface_port *ifport;
	char *element;

	/* port in chain ? */
	if (!interface->ifport) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects previous 'port' definition.\n", filename, line, parameter);
		return(-1);
	}
	/* goto end of chain */
	ifport = interface->ifport;
	while(ifport->next)
		ifport = ifport->next;
	ifport->ss5 |= SS5_ENABLE;
	while((element = strsep(&value, " "))) {
		if (element[0] == '\0')
			continue;
		if (!strcasecmp(element, "connect"))
			ifport->ss5 |= SS5_FEATURE_CONNECT;
		else
		if (!strcasecmp(element, "nodisconnect"))
			ifport->ss5 |= SS5_FEATURE_NODISCONNECT;
		else
		if (!strcasecmp(element, "releaseguardtimer"))
			ifport->ss5 |= SS5_FEATURE_RELEASEGUARDTIMER;
		else
		if (!strcasecmp(element, "bell"))
			ifport->ss5 |= SS5_FEATURE_BELL;
		else
		if (!strcasecmp(element, "pulsedialing"))
			ifport->ss5 |= SS5_FEATURE_PULSEDIALING;
		else
		if (!strcasecmp(element, "delay"))
			ifport->ss5 |= SS5_FEATURE_DELAY;
		else
		if (!strcasecmp(element, "starrelease"))
			ifport->ss5 |= SS5_FEATURE_STAR_RELEASE;
		else
		if (!strcasecmp(element, "suppress"))
			ifport->ss5 |= SS5_FEATURE_SUPPRESS;
		else {
			SPRINT(interface_error, "Error in %s (line %d): parameter '%s' does not allow value element '%s'.\n", filename, line, parameter, element);
			return(-1);
		}
	}
	return(0);
}
#endif
static int inter_remote(struct interface *interface, char *filename, int line, char *parameter, char *value)
{
	struct interface_port *ifport;
	struct interface *searchif;

	if (!value[0]) {
		SPRINT(interface_error, "Error in %s (line %d): parameter '%s' expects application name as value.\n", filename, line, parameter);
		return(-1);
	}
	searchif = interface_newlist;
	while(searchif) {
		ifport = searchif->ifport;
		while(ifport) {
			if (ifport->remote && !strcmp(ifport->remote_app, value)) {
				SPRINT(interface_error, "Error in %s (line %d): port '%s' already uses remote application '%s'.\n", filename, line, ifport->portname, value);
				return(-1);
			}
			ifport = ifport->next;
		}
		searchif = searchif->next;
	}

	/* set portname */
	if (inter_portname(interface, filename, line, (char *)"portname", options.loopback_lcr))
		return(-1);

	/* goto end of chain again to set application name */
	ifport = interface->ifport;
	while(ifport->next)
		ifport = ifport->next;
	ifport->remote = 1;
	SCPY(ifport->remote_app, value);


	return(0);
}


/*
 * structure of parameters
 */
struct interface_param interface_param[] = {
	{ "extension", &inter_extension, "",
	"If keyword is given, calls to interface are handled as internal extensions."},

	{ "extern", &inter_extern, "",
	"If keyword is given, this interface will be used for external calls.\n"
	"Calls require an external interface, if the routing action 'extern' is used\nwithout specific interface given.\n"
	"Calls forwarded by extension's 'settings' also require an external interface."},

	{"tones", &inter_tones, "yes | no",
	"Interface generates tones during call setup and release, or not.\nBy default only NT-mode ports generate tones."},

	{"earlyb", &inter_earlyb, "yes | no",
	"Interface receives and bridges tones during call setup and release, or not.\nBy default only TE-mode ports receive tones."},

	{"hunt", &inter_hunt, "linear | roundrobin",
	"Select the algorithm for selecting port with free channel."},

	{"port", &inter_port, "<number>",
	""},
	{"portnum", &inter_portnum, "<number>",
	"Give exactly one port for this interface.\nTo give multiple ports, add more lines with port parameters."},
	{"portname", &inter_portname, "<name>",
	"Same as 'portnum', but the name is given instead.\nUse 'isdninfo' to list all available ports and names."},

	{"block", &inter_block, "",
	"If keyword is given, calls on this interface are blocked.\n"
	"This parameter must follow a 'port' parameter."},

	{"ptp", &inter_ptp, "",
	"The given port above is opened as point-to-point.\n"
	"This is required on NT-mode ports that are multipoint by default.\n"
	"This parameter must follow a 'port' parameter."},

#if 0
	{"ptmp", &inter_ptmp, "",
	"The given port above is opened as point-to-multipoint.\n"
	"This is required on PRI NT-mode ports that are point-to-point by default.\n"
	"This parameter must follow a 'port' parameter."},
#endif

	{"nt", &inter_nt, "",
	"The given port above is opened in NT-mode.\n"
	"This is required on interfaces that support both NT-mode and TE-mode.\n"
	"This parameter must follow a 'port' parameter."},

	{"te-special", &inter_tespecial, "",
	"The given port uses a modified TE-mode.\n"
	"All information elements that are allowed Network->User will then be\n"
	"transmitted User->Network also. This is usefull to pass all informations\n"
	"between two interconnected LCRs, like 'redirected number' or 'display'.\n"
	"Note that this is not compliant with ISDN protocol.\n"
	"This parameter must follow a 'port' parameter."},

	{"layer1hold", &inter_l1hold, "yes | no",
	"The given port will not release layer 1 after layer 2 is down.\n"
	"It is required to keep layer 1 of telephones up, to solve activation problems.\n"
	"This parameter must follow a 'port' parameter."},

	{"layer2hold", &inter_l2hold, "yes | no",
	"The given port will continuously try to establish layer 2 link and hold it.\n"
	"It is required for PTP links in most cases, therefore it is default.\n"
	"This parameter must follow a 'port' parameter."},

	{"channel-out", &inter_channel_out, "[force,][<number>][,...][,free][,any][,no]",
	"Channel selection list for all outgoing calls to the interface.\n"
	"A free channels is searched in order of appearance.\n"
	"This parameter must follow a 'port' parameter.\n"
	" force - Forces the selected port with no acceptable alternative (see Q.931).\n"
	"  -> this will be automatically set for multipoint (ptmp) NT-mode ports\n"
	" <number>[,...] - List of channels to search.\n"
	" free - Select any free channel\n"
	" any - On outgoing calls, signal 'any channel acceptable'. (see DSS1)\n"
	" no - Signal 'no channel available' aka 'call waiting'. (see DSS1)"},

	{"channel-in", &inter_channel_in, "[<number>][,...][,free]",
	"Channel selection list for all incoming calls from the interface.\n"
	"A free channels is accepted if in the list.\n"
	"If any channel was requested, the first free channel found is selected.\n"
	"This parameter must follow a 'port' parameter.\n"
	" <number>[,...] - List of channels to accept.\n"
	" free - Accept any free channel"},

	{"timeouts", &inter_timeouts, "<setup> <dialing> <proceeding> <alerting> <disconnect>",
	"Timeout values for call states. They are both for incoming and outgoing states.\n"
	"The default is 120 seconds for all states. Use 0 to disable.\n"
	"This parameter must follow a 'port' parameter.\n"},

	{"msn", &inter_msn, "<default MSN>,[<additional MSN>[,...]]",
	"Incoming caller ID is checked against given MSN numbers.\n"
	"If the caller ID is not found in this list, it is overwritten by the first MSN"},

	{"screen-in", &inter_screen_in, "[options] <old caller ID>[%] [options] <new caller ID>[%]",
	"Adds an entry for incoming calls to the caller ID screen list.\n"
	"If the given 'old caller ID' matches, it is replaced by the 'new caller ID'\n"
	"If '%' is given after old caller ID, it matches even if caller ID has\n"
	"additional digits.\n"
	"If '%' is given after mew caller ID, additinal digits of the 'old caller ID'\n"
	"are added.\n"
	"Options can be:\n"
	" unknown | subsciber | national | international - Change caller ID type.\n"
	" present | restrict - Change presentation of caller ID."},
		
	{"screen-out", &inter_screen_out, "[options] <old caller ID>[%] [options] <new caller ID>[%]",
	"Adds an entry for outgoing calls to the caller ID screen list.\n"
	"See 'screen-in' for help."},

	{"nodtmf", &inter_nodtmf, "",
	"Disables DTMF detection for this interface.\n"
	"This parameter must follow a 'port' parameter."},

	{"filter", &inter_filter, "<filter> <parameters>",
	"Adds/appends a filter. Filters are ordered in transmit direction.\n"
	"gain <tx-volume> <rx-volume> - Changes volume (-8 .. 8)\n"
	"pipeline <string> - Sets echo cancelation pipeline.\n"
	"blowfish <key> - Adds encryption. Key must be 4-56 bytes (8-112 hex characters."},

	{"dialmax", &inter_dialmax, "<digits>",
	"Limits the number of digits in setup/information message."},

	{"tones_dir", &inter_tones_dir, "<path>",
	"Overrides the given tone_dir in options.conf.\n"
	"To used kernel tones in mISDN_dsp.ko, say 'american', 'german', or 'oldgerman'."},

	{"gsm", &inter_gsm, "",
	""},
	{"gsm-bs", &inter_gsm_bs, "",
	"Sets up GSM base station interface for using OpenBSC.\n"
	"See the default/gsm.conf for configuration for this version."},
	{"gsm-ms", &inter_gsm_ms, "[<socket>]",
	"Sets up GSM mobile station interface for using Osmocom-BB.\n"
	"See the default/gsm.conf for configuration for this version.\n"
	"The name of the MS folows the interface name.\n"
	"The socket is /tmp/osmocom_l2 by default and need to be changed when multiple\n"
	"MS interfaces are used."},
	{"nonotify", &inter_nonotify, "",
	"Prevents sending notify messages to this interface. A call placed on hold will\n"
	"Not affect the remote end (phone or telcom switch).\n"
	"This parameter must follow a 'port' parameter."},

#ifdef WITH_SS5
	{"ccitt5", &inter_ss5, "[<feature> [feature ...]]",
	"Interface uses CCITT No. 5 inband signalling rather than D-channel.\n"
	"This feature causes CPU load to rise and has no practical intend.\n"
	"If you don't know what it is, you don't need it.\n"
	"Features apply to protocol behaviour and blueboxing specials, they are:\n"
	" connect - Connect incomming call to throughconnect audio, if required.\n"
	" nodisconnect - Don't disconnect if incomming exchange disconnects.\n"
	" releaseguardtimer - Tries to prevent Blueboxing by a longer release-guard.\n"
	" bell - Allow releasing and pulse-dialing via 2600 Hz like old Bell systems.\n"
	" pulsedialing - Use pulse dialing on outgoing exchange. (takes long!)\n"
	" delay - Use on incomming exchange, to make you feel a delay when blueboxing.\n"
	" starrelease - Pulse dialing a star (11 pulses per digit) clears current call.\n"
	" suppress - Suppress received tones, as they will be recognized."},
#endif

	{"remote", &inter_remote, "<application>",
	"Sets up an interface that communicates with the remote application.\n"
	"Use \"asterisk\" to use chan_lcr as remote application."},

	{NULL, NULL, NULL, NULL}
};

/* read interfaces
 *
 * read settings from interface.conf
 */
char interface_error[256];
struct interface *read_interfaces(void)
{
	FILE			*fp = NULL;
	char			filename[128];
	char			*p;
	unsigned int		line, i;
	char			buffer[256];
	struct interface	*interface = NULL, /* in case no interface */
				**interfacep = &interface_newlist;
	char			parameter[128];
	char			value[256];
	int			expecting = 1; /* expecting new interface */
	struct interface_param	*ifparam;

	if (interface_newlist != NULL)
		FATAL("list is not empty.\n");
	interface_error[0] = '\0';
	SPRINT(filename, "%s/interface.conf", CONFIG_DATA);

	if (!(fp = fopen(filename,"r"))) {
		SPRINT(interface_error, "Cannot open '%s'\n", filename);
		goto error;
	}

	line=0;
	while((GETLINE(buffer, fp))) {
		p=buffer;
		line++;

		while(*p <= 32) { /* skip spaces */
			if (*p == 0)
				break;
			p++;
		}
		if (*p==0 || *p=='#') /* ignore comments and empty line */
			continue;

		parameter[0]=0;
		value[0]=0;
		i=0; /* read parameter */
		while(*p > 32) {
			if (i+1 >= sizeof(parameter)) {
				SPRINT(interface_error, "Error in %s (line %d): parameter name too long.\n",filename,line);
				goto error;
			}
			parameter[i+1] = '\0';
			parameter[i++] = *p++;
		}

		while(*p <= 32) { /* skip spaces */
			if (*p == 0)
				break;
			p++;
		}

		if (*p!=0 && *p!='#') { /* missing name */
			i=0; /* read until end */
			while(*p!=0 && *p!='#') {
				if (i+1 >= sizeof(value)) {
					SPRINT(interface_error, "Error in %s (line %d): value too long.\n", filename, line);
					goto error;
				}
				value[i+1] = '\0';
				value[i++] = *p++;
			}

			/* remove trailing spaces from value */
			while(i) {
				if (value[i-1]==0 || value[i-1]>32)
					break;
				value[i-1] = '\0';
				i--;
			}
		}

		/* check for interface name as first statement */
		if (expecting && parameter[0]!='[') {
			SPRINT(interface_error, "Error in %s (line %d): expecting interface name inside [ and ], but got: '%s'.\n", filename, line, parameter);
			goto error;
		}
		expecting = 0;

		/* check for new interface */
		if (parameter[0] == '[') {
			if (parameter[strlen(parameter)-1] != ']') {
				SPRINT(interface_error, "Error in %s (line %d): expecting interface name inside [ and ], but got: '%s'.\n", filename, line, parameter);
				goto error;
			}
			parameter[strlen(parameter)-1] = '\0';

			/* check if interface name already exists */
			interface = interface_newlist;
			while(interface) {
				if (!strcasecmp(interface->name, parameter+1)) {
					SPRINT(interface_error, "Error in %s (line %d): interface name '%s' already defined above.\n", filename, line, parameter+1);
					goto error;
				}
				interface = interface->next;
			}

			/* append interface to new list */
			interface = (struct interface *)MALLOC(sizeof(struct interface));
			memuse++;

			/* name interface */
			SCPY(interface->name, parameter+1);

			/* attach */
			*interfacep = interface;
			interfacep = &interface->next;

			continue;
		}

		ifparam = interface_param;
		while(ifparam->name) {
			if (!strcasecmp(parameter, ifparam->name)) {
				if (ifparam->func(interface, filename, line, parameter, value))
					goto error;
				break;
			}
			ifparam++;
		}
		if (ifparam->name)
			continue;

		SPRINT(interface_error, "Error in %s (line %d): unknown parameter: '%s'.\n", filename, line, parameter);
		goto error;
	}

	if (fp) fclose(fp);
	return(interface_newlist);
error:
	PERROR_RUNTIME("%s", interface_error);
	if (fp) fclose(fp);
	free_interfaces(interface_newlist);
	interface_newlist = NULL;
	return(NULL);
}


/*
 * freeing chain of interfaces
 */
void free_interfaces(struct interface *interface)
{
	void *temp;
	struct interface_port *ifport;
	struct select_channel *selchannel;
	struct interface_msn *ifmsn;
	struct interface_screen *ifscreen;

	while(interface) {
		ifport = interface->ifport;
		while(ifport) {
			selchannel = ifport->in_channel;
			while(selchannel) {
				temp = selchannel;
				selchannel = selchannel->next;
				FREE(temp, sizeof(struct select_channel));
				memuse--;
			}
			selchannel = ifport->out_channel;
			while(selchannel) {
				temp = selchannel;
				selchannel = selchannel->next;
				FREE(temp, sizeof(struct select_channel));
				memuse--;
			}
			temp = ifport;
			ifport = ifport->next;
			FREE(temp, sizeof(struct interface_port));
			memuse--;
		}
		ifmsn = interface->ifmsn;
		while(ifmsn) {
			temp = ifmsn;
			ifmsn = ifmsn->next;
			FREE(temp, sizeof(struct interface_msn));
			memuse--;
		}
		ifscreen = interface->ifscreen_in;
		while(ifscreen) {
			temp = ifscreen;
			ifscreen = ifscreen->next;
			FREE(temp, sizeof(struct interface_screen));
			memuse--;
		}
		ifscreen = interface->ifscreen_out;
		while(ifscreen) {
			temp = ifscreen;
			ifscreen = ifscreen->next;
			FREE(temp, sizeof(struct interface_screen));
			memuse--;
		}
		temp = interface;
		interface = interface->next;
		FREE(temp, sizeof(struct interface));
		memuse--;
	}
}

/*
 * defaults of ports if not specified by config
 */
static void set_defaults(struct interface_port *ifport)
{
	/* default channel selection list */
	if (!ifport->out_channel)
		default_out_channel(ifport);
	if (!ifport->in_channel)
		default_in_channel(ifport);
	/* must force the channel on PTMP/NT ports */
	if (!ifport->mISDNport->ptp && ifport->mISDNport->ntmode)
		ifport->channel_force = 1;
	/* default is_tones */
	if (ifport->interface->is_tones)
		ifport->mISDNport->tones = (ifport->interface->is_tones==IS_YES);
	else
		ifport->mISDNport->tones = (ifport->mISDNport->ntmode || ifport->mISDNport->ss5)?1:0;
	/* default is_earlyb */
	if (ifport->interface->is_earlyb)
		ifport->mISDNport->earlyb = (ifport->interface->is_earlyb==IS_YES);
	else
		ifport->mISDNport->earlyb = (ifport->mISDNport->ntmode && !ifport->mISDNport->ss5)?0:1;
	/* set locally flag */
	if (ifport->interface->extension)
		ifport->mISDNport->locally = 1;
	else
		ifport->mISDNport->locally = 0;
}


/*
 * all links between mISDNport and interface are made
 * unused mISDNports are closed, new mISDNports are opened
 * also set default select_channel lists
 */
void relink_interfaces(void)
{
	struct mISDNport *mISDNport;
	struct interface *interface;
	struct interface_port *ifport;

	/* unlink all mISDNports */
	mISDNport = mISDNport_first;
	while(mISDNport) {
		mISDNport->ifport = NULL;
		mISDNport = mISDNport->next;
	}

	/* relink existing mISDNports */
	interface = interface_newlist;
	while(interface) {
		ifport = interface->ifport;
		while(ifport) {
			mISDNport = mISDNport_first;
			while(mISDNport) {
				if (!strcmp(mISDNport->name, ifport->portname))
					ifport->portnum = mISDNport->portnum; /* same name, so we use same number */
				if (mISDNport->portnum == ifport->portnum) {
					PDEBUG(DEBUG_ISDN, "Port %d:%s relinking!\n", ifport->portnum, ifport->portname);
					ifport->mISDNport = mISDNport;
					mISDNport->ifport = ifport;
					set_defaults(ifport);
				}
				mISDNport = mISDNport->next;
			}
			ifport = ifport->next;
		}
		interface = interface->next;
	}

	/* close unused mISDNports */
	closeagain:
	mISDNport = mISDNport_first;
	while(mISDNport) {
		if (mISDNport->ifport == NULL) {
			PDEBUG(DEBUG_ISDN, "Port %d is not used anymore and will be closed\n", mISDNport->portnum);
			/* remove all port objects and destroy port */
#ifdef WITH_GSM_MS
			if (ifport->gsm_ms)
				gsm_ms_delete(ifport->gsm_ms_name);
#endif
#ifdef WITH_GSM_BS
			if (ifport->gsm_bs)
				gsm_bs_exit(0);
#endif
			mISDNport_close(mISDNport);
			goto closeagain;
		}
		mISDNport = mISDNport->next;
	}

	/* open and link new mISDNports */
	interface = interface_newlist;
	while(interface) {
		ifport = interface->ifport;
		while(ifport) {
			if (!ifport->mISDNport) {
				load_port(ifport);
			}
			ifport = ifport->next;
		}
		interface = interface->next;
	}

}


/*
 * load port
 */
void load_port(struct interface_port *ifport)
{
	struct mISDNport *mISDNport;

	/* open new port */
	mISDNport = mISDNport_open(ifport);
	if (mISDNport) {
		/* link port */
		ifport->mISDNport = mISDNport;
		mISDNport->ifport = ifport;
		/* set number and name */
		ifport->portnum = mISDNport->portnum;
		SCPY(ifport->portname, mISDNport->name);
		/* set defaults */
		set_defaults(ifport);
		/* load static port instances */
		mISDNport_static(mISDNport);
#ifdef WITH_GSM_MS
		if (ifport->gsm_ms)
			gsm_ms_new(ifport->gsm_ms_name);
#endif
#ifdef WITH_GSM_BS
		if (ifport->gsm_bs)
			gsm_bs_init();
#endif
	} else {
		ifport->block = 2; /* not available */
	}
}

/*
 * give summary of interface syntax
 */
void doc_interface(void)
{
	struct interface_param *ifparam;
	
	printf("Syntax overview\n");
	printf("---------------\n\n");

	printf("[<name>]\n");
	ifparam = interface_param;
	while(ifparam->name) {
		if (ifparam->name[0])
			printf("%s %s\n", ifparam->name, ifparam->usage);
		ifparam++;
	}

	ifparam = interface_param;
	while(ifparam->name) {
		if (ifparam->name[0]) {
			printf("\nParameter: %s %s\n", ifparam->name, ifparam->usage);
			printf("%s\n", ifparam->help);
		}
		ifparam++;
	}
}


/* screen caller id
 * out==0: incoming caller id, out==1: outgoing caller id
 */
void do_screen(int out, char *id, int idsize, int *type, int *present, struct interface *interface)
{
	char			*msn1;
	struct interface_msn	*ifmsn;
	struct interface_screen	*ifscreen;
	char suffix[64];

	/* screen incoming caller id */
	if (!out) {
		/* check for MSN numbers, use first MSN if no match */
		msn1 = NULL;
		ifmsn = interface->ifmsn;
		while(ifmsn) {
			if (!msn1)
				msn1 = ifmsn->msn;
			if (!strcmp(ifmsn->msn, id)) {
				break;
			}
			ifmsn = ifmsn->next;
		}
		if (ifmsn) {
			start_trace(-1, interface, numberrize_callerinfo(id, *type, options.national, options.international), NULL, DIRECTION_IN, 0, 0, "SCREEN (found in MSN list)");
			add_trace("msn", NULL, "%s", id);
			end_trace();
		}
		if (!ifmsn && msn1) { // not in list, first msn given
			start_trace(-1, interface, numberrize_callerinfo(id, *type, options.national, options.international), NULL, DIRECTION_IN, 0, 0, "SCREEN (not found in MSN list)");
			add_trace("msn", "given", "%s", id);
			add_trace("msn", "used", "%s", msn1);
			end_trace();
			UNCPY(id, msn1, idsize);
			id[idsize-1] = '\0';
		}
	}

	/* check screen list */
	if (out)
		ifscreen = interface->ifscreen_out;
	else
		ifscreen = interface->ifscreen_in;
	while (ifscreen) {
		if (ifscreen->match_type==-1 || ifscreen->match_type==*type)
		if (ifscreen->match_present==-1 || ifscreen->match_present==*present) {
			if (strchr(ifscreen->match,'%')) {
				if (!strncmp(ifscreen->match, id, strchr(ifscreen->match,'%')-ifscreen->match))
					break;
			} else {
				if (!strcmp(ifscreen->match, id))
					break;
			}
		}
		ifscreen = ifscreen->next;
	}
	if (ifscreen) { // match
		start_trace(-1, interface, numberrize_callerinfo(id, *type, options.national, options.international), NULL, out?DIRECTION_OUT:DIRECTION_IN, 0, 0, "SCREEN (found in screen list)");
		switch(*type) {
			case INFO_NTYPE_UNKNOWN:
			add_trace("given", "type", "unknown");
			break;
			case INFO_NTYPE_SUBSCRIBER:
			add_trace("given", "type", "subscriber");
			break;
			case INFO_NTYPE_NATIONAL:
			add_trace("given", "type", "national");
			break;
			case INFO_NTYPE_INTERNATIONAL:
			add_trace("given", "type", "international");
			break;
		}
		switch(*present) {
			case INFO_PRESENT_ALLOWED:
			add_trace("given", "present", "allowed");
			break;
			case INFO_PRESENT_RESTRICTED:
			add_trace("given", "present", "restricted");
			break;
			case INFO_PRESENT_NOTAVAIL:
			add_trace("given", "present", "not available");
			break;
		}
		add_trace("given", "id", "%s", id[0]?id:"<empty>");
		if (ifscreen->result_type != -1) {
			*type = ifscreen->result_type;
			switch(*type) {
				case INFO_NTYPE_UNKNOWN:
				add_trace("used", "type", "unknown");
				break;
				case INFO_NTYPE_SUBSCRIBER:
				add_trace("used", "type", "subscriber");
				break;
				case INFO_NTYPE_NATIONAL:
				add_trace("used", "type", "national");
				break;
				case INFO_NTYPE_INTERNATIONAL:
				add_trace("used", "type", "international");
				break;
			}
		}
		if (ifscreen->result_present != -1) {
			*present = ifscreen->result_present;
			switch(*present) {
				case INFO_PRESENT_ALLOWED:
				add_trace("used", "present", "allowed");
				break;
				case INFO_PRESENT_RESTRICTED:
				add_trace("used", "present", "restricted");
				break;
				case INFO_PRESENT_NOTAVAIL:
				add_trace("used", "present", "not available");
				break;
			}
		}
		if (strchr(ifscreen->match,'%')) {
			SCPY(suffix, strchr(ifscreen->match,'%') - ifscreen->match + id);
			UNCPY(id, ifscreen->result, idsize);
			id[idsize-1] = '\0';
			if (strchr(id,'%')) {
				*strchr(id,'%') = '\0';
				UNCAT(id, suffix, idsize);
				id[idsize-1] = '\0';
			}
		} else {
			UNCPY(id, ifscreen->result, idsize);
			id[idsize-1] = '\0';
		}
		add_trace("used", "id", "%s", id[0]?id:"<empty>");
		end_trace();
	}
}