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 from binascii import unhexlify,hexlify
21 # Check and convert hardware/nic/mac address type
23 def __init__(self,value="00:00:00:00:00:00") :
28 self._hw_string = value
29 self._StringToNumlist(value)
31 elif hw_type == list :
32 self._hw_numlist = value
34 self._NumlistToString()
35 else : raise TypeError , 'hwmac init : Valid types are tr and list'
39 # Check if _hw_numlist is valid and raise error if not.
40 def _CheckNumList(self) :
41 if len(self._hw_numlist) != 6 : raise ValueError , "hwmac : wrong list length."
42 for part in self._hw_numlist :
43 if type (part) != int : raise TypeError , "hwmac : each element of list must be int"
44 if part < 0 or part > 255 : raise ValueError , "hwmac : need numbers between 0 and 255."
48 def _StringToNumlist(self,value):
49 self._hw_string = self._hw_string.replace("-",":").replace(".",":")
50 self._hw_string = self._hw_string.lower()
53 for twochar in self._hw_string.split(":"):
54 self._hw_numlist.append(ord(unhexlify(twochar)))
56 # Convert NumList type ip to String type ip
57 def _NumlistToString(self) :
58 self._hw_string = ":".join(map(hexlify,map(chr,self._hw_numlist)))
60 # Convert String type ip to NumList type ip
63 return self._hw_string
66 # return ip list (useful for DhcpPacket class)
68 return self._hw_numlist
71 return self._hw_string.__hash__()
74 return self._hw_string
76 def __cmp__(self,other) :
77 if self._hw_string == other : return 0
80 def __nonzero__(self) :
81 if self._hw_string != "00:00:00:00:00:00" : return 1