Documentation ¶
Index ¶
- Constants
- func Checksum(buf []byte, initial uint16) uint16
- func ChecksumCombine(a, b uint16) uint16
- func Decode(packet []byte) (net.IP, net.IP, int, []byte, error)
- func IPVersion(b []byte) int
- func IsV4MulticastAddress(addr net.IP) bool
- func PseudoHeaderChecksum(protocol int, srcAddr net.IP, dstAddr net.IP) uint16
- type IPv4
- func (b IPv4) CalculateChecksum() uint16
- func (b IPv4) Checksum() uint16
- func (b IPv4) DestinationAddress() net.IP
- 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(pktSize int) 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 net.IP)
- func (b IPv4) SetFlagsFragmentOffset(flags uint8, offset uint16)
- func (b IPv4) SetID(v uint16)
- func (b IPv4) SetSourceAddress(addr net.IP)
- func (b IPv4) SetTOS(v uint8, _ uint32)
- func (b IPv4) SetTotalLength(totalLength uint16)
- func (b IPv4) SourceAddress() net.IP
- func (b IPv4) TOS() (uint8, uint32)
- func (b IPv4) TTL() uint8
- func (b IPv4) TotalLength() uint16
- func (b IPv4) TransportProtocol() uint8
- type IPv4Fields
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 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 )
const ( IPv4FlagMoreFragments = 1 << iota IPv4FlagDontFragment )
Flags that may be set in an IPv4 packet.
Variables ¶
This section is empty.
Functions ¶
func Checksum ¶
Checksum calculates 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 Decode ¶
Parse a layer 3 packet (ipv4 or ipv6), and return - src, dst IP addresses, - layer 4 protocol - payload - error
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 IsV4MulticastAddress ¶
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 ¶
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 ¶
CalculateChecksum calculates the checksum of the ipv4 header.
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.
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.
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 ¶
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.