LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   ns2 script issue (https://www.linuxquestions.org/questions/linux-software-2/ns2-script-issue-4175479297/)

sumegha13 10-02-2013 03:36 AM

ns2 script issue
 
Hello Guys,

While creating new protocol in ns2 i am getting the below error.Anyone knows the issue?

ns:
[code omitted because of length]
: invalid command name "Agent/security"
while executing
"Agent/security set accessible_var_ true"

TB0ne 10-02-2013 09:17 AM

Quote:

Originally Posted by sumegha13 (Post 5038464)
Hello Guys,
While creating new protocol in ns2 i am getting the below error.Anyone knows the issue?

ns:
[code omitted because of length]
: invalid command name "Agent/security"
while executing
"Agent/security set accessible_var_ true"

Yep...you have error(s) in your script. Since you don't post it, or give any details at ALL about what you're doing/trying, what do you think we'll be able to tell you?

sumegha13 10-05-2013 11:40 AM

Hello,

I am trying to add new encryption / decryption function in ns2.I have tried all the steps to add the new protocol in ns2,but it didn't work so if anyone knows that steps than can you pls tell me from start.As i am new to ns2 i don't have that much idea of how to make new protocol or add new things in ns2.

I am placing the code of my three files
1)security_packet.cc
2)security_packet.h
3)security.tcl

security_packet.cc
Code:

#include "security_packet.h"
#include "string.h"

int hdr_security_packet::offset_;
static class Security_packetHeaderClass : public PacketHeaderClass {
public:
        Security_packetHeaderClass() : PacketHeaderClass("PacketHeader/Security_packet",sizeof(hdr_security_packet)) {
                bind_offset(&hdr_security_packet::offset_);
        }
} class_security_packethdr;


static class Security_packetClass : public TclClass {
public:
        Security_packetClass() : TclClass("Agent/Security_packet") {}
        TclObject* create(int, const char*const*) {
                return (new Security_packetAgent());
        }
} class_security_packet;


Security_packetAgent::Security_packetAgent() : Agent(PT_SECURITY_PACKET), seq(0), oneway(0)
{
        bind("packetSize_", &size_);
}

int Security_packetAgent::command(int argc, const char*const* argv)
{

if (argc ==3) {

    if (strcmp(argv[1], "send") == 0) {
      // Create a new packet
      Packet* pkt = allocpkt();
      // Access the security packet header for the new packet:
      hdr_security_packet* hdr = hdr_security_packet::access(pkt);
      // Set the 'ret' field to 0, so the receiving node
      // knows that it has to generate an acknowledge packet
      hdr->ret = 0;
      hdr->seq = seq++;
      // Store the current time in the 'send_time' field
      hdr->send_time = Scheduler::instance().clock();
      // copy date to be sent to header
      strcpy(hdr->data, argv[2]);
      //----------------hashing------------------------
      hdr->hashvalue = hashing(hdr->data,(unsigned int)strlen(hdr->data));
      printf("Message sent %s with hashing %d\n",hdr->data,hdr->hashvalue);
      // ---------- encrypt the data ---------------
      encryption(hdr->data);
      //-----------------------------------
      // Send the packet
      send(pkt, 0);
      // return TCL_OK, so the calling function knows that
      // the command has been processed
      return (TCL_OK);   
    }   
    else if (strcmp(argv[1], "start-WL-brdcast") == 0) {
      Packet* pkt = allocpkt();
     
      hdr_ip* iph = HDR_IP(pkt);
      hdr_security_packet* ph = hdr_security_packet::access(pkt);
      strcpy(ph->data, "test");
     
      iph->daddr() = IP_BROADCAST;
      iph->dport() = iph->sport();
      ph->ret = 0;
      send(pkt, (Handler*) 0);
      return (TCL_OK);
    }

    else if (strcmp(argv[1], "oneway") == 0) {
      oneway=1;
      return (TCL_OK);
    }
  }
 
  // If the command hasn't been processed by SecurityAgent()::command,
  // call the command() function for the base class
  return (Agent::command(argc, argv));
}
// -- CESAR encryption function ----------
void Security_packetAgent::encryption(char out[])
{
        int key =3;
        int i=0;
                for (i=0;i<strlen(out);i++)
        {
                out[i]=(out[i]+key)%128;
        }
}
// ---- CESAR decryption  ------------------
void Security_packetAgent::decryption(char out[])
{
        int key =3;
        int i=0;
                for (i=0;i<strlen(out);i++)
        {
                out[i]=(out[i]-key)%128;
        }
       
}
//---------------hashing fucntion-------------
unsigned int Security_packetAgent::hashing(char value[], unsigned int len)
{
  char *word = value;
  unsigned int ret = 0;
  unsigned int i; 
  for(i=0; i < len; i++)
  {
      int mod = i % 32;
      ret ^=(unsigned int) (word[i]) << mod;
      ret ^=(unsigned int) (word[i]) >> (32 - mod);
  }
  return ret;
}
//-------------------------------
void Security_packetAgent::recv(Packet* pkt, Handler*)
{
  // Access the IP header for the received packet:
  hdr_ip* hdrip = hdr_ip::access(pkt);
 
  // Access the security packet header for the received packet:
  hdr_security_packet* hdr = hdr_security_packet::access(pkt);
 

  // check if in brdcast mode
  if ((u_int32_t)hdrip->daddr() == IP_BROADCAST)
  {
    if (hdr->ret == 0)
    {
     
      printf("Recv BRDCAST Security_packet REQ : at %d.%d from %d.%d\n", here_.addr_, here_.port_, hdrip->saddr(), hdrip->sport());
      Packet::free(pkt);
     
      // create reply
      Packet* pktret = allocpkt();

      hdr_security_packet* hdrret = hdr_security_packet::access(pktret);
      hdr_cmn* ch = HDR_CMN(pktret);
      hdr_ip* ipret = hdr_ip::access(pktret);
     
      hdrret->ret = 1;
     
      // add brdcast address
      ipret->daddr() = IP_BROADCAST;
      ipret->dport() = ipret->sport();
      send(pktret, 0);   
    }
    else
    {
      printf("Recv BRDCAST security_packet REPLY : at %d.%d from %d.%d\n", here_.addr_, here_.port_, hdrip->saddr(), hdrip->sport());
      Packet::free(pkt);
    }
    return;
  }
// end of broadcast mode
 
  if (hdr->ret == 0)
  {
    // Send an 'echo'. First save the old packet's send_time
    double stime = hdr->send_time;
    //---------decrypt encrypted packet-------------//
    char original_data[128];
    char encrypted_data[128];
    strcpy(encrypted_data,hdr->data); //copy the data of the original packet
    strcpy(original_data,hdr->data);
    int rcv_seq = hdr->seq;
    //----------------show the encrypted packet at receiving node-----------//
         
    char out[105];
    unsigned int newhash;
    char authenticate_result[50];

    // show encryted data then decrytp it and show
    decryption(original_data);
    newhash=hashing(original_data,strlen(original_data));
    if(newhash==hdr->hashvalue)
    {
        printf("data intergity ensured\n");
        strcpy(authenticate_result,"Message_Accepted");
    }
    else
    {
        printf("data modified %d\n",newhash);
        strcpy(authenticate_result,"MESSAGE_ERRROR-Integrity violation");
    }
    sprintf(out, "%s recv %d %3.1f %s %s %d", name(), hdrip->src_.addr_ >> Address::instance().NodeShift_[1],
                        (Scheduler::instance().clock()-hdr->send_time) * 1000, encrypted_data, original_data,hdr->hashvalue);
    Tcl& tcl = Tcl::instance();
    tcl.eval(out);

    // Discard the packet
    Packet::free(pkt);
    // Create a new packet
    Packet* pktret = allocpkt();
    // Access the header for the new packet:
    hdr_security_packet* hdrret = hdr_security_packet::access(pktret);
    // Set the 'ret' field to 1, so the receiver won't send
    // another echo
    hdrret->ret = 1;
    // Set the send_time field to the correct value
    hdrret->send_time = stime;
   
    hdrret->rcv_time = Scheduler::instance().clock();
    hdrret->seq = rcv_seq;
    strcpy(hdrret->data, authenticate_result);//save data to new packet
    // Send the packet back to the originator
    send(pktret, 0);
  }
  else
  {
    char out[105];
    // showing at originator node when packet comes back       
   
    sprintf(out, "%s recv %d %3.1f %s _ %d", name(), hdrip->src_.addr_ >> Address::instance().NodeShift_[1],
                        (Scheduler::instance().clock()-hdr->send_time) * 1000, hdr->data, hdr->hashvalue);
    Tcl& tcl = Tcl::instance();
    tcl.eval(out);
    // Discard the packet
    Packet::free(pkt);
  }
}

security_packet.h
Code:

//-----------------------------------
// Dated: April 25,2005
// Created: Sam Tran and Tuan Nguyen.
// file name: security_packet.h
//-----------------------------------
#ifndef ns_security_packet_h
#define ns_security_packet_h

#include "agent.h"
#include "tclcl.h"
#include "packet.h"
#include "address.h"
#include "ip.h"

struct hdr_security_packet {
        char ret;
        double send_time;
        double rcv_time;        // when security packet arrived at receiver
        int seq;                // sequence number
        char data[128];
        unsigned int hashvalue;

        // Header access methods
        static int offset_; // required by PacketHeaderManager
        inline static int& offset() { return offset_; }
        inline static hdr_security_packet* access(const Packet* p) {
                return (hdr_security_packet*) p->access(offset_);
        }
};

class Security_packetAgent : public Agent {
public:
        Security_packetAgent();
        int seq;       
        int oneway;        // enable seq number and one-way delay printouts
        virtual int command(int argc, const char*const* argv);
        virtual void recv(Packet*, Handler*);
        void encryption(char* out);
        void decryption(char* out);
        unsigned int hashing (char value[], unsigned int len);
};
#endif // ns_security_packet_h

security.tcl
Code:

#Create a simulator object
set ns [new Simulator]

#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red

#Open a trace file
set nf [open out.nam w]
$ns namtrace-all $nf

#Define a 'finish' procedure
proc finish {} {
        global ns nf
        $ns flush-trace
        close $nf
        exec nam out.nam &
        exit 0
}

#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
set n5 [$ns node]

#Create links between the nodes
$ns duplex-link $n0 $n2 5Mb 10ms DropTail
$ns duplex-link $n1 $n2 5Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.5Mb 10ms DropTail
$ns duplex-link $n3 $n4 5Mb 10ms DropTail
$ns duplex-link $n3 $n5 5Mb 10ms DropTail

#Set Queue Size of link (n2-n3) to 100
$ns queue-limit $n2 $n3 100

$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
$ns duplex-link-op $n3 $n4 orient right-up
$ns duplex-link-op $n3 $n5 orient right-down

#Define a 'recv' function for the class 'Agent/Security_packet'
Agent/Security_packet instproc recv {from rtt mess originmess hash} {
        $self instvar node_
        puts "node [$node_ id] received packet from \
              $from with trip-time $rtt ms - contend: $mess - decrypted $originmess -hash: $hash"
}

#Create two security agents and attach them to the nodes n0 and n2
set p0 [new Agent/Security_packet]
$ns attach-agent $n0 $p0
$p0 set class_ 1

set p1 [new Agent/Security_packet]
$ns attach-agent $n1 $p1
$p1 set class_ 1

set p2 [new Agent/Security_packet]
$ns attach-agent $n4 $p2
$p2 set class_ 2

set p3 [new Agent/Security_packet]
$ns attach-agent $n5 $p3
$p3 set class_ 2

#Connect the two agents
$ns connect $p0 $p3
$ns connect $p1 $p2


#Schedule events

for {set i 1} {$i < 2} {incr i} {
        set result [expr $i /2]
        $ns at $result "$p0 send itisalongmessageIcansend"
        $ns at [expr $result + 0.02] "$p1 send  Itisashotermessage"
        $ns at [expr $result + 0.04] "$p2 send test3"
        $ns at [expr $result + 0.06] "$p3 send test4"
}
$ns at 1.0 "finish"

#Run the simulation
$ns run


Raaha Priya 04-08-2015 04:45 PM

Any luck clearing the error??
 
Hi,
I got the same error while adding the protocol. Have you cleared the error? Can you tell me how you cleared the error

knudfl 04-08-2015 06:10 PM

Post #4, @Raaha Priya : Welcome to LQ.

You have two posts about "got the same error".
Which protocol are you trying to add ?


The above error "ns: [code omitted because of length]"
.. happens when you add some surplus entries to e.g. tcl/lib/ns-default.tcl .
Or : Another (conflicting) protocol was added to tcl/lib/{ files.tcl },
.. before the protocol that you are now trying to add.


-

Raaha Priya 04-08-2015 06:37 PM

I am trying to add a new security protocol in ns2.35 I followed the instructions given in this link "http://www.nsnam.com/2015/03/security-protocol-packet-in-ns2.html". But I ended with the following error
invalid command name "Agent/Security_packet"
while executing
"Agent/Security_packet instproc recv {from rtt mess originmess hash} {
$self instvar node_
puts "node [$node_ id] received packet from \
..."
(file "security.tcl" line 51)

Please help me with this

The other protocol I tried to add was vanetrbc protocol. I ended up with a similar error for that too.
I could not figure out the reason for the error as I am a beginner. Kindly help me with it.

knudfl 04-08-2015 08:13 PM

ns-2.35 + Security Protocol
 
Re #6.

Based on http://www.nsnam.com/2015/03/securit...et-in-ns2.html
$ tar xvf ns-allinone-2.35_gcc482.tar.gz
https://drive.google.com/file/d/0B7S...ew?usp=sharing
$ cd ns-allinone-2.35/
$ patch -p0 < security_ns235.patch
https://drive.google.com/file/d/0B7S...ew?usp=sharing
$ ./install
$ cd ns-2.35/
$ sudo make install
$ cp ns ns235-security ( This is your backup )
$ sudo cp ns235-security /usr/local/bin/
Simulation :
$ cd security/
$ ../ns security.tcl : Perfect.
( Or : $ ns235-security security.tcl ).


* About "vanetrbc" : Is it this one ?
http://gcorser.weebly.com/install-vanetrbc.html


-

Raaha Priya 04-08-2015 09:14 PM

Thanks for helping me out. I'll try the procedure for adding the security protocol and let you know if it works for me.

And regarding vanetrbc, yes I have used the link that you have provided for adding the protocol. While running the test tcl file (vntest.tcl) I got the following error

invalid command name "Agent/VanetRBC"
while executing
"Agent/VanetRBC create _o2015 0"
invoked from within
"catch "$className create $o $args" msg"
invoked from within
"if [catch "$className create $o $args" msg] {
if [string match "__FAILED_SHADOW_OBJECT_" $msg] {
delete $o
return ""
}
global errorInfo
error "class $..."
(procedure "new" line 3)
invoked from within
"new Agent/VanetRBC $i"
("for" body line 2)
invoked from within
"for {set i 0} {$i < $opt(nn)} {incr i} {
set p($i) [new Agent/VanetRBC $i]
$ns_ attach-agent $node($i) $p($i)
# set the logtarget for ever..."
(file "vanet.tcl" line 225)

Thanks in advance

knudfl 04-08-2015 10:38 PM

ns-2.35 + vanetrbc
 
Re #8.

VanetRBC :
Setup another ns-allinone-2.35 in a new directory, for testing vanetrbc.

$ tar xvf ns-allinone-2.35_gcc482.tar.gz
$ cd ns-allinone-2.35/
$ zcat vanetrbc_ns235.patch.gz | patch -p0
https://drive.google.com/file/d/0B7S...ew?usp=sharing
$ ./install
$ cd ns-2.35/
$ cp ns ns235-vanetrbc
$ sudo cp ns235-vanetrbc /usr/local/bin/
Simulation :
$ cd vanetrbc/
$ ns235-vanetrbc vntest-2015.tcl : Lots of activity in the Nam window.


-

Raaha Priya 04-08-2015 10:47 PM

Can we have two ns2 on the same os. Im using ubuntu 12.04. And i already have ns2.35 in the home directory. Now can i install ns2 in some other directory? Some of my friends used two versions of ns2 in the same os. But that din work properly and they had to change the os and start from the beginning.

knudfl 04-08-2015 11:15 PM

Re #10.

I have about 150 times ns-allinone-2.xx in /home/knudfl/<dir>/.
So I guess you can have 3 or 4 to test your projects.
As you can see from the above posts, the executable 'ns' can have any name.
'ns' or 'ns-new-name' is hard coded to know the location of it`s libraries :
ns-allinone-2.xx/{ lib/*, bin/tcsh8* }.

And the same for 'nam' : cd nam-1.15/ && sudo make install
... Etc. etc.

Never add ns, nam, etc. etc. to .bashrc . Not required.


-

Raaha Priya 04-11-2015 12:29 PM

Thanks for your guidance.
I have a doubt. I am trying to create a simulation environment where nodes will communicate using vanetrbc and security will be implemented using the security protocol. So a single tcl file will be using both the protocols. But how can I do so if I use different ns for different protocols??

knudfl 04-11-2015 12:55 PM

Re #12.
Quote:

I am trying to create a simulation environment where nodes will communicate using vanetrbc and security
Please test VanetRBC.

There may not be any problems using the two patches in the same ns2.
'security_ns235.patch' and 'vanetrbc_ns235.patch'.
I will have a look at that .... later.


-

Raaha Priya 04-11-2015 12:57 PM

Thank you so much

Do you have any idea about TraNS??

Raaha Priya 04-11-2015 01:02 PM

I tested VANETRBC. It works fine. Thank you so much for the detailed procedure.

However when I tried to patch the security protocol, I ended up with error.
The following is the error i got

In file included from ./common/agent.h:41:0,
from security/security.h:9,
from security/security.cc:4:
./common/packet.h: In static member function ‘static void p_info::initName()’:
./common/packet.h:330:9: error: ‘PT_SECURITY_PACKET’ was not declared in this scope
security/security.cc: In constructor ‘Security_packetAgent::Security_packetAgent()’:
security/security.cc:25:54: error: ‘PT_SECURITY_PACKET’ was not declared in this scope
security/security.cc: In member function ‘void Security_packetAgent::encryption(char*)’:
security/security.cc:89:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
security/security.cc: In member function ‘void Security_packetAgent::decryption(char*)’:
security/security.cc:99:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
security/security.cc: In member function ‘virtual void Security_packetAgent::recv(Packet*, Handler*)’:
security/security.cc:142:16: warning: unused variable ‘ch’ [-Wunused-variable]
make: *** [security/security.o] Error 1
Ns make failed!

I tried this the other way too. I first patched the security file and then vanetrbc. I ended up with the same error and NS make failed.


Quote:

Originally Posted by knudfl (Post 5345887)
Re #12.

Please test VanetRBC.

There may not be any problems using the two patches in the same ns2.
'security_ns235.patch' and 'vanetrbc_ns235.patch'.
I will have a look at that .... later.


-



All times are GMT -5. The time now is 01:39 AM.