* added include because of bug report of Patrick McCarty <mccartyp@gmail.com>
[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.22 2007/04/25 22:08:06 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
265         memset(&ipz, 0, sizeof(ipz));
266
267     int ret;
268     //regex string for validating the hostnames
269     char *regex_name = "^[[:alnum:]_.-]+$";
270     //regex string for the service line
271     char *regex_service = olsr_malloc(256*sizeof(char) + strlen(my_suffix), "new *char from name_init for regex_service");
272
273     //compile the regex from the string
274         if ((ret = regcomp(&regex_t_name, regex_name , REG_EXTENDED)) != 0)
275     {
276                 /* #2: call regerror() if regcomp failed 
277          * commented out, because only for debuggin needed
278          *
279         int errmsgsz = regerror(ret, &regex_t_name, NULL, 0);
280         char *errmsg = malloc(errmsgsz);
281         regerror(ret, &regex_t_name, errmsg, errmsgsz);
282         fprintf(stderr, "regcomp: %s", errmsg);
283         free(errmsg);
284         regfree(&regex_t_name);
285          * */
286         olsr_printf(0, "compilation of regex \"%s\" for hostname failed", regex_name);
287         }
288                   
289   // a service line is something like prot://hostname.suffix:port|tcp|my little description about this service
290   //                  for example     http://router.olsr:80|tcp|my homepage
291   //                     prot     ://  (hostname.suffix     OR         ip)
292   //regex_service = "^[[:alnum:]]+://(([[:alnum:]_.-]+.olsr)|([[:digit:]]{1,3}\\.[[:digit:]]{1,3}\\.[[:digit:]]{1,3}\\.[[:digit:]]{1,3}))
293   //                 :    port              /path      |(tcp OR udp) |short description
294   //                 :[[:digit:]]+[[:alnum:]/?._=#-]*\\|(tcp|udp)\\|[^|[:cntrl:]]+$";
295   strcat (strcat (strcat(regex_service, "^[[:alnum:]]+://(([[:alnum:]_.-]+"),
296                   my_suffix),
297           ")|([[:digit:]]{1,3}\\.[[:digit:]]{1,3}\\.[[:digit:]]{1,3}\\.[[:digit:]]{1,3})):[[:digit:]]+[[:alnum:]/?._=#-]*\\|(tcp|udp)\\|[^|[:cntrl:]]+$");
298
299         /* #1: call regcomp() to compile the regex */
300         if ((ret = regcomp(&regex_t_service, regex_service , REG_EXTENDED )) != 0)
301     {
302                 /* #2: call regerror() if regcomp failed 
303          * commented out, because only for debuggin needed
304          *
305                 int errmsgsz = regerror(ret, &regex_t_service, NULL, 0);
306                 char *errmsg = malloc(errmsgsz);
307                 regerror(ret, &regex_t_service, errmsg, errmsgsz);
308                 fprintf(stderr, "regcomp: %s", errmsg);
309                 free(errmsg);
310                 regfree(&regex_t_service);
311          * */
312         olsr_printf(0, "compilation of regex \"%s\" for hostname failed", regex_name);
313         }
314     free(regex_service);
315     regex_service = NULL;
316
317     //fill in main addr for all entries with ip==0
318     //this does not matter for service, because the ip does not matter
319     //for service
320
321         for (name = my_names; name != NULL; name = name->next) {
322         if (memcmp(&name->ip, &ipz, olsr_cnf->ipsize) == 0) {
323             olsr_printf(2, "NAME PLUGIN: insert main addr for name %s \n", name->name);
324                         memcpy(&name->ip, &olsr_cnf->main_addr, olsr_cnf->ipsize);
325         }
326     }
327         for (name = my_forwarders; name != NULL; name = name->next) {
328         if (name->ip.v4 == 0) {
329             olsr_printf(2, "NAME PLUGIN: insert main addr for name %s \n", name->name);
330                         memcpy(&name->ip, &olsr_cnf->main_addr, olsr_cnf->ipsize);
331         }
332     }
333
334     //check if entries I want to announce myself are valid and allowed
335     my_names = remove_nonvalid_names_from_list(my_names, NAME_HOST);
336     my_forwarders = remove_nonvalid_names_from_list(my_forwarders, NAME_FORWARDER);
337     my_services = remove_nonvalid_names_from_list(my_services, NAME_SERVICE);
338
339
340         /* register functions with olsrd */
341         olsr_parser_add_function(&olsr_parser, PARSER_TYPE, 1);
342         olsr_register_timeout_function(&olsr_timeout);
343         olsr_register_scheduler_event(&olsr_event, NULL, my_interval, 0, NULL);
344
345         return 1;
346 }
347
348
349 struct name_entry*
350 remove_nonvalid_names_from_list(struct name_entry *my_list, int type) 
351 {
352     struct name_entry *next = my_list;
353     olsr_bool valid = OLSR_FALSE;
354     if (my_list == NULL) {
355         return NULL;
356     } 
357
358     switch (type) {
359         case NAME_HOST:
360             valid = is_name_wellformed(my_list->name) && allowed_ip(&my_list->ip); 
361             break;
362         case NAME_FORWARDER:
363             valid = allowed_ip(&my_list->ip);
364             break;
365         case NAME_SERVICE:
366             valid = allowed_service(my_list->name);
367             break;
368     }
369     
370     if ( !valid  ) {
371         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));
372         next = my_list->next;
373         free(my_list->name);
374         my_list->name = NULL;
375         free(my_list);
376         my_list = NULL;
377         return remove_nonvalid_names_from_list(next, type);
378     } else {
379         olsr_printf(2, "NAME PLUGIN: validate parameter %s (%s) -> OK\n", my_list->name, olsr_ip_to_string(&my_list->ip));
380         my_list->next = remove_nonvalid_names_from_list(my_list->next, type);
381         return my_list;    
382     }
383 }
384
385
386
387 /**
388  * called at unload: free everything
389  *
390  * XXX: should I delete the hosts/services/resolv.conf files on exit?
391  */
392 void
393 name_destructor(void)
394 {
395         olsr_printf(2, "NAME PLUGIN: exit. cleaning up...\n");
396         
397         free_name_entry_list(&my_names);
398         free_name_entry_list(&my_services);
399         free_name_entry_list(&my_forwarders);
400
401     free_all_list_entries(list);
402     free_all_list_entries(service_list);
403     free_all_list_entries(forwarder_list);
404
405     regfree(&regex_t_name);
406     regfree(&regex_t_service);
407         
408 }
409
410 /* free all list entries */
411 void 
412 free_all_list_entries(struct db_entry **this_db_list) 
413 {
414         int i;
415         
416         for(i = 0; i < HASHSIZE; i++)
417         {
418         struct db_entry **tmp = &this_db_list[i];
419                 while(*tmp != NULL)
420                 {
421             struct db_entry *to_delete = *tmp;
422                         *tmp = (*tmp)->next;
423                         free_name_entry_list(&to_delete->names);
424                         free(to_delete);
425                         to_delete = NULL;
426                 }
427         }
428 }
429
430
431 /**
432  * A timeout function called every time
433  *
434  * XXX:the scheduler is polled (by default 10 times a sec,
435  * which is far too often):
436  *
437  * time out old list entries
438  * and write changes to file
439  */
440 void
441 olsr_timeout(void)
442 {
443     timeout_old_names(list, &name_table_changed);
444     timeout_old_names(forwarder_list, &forwarder_table_changed);
445     timeout_old_names(service_list, &service_table_changed);
446      
447         write_resolv_file();
448         write_hosts_file();
449         write_services_file();
450 }
451
452 void
453 timeout_old_names(struct db_entry **this_list, olsr_bool *this_table_changed)
454 {
455         struct db_entry **tmp;
456         struct db_entry *to_delete;
457         int index;
458
459         for(index=0;index<HASHSIZE;index++)
460         {
461                 for (tmp = &this_list[index]; *tmp != NULL; )
462                 {
463                         /* check if the entry for this ip is timed out */
464                         if (olsr_timed_out(&(*tmp)->timer))
465                         {
466                                 to_delete = *tmp;
467                 /* update the pointer in the linked list */
468                                 *tmp = (*tmp)->next;
469                                 
470                                 olsr_printf(2, "NAME PLUGIN: %s timed out... deleting\n", 
471                                         olsr_ip_to_string(&to_delete->originator));
472         
473                                 /* Delete */
474                                 free_name_entry_list(&to_delete->names);
475                                 free(to_delete);
476                                 *this_table_changed = OLSR_TRUE;
477                         } else {
478                                 tmp = &(*tmp)->next;
479                         }
480                 }
481         }
482 }
483
484
485 /**
486  * Scheduled event: generate and send NAME packet
487  */
488 void
489 olsr_event(void *foo __attribute__((unused)))
490 {
491         union olsr_message *message = (union olsr_message*)buffer;
492         struct interface *ifn;
493         int namesize;
494   
495         /* looping trough interfaces */
496         for (ifn = ifnet; ifn ; ifn = ifn->int_next) 
497         {
498                 olsr_printf(3, "NAME PLUGIN: Generating packet - [%s]\n", ifn->int_name);
499
500                 /* fill message */
501                 if(olsr_cnf->ip_version == AF_INET)
502                 {
503                         /* IPv4 */
504                         message->v4.olsr_msgtype = MESSAGE_TYPE;
505                         message->v4.olsr_vtime = double_to_me(my_timeout);
506                         memcpy(&message->v4.originator, &olsr_cnf->main_addr, olsr_cnf->ipsize);
507                         message->v4.ttl = MAX_TTL;
508                         message->v4.hopcnt = 0;
509                         message->v4.seqno = htons(get_msg_seqno());
510                         
511                         namesize = encap_namemsg((struct namemsg*)&message->v4.message);
512                         namesize = namesize + sizeof(struct olsrmsg);
513                         
514                         message->v4.olsr_msgsize = htons(namesize);
515                 }
516                 else
517                 {
518                         /* IPv6 */
519                         message->v6.olsr_msgtype = MESSAGE_TYPE;
520                         message->v6.olsr_vtime = double_to_me(my_timeout);
521                         memcpy(&message->v6.originator, &olsr_cnf->main_addr, olsr_cnf->ipsize);
522                         message->v6.ttl = MAX_TTL;
523                         message->v6.hopcnt = 0;
524                         message->v6.seqno = htons(get_msg_seqno());
525                         
526                         namesize = encap_namemsg((struct namemsg*)&message->v6.message);
527                         namesize = namesize + sizeof(struct olsrmsg6);
528                         
529                         message->v6.olsr_msgsize = htons(namesize);
530                 }
531                 
532                 if(net_outbuffer_push(ifn, message, namesize) != namesize ) {
533                         /* send data and try again */
534                         net_output(ifn);
535                         if(net_outbuffer_push(ifn, message, namesize) != namesize ) {
536                                 olsr_printf(1, "NAME PLUGIN: could not send on interface: %s\n", ifn->int_name);
537                         }
538                 }
539         }
540 }
541
542
543 /**
544  * Parse name olsr message of NAME type
545  */
546 void
547 olsr_parser(union olsr_message *m, struct interface *in_if, union olsr_ip_addr *ipaddr)
548 {
549         struct namemsg *namemessage;
550         union olsr_ip_addr originator;
551         double vtime;
552         int size;
553         olsr_u16_t seqno;
554
555         /* Fetch the originator of the messsage */
556         if(olsr_cnf->ip_version == AF_INET) {
557                 memcpy(&originator, &m->v4.originator, olsr_cnf->ipsize);
558                 seqno = ntohs(m->v4.seqno);
559         } else {
560                 memcpy(&originator, &m->v6.originator, olsr_cnf->ipsize);
561                 seqno = ntohs(m->v6.seqno);
562         }
563                 
564         /* Fetch the message based on IP version */
565         if(olsr_cnf->ip_version == AF_INET) {
566                 vtime = ME_TO_DOUBLE(m->v4.olsr_vtime);
567                 size = ntohs(m->v4.olsr_msgsize);
568                 namemessage = (struct namemsg*)&m->v4.message;
569         }
570         else {
571                 vtime = ME_TO_DOUBLE(m->v6.olsr_vtime);
572                 size = ntohs(m->v6.olsr_msgsize);
573                 namemessage = (struct namemsg*)&m->v6.message;
574         }
575
576         /* Check if message originated from this node. 
577         If so - back off */
578         if(memcmp(&originator, &olsr_cnf->main_addr, olsr_cnf->ipsize) == 0)
579                 return;
580
581         /* Check that the neighbor this message was received from is symmetric. 
582         If not - back off*/
583         if(check_neighbor_link(ipaddr) != SYM_LINK) {
584                 olsr_printf(3, "NAME PLUGIN: Received msg from NON SYM neighbor %s\n", olsr_ip_to_string(ipaddr));
585                 return;
586         }
587
588         /* Check if this message has been processed before
589         * Remeber that this also registeres the message as
590         * processed if nessecary
591         */
592         if(olsr_check_dup_table_proc(&originator, seqno)) {
593                 /* If not so - process */
594         update_name_entry(&originator, namemessage, size, vtime);
595         }
596
597         /* Forward the message if nessecary
598         * default_fwd does all the work for us! */
599         olsr_forward_message(m, &originator, seqno, in_if, ipaddr);
600 }
601
602
603 /**
604  * Encapsulate a name message into a packet. 
605  *
606  * It assumed that there is enough space in the buffer to do this!
607  *
608  * Returns: the length of the message that was appended
609  */
610 int
611 encap_namemsg(struct namemsg* msg)
612 {
613         struct name_entry *my_name;
614         struct name_entry *my_service;
615
616     // add the hostname, service and forwarder entries after the namemsg header
617         char* pos = (char*)msg + sizeof(struct namemsg);
618         short i=0;
619
620         // names
621         for (my_name = my_names; my_name!=NULL; my_name = my_name->next)
622         {
623         pos = create_packet( (struct name*) pos, my_name);
624                 i++;
625         }
626         // forwarders
627         for (my_name = my_forwarders; my_name!=NULL; my_name = my_name->next)
628         {
629         pos = create_packet( (struct name*) pos, my_name);
630                 i++;
631         }
632         // services
633         for (my_service = my_services; my_service!=NULL; my_service = my_service->next)
634         {
635         pos = create_packet( (struct name*) pos, my_service);
636                 i++;
637         }
638     
639     // write the namemsg header with the number of announced entries and the protocol version
640         msg->nr_names = htons(i);
641         msg->version = htons(NAME_PROTOCOL_VERSION);
642
643         return pos - (char*)msg; //length
644 }
645
646
647 /**
648  * convert each of my to be announced name_entries into network
649  * compatible format
650  *
651  * return the length of the name packet
652  */
653 char*  
654 create_packet(struct name* to, struct name_entry *from)
655 {
656     char *pos = (char*) to;
657     int k;
658     olsr_printf(3, "NAME PLUGIN: Announcing name %s (%s) %d\n", 
659         from->name, olsr_ip_to_string(&from->ip), from->len);
660     to->type = htons(from->type);
661     to->len = htons(from->len);
662     memcpy(&to->ip, &from->ip, olsr_cnf->ipsize);
663     pos += sizeof(struct name);
664     strncpy(pos, from->name, from->len);
665     pos += from->len;
666     for (k = from->len; (k & 3) != 0; k++)
667         *pos++ = '\0';
668     return pos;
669 }
670
671 /**
672  * decapsulate a received name, service or forwarder and update the corresponding hash table if necessary
673  */
674 void
675 decap_namemsg(struct name *from_packet, struct name_entry **to, olsr_bool *this_table_changed )
676 {
677         struct name_entry *tmp;
678         struct name_entry *already_saved_name_entries;
679     char *name = (char*)from_packet + sizeof(struct name);
680         olsr_printf(4, "\nNAME PLUGIN: decapsulating received name, service or forwarder \n");
681     int type_of_from_packet = ntohs(from_packet->type);
682     unsigned int len_of_name = ntohs(from_packet->len);
683     
684     // don't insert the received entry again, if it has already been inserted in the hash table. 
685     // Instead only the validity time is set in insert_new_name_in_list function, which calls this one
686     for (already_saved_name_entries = (*to); already_saved_name_entries != NULL ; already_saved_name_entries = already_saved_name_entries->next)
687     {
688         if ( (type_of_from_packet==NAME_HOST || type_of_from_packet==NAME_SERVICE) && strncmp(already_saved_name_entries->name, name, len_of_name) == 0 ) {
689             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));
690             return;
691         } else  if (type_of_from_packet==NAME_FORWARDER && COMP_IP(&already_saved_name_entries->ip, &from_packet->ip) ) {
692             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));
693             return;
694         }
695     }
696
697     //XXX: should I check the from_packet->ip here? If so, why not also check the ip fro HOST and SERVICE?
698     if( (type_of_from_packet==NAME_HOST && !is_name_wellformed(name))  ||  (type_of_from_packet==NAME_SERVICE && !is_service_wellformed(name)) ) {
699         olsr_printf(4, "\nNAME PLUGIN: invalid name [%s] received, skipping.\n", name );
700         return;
701     }
702
703     //ignore all packets with a too long name
704     //or a spoofed len of its included name string
705     if (len_of_name > MAX_NAME || strlen(name) != len_of_name) {
706         olsr_printf(4, "\nNAME PLUGIN: from_packet->len %d > MAX_NAME %d or from_packet->len %d !0 strlen(name [%s] in packet)\n", 
707                 len_of_name, MAX_NAME, len_of_name, name );
708         return;
709     }
710
711     //if not yet known entry 
712     tmp = olsr_malloc(sizeof(struct name_entry), "new name_entry");             
713     tmp->type = ntohs(from_packet->type);
714     tmp->len = len_of_name > MAX_NAME ? MAX_NAME : ntohs(from_packet->len);
715     tmp->name = olsr_malloc(tmp->len+1, "new name_entry name");
716     memcpy(&tmp->ip, &from_packet->ip, olsr_cnf->ipsize);
717     strncpy(tmp->name, name, tmp->len);
718     tmp->name[tmp->len] = '\0';
719
720     olsr_printf(3, "\nNAME PLUGIN: create new name/service/forwarder entry %s (%s) [len=%d] [type=%d] in linked list\n", 
721         tmp->name, olsr_ip_to_string(&tmp->ip), tmp->len, tmp->type);
722
723     *this_table_changed = OLSR_TRUE;
724
725     // queue to front
726     tmp->next = *to;
727     *to = tmp;
728 }
729
730
731 /**
732  * unpack the received message and delegate to the decapsilation function for each
733  * name/service/forwarder entry in the message
734  */
735 void
736 update_name_entry(union olsr_ip_addr *originator, struct namemsg *msg, int msg_size, double vtime)
737 {
738         char *pos, *end_pos;
739         struct name *from_packet; 
740         int i;
741
742         olsr_printf(3, "NAME PLUGIN: Received Message from %s\n", 
743                 olsr_ip_to_string(originator));
744         
745         if (ntohs(msg->version) != NAME_PROTOCOL_VERSION) {
746                 olsr_printf(3, "NAME PLUGIN: ignoring wrong version %d\n", msg->version);
747                 return;
748         }
749         
750         /* now add the names from the message */
751         pos = (char*)msg + sizeof(struct namemsg);
752         end_pos = pos + msg_size - sizeof(struct name*); // at least one struct name has to be left     
753
754         for (i=ntohs(msg->nr_names); i > 0 && pos<end_pos; i--) 
755         {
756                 from_packet = (struct name*)pos;
757                 
758         switch (ntohs(from_packet->type)) {
759             case NAME_HOST: 
760                 insert_new_name_in_list(originator, list, from_packet, &name_table_changed, vtime); 
761                 break;
762             case NAME_FORWARDER:
763                 insert_new_name_in_list(originator, forwarder_list, from_packet, &forwarder_table_changed, vtime); 
764                 break;
765             case NAME_SERVICE:
766                 insert_new_name_in_list(originator, service_list, from_packet, &service_table_changed, vtime); 
767                 break;
768             default:
769                 olsr_printf(3, "NAME PLUGIN: Received Message of unknown type [%d] from (%s)\n", from_packet->type, olsr_ip_to_string(originator));
770                 break;
771         }
772
773                 pos += sizeof(struct name);
774                 pos += 1 + (( ntohs(from_packet->len) - 1) | 3);
775     }
776         if (i!=0)
777                 olsr_printf(4, "NAME PLUGIN: Lost %d entries in received packet due to length inconsistency (%s)\n", i, olsr_ip_to_string(originator));
778 }
779
780
781 /**
782  * insert all the new names,services and forwarders from a received packet into the
783  * corresponding entry for this ip in the corresponding hash table
784  */
785 void
786 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)
787 {
788         int hash;
789         struct db_entry *entry;
790
791     olsr_bool entry_found = OLSR_FALSE;
792
793         hash = olsr_hashing(originator);
794
795             /* find the entry for originator, if there is already one */
796             for (entry = this_list[hash]; entry != NULL; entry = entry->next)
797             {
798                 if (memcmp(originator, &entry->originator, olsr_cnf->ipsize) == 0) {
799                     // found
800                     olsr_printf(4, "NAME PLUGIN: found entry for (%s) in its hash table\n", olsr_ip_to_string(originator));
801                 
802                     //delegate to function for parsing the packet and linking it to entry->names
803                     decap_namemsg(from_packet, &entry->names, this_table_changed);
804                     
805                     olsr_get_timestamp(vtime * 1000, &entry->timer);
806                    
807                     entry_found = OLSR_TRUE;
808                 }
809             }
810             if (! entry_found)
811             {
812                 olsr_printf(3, "NAME PLUGIN: create new db entry for ip (%s) in hash table\n", olsr_ip_to_string(originator));
813                     
814                 /* insert a new entry */
815                 entry = olsr_malloc(sizeof(struct db_entry), "new db_entry");
816                 
817                 memcpy(&entry->originator, originator, olsr_cnf->ipsize);
818                 olsr_get_timestamp(vtime * 1000, &entry->timer);
819                 entry->names = NULL;
820
821                 // queue to front
822                 entry->next = this_list[hash];
823                 this_list[hash] = entry;
824                 
825                 //delegate to function for parsing the packet and linking it to entry->names
826                 decap_namemsg(from_packet, &entry->names, this_table_changed);
827             }
828 }
829
830 /**
831  * write names to a file in /etc/hosts compatible format
832  */
833 void
834 write_hosts_file(void)
835 {
836         int hash;
837         struct name_entry *name;
838         struct db_entry *entry;
839         FILE* hosts;
840         FILE* add_hosts;
841         int c=0;
842         time_t currtime;
843   
844         if (!name_table_changed)
845                 return;
846
847         olsr_printf(2, "NAME PLUGIN: writing hosts file\n");
848                       
849         hosts = fopen( my_hosts_file, "w" );
850         if (hosts == NULL) {
851                 olsr_printf(2, "NAME PLUGIN: cant write hosts file\n");
852                 return;
853         }
854         
855         fprintf(hosts, "### this /etc/hosts file is overwritten regularly by olsrd\n");
856         fprintf(hosts, "### do not edit\n\n");
857
858         fprintf(hosts, "127.0.0.1\tlocalhost\n");
859         fprintf(hosts, "::1\t\tlocalhost\n\n");
860         
861         // copy content from additional hosts filename
862         if (my_add_hosts[0] != '\0') {
863                 add_hosts = fopen( my_add_hosts, "r" );
864                 if (add_hosts == NULL) {
865                         olsr_printf(2, "NAME PLUGIN: cant open additional hosts file\n");
866                 }
867                 else {
868                         fprintf(hosts, "### contents from '%s' ###\n\n", my_add_hosts);
869                         while ((c = getc(add_hosts)) != EOF)
870                                 putc(c, hosts);
871                 }
872                 fclose(add_hosts);              
873                 fprintf(hosts, "\n### olsr names ###\n\n");
874         }
875         
876         // write own names
877         for (name = my_names; name != NULL; name = name->next) {
878         fprintf(hosts, "%s\t%s%s\t# myself\n", olsr_ip_to_string(&name->ip), name->name, my_suffix );
879         }
880         
881         // write received names
882         for (hash = 0; hash < HASHSIZE; hash++) 
883         {
884                 for(entry = list[hash]; entry != NULL; entry = entry->next)
885                 {
886                         for (name = entry->names; name != NULL; name = name->next) 
887                         {
888                 olsr_printf(6, "%s\t%s%s", olsr_ip_to_string(&name->ip), name->name, my_suffix);
889                 olsr_printf(6, "\t#%s\n", olsr_ip_to_string(&entry->originator));
890                 
891                 fprintf(hosts, "%s\t%s%s", olsr_ip_to_string(&name->ip), name->name, my_suffix);
892                 fprintf(hosts, "\t# %s\n", olsr_ip_to_string(&entry->originator));
893                         }
894                 }
895         }
896
897         if (time(&currtime)) {
898                 fprintf(hosts, "\n### written by olsrd at %s", ctime(&currtime));
899         }
900           
901         fclose(hosts);
902         name_table_changed = OLSR_FALSE;
903 }
904
905
906 /**
907  * write services to a file in the format:
908  * service  #originator ip
909  *
910  * since service has a special format
911  * each line will look similar to e.g.
912  * http://me.olsr:80|tcp|my little homepage
913  */
914 void
915 write_services_file(void)
916 {
917         int hash;
918         struct name_entry *name;
919         struct db_entry *entry;
920         FILE* services_file;
921         time_t currtime;
922   
923   
924         if (!service_table_changed)
925                 return;
926
927         olsr_printf(2, "NAME PLUGIN: writing services file\n");
928                       
929         services_file = fopen( my_services_file, "w" );
930         if (services_file == NULL) {
931                 olsr_printf(2, "NAME PLUGIN: cant write services_file file\n");
932                 return;
933         }
934         
935         fprintf(services_file, "### this file is overwritten regularly by olsrd\n");
936         fprintf(services_file, "### do not edit\n\n");
937
938         
939         // write own services
940         for (name = my_services; name != NULL; name = name->next) {
941         fprintf(services_file, "%s\t# my own service\n", name->name);
942         }
943         
944         // write received services
945         for (hash = 0; hash < HASHSIZE; hash++) 
946         {
947                 for(entry = service_list[hash]; entry != NULL; entry = entry->next)
948                 {
949                         for (name = entry->names; name != NULL; name = name->next) 
950                         {
951                 olsr_printf(6, "%s\t",  name->name);
952                 olsr_printf(6, "\t#%s\n", olsr_ip_to_string(&entry->originator));
953                 
954                 fprintf(services_file, "%s\t", name->name );
955                 fprintf(services_file, "\t#%s\n", olsr_ip_to_string(&entry->originator));
956                         }
957                 }
958         }
959
960         if (time(&currtime)) {
961                 fprintf(services_file, "\n### written by olsrd at %s", ctime(&currtime));
962         }
963           
964         fclose(services_file);
965         service_table_changed = OLSR_FALSE;
966 }
967
968 /**
969  * write the 3 best upstream DNS servers to resolv.conf file
970  * best means the 3 with the best etx value in routing table
971  */
972 void
973 write_resolv_file(void)
974 {
975         int hash;
976         struct name_entry *name, *tmp_dns, *last_dns, *dnslist = NULL;
977         struct db_entry *entry;
978         struct rt_entry *best_routes = NULL;
979         struct rt_entry *route, *tmp = NULL, *last;
980         FILE* resolv;
981         int i=0;
982         time_t currtime;
983         
984         if (!forwarder_table_changed || my_forwarders != NULL || my_resolv_file[0] == '\0')
985                 return;
986
987         for (hash = 0; hash < HASHSIZE; hash++) 
988         {
989                 for(entry = forwarder_list[hash]; entry != NULL; entry = entry->next)
990                 {
991                         for (name = entry->names; name != NULL; name = name->next) 
992                         {
993                                 
994                                 /* find the nearest one */
995                                 route = host_lookup_routing_table(&name->ip);
996                                 if (route==NULL) // it's possible that route is not present yet
997                                         continue;
998                                 
999                                 if (best_routes == NULL || route->rt_etx < best_routes->rt_etx) {
1000                                         olsr_printf(6, "NAME PLUGIN: best nameserver %s\n",
1001                                                 olsr_ip_to_string(&name->ip));
1002                                         if (best_routes!=NULL)
1003                                                 olsr_printf(6, "NAME PLUGIN: better than %f (%s)\n",
1004                                                         best_routes->rt_etx,
1005                                                         olsr_ip_to_string(&best_routes->rt_dst));
1006                                         
1007                                         tmp = olsr_malloc(sizeof(struct rt_entry), "new rt_entry");
1008                                         memcpy(&tmp->rt_dst, &route->rt_dst, olsr_cnf->ipsize);
1009                                         tmp->rt_etx = route->rt_etx;
1010                                         tmp->next = best_routes;
1011                                         best_routes = tmp;
1012                                         tmp_dns = olsr_malloc(sizeof(struct name_entry), "write_resolv name_entry");
1013                                         COPY_IP(&tmp_dns->ip, &name->ip);
1014                                         tmp_dns->type = name->type;
1015                                         tmp_dns->len = 0;
1016                                         tmp_dns->name = NULL;
1017                                         tmp_dns->next = dnslist;
1018                                         dnslist = tmp_dns;
1019                                 } else {
1020                                         // queue in etx order
1021                                         last = best_routes;
1022                                         last_dns = dnslist;
1023                                         while ( last->next!=NULL && i<3 ) {
1024                                                 if (last->next->rt_etx > route->rt_etx)
1025                                                         break;
1026                                                 last = last->next;
1027                                                 last_dns = last_dns->next;
1028                                                 i++;
1029                                         }
1030                                         if (i<3) {
1031                                                 olsr_printf(6, "NAME PLUGIN: queue %f (%s)",
1032                                                         route->rt_etx,
1033                                                         olsr_ip_to_string(&name->ip));
1034                                                 olsr_printf(6, " after %f (%s)\n", 
1035                                                         last->rt_etx, olsr_ip_to_string(&last_dns->ip));
1036                                                 
1037                                                 tmp = olsr_malloc(sizeof(struct rt_entry), "new rt_entry");
1038                                                 memcpy(&tmp->rt_dst, &route->rt_dst, olsr_cnf->ipsize);
1039                                                 tmp->rt_etx = route->rt_etx;
1040                                                 tmp->next = last->next;
1041                                                 last->next = tmp;
1042
1043                                                 tmp_dns = olsr_malloc(sizeof(struct name_entry), "write_resolv name_entry");
1044                                                 COPY_IP(&tmp_dns->ip, &name->ip);
1045                                                 tmp_dns->type = name->type;
1046                                                 tmp_dns->len = 0;
1047                                                 tmp_dns->name = NULL;
1048                                                 tmp_dns->next = last_dns->next;
1049                                                 last_dns->next = tmp_dns;
1050                                         } else {
1051                                                 olsr_printf(6, "NAME PLUGIN: don't need more than 3 nameservers\n");
1052                                         }
1053                                 }
1054                         }
1055                 }
1056         }
1057         if (best_routes==NULL)
1058                 return;
1059                  
1060         /* write to file */
1061     olsr_printf(2, "NAME PLUGIN: try to write to resolv file\n");
1062         resolv = fopen( my_resolv_file, "w" );
1063         if (resolv == NULL) {
1064                 olsr_printf(2, "NAME PLUGIN: can't write resolv file\n");
1065                 return;
1066         }
1067         fprintf(resolv, "### this file is overwritten regularly by olsrd\n");
1068         fprintf(resolv, "### do not edit\n\n");
1069         i=0;
1070         for (tmp_dns=dnslist; tmp_dns!=NULL && i<3; tmp_dns=tmp_dns->next) {
1071                 olsr_printf(6, "NAME PLUGIN: nameserver %s\n", olsr_ip_to_string(&tmp_dns->ip));
1072                 fprintf(resolv, "nameserver %s\n", olsr_ip_to_string(&tmp_dns->ip));
1073                 i++;
1074         }
1075         free_name_entry_list(&dnslist);
1076         if(tmp != NULL) {
1077             free_routing_table_list(&tmp);
1078         }
1079         if (time(&currtime)) {
1080                 fprintf(resolv, "\n### written by olsrd at %s", ctime(&currtime));
1081         }
1082         fclose(resolv);
1083         forwarder_table_changed = OLSR_FALSE;
1084 }
1085
1086
1087 /**
1088  * completely free a list of name_entries
1089  */
1090 void 
1091 free_name_entry_list(struct name_entry **list) 
1092 {
1093         struct name_entry **tmp = list;
1094         struct name_entry *to_delete;
1095         while (*tmp != NULL) {
1096                 to_delete = *tmp;
1097                 *tmp = (*tmp)->next;
1098                 free( to_delete->name );
1099                 to_delete->name = NULL;
1100                 free( to_delete );
1101                 to_delete = NULL;
1102         }
1103 }
1104
1105
1106 /**
1107  * completely free a list of rt_entries
1108  */
1109 static void 
1110 free_routing_table_list(struct rt_entry **list) 
1111 {
1112         struct rt_entry **tmp = list;
1113         struct rt_entry *to_delete;
1114         while (*tmp != NULL) {
1115                 to_delete = *tmp;
1116                 *tmp = (*tmp)->next;
1117                 free( to_delete );
1118                 to_delete = NULL;
1119         }
1120 }
1121
1122
1123 /**
1124  * we only allow names for IP addresses which we are
1125  * responsible for: 
1126  * so the IP must either be from one of the interfaces
1127  * or inside a HNA which we have configured 
1128  */
1129 olsr_bool
1130 allowed_ip(union olsr_ip_addr *addr)
1131 {
1132         struct hna4_entry *hna4;
1133         struct hna6_entry *hna6;
1134         struct interface *iface;
1135         union olsr_ip_addr tmp_ip, tmp_msk;
1136         
1137         olsr_printf(6, "checking %s\n", olsr_ip_to_string(addr));
1138         
1139         for(iface = ifnet; iface; iface = iface->int_next)
1140         {
1141                 olsr_printf(6, "interface %s\n", olsr_ip_to_string(&iface->ip_addr));
1142                 if (COMP_IP(&iface->ip_addr, addr)) {
1143                         olsr_printf(6, "MATCHED\n");
1144                         return OLSR_TRUE;
1145                 }
1146         }
1147         
1148         if (olsr_cnf->ip_version == AF_INET) {
1149                 for (hna4 = olsr_cnf->hna4_entries; hna4; hna4 = hna4->next)
1150                 {
1151                         olsr_printf(6, "HNA %s/%s\n", 
1152                                 olsr_ip_to_string(&hna4->net),
1153                                 olsr_ip_to_string(&hna4->netmask));
1154         
1155                         if ( hna4->netmask.v4 != 0 &&
1156                             (addr->v4 & hna4->netmask.v4) == hna4->net.v4 ) {
1157                                 olsr_printf(6, "MATCHED\n");
1158                                 return OLSR_TRUE;
1159                         }
1160                 }
1161         } else {
1162                 int i;
1163
1164                 for (hna6 = olsr_cnf->hna6_entries; hna6; hna6 = hna6->next)
1165                 {
1166                         olsr_printf(6, "HNA %s/%d\n", 
1167                                 olsr_ip_to_string(&hna6->net),
1168                                 hna6->prefix_len);
1169                         if ( hna6->prefix_len == 0 )
1170                                 continue;
1171                         olsr_prefix_to_netmask(&tmp_msk, hna6->prefix_len);
1172                         for (i = 0; i < 16; i++) {
1173                                 tmp_ip.v6.s6_addr[i] = addr->v6.s6_addr[i] &
1174                                         tmp_msk.v6.s6_addr[i];
1175                         }
1176                         if (COMP_IP(&tmp_ip, &hna6->net)) {
1177                                 olsr_printf(6, "MATCHED\n");
1178                                 return OLSR_TRUE;
1179                         }
1180                 }
1181         }
1182         return OLSR_FALSE;
1183 }
1184
1185 static struct rt_entry *
1186 host_lookup_routing_table(union olsr_ip_addr *dst)
1187 {
1188         olsr_u32_t index;
1189         union olsr_ip_addr tmp_ip, tmp_msk;
1190         struct rt_entry *walker;
1191   
1192         walker = olsr_lookup_routing_table(dst);
1193         if (walker != NULL)
1194                 return walker;
1195
1196         for (index = 0; index < HASHSIZE; index++) {
1197                 for (walker = hna_routes[index].next;
1198                     walker != &hna_routes[index]; walker = walker->next) {
1199                         if (COMP_IP(&walker->rt_dst, dst))
1200                                 return walker;
1201                         if (olsr_cnf->ip_version == AF_INET) {
1202                                 if ( walker->rt_mask.v4 != 0 &&
1203                                     (dst->v4 & walker->rt_mask.v4) ==
1204                                     walker->rt_dst.v4 ) {
1205                                         olsr_printf(6, "MATCHED\n");
1206                                         return walker;
1207                                 }
1208                         } else {
1209                                 int i;
1210
1211                                 if ( walker->rt_mask.v6 == 0 )
1212                                         continue;
1213                                 olsr_prefix_to_netmask(&tmp_msk,
1214                                     walker->rt_mask.v6);
1215                                 for (i = 0; i < 16; i++) {
1216                                         tmp_ip.v6.s6_addr[i] =
1217                                             dst->v6.s6_addr[i] &
1218                                             tmp_msk.v6.s6_addr[i];
1219                                 }
1220                                 if (COMP_IP(&tmp_ip, &walker->rt_dst)) {
1221                                         olsr_printf(6, "MATCHED\n");
1222                                         return walker;
1223                                 }
1224                         }
1225                 }
1226         }
1227         return NULL;
1228 }
1229
1230 /** check if name has the right syntax, i.e. it must adhere to a special regex 
1231  * stored in regex_t_name
1232  * necessary to avaid names like "0.0.0.0 google.de\n etc"
1233  */
1234 olsr_bool
1235 is_name_wellformed(char *name) {
1236     return regexec(&regex_t_name, name, 1, &regmatch_t_name, 0) == 0 ;
1237 }
1238
1239
1240 /**
1241  * check if the service is in the right syntax and also that the hostname 
1242  * or ip whithin the service is allowed
1243  */
1244 olsr_bool
1245 allowed_service(char *service_line)
1246 {
1247     /* the call of is_service_wellformed generates the submatches stored in regmatch_t_service
1248      * these are then used by allowed_hostname_or_ip_in_service
1249      * see regexec(3) for more infos */
1250     if (!is_service_wellformed(service_line)) {
1251         return OLSR_FALSE;
1252     } else if (!allowed_hostname_or_ip_in_service(service_line, &(regmatch_t_service[1]))) {
1253         return OLSR_FALSE;
1254     } 
1255
1256    return OLSR_TRUE;
1257 }
1258
1259 olsr_bool
1260 allowed_hostname_or_ip_in_service(char *service_line, regmatch_t *hostname_or_ip_match) 
1261 {
1262     char *hostname_or_ip;
1263     union olsr_ip_addr olsr_ip;
1264     struct name_entry *name;
1265     if (hostname_or_ip_match->rm_so < 0 || hostname_or_ip_match->rm_eo < 0) {
1266           return OLSR_FALSE;
1267     } 
1268
1269     hostname_or_ip = strndup(&service_line[hostname_or_ip_match->rm_so], hostname_or_ip_match->rm_eo - hostname_or_ip_match->rm_so);
1270     //hostname is one of the names, that I announce (i.e. one that i am allowed to announce)
1271     for (name = my_names; name != NULL; name = name->next) {
1272         if (strncmp(name->name, hostname_or_ip, name->len - strlen(my_suffix)) == 0 ) {
1273             olsr_printf(4, "NAME PLUGIN: hostname %s in service %s is OK\n", hostname_or_ip, service_line);
1274             free(hostname_or_ip);
1275             hostname_or_ip = NULL;
1276             return OLSR_TRUE;
1277         }
1278     }
1279     
1280     //ip in service-line is allowed 
1281     if (inet_pton(olsr_cnf->ip_version, hostname_or_ip, &olsr_ip) > 0) {
1282         if (allowed_ip(&olsr_ip)) {
1283             olsr_printf(2, "NAME PLUGIN: ip %s in service %s is OK\n", olsr_ip_to_string(&olsr_ip), service_line);
1284             free(hostname_or_ip);
1285             hostname_or_ip = NULL;
1286             return OLSR_TRUE;
1287         }
1288     }
1289
1290     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);
1291     free(hostname_or_ip);
1292     hostname_or_ip = NULL;
1293
1294     return OLSR_FALSE;
1295 }
1296
1297 /**
1298  * check if the service matches the syntax 
1299  * of "protocol://host:port/path|tcp_or_udp|a short description", 
1300  * which is given in the regex regex_t_service
1301  */
1302 olsr_bool
1303 is_service_wellformed(char *service_line)
1304 {
1305     return regexec(&regex_t_service, service_line, pmatch_service, regmatch_t_service, 0) == 0;
1306 }
1307
1308 /*
1309  * Local Variables:
1310  * mode: c
1311  * c-indent-tabs-mode: t
1312  * c-basic-offset: 4
1313  * tab-width: 4
1314  * End:
1315  */