forked from mohnkhan/xavl2tp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipv6file.c
executable file
·1078 lines (1035 loc) · 29.5 KB
/
ipv6file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Layer Two Tunnelling Protocol Daemon
* Copyright (C) 1998 Adtran, Inc.
* Copyright (C) 2002 Jeff McAdams
*
* Mark Spencer
*
* This software is distributed under the terms
* of the GPL, which you should have received
* along with this source.
*
* File format handling
*
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "l2tp.h"
struct lns6 *lnslist6;
struct lac6 *laclist6;
struct lns6 *deflns6;
struct lac6 *deflac6;
//struct global gconfig;//TODO: use original global gconfig
char filerr[STRLEN];
int parse_config6 (FILE *);
struct keyword words6[];
//RY: start
//TODO: modified for INADDR_ANY: done
int init_config6 ()
{
FILE *f;
int returnedValue;
gconfig.port = UDP_LISTEN_PORT;
//TODO: need to change to in6addr_any as it is INADDR_ANY in ipv4
// Default is to bind (listen) to all interfaces
// Assigning zero to listenaddr6 (IN6ADDR_INIT_ANY)
// memset((void*) &gconfig.ipaddr.listenaddr6, 0, 16);
gconfig.ipaddr.listenaddr6 = in6addr_any;
gconfig.debug_avp = 0;
gconfig.debug_network = 0;
gconfig.packet_dump = 0;
gconfig.debug_tunnel = 0;
gconfig.debug_state = 0;
lnslist6 = NULL;
laclist6 = NULL;
deflac6 = (struct lac6 *) malloc (sizeof (struct lac6));
f = fopen (gconfig.configfile, "r");
if (!f)
{
f = fopen (gconfig.altconfigfile, "r");
if (f)
{
l2tp_log (LOG_WARNING, "%s: Using old style config files %s and %s\n",
__FUNCTION__, gconfig.altconfigfile, gconfig.altauthfile);
strncpy (gconfig.authfile, gconfig.altauthfile,
sizeof (gconfig.authfile));
}
else
{
l2tp_log (LOG_CRIT, "%s: Unable to open config file %s or %s\n",
__FUNCTION__, gconfig.configfile, gconfig.altconfigfile);
return -1;
}
}
returnedValue = parse_config6 (f);
fclose (f);
return (returnedValue);
filerr[0] = 0;
}
struct lns6 *new_lns6 ()
{
struct lns6 *tmp;
tmp = (struct lns6 *) malloc (sizeof (struct lns6));
if (!tmp)
{
l2tp_log (LOG_CRIT, "%s: Unable to allocate memory for new lns6\n",
__FUNCTION__);
return NULL;
}
tmp->next = NULL;
tmp->exclusive = 0;
//tmp->localaddr = 0;
memset((char*)tmp->localaddr,0,16);//RY:
tmp->tun_rws = DEFAULT_RWS_SIZE;
tmp->call_rws = DEFAULT_RWS_SIZE;
tmp->hbit = 0;
tmp->lbit = 0;
tmp->authpeer = 0;
tmp->authself = -1;
tmp->authname[0] = 0;
tmp->peername[0] = 0;
tmp->hostname[0] = 0;
tmp->entname[0] = 0;
tmp->range = NULL;
tmp->assign_ip = 1; /* default to 'yes' */
tmp->lacs = NULL;
tmp->passwdauth = 0;
tmp->pap_require = 0;
tmp->pap_refuse = 0;
tmp->chap_require = 0;
tmp->chap_refuse = 0;
tmp->idle = 0;
tmp->pridns = 0;
tmp->secdns = 0;
tmp->priwins = 0;
tmp->secwins = 0;
tmp->proxyarp = 0;
tmp->proxyauth = 0;
tmp->challenge = 0;
tmp->debug = 0;
tmp->pppoptfile[0] = 0;
tmp->t = NULL;
return tmp;
}
struct lac6 *new_lac6 ()
{
struct lac6 *tmp;
tmp = (struct lac6 *) malloc (sizeof (struct lac6));
if (!tmp)
{
l2tp_log (LOG_CRIT, "%s: Unable to allocate memory for lac entry!\n",
__FUNCTION__);
return NULL;
}
tmp->next = NULL;
tmp->rsched = NULL;
memset((char*)tmp->localaddr,0,16); //RY: since ipv6 is denoted in array
//tmp->remoteaddr = 0;//RY: since, ipv6 is denoted in array
memset((char*)tmp->remoteaddr, 0, 16);
tmp->lns = 0;
tmp->tun_rws = DEFAULT_RWS_SIZE;
tmp->call_rws = DEFAULT_RWS_SIZE;
tmp->hbit = 0;
tmp->lbit = 0;
tmp->authpeer = 0;
tmp->authself = -1;
tmp->authname[0] = 0;
tmp->peername[0] = 0;
tmp->hostname[0] = 0;
tmp->entname[0] = 0;
tmp->pap_require = 0;
tmp->pap_refuse = 0;
tmp->chap_require = 0;
tmp->chap_refuse = 0;
tmp->t = NULL;
tmp->redial = 0;
tmp->rtries = 0;
tmp->rmax = 0;
tmp->challenge = 0;
tmp->autodial = 0;
tmp->rtimeout = 30;
tmp->active = 0;
tmp->debug = 0;
tmp->pppoptfile[0] = 0;
tmp->defaultroute = 0;
return tmp;
}
int set_rws6 (char *word, char *value, int context, void *item)
{
if (atoi (value) < -1)
{
snprintf (filerr, sizeof (filerr),
"receive window size must be at least -1\n");
return -1;
}
switch (context & ~CONTEXT_DEFAULT)
{
case CONTEXT_LAC:
if (word[0] == 'c')
set_int (word, value, &(((struct lac6 *) item)->call_rws));
if (word[0] == 't')
{
set_int (word, value, &(((struct lac6 *) item)->tun_rws));
if (((struct lac6 *) item)->tun_rws < 1)
{
snprintf (filerr, sizeof (filerr),
"receive window size for tunnels must be at least 1\n");
return -1;
}
}
break;
case CONTEXT_LNS:
if (word[0] == 'c')
set_int (word, value, &(((struct lns6 *) item)->call_rws));
if (word[0] == 't')
{
set_int (word, value, &(((struct lns6 *) item)->tun_rws));
if (((struct lns6 *) item)->tun_rws < 1)
{
snprintf (filerr, sizeof (filerr),
"receive window size for tunnels must be at least 1\n");
return -1;
}
}
break;
default:
snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
word);
return -1;
}
return 0;
}
//RY: uses lac6, so redifned again.
int set_autodial6 (char *word, char *value, int context, void *item)
{
switch (context & ~CONTEXT_DEFAULT)
{
case CONTEXT_LAC:
if (set_boolean (word, value, &(((struct lac6 *) item)->autodial)))
return -1;
break;
default:
snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
word);
return -1;
}
return 0;
}
int set_flow6 (char *word, char *value, int context, void *item)
{
int v;
set_boolean (word, value, &v);
if (v < 0)
return -1;
switch (context & ~CONTEXT_DEFAULT)
{
case CONTEXT_LAC:
if (v)
{
if (((struct lac6 *) item)->call_rws < 0)
((struct lac6 *) item)->call_rws = 0;
}
else
{
((struct lac6 *) item)->call_rws = -1;
}
break;
case CONTEXT_LNS:
if (v)
{
if (((struct lns6 *) item)->call_rws < 0)
((struct lns6 *) item)->call_rws = 0;
}
else
{
((struct lns6 *) item)->call_rws = -1;
}
break;
default:
snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
word);
return -1;
}
return 0;
}
int set_defaultroute6 (char *word, char *value, int context, void *item)
{
switch (context & ~CONTEXT_DEFAULT)
{
case CONTEXT_LAC:
if (set_boolean (word, value, &(((struct lac6 *) item)->defaultroute)))
return -1;
break;
default:
snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
word);
return -1;
}
return 0;
}
int set_authname6 (char *word, char *value, int context, void *item)
{
struct lac6 *l = (struct lac6 *) item;
struct lns6 *n = (struct lns6 *) item;
switch (context & ~CONTEXT_DEFAULT)
{
case CONTEXT_LNS:
if (set_string (word, value, n->authname, sizeof (n->authname)))
return -1;
break;
case CONTEXT_LAC:
if (set_string (word, value, l->authname, sizeof (l->authname)))
return -1;
break;
default:
snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
word);
return -1;
}
return 0;
}
int set_hostname6 (char *word, char *value, int context, void *item)
{
struct lac6 *l = (struct lac6 *) item;
struct lns6 *n = (struct lns6 *) item;
switch (context & ~CONTEXT_DEFAULT)
{
case CONTEXT_LNS:
if (set_string (word, value, n->hostname, sizeof (n->hostname)))
return -1;
break;
case CONTEXT_LAC:
if (set_string (word, value, l->hostname, sizeof (l->hostname)))
return -1;
break;
default:
snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
word);
return -1;
}
return 0;
}
int set_passwdauth6 (char *word, char *value, int context, void *item)
{
switch (context & ~CONTEXT_DEFAULT)
{
case CONTEXT_LNS:
if (set_boolean (word, value, &(((struct lns6 *) item)->passwdauth)))
return -1;
break;
default:
snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
word);
return -1;
}
return 0;
}
int set_hbit6 (char *word, char *value, int context, void *item)
{
switch (context & ~CONTEXT_DEFAULT)
{
case CONTEXT_LAC:
if (set_boolean (word, value, &(((struct lac6 *) item)->hbit)))
return -1;
break;
case CONTEXT_LNS:
if (set_boolean (word, value, &(((struct lns6 *) item)->hbit)))
return -1;
break;
default:
snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
word);
return -1;
}
return 0;
}
int set_challenge6 (char *word, char *value, int context, void *item)
{
switch (context & ~CONTEXT_DEFAULT)
{
case CONTEXT_LAC:
if (set_boolean (word, value, &(((struct lac6 *) item)->challenge)))
return -1;
break;
case CONTEXT_LNS:
if (set_boolean (word, value, &(((struct lns6 *) item)->challenge)))
return -1;
break;
default:
snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
word);
return -1;
}
return 0;
}
int set_lbit6 (char *word, char *value, int context, void *item)
{
switch (context & ~CONTEXT_DEFAULT)
{
case CONTEXT_LAC:
if (set_boolean (word, value, &(((struct lac6 *) item)->lbit)))
return -1;
break;
case CONTEXT_LNS:
if (set_boolean (word, value, &(((struct lns6 *) item)->lbit)))
return -1;
break;
default:
snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
word);
return -1;
}
return 0;
}
int set_debug6 (char *word, char *value, int context, void *item)
{
switch (context & ~CONTEXT_DEFAULT)
{
case CONTEXT_LAC:
if (set_boolean (word, value, &(((struct lac6 *) item)->debug)))
return -1;
break;
case CONTEXT_LNS:
if (set_boolean (word, value, &(((struct lns6 *) item)->debug)))
return -1;
break;
default:
snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
word);
return -1;
}
return 0;
}
int set_pppoptfile6 (char *word, char *value, int context, void *item)
{
struct lac6 *l = (struct lac6 *) item;
struct lns6 *n = (struct lns6 *) item;
switch (context & ~CONTEXT_DEFAULT)
{
case CONTEXT_LNS:
if (set_string (word, value, n->pppoptfile, sizeof (n->pppoptfile)))
return -1;
break;
case CONTEXT_LAC:
if (set_string (word, value, l->pppoptfile, sizeof (l->pppoptfile)))
return -1;
break;
default:
snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
word);
return -1;
}
return 0;
}
int set_papchap6 (char *word, char *value, int context, void *item)
{
int result;
char *c;
struct lac6 *l = (struct lac6 *) item;
struct lns6 *n = (struct lns6 *) item;
if (set_boolean (word, value, &result))
return -1;
c = strchr (word, ' ');
c++;
switch (context & ~CONTEXT_DEFAULT)
{
case CONTEXT_LAC:
if (c[0] == 'p') /* PAP */
if (word[2] == 'f')
l->pap_refuse = result;
else
l->pap_require = result;
else if (c[0] == 'a') /* Authentication */
if (word[2] == 'f')
l->authself = result;
else
l->authpeer = result;
else /* CHAP */ if (word[2] == 'f')
l->chap_refuse = result;
else
l->chap_require = result;
break;
case CONTEXT_LNS:
if (c[0] == 'p') /* PAP */
if (word[2] == 'f')
n->pap_refuse = result;
else
n->pap_require = result;
else if (c[0] == 'a') /* Authentication */
if (word[2] == 'f')
n->authself = !result;
else
n->authpeer = result;
else /* CHAP */ if (word[2] == 'f')
n->chap_refuse = result;
else
n->chap_require = result;
break;
default:
snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
word);
return -1;
}
return 0;
}
int set_redial6 (char *word, char *value, int context, void *item)
{
switch (context & ~CONTEXT_DEFAULT)
{
case CONTEXT_LAC:
if (set_boolean (word, value, &(((struct lac6 *) item)->redial)))
return -1;
break;
default:
snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
word);
return -1;
}
return 0;
}
int set_assignip6 (char *word, char *value, int context, void *item)
{
switch (context & ~CONTEXT_DEFAULT)
{
case CONTEXT_LNS:
if (set_boolean (word, value, &(((struct lns6 *) item)->assign_ip)))
return -1;
break;
default:
snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
word);
return -1;
}
return 0;
}
//RY: start here
//TODO: need to check wrt to socket related stuff
struct iprange6 *set_range6 (char *word, char *value, struct iprange6 *in)
{
char *c, *d = NULL, *e = NULL;
struct iprange6 *ipr, *p;
struct sockaddr_in6 *Temp;
struct addrinfo myaddr, *hp;
int count = 0;
c = strchr (value, '-');
if (c)
{
d = c + 1;
*c = 0;
while ((c >= value) && (*c < 33))
*(c--) = 0;
while (*d && (*d < 33))
d++;
}
if (!strlen (value) || (c && !strlen (d)))
{
snprintf (filerr, sizeof (filerr),
"format is '%s <host or ip> - <host or ip>'\n", word);
return NULL;
}
ipr = (struct iprange6 *) malloc (sizeof (struct iprange6));
ipr->next = NULL;
//RY: start
memset(&myaddr, 0, sizeof(myaddr));
myaddr.ai_family = PF_INET6;
myaddr.ai_flags = AI_PASSIVE;
getaddrinfo(value, NULL, &myaddr, &hp );
//hp = gethostbyname (value);
//RY: end
if (!hp)
{
snprintf (filerr, sizeof (filerr), "Unknown host %s\n", value);
free (ipr);
return NULL;
}
Temp = (struct sockaddr_in6*)hp->ai_addr;//RY: typecasting into sockaddr_in6 type
bcopy (&Temp->sin6_addr.s6_addr16[0], &ipr->start[0], sizeof(Temp->sin6_addr.s6_addr16[0]));// result is in addr
//bcopy (hp->h_addr, &ipr->start, sizeof (unsigned int));
if (c)
{
e = d;
while(*e != '\0') {
if (*e++ == '.')
count++;
}
if (count != 3) {
char ip_hi[16];
strcpy(ip_hi, value);
e = strrchr(ip_hi, '.')+1;
// Copy the last field + null terminator
strcpy(e, d);
d = ip_hi;
}
//hp = gethostbyname (d);
getaddrinfo(d, NULL, &myaddr, &hp ); //RY: myaddr has already been filled.
if (!hp)
{
snprintf (filerr, sizeof (filerr), "Unknown host %s\n", d);
free (ipr);
return NULL;
}
//bcopy (hp->h_addr, &ipr->end, sizeof (unsigned int));
Temp = (struct sockaddr_in6*)hp->ai_addr; //RY: typecast to sockaddr_in6
bcopy (&Temp->sin6_addr.s6_addr16[0], &ipr->end[0], sizeof(Temp->sin6_addr));// result in ipr_end
}
else
//ipr->end = ipr->start; //RY: as these are represented as array now.
memcpy(ipr->end, ipr->start, sizeof(ipr->start));
if (0 /*ntohl (ipr->start) > ntohl (ipr->end)*/) //TODO: diable for testing
{
snprintf (filerr, sizeof (filerr), "start is greater than end!\n");
free (ipr);
return NULL;
}
if (word[0] == 'n')
ipr->sense = SENSE_DENY;
else
ipr->sense = SENSE_ALLOW;
p = in;
if (p)
{
while (p->next)
p = p->next;
p->next = ipr;
return in;
}
else
return ipr;
}
//RY: end here
int set_iprange6 (char *word, char *value, int context, void *item)
{
struct lns6 *lns6 = (struct lns6 *) item;
switch (context & ~CONTEXT_DEFAULT)
{
case CONTEXT_LNS:
break;
default:
snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
word);
return -1;
}
lns6->range = set_range6 (word, value, lns6->range);
if (!lns6->range)
return -1;
#ifdef DEBUG_FILE
l2tp_log (LOG_DEBUG, "range start = %x, end = %x, sense=%ud\n",
ntohl (lns6->range->start), ntohl (lns6->range->end), lns6->range->sense);
#endif
return 0;
}
int set_lac6 (char *word, char *value, int context, void *item)
{
struct lns6 *lns6 = (struct lns6 *) item;
switch (context & ~CONTEXT_DEFAULT)
{
case CONTEXT_LNS:
break;
default:
snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
word);
return -1;
}
lns6->lacs = set_range6 (word, value, lns6->lacs);
if (!lns6->lacs)
return -1;
#ifdef DEBUG_FILE
l2tp_log (LOG_DEBUG, "lac start = %x, end = %x, sense=%ud\n",
ntohl (lns6->lacs->start), ntohl (lns6->lacs->end), lns6->lacs->sense);
#endif
return 0;
}
int set_exclusive6 (char *word, char *value, int context, void *item)
{
switch (context & ~CONTEXT_DEFAULT)
{
case CONTEXT_LNS:
if (set_boolean (word, value, &(((struct lns6 *) item)->exclusive)))
return -1;
break;
default:
snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
word);
return -1;
}
return 0;
}
int set_localaddr6 (char *word, char *value, int context, void *item)
{
struct lac6 *l;
struct lns6 *n;
switch (context & ~CONTEXT_DEFAULT)
{
case CONTEXT_LAC:
l = (struct lac6 *) item;
return set_ip6 (word, value, &(l->localaddr[0]));
case CONTEXT_LNS:
n = (struct lns6 *) item;
return set_ip6 (word, value, &(n->localaddr[0]));
default:
snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
word);
return -1;
}
return 0;
}
int set_remoteaddr6 (char *word, char *value, int context, void *item)
{
struct lac6 *l;
switch (context & ~CONTEXT_DEFAULT)
{
case CONTEXT_LAC:
l = (struct lac6 *) item;
return set_ip6 (word, value, (l->remoteaddr)); //TODO: check fr warning
default:
snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
word);
return -1;
}
return 0;
}
int set_lns6 (char *word, char *value, int context, void *item)
{
#if 0
struct hostent *hp;
#endif
struct lac6 *l;
struct host *ipr, *pos;
char *d;
switch (context & ~CONTEXT_DEFAULT)
{
case CONTEXT_LAC:
#ifdef DEBUG_FILE
l2tp_log (LOG_DEBUG, "set_lns6: setting LNS to '%s'\n", value);
#endif
l = (struct lac6 *) item;
d = strchr (value, ':');
if (d)
{
d[0] = 0;
d++;
}
#if 0
// why would you want to lookup hostnames at this time?
hp = gethostbyname (value);
if (!hp)
{
snprintf (filerr, sizeof (filerr), "no such host '%s'\n", value);
return -1;
}
#endif
ipr = malloc (sizeof (struct host));
ipr->next = NULL;
pos = l->lns;
if (!pos)
{
l->lns = ipr;
}
else
{
while (pos->next)
pos = pos->next;
pos->next = ipr;
}
strncpy (ipr->hostname, value, sizeof (ipr->hostname));
if (d)
ipr->port = atoi (d);
else
ipr->port = UDP_LISTEN_PORT;
break;
default:
snprintf (filerr, sizeof (filerr), "'%s' not valid in this context\n",
word);
return -1;
}
return 0;
}
int parse_config6 (FILE * f)
{
/* Read in the configuration file handed to us */
/* FIXME: I should check for incompatible options */
int context = 0;
char buf[STRLEN];
char *s, *d, *t;
int linenum = 0;
int def = 0;
struct keyword *kw;
void *data = NULL;
struct lns6 *tl;
struct lac6 *tc;
while (!feof (f))
{
fgets (buf, sizeof (buf), f);
if (feof (f))
break;
linenum++;
s = buf;
/* Strip comments */
while (*s && *s != ';')
s++;
*s = 0;
s = buf;
if (!strlen (buf))
continue;
while ((*s < 33) && *s)
s++; /* Skip over beginning white space */
t = s + strlen (s);
while ((t >= s) && (*t < 33))
*(t--) = 0; /* Ditch trailing white space */
if (!strlen (s))
continue;
if (s[0] == '[')
{
/* We've got a context description */
if (!(t = strchr (s, ']')))
{
l2tp_log (LOG_CRIT, "parse_config: line %d: No closing bracket\n",
linenum);
return -1;
}
t[0] = 0;
s++;
if ((d = strchr (s, ' ')))
{
/* There's a parameter */
d[0] = 0;
d++;
}
if (d && !strcasecmp (d, "default"))
def = CONTEXT_DEFAULT;
else
def = 0;
if (!strcasecmp (s, "global"))
{
context = CONTEXT_GLOBAL;
#ifdef DEBUG_FILE
l2tp_log (LOG_DEBUG,
"parse_config: global context descriptor %s\n",
d ? d : "");
#endif
data = &gconfig;
}
else if (!strcasecmp (s, "lns6"))
{
context = CONTEXT_LNS;
if (def)
{
if (!deflns6)
{
deflns6 = new_lns6 ();
strncpy (deflns6->entname, "default",
sizeof (deflns6->entname));
}
data = deflns6;
continue;
}
data = NULL;
tl = lnslist6;
if (d)
{
while (tl)
{
if (!strcasecmp (d, tl->entname))
break;
tl = tl->next;
}
if (tl)
data = tl;
}
if (!data)
{
data = new_lns6 ();
if (!data)
return -1;
((struct lns6 *) data)->next = lnslist6;
lnslist6 = (struct lns6 *) data;
}
if (d)
strncpy (((struct lns6 *) data)->entname,
d, sizeof (((struct lns6 *) data)->entname));
#ifdef DEBUG_FILE
l2tp_log (LOG_DEBUG, "parse_config: lns context descriptor %s\n",
d ? d : "");
#endif
}
else if (!strcasecmp (s, "lac6"))
{
context = CONTEXT_LAC;
if (def)
{
if (!deflac6)
{
deflac6 = new_lac6 ();
strncpy (deflac6->entname, "default",
sizeof (deflac6->entname));
}
data = deflac6;
continue;
}
data = NULL;
tc = laclist6;
if (d)
{
while (tc)
{
if (!strcasecmp (d, tc->entname))
break;
tc = tc->next;
}
if (tc)
data = tc;
}
if (!data)
{
data = new_lac6 ();
if (!data)
return -1;
((struct lac6 *) data)->next = laclist6;
laclist6 = (struct lac6 *) data;
}
if (d)
strncpy (((struct lac6 *) data)->entname,
d, sizeof (((struct lac6 *) data)->entname));
#ifdef DEBUG_FILE
l2tp_log (LOG_DEBUG, "parse_config: lac context descriptor %s\n",
d ? d : "");
#endif
}
else
{
l2tp_log (LOG_WARNING,
"parse_config: line %d: unknown context '%s'\n", linenum,
s);
return -1;
}
}
else
{
if (!context)
{
l2tp_log (LOG_WARNING,
"parse_config: line %d: data '%s' occurs with no context\n",
linenum, s);
return -1;
}
if (!(t = strchr (s, '=')))
{
l2tp_log (LOG_WARNING, "parse_config: line %d: no '=' in data\n",
linenum);
return -1;
}
d = t;
d--;
t++;
while ((d >= s) && (*d < 33))
d--;
d++;
*d = 0;
while (*t && (*t < 33))
t++;
#ifdef DEBUG_FILE
l2tp_log (LOG_DEBUG, "parse_config: field is %s, value is %s\n", s, t);
#endif
/* Okay, bit twidling is done. Let's handle this */
for (kw = words6; kw->keyword; kw++)
{
if (!strcasecmp (s, kw->keyword))
{
if (kw->handler (s, t, context | def, data))
{
l2tp_log (LOG_WARNING, "parse_config: line %d: %s", linenum,
filerr);
return -1;
}
break;
}
}
if (!kw->keyword)
{
l2tp_log (LOG_CRIT, "parse_config: line %d: Unknown field '%s'\n",
linenum, s);
return -1;
}
}
}
return 0;