diff --git a/attribute.go b/attribute.go index 6e7a5a8..48ff808 100644 --- a/attribute.go +++ b/attribute.go @@ -21,7 +21,7 @@ func NewAttributeDecoder(b []byte) (*netlink.AttributeDecoder, error) { return ad, nil } -// NewAttributeDecoder instantiates a new netlink.AttributeEncoder +// NewAttributeEncoder instantiates a new netlink.AttributeEncoder // configured with a Big Endian byte order. func NewAttributeEncoder() *netlink.AttributeEncoder { ae := netlink.NewAttributeEncoder() diff --git a/conn.go b/conn.go index 191c39b..ed72d68 100644 --- a/conn.go +++ b/conn.go @@ -2,6 +2,7 @@ package netfilter import ( "sync" + "time" "github.com/pkg/errors" @@ -122,3 +123,36 @@ func (c *Conn) IsMulticast() bool { func (c *Conn) SetOption(option netlink.ConnOption, enable bool) error { return c.conn.SetOption(option, enable) } + +// SetDeadline sets the read and write deadlines associated with the connection. +// +// Deadline functionality is only supported on Go 1.12+. Calling this function +// on older versions of Go will result in an error. +func (c *Conn) SetDeadline(t time.Time) error { + return c.conn.SetDeadline(t) +} + +// SetReadDeadline sets the read deadline associated with the connection. +// +// Deadline functionality is only supported on Go 1.12+. Calling this function +// on older versions of Go will result in an error. +func (c *Conn) SetReadDeadline(t time.Time) error { + return c.conn.SetReadDeadline(t) +} + +// SetWriteDeadline sets the write deadline associated with the connection. +// +// Deadline functionality is only supported on Go 1.12+. Calling this function on older versions of Go will result in an error. +func (c *Conn) SetWriteDeadline(t time.Time) error { + return c.conn.SetWriteDeadline(t) +} + +// SetReadBuffer sets the size of the operating system's receive buffer associated with the Conn. +func (c *Conn) SetReadBuffer(bytes int) error { + return c.conn.SetReadBuffer(bytes) +} + +// SetWriteBuffer sets the size of the operating system's transmit buffer associated with the Conn. +func (c *Conn) SetWriteBuffer(bytes int) error { + return c.conn.SetWriteBuffer(bytes) +} diff --git a/conn_test.go b/conn_test.go index 6f2c1f1..aa01cb7 100644 --- a/conn_test.go +++ b/conn_test.go @@ -1,7 +1,9 @@ package netfilter import ( + "math" "testing" + "time" "github.com/pkg/errors" @@ -81,7 +83,7 @@ func TestConnQuery(t *testing.T) { func TestConnQueryMulticast(t *testing.T) { - // Dummy Conn initially marked as Multicast + // Dummy Conn initially marked as Multicast. connMulticast := Conn{isMulticast: true} assert.Equal(t, connMulticast.IsMulticast(), true) @@ -95,7 +97,7 @@ func TestConnQueryMulticast(t *testing.T) { func TestConnReceive(t *testing.T) { - // Inject a message directly into the nltest connection + // Inject a message directly into the nltest connection. _, _ = connEcho.conn.Send(nlMsgReqAck) // Drain the socket @@ -104,3 +106,31 @@ func TestConnReceive(t *testing.T) { t.Fatalf("error in Receive: %v", err) } } + +func TestConnDeadline(t *testing.T) { + + c, err := Dial(nil) + require.NoError(t, err, "opening Conn") + + require.NoError(t, c.SetDeadline(time.Now().Add(time.Second)), "setting global deadline") + require.NoError(t, c.SetReadDeadline(time.Now().Add(time.Second)), "setting read deadline") + require.NoError(t, c.SetWriteDeadline(time.Now().Add(time.Second)), "setting write deadline") + + err = c.Close() + require.NoError(t, err, "closing Conn") +} + +func TestConnBuffers(t *testing.T) { + + c, err := Dial(nil) + require.NoError(t, err, "opening Conn") + + require.NoError(t, c.SetReadBuffer(256), "setting read buffer") + require.Error(t, c.SetReadBuffer(math.MaxInt32+1), "setting invalid read buffer") + + require.NoError(t, c.SetWriteBuffer(256), "setting write buffer") + require.Error(t, c.SetWriteBuffer(math.MaxInt32+1), "setting invalid write buffer") + + err = c.Close() + require.NoError(t, err, "closing Conn") +}