packets

package
v1.1.5 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2023 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package packetserr is the package which contains the errors for the packets package. This package on its own doesn't do much and is depended on by the packets package.

Index

Constants

This section is empty.

Variables

View Source
var ChecksumInvalidKind = errors.New("Checksum kind should either be 'tcp' OR 'udp'.")

ChecksumInvalidKind is a type that implements the error interface. It's used when an invalid packet kind is provided to the ChecksumIPv4 function.

View Source
var TCPDataOffsetInvalid = errors.New("DataOffset field must be at least 5 and no more than 15")

TCPDataOffsetInvalid is a type that implements the error interface. It's used for errors marshaling the TCPHeader data.

Functions

func ChecksumIPv4

func ChecksumIPv4(data []byte, kind, laddr, raddr string) (uint16, error)

ChecksumIPv4 is a function for computing the TCP checksum of an IPv4 packet. The kind field is either 'tcp' or 'udp' and returns an error if invalid input is given.

The returned error type may be packetserr.ChecksumInvalidKind if an invalid kind field is provided.

func CopySlice

func CopySlice(in []byte) (out []byte)

func CreateSudoIPHeader

func CreateSudoIPHeader(SrcIP, DstIP []byte, protocol uint8, length uint32) (csum uint32)

func ParsePacket

func ParsePacket(packet []byte, NP uint16, SRCIP []byte, NIP [4]byte)

func RecalculateIPv4HeaderChecksum

func RecalculateIPv4HeaderChecksum(bytes []byte)

func RecalculateTCPChecksum

func RecalculateTCPChecksum(IPv4Header []byte, TCPPacket []byte)

Types

type IPHeader

type IPHeader struct {
	Bytes []byte

	Version     byte
	Length      byte
	TotalLength uint16
	Protocol    byte
	Checksum    uint16
	ID          uint16

	ToS byte

	FlagReserved   uint16
	FlagMF         uint16
	FlagDF         uint16
	FragmentOffset uint16

	TTL byte

	DestinationIPBytes []byte
	SourceIPBytes      []byte

	Options []byte
}

type TCPDataOffsetTooSmall

type TCPDataOffsetTooSmall struct {
	ExpectedSize uint8
}

TCPDataOffsetTooSmall is a type that implements the error interface. It's used for errors marshaling the TCPHeader data. Specifically, this is used when the DataOffset is too small for the amount of data in the TCP header.

func (TCPDataOffsetTooSmall) Error

func (e TCPDataOffsetTooSmall) Error() string

type TCPHeader

type TCPHeader struct {
	SourcePort      uint16
	DestinationPort uint16
	SeqNum          uint32
	AckNum          uint32
	DataOffset      uint8 // should be either 0 or >= 5 or <=15 (default: 5); if 0 will be auto-set
	Reserved        uint8 // this should always be 0
	NS              bool
	CWR             bool
	ECE             bool
	URG             bool
	ACK             bool
	PSH             bool
	RST             bool
	SYN             bool
	FIN             bool
	WindowSize      uint16 // if set to 0 this becomes 65535
	Checksum        uint16 // suggest setting this to 0 thus offloading to the kernel
	UrgentPointer   uint16
	Options         TCPOptionSlice // optional TCP options; see TCPOption comment for more info
}

TCPHeader is a struct representing a TCP header. The options portion of the TCP header is not implemented in this struct.

This struct is a simplified representation of a TCP header. This includes making the control (CTRL) bits boolean fields, instead of forcing users of this package to do their own bitshifting.

func UnmarshalTCPHeader

func UnmarshalTCPHeader(data []byte) (*TCPHeader, error)

UnmarshalTCPHeader is a function that takes a byte slice and parses it in to an instance of *TCPHeader. This also assumes the packet is properly formatted.

func (*TCPHeader) Marshal

func (tcp *TCPHeader) Marshal() ([]byte, error)

Marshal is a function to marshal the *TCPHeader instance to a byte slice without explicitly calculating the checksum for the data. Because there is no checksumming of the data, the local and remote addresses are not required.

To note, if the checksum is not provided (i.e. 0) the kernel SHOULD automatically calculate this for you.

However, if the *TCPHeader instance has the Checksum field set, it will be included in the marshaled data.

The error field may be of packetserr.TCPDataOffsetInvalid, packetserr.TCPDataOffsetTooSmall, packetserr.TCPOptionDataTooLong, or packetserr.TCPOptionDataInvalid types. See their documentation for more information.

func (*TCPHeader) MarshalWithChecksum

func (tcp *TCPHeader) MarshalWithChecksum(laddr, raddr string) ([]byte, error)

MarshalWithChecksum is a function to marshal the TCPHeader to a byte slice. This function is almost the same as Marshal() However, this calculates also the TCP checksum and adds it to the header / marshaled data.

It's suggested that you use Marshal() instead and offload the checksumming to your kernel (which should do it automatically if field is zero).

The error field may be of packetserr.TCPDataOffsetInvalid, packetserr.TCPDataOffsetTooSmall, packetserr.TCPOptionDataTooLong, or packetserr.TCPOptionDataInvalid types. See their documentation for more information.

type TCPOption

type TCPOption struct {
	Kind   uint8
	Length uint8
	Data   []byte
}

TCPOption is a struct to hold the data for the various options available for the TCP header. See this Wikipedia article for more information:

https://en.wikipedia.org/wiki/Transmission_Control_Protocol#TCP_segment_structure

The struct fields should be fairly self-explanatory after reading the above. To note, in all cases the Length field should be equal to 2 + len(Data). This is because the Length field, per the RFC, must count itself and the Kind field (which are both one byte).

As a convenience, if the Length is set to zero it will be automatically calculated and set at the time of marshaling.

type TCPOptionDataInvalid

type TCPOptionDataInvalid struct {
	Index int
}

TCPOptionDataInvalid is a type that implements the error interface. It's used for errors marshaling the TCPHeader data. Specifically, this is used when the TCP Options Length field doesn't match the data provided.

func (TCPOptionDataInvalid) Error

func (e TCPOptionDataInvalid) Error() string

type TCPOptionDataTooLong

type TCPOptionDataTooLong struct {
	Index int
}

TCPOptionDataTooLong is a type that implements the error interface. It's used for errors marshaling the TCPHeader data. Specifically, this is use for when the TCP Options Data field is too long for the Options field as per the RFC.

func (TCPOptionDataTooLong) Error

func (e TCPOptionDataTooLong) Error() string

type TCPOptionSlice

type TCPOptionSlice []*TCPOption

TCPOptionSlice is a slice of TCPOptions for use in the TCPHeader Options field.

func UnmarshalTCPOptionSlice

func UnmarshalTCPOptionSlice(data []byte) (TCPOptionSlice, error)

UnmarshalTCPOptionSlice is a function that takes a byte slice and converts it in to a TCPOptionSlice.

func (TCPOptionSlice) Marshal

func (tcpos TCPOptionSlice) Marshal() ([]byte, error)

Marshal is a method to marshal the TCPOptionSlice to the raw bytes for use in the TCPHeader.Marshal() method. It's exported mainly for convenience as marsahling uses this function itself.

type TCPOptionsOverflow

type TCPOptionsOverflow struct {
	MaxSize int
}

TCPOptionsOverflow is a type that implements the error interface. It's used for errors marshaling the TCPHeader data. Specifically, this is used when the TCP Options field exceeds its maximum length as specified by the RFC.

func (TCPOptionsOverflow) Error

func (e TCPOptionsOverflow) Error() string

type UDPHeader

type UDPHeader struct {
	SourcePort      uint16
	DestinationPort uint16
	Length          uint16 // if set to zero it will be automatically set
	Checksum        uint16
	Payload         []byte
}

UDPHeader is the struct representing a UDP header.

func UnmarshalUDPHeader

func UnmarshalUDPHeader(data []byte) (*UDPHeader, error)

UnmarshalUDPHeader is a function that takes a byte slice anf formats it in to an instance of *UDPHeader. This also assumes the packet is properly formatted.

func (*UDPHeader) Marshal

func (udp *UDPHeader) Marshal() ([]byte, error)

Marshal is a function to marshal the *UDPHeader instance to a byte slice without explicitly calculating the checkum for the data.

To note, if the checksum is not provided (i.e., 0) the kernel SHOULD automatically calculate this for you.

If the Checksum field is set, it will be included in the marshaled data.

func (*UDPHeader) MarshalWithChecksum

func (udp *UDPHeader) MarshalWithChecksum(laddr, raddr string) ([]byte, error)

MarshalWithChecksum is a function to marshal the *UDPHeader instance to a byte slice including the calculation of the Checksum field.

It's suggested that you use Marshal() instead and offload the checksumming to your kernel (which should do it automatically if field is zero)

Because of the requirement to create a pseudoheader to do the checksumming the local IPv4 address and remote IPv4 address must be provided in string form.

type UDPPayloadTooLarge

type UDPPayloadTooLarge struct {
	MaxSize, Len int
}

UDPPayloadTooLarge is a type that implements the error interface. It's used for errors marshaling the UDPHeader data. Specifically, this is use for when the UDP payload is too large.

func (UDPPayloadTooLarge) Error

func (e UDPPayloadTooLarge) Error() string

Jump to

Keyboard shortcuts

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