def __init__(self, classless_addresses): """ Accepts a sequence of CIDR entries and routers to produce classless static route byte-blocks. You'll probably want to put (("0.0.0.0", 0), packet.getOption("router")) at the start to ensure the default gateway is set as expected. :param classless_addresses: ((network IPv4, CIDR-mask), router IPv4) elements, like (("169.254.0.0", 16), "0.0.0.0"). :except ValueError: An illegal address or mask was encountered. """ self._value = [] for ((ip, mask), router) in classless_addresses: ip = IPv4(ip) router = IPv4(router) if not 0 <= mask <= 32: raise ValueError("CIDR mask %(mask)i is not between 0 and 32" % { 'mask': mask, }) width = mask / 8 if mask % 8: width += 1 self._value.append(mask) if width: self._value.extend(tuple(ip)[:width]) self._value.extend(router)
def __init__(self, data): """ Formats the given data into multiple IPv4 addresses or RFC1035-formatted strings. :param str data: The comma-delimited IPv4s or FQDNs to process. :except ValueError: Both IPv4s and FQDNs were provided. """ ip_4_mode = False dns_mode = False self._value = [] for token in (tok for tok in (t.strip() for t in data.split(',')) if tok): try: self._value.extend(IPv4(token)) ip_4_mode = True except ValueError: self._value += _rfc1035Parse(token) dns_mode = True if ip_4_mode == dns_mode: raise ValueError("'%(data)s contains both IPv4 and DNS-based entries" % { 'data': data, }) self._value.insert(0, int(ip_4_mode))
def __init__(self, mandatory, data): """ Formats native IPv4s as encoded IPv4 addresses. :param bool mandatory: True if the IPv4 addresses have to be respected. :param str data: The comma-delimited IPv4s to process. """ self._value = [int(mandatory)] for token in (tok for tok in (t.strip() for t in data.split(',')) if tok): self._value.extend(IPv4(token))
def extractIPOrNone(self, option): """ Provides the IP associated with a DHCP field or option. :param option: The numeric ID or name of the option to check. :return: The associated address or None, if undefined. """ addr = self.getOption(option) if not addr or not any(addr): return None return IPv4(addr)
def __init__(self, values): """ Formats the given data into multiple IPv4 addresses associated with sub-option codes. :param sequence values: A sequence of (code:int, IPv4s:string) elements. """ self._value = [] for (code, addresses) in values: self._value.append(code) for token in [tok for tok in [address.strip() for address in addresses.split(',')] if tok]: self._value.extend(IPv4(token))
def __init__(self, isns_functions, dd_access, admin_flags, isns_security, ips): """ Sets iSNS configuration parameters. :param int isns_functions: A sixteen-bit value. :param int dd_access: A sixteen-bit value. :param int admin_flags: A sixteen-bit value. :param int isns_security: A thirty-two-bit value. :param str ips: Comma-delimited IPv4s to be processed. """ isns_functions = intToList(isns_functions) dd_access = intToList(dd_access) admin_flags = intToList(admin_flags) isns_security = longToList(isns_security) self._value = isns_functions + dd_access + admin_flags + isns_security for token in [tok for tok in [t.strip() for t in ips.split(',')] if tok]: self._value.extend(IPv4(token))
while True: pkt, addr = s.recvfrom(65565) pcap.write(pkt) eth = Ethernet(pkt) t = time.localtime() print("%d's Packet." % count_pkt) print('Time: {}.{}.{}, {}.{}.{}'.format(t.tm_mon, t.tm_mday, t.tm_year, t.tm_hour, t.tm_min, t.tm_sec)) print('Ethernet Frame:') print(TAB_1 + 'Destination: {}, Source: {}, Protocol: {}'.format( eth.dest_mac, eth.src_mac, eth.proto)) # IPv4 if eth.proto == 8: ipv4 = IPv4(eth.data) print(TAB_1 + 'IPv4 Packet:') print(TAB_2 + 'Version: {}, Header Length: {}, TTL: {},'.format( ipv4.version, ipv4.header_length, ipv4.ttl)) print(TAB_2 + 'Protocol: {}, Source: {}, Target: {}'.format( ipv4.proto, ipv4.src, ipv4.target)) # TCP if ipv4.proto == 6: tcp = TCP(ipv4.data) print(TAB_1 + 'TCP Segment:') print(TAB_2 + 'Source Port: {}, Destination Port: {}'.format( tcp.src_port, tcp.dest_port)) print(TAB_2 + 'Sequence: {}, Acknowledgment: {}'.format( tcp.sequence, tcp.acknowledgment)) print(TAB_2 + 'Flags:')
def setUp(self): self.examples = [ IPv4('192.168.192.10', '255.255.255.248'), IPv4('10.16.3.65', '255.255.254.0') ]