icmp ping实现
时间: 2024-07-10 10:00:30 浏览: 51
ICMP Ping是一种基于Internet Control Message Protocol(ICMP)的网络诊断工具,用于测试网络连通性。当一台设备向另一台发送一个ICMP Echo Request(也称为Ping请求)时,目标设备会回应一个ICMP Echo Reply(响应),如果网络连接正常,这个过程就会非常快速。以下是ICMP Ping实现的基本步骤:
1. 发送端(客户端)创建一个ICMP Echo Request消息,包含一个序列号和一个时间戳。
2. 这个消息被封装在一个IP数据包中,目标地址是接收端的IP地址。
3. IP数据包携带协议类型(1)和代码(8)标识为ICMP Echo,然后发送到目标网络。
4. 目标网络接收到这个请求后,会检查合法性,然后创建一个ICMP Echo Reply,包括原始的序列号、时间戳以及从接收到现在的时间差(往返延迟)。
5. 这个Echo Reply返回到发送端,通常还会附带一些错误信息(如果有的话)。
6. 发送端解析Echo Reply,比较序列号和时间戳,验证消息是否完整,然后计算并显示往返延迟。
相关问题
头歌icmp ping实现第三关
实现icmp ping需要使用原始套接字,发送ICMP请求并等待响应。以下是实现icmp ping的大致步骤:
1. 创建原始套接字,可以使用socket函数设置socket类型为SOCK_RAW,协议为IPPROTO_ICMP。
2. 构造ICMP请求报文,包括ICMP头和数据部分。ICMP头中需要设置type和code字段为ICMP_ECHO_REQUEST和0,以及设置校验和checksum字段。
3. 将ICMP请求报文发送到目标IP地址,可以使用sendto函数。
4. 等待响应报文,可以使用recvfrom函数。接收到响应报文后,可以解析出ICMP头中的字段,判断是否为ICMP_ECHO_REPLY类型,以及校验和是否正确。
5. 计算并输出ping的延迟时间,可以使用gettimeofday函数获取当前时间,并将其与ICMP请求报文中的timestamp字段进行比较计算。
6. 关闭套接字,释放资源。
需要注意的是,实现icmp ping需要root权限,因为使用了原始套接字。同时,防火墙可能会屏蔽ICMP请求报文,导致无法收到响应,需要进行相应的配置。
头歌icmp ping实现第三关代码答案
以下是一个使用C语言实现icmp ping的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/ip_icmp.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <sys/time.h>
#define PACKET_SIZE 4096
#define MAX_WAIT_TIME 5
#define MAX_NO_PACKETS 3
char sendpacket[PACKET_SIZE];
char recvpacket[PACKET_SIZE];
int sockfd, datalen = 56;
int nsend = 0, nreceived = 0;
struct sockaddr_in dest_addr;
pid_t pid;
void send_packet();
void recv_packet();
void calc_rtt(struct timeval tvrecv);
void tv_sub(struct timeval *out, struct timeval *in);
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() {
memset(sendpacket, 0, sizeof(sendpacket));
struct icmp *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, 'a', datalen);
gettimeofday((struct timeval*)icmp->icmp_data, NULL);
icmp->icmp_cksum = cal_chksum((unsigned short*)icmp, sizeof(struct icmp) + datalen);
sendto(sockfd, sendpacket, sizeof(struct icmp) + datalen, 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
}
void recv_packet() {
int n;
socklen_t fromlen;
struct timeval tvrecv;
struct ip *ip;
struct icmp *icmp;
fromlen = sizeof(dest_addr);
if ((n = recvfrom(sockfd, recvpacket, sizeof(recvpacket), 0, (struct sockaddr *)&dest_addr, &fromlen)) < 0) {
perror("recvfrom error");
return;
}
gettimeofday(&tvrecv, NULL);
ip = (struct ip*)recvpacket;
icmp = (struct icmp*)(recvpacket + (ip->ip_hl << 2));
if (icmp->icmp_type == ICMP_ECHOREPLY && icmp->icmp_id == pid) {
calc_rtt(tvrecv);
}
}
void calc_rtt(struct timeval tvrecv) {
struct timeval *tvsend;
double rtt;
tvsend = (struct timeval*)recvpacket;
tv_sub(&tvrecv, tvsend);
rtt = tvrecv.tv_sec * 1000.0 + tvrecv.tv_usec / 1000.0;
printf("%d bytes from %s: icmp_seq=%u ttl=%d time=%.3f ms\n", datalen, inet_ntoa(dest_addr.sin_addr), nreceived + 1, ip->ip_ttl, rtt);
nreceived++;
}
void tv_sub(struct timeval *out, struct timeval *in) {
if ((out->tv_usec -= in->tv_usec) < 0) {
--out->tv_sec;
out->tv_usec += 1000000;
}
out->tv_sec -= in->tv_sec;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("usage: %s <hostname/IP address>\n", argv[0]);
exit(1);
}
struct hostent *host;
host = gethostbyname(argv[1]);
if (host == NULL) {
printf("unknown host %s\n", argv[1]);
exit(1);
}
pid = getpid();
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sockfd < 0) {
perror("socket error");
exit(1);
}
dest_addr.sin_family = AF_INET;
dest_addr.sin_addr = *((struct in_addr *)host->h_addr);
printf("PING %s (%s): %d data bytes\n", argv[1], inet_ntoa(dest_addr.sin_addr), datalen);
while (nsend < MAX_NO_PACKETS) {
send_packet();
recv_packet();
sleep(1);
}
printf("--- %s ping statistics ---\n", argv[1]);
printf("%d packets transmitted, %d received, %.0f%% packet loss\n", nsend, nreceived, (nsend - nreceived) * 100.0 / nsend);
close(sockfd);
return 0;
}
```
注意在编译时需要加上-lm选项,即:
```bash
gcc icmp_ping.c -o icmp_ping -lm
```
其中-lm选项是为了链接数学库,因为代码中使用了数学库中的函数。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)