【问题标题】:How do I bind local ipv6 address to my node.js program?如何将本地 ipv6 地址绑定到我的 node.js 程序?
【发布时间】:2015-07-20 13:42:07
【问题描述】:

我刚刚在nodejs中创建了这个简单的程序,但是我无法将它绑定到我的网卡的ipv6地址。

我在 API 文档中阅读了以下内容

localAddress:为网络连接绑定的本地接口。

var http = require('http');

var options = {
  hostname: 'www.whatismyipv6.com',
  localAddress: '2a01:xxxx:xxxx:xxxx::2' //a real ipv6 address here
};

var req = http.request(options, function(res) {
  res.on('data', function (chunk) {
    console.log(chunk.toString());
  });
});

req.on('error', function(e) {
  console.log('ERROR: ' + e.message);
});

req.end();

但是当我执行程序时,我得到了这个。记下 ipv4 地址。

<head>
<title>WhatIsMyIPv6? (IPv4: xx.xx.xxx.xxx)</title>
<meta name="bitly-verification" content="984886d337a6"/>
</head>

看起来 nodejs 忽略了 localAddress 并直接绑定到 ipv4 地址。

# node --version
v0.8.0

【问题讨论】:

    标签: http node.js request ipv6


    【解决方案1】:

    可以强制节点使用 ipv6 连接,至少它适用于节点 v0.12.7。您的选项将如下所示:

    var options = {
      hostname: 'www.whatismyipv6.com',
      localAddress: '2a01:xxxx:xxxx:xxxx::2', //a real ipv6 address here
      family: 6
    };
    

    【讨论】:

      【解决方案2】:

      很遗憾,您在这台以及其他任何同时发布了 A 和 AAAA RR 的主机上都不走运。

      http.js 不会将 addressType (地址族)传递到堆栈中,因此在调用的底部是 lookup() dns.js 中的strong> func 只是发出一个普通的getaddrinfo() 调用并获取第一个返回的结果,在此示例中为目标主机的 IPv4 地址的 A RR .

      如果您检查 dns.js,您可以看到 lookup() 只是在未指定家庭时从结果中弹出地址 [0]:

      function onanswer(addresses) {
          if (addresses) {
            if (family) {
              callback(null, addresses[0], family);
            } else {
              callback(null, addresses[0], addresses[0].indexOf(':') >= 0 ? 6 : 4);
            }
          } else {
            callback(errnoException(process._errno, 'getaddrinfo'));
          }
        }
      

      如您所见,它确实努力为该结果设置族;在您的示例中,第一个结果被标识为 IPv4 系列,并且随着此渗透备份堆栈绑定相应完成,覆盖您的 localAddress 规范。

      您可以通过将目标主机更改为 ipv6.whatismyipv6.com 来看到这一点;神奇的是,您的 localhost 的 IPv6 地址现在已按您的意愿绑定。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-02-20
        • 1970-01-01
        • 1970-01-01
        • 2011-01-28
        • 1970-01-01
        • 2012-06-15
        • 1970-01-01
        相关资源
        最近更新 更多