📄 interface_list.c
字号:
/* Kernel AODV v2.0National Institute of Standards and Technology Luke Klein-Berndt----------------------------------------------------- Version 2.0 new features: * Updated to AODV draft version 11 * Managed internet gatewaying * Monitor wireles signal strength * Many bug fixes!-----------------------------------------------------Originally based upon MadHoc code. I am notsure how much of it is left anymore, but MadHocproved to be a great starting point.MadHoc was written by - Fredrik Lilieblad,Oskar Mattsson, Petra Nylund, Dan Ouchterlonyand Anders Roxenhag Mail: mad-hoc@flyinglinux.netThis software is Open Source under the GNU General Public Licence.*/#include "interface_list.h"/**************************************************** interface_list----------------------------------------------------This is a list of the interfaces available on thecomputer****************************************************/struct interface_list_entry *interface_list;extern struct route_table_entry *g_my_entry;extern u_int32_t g_my_ip;extern u_int32_t g_broadcast_ip;/**************************************************** start_HELLO----------------------------------------------------Starts sending hello messages out for a givenIP address****************************************************/int start_HELLO (u_int32_t ip){ struct rrep *tmp_rrep; u_int32_t random_number; //creates the RREP you wish to send out if ((tmp_rrep = kmalloc(sizeof(struct rrep),GFP_ATOMIC)) == NULL) {#ifndef NO_ERROR printk("GEN_RREQ: Can't allocate new rreq\n");#endif return 1; } //fillin the RREP tmp_rrep->dst_ip=ip; tmp_rrep->src_ip=ip; tmp_rrep->hop_count=0; tmp_rrep->lifetime=htonl (ALLOWED_HELLO_LOSS * HELLO_INTERVAL);//+NODE_TRAVERSAL_TIME; tmp_rrep->dst_seq=0; tmp_rrep->type=2; tmp_rrep->r=0; tmp_rrep->reserved1=0; tmp_rrep->reserved2=0; tmp_rrep->prefix_sz=0; tmp_rrep->a=0; //Insert it into the timer queue! get_random_bytes(&random_number,sizeof(u_int32_t)); random_number=(43*random_number)%HELLO_INTERVAL; insert_timer_queue_entry(getcurrtime() + random_number, tmp_rrep, sizeof(struct rrep), tmp_rrep->dst_ip,0,1,EVENT_HELLO); update_timer_queue(); return 0;}/**************************************************** init_interface_list----------------------------------------------------Discover the available interfaces and add themto the list****************************************************/int init_interface_list(){ struct route_table_entry *tmp_route; struct interface_list_entry *new_entry=NULL; struct net_device *dev; struct in_device *tmp_indev; int error; //dev_base is a kernel variable pointing to a list of //all the available netdevices and is maintained //by the kernel dev= dev_base; interface_list=NULL; //look at all the devices while (dev!=NULL) { //we don't want to include the loopback device //or any device which doesn't have a completed //ip section if ((dev->ip_ptr!=NULL) && (strcmp(dev->name,g_block_dev)!=0) && ((strcmp(g_aodv_dev,"")==0) || (strcmp(dev->name,g_aodv_dev)==0))) {#ifdef MESSAGES printk("INTERFACE LIST: Adding interface: %s \n",dev->name);#endif //find the IP for the dev tmp_indev=(struct in_device *) dev->ip_ptr; if (tmp_indev->ifa_list != NULL) //Thanks to Ronnie Hedlund for this fix { //prevents NULL error when accessing //device without an IP... //we are going to want to include a route to ourself tmp_route=create_route_table_entry(); tmp_route->dst_ip = tmp_indev->ifa_list->ifa_address; //fill in the info for the route tmp_route->self_route=1; tmp_route->dst_seq = 1; tmp_route->old_dst_seq=0; tmp_route->rreq_id = 1; tmp_route->hop_count = 0; tmp_route->next_hop = tmp_route->dst_ip; tmp_route->lifetime = -1; tmp_route->route_valid = 1; tmp_route->route_seq_valid = 1; tmp_route->precursors=NULL; tmp_route->dev=dev; //save the entry as being our own... so the last dev read in //becomes our default dev. g_my_entry = tmp_route; g_my_ip=g_my_entry->dst_ip; if ((strcmp(dev->name,"lo")!=0) || USE_LO) { if ((new_entry = kmalloc(sizeof(struct interface_list_entry),GFP_ATOMIC)) == NULL) /* Couldn't create a new entry in the routing table */ return 1; //fill in the info on the dev new_entry->ip=tmp_route->dst_ip; new_entry->route_entry=tmp_route; new_entry->next=interface_list; new_entry->dev=dev; new_entry->last_hello=getcurrtime(); new_entry->last_broadcast=0; interface_list=new_entry; strncpy(new_entry->name,dev->name,IFNAMSIZ); //start sending out a hello message for this dev start_HELLO(new_entry->ip); //insert a broadcast address into the kernel routing table using this dev //create the socket... set it to type datagram error = sock_create(PF_INET,SOCK_DGRAM,0,&(new_entry->sock)); if (error<0) { printk(KERN_ERR "Error during creation of socket; terminating, %d\n",error); return 0; } init_sock(new_entry->sock,new_entry->ip,dev->name); insert_kernel_route_entry(tmp_route->dst_ip,tmp_route->dst_ip,new_entry->name); } } } //next dev dev= dev->next; } return 0;}struct interface_list_entry *find_first_interface_entry(){ return interface_list;}/**************************************************** find_dev_ip----------------------------------------------------It will find the IP for a dev****************************************************/u_int32_t find_dev_ip(struct net_device *dev){ struct in_device *tmp_indev; //make sure we get a valid DEV if (dev==NULL) { #ifndef NO_ERROR printk ("FIND_DEV_IP: we gotta NULL DEV! "); #endif return 0; } //make sure that dev has an IP section if (dev->ip_ptr==NULL) { #ifndef NO_ERROR printk ("FIND_DEV_IP: we gotta NULL ip_ptr!! "); #endif return 0; } //find that ip! tmp_indev=(struct in_device *) dev->ip_ptr; return (tmp_indev->ifa_list->ifa_address);}/**************************************************** find_interface_by_dev----------------------------------------------------Finds an interface by matching up the dev tothe dev in the interface list****************************************************/struct interface_list_entry *find_interface_by_dev(struct net_device *dev){ struct interface_list_entry *tmp_entry=interface_list; struct in_device *tmp_indev; u_int32_t tmp_ip; //Make sure the dev is legit if (dev==NULL) { #ifndef NO_ERROR printk ("FIND_INTERFACE_BY_DEV: we gotta NULL DEV! \n"); #endif return NULL; } //make sure it has a legit ip section if (dev->ip_ptr==NULL) { #ifndef NO_ERROR printk ("FIND_INTERFACE_BY_DEV: we gotta NULL ip_ptr!! \n"); #endif return NULL; } //find the ip address for the dev tmp_indev=(struct in_device *) dev->ip_ptr; tmp_ip = tmp_indev->ifa_list->ifa_address; //search the interface list for a device with the same ip while (tmp_entry!=NULL) { if (tmp_entry->ip==tmp_ip) return tmp_entry; tmp_entry=tmp_entry->next; } #ifdef MESSAGES printk("FIND_INTERFACE_BY_DEV: Failed search for matching interface for: %s which has an ip of: %s\n",dev->name,inet_ntoa(tmp_ip)); #endif return NULL;}/**************************************************** find_interface_by_ip----------------------------------------------------Finds an interface list entry by comparing IPs****************************************************/struct interface_list_entry *find_interface_by_ip(u_int32_t ip){ struct interface_list_entry *tmp_entry=interface_list; //go through the whole list while (tmp_entry!=NULL) { //if it finds a match if (tmp_entry->ip==ip) return tmp_entry; tmp_entry=tmp_entry->next; } return NULL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -