stun

package
v0.0.0-...-2295297 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2017 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	IPv4 = 0x01
	IPv6 = 0x02
)

IP address family

View Source
const (
	ChangeIP   uint64 = 0x04
	ChangePort        = 0x02
)

IP address family

View Source
const (
	KindRequest    uint16 = 0x0000
	KindIndication uint16 = 0x0010
	KindResponse   uint16 = 0x0100
	KindError      uint16 = 0x0110
)
View Source
const (
	EndpointIndependent  = "endpoint-independent"
	AddressDependent     = "address-dependent"
	AddressPortDependent = "address-port-dependent"
)
View Source
const (
	MethodBinding           uint16 = 0x001 // RFC 5389
	MethodSharedSecret      uint16 = 0x002
	MethodAllocate          uint16 = 0x003 // RFC 5766
	MethodRefresh           uint16 = 0x004
	MethodSend              uint16 = 0x006
	MethodData              uint16 = 0x007
	MethodCreatePermission  uint16 = 0x008
	MethodChannelBind       uint16 = 0x009
	MethodConnect           uint16 = 0x00a // RFC 6062
	MethodConnectionBind    uint16 = 0x00b
	MethodConnectionAttempt uint16 = 0x00c
)

STUN methods.

View Source
const (
	AttrMappedAddress              uint16 = 0x0001 // RFC 5389
	AttrChangeRequest              uint16 = 0x0003 // RFC 5780
	AttrUsername                   uint16 = 0x0006 // RFC 5389
	AttrMessageIntegrity           uint16 = 0x0008
	AttrErrorCode                  uint16 = 0x0009
	AttrUnknownAttributes          uint16 = 0x000a
	AttrChannelNumber              uint16 = 0x000c // RFC 5766
	AttrLifetime                   uint16 = 0x000d
	AttrXorPeerAddress             uint16 = 0x0012
	AttrData                       uint16 = 0x0013
	AttrRealm                      uint16 = 0x0014 // RFC 5389
	AttrNonce                      uint16 = 0x0015
	AttrXorRelayedAddress          uint16 = 0x0016 // RFC 5766
	AttrRequestedAddressFamily     uint16 = 0x0017 // RFC 6156
	AttrEvenPort                   uint16 = 0x0018 // RFC 5766
	AttrRequestedTransport         uint16 = 0x0019
	AttrDontFragment               uint16 = 0x001a
	AttrAccessToken                uint16 = 0x001b // RFC 7635
	AttrXorMappedAddress           uint16 = 0x0020 // RFC 5389
	AttrReservationToken           uint16 = 0x0022 // RFC 5766
	AttrPriority                   uint16 = 0x0024 // RFC 5245
	AttrUseCandidate               uint16 = 0x0025
	AttrPadding                    uint16 = 0x0026 // RFC 5780
	AttrResponsePort               uint16 = 0x0027
	AttrConnectionID               uint16 = 0x002a // RFC 6062
	AttrSoftware                   uint16 = 0x8022 // RFC 5389
	AttrAlternateServer            uint16 = 0x8023
	AttrTransactionTransmitCounter uint16 = 0x8025 // RFC 7982
	AttrCacheTimeout               uint16 = 0x8027 // RFC 5780
	AttrFingerprint                uint16 = 0x8028 // RFC 5389
	AttrIceControlled              uint16 = 0x8029 // RFC 5245
	AttrIceControlling             uint16 = 0x802a
	AttrResponseOrigin             uint16 = 0x802b // RFC 5780
	AttrOtherAddress               uint16 = 0x802c
	AttrEcnCheck                   uint16 = 0x802d // RFC 6679
	AttrThirdPartyAuthorization    uint16 = 0x802e // RFC 7635
	AttrMobilityTicket             uint16 = 0x8030 // RFC 8016
)

STUN attributes.

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

Deprecated: For backwards compatibility only.

View Source
const (
	CodeTryAlternate                 int = 300 // RFC 5389
	CodeBadRequest                   int = 400
	CodeUnauthorized                 int = 401
	CodeForbidden                    int = 403 // RFC 5766
	CodeMobilityForbidden            int = 405 // RFC 8016
	CodeUnknownAttribute             int = 420 // RFC 5389
	CodeAllocationMismatch           int = 437 // RFC 5766
	CodeStaleNonce                   int = 438 // RFC 5389
	CodeAddressFamilyNotSupported    int = 440 // RFC 6156
	CodeWrongCredentials             int = 441 // RFC 5766
	CodeUnsupportedTransportProtocol int = 442
	CodePeerAddressFamilyMismatch    int = 443 // RFC 6156
	CodeConnectionAlreadyExists      int = 446 // RFC 6062
	CodeConnectionTimeoutOrFailure   int = 447
	CodeAllocationQuotaReached       int = 486 // RFC 5766
	CodeRoleConflict                 int = 487 // RFC 5245
	CodeServerError                  int = 500 // RFC 5389
	CodeInsufficientCapacity         int = 508 // RFC 5766
)

STUN error codes.

Variables

View Source
var DefaultConfig = &Config{
	RetransmissionTimeout: 500 * time.Millisecond,
	TransactionTimeout:    39500 * time.Millisecond,
	Software:              "pixelbender/go-stun",
}

Functions

func AttrName

func AttrName(typ uint16) string

func Discover

func Discover(uri string) (net.PacketConn, net.Addr, error)

func ErrorText

func ErrorText(code int) string

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

func ListenAndServe

func ListenAndServe(network, laddr string, config *Config) error

func LocalAddrs

func LocalAddrs() []*net.IPAddr

func MethodName

func MethodName(typ uint16) string

func NewAddr

func NewAddr(network string, ip net.IP, port int) net.Addr

func NewTransaction

func NewTransaction() []byte

func SockAddr

func SockAddr(v net.Addr) (net.IP, int)

Types

type Agent

type Agent struct {
	Handler Handler
	// contains filtered or unexported fields
}

func NewAgent

func NewAgent(config *Config) *Agent

func (*Agent) RoundTrip

func (a *Agent) RoundTrip(req *Message, to Transport) (res *Message, from Transport, err error)

func (*Agent) Send

func (a *Agent) Send(msg *Message, tr Transport) (err error)

func (*Agent) ServeConn

func (a *Agent) ServeConn(c net.Conn) error

func (*Agent) ServePacket

func (a *Agent) ServePacket(c net.PacketConn) error

func (*Agent) ServeSTUN

func (a *Agent) ServeSTUN(msg *Message, tr Transport)

func (*Agent) ServeTransport

func (a *Agent) ServeTransport(b []byte, tr Transport) (n int, err error)

type Attr

type Attr interface {
	Type() uint16
	Marshal(p []byte) []byte
	Unmarshal(b []byte) error
}

Attribute represents a STUN attribute.

var Fingerprint Attr = &fingerprint{}

func Addr

func Addr(typ uint16, v net.Addr) Attr

func Bytes

func Bytes(typ uint16, v []byte) Attr

func Flag

func Flag(v uint16) Attr

func IP

func IP(typ uint16, ip net.IP) Attr

func Int

func Int(typ uint16, v uint64) Attr

func MessageIntegrity

func MessageIntegrity(key []byte) Attr

func String

func String(typ uint16, v string) Attr

type AuthMethod

type AuthMethod func(sess *Session) error

func LongTermAuthMethod

func LongTermAuthMethod(username, password string) AuthMethod

LongTermAuthMethod returns AuthMethod for long-term credentials. Key = MD5(username ":" realm ":" SASLprep(password)). SASLprep is defined in RFC 4013.

func ShortTermAuthMethod

func ShortTermAuthMethod(password string) AuthMethod

ShotTermAuthMethod returns AuthMethod for short-term credentials. Key = SASLprep(password). SASLprep is defined in RFC 4013.

type Config

type Config struct {
	// AuthMethod returns a key for MESSAGE-INTEGRITY attribute
	AuthMethod AuthMethod
	// Retransmission timeout, default is 500 milliseconds
	RetransmissionTimeout time.Duration
	// Transaction timeout, default is 39.5 seconds
	TransactionTimeout time.Duration
	// Fingerprint, if true all outgoing messages contain FINGERPRINT attribute
	Fingerprint bool
	// Software is a SOFTWARE attribute value for outgoing messages, if not empty
	Software string
	// Logf, if set all sent and received messages printed using Logf
	Logf func(format string, args ...interface{})
}

func (*Config) Clone

func (c *Config) Clone() *Config

type Conn

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

func Dial

func Dial(uri string, config *Config) (*Conn, error)

func NewConn

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

func (*Conn) Discover

func (c *Conn) Discover() (net.Addr, error)

func (*Conn) Network

func (c *Conn) Network() string

func (*Conn) Request

func (c *Conn) Request(req *Message) (res *Message, err error)

func (*Conn) RequestTransport

func (c *Conn) RequestTransport(req *Message, to Transport) (res *Message, from Transport, err error)

type Detector

type Detector struct {
	*Conn
}

func NewDetector

func NewDetector(c *Conn) *Detector

func (*Detector) DiscoverChange

func (d *Detector) DiscoverChange(change uint64) error

func (*Detector) DiscoverOther

func (d *Detector) DiscoverOther(addr net.Addr) (net.Addr, error)

func (*Detector) Filtering

func (d *Detector) Filtering() (string, error)

func (*Detector) Hairpinning

func (d *Detector) Hairpinning() error

func (*Detector) Mapping

func (d *Detector) Mapping() (string, error)

type Error

type Error struct {
	Code   int
	Reason string
}

Error represents the ERROR-CODE attribute.

func NewError

func NewError(code int) *Error

func (*Error) Error

func (e *Error) Error() string

func (*Error) Marshal

func (e *Error) Marshal(p []byte) []byte

func (*Error) String

func (e *Error) String() string

func (*Error) Type

func (*Error) Type() uint16

func (*Error) Unmarshal

func (e *Error) Unmarshal(b []byte) error

type Handler

type Handler interface {
	ServeSTUN(msg *Message, tr Transport)
}

type HandlerFunc

type HandlerFunc func(msg *Message, tr Transport)

func (HandlerFunc) ServeSTUN

func (h HandlerFunc) ServeSTUN(msg *Message, tr Transport)

type Listener

type Listener interface {
	Addr() net.Addr
	Close() error
}

type Marshaler

type Marshaler interface {
	Marshal(b []byte) []byte
}

type Message

type Message struct {
	Type        uint16
	Transaction []byte
	Attributes  []Attr
}

Message represents a STUN message.

func UnmarshalMessage

func UnmarshalMessage(b []byte) (*Message, error)

func (*Message) Add

func (m *Message) Add(attr Attr)

func (*Message) CheckFingerprint

func (m *Message) CheckFingerprint() bool

func (*Message) CheckIntegrity

func (m *Message) CheckIntegrity(key []byte) bool

func (*Message) Del

func (m *Message) Del(typ uint16)

func (*Message) Get

func (m *Message) Get(typ uint16) (attr Attr)

func (*Message) GetAddr

func (m *Message) GetAddr(network string, typ ...uint16) net.Addr

func (*Message) GetBytes

func (m *Message) GetBytes(typ uint16) []byte

func (*Message) GetError

func (m *Message) GetError() *Error

func (*Message) GetInt

func (m *Message) GetInt(typ uint16) (v uint64, ok bool)

func (*Message) GetString

func (m *Message) GetString(typ uint16) string

func (*Message) Has

func (m *Message) Has(typ uint16) bool

func (*Message) Kind

func (m *Message) Kind() uint16

func (*Message) Marshal

func (m *Message) Marshal(p []byte) []byte

func (*Message) Method

func (m *Message) Method() uint16

func (*Message) Set

func (m *Message) Set(attr Attr)

func (*Message) String

func (m *Message) String() string

func (*Message) Unmarshal

func (m *Message) Unmarshal(b []byte) (n int, err error)

type Server

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

func NewServer

func NewServer(config *Config) *Server

func (*Server) Close

func (srv *Server) Close() error

func (*Server) ListenAndServe

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

func (*Server) ServeSTUN

func (srv *Server) ServeSTUN(msg *Message, from Transport)

type Session

type Session struct {
	Realm    string
	Nonce    string
	Username string
	Key      []byte
}

type Transport

type Transport interface {
	LocalAddr() net.Addr
	RemoteAddr() net.Addr
	Write(p []byte) (int, error)
	Close() error
}

type TransportHandler

type TransportHandler interface {
	ServeTransport(b []byte, tr Transport) (int, error)
}

Jump to

Keyboard shortcuts

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