Saturday, 29 December 2012

C code for character stuffing


#include<stdio.h>
#include<conio.h>
void ins(char,int);
void del(int,int);
char fr[50];
main()
{
 int i,cnt=0,ch;
 clrscr();
 printf("Enter the frame:\t");
 gets(fr);
 /*Charecter Stuffing*/
            ins('s',0);
            ins('e',5);
            //printf("%s",fr);
            for(i=0;i<strlen(fr);i++)
            {
             if(fr[i]=='d'&&fr[i+1]=='l'&&fr[i+2]=='e'&&i!=3&&i!=strlen(fr)-3)
             {
              ins('m',i+3);i+=6;
             }
            }
            printf("\n\nStuffed bit is \t%s\t",fr);getch();
            /*Destuffing*/
            cnt=0;
            del(0,6);
            del(strlen(fr)-6,6);
            for(i=0;i<strlen(fr);i++)
            {
             if(fr[i]=='d'&&fr[i+1]=='l'&&fr[i+2]=='e')
             {
             cnt+=1;
             if(cnt==1)
             {         del(i,3);}
             }else
             {cnt=0;}
            }
            printf("\n\nDestuffed bit is \t%s",fr);getch();
            return 0;
 }
void ins(char in,int p)
{
 char dup[50];
 int i;
 strcpy(dup,fr);
            if(in=='s')
            {
             fr[p]='s';
             fr[p+1]='t';
             fr[p+2]='x';
             fr[p+3]='d';
             fr[p+4]='l';
             fr[p+5]='e';
            for(i=p+6;i<strlen(fr)+6;i++)
            {
             fr[i]=dup[i-6];
            }
            }
            if(in=='e')
            {
             strcat(fr,"etxdle");return;
            }
            if(in=='m')
            {
             fr[p]='d';
             fr[p+1]='l';
             fr[p+2]='e';
             for(i=p+3;i<strlen(fr)+3;i++)
             {
             fr[i]=dup[i-3];
             }
            }
            }
void del(int q,int n)
{
 int i;
            for(i=q;i<strlen(fr);i++)
            {
             fr[i]=fr[i+n];
            }
}
OUTPUT:



Monday, 24 December 2012

IP/IPv4: Internet Protocol

Overview 

- The Internet Protocol (IP) is a network-layer (Layer 3) protocol in the OSI model that contains addressing information and some control information to enable packets being routed in network.
-  IP is the primary network-layer protocol in theTCP/IP protocol suite . Along with the Transmission Control Protocol (TCP ), IP represents the heart of the Internet protocols. IP is equally well suited for both LAN and WAN communications.

 -  IP (Internet Protocol) has two primary responsibilities:
     providing connectionless,
     best-effort delivery of datagrams through a network; and
     providing fragmentation and reassembly of datagrams to support data links with different maximum-transmission unit (MTU) sizes.

- The IP addressing scheme is integral to the process of routing IP datagrams through an internetwork. Each IP address has specific components and follows a basic format. These IP addresses can be subdivided and used to create addresses for subnetworks.
- Each computer (known as host) on a TCP/IP network is assigned an unique logical address (32-bit in IPv4) that is divided into two main parts:
 the network number and the host number.

- The network number identifies a network and must be assigned by the Internet Network Information Center (InterNIC) if the network is to be part of the Internet. An Internet Service Provider (ISP) can obtain blocks of network addresses from the InterNIC and can itself assign address space as necessary. The host number identifies a host on a network and is assigned by the local network administrator.

- When you send or receive data (for example, an e-mail note or a Web page), the message gets divided into little chunks called packets. 
- Each of these packets contains both the sender's Internet address and the receiver's address.Because a message is divided into a number of packets, each packet can, if necessary, be sent by a different route across the Internet.
- Packets can arrive in a different order than the order they were sent in. The Internet Protocol just delivers them. It's up to another protocol, the Transmission Control Protocol ( TCP) to put them back in the right order.

All other protocols within the TCP/IP suite, except ARP and RARP, use IP to route frames from host to host.

Protocol Structure - IP/IPv4 Header (Internet Protocol version 4)

- Version -the version of IP currently used.

- IP Header Length (IHL) - datagram header length. Points to the beginning of the data. The minimum value for a correct header is 5.

- Type-of-Service- Indicates the quality of service desired by specifying how an upper-layer protocol would like a current datagram to be handled, and assigns datagrams various levels of importance. This field is used for the assignment of Precedence, Delay, Throughput and Reliability.

- Total Length- Specifies the length, in bytes, of the entire IP packet, including the data and header. The maximum length could be specified by this field is 65,535 bytes. Typically, hosts are prepared to accept datagrams up to 576 bytes.

- Identification- Contains an integer that identifies the current datagram. This field is assigned by sender to help receiver to assemble the datagram fragments.

- Flags - Consists of a 3-bit field of which the two low-order (least-significant) bits control fragmentation. The low-order bit specifies whether the packet can be fragmented. The middle bit specifies whether the packet is the last fragment in a series of fragmented packets. The third or high-order bit is not used.

- Fragment Offset - This 13 bits field indicates the position of the fragment's data relative to the beginning of the data in the original datagram, which allows the destination IP process to properly reconstruct the original datagram.

- Time-to-Live - It is a counter that gradually decrements down to zero, at which point the datagram is discarded. This keeps packets from looping endlessly.

- Protocol- Indicates which upper-layer protocol receives incoming packets after IP processing is complete.

- Header Checksum- Helps ensure IP header integrity. Since some header fields change, e.g., Time To Live, this is recomputed and verified at each point that the Internet header is processed.

- Source Address-Specifies the sending node.

- Destination Address-Specifies the receiving node.

- Options- Allows IP to support various options, such as security.

- Data - Contains upper-layer information.

CN 3-2 lab programs

PROGRAM 1: Implement the data link layer framing methods such as character stuffing and bit stuffing.

                                                 CHARACTER STUFFINIG 

DESCRIPTION:

The first framing method is used in the header to specify the number of characters in frame.
In second method delimit by DLESTX DLEETX.
One way to solve this is to have the senders data link layer, insert an ASCII DLE char just before each accidental flag bit character in data.

FOR EXAMPLE:

‘AB’

Data send by network layer is A B X.
Data after being character stuffing by data link layer DLE STX A DLE ETX.
After destuffing we get original data.

i.e., AB \0

ALGORITHM:

- First read the bit stream clearly.

- Scan for data stream for the continuous 6 is if it is form the in stream bit delimiter.

- In character stuffing also read the given frame clearly.

- Insert DLESTX, data link escaping character starting of the frame.

- If DLE occurs then in the frame insert another DLE after DLE.

- At the end of frame insert DLEETX data link escaping character ending text.

- To destuff the above stuffed frame use simply remove appended DLESTX, DLE and DLEETX.

                                                       BIT STUFFING:
DESCRIPTION:

Starting and ending flags with bit stuffing is one of the framing methods to specify the number of characters or bits in a frame.
Each frame in this techniques begins and ends with a special bit pattern 01111110 called a flag byte.
Whenever the sender’s data link layer encodes five consecutive ones in the data, it automatically stuffs a 0 bit into the outgoing bit stream.

FOR EXAMPLE:


Original data
011011111111111111110010

Data as they appear on the line
01101111101111101111100010

Stuffed Bits

Data stored in the receiver’s memory after destuffing
011011111111111111110010

C CODE FOR BIT STUFFING: 

#include<stdio.h>
#include<conio.h>
void ins(char,int);
void del(int);
char fr[50];
main()
{
int i,cnt=0;
clrscr();
printf("Enter the frame:\t");
gets(fr);
for(i=0;i<strlen(fr);i++)
{
if(fr[i]!='0'&&fr[i]!='1')
{
printf("ivalid input\n press any key to exit");
getch();
exit(0);
}
}
/*Stuffing the given bit*/
for(i=0;i<strlen(fr);i++)
{
if(cnt==5)
{
cnt=0;
ins('0',i);
}
if(fr[i]=='1'){cnt+=1;}else{cnt=0;}
}
printf("\n\nStuffed Frame is \t%s\n\n",fr);
getch();
/*Destuffing of aframe*/
cnt=0;
for(i=0;i<strlen(fr);i++)
{
if(cnt==5)
{
cnt=0;
del(i);
}
if(fr[i]=='1'){cnt+=1;}else{cnt=0;}
}
printf("Destuffed Frame is \t%s",fr);
getch();
return 0;
}
void ins(char in,int p)
{
char dup[50];
int i;
strcpy(dup,fr);
fr[p]=in;
for(i=p+1;i<strlen(fr)+1;i++)
{
fr[i]=dup[i-1];
}
}
void del(int q)
{
int i;
for(i=q;i<strlen(fr);i++)
{ 
fr[i]=fr[i+1]; 
} 
}

Explain about Local Area Network (LAN)


Local Area Networks:

Local area networks, generally called LANs, are privately-owned networks within a single building or campus of up to a few kilometres in size. They are widely used to connect personal computers and workstations in company offices and factories to share resources (e.g., printers) and exchange information.

LANs are distinguished from other kinds of networks by three characteristics:
(1) Their size,
(2) Their transmission technology, and
(3) Their topology.

             LANs are restricted in size, which means that the worst-case transmission time is bounded and known in advance. Knowing this bound makes it possible to use certain kinds of designs that would not otherwise be possible. It also simplifies network management.

             LANs may use a transmission technology consisting of a cable to which all the machines are attached, like the telephone company party lines once used in rural areas. Traditional LANs run at speeds of 10 Mbps to 100 Mbps, have low delay (microseconds or nanoseconds), and make very few errors. Newer LANs operate at up to 10 Gbps.

             Various topologies are possible for broadcast LANs. Figure1 shows two of them. In a bus (i.e., a linear cable) network, at any instant at most one machine is the master and is allowed to transmit. All other machines are required to refrain from sending. An arbitration mechanism is needed to resolve conflicts when two or more machines want to transmit simultaneously. The arbitration mechanism may be centralized or distributed. IEEE 802.3, popularly called Ethernet, for example, is a bus-based broadcast network with decentralized control, usually operating at 10 Mbps to 10 Gbps. Computers on an Ethernet can transmit
whenever they want to; if two or more packets collide, each computer just waits a random time and tries again later.
                  fig: Two Broadcast networks            a) Bus                 b)Ring


          A second type of broadcast system is the ring. In a ring, each bit propagates around on its own, not waiting for the rest of the packet to which it belongs. Typically, each bit circumnavigates the entire ring in the time it takes to transmit a few bits, often before the complete packet has even been transmitted. As with all other broadcast systems, some rule is needed for arbitrating simultaneous accesses to the ring. Various methods, such as having the machines take turns, are in use. IEEE 802.5 (the IBM token ring), is a ring-based LAN operating at 4 and 16 Mbps. FDDI is another example of a ring network.

Wednesday, 12 December 2012

CONGESTION CONTROL


Congestion: When too many packets are sent to a subnet more than its capacity, the Situation that arises is called   congestion.

Reasons for Congestion:

  1. If input packets coming   from 3 or 4 lines, requires   only   one particular output   line.
  2. If routers are supplied   with infinite   amount of memory, packets take longtime to reach to the front of queue where duplicates are generated as they are timed out.
  3. Slow processors cause congestion.
  4. Low bandwidth lines also cause congestion.
  5. Congestion feeds upon itself and cause congestion.
Congestion Control Algorithms: 

These algorithms control congestion. These are mainly divided into two groups:

  1. Open Loop Solutions.                                     2. Closed Loop Solutions.
 Open Loop Solutions attempt to solve the problems by good design to make sure it does not occur in the first place. Once the system is up and running, mid course corrections are not made.
 Closed Loop Solutions are based on the concepts of a feedback loop. It has 3 parts.
Ø  Monitor the system to detest when and where congestion occurs.
Ø  Pass this information to places where action can be taken.
Ø  Adjust system operation to correct the problem.

These closed loop algorithms are further divided into two categories:
  • Implicit feedback:   The source reduces the congestion existence by making local observations.
  • Explicit feed back:   Packets are sent back from the point of congestion to warn source
Open Loop Solutions:
Ø  Congestion Prevention Policies
Ø  Traffic Shaping
Ø  Flow Specifications

1. Congestion prevention policies:

Congestion is prevented using appropriate policies at various levels.

Layer

Policies

Transport

  1. Retransmission policy
  2. Out-of-order caching policy
  3. Acknowledgement policy
  4. Flow control policy
  5. Timeout Determination

Network

  1. Virtual circuits versus data gram inside the subnet
  2. Packet queuing service policy
  3. Packet discard policy
  4. Routing algorithm
  5. Packet lifetime Management

Data Link

  1. Retransmission policy
  2. Out-of-order catching policy
  3. Acknowledgement policy
  4. Flow control policy

Retransmission policy:   Deals with how fast a sender times out and what it transmits upon time out.
Out-of–order Catching policy:If receivers routinely discard all out-of-order packets, (packets arrived without order) they have to be retransmitted.
Acknowledgement policy: If each packet is acknowledged immediately, acknowledged packets generate extra traffic.This policy deals with piggybacking.
Flow Control policy: A tight flow control scheme (ex: a small window) reduces the data rate and thus helps fight congestion.
Timeout Determination:   It is harder as transit time across the network is less predictable than transit time over a wire between two routers.
Virtual Circuits vs Data grams: This affects congestion as many algorithms work only with virtual circuits.
Packets queuing and Service policy:  Relates to whether routers have one queue per input line, one queue per output line or both.
Packet Discard policy:    Tells which packet is dropped when there is no space.
Routing Algorithm:   With this, Traffic is spreaded over all the lines.
Packet lifetime management: Deals with how long a packet may live before being discarded.

 2.  Traffic Shaping: 
It is the process of forcing the packets to be transmitted at a more predictable rate. This approach is widely used in ATM Networks to manage congestion. When a virtual circuit is set up, the user and the subnet agree on a certain traffic pattern for that circuit. Monitoring a traffic flow based on agreement made is called “Traffic Policing”.

Traffic shaping can be implemented with any of the two techniques:
 ·         Leaky Bucket Algorithm                                 ·         Token Bucket Algorithm

The Leaky Bucket Algorithm:   

Imagine a bucket with a small hole in the bottom. No matter, at what rate water enters the bucket, the outflow is at a constant rate, ‘p’, when there is any water in the bucket and ‘r’, when the bucket is empty. Also, once the bucket is full, any additional water entering it spills over the sides and is lost. The same idea can be applied to packets.

 
               Conceptually, each host is connected to the network by an interface containing a leaky bucket, i.e., a finite internal queue. If a packet arrives at the queue when it is full, it is discarded. In other words, if one or more processes with in the host try to send a packet when the maximum number are already queued, the new packet is unceremoniously discarded. This arrangement can be built into the h/w inter face or simulated by the host operating system. It was first proposed by TURNER and is called the “ LEAKY BUCKET ALGORITHM ”.
                                              
                                                                                                                                                                                                                                                            
               The host is allowed to put one packet per clock tick onto the network, which turns an uneven flow of packets from the user processes inside the host into an even flow of packets onto the network, smoothing out bursts and greatly reducing the chances of congestion.

The Token Bucket Algorithm: The algorithm that allows the output to speedup when large bursts arrive and one that never loses data is the TOKEN BUCKET ALGORITHM. In this algorithm, the leaky bucket holds tokens, generated by a clock at the rate of one token every DT sec. This algorithm allows to save up permission by hosts, up to the maximum size of the bucket, ‘n’ i.e., bursts of up to ‘n’ packets can be sent at once, allowing some burstiness in output stream and giving faster response to sudden bursts of input.


In the above circuit, we see a bucket   holding 3 tokens, with 5 packets waiting to be transmitted. For a packet to be transmitted, it must be capture and destroy one token. In the above example, 3 out of 5 packets have gotten through by capturing the 3 tokens in the bucket, but the other 2 are struck waiting for 2 more tokens to be generated. The implementation of the token bucket algorithm is just a variable that counts tokens. The counter is incremented by 1, every   DT and decremented by 1, when a packet is sent. When the counter hits ‘0’, no packets may be sent.
The major advantage of the token bucket algorithm is that it throws away tokens instead of packets, when the bucket fills up.

THE NETWORK LAYER


Services provided to the transport layer:

  1. The services are independent of subnet technology.
  2. The transport layer is shielded from the number, type and topology of subnets present.
  3. Virtual Circuit   à  A connection of the subnet.

    Data gram  à  Independent packets of a connection-less organization.
     
    The network addresses made available to  transport layer uses a uniform numbering plan .





Internetworking Styles:
·         Concatenated Virtual Circuit Subnets                        (Connection-Oriented)
·         Datagram Model                                             (Connection-Less)

Concatenated Virtual Circuit Subnets:
            A connection to a host in a distant network is set up in a way similar to the way connections are normally established. The subnet sees that the destination is remote and builds a virtual circuit to the router nearest the destination network. Then it constructs a virtual circuit from that router to an external gateway, which records the existence of virtual circuit in its tables and proceeds to build another virtual circuit to a router in next subnet. This process continues until the destination host has been reached.

Once data packets begin following along the path, each gateway relays incoming packets, converting between packet formats and virtual circuit numbers as needed. Clearly, all data packets must traverse the same sequence of gateways, and thus arrive in order.

            The essential feature of this approach is that a sequence of virtual circuits is set up from the source through one or more gateways to the destination. Each gateway maintains tables telling which virtual circuits pass through it, where they are to be routed, and what the new virtual circuit number is.

Datagram Model:
In this model, the only service the n/w layer offers to the transport layer is the ability to inject datagrams into the subnet and hope for the best. Datagrams from one host to another host travel through different routes through the inter network. A routing decision is made separately for each packet, possibly depending on the traffic at the moment the packet is sent. This strategy can use multiple routes and thus achieve a higher band width than the concatenated virtual circuit model.

   Differences between a Virtual Circuit and a Datagram:

s.no          
Issue
    Data gram subnet
   Virtual circuit subnet

1.

2.



3.


4.



5.

Circuit setup

Addressing



State information

Routing



Congestion control

Not required

Each packet contains the full source and destination address

Subnet does not hold state information

Each packet is routed independently


Difficult

Required

Each packet contains a short virtual circuit number


Each virtual circuit  requires subnet table space

Route chosen when virtual circuit is setup &all packets follow this route

Easy, if enough buffers can be allocated in advance for each virtual circuit




Tuesday, 11 December 2012

what is IPv6?

Why a new IP generation ?
-> IPv4 adresses become a scarce resource (32 bits, nominally about 4.3'' adresses)

-> The final block of IPv4 address has been allocated this year (2011).

-> The shortage of IPv4 addresses is a practical problem

-> An initial addressing crisis in IPv4 was alleviated by CIDR (-> more flexible allocation of address blocks)...
->   ... giving IPv4 addressing a bit more lifetime.

IPv6 Design Goals:

-  simple header structure
-  improved routing by address aggregation
-  better multi-casting and any-casting
-  possibility of using Quality of Service
-  integrated security
-  support and identification of traffic flows

Transition to IPv6 - 3 Basic Methods:

-> Dual Stack
        Implemented in hosts to allow interworking with IPv6 and IPv4 networks
-> Tunnels
        Used for IPv6 to IPv6 interworking via an IPv4 network
-> Translators
        Used for IPv6 to IPv4 interworking by translating the headers

Types of IPv6 Addresses:

Like IPv4…
1.Unicast
   An identifier for a single interface. A packet sent to a unicast address is delivered to the interface identified by that address.
2.Multicast (a mandatory part of IPv6)
   An identifier for a set of interfaces (typically belonging to different nodes). A packet sent to a multicast address is delivered to all interfaces identified by that address.
3. Anycast:
   An identifier for a set of interfaces (typically belonging to different nodes). A packet sent to an anycast address is delivered to the "nearest" one, according to the routing protocols' measure of distance.

What is not in IPv6

Broadcast
* There is no broadcast in IPv6.
* This functionality is taken over by multicast.
* One consequence of this is that the “all 0s” and “all 1s” addresses are legal in IPv6.

IPv6 addresses

Representation of Addresses:

-> All IPv6 addresses are 128 bits
-> Written as 8 sets of 4 hex digits (16 bits each) separated by colons
      Leading zeros in group may be omitted
      One contiguous set of all-zero groups may be replaced by “::”
      Only one such group can be replaced
-> Example
          3ffe:3700:0200:00ff:0000:0000:0000:0001
-> This can be written as
        3ffe:3700:200:ff:0:0:0:1 or   3ffe:3700:200:ff::1
-> All three reduction methods are used here.

Examples of types of Unicast Addresses:

-> Normal addresses
2001:0db8:85a3:0000:0000:8a2e:0370:7334
-> Unspecified address
      All zeros (::) - no such host
      Used as source address during initialization
      Also used in representing default
-> Compatible IPv4 addresses
     E.g. form ::ffff:a.b.c.d where a.b.c.d is the Ipv4 address
     Provides IPv6 stations with access to IPv4 stations
-> Loopback address
     Low-order one bit (::1)
     Same as 127.0.0.1 in IPv4

Ipv6 Addresses:

-> Multicast
     FF/8
      FFfs                                 f=flags, s=scope
-> Anycast
     subnet-prefix::0
-> Unicast is everything else, 
     eg.  D202:23F8:17::12A1:0:A3
-> Site local like 10..., 172.16.., 192.168..
     FE/8
-> Example address (used in documentation)
     2001:DB8...

Embedding IPv6 addresses in URL’s

To use a literal IPv6 address in a URL, the address should be enclosed in "[" and "]" characters. For example the following IPv6 addresses:
FEDC:BA98:7654:3210:FEDC:BA98:7654:3210
3ffe:2a00:100:7031::1
::ffff:192.9.5.5
2010:836B:4179::836B:4179
would be represented as in the following example URLs: 
http://[2001:BA98:7654:3210::BA98:7654:3210]:80/index.html
http://[3ffe:2a00:100:7031::1]
http://[::ffff:192.9.5.5]/ipng
http://[2010:836B:4179::836B:4179]
Source: RFC 2732

Compatibility with IPv4 Applications:

-> Applications should be able to hold a combination of IPv4/TCP, IPv4/UDP, IPv6/TCP and IPv6/UDP sockets simultaneously within the same process.
-> Applications using the original API should continue to operate as they did on systems supporting only IPv4. That is, they should continue to interoperate with IPv4 nodes.
-> The IPv4 address is encoded into the low-order 32 bits of the IPv6 address, and the high-order 96 bits hold a set prefix 0:0:0:0:0:FFFF. IPv4-mapped addresses are written as follows:
                                         : : FFFF:<IPv4-address>
-> These addresses can be generated automatically by the getaddrinfo() function.
-> Applications may use AF_INET6 sockets to open TCP connections to IPv4 nodes, or send UDP packets to IPv4 nodes, by simply encoding the destination's IPv4 address as an IPv4-mapped IPv6 address.