简体   繁体   English

如何计算udp数据包大小libpcap

[英]how to calculate udp packet size libpcap

From a linux OS I am trying to write my own data usage monitor in C or python. 从Linux操作系统,我试图用C或python编写自己的数据使用情况监视器。 I've searched and researched for a couple of days now. 我已经搜索和研究了几天。 Currently I am trying to adapt sniffex.c to suit my needs. 目前,我正在尝试使sniffex.c适应我的需求。 I've succeeded in verifying the total bytes sent and received during a few ftp sessions. 我已经成功验证了几个ftp会话期间发送和接收的总字节数。

In sniffex.c the tcp packet size is calculated. 在sniffex.c中,将计算tcp数据包的大小。 My question is how do you calculate the UDP packet size? 我的问题是如何计算UDP数据包大小? I've searched on this topic, but have not found anything. 我已经搜索了该主题,但没有发现任何东西。 Does this question make sense? 这个问题有意义吗?

Update: 更新:

The function where the packet sizes are computed looks like this: 计算数据包大小的函数如下所示:

got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet)
{
...
int size_payload;
...
case IPPROTO_UDP:
    printf("   Protocol: UDP\n");
    size_payload = header->len;
...
}

Do I still need to add 4 to size_payload ? 我还需要在size_payload上加4吗?

The callback to this function looks like this: 该函数的回调如下所示:

/* now we can set our callback function */
pcap_loop(handle, num_packets, got_packet, NULL);

If you have an UDP datagram, you can get its size from its header. 如果您有UDP数据报,则可以从其报头中获取其大小。

For example if you have a pointer char *hdr to UDP datagram header, you can get its length by such a construction 例如,如果您有一个指向UDP数据报头的char * hdr指针,则可以通过这样的构造来获取其长度

int length = *(int *)(hdr + 4);

More about UDP http://en.wikipedia.org/wiki/User_Datagram_Protocol 有关UDP的更多信息http://en.wikipedia.org/wiki/User_Datagram_Protocol

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM