sip

package
v1.3.9 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: GPL-3.0 Imports: 11 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_RECORD_INFO              = "RecordInfo"
	MESSAGE_BROADCAST                = "Broadcast"
	MESSAGE_DEVICE_STATUS            = "DeviceStatus"
	MESSAGE_DEVICE_CTL               = "DeviceControl"
	MESSAGE_KEEP_ALIVE               = "Keepalive"
	MESSAGE_ALARM                    = "Alarm"
	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 (
	LIVE_STREAM_PUSH LiveType = iota
	LIVE_CDN_PUSH
	ExecStop ExecType = iota
	ExecPlay
)
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>
`
	//DeviceStatusXml 查询设备状态xml样式
	DeviceStatusXml = `<?xml version="1.0"?>
	<Query>
		<CmdType>DeviceStatus</CmdType>
		<SN>%d</SN>
		<DeviceID>%s</DeviceID>
	</Query>
`
	//KeepLiveXml 心跳命令xml
	KeepLiveXml = `` /* 126-byte string literal not displayed */

)
View Source
var (
	AlarmResponseXML = `<?xml version="1.0"?>
<Response>
<CmdType>Alarm</CmdType>
<SN>17430</SN>
<DeviceID>%s</DeviceID>
</Response>
`
	NotifyResponseXML = `` /* 129-byte string literal not displayed */

	DeviceResponseXML = `` /* 244-byte string literal not displayed */

)

AlarmResponseXML alarm response xml样式

Functions

func BuildAlarmResponseXML

func BuildAlarmResponseXML(id string) string

BuildRecordInfoXML 获取录像文件列表指令

func BuildCatalogXML

func BuildCatalogXML(sn int, id string) string

BuildCatalogXML 获取NVR下设备列表指令

func BuildDeviceInfoXML

func BuildDeviceInfoXML(sn int, id string) string

BuildDeviceInfoXML 获取设备详情指令

func BuildDeviceStatusXml added in v1.0.3

func BuildDeviceStatusXml(sn int, id string) string

BuildDeviceStatusXml 获取设备状态指令

func BuildKeepLvieXML added in v1.0.3

func BuildKeepLvieXML(sn int, id string) string

BuildKeepLvieXML 设备状态报送指令

func BuildRecordInfoXML

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

BuildRecordInfoXML 获取录像文件列表指令

func CreatePtzCmd

func CreatePtzCmd(ptzType PTZ_Cmd_type, param int) string

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

func RandBranch() string

z9hG4bK + 10个随机数字

func XmlMashal added in v1.0.6

func XmlMashal(mode interface{}) (string, error)

Types

type Authorization

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

func NewAuthorization added in v1.0.7

func NewAuthorization(username, realm, nonce, uri, pwd, algorithm string) *Authorization

func ToAuthorization added in v1.0.8

func ToAuthorization(username, uri, pwd string, authen *WwwAuthenticate) *Authorization

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 ChannelInfo added in v1.0.6

type ChannelInfo struct {
	DeviceID     string
	Name         string
	Manufacturer string
	Model        string
	Owner        string
	CivilCode    string //对应行政区域代码
	Block        string //对应组织机构代码 符合GA/T 380-2011
	Address      string //设备安装地址
	Parental     string
	ParentID     string //设备父级设备ID/虚拟组织
	SafetyWay    string
	RegisterWay  string
	Secrecy      uint
	IPAddress    string
	Port         uint16
	Status       string
	Longitude    string
	Latitude     string
}

type CmdCdnLiveResult added in v1.3.3

type CmdCdnLiveResult struct {
	XMLName  xml.Name `xml:"Reponse"`
	CmdType  string
	DeviceID string
	SN       string
	Result   string
	Extends  string //扩展数据
}

func (*CmdCdnLiveResult) Decode added in v1.3.4

func (r *CmdCdnLiveResult) Decode(data string) error

func (*CmdCdnLiveResult) String added in v1.3.4

func (r *CmdCdnLiveResult) String() string

type CmdCdnLiver added in v1.3.3

type CmdCdnLiver struct {
	XMLName  xml.Name `xml:"Control"`
	CmdType  string
	DeviceID string
	SN       string
	LiveType LiveType //直播类型: 0:流媒体推流 1:CDN推流
	ExecCmd  ExecType // 执行类型: 0:stop 1:start
	Endtimes uint64   //结束时间
}

func (*CmdCdnLiver) Decode added in v1.3.4

func (c *CmdCdnLiver) Decode(data string) error

func (*CmdCdnLiver) String added in v1.3.4

func (c *CmdCdnLiver) 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 ExecType added in v1.3.3

type ExecType int

func (ExecType) String added in v1.3.3

func (e ExecType) String() string

type LiveType added in v1.3.3

type LiveType int

func (LiveType) String added in v1.3.3

func (l LiveType) 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
	XProtocol       XProtocol //协议类型: GB28181,PMS-SIP
	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 PTZ_Cmd_type

type PTZ_Cmd_type int
const (
	Ptz_ctrl_halt      PTZ_Cmd_type = iota //停止0
	Ptz_ctrl_right                         //右转1
	Ptz_ctrl_rightup                       //右上2
	Ptz_ctrl_up                            //向上3
	Ptz_ctrl_leftup                        //左上4
	Ptz_ctrl_left                          //向左5
	Ptz_ctrl_leftdown                      //左下6
	Ptz_ctrl_down                          //向下7
	Ptz_ctrl_rightdown                     //右下8
	Ptz_ctrl_zoom                          //镜头放大/缩小9
	Ptz_ctrl_iris                          //光圈放大/缩小10
	Ptz_ctrl_focus                         //镜头聚焦/放焦11
)

type PtzCmdBody added in v1.3.4

type PtzCmdBody struct {
	XMLName  xml.Name `xml:"Control"`
	CmdType  string
	DeviceID string
	SN       string
	PTZCmd   string
}

func (*PtzCmdBody) Decode added in v1.3.4

func (c *PtzCmdBody) Decode(data string) error

func (*PtzCmdBody) String added in v1.3.4

func (c *PtzCmdBody) String() string

type QueryBody added in v1.0.6

type QueryBody struct {
	XMLName  xml.Name
	CmdType  string
	DeviceID string
	SN       string
}

type ReponseBody added in v1.0.6

type ReponseBody struct {
	XMLName    xml.Name `xml:"Reponse"`
	CmdType    string
	DeviceID   string
	SN         string
	SumNum     uint16
	DeviceList []*ChannelInfo `xml:"DeviceList>Item"`
}

type Request

type Request struct {
	*Message
}

Request Request

type Response

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) GetAlgorithm added in v1.0.8

func (w *WwwAuthenticate) GetAlgorithm() string

func (*WwwAuthenticate) GetNonce added in v1.0.8

func (w *WwwAuthenticate) GetNonce() string

func (*WwwAuthenticate) GetRealm added in v1.0.8

func (w *WwwAuthenticate) GetRealm() string

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

type XProtocol added in v1.3.0

type XProtocol string
const (
	X_GB28181 XProtocol = "GB28181"
	X_PMS     XProtocol = "PMS-SIP"
)

Jump to

Keyboard shortcuts

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