netlink

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2021 License: MIT Imports: 6 Imported by: 0

README

netlink-go is a netlink API for golang. It is designed to be easy to use and abstract away some of the lifecycle bulk and boilerplate.

THIS PROJECT IS NOT YET USABLE AND SHOULD BE AVOIDED FOR ALL CRITICAL/PRODUCTION SYSTEMS

This is, for now, a mostly learning exercise. A stable version is being worked towards, that will support all of the RTNETLINK features.

Supported Features and Modules

RTNETLINK refers to the linux routing socket. It allows for reading and altering the kernel's routing tables.

Currently, only notifications for new and deleted routes are supported

Examples

Simple Alert Notify

c, err := netlink.New()
if err != nil {
    //handle err
}

log.Println("Register alert handler")
notifyChan := make(chan *netlink.Message, 100)
c.Notify(notifyChan)

for msg := range notifyChan {
    //handle message
}

Simple Alert Callback

c, err := netlink.New()
if err != nil {
    //handle err
}

log.Println("Register alert handler")
notifyChan := make(chan *netlink.Message, 100)
c.Handle(func(m *netlink.Message){
    //handle m
})

Documentation

Overview

netlink is a basic wrapper for the linux netlink kernel module, designed to be interacted with from userspace.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

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

Connection is a wrapper to the underlying linux socket that communicates with the kernel module. Despite its name, the underyling communication is not a long lived. Binds to the socket only happen when the Connection is ready to poll, and they are closed once they are read (or a timeout is hit)

func New

func New() (*Connection, error)

Net returns a new Connection instance that will bind to the kernel socket using the AF_NETLINK and RTMGRP_IPV4_ROUTE flags

func NewWithOpts

func NewWithOpts(opts *ConnectionOptions) (*Connection, error)

NewWithOpts returns a new Connection instance that will bind to the kernel socket using the specified parameter flags

func (*Connection) Close

func (z *Connection) Close() error

Close instructs the running routines to terminate

func (*Connection) Handle

func (z *Connection) Handle(h MessageHandler)

Handle registers a function callback that all incoming messages will be passed to. Notify takes precedence over handle, and this method will not be called if a notify channel has been registered (and will also stop being called after a notify channel has been registered)

func (*Connection) Notify

func (z *Connection) Notify(c chan *Message)

Notify registers a provided chan *Message that the connection will write all incoming requests to. Notify takes precedence over Handle

func (*Connection) SendMessage

func (z *Connection) SendMessage(m *Message) error

SendMessage marshals a payload and delivers it to the kernel socket. Currently not implemented

type ConnectionOptions

type ConnectionOptions struct {
	Family uint16
	Groups uint32
}

ConnectionOptions defines the flag passed at bind time to the kernel socket. It contains the necessary parameters to define which messages and sub-modules should be available to this socket

type Header struct {
	Length uint32
	Type   uint16
	Flags  uint16
	Pid    uint32
}

Header is a basic type wrapper for netlink header messages

type Message

type Message struct {
	Header
	Data []byte
}

Message is a basic type wrapper that extends a header with the corresponding data body (if present)

func DecodeMessge

func DecodeMessge(b []byte) (*Message, error)

DecodeMessage unmarshals a byte slice into a Message pointer, returning an error if the raw bytes cannot be unmarshalled correctly

type MessageHandler

type MessageHandler func(*Message)

MessageHandler is a function type that defines a basic callback type

Jump to

Keyboard shortcuts

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