ipv4

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: BSD-3-Clause Imports: 5 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// RFC791 defines the minimum MTU for an IPv4 packet as 68, meaning a payload of 48 bytes when no IPv4 options included.
	MinimumMTU = 68
)

Variables

This section is empty.

Functions

func AppendFormatAddr

func AppendFormatAddr(dst []byte, addr [4]byte) []byte

AppendFormatAddr appends the dotted-decimal text representation of an IPv4 address to dst. Zero heap allocations.

func IsMulticast

func IsMulticast(addr [4]byte) bool

IsMulticast reports whether addr is an IPv4 multicast address (224.0.0.0/4), i.e. the most significant nibble is 0xE (1110 in binary) as defined in RFC1112.

Types

type Flags

type Flags uint16

Flags holds fragmentation field data of an IPv4 header. It is 16 bits long.

const (
	FlagOffsetMask = (1 << flagIsEvilPos) - 1

	FlagDontFragment  Flags = 1 << flagDontFragPos
	FlagMoreFragments Flags = 1 << flagMoreFragPos
)

func NewFlags

func NewFlags(fragOffset uint16, dontFrag, moreFrag bool) Flags

func (Flags) DontFragment

func (f Flags) DontFragment() bool

DontFragment specifies whether the datagram can not be fragmented. This can be used when sending packets to a host that does not have resources to perform reassembly of fragments. If the DontFragment(DF) flag is set, and fragmentation is required to route the packet, then the packet is dropped.

func (Flags) FragmentOffset

func (f Flags) FragmentOffset() uint16

FragmentOffset specifies the offset of a particular fragment relative to the beginning of the original unfragmented IP datagram. Fragments are specified in units of 8 bytes, which is why fragment lengths are always a multiple of 8; except the last, which may be smaller. The fragmentation offset value for the first fragment is always 0.

func (Flags) IsEvil

func (f Flags) IsEvil() bool

IsEvil returns true if evil bit set as per RFC3514.

func (Flags) MoreFragments

func (f Flags) MoreFragments() bool

MoreFragments is cleared for unfragmented packets. For fragmented packets, all fragments except the last have the MF flag set. The last fragment has a non-zero Fragment Offset field, so it can still be differentiated from an unfragmented packet.

type Frame

type Frame struct {
	// contains filtered or unexported fields
}

Frame encapsulates the raw data of an IPv4 packet and provides methods for manipulating, validating and retreiving fields and payload data. See RFC791. Below is an example of setting IPv4 fields for MDNS:

func NewFrame

func NewFrame(buf []byte) (Frame, error)

NewFrame returns a new Frame with data set to buf. An error is returned if the buffer size is smaller than 20. Users should still call Frame.ValidateSize before working with payload/options of frames to avoid panics.

func (Frame) CRC

func (ifrm Frame) CRC() uint16

CRC returns the cyclic-redundancy-check (checksum) field of the IPv4 header.

func (Frame) CRCWriteHeader

func (ifrm Frame) CRCWriteHeader(crc *lneto.CRC791)

func (Frame) CRCWriteTCPPseudo

func (ifrm Frame) CRCWriteTCPPseudo(crc *lneto.CRC791)

CRCWriteTCPPseudo is used to calculate the TCP checksum for IPv4 framed packets. To calculate the CRC of a TCP frame:

ifrm.CRCWriteTCPPseudo(&crc)
// tfrm.SetCRC(0) // Zero if calculating frame for sending out.
crcValue := crc.PayloadSum16(ifrm.Payload()) // If validating a received frame crcValue should be zero here.

func (Frame) CRCWriteUDPPseudo

func (ifrm Frame) CRCWriteUDPPseudo(crc *lneto.CRC791, udpLength uint16)

CRCWriteUDPPseudo writes the IPv4 UDP pseudo-header into crc for checksum calculation. Typical usage for computing or validating a UDP checksum:

ifrm.CRCWriteUDPPseudo(&crc, ufrm.Length())
// ufrm.SetCRC(0) // Zero before computing checksum for transmission.
crcValue := crc.PayloadSum16(ifrm.Payload()) // For received frames, crcValue should be zero if valid.

func (Frame) CalculateHeaderCRC

func (ifrm Frame) CalculateHeaderCRC() uint16

CalculateHeaderCRC calculates the CRC for this IPv4 frame including CRC field. If the result of this function is 0 then the CRC is valid for the frame. To calculate the CRC for a frame set the CRC field to zero and then call this function:

ifrm.SetCRC(0)
crcValue := ifrm.CalculateHeaderCRC()
ifrm.SetCRC(crcValue)

func (Frame) ClearHeader

func (ifrm Frame) ClearHeader()

ClearHeader zeros out the fixed(non-variable) header contents.

func (Frame) DestinationAddr

func (ifrm Frame) DestinationAddr() *[4]byte

DestinationAddr returns pointer to the destination IPv4 address in the IP header.

func (Frame) Flags

func (ifrm Frame) Flags() Flags

Flags returns the Flags of the IP packet.

func (Frame) HeaderLength

func (ifrm Frame) HeaderLength() int

HeaderLength returns the length of the IPv4 header as calculated using IHL. It includes IP options.

func (Frame) ID

func (ifrm Frame) ID() uint16

ID is an identification field and is primarily used for uniquely identifying the group of fragments of a single IP datagram.

func (Frame) Options

func (ifrm Frame) Options() []byte

Options returns the options portion of the IPv4 header. May be zero lengthed. Be sure to call Frame.ValidateSize beforehand to avoid panic.

func (Frame) Payload

func (ifrm Frame) Payload() []byte

Payload returns the contents of the IPv4 packet, which may be zero sized. Be sure to call Frame.ValidateSize beforehand to avoid panic.

func (Frame) Protocol

func (ifrm Frame) Protocol() lneto.IPProto

Protocol field defines the protocol used in the data portion of the IP datagram. TCP is 6, UDP is 17. See [IPProto].

func (Frame) RawData

func (ifrm Frame) RawData() []byte

RawData returns the underlying slice with which the frame was created.

func (Frame) SetCRC

func (ifrm Frame) SetCRC(cs uint16)

SetCRC sets the CRC field of the IP packet. See Frame.CRC.

func (Frame) SetFlags

func (ifrm Frame) SetFlags(flags Flags)

SetFlags sets the IPv4 flags field. See Flags.

func (Frame) SetID

func (ifrm Frame) SetID(id uint16)

SetID sets ID field. See Frame.ID.

func (Frame) SetProtocol

func (ifrm Frame) SetProtocol(proto lneto.IPProto)

SetProtocol sets protocol field. See Frame.Protocol and lneto.IPProto.

func (Frame) SetTTL

func (ifrm Frame) SetTTL(ttl uint8)

SetTTL sets the IP frame's TTL field. See Frame.TTL.

func (Frame) SetToS

func (ifrm Frame) SetToS(tos ToS)

SetToS sets ToS field. See Frame.ToS.

func (Frame) SetTotalLength

func (ifrm Frame) SetTotalLength(tl uint16)

SetTotalLength sets TotalLength field. See Frame.TotalLength.

func (Frame) SetVersionAndIHL

func (ifrm Frame) SetVersionAndIHL(version, IHL uint8)

SetVersionAndIHL sets the version and IHL fields in the IPv4 header. Version should always be 4.

func (Frame) SourceAddr

func (ifrm Frame) SourceAddr() *[4]byte

SourceAddr returns pointer to the source IPv4 address in the IP header.

func (Frame) String

func (ifrm Frame) String() string

func (Frame) TTL

func (ifrm Frame) TTL() uint8

TTL is an eight-bit time to live field limits a datagram's lifetime to prevent network failure in the event of a routing loop. In practice, the field is used as a hop count—when the datagram arrives at a router, the router decrements the TTL field by one. When the TTL field hits zero, the router discards the packet and typically sends an ICMP time exceeded message to the sender.

func (Frame) ToS

func (ifrm Frame) ToS() ToS

ToS (Type of Service) contains Differential Services Code Point (DSCP) and Explicit Congestion Notification (ECN) union data.

DSCP originally defined as the type of service (ToS), this field specifies differentiated services (DiffServ) per RFC 2474. Real-time data streaming makes use of the DSCP field. An example is Voice over IP (VoIP), which is used for interactive voice services.

ECN is defined in RFC 3168 and allows end-to-end notification of network congestion without dropping packets. ECN is an optional feature available when both endpoints support it and effective when also supported by the underlying network.

func (Frame) TotalLength

func (ifrm Frame) TotalLength() uint16

TotalLength defines the entire packet size in bytes, including IP header and data. The minimum size is 20 bytes (IPv4 header without data) and the maximum is 65,535 bytes. All hosts are required to be able to reassemble datagrams of size up to 576 bytes, but most modern hosts handle much larger packets.

Links may impose further restrictions on the packet size, in which case datagrams must be fragmented. Fragmentation in IPv4 is performed in either the sending host or in routers. Reassembly is performed at the receiving host.

func (Frame) ValidateExceptCRC

func (ifrm Frame) ValidateExceptCRC(v *lneto.Validator)

ValidateExceptCRC checks for invalid frame values but does not check CRC.

func (Frame) ValidateSize

func (ifrm Frame) ValidateSize(v *lneto.Validator)

ValidateSize checks the frame's size fields and compares with the actual buffer the frame. It returns a non-nil error on finding an inconsistency.

func (Frame) VersionAndIHL

func (ifrm Frame) VersionAndIHL() (version, IHL uint8)

VersionAndIHL returns the version and IHL fields in the IPv4 header. Version should always be 4.

type ToS

type ToS uint8

ToS represents the Traffic Class (a.k.a Type of Service). It is 8 bits long. 6 MSB are Differentiated Services; 2 LSB are Explicit Congenstion Notification.

func NewToS

func NewToS(ECN, DS uint8) ToS

NewToS returns a ToS from an Explicit Congestion Notification value and a Differentiated Services Field value.

func (ToS) DS

func (tos ToS) DS() uint8

DS returns the top 6 bits of the IPv4 ToS holding the Differentiated Services field which is used to classify packets.

func (ToS) ECN

func (tos ToS) ECN() uint8

ECN is the Explicit Congestion Notification which provides congestion control and non-congestion control traffic.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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