Fix Incorrect Logging for IPV6 Addresses (#5204)

* fix ipv6 issues
* Merge branch 'master' into fixIPV6
* imports
* Merge branch 'fixIPV6' of https://github.com/prysmaticlabs/geth-sharding into fixIPV6
* Merge branch 'master' into fixIPV6
This commit is contained in:
Nishant Das 2020-03-26 01:19:11 +08:00 committed by GitHub
parent 2ad5cec56c
commit e077d3ddc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 17 deletions

View File

@ -39,7 +39,14 @@ func createListener(ipAddr net.IP, privKey *ecdsa.PrivateKey, cfg *Config) *disc
IP: ipAddr,
Port: int(cfg.UDPPort),
}
conn, err := net.ListenUDP("udp4", udpAddr)
// assume ip is either ipv4 or ipv6
networkVersion := ""
if ipAddr.To4() != nil {
networkVersion = "udp4"
} else {
networkVersion = "udp6"
}
conn, err := net.ListenUDP(networkVersion, udpAddr)
if err != nil {
log.Fatal(err)
}

View File

@ -11,13 +11,14 @@ import (
filter "github.com/libp2p/go-maddr-filter"
"github.com/multiformats/go-multiaddr"
ma "github.com/multiformats/go-multiaddr"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/connmgr"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
)
// buildOptions for the libp2p host.
func buildOptions(cfg *Config, ip net.IP, priKey *ecdsa.PrivateKey) []libp2p.Option {
listen, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, cfg.TCPPort))
listen, err := multiAddressBuilder(ip.String(), cfg.TCPPort)
if err != nil {
log.Fatalf("Failed to p2p listen: %v", err)
}
@ -42,11 +43,7 @@ func buildOptions(cfg *Config, ip net.IP, priKey *ecdsa.PrivateKey) []libp2p.Opt
}
if cfg.HostAddress != "" {
options = append(options, libp2p.AddrsFactory(func(addrs []multiaddr.Multiaddr) []multiaddr.Multiaddr {
protocol := "ip4"
if ip := net.ParseIP(cfg.HostAddress); ip.To4() == nil {
protocol = "ip6"
}
external, err := multiaddr.NewMultiaddr(fmt.Sprintf("/%s/%s/tcp/%d", protocol, cfg.HostAddress, cfg.TCPPort))
external, err := multiAddressBuilder(cfg.HostAddress, cfg.TCPPort)
if err != nil {
log.WithError(err).Error("Unable to create external multiaddress")
} else {
@ -71,7 +68,7 @@ func buildOptions(cfg *Config, ip net.IP, priKey *ecdsa.PrivateKey) []libp2p.Opt
log.Errorf("Invalid local ip provided: %s", cfg.LocalIP)
return options
}
listen, err = ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", cfg.LocalIP, cfg.TCPPort))
listen, err = multiAddressBuilder(cfg.LocalIP, cfg.TCPPort)
if err != nil {
log.Fatalf("Failed to p2p listen: %v", err)
}
@ -80,6 +77,17 @@ func buildOptions(cfg *Config, ip net.IP, priKey *ecdsa.PrivateKey) []libp2p.Opt
return options
}
func multiAddressBuilder(ipAddr string, port uint) (ma.Multiaddr, error) {
parsedIP := net.ParseIP(ipAddr)
if parsedIP.To4() == nil && parsedIP.To16() == nil {
return nil, errors.Errorf("invalid ip address provided: %s", ipAddr)
}
if parsedIP.To4() != nil {
return ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ipAddr, port))
}
return ma.NewMultiaddr(fmt.Sprintf("/ip6/%s/tcp/%d", ipAddr, port))
}
// Adds a private key to the libp2p option if the option was provided.
// If the private key file is missing or cannot be read, or if the
// private key contents cannot be marshaled, an exception is thrown.

View File

@ -173,7 +173,6 @@ func (s *Service) Start() {
return
}
s.dv5Listener = listener
go s.listenForNewNodes()
}
@ -221,13 +220,13 @@ func (s *Service) Start() {
runutil.RunEvery(s.ctx, 10*time.Second, s.updateMetrics)
multiAddrs := s.host.Network().ListenAddresses()
logIP4Addr(s.host.ID(), multiAddrs...)
logIPAddr(s.host.ID(), multiAddrs...)
p2pHostAddress := s.cfg.HostAddress
p2pTCPPort := s.cfg.TCPPort
if p2pHostAddress != "" {
logExternalIP4Addr(s.host.ID(), p2pHostAddress, p2pTCPPort)
logExternalIPAddr(s.host.ID(), p2pHostAddress, p2pTCPPort)
}
p2pHostDNS := s.cfg.HostDNS
@ -468,10 +467,10 @@ func (s *Service) addKadDHTNodesToExclusionList(addr string) error {
return nil
}
func logIP4Addr(id peer.ID, addrs ...ma.Multiaddr) {
func logIPAddr(id peer.ID, addrs ...ma.Multiaddr) {
var correctAddr ma.Multiaddr
for _, addr := range addrs {
if strings.Contains(addr.String(), "/ip4/") {
if strings.Contains(addr.String(), "/ip4/") || strings.Contains(addr.String(), "/ip6/") {
correctAddr = addr
break
}
@ -484,13 +483,16 @@ func logIP4Addr(id peer.ID, addrs ...ma.Multiaddr) {
}
}
func logExternalIP4Addr(id peer.ID, addr string, port uint) {
func logExternalIPAddr(id peer.ID, addr string, port uint) {
if addr != "" {
p := strconv.FormatUint(uint64(port), 10)
multiAddr, err := multiAddressBuilder(addr, port)
if err != nil {
log.Errorf("Could not create multiaddress: %v", err)
return
}
log.WithField(
"multiAddr",
"/ip4/"+addr+"/tcp/"+p+"/p2p/"+id.String(),
multiAddr.String()+"/p2p/"+id.String(),
).Info("Node started external p2p server")
}
}