remove the need for C99 math (closes: #1579)
[openwrt/svn-archive/archive.git] / net / olsrd / patches / 130-olsrd-nameservice+services.patch
1 diff -Nur olsrd-0.4.10.orig/lib/nameservice/src/nameservice.c olsrd-0.4.10/lib/nameservice/src/nameservice.c
2 --- olsrd-0.4.10.orig/lib/nameservice/src/nameservice.c 2005-05-29 14:47:42.000000000 +0200
3 +++ olsrd-0.4.10/lib/nameservice/src/nameservice.c 2006-12-01 08:26:58.000000000 +0100
4 @@ -1,4 +1,5 @@
5 /*
6 + * Copyright (c) 2006, Jens Nachtigall <nachtigall@web.de>
7 * Copyright (c) 2005, Bruno Randolf <bruno.randolf@4g-systems.biz>
8 * Copyright (c) 2004, Andreas Tønnesen(andreto-at-olsr.org)
9 * All rights reserved.
10 @@ -39,6 +40,9 @@
11 #include <string.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 +#include <ctype.h>
15 +#include <sys/types.h>
16 +#include <regex.h>
17
18 #include "olsr.h"
19 #include "net_olsr.h"
20 @@ -53,6 +57,7 @@
21 #include "olsrd_copy.h"
22 #include "compat.h"
23
24 +
25 /* send buffer: huge */
26 static char buffer[10240];
27
28 @@ -62,15 +67,35 @@
29 static char my_suffix[MAX_SUFFIX];
30 static int my_interval = EMISSION_INTERVAL;
31 static double my_timeout = NAME_VALID_TIME;
32 -static olsr_bool have_dns_server = OLSR_FALSE;
33 -static union olsr_ip_addr my_dns_server;
34 static char my_resolv_file[MAX_FILE +1];
35 +static char my_services_file[MAX_FILE + 1];
36
37 -/* the database (using hashing) */
38 +/* the databases (using hashing)
39 + * for hostnames, service_lines and dns-servers
40 + *
41 + * my own hostnames, service_lines and dns-servers
42 + * are store in a linked list (without hashing)
43 + * */
44 struct db_entry* list[HASHSIZE];
45 struct name_entry *my_names = NULL;
46 olsr_bool name_table_changed = OLSR_TRUE;
47
48 +struct db_entry* service_list[HASHSIZE];
49 +struct name_entry *my_services = NULL;
50 +olsr_bool service_table_changed = OLSR_TRUE;
51 +
52 +struct db_entry* forwarder_list[HASHSIZE];
53 +struct name_entry *my_forwarders = NULL;
54 +olsr_bool forwarder_table_changed = OLSR_TRUE;
55 +
56 +/* regualar expression to be matched by valid hostnames, compiled in name_init() */
57 +regex_t regex_t_name;
58 +regmatch_t regmatch_t_name;
59 +
60 +/* regualar expression to be matched by valid service_lines, compiled in name_init() */
61 +regex_t regex_t_service;
62 +int pmatch_service = 10;
63 +regmatch_t regmatch_t_service[10];
64
65 /**
66 * do initialization
67 @@ -84,27 +109,39 @@
68 int len;
69
70 GetWindowsDirectory(my_hosts_file, MAX_FILE - 12);
71 + GetWindowsDirectory(my_services_file, MAX_FILE - 12);
72
73 len = strlen(my_hosts_file);
74 -
75 if (my_hosts_file[len - 1] != '\\')
76 my_hosts_file[len++] = '\\';
77 -
78 strcpy(my_hosts_file + len, "hosts_olsr");
79 +
80 + len = strlen(my_services_file);
81 + if (my_services_file[len - 1] != '\\')
82 + my_services_file[len++] = '\\';
83 + strcpy(my_services_file + len, "services_olsr");
84 +
85 + len = strlen(my_resolv_file);
86 + if (my_resolv_file[len - 1] != '\\')
87 + my_resolv_file[len++] = '\\';
88 + strcpy(my_resolv_file + len, "resolvconf_olsr");
89 #else
90 strcpy(my_hosts_file, "/var/run/hosts_olsr");
91 + strcpy(my_services_file, "/var/run/services_olsr");
92 + strcpy(my_resolv_file, "/var/run/resolvconf_olsr");
93 #endif
94
95 my_suffix[0] = '\0';
96 my_add_hosts[0] = '\0';
97 - my_resolv_file[0] = '\0';
98
99 - /* init list */
100 + /* init lists */
101 for(i = 0; i < HASHSIZE; i++) {
102 list[i] = NULL;
103 + forwarder_list[i] = NULL;
104 + service_list[i] = NULL;
105 }
106
107 - memset(&my_dns_server, 0, sizeof(my_dns_server));
108 +
109 }
110
111
112 @@ -116,56 +153,53 @@
113 {
114 if(!strcmp(key, "interval")) {
115 my_interval = atoi(value);
116 - printf("\nNAME PLUGIN: parameter interval: %d\n", my_interval);
117 + olsr_printf(1,"\nNAME PLUGIN: parameter interval: %d", my_interval);
118 }
119 else if(!strcmp(key, "timeout")) {
120 my_timeout = atof(value);
121 - printf("\nNAME PLUGIN: parameter timeout: %f\n", my_timeout);
122 + olsr_printf(1,"\nNAME PLUGIN: parameter timeout: %f", my_timeout);
123 }
124 else if(!strcmp(key, "hosts-file")) {
125 strncpy( my_hosts_file, value, MAX_FILE );
126 - printf("\nNAME PLUGIN: parameter filename: %s\n", my_hosts_file);
127 + olsr_printf(1,"\nNAME PLUGIN: parameter filename: %s", my_hosts_file);
128 }
129 else if(!strcmp(key, "resolv-file")) {
130 strncpy(my_resolv_file, value, MAX_FILE);
131 - printf("\nNAME PLUGIN: parameter resolv file: %s\n", my_resolv_file);
132 + olsr_printf(1,"\nNAME PLUGIN: parameter resolv-file: %s", my_resolv_file);
133 }
134 else if(!strcmp(key, "suffix")) {
135 strncpy(my_suffix, value, MAX_SUFFIX);
136 - printf("\nNAME PLUGIN: parameter suffix: %s\n", my_suffix);
137 + olsr_printf(1,"\nNAME PLUGIN: parameter suffix: %s", my_suffix);
138 }
139 else if(!strcmp(key, "add-hosts")) {
140 strncpy(my_add_hosts, value, MAX_FILE);
141 - printf("\nNAME PLUGIN: parameter additional host: %s\n", my_add_hosts);
142 + olsr_printf(1,"\nNAME PLUGIN: parameter additional host: %s", my_add_hosts);
143 + }
144 + else if(!strcmp(key, "services-file")) {
145 + strncpy(my_services_file, value, MAX_FILE);
146 + olsr_printf(1,"\nNAME PLUGIN: parameter services-file: %s", my_services_file);
147 }
148 else if(!strcmp(key, "dns-server")) {
149 struct in_addr ip;
150 if (strlen(value) == 0) {
151 - // set dns server ip to main address
152 - // which is not known yet
153 - have_dns_server = OLSR_TRUE;
154 - }
155 - else if (inet_aton(value, &ip)) {
156 - my_dns_server.v4 = ip.s_addr;
157 - have_dns_server = OLSR_TRUE;
158 - }
159 - else {
160 - printf("\nNAME PLUGIN: invalid dns-server IP %s\n", value);
161 - }
162 + my_forwarders = add_name_to_list(my_forwarders, "", NAME_FORWARDER, NULL);
163 + olsr_printf(1,"\nNAME PLUGIN: parameter dns-server: (main address)");
164 + } else if (inet_aton(value, &ip)) {
165 + my_forwarders = add_name_to_list(my_forwarders, "", NAME_FORWARDER, &ip);
166 + olsr_printf(1,"\nNAME PLUGIN: parameter dns-server: (%s)", value);
167 + } else {
168 + olsr_printf(1,"\nNAME PLUGIN: invalid parameter dns-server: %s ", value);
169 + }
170 }
171 else if(!strcmp(key, "name")) {
172 // name for main address
173 - struct name_entry *tmp;
174 - tmp = malloc(sizeof(struct name_entry));
175 - tmp->name = strndup( value, MAX_NAME );
176 - tmp->len = strlen( tmp->name );
177 - tmp->type = NAME_HOST;
178 - // will be set to main_addr later
179 - memset(&tmp->ip, 0, sizeof(tmp->ip));
180 - tmp->next = my_names;
181 - my_names = tmp;
182 -
183 - printf("\nNAME PLUGIN: parameter name: %s (main address)\n", tmp->name);
184 + my_names = add_name_to_list(my_names, value, NAME_HOST, NULL);
185 + olsr_printf(1,"\nNAME PLUGIN: parameter name: %s (main address)", value);
186 + }
187 + else if(!strcmp(key, "service")) {
188 + // name for main address
189 + my_services = add_name_to_list(my_services, value, NAME_SERVICE, NULL);
190 + olsr_printf(1,"\nNAME PLUGIN: parameter service: %s (main address)", value);
191 }
192 else {
193 // assume this is an IP address and hostname
194 @@ -173,33 +207,46 @@
195
196 if (inet_aton(key, &ip)) {
197 // the IP is validated later
198 - struct name_entry *tmp;
199 - tmp = malloc(sizeof(struct name_entry));
200 - tmp->name = strndup( value, MAX_NAME );
201 - tmp->len = strlen( tmp->name );
202 - tmp->type = NAME_HOST;
203 - tmp->ip.v4 = ip.s_addr;
204 - tmp->next = my_names;
205 - my_names = tmp;
206 - printf("\nNAME PLUGIN: parameter %s (%s)\n", tmp->name,
207 - olsr_ip_to_string(&tmp->ip));
208 + my_names = add_name_to_list(my_names, value, NAME_HOST, &ip);
209 + olsr_printf(1,"\nNAME PLUGIN: parameter name %s (%s)", value, key);
210 }
211 else {
212 - printf("\nNAME PLUGIN: invalid IP %s for name %s!\n", key, value);
213 + olsr_printf(1,"\nNAME PLUGIN: invalid parameter name: %s (%s)!", value, key);
214 }
215 }
216
217 return 1;
218 }
219
220 +/**
221 + * queue the name/forwarder/service given in value
222 + * to the front of my_list
223 + */
224 +struct name_entry*
225 +add_name_to_list(struct name_entry *my_list, char *value, int type, struct in_addr *ip)
226 +{
227 + struct name_entry *tmp;
228 + tmp = olsr_malloc(sizeof(struct name_entry), "new name_entry add_name_to_list");
229 + tmp->name = strndup( value, MAX_NAME );
230 + tmp->len = strlen( tmp->name );
231 + tmp->type = type;
232 + // all IPs with value 0 will be set to main_addr later
233 + if (ip==NULL)
234 + memset(&tmp->ip, 0, sizeof(tmp->ip));
235 + else
236 + tmp->ip.v4 = ip->s_addr;
237 + tmp->next = my_list;
238 + return tmp;
239 +}
240 +
241
242 /**
243 * last initialization
244 *
245 * we have to do this here because some things like main_addr
246 - * are not known before
247 + * or the dns suffix (for validation) are not known before
248 *
249 - * this is beause of the order in which the plugin is initzalized
250 + * this is beause of the order in which the plugin is initialized
251 * by the plugin loader:
252 * - first the parameters are sent
253 * - then register_olsr_data() from olsrd_plugin.c is called
254 @@ -210,50 +257,78 @@
255 name_init()
256 {
257 struct name_entry *name;
258 - struct name_entry *prev=NULL;
259 -
260 - /* fixup names and IP addresses */
261 + int ret;
262 + //regex string for validating the hostnames
263 + char *regex_name = "^[[:alnum:]_.-]+$";
264 + //regex string for the service line
265 + char *regex_service = olsr_malloc(256*sizeof(char) + strlen(my_suffix), "new *char from name_init for regex_service");
266 +
267 + //compile the regex from the string
268 + if ((ret = regcomp(&regex_t_name, regex_name , REG_EXTENDED)) != 0)
269 + {
270 + /* #2: call regerror() if regcomp failed
271 + * commented out, because only for debuggin needed
272 + *
273 + int errmsgsz = regerror(ret, &regex_t_name, NULL, 0);
274 + char *errmsg = malloc(errmsgsz);
275 + regerror(ret, &regex_t_name, errmsg, errmsgsz);
276 + fprintf(stderr, "regcomp: %s", errmsg);
277 + free(errmsg);
278 + regfree(&regex_t_name);
279 + * */
280 + olsr_printf(0, "compilation of regex \"%s\" for hostname failed", regex_name);
281 + }
282 +
283 + // a service line is something like prot://hostname.suffix:port|tcp|my little description about this service
284 + // for example http://router.olsr:80|tcp|my homepage
285 + // prot :// (hostname.suffix OR ip)
286 + //regex_service = "^[[:alnum:]]+://(([[:alnum:]_.-]+.olsr)|([[:digit:]]{1,3}\\.[[:digit:]]{1,3}\\.[[:digit:]]{1,3}\\.[[:digit:]]{1,3}))
287 + // : port /path |(tcp OR udp) |short description
288 + // :[[:digit:]]+[[:alnum:]/?._=#-]*\\|(tcp|udp)\\|[^|[:cntrl:]]+$";
289 + strcat (strcat (strcat(regex_service, "^[[:alnum:]]+://(([[:alnum:]_.-]+"),
290 + my_suffix),
291 + ")|([[:digit:]]{1,3}\\.[[:digit:]]{1,3}\\.[[:digit:]]{1,3}\\.[[:digit:]]{1,3})):[[:digit:]]+[[:alnum:]/?._=#-]*\\|(tcp|udp)\\|[^|[:cntrl:]]+$");
292 +
293 + /* #1: call regcomp() to compile the regex */
294 + if ((ret = regcomp(&regex_t_service, regex_service , REG_EXTENDED )) != 0)
295 + {
296 + /* #2: call regerror() if regcomp failed
297 + * commented out, because only for debuggin needed
298 + *
299 + int errmsgsz = regerror(ret, &regex_t_service, NULL, 0);
300 + char *errmsg = malloc(errmsgsz);
301 + regerror(ret, &regex_t_service, errmsg, errmsgsz);
302 + fprintf(stderr, "regcomp: %s", errmsg);
303 + free(errmsg);
304 + regfree(&regex_t_service);
305 + * */
306 + olsr_printf(0, "compilation of regex \"%s\" for hostname failed", regex_name);
307 + }
308 + free(regex_service);
309 + regex_service = NULL;
310 +
311 + //fill in main addr for all entries with ip==0
312 + //this does not matter for service, because the ip does not matter
313 + //for service
314 for (name = my_names; name != NULL; name = name->next) {
315 - if (name->ip.v4 == 0) {
316 - // insert main_addr
317 + if (name->ip.v4 == 0) {
318 + olsr_printf(2, "NAME PLUGIN: insert main addr for name %s \n", name->name);
319 memcpy(&name->ip, &main_addr, ipsize);
320 - prev = name;
321 - } else {
322 - // IP from config file
323 - // check if we are allowed to announce a name for this IP
324 - // we can only do this if we also announce the IP
325 - if (!allowed_ip(&name->ip)) {
326 - olsr_printf(1, "NAME PLUGIN: name for unknown IP %s not allowed, fix your config!\n",
327 - olsr_ip_to_string(&name->ip));
328 - if (prev!=NULL) {
329 - prev->next = name->next;
330 - free(name->name);
331 - free(name);
332 - }
333 - }
334 - else {
335 - prev = name;
336 - }
337 - }
338 - }
339 + }
340 + }
341 + for (name = my_forwarders; name != NULL; name = name->next) {
342 + if (name->ip.v4 == 0) {
343 + olsr_printf(2, "NAME PLUGIN: insert main addr for name %s \n", name->name);
344 + memcpy(&name->ip, &main_addr, ipsize);
345 + }
346 + }
347 +
348 + //check if entries I want to announce myself are valid and allowed
349 + my_names = remove_nonvalid_names_from_list(my_names, NAME_HOST);
350 + my_forwarders = remove_nonvalid_names_from_list(my_forwarders, NAME_FORWARDER);
351 + my_services = remove_nonvalid_names_from_list(my_services, NAME_SERVICE);
352 +
353
354 - if (have_dns_server) {
355 - if (my_dns_server.v4 == 0) {
356 - memcpy(&my_dns_server, &main_addr, ipsize);
357 - printf("\nNAME PLUGIN: announcing upstream DNS server: %s\n",
358 - olsr_ip_to_string(&my_dns_server));
359 - }
360 - else if (!allowed_ip(&my_dns_server)) {
361 - printf("NAME PLUGIN: announcing DNS server on unknown IP %s is not allowed, fix your config!\n",
362 - olsr_ip_to_string(&my_dns_server));
363 - memset(&my_dns_server, 0, sizeof(my_dns_server));
364 - have_dns_server = OLSR_FALSE;
365 - }
366 - else {
367 - printf("\nNAME PLUGIN: announcing upstream DNS server: %s\n",
368 - olsr_ip_to_string(&my_dns_server));
369 - }
370 - }
371
372 /* register functions with olsrd */
373 olsr_parser_add_function(&olsr_parser, PARSER_TYPE, 1);
374 @@ -263,22 +338,75 @@
375 return 1;
376 }
377
378 +struct name_entry*
379 +remove_nonvalid_names_from_list(struct name_entry *my_list, int type)
380 +{
381 + struct name_entry *next = my_list;
382 + olsr_bool valid = OLSR_FALSE;
383 + if (my_list == NULL) {
384 + return NULL;
385 + }
386 +
387 + switch (type) {
388 + case NAME_HOST:
389 + valid = is_name_wellformed(my_list->name) && allowed_ip(&my_list->ip);
390 + break;
391 + case NAME_FORWARDER:
392 + valid = allowed_ip(&my_list->ip);
393 + break;
394 + case NAME_SERVICE:
395 + valid = allowed_service(my_list->name);
396 + break;
397 + }
398 +
399 + if ( !valid ) {
400 + olsr_printf(1, "NAME PLUGIN: invalid or malformed parameter %s (%s), fix your config!\n", my_list->name, olsr_ip_to_string(&my_list->ip));
401 + next = my_list->next;
402 + free(my_list->name);
403 + my_list->name = NULL;
404 + free(my_list);
405 + my_list = NULL;
406 + return remove_nonvalid_names_from_list(next, type);
407 + } else {
408 + olsr_printf(2, "NAME PLUGIN: validate parameter %s (%s) -> OK\n", my_list->name, olsr_ip_to_string(&my_list->ip));
409 + my_list->next = remove_nonvalid_names_from_list(my_list->next, type);
410 + return my_list;
411 + }
412 +}
413 +
414 +
415
416 /**
417 * called at unload: free everything
418 + *
419 + * XXX: should I delete the hosts/services/resolv.conf files on exit?
420 */
421 void
422 name_destructor()
423 {
424 - int i;
425 - struct db_entry **tmp;
426 - struct db_entry *to_delete;
427 -
428 olsr_printf(2, "NAME PLUGIN: exit. cleaning up...\n");
429
430 free_name_entry_list(&my_names);
431 + free_name_entry_list(&my_services);
432 + free_name_entry_list(&my_forwarders);
433 +
434 + free_all_list_entries(list);
435 + free_all_list_entries(service_list);
436 + free_all_list_entries(forwarder_list);
437 +
438 + regfree(&regex_t_name);
439 + regfree(&regex_t_service);
440 +
441 +}
442 +
443 +/* free all list entries */
444 +void
445 +free_all_list_entries(struct db_entry **this_db_list)
446 +{
447 + int i;
448 + struct db_entry **tmp;
449 + struct db_entry *to_delete;
450
451 - /* free list entries */
452 for(i = 0; i < HASHSIZE; i++)
453 {
454 tmp = &list[i];
455 @@ -296,24 +424,41 @@
456
457 /**
458 * A timeout function called every time
459 - * the scheduler is polled: time out old list entries
460 + *
461 + * XXX:the scheduler is polled (by default 10 times a sec,
462 + * which is far too often):
463 + *
464 + * time out old list entries
465 * and write changes to file
466 */
467 void
468 olsr_timeout()
469 {
470 + timeout_old_names(list, &name_table_changed);
471 + timeout_old_names(forwarder_list, &forwarder_table_changed);
472 + timeout_old_names(service_list, &service_table_changed);
473 +
474 + write_resolv_file();
475 + write_hosts_file();
476 + write_services_file();
477 +}
478 +
479 +void
480 +timeout_old_names(struct db_entry **this_list, olsr_bool *this_table_changed)
481 +{
482 struct db_entry **tmp;
483 struct db_entry *to_delete;
484 int index;
485
486 for(index=0;index<HASHSIZE;index++)
487 {
488 - for (tmp = &list[index]; *tmp != NULL; )
489 + for (tmp = &this_list[index]; *tmp != NULL; )
490 {
491 - /* check if the entry is timed out */
492 + /* check if the entry for this ip is timed out */
493 if (olsr_timed_out(&(*tmp)->timer))
494 {
495 to_delete = *tmp;
496 + /* update the pointer in the linked list */
497 *tmp = (*tmp)->next;
498
499 olsr_printf(2, "NAME PLUGIN: %s timed out... deleting\n",
500 @@ -322,17 +467,17 @@
501 /* Delete */
502 free_name_entry_list(&to_delete->names);
503 free(to_delete);
504 - name_table_changed = OLSR_TRUE;
505 + *this_table_changed = OLSR_TRUE;
506 } else {
507 tmp = &(*tmp)->next;
508 }
509 }
510 }
511 - write_resolv_file();
512 - write_hosts_file();
513 }
514
515
516 +
517 +
518 /**
519 * Scheduled event: generate and send NAME packet
520 */
521 @@ -429,21 +574,19 @@
522 return;
523 }
524
525 - /* Check if this message has been processed before
526 - * Remeber that this also registeres the message as
527 + /* Check if this message has not been processed before
528 + * Remember that this also registers the message as
529 * processed if nessecary
530 */
531 - if(!olsr_check_dup_table_proc(&originator, ntohs(m->v4.seqno))) {
532 - /* If so - do not process */
533 - goto forward;
534 - }
535 -
536 - update_name_entry(&originator, namemessage, size, vtime);
537 + if(olsr_check_dup_table_proc(&originator, ntohs(m->v4.seqno))) {
538 + /* If not already processed - do so */
539 + update_name_entry(&originator, namemessage, size, vtime);
540 + }
541 +
542 + /* Forward the message if nessecary
543 + * default_fwd does all the work for us! */
544 + olsr_forward_message(m, &originator, ntohs(m->v4.seqno), in_if, in_addr);
545
546 -forward:
547 - /* Forward the message if nessecary
548 - * default_fwd does all the work for us! */
549 - olsr_forward_message(m, &originator, ntohs(m->v4.seqno), in_if, in_addr);
550 }
551
552
553 @@ -457,152 +600,224 @@
554 int
555 encap_namemsg(struct namemsg* msg)
556 {
557 - struct name_entry *my_name = my_names;
558 - struct name* to_packet;
559 + struct name_entry *my_name;
560 + struct name_entry *my_service;
561 +
562 + // add the hostname, service and forwarder entries after the namemsg header
563 char* pos = (char*)msg + sizeof(struct namemsg);
564 short i=0;
565 - int k;
566 -
567 - // upstream dns server
568 - if (have_dns_server) {
569 - olsr_printf(3, "NAME PLUGIN: Announcing DNS server (%s)\n",
570 - olsr_ip_to_string(&my_dns_server));
571 - to_packet = (struct name*)pos;
572 - to_packet->type = htons(NAME_FORWARDER);
573 - to_packet->len = htons(0);
574 - memcpy(&to_packet->ip, &my_dns_server, ipsize);
575 - pos += sizeof(struct name);
576 - i++;
577 - }
578 +
579
580 // names
581 for (my_name = my_names; my_name!=NULL; my_name = my_name->next)
582 {
583 - olsr_printf(3, "NAME PLUGIN: Announcing name %s (%s) %d\n",
584 - my_name->name, olsr_ip_to_string(&my_name->ip), my_name->len);
585 -
586 - to_packet = (struct name*)pos;
587 - to_packet->type = htons(my_name->type);
588 - to_packet->len = htons(my_name->len);
589 - memcpy(&to_packet->ip, &my_name->ip, ipsize);
590 - pos += sizeof(struct name);
591 - strncpy(pos, my_name->name, my_name->len);
592 - pos += my_name->len;
593 - // padding to 4 byte boundaries
594 - for (k = my_name->len; (k & 3) != 0; k++)
595 - *pos++ = '\0';
596 + pos = create_packet( (struct name*) pos, my_name);
597 i++;
598 }
599 + // forwarders
600 + for (my_name = my_forwarders; my_name!=NULL; my_name = my_name->next)
601 + {
602 + pos = create_packet( (struct name*) pos, my_name);
603 + i++;
604 + }
605 + // services
606 + for (my_service = my_services; my_service!=NULL; my_service = my_service->next)
607 + {
608 + pos = create_packet( (struct name*) pos, my_service);
609 + i++;
610 + }
611 +
612 + // write the namemsg header with the number of announced entries and the protocol version
613 msg->nr_names = htons(i);
614 msg->version = htons(NAME_PROTOCOL_VERSION);
615 +
616 return pos - (char*)msg; //length
617 }
618
619
620 /**
621 - * decapsulate a name message and update name_entries if necessary
622 + * convert each of my to be announced name_entries into network
623 + * compatible format
624 + *
625 + * return the length of the name packet
626 + */
627 +char*
628 +create_packet(struct name* to, struct name_entry *from)
629 +{
630 + char *pos = (char*) to;
631 + int k;
632 + olsr_printf(3, "NAME PLUGIN: Announcing name %s (%s) %d\n",
633 + from->name, olsr_ip_to_string(&from->ip), from->len);
634 + to->type = htons(from->type);
635 + to->len = htons(from->len);
636 + memcpy(&to->ip, &from->ip, ipsize);
637 + pos += sizeof(struct name);
638 + strncpy(pos, from->name, from->len);
639 + pos += from->len;
640 + for (k = from->len; (k & 3) != 0; k++)
641 + *pos++ = '\0';
642 + return pos;
643 +}
644 +
645 +/**
646 + * decapsulate a received name, service or forwarder and update the corresponding hash table if necessary
647 + */
648 +void
649 +decap_namemsg(struct name *from_packet, struct name_entry **to, olsr_bool *this_table_changed )
650 +{
651 + struct name_entry *tmp;
652 + struct name_entry *already_saved_name_entries;
653 + char *name = (char*)from_packet + sizeof(struct name);
654 + olsr_printf(4, "\nNAME PLUGIN: decapsulating received name, service or forwarder \n");
655 + int type_of_from_packet = ntohs(from_packet->type);
656 + unsigned int len_of_name = ntohs(from_packet->len);
657 +
658 + // don't insert the received entry again, if it has already been inserted in the hash table.
659 + // Instead only the validity time is set in insert_new_name_in_list function, which calls this one
660 + for (already_saved_name_entries = (*to); already_saved_name_entries != NULL ; already_saved_name_entries = already_saved_name_entries->next)
661 + {
662 + if ( (type_of_from_packet==NAME_HOST || type_of_from_packet==NAME_SERVICE) && strncmp(already_saved_name_entries->name, name, len_of_name) == 0 ) {
663 + olsr_printf(4, "\nNAME PLUGIN: received name or service entry %s (%s) already in hash table\n", name, olsr_ip_to_string(&already_saved_name_entries->ip));
664 + return;
665 + } else if (type_of_from_packet==NAME_FORWARDER && COMP_IP(&already_saved_name_entries->ip, &from_packet->ip) ) {
666 + olsr_printf(4, "\nNAME PLUGIN: received forwarder entry %s (%s) already in hash table\n", name, olsr_ip_to_string(&already_saved_name_entries->ip));
667 + return;
668 + }
669 + }
670 +
671 + //XXX: should I check the from_packet->ip here? If so, why not also check the ip fro HOST and SERVICE?
672 + if( (type_of_from_packet==NAME_HOST && !is_name_wellformed(name)) || (type_of_from_packet==NAME_SERVICE && !is_service_wellformed(name)) ) {
673 + olsr_printf(4, "\nNAME PLUGIN: invalid name [%s] received, skipping.\n", name );
674 + return;
675 + }
676 +
677 + //ignore all packets with a too long name
678 + //or a spoofed len of its included name string
679 + if (len_of_name > MAX_NAME || strlen(name) != len_of_name) {
680 + olsr_printf(4, "\nNAME PLUGIN: from_packet->len %d > MAX_NAME %d or from_packet->len %d !0 strlen(name [%s] in packet)\n",
681 + len_of_name, MAX_NAME, len_of_name, name );
682 + return;
683 + }
684 +
685 + //if not yet known entry
686 + tmp = olsr_malloc(sizeof(struct name_entry), "new name_entry");
687 + tmp->type = ntohs(from_packet->type);
688 + tmp->len = len_of_name > MAX_NAME ? MAX_NAME : ntohs(from_packet->len);
689 + tmp->name = olsr_malloc(tmp->len+1, "new name_entry name");
690 + memcpy(&tmp->ip, &from_packet->ip, ipsize);
691 + strncpy(tmp->name, name, tmp->len);
692 + tmp->name[tmp->len] = '\0';
693 +
694 + olsr_printf(3, "\nNAME PLUGIN: create new name/service/forwarder entry %s (%s) [len=%d] [type=%d] in linked list\n",
695 + tmp->name, olsr_ip_to_string(&tmp->ip), tmp->len, tmp->type);
696 +
697 + *this_table_changed = OLSR_TRUE;
698 +
699 + // queue to front
700 + tmp->next = *to;
701 + *to = tmp;
702 +}
703 +
704 +
705 +/**
706 + * unpack the received message and delegate to the decapsilation function for each
707 + * name/service/forwarder entry in the message
708 */
709 void
710 -decap_namemsg( struct namemsg *msg, int size, struct name_entry **to )
711 +update_name_entry(union olsr_ip_addr *originator, struct namemsg *msg, int msg_size, double vtime)
712 {
713 char *pos, *end_pos;
714 - struct name_entry *tmp;
715 struct name *from_packet;
716 int i;
717 +
718 + olsr_printf(3, "NAME PLUGIN: Received Message from %s\n",
719 + olsr_ip_to_string(originator));
720
721 - olsr_printf(4, "NAME PLUGIN: decapsulating name msg (size %d)\n", size);
722 -
723 - if (ntohs(msg->version) != NAME_PROTOCOL_VERSION) {
724 + if (ntohs(msg->version) != NAME_PROTOCOL_VERSION) {
725 olsr_printf(3, "NAME PLUGIN: ignoring wrong version %d\n", msg->version);
726 return;
727 }
728 -
729 - // for now ist easier to just delete everything, than
730 - // to update selectively
731 - free_name_entry_list(to);
732 - name_table_changed = OLSR_TRUE;
733 -
734 +
735 +
736 /* now add the names from the message */
737 pos = (char*)msg + sizeof(struct namemsg);
738 - end_pos = pos + size - sizeof(struct name*); // at least one struct name has to be left
739 -
740 + end_pos = pos + msg_size - sizeof(struct name*); // at least one struct name has to be left
741 +
742 for (i=ntohs(msg->nr_names); i > 0 && pos<end_pos; i--)
743 {
744 from_packet = (struct name*)pos;
745 -
746 - tmp = olsr_malloc(sizeof(struct name_entry), "new name_entry");
747 - tmp->type = ntohs(from_packet->type);
748 - tmp->len = ntohs(from_packet->len) > MAX_NAME ? MAX_NAME : ntohs(from_packet->len);
749 - tmp->name = olsr_malloc(tmp->len+1, "new name_entry name");
750 - memcpy(&tmp->ip, &from_packet->ip, ipsize);
751 +
752 + switch (ntohs(from_packet->type)) {
753 + case NAME_HOST:
754 + insert_new_name_in_list(originator, list, from_packet, &name_table_changed, vtime);
755 + break;
756 + case NAME_FORWARDER:
757 + insert_new_name_in_list(originator, forwarder_list, from_packet, &forwarder_table_changed, vtime);
758 + break;
759 + case NAME_SERVICE:
760 + insert_new_name_in_list(originator, service_list, from_packet, &service_table_changed, vtime);
761 + break;
762 + default:
763 + olsr_printf(3, "NAME PLUGIN: Received Message of unknown type [%d] from (%s)\n", from_packet->type, olsr_ip_to_string(originator));
764 + break;
765 + }
766 pos += sizeof(struct name);
767 - strncpy(tmp->name, pos, tmp->len);
768 - tmp->name[tmp->len] = '\0';
769 -
770 - olsr_printf(3, "NAME PLUGIN: New name %s (%s) %d %d\n",
771 - tmp->name, olsr_ip_to_string(&tmp->ip), tmp->len, tmp->type);
772 -
773 - // queue to front
774 - tmp->next = *to;
775 - *to = tmp;
776 -
777 - // name + padding
778 - pos += 1 + ((tmp->len - 1) | 3);
779 - }
780 + pos += 1 + (( ntohs(from_packet->len) - 1) | 3);
781 + }
782 if (i!=0)
783 - olsr_printf(4, "NAME PLUGIN: Lost %d names due to length inconsistency\n", i);
784 + olsr_printf(4, "NAME PLUGIN: Lost %d entries in received packet due to length inconsistency (%s)\n", i, olsr_ip_to_string(originator));
785 }
786
787
788 /**
789 - * Update or register a new name entry
790 + * insert all the new names,services and forwarders from a received packet into the
791 + * corresponding entry for this ip in the corresponding hash table
792 */
793 void
794 -update_name_entry(union olsr_ip_addr *originator, struct namemsg *msg, int msg_size, double vtime)
795 +insert_new_name_in_list(union olsr_ip_addr *originator, struct db_entry **this_list, struct name *from_packet, olsr_bool *this_table_changed, double vtime)
796 {
797 int hash;
798 struct db_entry *entry;
799 -
800 - olsr_printf(3, "NAME PLUGIN: Received Name Message from %s\n",
801 - olsr_ip_to_string(originator));
802 + olsr_bool entry_found = OLSR_FALSE;
803 + entry_found = OLSR_FALSE;
804
805 hash = olsr_hashing(originator);
806
807 - /* find the entry for originator */
808 - for (entry = list[hash]; entry != NULL; entry = entry->next)
809 - {
810 - if (memcmp(originator, &entry->originator, ipsize) == 0) {
811 - // found
812 - olsr_printf(4, "NAME PLUGIN: %s found\n",
813 - olsr_ip_to_string(originator));
814 -
815 - decap_namemsg(msg, msg_size, &entry->names);
816 -
817 - olsr_get_timestamp(vtime * 1000, &entry->timer);
818 -
819 - return;
820 - }
821 - }
822 -
823 - olsr_printf(3, "NAME PLUGIN: New entry %s\n",
824 - olsr_ip_to_string(originator));
825 -
826 - /* insert a new entry */
827 - entry = olsr_malloc(sizeof(struct db_entry), "new db_entry");
828 -
829 - memcpy(&entry->originator, originator, ipsize);
830 - olsr_get_timestamp(vtime * 1000, &entry->timer);
831 - entry->names = NULL;
832 - // queue to front
833 - entry->next = list[hash];
834 - list[hash] = entry;
835 -
836 - decap_namemsg(msg, msg_size, &entry->names);
837 -
838 - name_table_changed = OLSR_TRUE;
839 + /* find the entry for originator, if there is already one */
840 + for (entry = this_list[hash]; entry != NULL; entry = entry->next)
841 + {
842 + if (memcmp(originator, &entry->originator, ipsize) == 0) {
843 + // found
844 + olsr_printf(4, "NAME PLUGIN: found entry for (%s) in its hash table\n", olsr_ip_to_string(originator));
845 +
846 + //delegate to function for parsing the packet and linking it to entry->names
847 + decap_namemsg(from_packet, &entry->names, this_table_changed);
848 +
849 + olsr_get_timestamp(vtime * 1000, &entry->timer);
850 +
851 + entry_found = OLSR_TRUE;
852 + }
853 + }
854 + if (! entry_found)
855 + {
856 + olsr_printf(3, "NAME PLUGIN: create new db entry for ip (%s) in hash table\n", olsr_ip_to_string(originator));
857 +
858 + /* insert a new entry */
859 + entry = olsr_malloc(sizeof(struct db_entry), "new db_entry");
860 +
861 + memcpy(&entry->originator, originator, ipsize);
862 + olsr_get_timestamp(vtime * 1000, &entry->timer);
863 + entry->names = NULL;
864 +
865 + // queue to front
866 + entry->next = this_list[hash];
867 + this_list[hash] = entry;
868 +
869 + //delegate to function for parsing the packet and linking it to entry->names
870 + decap_namemsg(from_packet, &entry->names, this_table_changed);
871 + }
872 }
873
874 -
875 /**
876 * write names to a file in /etc/hosts compatible format
877 */
878 @@ -650,8 +865,7 @@
879
880 // write own names
881 for (name = my_names; name != NULL; name = name->next) {
882 - fprintf(hosts, "%s\t%s%s\t# myself\n", olsr_ip_to_string(&name->ip),
883 - name->name, my_suffix );
884 + fprintf(hosts, "%s\t%s%s\t# myself\n", olsr_ip_to_string(&name->ip), name->name, my_suffix );
885 }
886
887 // write received names
888 @@ -661,13 +875,11 @@
889 {
890 for (name = entry->names; name != NULL; name = name->next)
891 {
892 - if (name->type == NAME_HOST) {
893 - olsr_printf(6, "%s\t%s%s", olsr_ip_to_string(&name->ip), name->name, my_suffix);
894 - olsr_printf(6, "\t#%s\n", olsr_ip_to_string(&entry->originator));
895 -
896 - fprintf(hosts, "%s\t%s%s", olsr_ip_to_string(&name->ip), name->name, my_suffix);
897 - fprintf(hosts, "\t# %s\n", olsr_ip_to_string(&entry->originator));
898 - }
899 + olsr_printf(6, "%s\t%s%s", olsr_ip_to_string(&name->ip), name->name, my_suffix);
900 + olsr_printf(6, "\t#%s\n", olsr_ip_to_string(&entry->originator));
901 +
902 + fprintf(hosts, "%s\t%s%s", olsr_ip_to_string(&name->ip), name->name, my_suffix);
903 + fprintf(hosts, "\t# %s\n", olsr_ip_to_string(&entry->originator));
904 }
905 }
906 }
907 @@ -682,7 +894,70 @@
908
909
910 /**
911 - * write upstream DNS servers to resolv.conf file
912 + * write services to a file in the format:
913 + * service #originator ip
914 + *
915 + * since service has a special format
916 + * each line will look similar to e.g.
917 + * http://me.olsr:80|tcp|my little homepage
918 + */
919 +void
920 +write_services_file()
921 +{
922 + int hash;
923 + struct name_entry *name;
924 + struct db_entry *entry;
925 + FILE* services_file;
926 + time_t currtime;
927 +
928 +
929 + if (!service_table_changed)
930 + return;
931 +
932 + olsr_printf(2, "NAME PLUGIN: writing services file\n");
933 +
934 + services_file = fopen( my_services_file, "w" );
935 + if (services_file == NULL) {
936 + olsr_printf(2, "NAME PLUGIN: cant write services_file file\n");
937 + return;
938 + }
939 +
940 + fprintf(services_file, "### this file is overwritten regularly by olsrd\n");
941 + fprintf(services_file, "### do not edit\n\n");
942 +
943 +
944 + // write own services
945 + for (name = my_services; name != NULL; name = name->next) {
946 + fprintf(services_file, "%s\t# my own service\n", name->name);
947 + }
948 +
949 + // write received services
950 + for (hash = 0; hash < HASHSIZE; hash++)
951 + {
952 + for(entry = service_list[hash]; entry != NULL; entry = entry->next)
953 + {
954 + for (name = entry->names; name != NULL; name = name->next)
955 + {
956 + olsr_printf(6, "%s\t", name->name);
957 + olsr_printf(6, "\t#%s\n", olsr_ip_to_string(&entry->originator));
958 +
959 + fprintf(services_file, "%s\t", name->name );
960 + fprintf(services_file, "\t#%s\n", olsr_ip_to_string(&entry->originator));
961 + }
962 + }
963 + }
964 +
965 + if (time(&currtime)) {
966 + fprintf(services_file, "\n### written by olsrd at %s", ctime(&currtime));
967 + }
968 +
969 + fclose(services_file);
970 + service_table_changed = OLSR_FALSE;
971 +}
972 +
973 +/**
974 + * write the 3 best upstream DNS servers to resolv.conf file
975 + * best means the 3 with the best etx value in routing table
976 */
977 void
978 write_resolv_file()
979 @@ -694,23 +969,17 @@
980 struct rt_entry *route, *tmp, *last;
981 FILE* resolv;
982 int i=0;
983 + time_t currtime;
984
985 - if (my_resolv_file[0] == '\0')
986 - return;
987 -
988 - if (!name_table_changed)
989 + if (!forwarder_table_changed || my_forwarders != NULL || my_resolv_file[0] == '\0')
990 return;
991
992 - olsr_printf(2, "NAME PLUGIN: writing resolv file\n");
993 -
994 for (hash = 0; hash < HASHSIZE; hash++)
995 {
996 - for(entry = list[hash]; entry != NULL; entry = entry->next)
997 + for(entry = forwarder_list[hash]; entry != NULL; entry = entry->next)
998 {
999 for (name = entry->names; name != NULL; name = name->next)
1000 {
1001 - if (name->type != NAME_FORWARDER)
1002 - continue;
1003
1004 /* find the nearest one */
1005 route = olsr_lookup_routing_table(&name->ip);
1006 @@ -762,18 +1031,25 @@
1007 return;
1008
1009 /* write to file */
1010 + olsr_printf(2, "NAME PLUGIN: try to write to resolv file\n");
1011 resolv = fopen( my_resolv_file, "w" );
1012 if (resolv == NULL) {
1013 olsr_printf(2, "NAME PLUGIN: can't write resolv file\n");
1014 return;
1015 }
1016 + fprintf(resolv, "### this file is overwritten regularly by olsrd\n");
1017 + fprintf(resolv, "### do not edit\n\n");
1018 i=0;
1019 for (tmp=best_routes; tmp!=NULL && i<3; tmp=tmp->next) {
1020 olsr_printf(6, "NAME PLUGIN: nameserver %s\n", olsr_ip_to_string(&tmp->rt_dst));
1021 fprintf(resolv, "nameserver %s\n", olsr_ip_to_string(&tmp->rt_dst));
1022 i++;
1023 }
1024 + if (time(&currtime)) {
1025 + fprintf(resolv, "\n### written by olsrd at %s", ctime(&currtime));
1026 + }
1027 fclose(resolv);
1028 + forwarder_table_changed = OLSR_FALSE;
1029 }
1030
1031
1032 @@ -789,6 +1065,7 @@
1033 to_delete = *tmp;
1034 *tmp = (*tmp)->next;
1035 free( to_delete->name );
1036 + to_delete->name = NULL;
1037 free( to_delete );
1038 to_delete = NULL;
1039 }
1040 @@ -831,3 +1108,84 @@
1041 }
1042 return OLSR_FALSE;
1043 }
1044 +
1045 +/** check if name has the right syntax, i.e. it must adhere to a special regex
1046 + * stored in regex_t_name
1047 + * necessary to avaid names like "0.0.0.0 google.de\n etc"
1048 + */
1049 +olsr_bool
1050 +is_name_wellformed(char *name) {
1051 + return regexec(&regex_t_name, name, 1, &regmatch_t_name, 0) == 0 ;
1052 +}
1053 +
1054 +
1055 +/**
1056 + * check if the service is in the right syntax and also that the hostname
1057 + * or ip whithin the service is allowed
1058 + */
1059 +olsr_bool
1060 +allowed_service(char *service_line)
1061 +{
1062 + /* the call of is_service_wellformed generates the submatches stored in regmatch_t_service
1063 + * these are then used by allowed_hostname_or_ip_in_service
1064 + * see regexec(3) for more infos */
1065 + if (!is_service_wellformed(service_line)) {
1066 + return OLSR_FALSE;
1067 + } else if (!allowed_hostname_or_ip_in_service(service_line, &(regmatch_t_service[1]))) {
1068 + return OLSR_FALSE;
1069 + }
1070 +
1071 + return OLSR_TRUE;
1072 +}
1073 +
1074 +olsr_bool
1075 +allowed_hostname_or_ip_in_service(char *service_line, regmatch_t *hostname_or_ip_match)
1076 +{
1077 + char *hostname_or_ip;
1078 + struct in_addr ip;
1079 + union olsr_ip_addr olsr_ip;
1080 + struct name_entry *name;
1081 + if (hostname_or_ip_match->rm_so < 0 || hostname_or_ip_match->rm_eo < 0) {
1082 + return OLSR_FALSE;
1083 + }
1084 +
1085 + hostname_or_ip = strndup(&service_line[hostname_or_ip_match->rm_so], hostname_or_ip_match->rm_eo - hostname_or_ip_match->rm_so);
1086 + //hostname is one of the names, that I announce (i.e. one that i am allowed to announce)
1087 + for (name = my_names; name != NULL; name = name->next) {
1088 + if (strncmp(name->name, hostname_or_ip, name->len - strlen(my_suffix)) == 0 ) {
1089 + olsr_printf(4, "NAME PLUGIN: hostname %s in service %s is OK\n", hostname_or_ip, service_line);
1090 + free(hostname_or_ip);
1091 + hostname_or_ip = NULL;
1092 + return OLSR_TRUE;
1093 + }
1094 + }
1095 +
1096 + //ip in service-line is allowed
1097 + if (inet_aton(hostname_or_ip, &ip)) {
1098 + olsr_ip.v4 = ip.s_addr;
1099 + if (allowed_ip(&olsr_ip)) {
1100 + olsr_printf(2, "NAME PLUGIN: ip %s in service %s is OK\n", olsr_ip_to_string(&olsr_ip), service_line);
1101 + free(hostname_or_ip);
1102 + hostname_or_ip = NULL;
1103 + return OLSR_TRUE;
1104 + }
1105 + }
1106 +
1107 + olsr_printf(1, "NAME PLUGIN: ip or hostname %s in service %s is NOT allowed (does not belong to you)\n", hostname_or_ip, service_line);
1108 + free(hostname_or_ip);
1109 + hostname_or_ip = NULL;
1110 +
1111 + return OLSR_FALSE;
1112 +}
1113 +
1114 +/**
1115 + * check if the service matches the syntax
1116 + * of "protocol://host:port/path|tcp_or_udp|a short description",
1117 + * which is given in the regex regex_t_service
1118 + */
1119 +olsr_bool
1120 +is_service_wellformed(char *service_line)
1121 +{
1122 + return regexec(&regex_t_service, service_line, pmatch_service, regmatch_t_service, 0) == 0;
1123 +}
1124 +
1125 diff -Nur olsrd-0.4.10.orig/lib/nameservice/src/nameservice.h olsrd-0.4.10/lib/nameservice/src/nameservice.h
1126 --- olsrd-0.4.10.orig/lib/nameservice/src/nameservice.h 2005-06-02 17:34:00.000000000 +0200
1127 +++ olsrd-0.4.10/lib/nameservice/src/nameservice.h 2006-12-01 08:26:58.000000000 +0100
1128 @@ -39,6 +39,7 @@
1129 #define _NAMESERVICE_PLUGIN
1130
1131 #include <sys/time.h>
1132 +#include <regex.h>
1133
1134 #include "olsr_types.h"
1135 #include "interfaces.h"
1136 @@ -51,19 +52,27 @@
1137 #define PLUGIN_VERSION "0.2"
1138 #define PLUGIN_AUTHOR "Bruno Randolf"
1139
1140 +// useful to set for the freifunkfirmware to remove all
1141 +// calls to olsr_printf by the empty statement ";"
1142 +//#define olsr_printf(...) ;
1143
1144 #define MESSAGE_TYPE 130
1145 #define PARSER_TYPE MESSAGE_TYPE
1146 #define EMISSION_INTERVAL 120 /* two minutes */
1147 -#define NAME_VALID_TIME 3600 /* one hour */
1148 +#define NAME_VALID_TIME 1800 /* half one hour */
1149
1150 #define NAME_PROTOCOL_VERSION 1
1151
1152 -#define MAX_NAME 255
1153 +#define MAX_NAME 127
1154 #define MAX_FILE 255
1155 -#define MAX_SUFFIX 255
1156 -
1157 +#define MAX_SUFFIX 63
1158
1159 +/**
1160 + * a linked list of name_entry
1161 + * if type is NAME_HOST, name is a hostname and ip its IP addr
1162 + * if type is NAME_FORWARDER, then ip is a dns-server (and name is irrelevant)
1163 + * if type is NAME_SERVICE, then name is a service-line (and the ip is irrelevant)
1164 + */
1165 struct name_entry
1166 {
1167 union olsr_ip_addr ip;
1168 @@ -73,7 +82,17 @@
1169 struct name_entry *next; /* linked list */
1170 };
1171
1172 -/* database entry */
1173 +/* *
1174 + * linked list of db_entries for each originator with
1175 + * originator being its main_addr
1176 + *
1177 + * names points to the name_entry with its hostname, dns-server or
1178 + * service-line entry
1179 + *
1180 + * all the db_entries are hashed in nameservice.c to avoid a too long list
1181 + * for many nodes in a net
1182 + *
1183 + * */
1184 struct db_entry
1185 {
1186 union olsr_ip_addr originator; /* IP address of the node this entry describes */
1187 @@ -98,8 +117,26 @@
1188 int
1189 encap_namemsg(struct namemsg *);
1190
1191 +struct name_entry*
1192 +add_name_to_list(struct name_entry *my_list, char *value, int type, struct in_addr *ip);
1193 +
1194 +struct name_entry*
1195 +remove_nonvalid_names_from_list(struct name_entry *my_list, int type);
1196 +
1197 +void
1198 +free_all_list_entries(struct db_entry **this_db_list) ;
1199 +
1200 void
1201 -decap_namemsg(struct namemsg *, int, struct name_entry**);
1202 +timeout_old_names(struct db_entry **this_list, olsr_bool *this_table_changed);
1203 +
1204 +void
1205 +decap_namemsg(struct name *from_packet, struct name_entry **to, olsr_bool *this_table_changed );
1206 +
1207 +void
1208 +insert_new_name_in_list(union olsr_ip_addr *originator, struct db_entry **this_list, struct name *from_packet, olsr_bool *this_table_changed, double vtime);
1209 +
1210 +olsr_bool
1211 +allowed_hostname_or_ip_in_service(char *service_line, regmatch_t *hostname_or_ip);
1212
1213 void
1214 update_name_entry(union olsr_ip_addr *, struct namemsg *, int, double);
1215 @@ -108,6 +145,9 @@
1216 write_hosts_file(void);
1217
1218 void
1219 +write_services_file(void);
1220 +
1221 +void
1222 write_resolv_file(void);
1223
1224 int
1225 @@ -119,6 +159,18 @@
1226 olsr_bool
1227 allowed_ip(union olsr_ip_addr *addr);
1228
1229 +olsr_bool
1230 +allowed_service(char *service_line);
1231 +
1232 +olsr_bool
1233 +is_name_wellformed(char *service_line);
1234 +
1235 +olsr_bool
1236 +is_service_wellformed(char *service_line);
1237 +
1238 +char*
1239 +create_packet(struct name *to, struct name_entry *from);
1240 +
1241 void
1242 name_constructor(void);
1243
1244 diff -Nur olsrd-0.4.10.orig/lib/nameservice/src/nameservice_msg.h olsrd-0.4.10/lib/nameservice/src/nameservice_msg.h
1245 --- olsrd-0.4.10.orig/lib/nameservice/src/nameservice_msg.h 2005-03-17 22:41:30.000000000 +0100
1246 +++ olsrd-0.4.10/lib/nameservice/src/nameservice_msg.h 2006-12-01 08:26:58.000000000 +0100
1247 @@ -38,29 +38,34 @@
1248 #ifndef _NAMESEVICE_MSG
1249 #define _NAMESEVICE_MSG
1250
1251 -
1252 +/* type of the packet of name_entry */
1253 typedef enum {
1254 NAME_HOST = 0,
1255 NAME_FORWARDER = 1,
1256 - NAME_SERVICE = 2
1257 + NAME_SERVICE = 2,
1258 } NAME_TYPE;
1259
1260 -
1261 +/**
1262 + * the name, forwarder or service entry as found in a packet within a
1263 + * message
1264 + **/
1265 struct name
1266 {
1267 olsr_u16_t type;
1268 olsr_u16_t len; // length of the name
1269 + // the ip of the hostname, or the ip of the dns-server
1270 + // ip is irrelevant for services
1271 union olsr_ip_addr ip;
1272 /*
1273 - * name is written in plain text after this struct and padded to 4 byte
1274 + * name or service is written in plain text after this struct and padded to 4 byte
1275 */
1276 };
1277
1278
1279 struct namemsg
1280 {
1281 - olsr_u16_t version;
1282 - olsr_u16_t nr_names; // number of following name messages
1283 + olsr_u16_t version; // version number of the nameservice plugin
1284 + olsr_u16_t nr_names; // number of following packets including names, forwarders or services
1285 /*
1286 * at least one struct name following
1287 */