stun

package
v0.0.0-...-8728e54 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2022 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AttrMappedAddress     = uint16(0x0001)
	AttrXorMappedAddress  = uint16(0x0020)
	AttrUsername          = uint16(0x0006)
	AttrMessageIntegrity  = uint16(0x0008)
	AttrErrorCode         = uint16(0x0009)
	AttrRealm             = uint16(0x0014)
	AttrNonce             = uint16(0x0015)
	AttrUnknownAttributes = uint16(0x000a)
	AttrSoftware          = uint16(0x8022)
	AttrAlternateServer   = uint16(0x8023)
	AttrFingerprint       = uint16(0x8028)
)

Attributes introduced by the RFC 5389 Section 18.2.

View Source
const (
	AttrChangeRequest  = uint16(0x0003)
	AttrPadding        = uint16(0x0026)
	AttrResponsePort   = uint16(0x0027)
	AttrResponseOrigin = uint16(0x802b)
	AttrOtherAddress   = uint16(0x802c)
)

Attributes introduced by the RFC 5780 Section 7.

View Source
const (
	AttrResponseAddress = uint16(0x0002)
	AttrSourceAddress   = uint16(0x0004)
	AttrChangedAddress  = uint16(0x0005)
	AttrPassword        = uint16(0x0007)
	AttrReflectedFrom   = uint16(0x000b)
)

Attributes introduced by the RFC 3489 Section 11.2 except listed above.

View Source
const (
	ChangeIP   = uint32(0x4)
	ChangePort = uint32(0x2)
)

Bits definition of CHANGE-REQUEST attribute.

View Source
const (
	CodeTryAlternate     = 300
	CodeBadRequest       = 400
	CodeUnauthorized     = 401
	CodeUnknownAttribute = 420
	CodeStaleNonce       = 438
	CodeServerError      = 500
)

Error codes introduced by the RFC 5389 Section 15.6

View Source
const (
	CodeStaleCredentials      = 430
	CodeIntegrityCheckFailure = 431
	CodeMissingUsername       = 432
	CodeUseTLS                = 433
	CodeGlobalFailure         = 600
)

Error codes introduced by the RFC 3489 Section 11.2.9 except listed in RFC 5389.

View Source
const (
	TypeRequest    = uint16(0x0000)
	TypeIndication = uint16(0x0010)
	TypeResponse   = uint16(0x0100)
	TypeError      = uint16(0x0110)
)

Types of a STUN message.

View Source
const AddrCodec = addrCodec(false)

AddrCodec is the codec for a transport address attribute.

View Source
const MethodBinding = uint16(0x0001)

MethodBinding is the STUN binding request method.

View Source
const RawCodec = rawCodec(false)

RawCodec encodes and decodes a STUN attribute as a raw byte array.

View Source
const StringCodec = rawCodec(true)

StringCodec encodes and decodes a STUN attribute as a string.

View Source
const XorAddrCodec = addrCodec(true)

XorAddrCodec is the codec for a XOR-obfuscated transport address attribute.

Variables

View Source
var DefaultConfig = &Config{
	GetAttributeCodec: GetAttributeCodec,
}
View Source
var ErrFormat = errors.New("stun: incorrect format")

ErrFormat is returned by Decode when a buffer is not a valid STUN message.

View Source
var ErrIncorrectFingerprint = errors.New("stun: incorrect fingerprint")

ErrIncorrectFingerprint is returned by Decode when a STUN message contains a FINGERPRINT attribute and it does not equal to checksum.

View Source
var ErrIntegrityCheckFailure = errors.New("stun: integrity check failure")

ErrIntegrityCheckFailure is returned by Decode when a STUN message contains a MESSAGE-INTEGRITY attribute and it does not equal to HMAC-SHA1 sum.

View Source
var ErrNoAddressResponse = errors.New("stun: no mapped address")
View Source
var ErrTruncated = errors.New("stun: truncated")

ErrFormat is returned by ReadMessage when a STUN message was truncated.

View Source
var ErrUnauthorized = errors.New("stun: unauthorized")

ErrUnauthorized is returned by Decode or RoundTrip when GetAuthKey is defined but a STUN request does not contain a MESSAGE-INTEGRITY attribute.

View Source
var ErrUnsupportedScheme = errors.New("stun: unsupported scheme")

Functions

func ErrorText

func ErrorText(code int) string

ErrorText returns a reason phrase text for the STUN error code. It returns the empty string if the code is unknown.

func GetAttributeName

func GetAttributeName(at uint16) (n string)

GetAttributeName returns a STUN attribute name. It returns the empty string if the attribute is unknown.

func GetServerAddress

func GetServerAddress(h string, secure bool) string

func ListenAndServe

func ListenAndServe(network, addr string, handler Handler) error

ListenAndServe listens on the network address and calls handler to serve requests.

func ListenAndServeTLS

func ListenAndServeTLS(network, addr string, certFile, keyFile string, handler Handler) error

ListenAndServeTLS listens on the network address secured by TLS and calls handler to serve requests.

func LongTermAuthKey

func LongTermAuthKey(username, password string) func(attrs Attributes) ([]byte, error)

Types

type Addr

type Addr struct {
	IP   net.IP
	Port int
}

Addr represents a transport address attribute.

func Lookup

func Lookup(uri, username, password string) (*Addr, error)

Lookup connects to the given STUN URI and makes the STUN binding request. Returns the server reflexive transport address (mapped address).

func (*Addr) String

func (addr *Addr) String() string

String returns the "host:port" form of the transport address.

type AttrCodec

type AttrCodec interface {
	Encode(w Writer, v interface{}) error
	Decode(r Reader) (interface{}, error)
}

AttrCodec interface represents a STUN attribute encoder/decoder.

func GetAttributeCodec

func GetAttributeCodec(at uint16) AttrCodec

GetAttributeCodec returns a STUN attribute codec for TURN. It returns the nil if the attribute type is unknown.

type Attributes

type Attributes map[uint16]interface{}

Attributes represents a set of STUN attributes.

func (Attributes) Has

func (at Attributes) Has(id uint16) (ok bool)

func (Attributes) String

func (at Attributes) String(id uint16) string

type Client

type Client struct {
	*Conn
	Timeout time.Duration
}

Client is a STUN client.

func NewClient

func NewClient(c net.Conn, config *Config) *Client

func (*Client) RoundTrip

func (c *Client) RoundTrip(req *Message) (res *Message, err error)

RoundTrip sends STUN request and waits for a STUN response within the transaction. Retransmits on unrelied over transport protocol connection. Tries to authorize No STUN redirect is supported. Returns STUN error responses as errors.

type Config

type Config struct {
	// GetAuthKey returns a key for a MESSAGE-INTEGRITY attribute generation and validation.
	// Key = MD5(username ":" realm ":" SASLprep(password)) for long-term credentials.
	// Key = SASLprep(password) for short-term credentials.
	// SASLprep is defined in RFC 4013.
	// The Username and Password fields are ignored if GetAuthKey is defined.
	GetAuthKey func(attrs Attributes) ([]byte, error)

	// GetAttributeCodec returns STUN attribute codec for the specified attribute type.
	// Using stun.GetAttributeCodec if GetAttributeCodec is nil.
	GetAttributeCodec func(at uint16) AttrCodec

	// Fingerprint controls whether a FINGERPRINT attribute will be generated.
	Fingerprint bool

	// Software is a value for SOFTWARE attribute.
	Software string
}

Config represents a STUN connection configuration.

type Conn

type Conn struct {
	net.Conn
	// contains filtered or unexported fields
}

A Conn represents the STUN connection and implements the STUN protocol over net.Conn interface.

func NewConn

func NewConn(inner net.Conn, config *Config) *Conn

NewConn creates a Conn connection over the c with specified configuration.

func (*Conn) ReadMessage

func (c *Conn) ReadMessage() (*Message, error)

ReadMessage reads STUN messages from the connection.

func (*Conn) WriteMessage

func (c *Conn) WriteMessage(msg *Message) error

WriteMessage writes the STUN message to the connection.

type Decoder

type Decoder struct {
	*Config
	// contains filtered or unexported fields
}

A Decoder reads and decodes STUN messages from a buffer.

func NewDecoder

func NewDecoder(config *Config) *Decoder

func (*Decoder) Decode

func (dec *Decoder) Decode(b []byte, key []byte) (*Message, error)

Decode reads STUN message from the buffer. Checks MESSAGE-INTEGRITY attribute if GetAuthKey is specified. Checks FINGERPRINT attribute if present. Returns io.EOF if the buffer size is not enough. Returns ErrUnknownAttrs containing unknown comprehension-required STUN attributes.

type Encoder

type Encoder struct {
	*Config
	// contains filtered or unexported fields
}

An Encoder writes STUN message to a buffer.

func NewEncoder

func NewEncoder(config *Config) *Encoder

func (*Encoder) Encode

func (enc *Encoder) Encode(m *Message) ([]byte, error)

Encode writes STUN message to the buffer. Generates MESSAGE-INTEGRITY attribute if Key is specified. Appends FINGERPRINT attribute if Fingerprint is true.

type ErrUnknownAttrs

type ErrUnknownAttrs struct {
	Attributes []uint16
}

ErrUnknownAttrs is returned when a STUN message contains unknown comprehension-required attributes.

func (ErrUnknownAttrs) Error

func (e ErrUnknownAttrs) Error() string

type Error

type Error struct {
	Code   int
	Reason string
}

Error represents the ERROR-CODE attribute.

func NewError

func NewError(code int) *Error

NewError returns Error with code and default reason phrase.

type Handler

type Handler interface {
	ServeSTUN(rw ResponseWriter, r *Message)
}

A Handler handles a STUN message.

type HandlerFunc

type HandlerFunc func(rw ResponseWriter, r *Message)

The HandlerFunc type is an adapter to allow the use of ordinary functions as STUN handlers.

func (HandlerFunc) ServeSTUN

func (f HandlerFunc) ServeSTUN(rw ResponseWriter, r *Message)

ServeSTUN calls f(rw, r).

type Message

type Message struct {
	Method      uint16
	Transaction []byte
	Attributes  Attributes
	Key         []byte
}

Message represents a STUN message.

func (*Message) IsType

func (m *Message) IsType(t uint16) bool

IsType checks if the STUN message corresponds the specified type.

type Reader

type Reader interface {
	// Next returns a slice of the next n bytes.
	Next(n int) ([]byte, error)
	// Available returns number of available unread bytes.
	Available() int
}

type ResponseWriter

type ResponseWriter interface {
	LocalAddr() net.Addr
	RemoteAddr() net.Addr
	// WriteResponse writes STUN response within the transaction.
	WriteMessage(msg *Message) error
}

type Server

type Server struct {
	*Config
	Realm   string
	Handler Handler
}

Server represents a STUN server.

func NewServer

func NewServer(config *Config) *Server

func (*Server) ListenAndServe

func (srv *Server) ListenAndServe(network, addr string) error

ListenAndServe listens on the network address and calls handler to serve requests. Accepted connections are configured to enable TCP keep-alives.

func (*Server) ListenAndServeTLS

func (srv *Server) ListenAndServeTLS(network, addr, certFile, keyFile string) error

ListenAndServeTLS listens on the network address secured by TLS and calls handler to serve requests. Accepted connections are configured to enable TCP keep-alives.

func (*Server) Serve

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

Serve accepts incoming connection on the listener and calls handler to serve STUN requests. Multiple goroutines may invoke Serve on the same Listener simultaneously.

func (*Server) ServePacket

func (srv *Server) ServePacket(l net.PacketConn) error

ServePacket receives incoming packets on the packet-oriented network listener and calls handler to serve STUN requests. Multiple goroutines may invoke ServePacket on the same PacketConn simultaneously.

func (*Server) ServeSTUN

func (srv *Server) ServeSTUN(rw ResponseWriter, r *Message)

ServeSTUN responds to the simple STUN binding request.

type Writer

type Writer interface {
	// Next returns a slice of the next n bytes.
	Next(n int) []byte
}

Jump to

Keyboard shortcuts

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