浏览代码

Add possibility to retrieve first and last address of IP block rather conveniently

dustin.born 7 年之前
父节点
当前提交
3fb4ad9b8b
共有 1 个文件被更改,包括 9 次插入2 次删除
  1. 9 2
      code/ID2TLib/IPv4.py

+ 9 - 2
code/ID2TLib/IPv4.py

@@ -96,7 +96,8 @@ class IPAddressBlock:
 		
 		self.ipnum = ip.to_int() & self._bitmask(netmask)
 		self.netmask = netmask
-	
+		self.last_ipnum = self.ipnum + self.block_size() - 1
+
 	@staticmethod
 	def parse(cidr: str):
 		match = re.match("^" + IPAddressBlock.CIDR_REGEXP + "$", cidr)
@@ -109,8 +110,14 @@ class IPAddressBlock:
 		return IPAddressBlock(ip, suffix)
 	
 	def block_size(self):
-		return 2 ** (32 - netmask)
+		return 2 ** (32 - self.netmask)
 	
+	def first_address(self):
+		return IPAddress.from_int(self.ipnum)
+
+	def last_address(self):
+		return IPAddress.from_int(self.last_ipnum)
+
 	def _bitmask(self, netmask):
 		ones = lambda x: (1 << x) - 1