简体   繁体   English

在Linux(C程序)下捕获数据包时使用相同的IP地址

[英]Same IP Addresses while capturing Packets under Linux (C Program)

I am receiving network packets by writing a code in c. 我通过在c中编写代码来接收网络数据包。 So far I am receiving packets correctly but the only problem I'm facing is that I'm having same IP addresses for Ethernet, TCP and UDP. 到目前为止,我可以正确接收数据包,但是我面临的唯一问题是我拥有用于​​以太网,TCP和UDP的相同IP地址。 i'm not having any trouble while receiving via tcpdump. 通过tcpdump接收时我没有任何麻烦。

Ethernet source of my system is: b0:10:41:fc:d7:2f 我系统的以太网源是: b0:10:41:fc:d7:2f
And IP address of my interface is: 192.168.10.145 我的接口的IP地址是: 192.168.10.145

These are received packets: 这些是收到的数据包:

162 >>> Received packet with 66 bytes: Ethernet src: b0:10:41:fc:d7:2f dst: b0:10:41:fc:d7:2f type: 0x800 IP version: 4 ihl: 5 ttl: 64 protocol: 6 src: 192.168.10.145 dst 192.168.10.145 TCP src: 46888 dst: 80 seq: 3048209837 win: 4508 ACK 000000: b728 0050 b5af fdad 0e1d 21a1 8010 119c .(.P......!..... 0x0010: e258 0000 0101 080a 5a05 1f81 0595 4669 162 >>>接收到的具有66个字节的数据包:以太网src:b0:10:41:fc:d7:2f dst:b0:10:41:fc:d7:2f类型:0x800 IP版本:4 ihl:5 ttl:64协议:6 src:192.168.10.145 dst 192.168.10.145 TCP src:46888 dst:80 seq:3048209837 win:4508 ACK 000000:b728 0050 b5af fdad 0e1d 21a1 8010 119c。(。P ......!... .. 0x0010:e258 0000 0101 080a 5a05 1f81 0595 4669

163 >>> Received packet with 66 bytes: Ethernet src: b0:10:41:fc:d7:2f dst: b0:10:41:fc:d7:2f type: 0x800 IP version: 4 ihl: 5 ttl: 64 protocol: 6 src: 192.168.10.145 dst 192.168.10.145 TCP src: 38836 dst: 443 seq: 1969857171 win: 341 ACK 000000: 97b4 01bb 7569 a293 0473 15bc 8010 0155 ....ui...s.....U 0x0010: 11f1 0000 0101 080a 4011 29b5 45f5 c4da 163 >>>接收到的具有66个字节的数据包:以太网src:b0:10:41:fc:d7:2f dst:b0:10:41:fc:d7:2f类型:0x800 IP版本:4 ihl:5 ttl:64协议:6 src:192.168.10.145 dst 192.168.10.145 TCP src:38836 dst:443 seq:1969857171 win:341 ACK 000000:97b4 01bb 7569 a293 0473 15bc 8010 0155 .... ui ... s ..... U 0x0010:11f1 0000 0101 080a 4011 29b5 45f5 c4da

164 >>> Received packet with 1024 bytes: Ethernet src: 0:1a:a0:3f:d6:fc dst: 0:1a:a0:3f:d6:fc type: 0x800 IP version: 4 ihl: 5 ttl: 64 protocol: 6 src: 110.93.233.24 dst 110.93.233.24 TCP src: 80 dst: 46888 seq: 236790177 win: 595 ACK 000000: 0050 b728 0e1d 21a1 b5af fdad 8010 0253 .P.(..!........S 0x0010: 6e5f 0000 0101 080a 0595 46a1 5a05 199a n_........FZ.. 0x0020: f107 eb73 1b82 1492 c88f e84c 101a 9416 ...s.......L.... 0x0030: 9a27 900f 2020 1985 836f 79d5 8a26 15fa .'.. ...oy..&.. 164 >>>接收到的具有1024个字节的数据包:以太网src:0:1a:a0:3f:d6:fc dst:0:1a:a0:3f:d6:fc类型:0x800 IP版本:4 ihl:5 ttl:64协议:6 src:110.93.233.24 dst 110.93.233.24 TCP src:80 dst:46888 seq:236790177 win:595 ACK 000000:0050 b728 0e1d 21a1 b5af fdad 8010 0253 .P。(.. .S 0x0010:6e5f 0000 0101 080a 0595 46a1 5a05 199a n _........ FZ .. 0x0020:f107 eb73 1b82 1492 c88f e84c 101a 9416 ... s ....... L .... 0x0030:9a27 900f 2020 1985 836f 79d5 8a26 15fa。'.. ... oy ...

And this is my code: 这是我的代码:

layer2: {
struct ethhdr *eth = (struct ethhdr*) data;
printf("\tEthernet src: %s dst: %s type: %#04x\n",
    ether_ntoa((const struct ether_addr*) eth->h_source),
    ether_ntoa((const struct ether_addr*) eth->h_dest),
    ntohs(eth->h_proto)
);
protocol = ntohs(eth->h_proto);
next_hdr = (char *) (eth + 1);}

layer3: switch (protocol) {
    case ETH_P_IP: {
        /* Parse IP protocol */
        struct iphdr *ip = (struct iphdr*) next_hdr;
        char buf[32];
        printf("\tIP version: %u ihl: %u ttl: %u protocol: %u src: %s dst %s\n",    
            ip->version,
            ip->ihl,
            ip->ttl,
            ip->protocol,
            inet_ntop(AF_INET, &ip->saddr, buf, sizeof(buf)),
            inet_ntop(AF_INET, &ip->daddr, buf, sizeof(buf))
        );

What am I doing wrong ? 我究竟做错了什么 ?

You're using the same buf to hold both IP addresses: 您使用相同的buf来保存两个IP地址:

inet_ntop(AF_INET, &ip->saddr, buf, sizeof(buf)),
inet_ntop(AF_INET, &ip->daddr, buf, sizeof(buf))

Because you're using the same buffer, and both calls to inet_ntop() are done before the call to printf() , the last call to inet_ntop() will overwrite the results from the first call. 因为您使用的是相同的缓冲区,并且对inet_ntop()两次调用都在对printf()的调用之前完成,所以对inet_ntop()的最后一次调用将覆盖第一次调用的结果。

If following the first Advise, wouldn't work. 如果遵循第一个建议,将无法正常工作。 Maybe you need a Buffer for Ethernet too ? 也许您还需要一个用于以太网的缓冲区?

Eg 例如

struct ethhdr *eth = (struct ethhdr *)Buffer; struct ethhdr * eth =(struct ethhdr *)缓冲区;

and get the Data with 并获得数据

eth->h_dest[0] , eth->h_dest[1] , eth->h_dest[2] , eth->h_dest[3] , eth->h_dest[4] , >eth->h_dest[5] eth-> h_dest [0],eth-> h_dest [1],eth-> h_dest [2],eth-> h_dest [3],eth-> h_dest [4],> eth-> h_dest [5]

?

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

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