stun

package
v0.0.0-...-19f4127 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2017 License: BSD-3-Clause Imports: 5 Imported by: 4

Documentation

Overview

Package stun represents the common data structures used in creating and parsing STUN messages.

Index

Constants

View Source
const (
	// MagicCookie is a STUN defined constant for wire representation of messages.
	MagicCookie uint32 = 0x2112A442
)

Variables

This section is empty.

Functions

func WriteAttributeHeader

func WriteAttributeHeader(buf *bytes.Buffer, a Attribute, msg *Message) error

WriteHeader will append a STUN attribute header onto a byte buffer for a given attribute and message pair.

Types

type Address

type Address struct {
	net.Addr
}

Address represents an Internet host and port

func NewAddress

func NewAddress(network string, host net.IP, port uint16) Address

NewAddress creates a new address representing a given host and port.

func NewAddressFromString

func NewAddressFromString(network, address string) Address

NewAddressFromString creates a new address from a string representation.

func (*Address) Family

func (a *Address) Family() uint16

Family provides the STUN family of an address. A value of 1 if IPv4 and 2 if IPv6

func (*Address) Host

func (a *Address) Host() net.IP

Host provides the net.IP of the Internet Host.

func (*Address) HostPart

func (a *Address) HostPart() net.Addr

HostPart provides an address representing the same host, but with a port of 0.

func (*Address) Port

func (a *Address) Port() uint16

Port provides the Port of an address, or 0 if there is no port.

type Attribute

type Attribute interface {
	// Type provides the type of an attribute for encoding it into a STUN message.
	Type() AttributeType
	// Encode returns the byte array of the contents of the attribute when it
	// should be appended to a given STUN message. The message is provided because
	// some attributes must calculate a checksum based upon the previous contents
	// of the message.
	Encode(*Message) ([]byte, error)
	// Decode instantiates an Attribute from a []byte, when being parsed out of
	// a byte stream represented by a Parser. The attribute is assumed to have
	// been parsed correctly when a nil error is returned.
	Decode([]byte, uint16, *Parser) error
	// Length returns the length of the encoded attribute body when it would be
	// encoded as part of a STUN message.
	Length(*Message) uint16
}

Attribute represents a single serializable STUN attribute.

func DecodeAttribute

func DecodeAttribute(data []byte, attrs AttributeSet, parser *Parser) (*Attribute, error)

DecodeAttribute returns a parsed Attribute representation of data based upon the known AttributeType's mapped by attrs.

func NewUnknownAttribute

func NewUnknownAttribute() Attribute

NewUnknownAttribute Instantiates a new instance of an UnknownAttribute cast to the Attribute Interface.

type AttributeSet

type AttributeSet map[AttributeType]func() Attribute

AttributeSet represents the mapping of known attribute types that a parser will use when parsing a STUN message.

type AttributeType

type AttributeType uint16

AttributeType is the numeric representation of a STUN attribute.

type Credentials

type Credentials struct {
	// Each conversation has a server-provided Nonce, learned in the first server
	// response.
	Nonce []byte
	// Conversations with a long-term identity will have a Username provided
	// out-of-band.
	Username string
	// Conversations with a long-term identity will have a Realm provided by the
	// server.
	Realm string
	// Conversations validated with a message integrity attribute must have a
	// password provided out-of-band.
	Password string
}

Credentials represent knowledge about a STUN conversation that is more general than a single message.

func (*Credentials) ForNewConnection

func (c *Credentials) ForNewConnection() *Credentials

ForNewConnection derives a new set of credentials with same authorization, as an existing set, but expecting a new Nonce to be provided.

func (Credentials) String

func (c Credentials) String() string

String represents credentials as a string for debugging.

type Header struct {
	// The Purpose of a STUN message is denoted by its Type.
	Type HeaderType
	// Length represents the total length of the Message Attributes.
	Length uint16
	// Each STUN message has a unique Id.
	Id [12]byte
}

Header represents the header of a STUN message.

func (*Header) Decode

func (h *Header) Decode(data []byte) error

Decode parses a header from its byte representation.

func (*Header) Encode

func (h *Header) Encode() ([]byte, error)

Encode the byte representation of a STUN header.

func (Header) String

func (h Header) String() string

String provides a textual representation of a STUN header for logging or debugging.

type HeaderType

type HeaderType uint16

HeaderType is a numeric representation of the type of STUN message and whether it is a request, response, indication, or error.

type Message

type Message struct {
	// A message has a header, with the message ID and type.
	Header
	// A message has associated credentials, representing the implicit
	// conversation the message is part of.
	Credentials
	// A message has a set of Attributes, representing the body of the message.
	Attributes []Attribute
}

Message represents a single STUN Message.

func Parse

func Parse(data []byte, credentials *Credentials, attrs AttributeSet) (*Message, error)

Parse creates a Message representation of a data byte stream given provided credentials and a known mapping of Attributes.

func (*Message) GetAttribute

func (m *Message) GetAttribute(typ AttributeType) *Attribute

GetAttribute extracts a single Attribute from the body of a Message

func (*Message) Serialize

func (m *Message) Serialize() ([]byte, error)

Serialize encodes the []byte representation of a STUN Message.

type Parser

type Parser struct {
	// The message representation generated by the parser.
	*Message
	// The credentials used to verify a parsed message.
	*Credentials
	// The known STUN attribute types.
	AttributeSet
	// The binary data to parse.
	Data []byte
	// Which subset of the Data is represented by Message.
	Offset uint16
}

Parser creates a Message representation of a serialized []byte stream.

type UnknownStunAttribute

type UnknownStunAttribute struct {
	// ClaimedType holds the AttributeType value of the unknown attribute.
	ClaimedType AttributeType
	// Data contains the unparsed body of the Attribute.
	Data []byte
}

UnknownStunAttribute represents a STUN attribute found in a message for which there is no parsed attribute type available.

func (*UnknownStunAttribute) Decode

func (h *UnknownStunAttribute) Decode(data []byte, length uint16, _ *Parser) error

func (*UnknownStunAttribute) Encode

func (h *UnknownStunAttribute) Encode(msg *Message) ([]byte, error)

func (*UnknownStunAttribute) Length

func (h *UnknownStunAttribute) Length(_ *Message) uint16

func (*UnknownStunAttribute) Type

Jump to

Keyboard shortcuts

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