def process_ipv4_csv(filename, asn_name_map): """ process csv for IPv4 input file """ ip_family = IP_FAMILY_IPV4 csv_rows = [] input_fieldnames = [MIN_IP_INT_COLUMN, MAX_IP_INT_COLUMN, ASN_STRING_COLUMN] with open(filename, 'rb') as csvfile: reader = csv.DictReader(csvfile, fieldnames=input_fieldnames) for row in reader: min_ip = IPAddress(v4_int_to_packed(int(row[MIN_IP_INT_COLUMN]))) max_ip = IPAddress(v4_int_to_packed(int(row[MAX_IP_INT_COLUMN]))) (asn_number, asn_name) = get_asn_number_name(row[ASN_STRING_COLUMN]) row[ASN_NUMBER_COLUMN] = asn_number row[ASN_NAME_COLUMN] = asn_name_map[asn_number] if asn_number in asn_name_map else asn_name row[MIN_IP_HEX_COLUMN] = hex_encode_ip(min_ip) row[MAX_IP_HEX_COLUMN] = hex_encode_ip(max_ip) row[MIN_IP_COLUMN] = str(min_ip) row[MAX_IP_COLUMN] = str(max_ip) row[IP_FAMILY_COLUMN] = ip_family del row[MIN_IP_INT_COLUMN] del row[MAX_IP_INT_COLUMN] csv_rows.append(row) # Write the new CSV file with new columns with open(OUTPUT_FILENAME, 'w') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=OUTPUT_FIELDNAMES) writer.writeheader() for row in csv_rows: writer.writerow(row)
def compose_update_message(self): """Return update message, prepare next prefix, decrease amount without checking it.""" prefix_packed = ipaddr.v4_int_to_packed(self.int_nextprefix) # print "DEBUG: prefix", self.int_nextprefix, "packed to", binascii.hexlify(prefix_packed) msg_out = self.update_message_without_prefix + prefix_packed self.int_nextprefix += 16 # Hardcoded, as open message specifies such netmask. self.updates_to_send -= 1 return msg_out
def ParsePackedIP(readable_ip): try: ip_address = ipaddr.IPv4Address(readable_ip) return str(ipaddr.v4_int_to_packed(int(ip_address))) except ValueError: pass try: ip_address = ipaddr.IPv6Address(readable_ip) return str(ipaddr.v6_int_to_packed(int(ip_address))) except ValueError: raise bigquery_client.BigqueryInvalidQueryError( 'Invalid readable ip.', None, None, None)
def __init__(self, args): """Initialize data according to command-line args.""" # Various auxiliary variables. # Hack: 4-byte AS number uses the same "int to packed" encoding as IPv4 addresses. asnumber_4bytes = ipaddr.v4_int_to_packed(args.asnumber) asnumber_2bytes = "\x5b\xa0" # AS_TRANS value, 23456 decadic. if args.asnumber < 65536: # AS number is mappable to 2 bytes asnumber_2bytes = asnumber_4bytes[2:4] # From now on, attribute docsrings are used. self.int_nextprefix = int(args.firstprefix) """Prefix IP address for next update message, as integer.""" self.updates_to_send = args.amount """Number of update messages left to be sent.""" # All information ready, so we can define messages. Mostly copied from play.py by Jozef Behran. # The following attributes are constant. self.bgp_marker = "\xFF" * 16 """Every message starts with this, see rfc4271#section-4.1""" self.keepalive_message = self.bgp_marker + ( "\x00\x13" # Size "\x04" # Type KEEPALIVE ) """KeepAlive message, see rfc4271#section-4.4""" # TODO: Notification for hold timer expiration can be handy. self.eor_message = self.bgp_marker + ( "\x00\x17" # Size "\x02" # Type (UPDATE) "\x00\x00" # Withdrawn routes length (0) "\x00\x00" # Total Path Attributes Length (0) ) """End-of-RIB marker, see rfc4724#section-2""" self.update_message_without_prefix = self.bgp_marker + ( "\x00\x30" # Size "\x02" # Type (UPDATE) "\x00\x00" # Withdrawn routes length (0) "\x00\x14" # Total Path Attributes Length (20) "\x40" # Flags ("Well-Known") "\x01" # Type (ORIGIN) "\x01" # Length (1) "\x00" # Origin: IGP "\x40" # Flags ("Well-Known") "\x02" # Type (AS_PATH) "\x06" # Length (6) "\x02" # AS segment type (AS_SEQUENCE) "\x01" # AS segment length (1) + asnumber_4bytes + # AS segment (4 bytes) "\x40" # Flags ("Well-Known") "\x03" # Type (NEXT_HOP) "\x04" # Length (4) + args.nexthop.packed + # IP address of the next hop (4 bytes) "\x1c" # IPv4 prefix length, see RFC 4271, page 20. This tool uses Network Mask: 255.255.255.240 ) """The IP address prefix (4 bytes) has to be appended to complete Update message, see rfc4271#section-4.3.""" self.open_message = self.bgp_marker + ( "\x00\x2d" # Size "\x01" # Type (OPEN) "\x04" # BGP Varsion (4) + asnumber_2bytes + # My Autonomous System # FIXME: The following hold time is hardcoded separately. Compute from initial hold_time value. "\x00\xb4" # Hold Time (180) + args.myip.packed + # BGP Identifer "\x10" # Optional parameters length "\x02" # Param type ("Capability Ad") "\x06" # Length (6 bytes) "\x01" # Capability type (NLRI Unicast), see RFC 4760, secton 8 "\x04" # Capability value length "\x00\x01" # AFI (Ipv4) "\x00" # (reserved) "\x01" # SAFI (Unicast) "\x02" # Param type ("Capability Ad") "\x06" # Length (6 bytes) "\x41" # "32 bit AS Numbers Support" (see RFC 6793, section 3) "\x04" # Capability value length + asnumber_4bytes # My AS in 32 bit format ) """Open message, see rfc4271#section-4.2"""