• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3if (!common.hasIPv6)
4  common.skip('no IPv6 support');
5
6const assert = require('assert');
7const dgram = require('dgram');
8const os = require('os');
9
10function linklocal() {
11  for (const [ifname, entries] of Object.entries(os.networkInterfaces())) {
12    for (const { address, family, scopeid } of entries) {
13      if (family === 'IPv6' && address.startsWith('fe80:')) {
14        return { address, ifname, scopeid };
15      }
16    }
17  }
18}
19const iface = linklocal();
20
21if (!iface)
22  common.skip('cannot find any IPv6 interfaces with a link local address');
23
24const address = `${iface.address}%${iface.ifname}`;
25const message = 'Hello, local world!';
26
27// Create a client socket for sending to the link-local address.
28const client = dgram.createSocket('udp6');
29
30// Create the server socket listening on the link-local address.
31const server = dgram.createSocket('udp6');
32
33server.on('listening', common.mustCall(() => {
34  const port = server.address().port;
35  client.send(message, 0, message.length, port, address);
36}));
37
38server.on('message', common.mustCall((buf, info) => {
39  const received = buf.toString();
40  assert.strictEqual(received, message);
41  // Check that the sender address is the one bound,
42  // including the link local scope identifier.
43  assert.strictEqual(
44    info.address,
45    common.isWindows ? `${iface.address}%${iface.scopeid}` : address
46  );
47  server.close();
48  client.close();
49}, 1));
50
51server.bind({ address });
52