Documentation
¶
Index ¶
- Constants
- func AppendFormatAddr(dst []byte, addr [4]byte) []byte
- func IsMulticast(addr [4]byte) bool
- type Flags
- type Frame
- func (ifrm Frame) CRC() uint16
- func (ifrm Frame) CRCWriteHeader(crc *lneto.CRC791)
- func (ifrm Frame) CRCWriteTCPPseudo(crc *lneto.CRC791)
- func (ifrm Frame) CRCWriteUDPPseudo(crc *lneto.CRC791, udpLength uint16)
- func (ifrm Frame) CalculateHeaderCRC() uint16
- func (ifrm Frame) ClearHeader()
- func (ifrm Frame) DestinationAddr() *[4]byte
- func (ifrm Frame) Flags() Flags
- func (ifrm Frame) HeaderLength() int
- func (ifrm Frame) ID() uint16
- func (ifrm Frame) Options() []byte
- func (ifrm Frame) Payload() []byte
- func (ifrm Frame) Protocol() lneto.IPProto
- func (ifrm Frame) RawData() []byte
- func (ifrm Frame) SetCRC(cs uint16)
- func (ifrm Frame) SetFlags(flags Flags)
- func (ifrm Frame) SetID(id uint16)
- func (ifrm Frame) SetProtocol(proto lneto.IPProto)
- func (ifrm Frame) SetTTL(ttl uint8)
- func (ifrm Frame) SetToS(tos ToS)
- func (ifrm Frame) SetTotalLength(tl uint16)
- func (ifrm Frame) SetVersionAndIHL(version, IHL uint8)
- func (ifrm Frame) SourceAddr() *[4]byte
- func (ifrm Frame) String() string
- func (ifrm Frame) TTL() uint8
- func (ifrm Frame) ToS() ToS
- func (ifrm Frame) TotalLength() uint16
- func (ifrm Frame) ValidateExceptCRC(v *lneto.Validator)
- func (ifrm Frame) ValidateSize(v *lneto.Validator)
- func (ifrm Frame) VersionAndIHL() (version, IHL uint8)
- type ToS
Constants ¶
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 ¶
AppendFormatAddr appends the dotted-decimal text representation of an IPv4 address to dst. Zero heap allocations.
func IsMulticast ¶
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.
func (Flags) DontFragment ¶
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 ¶
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) MoreFragments ¶
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 ¶
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) CRCWriteHeader ¶
func (Frame) CRCWriteTCPPseudo ¶
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 ¶
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 ¶
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 ¶
DestinationAddr returns pointer to the destination IPv4 address in the IP header.
func (Frame) HeaderLength ¶
HeaderLength returns the length of the IPv4 header as calculated using IHL. It includes IP options.
func (Frame) ID ¶
ID is an identification field and is primarily used for uniquely identifying the group of fragments of a single IP datagram.
func (Frame) Options ¶
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 ¶
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 ¶
Protocol field defines the protocol used in the data portion of the IP datagram. TCP is 6, UDP is 17. See [IPProto].
func (Frame) SetProtocol ¶
SetProtocol sets protocol field. See Frame.Protocol and lneto.IPProto.
func (Frame) SetTotalLength ¶
SetTotalLength sets TotalLength field. See Frame.TotalLength.
func (Frame) SetVersionAndIHL ¶
SetVersionAndIHL sets the version and IHL fields in the IPv4 header. Version should always be 4.
func (Frame) SourceAddr ¶
SourceAddr returns pointer to the source IPv4 address in the IP header.
func (Frame) TTL ¶
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 ¶
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 ¶
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 ¶
ValidateExceptCRC checks for invalid frame values but does not check CRC.
func (Frame) ValidateSize ¶
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 ¶
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 ¶
NewToS returns a ToS from an Explicit Congestion Notification value and a Differentiated Services Field value.