Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions dot/network/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ type Logger interface {
Warnf(format string, args ...interface{})
Errorf(format string, args ...interface{})
}

// MDNS is the mDNS service interface.
type MDNS interface {
Start() error
Stop() error
}
95 changes: 0 additions & 95 deletions dot/network/mdns.go

This file was deleted.

21 changes: 17 additions & 4 deletions dot/network/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package network
import (
"context"
"errors"
"fmt"
"math/big"
"strings"
"sync"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/ChainSafe/gossamer/dot/peerset"
"github.com/ChainSafe/gossamer/dot/telemetry"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/internal/mdns"
"github.com/ChainSafe/gossamer/internal/metrics"
"github.com/ChainSafe/gossamer/lib/common"
libp2pnetwork "github.com/libp2p/go-libp2p-core/network"
Expand Down Expand Up @@ -103,7 +105,7 @@ type Service struct {

cfg *Config
host *host
mdns *mdns
mdns MDNS
gossip *gossip
bufPool *sync.Pool
streamManager *streamManager
Expand Down Expand Up @@ -186,12 +188,20 @@ func NewService(cfg *Config) (*Service, error) {
},
}

serviceTag := string(host.protocolID)
notifee := mdns.NewNotifeeTracker(host.p2pHost.Peerstore(), host.cm.peerSetHandler)
mdnsLogger := log.NewFromGlobal(log.AddContext("module", "mdns"))
mdnsLogger.Debugf(
"Creating mDNS discovery service with host %s and protocol %s...",
host.id(), host.protocolID)
mdnsService := mdns.NewService(host.p2pHost, serviceTag, mdnsLogger, notifee)

network := &Service{
ctx: ctx,
cancel: cancel,
cfg: cfg,
host: host,
mdns: newMDNS(host),
mdns: mdnsService,
gossip: newGossip(),
blockState: cfg.BlockState,
transactionHandler: cfg.TransactionHandler,
Expand Down Expand Up @@ -303,7 +313,10 @@ func (s *Service) Start() error {
s.startPeerSetHandler()

if !s.noMDNS {
s.mdns.start()
err = s.mdns.Start()
if err != nil {
return fmt.Errorf("starting mDNS service: %w", err)
}
}

if !s.noDiscover {
Expand Down Expand Up @@ -443,7 +456,7 @@ func (s *Service) Stop() error {
s.cancel()

// close mDNS discovery service
err := s.mdns.close()
err := s.mdns.Stop()
if err != nil {
logger.Errorf("Failed to close mDNS discovery service: %s", err)
}
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ require (
github.com/libp2p/go-openssl v0.0.7 // indirect
github.com/libp2p/go-reuseport v0.2.0 // indirect
github.com/libp2p/go-yamux/v3 v3.1.2 // indirect
github.com/libp2p/zeroconf/v2 v2.1.1 // indirect
github.com/lucas-clemente/quic-go v0.27.1 // indirect
github.com/marten-seemann/qtls-go1-16 v0.1.5 // indirect
github.com/marten-seemann/qtls-go1-17 v0.1.1 // indirect
Expand Down Expand Up @@ -170,7 +169,7 @@ require (
github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce // indirect
github.com/vedhavyas/go-subkey v1.0.3 // indirect
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9 // indirect
github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 // indirect
go.opencensus.io v0.23.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,6 @@ github.com/libp2p/go-yamux/v3 v3.0.1/go.mod h1:s2LsDhHbh+RfCsQoICSYt58U2f8ijtPAN
github.com/libp2p/go-yamux/v3 v3.0.2/go.mod h1:s2LsDhHbh+RfCsQoICSYt58U2f8ijtPANFD8BmE74Bo=
github.com/libp2p/go-yamux/v3 v3.1.2 h1:lNEy28MBk1HavUAlzKgShp+F6mn/ea1nDYWftZhFW9Q=
github.com/libp2p/go-yamux/v3 v3.1.2/go.mod h1:jeLEQgLXqE2YqX1ilAClIfCMDY+0uXQUKmmb/qp0gT4=
github.com/libp2p/zeroconf/v2 v2.1.1 h1:XAuSczA96MYkVwH+LqqqCUZb2yH3krobMJ1YE+0hG2s=
github.com/libp2p/zeroconf/v2 v2.1.1/go.mod h1:fuJqLnUwZTshS3U/bMRJ3+ow/v9oid1n0DmyYyNO1Xs=
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=
Expand Down
60 changes: 60 additions & 0 deletions internal/mdns/dialable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2022 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

package mdns

import (
"errors"
"fmt"
"net"

manet "github.com/multiformats/go-multiaddr/net"
)

var (
ErrTCPListenAddressNotFound = errors.New("TCP listen address not found")
)

func getMDNSIPsAndPort(network interfaceListenAddressesGetter) (ips []net.IP, port uint16) {
tcpAddresses, err := getDialableListenAddrs(network)
if err != nil {
const defaultPort = 4001
return nil, defaultPort
}

ips = make([]net.IP, len(tcpAddresses))
for i := range tcpAddresses {
ips[i] = tcpAddresses[i].IP
}
port = uint16(tcpAddresses[0].Port)

return ips, port
}

func getDialableListenAddrs(network interfaceListenAddressesGetter) (tcpAddresses []*net.TCPAddr, err error) {
multiAddresses, err := network.InterfaceListenAddresses()
if err != nil {
return nil, fmt.Errorf("listing host interface listen addresses: %w", err)
}

tcpAddresses = make([]*net.TCPAddr, 0, len(multiAddresses))
for _, multiAddress := range multiAddresses {
netAddress, err := manet.ToNetAddr(multiAddress)
if err != nil {
continue
}

tcpAddress, ok := netAddress.(*net.TCPAddr)
if !ok {
continue
}

tcpAddresses = append(tcpAddresses, tcpAddress)
}

if len(tcpAddresses) == 0 {
return nil, fmt.Errorf("%w: in %d multiaddresses", ErrTCPListenAddressNotFound, len(multiAddresses))
}

return tcpAddresses, nil
}
32 changes: 32 additions & 0 deletions internal/mdns/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2022 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only

package mdns

import (
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/multiformats/go-multiaddr"
)

// Logger is a logger interface for the mDNS service.
type Logger interface {
Debugf(format string, args ...any)
Warnf(format string, args ...any)
}

// IDNetworker can return the peer ID and a network interface.
type IDNetworker interface {
ID() peer.ID
Networker
}

// Networker can return a network interface.
type Networker interface {
Network() network.Network
}

// interfaceListenAddressesGetter returns the listen addresses of the interfaces.
type interfaceListenAddressesGetter interface {
InterfaceListenAddresses() ([]multiaddr.Multiaddr, error)
}
Loading