hairpin

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2021 License: Apache-2.0 Imports: 8 Imported by: 2

README

hairpin

Hairpin creates a synchronous, in-memory, half-duplex connection, and allows to set a custom function to process the data that is going through the connection.

The data written in the connection is executed by the custom Handler and sent back to the connection. Operations are serialized, once a packet is processed, Writes() are blocked until the processed packet is Read().

Partial Reads are allowed, but Writes() will not be unblocked until the Read() buffer is fully drained.

Hairpin implements golang net.Conn interface and is very useful for protocol testing.

Example

Implement an in-memory DNS resolver using a custom handler that is able to process DNS requests

https://github.com/aojea/mem-resolver

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Hairpin

func Hairpin(handler func(b []byte) []byte) net.Conn

Hairpin creates a half-duplex, in-memory, synchronous stream connection where data written on the connection is processed by the handler and then read back on the same connection. Reads and Write are serialized, Writes are blocked by Reads. If not handler is specified, data is copied directly from Tx to Rx. The handler should be safe for concurrent use by multiple goroutines

func PacketHairpin

func PacketHairpin(handler func(b []byte) []byte) net.Conn

PacketHairpin creates a half-duplex, in-memory, synchronous packet connection where data written on the connection is processed by the handler and then read back on the same connection. Reads and Write are serialized, Writes are blocked by Reads. If not handler is specified, data is copied directly from Tx to Rx. The handler should be safe for concurrent use by multiple goroutines

Types

type HairpinDialer

type HairpinDialer struct {
	Handler func(b []byte) []byte
}

HairpinDialer contains options to Dial a Hairpin connection

func (*HairpinDialer) Dial

func (h *HairpinDialer) Dial(ctx context.Context, network, address string) (net.Conn, error)

Dial creates an in memory connection that is processed by the packet handler

type HairpinListener

type HairpinListener struct {
	Handler func(b []byte) []byte
	// contains filtered or unexported fields
}

HairpinListener contains options to create a Listener that creates Hairpin connections

func (*HairpinListener) Accept

func (h *HairpinListener) Accept() (net.Conn, error)

func (*HairpinListener) Addr

func (h *HairpinListener) Addr() net.Addr

func (*HairpinListener) Close

func (h *HairpinListener) Close() error

func (*HairpinListener) Listen

func (h *HairpinListener) Listen(network, address string) (net.Listener, error)

type PacketHairpinDialer

type PacketHairpinDialer struct {
	PacketHandler func(b []byte) []byte
}

PacketHairpinDialer contains options to Dial a PacketHairpin connection

func (*PacketHairpinDialer) Dial

func (p *PacketHairpinDialer) Dial(ctx context.Context, network, address string) (net.Conn, error)

Dial creates an in memory connection that is processed by the packet handler

type PacketHairpinListener

type PacketHairpinListener struct {
	PacketHandler func(b []byte) []byte
	// contains filtered or unexported fields
}

PacketHairpinListener contains options to create a Listener that creates PacketHairpin connections

func (*PacketHairpinListener) Accept

func (p *PacketHairpinListener) Accept() (net.Conn, error)

func (*PacketHairpinListener) Addr

func (p *PacketHairpinListener) Addr() net.Addr

func (*PacketHairpinListener) Close

func (p *PacketHairpinListener) Close() error

func (*PacketHairpinListener) Listen

func (p *PacketHairpinListener) Listen(network, address string) (net.Listener, error)

Jump to

Keyboard shortcuts

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