Skip to content

Commit

Permalink
fix: ignore IPv6 link-local addresses (#2865)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
achingbrain authored Nov 29, 2024
1 parent 406b391 commit f8da60e
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/transport-websockets/src/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import net from 'node:net'
import os from 'node:os'
import { TypedEventEmitter, setMaxListeners } from '@libp2p/interface'
import { ipPortToMultiaddr as toMultiaddr } from '@libp2p/utils/ip-port-to-multiaddr'
import { isLinkLocalIp } from '@libp2p/utils/link-local-ip'
import { multiaddr } from '@multiformats/multiaddr'
import { WebSockets, WebSocketsSecure } from '@multiformats/multiaddr-matcher'
import duplex from 'it-ws/duplex'
Expand Down Expand Up @@ -380,11 +381,17 @@ export class WebSocketListener extends TypedEventEmitter<ListenerEvents> impleme
return
}

niInfos.forEach(ni => {
if (ni.family === 'IPv6') {
multiaddrs.push(multiaddr(`/ip${options.family}/${ni.address}/${options.transport}/${address.port}`))
for (const ni of niInfos) {
if (ni.family !== 'IPv6') {
continue
}
})

if (isLinkLocalIp(ni.address)) {
continue
}

multiaddrs.push(multiaddr(`/ip${options.family}/${ni.address}/${options.transport}/${address.port}`))
}
})
} else {
multiaddrs.push(multiaddr(`/ip${options.family}/${options.host}/${options.transport}/${address.port}`))
Expand Down

0 comments on commit f8da60e

Please sign in to comment.