Skip to content

Commit

Permalink
Fix mdns when listening with ipv6 wildcard (which includes ipv4)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianopolous committed May 20, 2024
1 parent 9895401 commit 0c6b2d5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion libp2p/src/main/kotlin/io/libp2p/discovery/MDnsDiscovery.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ class MDnsDiscovery(
val address = host.listenAddresses().find {
it.has(Protocol.IP4)
}
val str = address?.getFirstComponent(Protocol.TCP)?.stringValue!!
val ipv6OnlyAddress = if (address == null)
host.listenAddresses().find {
it.has(Protocol.IP6)
} else address
val str = ipv6OnlyAddress?.getFirstComponent(Protocol.TCP)?.stringValue!!
return Integer.parseInt(str)
}

Expand Down
21 changes: 21 additions & 0 deletions libp2p/src/test/kotlin/io/libp2p/discovery/MDnsDiscoveryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ class MDnsDiscoveryTest {
}
}

val hostIpv6 = object : NullHost() {
override val peerId: PeerId = PeerId.fromPubKey(
generateEcdsaKeyPair().second
)

override fun listenAddresses(): List<Multiaddr> {
return listOf(
Multiaddr("/ip6/::/tcp/4001")
)
}
}

val otherHost = object : NullHost() {
override val peerId: PeerId = PeerId.fromPubKey(
generateEcdsaKeyPair().second
Expand All @@ -47,6 +59,15 @@ class MDnsDiscoveryTest {
discoverer.stop().get(1, TimeUnit.SECONDS)
}

@Test
fun `start and stop discovery ipv6`() {
val discoverer = MDnsDiscovery(hostIpv6, testServiceTag)

discoverer.start().get(1, TimeUnit.SECONDS)
TimeUnit.MILLISECONDS.sleep(100)
discoverer.stop().get(1, TimeUnit.SECONDS)
}

@Test
fun `start discovery and listen for self`() {
var peerInfo: PeerInfo? = null
Expand Down

0 comments on commit 0c6b2d5

Please sign in to comment.