Add ipv6 multiaddr support (#5199)

* Add ipv6 multiaddr support
* allow ipv6 for discv5
This commit is contained in:
Preston Van Loon 2020-03-24 21:03:51 -07:00 committed by GitHub
parent e15d92df06
commit 48d4a8655a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 2 deletions

View File

@ -49,7 +49,7 @@ func createListener(ipAddr net.IP, privKey *ecdsa.PrivateKey, cfg *Config) *disc
} }
if cfg.HostAddress != "" { if cfg.HostAddress != "" {
hostIP := net.ParseIP(cfg.HostAddress) hostIP := net.ParseIP(cfg.HostAddress)
if hostIP.To4() == nil { if hostIP.To4() == nil && hostIP.To16() == nil {
log.Errorf("Invalid host address given: %s", hostIP.String()) log.Errorf("Invalid host address given: %s", hostIP.String())
} else { } else {
localNode.SetFallbackIP(hostIP) localNode.SetFallbackIP(hostIP)

View File

@ -42,7 +42,11 @@ func buildOptions(cfg *Config, ip net.IP, priKey *ecdsa.PrivateKey) []libp2p.Opt
} }
if cfg.HostAddress != "" { if cfg.HostAddress != "" {
options = append(options, libp2p.AddrsFactory(func(addrs []multiaddr.Multiaddr) []multiaddr.Multiaddr { options = append(options, libp2p.AddrsFactory(func(addrs []multiaddr.Multiaddr) []multiaddr.Multiaddr {
external, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", cfg.HostAddress, cfg.TCPPort)) 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))
if err != nil { if err != nil {
log.WithError(err).Error("Unable to create external multiaddress") log.WithError(err).Error("Unable to create external multiaddress")
} else { } else {