Question

please look at the code snippet

char ipAddr[] = {192, 168, 88, 2};
struct iphdr *ip_hdr = (struct iphdr*)(some_valid_eth_hdr_pointer + 1);
if (0 == memcmp((void*)(ip_hdr->saddr), (void*)ipAddr, 4)) /*memcmp cause my whole system crashed*/
{
    printk("ip source addr matched\n");
}

the code is extracted from a linux netfilter hook function, which means the ip_hdr->saddr may belongs to the kernel space memory, and i am pretty sure the ip_hdr points to valid memory. still dont know what the problem is, so please help me out.

thanks in advance !

Was it helpful?

Solution

Well, here's your problem:

memcmp((void*)(ip_hdr->saddr), ...

This interprets the source IP address as a pointer. What you wanted to do was this:

memcmp(&ip_hdr->saddr, ...)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top