Documentation
¶
Overview ¶
Package header provides the implementation of the encoding and decoding of network protocol headers.
Index ¶
- Constants
- func Checksum(buf []byte, initial uint16) uint16
- func ChecksumCombine(a, b uint16) uint16
- func IPVersion(b []byte) int
- func PseudoHeaderChecksum(protocol tcpip.TransportProtocolNumber, srcAddr, dstAddr tcpip.Address) uint16
- type GUE
- type GUEFields
- type IPv4
- func (b IPv4) CalculateChecksum() uint16
- func (b IPv4) Checksum() uint16
- func (b IPv4) DestinationAddress() tcpip.Address
- func (b IPv4) Encode(i *IPv4Fields)
- func (b IPv4) EncodePartial(partialChecksum, totalLength uint16)
- func (b IPv4) Flags() uint8
- func (b IPv4) FragmentOffset() uint16
- func (b IPv4) HeaderLength() uint8
- func (b IPv4) ID() uint16
- func (b IPv4) IsValid() bool
- func (b IPv4) Payload() []byte
- func (b IPv4) PayloadLength() uint16
- func (b IPv4) Protocol() uint8
- func (b IPv4) SetChecksum(v uint16)
- func (b IPv4) SetDestinationAddress(addr tcpip.Address)
- func (b IPv4) SetFlagsFragmentOffset(flags uint8, offset uint16)
- func (b IPv4) SetSourceAddress(addr tcpip.Address)
- func (b IPv4) SetTOS(v uint8, _ uint32)
- func (b IPv4) SetTotalLength(totalLength uint16)
- func (b IPv4) SourceAddress() tcpip.Address
- func (b IPv4) TOS() (uint8, uint32)
- func (b IPv4) TTL() uint8
- func (b IPv4) TotalLength() uint16
- func (b IPv4) TransportProtocol() tcpip.TransportProtocolNumber
- type IPv4Fields
- type Network
- type Transport
- type UDP
- func (b UDP) CalculateChecksum(partialChecksum, totalLen uint16) uint16
- func (b UDP) Checksum() uint16
- func (b UDP) DestinationPort() uint16
- func (b UDP) Encode(u *UDPFields)
- func (b UDP) Length() uint16
- func (b UDP) Payload() []byte
- func (b UDP) SetChecksum(checksum uint16)
- func (b UDP) SetDestinationPort(port uint16)
- func (b UDP) SetSourcePort(port uint16)
- func (b UDP) SourcePort() uint16
- type UDPFields
Constants ¶
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 )
const ( IPv4FlagMoreFragments = 1 << iota IPv4FlagDontFragment )
Flags that may be set in an IPv4 packet.
const ( // UDPMinimumSize is the minimum size of a valid UDP packet. UDPMinimumSize = 8 // UDPProtocolNumber is UDP's transport protocol number. UDPProtocolNumber tcpip.TransportProtocolNumber = 17 )
const (
// GUEMinimumSize is the minimum size of a valid GUE packet.
GUEMinimumSize = 4
)
const (
// IPv6MinimumSize is the minimum size of a valid IPv6 packet.
IPv6MinimumSize = 40
)
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 ¶
Checksum calculate the checksum (as defined in RFC 1071) of the bytes in the given byte array.
func ChecksumCombine ¶
ChecksumCombine combines the two uint16 to form their checksum. This is done by adding them and the carry.
func IPVersion ¶
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) HeaderLength ¶
HeaderLength returns the total length of the GUE header.
func (GUE) TypeAndControl ¶
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 ¶
CalculateChecksum calculates the checksum of the ipv4 header. TODO
func (IPv4) DestinationAddress ¶
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 ¶
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) FragmentOffset ¶
FragmentOffset returns the "fragment offset" field of the ipv4 header.
func (IPv4) HeaderLength ¶
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) PayloadLength ¶
PayloadLength returns the length of the payload portion of the ipv4 packet.
func (IPv4) SetChecksum ¶
SetChecksum sets the checksum field of the ipv4 header.
func (IPv4) SetDestinationAddress ¶
SetDestinationAddress sets the "destination address" field of the ipv4 header.
func (IPv4) SetFlagsFragmentOffset ¶
SetFlagsFragmentOffset sets the "flags" and "fragment offset" fields of the ipv4 header.
func (IPv4) SetSourceAddress ¶
SetSourceAddress sets the "source address" field of the ipv4 header.
func (IPv4) SetTotalLength ¶
SetTotalLength sets the "total length" field of the ipv4 header.
func (IPv4) SourceAddress ¶
SourceAddress returns the "source address" field of the ipv4 header.
func (IPv4) TotalLength ¶
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 ¶
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) DestinationPort ¶
DestinationPort returns the "destination port" field of the udp header.
func (UDP) SetChecksum ¶
SetChecksum sets the "checksum" field of the udp header.
func (UDP) SetDestinationPort ¶
SetDestinationPort sets the "destination port" field of the udp header.
func (UDP) SetSourcePort ¶
SetSourcePort sets the "source port" field of the udp header.
func (UDP) SourcePort ¶
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.