Package ethernet implements marshaling and unmarshaling of IEEE 802.3 Ethernet II frames and IEEE 802.1Q VLAN tags. MIT Licensed.

ethernet Build Status GoDoc Go Report Card

Package ethernet implements marshaling and unmarshaling of IEEE 802.3 Ethernet II frames and IEEE 802.1Q VLAN tags. MIT Licensed.

For more information about using Ethernet frames in Go, check out my blog post: Network Protocol Breakdown: Ethernet and Go.

Owner
Matt Layher
Software Engineer. Go, Linux, and open source software enthusiast. On and ever upward.
Matt Layher
Similar Resources

Gmqtt is a flexible, high-performance MQTT broker library that fully implements the MQTT protocol V3.1.1 and V5 in golang

中文文档 Gmqtt News: MQTT V5 is now supported. But due to those new features in v5, there area lots of breaking changes. If you have any migration problem

Jan 5, 2023

A little ping pong service that implements rate limiting with golang

Fred the Guardian Introduction Writing a little ping pong service that implements rate limiting with the programming language golang. Requirements Web

Jan 2, 2022

P2PDistributedHashTable - A golang Kademlia/Bittorrent DHT library that implements BEP5

P2PDistributedHashTable - A golang Kademlia/Bittorrent DHT library that implements BEP5

This is a golang Kademlia/Bittorrent DHT library that implements BEP 5. It's typ

Apr 10, 2022

TritonHTTP - A simple web server that implements a subset of the HTTP/1.1 protocol specification

TritonHTTP Spec Summary Here we provide a concise summary of the TritonHTTP spec. You should read the spec doc for more details and clarifications. HT

Nov 5, 2022

Fetch-npm-package - A small utility that can be used to fetch a given version of a NPM package

Use fetch-npm-package package version output-dir E.g. fetch-npm-package is

May 21, 2022

Go package to simulate bandwidth, latency and packet loss for net.PacketConn and net.Conn interfaces

lossy Go package to simulate bandwidth, latency and packet loss for net.PacketConn and net.Conn interfaces. Its main usage is to test robustness of ap

Oct 14, 2022

GopherTalk: a multi-user chat powered by GO to explore its standard library and features like sockets, goroutines, channels and sync package

GopherTalk: a multi-user chat powered by GO to explore its standard library and features like sockets, goroutines, channels and sync package

GopherTalk GopherTalk is a multi-user chat powered by GO to explore its standard

Jun 6, 2022

golibwireshark - Package use libwireshark library to decode pcap file and analyse dissection data.

golibwireshark Package golibwireshark use libwireshark library to decode pcap file and analyse dissection data. This package can only be used in OS li

Nov 26, 2022
Comments
  • Where does c.WriteTo come from?

    Where does c.WriteTo come from?

    In etherecho c.WriteTo function is used. https://github.com/mdlayher/ethernet/blob/529eae5b61181083b306246aa4c93f0761efa8a2/cmd/etherecho/main.go#L85 Where does this function come from?

  • Enhancing `ethernet/cmd` for peripheral testing

    Enhancing `ethernet/cmd` for peripheral testing

    First of all, thank you for this library! I'd like to suggest a modification to etherecho which I believe would help me and others have more flexibility for smaller targets. In particular I am writing a ENC28J60 library for tinygo and expect to be using etherecho to test input/output ethernet frames. I was wondering if it is reasonable to think the following modifications to etherecho would help me:

    • Making etherType=0xcccc (the unused EtherType) optional. allowing specification of other types through strings (ARP,etc)
    • Setting a destination address

    I've made my own etherecho, Here's the result (not tested):

    // Command etherecho broadcasts a message to all machines in the same network
    // segment, and listens for other messages from other etherecho servers.
    //
    // etherecho only works on Linux and BSD, and requires root permission or
    // CAP_NET_ADMIN on Linux.
    package main
    
    import (
    	"flag"
    	"log"
    	"net"
    	"os"
    	"time"
    
    	"github.com/mdlayher/ethernet"
    	"github.com/mdlayher/raw"
    )
    
    // Make use of an unassigned EtherType for etherecho.
    // https://www.iana.org/assignments/ieee-802-numbers/ieee-802-numbers.xhtml
    const UnusedEtherType = 0xcccc
    
    func main() {
    	var (
    		ifaceFlag   = flag.String("i", "", "network interface to use to send and receive messages")
    		msgFlag     = flag.String("m", "", "message to be sent (default: system's hostname)")
    		dstAddrFlag = flag.String("d", "", "destination address (default is broadcast: ff:ff:ff:ff:ff:ff)")
    		etherType   = flag.Int("e", UnusedEtherType, "Ether-Type (default is unused EtherType: 0xcccc, see https://www.iana.org/assignments/ieee-802-numbers/ieee-802-numbers.xhtml)")
    	)
    
    	flag.Parse()
    
    	// Open a raw socket on the specified interface, and configure it to accept
    	// traffic with etherecho's EtherType.
    	ifi, err := net.InterfaceByName(*ifaceFlag)
    	if err != nil {
    		log.Fatalf("failed to find interface %q: %v", *ifaceFlag, err)
    	}
    
    	dstAddr := ethernet.Broadcast
    	if *dstAddrFlag != "" {
    		dstAddr, err = net.ParseMAC(*dstAddrFlag)
    		if err != nil {
    			log.Fatalf("failed to find interface %q: %v", *ifaceFlag, err)
    		}
    	}
    
    	c, err := raw.ListenPacket(ifi, uint16(*etherType), nil)
    	if err != nil {
    		log.Fatalf("failed to listen: %v", err)
    	}
    
    	// Default message to system's hostname if empty.
    	msg := *msgFlag
    	if msg == "" {
    		msg, err = os.Hostname()
    		if err != nil {
    			log.Fatalf("failed to retrieve hostname: %v", err)
    		}
    	}
    
    	// Send messages in one goroutine, receive messages in another.
    	go sendMessages(c, dstAddr, ifi.HardwareAddr, ethernet.EtherType(*etherType), msg)
    	go receiveMessages(c, ifi.MTU)
    
    	// Block forever.
    	select {}
    }
    
    // sendMessages continuously sends a message over a connection at regular intervals,
    // sourced from specified hardware address.
    func sendMessages(c net.PacketConn, dst, source net.HardwareAddr, etherType ethernet.EtherType, msg string) {
    	// Message is broadcast to all machines in same network segment.
    	f := &ethernet.Frame{
    		Destination: dst,
    		Source:      source,
    		EtherType:   etherType,
    		Payload:     []byte(msg),
    	}
    
    	b, err := f.MarshalBinary()
    	if err != nil {
    		log.Fatalf("failed to marshal ethernet frame: %v", err)
    	}
    
    	// Required by Linux, even though the Ethernet frame has a destination.
    	// Unused by BSD.
    	dstAddr := &raw.Addr{
    		HardwareAddr: dst,
    	}
    
    	// Send message forever.
    	t := time.NewTicker(1 * time.Second)
    	for range t.C {
    		if _, err := c.WriteTo(b, dstAddr); err != nil {
    			log.Fatalf("failed to send message: %v", err)
    		}
    	}
    }
    
    // receiveMessages continuously receives messages over a connection. The messages
    // may be up to the interface's MTU in size.
    func receiveMessages(c net.PacketConn, mtu int) {
    	var f ethernet.Frame
    	b := make([]byte, mtu)
    
    	// Keep receiving messages forever.
    	for {
    		n, addr, err := c.ReadFrom(b)
    		if err != nil {
    			log.Fatalf("failed to receive message: %v", err)
    		}
    
    		// Unpack Ethernet II 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))
    	}
    }
    
  • Swap in github.com/mdlayher/packet

    Swap in github.com/mdlayher/packet

    I also gofumpted the code.

    I will also send PR for mdlayher/arp. I will send it after this merges to use the latest version of ethernet (arp depends on ethernet because of cmd/proxyarpd).

  • ethernet: issue with Q-in-Q tagging

    ethernet: issue with Q-in-Q tagging

    It appears I've made a mistake with my Q-in-Q tagging implementation.

    https://en.wikipedia.org/wiki/IEEE_802.1Q#Double_tagging

    Note that the TPID for S-VLAN is different than C-VLAN. I'll probably just end up splitting them into two separate fields instead of the slice that exists now.

Related tags
V3IO Frames ("Frames") is a Golang based remote data frames access (over gRPC or HTTP stream)

V3IO Frames ("Frames") is a multi-model open-source data-access library that provides a unified high-performance DataFrame API for working with different types of data sources (backends). The library was developed by Iguazio to simplify working with data in the Iguazio Data Science Platform ("the platform"), but it can be extended to support additional backend types.

Oct 1, 2022
Package arp implements the ARP protocol, as described in RFC 826. MIT Licensed.

arp Package arp implements the ARP protocol, as described in RFC 826. MIT Licensed. Portions of this code are taken from the Go standard library. The

Dec 20, 2022
Package dhcp6 implements a DHCPv6 server, as described in RFC 3315. MIT Licensed.

dhcp6 Package dhcp6 implements a DHCPv6 server, as described in IETF RFC 3315. MIT Licensed. At this time, the API is not stable, and may change over

Sep 27, 2022
A Go package for sending and receiving ethernet frames. Currently supporting Linux, Freebsd, and OS X.

ether ether is a go package for sending and receiving ethernet frames. Currently supported platform: BPF based OS X FreeBSD AF_PACKET based Linux Docu

Sep 27, 2022
The Swiss Army knife for 802.11, BLE and Ethernet networks reconnaissance and MITM attacks.
The Swiss Army knife for 802.11, BLE and Ethernet networks reconnaissance and MITM attacks.

bettercap is a powerful, easily extensible and portable framework written in Go which aims to offer to security researchers, red teamers and reverse e

Jan 3, 2023
A Protocol Buffers compiler that generates optimized marshaling & unmarshaling Go code for ProtoBuf APIv2

vtprotobuf, the Vitess Protocol Buffers compiler This repository provides the protoc-gen-go-vtproto plug-in for protoc, which is used by Vitess to gen

Jan 1, 2023
Package raw enables reading and writing data at the device driver level for a network interface. MIT Licensed.

raw Package raw enables reading and writing data at the device driver level for a network interface. MIT Licensed. For more information about using ra

Dec 28, 2022
Package socket provides a low-level network connection type which integrates with Go's runtime network poller to provide asynchronous I/O and deadline support. MIT Licensed.

socket Package socket provides a low-level network connection type which integrates with Go's runtime network poller to provide asynchronous I/O and d

Dec 14, 2022
CoreRAD is an extensible and observable IPv6 Neighbor Discovery Protocol router advertisement daemon. Apache 2.0 Licensed.
CoreRAD is an extensible and observable IPv6 Neighbor Discovery Protocol router advertisement daemon. Apache 2.0 Licensed.

CoreRAD CoreRAD is an extensible and observable IPv6 Neighbor Discovery Protocol router advertisement daemon. Apache 2.0 Licensed. To get started with

Nov 14, 2022
Gcra - Package gcra implements the generic cell rate algorithm

gcra Package gcra implements the generic cell rate algorithm (GCRA). Example opt

Jan 23, 2022