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

Add Sequence number to OpError type #218

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions conn_linux_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@ func TestConnReceiveErrorLinux(t *testing.T) {
Header: netlink.Header{
Length: 20,
Type: netlink.Error,
Sequence: 1,
Sequence: 1234,
PID: 1,
},
// -2, little endian (ENOENT)
Data: []byte{0xfe, 0xff, 0xff, 0xff},
}},
want: &netlink.OpError{
Op: "receive",
Err: unix.ENOENT,
Op: "receive",
Err: unix.ENOENT,
Sequence: 1234,
},
},
{
Expand Down
5 changes: 5 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ type OpError struct {
// library.
Err error

// Sequence contains the sequence number of the netlink message which caused
// this error. This will only be valid if Op is "receive", otherwise this
// field will be set to zero.
Sequence uint32

// Message and Offset contain additional error information provided by the
// kernel when the ExtendedAcknowledge option is set on a Conn and the
// kernel indicates the AcknowledgeTLVs flag in a response. If this option
Expand Down
21 changes: 13 additions & 8 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,24 +276,27 @@ func checkMessage(m Message) error {
return nil
}

oerr := &OpError{
Op: "receive",
Sequence: m.Header.Sequence,
}

// Errno occupies 4 bytes.
const endErrno = 4
if len(m.Data) < endErrno {
return newOpError("receive", errShortErrorMessage)
oerr.Err = errShortErrorMessage
return oerr
}

c := nlenc.Int32(m.Data[:endErrno])
if c == 0 {
// 0 indicates no error.
return nil
}

oerr := &OpError{
Op: "receive",
} else {
// Error code is a negative integer, convert it into an OS-specific raw
// system call error, but do not wrap with os.NewSyscallError to signify
// that this error was produced by a netlink message; not a system call.
Err: newError(-1 * int(c)),
oerr.Err = newError(-1 * int(c))
}

// TODO(mdlayher): investigate the Capped flag.
Expand All @@ -309,7 +312,8 @@ func checkMessage(m Message) error {
if hasHeader {
// There is an nlmsghdr preceding the TLVs.
if len(m.Data) < endErrno+nlmsgHeaderLen {
return newOpError("receive", errShortErrorMessage)
oerr.Err = errShortErrorMessage
return oerr
}

// The TLVs should be at the offset indicated by the nlmsghdr.length,
Expand All @@ -319,7 +323,8 @@ func checkMessage(m Message) error {
off = endErrno + int(h.Length)

if len(m.Data) < off {
return newOpError("receive", errShortErrorMessage)
oerr.Err = errShortErrorMessage
return oerr
}
} else {
// There is no nlmsghdr preceding the TLVs, parse them directly.
Expand Down