cattp

package module
v0.0.0-...-fa27e1b Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2015 License: BSD-3-Clause Imports: 11 Imported by: 0

README

CAT_TP

CAT_TP is a golang package implementing a CAT_TP protocol layer for both, client and server side applications. The decoding of CAT_TP packets from a PCAP stream is also supported.

##Specification:

This implementation has been built based on the ETSI specification and makes heavy use of this libpcap wrapper by miekg. Thanks !

Documentation:

Is available at at godoc.org.

TODO

  • Deal with MaxSDU and MaxPDU size from SYN packet.
  • Implement CAT_TP port allocation. Currently source and destination ports must be specified explicitly.
  • Add callback hooks for connection states.
  • Implement tcpdump like command line util

Documentation

Index

Examples

Constants

View Source
const (
	UNK     = iota
	SYN     = iota
	SYNACK  = iota
	ACK     = iota
	RST     = iota
	DATAACK = iota
	NUL     = iota
	EAK     = iota
)
View Source
const (
	FLAG_MASK = 0xFC
	SYN_FLAG  = 0x80
	ACK_FLAG  = 0x40
	EAK_FLAG  = 0x20
	RST_FLAG  = 0x10
	NUL_FLAG  = 0x08
	SEG_FLAG  = 0x04
)
View Source
const (
	BASE_HLEN = 18
	SYN_HLEN  = 5
)
View Source
const (
	DefaultMaxPDUSize  = 1024
	DefaultMaxSDUSize  = 512
	DefaultSrcPort     = 1
	DefaultDestPort    = 9000
	DefaultWindowSize  = 10
	MaxHeaderLen       = 255
	MaxIdenficationLen = MaxHeaderLen - (BASE_HLEN + 4 + 1)
	MaxEAKs            = 118 /*(MaxHederLen - (BASE_HLEN))/2 */
)
View Source
const (
	CLOSE     = iota
	LISTEN    = iota
	SYNRCVD   = iota
	OPEN      = iota
	CLOSEWAIT = iota
	SYNSENT   = iota
)

Connection states

View Source
const (
	CONNECTION_HANDLER = iota
	RECEIVEWINDOW_SIZE = iota
	IDENTIFICATION     = iota
)

Socket parameter

View Source
const (
	LOGPREFIX = "CATTP"
)

Variables

View Source
var CLOSEWAITTimeout time.Duration = 1 * time.Second
View Source
var RetransmitCount int = 2

RetransmitCount is a configurable value to specify the amount of retries that will be performed until a connection is considered to be disconnected.

View Source
var RetransmitTimeout time.Duration = 2 * time.Second

Functions

func BufferHandler

func BufferHandler(c *Connection, ps []*Header, data []byte)

BufferHandler is the default handler that pushes all incoming data to the corresponding buffer. This data can be read by using the io.Reader interface accordingly.

func EchoHandler

func EchoHandler(c *Connection, ps []*Header, data []byte)

EchoHandler is a default handler that just prints the payload to stdout.

func IgnoreHandler

func IgnoreHandler(c *Connection, ps []*Header, data []byte)

IgnoreHandler ignoresincoming bytes

func KeepListening

func KeepListening(as string, lport uint16, handler Handler, params ...SocketParameters) (err error)

Listen starts a CAT_TP server synchronously. It will block until the server is in state CLOSED.

func LogHandler

func LogHandler(c *Connection, ps []*Header, data []byte)

LogHandler logs the packet structure to the logging framework.

func NewSeqNo

func NewSeqNo() uint16

func Nibble

func Nibble(data []byte) []byte

switches the byte array per nibble (like big to little endian just with nibbles)

Types

type Connection

type Connection struct {
	// contains filtered or unexported fields
}

Connection represents a CAT_TP connection.

func Connect

func Connect(addr string, lport, rport uint16, id []byte, handler Handler, params ...SocketParameters) (con *Connection, err error)

Connect tries to connect to a remote CAT_TP server. It is not blocking.

func ConnectWait

func ConnectWait(addr string, lport, rport uint16, id []byte, handler Handler, params ...SocketParameters) (con *Connection, err error)

ConnectWait is a convenient function for a blocking connect.

func NewConnection

func NewConnection(lport, rport uint16) (con *Connection)

NewConnection creates a new empty Connection object.

func (*Connection) Close

func (c *Connection) Close() (err error)

Close gracefully closes a CAT_TP connection.

func (*Connection) Identification

func (c *Connection) Identification() []byte

func (*Connection) Kill

func (con *Connection) Kill()

Kill closes a client connection immediately.

func (*Connection) LocalAddr

func (c *Connection) LocalAddr() net.Addr

func (*Connection) LocalPort

func (c *Connection) LocalPort() uint16

func (*Connection) RCV_CUR_SEQ_NB

func (c *Connection) RCV_CUR_SEQ_NB() uint16

This is the sequence number of the next PDU that is to be sent.

func (*Connection) RCV_INI_SEQ_NB

func (c *Connection) RCV_INI_SEQ_NB() uint16

This is the initial sequence number for the sending activity. This shall be the sequence number that was previously sent in the SYN PDU.

func (*Connection) RCV_OUT_OF_SEQ_PDU_SEQ_NB

func (c *Connection) RCV_OUT_OF_SEQ_PDU_SEQ_NB() uint16

This is the sequence numbers that have been received out of sequence.

func (*Connection) RCV_PDU_SIZE_MAX

func (c *Connection) RCV_PDU_SIZE_MAX() uint16

This is the largest PDU size that may be sent.

func (*Connection) RCV_SDU_SIZE_MAX

func (c *Connection) RCV_SDU_SIZE_MAX() uint16

This is the largest SDU size that may be sent.

func (*Connection) RCV_WIN_SIZE

func (c *Connection) RCV_WIN_SIZE() uint16

This is the number of PDUs that can be received, counting from SND_UNA_PDU_SEQ_NB-1.

func (*Connection) Read

func (c *Connection) Read(b []byte) (co int, err error)

Read reads bytes from a connection. The Buffer handler must have been used to make this working.

func (*Connection) RemoteAddr

func (c *Connection) RemoteAddr() *net.UDPAddr

func (*Connection) RemotePort

func (c *Connection) RemotePort() uint16

func (*Connection) SND_INI_SEQ_NB

func (c *Connection) SND_INI_SEQ_NB() uint16

This is the initial sequence number for the sending activity. This shall be the sequence number that was previously sent in the SYN PDU.

func (*Connection) SND_NXT_SEQ_NB

func (c *Connection) SND_NXT_SEQ_NB() uint16

This is the sequence number of the next PDU that is to be sent.

func (*Connection) SND_PDU_SIZE_MAX

func (c *Connection) SND_PDU_SIZE_MAX() uint16

This is the largest PDU size that may be sent.

func (*Connection) SND_SDU_SIZE_MAX

func (c *Connection) SND_SDU_SIZE_MAX() uint16

This is the largest SDU size that may be sent.

func (*Connection) SND_UNA_PDU_SEQ_NB

func (c *Connection) SND_UNA_PDU_SEQ_NB() uint16

This is the sequence number of the oldest unacknowledged PDU that was sent. If all sent PDUs were acknowledged, it is equal to the sequence number of the next PDU that is to be sent (SND_NXT_SEQ_NB).

func (*Connection) SND_WIN_SIZE

func (c *Connection) SND_WIN_SIZE() uint16

This is the number of PDUs that can be received, counting from SND_UNA_PDU_SEQ_NB-1.

func (*Connection) Send

func (c *Connection) Send(h *Header) (err error)

Send is an exported method to manually send individual packets. It is intended to be used for testing scenarios and debugging only.

func (*Connection) StateS

func (c *Connection) StateS() (ret string)

StateS returns the current state of the connection in human readable form.

func (*Connection) WaitForClose

func (con *Connection) WaitForClose() error

WaitForClose block until the connection is in state CLOSED.

func (*Connection) WaitForConnect

func (con *Connection) WaitForConnect() error

WaitForConnect blocks until the connection is established or the connection failed.

func (*Connection) Write

func (c *Connection) Write(b []byte) (co int, err error)

Write sends a sequence of bytes to the CAT_TP connection.

type ConnectionHandler

type ConnectionHandler func(c *Connection)

ConnectionHandler is the callback type for an incoming connection.

type Dumper

type Dumper interface {
	Dump(data []byte) string
}
var OctetDumper Dumper = HexDumper{}

type Handler

type Handler func(c *Connection, ps []*Header, data []byte)

Handler is the callback type for incoming data (on both sides)

type Header struct {
	// contains filtered or unexported fields
}

func New

func New(flags, version byte, srcport, destport, seq, ack, wsize, datlen uint16) (ret *Header)

func NewACK

func NewACK(p *Header, seqno, wsize uint16) *Header

func NewDataACK

func NewDataACK(version byte, srcport, destport, seqno, ackno, wsize uint16, data []byte) (ret *Header)

func NewHeader

func NewHeader(raw []byte) (ret *Header, err error)

NewHeader creates a new cattp packet based on an existing pcap packet. Error is returned if the pcap packet is not a cattp packet.

func NewNUL

func NewNUL(version byte, srcport, destport, seq, ack, wsize uint16) *Header

func NewRST

func NewRST(version byte, srcport, destport, seq, ack uint16, rc byte) (ret *Header)

func NewSYN

func NewSYN(version byte, srcport, destport, wsize, maxpdusize, maxsdusize uint16, identification []byte) (ret *Header)

func NewSYNACK

func NewSYNACK(syn *Header, wsize, maxpdusize, maxsdusize uint16, ids ...[]byte) (ret *Header)

func (*Header) ACK

func (p *Header) ACK() bool

func (*Header) AckNo

func (p *Header) AckNo() uint16

func (*Header) BinaryString

func (p *Header) BinaryString(mc ...int) string

func (*Header) CheckSum

func (p *Header) CheckSum() uint16

func (*Header) DataLen

func (p *Header) DataLen() uint16

func (*Header) DestPort

func (p *Header) DestPort() uint16

func (*Header) EAK

func (p *Header) EAK() bool

func (*Header) ExtendedAcks

func (p *Header) ExtendedAcks() []uint16

func (*Header) FlagString

func (p *Header) FlagString() string

func (*Header) Flags

func (p *Header) Flags() byte

func (*Header) HashString

func (p *Header) HashString() string

HashString returns a hash over the entire cattp packet (udp payload)

func (*Header) HeaderLen

func (p *Header) HeaderLen() uint8

func (*Header) Identification

func (p *Header) Identification() []byte

func (*Header) IdentificationLen

func (p *Header) IdentificationLen() byte

func (*Header) MaxPDUSize

func (p *Header) MaxPDUSize() uint16

func (*Header) MaxSDUSize

func (p *Header) MaxSDUSize() uint16

func (*Header) NUL

func (p *Header) NUL() bool

func (*Header) NeedsAck

func (p *Header) NeedsAck() bool

func (*Header) Payload

func (p *Header) Payload() []byte

func (*Header) RST

func (p *Header) RST() bool

func (*Header) Raw

func (p *Header) Raw() []byte

func (*Header) ReasonCode

func (p *Header) ReasonCode() byte

func (*Header) ReasonString

func (p *Header) ReasonString() string

ReasonString returns a human readable message for the reason code of the RST packet.

func (*Header) SEG

func (p *Header) SEG() bool

func (*Header) SYN

func (p *Header) SYN() bool

func (*Header) SeqNo

func (p *Header) SeqNo() uint16

func (*Header) SetEAK

func (p *Header) SetEAK(eaks []uint16) error

func (*Header) SrcPort

func (p *Header) SrcPort() uint16

func (*Header) String

func (p *Header) String() string

func (*Header) SupportsEAK

func (p *Header) SupportsEAK() bool

func (*Header) TotalLen

func (p *Header) TotalLen() int

func (*Header) Type

func (p *Header) Type() int

func (*Header) TypeS

func (p *Header) TypeS() (ret string)

func (*Header) TypeString

func (p *Header) TypeString() string

func (*Header) UpdateCheckSum

func (p *Header) UpdateCheckSum()

func (*Header) Version

func (p *Header) Version() byte

func (*Header) WindowSize

func (p *Header) WindowSize() uint16

type HexDumper

type HexDumper struct{}

func (HexDumper) Dump

func (h HexDumper) Dump(data []byte) string

type ParameterKey

type ParameterKey int

type ParameterValue

type ParameterValue interface{}

type ReceiveWindow

type ReceiveWindow struct {
	// contains filtered or unexported fields
}

ReceiveWindow keeps track of the incoming sequence. Out of sequence packets will be stored and only returned in the correct sequence.

func NewReceiveWindow

func NewReceiveWindow(inorder uint16, size uint16) *ReceiveWindow

NewReceiveWindow initializes a new receive window.

func (*ReceiveWindow) Receive

func (s *ReceiveWindow) Receive(h *Header) (ret []*Header, err error)

Receive takes a packet and checks whether it is out of sequence. If not it will be sorted and an array of in-sequence-packets will be returned. This array is empty if no in-sequence packets are available.

func (*ReceiveWindow) Used

func (s *ReceiveWindow) Used() uint16

Used returns the amount of packets that remain in the receive window.

func (*ReceiveWindow) WindowSize

func (s *ReceiveWindow) WindowSize() uint16

WindowSize determines the space that is left in the receive window. It is used to set the window size in each sent packet.

type SendWindow

type SendWindow struct {
	// contains filtered or unexported fields
}

SendWindow keeps track of which sent packets have been acknowledged or not. Acknowledged packets are removed from the window and others could be retransmitted.

func NewSendWindow

func NewSendWindow(last uint16, size uint16) *SendWindow

NewSendWindow initializes a sending window.

func (*SendWindow) Ack

func (c *SendWindow) Ack(h *Header) error

Ack acknowledges an incoming packet. If the packet with the acknowledged sequence number is in the sending window, it will be removed

func (*SendWindow) Add

func (s *SendWindow) Add(h *Header) *sendTimeout

Add adds a packet to the sent window once it was sent.

func (*SendWindow) Get

func (s *SendWindow) Get(sn uint16) *Header

Get returns a packet from the sending window based in its sequence number.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server represents a listen server (UDP) that accepts client connections.

Example
//A simple echo server
server, err := Listen("localhost:8770", 9000,
	func(c *Connection, h []*Header, data []byte) {
		c.Write(data)
	})

if err != nil {
	Log.Fatalf("Could not create listen server: %s", err)
}

client, err := ConnectWait("localhost:8770", 1, 9000, []byte{},
	func(c *Connection, h []*Header, data []byte) {
		fmt.Printf("%s", string(data))
	})

if err != nil {
	Log.Fatalf("Could not connect to server: %s", err)
}

client.Write([]byte("Hello World!"))

time.Sleep(200 * time.Millisecond)

client.Close()
server.CloseWait()
Output:

Hello World!

func Listen

func Listen(as string, lport uint16, handler Handler, params ...SocketParameters) (srv *Server, err error)

Listen starts a CAT_TP server asynchronously.

func (*Server) Close

func (s *Server) Close() (err error)

Close gracefully closes a listen socket.

func (*Server) CloseWait

func (s *Server) CloseWait() error

CloseWait is a convenience method for a blocking Close().

func (*Server) Kill

func (srv *Server) Kill(clients bool)

Kill closes server connection immediately.

func (*Server) SetListener

func (srv *Server) SetListener(lport uint16, handler Handler, params ...SocketParameters) *Server

SetListener adds another CATTP port Listener to an existing UDP server socket. This functon can be used to listen to multiple CATTP ports on a single UDP server socket.

func (*Server) Wait

func (s *Server) Wait() error

Wait blocks until the listen socket is closed. This includes all open client connections.

type SocketParameters

type SocketParameters map[ParameterKey]ParameterValue

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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