Documentation ¶
Index ¶
- Constants
- Variables
- func BufferHandler(c *Connection, ps []*Header, data []byte)
- func EchoHandler(c *Connection, ps []*Header, data []byte)
- func IgnoreHandler(c *Connection, ps []*Header, data []byte)
- func KeepListening(as string, lport uint16, handler Handler, params ...SocketParameters) (err error)
- func LogHandler(c *Connection, ps []*Header, data []byte)
- func NewSeqNo() uint16
- func Nibble(data []byte) []byte
- type Connection
- func (c *Connection) Close() (err error)
- func (c *Connection) Identification() []byte
- func (con *Connection) Kill()
- func (c *Connection) LocalAddr() net.Addr
- func (c *Connection) LocalPort() uint16
- func (c *Connection) RCV_CUR_SEQ_NB() uint16
- func (c *Connection) RCV_INI_SEQ_NB() uint16
- func (c *Connection) RCV_OUT_OF_SEQ_PDU_SEQ_NB() uint16
- func (c *Connection) RCV_PDU_SIZE_MAX() uint16
- func (c *Connection) RCV_SDU_SIZE_MAX() uint16
- func (c *Connection) RCV_WIN_SIZE() uint16
- func (c *Connection) Read(b []byte) (co int, err error)
- func (c *Connection) RemoteAddr() *net.UDPAddr
- func (c *Connection) RemotePort() uint16
- func (c *Connection) SND_INI_SEQ_NB() uint16
- func (c *Connection) SND_NXT_SEQ_NB() uint16
- func (c *Connection) SND_PDU_SIZE_MAX() uint16
- func (c *Connection) SND_SDU_SIZE_MAX() uint16
- func (c *Connection) SND_UNA_PDU_SEQ_NB() uint16
- func (c *Connection) SND_WIN_SIZE() uint16
- func (c *Connection) Send(h *Header) (err error)
- func (c *Connection) StateS() (ret string)
- func (con *Connection) WaitForClose() error
- func (con *Connection) WaitForConnect() error
- func (c *Connection) Write(b []byte) (co int, err error)
- type ConnectionHandler
- type Dumper
- type Handler
- type Header
- func New(flags, version byte, srcport, destport, seq, ack, wsize, datlen uint16) (ret *Header)
- func NewACK(p *Header, seqno, wsize uint16) *Header
- func NewDataACK(version byte, srcport, destport, seqno, ackno, wsize uint16, data []byte) (ret *Header)
- func NewHeader(raw []byte) (ret *Header, err error)
- func NewNUL(version byte, srcport, destport, seq, ack, wsize uint16) *Header
- func NewRST(version byte, srcport, destport, seq, ack uint16, rc byte) (ret *Header)
- func NewSYN(version byte, srcport, destport, wsize, maxpdusize, maxsdusize uint16, ...) (ret *Header)
- func NewSYNACK(syn *Header, wsize, maxpdusize, maxsdusize uint16, ids ...[]byte) (ret *Header)
- func (p *Header) ACK() bool
- func (p *Header) AckNo() uint16
- func (p *Header) BinaryString(mc ...int) string
- func (p *Header) CheckSum() uint16
- func (p *Header) DataLen() uint16
- func (p *Header) DestPort() uint16
- func (p *Header) EAK() bool
- func (p *Header) ExtendedAcks() []uint16
- func (p *Header) FlagString() string
- func (p *Header) Flags() byte
- func (p *Header) HashString() string
- func (p *Header) HeaderLen() uint8
- func (p *Header) Identification() []byte
- func (p *Header) IdentificationLen() byte
- func (p *Header) MaxPDUSize() uint16
- func (p *Header) MaxSDUSize() uint16
- func (p *Header) NUL() bool
- func (p *Header) NeedsAck() bool
- func (p *Header) Payload() []byte
- func (p *Header) RST() bool
- func (p *Header) Raw() []byte
- func (p *Header) ReasonCode() byte
- func (p *Header) ReasonString() string
- func (p *Header) SEG() bool
- func (p *Header) SYN() bool
- func (p *Header) SeqNo() uint16
- func (p *Header) SetEAK(eaks []uint16) error
- func (p *Header) SrcPort() uint16
- func (p *Header) String() string
- func (p *Header) SupportsEAK() bool
- func (p *Header) TotalLen() int
- func (p *Header) Type() int
- func (p *Header) TypeS() (ret string)
- func (p *Header) TypeString() string
- func (p *Header) UpdateCheckSum()
- func (p *Header) Version() byte
- func (p *Header) WindowSize() uint16
- type HexDumper
- type ParameterKey
- type ParameterValue
- type ReceiveWindow
- type SendWindow
- type Server
- type SocketParameters
Examples ¶
Constants ¶
const ( UNK = iota SYN = iota SYNACK = iota ACK = iota RST = iota DATAACK = iota NUL = iota EAK = iota )
const ( FLAG_MASK = 0xFC SYN_FLAG = 0x80 ACK_FLAG = 0x40 EAK_FLAG = 0x20 RST_FLAG = 0x10 NUL_FLAG = 0x08 SEG_FLAG = 0x04 )
const ( BASE_HLEN = 18 SYN_HLEN = 5 )
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 */ )
const ( CLOSE = iota LISTEN = iota SYNRCVD = iota OPEN = iota CLOSEWAIT = iota SYNSENT = iota )
Connection states
const ( CONNECTION_HANDLER = iota RECEIVEWINDOW_SIZE = iota IDENTIFICATION = iota )
Socket parameter
const (
LOGPREFIX = "CATTP"
)
Variables ¶
var CLOSEWAITTimeout time.Duration = 1 * time.Second
var Log *log.Logger
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.
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.
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.
type ConnectionHandler ¶
type ConnectionHandler func(c *Connection)
ConnectionHandler is the callback type for an incoming connection.
type Handler ¶
type Handler func(c *Connection, ps []*Header, data []byte)
Handler is the callback type for incoming data (on both sides)
type Header ¶
type Header struct {
// contains filtered or unexported fields
}
func NewDataACK ¶
func NewHeader ¶
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 (*Header) BinaryString ¶
func (*Header) ExtendedAcks ¶
func (*Header) FlagString ¶
func (*Header) HashString ¶
HashString returns a hash over the entire cattp packet (udp payload)
func (*Header) Identification ¶
func (*Header) IdentificationLen ¶
func (*Header) MaxPDUSize ¶
func (*Header) MaxSDUSize ¶
func (*Header) ReasonCode ¶
func (*Header) ReasonString ¶
ReasonString returns a human readable message for the reason code of the RST packet.
func (*Header) SupportsEAK ¶
func (*Header) TypeString ¶
func (*Header) UpdateCheckSum ¶
func (p *Header) UpdateCheckSum()
func (*Header) WindowSize ¶
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) 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.
type SocketParameters ¶
type SocketParameters map[ParameterKey]ParameterValue