Skip to content

Commit

Permalink
feat: remove NewAckRequest and handle better transaction ACK request
Browse files Browse the repository at this point in the history
  • Loading branch information
emiago committed Dec 16, 2024
1 parent 184adeb commit 27a286e
Showing 1 changed file with 18 additions and 36 deletions.
54 changes: 18 additions & 36 deletions sip/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,21 +214,21 @@ func (req *Request) Destination() string {
return fmt.Sprintf("%v:%v", host, port)
}

// NewAckRequest creates ACK request for 2xx INVITE
// https://tools.ietf.org/html/rfc3261#section-13.2.2.4
// NOTE: it does not copy Via header. This is left to transport or caller to enforce
// Deprecated: use DialogClient for building dialogs
func NewAckRequest(inviteRequest *Request, inviteResponse *Response, body []byte) *Request {
Recipient := &inviteRequest.Recipient
if contact := inviteResponse.Contact(); contact != nil {
Recipient = &contact.Address
}
// newAckRequestNon2xx follows rules as here. This is not dialog ACK instead it is transaction ACK.
// https://datatracker.ietf.org/doc/html/rfc3261#section-17.1.1.3
func newAckRequestNon2xx(inviteRequest *Request, inviteResponse *Response, body []byte) *Request {
recipient := &inviteRequest.Recipient
ackRequest := NewRequest(
ACK,
*Recipient.Clone(),
*recipient.Clone(),
)
ackRequest.SipVersion = inviteRequest.SipVersion

// The ACK MUST contain a single Via header field, and
// this MUST be equal to the top Via header field of the original
// request.
CopyHeaders("Via", inviteRequest, ackRequest)

if len(inviteRequest.GetHeaders("Route")) > 0 {
CopyHeaders("Route", inviteRequest, ackRequest)
} else {
Expand Down Expand Up @@ -258,42 +258,24 @@ func NewAckRequest(inviteRequest *Request, inviteResponse *Response, body []byte
ackRequest.AppendHeader(h.headerClone())
}

// Seq header field in the ACK MUST contain the same
// value for the sequence number as was present in the original request,
// but the method parameter MUST be equal to "ACK"
cseq := ackRequest.CSeq()
cseq.MethodName = ACK

/*
A UAC SHOULD include a Contact header field in any target refresh
requests within a dialog, and unless there is a need to change it,
the URI SHOULD be the same as used in previous requests within the
dialog. If the "secure" flag is true, that URI MUST be a SIPS URI.
As discussed in Section 12.2.2, a Contact header field in a target
refresh request updates the remote target URI. This allows a UA to
provide a new contact address, should its address change during the
duration of the dialog.
*/

if h := inviteRequest.Contact(); h != nil {
ackRequest.AppendHeader(h.headerClone())
}

ackRequest.SetBody(body)
ackRequest.SetTransport(inviteRequest.Transport())
ackRequest.SetSource(inviteRequest.Source())
// NOTE forcing here destination will be wrong if we have custom Route handling by proxies
// ackRequest.SetDestination(inviteRequest.Destination())

return ackRequest
}

func newAckRequestNon2xx(inviteRequest *Request, inviteResponse *Response, body []byte) *Request {
ackRequest := NewAckRequest(inviteRequest, inviteResponse, body)

CopyHeaders("Via", inviteRequest, ackRequest)
if inviteResponse.IsSuccess() {
// update branch, 2xx ACK is separate Tx
viaHop := ackRequest.Via()
viaHop.Params.Add("branch", GenerateBranch())
}
// if inviteResponse.IsSuccess() {
// // update branch, 2xx ACK is separate Tx
// viaHop := ackRequest.Via()
// viaHop.Params.Add("branch", GenerateBranch())
// }
return ackRequest
}

Expand Down

0 comments on commit 27a286e

Please sign in to comment.