Sunday 2 November 2014

  Installing Errors and Solutions on fedora ns2 installation,

One of the most easiest way to install ns2 is to download ns-allinone package. So download ns-allinone-2.34.tar.gz.
now follow these steps.

1. go to directory where you saved tar.gz file and execute following command
tar -xzf ns-allinone-2.34.tar.gz
 

2. cd ns-allinone-2.34/
 

3. now install the package using (switct to root or use sudo)
./install
 

4. first problem you encounter is "g++ command not found" otherwise skip this step
soln- yum install gcc-g++
now try 3rd step again
 

5. you might encounter problem of "/home/user.../generic/tk.h:557: error expected declaration specifiers or '...' before 'Window' and similar other errors while installing tk (otherwise skip this step)
soln- yum install libX11-devel
now try 3rd step
 

6. if you get "can't find X includeds otcl-1.13 configuration failed! Exiting.."
soln- yum install libXt-devel or yum groupinstall "X Software Development" later one is better.
now try 3rd step and it will run successfully

How to Patch i.e., Inclusion of new files into patch and creation of patch on linux

Programmer who take part in some kind of big code needs to generate patch files. Begginers do get trouble here as how to create, there is simpple command on linux diff use that as follows
Suppose u have old code in oldmake folder and new code in home/newmake. 
 
Then follow these steps.

1. cd home
2. diff -urN /home/waste/oldmake newmake > patch1
 
Now you might have written some new file instead of modifying any file. There is no need to worry diff is already taken care of that stuff, your patch includes those files.

How to use patch
if you want to apply patch inside /home/checkmake then go to checkmake folder

1. cd /home/checkmake
2. patch -p patch1

By Using binary number in C or C++

Many C learner face the problem of initializing a variable with binary value or assign a integer variable with binary values. Don't worry its very easy as described in C you can use 0x for hex, 0 for octal similarly 0b defines binary. for example if you want to assign binary 1101 (i.e 13) to variable. then use this
int var = 0b1101;

Undefined reference to static variable in c++

Suppose i have a code which need to reference static variable
so we have following code in trial.h
class Trial
{
static int v;
void setCode();
}

in trail.cc
#include "trial.h"
void Trial::setCode()
{
v=1;
}

This above code will give undefined reference to Trial::v error. To remove this error either remove static word from v in .h file or if it is necessary to have static word. then do as following.
in trial.cc
#include "trial.h"
int Trial::v;
void Trial::setCode()
{
v=1;
}
now your error is gone . happy coding

Remote desktop on fedora from fedora client

Most of the times peoples don't always like to reach office or other machines where actual work is done. Suppose you have a very high resource machine located at some distance from your current place and you don't want to go there for work. Here i provide a way to people who don't how to remote fedora machines from fedora clients.
On client fedora machine:
"yum install tsclient"

you may require sudo permission to run above command.


On fedora server:

"yum install vnc-server"

this will install vnc server on your fedora machine. then set the password for your vnc server using below command

"vncpasswd"

this will prompt for a password. Enter the password which you want to use later when you trying to connect from fedora client. Now time to start your vnc server

"service vncserver start"

Now if you try to connect from client to this server using command
"vncviewer serverip"

it may give you "unable connect to socket: Connection refused (111)"

then use sudo permission on server to stop firewalls using
"service iptables stop"

now you all set to remote the server.

Note:- it may still shows "unable connect to socket: Connection refused (111)" in that case cancel your server at the current port and start the server with next port and try vncviewer serverip :port

How to send user data in ns2

If you are trying to send your own data (i.e user defined data) over a network in ns2 then you might have tried different function of Packet. If you have tried out allocating PacketData and setting it into Packet then it might have leaking the memory and eats up swap space and later processor time too. Generally for small data transfer it wont be able to figure it out but when you transfer data in order of 10 of MBs then you might face this challenge. You might have tried creating your own Packet type etc. if it works then its good but i don't think it will.
So what we can do?

Solution is that create your own application layer and transport layer well you can edit existing but those are might be used by other traffics and did not give proper results in other application which you don't want. Its always nice to create a new class. So derive a class from application layer and another class from transport layer.
Add one char * data and int size field in packet header.

In application read data from file and assign it to new pointer, create a packet and point data field of packet to this new pointer with size and send this packet to transport layer. At transport layer copy the packet header into newly created header (all existing transport layer always create a new packet) and send it normally to other existing layers with out any problem.

On receiving at transport layer either you can process it here or pass the header to application layer for further process. Now process the data in receive side of application layer and free the pointer pointed by data pointer of header. That's it and you are done.


Here goes the important part of the code but this is for UDP, similar things can be done on TCP (TCP code will look complex that's why i am uploading UDP code)
void mimoUdpAgent::sendmsg(int nbytes, AppData* data, const char* flags)
{
    Packet *p;
    int n;
    hdr_cmn cmn_buf = *(hdr_cmn *)flags;
    assert (size_ > 0);

    n = nbytes / size_;

    if (nbytes == -1) {
        printf("Error:  sendmsg() for UDP should not be -1\n");
        return;
    }   

    // If they are sending data, then it must fit within a single packet.
    if (data && nbytes > size_) {
        printf("Error: data greater than maximum UDP packet size\n");
        return;
    }

    double local_time = Scheduler::instance().clock();
    while (n-- > 0) {
        p = allocpkt();
        hdr_cmn::access(p)->size() = size_;
        hdr_rtp* rh = hdr_rtp::access(p);
        rh->flags() = 0;
        rh->seqno() = ++seqno_;
        hdr_cmn::access(p)->timestamp() =
            (u_int32_t)(SAMPLERATE*local_time);
       
        hdr_cmn::access(p)->last_ = cmn_buf.last_;
       
        hdr_cmn::access(p)->realPayload = cmn_buf.realPayload;
        hdr_cmn::access(p)->file = cmn_buf.file;
        hdr_cmn::access(p)->frame_id_ = cmn_buf.frame_id_;
        char buf[100];
        if(openfile!=0){
            hdr_cmn::access(p)->pkt_id_ = id_++;
        }
       
        // add "beginning of talkspurt" labels (tcl/ex/test-rcvr.tcl)
        if (flags && (0 ==strcmp(flags, "NEW_BURST")))
            rh->flags() |= RTP_M;
        p->setdata(data);
        target_->recv(p);
    }
    n = nbytes % size_;
    if (n > 0) {
        p = allocpkt();
        hdr_cmn::access(p)->size() = n;
        hdr_rtp* rh = hdr_rtp::access(p);
        rh->flags() = 0;
        rh->seqno() = ++seqno_;
        hdr_cmn::access(p)->timestamp() =
            (u_int32_t)(SAMPLERATE*local_time);
        hdr_cmn::access(p)->last_ = cmn_buf.last_;
        hdr_cmn::access(p)->realPayload = cmn_buf.realPayload;

        hdr_cmn::access(p)->file = cmn_buf.file;
        if (flags && (0 == strcmp(flags, "NEW_BURST")))
            rh->flags() |= RTP_M;
        p->setdata(data);
        target_->recv(p);
    }
    idle();
}

Note: Part of the code is copied from work of other person.

I have added many fields here but realPayload and last are important field. Last will tell the end of data and realPayload contains actual data transferred from application.

Installing ns-2.35 on Mountain Lion, Mavericks and Yosemite (OS X 10.8/9/10)


Here i will provide step by step procedure for you to install ns-2.35 on mac OS X 10.8/9/10 which i successfully installed on my mac (fully tested by me and its working)


Credits:- ISI, Brain Adamson(for great patch) ,Henrique Zelak Leite Bastos for the right help and last but not the least APPLE for Mac OS X.(the best a dev. can get)



1. Enabling root user. (not a necessary step, you can skip if you want)

Enable root by following these steps http://support.apple.com/kb/PH11331


2. Download files and install them.


   a. Download ns 2.35 from  http://sourceforge.net/projects/nsnam/files/allinone/ns-allinone-2.35/ns-allinone-2.35.tar.gz/download


   b. Download XCODE from app store and install command line tools from preferences of XCODE (not needed for XCODE 5 or later).


   c. Download XQuartz from http://xquartz.macosforge.org/ (this installs X11 libraries)
install and reboot .


3. Choose install directory.

Place the downloaded ns-allione-2.35.tar.gz file in the location you want to install it. 


4. Extract ns files.

Go to location where you have put your downloaded ns file and run 

tar zxvf ns-allinone-2.35.tar.gz 


6.  Get into the Directory.

Execute  cd ns-allinone-2.35


7. Download Patch.

Download zip file from https://github.com/aemreunal/ns-allinone-2.35-Mac-OS-10.9-install-patch . Extract and put these files in ns-allinone-2.35 directory.


8. Execute command

Execute sudo ./install64 (will give paths after completion. Copy them)


9. Save Paths.


So now we save them properly. Come back to your user/home directory and run

touch ~/.bash_profile (if you dont have a bash_profile file in your home directory).
open ~/.bash_profile (opens bash_profile file in textEdit).

now enter the paths like 

export PATH=$PATH:path1:path2:path3( Here paths should be separated by colon)

similarly for

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:path1:path2( Here paths should be separated by colon)

and also for

export TCL_LIBRARY=$TCL_LIBRARY:path 


Save and Exit. Now run

source ~/.bash_profile 

Type nam and .... Bingo!!! if you see the nam window.(or else recheck 9th step)

Tutorial to implement protoname in ns2.35


This is an Implementation tutorial of new manet(Mobile Ad-hoc NETworks) unicast protocol name protoname.

The whole credit goes to Francisco J. Ros and Pedro M. Ruiz for their beautiful work and excellent explanation they provide. The link to their work is below. It contains a PDF file also which explains you in detail and I recommend you to go through it once.

List of files to be modified

  • ~/ns-allinone-2.35/ns-2.35/common/packet.h
  • ~/ns-allinone-2.35/ns-2.35/trace/cmu-trace.h
  • ~/ns-allinone-2.35/ns-2.35/trace/cmu-trace.cc
  • ~/ns-allinone-2.35/ns-2.35/tcl/lib/ns-packet.tcl
  • ~/ns-allinone-2.35/ns-2.35/tcl/lib/ns-default.tcl 
  • ~/ns-allinone-2.35/ns-2.35/tcl/lib/ns-lib.tcl
  • ~/ns-allinone-2.35/ns-2.35/queue/priqueue.cc

 
** Note: Text in red is the part that is to be added or modified in given file and location and purple colour denotes the terminal commands.

** Note: mod stands for modification.

** Note: Line number is approx and is provided for your convenience. Please check the methods below and above before inserting.

Step 1:

download protoname.rar

http://www.cs.nccu.edu.tw/~g10031/protoname.rar

Extract the files and place them in ~/ns-allinone-2.35/ns-2.35/protoname directory(create the directory if not present)

Step 2:

gedit ~/ns-allinone-2.35/ns-2.35/common/packet.h (mods at 3 places)

  • a.       its somewhere near line 200

static const packet_t PT_DCCP = 63;
static const packet_t PT_DCCP_REQ = 64;
static const packet_t PT_DCCP_RESP = 65;
static const packet_t PT_DCCP_ACK = 66;
static const packet_t PT_DCCP_DATA = 67;
static const packet_t PT_DCCP_DATAACK = 68;
static const packet_t PT_DCCP_CLOSE  = 69;
static const packet_t PT_DCCP_CLOSEREQ = 70;
static const packet_t PT_DCCP_RESET = 71;

        // M-DART packets
static const packet_t PT_MDART = 72;

        // insert new packet types here

static const packet_t PT_PROTONAME = 73;

static packet_t       PT_NTYPE = 74; // This MUST be the LAST one


  • b. near line 250

static bool data_packet(packet_t type) {
return ( (type) == PT_TCP || \
        (type) == PT_TELNET || \
        (type) == PT_CBR || \
        (type) == PT_AUDIO || \
        (type) == PT_VIDEO || \
        (type) == PT_ACK || \
        (type) == PT_SCTP || \
        (type) == PT_SCTP_APP1 || \
        (type) == PT_HDLC || \  //(remember this mod also)
(type) == PT_PROTONAME \ 
       );

  • c.      near line 422
static void initName()
{
                .....
                name_[PT_DCCP_REQ]="DCCP_Request";
name_[PT_DCCP_RESP]="DCCP_Response";
name_[PT_DCCP_ACK]="DCCP_Ack";
name_[PT_DCCP_DATA]="DCCP_Data";
name_[PT_DCCP_DATAACK]="DCCP_DataAck";
name_[PT_DCCP_CLOSE]="DCCP_Close";
name_[PT_DCCP_CLOSEREQ]="DCCP_CloseReq";
name_[PT_DCCP_RESET]="DCCP_Reset";
name_[PT_PROTONAME]="protoname"; 
name_[PT_NTYPE]= "undefined";
}


save and close.

Step 3:  

gedit ~/ns-allinone-2.35/ns-2.35/trace/cmu-trace.h (mod at 1 place)

  • a. Near line 164.

class CMUTrace: public Trace {
public:
CMUTrace (const char * s, char t);
...
private:
char tracename [MAX_ID_LEN + 1];
int nodeColor [MAX_NODE];
...
void format_tora (Packet * p, int offset);
void format_imep (Packet * p, int offset);
void format_aodv (Packet * p, int offset);
/ / ----------------------------------------------
void format_protoname (Packet *p, int offset);
/ / ----------------------------------------------
void format_aomdv (Packet * p, int offset);
void format_mdart (Packet * p, int offset);

/ / This holds all the tracers added at run-time
static PacketTracer * pktTrc_;

};
# Endif / * __ cmu_trace__ * /

save and close.

Step 4:

gedit ~/ns-allinone-2.35/ns-2.35/trace/cmu-trace.cc (mods at 3 places)

  • a. Add this line at start

#include <protoname/protoname_pkt.h>

  • b. Near line 1168
void
CMUTrace :: format_mdart (Packet * p, int offset)  {

.........
........
........
}

void
CMUTrace::format_protoname(Packet *p, int offset)
{
    struct hdr_protoname_pkt* ph = HDR_PROTONAME_PKT(p);
    if (pt_->tagged())
    {
            sprintf(pt_->buffer() + offset, "-protoname:o %d -protoname:s %d -protoname:l %d ", ph->pkt_src(), ph->pkt_seq_num(), ph->pkt_len());
        }
    else if (newtrace_)
    {
            sprintf(pt_->buffer() + offset, "-P protoname -Po %d -Ps %d -Pl %d ", ph->pkt_src(), ph->pkt_seq_num(), ph->pkt_len());
        }
    else
    {
            sprintf(pt_->buffer() + offset, "[protoname %d %d %d] ", ph->pkt_src(), ph->pkt_seq_num(), ph->pkt_len());
        }
}



  • c. Near line 1477
void CMUTrace :: format (Packet * p, const char * why)
{...
switch (ch-> ptype ()) {
case PT_MAC:
...
case PT_GAF:
case PT_PING:
break;
/ / --------------------------------------------
case PT_PROTONAME:
format_protoname(p, offset);
break;
/ / --------------------------------------------
default:
...
}

save and close.

Step 5:

gedit ~/ns-allinone-2.35/ns-2.35/tcl/lib/ns-packet.tcl  (mod at 1 place)

  • a. Near line 172

# Mobility, Ad-Hoc Networks, Sensor Nets:
    AODV     # routing protocol for ad-hoc networks
    Protoname # new routing protocol for ad-hoc networks
    Diffusion     # diffusion/diffusion.cc
    IMEP     # Internet MANET Encapsulation Protocol, for ad-hoc

Save and close.

Step 6:

gedit ~/ns-allinone-2.35/ns-2.35/tcl/lib/ns-default.tcl  (mod at 1 place)

  • a. Add at the last.

# Defaults defined for Protoname
Agent/Protoname set accessible_var_ true


save and close.

step 7:

gedit ~/ns-allinone-2.35/ns-2.35/tcl/lib/ns-lib.tcl  (mod at 2 places)

  • a. Near line 671

switch-exact $ routingAgent_ {
...
ManualRtg {
set ragent [$ self create-manual-rtg-agent $
}
# / ------------------------------------------------ -
Protoname 
{
         set ragent [$self create-protoname-agent $node]

}
# / ------------------------------------------------ -
default {
...
}


  • b. Near line 2278

Simulator instproc create-omnimcast-agent {node} {
...
}
# / ------------------------------------------------ -----
Simulator instproc create-protoname-agent {node} {
    # Create Protoname routing agent
    set ragent [new Agent/Protoname [$node node-addr]]
    $self at 0.0 "$ragent start"
    $node set ragent_ $ragent
    return $ragent
}

# / ------------------------------------------------ -----
# XXX These are very simulation-specific methods, why should they belon
Simulator instproc put-in-list {agent} {
...
}


Step 8:

gedit ~/ns-allinone-2.35/ns-2.35/queue/priqueue.cc (mod at 1 place)

  • a. Near line 95.
void
PriQueue :: recv (Packet * p, Handler * h)
{
struct hdr_cmn * ch = HDR_CMN (p);

if (Prefer_Routing_Protocols) {
switch (ch-> ptype ()) {
...
case PT_MDART:
/ / --------------------------------------------
case PT_PROTONAME:
/ / --------------------------------------------
recvHighPriority (p, h);
break;
default:
Queue :: recv (p, h);
}
}
else {
Queue :: recv (p, h);
}
}

save and close.

Step 9:

gedit ~/ns-allinone-2.35/ns-2.35/Makefile (mod at 1 place)

  • a. Near line 336

OBJ_CC = \
tools / random.o tools / rng.o tools / ranvar.o common / misc.o common /
...
wpan/p802_15_4trace.o wpan/p802_15_4transac.o \
apps / pbc.o \
# / / ----------------------------------------------- -
protoname/protoname.o protoname/protoname_rtable.o \
# / / ----------------------------------------------- -
$ (OBJ_STL)

save and close.

Step 10:

build it now, changes done ( run these in terminal in ~/ns-allinone-2.35/ns-2.35 directory )

  • a. make clean
  • b. touch common/packet.cc
  • c. make

(if you are getting some errors check the spaces in the editing you did above)


Step 11:

gedit ~/ns-allinone-2.35/ns-2.35/test.tcl

copy and paste

set ns [new Simulator]
$ns node-config -Routing protoname   
set nf [open out.nam w]     
$ns namtrace-all $nf       
set nd [open out.tr w]       
$ns trace-all $nd             
  proc finish {} {
          global ns nf  nd
          $ns flush-trace 
          close $nf       
          close $nd       
          exec nam out.nam &
          exit 0
   }

for {set i 0} {$i < 7} {incr i} {set n($i) [$ns node] }
for {set i 0} {$i < 7} {incr i} {
$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail
}
set udp0 [new Agent/UDP]   
$ns attach-agent $n(0) $udp0
set cbr0 [new Application/Traffic/CBR] 
$cbr0 set packetSize_ 500     
$cbr0 set interval_ 0.005      
$cbr0 attach-agent $udp0
set null0 [new Agent/Null]
$ns attach-agent $n(3) $null0
$ns connect $udp0 $null0 
$ns at 0.5 "$cbr0 start"
$ns rtmodel-at 1.0 down $n(1) $n(2) 
$ns rtmodel-at 2.0 up $n(1) $n(2)   
$ns at 4.5 "$cbr0 stop"
$ns at 5.0 "finish"
$ns run


save and close

ns ~/ns-allinone-2.35/ns-2.35/test.tcl

and boom you are getting the nam (if not sorry troubleshoot once by going through the tutorial again as I am getting the outputs completely and so should you)

Expected Errors
  1. Got this error

    trace/cmu-trace.cc:1176: error: stray '\302' inprogram
    trace/cmu-trace.cc:1176: error: stray '\240' inprogram
    trace/cmu-trace.cc:1176: error: stray '\302' inprogram
    trace/cmu-trace.cc:1176: error: stray '\240' inprogram
    ...
    trace/cmu-trace.cc:1183: error: stray '\302' inprogram
    trace/cmu-trace.cc:1183: error: stray '\240' inprogram
     
    Solution
    check your cmu.trace.cc file... its a syntactic error.. see you inserted the changes in the right place with right syntax... I did my successfully not in just one attempt... errors come but you need to remove them ... be vigilant ... cheers!!!

  2. Thank you I solved that but still got this error

    trace/cmu-trace.cc:1120: error: stray _FUNCTION_ not declared

    How do I include the FUNCTION?

    Great tutorial! I have followed the orignal tutorial by Francisco J. Ros and Pedro M. Ruiz, and made some modifications to successfully compile it, after which I read your post and used the test script. I was wondering what "protoname" does exactly? I didn't know how to test it until I saw your post. So there must be something I didn't quite understand. Thanks :)

    Possible Question can you tell me what protoname does exactly ??

    Solution
    protoname does not implement any extra ordinary features , it just teaches you basics of creating a user defined protocol and how to add it to ns and use that protocol.

    You can read the .pdf file in the protoname folder. It explains everything in detail. first it pings the neighbours to locate them and then find the path.

  3. Possible Question
    how can i use SPIN routing protocol in wireless sensor network in ns2, i have SPIN code but don't know how to implement it....
     
    Solution
    read ns-manual . They have taught how to implement your own protocol (if SPIN is a user defined one).

  4. Hello..... nice to meet everyone...... I am working the project of black hole attack in AODV protcol. I want to know how can I add new packet type in AODV protocol. I write alarm packet type in aodv protocol such as Hello packet. I declare this packet in aodv_packet.h and then I write the send and receive fun in aodv.h. I use by calling this fun in aodv.cc. When I am compiling Ns-2, there is no error. But, when I run TCL script, there is error such as Invalid AODV pcket format. So, Anybody help me. How can I add alarm packet in aodv protocol??????

    trace your program control... use debugging tools


  5. hey i m getting dis error
    common/tclAppInit.o: In function `Tcl_AppInit':
    tclAppInit.cc:(.text+0xec): undefined reference to `et_ns_ptypes'
    collect2: error: ld returned 1 exit status
    make: *** [ns] Error 1re-check the changes you made.... especially in first 3 steps
     
  6. but following your instructions i got it working!
  7. but i was curious of step 11.. where exactly is the new implemented protocol being used?
    is it this line? $ns node-config -Routing protoname

    yup... try using AODV in place of protoname or DSDV...

How to color nodes in NS2

You can color the nodes in NS2. It is bit tricky to color them in a wireless network. We can do this by following code.

set node0 [$ns node]
$node0 color "blue"
$ns at 0.0 "$node0 color blue"