Esempio n. 1
0
func main() {
	// This imitates the functionality of rtnl_wilddump_req_filter from bridge/vlan.c in the
	// iproute2 code. Note that the filter requires kernel 3.9 or newer
	data := make([]byte, 4)
	binary.LittleEndian.PutUint32(data, link.RTEXT_FILTER_BRVLAN)
	lnmsg := link.NewHeader(rtnetlink.AF_BRIDGE, 0, 0, 0, 0)
	msg := rtnetlink.NewMessage(lnmsg, []netlink.Attribute{{link.IFLA_EXT_MASK, data}})
	nlmsg, err := netlink.NewMessage(rtnetlink.RTM_GETLINK, netlink.NLM_F_DUMP|netlink.NLM_F_REQUEST, msg)
	if err != nil {
		log.Panicf("Couldn't construct message: %v", err)
	}
	nlsock, err := netlink.Dial(netlink.NETLINK_ROUTE)
	if err != nil {
		log.Panicf("Couldn't dial netlink: %v", err)
	}
	h := netlink.NewHandler(nlsock)
	ec := make(chan error)
	go h.Start(ec)
	c, err := h.Query(*nlmsg, 1)
	if err != nil {
		log.Panicf("Couldn't write netlink: %v", err)
	}
	for i := range c {
		if i.Header.MessageType() == netlink.NLMSG_DONE {
			break
		}
		switch i.Header.MessageType() {
		case rtnetlink.RTM_NEWLINK:
			hdr := &link.Header{}
			msg := rtnetlink.NewMessage(hdr, nil)
			err = msg.UnmarshalNetlink(i.Body)
			if err == nil {
				log.Printf("Link[%d] (Family: %v; Type: %v; Flags: %v; Changes: %v)",
					hdr.InterfaceIndex(),
					hdr.InterfaceFamily(), hdr.InterfaceType(), hdr.Flags(),
					hdr.InterfaceChanges())
				for i := range msg.Attributes {
					log.Printf("Attribute[%d]: %v", i, msg.Attributes[i])
				}
			} else {
				log.Printf("Unmarshal error: %v", err)
			}
		default:
			log.Printf("Unknown type: %v", i)
		}
	}
}
Esempio n. 2
0
func ParseMessage(msg netlink.Message) (ret *rtnetlink.Message, err error) {
	hdr := &Header{}
	ret = rtnetlink.NewMessage(hdr, nil)
	err = ret.UnmarshalNetlink(msg.Body)
	if err != nil {
		ret = nil
		return
	}
	return
}
Esempio n. 3
0
func main() {
	nlmsg, err := netlink.NewMessage(rtnetlink.RTM_GETADDR, netlink.NLM_F_DUMP|netlink.NLM_F_REQUEST, &addr.Header{})
	if err != nil {
		log.Panicf("Couldn't construct message: %v", err)
	}
	//log.Printf("Dialing: %v", nlmsg)
	nlsock, err := netlink.Dial(netlink.NETLINK_ROUTE)
	if err != nil {
		log.Panicf("Couldn't dial netlink: %v", err)
	}
	h := netlink.NewHandler(nlsock)
	ec := make(chan error)
	go logec(ec)
	go h.Start(ec)
	//log.Printf("Sending query: %v", nlmsg)
	c, err := h.Query(*nlmsg, 1)
	//log.Printf("Sent query: %v", nlmsg.Header)
	if err != nil {
		log.Panicf("Couldn't write netlink: %v", err)
	}
	for i := range c {
		if i.Header.MessageType() == netlink.NLMSG_DONE {
			break
		}
		switch i.Header.MessageType() {
		case rtnetlink.RTM_NEWADDR:
			hdr := &addr.Header{}
			msg := rtnetlink.NewMessage(hdr, nil)
			err = msg.UnmarshalNetlink(i.Body)
			if err == nil {
				log.Printf("Family: %s; Length: %d; Flags: %v; Scope: %v; IFIndex: %d",
					hdr.AddressFamily(), hdr.PrefixLength(), hdr.Flags(), hdr.Scope(),
					hdr.InterfaceIndex())

				for i := range msg.Attributes {
					attr := msg.Attributes[i]
					log.Printf("Attribute[%s] = %v", addr.AttributeTypeStrings[attr.Type], attr.Body)
				}
			} else {
				log.Printf("Unmarshal error: %v", err)
			}
		default:
			log.Printf("Unknown type: %v", i)
		}
		if err != nil {
			log.Printf("Handler error: %v", err)
		}
	}
}
Esempio n. 4
0
func main() {
	nlmsg, err := netlink.NewMessage(xfrm.XFRM_MSG_GETPOLICY, netlink.NLM_F_DUMP|netlink.NLM_F_REQUEST, &link.Header{})
	if err != nil {
		log.Panicf("Couldn't construct message: %v", err)
	}
	nlsock, err := netlink.Dial(netlink.NETLINK_XFRM)
	if err != nil {
		log.Panicf("Couldn't dial netlink: %v", err)
	}
	h := netlink.NewHandler(nlsock)
	ec := make(chan error)
	go h.Start(ec)
	c, err := h.Query(*nlmsg, 1)
	if err != nil {
		log.Panicf("Couldn't write netlink: %v", err)
	}
	for i := range c {
		if i.Header.MessageType() == netlink.NLMSG_DONE {
			break
		}
		if i.Header.MessageType() == netlink.NLMSG_ERROR {
			emsg := &netlink.Error{}
			err = emsg.UnmarshalNetlink(i.Body)
			if err == nil && emsg.Code() != 0 {
				log.Printf("Netlink error: %v", emsg.Error())
			}
			break
		}
		switch i.Header.MessageType() {
		case xfrm.XFRM_MSG_NEWPOLICY:
			hdr := &xfrm.Header{}
			msg := rtnetlink.NewMessage(hdr, nil)
			err = msg.UnmarshalNetlink(i.Body)
			if err == nil {
				log.Printf("Policy: %v", hdr)
				for i := range msg.Attributes {
					log.Printf("Attribute[%d]: %v", i, msg.Attributes[i])
				}
			} else {
				log.Printf("Unmarshal error: %v", err)
			}
		default:
			log.Printf("Unknown type: %v", i)
		}
	}
}
Esempio n. 5
0
func main() {
	nlmsg, err := netlink.NewMessage(rtnetlink.RTM_GETROUTE, netlink.NLM_F_DUMP|netlink.NLM_F_REQUEST, &link.Header)
	if err != nil {
		log.Panicf("Couldn't construct message: %v", err)
	}
	nlsock, err := netlink.Dial(netlink.NETLINK_ROUTE)
	if err != nil {
		log.Panicf("Couldn't dial netlink: %v", err)
	}
	h := netlink.NewHandler(nlsock)
	ec := make(chan error)
	go h.Start(ec)
	c, err := h.Query(*nlmsg, 1)
	if err != nil {
		log.Panicf("Couldn't write netlink: %v", err)
	}
	for i := range c {
		if i.Header.MessageType() == netlink.NLMSG_DONE {
			break
		}
		switch i.Header.MessageType() {
		case rtnetlink.RTM_NEWROUTE:
			hdr := &route.Header{}
			msg := rtnetlink.NewMessage(hdr, nil)
			err = msg.UnmarshalNetlink(i.Body)

			if err == nil {
				log.Printf("Route: %v (%d/%d) TOS: %d; (Table: %v; Origin: %v; Scope: %v; Type: %v; Flags: %v",
					hdr.AddressFamily(), hdr.AddressDestLength(), hdr.AddressSourceLength(),
					hdr.TOS(), hdr.RoutingTable(), hdr.RouteOrigin(), hdr.AddressScope(),
					hdr.RouteType(), hdr.Flags())
				for i := range msg.Attributes {
					log.Printf("Attribute[%d]: (%v) %v", i, route.AttributeTypeStrings[msg.Attributes[i].Type], msg.Attributes[i])
				}
			} else {
				log.Printf("Unmarshal error: %v", err)
			}
		default:
			log.Printf("Unknown type: %v", i)
		}
	}
}
Esempio n. 6
0
func main() {
	nlmsg, err := netlink.NewMessage(rtnetlink.RTM_GETLINK, netlink.NLM_F_DUMP|netlink.NLM_F_REQUEST, &link.Header{})
	if err != nil {
		log.Panicf("Couldn't construct message: %v", err)
	}
	nlsock, err := netlink.Dial(netlink.NETLINK_ROUTE)
	if err != nil {
		log.Panicf("Couldn't dial netlink: %v", err)
	}
	h := netlink.NewHandler(nlsock)
	ec := make(chan error)
	go h.Start(ec)
	c, err := h.Query(*nlmsg, 1)
	if err != nil {
		log.Panicf("Couldn't write netlink: %v", err)
	}
	for i := range c {
		if i.Header.MessageType() == netlink.NLMSG_DONE {
			break
		}
		switch i.Header.MessageType() {
		case rtnetlink.RTM_NEWLINK:
			hdr := &link.Header{}
			msg := rtnetlink.NewMessage(hdr, nil)
			err = msg.UnmarshalNetlink(i.Body)
			if err == nil {
				log.Printf("Link[%d] (Family: %v; Type: %v; Flags: %v; Changes: %v)",
					hdr.InterfaceIndex(),
					hdr.InterfaceFamily(), hdr.InterfaceType(), hdr.Flags(),
					hdr.InterfaceChanges())
				for i := range msg.Attributes {
					log.Printf("Attribute[%d]: %v", i, msg.Attributes[i])
				}
			} else {
				log.Printf("Unmarshal error: %v", err)
			}
		default:
			log.Printf("Unknown type: %v", i)
		}
	}
}