Skip to content

Commit

Permalink
fix: use real port to request and add go.mod
Browse files Browse the repository at this point in the history
  • Loading branch information
chaslin authored and jart committed Jun 27, 2020
1 parent b025f16 commit 89db244
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
*.test
/fone/fone
.*
/sftp-config.json
/main
/main.go
/main_test.go
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/jart/gosip

go 1.14
Empty file added go.sum
Empty file.
20 changes: 15 additions & 5 deletions sip/receiver.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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() {
Expand Down
23 changes: 19 additions & 4 deletions sip/route.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -16,9 +16,11 @@ package sip

import (
"errors"
"github.com/jart/gosip/util"
"log"
"net"
"strconv"

"github.com/jart/gosip/util"
)

type AddressRoute struct {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 89db244

Please sign in to comment.