Skip to content

Commit

Permalink
Add route attributes - MTU, AdvMSS, Priority
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Lopintsev <cldlss@yandex-team.ru>
  • Loading branch information
Alexander Lopintsev committed Nov 21, 2023
1 parent cf9ca9e commit e931118
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
32 changes: 24 additions & 8 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,11 @@ func (d *DNS) Copy() *DNS {
}

type Route struct {
Dst net.IPNet
GW net.IP
Dst net.IPNet
GW net.IP
MTU int
AdvMSS int
Priority int
}

func (r *Route) String() string {
Expand All @@ -143,8 +146,11 @@ func (r *Route) Copy() *Route {
}

return &Route{
Dst: r.Dst,
GW: r.GW,
Dst: r.Dst,
GW: r.GW,
MTU: r.MTU,
AdvMSS: r.AdvMSS,
Priority: r.Priority,
}
}

Expand Down Expand Up @@ -195,8 +201,11 @@ func (e *Error) Print() error {

// JSON (un)marshallable types
type route struct {
Dst IPNet `json:"dst"`
GW net.IP `json:"gw,omitempty"`
Dst IPNet `json:"dst"`
GW net.IP `json:"gw,omitempty"`
MTU int `json:"mtu,omitempty"`
AdvMSS int `json:"advmss,omitempty"`
Priority int `json:"priority,omitempty"`
}

func (r *Route) UnmarshalJSON(data []byte) error {
Expand All @@ -207,13 +216,20 @@ func (r *Route) UnmarshalJSON(data []byte) error {

r.Dst = net.IPNet(rt.Dst)
r.GW = rt.GW
r.MTU = rt.MTU
r.AdvMSS = rt.AdvMSS
r.Priority = rt.Priority

return nil
}

func (r Route) MarshalJSON() ([]byte, error) {
rt := route{
Dst: IPNet(r.Dst),
GW: r.GW,
Dst: IPNet(r.Dst),
GW: r.GW,
MTU: r.MTU,
AdvMSS: r.AdvMSS,
Priority: r.Priority,
}

return json.Marshal(rt)
Expand Down
9 changes: 6 additions & 3 deletions pkg/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,17 @@ var _ = Describe("Types", func() {
IP: net.ParseIP("1.2.3.0"),
Mask: net.CIDRMask(24, 32),
},
GW: net.ParseIP("1.2.3.1"),
GW: net.ParseIP("1.2.3.1"),
MTU: 1500,
AdvMSS: 1340,
Priority: 100,
}
})

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" }`))
Expect(jsonBytes).To(MatchJSON(`{ "dst": "1.2.3.0/24", "gw": "1.2.3.1", "mtu": 1500, "advmss": 1340, "priority": 100 }`))

var unmarshaled types.Route
Expect(json.Unmarshal(jsonBytes, &unmarshaled)).To(Succeed())
Expand All @@ -110,7 +113,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}`))
Expect(example.String()).To(Equal(`{Dst:{IP:1.2.3.0 Mask:ffffff00} GW:1.2.3.1 MTU:1500 AdvMSS:1340 Priority:100}`))
})
})

Expand Down

0 comments on commit e931118

Please sign in to comment.