proto

package module
Version: v0.0.0-...-c90eeff Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2016 License: BSD-3-Clause Imports: 2 Imported by: 0

README

proto Circle CI GoDoc BSD License

Lean, mean protocol decoding and encoding.

Usage

package main

import (
	"log"

	"github.com/PreetamJinka/proto"
)

func main() {
	b := []byte{
		0, 15, 248, 20, 48, 0, 0, 37, 144, 82, 230, 31,
		134, 221, 96, 0, 0, 0, 0, 40, 6, 64, 38, 32, 1,
		0, 80, 7, 0, 6, 0, 0, 0, 0, 0, 1, 0, 3, 38, 32,
		1, 0, 80, 7, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 217,
		104, 0, 80, 184, 89, 70, 22, 0, 0, 0, 0, 160, 2,
		22, 128, 239, 131, 0, 0, 2, 4, 5, 160, 4, 2, 8,
		10, 184, 73, 195, 65, 0, 0, 0, 0, 1, 3, 3, 7,
	}

	ethernetFrame, err := proto.DecodeEthernet(b)
	if err != nil {
		log.Fatal(err)
	}

	if ethernetFrame.EtherType != 0x86dd {
		log.Fatalf("expected to see EtherType %x, got %x", 0x86dd, ethernetFrame.EtherType)
	}

	ipv6Packet, err := proto.DecodeIPv6(ethernetFrame.Payload)
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("Decoded an IPv6 packet: %#+v", ipv6Packet)

	if ipv6Packet.NextHeader != 0x6 {
		log.Fatalf("expected to see NextHeader %x, got %x", 0x6, ipv6Packet.NextHeader)
	}

	tcpPacket, err := proto.DecodeTCP(ipv6Packet.Payload)
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("Decoded a TCP packet: %#+v", tcpPacket)
}

Notes

Payloads are sub-sliced, not copied, so you might want to make copies if you're reusing buffers that you're decoding.

TCP and UDP encoding is not implemented.

License

BSD (see LICENSE)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrorNotEnoughBytes is returned when the length of the []byte
	// to be decoded does not exceed the minimum header length.
	ErrorNotEnoughBytes = errors.New("proto: not enough bytes available to decode")
)

Functions

This section is empty.

Types

type EthernetFrame

type EthernetFrame struct {
	Destination net.HardwareAddr `json:"source"`
	Source      net.HardwareAddr `json:"destination"`
	VlanTag     uint32           `json:"vlanTag"`
	EtherType   uint16           `json:"etherType"`
	Payload     []byte           `json:"payload"`
}

EthernetFrame represents an Ethernet frame.

func DecodeEthernet

func DecodeEthernet(b []byte) (EthernetFrame, error)

DecodeEthernet decodes an Ethernet frame.

func (EthernetFrame) Bytes

func (f EthernetFrame) Bytes() []byte

Bytes returns an encoded Ethernet frame.

type IPv4Packet

type IPv4Packet struct {
	Version              uint8  `json:"version"`
	InternetHeaderLength uint8  `json:"internetHeaderLength"`
	DSCP                 uint8  `json:"dscp"`
	ECN                  uint8  `json:"ecn"`
	Length               uint16 `json:"length"`
	Identification       uint16 `json:"indentification"`
	Flags                uint8  `json:"flags"`
	FragmentationOffset  uint16 `json:"fragmentationOffset"`
	TimeToLive           uint8  `json:"timeToLive"`
	Protocol             uint8  `json:"protocol"`
	HeaderChecksum       uint16 `json:"headerChecksum"`
	Source               net.IP `json:"source"`
	Destination          net.IP `json:"destination"`
	Options              []byte `json:"options"`
	Payload              []byte `json:"payload"`
}

IPv4Packet represents an IPv4 packet.

func DecodeIPv4

func DecodeIPv4(b []byte) (IPv4Packet, error)

DecodeIPv4 decodes an IPv4 packet.

func (IPv4Packet) Bytes

func (p IPv4Packet) Bytes() []byte

Bytes returns an encoded IPv4 packet.

func (IPv4Packet) ComputeChecksum

func (p IPv4Packet) ComputeChecksum() uint16

ComputeChecksum returns the checksum for the IPv4 packet.

type IPv6Packet

type IPv6Packet struct {
	Version      uint8  `json:"version"`
	TrafficClass uint8  `json:"trafficClass"`
	FlowLabel    uint32 `json:"flowLabel"`
	Length       uint16 `json:"length"`
	NextHeader   uint8  `json:"nextHeader"`
	HopLimit     uint8  `json:"hopLimit"`
	Source       net.IP `json:"source"`
	Destination  net.IP `json:"destination"`
	Payload      []byte `json:"payload"`
}

IPv6Packet represents an IPv6 packet.

func DecodeIPv6

func DecodeIPv6(b []byte) (IPv6Packet, error)

DecodeIPv6 decodes an IPv6 packet.

func (IPv6Packet) Bytes

func (p IPv6Packet) Bytes() []byte

Bytes returns an encoded IPv6 packet.

type TCPPacket

type TCPPacket struct {
	SourcePort            uint16 `json:"sourcePort"`
	DestinationPort       uint16 `json:"destinationPort"`
	SequenceNumber        uint32 `json:"sequenceNumber"`
	AcknowledgementNumber uint32 `json:"acknowledgementNumber"`
	DataOffset            uint8  `json:"dataOffset"`

	// We have 9 bits of flags
	// so we're going to waste a
	// few bits of space using a
	// uint16.
	Flags uint16 `json:"flags"`

	WindowSize    uint16 `json:"windowSize"`
	Checksum      uint16 `json:"checksum"`
	UrgentPointer uint16 `json:"urgentPointer"`

	Options []byte `json:"options"`
	Payload []byte `json:"payload"`
}

TCPPacket represents a TCP packet.

func DecodeTCP

func DecodeTCP(b []byte) (TCPPacket, error)

DecodeTCP decodes an TCP packet.

func (TCPPacket) HasACK

func (p TCPPacket) HasACK() bool

HasACK returns true if the ACK flag is set.

func (TCPPacket) HasCWR

func (p TCPPacket) HasCWR() bool

HasCWR returns true if the CWR flag is set.

func (TCPPacket) HasECE

func (p TCPPacket) HasECE() bool

HasECE returns true if the ECE flag is set.

func (TCPPacket) HasFIN

func (p TCPPacket) HasFIN() bool

HasFIN returns true if the FIN flag is set.

func (TCPPacket) HasNS

func (p TCPPacket) HasNS() bool

HasNS returns true if the NS flag is set.

func (TCPPacket) HasPSH

func (p TCPPacket) HasPSH() bool

HasPSH returns true if the PSH flag is set.

func (TCPPacket) HasRST

func (p TCPPacket) HasRST() bool

HasRST return true if the RST flag is set.

func (TCPPacket) HasSYN

func (p TCPPacket) HasSYN() bool

HasSYN returns true if the SYN flag is set.

func (TCPPacket) HasURG

func (p TCPPacket) HasURG() bool

HasURG returns true if the URG flag is set.

type UDPPacket

type UDPPacket struct {
	SourcePort      uint16 `json:"sourcePort"`
	DestinationPort uint16 `json:"destinationPort"`
	Length          uint16 `json:"length"`
	Checksum        uint16 `json:"checksum"`
	Payload         []byte `json:"payload"`
}

UDPPacket represents a UDP packet.

func DecodeUDP

func DecodeUDP(b []byte) (UDPPacket, error)

DecodeUDP decodes an UDP packet.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL