From f887d22e3f4d155713455c8cbafd97cadb354312 Mon Sep 17 00:00:00 2001 From: Emir Aganovic Date: Fri, 2 Jun 2023 22:02:31 +0200 Subject: [PATCH] Doing some final tests. Adding max forward dec handling --- client.go | 38 ++++++++++--------- example/proxysip/main.go | 4 ++ .../proxysip/scripts/clients/pjsua-register | 4 +- parser/parser.go | 1 - sip/headers.go | 8 ++++ sip/headers_test.go | 6 +++ 6 files changed, 40 insertions(+), 21 deletions(-) diff --git a/client.go b/client.go index 9a20a4a..a0529a1 100644 --- a/client.go +++ b/client.go @@ -1,6 +1,7 @@ package sipgo import ( + "fmt" "net" "github.com/emiago/sipgo/sip" @@ -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") + } } diff --git a/example/proxysip/main.go b/example/proxysip/main.go index 8f59fe6..fb9768c 100644 --- a/example/proxysip/main.go +++ b/example/proxysip/main.go @@ -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) @@ -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") diff --git a/example/proxysip/scripts/clients/pjsua-register b/example/proxysip/scripts/clients/pjsua-register index c7e056f..0124fdb 100755 --- a/example/proxysip/scripts/clients/pjsua-register +++ b/example/proxysip/scripts/clients/pjsua-register @@ -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 @@ -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 \ diff --git a/parser/parser.go b/parser/parser.go index b4e069d..2ae3a66 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -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) diff --git a/sip/headers.go b/sip/headers.go index cc04fff..8b1957b 100644 --- a/sip/headers.go +++ b/sip/headers.go @@ -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 diff --git a/sip/headers_test.go b/sip/headers_test.go index 7ed7a33..88834f9 100644 --- a/sip/headers_test.go +++ b/sip/headers_test.go @@ -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()) +}