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