From 89db24411fbd46649c0eb02f58c34be225422b72 Mon Sep 17 00:00:00 2001 From: chaslin Date: Sat, 27 Jun 2020 23:36:49 +0800 Subject: [PATCH] fix: use real port to request and add go.mod --- .gitignore | 5 +++++ go.mod | 3 +++ go.sum | 0 sip/receiver.go | 20 +++++++++++++++----- sip/route.go | 23 +++++++++++++++++++---- 5 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/.gitignore b/.gitignore index d8ad1dd..ee0a5ac 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,7 @@ *.test /fone/fone +.* +/sftp-config.json +/main +/main.go +/main_test.go \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ceb4bb0 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/jart/gosip + +go 1.14 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/sip/receiver.go b/sip/receiver.go index 8160ad9..f0d8080 100644 --- a/sip/receiver.go +++ b/sip/receiver.go @@ -1,11 +1,11 @@ // Copyright 2020 Justine Alexandra Roberts Tunney -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -59,8 +59,18 @@ func addReceived(msg *Msg, addr *net.UDPAddr) { return } if int(msg.Via.Port) != addr.Port { - if msg.Via.Param.Get("rport") == nil { - msg.Via.Param = &Param{"rport", string(addr.Port), msg.Via.Param} + + rport := msg.Via.Param.Get("rport") + port := strconv.Itoa(addr.Port) + + if rport == nil { + msg.Via.Param = &Param{"rport", port, msg.Via.Param} + } else { + + // implied rport is 5060, but some NAT will use another port,we use real port instead + if len(rport.Value) == 0 { + rport.Value = port + } } } if msg.Via.Host != addr.IP.String() { diff --git a/sip/route.go b/sip/route.go index feb696b..b414370 100644 --- a/sip/route.go +++ b/sip/route.go @@ -1,11 +1,11 @@ // Copyright 2020 Justine Alexandra Roberts Tunney -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -16,9 +16,11 @@ package sip import ( "errors" - "github.com/jart/gosip/util" "log" "net" + "strconv" + + "github.com/jart/gosip/util" ) type AddressRoute struct { @@ -70,10 +72,23 @@ func RouteMessage(via *Via, contact *Addr, msg *Msg) (host string, port uint16, if via.CompareHostPort(msg.Via) { msg.Via = msg.Via.Next } + host, port = msg.Via.Host, msg.Via.Port if received := msg.Via.Param.Get("received"); received != nil { host = received.Value } + + // fix: Get real port from rport field. + // Request path like UAC->NAT->UAS will change port(according to NAT type) sometime, + // we should use rport as real port in request-line + if rport := msg.Via.Param.Get("rport"); rport != nil && len(rport.Value) > 0 { + + i, err := strconv.ParseInt(rport.Value, 10, 16) + if err != nil { + return "", 0, err + } + port = uint16(i) + } } else { if contact.CompareHostPort(msg.Route) { msg.Route = msg.Route.Next