在C++中,处理ICMP消息通常需要使用原始套接字(raw socket)
#include <iostream>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/icmp.h>
#include <unistd.h>
#include <cstring>
#define ICMP_BUFFER_SIZE 1500
int main(int argc, char *argv[]) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0]<< " <ICMP_message>" << std::endl;
return 1;
}
int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sockfd < 0) {
perror("socket");
return 1;
}
struct sockaddr_in icmp_addr;
memset(&icmp_addr, 0, sizeof(icmp_addr));
icmp_addr.sin_family = AF_INET;
icmp_addr.sin_port = htons(0);
icmp_addr.sin_addr.s_addr = inet_addr(argv[1]);
struct icmp *icmp_header = (struct icmp *)argv[1];
if (sendto(sockfd, icmp_header, ICMP_BUFFER_SIZE, 0, (struct sockaddr *)&icmp_addr, sizeof(icmp_addr)) < 0) {
perror("sendto");
close(sockfd);
return 1;
}
close(sockfd);
return 0;
}
要编译此程序,请使用以下命令:
g++ -o icmp_sender icmp_sender.cpp
然后,你可以使用以下命令发送ICMP Echo请求(ping):
sudo ./icmp_sender <destination_ip>
请注意,运行此程序可能需要管理员权限,因为它需要创建原始套接字。
要处理收到的ICMP消息,你需要编写一个循环来接收数据包,然后解析ICMP头。这涉及到检查ICMP类型和代码字段,以及获取相关的数据包信息(如源IP和目标IP)。这里有一个简单的示例,展示了如何接收和解析ICMP Echo请求(ping响应):
#include <iostream>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/icmp.h>
#include <unistd.h>
#include <cstring>
#define BUF_SIZE 1500
int main(int argc, char *argv[]) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0]<< " <port>" << std::endl;
return 1;
}
int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sockfd < 0) {
perror("socket");
return 1;
}
struct sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(atoi(argv[1]));
serv_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
perror("bind");
close(sockfd);
return 1;
}
struct sockaddr_in from;
socklen_t from_size = sizeof(from);
char buffer[BUF_SIZE];
while (1) {
int n = recvfrom(sockfd, buffer, BUF_SIZE, 0, (struct sockaddr *)&from, &from_size);
if (n < 0) {
perror("recvfrom");
continue;
}
struct icmp *icmp_header = (struct icmp *)(buffer + 20); // Skip Ethernet header
if (icmp_header->icmp_type == ICMP_ECHO && icmp_header->icmp_code == 0) {
std::cout << "Received ICMP Echo reply from " << inet_ntoa(from.sin_addr) << std::endl;
}
}
close(sockfd);
return 0;
}
要编译此程序,请使用以下命令:
g++ -o icmp_receiver icmp_receiver.cpp
然后,你可以使用以下命令运行此程序并监听指定的端口:
sudo ./icmp_receiver <port>
请注意,运行此程序可能需要管理员权限,因为它需要创建原始套接字。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。