From a4d4a0d1ff21186d931e3f89a49cb57241edf121 Mon Sep 17 00:00:00 2001 From: Lionel Jouin Date: Sat, 17 Feb 2024 23:37:22 +0100 Subject: [PATCH 1/2] Add Scope property for routes Fixes: #598 Signed-off-by: Lionel Jouin --- SPEC.md | 1 + pkg/types/types.go | 7 ++++++- pkg/types/types_test.go | 5 +++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/SPEC.md b/SPEC.md index a6235af2..dcd10783 100644 --- a/SPEC.md +++ b/SPEC.md @@ -581,6 +581,7 @@ Plugins must output a JSON object with the following keys upon a successful `ADD - `advmss` (uint): The MSS (Maximal Segment Size) to advertise to these destinations when establishing TCP connections. - `priority` (uint): The priority of route, lower is higher. - `table` (uint): The table to add the route to. + - `scope` (uint): The scope of the destinations covered by the route prefix (global (0), link (253), host (254)). - `dns`: a dictionary consisting of DNS configuration information - `nameservers` (list of strings): list of a priority-ordered list of DNS nameservers that this network is aware of. Each entry in the list is a string containing either an IPv4 or an IPv6 address. - `domain` (string): the local domain used for short hostname lookups. diff --git a/pkg/types/types.go b/pkg/types/types.go index ea307e4a..0547cc67 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -171,6 +171,7 @@ type Route struct { AdvMSS int Priority int Table *int + Scope int } func (r *Route) String() string { @@ -179,7 +180,7 @@ func (r *Route) String() string { table = fmt.Sprintf("%d", *r.Table) } - return fmt.Sprintf("{Dst:%+v GW:%v MTU:%d AdvMSS:%d Priority:%d Table:%s}", r.Dst, r.GW, r.MTU, r.AdvMSS, r.Priority, table) + return fmt.Sprintf("{Dst:%+v GW:%v MTU:%d AdvMSS:%d Priority:%d Table:%s Scope:%d}", r.Dst, r.GW, r.MTU, r.AdvMSS, r.Priority, table, r.Scope) } func (r *Route) Copy() *Route { @@ -193,6 +194,7 @@ func (r *Route) Copy() *Route { MTU: r.MTU, AdvMSS: r.AdvMSS, Priority: r.Priority, + Scope: r.Scope, } if r.Table != nil { @@ -256,6 +258,7 @@ type route struct { AdvMSS int `json:"advmss,omitempty"` Priority int `json:"priority,omitempty"` Table *int `json:"table,omitempty"` + Scope int `json:"scope,omitempty"` } func (r *Route) UnmarshalJSON(data []byte) error { @@ -270,6 +273,7 @@ func (r *Route) UnmarshalJSON(data []byte) error { r.AdvMSS = rt.AdvMSS r.Priority = rt.Priority r.Table = rt.Table + r.Scope = rt.Scope return nil } @@ -282,6 +286,7 @@ func (r Route) MarshalJSON() ([]byte, error) { AdvMSS: r.AdvMSS, Priority: r.Priority, Table: r.Table, + Scope: r.Scope, } return json.Marshal(rt) diff --git a/pkg/types/types_test.go b/pkg/types/types_test.go index 149c2bb0..33ec1c53 100644 --- a/pkg/types/types_test.go +++ b/pkg/types/types_test.go @@ -93,13 +93,14 @@ var _ = Describe("Types", func() { AdvMSS: 1340, Priority: 100, Table: types040.Int(50), + Scope: 253, } }) It("marshals and unmarshals to JSON", func() { jsonBytes, err := json.Marshal(example) Expect(err).NotTo(HaveOccurred()) - Expect(jsonBytes).To(MatchJSON(`{ "dst": "1.2.3.0/24", "gw": "1.2.3.1", "mtu": 1500, "advmss": 1340, "priority": 100, "table": 50 }`)) + Expect(jsonBytes).To(MatchJSON(`{ "dst": "1.2.3.0/24", "gw": "1.2.3.1", "mtu": 1500, "advmss": 1340, "priority": 100, "table": 50, "scope": 253 }`)) var unmarshaled types.Route Expect(json.Unmarshal(jsonBytes, &unmarshaled)).To(Succeed()) @@ -115,7 +116,7 @@ var _ = Describe("Types", func() { }) It("formats as a string with a hex mask", func() { - Expect(example.String()).To(Equal(`{Dst:{IP:1.2.3.0 Mask:ffffff00} GW:1.2.3.1 MTU:1500 AdvMSS:1340 Priority:100 Table:50}`)) + Expect(example.String()).To(Equal(`{Dst:{IP:1.2.3.0 Mask:ffffff00} GW:1.2.3.1 MTU:1500 AdvMSS:1340 Priority:100 Table:50 Scope:253}`)) }) }) From 00576a2e55bfb167d81924d97bcf66ca671f6534 Mon Sep 17 00:00:00 2001 From: Lionel Jouin Date: Mon, 19 Feb 2024 18:57:59 +0100 Subject: [PATCH 2/2] Scope property as int pointer Signed-off-by: Lionel Jouin --- pkg/types/types.go | 16 +++++++++++++--- pkg/types/types_test.go | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/pkg/types/types.go b/pkg/types/types.go index 0547cc67..ed7a1bd9 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -171,7 +171,7 @@ type Route struct { AdvMSS int Priority int Table *int - Scope int + Scope *int } func (r *Route) String() string { @@ -180,7 +180,12 @@ func (r *Route) String() string { table = fmt.Sprintf("%d", *r.Table) } - return fmt.Sprintf("{Dst:%+v GW:%v MTU:%d AdvMSS:%d Priority:%d Table:%s Scope:%d}", r.Dst, r.GW, r.MTU, r.AdvMSS, r.Priority, table, r.Scope) + scope := "" + if r.Scope != nil { + scope = fmt.Sprintf("%d", *r.Scope) + } + + return fmt.Sprintf("{Dst:%+v GW:%v MTU:%d AdvMSS:%d Priority:%d Table:%s Scope:%s}", r.Dst, r.GW, r.MTU, r.AdvMSS, r.Priority, table, scope) } func (r *Route) Copy() *Route { @@ -202,6 +207,11 @@ func (r *Route) Copy() *Route { route.Table = &table } + if r.Scope != nil { + scope := *r.Scope + route.Scope = &scope + } + return route } @@ -258,7 +268,7 @@ type route struct { AdvMSS int `json:"advmss,omitempty"` Priority int `json:"priority,omitempty"` Table *int `json:"table,omitempty"` - Scope int `json:"scope,omitempty"` + Scope *int `json:"scope,omitempty"` } func (r *Route) UnmarshalJSON(data []byte) error { diff --git a/pkg/types/types_test.go b/pkg/types/types_test.go index 33ec1c53..73cd9ec6 100644 --- a/pkg/types/types_test.go +++ b/pkg/types/types_test.go @@ -93,7 +93,7 @@ var _ = Describe("Types", func() { AdvMSS: 1340, Priority: 100, Table: types040.Int(50), - Scope: 253, + Scope: types040.Int(253), } })