How to perform Safety Message Broadcasting in VANET using NS2?

Description

In VANET safety message broadcast is the essential functionality. The code segment given below provides the data broadcast to neighbors by a source node using GPSR routing protocol.

  • TCL part :

    # Data Transmission from source to destination

    set udp [new Agent/UDP]
    $ns_ attach-agent $node_($source) $udp
    set null [new Agent/Null]
    $ns_ attach-agent $node_($destination) $null
    $ns_ connect $udp $null
    set cbr [new Application/Traffic/CBR]
    $cbr attach-agent $udp
    $cbr set packetSize_ 512
    $cbr set interval_ 1.0
    $ns_ at 28.0   "$cbr start"
    $ns_ at 28.1   "$ns_ trace-annotate \" Broadcasting the Data from source to neighbor nodes\""
    $ns_ at 28.5   "$cbr stop"

  • C++ part :

    void GPSRAgent::send_broadcast(Packet *p) {
    struct hdr_cmn *cmh = HDR_CMN(p);
    struct hdr_ip *iph = HDR_IP(p);
    struct hdr_gpsr_send *gsh = HDR_GPSR_SEND(p);
    cmh->next_hop_ = IP_BROADCAST;
    cmh->last_hop_ = my_id_;
    cmh->addr_type_ = NS_AF_INET;
    iph->daddr() = IP_BROADCAST;
    iph->saddr() = my_id_;
    iph->sport() = RT_PORT;
    iph->dport() = RT_PORT;
    iph->ttl_    = IP_DEF_TTL;
    gsh->type_ = GPSRTYPE_SEND;
    gsh->x_ = (float)my_x_;
    gsh->y_ = (float)my_y_;
    send(p, 0);
    }