Network Protocol Breakdown: Ethernet and Go

Introduction to Ethernet frames

Source: https://en.wikipedia.org/wiki/Ethernet_frame#Ethernet_II

Crafting an Ethernet frame in Go

// The frame to be sent over the network.
f := &ethernet.Frame{
// Broadcast frame to all machines on same network segment.
Destination: ethernet.Broadcast,
// Identify our machine as the sender.
Source: net.HardwareAddr{0xde, 0xad, 0xbe, 0xef, 0xde, 0xad},
// Identify frame with an unused EtherType.
EtherType: 0xcccc,
// Send a simple message.
Payload: []byte("hello world"),
}
// Marshal the Go representation of a frame to
// the Ethernet frame format.
b, err := f.MarshalBinary()
if err != nil {
log.Fatalf("failed to marshal frame: %v", err)
}
// Send the marshaled frame to the network.
sendEthernetFrame(b)

Introduction to VLAN tags

Source: http://sclabs.blogspot.com/2014/10/ccnp-switch-vlans-and-trunks.html *note: CFI has now been re-purposed as Drop Eligible Indicator (DEI) instead.
  • Priority (3 bits): an IEEE P8021.p class of service level.
  • Drop Eligible Indicator (DEI; formerly CFI) (1 bit): indicates if a frame may be dropped in the presence of network congestion.
  • VLAN ID (VID) (12 bits): specifies the VLAN to which the frame belongs. Each VID creates a unique network segment.

Specifying VLAN tags for Ethernet frames in Go

// The frame to be sent over the network.
f := &ethernet.Frame{
// Broadcast frame to all machines on same network segment.
Destination: ethernet.Broadcast,
// Identify our machine as the sender.
Source: net.HardwareAddr{0xde, 0xad, 0xbe, 0xef, 0xde, 0xad},
// Tag traffic to VLAN 10. If needed, multiple tags
// can be applied for Q-in-Q.
VLAN: []*ethernet.VLAN{{
ID: 10,
}},
// Identify frame with an unused EtherType.
EtherType: 0xcccc,
// Send a simple message.
Payload: []byte("hello world"),
}

Sending and receiving Ethernet frames over the network

// Select the eth0 interface to use for Ethernet traffic.
ifi, err := net.InterfaceByName("eth0")
if err != nil {
log.Fatalf("failed to open interface: %v", err)
}
// Open a raw socket using same EtherType as our frame.
c, err := raw.ListenPacket(ifi, 0xcccc)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
defer c.Close()
// Marshal a frame to its binary format.
f := newEthernetFrame("hello world")
b, err := f.MarshalBinary()
if err != nil {
log.Fatalf("failed to marshal frame: %v", err)
}
// Broadcast the frame to all devices on our network segment.
addr := &raw.Addr{HardwareAddr: ethernet.Broadcast}
if _, err := c.WriteTo(b, addr); err != nil {
log.Fatalf("failed to write frame: %v", err)
}
// Select the eth0 interface to use for Ethernet traffic.
ifi, err := net.InterfaceByName("eth0")
if err != nil {
log.Fatalf("failed to open interface: %v", err)
}
// Open a raw socket using same EtherType as our frame.
c, err := raw.ListenPacket(ifi, 0xcccc)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
defer c.Close()
// Accept frames up to interface's MTU in size.
b := make([]byte, ifi.MTU)
var f ethernet.Frame
// Keep reading frames.
for {
n, addr, err := c.ReadFrom(b)
if err != nil {
log.Fatalf("failed to receive message: %v", err)
}
// Unpack Ethernet frame into Go representation.
if err := (&f).UnmarshalBinary(b[:n]); err != nil {
log.Fatalf("failed to unmarshal ethernet frame: %v", err)
}
// Display source of message and message itself.
log.Printf("[%s] %s", addr.String(), string(f.Payload))
}

Summary

Links

References

--

--

--

No longer active here, please see https://mdlayher.com/. Thanks!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

ShardingSphere 4.x -Sharding-Scaling(Alpha)

Docker 08 / Automated Builds

RxJava2: Schedulers 101 or simplified concurrency, part 1.

5 Free Cheat Sheets That Every Developer Should Know About.

How to Plan for Your Big Mobile App Launch

Where Software Development interest started

Troop burst Boko Haram logistics base in Gujba Today

Free Coding Bootcamp Guide

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Matt Layher

Matt Layher

No longer active here, please see https://mdlayher.com/. Thanks!

More from Medium

Graceful shutdown concurrent Go program with WaitGroup and Context

Part 2: Grpc Proto Code Generation Using Protoc for Message and Services

BatchConn — sendmmsg/recvmmsg in Go

Network Visualization

Importing private repos from Github