/* * ifs_del_byname: deletes from the `ifs' array the device whose name is equal * to `dev_name' */ void ifs_del_byname(interface *ifs, int *ifs_n, char *dev_name) { int if_pos; if_pos=ifs_find_devname(ifs, *ifs_n, dev_name); if(if_pos < 0) return; ifs_del(ifs, ifs_n, if_pos); }
/* * ifs_del_all_name: deleted from the `ifs' array all the device which have a * device name that begins with `dev_name'. For example, * ifs_del_all_name(ifs, ifs_n, "tun") deletes all the tunnel iifs */ void ifs_del_all_name(interface *ifs, int *ifs_n, char *dev_name) { int i, dev_len; if(!dev_name || (dev_len=strlen(dev_name)) > IFNAMSIZ) return; for(i=0; i<(*ifs_n); i++) { if(ifs[i].dev_name && !strncmp(ifs[i].dev_name, dev_name, dev_len)) { ifs_del(ifs, ifs_n, i); if(i <= (*ifs_n)-1) i--; } } }
int exclude_interface(void) { int i; printf("Number of Interfaces in Use: %d\n", me.cur_ifs_n); for (i = 0; i < me.cur_ifs_n; i++) printf("Interface names in Use: %s", me.cur_ifs[i].dev_name); for (i = 0; i < me.cur_ifs_n; i++) { if (strcmp(me.cur_ifs[i].dev_name, optarg) == 0) { printf("Interface %s removed, And replaced with %s", me.cur_ifs[i].dev_name, me.cur_ifs[me.cur_ifs_n].dev_name); ifs_del(me.cur_ifs, &me.cur_ifs_n, i); return 0; } else fatal("Interface %s not found!", optarg); } }
/* * init_internet_gateway_search: * Initialization of the igs.c code. */ void init_internet_gateway_search(void) { inet_prefix new_gw; char new_gw_dev[IFNAMSIZ]; pthread_t ping_thread; pthread_attr_t t_attr; int i, ret, res, e; active_gws = 0; igw_multi_gw_disabled = 0; setzero(multigw_nh, sizeof(igw_nexthop) * MAX_MULTIPATH_ROUTES); /* * Just return if we aren't in restricted mode or if the user doesn't * want to use shared internet connections */ if (!restricted_mode || (!server_opt.use_shared_inet && !server_opt.share_internet)) return; loginfo("Activating the Internet Gateway Search engine"); init_igws(&me.igws, &me.igws_counter, GET_LEVELS(my_family)); init_tunnels_ifs(); /* delete all the old tunnels */ del_all_tunnel_ifs(0, 0, 0, NTK_TUNL_PREFIX); /* * Bring tunl0 up (just to test if the ipip module is loaded) */ loginfo("Checking if \"" DEFAULT_TUNL_IF "\" exists"); if (tunnel_change(0, 0, 0, DEFAULT_TUNL_PREFIX, DEFAULT_TUNL_NUMBER) < 0) { printf("Cannot read \"" DEFAULT_TUNL_IF "\". " "Is the \"ipip\" kernel module loaded?\n" " If you don't care about using the shared internet " "connections of the ntk nodes\n" " around you, disable the \"use_shared_inet\" option " "in netsukuku.conf"); del_resolv_conf("nameserver 127.0.0.1", "/etc/resolv.conf"); exit(1); } ifs_del_all_name(me.cur_ifs, &me.cur_ifs_n, NTK_TUNL_PREFIX); ifs_del_all_name(me.cur_ifs, &me.cur_ifs_n, DEFAULT_TUNL_PREFIX); /* * Delete old routing rules */ reset_igw_rules(); /* * Init netfilter */ res = mark_init(server_opt.share_internet); if (res) { error(err_str); error("Cannot set the netfilter rules needed for the multi-igw. " "This feature will be disabled"); igw_multi_gw_disabled = 1; } /* * Check anomalies: from this point we initialize stuff only if we * have an Inet connection */ if (!server_opt.inet_connection) return; if (!server_opt.inet_hosts) fatal("You didn't specified any Internet hosts in the " "configuration file. What hosts should I ping?"); /* * If we are sharing our internet connection, activate the * masquerading. */ if (server_opt.share_internet) { igw_exec_masquerade_sh(server_opt.ip_masq_script, 0); if (!server_opt.ip_masq_script) fatal("No masquerading script was configured!"); }; /* * Get the default gateway route currently set in the kernel routing * table */ setzero(&new_gw, sizeof(inet_prefix)); ret = rt_get_default_gw(&new_gw, new_gw_dev); /* * If there is no IP set in the route, fetch it at least from the * device included in it. */ if (!new_gw.family && *new_gw_dev) { if (get_dev_ip(&new_gw, my_family, new_gw_dev) < 0) (*new_gw_dev) = 0; } if (ret < 0 || (!*new_gw_dev && !new_gw.family)) { /* Nothing useful has been found */ loginfo("The retrieval of the default gw from the kernel failed."); if (!server_opt.inet_gw.data[0]) fatal("The default gw isn't set in the kernel and you " "didn't specified it in netsukuku.conf. " "Cannot continue!"); } else if (!server_opt.inet_gw_dev || strncmp(new_gw_dev, server_opt.inet_gw_dev, IFNAMSIZ) || memcmp(new_gw.data, server_opt.inet_gw.data, MAX_IP_SZ)) { if (server_opt.inet_gw.data[0]) loginfo("Your specified Internet gateway doesn't match with " "the one currently stored in the kernel routing table." "I'm going to use the kernel gateway: %s dev %s", inet_to_str(new_gw), new_gw_dev); if (!server_opt.inet_gw_dev) server_opt.inet_gw_dev = xstrdup(new_gw_dev); else strncpy(server_opt.inet_gw_dev, new_gw_dev, IFNAMSIZ); memcpy(&server_opt.inet_gw, &new_gw, sizeof(inet_prefix)); /* Delete the default gw, we are replacing it */ rt_delete_def_gw(0); } loginfo("Using \"%s dev %s\" as your first Internet gateway.", inet_to_str(server_opt.inet_gw), server_opt.inet_gw_dev); if (rt_replace_def_gw(server_opt.inet_gw_dev, server_opt.inet_gw, 0)) fatal("Cannot set the default gw to %s %s", inet_to_str(server_opt.inet_gw), server_opt.inet_gw_dev); active_gws++; /* * Activate the anti-loop multi-igw shield */ if (server_opt.share_internet) { rule_add(0, 0, 0, 0, FWMARK_ALISHIELD, RTTABLE_ALISHIELD); if (rt_replace_def_gw(server_opt.inet_gw_dev, server_opt.inet_gw, RTTABLE_ALISHIELD)) { error("Cannot set the default route in the ALISHIELD table. " "Disabling the multi-inet_gw feature"); igw_multi_gw_disabled = 1; } } /* * Activate the traffic shaping for the `server_opt.inet_gw_dev' * device */ if (server_opt.shape_internet) igw_exec_tcshaper_sh(server_opt.tc_shaper_script, 0, server_opt.inet_gw_dev, server_opt.my_upload_bw, server_opt.my_dnload_bw); for (i = 0; i < me.cur_ifs_n; i++) if (!strcmp(me.cur_ifs[i].dev_name, server_opt.inet_gw_dev)) { for (e = 0; e < server_opt.ifs_n; e++) if (!strcmp(server_opt.ifs[i], server_opt.inet_gw_dev)) fatal("You specified the \"%s\" interface" " in the options, but this device is also" " part of the primary Internet gw route." " Don't include \"%s\" in the list of " "interfaces utilised by the daemon", server_opt.inet_gw_dev, server_opt.inet_gw_dev); loginfo("Deleting the \"%s\" interface from the device " "list since it is part of the primary Internet" " gw route.", me.cur_ifs[i].dev_name); ifs_del(me.cur_ifs, &me.cur_ifs_n, i); if (me.cur_ifs_n <= 0) fatal ("The deleted interface cannot be used by NetsukukuD because it is part\n" " of your primary Internet gw route. You have to specify another\n" " interface with the -i option or you won't be able share your" " Internet connection"); } loginfo("Launching the first ping to the Internet hosts"); if (!server_opt.disable_andna) internet_hosts_to_ip(); me.inet_connected = igw_check_inet_conn(); if (me.inet_connected) loginfo("The Internet connection is up & running"); else loginfo("The Internet connection appears to be down"); if (!me.inet_connected && server_opt.share_internet) fatal("We are not connected to the Internet, but you want to " "share your connection. Please check your options"); debug(DBG_SOFT, "Evoking the Internet ping daemon."); pthread_attr_init(&t_attr); pthread_attr_setdetachstate(&t_attr, PTHREAD_CREATE_DETACHED); pthread_create(&ping_thread, &t_attr, igw_check_inet_conn_t, 0); }
/* * radar_scan * * It starts the scan of the local area. * * It sends MAX_RADAR_SCANS packets in broadcast then it waits MAX_RADAR_WAIT * and in the while the echo replies are gathered. After MAX_RADAR_WAIT it * stops to receive echo replies and it does a statistical analysis of the * gathered echo replies, it updates the r_nodes in the map and sends a qspn * round if something is changed in the map and if the `activate_qspn' argument * is non zero. * * It returns 1 if another radar_scan is in progress, -1 if something went * wrong, 0 on success. */ int radar_scan(int activate_qspn) { pthread_t thread; PACKET pkt; int i, d, *p; ssize_t err; u_char echo_scan; /* We are already doing a radar scan, that's not good */ if (radar_scan_mutex) return 1; radar_scan_mutex = 1; /* * We create the PACKET */ setzero(&pkt, sizeof(PACKET)); inet_setip_bcast(&pkt.to, my_family); my_echo_id = rand(); gettimeofday(&scan_start, 0); /* * Send a bouquet of ECHO_ME pkts */ if (me.cur_node->flags & MAP_HNODE) { pkt.hdr.sz = sizeof(u_char); pkt.hdr.flags |= HOOK_PKT | BCAST_PKT; pkt.msg = xmalloc(pkt.hdr.sz); debug(DBG_INSANE, "Radar scan 0x%x activated", my_echo_id); } else total_radars++; if (restricted_mode) pkt.hdr.flags |= RESTRICTED_PKT; /* Loop through the me.cur_ifs array, sending the bouquet using all the * interfaces we have */ for (d = 0; d < me.cur_ifs_n; d++) { pkt_add_dev(&pkt, &me.cur_ifs[d], 1); pkt.sk = 0; /* Create a new socket */ /* Send MAX_RADAR_SCANS# packets using me.cur_ifs[d] as * outgoing interface */ for (i = 0, echo_scan = 0; i < MAX_RADAR_SCANS; i++, echo_scan++) { if (me.cur_node->flags & MAP_HNODE) memcpy(pkt.msg, &echo_scan, sizeof(u_char)); err = send_rq(&pkt, 0, ECHO_ME, my_echo_id, 0, 0, 0); if (err < 0) { if (errno == ENODEV) { /* * The me.cur_ifs[d] device doesn't * exist anymore. Delete it. */ fatal("The device \"%s\" has been removed", me.cur_ifs[d].dev_name); ifs_del(me.cur_ifs, &me.cur_ifs_n, d); d--; } else error(ERROR_MSG "Error while sending the" " scan 0x%x... skipping", ERROR_FUNC, my_echo_id); break; } radar_scans[d]++; total_radar_scans++; } if (!radar_scans[d]) error("radar_scan(): The scan 0x%x on the %s interface failed." " Not a single scan was sent", my_echo_id, pkt.dev->dev_name); if (pkt.sk > 0) inet_close(&pkt.sk); } pkt_free(&pkt, 1); if (!total_radar_scans) { error("radar_scan(): The scan 0x%x failed. It wasn't possible " "to send a single scan", my_echo_id); return -1; } xtimer(max_radar_wait, max_radar_wait << 1, &radar_wait_counter); final_radar_queue(); radar_update_map(); if (activate_qspn) for (i = 0; i < me.cur_quadg.levels; i++) if (send_qspn_now[i]) { p = xmalloc(sizeof(int)); *p = i; /* We start a new qspn_round in the `i'-th level */ pthread_create(&thread, &radar_qspn_send_t_attr, radar_qspn_send_t, (void *) p); } if (!(me.cur_node->flags & MAP_HNODE)) reset_radar(); radar_scan_mutex = 0; return 0; }