0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-dgram-udp6-link-local-address.js
Antoine du Hamel 70b516e4db dns: accept 'IPv4' and 'IPv6' for family
Refs: https://github.com/nodejs/node/issues/43014

PR-URL: https://github.com/nodejs/node/pull/43054
Fixes: https://github.com/nodejs/node/issues/43014
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Beth Griggs <bgriggs@redhat.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
2022-06-15 18:02:01 -04:00

54 lines
1.5 KiB
JavaScript

'use strict';
const common = require('../common');
if (!common.hasIPv6)
common.skip('no IPv6 support');
const assert = require('assert');
const dgram = require('dgram');
const os = require('os');
const { isWindows } = common;
function linklocal() {
for (const [ifname, entries] of Object.entries(os.networkInterfaces())) {
for (const { address, family, scopeid } of entries) {
if (family === 'IPv6' && address.startsWith('fe80:')) {
return { address, ifname, scopeid };
}
}
}
}
const iface = linklocal();
if (!iface)
common.skip('cannot find any IPv6 interfaces with a link local address');
const address = isWindows ? iface.address : `${iface.address}%${iface.ifname}`;
const message = 'Hello, local world!';
// Create a client socket for sending to the link-local address.
const client = dgram.createSocket('udp6');
// Create the server socket listening on the link-local address.
const server = dgram.createSocket('udp6');
server.on('listening', common.mustCall(() => {
const port = server.address().port;
client.send(message, 0, message.length, port, address);
}));
server.on('message', common.mustCall((buf, info) => {
const received = buf.toString();
assert.strictEqual(received, message);
// Check that the sender address is the one bound,
// including the link local scope identifier.
assert.strictEqual(
info.address,
isWindows ? `${iface.address}%${iface.scopeid}` : address
);
server.close();
client.close();
}, 1));
server.bind({ address });