2 # Copyright (C) 2005 Mathieu Ignacio -- mignacio@april.org
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 # Check and convert ipv4 address type
21 def __init__(self,value="0.0.0.0") :
24 if not self.CheckString(value) : raise ValueError, "ipv4 string argument is not an valid ip "
25 self._ip_string = value
26 self._StringToNumlist()
28 self._NumlistToString()
29 elif ip_type == list :
30 if not self.CheckNumList(value) : raise ValueError, "ipv4 list argument is not an valid ip "
31 self._ip_numlist = value
32 self._NumlistToString()
34 elif ip_type == int or ip_type == long:
37 self._NumlistToString()
38 elif ip_type == bool :
41 self._NumlistToString()
43 else : raise TypeError , 'ipv4 init : Valid types are str, list, int or long'
47 # Convert Long type ip to numlist ip
48 def _LongToNumlist(self) :
49 self._ip_numlist = [self._ip_long >> 24 & 0xFF]
50 self._ip_numlist.append(self._ip_long >> 16 & 0xFF)
51 self._ip_numlist.append(self._ip_long >> 8 & 0xFF)
52 self._ip_numlist.append(self._ip_long & 0xFF)
53 if not self.CheckNumList(self._ip_numlist) : raise ValueError, "ipv4 list argument is not an valid ip "
54 # Convert String type ip to Long type ip
55 def _StringToLong(self) :
56 ip_numlist = map(int,self._ip_string.split('.'))
57 self._ip_long = ip_numlist[3] + ip_numlist[2]*256 + ip_numlist[1]*256*256 + ip_numlist[0]*256*256*256
58 if not self.CheckNumList(self._ip_numlist) : raise ValueError, "ipv4 list argument is not an valid ip "
59 # Convert NumList type ip to String type ip
60 def _NumlistToString(self) :
61 self._ip_string = ".".join(map(str,self._ip_numlist))
62 if not self.CheckNumList(self._ip_numlist) : raise ValueError, "ipv4 list argument is not an valid ip "
63 # Convert String type ip to NumList type ip
64 def _StringToNumlist(self) :
65 self._ip_numlist = map(int,self._ip_string.split('.'))
66 if not self.CheckNumList(self._ip_numlist) : raise ValueError, "ipv4 list argument is not an valid ip "
68 """ Public methods """
69 # Check if _ip_numlist is valid and raise error if not.
71 def CheckNumList(self,value) :
72 if len(value) != 4 : return False
74 if part < 0 or part > 255 : return False
77 # Check if _ip_numlist is valid and raise error if not.
78 def CheckString(self,value) :
79 tmp = value.split('.')
80 if len(tmp) != 4 : return False
82 if not each.isdigit() : return False
87 return self._ip_string
90 # return ip list (useful for DhcpPacket class)
92 return self._ip_numlist
94 # return Long ip type (useful for SQL ip address backend)
101 """ Useful function for native python operations """
104 return self._ip_long.__hash__()
107 return self._ip_string
109 def __cmp__(self,other) :
110 return cmp(self._ip_long, other._ip_long);
112 def __nonzero__(self) :
113 if self._ip_long != 0 : return 1