ethernet

package module
v0.0.0-...-a34bbc6 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2020 License: Apache-2.0 Imports: 6 Imported by: 2

README

ethernet Build Status GoDoc Go Report Card Coverage Status

uml class diagram

@startuml

title ethernet

class Frame {
    +Destination net.HardwareAddr
    +Source net.HardwareAddr
    +VLAN   []*VLAN
    +EtherType EtherType
    +Payload []byte
    +MarshalBinary() []byte
    +UnmarshalBinary([]byte)
    +MarshalFCS() []byte
    +UnmarshalFCS([]byte)
    -read([]byte)
    -length()
}

enum EtherType {
    IPv4
    ARP
    IPv6
    VLAN
}

enum Priority {
	Background
	BestEffort
	ExcellentEffort
    CriticalApplications
	Video
	Voice
	InternetworkControl
	NetworkControl
}

interface net.HardwareAddr {}

class VLAN {
    +Priority
    +DropEligible
    +ID
    +MarshalBinary() []byte
    -read([]byte)
    +UnmarshalBinary([]byte)
}

Frame *-- VLAN

@enduml

Documentation

Overview

Package ethernet implements marshaling and unmarshaling of IEEE 802.3 Ethernet II frames and IEEE 802.10 VLAN tags.

Index

Constants

View Source
const (
	// VLANNone is a special VLAN ID which indicates that no VLAN is being
	// used in a Frame. In this case, the VLAN's other fields may be used
	// to indicate a Frame's priority
	VLANNone = 0x000

	// VLANMax is a reserved VLAN ID which may indicate a wildcard in some
	// management system, but may not configured or transmitted in a
	// VLAN tag.
	VLANMax = 0xfff
)

Variables

View Source
var (
	// Broadcast is a special hardware address which indicates a Frame should be
	// sent to every device on a given LAN segment.
	Broadcast = net.HardwareAddr{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}

	// ErrInvalidFCS is returned when Frame.UnmarshalFCS detects an incorrect
	// Ethernet frame check sequence in a byte slice for a Frame.
	ErrInvalidFCS = errors.New("invalid frame check sequence")
)
View Source
var (
	// ErrInvalidVLAN is returned when a VLAN tag is invalid due to one of the
	// following reasons:
	//   - Priority of greater than 7 is detected
	//   - ID of greater than 4094 (0xffe) is detected
	ErrInvalidVLAN = errors.New("invalid VLAN")
)

Functions

This section is empty.

Types

type EtherType

type EtherType uint16

An EtherType is a value used to identify an upper layer protocol encapsulated in a Frame

const (
	EtherTypeIPv4 EtherType = 0x0800
	EtherTypeARP  EtherType = 0x0806
	EtherTypeVLAN EtherType = 0x8100
	EtherTypeIPv6 EtherType = 0x86DD
)

CommonEtherType values frequently used in a Frame

func (EtherType) String

func (i EtherType) String() string

type Frame

type Frame struct {
	// Destination specifies the destination hardware address for this Frame.
	// If this address is set to Broadcast, the Frame will be sent to every
	// device on a given LAN segment.
	Destination net.HardwareAddr

	// Source specifies the source hardware address for this Frame. Typically,
	// this hardware address is the address of the network interface used to send
	// this Frame.
	Source net.HardwareAddr

	// Vlan specifies one or more optional 802.1Q VLAN tags, which many or may
	// not be present in a Frame. If no VLAN tags are present, this length of
	// the slice will be 0.
	VLAN []*VLAN

	// EtherType is a value used to identify an upper layer protocol
	// encapsulated in this Frame.
	EtherType EtherType

	// Payload is a variable length data payload encapsulated by this Frame
	Payload []byte
}

A Frame is an IEEE 802.3 Ethernet II frame. A Frame contains information such as source and destination hardware addresses, zero or more optional 802.1Q VLAN tags, an EtherType, and payload data.

func (*Frame) MarshalBinary

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

MarshalBinary allocates a byte slice and marshals a Frame into binary form.

If one or more VLANs are set and their IDs are too large (greater than 4094), or one or more VLANs' priority are too large (greater than 7), ErrInvalidVLAN is returned

func (*Frame) MarshalFCS

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

MarshalFCS allocates a byte slice, marshals a Frame into binary form, and finally calculates and places a 4-byte IEEE CRC32 frame check sequence at the end of the slice

func (*Frame) UnmarshalBinary

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

UnmarshalBinary unmarshals a byte slice into a Frame

If the byte slice does not contain enough data to unmarshal a valid Frame, io.ErrUnexpectedEOF is returned.

If one or more VLANs are detected and their IDs are too large (greater than 4094), ErrInvalidVLAN is returned

func (*Frame) UnmarshalFCS

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

UnmarshalFCS computes the IEEE CRC32 frame check sequence of a Frame, verifies it against the checksum present in the byte slice, and finally, unmarshals a byte slice into a Frame

type Priority

type Priority uint8

Priority is an IEEE P802.1p priority level. Priority can be any value from 0 to 7

It is important to note that priority 1 (PriorityBackground) actually has a lower priority than 0 (PriorityBestEffor). All other Priority constants indicate higher priority as the integer values increase

const (
	PriorityBackground           Priority = 1
	PriorityBestEffort           Priority = 0
	PriorityExcellentEffort      Priority = 2
	PriorityCriticalApplications Priority = 3
	PriorityVideo                Priority = 4
	PriorityVoice                Priority = 5
	PriorityInternetworkControl  Priority = 6
	PriorityNetworkControl       Priority = 7
)

IEEE P802.1p recommended priority levels. Note that PriorityBackground has a lower prirority than PriorityBestEffor

type VLAN

type VLAN struct {
	// Priority specifies an IEEE 802.1p priority level.
	Priority Priority

	// DropEligible indicates if a Frame is eligible to be dropped in the
	// presence of network congestion
	DropEligible bool

	// ID specifies the VLAN ID for a Frame. ID can be any value from 0 to
	// 4094 (0x000 to 0xffe), allowing up to 4094 VLANs.
	//
	// If ID is 0 (0x000, VLANNone), no VLAN is specified, and the other fields
	// simply indicate a Frame's priority
	ID uint16
}

A VLAN is an IEEE 802.1Q Virtual LAN (VLAN) tag. A VLAN contains information regarding traffic priority and a VLAN identifier for a given Frame.

func (*VLAN) MarshalBinary

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

MarshalBinary allocates a byte slice and marshals a VLAN into binary form.

If a VLAN ID is too large (greater than 4094), ErrInvalidVLAN is returned. If a VLAN priority is too large (greater than 7), ErrInvalidVLAN is returned.

func (*VLAN) UnmarshalBinary

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

UnmarshalBinary unmarshals a byte slice into a VLAN

If the byte slice does not contain exactly 2 bytes of data, io.ErrUnexpectedEOF is returned

If a VLAN ID is too large (greater than 4094), ErrInvalidVLAN is returned.

Directories

Path Synopsis
cmd
etherecho
Command etherecho broadcasts a message to all machines in the same network segment, and listens for other messages from other etherecho servers.
Command etherecho broadcasts a message to all machines in the same network segment, and listens for other messages from other etherecho servers.

Jump to

Keyboard shortcuts

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