sip

package
v3.0.7 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2022 License: AGPL-3.0 Imports: 8 Imported by: 0

README

SIP消息

SIP-message    =  Request / Response
Request        =  Request-Line
                  *( message-header )
                  CRLF
                  [ message-body ]

SIP消息头域

message-header  =  (Accept
                /  Accept-Encoding
                /  Accept-Language
                /  Alert-Info
                /  Allow
                /  Authentication-Info
                /  Authorization
                /  Call-ID
                /  Call-Info
                /  Contact
                /  Content-Disposition
                /  Content-Encoding
                /  Content-Language
                /  Content-Length
                /  Content-Type
                /  CSeq
                /  Date
                /  Error-Info
                /  Expires
                /  From
                /  In-Reply-To
                /  Max-Forwards
                /  MIME-Version
                /  Min-Expires
                /  Organization
                /  Priority
                /  Proxy-Authenticate
                /  Proxy-Authorization
                /  Proxy-Require
                /  Record-Route
                /  Reply-To
                /  Require
                /  Retry-After
                /  Route
                /  Server
                /  Subject
                /  Supported
                /  Timestamp
                /  To
                /  Unsupported
                /  User-Agent
                /  Via
                /  Warning
                /  WWW-Authenticate
                /  extension-header) CRLF

SIP 响应状态码

Informational  =  "100"  ;  Trying
              /   "180"  ;  Ringing
              /   "181"  ;  Call Is Being Forwarded
              /   "182"  ;  Queued
              /   "183"  ;  Session Progress
Success  =  "200"  ;  OK

Redirection  =  "300"  ;  Multiple Choices
            /   "301"  ;  Moved Permanently
            /   "302"  ;  Moved Temporarily
            /   "305"  ;  Use Proxy
            /   "380"  ;  Alternative Service

Client-Error  =  "400"  ;  Bad Request
             /   "401"  ;  Unauthorized
             /   "402"  ;  Payment Required
             /   "403"  ;  Forbidden
             /   "404"  ;  Not Found
             /   "405"  ;  Method Not Allowed
             /   "406"  ;  Not Acceptable
             /   "407"  ;  Proxy Authentication Required
             /   "408"  ;  Request Timeout
             /   "410"  ;  Gone
             /   "413"  ;  Request Entity Too Large
             /   "414"  ;  Request-URI Too Large
             /   "415"  ;  Unsupported Media Type
             /   "416"  ;  Unsupported URI Scheme
             /   "420"  ;  Bad Extension
             /   "421"  ;  Extension Required
             /   "423"  ;  Interval Too Brief
             /   "480"  ;  Temporarily not available
             /   "481"  ;  Call Leg/Transaction Does Not Exist
             /   "482"  ;  Loop Detected
             /   "483"  ;  Too Many Hops
             /   "484"  ;  Address Incomplete
             /   "485"  ;  Ambiguous
             /   "486"  ;  Busy Here
             /   "487"  ;  Request Terminated
             /   "488"  ;  Not Acceptable Here
             /   "491"  ;  Request Pending
             /   "493"  ;  Undecipherable

Server-Error  =  "500"  ;  Internal Server Error
             /   "501"  ;  Not Implemented
             /   "502"  ;  Bad Gateway
             /   "503"  ;  Service Unavailable
             /   "504"  ;  Server Time-out
             /   "505"  ;  SIP Version not supported
             /   "513"  ;  Message Too Large
Global-Failure  =  "600"  ;  Busy Everywhere
               /   "603"  ;  Decline
               /   "604"  ;  Does not exist anywhere
               /   "606"  ;  Not Acceptable

Documentation

Index

Constants

View Source
const (
	MESSAGE_CATALOG                  = "Catalog"
	MESSAGE_DEVICE_INFO              = "DeviceInfo"
	MESSAGE_BROADCAST                = "Broadcast"
	MESSAGE_DEVICE_STATUS            = "DeviceStatus"
	MESSAGE_KEEP_ALIVE               = "Keepalive"
	MESSAGE_MOBILE_POSITION          = "MobilePosition"
	MESSAGE_MOBILE_POSITION_INTERVAL = "Interval"

	ELEMENT_DEVICE_ID   = "DeviceID"
	ELEMENT_DEVICE_LIST = "DeviceList"
	ELEMENT_NAME        = "Name"
	ELEMENT_STATUS      = "Status"
)

sip message body xml解析的字段

View Source
const (
	VERSION         = "SIP/2.0"  // sip version
	CRLF            = "\r\n"     // 0x0D0A
	CRLFCRLF        = "\r\n\r\n" // 0x0D0A0D0A
	DIGEST_ALGO_MD5 = "MD5"
)

换行符号: linux,unix : \r\n windows : \n Mac OS : \r

Variables

View Source
var (
	// CatalogXML 获取设备列表xml样式
	CatalogXML = `<?xml version="1.0"?><Query>
<CmdType>Catalog</CmdType>
<SN>%d</SN>
<DeviceID>%s</DeviceID>
</Query>
`
	// RecordInfoXML 获取录像文件列表xml样式
	RecordInfoXML = `` /* 192-byte string literal not displayed */

	// DeviceInfoXML 查询设备详情xml样式
	DeviceInfoXML = `<?xml version="1.0"?>
<Query>
<CmdType>DeviceInfo</CmdType>
<SN>%d</SN>
<DeviceID>%s</DeviceID>
</Query>
`
)
View Source
var (
	AlarmResponseXML = `<?xml version="1.0"?>
<Response>
<CmdType>Alarm</CmdType>
<SN>17430</SN>
<DeviceID>%s</DeviceID>
</Response>
`
)

AlarmResponseXML alarm response xml样式

Functions

func BuildAlarmResponseXML added in v3.0.4

func BuildAlarmResponseXML(id string) string

BuildRecordInfoXML 获取录像文件列表指令

func BuildCatalogXML added in v3.0.4

func BuildCatalogXML(sn int, id string) string

BuildCatalogXML 获取NVR下设备列表指令

func BuildDeviceInfoXML added in v3.0.4

func BuildDeviceInfoXML(sn int, id string) string

BuildDeviceInfoXML 获取设备详情指令

func BuildRecordInfoXML added in v3.0.4

func BuildRecordInfoXML(sn int, id string, start, end int64) string

BuildRecordInfoXML 获取录像文件列表指令

func DemoMessage

func DemoMessage()

func DemoVIA

func DemoVIA()

func DumpError

func DumpError(code int) string

func Encode

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

func RandBranch added in v3.0.4

func RandBranch() string

z9hG4bK + 10个随机数字

Types

type Authorization

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

func (*Authorization) GetUsername

func (a *Authorization) GetUsername() string

func (*Authorization) Parse

func (a *Authorization) Parse(str string) error

func (*Authorization) String

func (a *Authorization) String() string

Authorization: Digest username="admin", realm="hik", nonce="a8afe6fcbee6331d89d3eb0d3d19ce39", uri="sip:130909115229300920@10.64.49.44:7100", response="907ddb1bcc25174d7de4a96c947fb066", algorithm=MD5, opaque="a853e4f25298413f"

func (*Authorization) Verify

func (a *Authorization) Verify(username, passwd, realm, nonce string) bool

type CSeq

type CSeq struct {
	ID     uint32
	Method Method
	// contains filtered or unexported fields
}

CSeq: 101 INVITE CSeq: 2 REGISTER

func (*CSeq) Parse

func (c *CSeq) Parse(str string) error

func (*CSeq) String

func (c *CSeq) String() string

type Contact

type Contact struct {
	Nickname string //可以没有
	Uri      URI    //

	//header params
	Params map[string]string // include tag/q/expires
	// contains filtered or unexported fields
}

To From Referto Contact From: <sip:34020000001320000001@3402000000>;tag=575945878 To: <sip:34020000002000000001@3402000000> Contact: <sip:34020000001320000001@27.38.49.149:49243> Contact: <sip:34020000001320000001@192.168.1.64:5060>;expires=0

func (*Contact) Parse

func (c *Contact) Parse(str string) (err error)

func (*Contact) String

func (c *Contact) String() string

type Message

type Message struct {
	Mode Mode //0:REQUEST, 1:RESPONSE

	StartLine       *StartLine
	Via             *Via           //Via
	From            *Contact       //From
	To              *Contact       //To
	CallID          string         //Call-ID
	CSeq            *CSeq          //CSeq
	Contact         *Contact       //Contact
	Authorization   *Authorization //Authorization
	MaxForwards     int            //Max-Forwards
	UserAgent       string         //User-Agent
	Subject         string         //Subject
	ContentType     string         //Content-Type
	Expires         int            //Expires
	ContentLength   int            //Content-Length
	Route           *Contact
	Body            string
	Addr            string
	Event           string
	Date            time.Time
	WwwAuthenticate *WwwAuthenticate //gb28181 密码验证 上级发给下级是WwwAuthenticate;下级发给上级是Authorization
	SourceAdd       net.Addr
	DestAdd         net.Addr
}

func BuildMessageRequest

func BuildMessageRequest(method Method, transport, sipSerial, sipRealm, username, srcIP string, srcPort uint16, expires, cseq int, body string) *Message

func Decode

func Decode(data []byte) (msg *Message, err error)

func (*Message) BuildOK

func (m *Message) BuildOK() *Message

func (*Message) BuildResponse

func (m *Message) BuildResponse(code int) *Message

func (*Message) BuildResponseWithPhrase

func (m *Message) BuildResponseWithPhrase(code int, phrase string) *Message

func (*Message) Destination

func (m *Message) Destination() string

目标地址:这个应该是用于通过route头域实现proxy这样的功能,暂时不支持

func (*Message) GetBranch

func (m *Message) GetBranch() string

func (*Message) GetMethod

func (m *Message) GetMethod() Method

func (*Message) GetMode

func (m *Message) GetMode() Mode

func (*Message) GetReason

func (m *Message) GetReason() string

response code and reason

func (*Message) GetStatusCode

func (m *Message) GetStatusCode() int

response code

func (*Message) IsReliable

func (m *Message) IsReliable() bool

此消息是否使用可靠传输

func (*Message) IsRequest

func (m *Message) IsRequest() bool

func (*Message) IsResponse

func (m *Message) IsResponse() bool

func (*Message) Source

func (m *Message) Source() string

构建响应消息的时候,会使用请求消息的 source 和 destination 请求消息的source,格式: host:port

type Method

type Method string

sip request method

const (
	ACK       Method = "ACK"
	BYE       Method = "BYE"
	CANCEL    Method = "CANCEL"
	INVITE    Method = "INVITE"
	OPTIONS   Method = "OPTIONS"
	REGISTER  Method = "REGISTER"
	NOTIFY    Method = "NOTIFY"
	SUBSCRIBE Method = "SUBSCRIBE"
	MESSAGE   Method = "MESSAGE"
	REFER     Method = "REFER"
	INFO      Method = "INFO"
	PRACK     Method = "PRACK"
	UPDATE    Method = "UPDATE"
	PUBLISH   Method = "PUBLISH"
)

type Mode

type Mode int

SIP消息类型:请求or响应

const (
	SIP_MESSAGE_REQUEST  Mode = 0
	SIP_MESSAGE_RESPONSE Mode = 1
)

type Request added in v3.0.4

type Request struct {
	*Message
}

Request Request

type Response added in v3.0.4

type Response struct {
	*Message
}

Response Response

type StartLine

type StartLine struct {

	//request line: method uri version
	Method  Method
	Uri     URI //Request-URI:请求的服务地址,不能包含空白字符或者控制字符,并且禁止用”<>”括上。
	Version string

	//status line: version code phrase
	Code int //status code
	// contains filtered or unexported fields
}

startline MESSAGE sip:34020000001320000001@3402000000 SIP/2.0 SIP/2.0 200 OK

func (*StartLine) String

func (l *StartLine) String() string

type URI

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

RFC3261 SIP-URI = "sip:" [ userinfo ] hostport

uri-parameters [ headers ]

SIPS-URI = "sips:" [ userinfo ] hostport

uri-parameters [ headers ]

userinfo = ( user / telephone-subscriber ) [ ":" password ] "@" user = 1*( unreserved / escaped / user-unreserved ) user-unreserved = "&" / "=" / "+" / "$" / "," / ";" / "?" / "/" password = *( unreserved / escaped /

"&" / "=" / "+" / "$" / "," )

hostport = host [ ":" port ] host = hostname / IPv4address / IPv6reference hostname = *( domainlabel "." ) toplabel [ "." ] domainlabel = alphanum

/ alphanum *( alphanum / "-" ) alphanum

toplabel = ALPHA / ALPHA *( alphanum / "-" ) alphanum

IPv4address = 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT IPv6reference = "[" IPv6address "]" IPv6address = hexpart [ ":" IPv4address ] hexpart = hexseq / hexseq "::" [ hexseq ] / "::" [ hexseq ] hexseq = hex4 *( ":" hex4) hex4 = 1*4HEXDIG port = 1*DIGIT

uri-parameters = *( ";" uri-parameter) uri-parameter = transport-param / user-param / method-param

/ ttl-param / maddr-param / lr-param / other-param

transport-param = "transport="

( "udp" / "tcp" / "sctp" / "tls"
/ other-transport)

other-transport = token user-param = "user=" ( "phone" / "ip" / other-user) other-user = token method-param = "method=" Method ttl-param = "ttl=" ttl maddr-param = "maddr=" host lr-param = "lr" other-param = pname [ "=" pvalue ] pname = 1*paramchar pvalue = 1*paramchar paramchar = param-unreserved / unreserved / escaped param-unreserved = "[" / "]" / "/" / ":" / "&" / "+" / "$"

headers = "?" header *( "&" header ) header = hname "=" hvalue hname = 1*( hnv-unreserved / unreserved / escaped ) hvalue = *( hnv-unreserved / unreserved / escaped ) hnv-unreserved = "[" / "]" / "/" / "?" / ":" / "+" / "$"

func NewURI

func NewURI(host string) URI

对于gb28181,request-uri 不带参数

func (*URI) Domain

func (u *URI) Domain() string

func (*URI) Host

func (u *URI) Host() string

func (*URI) IP

func (u *URI) IP() string

func (*URI) Port

func (u *URI) Port() string

func (*URI) String

func (u *URI) String() string

func (*URI) UserInfo

func (u *URI) UserInfo() string

type Via

type Via struct {
	Version   string // sip version: default to  SIP/2.0
	Transport string // UDP,TCP ,TLS , SCTP
	Host      string // sent-by : host:port
	Port      string //
	//header params
	Params map[string]string // include branch/rport/received/ttl/maddr
	// contains filtered or unexported fields
}

Via = ( "Via" / "v" ) HCOLON via-parm *(COMMA via-parm) via-parm = sent-protocol LWS sent-by *( SEMI via-params ) via-params = via-ttl / via-maddr

/ via-received / via-branch
/ via-extension

via-ttl = "ttl" EQUAL ttl via-maddr = "maddr" EQUAL host via-received = "received" EQUAL (IPv4address / IPv6address) via-branch = "branch" EQUAL token via-extension = generic-param sent-protocol = protocol-name SLASH protocol-version

SLASH transport

protocol-name = "SIP" / token protocol-version = token transport = "UDP" / "TCP" / "TLS" / "SCTP"

/ other-transport

sent-by = host [ COLON port ] ttl = 1*3DIGIT ; 0 to 255

func (*Via) GetBranch

func (v *Via) GetBranch() string

func (*Via) GetSendBy

func (v *Via) GetSendBy() string

func (*Via) Parse

func (v *Via) Parse(str string) (err error)

注意via允许以下这种添加空白 Via: SIP / 2.0 / UDP first.example.com: 4000;ttl=16 ;maddr=224.2.0.1 ;branch=z9hG4bKa7c6a8dlze.1 Via: SIP/2.0/UDP 192.168.1.64:5060;rport=5060;received=192.168.1.64;branch=z9hG4bK1000615294

func (*Via) String

func (v *Via) String() string

type WwwAuthenticate

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

func NewWwwAuthenticate

func NewWwwAuthenticate(realm, nonce, algorithm string) *WwwAuthenticate

func (*WwwAuthenticate) Parse

func (w *WwwAuthenticate) Parse(str string) error

func (*WwwAuthenticate) String

func (w *WwwAuthenticate) String() string

WWW-Authenticate: Digest realm="hik", nonce="a8afe6fcbee6331d89d3eb0d3d19ce39", opaque="a853e4f25298413f9bf3a9aa6767857d", algorithm=MD5

Jump to

Keyboard shortcuts

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