Skip to content

Instantly share code, notes, and snippets.

@coxley
Last active February 24, 2016 20:19
Show Gist options
  • Save coxley/9165ac5316195d4a5a29 to your computer and use it in GitHub Desktop.
Save coxley/9165ac5316195d4a5a29 to your computer and use it in GitHub Desktop.
ASPLAIN to ASDOT (AS-PLAIN AS-DOT) and reverse
def plain_to_dot(asn): # -> str
'''Take ASPLAIN and return ASDOT notation
asn: int
'''
barray = struct.pack('>I', asn)
return '%d.%d' % struct.unpack('>HH', barray)
def dot_to_plain(asn): # -> int
'''Take ASDOT and return ASPLAIN notation
asn: string - two nums separated by period (.)
'''
a1, a2 = asn.split('.')
barray = struct.pack('>HH', int(a1), int(a2))
return struct.unpack('>I', barray)[0]
def bytesize(i): # -> int
'''Return bytesize'''
return (i.bit_length() + 7) // 8
@jathanism
Copy link

Hold onto this for dropbox/nsot#148 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment