我正在尝试编写一个C程序,该程序将UDP数据包发送到给定的IP地址,并等待路由器的ICMP响应,告知生存时间已到期。它保持非常简单,因为我只想先了解机制。我需要的是有人检查我的代码,提供有关错误和缺失的反馈。我对C编程非常缺乏经验,但我必须将其作为一项任务 - 尽我所能去理解......
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/ip.h>
// The packet length
#define PCKT_LEN 8192
// Source IP, source port, target IP, target port from the command line arguments
int main(int argc, char *argv[])
{
int send, recv, resp;
int ttl = 1; // Time to live
char buffer[PCKT_LEN];
// Destination address
struct sockaddr_in dst;
// ICMP Response
struct msghdr icmp;
memset(buffer, 0, PCKT_LEN);
// Create a raw socket with UDP protocol
if ((send = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
{
printf("Could not process socket() [send].\n");
return EXIT_FAILURE;
}
// ICMP Socket
if ((recv = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
{
printf("Could not process socket() [recv].\n");
return EXIT_FAILURE;
}
dst.sin_family = AF_INET;
dst.sin_addr.s_addr = inet_addr("74.125.39.106");
dst.sin_port = htons(60001);
// Define time to life
if(setsockopt(send, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) < 0)
{
printf("Could not process setsockopt().\n");
return EXIT_FAILURE;
}
if(sendto(send, buffer, sizeof(buffer), 0, (struct sockaddr *) &dst, sizeof(dst)) < 0)
{
printf("Could not process sendto().\n");
return EXIT_FAILURE;
}
if((resp = recvmsg(recv, (struct msghdr *) &icmp, IP_RECVERR|MSG_ERRQUEUE)) < 0 )
{
printf("Could not process recvmsg().\n");
return EXIT_FAILURE;
}
close(send);
close(recv);
return 0;
}
我一直收到“无法处理recvmsg()”。我不知道还有什么可以尝试的。我想收到ICMP答案并阅读其发件人IP地址。
期待有用的提示。
祝你好运!
答案 0 :(得分:6)
我通常使用
recvfrom(serversock, buf, 100, 0, (struct sockaddr *)&rcv,&size);
printf("Received packet from %s:%d\nData: %s\n\n", inet_ntoa(rcv.sin_addr), ntohs(rcv.sin_port), buf);