-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathparser.c
1744 lines (1584 loc) · 43.6 KB
/
parser.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
#include <expat.h>
#include <sys/stat.h>
#include "lib.h"
#ifdef XML_LARGE_SIZE
#if defined(XML_USE_MSC_EXTENSIONS) && _MSC_VER < 1400
#define XML_FMT_INT_MOD "I64"
#else
#define XML_FMT_INT_MOD "ll"
#endif
#else
#define XML_FMT_INT_MOD "l"
#endif
static XML_Parser parser;
static const char *_xml_name;
static char *_prefix;
static struct mapping_flags _mflags;
static int _fam = AF_INET;
static void *_mapping;
u_char _fncs;
u_char lisp_te=0;
u_char srcport_rand = 1;
char *config_file[6];
/* compare map entry ip addresses
* ip1 > ip2 --> 1
* ip2 > ip1 --> -1
* ip2 = ip1 --> 0
* Note: ipv6 > ipv4
*/
int
_map_entry_cmp(void *data, void *entry)
{
union sockunion *ins_s, *exis_s;
int mmask = 0;
uint8_t *ins_ip, *exis_ip;
ins_ip = exis_ip = NULL;
ins_s = &((struct map_entry *)data)->rloc;
exis_s = &((struct map_entry *)entry)-> rloc;
if (ins_s->sa.sa_family != exis_s->sa.sa_family) {
if (ins_s->sa.sa_family == AF_INET)
/* ins_s (ipv4) < exis_s (ipv6) */
return -1;
else
/* ins_s (ipv6) > exis_s (ipv4) */
return 1;
}
/* same afi */
switch (ins_s->sa.sa_family) {
case AF_INET:
mmask = 4;
ins_ip = (uint8_t *)&ins_s->sin.sin_addr;
exis_ip = (uint8_t *)&exis_s->sin.sin_addr;
break;
case AF_INET6:
mmask = 16;
ins_ip = (uint8_t *)&ins_s->sin6.sin6_addr;
exis_ip = (uint8_t *)&exis_s->sin6.sin6_addr;
}
while (ins_ip && exis_ip && mmask > 0 && (*ins_ip == *exis_ip) ) {
ins_ip++;
exis_ip++;
mmask--;
}
if (mmask == 0)
return 0;
if (*ins_ip > *exis_ip)
return 1;
else
return -1;
}
int
get_my_addr(int afi, union sockunion *sk)
{
struct ifaddrs *ifap, *ifa;
char buf[NI_MAXHOST];
if (getifaddrs(&ifap) == -1)
return -1;
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
/* ignore: */
/* interface with not ip or not sam*/
if (ifa->ifa_addr == NULL)
continue;
/* interface with not same afi */
if (ifa->ifa_addr->sa_family != afi)
continue;
/* look back interface */
if (getnameinfo(ifa->ifa_addr,SA_LEN(ifa->ifa_addr->sa_family),
buf,NI_MAXHOST,NULL,0,NI_NUMERICHOST) != 0) {
continue;
}
if (!(strcmp(LOOPBACK,buf) && strcmp(LOOPBACK6,buf) && strncmp(LINK_LOCAL,buf,LINK_LOCAL_LEN)))
continue;
/* compare addr */
switch (afi) {
case AF_INET:
memcpy((void *)&sk->sin.sin_addr, (void *)&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr,
sizeof(struct in_addr));
goto exit_fnc;
case AF_INET6:
memcpy((void *)&sk->sin6.sin6_addr, (void *)&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr,
sizeof(struct in6_addr));
goto exit_fnc;
}
}
return -1;
exit_fnc:
freeifaddrs(ifap);
return 0;
}
void
_err_config(char *err_msg) {
printf("Error Configure file: %s, at line %" XML_FMT_INT_MOD "u\n",err_msg,XML_GetCurrentLineNumber(parser));
cp_log(LLOG, "Error Configure file: %s, at line %" XML_FMT_INT_MOD "u\n",err_msg,XML_GetCurrentLineNumber(parser));
}
/* validate if an eid-prefix is overlap or not
xTR EID must belong to root
GEID, GREID must only belong to root and not overlap
EID must belong to GEID and overlap each other
EID-referral must belong to GREID and overlap each other
*/
int
_valid_prefix(struct prefix *p, int type)
{
struct db_node *rn = NULL;
struct db_table *table;
table = ms_get_db_table(ms_db,p);
rn = db_node_match_prefix(table, p);
/* database seem empty */
if (!rn)
return 1;
/* duplicate EID */
if (rn && rn->p.prefixlen == p->prefixlen)
return 2;
switch (type) {
case _MAPP_XTR:
/* EID-prefix of xTR must have target is root but can not overlap*/
while (rn != table->top && !rn->flags)
rn = rn->parent;
return (rn == table->top || ((struct mapping_flags *)rn->flags)->range == _MAPP_XTR);
break;
case _GEID:
case _GREID:
/* _GEID prefix must have target is root and not overlap */
while (rn != table->top && !rn->flags)
rn = rn->parent;
return (rn == table->top);
break;
case _EID:
/* _EID prefix must have target is one of _GEID*/
while (rn != table->top && !rn->flags)
rn = rn->parent;
return (rn == table->top || ((struct mapping_flags *)rn->flags)->range & (_GEID | _EID));
break;
case _REID:
/* _EID prefix must have target is one of _GREID*/
while (rn != table->top && !rn->flags)
rn = rn->parent;
return (rn == table->top || ((struct mapping_flags *)rn->flags)->range == _GREID);
break;
}
return 0;
}
/* calc sum of weight of rloc chain for a priority */
int
sw_rloc(struct list_t *rlc, uint8_t priority)
{
int sw = 0;
struct list_entry_t *iter;
struct map_entry *me;
if (!rlc)
return -1;
iter = rlc->head.next;
while (iter != &rlc->tail) {
if (!(me = (struct map_entry *)iter->data))
return -1;
if (me->priority == priority)
sw += me->weight;
iter = iter->next;
}
return sw;
}
/* calc sum of weight of elp chain for a priority */
int
sw_elp(struct list_t *elp, uint8_t priority)
{
int sw = 0;
struct list_entry_t *iter;
struct pe_entry *pe;
if (!elp)
return -1;
iter = elp->head.next;
while (iter != &elp->tail) {
if (!(pe = (struct pe_entry *)iter->data))
return -1;
if (pe->priority == priority)
sw += pe->weight;
iter = iter->next;
}
return sw;
}
int
_get_file_size(const char *filename)
{
struct stat sb;
if (stat(filename, &sb) == 0)
return sb.st_size;
else
return -1;
}
int
xml_configure(const char *filename,
void (*startElement)(void *, const char *, const char **),
void (*endElement)(void *, const char *),
void (*getElementValue)(void *, const XML_Char *, int))
{
int done;
int bsize;
char *buf;
FILE *config;
if ((bsize = _get_file_size(filename)) == -1) {
cp_log(LLOG, "Error configure file: can not open file %s\n",filename);
exit(1);
}
buf = (char *)malloc(bsize+1);
bzero(buf, bsize+1);
parser = XML_ParserCreate(NULL);
XML_SetUserData(parser, NULL);
XML_SetStartElementHandler(parser, startElement);
XML_SetEndElementHandler(parser, endElement);
XML_SetCharacterDataHandler(parser, getElementValue);
config = fopen(filename, "r");
do {
unsigned int len = (int)fread(buf, 1, bsize, config);
done = len < sizeof(buf);
if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) {
cp_log(LLOG, "Error Configure file: %s at line %" XML_FMT_INT_MOD "u\n",\
XML_ErrorString(XML_GetErrorCode(parser)),\
XML_GetCurrentLineNumber(parser));
fclose(config);
exit(1);
}
} while (!done);
XML_ParserFree(parser);
fclose(config);
return 0;
}
/* ====================================================================
* Parse for xTR configure
*/
struct ms_entry *xtr_ms_entry;
struct mr_entry *xtr_mr_entry;
struct pe_entry *pe;
struct hop_entry *hop;
static int
ms_match_id(void *id, void *ms_entry)
{
struct ms_entry *ms;
uint8_t *i;
ms = (struct ms_entry *)ms_entry;
i = (uint8_t *)id;
if (ms->id == *i)
return 0;
return 1;
}
static void XMLCALL
xtr_startElement(void *userData, const char *name, const char **atts)
{
int len;
struct map_entry *entry;
char *msids;
if ((0 == strcasecmp(name, "eid_prefix")) ||
(0 == strcasecmp(name, "eid"))) {
msids = NULL;
while (*atts) {
/* EID prefix */
if (0 == strcasecmp(*atts, "ms_ids")) {
atts++;
msids = (char *)*atts;
}
if (0 == strcasecmp(*atts, "prefix")) {
/*get eid-prefix */
struct prefix p1;
atts++;
len = strlen(*atts);
_prefix = (char *)calloc(1, len+1);
memcpy(_prefix, *atts, len);
*(_prefix + len) = '\0';
if (str2prefix (_prefix, &p1) <=0) {
_err_config("invalid prefix");
exit(1);
}
apply_mask(&p1);
/* append to db */
if (!_valid_prefix(&p1, _MAPP_XTR) || !(_mapping = generic_mapping_new(&p1)) ) {
_err_config("invalid prefix");
exit(1);
}
bzero(&_mflags, sizeof(struct mapping_flags));
_mflags.range = _MAPP_XTR;
_mflags.act = 0;
_mflags.A = 1; /* I'm ETR so I owns the EID-prefix */
list_insert(etr_db, _mapping, NULL);
struct list_entry_t *p;
struct ms_entry *ms;
if ((msids != NULL) && (0 != strcasecmp(msids, "all")) && (0 != strcasecmp(msids, "none"))) {
char data[50][255];
char *tk;
char *ptr;
char *sep_t = "; ";
int i = 0, j; /*counter */
uint8_t m;
i = 0;
/*ms_id="ms1; ms2; ...; msn" */
for (tk = strtok_r(msids, sep_t, &ptr); tk ; tk = strtok_r(NULL, sep_t, &ptr))
strcpy(data[i++], tk);
for (j = 0; j < i ; j++) {
/* assign EID to Map-server */
m = atoi(data[j]);
if (xtr_ms && ((p = list_search(xtr_ms,(void *)&m,ms_match_id)) != NULL)) {
ms = (struct ms_entry *)p->data;
list_insert(ms->eids, _mapping, NULL);
}
else{
printf("Error Configure file: MS ID %d invalid, at line %" XML_FMT_INT_MOD "u\n",m, XML_GetCurrentLineNumber(parser));
exit(1);
};
}
}else{/* assign EID to all MS */
if ((msids == NULL) || (msids && (0 != strcasecmp(msids, "none")) )) {
if (xtr_ms) {
p = xtr_ms->head.next;
while (p != &xtr_ms->tail) {
ms = (struct ms_entry *)p->data;
list_insert(ms->eids, _mapping, NULL);
p = p->next;
}
}
}
};
}
/* ACT bits */
if (0 == strcasecmp(*atts, "act")) {
atts++;
_mflags.act = atoi(*atts);
}
/* Echo-noncable */
if (0 == strcasecmp(*atts, "a")) {
atts++;
_mflags.A = (strcasecmp(*atts, "true")==0);
}
/* Version */
if (0 == strcasecmp(*atts, "version")) {
atts++;
_mflags.version = atoi(*atts);
}
/* TTL */
if (0 == strcasecmp(*atts, "ttl")) {
atts++;
_mflags.ttl = atoi(*atts);
}
/**/
atts++;
}
} else {
if (0 == strcasecmp(name, "address") ||
0 == strcasecmp(name, "ms") ||
0 == strcasecmp(name, "mr") ||
0 == strcasecmp(name, "petr") ||
0 == strcasecmp(name, "elp")||
0 == strcasecmp(name, "hop") ) {
if (0 == strcasecmp(name, "ms")) {
xtr_ms_entry = calloc(1, sizeof(struct ms_entry));
xtr_ms_entry->id = -1;
xtr_ms_entry->proxy = 0;
xtr_ms_entry->eids = list_init();
list_insert(xtr_ms, xtr_ms_entry, NULL);
}
if (0 == strcasecmp(name, "elp")) {
pe = calloc(1,sizeof(struct pe_entry));
}
if (0 == strcasecmp(name, "address") )
_fam = AF_INET;
if (0 == strcasecmp(name, "hop")) {
_fam = AF_INET;
hop = calloc(1, sizeof(struct hop_entry));
}
while (*atts) {
if (0 == strcasecmp(*atts, "family")) {
atts++;
_fam = (0 == strcasecmp(*atts, "IPv6"))?AF_INET6:AF_INET;
}
if (0 == strcasecmp(*atts, "id")) {
struct list_entry_t *pr;
atts++;
xtr_ms_entry->id = atoi(*atts);
if (xtr_ms && (pr = list_search(xtr_ms, (void *)&(xtr_ms_entry->id),
ms_match_id)) && (pr->data != xtr_ms_entry) ) {
_err_config("duplicate Map server id");
pr = xtr_ms->head.next;
exit(1);
}
}
if (0 == strcasecmp(*atts, "key")) {
atts++;
len = strlen(*atts);
xtr_ms_entry->key = (char *)calloc(1, len+1);
memcpy(xtr_ms_entry->key, *atts, len);
xtr_ms_entry->key[len] = '\0';
}
if (0 == strcasecmp(*atts, "proxy")) {
atts++;
xtr_ms_entry->proxy = (strncasecmp(*atts,"yes",3) == 0)? _ACTIVE: _NOACTIVE;
}
if (pe) {
if (0 == strcasecmp(*atts, "priority")) {
atts++;
pe->priority = atoi(*atts);
}
if (0 == strcasecmp(*atts, "m_priority")) {
atts++;
pe->m_priority = atoi(*atts);
}
if (0 == strcasecmp(*atts, "weight")) {
atts++;
pe->weight = atoi(*atts);
}
if (0 == strcasecmp(*atts, "m_weight")) {
atts++;
pe->m_weight = atoi(*atts);
}
}
atts++;
}
}else{
if (0 == strcasecmp(name, "rloc")) {
if (userData || !_mapping) {
_err_config("Mismatch tag");
exit(1);
}
entry = calloc(1, sizeof(struct map_entry));
XML_SetUserData(parser, entry);
}
}
}
_xml_name = name;
}
static void XMLCALL
xtr_endElement(void *userData, const char *name)
{
struct map_entry *entry;
entry = (struct map_entry*)userData;
if (0 == strcasecmp(name, "rloc")) {
if (!entry) {
_err_config("Mismatch tag");
exit(1);
}
XML_SetUserData(parser, NULL);
if (entry->priority > 0 && entry->weight > 0 &&
(entry->weight + sw_rloc(((struct db_node *)_mapping)->info,entry->priority)) <= 100 ) {
generic_mapping_add_rloc(_mapping, entry);
}else{
_err_config("Incorrect priority or weight (sum weight must <=100 for each priority)");
exit(1);
}
entry = NULL;
}else{
if (0 == strcasecmp(name, "eid_prefix") || 0 == strcasecmp(name, "eid")) {
generic_mapping_set_flags(_mapping, &_mflags);
bzero(&_mflags, sizeof(struct mapping_flags));
free(_prefix);
_prefix = NULL;
_mapping = NULL;
}
/* if (0 == strcasecmp(name, "db") ||
0 == strcasecmp(name, "mapserver") ||
0 == strcasecmp(name, "mapresolver")) {
} */
if (0 == strcasecmp(name, "ms")) {
xtr_ms_entry = NULL;
}
if (0 == strcasecmp(name, "mr")) {
xtr_mr_entry = NULL;
}
if (0 == strcasecmp(name, "elp")) {
if (entry && !entry->pe)
entry->pe = list_init();
if (entry && entry->pe && (pe->weight + sw_elp(entry->pe,pe->priority) <=100))
list_insert(entry->pe, pe,NULL);
else{
_err_config("Incorrect priority or weight (sum weight must <=100 for each priority)");
exit(1);
}
pe = NULL;
}
}
}
static void XMLCALL
xtr_getElementValue(void *userData, const XML_Char *s, int len)
{
struct map_entry *entry;
void *ptr;
char buf[len+1];
buf[len] = '\0';
memcpy(buf, s, len);
entry = (struct map_entry *)userData;
if (entry) {
if (0 == strcasecmp(_xml_name, "priority")) {
entry->priority = atoi(buf);
}else if (0 == strcasecmp(_xml_name, "m_priority")) {
entry->m_priority = atoi(buf);
}else if (0 == strcasecmp(_xml_name, "weight")) {
entry->weight = atoi(buf);
}else if (0 == strcasecmp(_xml_name, "m_weight")) {
entry->m_weight = atoi(buf);
}else if (0 == strcasecmp(_xml_name, "reachable")) {
entry->r = (strcasecmp(buf, "true")==0);
}else if (0 == strcasecmp(_xml_name, "local")) {
entry->L = (strcasecmp(buf, "true")==0);
}else if (0 == strcasecmp(_xml_name, "rloc-probing")) {
entry->p = (strcasecmp(buf, "true")==0);
}else if (0 == strcasecmp(_xml_name, "hop")) {
hop->addr.sa.sa_family = _fam;
switch (_fam) {
case AF_INET:
ptr = &hop->addr.sin.sin_addr;
break;
case AF_INET6:
ptr = &hop->addr.sin6.sin6_addr;
break;
default:
_fam = AF_INET;
ptr = &hop->addr.sa.sa_data;
break;
}
if (inet_pton(_fam, buf, ptr) <=0) {
_err_config("invalid address");
exit(1);
}
if (pe && !pe->hop)
pe->hop = list_init();
if (pe)
list_insert(pe->hop, hop,NULL);
else{
_err_config("missing elp");
exit(1);
}
hop = NULL;
_fam = AF_INET;
}else if (0 == strcasecmp(_xml_name, "address")) {
entry->rloc.sa.sa_family = _fam;
switch (_fam) {
case AF_INET:
ptr = &entry->rloc.sin.sin_addr;
break;
case AF_INET6:
ptr = &entry->rloc.sin6.sin6_addr;
break;
default:
_fam = AF_INET;
ptr = &entry->rloc.sa.sa_data;
break;
}
if (inet_pton(_fam, buf, ptr) <=0) {
_err_config("invalid address");
exit(1);
}
_fam = AF_INET;
}
}
if (0 == strcasecmp(_xml_name, "ms")) {
switch (_fam) {
case AF_INET:
xtr_ms_entry->addr.sin.sin_family = _fam;
xtr_ms_entry->addr.sin.sin_port = htons(LISP_CP_PORT);
ptr = &xtr_ms_entry->addr.sin.sin_addr;
break;
case AF_INET6:
xtr_ms_entry->addr.sin6.sin6_family = _fam;
xtr_ms_entry->addr.sin6.sin6_port = htons(LISP_CP_PORT);
ptr = &xtr_ms_entry->addr.sin6.sin6_addr;
break;
default:
xtr_ms_entry->addr.sa.sa_family = AF_INET;
xtr_ms_entry->addr.sin.sin_port = htons(LISP_CP_PORT);
ptr = &xtr_ms_entry->addr.sa.sa_data;
break;
}
if (inet_pton(_fam, buf, ptr) <=0) {
_err_config("invalid address");
exit(1);
}
_fam = AF_INET;
}else{
if (0 == strcasecmp(_xml_name, "mr")) {
xtr_mr_entry = calloc(1, sizeof(struct mr_entry));
union sockunion *mr = &xtr_mr_entry->addr;
switch (_fam) {
case AF_INET:
mr->sin.sin_family = _fam;
mr->sin.sin_port = htons(LISP_CP_PORT);
ptr = &mr->sin.sin_addr;
break;
case AF_INET6:
mr->sin6.sin6_family = _fam;
mr->sin6.sin6_port = htons(LISP_CP_PORT);
ptr = &mr->sin6.sin6_addr;
break;
default:
mr->sa.sa_family = AF_INET;
mr->sin.sin_port = htons(LISP_CP_PORT);
ptr = &mr->sa.sa_data;
break;
}
if (inet_pton(_fam, buf, ptr) <=0) {
_err_config("invalid address");
exit(1);
}
list_insert(xtr_mr,xtr_mr_entry, NULL);
_fam = AF_INET;
}
}
if (0 == strcasecmp(_xml_name, "petr")) {
struct petr_entry *petr;
petr = calloc(1, sizeof(struct petr_entry));
switch (_fam) {
case AF_INET:
petr->addr.sin.sin_family = _fam;
ptr = &petr->addr.sin.sin_addr;
break;
case AF_INET6:
petr->addr.sin6.sin6_family = _fam;
ptr = &petr->addr.sin6.sin6_addr;
break;
default:
petr->addr.sa.sa_family = AF_INET;
ptr = &petr->addr.sa.sa_data;
break;
}
petr->addr.sin.sin_port = htons(LISP_DP_PORT);
if (inet_pton(_fam, buf, ptr) <=0) {
_err_config("invalid address");
exit(1);
}
list_insert(xtr_petr,petr, NULL);
_fam = AF_INET;
}
_xml_name = "DUMMY";
}
/*====================================================================
* Parse for map-resolve configure
*/
static void XMLCALL
mr_startElement(void *userData, const char *name, const char **atts)
{
int len;
struct map_entry *entry;
if ((0 == strcasecmp(name, "eid_prefix")) ||
(0 == strcasecmp(name, "eid")) ) {
_mflags.iid = 0;
while (*atts) {
/* EID prefix */
if (0 == strcasecmp(*atts, "prefix")) {
struct prefix p1;
atts++;
len = strlen(*atts);
_prefix = (char *)calloc(1, len+1);
memcpy(_prefix, *atts, len);
*(_prefix + len) = '\0';
if (str2prefix (_prefix, &p1) <=0) {
_err_config("invalid prefix");
exit(1);
}
apply_mask(&p1);
if (!_valid_prefix(&p1, _REID) || !(_mapping = generic_mapping_new(&p1))) {
_err_config("invalid prefix - overlap");
exit(0);
}
bzero(&_mflags, sizeof(struct mapping_flags));
_mflags.range = _MAPP;
}
_mflags.A = 0;
_mflags.referral = LISP_REFERRAL_NODE_REFERRAL+1;
/* ACT bits */
if (0 == strcasecmp(*atts, "act")) {
atts++;
_mflags.act = atoi(*atts);
}
if (0 == strcasecmp(*atts, "iid")) {
atts++;
_mflags.iid = atoi(*atts);
}
/* Echo-noncable */
if (0 == strcasecmp(*atts, "a")) {
atts++;
_mflags.A = (strcasecmp(*atts, "true")==0);
}
/* Version */
if (0 == strcasecmp(*atts, "version")) {
atts++;
_mflags.version = atoi(*atts);
}
/* TTL */
if (0 == strcasecmp(*atts, "ttl")) {
atts++;
_mflags.ttl = atoi(*atts);
}
/* Referral */
if (0 == strcasecmp(*atts, "referral")) {
atts++;
//in case can not detect type of referral, set to NODE_REFERRAL;
_mflags.referral = LISP_REFERRAL_NODE_REFERRAL+1;
if (strcasecmp(*atts, "true")==0 || strcasecmp(*atts, "node")==0)
_mflags.referral = LISP_REFERRAL_NODE_REFERRAL+1;
if (strcasecmp(*atts, "ms")==0)
_mflags.referral = LISP_REFERRAL_MS_REFERRAL+1;
}
/* Incomplete Referral */
if (0 == strcasecmp(*atts, "incomplete")) {
atts++;
_mflags.incomplete = (strcasecmp(*atts, "true")==0);
}
/**/
atts++;
}
} else if (0 == strcasecmp(name, "address")) {
_fam = AF_INET;
while (*atts) {
if (0 == strcasecmp(*atts, "family")) {
atts++;
_fam = (0 == strcasecmp(*atts, "IPv6"))?AF_INET6:AF_INET;
}
else
_fam = AF_INET;
atts++;
}
}
if (!userData) {
entry = calloc(1, sizeof(struct map_entry));
XML_SetUserData(parser, entry);
}
_xml_name = name;
}
static void XMLCALL
mr_endElement(void *userData, const char *name)
{
struct map_entry *entry;
if ((0 == strcasecmp(name, "ddt_node")) ||
(0 == strcasecmp(name, "rloc"))) {
entry = (struct map_entry*)userData;
XML_SetUserData(parser, NULL);
generic_mapping_add_rloc(_mapping, entry);
}else if ((0 == strcasecmp(name, "eid_prefix")) ||
(0 == strcasecmp(name, "eid"))) {
generic_mapping_set_flags(_mapping, &_mflags);
bzero(&_mflags, sizeof(struct mapping_flags));
free(_prefix);
_prefix = NULL;
}
}
static void XMLCALL
mr_getElementValue(void *userData, const XML_Char *s, int len)
{
struct map_entry *entry;
char buf[len+1];
buf[len] = '\0';
memcpy(buf, s, len);
entry = (struct map_entry *)userData;
if (0 == strcasecmp(_xml_name, "priority")) {
entry->priority = atoi(buf);
}else if (0 == strcasecmp(_xml_name, "weight")) {
entry->weight = atoi(buf);
}else if (0 == strcasecmp(_xml_name, "reachable")) {
entry->r = (strcasecmp(buf, "true")==0);
}else if (0 == strcasecmp(_xml_name, "address")) {
void *ptr;
entry->rloc.sa.sa_family = _fam;
switch (_fam) {
case AF_INET:
ptr = &entry->rloc.sin.sin_addr;
break;
case AF_INET6:
ptr = &entry->rloc.sin6.sin6_addr;
break;
default:
ptr = &entry->rloc.sa.sa_data;
break;
}
if (inet_pton(_fam, buf, ptr) <=0) {
_err_config("invalid address");
exit(1);
}
_fam = AF_INET;
}
_xml_name = "DUMMY";
}
/*====================================================================
* Parse for node configure
*/
static void XMLCALL
node_startElement(void *userData, const char *name, const char **atts)
{
int len;
struct map_entry *entry;
if (0 == strcasecmp(name, "delegated_eid_prefix") ) {
_mflags.iid = 0;
while (*atts) {
/* EID prefix */
if (0 == strcasecmp(*atts, "prefix")) {
struct prefix p1;
atts++;
len = strlen(*atts);
_prefix = (char *)calloc(1, len+1);
memcpy(_prefix, *atts, len);
*(_prefix + len) = '\0';
if (str2prefix (_prefix, &p1) <=0) {
_err_config("invalid prefix");
exit(1);
}
apply_mask(&p1);
if (!_valid_prefix(&p1, _REID) || !(_mapping = generic_mapping_new(&p1))) {
_err_config("invalid prefix - overlap");
exit(0);
}
bzero(&_mflags, sizeof(struct mapping_flags));
_mflags.range = _MAPP;
}
/* set default value of flags, override by user */
_mflags.A = 1;
_mflags.ttl = 60;
_mflags.referral = LISP_REFERRAL_NODE_REFERRAL+1;
_mflags.incomplete = 0;
/* ACT bits */
if (0 == strcasecmp(*atts, "act")) {
atts++;
_mflags.act = atoi(*atts);
}
if (0 == strcasecmp(*atts, "iid")) {
atts++;
_mflags.iid = atoi(*atts);
}
/* Echo-noncable */
if (0 == strcasecmp(*atts, "a")) {
atts++;
_mflags.A = (strcasecmp(*atts, "true")==0);
}
/* Version */
if (0 == strcasecmp(*atts, "version")) {
atts++;
_mflags.version = atoi(*atts);
}
/* TTL */
if (0 == strcasecmp(*atts, "ttl")) {
atts++;
_mflags.ttl = atoi(*atts);
}
/* Referral */
if (0 == strcasecmp(*atts, "referral")) {
atts++;
//in case can not detect type of referral, set to NODE_REFERRAL;
_mflags.referral = LISP_REFERRAL_NODE_REFERRAL+1;
if (strcasecmp(*atts, "true")==0 || strcasecmp(*atts, "node")==0 )
_mflags.referral = LISP_REFERRAL_NODE_REFERRAL+1;
if (strcasecmp(*atts, "ms")==0)
_mflags.referral = LISP_REFERRAL_MS_REFERRAL+1;
}
/* Incomplete Referral */
if (0 == strcasecmp(*atts, "incomplete")) {
atts++;
_mflags.incomplete = (strcasecmp(*atts, "true")==0);
}
/**/
atts++;
}
} else if (0 == strcasecmp(name, "address")) {
_fam = AF_INET;
while (*atts) {
if (0 == strcasecmp(*atts, "family")) {
atts++;
_fam = (0 == strcasecmp(*atts, "IPv6"))?AF_INET6:AF_INET;
}
else
_fam = AF_INET;
atts++;
}
}
if (!userData) {
entry = calloc(1, sizeof(struct map_entry));
XML_SetUserData(parser, entry);
}
_xml_name = name;
}
static void XMLCALL
node_endElement(void *userData, const char *name)
{
struct map_entry *entry;
if (0 == strcasecmp(name, "ddt_node")) {
entry = (struct map_entry*)userData;
XML_SetUserData(parser, NULL);
generic_mapping_add_rloc(_mapping, entry);
}else if (0 == strcasecmp(name, "delegated_eid_prefix")) {
generic_mapping_set_flags(_mapping, &_mflags);
bzero(&_mflags, sizeof(struct mapping_flags));
free(_prefix);
_prefix = NULL;
}
}
static void XMLCALL
node_getElementValue(void *userData, const XML_Char *s, int len)
{
struct map_entry *entry;
struct db_table *db;
struct db_node *dn;
struct prefix pf;