diff --git a/client/internal/engine.go b/client/internal/engine.go index 6acd834e3..8f0fb7c84 100644 --- a/client/internal/engine.go +++ b/client/internal/engine.go @@ -85,7 +85,7 @@ type Engine struct { udpMuxConn *net.UDPConn udpMuxConnSrflx *net.UDPConn - // networkSerial is the latest Serial (state ID) of the network sent by the Management service + // networkSerial is the latest CurrentSerial (state ID) of the network sent by the Management service networkSerial uint64 } diff --git a/client/internal/engine_test.go b/client/internal/engine_test.go index a941e0125..5d66473ca 100644 --- a/client/internal/engine_test.go +++ b/client/internal/engine_test.go @@ -96,7 +96,7 @@ func TestEngine_UpdateNetworkMap(t *testing.T) { expectedSerial: 1, } - // 2nd case - one extra peer added and network map has Serial grater than local => apply the update + // 2nd case - one extra peer added and network map has CurrentSerial grater than local => apply the update case2 := testCase{ name: "input with an old peer and a new peer to add", networkMap: &mgmtProto.NetworkMap{ diff --git a/management/proto/management.pb.go b/management/proto/management.pb.go index a527a10b2..074c896d2 100644 --- a/management/proto/management.pb.go +++ b/management/proto/management.pb.go @@ -836,7 +836,7 @@ type NetworkMap struct { // Serial is an ID of the network state to be used by clients to order updates. // The larger the Serial the newer the configuration. // E.g. the client app should keep track of this id locally and discard all the configurations with a lower value - Serial uint64 `protobuf:"varint,1,opt,name=Serial,proto3" json:"Serial,omitempty"` + Serial uint64 `protobuf:"varint,1,opt,name=CurrentSerial,proto3" json:"CurrentSerial,omitempty"` // PeerConfig represents configuration of a peer PeerConfig *PeerConfig `protobuf:"bytes,2,opt,name=peerConfig,proto3" json:"peerConfig,omitempty"` // RemotePeerConfig represents a list of remote peers that the receiver can connect to diff --git a/management/server/account_test.go b/management/server/account_test.go index 1eb0c4f04..ac32a1c0f 100644 --- a/management/server/account_test.go +++ b/management/server/account_test.go @@ -397,7 +397,7 @@ func TestAccountManager_AddPeer(t *testing.T) { t.Fatal(err) } - serial := account.Network.Serial() //should be 0 + serial := account.Network.CurrentSerial() //should be 0 var setupKey *SetupKey for _, key := range account.SetupKeys { @@ -409,8 +409,8 @@ func TestAccountManager_AddPeer(t *testing.T) { return } - if account.Network.serial != 0 { - t.Errorf("expecting account network to have an initial serial=0") + if account.Network.Serial != 0 { + t.Errorf("expecting account network to have an initial Serial=0") return } @@ -446,8 +446,8 @@ func TestAccountManager_AddPeer(t *testing.T) { t.Errorf("expecting just added peer to have IP = %s, got %s", expectedPeerIP, peer.IP.String()) } - if account.Network.Serial() != 1 { - t.Errorf("expecting Network serial=%d to be incremented by 1 and be equal to %d when adding new peer to account", serial, account.Network.Serial()) + if account.Network.CurrentSerial() != 1 { + t.Errorf("expecting Network Serial=%d to be incremented by 1 and be equal to %d when adding new peer to account", serial, account.Network.CurrentSerial()) } } @@ -498,8 +498,8 @@ func TestAccountManager_DeletePeer(t *testing.T) { return } - if account.Network.Serial() != 2 { - t.Errorf("expecting Network serial=%d to be incremented and be equal to 2 after adding and deleteing a peer", account.Network.Serial()) + if account.Network.CurrentSerial() != 2 { + t.Errorf("expecting Network Serial=%d to be incremented and be equal to 2 after adding and deleteing a peer", account.Network.CurrentSerial()) } } diff --git a/management/server/grpcserver.go b/management/server/grpcserver.go index e18d5100a..d0a09f03f 100644 --- a/management/server/grpcserver.go +++ b/management/server/grpcserver.go @@ -173,7 +173,7 @@ func (s *Server) registerPeer(peerKey wgtypes.Key, req *proto.LoginRequest) (*Pe peersToSend = append(peersToSend, p) } } - update := toSyncResponse(s.config, peer, peersToSend, nil, networkMap.Network.Serial()) + update := toSyncResponse(s.config, peer, peersToSend, nil, networkMap.Network.CurrentSerial()) err = s.peersUpdateManager.SendUpdate(remotePeer.Key, &UpdateMessage{Update: update}) if err != nil { // todo rethink if we should keep this return @@ -362,7 +362,7 @@ func (s *Server) sendInitialSync(peerKey wgtypes.Key, peer *Peer, srv proto.Mana } else { turnCredentials = nil } - plainResp := toSyncResponse(s.config, peer, networkMap.Peers, turnCredentials, networkMap.Network.Serial()) + plainResp := toSyncResponse(s.config, peer, networkMap.Peers, turnCredentials, networkMap.Network.CurrentSerial()) encryptedResp, err := encryption.EncryptMessage(peerKey, s.wgKey, plainResp) if err != nil { diff --git a/management/server/management_proto_test.go b/management/server/management_proto_test.go index dab2c2ed3..0aedce662 100644 --- a/management/server/management_proto_test.go +++ b/management/server/management_proto_test.go @@ -258,7 +258,7 @@ func Test_SyncProtocol(t *testing.T) { } if networkMap.GetSerial() <= 0 { - t.Fatalf("expecting SyncResponse to have NetworkMap with a positive Network Serial, actual %d", networkMap.GetSerial()) + t.Fatalf("expecting SyncResponse to have NetworkMap with a positive Network CurrentSerial, actual %d", networkMap.GetSerial()) } } diff --git a/management/server/network.go b/management/server/network.go index b495d2858..22ec811e2 100644 --- a/management/server/network.go +++ b/management/server/network.go @@ -22,34 +22,34 @@ type Network struct { Id string Net net.IPNet Dns string - // serial is an ID that increments by 1 when any change to the network happened (e.g. new peer has been added). + // Serial is an ID that increments by 1 when any change to the network happened (e.g. new peer has been added). // Used to synchronize state to the client apps. - serial uint64 + Serial uint64 mu sync.Mutex `json:"-"` } -// NewNetwork creates a new Network initializing it with a serial=0 +// NewNetwork creates a new Network initializing it with a Serial=0 func NewNetwork() *Network { return &Network{ Id: xid.New().String(), Net: net.IPNet{IP: net.ParseIP("100.64.0.0"), Mask: net.IPMask{255, 192, 0, 0}}, Dns: "", - serial: 0} + Serial: 0} } -// IncSerial increments serial by 1 reflecting that the network state has been changed +// IncSerial increments Serial by 1 reflecting that the network state has been changed func (n *Network) IncSerial() { n.mu.Lock() defer n.mu.Unlock() - n.serial = n.serial + 1 + n.Serial = n.Serial + 1 } -// Serial returns the Network.serial of the network (latest state id) -func (n *Network) Serial() uint64 { +// CurrentSerial returns the Network.Serial of the network (latest state id) +func (n *Network) CurrentSerial() uint64 { n.mu.Lock() defer n.mu.Unlock() - return n.serial + return n.Serial } func (n *Network) Copy() *Network { @@ -57,7 +57,7 @@ func (n *Network) Copy() *Network { Id: n.Id, Net: n.Net, Dns: n.Dns, - serial: n.serial, + Serial: n.Serial, } } diff --git a/management/server/peer.go b/management/server/peer.go index 6de5e2495..8f0f1f042 100644 --- a/management/server/peer.go +++ b/management/server/peer.go @@ -142,7 +142,7 @@ func (am *DefaultAccountManager) DeletePeer(accountId string, peerKey string) (* RemotePeersIsEmpty: true, // new field NetworkMap: &proto.NetworkMap{ - Serial: account.Network.Serial(), + Serial: account.Network.CurrentSerial(), RemotePeers: []*proto.RemotePeerConfig{}, RemotePeersIsEmpty: true, }, @@ -173,7 +173,7 @@ func (am *DefaultAccountManager) DeletePeer(accountId string, peerKey string) (* RemotePeersIsEmpty: len(update) == 0, // new field NetworkMap: &proto.NetworkMap{ - Serial: account.Network.Serial(), + Serial: account.Network.CurrentSerial(), RemotePeers: update, RemotePeersIsEmpty: len(update) == 0, },