Documentation ¶
Index ¶
- Constants
- Variables
- func BuildAndSend(conn net.PacketConn, addr *TransportAddr, class MessageClass, method Method, ...) error
- func GenerateTransactionID() []byte
- func IsSTUN(packet []byte) bool
- func MessageIntegrityCalculateHMAC(key, message []byte) ([]byte, error)
- type AttrType
- type Attribute
- type ChannelData
- type ChannelNumber
- type Client
- type Data
- type ErrorCode
- type EvenPort
- type Fingerprint
- type IceControlled
- type IceControlling
- type Lifetime
- type Message
- type MessageClass
- type MessageIntegrity
- type Method
- type Nonce
- type PacketType
- type Priority
- type RawAttribute
- type Realm
- type RequestedTransport
- type ReservationToken
- type Software
- type TransportAddr
- type UnknownAttributes
- type UseCandidate
- type Username
- type XorAddress
- type XorMappedAddress
- type XorPeerAddress
- type XorRelayedAddress
Constants ¶
const (
ProtocolUDP protocolNumber = 0x11
)
ProtocolUDP User Datagram https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
const (
// TransactionIDSize is the size of the transaction according to RFC 5389: 96 bits
TransactionIDSize = 96 / 8
)
Variables ¶
var ( // Err300TryAlternate a ErrorCode value - TryAlernate Err300TryAlternate = ErrorCode{3, 0, []byte("Try Alternate: The client should contact an alternate server for this request.")} // Err400BadRequest a ErrorCode value - BadRequest Err400BadRequest = ErrorCode{4, 0, []byte("Bad Request: The request was malformed.")} Err401Unauthorized = ErrorCode{4, 1, []byte("Unauthorized: The request did not contain the correct credentials to proceed.")} // Err420UnknownAttributes a ErrorCode value - UnknownAttributes Err420UnknownAttributes = ErrorCode{4, 20, []byte("Unknown Attribute: The server received a STUN packet containing a comprehension-required attribute that it did not understand.")} // Err437AllocationMismatch a ErrorCode value - AllocationMismatch Err437AllocationMismatch = ErrorCode{4, 37, []byte("AllocationMismatch: 5-TUPLE didn't match, or conflicted with existing state.")} // Err438StaleNonce a ErrorCode value - StaleNonce Err438StaleNonce = ErrorCode{4, 38, []byte("Stale Nonce: The NONCE used by the client was no longer valid.")} // Err442UnsupportedTransportProtocol a ErrorCode value - UnsupportedTransportProtocol Err442UnsupportedTransportProtocol = ErrorCode{4, 42, []byte("Unsupported Transport Protocol: UDP is the only supported transport protocol.")} // Err500ServerError a ErrorCode value - ServerError Err500ServerError = ErrorCode{5, 0, []byte("Server Error: The server has suffered a temporary error.")} // Err508InsufficentCapacity a ErrorCode value - InsufficentCapacity Err508InsufficentCapacity = ErrorCode{5, 8, []byte("Insufficent Capacity: The server doesn't have the capacity to fulfill this request.")} )
var ( // ErrResponseTooBig is returned if more than maxMessageSize bytes are returned in the response // see https://tools.ietf.org/html/rfc5389#section-7 for the size limit ErrResponseTooBig = errors.New("received too much data") )
Functions ¶
func BuildAndSend ¶
func BuildAndSend(conn net.PacketConn, addr *TransportAddr, class MessageClass, method Method, transactionID []byte, attrs ...Attribute) error
BuildAndSend is building message, pack using attribute and send
func GenerateTransactionID ¶
func GenerateTransactionID() []byte
GenerateTransactionID returns 16bytes ids
func IsSTUN ¶
IsSTUN determines if a package is likely a STUN package Used for de-multiplexing STUN packages
func MessageIntegrityCalculateHMAC ¶
MessageIntegrityCalculateHMAC returns hmac checksum
Types ¶
type AttrType ¶
type AttrType uint16
AttrType represents an attribute type https://tools.ietf.org/html/rfc5389#section-15
const ( AttrMappedAddress AttrType = 0x0001 // STUN AttrResponseAddress AttrType = 0x0002 // STUN Invalid AttrChangeAddress AttrType = 0x0003 // STUN Invalid AttrSourceAddress AttrType = 0x0004 // STUN Invalid AttrChangedAddress AttrType = 0x0005 // STUN Invalid AttrUsername AttrType = 0x0006 // STUN AttrPassword AttrType = 0x0007 // STUN Invalid AttrMessageIntegrity AttrType = 0x0008 // STUN AttrErrorCode AttrType = 0x0009 // STUN AttrUnknownAttributes AttrType = 0x000A // STUN AttrReflectedFrom AttrType = 0x000B // STUN Invalid AttrChannelNumber AttrType = 0x000C // TURN AttrLifetime AttrType = 0x000D // TURN AttrBandwidth AttrType = 0x0010 // TURN Invalid AttrXORPeerAddress AttrType = 0x0012 // TURN AttrData AttrType = 0x0013 // TURN AttrXORRelayedAddress AttrType = 0x0016 // TURN AttrEvenPort AttrType = 0x0018 // TURN AttrRequestedTransport AttrType = 0x0019 // TURN AttrDontFragment AttrType = 0x001A // TURN AttrTimerVal AttrType = 0x0021 // TURN AttrReservationToken AttrType = 0x0022 // TURN AttrRealm AttrType = 0x0014 // STUN AttrNonce AttrType = 0x0015 // STUN AttrXORMappedAddress AttrType = 0x0020 // STUN )
Comprehension-required range (0x0000-0x7FFF)
const ( AttrSoftware AttrType = 0x8022 // STUN AttrAlternateServer AttrType = 0x8023 // STUN AttrFingerprint AttrType = 0x8028 // STUN )
Comprehension-optional range (0x8000-0xFFFF):
const ( AttrPriority AttrType = 0x0024 // STUN ICE AttrUseCandidate AttrType = 0x0025 // STUN ICE AttrIceControlled AttrType = 0x8029 // STUN ICE AttrIceControlling AttrType = 0x802A // STUN ICE )
https://tools.ietf.org/html/rfc5245#section-19.1 ICE STUN Attributes
type Attribute ¶
type Attribute interface { Pack(message *Message) error Unpack(message *Message, rawAttribute *RawAttribute) error }
Attribute can implement Pack and Unpack
type ChannelData ¶
ChannelData is struct including ChannelNumber and Data
func NewChannelData ¶
func NewChannelData(packet []byte) (*ChannelData, error)
NewChannelData return ChannelData from packet
type ChannelNumber ¶
type ChannelNumber struct {
ChannelNumber uint16
}
A ChannelNumber is a 4-byte header that identifies a Channel. Each channel number in use is bound to a specific peer and thus serves as a shorthand for the peer's host transport address. https://tools.ietf.org/html/rfc5766#section-2.5
func (*ChannelNumber) Pack ¶
func (x *ChannelNumber) Pack(message *Message) error
Pack a ChannelNumber attribute, adding it to the passed message
func (*ChannelNumber) Unpack ¶
func (x *ChannelNumber) Unpack(message *Message, rawAttribute *RawAttribute) error
Unpack a ChannelNumber, deserializing the rawAttribute and populating the struct
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a STUN client that sents STUN requests and receives STUN responses
type Data ¶
type Data struct {
Data []byte
}
A Data represents the attribute is present in all Send and Data indications. The value portion of this attribute is variable length and consists of the application data (that is, the data that would immediately follow the UDP header if the data was been sent directly between the client and the peer). If the length of this attribute is not a multiple of 4, then padding must be added after this attribute.
type ErrorCode ¶
An ErrorCode attribute is used in error response messages. It contains a numeric error code value in the range of 300 to 699 plus a textual reason phrase encoded in UTF-8
0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Reserved, should be 0 |Class| Number | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Reason Phrase (variable) .. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
type EvenPort ¶
type EvenPort struct {
ReserveAdditional bool
}
A EvenPort attribute allows the client to request that the port in the relayed transport address be even, and (optionally) that the server reserve the next-higher port number. The value portion of this attribute is 1 byte long.
type Fingerprint ¶
type Fingerprint struct {
Fingerprint uint32
}
A Fingerprint attribute MAY be present in all STUN messages. The value of the attribute is computed as the CRC-32 of the STUN message up to (but excluding) the FINGERPRINT attribute itself, XOR'ed with the 32-bit value 0x5354554e (the XOR helps in cases where an application packet is also using CRC-32 in it)
func (*Fingerprint) Unpack ¶
func (s *Fingerprint) Unpack(message *Message, rawAttribute *RawAttribute) error
Unpack with Fingerprint
type IceControlled ¶
type IceControlled struct {
TieBreaker uint64
}
IceControlled struct representated tiebreak
func (*IceControlled) Pack ¶
func (i *IceControlled) Pack(message *Message) error
Pack with TieBreak
func (*IceControlled) Unpack ¶
func (i *IceControlled) Unpack(message *Message, rawAttribute *RawAttribute) error
Unpack with TieBreak
type IceControlling ¶
type IceControlling struct {
TieBreaker uint64
}
IceControlling struct representated tiebreak
func (*IceControlling) Pack ¶
func (i *IceControlling) Pack(message *Message) error
Pack with TieBreak
func (*IceControlling) Unpack ¶
func (i *IceControlling) Unpack(message *Message, rawAttribute *RawAttribute) error
Unpack with TieBreak
type Message ¶
type Message struct { Class MessageClass Method Method Length uint16 TransactionID []byte Attributes []*RawAttribute Raw []byte }
Message structs
func Build ¶
func Build(class MessageClass, method Method, transactionID []byte, attrs ...Attribute) (*Message, error)
Build return messsage which is built from class, method, transactionID, and pack message using attribute
func NewMessage ¶
NewMessage parses a binary STUN message into a Message struct TODO Break this apart, too big
func (*Message) AddAttribute ¶
AddAttribute append bytes formatted RawAttribute to message
func (*Message) GetAllAttributes ¶
func (m *Message) GetAllAttributes(attrType AttrType) ([]*RawAttribute, bool)
GetAllAttributes can get all RawAttributes which adopt attrbute type
func (*Message) GetOneAttribute ¶
func (m *Message) GetOneAttribute(attrType AttrType) (*RawAttribute, bool)
GetOneAttribute can get a RawAttribute which adopts attrbute type
type MessageClass ¶
type MessageClass byte
MessageClass of 0b00 is a request, a class of 0b01 is an
indication, a class of 0b10 is a success response, and a class of 0b11 is an error response.
https://tools.ietf.org/html/rfc5389#section-6
const ( // ClassRequest describes a request method type ClassRequest MessageClass = 0x00 // ClassIndication describes an indication method type ClassIndication MessageClass = 0x01 // ClassSuccessResponse describes an success response method type ClassSuccessResponse MessageClass = 0x02 // ClassErrorResponse describes an error response method type ClassErrorResponse MessageClass = 0x03 )
func (MessageClass) String ¶
func (m MessageClass) String() string
String prints the known class names and a hex format for unknown class names
type MessageIntegrity ¶
type MessageIntegrity struct {
Key []byte
}
MessageIntegrity is struct represented MESSAGE-INTEGRITY attribute rfc5389#section-15.4
func (*MessageIntegrity) Pack ¶
func (m *MessageIntegrity) Pack(message *Message) error
Pack message with MessageIntegrity
func (*MessageIntegrity) Unpack ¶
func (m *MessageIntegrity) Unpack(message *Message, rawAttribute *RawAttribute) error
Unpack copy from Key to rawAttribute.Value
type Method ¶
type Method uint16
Method is selector which can be select MethodType
const ( MethodBinding Method = 0x01 // STUN MethodAllocate Method = 0x03 // TURN (Req/Rsp) MethodRefresh Method = 0x04 // TURN (Req/Rsp) MethodSend Method = 0x06 // TURN (Ind) MethodData Method = 0x07 // TURN (Ind) MethodCreatePermission Method = 0x08 // TURN (Req/Rsp) MethodChannelBind Method = 0x09 // TURN (Req/Rsp) )
Method Type
type Nonce ¶
type Nonce struct {
Nonce string
}
Nonce struct represented by NONCE attribute rfc5389#section-15.8
type PacketType ¶
type PacketType int
PacketType is whether STUN or ChannelData
const ( PacketTypeSTUN PacketType = iota PacketTypeChannelData PacketType = iota )
PacketTypes
func GetPacketType ¶
func GetPacketType(packet []byte) (PacketType, error)
GetPacketType returns PacketType(whether STUN or ChannelData)
type Priority ¶
type Priority struct {
Priority uint32
}
Priority is a STUN Priority message
type RawAttribute ¶
RawAttribute represents an unprocessed view of the TLV structure for an attribute https://tools.ietf.org/html/rfc5389#section-15
type Realm ¶
type Realm struct {
Realm string
}
Realm represented REALM attribute rfc5389#section-15.7
type RequestedTransport ¶
type RequestedTransport struct {
Protocol protocolNumber
}
RequestedTransport represented transport protocol
func (*RequestedTransport) Pack ¶
func (r *RequestedTransport) Pack(message *Message) error
Pack always error
func (*RequestedTransport) Unpack ¶
func (r *RequestedTransport) Unpack(message *Message, rawAttribute *RawAttribute) error
Unpack RequestedTransport protocol
type ReservationToken ¶
type ReservationToken struct {
ReservationToken string
}
ReservationToken struct representated RESERVATION-TOKEN attribute rfc5766#section-14.9
func (*ReservationToken) Pack ¶
func (r *ReservationToken) Pack(message *Message) error
Pack with checking reservationTokenMaxLength
func (*ReservationToken) Unpack ¶
func (r *ReservationToken) Unpack(message *Message, rawAttribute *RawAttribute) error
Unpack ReservationToken
type Software ¶
type Software struct {
Software string
}
Software struct has SOFTWARE field that rfc5389#section-15.10
type TransportAddr ¶
TransportAddr is struct with ip address and port number
func NewTransportAddr ¶
func NewTransportAddr(addr net.Addr) (*TransportAddr, error)
NewTransportAddr returns transportadd struct within address and port
func (*TransportAddr) Addr ¶
func (a *TransportAddr) Addr() net.Addr
Addr returns net.UDPAddr from TransportAddr
func (*TransportAddr) Equal ¶
func (a *TransportAddr) Equal(b *TransportAddr) bool
Equal returns both of address and port is same
func (*TransportAddr) String ¶
func (a *TransportAddr) String() string
String returns "address:port"
type UnknownAttributes ¶
type UnknownAttributes struct {
Attributes []AttrType
}
UnknownAttributes has several attrTypes
func (*UnknownAttributes) Pack ¶
func (u *UnknownAttributes) Pack(message *Message) error
Pack AttrUnknownAttributes
func (*UnknownAttributes) Unpack ¶
func (u *UnknownAttributes) Unpack(message *Message, rawAttribute *RawAttribute) error
Unpack always returns error
type UseCandidate ¶
type UseCandidate struct { }
UseCandidate has no field struct
func (*UseCandidate) Pack ¶
func (u *UseCandidate) Pack(message *Message) error
Pack with use-candidate attribute
func (*UseCandidate) Unpack ¶
func (u *UseCandidate) Unpack(message *Message, rawAttribute *RawAttribute) error
Unpack use-candidate attribute
type Username ¶
type Username struct {
Username string
}
Username struct
type XorAddress ¶
XorAddress is struct with in ip address and port number
func (*XorAddress) Unpack ¶
func (x *XorAddress) Unpack(message *Message, rawAttribute *RawAttribute) error
Unpack message checking address and port suites
type XorMappedAddress ¶
type XorMappedAddress struct {
XorAddress
}
XorMappedAddress https://tools.ietf.org/html/rfc5389#section-15.2
func (*XorMappedAddress) Pack ¶
func (x *XorMappedAddress) Pack(message *Message) error
Pack writes an XorMappedAddress into a message
type XorPeerAddress ¶
type XorPeerAddress struct {
XorAddress
}
XorPeerAddress include XorAddress which encoded in the same way as XOR-MAPPED-ADDRESS [RFC5389]
func (*XorPeerAddress) Pack ¶
func (x *XorPeerAddress) Pack(message *Message) error
Pack using XOR-PEER-ADDRESS
type XorRelayedAddress ¶
type XorRelayedAddress struct {
XorAddress
}
XorRelayedAddress include XorAddress which encoded in the same way as XOR-MAPPED-ADDRESS [RFC5389]
func (*XorRelayedAddress) Pack ¶
func (x *XorRelayedAddress) Pack(message *Message) error
Pack using XOR-RELAYED-ADDRESS
Source Files ¶
- attr_channel_number.go
- attr_data.go
- attr_error_code.go
- attr_even_port.go
- attr_fingerprint.go
- attr_ice_controlled.go
- attr_ice_controlling.go
- attr_lifetime.go
- attr_message_integrity.go
- attr_nonce.go
- attr_priority.go
- attr_realm.go
- attr_requested_transport.go
- attr_reservation_token.go
- attr_software.go
- attr_unknown_attributes.go
- attr_use_candidate.go
- attr_username.go
- attr_xor_mapped_address.go
- attr_xor_peer_address.go
- attr_xor_relayed_address.go
- attribute.go
- channel_data.go
- client.go
- encoding.go
- message.go
- message_builder.go
- packet.go
- transport_addr.go
- xor_address.go