IPv6作为可比较的JavaScript字符串? [英] IPv6 as a comparable JavaScript string?

查看:113
本文介绍了IPv6作为可比较的JavaScript字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在IPv4时代,事情很简单,因为IPv4地址可以转换成简单的32位整数,然后用于各种比较计算。

In the age of IPv4 things were easy, because an IPv4 address could be converted into a simple 32-bit integer and then used for all kinds of comparative calculations.

对于IPv6来说,它有点尴尬,因为一方面,JavaScript本身不支持128位整数,并且它们的转换不是直截了当的所有。只留下处理IPv6字符串表示的选项。

With IPv6 it is a bit more awkward, because for one thing, 128-bit integers are not natively supported by JavaScript, and their conversion isn't straightforward at all. Which only leaves the option of dealing with a string presentation for IPv6.

如何转换IPv6任何已知格式的地址到可比较的字符串中?

How to convert an IPv6 address of any known format into a comparable string(s)?


  1. 对于任何可比较的字符串,如果地址A在地址B之前,则条件 A< B 必须在JavaScript中生成 true 。类似的逻辑必须对其余的比较有效: === < = > > =

  2. 对于每个IPv6,必须生成尽可能多的字符串必须覆盖地址内的每个范围,即每个范围的起始地址+结束地址。

  1. For any comparable string, if address A precedes address B then condition A < B must produce true in JavaScript. Similar logic must be valid for the rest of comparisons: ===, <=, > and >=.
  2. For each IPv6 there must be generated as many strings as necessary to cover every range within the address, i.e. Start Address + End Address for every range.


推荐答案

将简化的IPv6地址格式转换为完整格式并不太困难。只有3条规则允许简化地址。以下是必须撤消的顺序中列出的规则,以便将地址转换回完整格式:

Conversion of simplified IPv6 address format to the full format is not too difficult. There are only 3 rules that allows addresses to be simplified. The following are the rules listed in the order they must be undone to convert the address back to the full format:


  1. Dotted-四元表示法(嵌入在IPv6地址中的IPv4地址)

  1. Dotted-quad notation (IPv4 address embedded inside IPv6 address)

可以省略前导零

零组可以缩写为 ::

从技术上讲,根据你的处理方式,可以交换2和3。

Technically, depending on how you do your processing, 2 and 3 may be swapped.

所以这里是一个简单的转换器,只能转换有效的IPv6地址(如果有的话,它保证会失败。你输入无效的IPv6地址,因为我没有做任何验证):

So here's a simple converter that only converts valid IPv6 addresses (it's guaranteed to fail miserably if you feed it invalid IPv6 address because I'm not doing any validation):

function full_IPv6 (ip_string) {
    // replace ipv4 address if any
    var ipv4 = ip_string.match(/(.*:)([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$)/);
    if (ipv4) {
        var ip_string = ipv4[1];
        ipv4 = ipv4[2].match(/[0-9]+/g);
        for (var i = 0;i < 4;i ++) {
            var byte = parseInt(ipv4[i],10);
            ipv4[i] = ("0" + byte.toString(16)).substr(-2);
        }
        ip_string += ipv4[0] + ipv4[1] + ':' + ipv4[2] + ipv4[3];
    }

    // take care of leading and trailing ::
    ip_string = ip_string.replace(/^:|:$/g, '');

    var ipv6 = ip_string.split(':');

    for (var i = 0; i < ipv6.length; i ++) {
        var hex = ipv6[i];
        if (hex != "") {
            // normalize leading zeros
            ipv6[i] = ("0000" + hex).substr(-4);
        }
        else {
            // normalize grouped zeros ::
            hex = [];
            for (var j = ipv6.length; j <= 8; j ++) {
                hex.push('0000');
            }
            ipv6[i] = hex.join(':');
        }
    }

    return ipv6.join(':');
}

您可以在之后执行嵌入式IPv4处理.split(':')但我已经用regexp编写了它。从上面的代码可以看出,该过程的每个步骤都相当简单。绊倒我的唯一一件事就是在最后一个for循环中 j< = 8 条件中的一个一个错误。

You can probably do the embedded IPv4 processing after the .split(':') but I've already written it with regexp in mind. As can be seen from the code above, each step of the process is fairly simple. The only thing that tripped me was an off-by-one error in the j<=8 condition in the last for loop.

这篇关于IPv6作为可比较的JavaScript字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆