Skip to content

Commit 9154ced

Browse files
authored
chore(dot/network): create and use internal/mdns (#2794)
1 parent db64e1b commit 9154ced

File tree

9 files changed

+369
-102
lines changed

9 files changed

+369
-102
lines changed

dot/network/interfaces.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,9 @@ type Logger interface {
1818
Warnf(format string, args ...interface{})
1919
Errorf(format string, args ...interface{})
2020
}
21+
22+
// MDNS is the mDNS service interface.
23+
type MDNS interface {
24+
Start() error
25+
Stop() error
26+
}

dot/network/mdns.go

Lines changed: 0 additions & 95 deletions
This file was deleted.

dot/network/service.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package network
66
import (
77
"context"
88
"errors"
9+
"fmt"
910
"math/big"
1011
"strings"
1112
"sync"
@@ -14,6 +15,7 @@ import (
1415
"github.com/ChainSafe/gossamer/dot/peerset"
1516
"github.com/ChainSafe/gossamer/dot/telemetry"
1617
"github.com/ChainSafe/gossamer/internal/log"
18+
"github.com/ChainSafe/gossamer/internal/mdns"
1719
"github.com/ChainSafe/gossamer/internal/metrics"
1820
"github.com/ChainSafe/gossamer/lib/common"
1921
libp2pnetwork "github.com/libp2p/go-libp2p-core/network"
@@ -103,7 +105,7 @@ type Service struct {
103105

104106
cfg *Config
105107
host *host
106-
mdns *mdns
108+
mdns MDNS
107109
gossip *gossip
108110
bufPool *sync.Pool
109111
streamManager *streamManager
@@ -186,12 +188,20 @@ func NewService(cfg *Config) (*Service, error) {
186188
},
187189
}
188190

191+
serviceTag := string(host.protocolID)
192+
notifee := mdns.NewNotifeeTracker(host.p2pHost.Peerstore(), host.cm.peerSetHandler)
193+
mdnsLogger := log.NewFromGlobal(log.AddContext("module", "mdns"))
194+
mdnsLogger.Debugf(
195+
"Creating mDNS discovery service with host %s and protocol %s...",
196+
host.id(), host.protocolID)
197+
mdnsService := mdns.NewService(host.p2pHost, serviceTag, mdnsLogger, notifee)
198+
189199
network := &Service{
190200
ctx: ctx,
191201
cancel: cancel,
192202
cfg: cfg,
193203
host: host,
194-
mdns: newMDNS(host),
204+
mdns: mdnsService,
195205
gossip: newGossip(),
196206
blockState: cfg.BlockState,
197207
transactionHandler: cfg.TransactionHandler,
@@ -303,7 +313,10 @@ func (s *Service) Start() error {
303313
s.startPeerSetHandler()
304314

305315
if !s.noMDNS {
306-
s.mdns.start()
316+
err = s.mdns.Start()
317+
if err != nil {
318+
return fmt.Errorf("starting mDNS service: %w", err)
319+
}
307320
}
308321

309322
if !s.noDiscover {
@@ -443,7 +456,7 @@ func (s *Service) Stop() error {
443456
s.cancel()
444457

445458
// close mDNS discovery service
446-
err := s.mdns.close()
459+
err := s.mdns.Stop()
447460
if err != nil {
448461
logger.Errorf("Failed to close mDNS discovery service: %s", err)
449462
}

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ require (
119119
github.com/libp2p/go-openssl v0.0.7 // indirect
120120
github.com/libp2p/go-reuseport v0.2.0 // indirect
121121
github.com/libp2p/go-yamux/v3 v3.1.2 // indirect
122-
github.com/libp2p/zeroconf/v2 v2.1.1 // indirect
123122
github.com/lucas-clemente/quic-go v0.27.1 // indirect
124123
github.com/marten-seemann/qtls-go1-16 v0.1.5 // indirect
125124
github.com/marten-seemann/qtls-go1-17 v0.1.1 // indirect
@@ -170,7 +169,7 @@ require (
170169
github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce // indirect
171170
github.com/vedhavyas/go-subkey v1.0.3 // indirect
172171
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
173-
github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9 // indirect
172+
github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9
174173
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 // indirect
175174
go.opencensus.io v0.23.0 // indirect
176175
go.uber.org/atomic v1.9.0 // indirect

go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,6 @@ github.com/libp2p/go-yamux/v3 v3.0.1/go.mod h1:s2LsDhHbh+RfCsQoICSYt58U2f8ijtPAN
711711
github.com/libp2p/go-yamux/v3 v3.0.2/go.mod h1:s2LsDhHbh+RfCsQoICSYt58U2f8ijtPANFD8BmE74Bo=
712712
github.com/libp2p/go-yamux/v3 v3.1.2 h1:lNEy28MBk1HavUAlzKgShp+F6mn/ea1nDYWftZhFW9Q=
713713
github.com/libp2p/go-yamux/v3 v3.1.2/go.mod h1:jeLEQgLXqE2YqX1ilAClIfCMDY+0uXQUKmmb/qp0gT4=
714-
github.com/libp2p/zeroconf/v2 v2.1.1 h1:XAuSczA96MYkVwH+LqqqCUZb2yH3krobMJ1YE+0hG2s=
715714
github.com/libp2p/zeroconf/v2 v2.1.1/go.mod h1:fuJqLnUwZTshS3U/bMRJ3+ow/v9oid1n0DmyYyNO1Xs=
716715
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
717716
github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4=

internal/mdns/dialable.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2022 ChainSafe Systems (ON)
2+
// SPDX-License-Identifier: LGPL-3.0-only
3+
4+
package mdns
5+
6+
import (
7+
"errors"
8+
"fmt"
9+
"net"
10+
11+
manet "github.com/multiformats/go-multiaddr/net"
12+
)
13+
14+
var (
15+
ErrTCPListenAddressNotFound = errors.New("TCP listen address not found")
16+
)
17+
18+
func getMDNSIPsAndPort(network interfaceListenAddressesGetter) (ips []net.IP, port uint16) {
19+
tcpAddresses, err := getDialableListenAddrs(network)
20+
if err != nil {
21+
const defaultPort = 4001
22+
return nil, defaultPort
23+
}
24+
25+
ips = make([]net.IP, len(tcpAddresses))
26+
for i := range tcpAddresses {
27+
ips[i] = tcpAddresses[i].IP
28+
}
29+
port = uint16(tcpAddresses[0].Port)
30+
31+
return ips, port
32+
}
33+
34+
func getDialableListenAddrs(network interfaceListenAddressesGetter) (tcpAddresses []*net.TCPAddr, err error) {
35+
multiAddresses, err := network.InterfaceListenAddresses()
36+
if err != nil {
37+
return nil, fmt.Errorf("listing host interface listen addresses: %w", err)
38+
}
39+
40+
tcpAddresses = make([]*net.TCPAddr, 0, len(multiAddresses))
41+
for _, multiAddress := range multiAddresses {
42+
netAddress, err := manet.ToNetAddr(multiAddress)
43+
if err != nil {
44+
continue
45+
}
46+
47+
tcpAddress, ok := netAddress.(*net.TCPAddr)
48+
if !ok {
49+
continue
50+
}
51+
52+
tcpAddresses = append(tcpAddresses, tcpAddress)
53+
}
54+
55+
if len(tcpAddresses) == 0 {
56+
return nil, fmt.Errorf("%w: in %d multiaddresses", ErrTCPListenAddressNotFound, len(multiAddresses))
57+
}
58+
59+
return tcpAddresses, nil
60+
}

internal/mdns/interfaces.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2022 ChainSafe Systems (ON)
2+
// SPDX-License-Identifier: LGPL-3.0-only
3+
4+
package mdns
5+
6+
import (
7+
"github.com/libp2p/go-libp2p-core/network"
8+
"github.com/libp2p/go-libp2p-core/peer"
9+
"github.com/multiformats/go-multiaddr"
10+
)
11+
12+
// Logger is a logger interface for the mDNS service.
13+
type Logger interface {
14+
Debugf(format string, args ...any)
15+
Warnf(format string, args ...any)
16+
}
17+
18+
// IDNetworker can return the peer ID and a network interface.
19+
type IDNetworker interface {
20+
ID() peer.ID
21+
Networker
22+
}
23+
24+
// Networker can return a network interface.
25+
type Networker interface {
26+
Network() network.Network
27+
}
28+
29+
// interfaceListenAddressesGetter returns the listen addresses of the interfaces.
30+
type interfaceListenAddressesGetter interface {
31+
InterfaceListenAddresses() ([]multiaddr.Multiaddr, error)
32+
}

0 commit comments

Comments
 (0)