LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-13-2011, 02:53 AM   #1
fahimeh_ak
LQ Newbie
 
Registered: Jul 2009
Posts: 13

Rep: Reputation: 0
NS2: How receive CBR packets at my own agent and duplicate them for new destination??


Hi
I created a new agent called SBC, and attach it to a node at the middle of CBR-source and sink. I want to receive every CBR packets in my own agent and after duplicating it for a new destination, forward the original message to it's destination.

So in my agent constructor definition, I used from following code:
Code:
/**********************************/
SBC2::SBC2() : Agent(PT_CBR) {

    dup_flag_=0;
//    set_pkttype(PT_CBR);
    puts("SBC2::SBC2\n");
}
/**********************************/
and I defines the recv() function as follow:

Code:
void SBC2::recv(Packet* p, Handler* h) {

   puts("\n\nReceived Packet\n\n");
   if (ch->ptype() == PT_CBR) {
      if (ih->saddr() == Sender() && ih->daddr() == Receiver() ) { 
	if (dup_flag_ == 1) {
   	   duplicate_data(p);//for destination true next SBC
        }
   return();
}

void SBC2::duplicate_data(Packet* p) {
//for next sbc
   puts("duplicate Packet \n");

   struct hdr_cmn* ch = HDR_CMN(p);
   struct hdr_ip* ih = HDR_IP(p);
   struct hdr_rtp *rh = hdr_rtp::access(p);

   Packet *np = allocpkt();
//   np=p; copy CBR packet to this new packet
   struct hdr_cmn* nch= HDR_CMN(np);
   struct hdr_ip* nih= HDR_IP(np);
   struct hdr_rtp *nrh = hdr_rtp::access(np);

   nch->direction() = ch->direction();
   nch->size() = ch->size();
   nch->error() = ch->error();
   nch->next_hop() = nextSBC_;  //Through new SBC
   nch->addr_type() = ch->addr_type();


   nih->saddr() = ih->saddr();
   nih->daddr() = newDest();   // To new IP
   nih->sport() = ih->sport();
   nih->dport() = ih->dport();
   nih->ttl() =ih->ttl();
   nih->flowid() = ih->flowid();

   nrh->seqno() = rh->seqno();

   Scheduler::instance().schedule(target_, p, JITTER);
}
but It doesn't work and doesn't receive any packet. I tested "PT_UDP" and "PT_RTP" as my agent packet type, too. what I must to do. Please help me. I have so little time and it'sso urgent for me

with thanks: F.Akbari
 
Old 08-13-2011, 03:35 AM   #2
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen, DK
Distribution: pclos2014, Slack14.1 DebWheezy, +50+ other Linux OS, for test only.
Posts: 13,522

Rep: Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503
Did you compile the code into the binary 'ns' ?

Assume code.cc : Make a copy of the Makefile, say "Makefile-2".

Add code.o to Makefile-2, at the end of section OBJ_CC.
( Line 330 - 331.)

cd ns-allinone-2.34/ns-2.34/ && make clean && make -f Makefile-2

..
 
Old 08-15-2011, 01:06 AM   #3
fahimeh_ak
LQ Newbie
 
Registered: Jul 2009
Posts: 13

Original Poster
Rep: Reputation: 0
I solved this problem!

Hi

I could solve the problem. to do that I used from following "SBC" agent definition:
Code:
#ifndef __fa_SBC_h__
#define __fa_SBC_h__

using namespace std;
#include "address.h"
#include <packet.h>
#include <agent.h>
#include <random.h>
#include <trace.h>
#include <map>
#include "ip.h"
#include "rtp.h"
#include "sip.h"
#include "sip-messages.h"
#include "tclcl.h"

#define JITTER (Random::uniform()*0.5)

//only for media stream packets
class SBC2 : public Agent {

   nsaddr_t  SBC_addr_;
   nsaddr_t  Sender_;
   nsaddr_t  Receiver1_; //if duplicate_flag==0 it's the main receiver
   nsaddr_t  Receiver2_;
   int dup_flag_;  //0: no duplication, 1: duplicate for new SBC,...
protected:
   NsObject* target1_;
   NsObject* target2_;

   inline nsaddr_t& SBC_addr() { return SBC_addr_; }
   inline nsaddr_t& Sender() { return Sender_; }
   inline nsaddr_t& Receiver1() { return Receiver1_; }
   inline nsaddr_t& Receiver2() { return Receiver2_; }

   inline NsObject* target1() { return target1_; }
   inline NsObject* target2() { return target2_; }

   void forward_data(Packet*);//for reciever1
   void duplicate_data(Packet*);//for reciever2
   void duplicate(int);//set dup_flag

public:
   SBC2(const char* ,const char* ,const char*);//snder,receiver1,receiver2
   int command(int, const char*const*);
   void recv(Packet*, Handler*);
};

#endif
Code:
//<fahimeh Akbari>

#include <iostream>
#include "fa_SBC.h"
#include "address.h"
#include "sip-proxy.h"
#include "SBC_Pkt.h"


int hdr_sbc_pkt::offset_;
static class SbcHeaderClass : public PacketHeaderClass {
public:
    SbcHeaderClass() : PacketHeaderClass("PacketHeader/SBC",sizeof(hdr_sbc_pkt)) {
  				      bind_offset(&hdr_sbc_pkt::offset_);  }
} class_rtProtoSbc_hdr;


static class SBC2Class : public TclClass {
public:
   SBC2Class() : TclClass("Agent/fa_SBC") {}
   TclObject* create(int argc, const char*const* argv) {
   return (new SBC2(argv[4],argv[5],argv[6]/*,argv[7]*/));
  }
} class_sipSBC;


SBC2::SBC2(const char* CBRsource,const char* CBRdest_main,const char* CBRdest_dup /*,const char* nextSBC*/) : Agent(PT_CBR) {
    Sender_= atoi(const_cast<char *>(CBRsource));
    Receiver1_= atoi(const_cast<char *>(CBRdest_main));
    Receiver2_= atoi(const_cast<char *>(CBRdest_dup));

    dup_flag_=0;
    puts("SBC2::SBC2 created successfully\n");
}


int SBC2::command(int argc, const char*const* argv) {
    if (argc == 2) {
        if (strcasecmp(argv[1], "start-duplication") == 0) {
           duplicate(1);
           return TCL_OK;
        }
        if (strcasecmp(argv[1], "stop-duplication") == 0) {
           duplicate(0);
           return TCL_OK;
        }
    }
    else if (argc == 3) {
	if(strcmp(argv[1], "target-node1") == 0) {
             target1_= (NsObject*) TclObject::lookup(argv[2]);
             if (!target1_){            
	         puts("Scheduler(target1): attempt to schedule an event with a NULL handler.");
	         puts("COMMAND !!!! \n");
                 return TCL_ERROR;
             }
             return TCL_OK;
        }
	if(strcmp(argv[1], "target-node2") == 0) {
             target2_= (NsObject*) TclObject::lookup(argv[2]);
             if (!target2_){            
	         puts("Scheduler(target1): attempt to schedule an event with a NULL handler.");
	         puts("COMMAND !!!! \n");
                 return TCL_ERROR;
             }
             return TCL_OK;
        }  

    }

    // Pass the command to the base class
    return Agent::command(argc, argv);
}

void SBC2::duplicate(int flag) {
   dup_flag_=flag;
   return;
}

void SBC2::recv(Packet* p, Handler* h) {
   struct hdr_cmn* ch = HDR_CMN(p);
   struct hdr_ip* ih = HDR_IP(p);
   puts("Recv ");
   if (ch->ptype() == PT_CBR) {
	if (dup_flag_ == 1) {
   	   duplicate_data(p->copy());     //for destination 2 true next SBC
           forward_data(p);               //for destination
	   return;
	}
        else {
           forward_data(p);//for destination 1
           return;
        }
    }
}


void SBC2::forward_data(Packet* p) {
   puts("forward ");
   struct hdr_ip* ih= HDR_IP(p);
   ih->daddr() = Receiver1();
   Scheduler::instance().schedule(target_, p, 0.0);
}

void SBC2::duplicate_data(Packet* p) {
   struct hdr_ip* ih= HDR_IP(p);
   ih->daddr() = Receiver2();
   Scheduler::instance().schedule(target_, p, 0.0);
}
and filally in my tcl script, I did something like this:
in my code the SBC1 must receive data and forward them for MH. If the command " #$ns_ at 40 "$SBC1 start-duplication" " is used, It duplicates data for MH2.
Code:
set MH1null [new Agent/Null]
set MH2null [new Agent/Null]

set SBC1 [new Agent/fa_SBC [AddrParams addr2id [$W(0) node-addr]] [AddrParams addr2id [$MH node-addr]] [AddrParams addr2id [$MH2 node-addr]] ]
$SBC1 target-node1 $MH    #the main target for sending data packets
$SBC1 target-node2 $MH2   ##the target for duplicating data packets

#$ns_ attach-agent $W(7) $SBC1    #node that must receive data and duplicate them for dup_destination
#$ns_ attach-agent $MH $MH1null   #main receiver
#$ns_ attach-agent $MH2 $MH2null  #duplicated data receiver

Last edited by fahimeh_ak; 08-16-2011 at 05:23 AM.
 
Old 08-15-2011, 01:53 AM   #4
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen, DK
Distribution: pclos2014, Slack14.1 DebWheezy, +50+ other Linux OS, for test only.
Posts: 13,522

Rep: Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503
Code tags : It's a slash " / " , not a backslash.

Must be : [/code].




P.S. : Good to see, that you found a solution.

..

Last edited by knudfl; 08-15-2011 at 02:01 AM.
 
Old 08-16-2011, 05:24 AM   #5
fahimeh_ak
LQ Newbie
 
Registered: Jul 2009
Posts: 13

Original Poster
Rep: Reputation: 0
Thank you knudfl.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot create CBR traffic in ns2 Cataj Linux - Newbie 21 08-13-2009 12:04 AM
Question about Destination 127.0.0.0/8 Packets Sent to Non-loopback Interfaces blackhole54 Linux - Networking 2 12-15-2008 10:59 PM
Cannot receive any packets through ppp0 !! mister_0101 Linux - Networking 1 05-06-2005 05:52 PM
Installed 2nd BCM5700 NIC, when Pinging on this subnet receive destination host unrea timhe Linux - Networking 2 05-12-2004 09:52 AM
userspace program for forwarding packets after amendment of the destination address becky_starr Linux - Software 0 01-12-2004 06:22 AM


All times are GMT -5. The time now is 02:27 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration