⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rrep.c

📁 一种AODV实现方法
💻 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 "rrep.h"/****************************************************   rrep----------------------------------------------------Handles sending and recieving RREPs****************************************************/extern struct route_table_entry *g_my_entry;extern u_int32_t g_my_ip;extern u_int32_t g_broadcast_ip;int recv_hello(struct event_queue_entry *working_packet){    struct route_table_entry *recv_route;    struct rrep *tmp_rrep;    struct neighbor_list_entry  *tmp_entry;    tmp_rrep=working_packet->data;    tmp_entry=find_neighbor_list_entry(tmp_rrep->src_ip);    if (tmp_entry==NULL)    {        tmp_entry=create_neighbor_list_entry(tmp_rrep->src_ip);        memcpy(&(tmp_entry->hw_addr),&(working_packet->src_hw_addr),sizeof(unsigned char) * ETH_ALEN);        tmp_entry->dev=working_packet->dev;        set_spy();    #ifdef TRACE        printk ("RREP: Node: %s is now our Neighbor.\n",inet_ntoa(tmp_rrep->src_ip));    #endif    }    delete_timer_queue_entry_of_id(tmp_entry->ip, EVENT_NEIGHBOR);    insert_timer_queue_entry(tmp_rrep->lifetime+getcurrtime()+20,NULL,0,tmp_rrep->src_ip,0,0,EVENT_NEIGHBOR);    update_timer_queue();    recv_route = find_route_table_entry(tmp_rrep->dst_ip);    if (recv_route==NULL)    {#ifdef TRACE        if (tmp_rrep!=NULL)            printk ("RREP: adding route for: %s.\n",inet_ntoa(tmp_rrep->src_ip));#endif        // No entry in RT found, generate a new        recv_route = create_route_table_entry();        recv_route->dst_ip = tmp_rrep->dst_ip;        recv_route->lifetime = 0;        recv_route->dev=working_packet->dev;        recv_route->next_hop = working_packet->src_ip;        recv_route->hop_count = tmp_rrep->hop_count ;        recv_route->route_valid=TRUE;        recv_route->route_seq_valid=TRUE;        insert_kernel_route_entry(recv_route->dst_ip, recv_route->next_hop,recv_route->dev->name);    }    if ((tmp_rrep->hop_count < recv_route->hop_count) || (recv_route->route_valid==FALSE))// || (( tmp_rrep->dst_seq>=recv_route->dst_seq) && (recv_route->next_hop!=recv_route->dst_ip)))    {        delete_kernel_route_entry(recv_route->dst_ip, recv_route->next_hop);        recv_route->dev=working_packet->dev;        recv_route->next_hop = working_packet->src_ip;        recv_route->hop_count = tmp_rrep->hop_count ;        recv_route->route_valid=TRUE;        recv_route->route_seq_valid=TRUE;        insert_kernel_route_entry(recv_route->dst_ip, recv_route->next_hop,recv_route->dev->name);    }    tmp_entry->route_entry=recv_route;    recv_route->lifetime = MAX(recv_route->lifetime,(tmp_rrep->lifetime+getcurrtime()+20));    recv_route->dst_seq = tmp_rrep->dst_seq;#ifdef TRACE    if ((getcurrtime()-tmp_entry->lifetime)>HELLO_INTERVAL*ALLOWED_HELLO_LOSS+20)    {        printk ("----> %u <------  since our last hello from Node:  ",getcurrtime()-tmp_entry->lifetime);        printk("%s  Life \n",inet_ntoa(tmp_entry->ip));    }#endif    tmp_entry->lifetime=getcurrtime();    return 0;}void convert_rrep_to_host(struct rrep *tmp_rrep){    tmp_rrep->dst_seq=ntohl(tmp_rrep->dst_seq);    tmp_rrep->lifetime=ntohl(tmp_rrep->lifetime);}void convert_rrep_to_network(struct rrep *tmp_rrep){    tmp_rrep->dst_seq=htonl(tmp_rrep->dst_seq);    tmp_rrep->lifetime=htonl(tmp_rrep->lifetime);}/****************************************************   recv_rrep----------------------------------------------------Handles recieving route replies. It is passedpackets from the AODV thread****************************************************/int recv_rrep(struct event_queue_entry *working_packet){    struct route_table_entry *send_route;    struct route_table_entry *recv_route;    struct rrep *tmp_rrep;    struct interface_list_entry *tmp_interface;    u_int64_t        curr_time;    u_int32_t	tmp_ip=0;#ifdef MESSAGES    char rt_ip[16];#endif#ifndef TRACE    char dst_ip[16];    char src_ip[16];#endif    tmp_interface=find_interface_by_dev(working_packet->dev);    //check to make sure the interface found    if (tmp_interface==NULL)    {    #ifndef NO_ERROR        printk("RECV_RREP: Error finding interface RREP recieved on!\n");#endif        return 0;    }    tmp_ip=tmp_interface->ip;    tmp_rrep=working_packet->data;    convert_rrep_to_host(tmp_rrep);    // increase the hop count on the rrep    tmp_rrep->hop_count = tmp_rrep->hop_count + 1;#ifdef TRACE    if(tmp_rrep->src_ip != tmp_rrep->dst_ip)    {        strcpy(dst_ip,inet_ntoa(tmp_rrep->dst_ip));        strcpy(src_ip,inet_ntoa(tmp_rrep->src_ip));        printk("RREP: Received a RREP: src: %s dst: %s \n",src_ip,dst_ip);    }#endif    if (tmp_rrep->hop_count==1)    {        //its a hello messages! HELLO WORLD!        recv_hello(working_packet);        return 1;    }    if ((tmp_ip!=0) && (working_packet->src_ip==tmp_ip))    {    	#ifdef MESSAGES        printk("RREP: Reading our own mail! \n");	#endif        return 0;    }    update_route_entry(tmp_rrep->dst_ip,  working_packet->src_ip, tmp_rrep->hop_count, tmp_rrep->dst_seq, working_packet->dev);    delete_timer_queue_entry_of_id(tmp_rrep->dst_ip,EVENT_RREQ);    recv_route = find_route_table_entry(tmp_rrep->dst_ip);    /* Remove RREQ from resend-queue */    //we might need to forward it    if((tmp_rrep->src_ip != tmp_ip) && (tmp_rrep->src_ip!=tmp_rrep->dst_ip))        /* If I'm not the destination of the RREP I forward it */    {#ifndef TRACE        strcpy(src_ip,inet_ntoa(tmp_rrep->src_ip));        strcpy(dst_ip,inet_ntoa(tmp_rrep->dst_ip));        printk("RREP: Forward a route to: %s from node: %s \n",dst_ip,src_ip);#endif        /* Get the entry to the source from RT */        send_route = find_route_table_entry(tmp_rrep->src_ip);        if (send_route!=NULL)        {            /* Add to precursors... */            if (insert_precursor_entry(recv_route, send_route->next_hop) == 1)            {                /* Couldn't add precursor. Ignore and continue */            }            recv_route->lifetime = curr_time + ACTIVE_ROUTE_TIMEOUT;            convert_rrep_to_network(tmp_rrep);            send_message(send_route->next_hop,NET_DIAMETER, working_packet->data, working_packet->size);        }	#ifndef NO_ERROR        else            printk("RREP: ERROR finding sending address!\n");	#endif    }#ifdef TRACE    printk("REC_RREP: Exited\n");#endif    return 0;}/****************************************************   gen_rrep----------------------------------------------------Generates a route reply****************************************************/int gen_rrep(u_int32_t src_ip, u_int32_t dst_ip, u_int32_t packet_src_ip, u_int32_t dst_seq, int grat_rrep){    struct rrep      tmp_rrep;    struct route_table_entry  *src_route;    struct route_table_entry  *dst_route;    struct interface_list_entry *tmp_interface=NULL;    u_int64_t        curr_time;  /* Current time */#ifdef MESSAGES    char srcip[16];    char dstip[16];#endif    curr_time = getcurrtime(); /* Get current time */#ifdef TRACE    printk("GEN_RREP: Entered\n");#endif    /* Get the source and destination IP address from the RREQ */    src_route = find_route_table_entry(src_ip);    dst_route = find_route_table_entry(dst_ip);#ifdef TRACE    printk("GEN_RREP: Found routes\n");#endif    if (dst_route!=NULL)        tmp_interface=find_interface_by_dev(dst_route->dev);    else        tmp_interface=find_interface_by_dev(src_route->dev);#ifndef NO_ERROR    if (dst_route==NULL)        printk("GEN_RREP: DST route invalid!\n");    if(src_route==NULL)        printk("GEN_RREP: SRC route invalid!\n");#endif    if ((dst_ip != tmp_interface->ip) && (dst_ip != g_broadcast_ip) && (grat_rrep))    {        /* Now send a datagram to the requested host telling it it has been        asked for */#ifdef TRACE        printk("GEN_RREP: Sending out Grateitous RREP\n");#endif        tmp_rrep.type = 2;        tmp_rrep.r = 0;        tmp_rrep.prefix_sz = 0;        tmp_rrep.reserved1=0;        tmp_rrep.reserved2=0;        /* Set the source to be the destination of the rreq (it has asked for        it itself)*/        tmp_rrep.src_ip = dst_ip;        /* Insert the rreq's source attributes */        tmp_rrep.dst_ip = src_ip;        //we want it to be in network byte order!        tmp_rrep.dst_seq = htonl(src_route->dst_seq);        tmp_rrep.hop_count = src_route->hop_count;        //we want it to be in network byte order!        tmp_rrep.lifetime = htonl(src_route->lifetime - curr_time);        /* Get info on the destination */#ifdef TRACE        printk("GEN_RREP: Sending out Datagram!\n");#endif#ifdef MESSAGES        strcpy(srcip,inet_ntoa(tmp_rrep.src_ip));        strcpy(dstip,inet_ntoa(tmp_rrep.dst_ip));        printk("RREP: Sending out a Grat RREP -  src: %s dst: %s \n",srcip,dstip);#endif        send_message(dst_route->next_hop,NET_DIAMETER, &tmp_rrep, sizeof(struct rrep));    }    /* Check if the destination IP of RREQ was this node */    if (dst_ip == tmp_interface->ip)    {        /* The RREQ was for this node */        /* Set the reply structure */        if (seq_greater(dst_seq,dst_route->dst_seq))            dst_route->dst_seq = dst_seq;        //we want it to be in network byte order!        tmp_rrep.dst_seq = htonl(dst_route->dst_seq);        tmp_rrep.hop_count = 0;        //we want it to be in network byte order!        tmp_rrep.lifetime = htonl(MY_ROUTE_TIMEOUT);    }    else    {        /* The RREQ was for a node in RT */        /* Set the reply structure */        //we want it to be in network byte order!        tmp_rrep.dst_seq = htonl(dst_route->dst_seq);        tmp_rrep.lifetime = htonl(dst_route->lifetime - curr_time);        tmp_rrep.hop_count = dst_route->hop_count;        /* Add to precursors... */        insert_precursor_entry(dst_route, packet_src_ip);        insert_precursor_entry(src_route, dst_route->next_hop);    }    /* Set the rest of the RREP structure */    tmp_rrep.src_ip = src_ip;    tmp_rrep.dst_ip = dst_ip;    tmp_rrep.type = 2;    tmp_rrep.r = 0;    tmp_rrep.prefix_sz = 0;#ifdef TRACE    strcpy(srcip,inet_ntoa(src_ip));    strcpy(dstip,inet_ntoa(dst_ip));    printk("RREP: sending out a rrep -  src: %s dst: %s \n",srcip,dstip);#endif    send_message(packet_src_ip,NET_DIAMETER, &tmp_rrep, sizeof(struct rrep));#ifdef TRACE    printk("GEN_RREP: Exited\n");#endif    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -