发送 UDP 数据包并从 C 中的路由器接收 ICMP 响应

发布于 2024-10-15 21:20:24 字数 1922 浏览 4 评论 0原文

我正在尝试编写一个 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 地址。

期待有用的提示。

此致!

I'm trying write a C program that sends an UDP packet to a given IP adress and waits for an ICMP response of a router telling that the time to live expired. It's kept very simple because I just want to understand the mechanism at first. What I need is someone checking my code giving feedback on what's wrong and what's missing. I'm very unexperienced at C-programming but I have to do it as an assignment - giving my best to understand it...

#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;
}

I keep receiving "Could not process recvmsg()." and I have no clue anymore what else to try. I'd like to receive an ICMP answer and read its senders IP-Address.

Looking forward to helpfull hints.

Best regards!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞美少年 2024-10-22 21:20:24

我通常使用

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);

I normally use

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文