解析以太网,IP和TCP标头

时间:2016-11-19 23:24:36

标签: c networking tcp ip ethernet

我想了解如何迭代使用pcap收集的数据包。

#include <pcap.h>
#include <netinet/if_ether.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>

void analyse(struct pcap_pkthdr *header, const unsigned char *packet, int verbose) {

    /** Ethernet header has a fixed value, IP header and TCP header don't **/
    ip_size = sizeof( struct ip );
    tcp_size = sizeof( struct tcphdr );

    /* Assign each pointer its correct value **/
    const struct ether_header *ethernet = ( struct ether_header* ) packet;
    const struct ip *ip = (struct ip*) ( packet + ETH_HLEN );
    const struct tcp *tcp = (struct tcphdr*) (packet + ETH_HLEN + ip_size );
    const char *payload = ( packet + ETH_HLEN + ip_size + tcp_size );

}

我可以确定ethernetiptcp,有效负载分别指向:

  • 数据链路层(以太网标头)的第一位
  • 网络层的第一位(IP标头)
  • 传输层的第一位(TCP标头)
  • 有效载荷的第一位

谢谢,

0 个答案:

没有答案
相关问题