php - Find the network distance between two IPv4 addresses (not geographic distance) -
given ipv4 address (needle) , unsorted array of ipv4 addresses (haystack), how programmatically determine single address in given haystack closest (network-wise, not geographically) needle?
since don't have access netmask of every address, solution should ignore netmasks , traceroute alike options.
all sorts of addresses used, mean: private, reserved, broadcast, lan , wan.
any in form of theory, pseudo-code, python, php or perl welcome.
the question getting ip address list between 2 ip addresses similar, quite cut it.
i'm still not quite sure you're asking, based on comment
@petergibson closest meant, 192.168.1.101 closer 192.168.56.1 172.30.130.66 . , 192.168.1.254 closer 192.168.1.240 192.168.2.1
you try following python code distance function:
import socket def dist(a, b): def to_num(addr): # parse address string integer quads quads = map(ord, socket.inet_aton(addr)) # spread quads out return reduce(lambda x,y: x * 0x10000 + y, quads) return abs(to_num(a) - to_num(b))
the numbers returned arbitrary, should suffice basic needs. i'm still not sure should broadcast addresses etc though.
some examples:
>>> dist('192.168.1.254', '192.168.1.240') 14l >>> dist('192.168.1.254', '192.168.2.1') 65283l >>> dist('192.168.1.101', '192.168.56.1') 3604380l >>> dist('192.168.1.101', '172.30.130.66') 5630092231245859l
Comments
Post a Comment