Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix : Announcing static addresses for mDNS #3096

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions p2p/discovery/mdns/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
Expand Down Expand Up @@ -143,6 +183,7 @@ func (s *mdnsService) startServer() error {
txts,
nil,
)

if err != nil {
return err
}
Expand Down