fmtp

package module
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2019 License: Unlicense Imports: 15 Imported by: 0

README

Package fmtp implements support for Flight Message Transfer Protocol v2.0

Usage

Create a client

client,_ := fmtp.NewClient("my id")

Connect & associate with a remote endpoint

conn, _ := client.Dial("address","id")

Send a message

conn.SendOperatorString("hello there")

TODO

  • Test server-side (handlers,etc.)
  • More tests
  • Better logging
  • More callbacks

Documentation

Overview

Package fmtp provides Flight Message Transfer Protocol (FMTP) support. It currently supports v2.0

Note that the FMTP protocol is a layer 5,6,7 protocol in the OSI stack.

Index

Constants

View Source
const (
	DefaultTi = 12 * time.Second
	DefaultTs = 60 * time.Second
	DefaultTr = 120 * time.Second
)

These are the default timer durations

View Source
const (

	// MaxBodyLen is the maximum body len in bytes
	MaxBodyLen = maxLength - headerLen // 65530 is the max body length

	// CompatBodyLen is the minimum size that shall be accepted by FMTP implementations
	CompatBodyLen = 10240
)
View Source
const ExpectedPrefix = "2001:4b50::/32"

ExpectedPrefix is the prefix of FMTP systems from which FMTP systems IPv6 addresses are derived

View Source
const ListeningPort = "8500"

ListeningPort is the port a server should listen to. It is not forced to use it, but it is highly recommended.

Variables

View Source
var (
	// ErrConnectionDeadlineExceeded is returned when the connection deadline (Ti) is exceeded
	ErrConnectionDeadlineExceeded = errors.New("connection deadline exceeded")

	// ErrConnectionRejectedByRemote is returned when the connection has been rejected by the remote party
	ErrConnectionRejectedByRemote = errors.New("connection rejected by remote party")

	// ErrConnectionRejectedByLocal is returned when the connection has been rejected by the local party
	ErrConnectionRejectedByLocal = errors.New("connection rejected for invalid credentials")
)
View Source
var (
	// ErrAssociationTimeoutExceeded happens when the association reception timeout (tr) is exceeded
	// It is returned when associating. Once associated, such error will shutdown the association.
	ErrAssociationTimeoutExceeded = errors.New("association reception timeout exceeded")
)

Functions

This section is empty.

Types

type Client

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

Client is what allows you to do FMTP requests.

func NewClient

func NewClient(id ID, setters ...ClientSetter) (*Client, error)

NewClient creates a new FMTP client

func (*Client) Connect

func (c *Client) Connect(ctx context.Context, address string, id ID) (*Conn, error)

Connect initiates an FMTP Connection. It is a wrapper around (*Client).NewConn(nil) and NewConn.Init(ctx, address, id) If the given context expires before the connection is complete, an error is returned. But once successfully established, the context has no effect.

func (*Client) Dial

func (c *Client) Dial(ctx context.Context, address string, id ID) (*Conn, error)

Dial Connects and Associates with a remote FMTP responder

FMTP dialing has two steps: first connect, then associate.

func (*Client) NewConn

func (c *Client) NewConn(h Handler) *Conn

NewConn creates a new connection

func (*Client) NewServer

func (c *Client) NewServer(addr string, h Handler) *Server

NewServer creates a server for use

type ClientSetter

type ClientSetter func(c *Client) error

ClientSetter is a client configuration setter

func SetDialer

func SetDialer(dialer *net.Dialer) ClientSetter

SetDialer sets a client's dialer

func SetTimers

func SetTimers(ti, ts, tr time.Duration) ClientSetter

SetTimers sets the timers

ti is the connection timer, it is only used when establishing connections
ts is the ...
tr is the ...

type Conn

type Conn struct {

	// ti is the maximum period of time in which data must be received during an FMTP connection attempt in order for it to be successful
	Ti time.Duration

	// ts is the maximum period of time in which data must be transmitted in order to maintain an FMTP association
	Ts time.Duration

	// tr is the maximum period of time in which data is to be received over an FMTP association
	Tr time.Duration

	// handler is the user's handler for OPERATOR and OPERATIONAL messages
	Handler Handler

	// ShutdownNotify notifies the user that a shutdown has been initiated
	ShutdownNotify func()
	// contains filtered or unexported fields
}

Conn holds the connection with an endpoint

func (*Conn) Associate

func (conn *Conn) Associate(ctx context.Context) error

Associate upgrades an FMTP Connection to an association If the given context expires before the connection is complete, an error is returned. But once successfully established, the context has no effect.

func (*Conn) Close

func (conn *Conn) Close() error

Close closes the association & connection without any grace

func (*Conn) Deassociate

func (conn *Conn) Deassociate(ctx context.Context) error

Deassociate de-associates gracefully

func (*Conn) Disconnect

func (conn *Conn) Disconnect(ctx context.Context) error

Disconnect disconnects a connection, gracefully

func (*Conn) Init

func (conn *Conn) Init(ctx context.Context, addr string, remote ID) error

Init initialises a connection

func (*Conn) RemoteAddr

func (conn *Conn) RemoteAddr() net.Addr

RemoteAddr returns the remote address behind a connection, if there is one

func (*Conn) RemoteID

func (conn *Conn) RemoteID() ID

RemoteID returns the ID of the connection's remote party, empty ID if not currently set

func (*Conn) Send

func (conn *Conn) Send(ctx context.Context, msg *Message) error

Send sends a message over a connection, making the agent associate it if needed.

func (*Conn) SetAcceptRemote

func (conn *Conn) SetAcceptRemote(f func(ID) bool) error

SetAcceptRemote sets the function that accepts remote IDs for incoming connections

func (*Conn) SetHandler

func (conn *Conn) SetHandler(h Handler)

SetHandler sets the handler for the incomming messages in a transmission

func (*Conn) SetTimers

func (conn *Conn) SetTimers(ti, tr, ts time.Duration)

SetTimers sets the connection timers

func (*Conn) SetUnderlying

func (conn *Conn) SetUnderlying(rwc io.ReadWriteCloser) error

SetUnderlying sets the underlying connection. The protocol requires TCP connection. However, for debugging, tunneling or other usecases, it can be beneficial to set a custom one. Note that in order for Remote Address reporting to work, it is best if the given io.ReadWriteCloser also has a RemoteAddr() net.Addr method !

func (*Conn) Write

func (conn *Conn) Write(b []byte) error

Write creates an operator message and sends it It allows you to treat a connection as a io.Writer For more options, use Send.

type Handler

type Handler interface {
	ServeFMTP(conn *Conn, msg *Message)
}

A Handler receives and processes an FMTP message.

type HandlerFunc

type HandlerFunc func(conn *Conn, msg *Message)

The HandlerFunc type is an adapter to allow the use of ordinary functions as FMTP handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler that calls f.

func (HandlerFunc) ServeFMTP

func (hf HandlerFunc) ServeFMTP(conn *Conn, msg *Message)

ServeFMTP satisfies the Handler interface

type ID

type ID string

An ID (Identification Value) is of maximum 32 Byte length

func (*ID) Check

func (id *ID) Check() error

Check checks an ID validity

type Message

type Message struct {
	Body io.ReadCloser
	// contains filtered or unexported fields
}

A Message is an FMTP message

func NewMessage

func NewMessage(typ Typ, r io.Reader) (*Message, error)

NewMessage returns a message of either Operational or Operator type See MaxBodyLen for the maximum size of a message's body

func NewOperationalMessage

func NewOperationalMessage(r io.Reader) (*Message, error)

NewOperationalMessage returns a message of Operational type

func NewOperatorMessage

func NewOperatorMessage(r io.Reader) (*Message, error)

NewOperatorMessage returns a message of Operator type

func NewOperatorMessageString

func NewOperatorMessageString(txt string) (*Message, error)

NewOperatorMessageString returns a message of Operator type built from the given string

func (*Message) ReadFrom

func (msg *Message) ReadFrom(r io.Reader) (int64, error)

ReadFrom creates a m.Message from an io.Reader.

func (*Message) Typ

func (msg *Message) Typ() Typ

Typ returns the message's type

func (*Message) WriteTo

func (msg *Message) WriteTo(w io.Writer) (int64, error)

WriteTo writes a Message to the given io.Writer. This consumes the Message Body.

type Server

type Server struct {
	// TCP address to listen on
	Addr string

	// Handler is the handler for new connections.
	Handler Handler

	// Timeouts
	Ti time.Duration
	Ts time.Duration
	Tr time.Duration

	// AcceptTCP is called when a new TCP connection is inbound
	// If AcceptTCP is nil, every incoming connections are accepted
	AcceptTCP func(remoteAddr net.Addr) bool

	// NotifyConn is called when a connection was successfuly established
	NotifyConn func(remoteAddr net.Addr, remoteID ID)
	// contains filtered or unexported fields
}

A Server defines parameters for running an FMTP server.

func (*Server) Close

func (srv *Server) Close() error

Close stops the server

func (*Server) ListenAndServe

func (srv *Server) ListenAndServe() error

ListenAndServe listens to an IP Address, and handles functions

func (*Server) Serve

func (srv *Server) Serve(l *net.TCPListener) error

Serve serves incoming connections on a net.Listener

func (*Server) Shutdown

func (srv *Server) Shutdown(ctx context.Context) error

Shutdown stops the server gracefully

type Typ

type Typ uint8
const (
	Operational Typ
	Operator
)

The following constants define the type of the message being carried

func (Typ) String

func (t Typ) String() string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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