Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Bugfix: current code computes the wrong netmask on some architectures.

See example below, it prints all possible netmasks with the current code
and fixed code. For network masks like 0xffffffe0, math.log(netmask, 2)
returns 4.99999999 on some architectures, which rounded gives 4, which
in turn leads to a netmask of 28 instead of the correct 27.

See the repeated 31 and 28 in the netmasks below.

    >>> import math
    >>> import ctypes
    >>> [32 - int(math.log(ctypes.c_uint32(~(0xffffffff << i)).value + 1, 2)) for i in range(32)]
    [32, 31, 31, 29, 28, 28, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
    >>> [32 - int(round(math.log(ctypes.c_uint32(~(0xffffffff << i)).value + 1, 2), 1)) for i in range(32)]
    [32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
  • Loading branch information...
commit 72447f7dfde4c60e1a4bdc7ce7014fe393bff57f 1 parent f5d2e82
Carlo Contavalli authored
Showing with 2 additions and 1 deletion.
  1. +2 −1  pynetlinux/ifconfig.py
View
3  pynetlinux/ifconfig.py
@@ -219,7 +219,8 @@ def get_netmask(self):
return 0
netmask = socket.ntohl(struct.unpack('16sH2xI8x', res)[2])
- return 32 - int(math.log(ctypes.c_uint32(~netmask).value + 1, 2))
+ return 32 - int(round(
+ math.log(ctypes.c_uint32(~netmask).value + 1, 2), 1))
def set_netmask(self, netmask):
Please sign in to comment.
Something went wrong with that request. Please try again.