|
| 1 | +""" |
| 2 | +Based on https://github.com/demskie/netparser |
| 3 | +MIT License - Copyright (c) 2019 alex |
| 4 | +""" |
| 5 | + |
| 6 | +from aikido_zen.helpers.ip_matcher import parse |
| 7 | + |
| 8 | +# Constants |
| 9 | +BEFORE = -1 |
| 10 | +EQUALS = 0 |
| 11 | +AFTER = 1 |
| 12 | + |
| 13 | + |
| 14 | +class Address: |
| 15 | + def __init__(self, address=None): |
| 16 | + self.arr = [] |
| 17 | + if address: |
| 18 | + net = parse.network(address) |
| 19 | + if net: |
| 20 | + self.arr = net["bytes"] |
| 21 | + |
| 22 | + def bytes(self): |
| 23 | + return self.arr if self.arr else [] |
| 24 | + |
| 25 | + def set_bytes(self, bytes_data): |
| 26 | + if len(bytes_data) == 4 or len(bytes_data) == 16: |
| 27 | + self.arr = bytes_data |
| 28 | + else: |
| 29 | + self.arr = [] |
| 30 | + return self |
| 31 | + |
| 32 | + def destroy(self): |
| 33 | + if self.is_valid(): |
| 34 | + self.arr = [] |
| 35 | + return self |
| 36 | + |
| 37 | + def is_valid(self): |
| 38 | + return len(self.arr) > 0 |
| 39 | + |
| 40 | + def is_ipv4(self): |
| 41 | + return len(self.arr) == 4 |
| 42 | + |
| 43 | + def is_ipv6(self): |
| 44 | + return len(self.arr) == 16 |
| 45 | + |
| 46 | + def duplicate(self): |
| 47 | + return Address().set_bytes(self.arr.copy()) |
| 48 | + |
| 49 | + def equals(self, address): |
| 50 | + return self.compare(address) == EQUALS |
| 51 | + |
| 52 | + def compare(self, address): |
| 53 | + if not self.is_valid() or not address.is_valid(): |
| 54 | + return None |
| 55 | + if self == address: |
| 56 | + return EQUALS |
| 57 | + if len(self.arr) < len(address.arr): |
| 58 | + return BEFORE |
| 59 | + if len(self.arr) > len(address.arr): |
| 60 | + return AFTER |
| 61 | + |
| 62 | + for i in range(len(self.arr)): |
| 63 | + if self.arr[i] < address.arr[i]: |
| 64 | + return BEFORE |
| 65 | + if self.arr[i] > address.arr[i]: |
| 66 | + return AFTER |
| 67 | + |
| 68 | + return EQUALS |
| 69 | + |
| 70 | + def apply_subnet_mask(self, cidr): |
| 71 | + if not self.is_valid(): |
| 72 | + return self |
| 73 | + mask_bits = len(self.arr) * 8 - cidr |
| 74 | + for i in range(len(self.arr) - 1, -1, -1): |
| 75 | + mask = max(0, min(mask_bits, 8)) |
| 76 | + if mask == 0: |
| 77 | + return self |
| 78 | + self.arr[i] &= ~((1 << mask) - 1) |
| 79 | + mask_bits -= 8 |
| 80 | + return self |
| 81 | + |
| 82 | + def is_base_address(self, cidr): |
| 83 | + if not self.is_valid() or cidr < 0 or cidr > len(self.arr) * 8: |
| 84 | + return False |
| 85 | + if cidr == len(self.arr) * 8: |
| 86 | + return True |
| 87 | + mask_bits = len(self.arr) * 8 - cidr |
| 88 | + for i in range(len(self.arr) - 1, -1, -1): |
| 89 | + mask = max(0, min(mask_bits, 8)) |
| 90 | + if mask == 0: |
| 91 | + return True |
| 92 | + if self.arr[i] != (self.arr[i] & ~((1 << mask) - 1)): |
| 93 | + return False |
| 94 | + mask_bits -= 8 |
| 95 | + return True |
| 96 | + |
| 97 | + def increase(self, cidr): |
| 98 | + if self.is_valid(): |
| 99 | + self.offset_address(cidr, True) |
| 100 | + else: |
| 101 | + self.destroy() |
| 102 | + return self |
| 103 | + |
| 104 | + def offset_address(self, cidr, forwards, throw_errors=False): |
| 105 | + target_byte = (cidr - 1) // 8 |
| 106 | + if self.is_valid() and 0 <= target_byte < len(self.arr): |
| 107 | + increment = 2 ** (8 - (cidr - target_byte * 8)) |
| 108 | + self.arr[target_byte] += increment * (1 if forwards else -1) |
| 109 | + if target_byte >= 0: |
| 110 | + if self.arr[target_byte] < 0: |
| 111 | + self.arr[target_byte] = 256 + (self.arr[target_byte] % 256) |
| 112 | + self.offset_address(target_byte * 8, forwards, throw_errors) |
| 113 | + elif self.arr[target_byte] > 255: |
| 114 | + self.arr[target_byte] %= 256 |
| 115 | + self.offset_address(target_byte * 8, forwards, throw_errors) |
| 116 | + else: |
| 117 | + self.destroy() |
| 118 | + else: |
| 119 | + self.destroy() |
0 commit comments