给定一个IPv4地址(指针)和一个未排序的IPv4地址数组(干草堆),我如何通过编程确定给定干草堆中哪一个地址最接近指针(从网络角度,而不是从地理角度)?
由于我不能访问每个地址的网络掩码,因此解决方案应该忽略类似于网络掩码和traceroute的选项。
使用了各种各样的地址,我的意思是:私人、保留、广播、lan和wan。
任何理论、伪代码、python、php或perl形式的帮助都是受欢迎的。
在两个IP地址之间获取IP地址列表的问题大致相似,但它确实很简单。
我仍然不太确定你在问什么,但根据你的评论
@PeterGibson最接近我的意思是,192.168.1.101更接近192.168.56.1比172.30.130.66更接近。192.168.1.254比192.168.2.1 更接近192.168.1.240
您可以尝试以下距离函数的python代码:
import socket
def dist(a, b):
def to_num(addr):
# parse the address string into integer quads
quads = map(ord, socket.inet_aton(addr))
# spread the quads out
return reduce(lambda x,y: x * 0x10000 + y, quads)
return abs(to_num(a) - to_num(b))
返回的数字相当随意,但应该足以满足基本需求。我仍然不确定它应该如何处理广播地址等。
一些例子:
>>> 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
我首先将数组分组,考虑保留的ip地址(http://en.wikipedia.org/wiki/Reserved_IP_addresses#Reserved_IPv4_addresses)。然后我会按地区把它们分开。也许一些树结构就足够了:根节点是顶级区域,然后,靠近叶子,它适用于较小的区域。
不是最漂亮的方法,但由于不需要非常高的性能,所以它可以完成任务。
<?php
$ip = isset($argv[1]) ? $argv[1] : '192.168.1.254';
printf("Input => %s => %u'n", $ip, ip2long($ip));
var_dump(
net_distance(
$ip,
array(
'87.86.85.84',
'173.194.41.170',
'192.168.1.150',
'192.168.1.245',
'192.168.2.2',
'192.168.56.50',
)
)
);
// The problem assumes that:
// a) any local (127) and lan (10,172,192) IPs are closer to each other than to any wan IP.
// b) any wan IP is closer to local and lan IPs than to other wan IP.
function net_distance($src_ip, $list)
{
if (in_array($src_ip, $list)) {
return $src_ip; // exact match
}
list($a0, $b0, $c0, $d0) = explode('.', $src_ip, 4);
$triples = array();
$doubles = array();
$singles = array();
foreach($list as $ip) {
list($a1, $b1, $c1, $d1) = explode('.', $ip, 4);
if ($a0 == $a1 && $b0 == $b1 && $c0 == $c1) { // match A.B.C.x
$triples[] = $ip;
}
elseif ($a0 == $a1 && $b0 == $b1) { // match A.B.x.y
$doubles[] = $ip;
}
elseif ($a0 == $a1 || (in_array($a0, array(127, 10, 172, 192)) && in_array($a1, array(127, 10, 172, 192)))) {
$singles[] = $ip; // match A.x.y.z or both As are *likely* to be lan addresses
}
}
if (count($triples) > 0) {
$list = $triples;
}
elseif (count($doubles) > 0) {
$list = $doubles;
}
elseif (count($singles) > 0) {
$list = $singles;
}
$min = PHP_INT_MAX;
$rtn = false;
$l1 = ip2long($src_ip);
foreach($list as $ip)
{
$l2 = ip2long($ip);
$d = ($l1 > $l2) ? $l1 - $l2 : $l2 - $l1;
// echo "'t" . str_pad($ip, 15, ' ', STR_PAD_RIGHT) . " => $l2 => $d'n";
if ($min > $d) {
$rtn = $ip;
$min = $d;
}
}
return $rtn;
}