* Author : Chaitanya bhargav M
* Date : 13 March 2010
* License : Free, but unreliable
* Description : Simpler ping without any options,
created just for fun!
*****************************************************/
#include<stdio.h>
#include<unistd.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<sys/time.h>
#include<sys/param.h>
#include<linux/if_ether.h>
#include<linux/ip.h>
#include<linux/icmp.h>
#include<linux/if_packet.h>
#include<net/if.h>
#include<string.h>
#include<stdlib.h>
#include<signal.h>
int sock_fd,transmitted = 0,received = 0;
float t_sum = 0;
void sig_handler(int sigNum)
{
printf("\n=======================ping stats======================");
printf("\n%d packets transmitted, %d received, avg.delay = %.3fms\n",transmitted,received,(t_sum/received));
printf("Packet loss = %d%%,",((transmitted-received)/(transmitted))*100);
printf(" Thanks for using! -Chaitu\n");
close(sock_fd);
exit(127);
}
#pragma pack(1)
struct _icmp_{
struct icmphdr icmp;
unsigned char data[22];
}packet;
unsigned short check_sum(unsigned char *buf, int len);
char *ip_ntoa(unsigned int ipAddr)
{
char *ip = (char *)malloc(16);
sprintf(ip,"%d.%d.%d.%d",(ipAddr&0xff000000)>>24,
(ipAddr&0x00ff0000)>>16,
(ipAddr&0x0000ff00)>>8,
(ipAddr&0x000000ff));
return ip;
}
struct timezone tz;
int main(int argc, char *argv[])
{
int retVal,sin_size,data_len;
unsigned short seq = 0x0000;
struct iphdr *ip;
struct icmphdr *icmp;
struct sockaddr_in whereto,sin ;
struct timeval *tp = (struct timeval *)&packet.data[0];
struct timeval *tp1;
struct timeval tv;
float delay;
if(argc != 2)
{
printf("Usage:\n ./pinger
exit(127);
}
whereto.sin_family = AF_INET;
whereto.sin_addr.s_addr = inet_addr(argv[1]);
memset(&packet.data[0],0,22);
unsigned char buf[1518];
sock_fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if(sock_fd < 0)
{
perror("socket");
exit(127);
}
signal(SIGINT,sig_handler);
while(1)
{
/* Send the packet */
gettimeofday(tp, &tz);
packet.icmp.type = ICMP_ECHO;
packet.icmp.code = 0;
packet.icmp.checksum = 0;
packet.icmp.un.echo.id = htons(0xc000);
packet.icmp.un.echo.sequence = htons(++seq);
packet.icmp.checksum = htons(check_sum((unsigned char *)&packet.icmp.type,30));
retVal = sendto(sock_fd, (unsigned char *)&packet, 30, 0,(struct sockaddr *) &whereto, sizeof(struct sockaddr));
if(retVal < 0)
{
perror("send to");
close(sock_fd);
exit(127);
}
transmitted++;
/* Receive Packet */
data_len = recvfrom(sock_fd, buf, 1518, 0 ,(struct sockaddr *)&sin, &sin_size);
ip = (struct iphdr *)buf;
icmp = (struct icmphdr *)(buf + ((ip->ihl) << 2));
if(icmp->type == ICMP_ECHOREPLY)
{
if(icmp->un.echo.id == htons(0xc000))
{
if(icmp->un.echo.sequence == htons(seq))
{
gettimeofday(&tv, &tz);
delay = (tv.tv_sec) - (tp->tv_sec) + (tv.tv_usec) - (tp->tv_usec);
printf("64 bytes from %s icmp_seq=%d ttl=%d delay=%.3fms\n",ip_ntoa(ntohl(ip->saddr)),seq,ip->ttl,(delay/1000));
received++;
t_sum += (delay/1000);
}
}
}
sleep(1);
}
return ;
}
unsigned short check_sum(unsigned char *buf, int len)
{
unsigned short len_16 = 0;
unsigned int len_32 = 0;
int i;
for(i =0; i < len; i+=2)
{
len_16 = (buf[i] << 8)&0xff00 | buf[i+1]&0xff;
len_32 += len_16;
}
if(len_32 >> 16)
{
len_32 = ((len_32>>16)&0xffff) + (len_32&0xffff);
}
if(len_32 >> 16)
{
len_32 = ((len_32>>16)&0xffff) + (len_32&0xffff);
}
len_32 = ~len_32;
return (len_32&0xffff);
}
No comments:
Post a Comment