Search code examples
clinuxpacket-sniffers

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


I have written a packet sniffer code everything is working but I want to separate packets from specific IP. To do so i need to access the buffer, which stores the IP address by using inet_ntop(). How can i compare "sbuf/dbuf" with specific IP. I tried by storing the IP in another 'char' array and compare them but it didn't work.

This is my code for IP's of incoming packets;

/* 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))
        );

Just help me in comparing specific IP with incoming packets, i'll do the rest.

I am stuck at this for a while now. HELP... !!!

I am working on linux.


Solution

  • You should do it the other way round; convert the IP you are looking for into an integer and compare that directly with ip->saddr:

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

    This should be more efficient and the better way to go.