轉載自:html
//http://blog.chinaunix.net/uid-25909722-id-2762079.html #include <stdio.h> #include <stdlib.h> #include <string.h> #include <strings.h> /* for bzero */ #include <signal.h> #include <sys/time.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #include <unistd.h> #include <netinet/in.h> #include <netinet/ip.h> #include <netinet/ip_icmp.h> #include <netdb.h> #include <setjmp.h> #include <errno.h> #define MAX_WAIT_TIME 5 #define PACKET_SIZE 4096 /* 數據包的大小 */ #define MAX_NO_PACKETS 3 /* 發送3個ICMP報文 */ char sendpacket[PACKET_SIZE]; /* 發送的數據包 */ char recvpacket[PACKET_SIZE]; /* 接收的數據包 */ pid_t pid; int sockfd; int datalen = 56; /* icmp數據包中數據的長度 */ int nsend = 0; /* 發送的次數 */ int nreceived = 0; /* 接收的次數 */ struct sockaddr_in dest_addr; /* icmp包目的地址 */ struct sockaddr_in from; /* icmp包源地址 */ struct timeval tvrecv; void statistics(int signo); unsigned short cal_chksum(unsigned short *addr,int len); int pack(int pack_no); void send_packet(void); void recv_packet(void); int unpack(char *buf,int len); void tv_sub(struct timeval *out,struct timeval *in); void statistics(int signo) { printf("\n--------------------PING statistics-------------------\n"); /* * 總共發送nsend個icmp包,總共接收到返回的nreceived個包, * icmp包的丟失率(nsend-nreceived)/nsend */ printf("%d packets transmitted, %d received , %%%d lost\n", nsend, nreceived, (nsend-nreceived)/nsend*100); close(sockfd); exit(1); } /* 計算校驗和的算法 */ unsigned short cal_chksum(unsigned short *addr,int len) { int sum=0; int nleft = len; unsigned short *w = addr; unsigned short answer = 0; /* 把ICMP報頭二進制數據以2字節爲單位累加起來 */ while(nleft > 1){ sum += *w++; nleft -= 2; } /* * 若ICMP報頭爲奇數個字節,會剩下最後一字節。 * 把最後一個字節視爲一個2字節數據的高字節, * 這2字節數據的低字節爲0,繼續累加 */ if(nleft == 1){ *(unsigned char *)(&answer) = *(unsigned char *)w; sum += answer; /* 這裏將 answer 轉換成 int 整數 */ } sum = (sum >> 16) + (sum & 0xffff); /* 高位低位相加 */ sum += (sum >> 16); /* 上一步溢出時,將溢出位也加到sum中 */ answer = ~sum; /* 注意類型轉換,如今的校驗和爲16位 */ return answer; } /* 設置ICMP報頭,以及將發送的時間設置爲ICMP的末尾的數據部分和校驗和 */ int pack(int pack_no) { int packsize; struct icmp *icmp; struct timeval *tval; icmp = (struct icmp*)sendpacket; icmp->icmp_type = ICMP_ECHO; /* icmp的類型 */ icmp->icmp_code = 0; /* icmp的編碼 */ icmp->icmp_cksum = 0; /* icmp的校驗和 */ icmp->icmp_seq = pack_no; /* icmp的順序號 */ icmp->icmp_id = pid; /* icmp的標誌符 */ packsize = 8 + datalen; /* icmp8字節的頭 加上數據的長度(datalen=56), packsize = 64 */ tval = (struct timeval *)icmp->icmp_data; /* 得到icmp結構中最後的數據部分的指針 */ gettimeofday(tval, NULL); /* 將發送的時間填入icmp結構中最後的數據部分 */ icmp->icmp_cksum = cal_chksum((unsigned short *)icmp, packsize);/*填充發送方的校驗和*/ return packsize; } /* 發送三個ICMP報文 */ void send_packet() { int packetsize; /* 每一次發送3個icmp包 */ while(nsend < MAX_NO_PACKETS){ // #define MAX_NO_PACKETS 3 nsend++; packetsize = pack(nsend); /* 設置ICMP報頭 */ if(sendto(sockfd, sendpacket, packetsize, 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr)) < 0){ perror("sendto error"); continue; } sleep(1); /* 每隔一秒發送一個ICMP報文 */ } } /* 接收全部ICMP報文 */ void recv_packet() { int n, fromlen; extern int errno; signal(SIGALRM,statistics); fromlen = sizeof(from); /* icmp包源地址的大小*/ while(nreceived < nsend){ alarm(MAX_WAIT_TIME); if((n = recvfrom(sockfd, recvpacket, sizeof(recvpacket), 0, (struct sockaddr *)&from, (socklen_t *)&fromlen)) < 0) { if(errno == EINTR) continue; perror("recvfrom error"); continue; } gettimeofday(&tvrecv, NULL); /* 記錄接收到icmp包時的時間 */ if(unpack(recvpacket, n) == -1) continue; nreceived++; } } /* 對ICMP報頭解包 */ int unpack(char *buf, int len) { int iphdrlen; struct ip *ip; struct icmp *icmp; struct timeval *tvsend; double rtt; ip = (struct ip *)buf; iphdrlen = ip->ip_hl << 2; /* 求ip報頭長度,即ip報頭的長度標誌乘4 */ icmp = (struct icmp *)(buf + iphdrlen); /* 越過ip報頭,指向ICMP報頭 */ len -= iphdrlen; /* ICMP報頭及ICMP數據報的總長度 */ if(len < 8){ /* 小於ICMP報頭長度則不合理 */ printf("ICMP packets\'s length is less than 8\n"); return -1; } /* 確保所接收的是我所發的的ICMP的迴應 */ if((icmp->icmp_type == ICMP_ECHOREPLY) && (icmp->icmp_id == pid)){ tvsend = (struct timeval *)icmp->icmp_data; tv_sub(&tvrecv, tvsend); /* 接收和發送的時間差 */ /* 以毫秒爲單位計算髮送和接收的時間差rtt */ rtt = tvrecv.tv_sec * 1000 + tvrecv.tv_usec / 1000; /* 顯示相關信息 */ printf("%d byte from %s: icmp_seq=%u ttl=%d time=%.3f ms\n", len, /* ICMP報頭及ICMP數據報的總長度 */ inet_ntoa(from.sin_addr), /* ICMP的源地址 */ icmp->icmp_seq, /* icmp包發送的順序 */ ip->ip_ttl, /* icmp存活的時間 */ rtt); /* 以毫秒爲單位計算髮送和接收的時間差rtt */ return 0; } else return -1; } int main(int argc,char *argv[]) { struct hostent *host; struct protoent *protocol; unsigned long inaddr = 0l; int size = 50*1024; //50k if(argc < 2){ printf("usage:%s hostname/IP address\n",argv[0]); exit(1); } if((protocol = getprotobyname("icmp")) == NULL){ perror("getprotobyname"); exit(1); } /* 生成使用ICMP的原始套接字,這種套接字只有root才能生成 */ if((sockfd = socket(AF_INET, SOCK_RAW, protocol->p_proto)) < 0){ perror("socket error"); exit(1); } setuid(getuid()); /* 回收root權限,設置當前用戶權限 */ /* * 擴大套接字接收緩衝區到50K這樣作主要爲了減少接收緩衝區溢出的 * 的可能性,若無心中ping一個廣播地址或多播地址,將會引來大量應答 */ setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size) ); bzero(&dest_addr, sizeof(dest_addr)); dest_addr.sin_family = AF_INET; /* 判斷argv[1]是主機名仍是ip地址 */ if((inaddr=inet_addr(argv[1])) == INADDR_NONE){ if((host = gethostbyname(argv[1])) == NULL){ /* 是主機名 */ perror("gethostbyname error"); exit(1); } memcpy((char*)&dest_addr.sin_addr, host->h_addr, host->h_length); }else {/* 是ip地址 */ memcpy((char*)&dest_addr.sin_addr, (char*)&inaddr, sizeof(inaddr)); } pid = getpid(); /*獲取main的進程id,用於設置ICMP的標誌符*/ printf("PING %s(%s): %d bytes data in ICMP packets.\n", argv[1], inet_ntoa(dest_addr.sin_addr), datalen); send_packet(); /* 發送全部ICMP報文 */ recv_packet(); /* 接收全部ICMP報文 */ statistics(SIGALRM); /* 進行統計 */ return 0; } /* 兩個timeval結構相減 */ void tv_sub(struct timeval *recv, struct timeval *send){ if((recv->tv_usec -= send->tv_usec) < 0){ --recv->tv_sec; recv->tv_usec += 1000000; } recv->tv_sec -= send->tv_sec; }
EOF算法