icmp->icmp_id == pid
时间: 2023-08-08 09:04:19 浏览: 48
这段代码通常出现在ping程序中,用于检查接收到的ICMP报文是否是自己发送的报文的响应。其中,icmp->icmp_id是ICMP报文中的标识符,pid则是ping程序的进程ID。当接收到的ICMP报文的标识符和ping程序的进程ID一致时,就说明这是自己发送的报文的响应。这段代码通常用于判断网络连通性以及测量网络延迟等。
相关问题
头歌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选项是为了链接数学库,因为代码中使用了数学库中的函数。
icmp协议分析C语言代码
以下是一个使用C语言实现的简单的ICMP协议分析代码,它可以发送和接收ICMP协议报文:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <time.h>
#include <netinet/ip_icmp.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;
// 计算校验和
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;
}
// 发送ICMP报文
void send_packet() {
memset(sendpacket, 0, sizeof(sendpacket));
struct icmp *icmp_hdr = (struct icmp *)sendpacket;
icmp_hdr->icmp_type = ICMP_ECHO;
icmp_hdr->icmp_code = 0;
icmp_hdr->icmp_cksum = 0;
icmp_hdr->icmp_id = pid;
icmp_hdr->icmp_seq = nsend++;
memset(icmp_hdr->icmp_data, 0xa5, datalen);
gettimeofday((struct timeval *)icmp_hdr->icmp_data, NULL);
icmp_hdr->icmp_cksum = cal_chksum((unsigned short *)icmp_hdr, datalen + 8);
sendto(sockfd, sendpacket, datalen + 8, 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
}
// 接收ICMP报文
void recv_packet() {
int n;
socklen_t 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;
}
perror("recvfrom error");
return;
}
struct iphdr *ip_hdr = (struct iphdr *)recvpacket;
int ip_hdr_len = ip_hdr->ihl << 2;
struct icmp *icmp_hdr = (struct icmp *)(recvpacket + ip_hdr_len);
int icmp_len = n - ip_hdr_len;
if (icmp_hdr->icmp_type == ICMP_ECHOREPLY && icmp_hdr->icmp_id == pid) {
struct timeval *tvsend = (struct timeval *)icmp_hdr->icmp_data;
struct timeval tvrecv;
gettimeofday(&tvrecv, NULL);
double rtt = (tvrecv.tv_sec - tvsend->tv_sec) * 1000.0 + (tvrecv.tv_usec - tvsend->tv_usec) / 1000.0;
printf("%d bytes from %s: icmp_seq=%u ttl=%d time=%.1f ms\n", icmp_len, inet_ntoa(dest_addr.sin_addr), icmp_hdr->icmp_seq, ip_hdr->ttl, rtt);
nreceived++;
}
}
}
// 主函数
int main(int argc, char *argv[]) {
struct hostent *host;
struct protoent *protocol;
unsigned long inaddr = 0l;
int waittime = MAX_WAIT_TIME;
int size = 50 * 1024;
if (argc < 2) {
printf("usage:%s hostname/IP address\n", argv[0]);
exit(1);
}
if ((protocol = getprotobyname("icmp")) == NULL) {
perror("getprotobyname");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_RAW, protocol->p_proto)) < 0) {
perror("socket error");
exit(1);
}
setuid(getuid());
setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
bzero(&dest_addr, sizeof(dest_addr));
dest_addr.sin_family = AF_INET;
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 {
dest_addr.sin_addr.s_addr = inaddr;
}
pid = getpid();
printf("PING %s(%s): %d bytes data in ICMP packets.\n", argv[1], inet_ntoa(dest_addr.sin_addr), datalen);
while (nsend < MAX_NO_PACKETS) {
send_packet();
recv_packet();
sleep(waittime);
}
printf("--- %s ping statistics ---\n", argv[1]);
printf("%d packets transmitted, %d received, %%%d lost\n", nsend, nreceived, (nsend - nreceived) / nsend * 100);
close(sockfd);
return 0;
}
```