Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose sockets attribute of nl.Handle #984

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions handle_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var pkgHandle = &Handle{}
// same netlink family share the same netlink socket,
// which gets released when the handle is Close'd.
type Handle struct {
sockets map[int]*nl.SocketHandle
Sockets map[int]*nl.SocketHandle
lookupByDump bool
}

Expand All @@ -39,7 +39,7 @@ func GetSocketTimeout() time.Duration {

// SupportsNetlinkFamily reports whether the passed netlink family is supported by this Handle
func (h *Handle) SupportsNetlinkFamily(nlFamily int) bool {
_, ok := h.sockets[nlFamily]
_, ok := h.Sockets[nlFamily]
return ok
}

Expand All @@ -60,7 +60,7 @@ func (h *Handle) SetSocketTimeout(to time.Duration) error {
return fmt.Errorf("invalid timeout, minimul value is %s", time.Microsecond)
}
tv := unix.NsecToTimeval(to.Nanoseconds())
for _, sh := range h.sockets {
for _, sh := range h.Sockets {
if err := sh.Socket.SetSendTimeout(&tv); err != nil {
return err
}
Expand All @@ -79,7 +79,7 @@ func (h *Handle) SetSocketReceiveBufferSize(size int, force bool) error {
if force {
opt = unix.SO_RCVBUFFORCE
}
for _, sh := range h.sockets {
for _, sh := range h.Sockets {
fd := sh.Socket.GetFd()
err := unix.SetsockoptInt(fd, unix.SOL_SOCKET, opt, size)
if err != nil {
Expand All @@ -93,9 +93,9 @@ func (h *Handle) SetSocketReceiveBufferSize(size int, force bool) error {
// socket in the netlink handle. The retrieved value should be the
// double to the one set for SetSocketReceiveBufferSize.
func (h *Handle) GetSocketReceiveBufferSize() ([]int, error) {
results := make([]int, len(h.sockets))
results := make([]int, len(h.Sockets))
i := 0
for _, sh := range h.sockets {
for _, sh := range h.Sockets {
fd := sh.Socket.GetFd()
size, err := unix.GetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_RCVBUF)
if err != nil {
Expand All @@ -109,7 +109,7 @@ func (h *Handle) GetSocketReceiveBufferSize() ([]int, error) {

// SetStrictCheck sets the strict check socket option for each socket in the netlink handle. Returns early if any set operation fails
func (h *Handle) SetStrictCheck(state bool) error {
for _, sh := range h.sockets {
for _, sh := range h.Sockets {
var stateInt int = 0
if state {
stateInt = 1
Expand All @@ -136,7 +136,7 @@ func NewHandleAtFrom(newNs, curNs netns.NsHandle) (*Handle, error) {
}

func newHandle(newNs, curNs netns.NsHandle, nlFamilies ...int) (*Handle, error) {
h := &Handle{sockets: map[int]*nl.SocketHandle{}}
h := &Handle{Sockets: map[int]*nl.SocketHandle{}}
fams := nl.SupportedNlFamilies
if len(nlFamilies) != 0 {
fams = nlFamilies
Expand All @@ -146,17 +146,17 @@ func newHandle(newNs, curNs netns.NsHandle, nlFamilies ...int) (*Handle, error)
if err != nil {
return nil, err
}
h.sockets[f] = &nl.SocketHandle{Socket: s}
h.Sockets[f] = &nl.SocketHandle{Socket: s}
}
return h, nil
}

// Close releases the resources allocated to this handle
func (h *Handle) Close() {
for _, sh := range h.sockets {
for _, sh := range h.Sockets {
sh.Close()
}
h.sockets = nil
h.Sockets = nil
}

// Delete releases the resources allocated to this handle
Expand All @@ -169,7 +169,7 @@ func (h *Handle) Delete() {

func (h *Handle) newNetlinkRequest(proto, flags int) *nl.NetlinkRequest {
// Do this so that package API still use nl package variable nextSeqNr
if h.sockets == nil {
if h.Sockets == nil {
return nl.NewNetlinkRequest(proto, flags)
}
return &nl.NetlinkRequest{
Expand All @@ -178,6 +178,6 @@ func (h *Handle) newNetlinkRequest(proto, flags int) *nl.NetlinkRequest {
Type: uint16(proto),
Flags: unix.NLM_F_REQUEST | uint16(flags),
},
Sockets: h.sockets,
Sockets: h.Sockets,
}
}
13 changes: 7 additions & 6 deletions handle_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build linux
// +build linux

package netlink
Expand Down Expand Up @@ -25,7 +26,7 @@ func TestHandleCreateClose(t *testing.T) {
t.Fatal(err)
}
for _, f := range nl.SupportedNlFamilies {
sh, ok := h.sockets[f]
sh, ok := h.Sockets[f]
if !ok {
t.Fatalf("Handle socket(s) for family %d was not created", f)
}
Expand All @@ -35,7 +36,7 @@ func TestHandleCreateClose(t *testing.T) {
}

h.Close()
if h.sockets != nil {
if h.Sockets != nil {
t.Fatalf("Handle socket(s) were not closed")
}
}
Expand Down Expand Up @@ -121,13 +122,13 @@ func TestHandleTimeout(t *testing.T) {
}
defer h.Close()

for _, sh := range h.sockets {
for _, sh := range h.Sockets {
verifySockTimeVal(t, sh.Socket.GetFd(), unix.Timeval{Sec: 0, Usec: 0})
}

h.SetSocketTimeout(2*time.Second + 8*time.Millisecond)

for _, sh := range h.sockets {
for _, sh := range h.Sockets {
verifySockTimeVal(t, sh.Socket.GetFd(), unix.Timeval{Sec: 2, Usec: 8000})
}
}
Expand All @@ -145,9 +146,9 @@ func TestHandleReceiveBuffer(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if len(sizes) != len(h.sockets) {
if len(sizes) != len(h.Sockets) {
t.Fatalf("Unexpected number of socket buffer sizes: %d (expected %d)",
len(sizes), len(h.sockets))
len(sizes), len(h.Sockets))
}
for _, s := range sizes {
if s < 65536 || s > 2*65536 {
Expand Down
Loading