internal/netlink: export generic connection
All checks were successful
Test / Create distribution (push) Successful in 1m16s
Test / Sandbox (push) Successful in 3m8s
Test / ShareFS (push) Successful in 4m18s
Test / Hakurei (push) Successful in 4m23s
Test / Sandbox (race detector) (push) Successful in 5m36s
Test / Hakurei (race detector) (push) Successful in 6m43s
Test / Flake checks (push) Successful in 1m27s

This enables abstractions around some families to be implemented in a separate package.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2026-03-27 18:10:17 +09:00
parent 6a87a96838
commit 6f14fe8221
4 changed files with 49 additions and 45 deletions

View File

@@ -7,11 +7,14 @@ import (
)
// RouteConn represents a NETLINK_ROUTE socket.
type RouteConn struct{ *conn }
type RouteConn struct{ conn *Conn }
// Close closes the underlying socket.
func (c *RouteConn) Close() error { return c.conn.Close() }
// DialRoute returns the address of a newly connected [RouteConn].
func DialRoute() (*RouteConn, error) {
c, err := dial(syscall.NETLINK_ROUTE, 0)
c, err := Dial(syscall.NETLINK_ROUTE, 0)
if err != nil {
return nil, err
}
@@ -19,9 +22,9 @@ func DialRoute() (*RouteConn, error) {
}
// rtnlConsume consumes a message from rtnetlink.
func (c *conn) rtnlConsume(resp []syscall.NetlinkMessage) error {
func (c *RouteConn) rtnlConsume(resp []syscall.NetlinkMessage) error {
for i := range resp {
if err := c.checkReply(&resp[i].Header); err != nil {
if err := c.conn.checkReply(&resp[i].Header); err != nil {
return err
}
@@ -62,7 +65,7 @@ func (c *RouteConn) writeIfAddrmsg(
msg *syscall.IfAddrmsg,
attrs ...RtAttrMsg[InAddr],
) bool {
c.typ, c.flags = typ, syscall.NLM_F_REQUEST|syscall.NLM_F_ACK|flags
c.conn.typ, c.conn.flags = typ, syscall.NLM_F_REQUEST|syscall.NLM_F_ACK|flags
if !add(c.conn, msg) {
return false
}
@@ -85,7 +88,7 @@ func (c *RouteConn) SendIfAddrmsg(
if !c.writeIfAddrmsg(typ, flags, msg, attrs...) {
return syscall.ENOMEM
}
return c.Roundtrip(ctx, c.conn.rtnlConsume)
return c.conn.Roundtrip(ctx, c.rtnlConsume)
}
// writeNewaddrLo writes a RTM_NEWADDR message for the loopback address.
@@ -114,7 +117,7 @@ func (c *RouteConn) SendNewaddrLo(ctx context.Context, lo uint32) error {
if !c.writeNewaddrLo(lo) {
return syscall.ENOMEM
}
return c.Roundtrip(ctx, c.conn.rtnlConsume)
return c.conn.Roundtrip(ctx, c.rtnlConsume)
}
// writeIfInfomsg writes an ifinfomsg structure to conn.
@@ -122,7 +125,7 @@ func (c *RouteConn) writeIfInfomsg(
typ, flags uint16,
msg *syscall.IfInfomsg,
) bool {
c.typ, c.flags = typ, syscall.NLM_F_REQUEST|syscall.NLM_F_ACK|flags
c.conn.typ, c.conn.flags = typ, syscall.NLM_F_REQUEST|syscall.NLM_F_ACK|flags
return add(c.conn, msg)
}
@@ -135,5 +138,5 @@ func (c *RouteConn) SendIfInfomsg(
if !c.writeIfInfomsg(typ, flags, msg) {
return syscall.ENOMEM
}
return c.Roundtrip(ctx, c.conn.rtnlConsume)
return c.conn.Roundtrip(ctx, c.rtnlConsume)
}