Skip to content

Commit

Permalink
Merge pull request #1068 from LionelJouin/scope
Browse files Browse the repository at this point in the history
Add Scope property for routes
  • Loading branch information
squeed committed Mar 5, 2024
2 parents 06d9dec + 00576a2 commit 845a737
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 16 additions & 1 deletion pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ type Route struct {
AdvMSS int
Priority int
Table *int
Scope *int
}

func (r *Route) String() string {
Expand All @@ -179,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}", r.Dst, r.GW, r.MTU, r.AdvMSS, r.Priority, table)
scope := "<nil>"
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 {
Expand All @@ -193,13 +199,19 @@ func (r *Route) Copy() *Route {
MTU: r.MTU,
AdvMSS: r.AdvMSS,
Priority: r.Priority,
Scope: r.Scope,
}

if r.Table != nil {
table := *r.Table
route.Table = &table
}

if r.Scope != nil {
scope := *r.Scope
route.Scope = &scope
}

return route
}

Expand Down Expand Up @@ -256,6 +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"`
}

func (r *Route) UnmarshalJSON(data []byte) error {
Expand All @@ -270,6 +283,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
}
Expand All @@ -282,6 +296,7 @@ func (r Route) MarshalJSON() ([]byte, error) {
AdvMSS: r.AdvMSS,
Priority: r.Priority,
Table: r.Table,
Scope: r.Scope,
}

return json.Marshal(rt)
Expand Down
5 changes: 3 additions & 2 deletions pkg/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,14 @@ var _ = Describe("Types", func() {
AdvMSS: 1340,
Priority: 100,
Table: types040.Int(50),
Scope: types040.Int(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())
Expand All @@ -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}`))
})
})

Expand Down

0 comments on commit 845a737

Please sign in to comment.