linux ping源码
时间: 2023-10-03 09:09:58 浏览: 65
ping源代码
5星 · 资源好评率100%
以下是 Linux 系统中 ping 命令的源代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/uio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#define PACKET_SIZE 4096
#define MAX_WAIT_TIME 5
#define MAX_NO_PACKETS 3
#define IP_HEADER_LEN 20
#define ICMP_HEADER_LEN 8
struct packet {
struct timeval tv;
char msg[PACKET_SIZE];
};
char sendpacket[PACKET_SIZE];
char recvpacket[PACKET_SIZE];
int sockfd, datalen = 56;
int nsend = 0, nreceived = 0;
pid_t pid;
struct sockaddr_in dest_addr;
struct timeval tvrecv;
void statistics(int signo) {
printf("\n--- %s ping statistics ---\n", inet_ntoa(dest_addr.sin_addr));
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 nleft = len;
int sum = 0;
unsigned short *w = addr;
unsigned short answer = 0;
while (nleft > 1) {
sum += *w++;
nleft -= 2;
}
if (nleft == 1) {
*(unsigned char *)(&answer) = *(unsigned char *)w;
sum += answer;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
answer = ~sum;
return answer;
}
void send_packet(void) {
struct icmp *icmp;
int len;
icmp = (struct icmp *)sendpacket;
icmp->icmp_type = ICMP_ECHO;
icmp->icmp_code = 0;
icmp->icmp_id = pid;
icmp->icmp_seq = nsend++;
memset(icmp->icmp_data, 0xa5, datalen);
gettimeofday((struct timeval *)icmp->icmp_data, NULL);
len = datalen + ICMP_HEADER_LEN;
icmp->icmp_cksum = 0;
icmp->icmp_cksum = cal_chksum((unsigned short *)icmp, len);
sendto(sockfd, sendpacket, len, 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
}
int recv_packet(void) {
int n, fromlen;
extern int errno;
fromlen = sizeof(dest_addr);
while (1) {
if ((n = recvfrom(sockfd, recvpacket, sizeof(recvpacket), 0, (struct sockaddr *)&dest_addr, &fromlen)) < 0) {
if (errno == EINTR)
continue;
return -1;
}
gettimeofday(&tvrecv, NULL);
struct ip *ip = (struct ip *)recvpacket;
int ip_header_len = ip->ip_hl << 2;
struct icmp *icmp = (struct icmp *)(recvpacket + ip_header_len);
int icmp_header_len = ICMP_HEADER_LEN;
if (n - ip_header_len < ICMP_HEADER_LEN) {
printf("ICMP packet length is less than %d\n", ICMP_HEADER_LEN);
return -1;
}
if ((icmp->icmp_type == ICMP_ECHOREPLY) && (icmp->icmp_id == pid)) {
nreceived++;
printf("%d bytes from %s: icmp_seq=%u ttl=%d time=%.1f ms\n",
n - ip_header_len - icmp_header_len,
inet_ntoa(dest_addr.sin_addr),
icmp->icmp_seq,
ip->ip_ttl,
(tvrecv.tv_sec - ((struct timeval *)icmp->icmp_data)->tv_sec) * 1000.0 + (tvrecv.tv_usec - ((struct timeval *)icmp->icmp_data)->tv_usec) / 1000.0);
}
else {
printf("received ICMP packet with type %d, code %d\n", icmp->icmp_type, icmp->icmp_code);
}
return 0;
}
}
void set_sockaddr(const char *host) {
struct hostent *hp;
struct sockaddr_in tmp;
memset(&tmp, 0, sizeof(tmp));
tmp.sin_family = AF_INET;
if ((tmp.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
if ((hp = gethostbyname(host)) == NULL) {
fprintf(stderr, "ping: unknown host %s\n", host);
exit(1);
}
tmp.sin_family = hp->h_addrtype;
memcpy((caddr_t)&tmp.sin_addr, hp->h_addr, hp->h_length);
}
dest_addr = tmp;
}
int main(int argc, char **argv) {
struct sockaddr_in from;
struct timeval timeout;
fd_set readfds;
int size = 50 * 1024;
pid = getpid();
if (argc < 2) {
printf("usage: %s hostname\n", argv[0]);
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) {
printf("create socket error\n");
exit(1);
}
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size))) {
printf("setsockopt error\n");
exit(1);
}
set_sockaddr(argv[1]);
signal(SIGINT, statistics);
while (nsend < MAX_NO_PACKETS) {
send_packet();
n = recv_packet();
if (n < 0) {
printf("recv error\n");
continue;
}
timeout.tv_sec = MAX_WAIT_TIME;
timeout.tv_usec = 0;
FD_ZERO(&readfds);
FD_SET(sockfd, &readfds);
if (select(sockfd + 1, &readfds, NULL, NULL, &timeout) <= 0) {
printf("select timeout\n");
continue;
}
if (FD_ISSET(sockfd, &readfds)) {
if (recv_packet() == -1) {
printf("recv_packet error\n");
continue;
}
}
}
statistics(SIGINT);
return 0;
}
```
以上代码实现了 ping 命令的基本功能,包括发送 ICMP 报文、接收 ICMP 报文并计算 RTT、输出统计信息等。其中,cal_chksum() 函数用于计算 ICMP 报文的校验和,set_sockaddr() 函数用于设置目标主机的地址信息,recv_packet() 函数用于接收 ICMP 报文并解析其中的信息。
阅读全文