📄 liteping.c
字号:
#include <stdio.h>#include <netinet/in.h>#include <netinet/in_systm.h>#include <netinet/ip.h>#include <netinet/ip_icmp.h>#include <sys/socket.h>#include <sys/time.h>#include <sys/signal.h>#include <errno.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <netdb.h>int main(int argc, char ** argv){ struct timeval tv; struct timeval * sendtime; struct timeval recvtime; struct timeval different; struct sockaddr_in host; struct sockaddr_in recvhost; struct hostent *address; struct icmphdr *sendhdr; struct icmphdr *recvhdr; struct ip* iphdr; int sock; int loop; int ret; int max_loop_time = 10; int addrlen = sizeof(struct sockaddr); int sendlen = sizeof(struct icmphdr) + sizeof(long); int ttl = 64; int hdrlen = sizeof(struct icmphdr) + sizeof(struct ip); int seq = 0; char * sendbuf[1024]; char * recvbuf[1024]; printf("\n~~~ Lite Ping By aLFRED ~~~\n"); switch( argc ) { case 3: max_loop_time = atoi(argv[2]); case 2: printf("~~~ Receive 1 parameter:\n"); printf("~~~ Param 1: %s\n", argv[1]); if( 3 == argc) printf("~~~ Param 2: %s times loop\n", argv[2]); printf("\n"); tv.tv_sec = 5;
tv.tv_usec = 0; break; default: printf("~~~ Usage: %s IP(or hostname) [number of loop time]\n\n", argv[0]); exit(0); break; } sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval)); setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(struct timeval)); setsockopt(sock, IPPROTO_IP, IP_TTL, (int *)&ttl, sizeof(int)); host.sin_family = AF_INET; if( (host.sin_addr.s_addr = inet_addr(argv[1])) != 0xffff ) { address = gethostbyname( argv[1] ); if( address ) { memcpy(&host.sin_addr , address->h_addr, address->h_length); } else { printf("~~~ The hostname is wrong.\n\n"); exit(1); } } printf("~~~ Ping %s(%s):\n",argv[1],inet_ntoa(host.sin_addr)); sendhdr = (struct icmphdr *)sendbuf; sendhdr -> type = ICMP_ECHO; sendhdr -> code = 0; sendhdr -> checksum = 0; (sendhdr -> un).echo.id = (unsigned short)getpid(); (sendhdr -> un).echo.sequence = seq; for(loop = 0; loop < max_loop_time ; loop++ ) { (sendhdr -> un).echo.sequence = seq++; sendtime = (struct timeval *)(sendbuf+sizeof(struct icmphdr)); gettimeofday(sendtime, NULL); if(sendto(sock, sendbuf, sendlen, 0, (struct sockaddr*)&host, addrlen)<0) { printf("~~~ Error\n",loop); continue; } ret = recvfrom(sock, recvbuf, 1024, 0, (struct sockaddr*)&recvhost, &addrlen); gettimeofday(&recvtime, NULL); if (ret < 0)
{
printf("~~~ Error\n",loop); continue;
} different.tv_sec = recvtime.tv_sec - sendtime->tv_sec; different.tv_usec = recvtime.tv_usec - sendtime->tv_usec; iphdr = (struct ip*)recvbuf; recvhdr = (struct icmphdr*)(recvbuf + 20); printf("~~~ Reply from %s: ", inet_ntoa(recvhost.sin_addr)); printf("%d bytes ",ret); printf("ttl=%d ",iphdr->ip_ttl); printf("time=%1.4fms\n",(different.tv_sec)*1000.0 + (different.tv_usec)/1000.0); } close(sock); printf("\n"); return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -