收到的UDP数据包长度

发布于 2024-08-21 11:12:51 字数 3621 浏览 12 评论 0原文

如何获取收到的UDP数据包的长度?使用wireshark我可以看到数据报的正确长度。如何在我的简单 udp 服务器程序中打印该值?我正在接收二进制数据(不可打印的 ascii 字符作为数据),因此我无法使用 strlen(buf) ,它会抛出错误的长度。

      if (ret=recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)==-1){
            error = ioctl(s, FIONREAD, &value);
            printf(" from ioctl UDP packet length is : %d error is : %d\n", value, error);
      }

从上面的代码来看,udp 数据包长度始终为“0”。有什么意见吗?

我也尝试过如下所示

         if (ret=recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)!=-1){
            unsigned short iphdrlen;
            struct iphdr* iph = (struct iphdr*)buf;
            iphdrlen =iph->ihl*4;

            printf("IP version :%d\n", ((unsigned int)((iph->version))));
            printf("protocol .. %d\n", ((unsigned int)(iph->protocol)));
            printf("total len .. %d\n", (ntohs(iph->tot_len)));
         }

上面的代码总是从 ip 标头返回错误的值?有什么意见吗?

原始 C 文件包含在此处。

#include "udp_common.h"

int main(void)
{
        struct sockaddr_in si_me, si_other;
        int s, i, slen=sizeof(si_other);
        unsigned char buf[BUFLEN];
        int noofbytes=0;
        int ret=0,error,value;

        memset(buf, 0, 512);
        if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
                diep("socket");

        memset((char *) &si_me, 0, sizeof(si_me));
        si_me.sin_family = AF_INET;
        si_me.sin_port = htons(PORT);
        si_me.sin_addr.s_addr = htonl(INADDR_ANY);
        if (bind(s, (struct sockaddr *)&si_me, sizeof(si_me))==-1)
                diep("bind");

        for (i=0; ; i++)
        //for (i=0; i<NPACK ; i++) 
        {
                if (ret=recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)==-1)
                diep("recvfrom()");
                printf(" source port is : %d %d\n", ntohs(si_other.sin_port), slen);

                unsigned short iphdrlen;
                struct iphdr* iph = (struct iphdr*)buf;
                iphdrlen =iph->ihl*4;

                printf("IP version :%d\n", ((unsigned int)((iph->version))));
                printf("protocol .. %d\n", ((unsigned int)(iph->protocol)));
                printf("total len .. %d\n", (ntohs(iph->tot_len)));
                error = ioctl(s, FIONREAD, &value);
                printf(" from ioctl UDP packet length is : %d error is : %d\n", value, error);
                printf(" Return code from recvfrom is : %d\n", ret);
                printf("Received packet from %s:%d\nData: %s\n\n",
                inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);

                int j=0;
                for ( j = 0; j <20; j++)
                {
                        char x = buf[j];
                        short int i;
                        for (i=1;i<9;i++)
                        {
                                ((x | (1<<8-i)) == x)?printf("1"):printf("0");
                        }
                        printf(" ");
                }
                printf("\n");
        }


        close(s);
        return 0;
}

我收到的输出如下:

 source port is : 4232 16
IP version :1
protocol .. 65
total len .. 4096
 from ioctl UDP packet length is : 0 error is : 0
 Return code from recvfrom is : 0
Received packet from 127.0.0.1:4232
Data: 

00010000 00000001 00010000 00000000 11110001 00010001 00010001 00100100 01000100 01000001 00010001 00100100 01000100 01000000 00000000 00010000 00000000 00010000 10100000 10100000 

上述解密数据与我发送的数据匹配。

How to get the length of received UDP packet? Using wireshark I could see the correct length of datagram. How can I print this value in my simple udp server program? I am receiving binary data (Non printable ascii characters as a data) So I can not use strlen(buf) which throws incorrect length.

      if (ret=recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)==-1){
            error = ioctl(s, FIONREAD, &value);
            printf(" from ioctl UDP packet length is : %d error is : %d\n", value, error);
      }

udp packet length is always '0' from the above code. Any comments?

I also tried like below

         if (ret=recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)!=-1){
            unsigned short iphdrlen;
            struct iphdr* iph = (struct iphdr*)buf;
            iphdrlen =iph->ihl*4;

            printf("IP version :%d\n", ((unsigned int)((iph->version))));
            printf("protocol .. %d\n", ((unsigned int)(iph->protocol)));
            printf("total len .. %d\n", (ntohs(iph->tot_len)));
         }

The above code always returns me wrong values from ip header? Any comments?

The original C file is included here.

#include "udp_common.h"

int main(void)
{
        struct sockaddr_in si_me, si_other;
        int s, i, slen=sizeof(si_other);
        unsigned char buf[BUFLEN];
        int noofbytes=0;
        int ret=0,error,value;

        memset(buf, 0, 512);
        if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
                diep("socket");

        memset((char *) &si_me, 0, sizeof(si_me));
        si_me.sin_family = AF_INET;
        si_me.sin_port = htons(PORT);
        si_me.sin_addr.s_addr = htonl(INADDR_ANY);
        if (bind(s, (struct sockaddr *)&si_me, sizeof(si_me))==-1)
                diep("bind");

        for (i=0; ; i++)
        //for (i=0; i<NPACK ; i++) 
        {
                if (ret=recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)==-1)
                diep("recvfrom()");
                printf(" source port is : %d %d\n", ntohs(si_other.sin_port), slen);

                unsigned short iphdrlen;
                struct iphdr* iph = (struct iphdr*)buf;
                iphdrlen =iph->ihl*4;

                printf("IP version :%d\n", ((unsigned int)((iph->version))));
                printf("protocol .. %d\n", ((unsigned int)(iph->protocol)));
                printf("total len .. %d\n", (ntohs(iph->tot_len)));
                error = ioctl(s, FIONREAD, &value);
                printf(" from ioctl UDP packet length is : %d error is : %d\n", value, error);
                printf(" Return code from recvfrom is : %d\n", ret);
                printf("Received packet from %s:%d\nData: %s\n\n",
                inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);

                int j=0;
                for ( j = 0; j <20; j++)
                {
                        char x = buf[j];
                        short int i;
                        for (i=1;i<9;i++)
                        {
                                ((x | (1<<8-i)) == x)?printf("1"):printf("0");
                        }
                        printf(" ");
                }
                printf("\n");
        }


        close(s);
        return 0;
}

And the output I am receivingis as follows:

 source port is : 4232 16
IP version :1
protocol .. 65
total len .. 4096
 from ioctl UDP packet length is : 0 error is : 0
 Return code from recvfrom is : 0
Received packet from 127.0.0.1:4232
Data: 

00010000 00000001 00010000 00000000 11110001 00010001 00010001 00100100 01000100 01000001 00010001 00100100 01000100 01000000 00000000 00010000 00000000 00010000 10100000 10100000 

The above decrypted data matched with my sent data.

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

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

发布评论

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

评论(3

离不开的别离 2024-08-28 11:12:52

你是如何创建你的套接字的?它是原始套接字(SOCK_RAW)还是dgram套接字(SOCK_DGRAM)?在第二种情况下,您不会获得 ip/udp 标头,而只会获得有效负载。

顺便说一句,数据包长度是函数 recvfrom 的返回值 - 在您的例子中是“ret”变量。检查 recvfrom 手册页。

How did you create your socket ? Is it a raw socket (SOCK_RAW) or a dgram socket (SOCK_DGRAM) ? In the second case, you won't get ip/udp headers, only payload.

By the way, the packet length is the returned value of the function recvfrom - in your case, the "ret" variable. Check recvfrom man page.

坦然微笑 2024-08-28 11:12:52

简短的回答(尽管它已经在上面的注释中):recvfrom 返回接收到的字节数。

Short answer (although it is already in the comments above): recvfrom returns the number of bytes received.

慵挽 2024-08-28 11:12:52

问题在这里:

if (ret=recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)==-1)

将其重写为:

ret=recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)
if (ret==-1)
{
}

ret 将包含字节数

problem is here:

if (ret=recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)==-1)

rewrite it as:

ret=recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *)&si_other, &slen)
if (ret==-1)
{
}

ret will contain the number of bytes

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