Skip to content

Commit 5740c3e

Browse files
Net 1115 (#2890)
* add endpointipv6 for host * keep endpointipv6 unchanged when enable static endpoint * handle ipv6 endpoint updates --------- Co-authored-by: abhishek9686 <[email protected]>
1 parent 879f372 commit 5740c3e

File tree

6 files changed

+33
-2
lines changed

6 files changed

+33
-2
lines changed

cli/cmd/host/update.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
var (
1515
apiHostFilePath string
1616
endpoint string
17+
endpoint6 string
1718
name string
1819
listenPort int
1920
mtu int
@@ -40,6 +41,7 @@ var hostUpdateCmd = &cobra.Command{
4041
} else {
4142
apiHost.ID = args[0]
4243
apiHost.EndpointIP = endpoint
44+
apiHost.EndpointIPv6 = endpoint6
4345
apiHost.Name = name
4446
apiHost.ListenPort = listenPort
4547
apiHost.MTU = mtu
@@ -54,6 +56,7 @@ var hostUpdateCmd = &cobra.Command{
5456
func init() {
5557
hostUpdateCmd.Flags().StringVar(&apiHostFilePath, "file", "", "Path to host_definition.json")
5658
hostUpdateCmd.Flags().StringVar(&endpoint, "endpoint", "", "Endpoint of the Host")
59+
hostUpdateCmd.Flags().StringVar(&endpoint6, "endpoint6", "", "IPv6 Endpoint of the Host")
5760
hostUpdateCmd.Flags().StringVar(&name, "name", "", "Host name")
5861
hostUpdateCmd.Flags().IntVar(&listenPort, "listen_port", 0, "Listen port of the host")
5962
hostUpdateCmd.Flags().IntVar(&mtu, "mtu", 0, "Host MTU size")

logic/hosts.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ func UpdateHost(newHost, currentHost *models.Host) {
217217
newHost.Nodes = currentHost.Nodes
218218
newHost.PublicKey = currentHost.PublicKey
219219
newHost.TrafficKeyPublic = currentHost.TrafficKeyPublic
220+
newHost.EndpointIPv6 = currentHost.EndpointIPv6
220221
// changeable fields
221222
if len(newHost.Version) == 0 {
222223
newHost.Version = currentHost.Version
@@ -258,6 +259,10 @@ func UpdateHostFromClient(newHost, currHost *models.Host) (sendPeerUpdate bool)
258259
currHost.EndpointIP = newHost.EndpointIP
259260
sendPeerUpdate = true
260261
}
262+
if currHost.EndpointIPv6.String() != newHost.EndpointIPv6.String() {
263+
currHost.EndpointIPv6 = newHost.EndpointIPv6
264+
sendPeerUpdate = true
265+
}
261266
currHost.DaemonInstalled = newHost.DaemonInstalled
262267
currHost.Debug = newHost.Debug
263268
currHost.Verbosity = newHost.Verbosity

logic/peers.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,21 @@ func GetPeerUpdateForHost(network string, host *models.Host, allNodes []models.N
207207
uselocal = false
208208
}
209209
}
210+
211+
//if host is ipv4 only or ipv4+ipv6, set the peer endpoint to ipv4 address, if host is ipv6 only, set the peer endpoint to ipv6 address
212+
peerEndpoint := peerHost.EndpointIP
213+
if ipv4 := host.EndpointIP.To4(); ipv4 != nil {
214+
peerEndpoint = peerHost.EndpointIP
215+
} else {
216+
//if peer host's ipv6 address is empty, it means that peer is an IPv4 only host
217+
//IPv4 only host could not communicate with IPv6 only host
218+
if peerHost.EndpointIPv6 != nil && peerHost.EndpointIPv6.String() != "" {
219+
peerEndpoint = peerHost.EndpointIPv6
220+
}
221+
}
222+
210223
peerConfig.Endpoint = &net.UDPAddr{
211-
IP: peerHost.EndpointIP,
224+
IP: peerEndpoint,
212225
Port: GetPeerListenPort(peerHost),
213226
}
214227

models/api_host.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type ApiHost struct {
2222
Interfaces []ApiIface `json:"interfaces" yaml:"interfaces"`
2323
DefaultInterface string `json:"defaultinterface" yaml:"defautlinterface"`
2424
EndpointIP string `json:"endpointip" yaml:"endpointip"`
25+
EndpointIPv6 string `json:"endpointipv6" yaml:"endpointipv6"`
2526
PublicKey string `json:"publickey"`
2627
MacAddress string `json:"macaddress"`
2728
Nodes []string `json:"nodes"`
@@ -43,6 +44,7 @@ func (h *Host) ConvertNMHostToAPI() *ApiHost {
4344
a := ApiHost{}
4445
a.Debug = h.Debug
4546
a.EndpointIP = h.EndpointIP.String()
47+
a.EndpointIPv6 = h.EndpointIPv6.String()
4648
a.FirewallInUse = h.FirewallInUse
4749
a.ID = h.ID.String()
4850
a.Interfaces = make([]ApiIface, len(h.Interfaces))
@@ -83,6 +85,11 @@ func (a *ApiHost) ConvertAPIHostToNMHost(currentHost *Host) *Host {
8385
} else {
8486
h.EndpointIP = net.ParseIP(a.EndpointIP)
8587
}
88+
if len(a.EndpointIPv6) == 0 || strings.Contains(a.EndpointIPv6, "nil") {
89+
h.EndpointIPv6 = currentHost.EndpointIPv6
90+
} else {
91+
h.EndpointIPv6 = net.ParseIP(a.EndpointIPv6)
92+
}
8693
h.Debug = a.Debug
8794
h.FirewallInUse = a.FirewallInUse
8895
h.IPForwarding = currentHost.IPForwarding

models/host.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type Host struct {
6363
Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
6464
DefaultInterface string `json:"defaultinterface" yaml:"defaultinterface"`
6565
EndpointIP net.IP `json:"endpointip" yaml:"endpointip"`
66+
EndpointIPv6 net.IP `json:"endpointipv6" yaml:"endpointipv6"`
6667
IsDocker bool `json:"isdocker" yaml:"isdocker"`
6768
IsK8S bool `json:"isk8s" yaml:"isk8s"`
6869
IsStatic bool `json:"isstatic" yaml:"isstatic"`

mq/handlers.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,11 @@ func HandleHostCheckin(h, currentHost *models.Host) bool {
282282
!h.EndpointIP.Equal(currentHost.EndpointIP) ||
283283
(len(h.NatType) > 0 && h.NatType != currentHost.NatType) ||
284284
h.DefaultInterface != currentHost.DefaultInterface ||
285-
(h.ListenPort != 0 && h.ListenPort != currentHost.ListenPort) || (h.WgPublicListenPort != 0 && h.WgPublicListenPort != currentHost.WgPublicListenPort)
285+
(h.ListenPort != 0 && h.ListenPort != currentHost.ListenPort) ||
286+
(h.WgPublicListenPort != 0 && h.WgPublicListenPort != currentHost.WgPublicListenPort) || (!h.EndpointIPv6.Equal(currentHost.EndpointIPv6))
286287
if ifaceDelta { // only save if something changes
287288
currentHost.EndpointIP = h.EndpointIP
289+
currentHost.EndpointIPv6 = h.EndpointIPv6
288290
currentHost.Interfaces = h.Interfaces
289291
currentHost.DefaultInterface = h.DefaultInterface
290292
currentHost.NatType = h.NatType

0 commit comments

Comments
 (0)