ifupdown

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2023 License: MIT Imports: 11 Imported by: 0

README

ifupdown

GoDoc

golang library for working with network interfaces defined via ifupdown. commonly recognized as /etc/network/interfaces.

library

features
  • read+parse interfaces file
  • write interfaces file
  • validate interfaces file (basic)
  • validate interfaces file (thorough)
  • translate interfaces file to JSON
  • translate JSON to interfaces file

cmd

  • ifup2json - translate interfaces file to JSON
  • json2ifup - translate JSON to interfaces file
example usage
ifup2json

cat /etc/network/interfaces | ifup2json

{
	"eth2": {
		"name": "eth2",
		"auto": true,
		"address": "192.168.69.5",
		"netmask": "////AA==",
		"gateway": "192.168.69.1",
		"config": 3,
		"version": 1,
		"hooks": {
			"pre_up": [
				"echo yeet"
			],
			"post_down": [
				"echo yeeted"
			]
		}
	},
	"lo": {
		"name": "lo",
		"auto": true,
		"config": 1,
		"version": 1,
		"hooks": {}
	},
	"ns3": {
		"name": "ns3",
		"auto": true,
		"address": "10.9.0.6",
		"netmask": "////AAAAAAAAAAAAAAAAAA==",
		"config": 3,
		"version": 1,
		"hooks": {
			"pre_up": [
				"ip link add dev ns3 type wireguard"
			],
			"post_up": [
				"wg setconf ns3 /etc/wireguard/ns3.conf"
			]
		}
	}
}
json2ifup

cat /etc/network/interfaces | ifup2json | json2ifup

auto eth2
iface eth2 inet static
	address 192.168.69.5
	netmask 255.255.255.0
	gateway 192.168.69.1
	pre-up echo yeet
	post-down echo yeeted

auto lo
iface lo inet loopback

auto ns3
iface ns3 inet static
	address 10.9.0.6
	netmask 255.255.255.0
	pre-up ip link add dev ns3 type wireguard
	post-up wg setconf ns3 /etc/wireguard/ns3.conf

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidAddress        = errors.New("invalid address")
	ErrInvalidMask           = errors.New("invalid mask")
	ErrInvalidBroadcast      = errors.New("invalid broadcast")
	ErrInvalidGateway        = errors.New("invalid gateway")
	ErrInvalidAddressVersion = errors.New("invalid address version")
	ErrAddressSetWhenDHCP    = errors.New("address set when DHCP enabled")
	ErrAddressNotSetStatic   = errors.New("address not set with static config")
	ErrMaskNotSetStatic      = errors.New("mask not set with static config")
	ErrAdressNotLoopback     = errors.New("address must be loopback when config is loopback")
	ErrInterfaceHasErrors    = errors.New("interface has errors")
	ErrUnallocatedInterface  = errors.New("unallocated interface")
	ErrInvalidIfaceData      = errors.New("invalid interface data provided")
	ErrMultipleInterfaces    = errors.New("multiple interfaces in data provided")
)

Functions

This section is empty.

Types

type AddressConfig

type AddressConfig uint8
const (
	AddressConfigUnset AddressConfig = iota
	AddressConfigLoopback
	AddressConfigDHCP
	AddressConfigStatic
	AddressConfigManual
)

func (AddressConfig) String

func (ac AddressConfig) String() string

type AddressVersion

type AddressVersion uint8
const (
	AddressVersionNil AddressVersion = iota
	AddressVersion4
	AddressVersion6
)

func (AddressVersion) String

func (at AddressVersion) String() string

type Hooks

type Hooks struct {
	// PreUp of the interface
	PreUp []string `json:"pre_up,omitempty"`
	// PostUp of the interface
	PostUp []string `json:"post_up,omitempty"`
	// PreDown of the interface
	PreDown []string `json:"pre_down,omitempty"`
	// PostDown of the interface
	PostDown []string `json:"post_down,omitempty"`
}

type Interfaces

type Interfaces map[string]*NetworkInterface

func (Interfaces) Read

func (i Interfaces) Read(p []byte) (int, error)

func (Interfaces) String

func (i Interfaces) String() string

func (Interfaces) UnmarshalJSON

func (i Interfaces) UnmarshalJSON(data []byte) error

type MultiParser

type MultiParser struct {
	Interfaces map[string]*NetworkInterface
	Errs       []error
	// contains filtered or unexported fields
}

func NewMultiParser

func NewMultiParser() *MultiParser

func (*MultiParser) Parse

func (p *MultiParser) Parse() (Interfaces, error)

func (*MultiParser) Write

func (p *MultiParser) Write(data []byte) (int, error)

type NetworkInterface

type NetworkInterface struct {
	// Name of the interface.
	Name string `json:"name"`
	// Hotplug determines if hot plugging is allowed.
	Hotplug bool `json:"hotplug,omitempty"`
	// Auto determines if the interface is automatically brought up.
	Auto bool `json:"auto,omitempty"`
	// Address determines the static IP address of the interface.
	Address net.IP `json:"address,omitempty"`

	// Netmask determines the netmask of the interface.
	Netmask net.IPMask `json:"netmask,omitempty"`

	Broadcast net.IP         `json:"broadcast,omitempty"`
	Gateway   net.IP         `json:"gateway,omitempty"`
	Config    AddressConfig  `json:"config,omitempty"`
	Version   AddressVersion `json:"version,omitempty"`

	// DNSServers of the interface.
	DNSServers []net.IP `json:"dns_servers,omitempty"`
	// DNSSearch of the interface.
	DNSSearch []string `json:"dns_search,omitempty"`
	// MACAddress of the interface.
	MACAddress net.HardwareAddr `json:"mac_address,omitempty"`
	// Hooks contains the pre/post up/down hooks.
	Hooks Hooks `json:"hooks,omitempty"`

	*sync.RWMutex
	// contains filtered or unexported fields
}

NetworkInterface follows the format of ifupdown /etc/network/interfaces

func NewNetworkInterface

func NewNetworkInterface(name string) *NetworkInterface

func (*NetworkInterface) Read

func (iface *NetworkInterface) Read(p []byte) (int, error)

func (*NetworkInterface) String

func (iface *NetworkInterface) String() string

func (*NetworkInterface) Validate

func (iface *NetworkInterface) Validate() error

func (*NetworkInterface) WithAddress

func (iface *NetworkInterface) WithAddress(address string) *NetworkInterface

func (*NetworkInterface) WithAddressConfig

func (iface *NetworkInterface) WithAddressConfig(config AddressConfig) *NetworkInterface

func (*NetworkInterface) WithAddressVersion

func (iface *NetworkInterface) WithAddressVersion(version AddressVersion) *NetworkInterface

func (*NetworkInterface) WithBroadcast

func (iface *NetworkInterface) WithBroadcast(broadcast string) *NetworkInterface

func (*NetworkInterface) WithConfigMethod

func (iface *NetworkInterface) WithConfigMethod(config AddressConfig) *NetworkInterface

func (*NetworkInterface) WithDHCP

func (iface *NetworkInterface) WithDHCP() *NetworkInterface

func (*NetworkInterface) WithDNS

func (iface *NetworkInterface) WithDNS(dnsServers []string) *NetworkInterface

func (*NetworkInterface) WithDNSSearch

func (iface *NetworkInterface) WithDNSSearch(dnsSearch []string) *NetworkInterface

func (*NetworkInterface) WithGateway

func (iface *NetworkInterface) WithGateway(gateway string) *NetworkInterface

func (*NetworkInterface) WithLoopback

func (iface *NetworkInterface) WithLoopback() *NetworkInterface

func (*NetworkInterface) WithMACAddress

func (iface *NetworkInterface) WithMACAddress(macAddress string) *NetworkInterface

func (*NetworkInterface) WithManual

func (iface *NetworkInterface) WithManual() *NetworkInterface

func (*NetworkInterface) WithNetmask

func (iface *NetworkInterface) WithNetmask(mask, bits int) *NetworkInterface

func (*NetworkInterface) WithStatic

func (iface *NetworkInterface) WithStatic() *NetworkInterface

func (*NetworkInterface) WithVersion

func (iface *NetworkInterface) WithVersion(version AddressVersion) *NetworkInterface

func (*NetworkInterface) Write

func (iface *NetworkInterface) Write(p []byte) (int, error)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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