s6-netdev/interfaces.go

73 lines
1.5 KiB
Go

package s6netdev
import (
"fmt"
"net"
"net/netip"
)
type IfMap map[string]*Iface
func (im IfMap) AddIf(i *Iface) {
im[i.Name] = i
}
func (i *Iface) NetdevIfaceAddAddr(addr string) (err error) {
var (
a netip.Prefix
)
if a, err = netip.ParsePrefix(addr); err != nil {
err = fmt.Errorf("Failed to parse address %s, %w", addr, err)
}
i.Addresses = append(i.Addresses, a)
return
}
func (i *Iface) NetdevIfaceAddRoute(def bool, route, r_type, via string, vrf *Iface, table *RouteTable, metric Metric) (err error) {
var (
r = Route{
Default: def,
Type: r_type,
Vrf: vrf,
Table: table,
Metric: metric,
}
)
if via != "" {
if r.Via, err = netip.ParseAddr(via); err != nil {
err = fmt.Errorf("Failed to parse via %s, %w", via, err)
}
}
if !def {
if r.Net, err = netip.ParsePrefix(route); err != nil {
err = fmt.Errorf("Failed to parse via %s, %w", via, err)
}
}
i.Routes = append(i.Routes, r)
return
}
func (i *Iface) NetdevIfaceMAC(mac string) (err error) {
i.MACAddr, err = net.ParseMAC(mac)
return
}
func (i *Iface) NetdevIfaceEnslave(s ...*Iface) {
i.Slaves = append(i.Slaves, s...)
}
func (i *Iface) NetdevIfaceSysctl(ipv int, key, value, def string) {
l := &i.Sysctls.V4
if ipv == 6 {
l = &i.Sysctls.V6
}
*l = append(*l, Property{Key: key, Value: value, Default: def})
}
func (i *Iface) NetdevIfaceProperty(key, value, def string) {
i.Properties = append(i.Properties, Property{Key: key, Value: value, Default: def})
}