Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test/dgram: Add support for IPv6 link local scope IDs in received UDP datagrams #14500

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/api/dgram.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ The event handler function is passed two arguments: `msg` and `rinfo`.
* `port` {number} The sender port.
* `size` {number} The message size.

If the source address of the incoming packet is an IPv6 link local
Trott marked this conversation as resolved.
Show resolved Hide resolved
address, the interface name is added to the `address`. For
example, a packet received on the `en0` interface might have the
address field set to `'fe80::2618:1234:ab11:3b9c%en0'`, where `'%en0'`
is the interface name as a zone id suffix.

### `socket.addMembership(multicastAddress[, multicastInterface])`
<!-- YAML
added: v0.6.9
Expand Down
15 changes: 14 additions & 1 deletion src/tcp_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,10 @@ Local<Object> AddressToJS(Environment* env,
const sockaddr* addr,
Local<Object> info) {
EscapableHandleScope scope(env->isolate());
char ip[INET6_ADDRSTRLEN];
char ip[INET6_ADDRSTRLEN + UV_IF_NAMESIZE];
const sockaddr_in* a4;
const sockaddr_in6* a6;

int port;

if (info.IsEmpty())
Expand All @@ -349,6 +350,18 @@ Local<Object> AddressToJS(Environment* env,
case AF_INET6:
a6 = reinterpret_cast<const sockaddr_in6*>(addr);
uv_inet_ntop(AF_INET6, &a6->sin6_addr, ip, sizeof ip);
// Add an interface identifier to a link local address.
if (IN6_IS_ADDR_LINKLOCAL(&a6->sin6_addr)) {
const size_t addrlen = strlen(ip);
CHECK_LT(addrlen, sizeof(ip));
ip[addrlen] = '%';
size_t scopeidlen = sizeof(ip) - addrlen - 1;
CHECK_GE(scopeidlen, UV_IF_NAMESIZE);
const int r = uv_if_indextoiid(a6->sin6_scope_id,
ip + addrlen + 1,
&scopeidlen);
CHECK_EQ(r, 0);
}
port = ntohs(a6->sin6_port);
info->Set(env->context(),
env->address_string(),
Expand Down
51 changes: 51 additions & 0 deletions test/parallel/test-dgram-udp6-link-local-address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'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');

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 = `${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,
common.isWindows ? `${iface.address}%${iface.scopeid}` : address
);
server.close();
client.close();
}, 1));

server.bind({ address });