3
0

IPv4.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import re
  2. class IPAddress:
  3. """
  4. A simple class encapsulating an ip-address. An IPAddress can be constructed by string, int and 4-element-list
  5. (e.g. [8, 8, 8, 8]). This is a leightweight class as it only contains string-to-ip-and-reverse-conversion
  6. and some convenience methods.
  7. """
  8. # a number between 0 and 255, no leading zeros
  9. _IP_NUMBER_REGEXP = r"(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)"
  10. # 4 numbers between 0 and 255, joined together with dots
  11. IP_REGEXP = r"{0}\.{0}\.{0}\.{0}".format(_IP_NUMBER_REGEXP)
  12. def __init__(self, intlist: "list[int]") -> "IPAddress":
  13. """
  14. Construct an ipv4-address with a list of 4 integers, e.g. to construct the ip 10.0.0.0 pass [10, 0, 0, 0]
  15. """
  16. if not isinstance(intlist, list) or not all(isinstance(n, int) for n in intlist):
  17. raise TypeError("The first constructor argument must be an list of ints")
  18. if not len(intlist) == 4 or not all(0 <= n <= 255 for n in intlist):
  19. raise ValueError("The integer list must contain 4 ints in range of 0 and 255, like an ip-address")
  20. # For easier calculations store the ip as integer, e.g. 10.0.0.0 is 0x0a000000
  21. self.ipnum = int.from_bytes(bytes(intlist), "big")
  22. @staticmethod
  23. def parse(ip: str) -> "IPAddress":
  24. """
  25. Parse an ip-address-string. If the string does not comply to the ipv4-format a ValueError is raised
  26. :param ip: A string-representation of an ip-address, e.g. "10.0.0.0"
  27. :return: IPAddress-object describing the ip-address
  28. """
  29. match = re.match("^" + IPAddress.IP_REGEXP + "$", ip)
  30. if not match:
  31. raise ValueError("%s is no ipv4-address" % ip)
  32. # the matches we get are the numbers of the ip-address (match 0 is the whole ip-address)
  33. numbers = [int(match.group(i)) for i in range(1, 5)]
  34. return IPAddress(numbers)
  35. @staticmethod
  36. def from_int(numeric: int) -> "IPAddress":
  37. if numeric not in range(1 << 32):
  38. raise ValueError("numeric value must be in uint-range")
  39. # to_bytes is the easiest way to split a 32-bit int into bytes
  40. return IPAddress(list(numeric.to_bytes(4, "big")))
  41. @staticmethod
  42. def is_ipv4(ip: str) -> bool:
  43. """
  44. Check if the supplied string is in ipv4-format
  45. """
  46. match = re.match("^" + IPAddress.IP_REGEXP + "$", ip)
  47. return True if match else False
  48. def to_int(self) -> int:
  49. """
  50. Convert the ip-address to a 32-bit uint, e.g. IPAddress.parse("10.0.0.255").to_int() returns 0x0a0000ff
  51. """
  52. return self.ipnum
  53. def is_private(self) -> bool:
  54. """
  55. Returns a boolean indicating if the ip-address lies in the private ip-segments (see ReservedIPBlocks)
  56. """
  57. return ReservedIPBlocks.is_private(self)
  58. def get_private_segment(self) -> bool:
  59. """
  60. Return the private ip-segment the ip-address belongs to (there are several)
  61. If this ip does not belong to a private ip-segment a ValueError is raised
  62. :return: IPAddressBlock
  63. """
  64. return ReservedIPBlocks.get_private_segment(self)
  65. def is_localhost(self) -> bool:
  66. """
  67. Returns a boolean indicating if the ip-address lies in the localhost-segment
  68. """
  69. return ReservedIPBlocks.is_localhost(self)
  70. def is_multicast(self) -> bool:
  71. """
  72. Returns a boolean indicating if the ip-address lies in the multicast-segment
  73. """
  74. return ReservedIPBlocks.is_multicast(self)
  75. def is_reserved(self) -> bool:
  76. """
  77. Returns a boolean indicating if the ip-address lies in the reserved-segment
  78. """
  79. return ReservedIPBlocks.is_reserved(self)
  80. def is_zero_conf(self) -> bool:
  81. """
  82. Returns a boolean indicating if the ip-address lies in the zeroconf-segment
  83. """
  84. return ReservedIPBlocks.is_zero_conf(self)
  85. def _tuple(self) -> (int,int,int,int):
  86. return tuple(self.ipnum.to_bytes(4, "big"))
  87. def __repr__(self) -> str:
  88. """
  89. Following the python style guide, eval(repr(obj)) should equal obj
  90. """
  91. return "IPAddress([%i, %i, %i, %i])" % self._tuple()
  92. def __str__(self) -> str:
  93. """
  94. Return the ip-address described by this object in ipv4-format
  95. """
  96. return "%i.%i.%i.%i" % self._tuple()
  97. def __hash__(self) -> int:
  98. return self.ipnum
  99. def __eq__(self, other) -> bool:
  100. if other is None:
  101. return False
  102. return isinstance(other, IPAddress) and self.ipnum == other.ipnum
  103. def __lt__(self, other) -> bool:
  104. if other is None:
  105. raise TypeError("Cannot compare to None")
  106. if not isinstance(other, IPAddress):
  107. raise NotImplemented # maybe other can compare to self
  108. return self.ipnum < other.ipnum
  109. def __int__(self) -> bool:
  110. return self.ipnum
  111. class IPAddressBlock:
  112. """
  113. This class describes a block of IPv4-addresses, just as a string in CIDR-notation does.
  114. It can be seen as a range of ip-addresses. To check if a block contains a ip-address
  115. simply use "ip in ip_block"
  116. """
  117. # this regex describes CIDR-notation (an ip-address plus "/XX", whereas XX is a number between 1 and 32)
  118. CIDR_REGEXP = IPAddress.IP_REGEXP + r"(\/(3[0-2]|[12]?\d)|)?"
  119. def __init__(self, ip: "Union(str, list, IPAddress)", netmask = 32) -> "IPAddressBlock":
  120. """
  121. Construct a ip-block given a ip-address and a netmask. Given an ip and a netmask,
  122. the constructed ip-block will describe the range ip/netmask (e.g. 127.0.0.1/8)
  123. :param ip: An ip-address, represented as IPAddress, string or 4-element-list
  124. """
  125. if isinstance(ip, str):
  126. ip = IPAddress.parse(ip)
  127. elif isinstance(ip, list):
  128. ip = IPAddress(ip)
  129. if not 1 <= netmask <= 32:
  130. raise ValueError("netmask must lie between 1 and 32")
  131. # clear the unnecessary bits in the base-ip, e.g. this will convert 10.0.0.255/24 to 10.0.0.0/24 which are equivalent
  132. self.ipnum = ip.to_int() & self._bitmask(netmask)
  133. self.netmask = netmask
  134. @staticmethod
  135. def parse(cidr: str) -> "IPAddressBlock":
  136. """
  137. Parse a string in cidr-notation and return a IPAddressBlock describing the ip-segment
  138. If the string is not in cidr-notation a ValueError is raised
  139. """
  140. match = re.match("^" + IPAddressBlock.CIDR_REGEXP + "$", cidr)
  141. if not match:
  142. raise ValueError("%s is no valid cidr-notation" % cidr)
  143. ip = [int(match.group(i)) for i in range(1, 5)]
  144. suffix = 32 if not match.group(6) else int(match.group(6))
  145. return IPAddressBlock(ip, suffix)
  146. def block_size(self) -> int:
  147. """
  148. Return the size of the ip-address-block. E.g. the size of someip/24 is 256
  149. """
  150. return 2 ** (32 - self.netmask)
  151. def first_address(self) -> IPAddress:
  152. """
  153. Return the first ip-address of the ip-block
  154. """
  155. return IPAddress.from_int(self.ipnum)
  156. def last_address(self) -> IPAddress:
  157. """
  158. Return the last ip-address of the ip-block
  159. """
  160. return IPAddress.from_int(self.ipnum + self.block_size() - 1)
  161. def _bitmask(self, netmask: int) -> int:
  162. ones = lambda x: (1 << x) - 1
  163. return ones(32) ^ ones(32 - netmask)
  164. def __repr__(self) -> str:
  165. """
  166. Conforming to python style-guide, eval(repr(obj)) equals obj
  167. """
  168. return "IPAddressBlock(%s, %i)" % (repr(IPAddress.from_int(self.ipnum)), self.netmask)
  169. def __str__(self) -> str:
  170. """
  171. Return a string in cidr-notation
  172. """
  173. return str(IPAddress.from_int(self.ipnum)) + "/" + str(self.netmask)
  174. def __contains__(self, ip: IPAddress) -> bool:
  175. return (ip.to_int() & self._bitmask(self.netmask)) == self.ipnum
  176. class ReservedIPBlocks:
  177. """
  178. To avoid magic values and save developers some research this class contains several constants
  179. describing special network-segments and some is_-methods to check if an ip is in the specified segment.
  180. """
  181. # a list of ip-addresses that can be used in private networks
  182. PRIVATE_IP_SEGMENTS = [
  183. IPAddressBlock.parse(block)
  184. for block in
  185. ("10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16")
  186. ]
  187. LOCALHOST_SEGMENT = IPAddressBlock.parse("127.0.0.0/8")
  188. MULTICAST_SEGMENT = IPAddressBlock.parse("224.0.0.0/4")
  189. RESERVED_SEGMENT = IPAddressBlock.parse("240.0.0.0/4")
  190. ZERO_CONF_SEGMENT = IPAddressBlock.parse("169.254.0.0/16")
  191. @staticmethod
  192. def is_private(ip: IPAddress) -> bool:
  193. return any(ip in block for block in ReservedIPBlocks.PRIVATE_IP_SEGMENTS)
  194. @staticmethod
  195. def get_private_segment(ip: IPAddress) -> "Optional[IPAddressBlock]":
  196. if not ReservedIPBlocks.is_private(ip):
  197. raise ValueError("%s is not part of a private IP segment" % ip)
  198. for block in ReservedIPBlocks.PRIVATE_IP_SEGMENTS:
  199. if ip in block:
  200. return block
  201. @staticmethod
  202. def is_localhost(ip: IPAddress) -> bool:
  203. return ip in ReservedIPBlocks.LOCALHOST_SEGMENT
  204. @staticmethod
  205. def is_multicast(ip: IPAddressBlock) -> bool:
  206. return ip in ReservedIPBlocks.MULTICAST_SEGMENT
  207. @staticmethod
  208. def is_reserved(ip: IPAddress) -> bool:
  209. return ip in ReservedIPBlocks.RESERVED_SEGMENT
  210. @staticmethod
  211. def is_zero_conf(ip: IPAddressBlock) -> bool:
  212. return ip in ReservedIPBlocks.ZERO_CONF_SEGMENT