linux ping源码
时间: 2023-09-15 13:23:10 浏览: 147
以下是 Linux ping 命令的主要源码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#define PING_PACKET_SIZE 64
#define PING_TIMEOUT 1000
#define MAX_PING_PACKETS 10
struct ping_packet {
struct timeval send_time;
struct timeval recv_time;
int seq;
};
static volatile int ping_done = 0;
static volatile int ping_count = 0;
static volatile int ping_recv_count = 0;
static void ping_handler(int signo)
{
if (signo == SIGALRM) {
ping_done = 1;
}
}
static unsigned short calculate_checksum(unsigned short *buf, int len)
{
unsigned long sum = 0;
while (len > 1) {
sum += *buf++;
len -= 2;
}
if (len == 1) {
sum += *(unsigned char*)buf;
}
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
return ~sum;
}
static void send_ping(int sock, struct sockaddr_in *addr, int seq)
{
struct ping_packet packet = {0};
packet.seq = seq;
gettimeofday(&packet.send_time, NULL);
if (sendto(sock, &packet, sizeof(packet), 0,
(struct sockaddr*)addr, sizeof(*addr)) < 0) {
perror("sendto");
}
}
static void recv_ping(int sock, struct sockaddr_in *addr, int seq)
{
struct ping_packet packet = {0};
socklen_t addrlen = sizeof(*addr);
if (recvfrom(sock, &packet, sizeof(packet), 0,
(struct sockaddr*)addr, &addrlen) < 0) {
if (errno == EINTR) {
return;
}
perror("recvfrom");
} else if (addrlen == sizeof(*addr)) {
if (packet.seq == seq) {
gettimeofday(&packet.recv_time, NULL);
printf("%d bytes from %s: icmp_seq=%d time=%ld.%06ld ms\n",
PING_PACKET_SIZE, inet_ntoa(addr->sin_addr),
packet.seq, packet.recv_time.tv_sec - packet.send_time.tv_sec,
packet.recv_time.tv_usec - packet.send_time.tv_usec);
ping_recv_count++;
}
}
}
static void ping(const char *host)
{
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
if (sock < 0) {
perror("socket");
return;
}
struct sockaddr_in addr = {0};
addr.sin_family = AF_INET;
addr.sin_port = htons(0);
if (inet_aton(host, &addr.sin_addr) == 0) {
struct hostent *he = gethostbyname(host);
if (he == NULL) {
perror("gethostbyname");
return;
}
memcpy(&addr.sin_addr, he->h_addr, he->h_length);
}
int i;
for (i = 0; i < MAX_PING_PACKETS; i++) {
send_ping(sock, &addr, i);
struct timeval timeout = {PING_TIMEOUT / 1000, (PING_TIMEOUT % 1000) * 1000};
fd_set fds;
FD_ZERO(&fds);
FD_SET(sock, &fds);
int ret = select(sock + 1, &fds, NULL, NULL, &timeout);
if (ret < 0) {
perror("select");
break;
} else if (ret == 0) {
printf("Request timeout for icmp_seq %d\n", i);
} else {
recv_ping(sock, &addr, i);
}
usleep(100000);
}
close(sock);
}
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "Usage: %s <host>\n", argv[0]);
return 1;
}
signal(SIGALRM, ping_handler);
ping_count = MAX_PING_PACKETS;
alarm(PING_TIMEOUT * MAX_PING_PACKETS / 1000);
ping(argv[1]);
printf("\n--- %s ping statistics ---\n", argv[1]);
printf("%d packets transmitted, %d received, %.1f%% packet loss\n",
ping_count, ping_recv_count, (float)(ping_count - ping_recv_count) / ping_count * 100);
return 0;
}
```
该程序使用 socket 创建一个 SOCK_DGRAM 类型的套接字,然后使用 ICMP 协议来进行 ping 测试。在每次 ping 测试中,它发送一个大小为 64 字节的数据包,然后等待回复。如果在超时时间内没有收到回复,则认为该请求超时。如果收到回复,则输出收到回复的信息。在程序结束时,它会输出 ping 统计信息。
该程序可以通过以下命令编译:
```
gcc -o ping ping.c
```
使用示例:
```
./ping www.google.com
```
阅读全文