s6-netdev/interfaces.go

73 lines
1.5 KiB
Go
Raw Normal View History

2024-09-04 12:22:10 +00:00
package s6netdev
import (
"fmt"
"net"
"net/netip"
2024-09-04 12:22:10 +00:00
)
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)
2024-09-04 12:22:10 +00:00
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,
2024-09-04 12:22:10 +00:00
}
)
2024-09-04 12:22:10 +00:00
if via != "" {
if r.Via, err = netip.ParseAddr(via); err != nil {
err = fmt.Errorf("Failed to parse via %s, %w", via, err)
}
2024-09-04 12:22:10 +00:00
}
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)
2024-09-04 12:22:10 +00:00
return
}
func (i *Iface) NetdevIfaceMAC(mac string) (err error) {
i.MACAddr, err = net.ParseMAC(mac)
return
}
2024-09-04 12:22:10 +00:00
func (i *Iface) NetdevIfaceEnslave(s ...*Iface) {
i.Slaves = append(i.Slaves, s...)
}
2024-09-04 12:22:10 +00:00
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})
}
2024-09-04 12:22:10 +00:00
func (i *Iface) NetdevIfaceProperty(key, value, def string) {
i.Properties = append(i.Properties, Property{Key: key, Value: value, Default: def})
2024-09-04 12:22:10 +00:00
}