pax_global_header00006660000000000000000000000064141542304240014511gustar00rootroot0000000000000052 comment=030878314e387340f2b49b161519600024f94bb9 genetlink-1.1.0/000077500000000000000000000000001415423042400134705ustar00rootroot00000000000000genetlink-1.1.0/.github/000077500000000000000000000000001415423042400150305ustar00rootroot00000000000000genetlink-1.1.0/.github/workflows/000077500000000000000000000000001415423042400170655ustar00rootroot00000000000000genetlink-1.1.0/.github/workflows/linux-test.yml000066400000000000000000000007621415423042400217310ustar00rootroot00000000000000name: Linux Test on: push: branches: - main pull_request: branches: - '*' jobs: build: strategy: matrix: go-version: [1.12, 1.17] runs-on: ubuntu-latest steps: - name: Set up Go uses: actions/setup-go@v2 with: go-version: ${{ matrix.go-version }} id: go - name: Check out code into the Go module directory uses: actions/checkout@v2 - name: Run tests run: go test -v -race -tags gofuzz ./... genetlink-1.1.0/.github/workflows/macos-test.yml000066400000000000000000000007361415423042400216750ustar00rootroot00000000000000name: macOS Test on: push: branches: - main pull_request: branches: - '*' jobs: build: strategy: matrix: go-version: [1.17] runs-on: macos-latest steps: - name: Set up Go uses: actions/setup-go@v2 with: go-version: ${{ matrix.go-version }} id: go - name: Check out code into the Go module directory uses: actions/checkout@v2 - name: Run tests run: go test -v -race ./... genetlink-1.1.0/.github/workflows/static-analysis.yml000066400000000000000000000012661415423042400227250ustar00rootroot00000000000000name: Static Analysis on: push: branches: - main pull_request: branches: - '*' jobs: build: strategy: matrix: go-version: [1.17] runs-on: ubuntu-latest steps: - name: Set up Go uses: actions/setup-go@v2 with: go-version: ${{ matrix.go-version }} id: go - name: Check out code into the Go module directory uses: actions/checkout@v2 - name: Install staticcheck run: go install honnef.co/go/tools/cmd/staticcheck - name: Print staticcheck version run: staticcheck -version - name: Run staticcheck run: staticcheck ./... - name: Run go vet run: go vet ./... genetlink-1.1.0/CHANGELOG.md000066400000000000000000000004401415423042400152770ustar00rootroot00000000000000# CHANGELOG ## v1.1.0 **This is the first release of package genetlink that only supports Go 1.12+. Users on older versions must use v1.0.0.** - [Improvement]: modernization of various parts of the code and documentation in prep for future work. ## v1.0.0 - Initial stable commit. genetlink-1.1.0/LICENSE.md000066400000000000000000000020751415423042400151000ustar00rootroot00000000000000MIT License =========== Copyright (C) 2016-2017 Matt Layher Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. genetlink-1.1.0/README.md000066400000000000000000000026161415423042400147540ustar00rootroot00000000000000# genetlink [![Test Status](https://github.com/mdlayher/genetlink/workflows/Linux%20Test/badge.svg)](https://github.com/mdlayher/genetlink/actions) [![Go Reference](https://pkg.go.dev/badge/github.com/mdlayher/genetlink.svg)](https://pkg.go.dev/github.com/mdlayher/genetlink) [![Go Report Card](https://goreportcard.com/badge/github.com/mdlayher/genetlink)](https://goreportcard.com/report/github.com/mdlayher/genetlink) Package `genetlink` implements generic netlink interactions and data types. MIT Licensed. For more information about how netlink and generic netlink work, check out my blog series on [Linux, Netlink, and Go](https://mdlayher.com/blog/linux-netlink-and-go-part-1-netlink/). If you have any questions or you'd like some guidance, please join us on [Gophers Slack](https://invite.slack.golangbridge.org) in the `#networking` channel! ## Stability See the [CHANGELOG](./CHANGELOG.md) file for a description of changes between releases. This package has a stable v1 API and any future breaking changes will prompt the release of a new major version. Features and bug fixes will continue to occur in the v1.x.x series. In order to reduce the maintenance burden, this package is only supported on Go 1.12+. Older versions of Go lack critical features and APIs which are necessary for this package to function correctly. **If you depend on this package in your applications, please use Go modules.** genetlink-1.1.0/conn.go000066400000000000000000000144671415423042400147700ustar00rootroot00000000000000package genetlink import ( "syscall" "time" "github.com/mdlayher/netlink" "golang.org/x/net/bpf" ) // Protocol is the netlink protocol constant used to specify generic netlink. const Protocol = 0x10 // unix.NETLINK_GENERIC // A Conn is a generic netlink connection. A Conn can be used to send and // receive generic netlink messages to and from netlink. // // A Conn is safe for concurrent use, but to avoid contention in // high-throughput applications, the caller should almost certainly create a // pool of Conns and distribute them among workers. type Conn struct { // Operating system-specific netlink connection. c *netlink.Conn } // Dial dials a generic netlink connection. Config specifies optional // configuration for the underlying netlink connection. If config is // nil, a default configuration will be used. func Dial(config *netlink.Config) (*Conn, error) { c, err := netlink.Dial(Protocol, config) if err != nil { return nil, err } return NewConn(c), nil } // NewConn creates a Conn that wraps an existing *netlink.Conn for // generic netlink communications. // // NewConn is primarily useful for tests. Most applications should use // Dial instead. func NewConn(c *netlink.Conn) *Conn { return &Conn{c: c} } // Close closes the connection and unblocks any pending read operations. func (c *Conn) Close() error { return c.c.Close() } // GetFamily retrieves a generic netlink family with the specified name. // // If the family does not exist, the error value can be checked using // `errors.Is(err, os.ErrNotExist)`. func (c *Conn) GetFamily(name string) (Family, error) { return c.getFamily(name) } // ListFamilies retrieves all registered generic netlink families. func (c *Conn) ListFamilies() ([]Family, error) { return c.listFamilies() } // JoinGroup joins a netlink multicast group by its ID. func (c *Conn) JoinGroup(group uint32) error { return c.c.JoinGroup(group) } // LeaveGroup leaves a netlink multicast group by its ID. func (c *Conn) LeaveGroup(group uint32) error { return c.c.LeaveGroup(group) } // SetBPF attaches an assembled BPF program to a Conn. func (c *Conn) SetBPF(filter []bpf.RawInstruction) error { return c.c.SetBPF(filter) } // RemoveBPF removes a BPF filter from a Conn. func (c *Conn) RemoveBPF() error { return c.c.RemoveBPF() } // SetOption enables or disables a netlink socket option for the Conn. func (c *Conn) SetOption(option netlink.ConnOption, enable bool) error { return c.c.SetOption(option, enable) } // SetReadBuffer sets the size of the operating system's receive buffer // associated with the Conn. func (c *Conn) SetReadBuffer(bytes int) error { return c.c.SetReadBuffer(bytes) } // SetWriteBuffer sets the size of the operating system's transmit buffer // associated with the Conn. func (c *Conn) SetWriteBuffer(bytes int) error { return c.c.SetWriteBuffer(bytes) } // SyscallConn returns a raw network connection. This implements the // syscall.Conn interface. // // SyscallConn is intended for advanced use cases, such as getting and setting // arbitrary socket options using the netlink socket's file descriptor. // // Once invoked, it is the caller's responsibility to ensure that operations // performed using Conn and the syscall.RawConn do not conflict with // each other. func (c *Conn) SyscallConn() (syscall.RawConn, error) { return c.c.SyscallConn() } // SetDeadline sets the read and write deadlines associated with the connection. func (c *Conn) SetDeadline(t time.Time) error { return c.c.SetDeadline(t) } // SetReadDeadline sets the read deadline associated with the connection. func (c *Conn) SetReadDeadline(t time.Time) error { return c.c.SetReadDeadline(t) } // SetWriteDeadline sets the write deadline associated with the connection. func (c *Conn) SetWriteDeadline(t time.Time) error { return c.c.SetWriteDeadline(t) } // Send sends a single Message to netlink, wrapping it in a netlink.Message // using the specified generic netlink family and flags. On success, Send // returns a copy of the netlink.Message with all parameters populated, for // later validation. func (c *Conn) Send(m Message, family uint16, flags netlink.HeaderFlags) (netlink.Message, error) { nm, err := packMessage(m, family, flags) if err != nil { return netlink.Message{}, err } reqnm, err := c.c.Send(nm) if err != nil { return netlink.Message{}, err } return reqnm, nil } // Receive receives one or more Messages from netlink. The netlink.Messages // used to wrap each Message are available for later validation. func (c *Conn) Receive() ([]Message, []netlink.Message, error) { msgs, err := c.c.Receive() if err != nil { return nil, nil, err } gmsgs, err := unpackMessages(msgs) if err != nil { return nil, nil, err } return gmsgs, msgs, nil } // Execute sends a single Message to netlink using Send, receives one or more // replies using Receive, and then checks the validity of the replies against // the request using netlink.Validate. // // Execute acquires a lock for the duration of the function call which blocks // concurrent calls to Send and Receive, in order to ensure consistency between // generic netlink request/reply messages. // // See the documentation of Send, Receive, and netlink.Validate for details // about each function. func (c *Conn) Execute(m Message, family uint16, flags netlink.HeaderFlags) ([]Message, error) { nm, err := packMessage(m, family, flags) if err != nil { return nil, err } // Locking behavior handled by netlink.Conn.Execute. msgs, err := c.c.Execute(nm) if err != nil { return nil, err } return unpackMessages(msgs) } // packMessage packs a generic netlink Message into a netlink.Message with the // appropriate generic netlink family and netlink flags. func packMessage(m Message, family uint16, flags netlink.HeaderFlags) (netlink.Message, error) { nm := netlink.Message{ Header: netlink.Header{ Type: netlink.HeaderType(family), Flags: flags, }, } mb, err := m.MarshalBinary() if err != nil { return netlink.Message{}, err } nm.Data = mb return nm, nil } // unpackMessages unpacks generic netlink Messages from a slice of netlink.Messages. func unpackMessages(msgs []netlink.Message) ([]Message, error) { gmsgs := make([]Message, 0, len(msgs)) for _, nm := range msgs { var gm Message if err := (&gm).UnmarshalBinary(nm.Data); err != nil { return nil, err } gmsgs = append(gmsgs, gm) } return gmsgs, nil } genetlink-1.1.0/conn_linux_integration_go1.13_test.go000066400000000000000000000076011415423042400226310ustar00rootroot00000000000000//go:build linux && go1.13 // +build linux,go1.13 package genetlink_test import ( "errors" "fmt" "net" "os" "testing" "github.com/mdlayher/genetlink" "github.com/mdlayher/netlink" ) func TestIntegrationConnGetFamilyIsNotExist(t *testing.T) { // Test that the documented behavior of returning an error that is compatible // with netlink.IsNotExist is correct. const name = "NOTEXISTS" c, err := genetlink.Dial(nil) if err != nil { t.Fatalf("failed to dial generic netlink: %v", err) } if _, err := c.GetFamily(name); !errors.Is(err, os.ErrNotExist) { t.Fatalf("expected not exist error, got: %v", err) } if err := c.Close(); err != nil { t.Fatalf("error closing netlink connection: %v", err) } } func TestIntegrationConnGetFamily(t *testing.T) { c, err := genetlink.Dial(nil) if err != nil { t.Fatalf("failed to dial generic netlink: %v", err) } const name = "nlctrl" family, err := c.GetFamily(name) if err != nil { // nlctrl *should* always exist in order for genetlink to work at all. if errors.Is(err, os.ErrNotExist) { t.Fatal(errNLCtrlMissing) } t.Fatalf("failed to query for family: %v", err) } if err := c.Close(); err != nil { t.Fatalf("error closing netlink connection: %v", err) } if want, got := name, family.Name; want != got { t.Fatalf("unexpected family name:\n- want: %q\n- got: %q", want, got) } } func TestIntegrationConnNL80211(t *testing.T) { c, err := genetlink.Dial(nil) if err != nil { t.Fatalf("failed to dial generic netlink: %v", err) } const ( name = "nl80211" nl80211CommandGetInterface = 5 nl80211AttributeInterfaceIndex = 3 nl80211AttributeInterfaceName = 4 nl80211AttributeAttributeMAC = 6 ) family, err := c.GetFamily(name) if err != nil { if errors.Is(err, os.ErrNotExist) { t.Skipf("skipping because %q family not available", name) } t.Fatalf("failed to query for family: %v", err) } req := genetlink.Message{ Header: genetlink.Header{ Command: nl80211CommandGetInterface, Version: family.Version, }, } flags := netlink.Request | netlink.Dump msgs, err := c.Execute(req, family.ID, flags) if err != nil { t.Fatalf("failed to execute: %v", err) } if err := c.Close(); err != nil { t.Fatalf("error closing netlink connection: %v", err) } type ifInfo struct { Index int Name string MAC net.HardwareAddr } var infos []ifInfo for _, m := range msgs { ad, err := netlink.NewAttributeDecoder(m.Data) if err != nil { t.Fatalf("failed to create attribute decoder: %v", err) } var info ifInfo for ad.Next() { switch ad.Type() { case nl80211AttributeInterfaceIndex: info.Index = int(ad.Uint32()) case nl80211AttributeInterfaceName: info.Name = ad.String() case nl80211AttributeAttributeMAC: ad.Do(func(b []byte) error { if l := len(b); l != 6 { return fmt.Errorf("unexpected MAC length: %d", l) } info.MAC = net.HardwareAddr(b) return nil }) } } if err := ad.Err(); err != nil { t.Fatalf("failed to decode attributes: %v", err) } infos = append(infos, info) } // Verify that nl80211 reported the same information as package net for _, info := range infos { // TODO(mdlayher): figure out why nl80211 returns a subdevice with // an empty name on newer kernel if info.Name == "" { continue } ifi, err := net.InterfaceByName(info.Name) if err != nil { t.Fatalf("error retrieving interface %q: %v", info.Name, err) } if want, got := ifi.Index, info.Index; want != got { t.Fatalf("unexpected interface index for %q:\n- want: %v\n- got: %v", ifi.Name, want, got) } if want, got := ifi.Name, info.Name; want != got { t.Fatalf("unexpected interface name:\n- want: %q\n- got: %q", want, got) } if want, got := ifi.HardwareAddr.String(), info.MAC.String(); want != got { t.Fatalf("unexpected interface MAC for %q:\n- want: %q\n- got: %q", ifi.Name, want, got) } } } genetlink-1.1.0/conn_linux_integration_test.go000066400000000000000000000077661415423042400216550ustar00rootroot00000000000000//go:build linux // +build linux package genetlink_test import ( "errors" "fmt" "os" "sync" "testing" "time" "github.com/google/go-cmp/cmp" "github.com/mdlayher/genetlink" "github.com/mdlayher/netlink" "golang.org/x/sys/unix" ) var errNLCtrlMissing = errors.New("family nlctrl appears to not exist, so " + "generic netlink is not functional on this machine; if you believe you " + "have received this message in error, please file an issue at " + "https://github.com/mdlayher/genetlink") func TestIntegrationConnListFamilies(t *testing.T) { c, err := genetlink.Dial(nil) if err != nil { t.Fatalf("failed to dial generic netlink: %v", err) } families, err := c.ListFamilies() if err != nil { t.Fatalf("failed to query for families: %v", err) } if err := c.Close(); err != nil { t.Fatalf("error closing netlink connection: %v", err) } // Should be at least nlctrl present var found bool const name = "nlctrl" for _, f := range families { if f.Name == name { found = true } } if !found { t.Fatal(errNLCtrlMissing) } } func TestIntegrationConnConcurrentRaceFree(t *testing.T) { c, err := genetlink.Dial(nil) if err != nil { t.Fatalf("failed to dial generic netlink: %v", err) } execN := func(n int) { for i := 0; i < n; i++ { // Don't expect a "valid" request/reply because we are not serializing // our Send/Receive calls via Execute or with an external lock. // // Just verify that we don't trigger the race detector, we got a // valid netlink response, and it can be decoded as a valid // netlink message. if _, err := c.Send(genetlink.Message{}, 0, netlink.Request|netlink.Acknowledge); err != nil { panicf("failed to send request: %v", err) } msgs, _, err := c.Receive() if err != nil { panicf("failed to receive reply: %v", err) } if l := len(msgs); l != 1 { panicf("unexpected number of reply messages: %d", l) } } } const ( workers = 16 iterations = 10000 ) var wg sync.WaitGroup wg.Add(workers) defer wg.Wait() for i := 0; i < workers; i++ { go func() { defer wg.Done() execN(iterations) }() } } func TestIntegrationConnConcurrentReceiveClose(t *testing.T) { c, err := genetlink.Dial(nil) if err != nil { t.Fatalf("failed to dial generic netlink: %v", err) } // Verify this test cannot block indefinitely due to Receive hanging after // a call to Close. timer := time.AfterFunc(10*time.Second, func() { panic("test took too long") }) defer timer.Stop() var wg sync.WaitGroup wg.Add(1) defer wg.Wait() go func() { defer wg.Done() _, _, err := c.Receive() if err == nil { panicf("expected an error, but none occurred") } // Expect an error due to file descriptor being closed. serr := err.(*netlink.OpError).Err.(*os.SyscallError).Err if diff := cmp.Diff(unix.EBADF, serr); diff != "" { panicf("unexpected error from receive (-want +got):\n%s", diff) } }() if err := c.Close(); err != nil { t.Fatalf("failed to close: %v", err) } } func TestIntegrationConnConcurrentSerializeExecute(t *testing.T) { c, err := genetlink.Dial(nil) if err != nil { t.Fatalf("failed to dial generic netlink: %v", err) } execN := func(n int, family string) { for i := 0; i < n; i++ { // GetFamily will internally call Execute to ensure its // request/response transaction is serialized appropriately, and // any errors doing so will be reported here. f, err := c.GetFamily(family) if err != nil { panicf("failed to get family %q: %v", family, err) } if diff := cmp.Diff(family, f.Name); diff != "" { panicf("unexpected family name (-want +got):\n%s", diff) } } } const iterations = 2000 // Pick families that are likely to exist on any given system. families := []string{ "nlctrl", "acpi_event", } var wg sync.WaitGroup wg.Add(len(families)) defer wg.Wait() for _, f := range families { go func(f string) { defer wg.Done() execN(iterations, f) }(f) } } func panicf(format string, a ...interface{}) { panic(fmt.Sprintf(format, a...)) } genetlink-1.1.0/conn_linux_test.go000066400000000000000000000063161415423042400172400ustar00rootroot00000000000000//go:build linux // +build linux package genetlink_test import ( "encoding" "fmt" "testing" "github.com/google/go-cmp/cmp" "github.com/mdlayher/genetlink" "github.com/mdlayher/genetlink/genltest" "github.com/mdlayher/netlink" "github.com/mdlayher/netlink/nltest" "golang.org/x/sys/unix" ) func TestConnExecute(t *testing.T) { req := genetlink.Message{ Header: genetlink.Header{ Command: 1, Version: 1, }, } wantnl := netlink.Message{ Header: netlink.Header{ Length: 20, Type: unix.GENL_ID_CTRL, Flags: netlink.Request, PID: nltest.PID, }, Data: mustMarshal(req), } wantgenl := []genetlink.Message{{ Header: genetlink.Header{ Command: 1, Version: 1, }, Data: []byte{0x01, 0x02, 0x03, 0x04}, }} c := genltest.Dial(func(_ genetlink.Message, nreq netlink.Message) ([]genetlink.Message, error) { if diff := diffNetlinkMessages(wantnl, nreq); diff != "" { t.Fatalf("unexpected sent netlink message (-want +got):\n%s", diff) } return wantgenl, nil }) msgs, err := c.Execute(req, unix.GENL_ID_CTRL, netlink.Request) if err != nil { t.Fatalf("failed to execute: %v", err) } if diff := cmp.Diff(wantgenl, msgs); diff != "" { t.Fatalf("unexpected replies (-want +got):\n%s", diff) } } func TestConnSend(t *testing.T) { const ( length = 24 family = unix.GENL_ID_CTRL flags = netlink.Request ) req := genetlink.Message{ Header: genetlink.Header{ Command: 1, Version: 1, }, Data: []byte{0x00, 0x01, 0x02, 0x03}, } want := netlink.Message{ Header: netlink.Header{ Length: length, Type: family, Flags: flags, PID: nltest.PID, }, Data: mustMarshal(req), } c := genltest.Dial(func(_ genetlink.Message, nreq netlink.Message) ([]genetlink.Message, error) { if diff := diffNetlinkMessages(want, nreq); diff != "" { t.Fatalf("unexpected sent netlink message (-want +got):\n%s", diff) } return nil, nil }) nlreq, err := c.Send(req, family, flags) if err != nil { t.Fatalf("failed to send: %v", err) } if diff := diffNetlinkMessages(want, nlreq); diff != "" { t.Fatalf("unexpected returned netlink message (-want +got):\n%s", diff) } } func TestConnReceive(t *testing.T) { gmsgs := []genetlink.Message{ { Header: genetlink.Header{ Command: 1, Version: 1, }, Data: make([]byte, 0), }, { Header: genetlink.Header{ Command: 2, Version: 1, }, Data: []byte{ 0x01, 0x02, 0x03, 0x04, }, }, } c := genltest.Dial(func(_ genetlink.Message, _ netlink.Message) ([]genetlink.Message, error) { return gmsgs, nil }) msgs, _, err := c.Receive() if err != nil { t.Fatalf("failed to receive messages: %v", err) } if diff := cmp.Diff(gmsgs, msgs); diff != "" { t.Fatalf("unexpected replies (-want +got):\n%s", diff) } } func mustMarshal(m encoding.BinaryMarshaler) []byte { b, err := m.MarshalBinary() if err != nil { panic(fmt.Sprintf("failed to marshal binary: %v", err)) } return b } // diffNetlinkMessages compares two netlink.Messages after zeroing their // sequence number fields that make equality checks in testing difficult. func diffNetlinkMessages(want, got netlink.Message) string { want.Header.Sequence = 0 got.Header.Sequence = 0 return cmp.Diff(want, got) } genetlink-1.1.0/doc.go000066400000000000000000000004061415423042400145640ustar00rootroot00000000000000// Package genetlink implements generic netlink interactions and data types. // // If you have any questions or you'd like some guidance, please join us on // Gophers Slack (https://invite.slack.golangbridge.org) in the #networking // channel! package genetlink genetlink-1.1.0/example_go1.13_test.go000066400000000000000000000071201415423042400175010ustar00rootroot00000000000000//go:build go1.13 // +build go1.13 package genetlink_test import ( "errors" "log" "net" "os" "github.com/mdlayher/genetlink" "github.com/mdlayher/netlink" ) // This example demonstrates using a genetlink.Conn's high level interface // to query for a specific generic netlink family. func ExampleConn_getFamily() { c, err := genetlink.Dial(nil) if err != nil { log.Fatalf("failed to dial generic netlink: %v", err) } defer c.Close() // Ask generic netlink about the generic netlink controller (nlctrl)'s // family information. const name = "nlctrl" family, err := c.GetFamily(name) if err != nil { // If a family doesn't exist, nothing to do. if errors.Is(err, os.ErrNotExist) { log.Printf("%q family not available", name) return } log.Fatalf("failed to query for family: %v", err) } log.Printf("%s: %+v", name, family) } // This example demonstrates using a genetlink.Conn's high level interface // to query for all known generic netlink families. func ExampleConn_listFamilies() { c, err := genetlink.Dial(nil) if err != nil { log.Fatalf("failed to dial generic netlink: %v", err) } defer c.Close() // Ask generic netlink about all families registered with it. families, err := c.ListFamilies() if err != nil { log.Fatalf("failed to query for families: %v", err) } for i, f := range families { log.Printf("#%02d: %+v", i, f) } } // This example demonstrates using a genetlink.Conn's high level and low // level interfaces to detect if nl80211 (netlink 802.11 WiFi device // generic netlink family) is available, and if it is, sending a request // to it to retrieve all WiFi interfaces. func ExampleConn_nl80211WiFi() { c, err := genetlink.Dial(nil) if err != nil { log.Fatalf("failed to dial generic netlink: %v", err) } defer c.Close() // Constants which are sourced from nl80211.h. const ( name = "nl80211" nl80211CommandGetInterface = 5 nl80211AttributeInterfaceIndex = 3 nl80211AttributeInterfaceName = 4 nl80211AttributeAttributeMAC = 6 ) // Ask generic netlink if nl80211 is available. family, err := c.GetFamily(name) if err != nil { if errors.Is(err, os.ErrNotExist) { log.Printf("%q family not available", name) return } log.Fatalf("failed to query for family: %v", err) } // Ask nl80211 to dump a list of all WiFi interfaces. req := genetlink.Message{ Header: genetlink.Header{ Command: nl80211CommandGetInterface, Version: family.Version, }, } // Send request specifically to nl80211 instead of generic netlink // controller (nlctrl). msgs, err := c.Execute(req, family.ID, netlink.Request|netlink.Dump) if err != nil { log.Fatalf("failed to execute: %v", err) } // ifInfo contains basic information about a WiFi interface. type ifInfo struct { Index int Name string MAC net.HardwareAddr } infos := make([]ifInfo, 0, len(msgs)) for _, m := range msgs { // nl80211's response contains packed netlink attributes. ad, err := netlink.NewAttributeDecoder(m.Data) if err != nil { log.Fatalf("failed to create attribute decoder: %v", err) } // Gather data about interface from attributes. var info ifInfo for ad.Next() { switch ad.Type() { case nl80211AttributeInterfaceIndex: info.Index = int(ad.Uint32()) case nl80211AttributeInterfaceName: info.Name = ad.String() case nl80211AttributeAttributeMAC: info.MAC = ad.Bytes() } } // Were any of the attributes malformed? if err := ad.Err(); err != nil { log.Fatalf("failed to decode attributes: %v", err) } infos = append(infos, info) } for i, info := range infos { log.Printf("#%02d: %+v", i, info) } } genetlink-1.1.0/family.go000066400000000000000000000005731415423042400153050ustar00rootroot00000000000000package genetlink // A Family is a generic netlink family. type Family struct { ID uint16 Version uint8 Name string Groups []MulticastGroup } // A MulticastGroup is a generic netlink multicast group, which can be joined // for notifications from generic netlink families when specific events take // place. type MulticastGroup struct { ID uint32 Name string } genetlink-1.1.0/family_linux.go000066400000000000000000000065341415423042400165270ustar00rootroot00000000000000//go:build linux // +build linux package genetlink import ( "errors" "fmt" "math" "github.com/mdlayher/netlink" "github.com/mdlayher/netlink/nlenc" "golang.org/x/sys/unix" ) // errInvalidFamilyVersion is returned when a family's version is greater // than an 8-bit integer. var errInvalidFamilyVersion = errors.New("invalid family version attribute") // getFamily retrieves a generic netlink family with the specified name. func (c *Conn) getFamily(name string) (Family, error) { b, err := netlink.MarshalAttributes([]netlink.Attribute{{ Type: unix.CTRL_ATTR_FAMILY_NAME, Data: nlenc.Bytes(name), }}) if err != nil { return Family{}, err } req := Message{ Header: Header{ Command: unix.CTRL_CMD_GETFAMILY, // TODO(mdlayher): grab nlctrl version? Version: 1, }, Data: b, } msgs, err := c.Execute(req, unix.GENL_ID_CTRL, netlink.Request) if err != nil { return Family{}, err } // TODO(mdlayher): consider interpreting generic netlink header values families, err := buildFamilies(msgs) if err != nil { return Family{}, err } if len(families) != 1 { // If this were to ever happen, netlink must be in a state where // its answers cannot be trusted panic(fmt.Sprintf("netlink returned multiple families for name: %q", name)) } return families[0], nil } // listFamilies retrieves all registered generic netlink families. func (c *Conn) listFamilies() ([]Family, error) { req := Message{ Header: Header{ Command: unix.CTRL_CMD_GETFAMILY, // TODO(mdlayher): grab nlctrl version? Version: 1, }, } msgs, err := c.Execute(req, unix.GENL_ID_CTRL, netlink.Request|netlink.Dump) if err != nil { return nil, err } return buildFamilies(msgs) } // buildFamilies builds a slice of Families by parsing attributes from the // input Messages. func buildFamilies(msgs []Message) ([]Family, error) { families := make([]Family, 0, len(msgs)) for _, m := range msgs { f, err := parseFamily(m.Data) if err != nil { return nil, err } families = append(families, f) } return families, nil } // parseFamily decodes netlink attributes into a Family. func parseFamily(b []byte) (Family, error) { ad, err := netlink.NewAttributeDecoder(b) if err != nil { return Family{}, err } var f Family for ad.Next() { switch ad.Type() { case unix.CTRL_ATTR_FAMILY_ID: f.ID = ad.Uint16() case unix.CTRL_ATTR_FAMILY_NAME: f.Name = ad.String() case unix.CTRL_ATTR_VERSION: v := ad.Uint32() if v > math.MaxUint8 { return Family{}, errInvalidFamilyVersion } f.Version = uint8(v) case unix.CTRL_ATTR_MCAST_GROUPS: ad.Nested(parseMulticastGroups(&f.Groups)) } } if err := ad.Err(); err != nil { return Family{}, err } return f, nil } // parseMulticastGroups parses an array of multicast group nested attributes // into a slice of MulticastGroups. func parseMulticastGroups(groups *[]MulticastGroup) func(*netlink.AttributeDecoder) error { return func(ad *netlink.AttributeDecoder) error { *groups = make([]MulticastGroup, 0, ad.Len()) for ad.Next() { ad.Nested(func(nad *netlink.AttributeDecoder) error { var g MulticastGroup for nad.Next() { switch nad.Type() { case unix.CTRL_ATTR_MCAST_GRP_NAME: g.Name = nad.String() case unix.CTRL_ATTR_MCAST_GRP_ID: g.ID = nad.Uint32() } } *groups = append(*groups, g) return nil }) } return nil } } genetlink-1.1.0/family_linux_test.go000066400000000000000000000141361415423042400175630ustar00rootroot00000000000000//go:build linux // +build linux package genetlink_test import ( "testing" "github.com/google/go-cmp/cmp" "github.com/mdlayher/genetlink" "github.com/mdlayher/genetlink/genltest" "github.com/mdlayher/netlink" "github.com/mdlayher/netlink/nlenc" "github.com/mdlayher/netlink/nltest" "golang.org/x/sys/unix" ) func TestConnGetFamily(t *testing.T) { const ( name = "nlctrl" version = 1 flags = netlink.Request ) wantgenl := genetlink.Message{ Header: genetlink.Header{ Command: unix.CTRL_CMD_GETFAMILY, Version: version, }, Data: nltest.MustMarshalAttributes([]netlink.Attribute{{ Type: unix.CTRL_ATTR_FAMILY_NAME, Data: nlenc.Bytes(name), }}), } c := genltest.Dial(func(greq genetlink.Message, nreq netlink.Message) ([]genetlink.Message, error) { if diff := cmp.Diff(flags, nreq.Header.Flags); diff != "" { t.Fatalf("unexpected netlink header flags (-want +got):\n%s", diff) } if diff := cmp.Diff(wantgenl, greq); diff != "" { t.Fatalf("unexpected generic netlink message (-want +got):\n%s", diff) } return []genetlink.Message{{ Header: genetlink.Header{ Command: unix.CTRL_CMD_NEWFAMILY, Version: version, }, Data: nltest.MustMarshalAttributes([]netlink.Attribute{ { Type: unix.CTRL_ATTR_FAMILY_NAME, Data: nlenc.Bytes(name), }, { Type: unix.CTRL_ATTR_FAMILY_ID, Data: nlenc.Uint16Bytes(16), }, { Type: unix.CTRL_ATTR_VERSION, Data: nlenc.Uint32Bytes(2), }, }), }}, nil }) family, err := c.GetFamily(name) if err != nil { t.Fatalf("failed to execute: %v", err) } wantFamily := genetlink.Family{ ID: 16, Version: 2, Name: name, } if diff := cmp.Diff(wantFamily, family); diff != "" { t.Fatalf("unexpected generic netlink family (-want +got):\n%s", diff) } } func TestConnFamilyList(t *testing.T) { const ( version = 1 flags = netlink.Request | netlink.Dump ) wantgenl := genetlink.Message{ Header: genetlink.Header{ Command: unix.CTRL_CMD_GETFAMILY, Version: version, }, Data: []byte{}, } c := genltest.Dial(func(greq genetlink.Message, nreq netlink.Message) ([]genetlink.Message, error) { if diff := cmp.Diff(flags, nreq.Header.Flags); diff != "" { t.Fatalf("unexpected netlink header flags (-want +got):\n%s", diff) } if diff := cmp.Diff(wantgenl, greq); diff != "" { t.Fatalf("unexpected generic netlink message (-want +got):\n%s", diff) } return []genetlink.Message{ { Header: genetlink.Header{ Command: unix.CTRL_CMD_NEWFAMILY, Version: version, }, Data: nltest.MustMarshalAttributes([]netlink.Attribute{ { Type: unix.CTRL_ATTR_FAMILY_NAME, Data: nlenc.Bytes("nlctrl"), }, { Type: unix.CTRL_ATTR_FAMILY_ID, Data: nlenc.Uint16Bytes(16), }, { Type: unix.CTRL_ATTR_VERSION, Data: nlenc.Uint32Bytes(2), }, }), }, { Header: genetlink.Header{ Command: unix.CTRL_CMD_NEWFAMILY, Version: version, }, Data: nltest.MustMarshalAttributes([]netlink.Attribute{ { Type: unix.CTRL_ATTR_FAMILY_NAME, Data: nlenc.Bytes("nl80211"), }, { Type: unix.CTRL_ATTR_FAMILY_ID, Data: nlenc.Uint16Bytes(26), }, { Type: unix.CTRL_ATTR_VERSION, Data: nlenc.Uint32Bytes(1), }, }), // Normally a "multi-part" done message would be here, but package // netlink takes care of trimming that away for us, so this package // assumes that has already been taken care of }, }, nil }) families, err := c.ListFamilies() if err != nil { t.Fatalf("failed to execute: %v", err) } wantFamilies := []genetlink.Family{ { ID: 16, Version: 2, Name: "nlctrl", }, { ID: 26, Version: 1, Name: "nl80211", }, } if diff := cmp.Diff(wantFamilies, families); diff != "" { t.Fatalf("unexpected generic netlink families (-want +got):\n%s", diff) } } func TestFamily_parseAttributes(t *testing.T) { tests := []struct { name string attrs []netlink.Attribute f genetlink.Family ok bool }{ { name: "version too large", attrs: []netlink.Attribute{{ Type: unix.CTRL_ATTR_VERSION, Data: []byte{0xff, 0x01, 0x00, 0x00}, }}, }, { name: "OK", attrs: []netlink.Attribute{ { Type: unix.CTRL_ATTR_FAMILY_ID, Data: []byte{0x10, 0x00}, }, { Type: unix.CTRL_ATTR_FAMILY_NAME, Data: nlenc.Bytes("nlctrl"), }, { Type: unix.CTRL_ATTR_VERSION, Data: []byte{0x02, 0x00, 0x00, 0x00}, }, { Type: unix.CTRL_ATTR_MCAST_GROUPS, Data: nltest.MustMarshalAttributes([]netlink.Attribute{ { Type: 1, Data: nltest.MustMarshalAttributes([]netlink.Attribute{ { Type: unix.CTRL_ATTR_MCAST_GRP_ID, Data: nlenc.Uint32Bytes(16), }, { Type: unix.CTRL_ATTR_MCAST_GRP_NAME, Data: nlenc.Bytes("notify"), }, }), }, { Type: 2, Data: nltest.MustMarshalAttributes([]netlink.Attribute{ { Type: unix.CTRL_ATTR_MCAST_GRP_ID, Data: nlenc.Uint32Bytes(17), }, { Type: unix.CTRL_ATTR_MCAST_GRP_NAME, Data: nlenc.Bytes("foobar"), }, }), }, }), }, }, f: genetlink.Family{ ID: 16, Version: 2, Name: "nlctrl", Groups: []genetlink.MulticastGroup{ { ID: 16, Name: "notify", }, { ID: 17, Name: "foobar", }, }, }, ok: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { c := genltest.Dial(func(_ genetlink.Message, _ netlink.Message) ([]genetlink.Message, error) { return []genetlink.Message{{ Data: nltest.MustMarshalAttributes(tt.attrs), }}, nil }) family, err := c.GetFamily("") if err != nil && tt.ok { t.Fatalf("unexpected error: %v", err) } if err == nil && !tt.ok { t.Fatal("expected an error, but none occurred") } if err != nil { return } if diff := cmp.Diff(tt.f, family); diff != "" { t.Fatalf("unexpected family (-want +got):\n%s", diff) } }) } } genetlink-1.1.0/family_others.go000066400000000000000000000010541415423042400166640ustar00rootroot00000000000000//go:build !linux // +build !linux package genetlink import ( "fmt" "runtime" ) // errUnimplemented is returned by all functions on platforms that // cannot make use of generic netlink. var errUnimplemented = fmt.Errorf("generic netlink not implemented on %s/%s", runtime.GOOS, runtime.GOARCH) // getFamily always returns an error. func (c *Conn) getFamily(name string) (Family, error) { return Family{}, errUnimplemented } // listFamilies always returns an error. func (c *Conn) listFamilies() ([]Family, error) { return nil, errUnimplemented } genetlink-1.1.0/fuzz.go000066400000000000000000000004621415423042400150170ustar00rootroot00000000000000//go:build gofuzz // +build gofuzz package genetlink func Fuzz(data []byte) int { return fuzzMessage(data) } func fuzzMessage(data []byte) int { var m Message if err := (&m).UnmarshalBinary(data); err != nil { return 0 } if _, err := m.MarshalBinary(); err != nil { panic(err) } return 1 } genetlink-1.1.0/genltest/000077500000000000000000000000001415423042400153155ustar00rootroot00000000000000genetlink-1.1.0/genltest/family_linux.go000066400000000000000000000046461415423042400203560ustar00rootroot00000000000000//go:build linux // +build linux package genltest import ( "fmt" "github.com/mdlayher/genetlink" "github.com/mdlayher/netlink" "golang.org/x/sys/unix" ) // serveFamily is the Linux implementation of ServeFamily. func serveFamily(f genetlink.Family, fn Func) Func { return func(greq genetlink.Message, nreq netlink.Message) ([]genetlink.Message, error) { // Only intercept "get family" commands to the generic netlink controller. if nreq.Header.Type != unix.GENL_ID_CTRL || greq.Header.Command != unix.CTRL_CMD_GETFAMILY { return fn(greq, nreq) } ad, err := netlink.NewAttributeDecoder(greq.Data) if err != nil { return nil, fmt.Errorf("genltest: failed to parse get family request attributes: %v", err) } // Ensure this request is for the family provided by f. for ad.Next() { if want, got := unix.CTRL_ATTR_FAMILY_NAME, int(ad.Type()); want != got { return nil, fmt.Errorf("genltest: unexpected get family request attribute: %d, want: %d", got, want) } if want, got := f.Name, ad.String(); want != got { return nil, fmt.Errorf("genltest: unexpected get family request value: %q, want: %q", got, want) } } if err := ad.Err(); err != nil { return nil, fmt.Errorf("genltest: unexpected error decoding get family request: %v", err) } // Return the family information for f. ae := netlink.NewAttributeEncoder() ae.Uint16(unix.CTRL_ATTR_FAMILY_ID, f.ID) ae.String(unix.CTRL_ATTR_FAMILY_NAME, f.Name) ae.Uint32(unix.CTRL_ATTR_VERSION, uint32(f.Version)) // Encode multicast group attributes if applicable. if len(f.Groups) > 0 { ae.Nested(unix.CTRL_ATTR_MCAST_GROUPS, encodeGroups(f.Groups)) } attrb, err := ae.Encode() if err != nil { return nil, err } return []genetlink.Message{{ Header: genetlink.Header{ Command: unix.CTRL_CMD_NEWFAMILY, // TODO(mdlayher): constant nlctrl version number? Version: 2, }, Data: attrb, }}, nil } } // encodeGroups encodes multicast groups as packed netlink attributes. func encodeGroups(groups []genetlink.MulticastGroup) func(ae *netlink.AttributeEncoder) error { return func(ae *netlink.AttributeEncoder) error { // Groups are a netlink "array" of nested attributes. for i, g := range groups { ae.Nested(uint16(i), func(nae *netlink.AttributeEncoder) error { nae.String(unix.CTRL_ATTR_MCAST_GRP_NAME, g.Name) nae.Uint32(unix.CTRL_ATTR_MCAST_GRP_ID, g.ID) return nil }) } return nil } } genetlink-1.1.0/genltest/family_linux_test.go000066400000000000000000000061231415423042400214050ustar00rootroot00000000000000//go:build linux // +build linux package genltest_test import ( "io" "testing" "github.com/google/go-cmp/cmp" "github.com/mdlayher/genetlink" "github.com/mdlayher/genetlink/genltest" "github.com/mdlayher/netlink" "github.com/mdlayher/netlink/nlenc" "github.com/mdlayher/netlink/nltest" "golang.org/x/sys/unix" ) func TestServeFamily(t *testing.T) { tests := []struct { name string f genetlink.Family fn func(c *genetlink.Conn) (*genetlink.Family, error) ok bool pass bool }{ { name: "error, wrong attribute type", fn: func(c *genetlink.Conn) (*genetlink.Family, error) { m := genetlink.Message{ Header: genetlink.Header{ Command: unix.CTRL_CMD_GETFAMILY, }, Data: nltest.MustMarshalAttributes([]netlink.Attribute{{ Type: 0xff, }}), } _, err := c.Execute(m, unix.GENL_ID_CTRL, 0) return nil, err }, }, { name: "error, wrong family name", f: genetlink.Family{Name: "foo"}, fn: func(c *genetlink.Conn) (*genetlink.Family, error) { m := genetlink.Message{ Header: genetlink.Header{ Command: unix.CTRL_CMD_GETFAMILY, }, Data: nltest.MustMarshalAttributes([]netlink.Attribute{{ Type: unix.CTRL_ATTR_FAMILY_NAME, Data: nlenc.Bytes("bar"), }}), } _, err := c.Execute(m, unix.GENL_ID_CTRL, 0) return nil, err }, }, { name: "ok, family foo", f: genetlink.Family{ ID: 1, Name: "foo", Version: 1, Groups: []genetlink.MulticastGroup{ { ID: 2, Name: "bar", }, { ID: 3, Name: "baz", }, }, }, fn: func(c *genetlink.Conn) (*genetlink.Family, error) { f, err := c.GetFamily("foo") return &f, err }, ok: true, }, { name: "pass, different family", fn: func(c *genetlink.Conn) (*genetlink.Family, error) { _, err := c.Execute(genetlink.Message{}, 2, 0) return nil, err }, ok: true, pass: true, }, { name: "pass, different command", fn: func(c *genetlink.Conn) (*genetlink.Family, error) { m := genetlink.Message{ Header: genetlink.Header{ Command: unix.CTRL_CMD_DELFAMILY, }, } _, err := c.Execute(m, unix.GENL_ID_CTRL, 0) return nil, err }, ok: true, pass: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var pass bool c := genltest.Dial(genltest.ServeFamily(tt.f, func(greq genetlink.Message, nreq netlink.Message) ([]genetlink.Message, error) { // Message was passed to inner handler. pass = true return nil, io.EOF }, )) defer c.Close() f, err := tt.fn(c) if err != nil && tt.ok { t.Fatalf("unexpected error: %v", err) } if err == nil && !tt.ok { t.Fatal("expected an error, but none occurred") } if err != nil { return } if !tt.pass { if diff := cmp.Diff(tt.f, *f); diff != "" { t.Fatalf("unexpected generic netlink family (-want +got):\n%s", diff) } } if diff := cmp.Diff(tt.pass, pass); diff != "" { t.Fatalf("unexpected function passthrough (-want +got):\n%s", diff) } }) } } genetlink-1.1.0/genltest/family_others.go000066400000000000000000000011111415423042400205030ustar00rootroot00000000000000//go:build !linux // +build !linux package genltest import ( "fmt" "runtime" "github.com/mdlayher/genetlink" "github.com/mdlayher/netlink" ) // errUnimplemented is returned by all functions on platforms that // cannot make use of genltest. var errUnimplemented = fmt.Errorf("genltest not implemented on %s/%s", runtime.GOOS, runtime.GOARCH) // serveFamily returns a Func which always returns an error. func serveFamily(f genetlink.Family, fn Func) Func { return func(_ genetlink.Message, _ netlink.Message) ([]genetlink.Message, error) { return nil, errUnimplemented } } genetlink-1.1.0/genltest/genltest.go000066400000000000000000000114551415423042400174770ustar00rootroot00000000000000// Package genltest provides utilities for generic netlink testing. package genltest import ( "fmt" "github.com/mdlayher/genetlink" "github.com/mdlayher/netlink" "github.com/mdlayher/netlink/nltest" ) // Error returns a netlink error to the caller with the specified error // number. func Error(number int) error { return &errnoError{number: number} } type errnoError struct { number int } func (err *errnoError) Error() string { return fmt.Sprintf("genltest errno: %d", err.number) } // A Func is a function that can be used to test genetlink.Conn interactions. // The function can choose to return zero or more generic netlink messages, // or an error if needed. // // For a netlink request/response interaction, the requests greq and nreq are // populated by genetlink.Conn.Send and passed to the function. greq is created // from the body of nreq. // // For multicast interactions, both greq and nreq are empty when passed to the function // when genetlink.Conn.Receive is called. // // If a Func returns an error, the error will be returned as-is to the caller. // If no messages and io.EOF are returned, no messages and no error will be // returned to the caller, simulating a multi-part message with no data. type Func func(greq genetlink.Message, nreq netlink.Message) ([]genetlink.Message, error) // Dial sets up a genetlink.Conn for testing using the specified Func. All requests // sent from the connection will be passed to the Func. The connection should be // closed as usual when it is no longer needed. func Dial(fn Func) *genetlink.Conn { return genetlink.NewConn(nltest.Dial(adapt(fn))) } // ServeFamily returns a Func that intercepts "get family" commands to the // generic netlink controller, verifies that the requested family name matches // the provided one, and then returns family information specified by f. // // Requests which are not related to requesting a family are passed through to fn. // // ServeFamily is primarily useful in tests for packages which interact with // a specific generic netlink family. func ServeFamily(f genetlink.Family, fn Func) Func { return serveFamily(f, fn) } // CheckRequest returns a Func that verifies that an incoming request message // has the specified generic netlink family, command, and netlink header flags, // and then passes the request through to fn. // // If family, command, or flags are set to the zero value, the specific check // for that value will be skipped for request message. func CheckRequest(family uint16, command uint8, flags netlink.HeaderFlags, fn Func) Func { base := nltest.CheckRequest( // Expect genetlink family in header type. []netlink.HeaderType{netlink.HeaderType(family)}, // Expect specified netlink flags. []netlink.HeaderFlags{flags}, // Make the next nltest function a noop. // TODO(mdlayher): modify nltest to eliminate the need for this? nltest.Func(func(_ []netlink.Message) ([]netlink.Message, error) { return nil, nil }), ) return func(greq genetlink.Message, nreq netlink.Message) ([]genetlink.Message, error) { if _, err := base([]netlink.Message{nreq}); err != nil { return nil, fmt.Errorf("genltest: netlink header validation failed: %v", err) } if want, got := command, greq.Header.Command; command != 0 && want != got { return nil, fmt.Errorf("genltest: unexpected generic netlink header command: %d, want: %d", got, want) } return fn(greq, nreq) } } var _ nltest.Func = adapt(nil) // adapt is an adapter function for a Func to be used as a nltest.Func. adapt // handles marshaling and unmarshaling of generic netlink messages. func adapt(fn Func) nltest.Func { return func(reqs []netlink.Message) ([]netlink.Message, error) { var req netlink.Message l := len(reqs) switch l { case 0: // No messages. case 1: // Use the first message. req = reqs[0] default: // Multiple messages; doesn't seem to occur with genetlink? return nil, fmt.Errorf("genltest: expected zero or one request, but got: %d", l) } var gm genetlink.Message // Populate message if some data has been passed in req. if len(req.Data) > 0 { if err := gm.UnmarshalBinary(req.Data); err != nil { return nil, err } } gmsgs, err := fn(gm, req) if err != nil { // An error was returned with an error number by the Func. // Pass this to the caller as a netlink message error. nerr, ok := err.(*errnoError) if !ok { return nil, err } return nltest.Error(nerr.number, reqs) } nmsgs := make([]netlink.Message, 0, len(gmsgs)) for _, msg := range gmsgs { b, err := msg.MarshalBinary() if err != nil { return nil, err } nmsgs = append(nmsgs, netlink.Message{ // Mimic the sequence and PID of the request for validation. Header: netlink.Header{ Sequence: req.Header.Sequence, PID: req.Header.PID, }, Data: b, }) } return nmsgs, nil } } genetlink-1.1.0/genltest/genltest_linux_test.go000066400000000000000000000015411415423042400217500ustar00rootroot00000000000000//go:build linux // +build linux package genltest_test import ( "os" "syscall" "testing" "github.com/mdlayher/genetlink" "github.com/mdlayher/genetlink/genltest" "github.com/mdlayher/netlink" ) func TestConnLinuxReceiveError(t *testing.T) { c := genltest.Dial(func(_ genetlink.Message, _ netlink.Message) ([]genetlink.Message, error) { return nil, genltest.Error(int(syscall.EPERM)) }) defer c.Close() // Send some generic request to enable the testing function to send // EPERM error back to us. if _, err := c.Send(genetlink.Message{}, 1, netlink.Request); err != nil { t.Fatalf("failed to send: %v", err) } _, _, err := c.Receive() if err == nil { t.Fatal("expected an error, but none occurred") } serr := err.(*netlink.OpError).Err if !os.IsPermission(serr) { t.Fatalf("expected permission denied error, but got: %v", err) } } genetlink-1.1.0/genltest/genltest_test.go000066400000000000000000000075371415423042400205440ustar00rootroot00000000000000package genltest_test import ( "bytes" "errors" "io" "reflect" "testing" "github.com/mdlayher/genetlink" "github.com/mdlayher/genetlink/genltest" "github.com/mdlayher/netlink" ) func TestConnSend(t *testing.T) { req := genetlink.Message{ Data: []byte{0xff, 0xff, 0xff, 0xff}, } c := genltest.Dial(func(creq genetlink.Message, _ netlink.Message) ([]genetlink.Message, error) { if want, got := req.Data, creq.Data; !bytes.Equal(want, got) { t.Fatalf("unexpected request data:\n- want: %v\n- got: %v", want, got) } return nil, nil }) defer c.Close() if _, err := c.Send(req, 1, 1); err != nil { t.Fatalf("failed to send request: %v", err) } } func TestConnExecuteOK(t *testing.T) { req := genetlink.Message{ Data: []byte{0xff}, } c := genltest.Dial(func(creq genetlink.Message, _ netlink.Message) ([]genetlink.Message, error) { // Turn the request back around to the client. return []genetlink.Message{creq}, nil }) defer c.Close() got, err := c.Execute(req, 1, 1) if err != nil { t.Fatalf("failed to execute request: %v", err) } if want := []genetlink.Message{req}; !reflect.DeepEqual(want, got) { t.Fatalf("unexpected response messages:\n- want: %v\n- got: %v", want, got) } } func TestConnExecuteNoMessages(t *testing.T) { c := genltest.Dial(func(_ genetlink.Message, _ netlink.Message) ([]genetlink.Message, error) { return nil, io.EOF }) defer c.Close() msgs, err := c.Execute(genetlink.Message{}, 0, 0) if err != nil { t.Fatalf("failed to execute: %v", err) } if l := len(msgs); l > 0 { t.Fatalf("expected no generic netlink messages, but got: %d", l) } } func TestConnReceiveNoMessages(t *testing.T) { c := genltest.Dial(func(_ genetlink.Message, _ netlink.Message) ([]genetlink.Message, error) { return nil, io.EOF }) defer c.Close() gmsgs, nmsgs, err := c.Receive() if err != nil { t.Fatalf("failed to execute: %v", err) } if l := len(gmsgs); l > 0 { t.Fatalf("expected no generic netlink messages, but got: %d", l) } if l := len(nmsgs); l > 0 { t.Fatalf("expected no netlink messages, but got: %d", l) } } func TestConnReceiveError(t *testing.T) { errFoo := errors.New("foo") c := genltest.Dial(func(_ genetlink.Message, _ netlink.Message) ([]genetlink.Message, error) { return nil, errFoo }) defer c.Close() _, _, err := c.Receive() if err.(*netlink.OpError).Err != errFoo { t.Fatalf("unexpected error: %v", err) } } func TestCheckRequest(t *testing.T) { tests := []struct { name string family uint16 command uint8 flags netlink.HeaderFlags greq genetlink.Message nreq netlink.Message ok bool }{ { name: "no checking", ok: true, }, { name: "bad family", family: 1, nreq: netlink.Message{ Header: netlink.Header{ // genetlink family is netlink header type. Type: 2, }, }, }, { name: "bad flags", flags: netlink.Request, nreq: netlink.Message{ Header: netlink.Header{ Flags: netlink.Replace, }, }, }, { name: "bad command", command: 1, greq: genetlink.Message{ Header: genetlink.Header{ Command: 2, }, }, }, { name: "ok", family: 1, command: 1, flags: netlink.Request, nreq: netlink.Message{ Header: netlink.Header{ Type: 1, Flags: netlink.Request, }, }, greq: genetlink.Message{ Header: genetlink.Header{ Command: 1, }, }, ok: true, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { fn := genltest.CheckRequest(tt.family, tt.command, tt.flags, noop) _, err := fn(tt.greq, tt.nreq) if err != nil && tt.ok { t.Fatalf("unexpected error: %v", err) } if err == nil && !tt.ok { t.Fatal("expected an error, but none occurred") } }) } } var noop = func(greq genetlink.Message, nreq netlink.Message) ([]genetlink.Message, error) { return nil, nil } genetlink-1.1.0/go.mod000066400000000000000000000004541415423042400146010ustar00rootroot00000000000000module github.com/mdlayher/genetlink go 1.13 require ( github.com/google/go-cmp v0.5.6 github.com/mdlayher/netlink v1.4.2 golang.org/x/net v0.0.0-20211208012354-db4efeb81f4b golang.org/x/sys v0.0.0-20211205182925-97ca703d548d golang.org/x/tools v0.1.8 // indirect honnef.co/go/tools v0.2.2 ) genetlink-1.1.0/go.sum000066400000000000000000000323341415423042400146300ustar00rootroot00000000000000github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.4.1 h1:GaI7EiDXDRfa8VshkTj7Fym7ha+y8/XxIgD2okUIjLw= github.com/BurntSushi/toml v0.4.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/cilium/ebpf v0.5.0/go.mod h1:4tRaxcgiL706VnOzHOdBlY8IEAIdxINsQBcU4xJJXRs= github.com/cilium/ebpf v0.7.0 h1:1k/q3ATgxSXRdrmPfH8d7YK0GfqVsEKZAX9dQZvs56k= github.com/cilium/ebpf v0.7.0/go.mod h1:/oI2+1shJiTGAMgl6/RgJr36Eo1jzrRcAWbcXO2usCA= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/frankban/quicktest v1.11.3 h1:8sXhOn0uLys67V8EsXLc6eszDs8VXWxL3iRvebPhedY= github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/josharian/native v0.0.0-20200817173448-b6b71def0850 h1:uhL5Gw7BINiiPAo24A2sxkcDI0Jt/sqp1v5xQCniEFA= github.com/josharian/native v0.0.0-20200817173448-b6b71def0850/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w= github.com/jsimonetti/rtnetlink v0.0.0-20190606172950-9527aa82566a/go.mod h1:Oz+70psSo5OFh8DBl0Zv2ACw7Esh6pPUphlvZG9x7uw= github.com/jsimonetti/rtnetlink v0.0.0-20200117123717-f846d4f6c1f4/go.mod h1:WGuG/smIU4J/54PblvSbh+xvCZmpJnFgr3ds6Z55XMQ= github.com/jsimonetti/rtnetlink v0.0.0-20201009170750-9c6f07d100c1/go.mod h1:hqoO/u39cqLeBLebZ8fWdE96O7FxrAsRYhnVOdgHxok= github.com/jsimonetti/rtnetlink v0.0.0-20201216134343-bde56ed16391/go.mod h1:cR77jAZG3Y3bsb8hF6fHJbFoyFukLFOkQ98S0pQz3xw= github.com/jsimonetti/rtnetlink v0.0.0-20201220180245-69540ac93943/go.mod h1:z4c53zj6Eex712ROyh8WI0ihysb5j2ROyV42iNogmAs= github.com/jsimonetti/rtnetlink v0.0.0-20210122163228-8d122574c736/go.mod h1:ZXpIyOK59ZnN7J0BV99cZUPmsqDRZ3eq5X+st7u/oSA= github.com/jsimonetti/rtnetlink v0.0.0-20210212075122-66c871082f2b/go.mod h1:8w9Rh8m+aHZIG69YPGGem1i5VzoyRC8nw2kA8B+ik5U= github.com/jsimonetti/rtnetlink v0.0.0-20210525051524-4cc836578190/go.mod h1:NmKSdU4VGSiv1bMsdqNALI4RSvvjtz65tTMCnD05qLo= github.com/jsimonetti/rtnetlink v0.0.0-20211022192332-93da33804786 h1:N527AHMa793TP5z5GNAn/VLPzlc0ewzWdeP/25gDfgQ= github.com/jsimonetti/rtnetlink v0.0.0-20211022192332-93da33804786/go.mod h1:v4hqbTdfQngbVSZJVWUhGE/lbTFf9jb+ygmNUDQMuOs= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/mdlayher/ethtool v0.0.0-20210210192532-2b88debcdd43/go.mod h1:+t7E0lkKfbBsebllff1xdTmyJt8lH37niI6kwFk9OTo= github.com/mdlayher/ethtool v0.0.0-20211028163843-288d040e9d60 h1:tHdB+hQRHU10CfcK0furo6rSNgZ38JT8uPh70c/pFD8= github.com/mdlayher/ethtool v0.0.0-20211028163843-288d040e9d60/go.mod h1:aYbhishWc4Ai3I2U4Gaa2n3kHWSwzme6EsG/46HRQbE= github.com/mdlayher/genetlink v1.0.0/go.mod h1:0rJ0h4itni50A86M2kHcgS85ttZazNt7a8H2a2cw0Gc= github.com/mdlayher/netlink v0.0.0-20190409211403-11939a169225/go.mod h1:eQB3mZE4aiYnlUsyGGCOpPETfdQq4Jhsgf1fk3cwQaA= github.com/mdlayher/netlink v1.0.0/go.mod h1:KxeJAFOFLG6AjpyDkQ/iIhxygIUKD+vcwqcnu43w/+M= github.com/mdlayher/netlink v1.1.0/go.mod h1:H4WCitaheIsdF9yOYu8CFmCgQthAPIWZmcKp9uZHgmY= github.com/mdlayher/netlink v1.1.1/go.mod h1:WTYpFb/WTvlRJAyKhZL5/uy69TDDpHHu2VZmb2XgV7o= github.com/mdlayher/netlink v1.2.0/go.mod h1:kwVW1io0AZy9A1E2YYgaD4Cj+C+GPkU6klXCMzIJ9p8= github.com/mdlayher/netlink v1.2.1/go.mod h1:bacnNlfhqHqqLo4WsYeXSqfyXkInQ9JneWI68v1KwSU= github.com/mdlayher/netlink v1.2.2-0.20210123213345-5cc92139ae3e/go.mod h1:bacnNlfhqHqqLo4WsYeXSqfyXkInQ9JneWI68v1KwSU= github.com/mdlayher/netlink v1.3.0/go.mod h1:xK/BssKuwcRXHrtN04UBkwQ6dY9VviGGuriDdoPSWys= github.com/mdlayher/netlink v1.4.0/go.mod h1:dRJi5IABcZpBD2A3D0Mv/AiX8I9uDEu5oGkAVrekmf8= github.com/mdlayher/netlink v1.4.1/go.mod h1:e4/KuJ+s8UhfUpO9z00/fDZZmhSrs+oxyqAS9cNgn6Q= github.com/mdlayher/netlink v1.4.2 h1:3sbnJWe/LETovA7yRZIX3f9McVOWV3OySH6iIBxiFfI= github.com/mdlayher/netlink v1.4.2/go.mod h1:13VaingaArGUTUxFLf/iEovKxXji32JAtF858jZYEug= github.com/mdlayher/socket v0.0.0-20210307095302-262dc9984e00/go.mod h1:GAFlyu4/XV68LkQKYzKhIo/WW7j3Zi0YRAz/BOoanUc= github.com/mdlayher/socket v0.0.0-20211007213009-516dcbdf0267/go.mod h1:nFZ1EtZYK8Gi/k6QNu7z7CgO20i/4ExeQswwWuPmG/g= github.com/mdlayher/socket v0.0.0-20211102153432-57e3fa563ecb h1:2dC7L10LmTqlyMVzFJ00qM25lqESg9Z4u3GuEXN5iHY= github.com/mdlayher/socket v0.0.0-20211102153432-57e3fa563ecb/go.mod h1:nFZ1EtZYK8Gi/k6QNu7z7CgO20i/4ExeQswwWuPmG/g= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38= golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20191007182048-72f939374954/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20201216054612-986b41b23924/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20210928044308-7d9f5e0b762b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211020060615-d418f374d309/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211201190559-0a0e4e1bb54c/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211208012354-db4efeb81f4b h1:MWaHNqZy3KTpuTMAGvv+Kw+ylsEpmyJZizz1dqxnu28= golang.org/x/net v0.0.0-20211208012354-db4efeb81f4b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190411185658-b44545bcd369/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190826190057-c7b8b68b1456/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201009025420-dfb3f7c4e634/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201118182958-a01c418693c7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201218084310-7d0127a74742/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210123111255-9b0068b26619/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210216163648-f7da38b97c65/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210305230114-8fe3ee5dd75b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211205182925-97ca703d548d h1:FjkYO/PPp4Wi0EAUOVLxePm7qVW4r4ctbWpURyuOD0E= golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo= golang.org/x/tools v0.1.8 h1:P1HhGGuLW4aAclzjtmJdf0mJOjVUZUzOTqkAkWL+l6w= golang.org/x/tools v0.1.8/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= honnef.co/go/tools v0.2.1/go.mod h1:lPVVZ2BS5TfnjLyizF7o7hv7j9/L+8cZY2hLyjP9cGY= honnef.co/go/tools v0.2.2 h1:MNh1AVMyVX23VUHE2O27jm6lNj3vjO5DexS4A1xvnzk= honnef.co/go/tools v0.2.2/go.mod h1:lPVVZ2BS5TfnjLyizF7o7hv7j9/L+8cZY2hLyjP9cGY= genetlink-1.1.0/message.go000066400000000000000000000031111415423042400154370ustar00rootroot00000000000000package genetlink import "errors" // errInvalidMessage is returned when a Message is malformed. var errInvalidMessage = errors.New("generic netlink message is invalid or too short") // A Header is a generic netlink header. A Header is sent and received with // each generic netlink message to indicate metadata regarding a Message. type Header struct { // Command specifies a command to issue to netlink. Command uint8 // Version specifies the version of a command to use. Version uint8 } // headerLen is the length of a Header. const headerLen = 4 // unix.GENL_HDRLEN // A Message is a generic netlink message. It contains a Header and an // arbitrary byte payload, which may be decoded using information from the // Header. // // Data is encoded using the native endianness of the host system. Use // the netlink.AttributeDecoder and netlink.AttributeEncoder types to decode // and encode data. type Message struct { Header Header Data []byte } // MarshalBinary marshals a Message into a byte slice. func (m Message) MarshalBinary() ([]byte, error) { b := make([]byte, headerLen) b[0] = m.Header.Command b[1] = m.Header.Version // b[2] and b[3] are padding bytes and set to zero return append(b, m.Data...), nil } // UnmarshalBinary unmarshals the contents of a byte slice into a Message. func (m *Message) UnmarshalBinary(b []byte) error { if len(b) < headerLen { return errInvalidMessage } // Don't allow reserved pad bytes to be set if b[2] != 0 || b[3] != 0 { return errInvalidMessage } m.Header.Command = b[0] m.Header.Version = b[1] m.Data = b[4:] return nil } genetlink-1.1.0/message_test.go000066400000000000000000000050021415423042400164770ustar00rootroot00000000000000package genetlink import ( "bytes" "reflect" "testing" ) func TestMessageMarshalBinary(t *testing.T) { tests := []struct { name string m Message b []byte err error }{ { name: "empty", m: Message{}, b: []byte{0x00, 0x00, 0x00, 0x00}, }, { name: "no data", m: Message{ Header: Header{ Command: 1, Version: 2, }, }, b: []byte{0x01, 0x02, 0x00, 0x00}, }, { name: "data", m: Message{ Header: Header{ Command: 1, Version: 2, }, Data: []byte{0x03, 0x04}, }, b: []byte{ 0x01, 0x02, 0x00, 0x00, 0x03, 0x04, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { b, err := tt.m.MarshalBinary() if want, got := tt.err, err; want != got { t.Fatalf("unexpected error:\n- want: %v\n- got: %v", want, got) } if err != nil { return } if want, got := tt.b, b; !bytes.Equal(want, got) { t.Fatalf("unexpected Message bytes:\n- want: [%# x]\n- got: [%# x]", want, got) } }) } } func TestMessageUnmarshalBinary(t *testing.T) { tests := []struct { name string b []byte m Message err error }{ { name: "empty", err: errInvalidMessage, }, { name: "short", b: make([]byte, 3), err: errInvalidMessage, }, { name: "1st reserved set", b: []byte{0x00, 0x00, 0x01, 0x00}, err: errInvalidMessage, }, { name: "2nd reserved set", b: []byte{0x00, 0x00, 0x00, 0x01}, err: errInvalidMessage, }, { name: "both reserved set", b: []byte{0x00, 0x00, 0x01, 0x01}, err: errInvalidMessage, }, { name: "zero value", b: []byte{0x00, 0x00, 0x00, 0x00}, m: Message{ Data: make([]byte, 0), }, }, { name: "no data", b: []byte{0x01, 0x02, 0x00, 0x00}, m: Message{ Header: Header{ Command: 1, Version: 2, }, Data: make([]byte, 0), }, }, { name: "data", b: []byte{ 0x01, 0x02, 0x00, 0x00, 0x03, 0x04, }, m: Message{ Header: Header{ Command: 1, Version: 2, }, Data: []byte{0x03, 0x04}, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var m Message err := (&m).UnmarshalBinary(tt.b) if want, got := tt.err, err; want != got { t.Fatalf("unexpected error:\n- want: %v\n- got: %v", want, got) } if err != nil { return } if want, got := tt.m, m; !reflect.DeepEqual(want, got) { t.Fatalf("unexpected Message:\n- want: %#v\n- got: %#v", want, got) } }) } } genetlink-1.1.0/tools.go000066400000000000000000000002311415423042400151530ustar00rootroot00000000000000//go:build tools // +build tools // This file is used to pin static analysis tool versions. package main import _ "honnef.co/go/tools/cmd/staticcheck"