Skip to content

Commit

Permalink
Conn - set read/write deadlines and buffer sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-mo committed Apr 4, 2020
1 parent dbdd426 commit ddd1644
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
34 changes: 34 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package netfilter

import (
"sync"
"time"

"github.com/pkg/errors"

Expand Down Expand Up @@ -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)
}
34 changes: 32 additions & 2 deletions conn_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package netfilter

import (
"math"
"testing"
"time"

"github.com/pkg/errors"

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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")
}

0 comments on commit ddd1644

Please sign in to comment.