/
nameservice.c
1677 lines (1458 loc) · 53.3 KB
/
nameservice.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
/*
* Copyright (c) 2006, Jens Nachtigall <nachtigall@web.de>
* Copyright (c) 2005, Bruno Randolf <bruno.randolf@4g-systems.biz>
* Copyright (c) 2004, Andreas Tonnesen(andreto-at-olsr.org)
* Copyright (c) 2007, Sven-Ola <sven-ola@gmx.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the UniK olsr daemon nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/*
* Dynamic linked library for UniK OLSRd
*/
#define _GNU_SOURCE 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/types.h>
#include <regex.h>
#include <sys/stat.h>
#include <signal.h>
#include <fcntl.h>
#include "olsr.h"
#include "ipcalc.h"
#include "net_olsr.h"
#include "routing_table.h"
#include "mantissa.h"
#include "scheduler.h"
#include "parser.h"
#include "duplicate_set.h"
#include "tc_set.h"
#include "hna_set.h"
#include "mid_set.h"
#include "link_set.h"
#include "plugin_util.h"
#include "nameservice.h"
#include "mapwrite.h"
#include "compat.h"
/* true if plugin has been configured */
static bool nameservice_configured = false;
/* config parameters */
static char my_hosts_file[MAX_FILE + 1];
static char my_sighup_pid_file[MAX_FILE + 1];
static char my_add_hosts[MAX_FILE + 1];
static char my_suffix[MAX_SUFFIX];
static int my_interval = EMISSION_INTERVAL;
static float my_timeout = NAME_VALID_TIME;
static char my_resolv_file[MAX_FILE + 1];
static char my_services_file[MAX_FILE + 1];
static char my_macs_file[MAX_FILE + 1];
static char my_name_change_script[MAX_FILE + 1];
static char my_services_change_script[MAX_FILE + 1];
static char my_macs_change_script[MAX_FILE + 1];
static char latlon_in_file[MAX_FILE + 1];
static char my_latlon_file[MAX_FILE + 1];
float my_lat = 0.0, my_lon = 0.0;
/* the databases (using hashing)
* for hostnames, service_lines and dns-servers
*
* my own hostnames, service_lines and dns-servers
* are store in a linked list (without hashing)
* */
static struct list_node name_list[HASHSIZE];
struct name_entry *my_names = NULL;
struct timer_entry *name_table_write = NULL;
static bool name_table_changed = true;
static struct list_node service_list[HASHSIZE];
static struct name_entry *my_services = NULL;
static bool service_table_changed = true;
static struct list_node mac_list[HASHSIZE];
static struct name_entry *my_macs = NULL;
static bool mac_table_changed = true;
static struct list_node forwarder_list[HASHSIZE];
static struct name_entry *my_forwarders = NULL;
static bool forwarder_table_changed = true;
struct list_node latlon_list[HASHSIZE];
static bool latlon_table_changed = true;
/* backoff timer for writing changes into a file */
struct timer_entry *write_file_timer = NULL;
/* periodic message generation */
struct timer_entry *msg_gen_timer = NULL;
/* regular expression to be matched by valid hostnames, compiled in name_init() */
static regex_t regex_t_name;
static regmatch_t regmatch_t_name;
/* regular expression to be matched by valid service_lines, compiled in name_init() */
static regex_t regex_t_service;
static int pmatch_service = 10;
static regmatch_t regmatch_t_service[10];
/**
* do initialization
*/
void
name_constructor(void)
{
int i;
#ifdef _WIN32
int len;
GetWindowsDirectory(my_hosts_file, MAX_FILE - 12);
GetWindowsDirectory(my_services_file, MAX_FILE - 12);
GetWindowsDirectory(my_macs_file, MAX_FILE - 12);
GetWindowsDirectory(my_resolv_file, MAX_FILE - 12);
len = strlen(my_hosts_file);
if (my_hosts_file[len - 1] != '\\')
strscat(my_hosts_file, "\\", sizeof(my_host_file));
strscat(my_hosts_file, "hosts_olsr", sizeof(my_host_file));
len = strlen(my_services_file);
if (my_services_file[len - 1] != '\\')
strscat(my_services_file, "\\", sizeof(my_services_file));
strscat(my_services_file, "services_olsr", sizeof(my_services_file));
len = strlen(my_macs_file);
if (my_macs_file[len - 1] != '\\')
strscat(my_macs_file, "\\", sizeof(my_macs_file));
strscat(my_macs_file, "macs_olsr", sizeof(my_macs_file));
len = strlen(my_resolv_file);
if (my_resolv_file[len - 1] != '\\')
strscat(my_resolv_file, "\\", sizeof(my_resolv_file));
strscat(my_resolv_file, "resolvconf_olsr", sizeof(my_resolv_file));
#else /* _WIN32 */
strscpy(my_hosts_file, "/var/run/hosts_olsr", sizeof(my_hosts_file));
strscpy(my_services_file, "/var/run/services_olsr", sizeof(my_services_file));
strscpy(my_macs_file, "/var/run/macs_olsr", sizeof(my_macs_file));
strscpy(my_resolv_file, "/var/run/resolvconf_olsr", sizeof(my_resolv_file));
*my_sighup_pid_file = 0;
#endif /* _WIN32 */
my_suffix[0] = '\0';
my_add_hosts[0] = '\0';
my_latlon_file[0] = '\0';
latlon_in_file[0] = '\0';
my_name_change_script[0] = '\0';
my_services_change_script[0] = '\0';
my_macs_change_script[0] = '\0';
/* init the lists heads */
for (i = 0; i < HASHSIZE; i++) {
list_head_init(&name_list[i]);
list_head_init(&forwarder_list[i]);
list_head_init(&service_list[i]);
list_head_init(&mac_list[i]);
list_head_init(&latlon_list[i]);
}
}
static int
set_nameservice_server(const char *value, void *data, set_plugin_parameter_addon addon)
{
union olsr_ip_addr ip;
struct name_entry **v = data;
if (0 == strlen(value)) {
*v = add_name_to_list(*v, "", addon.ui, NULL);
OLSR_PRINTF(1, "%s got %s (main address)\n", "Got", value);
return 0;
} else if (0 < inet_pton(olsr_cnf->ip_version, value, &ip)) {
*v = add_name_to_list(*v, "", addon.ui, &ip);
OLSR_PRINTF(1, "%s got %s\n", "Got", value);
return 0;
} else {
OLSR_PRINTF(0, "Illegal IP address \"%s\"", value);
}
return 1;
}
static int
set_nameservice_name(const char *value, void *data, set_plugin_parameter_addon addon)
{
struct name_entry **v = data;
if (0 < strlen(value)) {
*v = add_name_to_list(*v, value, addon.ui, NULL);
OLSR_PRINTF(1, "%s got %s (main address)\n", "Got", value);
return 0;
} else {
OLSR_PRINTF(0, "Illegal name \"%s\"", value);
}
return 1;
}
static int
set_nameservice_host(const char *value, void *data, set_plugin_parameter_addon addon)
{
union olsr_ip_addr ip;
struct name_entry **v = data;
if (0 < inet_pton(olsr_cnf->ip_version, addon.pc, &ip)) {
// the IP is validated later
*v = add_name_to_list(*v, value, NAME_HOST, &ip);
OLSR_PRINTF(1, "%s: %s got %s\n", "Got", addon.pc, value);
return 0;
} else {
OLSR_PRINTF(0, "%s: Illegal IP address \"%s\"", addon.pc, value);
}
return 1;
}
static int
set_nameservice_float(const char *value, void *data, set_plugin_parameter_addon addon __attribute__ ((unused)))
{
if (data != NULL) {
sscanf(value, "%f", (float *)data);
OLSR_PRINTF(1, "%s float %f\n", "Got", (double)(*(float *)data));
} else {
OLSR_PRINTF(0, "%s float %s\n", "Ignored", value);
}
return 0;
}
/* *INDENT-OFF* */
static const struct olsrd_plugin_parameters plugin_parameters[] = {
{ .name = "interval", .set_plugin_parameter = &set_plugin_int, .data = &my_interval },
{ .name = "timeout", .set_plugin_parameter = &set_nameservice_float, .data = &my_timeout },
{ .name = "sighup-pid-file", .set_plugin_parameter = &set_plugin_string, .data = &my_sighup_pid_file, .addon = {sizeof(my_sighup_pid_file)} },
{ .name = "hosts-file", .set_plugin_parameter = &set_plugin_string, .data = &my_hosts_file, .addon = {sizeof(my_hosts_file)} },
{ .name = "name-change-script", .set_plugin_parameter = &set_plugin_string, .data = &my_name_change_script, .addon = {sizeof(my_name_change_script)} },
{ .name = "services-change-script", .set_plugin_parameter = &set_plugin_string, .data = &my_services_change_script, .addon = {sizeof(my_services_change_script)} },
{ .name = "macs-change-script", .set_plugin_parameter = &set_plugin_string, .data = &my_macs_change_script, .addon = {sizeof(my_macs_change_script)} },
{ .name = "resolv-file", .set_plugin_parameter = &set_plugin_string, .data = &my_resolv_file, .addon = {sizeof(my_resolv_file)} },
{ .name = "suffix", .set_plugin_parameter = &set_plugin_string, .data = &my_suffix, .addon = {sizeof(my_suffix)} },
{ .name = "add-hosts", .set_plugin_parameter = &set_plugin_string, .data = &my_add_hosts, .addon = {sizeof(my_add_hosts)} },
{ .name = "services-file", .set_plugin_parameter = &set_plugin_string, .data = &my_services_file, .addon = {sizeof(my_services_file)} },
{ .name = "macs-file", .set_plugin_parameter = &set_plugin_string, .data = &my_macs_file, .addon = {sizeof(my_macs_file)} },
{ .name = "lat", .set_plugin_parameter = &set_nameservice_float, .data = &my_lat },
{ .name = "lon", .set_plugin_parameter = &set_nameservice_float, .data = &my_lon },
{ .name = "latlon-file", .set_plugin_parameter = &set_plugin_string, .data = &my_latlon_file, .addon = {sizeof(my_latlon_file)} },
{ .name = "latlon-infile", .set_plugin_parameter = &set_plugin_string, .data = &latlon_in_file, .addon = {sizeof(latlon_in_file)} },
{ .name = "dns-server", .set_plugin_parameter = &set_nameservice_server, .data = &my_forwarders, .addon = {NAME_FORWARDER} },
{ .name = "name", .set_plugin_parameter = &set_nameservice_name, .data = &my_names, .addon = {NAME_HOST} },
{ .name = "service", .set_plugin_parameter = &set_nameservice_name, .data = &my_services, .addon = {NAME_SERVICE} },
{ .name = "mac", .set_plugin_parameter = &set_nameservice_name, .data = &my_macs, .addon = {NAME_MACADDR} },
{ .name = "", .set_plugin_parameter = &set_nameservice_host, .data = &my_names },
};
/* *INDENT-OFF* */
void
olsrd_get_plugin_parameters(const struct olsrd_plugin_parameters **params, int *size)
{
*params = plugin_parameters;
*size = sizeof(plugin_parameters) / sizeof(*plugin_parameters);
}
/**
* queue the name/forwarder/service given in value
* to the front of my_list
*/
struct name_entry *
add_name_to_list(struct name_entry *my_list, const char *value, int type, const union olsr_ip_addr *ip)
{
struct name_entry *tmp = olsr_malloc(sizeof(struct name_entry),
"new name_entry add_name_to_list");
tmp->name = strndup(value, MAX_NAME);
tmp->len = strlen(tmp->name);
tmp->type = type;
// all IPs with value 0 will be set to main_addr later
if (ip == NULL)
memset(&tmp->ip, 0, sizeof(tmp->ip));
else
tmp->ip = *ip;
tmp->next = my_list;
return tmp;
}
/**
* last initialization
*
* we have to do this here because some things like main_addr
* or the dns suffix (for validation) are not known before
*
* this is beause of the order in which the plugin is initialized
* by the plugin loader:
* - first the parameters are sent
* - then register_olsr_data() from olsrd_plugin.c is called
* which sets up main_addr and some other variables
* - register_olsr_data() then then finally calls this function
*/
int
name_init(void)
{
/* register functions with olsrd */
olsr_parser_add_function(&olsr_parser, PARSER_TYPE);
/* periodic message generation */
msg_gen_timer = olsr_start_timer(my_interval * MSEC_PER_SEC, EMISSION_JITTER, OLSR_TIMER_PERIODIC, &olsr_namesvc_gen, NULL, 0);
return 1;
}
static void name_lazy_init(void) {
struct name_entry *name;
union olsr_ip_addr ipz;
int ret;
//regex string for validating the hostnames
const char *regex_name = "^[[:alnum:]_.-]+$";
//regex string for the service line
size_t regex_size;
char *regex_service;
/* wait for configured master IP */
if (ipequal(&olsr_cnf->main_addr, &olsr_ip_zero)) {
return;
}
nameservice_configured = true;
regex_size = 256 * sizeof(char) + strlen(my_suffix);
regex_service = olsr_malloc(regex_size, "new *char from name_init for regex_service");
memset(&ipz, 0, sizeof(ipz));
//compile the regex from the string
if ((ret = regcomp(®ex_t_name, regex_name, REG_EXTENDED)) != 0) {
/* #2: call regerror() if regcomp failed
* commented out, because only for debuggin needed
*
int errmsgsz = regerror(ret, ®ex_t_name, NULL, 0);
char *errmsg = malloc(errmsgsz);
regerror(ret, ®ex_t_name, errmsg, errmsgsz);
fprintf(stderr, "regcomp: %s", errmsg);
free(errmsg);
regfree(®ex_t_name);
* */
OLSR_PRINTF(0, "compilation of regex \"%s\" for hostname failed", regex_name);
}
// a service line is something like prot://hostname.suffix:port|tcp|my little description about this service
// for example http://router.olsr:80|tcp|my homepage
// prot :// (hostname.suffix OR ip)
//regex_service = "^[[:alnum:]]+://(([[:alnum:]_.-]+.olsr)|([[:digit:]]{1,3}\\.[[:digit:]]{1,3}\\.[[:digit:]]{1,3}\\.[[:digit:]]{1,3}))
// : port /path |(tcp OR udp) |short description
// :[[:digit:]]+[[:alnum:]/?._=#-]*\\|(tcp|udp)\\|[^|[:cntrl:]]+$";
strscpy(regex_service, "^[[:alnum:]]+://(([[:alnum:]_.-]+", regex_size);
strscat(regex_service, my_suffix, regex_size);
strscat(regex_service,
")|([[:digit:]]{1,3}\\.[[:digit:]]{1,3}\\.[[:digit:]]{1,3}\\.[[:digit:]]{1,3})):[[:digit:]]+[[:alnum:]/?._=#-]*\\|(tcp|udp)\\|[^|[:cntrl:]]+$",
regex_size);
/* #1: call regcomp() to compile the regex */
if ((ret = regcomp(®ex_t_service, regex_service, REG_EXTENDED)) != 0) {
/* #2: call regerror() if regcomp failed
* commented out, because only for debuggin needed
*
int errmsgsz = regerror(ret, ®ex_t_service, NULL, 0);
char *errmsg = malloc(errmsgsz);
regerror(ret, ®ex_t_service, errmsg, errmsgsz);
fprintf(stderr, "regcomp: %s", errmsg);
free(errmsg);
regfree(®ex_t_service);
* */
OLSR_PRINTF(0, "compilation of regex \"%s\" for hostname failed", regex_name);
}
free(regex_service);
regex_service = NULL;
//fill in main addr for all entries with ip==0
//this does not matter for service, because the ip does not matter
//for service
for (name = my_names; name != NULL; name = name->next) {
if (ipequal(&name->ip, &ipz)) {
OLSR_PRINTF(2, "NAME PLUGIN: insert main addr for name %s \n", name->name);
name->ip = olsr_cnf->main_addr;
}
}
for (name = my_forwarders; name != NULL; name = name->next) {
if (name->ip.v4.s_addr == 0) {
OLSR_PRINTF(2, "NAME PLUGIN: insert main addr for name %s \n", name->name);
name->ip = olsr_cnf->main_addr;
}
}
//check if entries I want to announce myself are valid and allowed
my_names = remove_nonvalid_names_from_list(my_names, NAME_HOST);
my_forwarders = remove_nonvalid_names_from_list(my_forwarders, NAME_FORWARDER);
my_services = remove_nonvalid_names_from_list(my_services, NAME_SERVICE);
my_macs = remove_nonvalid_names_from_list(my_macs, NAME_MACADDR);
mapwrite_init(my_latlon_file);
return;
}
struct name_entry *
remove_nonvalid_names_from_list(struct name_entry *my_list, int type)
{
struct name_entry *next = my_list;
bool valid = false;
if (my_list == NULL) {
return NULL;
}
switch (type) {
case NAME_HOST:
valid = is_name_wellformed(my_list->name) && allowed_ip(&my_list->ip);
break;
case NAME_FORWARDER:
valid = allowed_ip(&my_list->ip);
break;
case NAME_SERVICE:
valid = allowed_service(my_list->name);
break;
case NAME_MACADDR:
valid = is_mac_wellformed(my_list->name);
break;
case NAME_LATLON:
valid = is_latlon_wellformed(my_list->name);
break;
default:
valid = false;
break;
}
if (!valid) {
struct ipaddr_str strbuf;
OLSR_PRINTF(1, "NAME PLUGIN: invalid or malformed parameter %s (%s), fix your config!\n", my_list->name,
olsr_ip_to_string(&strbuf, &my_list->ip));
next = my_list->next;
free(my_list->name);
my_list->name = NULL;
free(my_list);
my_list = NULL;
return remove_nonvalid_names_from_list(next, type);
} else {
struct ipaddr_str strbuf;
OLSR_PRINTF(2, "NAME PLUGIN: validate parameter %s (%s) -> OK\n", my_list->name, olsr_ip_to_string(&strbuf, &my_list->ip));
my_list->next = remove_nonvalid_names_from_list(my_list->next, type);
return my_list;
}
}
/**
* called at unload: free everything
*
* XXX: should I delete the hosts/services/resolv.conf files on exit?
*/
void
name_destructor(void)
{
OLSR_PRINTF(2, "NAME PLUGIN: exit. cleaning up...\n");
free_name_entry_list(&my_names);
free_name_entry_list(&my_services);
free_name_entry_list(&my_macs);
free_name_entry_list(&my_forwarders);
free_all_list_entries(name_list);
free_all_list_entries(service_list);
free_all_list_entries(mac_list);
free_all_list_entries(forwarder_list);
free_all_list_entries(latlon_list);
olsr_stop_timer(write_file_timer);
olsr_stop_timer(msg_gen_timer);
regfree(®ex_t_name);
regfree(®ex_t_service);
mapwrite_exit();
}
/* free all list entries */
void
free_all_list_entries(struct list_node *this_db_list)
{
struct db_entry *db;
struct list_node *list_head, *list_node, *list_node_next;
int i;
for (i = 0; i < HASHSIZE; i++) {
list_head = &this_db_list[i];
for (list_node = list_head->next; list_node != list_head; list_node = list_node_next) {
/* prefetch next node before loosing context */
list_node_next = list_node->next;
db = list2db(list_node);
olsr_namesvc_delete_db_entry(db);
}
}
}
/**
* The write file timer has fired.
*/
void
olsr_expire_write_file_timer(void *context __attribute__ ((unused)))
{
write_file_timer = NULL;
write_resolv_file(); /* if forwarder_table_changed */
write_hosts_file(); /* if name_table_changed */
write_services_file(false); /* if service_table_changed */
write_services_file(true); /* if mac_table_changed */
#ifdef _WIN32
write_latlon_file(); /* if latlon_table_changed */
#endif /* _WIN32 */
}
/*
* Kick a timer to write everything into a file.
* This also paces things a bit.
*/
static void
olsr_start_write_file_timer(void)
{
if (write_file_timer) {
return;
}
write_file_timer = olsr_start_timer(5 * MSEC_PER_SEC, 5, OLSR_TIMER_ONESHOT, olsr_expire_write_file_timer, NULL, 0);
}
/*
* Delete and unlink db_entry.
*/
void
olsr_namesvc_delete_db_entry(struct db_entry *db)
{
struct ipaddr_str strbuf;
OLSR_PRINTF(2, "NAME PLUGIN: %s timed out... deleting\n", olsr_ip_to_string(&strbuf, &db->originator));
olsr_start_write_file_timer();
olsr_stop_timer(db->db_timer); /* stop timer if running */
/* Delete */
free_name_entry_list(&db->names);
list_remove(&db->db_list);
free(db);
}
/**
* Callback for the db validity timer.
*/
static void
olsr_nameservice_expire_db_timer(void *context)
{
struct db_entry *db;
db = (struct db_entry *)context;
db->db_timer = NULL; /* be pedandic */
olsr_namesvc_delete_db_entry(db);
}
/**
* Scheduled event: generate and send NAME packet
*/
void
olsr_namesvc_gen(void *foo __attribute__ ((unused)))
{
/* send buffer: huge */
char buffer[10240];
union olsr_message *message = (union olsr_message *)buffer;
struct interface *ifn;
int namesize;
if (!nameservice_configured) {
name_lazy_init();
if (!nameservice_configured) {
return;
}
}
/* fill message */
if (olsr_cnf->ip_version == AF_INET) {
/* IPv4 */
message->v4.olsr_msgtype = MESSAGE_TYPE;
message->v4.olsr_vtime = reltime_to_me(my_timeout * MSEC_PER_SEC);
memcpy(&message->v4.originator, &olsr_cnf->main_addr, olsr_cnf->ipsize);
message->v4.ttl = MAX_TTL;
message->v4.hopcnt = 0;
message->v4.seqno = htons(get_msg_seqno());
namesize = encap_namemsg((struct namemsg *)ARM_NOWARN_ALIGN(&message->v4.message));
namesize = namesize + sizeof(struct olsrmsg);
message->v4.olsr_msgsize = htons(namesize);
} else {
/* IPv6 */
message->v6.olsr_msgtype = MESSAGE_TYPE;
message->v6.olsr_vtime = reltime_to_me(my_timeout * MSEC_PER_SEC);
memcpy(&message->v6.originator, &olsr_cnf->main_addr, olsr_cnf->ipsize);
message->v6.ttl = MAX_TTL;
message->v6.hopcnt = 0;
message->v6.seqno = htons(get_msg_seqno());
namesize = encap_namemsg((struct namemsg *)ARM_NOWARN_ALIGN(&message->v6.message));
namesize = namesize + sizeof(struct olsrmsg6);
message->v6.olsr_msgsize = htons(namesize);
}
/* looping trough interfaces */
for (ifn = ifnet; ifn; ifn = ifn->int_next) {
OLSR_PRINTF(3, "NAME PLUGIN: Generating packet - [%s]\n", ifn->int_name);
if (net_outbuffer_push(ifn, message, namesize) != namesize) {
/* send data and try again */
net_output(ifn);
if (net_outbuffer_push(ifn, message, namesize) != namesize) {
OLSR_PRINTF(1, "NAME PLUGIN: could not send on interface: %s\n", ifn->int_name);
}
}
}
}
/**
* Parse name olsr message of NAME type
*/
bool
olsr_parser(union olsr_message *m, struct interface *in_if __attribute__ ((unused)), union olsr_ip_addr *ipaddr)
{
struct namemsg *namemessage;
union olsr_ip_addr originator;
olsr_reltime vtime;
int size;
if (!nameservice_configured) {
name_lazy_init();
if (!nameservice_configured) {
return false;
}
}
/* Fetch the originator of the messsage */
if (olsr_cnf->ip_version == AF_INET) {
memcpy(&originator, &m->v4.originator, olsr_cnf->ipsize);
} else {
memcpy(&originator, &m->v6.originator, olsr_cnf->ipsize);
}
/* Fetch the message based on IP version */
if (olsr_cnf->ip_version == AF_INET) {
vtime = me_to_reltime(m->v4.olsr_vtime);
size = ntohs(m->v4.olsr_msgsize);
namemessage = (struct namemsg *)ARM_NOWARN_ALIGN(&m->v4.message);
} else {
vtime = me_to_reltime(m->v6.olsr_vtime);
size = ntohs(m->v6.olsr_msgsize);
namemessage = (struct namemsg *)ARM_NOWARN_ALIGN(&m->v6.message);
}
/* Check if message originated from this node.
If so - back off */
if (ipequal(&originator, &olsr_cnf->main_addr))
return false;
/* Check that the neighbor this message was received from is symmetric.
If not - back off */
if (check_neighbor_link(ipaddr) != SYM_LINK) {
struct ipaddr_str strbuf;
OLSR_PRINTF(3, "NAME PLUGIN: Received msg from NON SYM neighbor %s\n", olsr_ip_to_string(&strbuf, ipaddr));
return false;
}
update_name_entry(&originator, namemessage, size, vtime);
/* Forward the message */
return true;
}
/**
* Encapsulate a name message into a packet.
*
* It assumed that there is enough space in the buffer to do this!
*
* Returns: the length of the message that was appended
*/
int
encap_namemsg(struct namemsg *msg)
{
struct name_entry *my_name;
// add the hostname, service and forwarder entries after the namemsg header
char *pos = (char *)msg + sizeof(struct namemsg);
short i = 0;
// names
for (my_name = my_names; my_name != NULL; my_name = my_name->next) {
pos = create_packet((struct name *)ARM_NOWARN_ALIGN(pos), my_name);
i++;
}
// forwarders
for (my_name = my_forwarders; my_name != NULL; my_name = my_name->next) {
pos = create_packet((struct name *)ARM_NOWARN_ALIGN(pos), my_name);
i++;
}
// services
for (my_name = my_services; my_name != NULL; my_name = my_name->next) {
pos = create_packet((struct name *)ARM_NOWARN_ALIGN(pos), my_name);
i++;
}
// macs
for (my_name = my_macs; my_name != NULL; my_name = my_name->next) {
pos = create_packet((struct name *)ARM_NOWARN_ALIGN(pos), my_name);
i++;
}
// latlon
if ('\0' != latlon_in_file[0]) {
FILE *in = fopen(latlon_in_file, "r");
if (in != NULL) {
if (fscanf(in, "%f,%f", &my_lat, &my_lon)) {}
fclose(in);
} else {
OLSR_PRINTF(0, "NAME PLUGIN: cant read latlon in file %s\n", latlon_in_file);
}
}
if (0.0f != my_lat && 0.0f != my_lon) {
char s[64];
struct name_entry e;
memset(&e, 0, sizeof(e));
sprintf(s, "%f,%f,%d", (double)my_lat, (double)my_lon, get_isdefhna_latlon());
e.len = strlen(s);
e.type = NAME_LATLON;
e.name = s;
lookup_defhna_latlon(&e.ip);
pos = create_packet((struct name *)ARM_NOWARN_ALIGN(pos), &e);
i++;
}
// write the namemsg header with the number of announced entries and the protocol version
msg->nr_names = htons(i);
msg->version = htons(NAME_PROTOCOL_VERSION);
return pos - (char *)msg; //length
}
/**
* convert each of my to be announced name_entries into network
* compatible format
*
* return the length of the name packet
*/
char *
create_packet(struct name *to, struct name_entry *from)
{
char *pos = (char *)to;
int k;
struct ipaddr_str strbuf;
OLSR_PRINTF(3, "NAME PLUGIN: Announcing name %s (%s) %d\n", from->name, olsr_ip_to_string(&strbuf, &from->ip), from->len);
to->type = htons(from->type);
to->len = htons(from->len);
to->ip = from->ip;
pos += sizeof(struct name);
memcpy(pos, from->name, from->len);
pos += from->len;
for (k = from->len; (k & 3) != 0; k++)
*pos++ = '\0';
return pos;
}
/**
* decapsulate a received name, service or forwarder and update the corresponding hash table if necessary
*/
void
decap_namemsg(struct name *from_packet, struct name_entry **to, bool * this_table_changed)
{
struct ipaddr_str strbuf;
struct name_entry *tmp;
struct name_entry *already_saved_name_entries;
char *name = (char *)from_packet + sizeof(struct name);
int type_of_from_packet = ntohs(from_packet->type);
unsigned int len_of_name = ntohs(from_packet->len);
OLSR_PRINTF(4, "NAME PLUGIN: decap type=%d, len=%d, name=%s\n", type_of_from_packet, len_of_name, name);
//XXX: should I check the from_packet->ip here? If so, why not also check the ip from HOST and SERVICE?
if ((type_of_from_packet == NAME_HOST && !is_name_wellformed(name))
|| (type_of_from_packet == NAME_SERVICE && !is_service_wellformed(name))
|| (type_of_from_packet == NAME_MACADDR && !is_mac_wellformed(name))
|| (type_of_from_packet == NAME_LATLON && !is_latlon_wellformed(name))) {
OLSR_PRINTF(4, "NAME PLUGIN: invalid name [%s] received, skipping.\n", name);
return;
}
//ignore all packets with a too long name
//or a spoofed len of its included name string
if (len_of_name > MAX_NAME || strlen(name) != len_of_name || NULL != strchr(name, '\\') || NULL != strchr(name, '\'')) {
OLSR_PRINTF(4, "NAME PLUGIN: from_packet->len %d > MAX_NAME %d or from_packet->len %d !0 strlen(name [%s] in packet)\n",
len_of_name, MAX_NAME, len_of_name, name);
return;
}
// don't insert the received entry again, if it has already been inserted in the hash table.
// Instead only the validity time is set in insert_new_name_in_list function, which calls this one
for (already_saved_name_entries = (*to); already_saved_name_entries != NULL;
already_saved_name_entries = already_saved_name_entries->next) {
if ((type_of_from_packet == NAME_HOST || type_of_from_packet == NAME_SERVICE)
&& strncmp(already_saved_name_entries->name, name, len_of_name) == 0) {
OLSR_PRINTF(4, "NAME PLUGIN: received name or service entry %s (%s) already in hash table\n", name,
olsr_ip_to_string(&strbuf, &already_saved_name_entries->ip));
return;
} else if (type_of_from_packet == NAME_FORWARDER && ipequal(&already_saved_name_entries->ip, &from_packet->ip)) {
OLSR_PRINTF(4, "NAME PLUGIN: received forwarder entry %s (%s) already in hash table\n", name,
olsr_ip_to_string(&strbuf, &already_saved_name_entries->ip));
return;
} else if (type_of_from_packet == NAME_LATLON) {
if (0 != strncmp(already_saved_name_entries->name, name, len_of_name)) {
OLSR_PRINTF(4, "NAME PLUGIN: updating name %s -> %s (%s)\n", already_saved_name_entries->name, name,
olsr_ip_to_string(&strbuf, &already_saved_name_entries->ip));
free(already_saved_name_entries->name);
already_saved_name_entries->name = olsr_malloc(len_of_name + 1, "upd name_entry name");
strscpy(already_saved_name_entries->name, name, len_of_name + 1);
*this_table_changed = true;
olsr_start_write_file_timer();
}
if (!ipequal(&already_saved_name_entries->ip, &from_packet->ip)) {
struct ipaddr_str strbuf2, strbuf3;
OLSR_PRINTF(4, "NAME PLUGIN: updating ip %s -> %s (%s)\n", olsr_ip_to_string(&strbuf, &already_saved_name_entries->ip),
olsr_ip_to_string(&strbuf2, &from_packet->ip), olsr_ip_to_string(&strbuf3, &already_saved_name_entries->ip));
already_saved_name_entries->ip = from_packet->ip;
*this_table_changed = true;
olsr_start_write_file_timer();
}
if (!*this_table_changed) {
OLSR_PRINTF(4, "NAME PLUGIN: received latlon entry %s (%s) already in hash table\n", name,
olsr_ip_to_string(&strbuf, &already_saved_name_entries->ip));
}
return;
}
}
//if not yet known entry
tmp = olsr_malloc(sizeof(struct name_entry), "new name_entry");
tmp->type = ntohs(from_packet->type);
tmp->len = ntohs(from_packet->len);
tmp->name = olsr_malloc(tmp->len + 1, "new name_entry name");
tmp->ip = from_packet->ip;
strscpy(tmp->name, name, tmp->len + 1);
OLSR_PRINTF(3, "\nNAME PLUGIN: create new name/service/forwarder entry %s (%s) [len=%d] [type=%d] in linked list\n", tmp->name,
olsr_ip_to_string(&strbuf, &tmp->ip), tmp->len, tmp->type);
*this_table_changed = true;
olsr_start_write_file_timer();
// queue to front
tmp->next = *to;
*to = tmp;
}
/**
* unpack the received message and delegate to the decapsulation function for each
* name/service/forwarder entry in the message
*/
void
update_name_entry(union olsr_ip_addr *originator, struct namemsg *msg, int msg_size, olsr_reltime vtime)
{
struct ipaddr_str strbuf;
char *pos, *end_pos;
struct name *from_packet;
int i;
OLSR_PRINTF(3, "NAME PLUGIN: Received Message from %s\n", olsr_ip_to_string(&strbuf, originator));
if (ntohs(msg->version) != NAME_PROTOCOL_VERSION) {
OLSR_PRINTF(3, "NAME PLUGIN: ignoring wrong version %d\n", msg->version);
return;
}
/* now add the names from the message */
pos = (char *)msg + sizeof(struct namemsg);
end_pos = pos + msg_size - sizeof(struct name *); // at least one struct name has to be left
for (i = ntohs(msg->nr_names); i > 0 && pos < end_pos; i--) {
from_packet = (struct name *)ARM_NOWARN_ALIGN(pos);
switch (ntohs(from_packet->type)) {
case NAME_HOST:
insert_new_name_in_list(originator, name_list, from_packet, &name_table_changed, vtime);
break;
case NAME_FORWARDER:
insert_new_name_in_list(originator, forwarder_list, from_packet, &forwarder_table_changed, vtime);
break;
case NAME_SERVICE:
insert_new_name_in_list(originator, service_list, from_packet, &service_table_changed, vtime);
break;
case NAME_MACADDR:
insert_new_name_in_list(originator, mac_list, from_packet, &mac_table_changed, vtime);
break;
case NAME_LATLON:
insert_new_name_in_list(originator, latlon_list, from_packet, &latlon_table_changed, vtime);
break;
default:
OLSR_PRINTF(3, "NAME PLUGIN: Received Message of unknown type [%d] from (%s)\n", from_packet->type,
olsr_ip_to_string(&strbuf, originator));
break;
}
pos += sizeof(struct name);
pos += 1 + ((ntohs(from_packet->len) - 1) | 3);
}
if (i != 0)
OLSR_PRINTF(4, "NAME PLUGIN: Lost %d entries in received packet due to length inconsistency (%s)\n", i,
olsr_ip_to_string(&strbuf, originator));
}
/**
* insert all the new names,services and forwarders from a received packet into the
* corresponding entry for this ip in the corresponding hash table
*/
void
insert_new_name_in_list(union olsr_ip_addr *originator, struct list_node *this_list, struct name *from_packet,
bool * this_table_changed, olsr_reltime vtime)
{
int hash;
struct db_entry *entry;
struct list_node *list_head, *list_node;
bool entry_found = false;
hash = olsr_ip_hashing(originator);
/* find the entry for originator, if there is already one */
list_head = &this_list[hash];
for (list_node = list_head->next; list_node != list_head; list_node = list_node->next) {
entry = list2db(list_node);
if (ipequal(originator, &entry->originator)) {
struct ipaddr_str strbuf;
// found
OLSR_PRINTF(4, "NAME PLUGIN: found entry for (%s) in its hash table\n", olsr_ip_to_string(&strbuf, originator));
//delegate to function for parsing the packet and linking it to entry->names
decap_namemsg(from_packet, &entry->names, this_table_changed);
olsr_set_timer(&entry->db_timer, vtime, OLSR_NAMESVC_DB_JITTER, OLSR_TIMER_ONESHOT, &olsr_nameservice_expire_db_timer, entry,
0);
entry_found = true;
}
}
if (!entry_found) {
struct ipaddr_str strbuf;
OLSR_PRINTF(3, "NAME PLUGIN: create new db entry for ip (%s) in hash table\n", olsr_ip_to_string(&strbuf, originator));
/* insert a new entry */
entry = olsr_malloc(sizeof(struct db_entry), "new db_entry");
memset(entry, 0, sizeof(struct db_entry));
entry->originator = *originator;
olsr_set_timer(&entry->db_timer, vtime, OLSR_LINK_LOSS_JITTER, OLSR_TIMER_ONESHOT, &olsr_nameservice_expire_db_timer, entry, 0);
entry->names = NULL;
/* insert to the list */
list_add_before(&this_list[hash], &entry->db_list);
//delegate to function for parsing the packet and linking it to entry->names
decap_namemsg(from_packet, &entry->names, this_table_changed);
}
}
#ifndef _WIN32
static void