From bb33361ac257cb8f23777b7ce43c5c6c54094ce0 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Tue, 17 Aug 2021 12:49:33 +0200 Subject: [PATCH 1/7] move the current mdns discovery implementation to discovery_legacy --- examples/chat-with-mdns/README.md | 6 +++--- examples/chat-with-mdns/mdns.go | 2 +- examples/ipfs-camp-2019/06-Pubsub/main.go | 2 +- examples/ipfs-camp-2019/07-Messaging/main.go | 2 +- examples/ipfs-camp-2019/08-End/main.go | 2 +- examples/pubsub/chat/main.go | 2 +- p2p/{discovery => discovery_legacy}/mdns.go | 0 p2p/{discovery => discovery_legacy}/mdns_test.go | 0 8 files changed, 8 insertions(+), 8 deletions(-) rename p2p/{discovery => discovery_legacy}/mdns.go (100%) rename p2p/{discovery => discovery_legacy}/mdns_test.go (100%) diff --git a/examples/chat-with-mdns/README.md b/examples/chat-with-mdns/README.md index 3cc0623a52..05833f32e7 100644 --- a/examples/chat-with-mdns/README.md +++ b/examples/chat-with-mdns/README.md @@ -57,12 +57,12 @@ func handleStream(stream net.Stream) { 3. **Find peers nearby using mdns** -Start [mdns discovery](https://godoc.org/github.com/libp2p/go-libp2p/p2p/discovery#NewMdnsService) service in host. +Start [mdns discovery](https://godoc.org/github.com/libp2p/go-libp2p/p2p/discovery_legacy#NewMdnsService) service in host. ```go ser, err := discovery.NewMdnsService(ctx, peerhost, time.Hour, rendezvous) ``` -register [Notifee interface](https://godoc.org/github.com/libp2p/go-libp2p/p2p/discovery#Notifee) with service so that we get notified about peer discovery +register [Notifee interface](https://godoc.org/github.com/libp2p/go-libp2p/p2p/discovery_legacy#Notifee) with service so that we get notified about peer discovery ```go n := &discoveryNotifee{} @@ -97,4 +97,4 @@ Finally we open stream to the peers we found, as we find them ``` ## Authors -1. Bineesh Lazar \ No newline at end of file +1. Bineesh Lazar diff --git a/examples/chat-with-mdns/mdns.go b/examples/chat-with-mdns/mdns.go index 1471fc819f..b150695964 100644 --- a/examples/chat-with-mdns/mdns.go +++ b/examples/chat-with-mdns/mdns.go @@ -6,7 +6,7 @@ import ( "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/peer" - "github.com/libp2p/go-libp2p/p2p/discovery" + discovery "github.com/libp2p/go-libp2p/p2p/discovery_legacy" ) type discoveryNotifee struct { diff --git a/examples/ipfs-camp-2019/06-Pubsub/main.go b/examples/ipfs-camp-2019/06-Pubsub/main.go index 2522db877f..f92c255ce2 100644 --- a/examples/ipfs-camp-2019/06-Pubsub/main.go +++ b/examples/ipfs-camp-2019/06-Pubsub/main.go @@ -18,7 +18,7 @@ import ( mplex "github.com/libp2p/go-libp2p-mplex" tls "github.com/libp2p/go-libp2p-tls" yamux "github.com/libp2p/go-libp2p-yamux" - "github.com/libp2p/go-libp2p/p2p/discovery" + discovery "github.com/libp2p/go-libp2p/p2p/discovery_legacy" tcp "github.com/libp2p/go-tcp-transport" ws "github.com/libp2p/go-ws-transport" "github.com/multiformats/go-multiaddr" diff --git a/examples/ipfs-camp-2019/07-Messaging/main.go b/examples/ipfs-camp-2019/07-Messaging/main.go index 04ef4c5502..dba70a1618 100644 --- a/examples/ipfs-camp-2019/07-Messaging/main.go +++ b/examples/ipfs-camp-2019/07-Messaging/main.go @@ -17,7 +17,7 @@ import ( pubsub "github.com/libp2p/go-libp2p-pubsub" tls "github.com/libp2p/go-libp2p-tls" yamux "github.com/libp2p/go-libp2p-yamux" - "github.com/libp2p/go-libp2p/p2p/discovery" + discovery "github.com/libp2p/go-libp2p/p2p/discovery_legacy" "github.com/libp2p/go-tcp-transport" ws "github.com/libp2p/go-ws-transport" "github.com/multiformats/go-multiaddr" diff --git a/examples/ipfs-camp-2019/08-End/main.go b/examples/ipfs-camp-2019/08-End/main.go index d9384933e1..b08760d71f 100644 --- a/examples/ipfs-camp-2019/08-End/main.go +++ b/examples/ipfs-camp-2019/08-End/main.go @@ -17,7 +17,7 @@ import ( pubsub "github.com/libp2p/go-libp2p-pubsub" tls "github.com/libp2p/go-libp2p-tls" yamux "github.com/libp2p/go-libp2p-yamux" - "github.com/libp2p/go-libp2p/p2p/discovery" + discovery "github.com/libp2p/go-libp2p/p2p/discovery_legacy" "github.com/libp2p/go-tcp-transport" ws "github.com/libp2p/go-ws-transport" "github.com/multiformats/go-multiaddr" diff --git a/examples/pubsub/chat/main.go b/examples/pubsub/chat/main.go index 644d497369..3ca19be345 100644 --- a/examples/pubsub/chat/main.go +++ b/examples/pubsub/chat/main.go @@ -9,7 +9,7 @@ import ( "github.com/libp2p/go-libp2p" "github.com/libp2p/go-libp2p-core/peer" - "github.com/libp2p/go-libp2p/p2p/discovery" + discovery "github.com/libp2p/go-libp2p/p2p/discovery_legacy" "github.com/libp2p/go-libp2p-core/host" diff --git a/p2p/discovery/mdns.go b/p2p/discovery_legacy/mdns.go similarity index 100% rename from p2p/discovery/mdns.go rename to p2p/discovery_legacy/mdns.go diff --git a/p2p/discovery/mdns_test.go b/p2p/discovery_legacy/mdns_test.go similarity index 100% rename from p2p/discovery/mdns_test.go rename to p2p/discovery_legacy/mdns_test.go From 81c5b23e7cb565c25c2767cec456eba3bc27679c Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Tue, 17 Aug 2021 12:58:38 +0200 Subject: [PATCH 2/7] use libp2p/zeroconf for mDNS discovery --- examples/chat-with-mdns/README.md | 6 +- examples/go.sum | 5 + examples/ipfs-camp-2019/06-Pubsub/main.go | 10 +- examples/ipfs-camp-2019/07-Messaging/main.go | 8 +- examples/ipfs-camp-2019/08-End/main.go | 8 +- examples/ipfs-camp-2019/go.sum | 6 +- examples/pubsub/chat/go.sum | 6 +- examples/pubsub/chat/main.go | 7 +- go.mod | 1 + go.sum | 5 + p2p/discovery/mdns.go | 218 +++++++++++++++++++ p2p/discovery/mdns_test.go | 118 ++++++++++ p2p/discovery_legacy/mdns.go | 17 +- p2p/discovery_legacy/mdns_test.go | 2 +- 14 files changed, 375 insertions(+), 42 deletions(-) create mode 100644 p2p/discovery/mdns.go create mode 100644 p2p/discovery/mdns_test.go diff --git a/examples/chat-with-mdns/README.md b/examples/chat-with-mdns/README.md index 05833f32e7..5b09f604b8 100644 --- a/examples/chat-with-mdns/README.md +++ b/examples/chat-with-mdns/README.md @@ -57,12 +57,12 @@ func handleStream(stream net.Stream) { 3. **Find peers nearby using mdns** -Start [mdns discovery](https://godoc.org/github.com/libp2p/go-libp2p/p2p/discovery_legacy#NewMdnsService) service in host. +Start [mdns discovery](https://godoc.org/github.com/libp2p/go-libp2p/p2p/discovery#NewMdnsService) service in host. ```go -ser, err := discovery.NewMdnsService(ctx, peerhost, time.Hour, rendezvous) +ser, err := discovery.NewMdnsService(peerhost, rendezvous) ``` -register [Notifee interface](https://godoc.org/github.com/libp2p/go-libp2p/p2p/discovery_legacy#Notifee) with service so that we get notified about peer discovery +register [Notifee interface](https://godoc.org/github.com/libp2p/go-libp2p/p2p/discovery#Notifee) with service so that we get notified about peer discovery ```go n := &discoveryNotifee{} diff --git a/examples/go.sum b/examples/go.sum index 3eea8d2567..7c2de5c88d 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -94,7 +94,10 @@ github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtE github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs= github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s= github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ= +github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= +github.com/cenkalti/backoff/v4 v4.1.1 h1:G2HAfAmvm/GcKan2oOQpBXOd2tT2G57ZnZGWa1PxPBQ= +github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= @@ -558,6 +561,8 @@ github.com/libp2p/go-yamux v1.4.1 h1:P1Fe9vF4th5JOxxgQvfbOHkrGqIZniTLf+ddhZp8YTI github.com/libp2p/go-yamux v1.4.1/go.mod h1:fr7aVgmdNGJK+N1g+b6DW6VxzbRCjCOejR/hkmpooHE= github.com/libp2p/go-yamux/v2 v2.2.0 h1:RwtpYZ2/wVviZ5+3pjC8qdQ4TKnrak0/E01N1UWoAFU= github.com/libp2p/go-yamux/v2 v2.2.0/go.mod h1:3So6P6TV6r75R9jiBpiIKgU/66lOarCZjqROGxzPpPQ= +github.com/libp2p/zeroconf/v2 v2.0.0 h1:qYAHAqUVh4hMSfu+iDTZNqH07wLGAvb1+DW4Tx/qUoQ= +github.com/libp2p/zeroconf/v2 v2.0.0/go.mod h1:J85R/d9joD8u8F9aHM8pBXygtG9W02enEwS+wWeL6yo= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4= github.com/lucas-clemente/quic-go v0.21.2 h1:8LqqL7nBQFDUINadW0fHV/xSaCQJgmJC0Gv+qUnjd78= diff --git a/examples/ipfs-camp-2019/06-Pubsub/main.go b/examples/ipfs-camp-2019/06-Pubsub/main.go index f92c255ce2..b5296e0cf8 100644 --- a/examples/ipfs-camp-2019/06-Pubsub/main.go +++ b/examples/ipfs-camp-2019/06-Pubsub/main.go @@ -6,7 +6,6 @@ import ( "os" "os/signal" "syscall" - "time" "github.com/libp2p/go-libp2p" "github.com/libp2p/go-libp2p-core/host" @@ -18,8 +17,8 @@ import ( mplex "github.com/libp2p/go-libp2p-mplex" tls "github.com/libp2p/go-libp2p-tls" yamux "github.com/libp2p/go-libp2p-yamux" - discovery "github.com/libp2p/go-libp2p/p2p/discovery_legacy" - tcp "github.com/libp2p/go-tcp-transport" + "github.com/libp2p/go-libp2p/p2p/discovery" + "github.com/libp2p/go-tcp-transport" ws "github.com/libp2p/go-ws-transport" "github.com/multiformats/go-multiaddr" ) @@ -102,10 +101,7 @@ func main() { fmt.Println("Connected to", targetInfo.ID) } - mdns, err := discovery.NewMdnsService(ctx, host, time.Second*10, "") - if err != nil { - panic(err) - } + mdns := discovery.NewMdnsService(host, "") notifee := &discoveryNotifee{h: host, ctx: ctx} mdns.RegisterNotifee(notifee) diff --git a/examples/ipfs-camp-2019/07-Messaging/main.go b/examples/ipfs-camp-2019/07-Messaging/main.go index dba70a1618..3a720373a5 100644 --- a/examples/ipfs-camp-2019/07-Messaging/main.go +++ b/examples/ipfs-camp-2019/07-Messaging/main.go @@ -6,7 +6,6 @@ import ( "os" "os/signal" "syscall" - "time" "github.com/libp2p/go-libp2p" "github.com/libp2p/go-libp2p-core/host" @@ -17,7 +16,7 @@ import ( pubsub "github.com/libp2p/go-libp2p-pubsub" tls "github.com/libp2p/go-libp2p-tls" yamux "github.com/libp2p/go-libp2p-yamux" - discovery "github.com/libp2p/go-libp2p/p2p/discovery_legacy" + "github.com/libp2p/go-libp2p/p2p/discovery" "github.com/libp2p/go-tcp-transport" ws "github.com/libp2p/go-ws-transport" "github.com/multiformats/go-multiaddr" @@ -110,10 +109,7 @@ func main() { fmt.Println("Connected to", targetInfo.ID) - mdns, err := discovery.NewMdnsService(ctx, host, time.Second*10, "") - if err != nil { - panic(err) - } + mdns := discovery.NewMdnsService(host, "") mdns.RegisterNotifee(&mdnsNotifee{h: host, ctx: ctx}) err = dht.Bootstrap(ctx) diff --git a/examples/ipfs-camp-2019/08-End/main.go b/examples/ipfs-camp-2019/08-End/main.go index b08760d71f..dc7bbbebaa 100644 --- a/examples/ipfs-camp-2019/08-End/main.go +++ b/examples/ipfs-camp-2019/08-End/main.go @@ -6,7 +6,6 @@ import ( "os" "os/signal" "syscall" - "time" "github.com/libp2p/go-libp2p" "github.com/libp2p/go-libp2p-core/host" @@ -17,7 +16,7 @@ import ( pubsub "github.com/libp2p/go-libp2p-pubsub" tls "github.com/libp2p/go-libp2p-tls" yamux "github.com/libp2p/go-libp2p-yamux" - discovery "github.com/libp2p/go-libp2p/p2p/discovery_legacy" + "github.com/libp2p/go-libp2p/p2p/discovery" "github.com/libp2p/go-tcp-transport" ws "github.com/libp2p/go-ws-transport" "github.com/multiformats/go-multiaddr" @@ -109,10 +108,7 @@ func main() { fmt.Println("Connected to", targetInfo.ID) - mdns, err := discovery.NewMdnsService(ctx, host, time.Second*10, "") - if err != nil { - panic(err) - } + mdns := discovery.NewMdnsService(host, "") mdns.RegisterNotifee(&mdnsNotifee{h: host, ctx: ctx}) err = dht.Bootstrap(ctx) diff --git a/examples/ipfs-camp-2019/go.sum b/examples/ipfs-camp-2019/go.sum index 6772a4273d..10542b29c6 100644 --- a/examples/ipfs-camp-2019/go.sum +++ b/examples/ipfs-camp-2019/go.sum @@ -95,7 +95,10 @@ github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtE github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs= github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s= github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ= +github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= +github.com/cenkalti/backoff/v4 v4.1.1 h1:G2HAfAmvm/GcKan2oOQpBXOd2tT2G57ZnZGWa1PxPBQ= +github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= @@ -561,6 +564,8 @@ github.com/libp2p/go-yamux v1.4.1 h1:P1Fe9vF4th5JOxxgQvfbOHkrGqIZniTLf+ddhZp8YTI github.com/libp2p/go-yamux v1.4.1/go.mod h1:fr7aVgmdNGJK+N1g+b6DW6VxzbRCjCOejR/hkmpooHE= github.com/libp2p/go-yamux/v2 v2.2.0 h1:RwtpYZ2/wVviZ5+3pjC8qdQ4TKnrak0/E01N1UWoAFU= github.com/libp2p/go-yamux/v2 v2.2.0/go.mod h1:3So6P6TV6r75R9jiBpiIKgU/66lOarCZjqROGxzPpPQ= +github.com/libp2p/zeroconf/v2 v2.0.0 h1:qYAHAqUVh4hMSfu+iDTZNqH07wLGAvb1+DW4Tx/qUoQ= +github.com/libp2p/zeroconf/v2 v2.0.0/go.mod h1:J85R/d9joD8u8F9aHM8pBXygtG9W02enEwS+wWeL6yo= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4= github.com/lucas-clemente/quic-go v0.21.2 h1:8LqqL7nBQFDUINadW0fHV/xSaCQJgmJC0Gv+qUnjd78= @@ -862,7 +867,6 @@ github.com/warpfork/go-wish v0.0.0-20200122115046-b9ea61034e4a/go.mod h1:x6AKhvS github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 h1:EKhdznlJHPMoKr0XTrX+IlJs1LH3lyx2nfr1dOlZ79k= github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1/go.mod h1:8UvriyWtv5Q5EOgjHaSseUEdkQfvwFv1I/In/O2M9gc= github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM= -github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9 h1:Y1/FEOpaCpD21WxrmfeIYCFPuVPRCY2XZTWzTNHGw30= github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9/go.mod h1:j4l84WPFclQPj320J9gp0XwNKBb3U0zt5CBqjPp22G4= github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 h1:E9S12nwJwEOXe2d6gT6qxdvqMnNq+VnSsKPgm2ZZNds= github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7/go.mod h1:X2c0RVCI1eSUFI8eLcY3c0423ykwiUdxLJtkDvruhjI= diff --git a/examples/pubsub/chat/go.sum b/examples/pubsub/chat/go.sum index f06373c0e9..5a4f7ed164 100644 --- a/examples/pubsub/chat/go.sum +++ b/examples/pubsub/chat/go.sum @@ -94,7 +94,10 @@ github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtE github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs= github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s= github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ= +github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= +github.com/cenkalti/backoff/v4 v4.1.1 h1:G2HAfAmvm/GcKan2oOQpBXOd2tT2G57ZnZGWa1PxPBQ= +github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= @@ -523,6 +526,8 @@ github.com/libp2p/go-yamux v1.4.1 h1:P1Fe9vF4th5JOxxgQvfbOHkrGqIZniTLf+ddhZp8YTI github.com/libp2p/go-yamux v1.4.1/go.mod h1:fr7aVgmdNGJK+N1g+b6DW6VxzbRCjCOejR/hkmpooHE= github.com/libp2p/go-yamux/v2 v2.2.0 h1:RwtpYZ2/wVviZ5+3pjC8qdQ4TKnrak0/E01N1UWoAFU= github.com/libp2p/go-yamux/v2 v2.2.0/go.mod h1:3So6P6TV6r75R9jiBpiIKgU/66lOarCZjqROGxzPpPQ= +github.com/libp2p/zeroconf/v2 v2.0.0 h1:qYAHAqUVh4hMSfu+iDTZNqH07wLGAvb1+DW4Tx/qUoQ= +github.com/libp2p/zeroconf/v2 v2.0.0/go.mod h1:J85R/d9joD8u8F9aHM8pBXygtG9W02enEwS+wWeL6yo= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4= github.com/lucas-clemente/quic-go v0.21.2 h1:8LqqL7nBQFDUINadW0fHV/xSaCQJgmJC0Gv+qUnjd78= @@ -821,7 +826,6 @@ github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49u github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM= github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1/go.mod h1:8UvriyWtv5Q5EOgjHaSseUEdkQfvwFv1I/In/O2M9gc= github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM= -github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9 h1:Y1/FEOpaCpD21WxrmfeIYCFPuVPRCY2XZTWzTNHGw30= github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9/go.mod h1:j4l84WPFclQPj320J9gp0XwNKBb3U0zt5CBqjPp22G4= github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 h1:E9S12nwJwEOXe2d6gT6qxdvqMnNq+VnSsKPgm2ZZNds= github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7/go.mod h1:X2c0RVCI1eSUFI8eLcY3c0423ykwiUdxLJtkDvruhjI= diff --git a/examples/pubsub/chat/main.go b/examples/pubsub/chat/main.go index 3ca19be345..43c60b49a5 100644 --- a/examples/pubsub/chat/main.go +++ b/examples/pubsub/chat/main.go @@ -9,7 +9,7 @@ import ( "github.com/libp2p/go-libp2p" "github.com/libp2p/go-libp2p-core/peer" - discovery "github.com/libp2p/go-libp2p/p2p/discovery_legacy" + "github.com/libp2p/go-libp2p/p2p/discovery" "github.com/libp2p/go-libp2p-core/host" @@ -107,10 +107,7 @@ func (n *discoveryNotifee) HandlePeerFound(pi peer.AddrInfo) { // This lets us automatically discover peers on the same LAN and connect to them. func setupDiscovery(ctx context.Context, h host.Host) error { // setup mDNS discovery to find local peers - disc, err := discovery.NewMdnsService(ctx, h, DiscoveryInterval, DiscoveryServiceTag) - if err != nil { - return err - } + disc := discovery.NewMdnsService(h, DiscoveryServiceTag) n := discoveryNotifee{h: h} disc.RegisterNotifee(&n) diff --git a/go.mod b/go.mod index f1b6563a9b..8a6b8f7fa7 100644 --- a/go.mod +++ b/go.mod @@ -40,6 +40,7 @@ require ( github.com/libp2p/go-stream-muxer-multistream v0.3.0 github.com/libp2p/go-tcp-transport v0.2.7 github.com/libp2p/go-ws-transport v0.5.0 + github.com/libp2p/zeroconf/v2 v2.0.0 github.com/miekg/dns v1.1.43 // indirect github.com/multiformats/go-multiaddr v0.4.0 github.com/multiformats/go-multiaddr-dns v0.3.1 diff --git a/go.sum b/go.sum index 0b4da93fdf..5f5cec0c9b 100644 --- a/go.sum +++ b/go.sum @@ -95,7 +95,10 @@ github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtE github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs= github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s= github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ= +github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= +github.com/cenkalti/backoff/v4 v4.1.1 h1:G2HAfAmvm/GcKan2oOQpBXOd2tT2G57ZnZGWa1PxPBQ= +github.com/cenkalti/backoff/v4 v4.1.1/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= @@ -598,6 +601,8 @@ github.com/libp2p/go-yamux v1.4.1 h1:P1Fe9vF4th5JOxxgQvfbOHkrGqIZniTLf+ddhZp8YTI github.com/libp2p/go-yamux v1.4.1/go.mod h1:fr7aVgmdNGJK+N1g+b6DW6VxzbRCjCOejR/hkmpooHE= github.com/libp2p/go-yamux/v2 v2.2.0 h1:RwtpYZ2/wVviZ5+3pjC8qdQ4TKnrak0/E01N1UWoAFU= github.com/libp2p/go-yamux/v2 v2.2.0/go.mod h1:3So6P6TV6r75R9jiBpiIKgU/66lOarCZjqROGxzPpPQ= +github.com/libp2p/zeroconf/v2 v2.0.0 h1:qYAHAqUVh4hMSfu+iDTZNqH07wLGAvb1+DW4Tx/qUoQ= +github.com/libp2p/zeroconf/v2 v2.0.0/go.mod h1:J85R/d9joD8u8F9aHM8pBXygtG9W02enEwS+wWeL6yo= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4= github.com/lucas-clemente/quic-go v0.21.2 h1:8LqqL7nBQFDUINadW0fHV/xSaCQJgmJC0Gv+qUnjd78= diff --git a/p2p/discovery/mdns.go b/p2p/discovery/mdns.go new file mode 100644 index 0000000000..bfd0ec72c9 --- /dev/null +++ b/p2p/discovery/mdns.go @@ -0,0 +1,218 @@ +package discovery + +import ( + "context" + "errors" + "io" + "net" + "strings" + "sync" + + logging "github.com/ipfs/go-log/v2" + "github.com/libp2p/go-libp2p-core/host" + "github.com/libp2p/go-libp2p-core/peer" + "github.com/libp2p/zeroconf/v2" + + ma "github.com/multiformats/go-multiaddr" + manet "github.com/multiformats/go-multiaddr/net" +) + +const ( + ServiceName = "_p2p._udp" + mdnsDomain = "local" + dnsaddrPrefix = "dnsaddr=" +) + +var log = logging.Logger("mdns") + +type Service interface { + io.Closer + RegisterNotifee(Notifee) + UnregisterNotifee(Notifee) +} + +type Notifee interface { + HandlePeerFound(peer.AddrInfo) +} + +type mdnsService struct { + host host.Host + serviceName string + + // This ctx is passed to the resolver. + // It is closed when Close() is called. + ctx context.Context + ctxCancel context.CancelFunc + + resolverRunning chan struct{} + server *zeroconf.Server + + mutex sync.Mutex + notifees []Notifee +} + +func NewMdnsService(host host.Host, serviceName string) *mdnsService { + ctx, cancel := context.WithCancel(context.Background()) + if serviceName == "" { + serviceName = ServiceName + } + s := &mdnsService{ + ctx: ctx, + ctxCancel: cancel, + resolverRunning: make(chan struct{}), + host: host, + serviceName: serviceName, + } + s.startServer() + s.startResolver() + return s +} + +func (s *mdnsService) Close() error { + s.ctxCancel() + if s.server != nil { + s.server.Shutdown() + } + <-s.resolverRunning + return nil +} + +// We don't really care about the IP addresses, but the spec (and various routers / firewalls) require us +// to send A and AAAA records. +func (s *mdnsService) getIPs(addrs []ma.Multiaddr) ([]string, error) { + var ip4, ip6 string + for _, addr := range addrs { + network, hostport, err := manet.DialArgs(addr) + if err != nil { + continue + } + host, _, err := net.SplitHostPort(hostport) + if err != nil { + continue + } + if ip4 == "" && (network == "udp4" || network == "tcp4") { + ip4 = host + } else if ip6 == "" && (network == "udp6" || network == "tcp6") { + ip6 = host + } + } + ips := make([]string, 0, 2) + if ip4 != "" { + ips = append(ips, ip4) + } + if ip6 != "" { + ips = append(ips, ip6) + } + if len(ips) == 0 { + return nil, errors.New("didn't find any IP addresses") + } + return ips, nil +} + +func (s *mdnsService) mdnsInstance() string { + return string(s.host.ID()) +} + +func (s *mdnsService) startServer() error { + interfaceAddrs, err := s.host.Network().InterfaceListenAddresses() + if err != nil { + return err + } + addrs, err := peer.AddrInfoToP2pAddrs(&peer.AddrInfo{ + ID: s.host.ID(), + Addrs: interfaceAddrs, + }) + if err != nil { + return err + } + var txts []string + for _, addr := range addrs { + if manet.IsThinWaist(addr) { // don't announce circuit addresses + txts = append(txts, dnsaddrPrefix+addr.String()) + } + } + + ips, err := s.getIPs(addrs) + if err != nil { + return err + } + + server, err := zeroconf.RegisterProxy( + s.mdnsInstance(), + s.serviceName, + mdnsDomain, + 4001, + s.host.ID().Pretty(), // TODO: deals with peer IDs longer than 63 characters + ips, + txts, + nil, + ) + if err != nil { + return err + } + s.server = server + return nil +} + +func (s *mdnsService) startResolver() { + entryChan := make(chan *zeroconf.ServiceEntry, 1000) + go func() { + for entry := range entryChan { + // We only care about the TXT records. + // Ignore A, AAAA and PTR. + addrs := make([]ma.Multiaddr, 0, len(entry.Text)) // assume that all TXT records are dnsaddrs + for _, s := range entry.Text { + if !strings.HasPrefix(s, dnsaddrPrefix) { + log.Debug("missing dnsaddr prefix") + continue + } + addr, err := ma.NewMultiaddr(s[len(dnsaddrPrefix):]) + if err != nil { + log.Debugf("failed to parse multiaddr: %s", err) + continue + } + addrs = append(addrs, addr) + } + infos, err := peer.AddrInfosFromP2pAddrs(addrs...) + if err != nil { + log.Debugf("failed to get peer info: %s", err) + continue + } + s.mutex.Lock() + for _, info := range infos { + for _, notif := range s.notifees { + go notif.HandlePeerFound(info) + } + } + s.mutex.Unlock() + } + }() + go func() { + defer close(s.resolverRunning) + if err := zeroconf.Browse(s.ctx, s.serviceName, mdnsDomain, entryChan); err != nil { + log.Debugf("zeroconf browsing failed: %s", err) + } + }() +} + +func (s *mdnsService) RegisterNotifee(n Notifee) { + s.mutex.Lock() + s.notifees = append(s.notifees, n) + s.mutex.Unlock() +} + +func (s *mdnsService) UnregisterNotifee(n Notifee) { + s.mutex.Lock() + defer s.mutex.Unlock() + + found := -1 + for i, notif := range s.notifees { + if notif == n { + found = i + break + } + } + if found != -1 { + s.notifees = append(s.notifees[:found], s.notifees[found+1:]...) + } +} diff --git a/p2p/discovery/mdns_test.go b/p2p/discovery/mdns_test.go new file mode 100644 index 0000000000..7f7b2a547c --- /dev/null +++ b/p2p/discovery/mdns_test.go @@ -0,0 +1,118 @@ +package discovery + +import ( + "context" + "sync" + "testing" + "time" + + "github.com/libp2p/go-libp2p" + "github.com/libp2p/go-libp2p-core/host" + "github.com/libp2p/go-libp2p-core/peer" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func setupMDNS(t *testing.T, notifee Notifee) (host.Host, *mdnsService) { + t.Helper() + host, err := libp2p.New(context.Background(), libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0")) + require.NoError(t, err) + s := NewMdnsService(host, "") + s.RegisterNotifee(notifee) + return host, s +} + +type notif struct { + mutex sync.Mutex + infos []peer.AddrInfo +} + +var _ Notifee = ¬if{} + +func (n *notif) HandlePeerFound(info peer.AddrInfo) { + n.mutex.Lock() + n.infos = append(n.infos, info) + n.mutex.Unlock() +} + +func (n *notif) GetPeers() []peer.AddrInfo { + n.mutex.Lock() + defer n.mutex.Unlock() + infos := make([]peer.AddrInfo, 0, len(n.infos)) + infos = append(infos, n.infos...) + return infos +} + +func TestSelfDiscovery(t *testing.T) { + notif := ¬if{} + host, s := setupMDNS(t, notif) + defer s.Close() + assert.Eventuallyf( + t, + func() bool { + var found bool + for _, info := range notif.GetPeers() { + if info.ID == host.ID() { + found = true + break + } + } + return found + }, + 5*time.Second, + 5*time.Millisecond, + "expected peer to find itself", + ) +} + +func TestOtherDiscovery(t *testing.T) { + const n = 4 + + notifs := make([]*notif, n) + hostIDs := make([]peer.ID, n) + for i := 0; i < n; i++ { + notif := ¬if{} + notifs[i] = notif + var s *mdnsService + host, s := setupMDNS(t, notif) + hostIDs[i] = host.ID() + defer s.Close() + } + + containsAllHostIDs := func(ids []peer.ID) bool { + for _, id := range hostIDs { + var found bool + for _, i := range ids { + if id == i { + found = true + break + } + } + if !found { + return false + } + } + return true + } + + assert.Eventuallyf( + t, + func() bool { + for _, notif := range notifs { + infos := notif.GetPeers() + ids := make([]peer.ID, 0, len(infos)) + for _, info := range infos { + ids = append(ids, info.ID) + } + if !containsAllHostIDs(ids) { + return false + } + } + return true + }, + 25*time.Second, + 5*time.Millisecond, + "expected peers to find each other", + ) +} diff --git a/p2p/discovery_legacy/mdns.go b/p2p/discovery_legacy/mdns.go index d78ace24bf..b1d0c025d8 100644 --- a/p2p/discovery_legacy/mdns.go +++ b/p2p/discovery_legacy/mdns.go @@ -1,15 +1,15 @@ -package discovery +package discovery_legacy import ( "context" "errors" - "io" "net" "sync" "time" "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/peer" + "github.com/libp2p/go-libp2p/p2p/discovery" logging "github.com/ipfs/go-log/v2" ma "github.com/multiformats/go-multiaddr" @@ -22,19 +22,12 @@ func init() { mdns.DisableLogging = true } -var log = logging.Logger("mdns") +var log = logging.Logger("mdns_legacy") const ServiceTag = "_ipfs-discovery._udp" -type Service interface { - io.Closer - RegisterNotifee(Notifee) - UnregisterNotifee(Notifee) -} - -type Notifee interface { - HandlePeerFound(peer.AddrInfo) -} +type Service = discovery.Service +type Notifee = discovery.Notifee type mdnsService struct { server *mdns.Server diff --git a/p2p/discovery_legacy/mdns_test.go b/p2p/discovery_legacy/mdns_test.go index 8307eb0db4..cfedfb6068 100644 --- a/p2p/discovery_legacy/mdns_test.go +++ b/p2p/discovery_legacy/mdns_test.go @@ -1,4 +1,4 @@ -package discovery +package discovery_legacy import ( "context" From 6d3c30f447fc28710b62e228c5d69161b9aeb0c8 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Wed, 18 Aug 2021 10:06:34 +0200 Subject: [PATCH 3/7] rename the discovery package to mdns --- examples/chat-with-mdns/mdns.go | 2 +- examples/ipfs-camp-2019/06-Pubsub/main.go | 4 ++-- examples/ipfs-camp-2019/07-Messaging/main.go | 4 ++-- examples/ipfs-camp-2019/08-End/main.go | 4 ++-- examples/pubsub/chat/main.go | 4 ++-- p2p/{discovery => mdns}/mdns.go | 2 +- p2p/{discovery => mdns}/mdns_test.go | 2 +- p2p/{discovery_legacy => mdns_legacy}/mdns.go | 8 ++++---- p2p/{discovery_legacy => mdns_legacy}/mdns_test.go | 2 +- 9 files changed, 16 insertions(+), 16 deletions(-) rename p2p/{discovery => mdns}/mdns.go (99%) rename p2p/{discovery => mdns}/mdns_test.go (99%) rename p2p/{discovery_legacy => mdns_legacy}/mdns.go (96%) rename p2p/{discovery_legacy => mdns_legacy}/mdns_test.go (97%) diff --git a/examples/chat-with-mdns/mdns.go b/examples/chat-with-mdns/mdns.go index b150695964..f80eff337b 100644 --- a/examples/chat-with-mdns/mdns.go +++ b/examples/chat-with-mdns/mdns.go @@ -6,7 +6,7 @@ import ( "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/peer" - discovery "github.com/libp2p/go-libp2p/p2p/discovery_legacy" + discovery "github.com/libp2p/go-libp2p/p2p/mdns_legacy" ) type discoveryNotifee struct { diff --git a/examples/ipfs-camp-2019/06-Pubsub/main.go b/examples/ipfs-camp-2019/06-Pubsub/main.go index b5296e0cf8..a16fe064da 100644 --- a/examples/ipfs-camp-2019/06-Pubsub/main.go +++ b/examples/ipfs-camp-2019/06-Pubsub/main.go @@ -17,7 +17,7 @@ import ( mplex "github.com/libp2p/go-libp2p-mplex" tls "github.com/libp2p/go-libp2p-tls" yamux "github.com/libp2p/go-libp2p-yamux" - "github.com/libp2p/go-libp2p/p2p/discovery" + "github.com/libp2p/go-libp2p/p2p/mdns" "github.com/libp2p/go-tcp-transport" ws "github.com/libp2p/go-ws-transport" "github.com/multiformats/go-multiaddr" @@ -101,7 +101,7 @@ func main() { fmt.Println("Connected to", targetInfo.ID) } - mdns := discovery.NewMdnsService(host, "") + mdns := mdns.NewMdnsService(host, "") notifee := &discoveryNotifee{h: host, ctx: ctx} mdns.RegisterNotifee(notifee) diff --git a/examples/ipfs-camp-2019/07-Messaging/main.go b/examples/ipfs-camp-2019/07-Messaging/main.go index 3a720373a5..20dd2769b5 100644 --- a/examples/ipfs-camp-2019/07-Messaging/main.go +++ b/examples/ipfs-camp-2019/07-Messaging/main.go @@ -16,7 +16,7 @@ import ( pubsub "github.com/libp2p/go-libp2p-pubsub" tls "github.com/libp2p/go-libp2p-tls" yamux "github.com/libp2p/go-libp2p-yamux" - "github.com/libp2p/go-libp2p/p2p/discovery" + "github.com/libp2p/go-libp2p/p2p/mdns" "github.com/libp2p/go-tcp-transport" ws "github.com/libp2p/go-ws-transport" "github.com/multiformats/go-multiaddr" @@ -109,7 +109,7 @@ func main() { fmt.Println("Connected to", targetInfo.ID) - mdns := discovery.NewMdnsService(host, "") + mdns := mdns.NewMdnsService(host, "") mdns.RegisterNotifee(&mdnsNotifee{h: host, ctx: ctx}) err = dht.Bootstrap(ctx) diff --git a/examples/ipfs-camp-2019/08-End/main.go b/examples/ipfs-camp-2019/08-End/main.go index dc7bbbebaa..7674820639 100644 --- a/examples/ipfs-camp-2019/08-End/main.go +++ b/examples/ipfs-camp-2019/08-End/main.go @@ -16,7 +16,7 @@ import ( pubsub "github.com/libp2p/go-libp2p-pubsub" tls "github.com/libp2p/go-libp2p-tls" yamux "github.com/libp2p/go-libp2p-yamux" - "github.com/libp2p/go-libp2p/p2p/discovery" + "github.com/libp2p/go-libp2p/p2p/mdns" "github.com/libp2p/go-tcp-transport" ws "github.com/libp2p/go-ws-transport" "github.com/multiformats/go-multiaddr" @@ -108,7 +108,7 @@ func main() { fmt.Println("Connected to", targetInfo.ID) - mdns := discovery.NewMdnsService(host, "") + mdns := mdns.NewMdnsService(host, "") mdns.RegisterNotifee(&mdnsNotifee{h: host, ctx: ctx}) err = dht.Bootstrap(ctx) diff --git a/examples/pubsub/chat/main.go b/examples/pubsub/chat/main.go index 43c60b49a5..1c7bd7cf17 100644 --- a/examples/pubsub/chat/main.go +++ b/examples/pubsub/chat/main.go @@ -9,7 +9,7 @@ import ( "github.com/libp2p/go-libp2p" "github.com/libp2p/go-libp2p-core/peer" - "github.com/libp2p/go-libp2p/p2p/discovery" + "github.com/libp2p/go-libp2p/p2p/mdns" "github.com/libp2p/go-libp2p-core/host" @@ -107,7 +107,7 @@ func (n *discoveryNotifee) HandlePeerFound(pi peer.AddrInfo) { // This lets us automatically discover peers on the same LAN and connect to them. func setupDiscovery(ctx context.Context, h host.Host) error { // setup mDNS discovery to find local peers - disc := discovery.NewMdnsService(h, DiscoveryServiceTag) + disc := mdns.NewMdnsService(h, DiscoveryServiceTag) n := discoveryNotifee{h: h} disc.RegisterNotifee(&n) diff --git a/p2p/discovery/mdns.go b/p2p/mdns/mdns.go similarity index 99% rename from p2p/discovery/mdns.go rename to p2p/mdns/mdns.go index bfd0ec72c9..b3ce28cd79 100644 --- a/p2p/discovery/mdns.go +++ b/p2p/mdns/mdns.go @@ -1,4 +1,4 @@ -package discovery +package mdns import ( "context" diff --git a/p2p/discovery/mdns_test.go b/p2p/mdns/mdns_test.go similarity index 99% rename from p2p/discovery/mdns_test.go rename to p2p/mdns/mdns_test.go index 7f7b2a547c..2f9c17c220 100644 --- a/p2p/discovery/mdns_test.go +++ b/p2p/mdns/mdns_test.go @@ -1,4 +1,4 @@ -package discovery +package mdns import ( "context" diff --git a/p2p/discovery_legacy/mdns.go b/p2p/mdns_legacy/mdns.go similarity index 96% rename from p2p/discovery_legacy/mdns.go rename to p2p/mdns_legacy/mdns.go index b1d0c025d8..1a045366bb 100644 --- a/p2p/discovery_legacy/mdns.go +++ b/p2p/mdns_legacy/mdns.go @@ -1,4 +1,4 @@ -package discovery_legacy +package mdns_legacy import ( "context" @@ -9,7 +9,7 @@ import ( "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/peer" - "github.com/libp2p/go-libp2p/p2p/discovery" + mdnsnew "github.com/libp2p/go-libp2p/p2p/mdns" logging "github.com/ipfs/go-log/v2" ma "github.com/multiformats/go-multiaddr" @@ -26,8 +26,8 @@ var log = logging.Logger("mdns_legacy") const ServiceTag = "_ipfs-discovery._udp" -type Service = discovery.Service -type Notifee = discovery.Notifee +type Service = mdnsnew.Service +type Notifee = mdnsnew.Notifee type mdnsService struct { server *mdns.Server diff --git a/p2p/discovery_legacy/mdns_test.go b/p2p/mdns_legacy/mdns_test.go similarity index 97% rename from p2p/discovery_legacy/mdns_test.go rename to p2p/mdns_legacy/mdns_test.go index cfedfb6068..7c6686a25d 100644 --- a/p2p/discovery_legacy/mdns_test.go +++ b/p2p/mdns_legacy/mdns_test.go @@ -1,4 +1,4 @@ -package discovery_legacy +package mdns_legacy import ( "context" From 50250b5725fdf64ecfe041c6344b92d89e58302c Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Wed, 18 Aug 2021 10:08:35 +0200 Subject: [PATCH 4/7] don't store the context --- p2p/mdns/mdns.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/p2p/mdns/mdns.go b/p2p/mdns/mdns.go index b3ce28cd79..64721a5e3b 100644 --- a/p2p/mdns/mdns.go +++ b/p2p/mdns/mdns.go @@ -39,9 +39,7 @@ type mdnsService struct { host host.Host serviceName string - // This ctx is passed to the resolver. - // It is closed when Close() is called. - ctx context.Context + // The context is canceled when Close() is called. ctxCancel context.CancelFunc resolverRunning chan struct{} @@ -57,14 +55,13 @@ func NewMdnsService(host host.Host, serviceName string) *mdnsService { serviceName = ServiceName } s := &mdnsService{ - ctx: ctx, ctxCancel: cancel, resolverRunning: make(chan struct{}), host: host, serviceName: serviceName, } s.startServer() - s.startResolver() + s.startResolver(ctx) return s } @@ -154,7 +151,7 @@ func (s *mdnsService) startServer() error { return nil } -func (s *mdnsService) startResolver() { +func (s *mdnsService) startResolver(ctx context.Context) { entryChan := make(chan *zeroconf.ServiceEntry, 1000) go func() { for entry := range entryChan { @@ -189,7 +186,7 @@ func (s *mdnsService) startResolver() { }() go func() { defer close(s.resolverRunning) - if err := zeroconf.Browse(s.ctx, s.serviceName, mdnsDomain, entryChan); err != nil { + if err := zeroconf.Browse(ctx, s.serviceName, mdnsDomain, entryChan); err != nil { log.Debugf("zeroconf browsing failed: %s", err) } }() From 5b65f9e0129e34357750259412eb691f7965d54f Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Wed, 18 Aug 2021 10:11:41 +0200 Subject: [PATCH 5/7] use a wait group for clean shutdown of the mdns resolver --- p2p/mdns/mdns.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/p2p/mdns/mdns.go b/p2p/mdns/mdns.go index 64721a5e3b..afbc143880 100644 --- a/p2p/mdns/mdns.go +++ b/p2p/mdns/mdns.go @@ -42,8 +42,8 @@ type mdnsService struct { // The context is canceled when Close() is called. ctxCancel context.CancelFunc - resolverRunning chan struct{} - server *zeroconf.Server + resolverWG sync.WaitGroup + server *zeroconf.Server mutex sync.Mutex notifees []Notifee @@ -55,10 +55,9 @@ func NewMdnsService(host host.Host, serviceName string) *mdnsService { serviceName = ServiceName } s := &mdnsService{ - ctxCancel: cancel, - resolverRunning: make(chan struct{}), - host: host, - serviceName: serviceName, + ctxCancel: cancel, + host: host, + serviceName: serviceName, } s.startServer() s.startResolver(ctx) @@ -70,7 +69,7 @@ func (s *mdnsService) Close() error { if s.server != nil { s.server.Shutdown() } - <-s.resolverRunning + s.resolverWG.Wait() return nil } @@ -152,8 +151,10 @@ func (s *mdnsService) startServer() error { } func (s *mdnsService) startResolver(ctx context.Context) { + s.resolverWG.Add(2) entryChan := make(chan *zeroconf.ServiceEntry, 1000) go func() { + defer s.resolverWG.Done() for entry := range entryChan { // We only care about the TXT records. // Ignore A, AAAA and PTR. @@ -185,7 +186,7 @@ func (s *mdnsService) startResolver(ctx context.Context) { } }() go func() { - defer close(s.resolverRunning) + defer s.resolverWG.Done() if err := zeroconf.Browse(ctx, s.serviceName, mdnsDomain, entryChan); err != nil { log.Debugf("zeroconf browsing failed: %s", err) } From 6b1dae017775a79594f14ca4befd47f5c008231c Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Wed, 18 Aug 2021 13:39:44 +0200 Subject: [PATCH 6/7] add comment about port numbers --- p2p/mdns/mdns.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/mdns/mdns.go b/p2p/mdns/mdns.go index afbc143880..e4e68c115c 100644 --- a/p2p/mdns/mdns.go +++ b/p2p/mdns/mdns.go @@ -137,7 +137,7 @@ func (s *mdnsService) startServer() error { s.mdnsInstance(), s.serviceName, mdnsDomain, - 4001, + 4001, // we have to pass in a port number here, but libp2p only uses the TXT records s.host.ID().Pretty(), // TODO: deals with peer IDs longer than 63 characters ips, txts, From bc695b3d94cc97739db121741400fc300ab68123 Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Wed, 18 Aug 2021 17:27:53 +0200 Subject: [PATCH 7/7] move the mdns packages p2p/discovery --- examples/chat-with-mdns/mdns.go | 4 ++-- examples/ipfs-camp-2019/06-Pubsub/main.go | 2 +- examples/ipfs-camp-2019/07-Messaging/main.go | 2 +- examples/ipfs-camp-2019/08-End/main.go | 2 +- examples/pubsub/chat/main.go | 6 ++---- p2p/{ => discovery}/mdns/mdns.go | 0 p2p/{ => discovery}/mdns/mdns_test.go | 0 p2p/{ => discovery}/mdns_legacy/mdns.go | 2 +- p2p/{ => discovery}/mdns_legacy/mdns_test.go | 0 9 files changed, 8 insertions(+), 10 deletions(-) rename p2p/{ => discovery}/mdns/mdns.go (100%) rename p2p/{ => discovery}/mdns/mdns_test.go (100%) rename p2p/{ => discovery}/mdns_legacy/mdns.go (98%) rename p2p/{ => discovery}/mdns_legacy/mdns_test.go (100%) diff --git a/examples/chat-with-mdns/mdns.go b/examples/chat-with-mdns/mdns.go index f80eff337b..178f7c2042 100644 --- a/examples/chat-with-mdns/mdns.go +++ b/examples/chat-with-mdns/mdns.go @@ -6,7 +6,7 @@ import ( "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/peer" - discovery "github.com/libp2p/go-libp2p/p2p/mdns_legacy" + mdns "github.com/libp2p/go-libp2p/p2p/discovery/mdns_legacy" ) type discoveryNotifee struct { @@ -21,7 +21,7 @@ func (n *discoveryNotifee) HandlePeerFound(pi peer.AddrInfo) { //Initialize the MDNS service func initMDNS(ctx context.Context, peerhost host.Host, rendezvous string) chan peer.AddrInfo { // An hour might be a long long period in practical applications. But this is fine for us - ser, err := discovery.NewMdnsService(ctx, peerhost, time.Hour, rendezvous) + ser, err := mdns.NewMdnsService(ctx, peerhost, time.Hour, rendezvous) if err != nil { panic(err) } diff --git a/examples/ipfs-camp-2019/06-Pubsub/main.go b/examples/ipfs-camp-2019/06-Pubsub/main.go index a16fe064da..95b83b99f8 100644 --- a/examples/ipfs-camp-2019/06-Pubsub/main.go +++ b/examples/ipfs-camp-2019/06-Pubsub/main.go @@ -17,7 +17,7 @@ import ( mplex "github.com/libp2p/go-libp2p-mplex" tls "github.com/libp2p/go-libp2p-tls" yamux "github.com/libp2p/go-libp2p-yamux" - "github.com/libp2p/go-libp2p/p2p/mdns" + "github.com/libp2p/go-libp2p/p2p/discovery/mdns" "github.com/libp2p/go-tcp-transport" ws "github.com/libp2p/go-ws-transport" "github.com/multiformats/go-multiaddr" diff --git a/examples/ipfs-camp-2019/07-Messaging/main.go b/examples/ipfs-camp-2019/07-Messaging/main.go index 20dd2769b5..afa4a70b84 100644 --- a/examples/ipfs-camp-2019/07-Messaging/main.go +++ b/examples/ipfs-camp-2019/07-Messaging/main.go @@ -16,7 +16,7 @@ import ( pubsub "github.com/libp2p/go-libp2p-pubsub" tls "github.com/libp2p/go-libp2p-tls" yamux "github.com/libp2p/go-libp2p-yamux" - "github.com/libp2p/go-libp2p/p2p/mdns" + "github.com/libp2p/go-libp2p/p2p/discovery/mdns" "github.com/libp2p/go-tcp-transport" ws "github.com/libp2p/go-ws-transport" "github.com/multiformats/go-multiaddr" diff --git a/examples/ipfs-camp-2019/08-End/main.go b/examples/ipfs-camp-2019/08-End/main.go index 7674820639..de786d1b35 100644 --- a/examples/ipfs-camp-2019/08-End/main.go +++ b/examples/ipfs-camp-2019/08-End/main.go @@ -16,7 +16,7 @@ import ( pubsub "github.com/libp2p/go-libp2p-pubsub" tls "github.com/libp2p/go-libp2p-tls" yamux "github.com/libp2p/go-libp2p-yamux" - "github.com/libp2p/go-libp2p/p2p/mdns" + "github.com/libp2p/go-libp2p/p2p/discovery/mdns" "github.com/libp2p/go-tcp-transport" ws "github.com/libp2p/go-ws-transport" "github.com/multiformats/go-multiaddr" diff --git a/examples/pubsub/chat/main.go b/examples/pubsub/chat/main.go index 1c7bd7cf17..5e3cd53dd7 100644 --- a/examples/pubsub/chat/main.go +++ b/examples/pubsub/chat/main.go @@ -8,12 +8,10 @@ import ( "time" "github.com/libp2p/go-libp2p" - "github.com/libp2p/go-libp2p-core/peer" - "github.com/libp2p/go-libp2p/p2p/mdns" - "github.com/libp2p/go-libp2p-core/host" - + "github.com/libp2p/go-libp2p-core/peer" pubsub "github.com/libp2p/go-libp2p-pubsub" + "github.com/libp2p/go-libp2p/p2p/discovery/mdns" ) // DiscoveryInterval is how often we re-publish our mDNS records. diff --git a/p2p/mdns/mdns.go b/p2p/discovery/mdns/mdns.go similarity index 100% rename from p2p/mdns/mdns.go rename to p2p/discovery/mdns/mdns.go diff --git a/p2p/mdns/mdns_test.go b/p2p/discovery/mdns/mdns_test.go similarity index 100% rename from p2p/mdns/mdns_test.go rename to p2p/discovery/mdns/mdns_test.go diff --git a/p2p/mdns_legacy/mdns.go b/p2p/discovery/mdns_legacy/mdns.go similarity index 98% rename from p2p/mdns_legacy/mdns.go rename to p2p/discovery/mdns_legacy/mdns.go index 1a045366bb..7472aea4af 100644 --- a/p2p/mdns_legacy/mdns.go +++ b/p2p/discovery/mdns_legacy/mdns.go @@ -9,7 +9,7 @@ import ( "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/peer" - mdnsnew "github.com/libp2p/go-libp2p/p2p/mdns" + mdnsnew "github.com/libp2p/go-libp2p/p2p/discovery/mdns" logging "github.com/ipfs/go-log/v2" ma "github.com/multiformats/go-multiaddr" diff --git a/p2p/mdns_legacy/mdns_test.go b/p2p/discovery/mdns_legacy/mdns_test.go similarity index 100% rename from p2p/mdns_legacy/mdns_test.go rename to p2p/discovery/mdns_legacy/mdns_test.go