Applicable VersionsNetSim StandardNetSim Pro


Rebroadcasting in NetSim:

To implement this project in NetSim, we have created Rebroadcast.c file inside the Application project. The file contains the following functions:

  • void rebroadcast_packet();

This function is used to rebroadcast the packet

  • static bool isRebroadcastAllowed();

This function is used to check whether rebroadcasting is allowed or not

  • void rebroadcast_add_packet_to_info();

This function is used to add the packet to the rebroadcast list

  • static void cleanup_broadcast_info();

This function is used to clean the broadcast information


Note: scroll down to see code changes for appropriate releases


Applicable Releasesv13.0


  • Open NetSim source code in Visual Studio 2019 by clicking on the Open Code button present in NetSim Home Screen via Your Work->Workspace Options->Open Code
  • Changes to int fn_NetSim_Application_Run()function in the APPLICATION_IN_EVENT, in Application.c file, within Application project 

/*This is used to generate next broadcast packet if the current device is present in the source list*/ 

_declspec (dllexport) int fn_NetSim_Application_Run() { switch(pstruEventDetails->nEventType)

case APPLICATION_OUT_EVENT: 

handle_app_out(); 

break; 

  • case APPLICATION_IN_EVENT: 

NetSim_PACKET* pstruPacket=pstruEventDetails->pPacket; 

if(pstruPacket->nPacketType != PacketType_Control && pstruPacket->pstruAppData- >nApplicationId && pstruPacket->nControlDataType/100 != PROTOCOL_APPLICATION) 

ptrAPPLICATION_INFO pstruappinfo; 

fnValidatePacket(pstruPacket); 

pstruappinfo=applicationInfo[pstruPacket->pstruAppData->nApplicationId-1]; 

pstruPacket->pstruAppData->dEndTime = pstruEventDetails->dEventTime; 

fn_NetSim_Application_Plot(pstruPacket); 

#ifdef REBROADCAST 

if (pstruappinfo->sourceList[0] == pstruPacket->nSourceId) 

#endif 

appmetrics_dest_add(pstruappinfo, pstruPacket, pstruEventDetails->nDeviceId); 

if(pstruappinfo->nAppType==TRAFFIC_PEER_TO_PEER && pstruPacket->pstruAppData- >nAppEndFlag==1) 

fn_NetSim_Application_P2P_MarkReceivedPacket(pstruappinfo,pstruPacket); fn_NetSim_Application_P2P_SendNextPiece(pstruappinfo,get_first_dest_from_packet(pstruPacket ),pstruEventDetails->dEventTime); 

}

  • Changes to handle_app_out() function, in APP_OUT.c file, within Application project

/*The code checks if the destination is ‘0’ i.e., Broadcast packet, then it adds the packet to rebroadcast list*/ 

//Fragment the packet 

int nSegmentCount = 0; 

double segmentsize = fn_NetSim_Stack_GetMSS(pstruPacket);

nSegmentCount = fn_NetSim_Stack_FragmentPacket(pstruPacket, (int)fn_NetSim_Stack_GetMSS(pstruPacket)); 

//add rebroadcast 

#ifdef REBROADCAST 

if (appInfo->sourceList[0] == pstruEventDetails->nDeviceId) 

#endif 

set_app_end_and_generate_next_packet(pstruPacket, otherDetails, destCount, dest); 

//Add the dummy payload to packet 

fn_NetSim_Add_DummyPayload(pstruPacket, appInfo);

#ifdef REBROADCAST 

if (appInfo->sourceList[0] == pstruEventDetails->nDeviceId) 

#endif 

appmetrics_src_add(appInfo, pstruPacket); 

appout_send_packet(s, appInfo, pstruPacket, nDeviceId); 

#ifdef REBROADCAST 

if (!dest[0]) 

rebroadcast_add_packet_to_info(pstruPacket, pstruEventDetails->dEventTime); 

#endif 

// REBROADCAST

 }


  • Changes to int fn_NetSim_Application_Run()function in the APPLICATION_IN_EVENT, in Application.c file, within Application project

/* It checks whether the destination is ‘0’ or not. If it is ‘0’, then it rebroadcasts the packet or else deletes the packet.*/ 

#ifdef REBROADCAST 

if (pstruappinfo->sourceList[0] == pstruPacket->nSourceId) 

#endif 

appmetrics_dest_add(pstruappinfo, pstruPacket, pstruEventDetails->nDeviceId); 

if(pstruappinfo->nAppType==TRAFFIC_PEER_TO_PEER && pstruPacket->pstruAppData- >nAppEndFlag==1) 

fn_NetSim_Application_P2P_MarkReceivedPacket(pstruappinfo,pstruPacket); fn_NetSim_Application_P2P_SendNextPiece(pstruappinfo,get_first_dest_from_packet(pstruPacke t),pstruEventDetails->dEventTime); 

if(pstruappinfo->nAppType == TRAFFIC_EMULATION && pstruPacket->szPayload) 

fn_NetSim_Dispatch_to_emulator(pstruPacket);

 } 

if (pstruappinfo->nAppType == TRAFFIC_BSM_APP) 

process_saej2735_packet(pstruPacket); 

#ifdef REBROADCAST 

UINT destCount; 

NETSIM_ID* dest = get_dest_from_packet(pstruPacket, &destCount); 

if (!dest[0]) 

rebroadcast_packet(pstruPacket, pstruEventDetails->nDeviceId, pstruEventDetails->dEventTime);

 } 

else 

#elif 

//Delete the packet 

fn_NetSim_Packet_FreePacket(pstruPacket); 

//add 

#endif 

// REBROADCAST 

#ifdef REBROADCAST 

#endif 

}


  • Added the following function declarations in the Application.h file, within the Application project

 

int fn_NetSim_Add_DummyPayload(NetSim_PACKET* packet, ptrAPPLICATION_INFO); 

//Encryption char xor_encrypt(char ch, long key); 

int aes256(char* str, int* len); 

int des(char* buf, int* len); 

//Application event handler 

void handle_app_out(); 

#define REBROADCAST 

void rebroadcast_add_packet_to_info(NetSim_PACKET* packet, double time); 

void rebroadcast_packet(NetSim_PACKET* packet, NETSIM_ID devId, double time); 

#endif 


Also refer: MANET_VANET-Rebroadcast _V13.0.pdf (tetcos.com) 


Applicable Releases

v11.1

v12



Code modifications done in NetSim:

  • Open NetSim source code in Visual Studio 2019 by clicking on the Open Code button present in NetSim Home Screen via Open Simulation->Workspace Options->Open Code
  • We have added the following lines of code(in red) in the handle_app_out() function in the APP_OUT.c file inside the Application project. This is used to generate the next packet if the current device is present in the source list.

pstruPacket->pstruTransportData->nTransportProtocol = appInfo->trxProtocol;


    //Fragment the packet

    int nSegmentCount = 0;

    double  segmentsize = fn_NetSim_Stack_GetMSS(pstruPacket);

    nSegmentCount = fn_NetSim_Stack_FragmentPacket(pstruPacket, (int)fn_NetSim_Stack_GetMSS(pstruPacket));


    set_app_end_and_generate_next_packet(pstruPacket, otherDetails, destCount, dest);


    //Add the dummy payload to packet

    fn_NetSim_Add_DummyPayload(pstruPacket, appInfo);


#ifdef REBROADCAST

    if(appInfo->sourceList[0]== pstruEventDetails->nDeviceId)

#endif


    appmetrics_src_add(appInfo, pstruPacket);



  • We have added the following lines of code in the handle_app_out() function in the APP_OUT.c file inside the Application project. If the destination is 0 i.e., broadcast then it adds the packet to the rebroadcast list

//Fragment the packet

    int nSegmentCount = 0;

    double  segmentsize = fn_NetSim_Stack_GetMSS(pstruPacket);

    nSegmentCount = fn_NetSim_Stack_FragmentPacket(pstruPacket, (int)fn_NetSim_Stack_GetMSS(pstruPacket));


    set_app_end_and_generate_next_packet(pstruPacket, otherDetails, destCount, dest);


    //Add the dummy payload to packet

    fn_NetSim_Add_DummyPayload(pstruPacket, appInfo);


#ifdef REBROADCAST

    if(appInfo->sourceList[0]== pstruEventDetails->nDeviceId)

#endif


    appmetrics_src_add(appInfo, pstruPacket);

#ifdef REBROADCAST

    if (!dest[0])

        rebroadcast_add_packet_to_info(pstruPacket, pstruEventDetails->dEventTime);

#endif //REBROADCAST


    appout_send_packet(s, appInfo, pstruPacket, nDeviceId);

}



  • Added the following lines of code in fn_NetSim_Application_Run() function in APPLICATION_IN_EVENT present in Application.c file inside Application project. It checks whether the destination is 0 or not. If it is 0, then it rebroadcasts the packet otherwise deletes the packet

if (pstruappinfo->nAppType == TRAFFIC_EMULATION && pstruPacket->szPayload)

                {

                    fn_NetSim_Dispatch_to_emulator(pstruPacket);

                }

                if (pstruappinfo->nAppType == TRAFFIC_BSM_APP)

                {

                    process_saej2735_packet(pstruPacket);

                }

#ifdef REBROADCAST

                UINT destCount;

                NETSIM_ID* dest = get_dest_from_packet(pstruPacket, &destCount);

                if (!dest[0])

                {

                    rebroadcast_packet(pstruPacket, pstruEventDetails->nDeviceId, pstruEventDetails->dEventTime);

                }

                else

                {

#elif

                //Delete the packet

                fn_NetSim_Packet_FreePacket(pstruPacket);

#endif //rebroadcast

#ifdef REBROADCAST

                }

#endif

            }

            // Here which type is placed is only getting processed next one is not getting processed

            else if (pstruPacket->nControlDataType == packet_COAP_REQUEST)

            {

                ptrAPPLICATION_INFO pstruappinfo;



  • Added the function declarations in the Application.h file


//Encryption

char xor_encrypt(char ch, long key);

int aes256(char* str, int* len);

int des(char* buf, int* len);


/****REBROADCAST****/

#define REBROADCAST

void rebroadcast_add_packet_to_info(NetSim_PACKET* packet, double time);

void rebroadcast_packet(NetSim_PACKET* packet, NETSIM_ID devId, double time);

#endif


//Application event handler

void handle_app_out();




  • Right-click on Application project, select Add->New Item. Enter the name as Rebroadcasting.c and click on Add button.


  • Copy paste the below code in Rebroadcasting.c file

#include "main.h"

#include "Application.h"


#define REBROADCAST_PROBABILITY 1

#define MAX_WAIT_FOR_REBROADCAST (10*SECOND)


static bool isRebroadcast()

{

    double d = NETSIM_RAND_01();

    if (d <= REBROADCAST_PROBABILITY)

        return true;

    else

        return false;

}


typedef struct stru_broadcast_info

{

    NETSIM_ID appId;

    NETSIM_ID srcId;

    UINT64 packetId;

    double dBroadcastTime;

    struct stru_broadcast_info* next;

}BROADCASTINFO, * ptrBROADCASTINFO;

static ptrBROADCASTINFO headInfo = NULL;

static ptrBROADCASTINFO tailInfo = NULL;


static void cleanup_broadcast_info(double time)

{

    ptrBROADCASTINFO b = headInfo;

    while (b)

    {

        if (time - b->dBroadcastTime > MAX_WAIT_FOR_REBROADCAST)

        {

            headInfo = b->next;

            if (!headInfo)

                tailInfo = NULL;

            free(b);

            b = headInfo;

        }

        else

        {

            break;

        }

    }

}


void rebroadcast_add_packet_to_info(NetSim_PACKET* packet,

    double time)

{

    cleanup_broadcast_info(time);

    ptrBROADCASTINFO b = calloc(1, sizeof * b);

    b->appId = packet->pstruAppData->nApplicationId;

    b->dBroadcastTime = time;

    b->packetId = packet->nPacketId;

    b->srcId = packet->nSourceId;

    if (headInfo)

    {

        tailInfo->next = b;

        tailInfo = b;

    }

    else

    {

        headInfo = b;

        tailInfo = b;

    }

}


static bool isRebroadcastAllowed(NetSim_PACKET* packet,

    NETSIM_ID devId)

{

    if (!isRebroadcast())

        return false;


    ptrBROADCASTINFO t = headInfo;

    while (t)

    {

        if (t->appId == packet->pstruAppData->nApplicationId &&

            t->packetId == packet->nPacketId &&

            t->srcId == devId)

            return false;

        t = t->next;

    }

    return true;

}



static NetSim_PACKET* reset_packet(NetSim_PACKET* packet,

    NETSIM_ID newSrc,

    double time)

{

    NetSim_PACKET* newPacket = fn_NetSim_Packet_CreatePacket(APPLICATION_LAYER);

    newPacket->dEventTime = time;

    newPacket->nSourceId = newSrc;

    newPacket->pstruNetworkData->szSourceIP = DEVICE_NWADDRESS(newSrc, 1);

    add_dest_to_packet(newPacket, 0);

    newPacket->pstruNetworkData->szDestIP = IP_COPY(packet->pstruNetworkData->szDestIP);

    newPacket->nControlDataType = packet->nControlDataType;

    newPacket->nPacketId = packet->nPacketId;

    newPacket->nPacketPriority = packet->nPacketPriority;

    newPacket->nPacketType = packet->nPacketType;

    newPacket->nQOS = packet->nQOS;

    newPacket->nServiceType = packet->nServiceType;

    strcpy(newPacket->szPacketType, packet->szPacketType);

    newPacket->pstruAppData->dPayload = packet->pstruAppData->dPayload;

    newPacket->pstruAppData->nApplicationId = packet->pstruAppData->nApplicationId;

    newPacket->pstruAppData->nAppType = packet->pstruAppData->nAppType;

    newPacket->pstruTransportData->nDestinationPort = packet->pstruTransportData->nDestinationPort;

    newPacket->pstruTransportData->nSourcePort = packet->pstruTransportData->nSourcePort;

    newPacket->pstruNetworkData->nTTL = MAX_TTL;

    return newPacket;

}


void rebroadcast_packet(NetSim_PACKET* packet,

    NETSIM_ID devId,

    double time)

{

    if (!isRebroadcastAllowed(packet, devId))

    {

        fn_NetSim_Packet_FreePacket(packet);

        return;

    }


    NetSim_PACKET* newPacket = reset_packet(packet, devId, time);

    fn_NetSim_Packet_FreePacket(packet);


    pstruEventDetails->nEventType = APPLICATION_OUT_EVENT;

    pstruEventDetails->dPacketSize = newPacket->pstruAppData->dPayload;

    pstruEventDetails->szOtherDetails = applicationInfo[newPacket->pstruAppData->nApplicationId - 1];

    pstruEventDetails->pPacket = newPacket;

    pstruEventDetails->nProtocolId = PROTOCOL_APPLICATION;

    pstruEventDetails->nApplicationId = NETWORK->nApplicationCount + 1;

    NETWORK->nApplicationCount += 1;

    fnpAddEvent(pstruEventDetails);

}


  • Set Win32 or x64 according to the NetSim build which you are using.


  • Right-click on Application project in Solution Explorer and select rebuild solution.


  • Upon rebuilding, libApplication.dll will get updated in the bin folder of the current workspace.
  •  Open NetSim and create a scenario as shown below and run the simulation for 10s.

  • In the above scenario, Node-1 is broadcasting the packet and it is received by nodes 2, 3, and 4. Now nodes 2, 3, and 4 will rebroadcast the same packet based on the probability value in Rebroadcast.c file.
  • After simulation, open Packet Trace and filter Packet_Id to 3 or any other id and observe that the nodes other than the source will rebroadcast the same packet



Applicable Releasesv10.2


Broadcasting:

Broadcasting is the process of sending a message from one node to all other nodes in an ad hoc network. It is a fundamental operation for communication in ad hoc networks as it allows for the update of network information and route discovery.


Attached herewith is a Rebroadcasting.zip folder which contains Code and Configuration file folders. The source codes present in Code folder can be loaded in Visual Studio using the NetSim.sln file. Users can then browse through the various associated files that are part of the Application project. 

Rebroadcasting:

Wireless Node A initiates a broadcast of a message and the message is received by nodes B, C and D. B, C and D rebroadcast the message if they have not broadcasted that before. Furthermore, this implementation involves a Rebroadcast_probability based on which the nodes resend the packets.

Probability based rebroadcasting - the decision of rebroadcast is based upon a random probability. This probability may be as simple as flipping a coin or it may be more complex involving probabilities which include parameters such as node density, duplicate packets received, battery power or a nodes participation within the network etc. Users can change the Rebroadcast probability macro present in Rebroadcast.c file shown below 

Rebroadcasting in NetSim:

To implement this project in NetSim, we have created Rebroadcast.c file inside Application project. The file contains the following functions:

  • void rebroadcast_packet();

This function is used to rebroadcast the packet

  • static bool isRebroadcastAllowed();

This function is used to check whether rebroadcasting is allowed or not

  • void rebroadcast_add_packet_to_info();

This function is used to add the packet to rebroadcast rebroadcast list

  • static void cleanup_broadcast_info();

This function is used to clean the broadcast information

Code modifications done in NetSim:

  • We have added the following lines of code in fn_NetSim_Application_Run() function in APPLICATION_OUT_EVENT present in Application.c file inside Application project. This is used to generate next packet if the current device is present in source list.

  • We have added the following lines of code in fn_NetSim_Application_Run() function in APPLICATION_OUT_EVENT present in Application.c file inside Application project. If the destination is 0 i.e., broadcast then it adds the packet to rebroadcast list

  • Added the following lines of code in fn_NetSim_Application_Run() function in APPLICATION_IN_EVENT present in Application.c file inside Application project. It checks whether the destination is 0 or not. If it is 0, then it rebroadcasts the packet otherwise deletes the packet

  • Added the function declarations in Application.h file

Steps:  

  • After you unzip the file, the folder would look like

  • Open Code folder and double click on the NetSim.sln file present to open the project in Visual Studio 2017.

  • Right click on Solution in Solution Explorer and select rebuild solution.

  • Upon rebuilding, libApplication.dll will get created in the DLL folder
  • Now copy the libApplication.dll from this DLL folder and paste it in NetSim bin folder present in the NetSim installation directory. The NetSim install directory would look something like < C:\Program Files (x86)\NetSim Standard\bin>
  • Note that there exists libApplication.dll in this bin folder. This is the default file being shipped with NetSim. The user is replacing this file with the newly built file.
  • Therefore, take care to rename the original libApplication.dll file, so that it isn’t lost. For example, you may rename it as libApplication_default.dll.
  • Run NetSim and open Configuration.netsim file present inside the Configuration file folder and run the simulation for 10 seconds.

  • In the above scenario Node-A is broadcasting the packet and it is received by the nodes B, C and D. Now nodes B, C and D will rebroadcast the same packet based on the probability value in Rebroadcast.c file.
  • After simulation, open Packet Trace and filter Packet_Id to 3 or any other id and observe that the nodes other than source will rebroadcast the same packet

  • Note that Users SHOULD NOT use the performance metrics provided at the end of simulation but should rather calculate the network performance metrics from the packet trace
  • Users can also create their own network scenarios in Single MANET/VANET and run simulation.