I am writing a kernel module and i have to compare to destination addresses..
I have an array of addresses to which i want to add these addresses dynamically. If the address is already in the array i drop it but if it is not i put it at the next available index.
A snipped of my code is as follows:
daddr = &udp_hdr(sock_buff)->dest; /* an example address just for comparison purposes */
saddr = &udp_hdr(sock_buff)->source; /* an example address just for comparison purposes */
int compare( __be32 addr1, __be32 addr2 ) {
addr1[0] = 0xff & add1 >> 24;
addr1[1] = 0xff & add1 >> 16;
addr1[2] = 0xff & add1 >> 8;
addr1[3] = 0xff & add1;
addr2[0] = 0xff & add2 >> 24;
addr2[1] = 0xff & add2 >> 16;
addr2[2] = 0xff & add2 >> 8;
addr2[3] = 0xff & add2;
for(i=0; i<4; i++) {
if(addr1[i]==addr2[i]) {;
i++;
if(i==4) {
return 0;
break;
} else return 1;
}
}
}
Then I’m comparing using the function
int compare( __be32 addr1, __be32 addr2 ) {
addr1[0] = 0xff & add1 >> 24;
addr1[1] = 0xff & add1 >> 16;
addr1[2] = 0xff & add1 >> 8;
addr1[3] = 0xff & add1;
addr2[0] = 0xff & add2 >> 24;
addr2[1] = 0xff & add2 >> 16;
addr2[2] = 0xff & add2 >> 8;
addr2[3] = 0xff & add2;
for(i=0; i<4; i++) {
if(addr1[i]==addr2[i]) {;
i++;
if(i==4) {
return 0;
break;
} else return 1;
}
}
}
However, calling with function with say
compare((__be32)daddr, (__be32)saddr)
comparing addresses like 192.168.1.2 and 192.132.1.2 returns true (herein 0) and in some cases it returns false(herein 1). Where am I going wrong or is there a better way of comparing addresses?
you end up incrementing
ii.ei++twice ifaddr1[i] == addr2[i]also theelseblock is a suspect i guess what you need is