ethernet

package module
v0.0.0-...-7a313fc Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: MIT Imports: 9 Imported by: 0

README

Ethernet Frame Serializarion/Deserazization library written in Go

The library implements frame serialization/deserialization in pure Go. The list of the supported Ethernet standards:

Usage Examples

See the *_test.go files.

How to Encode?

	dstAddr := ethernet.NewHardwareAddr(0x8C, 0x8E, 0xC4, 0xFF, 0x9E, 0xA2)
	srcAddr := ethernet.NewHardwareAddr(0x8C, 0x8E, 0xC4, 0xAA, 0x4E, 0xF1)
	f := ethernet.NewFrame(srcAddr, dst, []byte("Hello :)"))
	f.SetTag8021q(&ethernet.Tag8021q{Tpid: 0x8100, Tci: ethernet.Encode8021qTci(3, 0, 1024)})
	b := f.Marshal()

How to Decode?

	b := f.Marshal()

	var f Frame
	err := ethernet.Unmarshal(b, &f)
	if err != nil {
		panic(err)
	}

	pcp, dei, vlan := ethernet.Decode8021qTci(f.Tag8021q().Tci)
	fmt.Println("PCP:", pcp)
	fmt.Println("DEI:", dei)
	fmt.Println("VLAN ID:", vlan)
	fmt.Println("EtherType:", f.EtherType())
	fmt.Println("Checksum (FCS):", f.FCS())

License

MIT

Documentation

Overview

Copyright (c) 2022 0x9ef. All rights reserved. Use of this source code is governed by an MIT license that can be found in the LICENSE file.

Copyright (c) 2022 0x9ef. All rights reserved. Use of this source code is governed by an MIT license that can be found in the LICENSE file.

Copyright (c) 2022 0x9ef. All rights reserved. Use of this source code is governed by an MIT license that can be found in the LICENSE file.

Copyright (c) 2022 0x9ef. All rights reserved. Use of this source code is governed by an MIT license that can be found in the LICENSE file.

Copyright (c) 2022 0x9ef. All rights reserved. Use of this source code is governed by an MIT license that can be found in the LICENSE file.

Copyright (c) 2022 0x9ef. All rights reserved. Use of this source code is governed by an MIT license that can be found in the LICENSE file.

Copyright (c) 2022 0x9ef. All rights reserved. Use of this source code is governed by an MIT license that can be found in the LICENSE file.

Index

Constants

View Source
const (
	// The minimal frame size is 64 bytes, comprising an 18-byte header and a payload of 46 bytes.
	MinFrameSize           = 64
	MinFrameSizeWithoutFCS = 60
	// The maximum frame size is 1518 bytes, 18 bytes of which are overhead (header and frame check sequence),
	// resulting in an MTU of 1500 bytes.
	MaxFrameSize = 1518
)
View Source
const (
	SubtypeData                = 0x0
	SubtypeQosData             = 0x8
	SubtypeAssociationReq      = 0x0
	SubtypeAssociationResp     = 0x1
	SubtypeReassociationReq    = 0x2
	SubtypeReassociationResp   = 0x3
	SubtypeProbeReq            = 0x4
	SubtypeProbeResp           = 0x5
	SubtypeTimingAdvertisement = 0x6
	SubtypeReserved            = 0x7
	SubtypeBeacon              = 0x8
	SubtypeAtim                = 0x9
	SubtypeDisassociation      = 0xA
	SubtypeAuthentication      = 0xB
	SubtypeDeauthentication    = 0xC
	SubtypeAction              = 0xD
	SubtypeNack                = 0xE
	SubtypeTrigger             = 0x2
	SubtypeTack                = 0x3
	SubtypeControlWrapper      = 0x7
	SubtypeRts                 = 0xB
	SubtypeCts                 = 0xC
	SubtypeAck                 = 0xD
)
View Source
const MaxFrame8011Size = 2304

802.11 frames are capable of transporting frames with an MSDU payload of 2,304 bytes of upper layer data.

Variables

View Source
var BroadcastAddr = HardwareAddr{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
View Source
var EmptyAddr = HardwareAddr{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}

Functions

func Decode80211Fc

func Decode80211Fc(encoded uint16) [11]uint16

func Decode80211Sc

func Decode80211Sc(encoded uint16) (fn uint16, sn uint16)

func Encode80211Fc

func Encode80211Fc(version uint16, ftype uint16, subtype uint16,
	tds uint16, fds uint16, mf uint16, rt uint16,
	pm uint16, md uint16, wep uint16, order uint16) uint16

func Encode80211Sc

func Encode80211Sc(fn uint16, sn uint16) uint16

func Encode8021qTCI

func Encode8021qTCI(pcp PCP, dei uint16, vlan uint16) uint16

Encode8021qTCI encodes PCP, DEI, VLAN using bitwise operations.

func Unmarshal

func Unmarshal(b []byte, f *Frame) error

Unmarshal unmarshaling a sequence of bytes into a Frame structure representation. If array size is less than minSize (64) returns error io.ErrUnexpectedEOF

Types

type EtherType

type EtherType uint16

EtherType is a two-octet field in an Ethernet frame. It is used to indicate which protocol is encapsulated in the payload of the frame and is used at the receiving end by the data link layer to determine how the payload is processed. The same field is also used to indicate the size of some Ethernet frames.

http://www.iana.org/assignments/ieee-802-numbers/ieee-802-numbers.xhtml

const (
	EtherTypeIPv4 EtherType = 0x8000
	EtherTypeIPv6 EtherType = 0x86DD
	EtherTypeVlan EtherType = 0x8100
)

type Frame

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

In computer networking, an Ethernet frame is a data link layer protocol data unit and uses the underlying Ethernet physical layer transport mechanisms. In other words, a data unit on an Ethernet link transports an Ethernet frame as its payload. An Ethernet frame is preceded by a preamble and start frame delimiter (SFD), which are both part of the Ethernet packet at the physical layer. Each Ethernet frame starts with an Ethernet header, which contains destination and source MAC addresses as its first two fields. The middle section of the frame is payload data including any headers for other protocols (for example, Internet Protocol) carried in the frame. The frame ends with a frame check sequence (FCS), which is a 32-bit cyclic redundancy check used to detect any in-transit corruption of data.

Preamble and SFD don't count into size of Ethernet frame size, because the physical layer determines where the frame starts.

func NewFrame

func NewFrame(src HardwareAddr, dst HardwareAddr, etherType EtherType, payload []byte) *Frame

NewFrame return constructed ethernet frame with basic source, destination MAC address and payload which this frame contains. If payload have lengh which less than minPayloadSize we fills remaining bytes with zeroes

func (*Frame) Destination

func (f *Frame) Destination() HardwareAddr

Destination return destination address from source frame

func (*Frame) EtherType

func (f *Frame) EtherType() EtherType

EtherType is a two-octet field in an Ethernet frame. It is used to indicate which protocol is encapsulated in the payload of the frame and is used at the receiving end by the data link layer to determine how the payload is processed. The same field is also used to indicate the size of some Ethernet frames.

func (*Frame) FCS

func (f *Frame) FCS() [4]byte

Frame Check Sequence (FCS) refers to the extra bits and characters added to data packets for error detection and control.

func (*Frame) Marshal

func (f *Frame) Marshal() []byte

Marshal serializes frame into the byte representation. If the structure contains 802.1Q tag, performs an additional encoding of the 802.1Q header within the frame.

func (*Frame) Payload

func (f *Frame) Payload() []byte

Payload the minimum payload is 42 octets when an 802.1Q tag (Tag8012q) is present and 46 octets when absent. When the actual payload is less, padding bytes are added accordingly. The maximum payload is 1500 octets. Non-standard jumbo frames allow for larger maximum payload size.

func (*Frame) SetFCS

func (f *Frame) SetFCS(fcs [4]byte)

func (*Frame) SetTag8021Q

func (f *Frame) SetTag8021Q(tag *Tag8021Q)

func (*Frame) Size

func (f *Frame) Size() int

Size return a serialized size of frame in bytes

func (*Frame) Source

func (f *Frame) Source() HardwareAddr

Source return sender source address

func (*Frame) String

func (f *Frame) String() string

func (*Frame) Tag8021Q

func (f *Frame) Tag8021Q() *Tag8021Q

Tag8021Q IEEE 802.1Q, often referred to as Dot1q, is the networking standard that supports virtual LANs (VLANs) on an IEEE 802.3 Ethernet network. The standard defines a system of VLAN tagging for Ethernet frames and the accompanying procedures to be used by bridges and switches in handling such frames. The standard also contains provisions for a quality-of-service (QOS) prioritization scheme commonly known as IEEE 802.1p and defines the Generic Attribute Registration Protocol.

type Frame80211

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

IEEE 802.11 is part of the IEEE 802 set of local area network (LAN) technical standards, and specifies the set of media access control (MAC) and physical layer (PHY) protocols for implementing wireless local area network (WLAN) computer communication. IEEE 802.11 uses various frequencies including, but not limited to, 2.4 GHz, 5 GHz, 6 GHz, and 60 GHz frequency bands. Although IEEE 802.11 specifications list channels that might be used, the radio frequency spectrum availability allowed varies significantly by regulatory domain.

func NewFrame80211

func NewFrame80211(addr1, addr2, addr3 HardwareAddr, addr4 *HardwareAddr, fc uint16, duration uint16, payload []byte) *Frame80211

func Unmarshal80211

func Unmarshal80211(b []byte) (*Frame80211, error)

func (*Frame80211) Destination

func (f *Frame80211) Destination() HardwareAddr

Destination return destination address (DA)

func (*Frame80211) Duration

func (f *Frame80211) Duration() uint16

Duration field carries the value of the Network Allocation Vector (NAV). Access to the medium is restricted for the time specified by the NAV

func (*Frame80211) FCS

func (f *Frame80211) FCS() [4]byte

Frame check sequence (FCS) refers to the extra bits and characters added to data packets for error detection and control.

func (*Frame80211) FrameControl

func (f *Frame80211) FrameControl() uint16

802.11 Control Frames assist with the delivery of Data & Management frames. Unlike management & data frames, Control frames does not have a frame body

func (*Frame80211) HT

func (f *Frame80211) HT() uint32

func (*Frame80211) Marshal

func (f *Frame80211) Marshal() []byte

func (*Frame80211) Payload

func (f *Frame80211) Payload() []byte

Payload return payload data, maximum payload size defined in max80211MSDU

func (*Frame80211) QOS

func (f *Frame80211) QOS() uint16

func (*Frame80211) Receiver

func (f *Frame80211) Receiver() HardwareAddr

Receiver return Receiver Address (RA)

func (*Frame80211) SC

func (f *Frame80211) SC() uint16

func (*Frame80211) SetDuration

func (f *Frame80211) SetDuration(duration uint16)

func (*Frame80211) SetFCS

func (f *Frame80211) SetFCS(fcs [4]byte)

func (*Frame80211) SetFrameControl

func (f *Frame80211) SetFrameControl(fc uint16)

func (*Frame80211) SetHT

func (f *Frame80211) SetHT(ht uint32)

func (*Frame80211) SetQOS

func (f *Frame80211) SetQOS(qos uint16)

func (*Frame80211) SetSC

func (f *Frame80211) SetSC(sc uint16)

func (*Frame80211) Size

func (f *Frame80211) Size() int

Size return seriailized size of frame in bytes

func (*Frame80211) Source

func (f *Frame80211) Source() HardwareAddr

Source return source address (SA)

func (*Frame80211) Transmitter

func (f *Frame80211) Transmitter() HardwareAddr

Transmitter return Transmitter Address (TA)

type FrameType

type FrameType uint16
const (
	Management FrameType = iota
	Control
	Data
	Reserved
)

type HardwareAddr

type HardwareAddr [6]byte

A media access control address (MAC address) is a unique identifier assigned to a network interface controller (NIC) for use as a network address in communications within a network segment. This use is common in most IEEE 802 networking technologies, including Ethernet, Wi-Fi, and Bluetooth. Within the Open Systems Interconnection (OSI) network model, MAC addresses are used in the medium access control protocol sublayer of the data link layer. As typically represented, MAC addresses are recognizable as six groups of two hexadecimal digits, separated by hyphens, colons, or without a separator.

func NewHardwareAddr

func NewHardwareAddr(b0, b1, b2, b3, b4, b5 byte) HardwareAddr

NewHardwareAddr returns a new MAC address as HardwareAddr

func ParseHardwareAddr

func ParseHardwareAddr(addr string) (HardwareAddr, error)

ParseHardwareAddr parses a MAC address from the string and return HardwareAddr

func (HardwareAddr) Compare

func (h HardwareAddr) Compare(raddr HardwareAddr) bool

Compare compares two MAC addresses for equality

func (HardwareAddr) IsEmpty

func (h HardwareAddr) IsEmpty() bool

IsEmpty returns true if MAC address have only zeroes

func (HardwareAddr) Nic

func (h HardwareAddr) Nic() [3]byte

Network Interface Controller

func (HardwareAddr) Oui

func (h HardwareAddr) Oui() [3]byte

Organisationally Unique Identifier

func (HardwareAddr) String

func (h HardwareAddr) String() string

String stringifies hexadecimal MAC address to output string. You have to manually check if the mac address is correct

type PCP

type PCP uint8
const (
	PcpBE      PCP = iota + 1 // Best Effort
	PcpBK                     // Background
	PcpEE                     // Excellent Effort
	PcpCA                     // Critical Applications
	PcpVI                     // Video, < 100 ms latency and jitter
	PcpVO                     // Voice, < 10 ms latency and jitter
	PcpIC                     // Internetwork Control
	PcpNC                     // Network Control (highest)
	LowestPCP  = PcpBE
	HighestPCP = PcpNC
)

func Decode8021qTCI

func Decode8021qTCI(encoded uint16) (pcp PCP, dei uint16, vlan uint16)

Decode8021qTCI decodes encoded TCI to 3 universal values PCP, DEI, VLAN

func (PCP) String

func (pcp PCP) String() string

type Tag8021Q

type Tag8021Q struct {
	TPID uint16
	TCI  uint16
}

Jump to

Keyboard shortcuts

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