header

package
v0.0.0-...-1a0588e Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2020 License: GPL-3.0 Imports: 2 Imported by: 0

Documentation

Overview

Package header provides the implementation of the encoding and decoding of network protocol headers.

Index

Constants

View Source
const (
	// IPv4MinimumSize is the minimum size of a valid IPv4 packet.
	IPv4MinimumSize = 20

	// IPv4MaximumHeaderSize is the maximum size of an IPv4 header. Given
	// that there are only 4 bits to represents the header length in 32-bit
	// units, the header cannot execeed 15*4 = 60 bytes.
	IPv4MaximumHeaderSize = 60

	// IPv4AddressSize is the size, in bytes, of an IPv4 address.
	IPv4AddressSize = 4

	// IPv4ProtocolNumber is IPv4's network protocol number.
	IPv4ProtocolNumber tcpip.NetworkProtocolNumber = 0x0800

	// IPv4Version is the version of the ipv4 procotol>
	IPv4Version = 4
)
View Source
const (
	IPv4FlagMoreFragments = 1 << iota
	IPv4FlagDontFragment
)

Flags that may be set in an IPv4 packet.

View Source
const (
	// UDPMinimumSize is the minimum size of a valid UDP packet.
	UDPMinimumSize = 8

	// UDPProtocolNumber is UDP's transport protocol number.
	UDPProtocolNumber tcpip.TransportProtocolNumber = 17
)
View Source
const (
	// GUEMinimumSize is the minimum size of a valid GUE packet.
	GUEMinimumSize = 4
)
View Source
const (
	// IPv6MinimumSize is the minimum size of a valid IPv6 packet.
	IPv6MinimumSize = 40
)
View Source
const (
	// MaxIPPacketSize is the maximum supported IP packet size, excluding
	// jumbograms. The maximum IPv4 packet size is 64k-1 (total size must fit
	// in 16 bits). For IPv6, the payload max size (excluding jumbograms) is
	// 64k-1 (also needs to fit in 16 bits). So we use 64k-1+2*m, where
	// m is the minimum IPv6 header size; we leave room for some potential
	// IP options.
	MaxIPPacketSize = 0xffff + 2*IPv6MinimumSize
)

Variables

This section is empty.

Functions

func Checksum

func Checksum(buf []byte, initial uint16) uint16

Checksum calculate the checksum (as defined in RFC 1071) of the bytes in the given byte array.

func ChecksumCombine

func ChecksumCombine(a, b uint16) uint16

ChecksumCombine combines the two uint16 to form their checksum. This is done by adding them and the carry.

func IPVersion

func IPVersion(b []byte) int

IPVersion returns the version of IP used in the given packet. It returns -1 if the packet is not large enough to contain the version field.

func PseudoHeaderChecksum

func PseudoHeaderChecksum(protocol tcpip.TransportProtocolNumber, srcAddr, dstAddr tcpip.Address) uint16

PseudoHeaderChecksum calculates the pseudo-header checksum for the given destination protocol and network address, ignoring the length field. Pseudo-headers are needed by transport layers when calculating their own checksums.

Types

type GUE

type GUE []byte

GUE represents a Generic UDP Encapsulation header stored in a byte array, the fields are described in https://tools.ietf.org/html/draft-ietf-nvo3-gue-01.

func (GUE) Encode

func (b GUE) Encode(i *GUEFields)

Encode encodes all the fields of the GUE header.

func (GUE) HeaderLength

func (b GUE) HeaderLength() uint8

HeaderLength returns the total length of the GUE header.

func (GUE) Protocol

func (b GUE) Protocol() uint8

Protocol returns the protocol field of the GUE header.

func (GUE) TypeAndControl

func (b GUE) TypeAndControl() uint8

TypeAndControl returns the GUE packet type (top 3 bits of the first byte, which includes the control bit).

type GUEFields

type GUEFields struct {
	// Type is the "type" field of the GUE header.
	Type uint8

	// Control is the "control" field of the GUE header.
	Control bool

	// HeaderLength is the "header length" field of the GUE header. It must
	// be at least 4 octets, and a multiple of 4 as well.
	HeaderLength uint8

	// Protocol is the "protocol" field of the GUE header. This is one of
	// the IPPROTO_* values.
	Protocol uint8
}

GUEFields contains the fields of a GUE packet. It is used to describe the fields of a packet that needs to be encode.

type IPv4

type IPv4 []byte

IPv4 represents an ipv4 header stored in a byte array.

func (IPv4) CalculateChecksum

func (b IPv4) CalculateChecksum() uint16

CalculateChecksum calculates the checksum of the ipv4 header. TODO

func (IPv4) Checksum

func (b IPv4) Checksum() uint16

Checksum returns the checksum field of the ipv4 header.

func (IPv4) DestinationAddress

func (b IPv4) DestinationAddress() tcpip.Address

DestinationAddress returns the "destination address" field of the ipv4 header.

func (IPv4) Encode

func (b IPv4) Encode(i *IPv4Fields)

Encode encodes all the fields of the ipv4 header.

func (IPv4) EncodePartial

func (b IPv4) EncodePartial(partialChecksum, totalLength uint16)

EncodePartial updates the total length and checksum fields of ipv4 header, taking in the partial checksum, which is the checksum of the header without the total length and checksum fields. It is useful in cases when similar packets are produced. TODO

func (IPv4) Flags

func (b IPv4) Flags() uint8

Flags returns the "flags" field of the ipv4 header.

func (IPv4) FragmentOffset

func (b IPv4) FragmentOffset() uint16

FragmentOffset returns the "fragment offset" field of the ipv4 header.

func (IPv4) HeaderLength

func (b IPv4) HeaderLength() uint8

HeaderLength returns the value of the "header length" field of the ipv4 header.

IHL has 4 bits that specify the number of 32-bit words (4bytes) in the header The minimum of IHL is 5

func (IPv4) ID

func (b IPv4) ID() uint16

ID returns the value of the identifier field of the ipv4 header.

func (IPv4) IsValid

func (b IPv4) IsValid() bool

IsValid performs basic validation on the packet.

func (IPv4) Payload

func (b IPv4) Payload() []byte

Payload implements Network.Payload.

func (IPv4) PayloadLength

func (b IPv4) PayloadLength() uint16

PayloadLength returns the length of the payload portion of the ipv4 packet.

func (IPv4) Protocol

func (b IPv4) Protocol() uint8

Protocol returns the value of the protocol field of the ipv4 header.

func (IPv4) SetChecksum

func (b IPv4) SetChecksum(v uint16)

SetChecksum sets the checksum field of the ipv4 header.

func (IPv4) SetDestinationAddress

func (b IPv4) SetDestinationAddress(addr tcpip.Address)

SetDestinationAddress sets the "destination address" field of the ipv4 header.

func (IPv4) SetFlagsFragmentOffset

func (b IPv4) SetFlagsFragmentOffset(flags uint8, offset uint16)

SetFlagsFragmentOffset sets the "flags" and "fragment offset" fields of the ipv4 header.

func (IPv4) SetSourceAddress

func (b IPv4) SetSourceAddress(addr tcpip.Address)

SetSourceAddress sets the "source address" field of the ipv4 header.

func (IPv4) SetTOS

func (b IPv4) SetTOS(v uint8, _ uint32)

SetTOS sets the "type of service" field of the ipv4 header.

func (IPv4) SetTotalLength

func (b IPv4) SetTotalLength(totalLength uint16)

SetTotalLength sets the "total length" field of the ipv4 header.

func (IPv4) SourceAddress

func (b IPv4) SourceAddress() tcpip.Address

SourceAddress returns the "source address" field of the ipv4 header.

func (IPv4) TOS

func (b IPv4) TOS() (uint8, uint32)

TOS returns the "type of service" field of the ipv4 header.

func (IPv4) TTL

func (b IPv4) TTL() uint8

TTL returns the "TTL" field of the ipv4 header.

func (IPv4) TotalLength

func (b IPv4) TotalLength() uint16

TotalLength returns the "total length" field of the ipv4 header.

func (IPv4) TransportProtocol

func (b IPv4) TransportProtocol() tcpip.TransportProtocolNumber

TransportProtocol implements Network.TransportProtocol.

type IPv4Fields

type IPv4Fields struct {
	// IHL is the "internet header length" field of an IPv4 packet.
	IHL uint8

	// TOS is the "type of service" field of an IPv4 packet.
	TOS uint8

	// TotalLength is the "total length" field of an IPv4 packet.
	TotalLength uint16

	// ID is the "identification" field of an IPv4 packet.
	ID uint16

	// Flags is the "flags" field of an IPv4 packet.
	Flags uint8

	// FragmentOffset is the "fragment offset" field of an IPv4 packet.
	FragmentOffset uint16

	// TTL is the "time to live" field of an IPv4 packet.
	TTL uint8

	// Protocol is the "protocol" field of an IPv4 packet.
	Protocol uint8

	// Checksum is the "checksum" field of an IPv4 packet.
	Checksum uint16

	// SrcAddr is the "source ip address" of an IPv4 packet.
	SrcAddr tcpip.Address

	// DstAddr is the "destination ip address" of an IPv4 packet.
	DstAddr tcpip.Address
}

IPv4Fields contains the fields of an IPv4 packet. It is used to describe the fields of a packet that needs to be encoded.

type Network

type Network interface {
	// SourceAddress returns the value of the "source address" field.
	SourceAddress() tcpip.Address

	// DestinationAddress returns the value of the "destination address"
	// field.
	DestinationAddress() tcpip.Address

	// Checksum returns the value of the "checksum" field.
	Checksum() uint16

	// SetSourceAddress sets the value of the "source address" field.
	SetSourceAddress(tcpip.Address)

	// SetDestinationAddress sets the value of the "destination address"
	// field.
	SetDestinationAddress(tcpip.Address)

	// SetChecksum sets the value of the "checksum" field.
	SetChecksum(uint16)

	// TransportProtocol returns the number of the transport protocol
	// stored in the payload.
	TransportProtocol() tcpip.TransportProtocolNumber

	// Payload returns a byte slice containing the payload of the network
	// packet.
	Payload() []byte

	// TOS returns the values of the "type of service" and "flow label" fields.
	TOS() (uint8, uint32)

	// SetTOS sets the values of the "type of service" and "flow lable" fields.
	SetTOS(t uint8, l uint32)
}

Network offers generic methods to query and/or update the fields of the header of a network protocol buffer.

type Transport

type Transport interface {
	// SourcePort returns the value of the "source port" fields.
	SourcePort() uint16

	// DestinationPort returns the value of the "destination port" fields.
	DestinationPort() uint16

	// Checksum returns the value of the "checksum" field.
	Checksum() uint16

	// SetSourcePort sets the value of the "source port" field.
	SetSourcePort(uint16)

	// SetDestinationPort sets the value of the "destination port" field.
	SetDestinationPort(uint16)

	// SetChecksum sets the value of the "checksum" field.
	SetChecksum(uint16)

	// Payload returns the data carried in the transport buffer.
	Payload() []byte
}

Transport offers generic methods to query and/or update the fields of the header of a transport protocol buffer.

type UDP

type UDP []byte

UDP represents a UDP header stored in a byte array.

func (UDP) CalculateChecksum

func (b UDP) CalculateChecksum(partialChecksum, totalLen uint16) uint16

CalculateChecksum calculates the checksum of the udp packet, given the total length of the packet and the checksum of the network-layer pseudo-header (excluding the total length) and the checksum of the payload.

func (UDP) Checksum

func (b UDP) Checksum() uint16

Checksum returns the "checksum" field of the udp header.

func (UDP) DestinationPort

func (b UDP) DestinationPort() uint16

DestinationPort returns the "destination port" field of the udp header.

func (UDP) Encode

func (b UDP) Encode(u *UDPFields)

Encode encodes all the fields of the udp header.

func (UDP) Length

func (b UDP) Length() uint16

Length returns the "length" field of the udp header.

func (UDP) Payload

func (b UDP) Payload() []byte

Payload returns the data contained in the UDP datagram.

func (UDP) SetChecksum

func (b UDP) SetChecksum(checksum uint16)

SetChecksum sets the "checksum" field of the udp header.

func (UDP) SetDestinationPort

func (b UDP) SetDestinationPort(port uint16)

SetDestinationPort sets the "destination port" field of the udp header.

func (UDP) SetSourcePort

func (b UDP) SetSourcePort(port uint16)

SetSourcePort sets the "source port" field of the udp header.

func (UDP) SourcePort

func (b UDP) SourcePort() uint16

SourcePort returns the "source port" field of the udp header.

type UDPFields

type UDPFields struct {
	// SrcPort is the "source port" field of a UDP packet.
	SrcPort uint16

	// DstPort is the "destination port" field of a UDP packet.
	DstPort uint16

	// Length is the "length" field of a UDP packet.
	Length uint16

	// Checksum is the "checksum" field of a UDP packet.
	Checksum uint16
}

UDPFields contains the fields of a UDP packet. It is used to describe the fields of a packet that needs to be encoded.

Jump to

Keyboard shortcuts

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