IP如何使用"inet_ntop()"存储在Buffer中

How does IP store in Buffer using "inet_ntop()"

我已经编写了一个数据包嗅探器代码,一切正常,但我想将数据包与特定 IP 分开。为此,我需要访问缓冲区,该缓冲区使用 inet_ntop() 来存储 IP 地址。我如何将 "sbuf/dbuf" 与特定 IP 进行比较。我尝试将 IP 存储在另一个 'char' 数组中并进行比较,但没有成功。

这是我的传入数据包 IP 代码;

/* Parse IP protocol */
        struct iphdr *ip = (struct iphdr*) next_hdr;
        char sbuf[32];
        char dbuf[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, sbuf, sizeof(sbuf)),
            inet_ntop(AF_INET, &ip->daddr, dbuf, sizeof(dbuf))
        );

只需帮助我将特定 IP 与传入数据包进行比较,剩下的我来做。

我被困在这个问题上有一段时间了。帮助...!!!

我正在研究 linux。

你应该反过来做;把你要找的IP转成一个整数直接和ip->saddr:

比较
in_addr_t x = inet_addr("1.2.3.4");
if (x == ip->saddr) {
    // Do something
}

这应该是更有效和更好的方法。