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_ipv4.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. # Check and convert ipv4 address type
  18. class ipv4:
  19. def __init__(self,value="0.0.0.0") :
  20. ip_type = type(value)
  21. if ip_type == str :
  22. if not self.CheckString(value) : raise ValueError, "ipv4 string argument is not an valid ip "
  23. self._ip_string = value
  24. self._StringToNumlist()
  25. self._StringToLong()
  26. self._NumlistToString()
  27. elif ip_type == list :
  28. if not self.CheckNumList(value) : raise ValueError, "ipv4 list argument is not an valid ip "
  29. self._ip_numlist = value
  30. self._NumlistToString()
  31. self._StringToLong()
  32. elif ip_type == int or ip_type == long:
  33. self._ip_long = value
  34. self._LongToNumlist()
  35. self._NumlistToString()
  36. elif ip_type == bool :
  37. self._ip_long = 0
  38. self._LongToNumlist()
  39. self._NumlistToString()
  40. else : raise TypeError , 'ipv4 init : Valid types are str, list, int or long'
  41. # Convert Long type ip to numlist ip
  42. def _LongToNumlist(self) :
  43. self._ip_numlist = [self._ip_long >> 24 & 0xFF]
  44. self._ip_numlist.append(self._ip_long >> 16 & 0xFF)
  45. self._ip_numlist.append(self._ip_long >> 8 & 0xFF)
  46. self._ip_numlist.append(self._ip_long & 0xFF)
  47. if not self.CheckNumList(self._ip_numlist) : raise ValueError, "ipv4 list argument is not an valid ip "
  48. # Convert String type ip to Long type ip
  49. def _StringToLong(self) :
  50. ip_numlist = map(int,self._ip_string.split('.'))
  51. self._ip_long = ip_numlist[3] + ip_numlist[2]*256 + ip_numlist[1]*256*256 + ip_numlist[0]*256*256*256
  52. if not self.CheckNumList(self._ip_numlist) : raise ValueError, "ipv4 list argument is not an valid ip "
  53. # Convert NumList type ip to String type ip
  54. def _NumlistToString(self) :
  55. self._ip_string = ".".join(map(str,self._ip_numlist))
  56. if not self.CheckNumList(self._ip_numlist) : raise ValueError, "ipv4 list argument is not an valid ip "
  57. # Convert String type ip to NumList type ip
  58. def _StringToNumlist(self) :
  59. self._ip_numlist = map(int,self._ip_string.split('.'))
  60. if not self.CheckNumList(self._ip_numlist) : raise ValueError, "ipv4 list argument is not an valid ip "
  61. """ Public methods """
  62. # Check if _ip_numlist is valid and raise error if not.
  63. # self._ip_numlist
  64. def CheckNumList(self,value) :
  65. if len(value) != 4 : return False
  66. for part in value :
  67. if part < 0 or part > 255 : return False
  68. return True
  69. # Check if _ip_numlist is valid and raise error if not.
  70. def CheckString(self,value) :
  71. tmp = value.strip().split('.')
  72. if len(tmp) != 4 : return False
  73. for each in tmp :
  74. if not each.isdigit() : return False
  75. return True
  76. # return ip string
  77. def str(self) :
  78. return self._ip_string
  79. # return ip list (useful for DhcpPacket class)
  80. def list(self) :
  81. return self._ip_numlist
  82. # return Long ip type (useful for SQL ip address backend)
  83. def int(self) :
  84. return self._ip_long
  85. """ Useful function for native python operations """
  86. def __hash__(self) :
  87. return self._ip_long.__hash__()
  88. def __repr__(self) :
  89. return self._ip_string
  90. def __cmp__(self,other) :
  91. return cmp(self._ip_long, other._ip_long);
  92. def __nonzero__(self) :
  93. if self._ip_long != 0 : return 1
  94. return 0