You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

type_hwmac.py 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # pydhcplib
  2. # Copyright (C) 2008 Mathieu Ignacio -- mignacio@april.org
  3. #
  4. # This file is part of pydhcplib.
  5. # Pydhcplib is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. from binascii import unhexlify,hexlify
  18. # Check and convert hardware/nic/mac address type
  19. class hwmac:
  20. def __init__(self,value="00:00:00:00:00:00") :
  21. self._hw_numlist = []
  22. self._hw_string = ""
  23. hw_type = type(value)
  24. if hw_type == str :
  25. value = value.strip()
  26. self._hw_string = value
  27. self._StringToNumlist(value)
  28. self._CheckNumList()
  29. elif hw_type == list :
  30. self._hw_numlist = value
  31. self._CheckNumList()
  32. self._NumlistToString()
  33. else : raise TypeError , 'hwmac init : Valid types are str and list'
  34. # Check if _hw_numlist is valid and raise error if not.
  35. def _CheckNumList(self) :
  36. if len(self._hw_numlist) != 6 : raise ValueError , "hwmac : wrong list length."
  37. for part in self._hw_numlist :
  38. if type (part) != int : raise TypeError , "hwmac : each element of list must be int"
  39. if part < 0 or part > 255 : raise ValueError , "hwmac : need numbers between 0 and 255."
  40. return True
  41. def _StringToNumlist(self,value):
  42. self._hw_string = self._hw_string.replace("-",":").replace(".",":")
  43. self._hw_string = self._hw_string.lower()
  44. for twochar in self._hw_string.split(":"):
  45. self._hw_numlist.append(ord(unhexlify(twochar)))
  46. # Convert NumList type ip to String type ip
  47. def _NumlistToString(self) :
  48. self._hw_string = ":".join(map(hexlify,map(chr,self._hw_numlist)))
  49. # Convert String type ip to NumList type ip
  50. # return ip string
  51. def str(self) :
  52. return self._hw_string
  53. # return ip list (useful for DhcpPacket class)
  54. def list(self) :
  55. return self._hw_numlist+[0]*10
  56. def __hash__(self) :
  57. return self._hw_string.__hash__()
  58. def __repr__(self) :
  59. return self._hw_string
  60. def __cmp__(self,other) :
  61. if self._hw_string == other : return 0
  62. return 1
  63. def __nonzero__(self) :
  64. if self._hw_string != "00:00:00:00:00:00" : return 1
  65. return 0