Documentation ¶
Index ¶
- Constants
- Variables
- func ErrorText(code int) string
- func GetAttributeName(at uint16) (n string)
- func GetServerAddress(h string, secure bool) string
- func ListenAndServe(network, addr string, handler Handler) error
- func ListenAndServeTLS(network, addr string, certFile, keyFile string, handler Handler) error
- func LongTermAuthKey(username, password string) func(attrs Attributes) ([]byte, error)
- type Addr
- type AttrCodec
- type Attributes
- type Client
- type Config
- type Conn
- type Decoder
- type Encoder
- type ErrUnknownAttrs
- type Error
- type Handler
- type HandlerFunc
- type Message
- type Reader
- type ResponseWriter
- type Server
- func (srv *Server) ListenAndServe(network, addr string) error
- func (srv *Server) ListenAndServeTLS(network, addr, certFile, keyFile string) error
- func (srv *Server) Serve(l net.Listener) error
- func (srv *Server) ServePacket(l net.PacketConn) error
- func (srv *Server) ServeSTUN(rw ResponseWriter, r *Message)
- type Writer
Constants ¶
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.
const ( AttrChangeRequest = uint16(0x0003) AttrPadding = uint16(0x0026) AttrResponsePort = uint16(0x0027) AttrResponseOrigin = uint16(0x802b) AttrOtherAddress = uint16(0x802c) )
Attributes introduced by the RFC 5780 Section 7.
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.
const ( ChangeIP = uint32(0x4) ChangePort = uint32(0x2) )
Bits definition of CHANGE-REQUEST attribute.
const ( CodeTryAlternate = 300 CodeBadRequest = 400 CodeUnknownAttribute = 420 CodeStaleNonce = 438 CodeServerError = 500 )
Error codes introduced by the RFC 5389 Section 15.6
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.
const ( TypeRequest = uint16(0x0000) TypeIndication = uint16(0x0010) TypeResponse = uint16(0x0100) TypeError = uint16(0x0110) )
Types of a STUN message.
const AddrCodec = addrCodec(false)
AddrCodec is the codec for a transport address attribute.
const MethodBinding = uint16(0x0001)
MethodBinding is the STUN binding request method.
const RawCodec = rawCodec(false)
RawCodec encodes and decodes a STUN attribute as a raw byte array.
const StringCodec = rawCodec(true)
StringCodec encodes and decodes a STUN attribute as a string.
const XorAddrCodec = addrCodec(true)
XorAddrCodec is the codec for a XOR-obfuscated transport address attribute.
Variables ¶
var DefaultConfig = &Config{ GetAttributeCodec: GetAttributeCodec, }
var ErrFormat = errors.New("stun: incorrect format")
ErrFormat is returned by Decode when a buffer is not a valid STUN message.
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.
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.
var ErrNoAddressResponse = errors.New("stun: no mapped address")
var ErrTruncated = errors.New("stun: truncated")
ErrFormat is returned by ReadMessage when a STUN message was truncated.
ErrUnauthorized is returned by Decode or RoundTrip when GetAuthKey is defined but a STUN request does not contain a MESSAGE-INTEGRITY attribute.
var ErrUnsupportedScheme = errors.New("stun: unsupported scheme")
Functions ¶
func ErrorText ¶
ErrorText returns a reason phrase text for the STUN error code. It returns the empty string if the code is unknown.
func GetAttributeName ¶
GetAttributeName returns a STUN attribute name. It returns the empty string if the attribute is unknown.
func GetServerAddress ¶
func ListenAndServe ¶
ListenAndServe listens on the network address and calls handler to serve requests.
func ListenAndServeTLS ¶
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 ¶
Addr represents a transport address attribute.
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 ¶
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 ¶
Client is a STUN client.
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 ¶
A Conn represents the STUN connection and implements the STUN protocol over net.Conn interface.
func (*Conn) ReadMessage ¶
ReadMessage reads STUN messages from the connection.
func (*Conn) WriteMessage ¶
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 (*Decoder) Decode ¶
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 ¶
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 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.
type ResponseWriter ¶
type Server ¶
Server represents a STUN server.
func (*Server) ListenAndServe ¶
ListenAndServe listens on the network address and calls handler to serve requests. Accepted connections are configured to enable TCP keep-alives.
func (*Server) ListenAndServeTLS ¶
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 ¶
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.