您好,登錄后才能下訂單哦!
在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;
}
要編譯此程序,請(qǐng)使用以下命令:
g++ -o icmp_sender icmp_sender.cpp
然后,你可以使用以下命令發(fā)送ICMP Echo請(qǐng)求(ping):
sudo ./icmp_sender <destination_ip>
請(qǐng)注意,運(yùn)行此程序可能需要管理員權(quán)限,因?yàn)樗枰獎(jiǎng)?chuàng)建原始套接字。
要處理收到的ICMP消息,你需要編寫(xiě)一個(gè)循環(huán)來(lái)接收數(shù)據(jù)包,然后解析ICMP頭。這涉及到檢查ICMP類(lèi)型和代碼字段,以及獲取相關(guān)的數(shù)據(jù)包信息(如源IP和目標(biāo)IP)。這里有一個(gè)簡(jiǎn)單的示例,展示了如何接收和解析ICMP Echo請(qǐng)求(ping響應(yīng)):
#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;
}
要編譯此程序,請(qǐng)使用以下命令:
g++ -o icmp_receiver icmp_receiver.cpp
然后,你可以使用以下命令運(yùn)行此程序并監(jiān)聽(tīng)指定的端口:
sudo ./icmp_receiver <port>
請(qǐng)注意,運(yùn)行此程序可能需要管理員權(quán)限,因?yàn)樗枰獎(jiǎng)?chuàng)建原始套接字。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。