Compare commits

..

1 Commits

Author SHA1 Message Date
Paul Greenberg 38a7441978
Merge dae73eaa9c into 583cd2bdea 2024-09-16 11:40:25 +02:00
1 changed files with 7 additions and 36 deletions

33
conn.go
View File

@ -42,15 +42,11 @@ type Conn struct {
messages []netlink.Message messages []netlink.Message
err error err error
nlconn *netlink.Conn // netlink socket using NETLINK_NETFILTER protocol. nlconn *netlink.Conn // netlink socket using NETLINK_NETFILTER protocol.
sockOptions []SockOption
} }
// ConnOption is an option to change the behavior of the nftables Conn returned by Open. // ConnOption is an option to change the behavior of the nftables Conn returned by Open.
type ConnOption func(*Conn) type ConnOption func(*Conn)
// SockOption is an option to change the behavior of the netlink socket used by the nftables Conn.
type SockOption func(*netlink.Conn) error
// New returns a netlink connection for querying and modifying nftables. Some // New returns a netlink connection for querying and modifying nftables. Some
// aspects of the new netlink connection can be configured using the options // aspects of the new netlink connection can be configured using the options
// WithNetNSFd, WithTestDial, and AsLasting. // WithNetNSFd, WithTestDial, and AsLasting.
@ -105,14 +101,6 @@ func WithTestDial(f nltest.Func) ConnOption {
} }
} }
// WithSockOptions sets the specified socket options when creating a new netlink
// connection.
func WithSockOptions(opts ...SockOption) ConnOption {
return func(cc *Conn) {
cc.sockOptions = append(cc.sockOptions, opts...)
}
}
// netlinkCloser is returned by netlinkConn(UnderLock) and must be called after // netlinkCloser is returned by netlinkConn(UnderLock) and must be called after
// being done with the returned netlink connection in order to properly close // being done with the returned netlink connection in order to properly close
// this connection, if necessary. // this connection, if necessary.
@ -296,28 +284,11 @@ func (cc *Conn) FlushRuleset() {
} }
func (cc *Conn) dialNetlink() (*netlink.Conn, error) { func (cc *Conn) dialNetlink() (*netlink.Conn, error) {
var (
conn *netlink.Conn
err error
)
if cc.TestDial != nil { if cc.TestDial != nil {
conn = nltest.Dial(cc.TestDial) return nltest.Dial(cc.TestDial), nil
} else {
conn, err = netlink.Dial(unix.NETLINK_NETFILTER, &netlink.Config{NetNS: cc.NetNS})
} }
if err != nil { return netlink.Dial(unix.NETLINK_NETFILTER, &netlink.Config{NetNS: cc.NetNS})
return nil, err
}
for _, opt := range cc.sockOptions {
if err := opt(conn); err != nil {
return nil, err
}
}
return conn, nil
} }
func (cc *Conn) setErr(err error) { func (cc *Conn) setErr(err error) {