Skip to content

Commit

Permalink
Add RouteChange API
Browse files Browse the repository at this point in the history
Signed-off-by: Maxime Soulé <[email protected]>
  • Loading branch information
maxatome committed Feb 26, 2024
1 parent 6ab7f5a commit 391cc95
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
4 changes: 4 additions & 0 deletions handle_unspecified.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ func (h *Handle) RouteAppend(route *Route) error {
return ErrNotImplemented
}

func (h *Handle) RouteChange(route *Route) error {
return ErrNotImplemented
}

func (h *Handle) RouteDel(route *Route) error {
return ErrNotImplemented
}
Expand Down
4 changes: 4 additions & 0 deletions netlink_unspecified.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ func RouteAppend(route *Route) error {
return ErrNotImplemented
}

func RouteChange(route *Route) error {
return ErrNotImplemented
}

func RouteDel(route *Route) error {
return ErrNotImplemented
}
Expand Down
15 changes: 15 additions & 0 deletions route_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,21 @@ func (h *Handle) RouteAddEcmp(route *Route) error {
return err
}

// RouteChange will add a route to the system.
// Equivalent to: `ip route change $route`
func RouteChange(route *Route) error {
return pkgHandle.RouteChange(route)
}

// RouteChange will add a route to the system.
// Equivalent to: `ip route change $route`
func (h *Handle) RouteChange(route *Route) error {
flags := unix.NLM_F_REPLACE | unix.NLM_F_ACK
req := h.newNetlinkRequest(unix.RTM_NEWROUTE, flags)
_, err := h.routeHandle(route, req, nl.NewRtMsg())
return err
}

// RouteReplace will add a route to the system.
// Equivalent to: `ip route replace $route`
func RouteReplace(route *Route) error {
Expand Down
67 changes: 66 additions & 1 deletion route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,72 @@ func TestRoute6AddDel(t *testing.T) {
}
}

func TestRouteChange(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()

// get loopback interface
link, err := LinkByName("lo")
if err != nil {
t.Fatal(err)
}

// bring the interface up
if err := LinkSetUp(link); err != nil {
t.Fatal(err)
}

// add a gateway route
dst := &net.IPNet{
IP: net.IPv4(192, 168, 0, 0),
Mask: net.CIDRMask(24, 32),
}

ip := net.IPv4(127, 1, 1, 1)
route := Route{LinkIndex: link.Attrs().Index, Dst: dst, Src: ip}

if err := RouteChange(&route); err == nil {
t.Fatal("Route added while it should fail")
}

if err := RouteAdd(&route); err != nil {
t.Fatal(err)
}
routes, err := RouteList(link, FAMILY_V4)
if err != nil {
t.Fatal(err)
}
if len(routes) != 1 {
t.Fatal("Route not added properly")
}

ip = net.IPv4(127, 1, 1, 2)
route = Route{LinkIndex: link.Attrs().Index, Dst: dst, Src: ip}
if err := RouteChange(&route); err != nil {
t.Fatal(err)
}

routes, err = RouteList(link, FAMILY_V4)
if err != nil {
t.Fatal(err)
}

if len(routes) != 1 || !routes[0].Src.Equal(ip) {
t.Fatal("Route not changed properly")
}

if err := RouteDel(&route); err != nil {
t.Fatal(err)
}
routes, err = RouteList(link, FAMILY_V4)
if err != nil {
t.Fatal(err)
}
if len(routes) != 0 {
t.Fatal("Route not removed properly")
}
}

func TestRouteReplace(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
Expand Down Expand Up @@ -390,7 +456,6 @@ func TestRouteReplace(t *testing.T) {
if len(routes) != 0 {
t.Fatal("Route not removed properly")
}

}

func TestRouteAppend(t *testing.T) {
Expand Down

0 comments on commit 391cc95

Please sign in to comment.