Esempio n. 1
0
/* Helper routine to generate a netlink.Route object from string descriptions */
func mkRoute(src, dst, dev, gw string) *netlink.Route {
	_, dst_net, err := net.ParseCIDR(dst)
	if err != nil {
		log.Fatalf("failed to parse route destination %s: %s", dst, err)
	}

	route := netlink.Route{Src: net.ParseIP(extract_ip(src)), Dst: dst_net}

	if dev != "" {
		link, err := netlink.LinkByName(dev)
		if err != nil {
			log.Fatalf("failed to look up outgoing interface %s: %s", dev, err)
		}
		route.LinkIndex = link.Attrs().Index
	}

	if gw != "" {
		route.Gw = net.ParseIP(gw)
	}
	return &route
}
Esempio n. 2
0
File: route.go Progetto: narry/weave
// This code proposed for addition to the vishvananda/netlink library
// deserializeRoute decodes a binary netlink message into a Route struct
func deserializeRoute(m []byte) (netlink.Route, error) {
	route := netlink.Route{}
	msg := nl.DeserializeRtMsg(m)
	attrs, err := nl.ParseRouteAttr(m[msg.Len():])
	if err != nil {
		return route, err
	}
	route.Scope = netlink.Scope(msg.Scope)

	native := nl.NativeEndian()
	for _, attr := range attrs {
		switch attr.Attr.Type {
		case syscall.RTA_GATEWAY:
			route.Gw = net.IP(attr.Value)
		case syscall.RTA_PREFSRC:
			route.Src = net.IP(attr.Value)
		case syscall.RTA_DST:
			route.Dst = &net.IPNet{
				IP:   attr.Value,
				Mask: net.CIDRMask(int(msg.Dst_len), 8*len(attr.Value)),
			}
		case syscall.RTA_OIF:
			routeIndex := int(native.Uint32(attr.Value[0:4]))
			route.LinkIndex = routeIndex
		}
	}
	return route, nil
}