IP Header in c
On-campus and online computer science courses to Learn the basic concepts of Computer Science.This tutorial will cover c ,c++, java, data structure and algorithm,computer graphics,microprocessor,analysis of algorithms,Digital Logic Design and Analysis,computer architecture,computer networks,operating system.
code in C:#include<stdio.h>
int i,j,counter=0,no,x,y,n,flag,bin[200],z,len,start,end,p,decimal;
unsigned int sum,k;
char a[200];
unsigned int dec( int x, int y);
void binary(unsigned int n, int len);
int bin_dec( int start, int end);
int main()
{
printf("Enter the TCP header\n");
scanf("%s",a);
printf("IP Version: %lu\n",dec(0,1));
printf("Header Length: %lu\n",4*dec(1,2));
binary(dec(2,4),8);
printf("Precedence: %lu\n",bin_dec(0,3) );
if(bin[3] == 1)
printf("Minimize Delay Requested\n");
if(bin[4] == 1)
printf("Maximize Throughput Requested\n");
if(bin[5] == 1)
printf("Maximize Reliability Requested\n");
if(bin[6] == 1)
printf("Minimize Cost Requested\n");
printf("Total Length: %lu\n",dec(4,8));
printf("Identification: %lu\n",dec(8,12));
binary( dec(12,16) ,16 );
if(bin[1] == 1)
printf("Do not Fragment Packet\n");
else
printf("Can be Fragmented\n");
if(bin[2] == 1)
printf("More Fragments pending\n");
else
printf("No more Fragments pending\n");
printf("Fragmentation Offset: %lu \n",( 8*bin_dec(3,16)-4*dec(1,2) ) );
printf("Time to live: %lu\n",dec(16,18));
printf("Protocol: ");
p=dec(18,20);
if(p == 1)
printf("ICMP\n");
if(p == 2)
printf("IGMP\n");
if(p == 89)
printf("OSPF\n");
if(p == 6)
printf("TCP\n");
if(p == 17)
printf("UDP\n");
printf("Header Checksum: %lu\n",dec(20,24));
binary( dec(24,32) ,32 );
printf("Source IP Address: %d.%d.%d.%d\n",bin_dec(0,8),bin_dec(8,16),bin_dec(16,24),bin_dec(24,32));
binary( dec(32,40) ,32 );
printf("Destination IP Address: %d.%d.%d.%d\n",bin_dec(0,8),bin_dec(8,16),bin_dec(16,24),bin_dec(24,32));
return 0;
}
unsigned int dec( int x, int y)
{ sum=0;k=1;
for(i=y-1;i>=x;i--)
{
if(a[i]>='0'&&a[i]<='9')
no=a[i]-'0';
if(a[i]>='A'&&a[i]<='F')
no=a[i]-55;
sum=sum+k*no;
k*=16;
}
return sum;
}
void binary(unsigned int n, int len)
{
for(i=0;i<100;i++) // initialization
bin[i]=0;
i=len-1;
while(n)
{
bin[i--]= n % 2;
n=n / 2;
}
}
int bin_dec( int start, int end)
{ decimal =0;k=1;
for( i=(end-1);i>=start;i--)
{
decimal=(bin[i]*k)+decimal;
k*=2;
}
return decimal;
}
output:- Enter the TCP header 4E86512165131219210600001214561981394867 IP Version: 4 Header Length: 56 Precedence: 4 Maximize Reliability Requested Minimize Cost Requested Total Length: 20769 Identification: 25875 Can be Fragmented No more Fragments pending Fragmentation Offset: 37008 Time to live: 33 Protocol: TCP Header Checksum: 0 Source IP Address: 18.20.86.25 Destination IP Address: 129.57.72.103 --------------------------------

0 comments: