1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
| #include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <linux/ip.h>
#include <linux/icmp.h>
#include <string.h>
#include <unistd.h>
unsigned short in_cksum(unsigned short *, int);
int main()
{
int addrlen;
int k;
char d_addr[17];
struct iphdr *ip;
struct icmphdr *icmp;
struct icmphdr *icmp_reply;
struct iphdr *ipreply;
char *packet;
char *buffer;
int raw;
char *n;
struct sockaddr_in serv;
int optval=1;
int sent;
printf("Enter the destination address : ");
fgets(d_addr,17,stdin);
n= strchr(d_addr, '\n'); /* search for newline character */
if ( n != NULL )
{
*n = '\0'; /* overwrite trailing newline */
}
// Allocate space
packet=malloc(sizeof(struct iphdr)+sizeof(struct icmphdr));
buffer=malloc(sizeof(struct iphdr)+sizeof(struct icmphdr));
//setting up the ip header
ip=(struct iphdr *)packet;
//Setting the values for the ip header
ip->ihl=5; //header length
ip->version=4; //version
ip->tos=0; //type of service
ip->tot_len=htons(sizeof(struct iphdr)+sizeof(struct icmphdr)); //toatal length
ip->id=htons(random()); //Assign id as a random number
ip->ttl=255; //time to live;
ip->protocol=IPPROTO_ICMP;//ICMP header follow next to the ip header
ip->check=0; //Assign checksum as zero for now
ip->saddr= INADDR_ANY;
ip->daddr=inet_addr(d_addr);
//Creating a raw socket
if((raw=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP))==-1)
{
perror("Something wrong with socket call :");
}
//setting the iphdrincl option
setsockopt(raw, IPPROTO_IP, IP_HDRINCL, &optval, sizeof(int));
//creating the icmp header
icmp=(struct icmphdr *)(packet+sizeof(struct iphdr));
icmp->type=ICMP_ECHO;
icmp->code = 0;
icmp->un.echo.id = 0;
icmp->un.echo.sequence = 0;
icmp->checksum = 0;
icmp-> checksum = in_cksum((unsigned short *)icmp, sizeof(struct icmphdr));
ip->check = in_cksum((unsigned short *)ip, sizeof(struct iphdr));
serv.sin_family=AF_INET;
serv.sin_addr.s_addr=ip->daddr;
sent=sendto(raw,packet,ntohs(ip->tot_len),0,(struct sockaddr *)&serv,sizeof(struct sockaddr));
/*
*listen for response
*/
addrlen = sizeof(serv);
if ((k=recvfrom(raw, buffer, (sizeof(struct iphdr) + sizeof(struct icmphdr)), 0, (struct sockaddr *)&serv, &addrlen)) == -1)
{
perror("recv");
}
ipreply=(struct iphdr *)buffer;
icmp_reply=(struct icmphdr *)(buffer + sizeof (struct iphdr));
struct in_addr inad;
printf("Received %d byte reply from: %s\n", k, inet_ntoa (serv.sin_addr));
inad.s_addr = ipreply->saddr;
printf("Src IP: %s\n", inet_ntoa (inad));
inad.s_addr = ipreply->daddr;
printf("Dst IP: %s\n", inet_ntoa (inad));
printf("ID: %d\n", ntohs(ipreply->id));
printf("TTL: %d\n", ipreply->ttl);
printf("ICMP type: %d\n",icmp_reply->type);
printf("ICMP code: %d\n",icmp_reply->code);
printf("ICMP echo ID: %d\n",icmp_reply->un.echo.id);
printf("ICMP echo seq: %d\n",icmp_reply->un.echo.sequence);
close(raw);
free(packet);
return 0;
}
/*
* in_cksum --
* Checksum routine for Internet Protocol
* family headers (C Version)
*/
unsigned short in_cksum(unsigned short *addr, int len)
{
register int sum = 0;
u_short answer = 0;
register u_short *w = addr;
register int nleft = len;
/*
* Our algorithm is simple, using a 32 bit accumulator (sum), we add
* sequential 16 bit words to it, and at the end, fold back all the
* carry bits from the top 16 bits into the lower 16 bits.
*/
while (nleft > 1)
{
sum += *w++;
nleft -= 2;
}
/* mop up an odd byte, if necessary */
if (nleft == 1)
{
*(u_char *) (&answer) = *(u_char *) w;
sum += answer;
}
/* add back carry outs from top 16 bits to low 16 bits */
sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
sum += (sum >> 16); /* add carry */
answer = ~sum; /* truncate to 16 bits */
return (answer);
} |
Partager