liv2hak
liv2hak

Reputation: 14980

inet_netof() function returns strange value

        addr4 = &((struct sockaddr_in *)ifa->ifa_netmask)->sin_addr;
        internal_addr = inet_netof(*addr4);
        printf("IPv4 MASK %08x \n",internal_addr);

I have the above code. It is printing the IPv4 MASK as

IPv4 MASK 00ffffff 

I am assuming this represents 255.255.255.0. Is that correct assumption? I want to convert that to 24 (an integer in CIDR notation)

Upvotes: 1

Views: 87

Answers (2)

Ron Maupin
Ron Maupin

Reputation: 6452

Per your desire to convert it to the number of mask bits (CIDR notation), just count the number of one bits in the mask.

Just loop through the 32-bit, unsigned integer (0x00ffffff), checking if it is greater than 0, if it is greater than 0 add 1 to your answer, then divide the unsigned integer by 2 (or shift it right 1 bit).

Upvotes: 2

tdao
tdao

Reputation: 17668

IPv4 MASK 00ffffff 

I am assuming this represents 255.255.255.0. Is that correct assumption?

That's correct. 00ffffff in network byte order becomes 0xff 0xff 0xff 0x00, which in turns, becomes 255.255.255.0 in dot notation

Upvotes: 1

Related Questions