package unipede import ( "fmt" "log" "net" "net/http" ) type ( UpConnectedRoute struct { Count int `json:"count" bson:"count"` Errors []Error `json:"errors" bson:"errors"` Items []UpConnectedRouteItem `json:"items" bson:"items"` Links map[string]Links `json:"links" bson:"links"` } UpConnectedRouteItem struct { CIDR int `json:"cidr" bson:"cidr"` Created string `json:"created" bson:"created"` Id int `json:"id" bson:"id"` Interface string `json:"interface" bson:"interface"` InterfaceAddress string `json:"interface_address" bson:"interface_address"` Network string `json:"network" bson:"network"` Succeeded string `json:"succeeded" bson:"succeeded"` Type string `json:"type" bson:"type"` UpDeviceId int `json:"up_device_id" bson:"up_device_id"` Updated string `json:"updated" bson:"updated"` VRF string `json:"vrf" bson:"vrf"` } UpRouteStatic struct { Count int `json:"count" bson:"count"` Errors []*Error `json:"errors" bson:"errors"` Items []*UpRouteItem `json:"items" bson:"items"` Links map[string]Links `json:"links" bson:"links"` } UpRouteItem struct { CIDR int `json:"cidr" bson:"cidr"` Created string `json:"created" bson:"created"` Id int `json:"id" bson:"id"` Interface string `json:"interface" bson:"interface"` Network string `json:"network" bson:"network"` NextHop string `json:"nexthop" bson:"nexthop"` UpDeviceId int `json:"up_device_id" bson:"up_device_id"` Updated string `json:"updated" bson:"updated"` VRF string `json:"vrf" bson:"vrf"` ipNet *net.IPNet } IPRoute struct { NextHop string `json:"next_hop" bson:"next_hop"` Networks []string `json:"networks" bson:"networks"` Recursive []*IPRecursiveError `json:"recursive_routes" bson:"recursive_routes"` Errors []string `json:"errors,omitempty" bson:"errors,omitempty"` } IPRecursiveError struct { Network string `json:"network" bson:"network"` NextHop string `json:"nexthop" bson:"nexthop"` RecursiveNetwork string `json:"recursive_network" bson:"recursive_network"` RecursiveNextHop string `json:"recursive_nexthop" bson:"recursive_nexthop"` } ) func (u *Client) UpConnectedRoutes(switchName string, options ...*UnipedeOptions) (*UpConnectedRoute, error) { o := processOptions(nil, nil, options...) req, err := u.newRequest(unipedeURL+"devices/"+switchName+"/connected_route", http.MethodGet, o, nil) if err != nil { return nil, err } routes := new(UpConnectedRoute) _, err = u.do(req, o, routes) return routes, err } func (u *Client) UpRoutes(switchName string, refresh *int, options ...*UnipedeOptions) (*UpRouteStatic, error) { o := processOptions(refresh, nil, options...) req, err := u.newRequest(unipedeURL+"devices/"+switchName+"/UpRoute::StaticRoute::View", http.MethodGet, o, nil) if err != nil { return nil, err } swconf := new(UpRouteStatic) _, err = u.do(req, o, swconf) return swconf, err } func (u *UpRouteStatic) GetGateway(s string) *IPRoute { return &IPRoute{ NextHop: s, Networks: func() (routes []string) { for _, r := range u.Items { if r.NextHop == s { routes = append(routes, r.Network) } } return }(), } } func (u *UpRouteStatic) GetRecursive(ipr *IPRoute) { u.GenerateIPNet() for _, i := range u.Items { r := ipr.isRecursive(i) if r != nil { ipr.Recursive = append(ipr.Recursive, r) } } } func (ipr *IPRoute) isRecursive(i *UpRouteItem) *IPRecursiveError { if i.ipNet == nil { return nil } if i.Network == "0.0.0.0/0" { return nil } for _, r := range ipr.Networks { _, ipnet, err := net.ParseCIDR(r) if err != nil { return nil } if ipnet.Contains(net.ParseIP(i.NextHop)) { return &IPRecursiveError{ Network: i.Network, NextHop: i.NextHop, RecursiveNetwork: r, RecursiveNextHop: ipr.NextHop, } } } return nil } func (u *UpRouteStatic) FindRecursive(ipr *IPRoute) (errors []string) { u.GenerateIPNet() for _, i := range u.Items { if i.ipNet == nil { continue } if i.Network == "0.0.0.0/0" { continue } for _, r := range ipr.Networks { _, ipnet, err := net.ParseCIDR(r) if err != nil { continue } if ipnet.Contains(net.ParseIP(i.NextHop)) { errors = append(errors, fmt.Sprintf("%s Nexthop %s is a recursive route, %s with nexthop %s", i.Network, i.NextHop, r, ipr.NextHop)) log.Printf("%s Nexthop %s is a recursive route, %s with nexthop %s", i.Network, i.NextHop, r, ipr.NextHop) } } } return } func MaskToString(m []byte) string { if len(m) == 4 { return fmt.Sprintf("%d.%d.%d.%d", m[0], m[1], m[2], m[3]) } return "" } // method to generate net.IPNet for each item func (u *UpRouteStatic) GenerateIPNet() { for _, i := range u.Items { if i.ipNet != nil { continue } _, ipnet, err := net.ParseCIDR(i.Network) if err != nil { log.Println(err) continue } i.ipNet = ipnet } } func (u *UpRouteStatic) GenerateRoutes(nextHop string) (routes []string) { u.GenerateIPNet() for _, i := range u.Items { if nextHop != "" { if nextHop == i.NextHop { routes = append(routes, fmt.Sprintf("ip route %s %s %s", i.ipNet.IP.String(), MaskToString(i.ipNet.Mask), i.NextHop)) } continue } routes = append(routes, fmt.Sprintf("ip route %s %s %s", i.ipNet.IP.String(), MaskToString(i.ipNet.Mask), i.NextHop)) } return } func (u *UpRouteStatic) GenerateNoRoutes(nextHop string) (routes []string) { u.GenerateIPNet() for _, i := range u.Items { if nextHop != "" { if nextHop == i.NextHop { routes = append(routes, fmt.Sprintf("no ip route %s %s %s", i.ipNet.IP.String(), MaskToString(i.ipNet.Mask), i.NextHop)) } continue } routes = append(routes, fmt.Sprintf("no ip route %s %s %s", i.ipNet.IP.String(), MaskToString(i.ipNet.Mask), i.NextHop)) } return } func (u *UpRouteStatic) ReplaceRoutes(nextHop, newNextHop string) (routes []string) { u.GenerateIPNet() for _, i := range u.Items { if nextHop != "" { if nextHop == i.NextHop { routes = append(routes, fmt.Sprintf("ip route %s %s %s", i.ipNet.IP.String(), MaskToString(i.ipNet.Mask), newNextHop)) } continue } routes = append(routes, fmt.Sprintf("ip route %s %s %s", i.ipNet.IP.String(), MaskToString(i.ipNet.Mask), newNextHop)) } return }