[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] [pytorctl/master 1/2] Fix determination of netmask from CIDR prefix length.



Author: Harry Bock <hbock@xxxxxxxxxxx>
Date: Sat, 29 May 2010 00:16:59 -0400
Subject: Fix determination of netmask from CIDR prefix length.
Commit: d96f599c6ceeb3ae5e3dfd6f5497fb9003042267

ExitPolicyLine parses IP/netmask combinations and supports CIDR notation.
The previous method for calculating an IPv4 netmask from a CIDR prefix length
causes the result to exceed 32 bits, but the result is never truncated,
causing a DeprecationWarning on 64-bit platforms when the integer is packed.

Use a better method for the calculation that always fits in 32 bits.
---
 TorCtl.py |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/TorCtl.py b/TorCtl.py
index 275719b..d6307ce 100755
--- a/TorCtl.py
+++ b/TorCtl.py
@@ -248,7 +248,7 @@ class ExitPolicyLine:
         if re.match(r"\d+.\d+.\d+.\d+", mask):
           self.netmask=struct.unpack(">I", socket.inet_aton(mask))[0]
         else:
-          self.netmask = ~(2**(32 - int(mask)) - 1)
+          self.netmask = 0xffffffff ^ (0xffffffff >> int(mask))
       self.ip = struct.unpack(">I", socket.inet_aton(ip))[0]
     self.ip &= self.netmask
     if port_low == "*":
-- 
1.6.5