All Projects → ghedo → go.pkt

ghedo / go.pkt

Licence: BSD-2-Clause license
📨 Go libraries for capturing, injecting, filtering, encoding and decoding network packets

Programming Languages

go
31211 projects - #10 most used programming language
c
50402 projects - #5 most used programming language

Projects that are alternatives of or similar to go.pkt

Socket-Programming-With-C
✉️ Learn Network Protocol and Network Programming
Stars: ✭ 147 (+23.53%)
Mutual labels:  protocol, network-programming
Quiche
🥧 Savoury implementation of the QUIC transport protocol and HTTP/3
Stars: ✭ 5,481 (+4505.88%)
Mutual labels:  protocol, network-programming
Msquic
Cross-platform, C implementation of the IETF QUIC protocol.
Stars: ✭ 2,501 (+2001.68%)
Mutual labels:  protocol, network-programming
Opcua Asyncio
OPC UA library for python > 3.6 asyncio
Stars: ✭ 251 (+110.92%)
Mutual labels:  protocol
StriderMqtt
A very thin MQTT client
Stars: ✭ 21 (-82.35%)
Mutual labels:  protocol
433MHz Tx Rx
Arduino based 433MHz Tx and Rx combo using Manchester protocol
Stars: ✭ 27 (-77.31%)
Mutual labels:  protocol
gofins
gofins is fins client written by Go to communicate with omron PLC
Stars: ✭ 55 (-53.78%)
Mutual labels:  protocol
Merkletreejs
🌱 Construct Merkle Trees and verify proofs in JavaScript.
Stars: ✭ 238 (+100%)
Mutual labels:  protocol
sharppcap
Official repository - Fully managed, cross platform (Windows, Mac, Linux) .NET library for capturing packets
Stars: ✭ 1,054 (+785.71%)
Mutual labels:  network-programming
osdp-python
A Python control panel implementation of the Open Supervised Device Protocol (OSDP)
Stars: ✭ 28 (-76.47%)
Mutual labels:  protocol
network
Monorepo containing all the main components of Streamr Network.
Stars: ✭ 522 (+338.66%)
Mutual labels:  protocol
goridge-php
PHP Goridge Protocol implementation
Stars: ✭ 53 (-55.46%)
Mutual labels:  protocol
procbridge
A super-lightweight IPC (Inter-Process Communication) protocol over TCP socket.
Stars: ✭ 118 (-0.84%)
Mutual labels:  protocol
seamless
Seamless is a framework to set up reproducible computations (and visualizations) that respond to changes in cells. Cells contain the input data as well as the source code of the computations, and all cells can be edited interactively.
Stars: ✭ 19 (-84.03%)
Mutual labels:  protocol
diaspora federation
A library that provides functionalities needed for the diaspora* federation protocol.
Stars: ✭ 97 (-18.49%)
Mutual labels:  protocol
Jreactive 8583
Java Client & Server for ISO8583 & Netty
Stars: ✭ 248 (+108.4%)
Mutual labels:  protocol
arancino-daemon
No description or website provided.
Stars: ✭ 12 (-89.92%)
Mutual labels:  protocol
republic-go
An official reference implementation of Republic Protocol, written in Go
Stars: ✭ 58 (-51.26%)
Mutual labels:  protocol
SuperSocketLite
SuperSocket 1.6 버전의 .NET Core 포팅
Stars: ✭ 48 (-59.66%)
Mutual labels:  network-programming
gonano
An implementation of the Nano cryptocurrency in Go
Stars: ✭ 34 (-71.43%)
Mutual labels:  protocol

go.pkt

Travis CI

go.pkt provides Go libraries for capturing, injecting, filtering, encoding and decoding network packets.

  • capture: provides the basic interface for packet capturing and injection. Different implementations ("pcap", "file", ...) are provided as subpackages.

  • filter: provides an API for compiling and manipulating BPF filters. A filter can be either compiled from tcpdump-like expressions, or created from basic BPF instructions. Filters can then be either applied to packet sources (see the capture package) or directly run against binary data.

  • packet: provides the interfaces for implementing packet encoders and decoders. Every supported protocol implements the Packet interface as a submodule of this package (e.g. packet/ipv4, packet/tcp, ...).

  • layers: 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.

  • network: provides utility functions for sending and receiving packets over the network. Basically, it hides some of the complexity of using the capture and layers packages together.

  • routing: provides network routing information about the system. It can either return all available routes or select a specific route depending on a destination address.

Getting Started

Capturing

Packet capturing is done using a packet "source" such as a network interface or a dump file.

In the following example we create a "pcap" capture handle using the eth0 network interface, we activate it and then capture packets using the Capture() method.

src, err := pcap.Open("eth0")
if err != nil {
	log.Fatal(err)
}
defer src.Close()

// you may configure the source further, e.g. by activating
// promiscuous mode.

err = src.Activate()
if err != nil {
	log.Fatal(err)
}

for {
	buf, err := src.Capture()
	if err != nil {
		log.Fatal(err)
	}

	log.Println("PACKET!!!")

	// do something with the packet
}

Injection

Similarly to packet capturing, packet injection requires a capture handle.

In the following example we create a capture handle like before and then use the Inject() method to send some data (we'll see later how to encode data in the propert formats).

dst, err := pcap.Open("eth0")
if err != nil {
	log.Fatal(err)
}
defer dst.Close()

// you may configure the source further, e.g. by activating
// promiscuous mode.

err = dst.Activate()
if err != nil {
	log.Fatal(err)
}

err = dst.Inject([]byte("random data"))
if err != nil {
	log.Fatal(err)
}

Filtering

Packet filtering is done by creating a filter (e.g. by compiling it from an expression) which can be either applied to a capture handle (by using the ApplyFilter() method) or used directly against a data buffer.

In the following example we create a filter by compiling a tcpdump-like expression and then try to match some data against it.

// Match UDP or TCP packets on top of Ethernet
flt, err := filter.Compile("udp or tcp", packet.Eth)
if err != nil {
	log.Fatal(err)
}

if flt.Match([]byte("random data")) {
	log.Println("MATCH!!!")
}

Encoding

Encoding packets is done by using the functions provided by the layers package.

In the following example we create an ARP packet on top of an Ethernet packet and we encode them to binary data by using the Pack() method. Note that you'll need to import the packages of the protocols used (packet/eth and packet/arp).

// 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)

Decoding

Like encoding, decoding is done by using the functions provided by the layers package.

The following example uses the UnpackAll() function to decode a whole chain of packets (e.g. ethernet -> ipv4 -> udp).

// 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)

Network

Instead of using the layers and capture packages together, the network package can be used instead.

The following example creates an ARP request packet and uses SendRecv() to send it and receive a suitable answer.

c, err := pcap.Open("eth0")
if err != nil {
	log.Fatal(err)
}
defer c.Close()

err = c.Activate()
if err != nil {
	log.Fatal(err)
}

// 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")

rsp_pkt, err := network.SendRecv(c, 0, eth_pkt, arp_pkt)
if err != nil {
	log.Fatal(err)
}

log.Println(rsp_pkt)

Routing

TODO

For more examples have a look at the examples directory in the source repository.

Dependencies

  • libpcap

Copyright

Copyright (C) 2014 Alessandro Ghedini [email protected]

See COPYING for the license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].