Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

bugfix + refactoring: use clean ipaddr module to determine if gateway…

… is in IP range
  • Loading branch information...
commit ba564054bbf853ddf04dfde54663b2ecb36487b2 1 parent 7ee472b
Liraz Siri authored
Showing with 96 additions and 20 deletions.
  1. +6 −4 confconsole.py
  2. +0 −16 ifutil.py
  3. +90 −0 ipaddr.py
View
10 confconsole.py
@@ -4,6 +4,7 @@
import os
import sys
import dialog
+import ipaddr
from string import Template
import ifutil
@@ -301,18 +302,19 @@ def _validate(addr, netmask, gateway, nameserver):
err = []
if not addr:
err.append("No IP address provided")
- elif not ifutil.is_ipaddr(addr):
+ elif not ipaddr.is_legal_ip(addr):
err.append("Invalid IP address: %s" % addr)
if not netmask:
err.append("No netmask provided")
- elif not ifutil.is_ipaddr(netmask):
+ elif not ipaddr.is_legal_ip(netmask):
err.append("Invalid netmask: %s" % netmask)
- if gateway and not ifutil.is_ipaddr(gateway):
+ if gateway and (not ipaddr.is_legal_ip(gateway) or
+ not gateway in ipaddr.IPRange(addr, netmask)):
err.append("Invalid gateway: %s" % gateway)
- if nameserver and not ifutil.is_ipaddr(nameserver):
+ if nameserver and not ipaddr.is_legal_ip(nameserver):
err.append("Invalid nameserver: %s" % nameserver)
return err
View
16 ifutil.py
@@ -4,7 +4,6 @@
import re
import time
import fcntl
-import struct
import socket
import executil
@@ -137,21 +136,6 @@ def _map_status(attrib):
return 'UNKNOWN'
-def is_ipaddr(ip):
- try:
- if len([ octet for octet in ip.split(".")
- if 255 >= int(octet) >= 0 ]) != 4:
- return False
- except ValueError:
- return False
-
- try:
- packed = socket.inet_aton(ip)
- except socket.error:
- return False
-
- return True
-
def ifup(ifname):
return executil.getoutput("ifup", ifname)
View
90 ipaddr.py
@@ -0,0 +1,90 @@
+# Copyright (c) 2009 Liraz Siri <liraz@turnkeylinux.org> - all rights reserved
+
+import string
+import struct
+import socket
+import math
+
+def is_legal_ip(ip):
+ try:
+ if len([ octet for octet in ip.split(".")
+ if 255 >= int(octet) >= 0 ]) != 4:
+ return False
+ except ValueError:
+ return False
+
+ try:
+ packed = socket.inet_aton(ip)
+ except socket.error:
+ return False
+
+ return True
+
+def _str2int(ip):
+ bytes = map(int, ip.split('.'))
+ ip, = struct.unpack("!L", struct.pack("BBBB", *bytes))
+ return ip
+
+def _int2str(num):
+ bytes = struct.unpack("BBBB", struct.pack("!L", num))
+ return string.join(map(str, bytes), '.')
+
+class Error(Exception):
+ pass
+
+class IP(long):
+ def __new__(cls, arg):
+ if isinstance(arg, IP):
+ return long.__new__(cls, long(arg))
+
+ elif isinstance(arg, (int, long)):
+ return long.__new__(cls, arg)
+
+ else:
+ if not is_legal_ip(arg):
+ raise Error("illegal ip (%s)" % arg)
+
+ return long.__new__(cls, _str2int(arg))
+
+ def __str__(self):
+ return _int2str(self)
+
+ def __repr__(self):
+ return "IP(%s)" % `str(self)`
+
+ def _numeric_method(method):
+ def f(self, other):
+ return IP(getattr(long, method)(self, other))
+
+ return f
+
+ __add__ = _numeric_method("__add__")
+ __sub__ = _numeric_method("__sub__")
+ __and__ = _numeric_method("__and__")
+ __xor__ = _numeric_method("__xor__")
+ __or__ = _numeric_method("__or__")
+
+class IPRange:
+ @classmethod
+ def from_cidr(cls, arg):
+ address, cidr = arg.split('/')
+ netmask = 2 ** 32 - (2 ** (32 - int(cidr)))
+ return cls(address, netmask)
+
+ def __init__(self, ip, netmask):
+ self.ip = IP(ip)
+ self.netmask = IP(netmask)
+ self.network = self.ip & self.netmask
+ self.broadcast = self.network + 2 ** 32 - self.netmask - 1
+ self.cidr = int(32 - math.log(2 ** 32 - self.netmask, 2))
+
+ def __contains__(self, ip):
+ return self.network < IP(ip) < self.broadcast
+
+ def __repr__(self):
+ return "IPRange('%s', '%s')" % (self.ip, self.netmask)
+
+ def fmt_cidr(self):
+ return "%s/%d" % (self.ip, self.cidr)
+
+ __str__ = fmt_cidr
Please sign in to comment.
Something went wrong with that request. Please try again.