ip

package
v0.16.2 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2020 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

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 exceed 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 uint32 = 0x0800

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

Flags that may be set in an IPv4 packet.

Variables

This section is empty.

Functions

func Checksum

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

Checksum calculates 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 Decode

func Decode(packet []byte) (net.IP, net.IP, int, []byte, error)

Parse a layer 3 packet (ipv4 or ipv6), and return - src, dst IP addresses, - layer 4 protocol - payload - error

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 IsV4MulticastAddress

func IsV4MulticastAddress(addr net.IP) bool

IsV4MulticastAddress determines if the provided address is an IPv4 multicast address (range 224.0.0.0 to 239.255.255.255). The four most significant bits will be 1110 = 0xe0.

func PseudoHeaderChecksum

func PseudoHeaderChecksum(protocol int, srcAddr net.IP, dstAddr net.IP) 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 checksum.

Types

type IPv4

type IPv4 []byte

IPv4 represents an ipv4 header stored in a byte array. Most of the methods of IPv4 access to the underlying slice without checking the boundaries and could panic because of 'index out of range'. Always call IsValid() to validate an instance of IPv4 before using other methods.

func (IPv4) CalculateChecksum

func (b IPv4) CalculateChecksum() uint16

CalculateChecksum calculates the checksum of the ipv4 header.

func (IPv4) Checksum

func (b IPv4) Checksum() uint16

Checksum returns the checksum field of the ipv4 header.

func (IPv4) DestinationAddress

func (b IPv4) DestinationAddress() net.IP

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.

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.

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(pktSize int) 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 net.IP)

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) SetID

func (b IPv4) SetID(v uint16)

SetID sets the identification field.

func (IPv4) SetSourceAddress

func (b IPv4) SetSourceAddress(addr net.IP)

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() net.IP

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() uint8

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 net.IP

	// DstAddr is the "destination ip address" of an IPv4 packet.
	DstAddr net.IP
}

IPv4Fields contains the fields of an IPv4 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