Robert Gordon wrote: > > I think this: > > 1460 } else { > 1461 if ((addr & 0x00ffffff) == 0) > 1462 mask = 0xff000000; > 1463 else if ((addr & 0x0000ffff) == 0) > 1464 mask = 0xffff0000; > 1465 else if ((addr & 0x000000ff) == 0) > 1466 mask = 0xffffff00; > 1467 else > 1468 mask = 0xffffffff; > 1469 } > > reads better as: > > if (IN_CLASSA(addr)) > mask = IN_CLASSA_NET; > else if (IN_CLASSB(addr)) > mask = IN_CLASSB_NET; > else if (IN_CLASSC(addr)) > mask = IN_CLASSC_NET; > else > mask = IP_HOST_MASK;
Might read better, but appears not to work correctly: You can see the difference with the first test: addr & 0x00ffffff == 0 versus addr & 0x80000000U == 0 The first says disallow any addr which have any bits set in the lower 3 octets. The second says only allow addr which have 0x80 set in the upper octect and ignore anything in the lower 3. I think you could say this: if ((addr & IN_CLASSA_HOST) == 0) { mask = IN_CLASSA_NET; } else if ((addr & IN_CLASSB_HOST) == 0) { mask = IN_CLASSB_NET; } else if ((addr & IN_CLASSC_HOST) == 0) { mask = IN_CLASSC_NET; } else { mask = IN_CLASSE_NET; } And that pans out via the attached test case... Some test outout before the 3rd try: ============ [th199096 at warlock ~/src]> ./a.out 192.168.2.1 192.168.2.1 yields 3232236033 and that reverse maps to 192.168.2.1 old way:: 16777215 that reverse maps to 0.255.255.255 65535 that reverse maps to 0.0.255.255 255 that reverse maps to 0.0.0.255 Is single IP! We would apply the following netmask (4294967295): 255.255.255.255 new way:: 2147483648 that reverse maps to 128.0.0.0 3221225472 that reverse maps to 192.0.0.0 3758096384 that reverse maps to 224.0.0.0 Is class C! We would apply the following netmask (4294967040): 255.255.255.0 [th199096 at warlock ~/src]> ./a.out 192.168.2.0 192.168.2.0 yields 3232236032 and that reverse maps to 192.168.2.0 old way:: 16777215 that reverse maps to 0.255.255.255 65535 that reverse maps to 0.0.255.255 255 that reverse maps to 0.0.0.255 Is class C! We would apply the following netmask (4294967040): 255.255.255.0 new way:: 2147483648 that reverse maps to 128.0.0.0 3221225472 that reverse maps to 192.0.0.0 3758096384 that reverse maps to 224.0.0.0 Is class C! We would apply the following netmask (4294967040): 255.255.255.0 [th199096 at warlock ~/src]> ./a.out 192.168.0.0 192.168.0.0 yields 3232235520 and that reverse maps to 192.168.0.0 old way:: 16777215 that reverse maps to 0.255.255.255 65535 that reverse maps to 0.0.255.255 255 that reverse maps to 0.0.0.255 Is class B! We would apply the following netmask (4294901760): 255.255.0.0 new way:: 2147483648 that reverse maps to 128.0.0.0 3221225472 that reverse maps to 192.0.0.0 3758096384 that reverse maps to 224.0.0.0 Is class C! We would apply the following netmask (4294967040): 255.255.255.0 [th199096 at warlock ~/src]> ./a.out 192.0.0.0 192.0.0.0 yields 3221225472 and that reverse maps to 192.0.0.0 old way:: 16777215 that reverse maps to 0.255.255.255 65535 that reverse maps to 0.0.255.255 255 that reverse maps to 0.0.0.255 Is class A! We would apply the following netmask (4278190080): 255.0.0.0 new way:: 2147483648 that reverse maps to 128.0.0.0 3221225472 that reverse maps to 192.0.0.0 3758096384 that reverse maps to 224.0.0.0 Is class C! We would apply the following netmask (4294967040): 255.255.255.0