diff --git a/p2p/discovery/mdns/mdns.go b/p2p/discovery/mdns/mdns.go index 637fcdcb08..54e6989a7f 100644 --- a/p2p/discovery/mdns/mdns.go +++ b/p2p/discovery/mdns/mdns.go @@ -3,14 +3,14 @@ package mdns import ( "context" "errors" + "github.com/libp2p/go-libp2p/core/event" + "github.com/libp2p/go-libp2p/core/host" + "github.com/libp2p/go-libp2p/core/peer" "io" "math/rand" "strings" "sync" - "github.com/libp2p/go-libp2p/core/host" - "github.com/libp2p/go-libp2p/core/peer" - "github.com/libp2p/zeroconf/v2" logging "github.com/ipfs/go-log/v2" @@ -48,6 +48,8 @@ type mdnsService struct { server *zeroconf.Server notifee Notifee + + mu sync.Mutex } func NewMdnsService(host host.Host, serviceName string, notifee Notifee) *mdnsService { @@ -69,10 +71,48 @@ func (s *mdnsService) Start() error { return err } s.startResolver(s.ctx) + + ipEvt, err := s.host.EventBus().Subscribe(&event.EvtLocalAddressesUpdated{}) + if err != nil { + if err = s.Close(); err != nil { + log.Errorf("failed to close mdns service: %s", err) + } + return err + } + go func() { + defer func() { + if err := ipEvt.Close(); err != nil { + log.Errorf("failed to close ipEvt: %s", err) + } + }() + + for { + select { + case <-s.ctx.Done(): + return + case evt := <-ipEvt.Out(): + if _, ok := evt.(event.EvtLocalAddressesUpdated); ok { + s.mu.Lock() + if s.server != nil { + s.server.Shutdown() + s.server = nil + } + if err = s.startServer(); err != nil { + log.Errorf("failed to restart mdns server: %s", err) + } + s.mu.Unlock() + } + } + } + }() + return nil } func (s *mdnsService) Close() error { + s.mu.Lock() + defer s.mu.Unlock() + s.ctxCancel() if s.server != nil { s.server.Shutdown() @@ -143,6 +183,7 @@ func (s *mdnsService) startServer() error { txts, nil, ) + if err != nil { return err }