学完了 ICMP 回显请求与应答报文,就可以用它来实现我们平时使用的 PING 命令了。
先来看看效果:
图1 自带的 ping 命令与我们自己实现的
1. 程序路径
本文使用的程序托管在 gitos 上:http://git.oschina.net/ivan_allen/unp
如果你已经 clone 过这个代码了,请使用 git pull
更新一下。本节程序所使用的程序路径是 unp/program/icmp/icmpecho
.
2. 程序设计
ping 命令本质上就是给目标主机发送一个 ICMP 回显请求报文,然后等待对方答复 ICMP 回显应答报文,如果对方不应答,可能就是我们的主机和对方的网路不通。
ping 命令还可以计算出往返时间,其实这很简单,我们只要在发送 ICMP 报文的时候,记录一下当前时间,再接收报文的时候再记录一下时间,两个时间相减就是 RTT 时间。
ping 命令中的 ttl 字段可以从 ip 数据报首部中拿到。
程序代码如下:
#include "common.h"
#define BUF_SIZE 4096
#define ICMP_SIZE 64
char *hostname; // 主机名或 ip
char recvbuf[BUF_SIZE]; // 接收缓冲区
char sendbuf[ICMP_SIZE]; // 发送缓冲区
void run(); // ping 主程序
void handler(int sig); // 信号处理函数
int nsend, nrecv; // 用于统计
int64_t start;
int main(int argc, char* argv[]) {
if (argc < 2) {
ERR_QUIT("Usage: %s <hostname or ip>\n", argv[0]);
}
hostname = argv[1];
registSignal(SIGALRM, handler);
registSignal(SIGINT, handler);
run();
}
void run() {
struct ip *ip; // ip 首部
struct icmp_echo *icmp_echo, *icmp_echo_reply; // 请求与应答
struct sockaddr_in to; // 目标主机地址
int i, len, sockfd, ret, nr;
int64_t timerecv, timesend;
double rtt;
nsend = 0; // 已经发送包的个数
nrecv = 0; // 已经接收包的个数
// 随机初始化
for (i = 0; i < ICMP_SIZE; ++i)
sendbuf[i] = "abcdefghijklmnopqrstuvwxyz"[i%26];
// 构造套接字地址,端口并没有什么用,随便填
ret = resolve(hostname, 0, &to);
if (ret < 0) ERR_EXIT("resolve");
// 创建原始套接字, 只接收承载 ICMP 协议的数据报
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sockfd < 0) ERR_EXIT("socket");
// 打印信息
WARNING("PING %s (%s) %d bytes of data.\n", hostname, inet_ntoa(to.sin_addr), ICMP_SIZE);
icmp_echo = (struct icmp_echo*)sendbuf;
ip = (struct ip*)recvbuf;
// 用于时间统计
start = now();
// 填充 icmp 回显请求报文, 这三个字段以后都不用再改,放在循环外面
icmp_echo->icmp_type = 8;
icmp_echo->icmp_code = 0;
icmp_echo->icmp_id = getpid() & 0xffff;
while(1) {
icmp_echo->icmp_cksum = 0;
icmp_echo->icmp_seq = nsend + 1; // 序号, 起始序号为 1
*((int64_t*)icmp_echo->icmp_data) = now(); // 当前时间,微秒
icmp_echo->icmp_cksum = cksum((unsigned short*)sendbuf, ICMP_SIZE); // 指定大小为 64
// 发送数据
ret = sendto(sockfd, sendbuf, ICMP_SIZE, 0, (struct sockaddr*)&to, sizeof(to));
if (ret < 0) {
if (errno == EINTR) continue;
ERR_EXIT("sendto");
}
++nsend; // 序号加 1
again:
// 接收 ip 数据报,超时时间设置为 5 秒。
alarm(5);
nr = recvfrom(sockfd, recvbuf, BUF_SIZE, 0, NULL, NULL);
if (nr < 0) {
if (errno == EINTR) {
ERR_PRINT("TIMEDOUT.\n");
continue; // 超时,丢包
}
ERR_EXIT("recvfrom");
}
// 从 ip 数据报里拿到 icmp 报文
icmp_echo_reply = (struct icmp_echo*)((char*)ip + (ip->ip_hl << 2));
if (icmp_echo_reply->icmp_type != 0 || icmp_echo_reply->icmp_code != 0
|| icmp_echo_reply->icmp_id != (getpid() & 0xffff)) {
// 收到的不是我们想要的包,重来继续收,不能使用 continue 是因为我们不能判定包就丢失了
goto again;
}
// 执行到这里说明收到了自己的包,将接收个数递增
++nrecv;
// 计算往返时间
timerecv = now();
timesend = *((int64_t*)icmp_echo_reply->icmp_data);
rtt = (timerecv - timesend) / 1000.0;
LOG("%d bytes from %s (%s): icmp_seq=%d ttl=%d time=%.1f ms\n",
ICMP_SIZE, hostname, inet_ntoa(to.sin_addr), icmp_echo_reply->icmp_seq, ip->ip_ttl, rtt);
sleep(1);
}
}
void handler(int sig) {
int64_t end;
if (sig == SIGINT) {
end = now();
WARNING("\n--- %s ping statistics ---\n", hostname);
WARNING("%d packets transmitted, %d received, %d%% packet loss, time %dms\n",
nsend, nrecv, (nsend - nrecv) * 100 / nsend, (int)(end - start) / 1000);
exit(0);
}
}
这段代码没有什么特别的地方,唯一要注意的就是为了防止对端没有回应应答报文而导致 recvfrom 阻塞,我们需要给 recvfrom 设置超时时间。使用 alarm 信号为读写 I/O 设置超时时间是惯用的技俩,这些以前博客里都写过。
代码中使用了 goto 语句,当然你也可以使用 while 循环,但在这里使用 goto 反而能让代码可读性更好。一味的抨击 goto 是不正确的,任何事物都有两面性。
3. 实验
- ping GitHub 首页
选 GitHub 是因为这个网站打开的速度很慢。
图2 ping GitHub
- ping Google 首页
选它是因为它在国内被墙,所以肯定会超时。
图3 ping Google.com
- ping 百度首页
图4 ping 百度首页
ping 国内的主机速度还是相当快的。
4. 总结
- 掌握 ping 命令原理