Skip to content

Commit 02ae6d6

Browse files
pekkanikanderMylesBorins
authored andcommitted
dgram: add IPv6 scope id suffix to received udp6 dgrams
Add IPv6 link local scope ID suffix to the rinfo address in those received upd6 datagrams whose source address is a link local address. Add a new test case, test-dgram-udp6-link-local-address, to verify that IPv6 UDP datagrams received from a link-local source address do contain the scope ID suffix in the rinfo address field. When a packet is received from a link-local source address, if the address does not contain the scope ID suffix, it is impossible to reply back to the sender, as the kernel is not able to determine the right network interface to send the packet through and returns with an error. Ref: #1649 PR-URL: #14500 Refs: #1649 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Stewart X Addison <[email protected]>
1 parent f4d61c7 commit 02ae6d6

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

doc/api/dgram.md

+6
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ The event handler function is passed two arguments: `msg` and `rinfo`.
9696
* `port` {number} The sender port.
9797
* `size` {number} The message size.
9898

99+
If the source address of the incoming packet is an IPv6 link local
100+
address, the interface name is added to the `address`. For
101+
example, a packet received on the `en0` interface might have the
102+
address field set to `'fe80::2618:1234:ab11:3b9c%en0'`, where `'%en0'`
103+
is the interface name as a zone id suffix.
104+
99105
### `socket.addMembership(multicastAddress[, multicastInterface])`
100106
<!-- YAML
101107
added: v0.6.9

src/tcp_wrap.cc

+14-1
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,10 @@ Local<Object> AddressToJS(Environment* env,
337337
const sockaddr* addr,
338338
Local<Object> info) {
339339
EscapableHandleScope scope(env->isolate());
340-
char ip[INET6_ADDRSTRLEN];
340+
char ip[INET6_ADDRSTRLEN + UV_IF_NAMESIZE];
341341
const sockaddr_in* a4;
342342
const sockaddr_in6* a6;
343+
343344
int port;
344345

345346
if (info.IsEmpty())
@@ -349,6 +350,18 @@ Local<Object> AddressToJS(Environment* env,
349350
case AF_INET6:
350351
a6 = reinterpret_cast<const sockaddr_in6*>(addr);
351352
uv_inet_ntop(AF_INET6, &a6->sin6_addr, ip, sizeof ip);
353+
// Add an interface identifier to a link local address.
354+
if (IN6_IS_ADDR_LINKLOCAL(&a6->sin6_addr)) {
355+
const size_t addrlen = strlen(ip);
356+
CHECK_LT(addrlen, sizeof(ip));
357+
ip[addrlen] = '%';
358+
size_t scopeidlen = sizeof(ip) - addrlen - 1;
359+
CHECK_GE(scopeidlen, UV_IF_NAMESIZE);
360+
const int r = uv_if_indextoiid(a6->sin6_scope_id,
361+
ip + addrlen + 1,
362+
&scopeidlen);
363+
CHECK_EQ(r, 0);
364+
}
352365
port = ntohs(a6->sin6_port);
353366
info->Set(env->context(),
354367
env->address_string(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
const common = require('../common');
3+
if (!common.hasIPv6)
4+
common.skip('no IPv6 support');
5+
6+
const assert = require('assert');
7+
const dgram = require('dgram');
8+
const os = require('os');
9+
10+
function 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+
}
19+
const iface = linklocal();
20+
21+
if (!iface)
22+
common.skip('cannot find any IPv6 interfaces with a link local address');
23+
24+
const address = `${iface.address}%${iface.ifname}`;
25+
const message = 'Hello, local world!';
26+
27+
// Create a client socket for sending to the link-local address.
28+
const client = dgram.createSocket('udp6');
29+
30+
// Create the server socket listening on the link-local address.
31+
const server = dgram.createSocket('udp6');
32+
33+
server.on('listening', common.mustCall(() => {
34+
const port = server.address().port;
35+
client.send(message, 0, message.length, port, address);
36+
}));
37+
38+
server.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+
51+
server.bind({ address });

0 commit comments

Comments
 (0)