netlink

package module
v0.0.0-...-72cb0c4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 27, 2013 License: BSD-3-Clause Imports: 10 Imported by: 1

README

  GoNetlink aims to provide native bindings (no external libraries) to NetLink for Go.

  It is a child project of GoGo (not released yet), but is growing to be usable in its own right at least as a reference project on "how to (start) talking to netlink with go".

  All API's are subject to change, and patches are absolutely welcome.  Currently the basic API attempts to mirror that of (RT)Netlink usage.

  RTManip is a sub-package that attempts to expose a limited subset of the functionality of Netlink without having to fight with the underlying structures.

  There's a very good chance there will be issues on non i386/amd64 archs, as they have not been tested and netlink doesn't always specify byte order.

  Currently working:
  - RTNetlink: route, link, and addr calls (marshal, unmarshal, get attributes)

  TODO (maybe):
  - NFQueue, SELinux, IPTables (when mainlined, or closer)
  - add tests
  - test on ARM (is ARM big-Endian?)
  - Higher level manipulation API

Documentation

Index

Constants

View Source
const (
	ALIGN_MINUS_ONE     = 3
	NOT_ALIGN_MINUS_ONE = ^3
)

Copyright (c) 2011, Abneptis LLC. All rights reserved. Copyright (c) 2013, Vishvananda Ishaya. All rights reserved. Original Author: James D. Nurmi <james@abneptis.com>

See LICENSE for details

View Source
const ERROR_LENGTH = HEADER_LENGTH + 4

Unlike other headers, errors MAY be longer than the minimum length.

View Source
const HEADER_LENGTH = 16

Variables

This section is empty.

Functions

func Align

func Align(pos int) int

Returns a four byte allined value.

func GetAttributeCString

func GetAttributeCString(af AttributeFinder, at AttributeType) (out string, err error)

Same as GetAttributeString, but expects the string to be NULL terminated, the null terminator will be stripped..

func GetAttributeString

func GetAttributeString(af AttributeFinder, at AttributeType) (out string, err error)

Gets an attribute value as a string. Note, for much of RTNetlink, you want GetAttributeCString, which will verify and chop the tailing NULL.

func GetAttributeUint32

func GetAttributeUint32(af AttributeFinder, at AttributeType) (out uint32, err error)

Returns the attribute as an uint32 (or an error if the attr size is not 32 bits.

func MarshalAttributes

func MarshalAttributes(in []Attribute) (out []byte, err error)

Returns the bytes of a marshalled list of attributes. Any marshalling error will cause the sequence to abort.

func Padded

func Padded(in []byte) (out []byte)

Returns a padded version of bytes

func SetAttributeCString

func SetAttributeCString(aw AttributeWriter, at AttributeType, s string)
Sets the attribute value to the string specified by 's'.

s will be NULL terminated.

Types

type Address

type Address struct {
	// contains filtered or unexported fields
}

Implements net.Addr for netlink addresses.

func (Address) Address

func (self Address) Address() string

Ewruena the "pid" of the request.

func (Address) Network

func (self Address) Network() string

Returns "netlink"

type Attribute

type Attribute struct {
	Type AttributeType
	Body []byte
}

An attribute is used to hold an Netlink Attribute. An attribute is stored as a Length-Type-Value tuple. Length and Type are 16 bit integers, so values may not exceed 2^16.

func UnmarshalAttributes

func UnmarshalAttributes(in []byte) (out []Attribute, err error)

Unmarshals a netlink attribute.

func (self Attribute) MarshalNetlink() (out []byte, err error)

Marshals a netlink attribute as a full LTV tuple.

type AttributeFinder

type AttributeFinder interface {
	GetAttribute(AttributeType) (Attribute, error)
}

The attributeFinder interface should be used by READERS, Writers will need to use an AttributeWriter

type AttributeType

type AttributeType uint16

A basic netlink type used for identifying nlattrs in a message.

type AttributeWriter

type AttributeWriter interface {
	SetAttribute(Attribute)
}

AttributeWriters allow attributes to be added/updated.

type Error

type Error [ERROR_LENGTH]byte

Represents a netlink Error message.

func (Error) Code

func (self Error) Code() int32

The error code (-errno) of the netlink message. 0 is used for netlink ACK's.

func (Error) Error

func (self Error) Error() string

Implements os.Error by using the syscall Errno error

func (self Error) MarshalNetlink() (out []byte, err error)

Marshals an error to the wire.

func (self *Error) UnmarshalNetlink(in []byte) (err error)

Unmarshals an error from a netlink message.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

A handler implements a simple Mux for netlink messages, ensuring each query gets a unique sequence number and a channel to collect responses.

func NewHandler

func NewHandler(sock *Socket) *Handler

func (*Handler) Query

func (self *Handler) Query(msg Message, l int) (ch chan Message, err error)

Send a message. If SequenceNumber is unset, Seq() will be used to generate one.

func (*Handler) Seq

func (self *Handler) Seq() (out uint32)

Used as an atomic counter for sequence numbering. No check is made to see that sequences aren't still in use on roll-over.

func (*Handler) Start

func (self *Handler) Start(echan chan error)

Usually called in a goroutine, Start spawns a thread that demux's incoming netlink responses. Echan is used to report internal netlink errors, and may be set to nil (but you will likely miss bugs!)

type Header [HEADER_LENGTH]byte

Represents the header of a netlink.Message

func NewHeader

func NewHeader(t MessageType, f MessageFlags, seq uint32) (h *Header)

func (Header) Len

func (self Header) Len() int
func (self Header) MarshalNetlink() (out []byte, err error)

func (Header) MessageFlags

func (self Header) MessageFlags() MessageFlags

func (Header) MessageLength

func (self Header) MessageLength() uint32

func (Header) MessagePid

func (self Header) MessagePid() uint32

func (Header) MessageSequence

func (self Header) MessageSequence() uint32

func (Header) MessageType

func (self Header) MessageType() MessageType

func (*Header) SetMessageLength

func (self *Header) SetMessageLength(in uint32)

func (*Header) SetMessageSequence

func (self *Header) SetMessageSequence(in uint32)
func (self *Header) UnmarshalNetlink(in []byte) (err error)

type Message

type Message struct {
	Header *Header
	Body   []byte
}

A netlink message contains a Netlink header, and a body of bytes.

func NewMessage

func NewMessage(t MessageType, f MessageFlags, u NetlinkMarshaler) (msg *Message, err error)

Creates a new message from a marshalable object

func ReadMessage

func ReadMessage(r io.Reader) (msg *Message, err error)

Reads a message from an io.Reader, with attributes alinged to 4 bytes. NB: Netlink uses a very strict protocol, and it is encouraged that r be a bufio.Reader

func (self Message) MarshalNetlink() (out []byte, err error)

Marshals a message with aligned attributes..

type MessageFlags

type MessageFlags uint16
const (
	NLM_F_REQUEST MessageFlags = 1 << iota
	NLM_F_MULTI
	NLM_F_ACK
	NLM_F_ECHO
)

include/linux/netlink.h ALL

const (
	NLM_F_ROOT MessageFlags = 0x100 << iota
	NLM_F_MATCH
	NLM_F_ATOMIC
	NLM_F_DUMP MessageFlags = (NLM_F_ROOT | NLM_F_MATCH)
)

Valid on Queries

const (
	NLM_F_REPLACE MessageFlags = 0x100 << iota
	NLM_F_EXCL
	NLM_F_CREATE
	NLM_F_APPEND
)

Valid on Updates

type MessageType

type MessageType uint16

The MessageType consists of 5 fixed "Netlink" types that indicate message flow. All messageTypes above MIN_TYPE should be considered the 'property' of the individual netlink socket family.

const (
	NLMSG_UNSPECIFIED MessageType = iota
	NLMSG_NOOP
	NLMSG_ERROR
	NLMSG_DONE
	NLMSG_OVERRUN
	MIN_TYPE = 0x10
)

include/linux/netlink.h

type NetlinkFamily

type NetlinkFamily uint16 //?
const (
	NETLINK_ROUTE NetlinkFamily = iota
	NETLINK_UNUSED
	NETLINK_USERSOCK
	NETLINK_FIREWALL
	NETLINK_INET_DIAG
	NETLINK_NFLOG
	NETLINK_XFRM
	NETLINK_SELINUX
	NETLINK_ISCSI
	NETLINK_AUDIT
	NETLINK_FIB_LOOKUP
	NETLINK_CONNECTOR
	NETLINK_NETFILTER
	NETLINK_IP6_FW
	NETLINK_DNRTMSG
	NETLINK_KOBJECT_UEVENT
	NETLINK_GENERIC
	NETLINK_RESERVED16 // WTF is NETLINK_DM ?
	NETLINK_SCSITRANSPORT
	NETLINK_ECRYPTFS
)

The netlinkFamily is used for dialing a netlink socket. A single socket can only be bound to a single family, and thus a single Handler.

type NetlinkMarshaler

type NetlinkMarshaler interface {
	MarshalNetlink() ([]byte, error)
}

NetlinkMarshaler's are used to format netlink data.

type Socket

type Socket struct {
	// contains filtered or unexported fields
}

A netlink.Socket implements the lowest level of netlink communications.

func Dial

func Dial(nlf NetlinkFamily) (rwc *Socket, err error)

Dials a netlink socket. Usually you do not need permissions for this, though specific commands may be rejected.

func (*Socket) Bind

func (self *Socket) Bind(pid, groups uint32) (err error)

Bind the netlink socket to receive multicast messages

func (*Socket) Close

func (self *Socket) Close() (err error)

Close the netlink socket

func (*Socket) Read

func (self *Socket) Read(in []byte) (n int, err error)

Reads from a netlink socket. Generally should be a bufio with at least an 8k buffer. More for machines with large routing tables.

func (*Socket) Write

func (self *Socket) Write(in []byte) (n int, err error)

Writes to the netlink socket. Data should be (1 or more) complete netlink frames, as netlink is not friendly w/ fragmentation.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL