📄 ping.cpp
字号:
#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <time.h>#include <unistd.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <netdb.h>#include <linux/ip.h>#include <linux/icmp.h>#include <netinet/in.h>#include <arpa/inet.h>#include "my_inet.h"//#define ICMP_ECHO 8//#define ICMP_ECHOREPLY 0#define ICMP_MIN 8 // minimum 8 byte icmp packet (just header)#define STATUS_FAILED 0xFFFF#define DEF_PACKET_SIZE 32#define DEF_PACKET_NUMBER 4#define MAX_PACKET 1024void fill_icmp_data(char *, int);unsigned short checksum( unsigned short *, int );int decode_resp(char *,int ,struct sockaddr_in *);void usage(char *progname){ fprintf(stderr,"Usage:\n"); fprintf(stderr,"%s [number of packets] [data_size]\n",progname); fprintf(stderr,"datasize can be up to 1Kb\n");}int main(int argc, char **argv){ struct sockaddr_in dest,from; struct hostent * hp; int bread,datasize,times; int fromlen = sizeof(from); int statistic = 0; char *dest_ip; char *icmp_data; char *recvbuf; unsigned int addr=0; unsigned short seq_no = 0; if (argc <2 ){ usage( argv[0] ); return -1; } int sock = socket( MY_PF_INET, SOCK_RAW, MY_IPPROTO_ICMP ); if( sock == -1 ){ fprintf(stderr,"create socket failed: %d\n", errno ); return -1; } struct icmp_filter filter; socklen_t size = sizeof (struct icmp_filter); getsockopt (sock, SOL_RAW, ICMP_FILTER, &filter, &size); printf("the filter: %x\n", filter.data ); filter.data = 0; //1 << ICMP_ECHOREPLY; int err = setsockopt( sock, SOL_RAW, ICMP_FILTER, &filter, sizeof(struct icmp_filter) ); if( err < 0 ) perror("error: "); memset( &filter, 0, sizeof( struct icmp_filter ) ); getsockopt( sock, SOL_RAW, ICMP_FILTER, &filter, &size ); printf("new filter: %x\n", filter.data); struct timeval timeout; timeout.tv_sec = 1; if( setsockopt( sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout) ) == -1 ){ fprintf(stderr,"failed to set recv timeout: %d\n", errno); perror("error"); return -1; } if( setsockopt( sock, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof(timeout) ) == -1 ){ fprintf(stderr,"failed to set send timeout: %d\n", errno); return -1; } memset( &dest,0,sizeof(dest) ); hp = gethostbyname(argv[1]); if( !hp ) addr = inet_addr(argv[1]); if( (!hp) && (addr == INADDR_NONE) ){ fprintf(stderr,"Unable to resolve %s\n",argv[1]); return -1; } if (hp != NULL) memcpy(&(dest.sin_addr),hp->h_addr,hp->h_length); else dest.sin_addr.s_addr = addr; hp->h_addrtype = MY_PF_INET; if (hp) dest.sin_family = hp->h_addrtype; else dest.sin_family = MY_PF_INET; dest_ip = inet_ntoa(dest.sin_addr); if(argc>2){ times=atoi(argv[2]); if(times == 0) times=DEF_PACKET_NUMBER; }else times=DEF_PACKET_NUMBER; if (argc >3){ datasize = atoi(argv[3]); if( datasize == 0 ) datasize = DEF_PACKET_SIZE; if( datasize >1024 ){ fprintf(stderr,"WARNING : data_size is too large !\n"); datasize = DEF_PACKET_SIZE; } }else datasize = DEF_PACKET_SIZE; datasize += sizeof( icmphdr ); icmp_data = (char*)malloc( MAX_PACKET ); recvbuf = (char*)malloc( MAX_PACKET ); memset( icmp_data,0,MAX_PACKET ); fill_icmp_data(icmp_data,datasize); fprintf(stdout,"\nPinging %s ....\n\n",dest_ip); for(int i = 0; i < times; i++ ){ printf("1\n"); ((icmphdr*)icmp_data)->checksum = 0; //((icmphdr*)icmp_data)->timestamp = time( NULL ); ((icmphdr*)icmp_data)->un.echo.sequence = seq_no++; ((icmphdr*)icmp_data)->checksum = checksum((unsigned short *)icmp_data,datasize); printf("2\n"); int bwrite = sendto( sock, icmp_data, datasize, 0, (struct sockaddr*)&dest,sizeof(dest) ); if( bwrite == -1 ){ perror("sendto failed: "); return -1; } printf("3 %d\n", bwrite ); int bread = recvfrom( sock, recvbuf, MAX_PACKET, 0, (struct sockaddr*)&from, (socklen_t *)&fromlen ); if( bread == -1 ){ perror("recvfrom failed: "); return -1; } printf("4\n"); if( !decode_resp( recvbuf, bread, &from) ) statistic++; /* 成功接收的数目++ */ sleep(1); } fprintf(stdout,"\nPing statistics for %s \n",dest_ip); fprintf(stdout," Packets: Sent = %d,Received = %d, Lost = %d (%2.0f%% loss)\n",times, statistic,(times-statistic), (float)(times-statistic)/times*100); close(sock); return 0;}int decode_resp(char *buf, int bytes, struct sockaddr_in *from){ iphdr *p_iphdr; icmphdr *p_icmphdr; unsigned short iphdrlen; p_iphdr = (iphdr *)buf; iphdrlen = (p_iphdr->ihl) * 4; if( bytes < iphdrlen + ICMP_MIN ) printf("Too few bytes from %s\n",inet_ntoa(from->sin_addr)); p_icmphdr = (icmphdr *)(buf + iphdrlen); //if (p_icmphdr->type != ICMP_ECHOREPLY) { // fprintf(stderr,"non-echo type %d recvd\n",p_icmphdr->type); // return 1; //} if ( p_icmphdr->un.echo.id != (unsigned short)getpid() ) { fprintf(stderr,"someone else's packet!\n"); return 1; } printf("%d bytes from %s:",bytes, inet_ntoa(from->sin_addr)); //printf(" icmp_seq = %d. ",p_icmphdr->i_seq); //printf(" time: %d ms ",time(NULL) - p_icmphdr->timestamp); printf("\n"); return 0;}unsigned short checksum( unsigned short *buffer, int size ){ unsigned long cksum=0; while(size >1) { cksum += *buffer++; size -= sizeof( unsigned short ); } if( size ) cksum += *( unsigned short *)buffer; cksum = (cksum >> 16) + (cksum & 0xffff); cksum += (cksum >>16); return ( unsigned short )(~cksum);} void fill_icmp_data(char * icmp_data, int datasize){ icmphdr *icmp_hdr; char *datapart; icmp_hdr = (icmphdr*)icmp_data; icmp_hdr->type = ICMP_ECHO; icmp_hdr->code = 0; icmp_hdr->un.echo.id = ( unsigned short )getpid(); icmp_hdr->checksum = 0; icmp_hdr->un.echo.sequence = 0; datapart = icmp_data + sizeof(icmphdr); memset(datapart,'E', datasize - sizeof(icmphdr));}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -