From 59d8ae4334a2dbf77ac9a366c8cb6a134fab9790 Mon Sep 17 00:00:00 2001 From: Tim Beatham Date: Thu, 4 Jan 2024 13:12:07 +0000 Subject: [PATCH] 81-seperate-synchronisation - More code comments --- pkg/mesh/route.go | 2 ++ pkg/query/query.go | 7 ++++++- pkg/robin/requester.go | 14 +++++++++++++- pkg/robin/responder.go | 2 ++ pkg/route/route.go | 3 +++ pkg/timers/timers.go | 15 --------------- 6 files changed, 26 insertions(+), 17 deletions(-) delete mode 100644 pkg/timers/timers.go diff --git a/pkg/mesh/route.go b/pkg/mesh/route.go index bb24a63..180decc 100644 --- a/pkg/mesh/route.go +++ b/pkg/mesh/route.go @@ -7,7 +7,9 @@ import ( "github.com/tim-beatham/smegmesh/pkg/lib" ) +// RouteManager: manager that leaks routes between meshes type RouteManager interface { + // UpdateRoutes: leak all routes in each mesh UpdateRoutes() error } diff --git a/pkg/query/query.go b/pkg/query/query.go index 6eaf82f..ad9e9d0 100644 --- a/pkg/query/query.go +++ b/pkg/query/query.go @@ -17,20 +17,24 @@ type Querier interface { Query(meshId string, queryParams string) ([]byte, error) } +// JmesQuerier: queries the datstore in JMESPath syntax type JmesQuerier struct { manager mesh.MeshManager } +// QueryError: query error if something went wrong type QueryError struct { msg string } +// QuerRoute: represents a route in the query type QueryRoute struct { Destination string `json:"destination"` HopCount int `json:"hopCount"` Path string `json:"path"` } +// QueryNode: represents a single node in the query type QueryNode struct { HostEndpoint string `json:"hostEndpoint"` PublicKey string `json:"publicKey"` @@ -48,7 +52,7 @@ func (m *QueryError) Error() string { return m.msg } -// Query: queries the data +// Query: queries the the datastore at the given meshid func (j *JmesQuerier) Query(meshId, queryParams string) ([]byte, error) { mesh, ok := j.manager.GetMeshes()[meshId] @@ -74,6 +78,7 @@ func (j *JmesQuerier) Query(meshId, queryParams string) ([]byte, error) { return bytes, err } +// MeshNodeToQuerynode: convert the mesh node into a query abstraction func MeshNodeToQueryNode(node mesh.MeshNode) *QueryNode { queryNode := new(QueryNode) queryNode.HostEndpoint = node.GetHostEndpoint() diff --git a/pkg/robin/requester.go b/pkg/robin/requester.go index 7b0f474..8a31c7e 100644 --- a/pkg/robin/requester.go +++ b/pkg/robin/requester.go @@ -14,10 +14,12 @@ import ( "github.com/tim-beatham/smegmesh/pkg/rpc" ) +// IpcHandler: represents a handler for ipc calls type IpcHandler struct { Server ctrlserver.CtrlServer } +// getOverrideConfiguration: override any specific WireGuard configuration func getOverrideConfiguration(args *ipc.WireGuardArgs) conf.WgConfiguration { overrideConf := conf.WgConfiguration{} @@ -40,6 +42,7 @@ func getOverrideConfiguration(args *ipc.WireGuardArgs) conf.WgConfiguration { return overrideConf } +// CreateMesh: create a new mesh network func (n *IpcHandler) CreateMesh(args *ipc.NewMeshArgs, reply *string) error { overrideConf := getOverrideConfiguration(&args.WgArgs) @@ -66,6 +69,7 @@ func (n *IpcHandler) CreateMesh(args *ipc.NewMeshArgs, reply *string) error { return err } +// ListMeshes: list mesh networks func (n *IpcHandler) ListMeshes(_ string, reply *ipc.ListMeshReply) error { meshNames := make([]string, len(n.Server.GetMeshManager().GetMeshes())) @@ -79,6 +83,7 @@ func (n *IpcHandler) ListMeshes(_ string, reply *ipc.ListMeshReply) error { return nil } +// JoinMesh: join a mesh network func (n *IpcHandler) JoinMesh(args *ipc.JoinMeshArgs, reply *string) error { overrideConf := getOverrideConfiguration(&args.WgArgs) @@ -140,7 +145,7 @@ func (n *IpcHandler) JoinMesh(args *ipc.JoinMeshArgs, reply *string) error { return nil } -// LeaveMesh leaves a mesh network +// LeaveMesh: leaves a mesh network func (n *IpcHandler) LeaveMesh(meshId string, reply *string) error { err := n.Server.GetMeshManager().LeaveMesh(meshId) @@ -150,6 +155,7 @@ func (n *IpcHandler) LeaveMesh(meshId string, reply *string) error { return err } +// GetMesh: get a mesh network at the given meshid func (n *IpcHandler) GetMesh(meshId string, reply *ipc.GetMeshReply) error { theMesh := n.Server.GetMeshManager().GetMesh(meshId) @@ -181,6 +187,7 @@ func (n *IpcHandler) GetMesh(meshId string, reply *ipc.GetMeshReply) error { return nil } +// Query: perform a jmespath query func (n *IpcHandler) Query(params ipc.QueryMesh, reply *string) error { queryResponse, err := n.Server.GetQuerier().Query(params.MeshId, params.Query) @@ -192,6 +199,7 @@ func (n *IpcHandler) Query(params ipc.QueryMesh, reply *string) error { return nil } +// PutDescription: change your description in the mesh func (n *IpcHandler) PutDescription(args ipc.PutDescriptionArgs, reply *string) error { err := n.Server.GetMeshManager().SetDescription(args.MeshId, args.Description) @@ -203,6 +211,7 @@ func (n *IpcHandler) PutDescription(args ipc.PutDescriptionArgs, reply *string) return nil } +// PutAlias: put your aliasin the mesh func (n *IpcHandler) PutAlias(args ipc.PutAliasArgs, reply *string) error { if args.Alias == "" { return fmt.Errorf("alias not provided") @@ -218,6 +227,7 @@ func (n *IpcHandler) PutAlias(args ipc.PutAliasArgs, reply *string) error { return nil } +// PutService: place a service in the mesh func (n *IpcHandler) PutService(service ipc.PutServiceArgs, reply *string) error { err := n.Server.GetMeshManager().SetService(service.MeshId, service.Service, service.Value) @@ -229,6 +239,7 @@ func (n *IpcHandler) PutService(service ipc.PutServiceArgs, reply *string) error return nil } +// DeleteService: withtract a service in the mesh func (n *IpcHandler) DeleteService(service ipc.DeleteServiceArgs, reply *string) error { err := n.Server.GetMeshManager().RemoveService(service.MeshId, service.Service) @@ -240,6 +251,7 @@ func (n *IpcHandler) DeleteService(service ipc.DeleteServiceArgs, reply *string) return nil } +// RobinIpcParams: parameters required to construct a new mesh network type RobinIpcParams struct { CtrlServer ctrlserver.CtrlServer } diff --git a/pkg/robin/responder.go b/pkg/robin/responder.go index fd71076..02814f2 100644 --- a/pkg/robin/responder.go +++ b/pkg/robin/responder.go @@ -8,11 +8,13 @@ import ( "github.com/tim-beatham/smegmesh/pkg/rpc" ) +// WgRpc: represents a WireGuard rpc call type WgRpc struct { rpc.UnimplementedMeshCtrlServerServer Server *ctrlserver.MeshCtrlServer } +// GetMesh: serialise the mesh network into bytes func (m *WgRpc) GetMesh(ctx context.Context, request *rpc.GetMeshRequest) (*rpc.GetMeshReply, error) { mesh := m.Server.MeshManager.GetMesh(request.MeshId) diff --git a/pkg/route/route.go b/pkg/route/route.go index 44376ae..10bb1c6 100644 --- a/pkg/route/route.go +++ b/pkg/route/route.go @@ -5,6 +5,7 @@ import ( "golang.org/x/sys/unix" ) +// RouteInstaller: install the routes to the given interface type RouteInstaller interface { InstallRoutes(devName string, routes ...lib.Route) error } @@ -19,6 +20,8 @@ func (r *RouteInstallerImpl) InstallRoutes(devName string, routes ...lib.Route) return err } + defer rtnl.Close() + err = rtnl.DeleteRoutes(devName, unix.AF_INET6, routes...) if err != nil { diff --git a/pkg/timers/timers.go b/pkg/timers/timers.go deleted file mode 100644 index 390aa9d..0000000 --- a/pkg/timers/timers.go +++ /dev/null @@ -1,15 +0,0 @@ -package timer - -import ( - "github.com/tim-beatham/smegmesh/pkg/ctrlserver" - "github.com/tim-beatham/smegmesh/pkg/lib" - logging "github.com/tim-beatham/smegmesh/pkg/log" -) - -func NewTimestampScheduler(ctrlServer *ctrlserver.MeshCtrlServer) lib.Timer { - timerFunc := func() error { - logging.Log.WriteInfof("Updated Timestamp") - return ctrlServer.MeshManager.UpdateTimeStamp() - } - return *lib.NewTimer(timerFunc, ctrlServer.Conf.HeartBeat) -}