🌐 US-Proxy
class="logged-out env-production page-responsive" style="word-wrap: break-word;" >
Skip to content

Commit f3630bc

Browse files
committed
support IPv6: preferred addr family (part five)
* resolve (host => IP) in 4 ordered passes: - from non-loopback & matching preferred - to any dialable (last resort) * joining node must have the primary's addr family ------- * prev. commit: d14c58a Signed-off-by: Alex Aizman <alex.aizman@gmail.com>
1 parent d14c58a commit f3630bc

4 files changed

Lines changed: 35 additions & 13 deletions

File tree

‎ais/htrun.go‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -420,12 +420,8 @@ func (h *htrun) initSnode(config *cmn.Config) {
420420

421421
// update net servers that were initialized with useIPv6=true
422422
g.netServ.pub.useIPv6 = false
423-
if config.HostNet.UseIntraControl {
424-
g.netServ.control.useIPv6 = false
425-
}
426-
if config.HostNet.UseIntraData {
427-
g.netServ.data.useIPv6 = false
428-
}
423+
g.netServ.control.useIPv6 = false
424+
g.netServ.data.useIPv6 = false
429425
}
430426

431427
if l := len(addrList); l > 1 {

‎ais/prxclu.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,8 @@ func (p *proxy) httpclupost(w http.ResponseWriter, r *http.Request) {
515515
return
516516
}
517517
}
518-
if _, err := cmn.ParseHost2IP(nsi.PubNet.Hostname, false /*local*/); err != nil {
518+
// in re useIPv6 - joining node must have the same
519+
if _, err := cmn.ParseHost2IP(nsi.PubNet.Hostname, false /*local*/, g.netServ.control.useIPv6); err != nil {
519520
p.writeErrf(w, r, "%s: failed to %s %s: invalid hostname: %v", p.si, apiOp, nsi.StringEx(), err)
520521
return
521522
}

‎ais/utils.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ func _selectHost(locIPs []*localIPInfo, hostnames []string, useIPv6 bool) (strin
254254
}
255255
ipstr = pip.String()
256256
} else {
257-
ip, err := cmn.Host2IP(hostNoBr, true /*local*/)
257+
ip, err := cmn.Host2IP(hostNoBr, true /*local*/, useIPv6)
258258
if err != nil {
259259
nlog.Errorln("failed to resolve hostname(?)", hostNoBr, "err:", err, "[idx:", i, len(hostnames))
260260
continue

‎cmn/network.go‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func ValidatePort(port int) (int, error) {
4646
}
4747

4848
// resolve `host` and returns the first usable (TCP dialable) IP address (v4 or v6)
49-
func Host2IP(host string, localTimeout bool) (net.IP, error) {
49+
func Host2IP(host string, localTimeout, preferV6 bool) (net.IP, error) {
5050
timeout := max(time.Second, Rom.MaxKeepalive())
5151
if localTimeout {
5252
timeout = max(time.Second, Rom.CplaneOperation())
@@ -59,14 +59,39 @@ func Host2IP(host string, localTimeout bool) (net.IP, error) {
5959
return nil, err
6060
}
6161

62-
// pass 1: prefer non-loopback if possible
62+
// pass 1: prefer non-loopback, matching preferred address family
63+
for _, addr := range addrs {
64+
ip := addr.IP
65+
if !IsDialableHostIP(ip) || ip.IsLoopback() {
66+
continue
67+
}
68+
isV6 := ip.To4() == nil
69+
if preferV6 == isV6 {
70+
return ip, nil
71+
}
72+
}
73+
74+
// pass 2: non-loopback, any family (fallback)
6375
for _, addr := range addrs {
6476
ip := addr.IP
6577
if IsDialableHostIP(ip) && !ip.IsLoopback() {
6678
return ip, nil
6779
}
6880
}
69-
// pass 2 (fallback)
81+
82+
// pass 3: loopback, preferred family first
83+
for _, addr := range addrs {
84+
ip := addr.IP
85+
if !IsDialableHostIP(ip) {
86+
continue
87+
}
88+
isV6 := ip.To4() == nil
89+
if preferV6 == isV6 {
90+
return ip, nil
91+
}
92+
}
93+
94+
// pass 4: any dialable (last resort)
7095
for _, addr := range addrs {
7196
ip := addr.IP
7297
if IsDialableHostIP(ip) {
@@ -116,7 +141,7 @@ func IsDialableHostIP(ip net.IP) bool {
116141
return true
117142
}
118143

119-
func ParseHost2IP(host string, local bool) (net.IP, error) {
144+
func ParseHost2IP(host string, localTimeout, preferV6 bool) (net.IP, error) {
120145
ip := net.ParseIP(host)
121146
if ip != nil {
122147
if !IsDialableHostIP(ip) {
@@ -126,7 +151,7 @@ func ParseHost2IP(host string, local bool) (net.IP, error) {
126151
}
127152
return ip, nil // is a parse-able IP addr
128153
}
129-
return Host2IP(host, local)
154+
return Host2IP(host, localTimeout, preferV6)
130155
}
131156

132157
func AddrToNetworkFamily(addr string) string {

0 commit comments

Comments
 (0)