PoiNtEr->: July 2012

                             Difference between a dream and an aim. A dream requires soundless sleep, whereas an aim requires sleepless efforts.

Search This Blog

Saturday, July 28, 2012

Advanced Data Structure Lecture Notes


PCS-101 Lecture Notes



Lecture Note 1  -- 28/07/2012


Lecture Note 2 -- 29/07/2012



For More notes click here

Saturday, July 21, 2012

Socket Programming - Handling Connections

Continuing from my Last Post -Socket Programming in C
To handle every connection we need a separate handling code to run along with the main server accepting connections.
One way to achieve this is using threads. The main server program accepts a connection and creates a new thread to handle communication for the connection, and then the server goes back to accept more connections.
                                       
On Linux threading can be done with the pthread (posix threads) library.
We shall now use threads to create handlers for each connection the server accepts. Lets do it man.


#include<stdio.h>
#include<string.h> //strlen
#include<stdlib.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h> //write

#include<pthread.h> //for threading , link with lpthread

void *connection_handler(void *);

int main(int argc , char *argv[])
{
int socket_desc , new_socket , c , *new_sock;
struct sockaddr_in server , client;
char *message;

//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}

//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8881 );

//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("bind failed");
return 1;
}
puts("bind done");

//Listen
listen(socket_desc , 3);

//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
while( (new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
{
puts("Connection accepted");

//Reply to the client
message = "Hello Client , I have received your connection. And now I will assign a handler for you\n";
write(new_socket , message , strlen(message));

pthread_t sniffer_thread;
new_sock = malloc(1);
*new_sock = new_socket;

if( pthread_create( &sniffer_thread , NULL ,  connection_handler , (void*) new_sock) < 0)
{
perror("could not create thread");
return 1;
}

//Now join the thread , so that we dont terminate before the thread
//pthread_join( sniffer_thread , NULL);
puts("Handler assigned");
}

if (new_socket<0)
{
perror("accept failed");
return 1;
}

return 0;
}

//This will handle connection for each client
void *connection_handler(void *socket_desc)
{
//Get the socket descriptor
int sock = *(int*)socket_desc;

char *message;

//Send some messages to the client
message = "Greetings! I am your connection handler\n";
write(sock , message , strlen(message));

message = "Its my duty to communicate with you";
write(sock , message , strlen(message));

//Free the socket pointer
free(socket_desc);

return 0;
}

Run the above server in one terminal and use other to communicate with server. Now the server will create a thread for each client connecting to it.

The telnet terminals would show :

$ telnet localhost 8881
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Hello Client , I have received your connection. And now I will assign a handler for you
Hello I am your connection handler
Its my duty to communicate with you

This one looks good , but the communication handler is also quite dumb. After the greeting it terminates. It should stay alive and keep communicating with the client.

One way to do this is by making the connection handler wait for some message from a client as long as the client is connected. If the client disconnects , the connection handler ends.

So the connection handler can be rewritten like this :


/* This will handle connection for each client

void *connection_handler(void *socket_desc)
{
//Get the socket descriptor
int sock = *(int*)socket_desc;
int read_size;
char *message , client_message[2000];

//Send some messages to the client
message = "Greetings! I am your connection handler\n";
write(sock , message , strlen(message));

message = "Now type something and i shall repeat what you type \n";
write(sock , message , strlen(message));

//Receive a message from client
while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
{
//Send the message back to client
write(sock , client_message , strlen(client_message));
}

if(read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}

//Free the socket pointer
free(socket_desc);

return 0;
}

The above connection handler takes some input from the client and replies back with the same. Simple! Here is how the telnet output might look

$ telnet localhost 8881
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Hello Client , I have received your connection. And now I will assign a handler for you
Greetings! I am your connection handler
Now type something and i shall repeat what you type
Hello
Hello
hi
hi
hehe
hehe
So now we have a server thats communicative. Thats useful now.
When compiling programs that use the pthread library you need to link the library. This is done like this
gcc program.c -lpthread

Sunday, July 1, 2012

How To Use Dynagen & Dynamips For Multiple CISCO Router Simulation On Ubuntu 12.04

Dynamips is a Cisco router emulator written by Christophe Fillot. It emulates 1700, 2600, 3600, 3700, and 7200 hardware platforms, and runs standard IOS images.


Dynagen is a text-based front end for Dynamips, which uses the “Hypervisor” mode for communication with Dynamips. Dynagen simplifies building and working with virtual networks:
  • Uses a simple, easy to understand configuration file for specifying virtual router hardware configurations
  • Simple syntax for interconnecting routers, bridges, frame-relay and ATM, and Ethernet switches. No need to deal with NetIOs
  • Can work in a client / server mode, with Dynagen running on your workstation communicating with Dynamips running on a back-end server. Dynagen can also control multiple Dynamips servers simultaneously for distributing large virtual networks across several machines. Or you can run Dynamips and Dyngen on the same system
  • Provides a management CLI for listing devices, starting, stopping, reloading, suspending, resuming, and connecting to the consoles of virtual routers.
 If you want to simulate a Cisco router, you can use dynamips but if you want to create a network topology you have to use both of dynagen and dynamips.
I'm using Ubuntu 12.04 to show an example now. First, we have to install dynagen (and dynamips) on Ubuntu 12.04.
sudo apt-get install dynagen

Now dynamips is a dependence of dynagen so it will automatically get installed with dynagen,if not then install it from synaptic manager
Three Routers Connected 
.
Let's start to work with sample network topology;

1: Start dynamips :

$dynamips -H 7200 &
Output:
vishal@Eva:~$ dynamips -H 7200 &
[1] 8936
vishal@Eva:~$ Cisco Router Simulation Platform (version 0.2.8-RC2-x86)
Copyright (c) 2005-2007 Christophe Fillot.
Build date: Jan 18 2011 19:22:29

ILT: loaded table "mips64j" from cache.
ILT: loaded table "mips64e" from cache.
ILT: loaded table "ppc32j" from cache.
ILT: loaded table "ppc32e" from cache.
Hypervisor TCP control server started (port 7200).

2:Create a dynagen configuration file(lab.net):
######################################################################
# [START INSTANCE1]

[eva.local:7200]
  udp=10002
  workingdir = /home/vishal/Desktop/cisco-labs/lab1/working

[[3725]]
  image = /home/vishal/cisco-ios/c3725-adventerprisek9-mz.124-15.T13.bin
  # npe=npe-150
  ram = 256
#  idlepc = ?? We will talk about it later.
  ghostios = true
  sparsemem = true
  idlemax = 100
  disk0=256
  

 [[Router R1]]
  model = 3725
  console = 2001
  autostart = false
#  F0/0 =
  S0/0 = R2 S0/0

 [[Router R2]]
  model = 3725
  console = 2002
  autostart = false
#  F0/0 =
  S0/0 = R1 S0/0
  S0/1 = R3 S0/0

 [[Router R3]]
  model = 3725
  console = 2003
  autostart = false
  S0/0 = R2 S0/1
# F0/0 =

# [FINISH INSTANCE1] 
################################################################################

$dynagen lab.net
 Sample Output:
vishal@Eva:~/Desktop/cisco-labs/lab1$ sudo dynagen lab.net
Reading configuration file...

Network successfully loaded

Dynagen management console for Dynamips and Pemuwrapper 0.11.0
Copyright (c) 2005-2007 Greg Anuzelli, contributions Pavel Skovajsa

=> list
Name       Type       State      Server                 Console   
R1         3725       stopped    eva.localhost:7200 2001      
R2         3725       stopped    eva.localhost:7200 2002      
R3         3725       stopped    eva.localhost:7200 2003      
=> ?

Documented commands (type help <topic>):
========================================
capture  confreg  cpuinfo  export  hist    list  py      save   show   suspend
clear    console  end      filter  idlepc  no    reload  send   start  telnet 
conf     copy     exit     help    import  push  resume  shell  stop   ver    

=> start R1
Warning: Starting R1 with no idle-pc value
100-VM 'R1' started
=> start R2
Warning: Starting R2 with no idle-pc value
100-VM 'R2' started
=> start R3
Warning: Starting R3 with no idle-pc value
100-VM 'R3' started
=> 


well you can remove the warning which routers are giving on start up i.e no idle-pc value by just adding 
idlepc = 0x60428c4c
in lab.net file in each router configuration

3:Log into Router:
Now guys either you can do telnet from other terminal to log into any of the three routers
ex: telnet eva.localhost 2001
2001 is port no. for R1 similarly 2002 is port no. for R2.
here eva.localhost is my local domain name.
Or you can also give command in dynagen shell:
=>console R1
and finally you will get a console like:
Router>

So that all for this post .Soon i'll will back with more info on how to configure the routers.
if you have any query or suggestion feel free to comment below.