Tuesday, 23 August 2011

protocols in computer networks


Introduction to Computer Network Protocols
The word protocol is derived from the Greek word “protocollon” which means a leaf of paper glued to manuscript volume. In computer protocols means a set of rules, a communication language or set of standards between two or more computing devices. Protocols exist at the several levels of the OSI (open system interconnectivity) layers model. In the telecommunication system, there are one more protocols at each layer of the telephone exchange. On the internet, there is a suite of the protocols known as TCP/IP protocols that are consisting of transmission control protocol, internet protocol, file transfer protocol, dynamic host configuration protocol, Border gateway protocol and a number of other protocols. 
In the telecommunication, a protocol is set of rules for data representation, authentication, and error detection. The communication protocols in the computer networking are intended for the secure, fast and error free data delivery between two communication devices. Communication protocols follow certain rules for the transmission of the data.
Protocols Properties
The widespread use of the communication protocols is a prerequisite to the internet. The term TCP/IP refers to the protocols suite and a pair of the TCP and IP protocols are the most important internet communication protocols. Most protocols in communication are layered together where the various tasks listed above are divided. Protocols stacks refer to the combination of the different protocols. The OSI reference model is the conceptual model that is used to represent the protocols stacks. There are different network protocols that perform different functions. Following is the description of the some of the most commonly used protocols.

Different protocols perform different functions so it is difficult to generalize the properties of the protocols. There are some basic properties of most of the protocols.
• Detection of the physical (wired or wireless connection)
• Handshaking
• How to format a message.
• How to send and receive a message.
• Negotiation of the various connections
• Correction of the corrupted or improperly formatted messages.
• Termination of the session.
HTTP (Hyper Text Transfer Protocol)
Hypertext transfer protocol is a method of transmitting the information on the web. HTTP basically publishes and retrieves the HTTP pages on the World Wide Web. HTTP is a language that is used to communicate between the browser and web server. The information that is transferred using HTTP can be plain text, audio, video, images, and hypertext. HTTP is a request/response protocol between the client and server. Many proxies, tunnels, and gateways can be existing between the web browser (client) and server (web server). An HTTP client initializes a request by establishing a TCP connection to a particular port on the remote host (typically 80 or 8080). An HTTP server listens to that port and receives a request message from the client. Upon receiving the request, server sends back 200 OK messages, its own message, an error message or other message.
POP3 (Post Office Protocol)
In computing, e-mail clients such as (MS outlook, outlook express and thunderbird) use Post office Protocol to retreive emails from the remote server over the TCP/IP connection. Nearly all the users of the Internet service providers use POP 3 in the email clients to retrieve the emails from the email servers. Most email applications use POP protocol.
SMTP (Simple Mail Transfer Protocol)
Simple Mail Transfer Protocol is a protocol that is used to send the email messages between the servers. Most email systems and email clients use the SMTP protocol to send messages to one server to another. In configuring an email application, you need to configure POP, SMTP and IMAP protocols in your email software. SMTP is a simple, text based protocol and one or more recipient of the message is specified and then the message is transferred. SMTP connection is easily tested by the Telnet utility. SMTP uses the by default TCP port number 25
FTP (File Transfer Protocol)
FTP or file transfer protocol is used to transfer (upload/download) data from one computer to another over the internet or through or computer network. FTP is a most commonly communication protocol for transferring the files over the internet. Typically, there are two computers are involved in the transferring the files a server and a client. The client computer that is running FTP client software such as Cuteftp and AceFTP etc initiates a connection with the remote computer (server). After successfully connected with the server, the client computer can perform a number of the operations like downloading the files, uploading, renaming and deleting the files, creating the new folders etc. Virtually operating system supports FTP protocols.
IP (Internet Protocol)
An Internet protocol (IP) is a unique address or identifier of each computer or communication devices on the network and internet. Any participating computer networking device such as routers, computers, printers, internet fax machines and switches may have their own unique IP address. Personal information about someone can be found by the IP address. Every domain on the internet must have a unique or shared IP address.
DHCP (Dynamic Host Configuration Protocol)
The DHCP or Dynamic Host Configuration Protocol is a set of rules used by a communication device such as router, computer or network adapter to allow the device to request and obtain and IP address from a server which has a list of the larger number of addresses. DHCP is a protocol that is used by the network computers to obtain the IP addresses and other settings such as gateway, DNS, subnet mask from the DHCP server. DHCP ensures that all the IP addresses are unique and the IP address management is done by the server and not by the human. The assignment of the IP addresses is expires after the predetermined period of time. DHCP works in four phases known as DORA such as Discover, Observe, Request and Authorize
IMAP (Internet Message Access Protocol)
The Internet Message Access Protocol known as IMAP is an application layer protocol that is used to access to access the emails on the remote servers. POP3 and IMAP are the two most commonly used email retrieval protocols. Most of the email clients such as outlook express, thunderbird and MS outlooks support POP3 and IMAP. The email messages are generally stored on the email server and the users generally retreive these messages whether by the web browser or email clients. IMAP is generally used in the large networks. IMAP allows users to access their messages instantly on their systems.
ARCNET
ARCNET is a local area network technology that uses token bus scheme for managing line sharing among the workstations. When a device on a network wants to send a message, it inserts a token that is set to 1 and when a destination device reads the message it resets the token to 0 so that the frame can be used by another device.
FDDI
Fiber distributed data interface (FDDI) provides a standard for data transmission in a local area network that can extend a range of 200 kilometers. The FDDI uses token ring protocol as its basis. FDDI local area network can support a large number of users and can cover a large geographical area. FDDI uses fiber optic as a standard communication medium. FDDI uses dual attached token ring topology. A FDDI network contains two token rings and the primary ring offers the capacity of 100 Mbits/s. FDDI is an ANSI standard network and it can support 500 stations in 2 kilometers.
UDP
The user datagram protocol is a most important protocol of the TCP/IP suite and is used to send the short messages known as datagram. Common network applications that uses UDP are DNS, online games, IPTV, TFTP and VOIP. UDP is very fast and light weight. UDP is an unreliable connectionless protocol that operates on the transport layer and it is sometimes called Universal Datagram Protocol.
X.25
X.25 is a standard protocol suite for wide area networks using a phone line or ISDN system. The X.25 standard was approved by CCITT now ITU in 1976.
TFTP
Trivial File Transfer Protocol (TFTP) is a very simple file transfer protocol with the very basic features of the FTP. TFTP can be implemented in a very small amount of memory. TFTP is useful for booting computers such as routers. TFTP is also used to transfer the files over the network. TFPT uses UDP and provides no security features.
SNMP
The simple network management protocol (SNMP) forms the TCP/IP suite. SNMP is used to manage the network attached devices of the complex network.
PPTP
The point to point tunneling protocol is used in the virtual private networks. PPP works by sending regular PPP session. PPTP is a method of implementing VPN networks.
xmpp
Extensible Messaging and Presence Protocol (XMPP) is an open-standard communications protocol for message-oriented middleware based on XML(Extensible Markup Language). The protocol was originally named Jabber, and was developed by the Jabber open-source community in 1999 for near-real-time, extensible instant messaging (IM), presence information, and contact list maintenance. Designed to be extensible, the protocol today also finds application in VoIP and file transfer signaling.
tls
Transport Layer Security (TLS) and its predecessor, Secure Sockets Layer (SSL), are cryptographic protocols that provide communication security over the Internet. TLS and SSL encrypt the segments of network connections above the Transport Layer, using asymmetric cryptography for privacy and a keyedmessage authentication code for message reliability.
telnet
Telnet is a network protocol used on the Internet or local area networks to provide a bidirectional interactive text-oriented communications facility using a virtualterminal connection. User data is interspersed in-band with Telnet control information in an 8-bit byte oriented data connection over the Transmission Control Protocol (TCP).
ssh
Secure Shell or SSH is a network protocol that allows data to be exchanged using a secure channel between two networked devices. The two major versions of the protocol are referred to as SSH1 or SSH-1 and SSH2or SSH-2. Used primarily on Linux and Unix based systems to access shell accounts, SSH was designed as a replacement for Telnet and other insecure remote shells, which send information, notably passwords, inplaintext, rendering them susceptible to packet analysis. The encryption used by SSH is intended to provide confidentiality and integrity of data over an unsecured network, such as the Internet.

Monday, 22 August 2011

some tricky questions on bit manupulation:

1.Write a program to swap odd and even bits in an integer with as few instructions as possible (e.g., bit 0 and bit 1 are swapped, bit 2 and bit 3 are swapped, etc).


ans:Mask all odd bits with 10101010 in binary (which is 0xAA), then shift them left to put them in the even bits. Then, perform a similar operation for even bits. This takes a total 5 instructions.
public static int swapOddEvenBits(int x)
 {
return ( ((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1) );
}


2.An inline function to test whether a number is power of 2?

ans:((n & (n-1)) == 0).

adding a node in linked list which is in ascending order


/* adds node to an ascending order linked list */
void add ( struct node **q, int num )
{
struct node *r, *temp = *q ;

r = malloc ( sizeof ( struct node ) ) ;
r -> data = num ;

/* if list is empty or if new node is to be inserted before the first node */
if ( *q == NULL || ( *q ) -> data > num )
{
*q = r ;
( *q ) -> link = temp ;
}
else
{
/* traverse the entire linked list to search the position to insert the
   new node */
while ( temp != NULL )
{
if ( temp -> data <= num && ( temp -> link -> data > num ||
temp -> link == NULL ))
{
r -> link = temp -> link ;
temp -> link = r ;
return ;
}
temp = temp -> link ;  /* go to the next node */
}
}
}

linked list implementation


 Program to maintain a linked list */

#include <stdio.h>
#include <conio.h>
#include <alloc.h>

/* structure containing a data part and link part */
struct node
{
int data ;
struct node * link ;
} ;

void append ( struct node **, int ) ;
void addatbeg ( struct node **, int ) ;
void addafter ( struct node *, int, int ) ;
void display ( struct node * ) ;
int count ( struct node * ) ;
void delete ( struct node **, int ) ;

void main( )
{
struct node *p ;
p = NULL ;  /* empty linked list */

printf ( "\nNo. of elements in the Linked List = %d", count ( p ) ) ;
append ( &p, 14 ) ;
append ( &p, 30 ) ;
append ( &p, 25 ) ;
append ( &p, 42 ) ;
append ( &p, 17 ) ;

display ( p ) ;

addatbeg ( &p, 999 ) ;
addatbeg ( &p, 888 ) ;
addatbeg ( &p, 777 ) ;

display ( p ) ;

addafter ( p, 7, 0 ) ;
addafter ( p, 2, 1 ) ;
addafter ( p, 5, 99 ) ;

display ( p ) ;
printf ( "\nNo. of elements in the Linked List = %d", count ( p ) ) ;

delete ( &p, 99 ) ;
delete ( &p, 1 ) ;
delete ( &p, 10 ) ;

display ( p ) ;
printf ( "\nNo. of elements in the Linked List = %d", count ( p ) ) ;
}

/* adds a node at the end of a linked list */
void append ( struct node **q, int num )
{
struct node *temp, *r ;

if ( *q == NULL ) /* if the list is empty, create first node */
{
temp = malloc ( sizeof ( struct node ) ) ;
temp -> data = num ;
temp -> link = NULL ;
*q = temp ;
}
else
{
temp = *q ;

/* go to last node */
while ( temp -> link != NULL )
temp = temp -> link ;

/* add node at the end */
r = malloc ( sizeof ( struct node ) ) ;
r -> data = num ;
r -> link = NULL ;
temp -> link = r ;
}
}

/* adds a new node at the beginning of the linked list */
void addatbeg ( struct node **q, int num )
{
struct node *temp ;

/* add new node */
temp = malloc ( sizeof ( struct node ) ) ;

temp -> data = num ;
temp -> link = *q ;
*q = temp ;
}

/* adds a new node after the specified number of nodes */
void addafter ( struct node *q, int loc, int num )
{
struct node *temp, *r ;
int i ;

temp = q ;
/* skip to desired portion */
for ( i = 0 ; i < loc ; i++ )
{
temp = temp -> link ;

/* if end of linked list is encountered */
if ( temp == NULL )
{
printf ( "\nThere are less than %d elements in list", loc ) ;
return ;
}
}

/* insert new node */
r = malloc ( sizeof ( struct node ) ) ;
r -> data = num ;
r -> link = temp -> link ;
temp -> link = r ;
}

/* displays the contents of the linked list */
void display ( struct node *q )
{
printf ( "\n" ) ;

/* traverse the entire linked list */
while ( q != NULL )
{
printf ( "%d ", q -> data ) ;
q = q -> link ;
}
}

/* counts the number of nodes present in the linked list */
int count ( struct node * q )
{
int c = 0 ;

/* traverse the entire linked list */
while ( q != NULL )
{
q = q -> link ;
c++ ;
}

return c ;
}

/* deletes the specified node from the linked list */
void delete ( struct node **q, int num )
{
struct node *old, *temp ;

temp = *q ;

while ( temp != NULL )
{
if ( temp -> data == num )
{
/* if node to be deleted is the first node in the linked list */
if ( temp == *q )
*q = temp -> link ;

/* deletes the intermediate nodes in the linked list */
else
old -> link = temp -> link ;

/* free the memory occupied by the node */
free ( temp ) ;
return ;
}

/* traverse the linked list till the last node is reached */
else
{
old = temp ;  /* old points to the previous node */
temp = temp -> link ;  /* go to the next node */
}
}

printf ( "\nElement %d not found", num ) ;
}

linked lists


1.snippet to reverse a linked list:

void reverse ( struct node **x )
{
struct node *q, *r, *s ;

q = *x ;
r = NULL ;

/* traverse the entire linked list */
while ( q != NULL )
{
s = r ;
r = q ;
q = q -> link ;
r -> link = s ;
}

*x = r ;
}


2.code to detect a loop in a linked list.

typedef struct node
{
void *data;
struct node *next;
}mynode;

mynode * find_loop(NODE * head)
{
mynode *current = head;
while(current->next != NULL)
{
mynode *temp = head;
while(temp->next != NULL && temp != current)
{
if(current->next == temp)
{
printf("\nFound a loop.");
return current;
}
temp = temp->next;
}
current = current->next;
}
return NULL;
}

dns server implementation

import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class dns implements ActionListener {

JLabel domain1,ip1;
JTextField dom,ip;
JButton getip,getdom;
public static void main(String[] args) {
dns obj = new dns();
obj.createui();
}
private void createui()
{
JFrame frame = new JFrame("JDBC All in One");
Container c = frame.getContentPane();
c.setLayout(new BoxLayout(c,BoxLayout.Y_AXIS));
domain1=new JLabel("domain:");
                ip1=new JLabel("ip address:");
                dom = new JTextField();
ip = new JTextField();
                JPanel pnlInput = new JPanel(new GridLayout(1,2));
                pnlInput.add(domain1);
pnlInput.add(dom);
pnlInput.add(ip1);
pnlInput.add(ip);
getip = new JButton("getip");
                getip.addActionListener(this);
                getdom=new JButton("getdom");
                getdom.addActionListener(this);
                JPanel pnlButton = new JPanel(new GridLayout(1,2));
                pnlButton.add(getip);
                 pnlButton.add(getdom);
                frame.add(pnlInput);
                frame.add(pnlButton);
frame.setSize(400,100);
frame.setVisible(true);
}


public void actionPerformed(ActionEvent evt)
   {
String cmd = evt.getActionCommand();

if(cmd.equals("getip"))
{
try
{
  InetAddress thisIp = java.net.InetAddress.getByName(dom.getText());;
  ip.setText(thisIp.getHostAddress());
}
catch(Exception e)
{
e.printStackTrace();
}

}
else if(cmd.equals("getdom"))
{
try
{
InetAddress x=InetAddress.getByName(ip.getText());
String hostname = x.getHostName();
dom.setText(hostname);
}
        catch(Exception e)
   {
  e.printStackTrace();
        }
    }
  }
}