#include "stdio.h"
#define U32 unsigned int
U32 atoinet(char *ip_str)
{
U32 ip = 0;
int a, b, c, d;
sscanf(ip_str, "%d.%d.%d.%d", &a, &b, &c, &d);
ip = a<<24|b<<16|c<<8|d;
return ip;
}
int is_same_subnet(U32 host, U32 remote, U32 hmask, U32 rmask)
{
if ((host & hmask) == (remote & rmask))
return 1;
else
return 0;
}
int is_mcast_ip(U32 ip)
{
U32 mcast_high = atoinet("224.0.0.0");
U32 mcast_low = atoinet("239.255.255.255");
if (ip <= mcast_high && ip >= mcast_low)
return 1;
else
return 0;
}
int main()
{
char *hostip = "192.168.2.1";
char *remote_ip = "192.168.2.10";
char *host_mask = "255.255.255.0";
char *remote_mask = "255.255.255.0";
U32 hip, rip, hmask, rmask;
hip = atoinet(hostip);
hmask = atoinet(host_mask);
rip = atoinet(remote_ip);
rmask = atoinet(remote_mask);
printf("Host IP: %x\n", hip);
printf("Neighbor IP: %x\n", rip);
printf("Host Mask: %x\n", hmask);
printf("Remote Mask: %x\n", rmask);
printf("Neighbor IP %s %s\n",
is_mcast_ip(rip)?"is multicast IP":"not a multicast IP");
printf("Host and Neighbor in same subnet: %s\n",
is_same_subnet(hip, rip, hmask, rmask)?"Yes":"No");
return 0;
}