layers

package
v0.0.0-...-c97f47a Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2020 License: BSD-2-Clause Imports: 15 Imported by: 0

Documentation

Overview

Provides utility functions for encoding and decoding packets to/from binary data. Differently from the basic "packet" interface, this can encode and decode complete "stacks" of packets, instead of manipulating single ones.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compose

func Compose(pkts ...packet.Packet) (packet.Packet, error)

Compose packets into a chain and update their values (e.g. length, payload protocol) accordingly.

func FindLayer

func FindLayer(p packet.Packet, layer packet.Type) packet.Packet

Return the first layer of the given type in the packet. If no suitable layer is found, return nil.

func Pack

func Pack(pkts ...packet.Packet) ([]byte, error)

Pack packets into their binary form. This will stack the packets before encoding them (see the Compose() method) and also calculate the checksums.

Example
package main

import (
	"log"
	"net"

	"github.com/ghedo/go.pkt/layers"

	"github.com/ghedo/go.pkt/packet/arp"
	"github.com/ghedo/go.pkt/packet/eth"
)

func main() {
	// Create an Ethernet packet
	eth_pkt := eth.Make()
	eth_pkt.SrcAddr, _ = net.ParseMAC("4c:72:b9:54:e5:3d")
	eth_pkt.DstAddr, _ = net.ParseMAC("ff:ff:ff:ff:ff:ff")

	// Create an ARP packet
	arp_pkt := arp.Make()
	arp_pkt.HWSrcAddr, _ = net.ParseMAC("4c:72:b9:54:e5:3d")
	arp_pkt.HWDstAddr, _ = net.ParseMAC("00:00:00:00:00:00")
	arp_pkt.ProtoSrcAddr = net.ParseIP("192.168.1.135")
	arp_pkt.ProtoDstAddr = net.ParseIP("192.168.1.254")

	buf, err := layers.Pack(eth_pkt, arp_pkt)
	if err != nil {
		log.Fatal(err)
	}

	// do something with the packet
	log.Println(buf)
}
Output:

func Unpack

func Unpack(buf []byte, pkts ...packet.Packet) (packet.Packet, error)

Unpack the given byte slice into the packet list supplied. Note that this will not check whether the packet types provided match the raw data. If the packet types to be decoded are unknown, UnpackAll() should be used instead.

Note that unpacking is done without copying the input slice, which means that if the slice is modifed, it may affect the packets that where unpacked from it. If you can't guarantee that the data slice won't change, you'll need to copy it and pass the copy to Unpack().

Example
package main

import (
	"log"

	"github.com/ghedo/go.pkt/layers"
	"github.com/ghedo/go.pkt/packet"
)

func main() {
	// Create the buf data
	buf := []byte("random data")

	// Assume Ethernet as datalink layer
	pkt, err := layers.UnpackAll(buf, packet.Eth)
	if err != nil {
		log.Fatal(err)
	}

	log.Println(pkt)
}
Output:

func UnpackAll

func UnpackAll(buf []byte, link_type packet.Type) (packet.Packet, error)

Recursively unpack the given byte slice into a packet. The link_type argument must specify the type of the first layer in the input data, successive layers will be detected automatically.

Note that unpacking is done without copying the input slice, which means that if the slice is modifed, it may affect the packets that where unpacked from it. If you can't guarantee that the data slice won't change, you'll need to copy it and pass the copy to UnpackAll().

Types

This section is empty.

Jump to

Keyboard shortcuts

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