Skip to content

Commit

Permalink
Doing some final tests. Adding max forward dec handling
Browse files Browse the repository at this point in the history
  • Loading branch information
emiago committed Jun 2, 2023
1 parent a79c2ee commit f887d22
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 21 deletions.
38 changes: 21 additions & 17 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sipgo

import (
"fmt"
"net"

"github.com/emiago/sipgo/sip"
Expand Down Expand Up @@ -198,29 +199,32 @@ func ClientRequestAddRecordRoute(c *Client, r *sip.Request) error {
return nil
}

// TODO
// Based on proxy setup https://www.rfc-editor.org/rfc/rfc3261#section-16
// ClientRequestDecreaseMaxForward should be used when forwarding request. It decreases max forward
// in case of 0 it returnes error
func ClientRequestDecreaseMaxForward(c *Client, r *sip.Request) error {
// TODO
maxfwd, exists := r.MaxForwards()
if !exists {
// TODO, should we return error here
return nil
}

maxfwd.Dec()

if maxfwd.Val() <= 0 {
return fmt.Errorf("Max forwards reached")
}
return nil
}

// ClientResponseRemoveVia is needed when handling client transaction response, where previously used in
// TransactionRequest with ClientRequestAddVia
func ClientResponseRemoveVia(c *Client, r *sip.Response) {
// via, exists := r.Via()
// if !exists {
// return
// }
// if via.Host == c.host {
// r.RemoveHeader("Via")
// }

r.RemoveHeaderOn("Via", c.removeViaCallback)
}

func (c *Client) removeViaCallback(h sip.Header) bool {
// This is slow
via := h.(*sip.ViaHeader)
return via.Host == c.host
via, exists := r.Via()
if !exists {
return
}
if via.Host == c.host {
r.RemoveHeader("Via")
}
}
4 changes: 4 additions & 0 deletions example/proxysip/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func main() {
transportType := flag.String("t", "udp", "Transport, default will be determined by request")
flag.Parse()

transport.UDPMTUSize = 10000
if *pprof {
runtime.SetBlockProfileRate(1)
runtime.SetMutexProfileFraction(1)
Expand Down Expand Up @@ -226,6 +227,9 @@ func setupSipProxy(proxydst string, ip string) *sipgo.Server {

var ackHandler = func(req *sip.Request, tx sip.ServerTransaction) {
dst := getDestination(req)
if dst == "" {
return
}
req.SetDestination(dst)
if err := client.WriteRequest(req, sipgo.ClientRequestAddVia); err != nil {
log.Error().Err(err).Msg("Send failed")
Expand Down
4 changes: 1 addition & 3 deletions example/proxysip/scripts/clients/pjsua-register
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if [ "${SIP_USERNAME}" == "" ]; then
exit 1
fi

SIP_SERVER_HOST=${SIP_SERVER_HOST:-localhost}
SIP_SERVER_HOST=${SIP_SERVER_HOST:-127.0.0.1}
SIP_SERVER_PORT=5060
SIP_PASSWORD=${SIP_PASSWORD:-asterisk}
# udp, tcp
Expand All @@ -20,11 +20,9 @@ RTP_PORT=$(shuf -i 56001-59999 -n 1)

pjsua \
--log-level=0 \
--app-log-level=0 \
--no-stderr \
--null-audio \
--snd-auto-close=0 \
--max-calls=15 \
--no-vad \
--use-compact-form \
--reg-timeout=$SIP_REGISTER_TIMEOUT \
Expand Down
1 change: 0 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ func (p *Parser) parseMsgHeader(msg sip.Message, headerText string) (err error)
if !ok {
// We have no registered parser for this header type,
// so we encapsulate the header data in a GenericHeader struct.
// p.log.Tracef("no parser for header type %s", fieldName)

// TODO Should we check for comma here as well ??
header := sip.NewHeader(fieldName, fieldText)
Expand Down
8 changes: 8 additions & 0 deletions sip/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,14 @@ func (h *MaxForwardsHeader) Value() string { return strconv.Itoa(int(*h)) }

func (h *MaxForwardsHeader) headerClone() Header { return h }

func (h *MaxForwardsHeader) Dec() {
*h = MaxForwardsHeader(uint32(*h) - 1)
}

func (h MaxForwardsHeader) Val() uint32 {
return uint32(h)
}

// ExpiresHeader is Expires header representation
type ExpiresHeader uint32

Expand Down
6 changes: 6 additions & 0 deletions sip/headers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ func BenchmarkHeadersPrepend(b *testing.B) {
hs.headerOrder = newOrder
})
}

func TestMaxForwardIncDec(t *testing.T) {
maxfwd := MaxForwardsHeader(70)
maxfwd.Dec()
assert.Equal(t, uint32(69), maxfwd.Val(), "Value returned %d", maxfwd.Val())
}

0 comments on commit f887d22

Please sign in to comment.