📄 nameservice.c
字号:
} } } /* if there is no best route we are done */ if (nameserver_routes[NAMESERVER_COUNT]==NULL) return; /* write to file */ OLSR_PRINTF(2, "NAME PLUGIN: try to write to resolv file\n"); resolv = fopen( my_resolv_file, "w" ); if (resolv == NULL) { OLSR_PRINTF(2, "NAME PLUGIN: can't write resolv file\n"); return; } fprintf(resolv, "### this file is overwritten regularly by olsrd\n"); fprintf(resolv, "### do not edit\n\n"); for (i = NAMESERVER_COUNT; i >= 0; i--) { route = nameserver_routes[i]; OLSR_PRINTF(2, "NAME PLUGIN: nameserver_routes #%d %p\n", i, route); if (!route) { continue; } OLSR_PRINTF(2, "NAME PLUGIN: nameserver %s\n", olsr_ip_to_string(&route->rt_dst.prefix)); fprintf(resolv, "nameserver %s\n", olsr_ip_to_string(&route->rt_dst.prefix)); } if (time(&currtime)) { fprintf(resolv, "\n### written by olsrd at %s", ctime(&currtime)); } fclose(resolv); forwarder_table_changed = OLSR_FALSE;}/** * completely free a list of name_entries */void free_name_entry_list(struct name_entry **list) { struct name_entry **tmp = list; struct name_entry *to_delete; while (*tmp != NULL) { to_delete = *tmp; *tmp = (*tmp)->next; free( to_delete->name ); to_delete->name = NULL; free( to_delete ); to_delete = NULL; }}/** * we only allow names for IP addresses which we are * responsible for: * so the IP must either be from one of the interfaces * or inside a HNA which we have configured */olsr_boolallowed_ip(union olsr_ip_addr *addr){ struct hna4_entry *hna4; struct hna6_entry *hna6; struct interface *iface; union olsr_ip_addr tmp_ip, tmp_msk; OLSR_PRINTF(6, "checking %s\n", olsr_ip_to_string(addr)); for(iface = ifnet; iface; iface = iface->int_next) { OLSR_PRINTF(6, "interface %s\n", olsr_ip_to_string(&iface->ip_addr)); if (COMP_IP(&iface->ip_addr, addr)) { OLSR_PRINTF(6, "MATCHED\n"); return OLSR_TRUE; } } if (olsr_cnf->ip_version == AF_INET) { for (hna4 = olsr_cnf->hna4_entries; hna4; hna4 = hna4->next) { OLSR_PRINTF(6, "HNA %s/%s\n", olsr_ip_to_string(&hna4->net), olsr_ip_to_string(&hna4->netmask)); if ( hna4->netmask.v4 != 0 && (addr->v4 & hna4->netmask.v4) == hna4->net.v4 ) { OLSR_PRINTF(6, "MATCHED\n"); return OLSR_TRUE; } } } else { int i; for (hna6 = olsr_cnf->hna6_entries; hna6; hna6 = hna6->next) { OLSR_PRINTF(6, "HNA %s/%d\n", olsr_ip_to_string(&hna6->net), hna6->prefix_len); if ( hna6->prefix_len == 0 ) continue; olsr_prefix_to_netmask(&tmp_msk, hna6->prefix_len); for (i = 0; i < 16; i++) { tmp_ip.v6.s6_addr[i] = addr->v6.s6_addr[i] & tmp_msk.v6.s6_addr[i]; } if (COMP_IP(&tmp_ip, &hna6->net)) { OLSR_PRINTF(6, "MATCHED\n"); return OLSR_TRUE; } } } return OLSR_FALSE;}/** check if name has the right syntax, i.e. it must adhere to a special regex * stored in regex_t_name * necessary to avaid names like "0.0.0.0 google.de\n etc" */olsr_boolis_name_wellformed(char *name) { return regexec(®ex_t_name, name, 1, ®match_t_name, 0) == 0 ;}/** * check if the service is in the right syntax and also that the hostname * or ip whithin the service is allowed */olsr_boolallowed_service(char *service_line){ /* the call of is_service_wellformed generates the submatches stored in regmatch_t_service * these are then used by allowed_hostname_or_ip_in_service * see regexec(3) for more infos */ if (!is_service_wellformed(service_line)) { return OLSR_FALSE; } else if (!allowed_hostname_or_ip_in_service(service_line, &(regmatch_t_service[1]))) { return OLSR_FALSE; } return OLSR_TRUE;}olsr_boolallowed_hostname_or_ip_in_service(char *service_line, regmatch_t *hostname_or_ip_match) { char *hostname_or_ip; union olsr_ip_addr olsr_ip; struct name_entry *name; if (hostname_or_ip_match->rm_so < 0 || hostname_or_ip_match->rm_eo < 0) { return OLSR_FALSE; } hostname_or_ip = strndup(&service_line[hostname_or_ip_match->rm_so], hostname_or_ip_match->rm_eo - hostname_or_ip_match->rm_so); //hostname is one of the names, that I announce (i.e. one that i am allowed to announce) for (name = my_names; name != NULL; name = name->next) { if (strncmp(name->name, hostname_or_ip, name->len - strlen(my_suffix)) == 0 ) { OLSR_PRINTF(4, "NAME PLUGIN: hostname %s in service %s is OK\n", hostname_or_ip, service_line); free(hostname_or_ip); hostname_or_ip = NULL; return OLSR_TRUE; } } //ip in service-line is allowed if (inet_pton(olsr_cnf->ip_version, hostname_or_ip, &olsr_ip) > 0) { if (allowed_ip(&olsr_ip)) { OLSR_PRINTF(2, "NAME PLUGIN: ip %s in service %s is OK\n", olsr_ip_to_string(&olsr_ip), service_line); free(hostname_or_ip); hostname_or_ip = NULL; return OLSR_TRUE; } } 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); free(hostname_or_ip); hostname_or_ip = NULL; return OLSR_FALSE;}/** * check if the service matches the syntax * of "protocol://host:port/path|tcp_or_udp|a short description", * which is given in the regex regex_t_service */olsr_boolis_service_wellformed(char *service_line){ return regexec(®ex_t_service, service_line, pmatch_service, regmatch_t_service, 0) == 0;}/** * check if the latlot matches the syntax */olsr_boolis_latlon_wellformed(char *latlon_line){ int hna = -1; float a = 0.0, b = 0.0; sscanf(latlon_line, "%f,%f,%d", &a, &b, &hna); return (a != 0.0 && b != 0.0 && -1 != hna);}/** * Returns 1 if this olsrd announces inet */olsr_bool get_isdefhna_latlon(void){ olsr_bool ret = OLSR_FALSE; if (AF_INET == olsr_cnf->ip_version) { struct hna4_entry *hna4; for(hna4 = olsr_cnf->hna4_entries; hna4; hna4 = hna4->next) { if (0 == hna4->netmask.v4) ret = OLSR_TRUE; } } else { struct hna6_entry *hna6; for(hna6 = olsr_cnf->hna6_entries; hna6; hna6 = hna6->next) { if (0 == hna6->prefix_len) ret = OLSR_TRUE; } } return ret;}/** * Grabs the current HNA selected default route */void lookup_defhna_latlon(union olsr_ip_addr *ip){ union olsr_ip_addr dest; struct rt_entry* rt_hna; memset(ip, 0, sizeof(ip)); memset(&dest, 0, sizeof(dest)); if (NULL != (rt_hna = olsr_lookup_routing_table(&dest))) { COPY_IP(ip, &rt_hna->rt_best->rtp_nexthop.gateway); }}/** * lookup a nodes name */char*lookup_name_latlon(union olsr_ip_addr *ip){ int hash; struct db_entry *entry; struct name_entry *name; for (hash = 0; hash < HASHSIZE; hash++) { for(entry = list[hash]; entry != NULL; entry = entry->next) { for (name = entry->names; name != NULL; name = name->next) { if (COMP_IP(&name->ip, ip)) return name->name; } } } return "";}/** * write latlon positions to a javascript file */voidwrite_latlon_file(void){ int hash; FILE* js; struct olsr_if *ifs; union olsr_ip_addr ip; struct tc_entry *tc; struct tc_edge_entry *tc_edge; if (!my_names || !latlon_table_changed) { return; } OLSR_PRINTF(2, "NAME PLUGIN: writing latlon file\n"); js = fopen( my_latlon_file, "w" ); if (js == NULL) { OLSR_PRINTF(0, "NAME PLUGIN: cant write latlon file\n"); return; } fprintf(js, "/* This file is overwritten regularly by olsrd */\n"); for (ifs = olsr_cnf->interfaces; ifs; ifs = ifs->next) { if (0 != ifs->interf) { if (olsr_cnf->ip_version == AF_INET) { /* * Didn't find a good sample to grab a simple * olsr_ip_addr from a given interface. Sven-Ola */ const char* p = olsr_ip_to_string(&olsr_cnf->main_addr); const char* q = sockaddr_to_string(&ifs->interf->int_addr); if (0 != strcmp(p, q)) { fprintf(js, "Mid('%s','%s');\n", p, q); } } else if (!(COMP_IP(&olsr_cnf->main_addr, (union olsr_ip_addr *)&ifs->interf->int6_addr.sin6_addr))) { fprintf(js, "Mid('%s','%s');\n", olsr_ip_to_string(&olsr_cnf->main_addr), olsr_ip_to_string((union olsr_ip_addr *)&ifs->interf->int6_addr.sin6_addr)); } } } for (hash = 0; hash < HASHSIZE; hash++) { struct mid_entry *entry = mid_set[hash].next; while(entry != &mid_set[hash]) { struct mid_address *alias = entry->aliases; while(alias) { fprintf(js, "Mid('%s','%s');\n", olsr_ip_to_string(&entry->main_addr), olsr_ip_to_string(&alias->alias)); alias = alias->next_alias; } entry = entry->next; } } lookup_defhna_latlon(&ip); fprintf(js, "Self('%s',%f,%f,%d,'%s','%s');\n", olsr_ip_to_string(&olsr_cnf->main_addr), my_lat, my_lon, get_isdefhna_latlon(), olsr_ip_to_string(&ip), my_names->name); for (hash = 0; hash < HASHSIZE; hash++) { struct db_entry *entry; for(entry = latlon_list[hash]; entry != NULL; entry = entry->next) { struct name_entry *name; for (name = entry->names; name != NULL; name = name->next) { fprintf(js, "Node('%s',%s,'%s','%s');\n", olsr_ip_to_string(&entry->originator), name->name, olsr_ip_to_string(&name->ip), lookup_name_latlon(&entry->originator)); } } } OLSR_FOR_ALL_TC_ENTRIES(tc) { OLSR_FOR_ALL_TC_EDGE_ENTRIES(tc, tc_edge) { fprintf(js, "Link('%s','%s',%f,%f,%f);\n", olsr_ip_to_string(&tc_edge->T_dest_addr), olsr_ip_to_string(&tc->addr), tc_edge->link_quality, tc_edge->inverse_link_quality, (tc_edge->link_quality * tc_edge->inverse_link_quality) ? 1.0 / (tc_edge->link_quality * tc_edge->inverse_link_quality) : 0.0); } OLSR_FOR_ALL_TC_EDGE_ENTRIES_END(tc, tc_edge); } OLSR_FOR_ALL_TC_ENTRIES_END(tc); fclose(js); latlon_table_changed = OLSR_FALSE;}/* * Local Variables: * mode: c * c-indent-tabs-mode: t * c-basic-offset: 4 * tab-width: 4 * End: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -