netlink

package
v1.0.3-0...-c2e6ac5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 11, 2022 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

This file is copied and modified from elastic/gosigar https://github.com/elastic/gosigar/tree/master/sys/linux

modified point:

  • delete NewInetDiagReq. gopsutil only support v2.
  • change connection state strings.

Index

Constants

View Source
const (
	// AllTCPStates is a flag to request all sockets in any TCP state.
	AllTCPStates = ^uint32(0)

	// TCPDIAG_GETSOCK is the netlink message type for requesting TCP diag data.
	// https://github.com/torvalds/linux/blob/v4.0/include/uapi/linux/inet_diag.h#L7
	TCPDIAG_GETSOCK = 18

	// SOCK_DIAG_BY_FAMILY is the netlink message type for requestion socket
	// diag data by family. This is newer and can be used with inet_diag_req_v2.
	// https://github.com/torvalds/linux/blob/v4.0/include/uapi/linux/sock_diag.h#L6
	SOCK_DIAG_BY_FAMILY = 20
)
View Source
const (
	INET_DIAG_NONE    = 0
	INET_DIAG_MEMINFO = 1 << iota
	INET_DIAG_INFO
	INET_DIAG_VEGASINFO
	INET_DIAG_CONG
	INET_DIAG_TOS
	INET_DIAG_TCLASS
	INET_DIAG_SKMEMINFO
	INET_DIAG_SHUTDOWN
	INET_DIAG_DCTCPINFO
	INET_DIAG_PROTOCOL /* response attribute only */
	INET_DIAG_SKV6ONLY
	INET_DIAG_LOCALS
	INET_DIAG_PEERS
	INET_DIAG_PAD
	INET_DIAG_MARK
)

Extensions that can be used in the InetDiagReqV2 request to ask for additional data. https://github.com/torvalds/linux/blob/v4.0/include/uapi/linux/inet_diag.h#L103

View Source
const (
	UDIAG_SHOW_NAME    = 0x1
	UDIAG_SHOW_VFS     = 0x00000002 /* show VFS inode info */
	UDIAG_SHOW_PEER    = 0x00000004 /* show peer socket info */
	UDIAG_SHOW_ICONS   = 0x00000008 /* show pending connections */
	UDIAG_SHOW_RQLEN   = 0x00000010 /* show skb receive queue len */
	UDIAG_SHOW_MEMINFO = 0x00000020 /* show memory info of a socket */
)

Show flags in unix diag request. https://github.com/torvalds/linux/blob/v4.0/include/uapi/linux/unix_diag.h#L16

View Source
const (
	/* UNIX_DIAG_NONE, standard nl API requires this attribute!  */
	UNIX_DIAG_NAME = iota
	UNIX_DIAG_VFS
	UNIX_DIAG_PEER
	UNIX_DIAG_ICONS
	UNIX_DIAG_RQLEN
	UNIX_DIAG_MEMINFO
	UNIX_DIAG_SHUTDOWN
)

Variables

This section is empty.

Functions

func GetEndian

func GetEndian() binary.ByteOrder

func NewInetDiagReqV2

func NewInetDiagReqV2(af uint8, proto uint8) syscall.NetlinkMessage

NewInetDiagReqV2 returns a new NetlinkMessage whose payload is an InetDiagReqV2. Callers should set their own sequence number in the returned message header.

func NewUnixDiagReq

func NewUnixDiagReq() syscall.NetlinkMessage

NewUnixDiagReq returns a new NetlinkMessage whose payload is an UnixDiagReq. Callers should set their own sequence number in the returned message header.

func ParseNetlinkError

func ParseNetlinkError(netlinkData []byte) error

ParseNetlinkError parses the errno from the data section of a syscall.NetlinkMessage. If netlinkData is less than 4 bytes an error describing the problem will be returned.

func ParseNetlinkRouteAttr

func ParseNetlinkRouteAttr(b []byte) ([]syscall.NetlinkRouteAttr, error)

ParseNetlinkRouteAttr parse Route attr from NetlinkMessage.Data

func RtaLength

func RtaLength(len int) int

RtaLength is a implementation of RTA_LENGTH macro.

func RtaPayload

func RtaPayload(rta *syscall.RtAttr) int

RtaPayload is a implementation of RTA_PAYLOAD macro.

Types

type InetDiagMsg

type InetDiagMsg struct {
	Family  uint8 // Address family.
	State   uint8 // TCP State
	Timer   uint8
	Retrans uint8

	ID InetDiagSockID

	Expires uint32
	RQueue  uint32 // Recv-Q
	WQueue  uint32 // Send-Q
	UID     uint32 // Socket owner UID
	Inode   uint32 // Inode of socket.
}

InetDiagMsg (inet_diag_msg) is the base info structure. It contains socket identity (addrs/ports/cookie) and the information shown by netstat. https://github.com/torvalds/linux/blob/v4.0/include/uapi/linux/inet_diag.h#L86

func InetConnections

func InetConnections(af uint8, proto uint8) ([]*InetDiagMsg, error)

func InetDiag

func InetDiag(request syscall.NetlinkMessage) ([]*InetDiagMsg, error)

InetDiag sends the given netlink request parses the responses with the assumption that they are inet_diag_msgs. This will allocate a temporary buffer for reading from the socket whose size will be the length of a page (usually 32k). Use NetlinkInetDiagWithBuf if you want to provide your own buffer.

func InetDiagWithBuf

func InetDiagWithBuf(request syscall.NetlinkMessage, readBuf []byte, resp io.Writer) ([]*InetDiagMsg, error)

InetDiagWithBuf sends the given netlink request parses the responses with the assumption that they are inet_diag_msgs. readBuf will be used to hold the raw data read from the socket. If the length is not large enough to hold the socket contents the data will be truncated. If readBuf is nil then a temporary buffer will be allocated for each invocation. The resp writer, if non-nil, will receive a copy of all bytes read (this is useful for debugging).

func ParseInetDiagMsg

func ParseInetDiagMsg(b []byte) (*InetDiagMsg, error)

ParseInetDiagMsg parse an InetDiagMsg from a byte slice. It assumes the InetDiagMsg starts at the beginning of b. Invoke this method to parse the payload of a netlink response.

func (InetDiagMsg) DstIP

func (m InetDiagMsg) DstIP() net.IP

DstIP returns the destination (remote) IP.

func (InetDiagMsg) DstPort

func (m InetDiagMsg) DstPort() int

DstPort returns the destination (remote) port.

func (*InetDiagMsg) FastHash

func (m *InetDiagMsg) FastHash() uint64

FastHash returns a hash calculated using FNV-1 of the source and destination addresses.

func (InetDiagMsg) SrcIP

func (m InetDiagMsg) SrcIP() net.IP

SrcIP returns the source (local) IP.

func (InetDiagMsg) SrcPort

func (m InetDiagMsg) SrcPort() int

SrcPort returns the source (local) port.

type InetDiagReqV2

type InetDiagReqV2 struct {
	Family   uint8
	Protocol uint8
	Ext      uint8
	Pad      uint8
	States   uint32
	ID       InetDiagSockID
}

InetDiagReqV2 (inet_diag_req_v2) is used to request diagnostic data. https://github.com/torvalds/linux/blob/v4.0/include/uapi/linux/inet_diag.h#L37

type InetDiagSockID

type InetDiagSockID struct {
	SPort  [2]byte  // Source port (big-endian).
	DPort  [2]byte  // Destination port (big-endian).
	Src    [16]byte // Source IP
	Dst    [16]byte // Destination IP
	If     uint32
	Cookie [2]uint32
}

InetDiagSockID (inet_diag_sockid) contains the socket identity. https://github.com/torvalds/linux/blob/v4.0/include/uapi/linux/inet_diag.h#L13

type NetlinkErrno

type NetlinkErrno uint32

NetlinkErrno represent the error code contained in a netlink message of type NLMSG_ERROR.

const (
	NLE_SUCCESS NetlinkErrno = iota
	NLE_FAILURE
	NLE_INTR
	NLE_BAD_SOCK
	NLE_AGAIN
	NLE_NOMEM
	NLE_EXIST
	NLE_INVAL
	NLE_RANGE
	NLE_MSGSIZE
	NLE_OPNOTSUPP
	NLE_AF_NOSUPPORT
	NLE_OBJ_NOTFOUND
	NLE_NOATTR
	NLE_MISSING_ATTR
	NLE_AF_MISMATCH
	NLE_SEQ_MISMATCH
	NLE_MSG_OVERFLOW
	NLE_MSG_TRUNC
	NLE_NOADDR
	NLE_SRCRT_NOSUPPORT
	NLE_MSG_TOOSHORT
	NLE_MSGTYPE_NOSUPPORT
	NLE_OBJ_MISMATCH
	NLE_NOCACHE
	NLE_BUSY
	NLE_PROTO_MISMATCH
	NLE_NOACCESS
	NLE_PERM
	NLE_PKTLOC_FILE
	NLE_PARSE_ERR
	NLE_NODEV
	NLE_IMMUTABLE
	NLE_DUMP_INTR
	NLE_ATTRSIZE
)

Netlink error codes.

func (NetlinkErrno) Error

func (e NetlinkErrno) Error() string

type Socket

type Socket struct {
	Family  uint8
	State   uint8
	Timer   uint8
	Retrans uint8
	ID      SocketID
	Expires uint32
	RQueue  uint32
	WQueue  uint32
	UID     uint32
	INode   uint32
}

Socket represents a netlink socket.

type SocketID

type SocketID struct {
	SourcePort      uint16
	DestinationPort uint16
	Source          net.IP
	Destination     net.IP
	Interface       uint32
	Cookie          [2]uint32
}

SocketID identifies a single socket.

type TCPState

type TCPState uint8

TCPState represents the state of a TCP connection.

const (
	TCP_ESTABLISHED TCPState = iota + 1
	TCP_SYN_SENT
	TCP_SYN_RECV
	TCP_FIN_WAIT1
	TCP_FIN_WAIT2
	TCP_TIME_WAIT
	TCP_CLOSE
	TCP_CLOSE_WAIT
	TCP_LAST_ACK
	TCP_LISTEN
	TCP_CLOSING /* Now a valid state */
)

https://github.com/torvalds/linux/blob/5924bbecd0267d87c24110cbe2041b5075173a25/include/net/tcp_states.h#L16

func (TCPState) String

func (s TCPState) String() string

type UnixDiagMsg

type UnixDiagMsg struct {
	Family uint8
	Type   uint8
	State  uint8
	Pad    uint8

	Inode  uint32
	Cookie [2]uint32
}

UnixDiagMsg (unix_diag_msg) represents return message. https://github.com/torvalds/linux/blob/v4.0/include/uapi/linux/unix_diag.h#L23

type UnixDiagMsgExtended

type UnixDiagMsgExtended struct {
	*UnixDiagMsg
	Path string
}

UnixDiagMsgExtended is a extended struct of UnixDiagMsg. Because UnixDiagMsg is used to parse so it can not include other information.

func ParseUnixDiagMsg

func ParseUnixDiagMsg(b []byte, dataLen int) (*UnixDiagMsgExtended, error)

ParseUnixDiagMsg parse an UnixDiagMsg from a byte slice. It assumes the UnixDiagMsg starts at the beginning of b. Invoke this method to parse the payload of a netlink response.

func UnixConnections

func UnixConnections() ([]*UnixDiagMsgExtended, error)

func UnixDiag

func UnixDiag(request syscall.NetlinkMessage) ([]*UnixDiagMsgExtended, error)

UnixDiag sends the given netlink request

func UnixDiagWithBuf

func UnixDiagWithBuf(request syscall.NetlinkMessage, readBuf []byte, resp io.Writer) ([]*UnixDiagMsgExtended, error)

UnixDiagWithBuf sends the given netlink request parses the responsesor debugging).

type UnixDiagReq

type UnixDiagReq struct {
	Family   uint8
	Protocol uint8
	Pad      uint16
	States   uint32
	Ino      uint32 // inode number
	Show     uint32 // report information status
	Cookie   [2]uint32
}

UnixDiagReq is used to request diagnostic data. https://github.com/torvalds/linux/blob/v4.0/include/uapi/linux/unix_diag.h#L6

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL