How to write a flooding module for wireless simulations?

 

2

 

1

 

3

 

4

 

5

 

6

 

7

 

8

 

From the above setting, we can see that if we transmit packets by “1.0.1.255”, only one hop node from the sender can receive packets. That means node 2 can get the packets sent by node 1. But node 3 and node 4 cannot receive the packets even if the destination address is a broadcast address.

 

Therefore, I wrote a flooding module. Please put the following codes under NCTUns-6.0/src/nctuns/module/route

(myflood.h)

#ifndef     __NCTUNS_myflood_h__

#define __NCTUNS_myflood_h__

 

#include <event.h>

#include <object.h>

#include <mylist.h>

 

class Packet;

 

class myflood : public NslObject {

 

 private:

       int                   ttl_;       

 

 public:

       

       myflood(u_int32_t type, u_int32_t id, struct plist* pl, const char *name);

       ~myflood();

       

        int                    init();      

       int                    recv(ePacket_ *pkt);

       int                    send(ePacket_ *pkt);

};

 

 

#endif      /* __NCTUNS_myflood_h__ */

 

(myflood.cc)

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <fcntl.h>

#include <sys/uio.h>

#include <unistd.h>

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <assert.h>

 

#include <nctuns_api.h>

#include <ethernet.h>

#include <ip.h>

#include <packet.h>

#include <route/myflood.h>

#include <exportStr.h>

 

 

MODULE_GENERATOR(myflood);

 

 

myflood::myflood(u_int32_t type, u_int32_t id, struct plist* pl, const char *name)

                : NslObject(type, id, pl, name)

{

        s_flowctl = DISABLED;

        r_flowctl = DISABLED;

      

        vBind("TTL", &ttl_);

    ttl_=2;  // 2 means 2 hops away from sender. If the packets need to be sent more hops, a higher value should be set.

}

 

 

myflood::~myflood() {

 

}

 

int myflood::init() {

 

  return (1);

}

 

int myflood::send(ePacket_ *pkt) {

 

        Packet                      *p;

        u_long                     dst;

        struct ip               *iph;      

 

        GET_PKT(p, pkt);

        iph=(struct ip *)p->pkt_sget();

        if(iph!=NULL)

          iph->ip_ttl=ttl_;

        //printf("S:TTL=%d, ttl_=%d\n", iph->ip_ttl, ttl_);     

 

  /* get destination ip address. */

  IP_DST(dst, p->pkt_sget());

 

        /* check to see if the broadcast address */

  if (is_ipv4_broadcast(get_nid(), dst)){

     //printf("S:This is a broadcast packet\n");

     return(put(pkt, sendtarget_));

  } else {

     //printf("S:This is not a broadcast packet and must be dropped\n");

     freePacket(pkt);

     return(1);

  }

}

 

int myflood::recv(ePacket_ *pkt) {

 

        Packet                      *p, *myp;

        u_long                     dst;

        ePacket_  *dupP;

        struct ip   *iph;

  

        assert(pkt&&(p=(Packet *)pkt->DataInfo_));

   

        /* get destination ip address */

        IP_DST(dst, p->pkt_sget());

 

        iph=(struct ip *)p->pkt_sget();

    

        /* filter destination address */

        if(is_ipv4_broadcast(get_nid(), dst)) {

                /* if the destination == myip, pass

                 * to upper layer

                 */

                dupP=pkt_copy(pkt);

                put(pkt, recvtarget_);

               

                /* flood the broadcast packet again */

                GET_PKT(myp, dupP);

                myp->pkt_setflow(PF_SEND);

                u_char ttl;

 

                GET_IP_TTL(ttl, myp->pkt_sget());

                //printf("R:TTL=%d\n", ttl);

                if(ttl>1) {

                  IP_DEC_TTL(myp->pkt_sget());

                  //GET_IP_TTL(ttl, myp->pkt_sget());

                  //printf("R:forward the packet again, TTL=%d\n", ttl);

                  return (put(dupP, sendtarget_));

                } else {

                    freePacket(dupP);

                    return(1);

                }   

        }

        else {      

          printf("R:This is not a broadcast packet and must be dropped\n");

          freePacket(pkt);

          return(1); 

        }

}

 

 

9

 

10

 

11

 

12

 

Now repeat the above simulation setting, but before click Simulation/Run, modify the test3.tcl (assume that the project name is test3)

13

 

14

 

15

 

16

 

17

 

18

 

19

 

20

 

Now click Simulation/Run to start simulation. After simulation, you can see like the following figure.

 

21

 

You can also find that pkt2, pkt3, thr2, and thr3 contain information in those files. But Node 4 still gets nothting.

This means that myflood module can help send packets 2 hops away from the sender.

You can change the ttl_ value in myflooding.cc or add the command like the following setting to control the transmission distance.

 

Module myflood : Node1_myflood_LINK_1

   Set Node1_myflood_LINK_1.TTL = 3

 

Dr. Chih-Heng Ke (http://csie.nqu.edu.tw/smallko), smallko@gmail.com

Department of Computer Science and Information Engineering, National Quemoy University, Taiwan