Skip to content

Commit f8da60e

Browse files
authored
fix: ignore IPv6 link-local addresses (#2865)
Network interfaces generate their own link-local addresses when bootstrapping to the network. With IPv4 these are discarded when a DHCP server assigns an address but IPv6 interfaces keep them around. These aren't useful addresses after a routable address has been assigned so do not return them as addresses we are listening on.
1 parent 406b391 commit f8da60e

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

packages/transport-websockets/src/listener.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import net from 'node:net'
44
import os from 'node:os'
55
import { TypedEventEmitter, setMaxListeners } from '@libp2p/interface'
66
import { ipPortToMultiaddr as toMultiaddr } from '@libp2p/utils/ip-port-to-multiaddr'
7+
import { isLinkLocalIp } from '@libp2p/utils/link-local-ip'
78
import { multiaddr } from '@multiformats/multiaddr'
89
import { WebSockets, WebSocketsSecure } from '@multiformats/multiaddr-matcher'
910
import duplex from 'it-ws/duplex'
@@ -380,11 +381,17 @@ export class WebSocketListener extends TypedEventEmitter<ListenerEvents> impleme
380381
return
381382
}
382383

383-
niInfos.forEach(ni => {
384-
if (ni.family === 'IPv6') {
385-
multiaddrs.push(multiaddr(`/ip${options.family}/${ni.address}/${options.transport}/${address.port}`))
384+
for (const ni of niInfos) {
385+
if (ni.family !== 'IPv6') {
386+
continue
386387
}
387-
})
388+
389+
if (isLinkLocalIp(ni.address)) {
390+
continue
391+
}
392+
393+
multiaddrs.push(multiaddr(`/ip${options.family}/${ni.address}/${options.transport}/${address.port}`))
394+
}
388395
})
389396
} else {
390397
multiaddrs.push(multiaddr(`/ip${options.family}/${options.host}/${options.transport}/${address.port}`))

0 commit comments

Comments
 (0)