📄 main.c
字号:
//struct sockaddr_in6 *ina; //struct if6_info if_in_use; struct if6_info *if_in_use=NULL/*,*list_tmp=NULL*/; struct ip6_addr *list_tmp=NULL; //int ret;#else char tmp_ifname[IFNAMSIZ]; struct sockaddr_in *ina; struct ifconf ifc; struct ifreq ifreq, *ifr; int i, iw_sock, if_sock = 0;#endif /* _IPV6 */ memset(&this_host, 0, sizeof(struct host_info)); memset(dev_indices, 0, sizeof(unsigned int) * MAX_NR_INTERFACES); //PL:#ifdef _IPV6 /* PL: for IPv6, user always need to specified which interface to use */ if (!ifname) { fprintf(stderr, "\nCould not find a wireless interface!\n"); fprintf(stderr, "Use -i <interface> to override...\n\n"); exit(-1); }#else if (!ifname) { /* No interface was given... search for first wireless. */ iw_sock = socket(PF_INET, SOCK_DGRAM, 0); ifc.ifc_len = sizeof(buf); ifc.ifc_buf = buf; if (ioctl(iw_sock, SIOCGIFCONF, &ifc) < 0) { fprintf(stderr, "Could not get wireless info\n"); exit(-1); } ifr = ifc.ifc_req; for (i = ifc.ifc_len / sizeof(struct ifreq); i >= 0; i--, ifr++) { struct iwreq req; strcpy(req.ifr_name, ifr->ifr_name); if (ioctl(iw_sock, SIOCGIWNAME, &req) >= 0) { strcpy(tmp_ifname, ifr->ifr_name); break; } } /* Did we find a wireless interface? */ if (!strlen(tmp_ifname)) { fprintf(stderr, "\nCould not find a wireless interface!\n"); fprintf(stderr, "Use -i <interface> to override...\n\n"); exit(-1); } strcpy(ifreq.ifr_name, tmp_ifname); if (ioctl(iw_sock, SIOCGIFINDEX, &ifreq) < 0) { log(LOG_ERR, errno, "Could not get index of %s", tmp_ifname); close(if_sock); exit(-1); } close(iw_sock); ifname = tmp_ifname; log(LOG_NOTICE, 0, "INIT: Attaching to %s, override with -i <if1,if2,...>.", tmp_ifname); }#endif /* _IPV6 */ strcpy(ifnames, ifname); /* Intitialize the local sequence number an rreq_id to zero */ this_host.seqno = 0; this_host.rreq_id = 0; /* Zero interfaces enabled so far... */ this_host.nif = 0; //PL:#ifdef _IPV6 //printf("ifname:%s!!!\n",ifname); iface = strtok(ifname, ","); do { /* strcpy(ifreq.ifr_name, iface); if (ioctl(if_sock, SIOCGIFINDEX, &ifreq) < 0) { log(LOG_ERR, errno, "Could not get index of %s", iface); close(if_sock); exit(-1); } */ //printf("ifname:%s iface:%s\n",ifname,iface); //memset(&if_in_use, 0, sizeof(if_in_use)); //printf("calling get_if6_info.....\n"); if_in_use_old=NULL; old_ip6addr_head=NULL; if_in_use_old = get_if6_info(iface); //printf("called successfully!\n"); /*if (if_in_use_old == NULL){ fprintf(stderr, "\nCould not find a %s interface!\n", iface); fprintf(stderr, "%s might not configure for IPv6.\n\n", iface); exit(-1); } for(list_tmp=if_in_use_old;list_tmp;list_tmp=list_tmp->next) printf("%s's ipv6 address:%s/%d\n",list_tmp->devname,ip6_to_str(list_tmp->ipv6_addr), list_tmp->plen); */ for(list_tmp=old_ip6addr_head;list_tmp;list_tmp=list_tmp->next) printf("%s's ipv6 address:%s/%d\n",iface,ip6_to_str(list_tmp->ip6_in6addr),list_tmp->plen); //printf("print list end!\n"); strcpy(myifname,iface); temp_address_config(); if_in_use = get_if6_info(iface); if (if_in_use==NULL) { fprintf(stderr, "\nCould not find a %s interface!\n", iface); fprintf(stderr, "%s might not configure for IPv6.\n\n", iface); exit(-1); } //PL: For some reason, the original code never store the ifindex in // this_host.devs[x].ifindex. DEV_NR(this_host.nif).ifindex = if_in_use->if_idx; //dev_indices[this_host.nif++] = ifreq.ifr_ifindex; dev_indices[this_host.nif++] = if_in_use->if_idx; //PL: For testing purpose //printf("dev_indices[0] = %d\n", dev_indices[0]); //strcpy(DEV_IFINDEX(ifreq.ifr_ifindex).ifname, iface); strcpy(DEV_IFINDEX(if_in_use->if_idx).ifname, iface); /* Get IP-address of interface... */ //PL: IP-address already in if_in_use /* ina = get_if_info(iface, SIOCGIFADDR); if (ina == NULL) exit(-1); DEV_IFINDEX(ifreq.ifr_ifindex).ipaddr = ntohl(ina->sin_addr.s_addr); */ memcpy(&(DEV_IFINDEX(if_in_use->if_idx).ipaddr), &(if_in_use->ipv6_addr), sizeof(struct in6_addr)); printf("ZJH:local ip address:%s\n",ip6_to_str(if_in_use->ipv6_addr)); /* Get netmask of interface... */ //PL: IPv6 doesn't have netmask, but plen instead /* ina = get_if_info(iface, SIOCGIFNETMASK); if (ina == NULL) exit(-1); DEV_IFINDEX(ifreq.ifr_ifindex).netmask = ntohl(ina->sin_addr.s_addr); */ DEV_IFINDEX(if_in_use->if_idx).plen = if_in_use->plen; printf("ZJH:prefix length:%d\n",if_in_use->plen); // PL: IPv6 doesn't have broadcast /* ina = get_if_info(iface, SIOCGIFBRDADDR); if (ina == NULL) exit(-1); DEV_IFINDEX(ifreq.ifr_ifindex).broadcast = ntohl(ina->sin_addr.s_addr); */ // PL: set the multicast address instead memcpy(&DEV_IFINDEX(if_in_use->if_idx).multicast, &ipv6_multicast_addr, sizeof (struct in6_addr)); printf("ZJH:multicast address:%s\n",ip6_to_str(ipv6_multicast_addr)); // set the internet gateway multicast address memcpy(&DEV_IFINDEX(if_in_use->if_idx).gateway_mcast,&gateway_mcast_addr,sizeof(struct in6_addr)); printf("ZJH:gateway multicast address:%s\n",ip6_to_str(gateway_mcast_addr)); //DEV_IFINDEX(ifreq.ifr_ifindex).enabled = 1; DEV_IFINDEX(if_in_use->if_idx).enabled = 1; if (this_host.nif >= MAX_NR_INTERFACES) break; } while ((iface = strtok(NULL, ",")));#else /* Not _IPV6 */ /* Find the indices of all interfaces to broadcast on... */ if_sock = socket(AF_INET, SOCK_DGRAM, 0); iface = strtok(ifname, ","); /* OK, now lookup interface information, and store it... */ do { strcpy(ifreq.ifr_name, iface); if (ioctl(if_sock, SIOCGIFINDEX, &ifreq) < 0) { log(LOG_ERR, errno, "Could not get index of %s", iface); close(if_sock); exit(-1); } dev_indices[this_host.nif++] = ifreq.ifr_ifindex; strcpy(DEV_IFINDEX(ifreq.ifr_ifindex).ifname, iface); /* Get IP-address of interface... */ ina = get_if_info(iface, SIOCGIFADDR); if (ina == NULL) exit(-1); DEV_IFINDEX(ifreq.ifr_ifindex).ipaddr = ntohl(ina->sin_addr.s_addr); /* Get netmask of interface... */ ina = get_if_info(iface, SIOCGIFNETMASK); if (ina == NULL) exit(-1); DEV_IFINDEX(ifreq.ifr_ifindex).netmask = ntohl(ina->sin_addr.s_addr); ina = get_if_info(iface, SIOCGIFBRDADDR); if (ina == NULL) exit(-1); DEV_IFINDEX(ifreq.ifr_ifindex).broadcast = ntohl(ina->sin_addr.s_addr); DEV_IFINDEX(ifreq.ifr_ifindex).enabled = 1; if (this_host.nif >= MAX_NR_INTERFACES) break; } while ((iface = strtok(NULL, ","))); close(if_sock);#endif /* _IPV6 */ /* Load kernel modules */ //PL:#ifdef _IPV6 load_ipv6_modules(ifnames);#else load_modules(ifnames);#endif /* _IPV6 */ /* Enable IP forwarding and set other kernel options... */ if (set_kernel_options() < 0) { fprintf(stderr, "Could not set kernel options!\n"); exit(-1); } if (internet_gw_mode) { //PL: log(LOG_NOTICE, 0, "INIT: Internet gateway mode enabled!"); this_host.gateway_mode = 1;#ifdef _IPV6 if (find_default_gw6())#else if (find_default_gw())#endif /* _IPV6 */ { old_default_gw_exist=1; }else old_default_gw_exist=0; /*else { this_host.gateway_mode = 0; sprintf(buf, "/sbin/route add default gw 127.0.0.1 dev lo"); system(buf); }*/ }} /* This signal handler ensures clean exits */void signal_handler(int type){ switch (type) { case SIGSEGV: log(LOG_ERR, 0, "SEGMENTATION FAULT!!!! Exiting!!! " "To get a core dump, compile with DEBUG option."); case SIGINT: case SIGHUP: case SIGTERM: default: exit(0); }}int main(int argc, char **argv){ static char *ifname = NULL; /* Name of interface to attach to */ fd_set rfds, readers; int n, nfds = 0, i; int daemonize = 0; struct timeval *timeout; //PL:#ifdef _IPV6 //char *multicast_addr_str = MULTICAST_ADDR_STR;#endif /* _IPV6 */ /* Remember the name of the executable... */ progname = strrchr(argv[0], '/'); printf("argv[0]:%s\n",argv[0]); if (progname) progname++; else progname = argv[0]; /* Use debug output as default */ debug = 1; /* Parse command line: */ while (1) { int opt; opt = getopt_long(argc, argv, "i:jln:dghp:q:r:s:uwxDV", longopts, 0); if (opt == EOF) break; switch (opt) { case 0: break; case 'd': debug = 0; daemonize = 1; break; case 'g': rreq_gratuitous = 1; break; case 'i': printf("optarg:%s\n",optarg); ifname = optarg; break; case 'j': hello_jittering = 0; break; case 'l': log_to_file = 1; break; case 'n': if (optarg && isdigit(*optarg)) { receive_n_hellos = atoi(optarg); if (receive_n_hellos < 2) { fprintf(stderr, "-n should be at least 2!\n"); exit(-1); } } break; case 'p': memset(gw_global_prefix,'\0',sizeof(gw_global_prefix)); is_gateway=1; printf("optarg:%s\n",optarg); if (optarg&&optarg[0]!='-') memcpy(gw_global_prefix,optarg,strlen(optarg)); else{ fprintf(stderr,"please assign gateway's global address prefix!(-p prefix/len)\n"); exit(-1); } break; case 'q': if (optarg && isdigit(*optarg)) hello_qual_threshold = atoi(optarg); break; case 'r': if (optarg && isdigit(*optarg)) rt_log_interval = atof(optarg) * 1000; break; case 's': if (optarg) spy_addrs = optarg; break; case 'u': unidir_hack = 1; break; case 'w': internet_gw_mode = 1; break; case 'x': expanding_ring_search = 0; break; case 'D': wait_on_reboot = 0; break; case 'V':#ifdef _IPV6 printf ("\nAODV6-SFU-UU v%s, AODV draft v10 (Uppsala University & Ericsson AB & Simon Fraser University)\nAuthor: Erik Nordstr鰉, erik.nordstrom@it.uu.se\nAuthor: Peter Lee, mclee@sfu.ca\n\n",AODV_UU_VERSION);#else printf ("\nAODV-UU v%s, AODV draft v10 (Uppsala University & Ericsson AB.)\nAuthor: Erik Nordstr鰉, erik.nordstrom@it.uu.se\n\n",AODV_UU_VERSION);#endif /* _IPV6 */ exit(0); break; case '?': case ':': exit(0); default: usage(0); } } /* Check that we are running as root */ if (geteuid() != 0) { fprintf(stderr, "aodvd: must be root\n"); exit(1); } /* Detach from terminal */ if (daemonize) { if (fork() != 0) exit(0); /* Close stdin, stdout and stderr... */ /* close(0); */ close(1); close(2); setsid(); } /* Make sure we cleanup at exit... */ atexit((void *) &cleanup); //PL: Setup multicast address#ifdef _IPV6 //inet_pton(AF_INET6, multicast_addr_str, &ipv6_multicast_addr);#endif /* _IPV6 */ /* Initialize data structures and services... */ log_init(); host_init(ifname); timer_queue_init(); rt_table_init(); packet_queue_init(); packet_input_init(); aodv_socket_init(); /* Make sure we run at high priority to make up for the user space packet processing... */ /* nice(-5); */ /* Catch SIGHUP, SIGINT and SIGTERM type signals */ signal(SIGHUP, signal_handler); signal(SIGINT, signal_handler); signal(SIGTERM, signal_handler); /* Only capture segmentation faults when we are not debugging... */#ifndef DEBUG signal(SIGSEGV, signal_handler);#endif /* Set sockets to watch... */ FD_ZERO(&readers); for (i = 0; i < nr_callbacks; i++) { FD_SET(callbacks[i].fd, &readers); if (callbacks[i].fd >= nfds) nfds = callbacks[i].fd + 1; } /* Set the wait on reboot timer... */ if (wait_on_reboot) { worb_timer.handler = wait_on_reboot_timeout; worb_timer.data = &wait_on_reboot; worb_timer.used = 0; timer_add_msec(&worb_timer, DELETE_PERIOD); log(LOG_NOTICE, 0, "INIT: In wait on reboot for %d milliseconds. Disable with \"-D\".", DELETE_PERIOD); } /* Schedule the first Hello */ hello_init(); if (rt_log_interval) log_rt_table_init(); /*begin manet address automatic configure*/ tentative_addrconf_init(); //PL: debug printf("**************************************************************\n"); printf("* AODVD6 are now started *\n"); printf("**************************************************************\n"); //printf("RREP_SIZE = %d\n", RREP_SIZE); while (1) { memcpy((char *) &rfds, (char *) &readers, sizeof(rfds)); timeout = timer_age_queue(); if ((n = select(nfds, &rfds, NULL, NULL, timeout)) < 0) { if (errno != EINTR) log(LOG_WARNING, errno, "main.c: Failed select (main loop)"); continue; } if (n > 0) { for (i = 0; i < nr_callbacks; i++) { if (FD_ISSET(callbacks[i].fd, &rfds)) { /* We don't want any timer SIGALRM's while executing the callback functions, therefore we block the timer... */ (*callbacks[i].func) (callbacks[i].fd); } } } } /* Main loop */ return 0;}static void cleanup(void){ char buf[100]; DEBUG(LOG_DEBUG, 0, "CLEANING UP!"); /*if (internet_gw_mode && !this_host.gateway_mode) system("/sbin/route del default"); */ remove_modules(); rt_table_destroy(); if(internet_gw_mode&&old_default_gw_exist){ memset(buf,'\0',sizeof(buf)); sprintf(buf,"route -A inet6 add %s/%d gw %s",ip6_to_str(ipv6_dest_default), IPV6_DEST_DEFAULT_PLEN,ip6_to_str(old_default_gw)); system(buf); } packet_input_cleanup(); aodv_socket_cleanup(); log_cleanup();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -