sipnet

package
v0.0.0-...-c962f29 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2020 License: MIT Imports: 11 Imported by: 4

Documentation

Overview

Package sipnet contains tools for connecting and communicating to SIP UAs.

Index

Constants

View Source
const (
	MethodInvite   = "INVITE"
	MethodAck      = "ACK"
	MethodBye      = "BYE"
	MethodCancel   = "CANCEL"
	MethodRegister = "REGISTER"
	MethodOptions  = "OPTIONS"
	MethodInfo     = "INFO"
)

SIP request methods.

View Source
const (
	StatusTrying               = 100
	StatusRinging              = 180
	StatusCallIsBeingForwarded = 181
	StatusQueued               = 182
	StatusSessionProgress      = 183

	StatusOK = 200

	StatusMultipleChoices    = 300
	StatusMovedPermanently   = 301
	StatusMovedTemporarily   = 302
	StatusUseProxy           = 305
	StatusAlternativeService = 380

	StatusBadRequest                  = 400
	StatusUnauthorized                = 401
	StatusPaymentRequired             = 402
	StatusForbidden                   = 403
	StatusNotFound                    = 404
	StatusMethodNotAllowed            = 405
	StatusNotAcceptable               = 406
	StatusProxyAuthenticationRequired = 407
	StatusRequestTimeout              = 408
	StatusGone                        = 410
	StatusRequestEntityTooLarge       = 413
	StatusRequestURITooLong           = 414
	StatusUnsupportedMediaType        = 415
	StatusUnsupportedURIScheme        = 416
	StatusBadExtension                = 420
	StatusExtensionRequired           = 421
	StatusIntervalTooBrief            = 423
	StatusNoResponse                  = 480
	StatusCallTransactionDoesNotExist = 481
	StatusLoopDetected                = 482
	StatusTooManyHops                 = 483
	StatusAddressIncomplete           = 484
	StatusAmbigious                   = 485
	StatusBusyHere                    = 486
	StatusRequestTerminated           = 487
	StatusNotAcceptableHere           = 488
	StatusRequestPending              = 491
	StatusUndecipherable              = 493

	StatusServerInternalError = 500
	StatusNotImplemented      = 501
	StatusBadGateway          = 502
	StatusServiceUnavailable  = 503
	StatusServerTimeout       = 504
	StatusVersionNotSupported = 505
	StatusMessageTooLarge     = 513

	StatusBusyEverywhere       = 600
	StatusDecline              = 603
	StatusDoesNotExistAnywhere = 604
	StatusUnacceptable         = 606
)

SIP response status codes.

View Source
const SIPVersion = "SIP/2.0"

SIPVersion is the version of SIP used by this library.

Variables

View Source
var ErrBadMessage = errors.New("sip: bad message")

ErrBadMessage is returned by ReadRequest and ReadResponse if the message received failed to be parsed.

View Source
var ErrClosed = errors.New("sip: closed")

ErrClosed is returned if AcceptRequest is called on a closed listener. io.EOF may also be returned on a closed underlying connection, in which the connection itself will also be returned.

View Source
var ErrInvalidTransport = errors.New("sip: invalid transport")

ErrInvalidTransport is returned by Dial if the transport provided is invalid.

View Source
var ErrParseError = errors.New("sip: parse error")

ErrParseError is returned when a piece of data fails to be parsed (i.e. a user line or via).

Functions

func Dial

func Dial(addr, transport string) (net.Conn, error)

Dial creates a connection to a SIP UA. It does NOT "dial" a user. addr is an IP:port string, transport is the transport protocol to be used, (i.e. "tcp" or "udp").

After dialling, you should use ReadResponse to read from the connection, and Request.WriteTo to write requests to the connection.

func ParseList

func ParseList(value string) []string

ParseList parses a comma, semicolon, or new line separated list of values and returns list elements.

Lifted from https://code.google.com/p/gorilla/source/browse/http/parser/parser.go which was ported from urllib2.parse_http_list, from the Python standard library.

func ParseUserHeader

func ParseUserHeader(h Header) (User, User, error)

ParseUserHeader returns the parsed users from the From, and the To fields respectively from the header.

func StatusText

func StatusText(code int) string

StatusText returns the human readable text representation of a status code.

Types

type Conn

type Conn struct {
	Transport   string
	Listener    *Listener
	Conn        net.Conn
	Address     net.Addr
	UdpReceiver chan []byte
	Closed      bool
	Locked      bool
	WriteBuffer *bytes.Buffer
	ReadMessage chan interface{}
	LastMessage time.Time

	ReceivedBranches map[string]time.Time
	BranchMutex      *sync.Mutex
}

Conn represents a connection with a UA. It can be on UDP or TCP.

func (*Conn) Addr

func (c *Conn) Addr() net.Addr

Addr returns the network address of the connected UA.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the connection.

func (*Conn) Flush

func (c *Conn) Flush() error

Flush flushes the buffered data to be written. In the case of using UDP, the buffered data will be written in a single UDP packet.

func (*Conn) Lock

func (c *Conn) Lock()

Lock must be called to use Read(). It locks the connection to be read by the user rather than by read by AcceptRequest().

func (*Conn) Read

func (c *Conn) Read() interface{}

Read reads either a *Request, a *Response, or an error from the connection.

func (*Conn) Unlock

func (c *Conn) Unlock()

Unlock should be called after the user is finished reading custom data to the connection.

func (*Conn) Write

func (c *Conn) Write(b []byte) (int, error)

Write writes data to a buffer.

type Flushable

type Flushable interface {
	Flush() error
}

Flushable is used by Request.WriteTo to determine whether or not the provided connection is flushable, and if so, writes and then flushes it.

type Header map[string]string

Header represents the headers of a SIP Request or Response.

func (Header) Del

func (h Header) Del(key string)

Del deletes the key and its value from the header. Deleting a non-existent key is a no-op.

func (Header) Get

func (h Header) Get(key string) string

Get returns the value at a given key. It returns an empty string if the key does not exist.

func (Header) Set

func (h Header) Set(key, value string)

Set sets a header key with a value.

func (Header) WriteTo

func (h Header) WriteTo(w io.Writer) (int64, error)

WriteTo writes the header data to a writer, with an additional CRLF (i.e. "\r\n") at the end.

type HeaderArgs

type HeaderArgs map[string]string

HeaderArgs represents the arguments which can be found in headers, and in other simple key value fields whose format is of a key=value with a delimiter.

func ParseHeaderArgs

func ParseHeaderArgs(str string) HeaderArgs

ParseHeaderArgs parses header arguments from a full header.

func ParsePairs

func ParsePairs(value string) HeaderArgs

ParsePairs extracts key/value pairs from comma, semicolon, or new line separated values.

Lifted from https://code.google.com/p/gorilla/source/browse/http/parser/parser.go

func (HeaderArgs) CRLFString

func (h HeaderArgs) CRLFString() string

CRLFString returns the header arguments as a CRLF separated string.

func (HeaderArgs) CommaString

func (h HeaderArgs) CommaString() string

CommaString returns the header arguments as a comma and space separated string.

func (HeaderArgs) Del

func (h HeaderArgs) Del(key string)

Del deletes the key and its value from the header arguments. Deleting a non-existent key is a no-op.

func (HeaderArgs) Get

func (h HeaderArgs) Get(key string) string

Get returns the value at a given key. It returns an empty string if the key does not exist.

func (HeaderArgs) SemicolonString

func (h HeaderArgs) SemicolonString() string

SemicolonString returns the header arguments as a semicolon separated unquoted strings with a leading semicolon.

func (HeaderArgs) Set

func (h HeaderArgs) Set(key, value string)

Set sets a header argument key with a value.

type Listener

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

Listener represents a TCP and UDP wrapper listener.

func Listen

func Listen(addr string) (*Listener, error)

Listen listens on an address (IP:port) on both TCP and UDP.

func (*Listener) AcceptRequest

func (l *Listener) AcceptRequest() (*Request, *Conn, error)

AcceptRequest blocks until it receives a Request message on either TCP or UDP listeners. Responses are to be written to *Conn (and then flushed).

func (*Listener) Addr

func (l *Listener) Addr() net.Addr

Addr returns the address the listener is listening on.

func (*Listener) Close

func (l *Listener) Close() error

Close closes both TCP and UDP listeners, and returns

type Request

type Request struct {
	Method     string
	Server     string
	SIPVersion string
	Header     Header
	Body       []byte
}

Request represents a SIP request (i.e. a message sent by a UAC to a UAS).

func NewRequest

func NewRequest() *Request

NewRequest returns a new request.

func ReadRequest

func ReadRequest(rd io.Reader) (*Request, error)

ReadRequest reads a SIP request (i.e. message from a UAC) from a reader.

func (*Request) WriteTo

func (r *Request) WriteTo(conn net.Conn) error

WriteTo writes the request data to a Conn. It automatically adds a a Content-Length to the header, calls Flush() on the Conn.

type Response

type Response struct {
	StatusCode int
	Status     string
	SIPVersion string
	Header     Header
	Body       []byte
}

Response represents a SIP response (i.e. a message sent by a UAS to a UAC).

func NewResponse

func NewResponse() *Response

NewResponse returns a new response.

func ReadResponse

func ReadResponse(rd io.Reader) (*Response, error)

ReadResponse reads a SIP response (i.e. message from a UAS) from a reader.

func (*Response) BadRequest

func (r *Response) BadRequest(conn *Conn, req *Request, reason string)

BadRequest responds to a Conn with a StatusBadRequest for convenience.

func (*Response) ServerError

func (r *Response) ServerError(conn *Conn, req *Request, reason string)

ServerError responds to a Conn with a StatusServerInternalError for convenience.

func (*Response) WriteTo

func (r *Response) WriteTo(conn *Conn, req *Request) error

WriteTo writes the response data to a Conn. It automatically adds a a Content-Length, CSeq, Call-ID and Via header. It also sets the Status message appropriately and automatically calls Flush() on the Conn.

type URI

type URI struct {
	Scheme    string
	Username  string
	Domain    string
	Arguments HeaderArgs
}

URI represents a Uniform Resource Identifier.

func ParseURI

func ParseURI(str string) (URI, error)

ParseURI parses a given URI into a URI struct.

func (URI) SchemeUserDomain

func (u URI) SchemeUserDomain() string

SchemeUserDomain returns the text representation of the scheme:user@domain.

func (URI) String

func (u URI) String() string

String returns the full text representation of the URI with additional semicolon arguments.

func (URI) UserDomain

func (u URI) UserDomain() string

UserDomain returns the text representation of user@domain.

type User

type User struct {
	Name      string
	URI       URI
	Arguments HeaderArgs
}

User represents a SIP user.

func ParseUser

func ParseUser(str string) (User, error)

ParseUser parses a given user line into a User.

func (User) String

func (u User) String() string

String returns the string representation of a user to be used on user lines.

type Via

type Via struct {
	SIPVersion string
	Transport  string
	Client     string
	Arguments  HeaderArgs
}

Via represents the contents of the Via header line.

func ParseVia

func ParseVia(str string) (Via, error)

ParseVia parses a given Via header value into a Via.

func (Via) String

func (v Via) String() string

String returns the string representation of the Via header line.

Jump to

Keyboard shortcuts

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