ns2中出现分段故障

segmentation fault in ns2

本文关键字:分段 故障 ns2      更新时间:2023-10-16

我正在研究ns2…在aodv中做了一些改变。Cc并添加了一些我自己的功能

void nb_traffic_update(int id,struct nb_traffic_stat **nblist,int nid,int flag,int hop_count)

. .为了检测天坑攻击,当我运行具有少量节点的代码时,我得到了结果,但当我增加节点数量时,我得到了分割错误。这是我的nb_traffic.h文件

    struct nb_traffic_stat
    {
        int id;
        int recvrequest;
        int routereply;
        int no_of_hops;
        //int no_of_updation;
        struct nb_traffic_stat *next;
    };
    struct traffic_stat
    {
       int id;
       struct nb_traffic_stat **list;
       struct traffic_stat *next;
    };
    struct ftraffic_stat
    {
       int sendrequest;
       int routereply;
    };

aodv.cc的修改

    struct traffic_stat *tlist=NULL,*ttail=NULL;
    void
    AODV::recvReply(Packet *p) {
    ...
    if (ih->daddr() == index) { // If I am the original source
    .....
    nb_traffic_update(index,&nblist,ih->saddr(),1,rp->rp_hop_count);//1 is for   receiving the route reply
     }
    } 
    void
    AODV::recvRequest(Packet *p) {
    ....
    /*after ensuring this is the new routerequest*/
    struct hdr_cmn *ch = HDR_CMN(p);
    if(ch->num_forwards()==1)
    {
       nb_traffic_update(index,&nblist,rq->rq_src,0,0);//0 is for receiving the request
    }
   }

我的邻居交通更新功能

   void nb_traffic_update(int id,struct nb_traffic_stat **nblist,int nid,int flag,int  hop_count)
    {
       int n;
       //printf("inside nb_traffic_update:%dn",id);
       if(*nblist==NULL)
       {
           struct nb_traffic_stat *ptr;
           ptr=(struct nb_traffic_stat*)malloc(sizeof(struct nb_traffic_stat));
           ptr->id=nid;
           ptr->next=NULL;
           if(flag==0)
           {
         ptr->recvrequest=1;
             ptr->routereply=0;
             ptr->no_of_hops=0;
            //ptr->no_of_updation=0;
           }
           else 
           {
             ptr->recvrequest=0;
             ptr->routereply=1;
             ptr->no_of_hops=hop_count;
             //ptr->no_of_updation=1;
           }
           *nblist=ptr;
           struct traffic_stat *sptr;
           sptr=tlist;
           while(sptr!=NULL&&sptr->id!=id)
           {
              sptr=sptr->next;
           }
           assert(sptr!=NULL);
           sptr->list=nblist;
      }
     else
     {
        int found=0;
        struct nb_traffic_stat *tptr,*prevtptr;
        tptr=*nblist;
        while(tptr!=NULL&&tptr->id<=nid)
        {
            if(tptr->id==nid)
           {
              found=1;
              break;
           }
           prevtptr=tptr;
           tptr=tptr->next;
        }
        if(found)
        {
          if(flag==0)
          {
           tptr->recvrequest++;
          }
          else 
          {
              tptr->routereply++;
              tptr->no_of_hops=hop_count;
              //tptr->no_of_updation++;
          }
        }
        else
        {
            struct nb_traffic_stat *ptr;
            ptr=(struct nb_traffic_stat*)malloc(sizeof(struct nb_traffic_stat));
            ptr->id=nid;
            if(flag==0)
            {
          ptr->recvrequest=1;
              ptr->routereply=0;
              ptr->no_of_hops=0;
              //ptr->no_of_updation=0;
            }
            else 
            {
              ptr->recvrequest=0;
              ptr->routereply=1;
              ptr->no_of_hops=hop_count;
              //ptr->no_of_updation=1;
            }
            ptr->next=prevtptr->next;
            prevtptr->next=ptr;
         }
        } 
       }

您没有在nb_traffic_update(int, nb_traffic_stat**, int, int, int)函数中检查nblist为NULL,从而导致段故障。

也在条件语句if (*nblist==NULL)中,您正在执行:*nblist=ptr;。这意味着*NULL = ptr;可能导致段故障

使用gdb运行tcl,它将显示导致段错误的函数…

for example, gdb -args ns path/to/tcl/script。tcl