ethernet

package module
v0.0.0-...-10e1afe Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2025 License: MIT Imports: 6 Imported by: 2

README

Ethernet

This package provide easy way to marshal and unmarshal 802.3 Ethernet and 802.1Q Ethernet frames. Written with pure Go. MIT License.

Documentation

Index

Constants

View Source
const (
	EtherTypeIPv4 = 0x0800
	EtherTypeARP  = 0x0806
	EtherTypeIPv6 = 0x86DD
	EtherTypeVLAN = 0x8100
	EtherTypeQinQ = 0x88a8
)

Most popular ether types

View Source
const VIDMax = 0xFFF

Variables

This section is empty.

Functions

This section is empty.

Types

type EtherSocket

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

func NewEtherSocket

func NewEtherSocket(sock *socket.Interface) (*EtherSocket, error)

NewEtherSocket create ether socket over device

func (*EtherSocket) GetGatewayHWAddr

func (es *EtherSocket) GetGatewayHWAddr() net.HardwareAddr

GetGatewayHWAddr returns mac address of device's default gateway

func (*EtherSocket) GetHWAddr

func (es *EtherSocket) GetHWAddr() net.HardwareAddr

GetHWAddr returns mac address of device

func (*EtherSocket) MTU

func (es *EtherSocket) MTU() int

MTU returns maximum transmission unit of device

func (*EtherSocket) Name

func (es *EtherSocket) Name() string

Name returns device name

func (*EtherSocket) Read

func (es *EtherSocket) Read() ([]byte, error)

Read returns payload of ethernet frame

func (*EtherSocket) ReadFrame

func (es *EtherSocket) ReadFrame() (*Frame, error)

ReadFrame returns full ethernet frame with payload

func (*EtherSocket) Write

func (es *EtherSocket) Write(data []byte) error

WriteTo writes data to socket, destination is default gateway. Use Ether Type IPv4

func (*EtherSocket) WriteFrame

func (es *EtherSocket) WriteFrame(f *Frame) error

func (*EtherSocket) WriteTo

func (es *EtherSocket) WriteTo(to net.HardwareAddr, data []byte) error

WriteTo writes data to socket, with certain destination mac addr. Use Ether Type IPv4

type Frame

type Frame struct {
	// Destination MAC address
	DestHarwAddr net.HardwareAddr
	// Source MAC address
	SrcHarwAddr net.HardwareAddr
	// IEEE 802.1Q (VLAN) header
	// IEEE 802.1Q is the networking standard that supports virtual local area networking (VLANs)
	// on an IEEE 802.3 Ethernet network.
	VLAN *VLAN
	// IEEE 802.1ad (VLAN) header
	// QinQ frame is a frame that has two VLAN 802.1Q headers (i.e. it is double-tagged).
	ServiceVLAN *VLAN
	// 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
	// https://en.wikipedia.org/wiki/EtherType
	EtherType uint16
	// Data of frame
	Payload []byte
}

Ethernet 802.3 frame

func (*Frame) Marshal

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

func (*Frame) Unmarshal

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

type VLAN

type VLAN struct {
	// Different PCP values can be used to prioritize different classes of traffic
	Priority uint8
	// This field was formerly designated Canonical Format Indicator (CFI)
	// with a value of 0 indicating a MAC address in canonical format.
	// It is always set to zero for Ethernet.
	DropEligible bool
	// Field specifying the VLAN to which the frame belongs.
	ID uint16
}

IEEE 802.1Q vlan structure

func (*VLAN) Marshal

func (v *VLAN) Marshal() ([]byte, error)

func (*VLAN) Unmarshal

func (v *VLAN) Unmarshal(b []byte) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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