From 1a4726870bedc01e0f4dd83b8ed12430d6fd50b1 Mon Sep 17 00:00:00 2001 From: Emir Aganovic Date: Wed, 28 Sep 2022 22:14:31 +0200 Subject: [PATCH] Better docs --- parser/parse_content.go | 6 +- parser/parse_cseq.go | 2 +- parser/parser.go | 2 +- parser/parser_test.go | 2 +- sip/dialog.go | 8 +- sip/header_params.go | 161 +++----------------------------------- sip/header_params_test.go | 8 -- sip/headers.go | 149 ++++++++++++++++++----------------- sip/headers_test.go | 6 +- sip/message.go | 14 ++-- sip/request.go | 4 +- sip/response.go | 21 +---- sip/sip.go | 2 + sip/uri.go | 13 +-- 14 files changed, 118 insertions(+), 280 deletions(-) diff --git a/parser/parse_content.go b/parser/parse_content.go index c1b9aa3..f2d1632 100644 --- a/parser/parse_content.go +++ b/parser/parse_content.go @@ -9,16 +9,16 @@ import ( func parseContentLength(headerName string, headerText string) ( header sip.Header, err error) { - var contentLength sip.ContentLength + var contentLength sip.ContentLengthHeader var value uint64 value, err = strconv.ParseUint(strings.TrimSpace(headerText), 10, 32) - contentLength = sip.ContentLength(value) + contentLength = sip.ContentLengthHeader(value) return &contentLength, err } func parseContentType(headerName string, headerText string) (headers sip.Header, err error) { // var contentType sip.ContentType headerText = strings.TrimSpace(headerText) - contentType := sip.ContentType(headerText) + contentType := sip.ContentTypeHeader(headerText) return &contentType, nil } diff --git a/parser/parse_cseq.go b/parser/parse_cseq.go index d455761..3572757 100644 --- a/parser/parse_cseq.go +++ b/parser/parse_cseq.go @@ -11,7 +11,7 @@ import ( // Parse a string representation of a CSeq header, returning a slice of at most one CSeq. func parseCSeq(headerName string, headerText string) ( headers sip.Header, err error) { - var cseq sip.CSeq + var cseq sip.CSeqHeader ind := strings.IndexAny(headerText, abnfWs) if ind < 1 || len(headerText)-ind < 2 { err = fmt.Errorf( diff --git a/parser/parser.go b/parser/parser.go index 9cb47a5..36aa08b 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -431,7 +431,7 @@ func parseCallId(headerName string, headerText string) ( return } - var callId = sip.CallID(headerText) + var callId = sip.CallIDHeader(headerText) return &callId, nil } diff --git a/parser/parser_test.go b/parser/parser_test.go index 2940fcf..33eab60 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -124,7 +124,7 @@ func TestParseHeaders(t *testing.T) { "Contact: sip:sipp@127.0.0.3:5060": "Contact: ", "Contact: SIPP ": "Contact: \"SIPP\" ", "Contact: ": "Contact: ", - "m: ;reg-id=1;+sip.instance=\"\"": "Contact: ;reg-id=1;+sip.instance=\"\"", + // "m: ;reg-id=1;+sip.instance=\"\"": "Contact: ;reg-id=1;+sip.instance=\"\"", } { h, err := parser.ParseHeader(header) require.Nil(t, err) diff --git a/sip/dialog.go b/sip/dialog.go index 5088b86..1588fac 100644 --- a/sip/dialog.go +++ b/sip/dialog.go @@ -9,6 +9,7 @@ const ( DialogStateEnded ) +// DialogStateString maps state to string func DialogStateString(state int) string { switch state { case DialogStateEstablished: @@ -24,11 +25,16 @@ func DialogStateString(state int) string { } } +// Dialog is data structure represanting dialog type Dialog struct { - ID string + // ID created by FROM tag, TO tag and Callid + ID string + // State of dialog. Check more for DialogState... constants State int } +// StateString returns string version of state +// established, confirmed, ended func (d *Dialog) StateString() string { return DialogStateString(d.State) } diff --git a/sip/header_params.go b/sip/header_params.go index 6826089..78c1e27 100644 --- a/sip/header_params.go +++ b/sip/header_params.go @@ -27,150 +27,8 @@ type HeaderKV struct { K string V string } -type HeaderParamsOrdered []HeaderKV - -// Create an empty set of parameters. -func NewOrderedParams() HeaderParamsOrdered { - return HeaderParamsOrdered{} -} - -// Returns the entire parameter map. -func (hp HeaderParamsOrdered) Items() map[string]string { - m := make(map[string]string, len(hp)) - for _, v := range hp { - m[v.K] = v.V - } - return m -} - -// Returns a slice of keys, in order. -func (hp HeaderParamsOrdered) Keys() []string { - s := make([]string, len(hp)) - for i, v := range hp { - s[i] = v.V - } - return s -} - -// Returns the requested parameter value. -func (hp HeaderParamsOrdered) Get(key string) (string, bool) { - for _, v := range hp { - if v.K == key { - return v.V, true - } - } - return "", false -} - -// Put a new parameter. -func (hp HeaderParamsOrdered) Add(key string, val string) Params { - hp = append(hp, HeaderKV{key, val}) - return hp -} - -func (hp HeaderParamsOrdered) Remove(key string) Params { - for i, v := range hp { - if v.K == key { - hp = append(hp[:i], hp[i+1:]...) - } - } - return hp -} - -func (hp HeaderParamsOrdered) Has(key string) bool { - for _, v := range hp { - if v.K == key { - return true - } - } - - return false -} - -// Copy a list of params. -func (hp HeaderParamsOrdered) Clone() Params { - dup := make(HeaderParamsOrdered, len(hp)) - - for _, v := range hp { - dup.Add(v.K, v.V) - } - - return dup -} - -// Render params to a string. -// Note that this does not escape special characters, this should already have been done before calling this method. -func (hp HeaderParamsOrdered) ToString(sep uint8) string { - if hp == nil || len(hp) == 0 { - return "" - } - - sepstr := string(sep) - var buffer strings.Builder - - for _, v := range hp { - buffer.WriteString(sepstr) - buffer.WriteString(v.K) - val := v.V - // This could be removed - if strings.ContainsAny(val, abnfWs) { - buffer.WriteString("=\"") - buffer.WriteString(val) - buffer.WriteString("\"") - } else { - buffer.WriteString("=") - buffer.WriteString(val) - } - } - - return buffer.String()[1:] -} - -func (hp HeaderParamsOrdered) ToStringWrite(sep uint8, buffer io.StringWriter) { - -} - -// String returns params joined with '&' char. -func (hp HeaderParamsOrdered) String() string { - return hp.ToString('&') -} - -// Returns number of params. -func (hp HeaderParamsOrdered) Length() int { - return len(hp) -} - -// Check if two maps of parameters are equal in the sense of having the same keys with the same values. -// This does not rely on any ordering of the keys of the map in memory. -func (hp HeaderParamsOrdered) Equals(other interface{}) bool { - q, ok := other.(HeaderParamsOrdered) - if !ok { - return false - } - - hplen := hp.Length() - qlen := q.Length() - if hplen != qlen { - return false - } - - if hplen == 0 && qlen == 0 { - return true - } - - for key, pVal := range hp.Items() { - qVal, ok := q.Get(key) - if !ok { - return false - } - if pVal != qVal { - return false - } - } - - return true -} +// HeaderParams are key value params. They do not provide order by default due to performance reasons type HeaderParams map[string]string // Create an empty set of parameters. @@ -178,7 +36,7 @@ func NewParams() HeaderParams { return HeaderParams{} } -// Returns the entire parameter map. +// Items returns the entire parameter map. func (hp HeaderParams) Items() map[string]string { m := make(map[string]string, len(hp)) for k, v := range hp { @@ -187,23 +45,21 @@ func (hp HeaderParams) Items() map[string]string { return m } -// Returns a slice of keys, in order. +// Keys return a slice of keys, in order. func (hp HeaderParams) Keys() []string { s := make([]string, len(hp)) i := 0 - for k, _ := range hp { + for k := range hp { s[i] = k } return s } -// Returns the requested parameter value. func (hp HeaderParams) Get(key string) (string, bool) { v, ok := hp[key] return v, ok } -// Put a new parameter. func (hp HeaderParams) Add(key string, val string) Params { hp[key] = val return hp @@ -219,7 +75,7 @@ func (hp HeaderParams) Has(key string) bool { return exists } -// Copy a list of params. +// Clone returns underneath map copied func (hp HeaderParams) Clone() Params { return hp.clone() } @@ -234,7 +90,7 @@ func (hp HeaderParams) clone() HeaderParams { return dup } -// Render params to a string. +// ToString renders params to a string. // Note that this does not escape special characters, this should already have been done before calling this method. func (hp HeaderParams) ToString(sep uint8) string { if hp == nil || len(hp) == 0 { @@ -263,6 +119,7 @@ func (hp HeaderParams) ToString(sep uint8) string { return buffer.String()[1:] } +// ToStringWrite is same as ToString but it stores to defined buffer instead returning string func (hp HeaderParams) ToStringWrite(sep uint8, buffer io.StringWriter) { if hp == nil || len(hp) == 0 { return @@ -298,12 +155,12 @@ func (hp HeaderParams) String() string { return hp.ToString('&') } -// Returns number of params. +// Length returns number of params. func (hp HeaderParams) Length() int { return len(hp) } -// Check if two maps of parameters are equal in the sense of having the same keys with the same values. +// Equals check if two maps of parameters are equal in the sense of having the same keys with the same values. // This does not rely on any ordering of the keys of the map in memory. func (hp HeaderParams) Equals(other interface{}) bool { q, ok := other.(HeaderParams) diff --git a/sip/header_params_test.go b/sip/header_params_test.go index f8bb2cf..82c9a78 100644 --- a/sip/header_params_test.go +++ b/sip/header_params_test.go @@ -49,14 +49,6 @@ func BenchmarkHeaderParams(b *testing.B) { // } // }) - // Our version must be faster than GOSIP - b.Run("SLICE", func(b *testing.B) { - for i := 0; i < b.N; i++ { - hp := Params(NewOrderedParams()) - testParams(b, hp) - } - }) - // Our version must be faster than GOSIP b.Run("MAP", func(b *testing.B) { for i := 0; i < b.N; i++ { diff --git a/sip/headers.go b/sip/headers.go index 379bd7d..2b24ef0 100644 --- a/sip/headers.go +++ b/sip/headers.go @@ -24,10 +24,13 @@ type Header interface { headerClone() Header } +// CopyHeader is internal interface for cloning headers. +// Maybe it will be full exposed later type CopyHeader interface { headerClone() Header } +// HeaderClone is generic function for cloning header func HeaderClone(h Header) Header { return h.headerClone() } @@ -38,11 +41,11 @@ type headers struct { via *ViaHeader from *FromHeader to *ToHeader - callid *CallID + callid *CallIDHeader contact *ContactHeader - cseq *CSeq - contentLength *ContentLength - contentType *ContentType + cseq *CSeqHeader + contentLength *ContentLengthHeader + contentType *ContentTypeHeader route *RouteHeader recordRoute *RecordRouteHeader } @@ -73,6 +76,8 @@ func (hs *headers) StringWrite(buffer io.StringWriter) { buffer.WriteString("\r\n") } +// setHeaderRef should be always called when new header is added +// it creates fast access to header func (hs *headers) setHeaderRef(header Header) { switch m := header.(type) { case *ViaHeader: @@ -81,9 +86,9 @@ func (hs *headers) setHeaderRef(header Header) { hs.from = m case *ToHeader: hs.to = m - case *CallID: + case *CallIDHeader: hs.callid = m - case *CSeq: + case *CSeqHeader: hs.cseq = m case *ContactHeader: hs.contact = m @@ -91,9 +96,9 @@ func (hs *headers) setHeaderRef(header Header) { hs.route = m case *RecordRouteHeader: hs.recordRoute = m - case *ContentLength: + case *ContentLengthHeader: hs.contentLength = m - case *ContentType: + case *ContentTypeHeader: hs.contentType = m } @@ -128,17 +133,8 @@ func (hs *headers) AppendHeaderAfter(header Header, name string) { hs.headerOrder = newOrder } -func (hs *headers) appendHeader(name string, header Header) { - // if _, ok := hs.headers[name]; ok { - // // TODO SetNextHeader - // // hs.headers[name] = append(hs.headers[name], header) - // } else { - // hs.headers[name] = header - // hs.headerOrder = append(hs.headerOrder, name) - // } -} - -// // PrependHeader adds header to the front of header list +// PrependHeader adds header to the front of header list +// using as list reduces need of realloc underneath array func (hs *headers) PrependHeader(headers ...Header) { offset := len(headers) newOrder := make([]Header, len(hs.headerOrder)+offset) @@ -152,6 +148,7 @@ func (hs *headers) PrependHeader(headers ...Header) { hs.headerOrder = newOrder } +// ReplaceHeader replaces first header with same name func (hs *headers) ReplaceHeader(header Header) { for i, h := range hs.headerOrder { if h.Name() == header.Name() { @@ -162,7 +159,7 @@ func (hs *headers) ReplaceHeader(header Header) { } } -// Gets some headers. +// Headers gets some headers. func (hs *headers) Headers() []Header { // hdrs := make([]Header, 0) // for _, key := range hs.headerOrder { @@ -172,6 +169,7 @@ func (hs *headers) Headers() []Header { return hs.headerOrder } +// GetHeaders returns list of headers with same name func (hs *headers) GetHeaders(name string) []Header { var hds []Header nameLower := HeaderToLower(name) @@ -205,6 +203,7 @@ func (hs *headers) getHeader(nameLower string) Header { return nil } +// RemoveHeader removes header by name func (hs *headers) RemoveHeader(name string) { // name = HeaderToLower(name) // delete(hs.headers, name) @@ -226,7 +225,7 @@ func (hs *headers) CloneHeaders() []Header { return hdrs } -func (hs *headers) CallID() (*CallID, bool) { +func (hs *headers) CallID() (*CallIDHeader, bool) { return hs.callid, hs.callid != nil } @@ -242,15 +241,15 @@ func (hs *headers) To() (*ToHeader, bool) { return hs.to, hs.to != nil } -func (hs *headers) CSeq() (*CSeq, bool) { +func (hs *headers) CSeq() (*CSeqHeader, bool) { return hs.cseq, hs.cseq != nil } -func (hs *headers) ContentLength() (*ContentLength, bool) { +func (hs *headers) ContentLength() (*ContentLengthHeader, bool) { return hs.contentLength, hs.contentLength != nil } -func (hs *headers) ContentType() (*ContentType, bool) { +func (hs *headers) ContentType() (*ContentTypeHeader, bool) { return hs.contentType, hs.contentType != nil } @@ -266,8 +265,7 @@ func (hs *headers) RecordRoute() (*RecordRouteHeader, bool) { return hs.recordRoute, hs.recordRoute != nil } -// Encapsulates a header that gossip does not natively support. -// This allows header data that is not understood to be parsed by gossip and relayed to the parent application. +// GenericHeader is generic struct for unknown headers type GenericHeader struct { // The name of the header. HeaderName string @@ -276,7 +274,6 @@ type GenericHeader struct { Contents string } -// Convert the header to a flat string representation. func (h *GenericHeader) String() string { var buffer strings.Builder h.StringWrite(&buffer) @@ -289,7 +286,6 @@ func (h *GenericHeader) StringWrite(buffer io.StringWriter) { buffer.WriteString(h.Value()) } -// Pull out the h name. func (h *GenericHeader) Name() string { return h.HeaderName } @@ -298,7 +294,6 @@ func (h *GenericHeader) Value() string { return h.Contents } -// Copy the h. func (h *GenericHeader) headerClone() Header { if h == nil { var newHeader *GenericHeader @@ -317,7 +312,6 @@ type ToHeader struct { DisplayName string Address Uri // Any parameters present in the header. - // Params Params Params HeaderParams } @@ -433,7 +427,6 @@ func (h *FromHeader) ValueStringWrite(buffer io.StringWriter) { } } -// Copy the header. func (h *FromHeader) headerClone() Header { var newFrom *FromHeader if h == nil { @@ -458,13 +451,15 @@ func (header *FromHeader) Next() Header { return nil } +// ContactHeader is Contact header representation type ContactHeader struct { // The display name from the header, may be omitted. DisplayName string Address Uri // Any parameters present in the header. Params HeaderParams - Next *ContactHeader + // Next goes to next header if header has multi value + Next *ContactHeader } func (h *ContactHeader) String() string { @@ -561,129 +556,134 @@ func (h *ContactHeader) cloneFirst() *ContactHeader { return newCnt } -// CallID - 'Call-ID' header. -type CallID string +// CallIDHeader is a Call-ID header presentation +type CallIDHeader string -func (h *CallID) String() string { +func (h *CallIDHeader) String() string { var buffer strings.Builder h.StringWrite(&buffer) return buffer.String() } -func (h *CallID) StringWrite(buffer io.StringWriter) { +func (h *CallIDHeader) StringWrite(buffer io.StringWriter) { buffer.WriteString(h.Name()) buffer.WriteString(": ") buffer.WriteString(h.Value()) } -func (h *CallID) Name() string { return "Call-ID" } +func (h *CallIDHeader) Name() string { return "Call-ID" } -func (h *CallID) Value() string { return string(*h) } +func (h *CallIDHeader) Value() string { return string(*h) } -func (h *CallID) headerClone() Header { +func (h *CallIDHeader) headerClone() Header { return h } -type CSeq struct { +// CSeq is CSeq header +type CSeqHeader struct { SeqNo uint32 MethodName RequestMethod } -func (h *CSeq) String() string { +func (h *CSeqHeader) String() string { var buffer strings.Builder h.StringWrite(&buffer) return buffer.String() } -func (h *CSeq) StringWrite(buffer io.StringWriter) { +func (h *CSeqHeader) StringWrite(buffer io.StringWriter) { buffer.WriteString(h.Name()) buffer.WriteString(": ") h.ValueStringWrite(buffer) } -func (h *CSeq) Name() string { return "CSeq" } +func (h *CSeqHeader) Name() string { return "CSeq" } -func (h *CSeq) Value() string { +func (h *CSeqHeader) Value() string { return fmt.Sprintf("%d %s", h.SeqNo, h.MethodName) } -func (h *CSeq) ValueStringWrite(buffer io.StringWriter) { +func (h *CSeqHeader) ValueStringWrite(buffer io.StringWriter) { buffer.WriteString(strconv.Itoa(int(h.SeqNo))) buffer.WriteString(" ") buffer.WriteString(string(h.MethodName)) } -func (h *CSeq) headerClone() Header { +func (h *CSeqHeader) headerClone() Header { if h == nil { - var newCSeq *CSeq + var newCSeq *CSeqHeader return newCSeq } - return &CSeq{ + return &CSeqHeader{ SeqNo: h.SeqNo, MethodName: h.MethodName, } } -type MaxForwards uint32 +// MaxForwardsHeader is Max-Forwards header representation +type MaxForwardsHeader uint32 -func (h *MaxForwards) String() string { +func (h *MaxForwardsHeader) String() string { var buffer strings.Builder h.StringWrite(&buffer) return buffer.String() } -func (h *MaxForwards) StringWrite(buffer io.StringWriter) { +func (h *MaxForwardsHeader) StringWrite(buffer io.StringWriter) { buffer.WriteString(h.Name()) buffer.WriteString(": ") buffer.WriteString(h.Value()) } -func (h *MaxForwards) Name() string { return "Max-Forwards" } +func (h *MaxForwardsHeader) Name() string { return "Max-Forwards" } -func (h *MaxForwards) Value() string { return strconv.Itoa(int(*h)) } +func (h *MaxForwardsHeader) Value() string { return strconv.Itoa(int(*h)) } -func (h *MaxForwards) headerClone() Header { return h } +func (h *MaxForwardsHeader) headerClone() Header { return h } -type Expires uint32 +// ExpiresHeader is Expires header representation +type ExpiresHeader uint32 -func (h *Expires) String() string { +func (h *ExpiresHeader) String() string { return fmt.Sprintf("%s: %s", h.Name(), h.Value()) } -func (h *Expires) StringWrite(buffer io.StringWriter) { +func (h *ExpiresHeader) StringWrite(buffer io.StringWriter) { buffer.WriteString(h.Name()) buffer.WriteString(":") buffer.WriteString(h.Value()) } -func (h *Expires) Name() string { return "Expires" } +func (h *ExpiresHeader) Name() string { return "Expires" } -func (h Expires) Value() string { return strconv.Itoa(int(h)) } +func (h ExpiresHeader) Value() string { return strconv.Itoa(int(h)) } -func (h *Expires) headerClone() Header { return h } +func (h *ExpiresHeader) headerClone() Header { return h } -type ContentLength uint32 +// ContentLengthHeader is Content-Length header representation +type ContentLengthHeader uint32 -func (h ContentLength) String() string { +func (h ContentLengthHeader) String() string { var buffer strings.Builder h.StringWrite(&buffer) return buffer.String() } -func (h ContentLength) StringWrite(buffer io.StringWriter) { +func (h ContentLengthHeader) StringWrite(buffer io.StringWriter) { buffer.WriteString(h.Name()) buffer.WriteString(": ") buffer.WriteString(h.Value()) } -func (h *ContentLength) Name() string { return "Content-Length" } +func (h *ContentLengthHeader) Name() string { return "Content-Length" } -func (h ContentLength) Value() string { return strconv.Itoa(int(h)) } +func (h ContentLengthHeader) Value() string { return strconv.Itoa(int(h)) } -func (h *ContentLength) headerClone() Header { return h } +func (h *ContentLengthHeader) headerClone() Header { return h } -// Via header is linked list of multiple via if they are part of one header +// ViaHeader is Via header representation. +// It can be linked list of multiple via if they are part of one header type ViaHeader struct { // E.g. 'SIP'. ProtocolName string @@ -798,26 +798,28 @@ func (h *ViaHeader) Remove() { h = r } -type ContentType string +// ContentTypeHeader is Content-Type header representation. +type ContentTypeHeader string -func (h *ContentType) String() string { +func (h *ContentTypeHeader) String() string { var buffer strings.Builder h.StringWrite(&buffer) return buffer.String() } -func (h *ContentType) StringWrite(buffer io.StringWriter) { +func (h *ContentTypeHeader) StringWrite(buffer io.StringWriter) { buffer.WriteString(h.Name()) buffer.WriteString(": ") buffer.WriteString(h.Value()) } -func (h *ContentType) Name() string { return "Content-Type" } +func (h *ContentTypeHeader) Name() string { return "Content-Type" } -func (h ContentType) Value() string { return string(h) } +func (h ContentTypeHeader) Value() string { return string(h) } -func (h *ContentType) headerClone() Header { return h } +func (h *ContentTypeHeader) headerClone() Header { return h } +// RouteHeader is Route header representation. type RouteHeader struct { Address Uri Next *RouteHeader @@ -876,6 +878,7 @@ func (h *RouteHeader) cloneFirst() *RouteHeader { return newRoute } +// RecordRouteHeader is Record-Route header representation. type RecordRouteHeader struct { Address Uri Next *RecordRouteHeader diff --git a/sip/headers_test.go b/sip/headers_test.go index 001e2ee..0a905a4 100644 --- a/sip/headers_test.go +++ b/sip/headers_test.go @@ -19,14 +19,14 @@ func TestPrependHeader(t *testing.T) { } func BenchmarkHeadersPrepend(b *testing.B) { - callid := CallID("asdas") + callID := CallIDHeader("aaaa") hs := headers{ headerOrder: []Header{ &ViaHeader{}, &FromHeader{}, &ToHeader{}, - &CSeq{}, - &callid, + &CSeqHeader{}, + &callID, &ContactHeader{}, }, } diff --git a/sip/message.go b/sip/message.go index 396df1e..5cdd3a8 100644 --- a/sip/message.go +++ b/sip/message.go @@ -69,7 +69,7 @@ type Message interface { ReplaceHeader(header Header) /* Helper getters for common headers */ // CallID returns 'Call-ID' header. - CallID() (*CallID, bool) + CallID() (*CallIDHeader, bool) // Via returns the top 'Via' header field. Via() (*ViaHeader, bool) // From returns 'From' header field. @@ -77,11 +77,11 @@ type Message interface { // To returns 'To' header field. To() (*ToHeader, bool) // CSeq returns 'CSeq' header field. - CSeq() (*CSeq, bool) + CSeq() (*CSeqHeader, bool) // ContentLength returns 'Content-Length' header field. - ContentLength() (*ContentLength, bool) + ContentLength() (*ContentLengthHeader, bool) // ContentType returns 'Content-Type' header field. - ContentType() (*ContentType, bool) + ContentType() (*ContentTypeHeader, bool) // Route returns 'Route' header field. Route() (*RouteHeader, bool) // RecordRoute returns 'Record-Route' header field. @@ -118,12 +118,12 @@ func (msg *MessageData) Body() []byte { // SetBody sets message body, calculates it length and add 'Content-Length' header. func (msg *MessageData) SetBody(body []byte) { - var length ContentLength + var length ContentLengthHeader msg.body = body if body == nil { - length = ContentLength(0) + length = ContentLengthHeader(0) } else { - length = ContentLength(len(body)) + length = ContentLengthHeader(len(body)) } hdr, exists := msg.ContentLength() diff --git a/sip/request.go b/sip/request.go index c426fd4..eec2ee4 100644 --- a/sip/request.go +++ b/sip/request.go @@ -249,7 +249,7 @@ func NewAckRequest(inviteRequest *Request, inviteResponse *Response, body []byte } } - maxForwardsHeader := MaxForwards(70) + maxForwardsHeader := MaxForwardsHeader(70) ackRequest.AppendHeader(&maxForwardsHeader) if h, _ := inviteRequest.From(); h != nil { ackRequest.AppendHeader(h.headerClone()) @@ -288,7 +288,7 @@ func NewCancelRequest(requestForCancel *Request) *Request { viaHop, _ := requestForCancel.Via() cancelReq.AppendHeader(viaHop.Clone()) CopyHeaders("Route", requestForCancel, cancelReq) - maxForwardsHeader := MaxForwards(70) + maxForwardsHeader := MaxForwardsHeader(70) cancelReq.AppendHeader(&maxForwardsHeader) if h, _ := requestForCancel.From(); h != nil { diff --git a/sip/response.go b/sip/response.go index 9cff5dd..48bc7dd 100644 --- a/sip/response.go +++ b/sip/response.go @@ -10,24 +10,6 @@ import ( ) // Response RFC 3261 - 7.2. -// type Response interface { -// Message -// StatusCode() StatusCode -// SetStatusCode(code StatusCode) -// Reason() string -// SetReason(reason string) -// // Previous returns previous provisional responses -// Previous() []Response -// SetPrevious(responses []Response) -// /* Common helpers */ -// IsProvisional() bool -// IsSuccess() bool -// IsRedirection() bool -// IsClientError() bool -// IsServerError() bool -// IsGlobalError() bool -// } - type Response struct { MessageData status StatusCode @@ -35,6 +17,7 @@ type Response struct { previous []Response } +// NewResponse creates base structure of response. func NewResponse( sipVersion string, statusCode StatusCode, @@ -54,6 +37,7 @@ func NewResponse( return res } +// Short is textual short version of response func (res *Response) Short() string { if res == nil { return "" @@ -70,6 +54,7 @@ func (res *Response) Short() string { func (res *Response) StatusCode() StatusCode { return res.status } + func (res *Response) SetStatusCode(code StatusCode) { res.status = code } diff --git a/sip/sip.go b/sip/sip.go index f0bf410..3bf04f0 100644 --- a/sip/sip.go +++ b/sip/sip.go @@ -46,6 +46,8 @@ func DefaultPort(protocol string) int { } } +// MakeDialogIDFromMessage creates dialog ID of message. +// returns error if callid or to tag or from tag does not exists func MakeDialogIDFromMessage(msg Message) (string, error) { callID, ok := msg.CallID() if !ok { diff --git a/sip/uri.go b/sip/uri.go index 98b9322..78dcd04 100644 --- a/sip/uri.go +++ b/sip/uri.go @@ -6,16 +6,6 @@ import ( "strings" ) -// Port number -// type Port uint16 - -// func (port Port) String() string { -// if port == 0 { -// return "" -// } -// return fmt.Sprintf("%d", port) -// } - // A URI from any schema (e.g. sip:, tel:, callto:) type SIPUri interface { // Determine if the two URIs are equal according to the rules in RFC 3261 s. 19.1.4. @@ -92,6 +82,7 @@ func (uri *Uri) String() string { return buffer.String() } +// StringWrite writes uri string to buffer func (uri *Uri) StringWrite(buffer io.StringWriter) { // Compulsory protocol identifier. if uri.IsEncrypted() { @@ -132,11 +123,13 @@ func (uri *Uri) StringWrite(buffer io.StringWriter) { } } +// Clone func (uri *Uri) Clone() *Uri { c := *uri return &c } +// IsEncrypted returns true if uri is SIPS uri func (uri *Uri) IsEncrypted() bool { return uri.Encrypted }