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