f42dec0efe41773d473fc3646c10ed24c79fa16f
[invirt/packages/invirt-dhcp.git] / code / pydhcplib / pydhcplib / type_ipv4.py
1 # Anemon Dhcp
2 # Copyright (C) 2005 Mathieu Ignacio -- mignacio@april.org
3 #
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.
8 #
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.
13 #
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
17
18
19 # Check and convert ipv4 address type
20 class ipv4:
21     def __init__(self,value="0.0.0.0") :
22         ip_type = type(value)
23         if ip_type == str :
24             if not self.CheckString(value) : raise ValueError, "ipv4 string argument is not an valid ip "
25             self._ip_string = value
26             self._StringToNumlist()
27             self._StringToLong()
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()
33             self._StringToLong()
34         elif ip_type == int or ip_type == long:
35             self._ip_long = value
36             self._LongToNumlist()
37             self._NumlistToString()
38         elif ip_type == bool :
39             self._ip_long = 0
40             self._LongToNumlist()
41             self._NumlistToString()
42             
43         else : raise TypeError , 'ipv4 init : Valid types are  str, list, int or long'
44
45
46
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 "
67
68     """ Public methods """
69     # Check if _ip_numlist is valid and raise error if not.
70     # self._ip_numlist
71     def CheckNumList(self,value) :
72         if len(value) != 4 : return False
73         for part in value :
74             if part < 0 or part > 255 : return False
75         return True
76
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
81         for each in tmp : 
82             if not each.isdigit() : return False
83         return True
84     
85     # return ip string
86     def str(self) :
87         return self._ip_string
88     __str__=str
89
90     # return ip list (useful for DhcpPacket class)
91     def list(self) :
92         return self._ip_numlist
93
94     # return Long ip type (useful for SQL ip address backend)
95     def int(self) :
96         return self._ip_long
97
98
99
100
101     """ Useful function for native python operations """
102
103     def __hash__(self) :
104         return self._ip_long.__hash__()
105
106     def __repr__(self) :
107         return self._ip_string
108
109     def __cmp__(self,other) :
110         return cmp(self._ip_long, other._ip_long);
111
112     def __nonzero__(self) :
113         if self._ip_long != 0 : return 1
114         return 0
115
116
117