Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-nat
Commits
a5aa3655
Unverified
Commit
a5aa3655
authored
6 years ago
by
Steven Allen
Committed by
GitHub
6 years ago
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #7 from libp2p/fix/map-usable
only map *usable* addresses
parents
491e7233
0e313162
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
0 deletions
+26
-0
nat.go
nat.go
+26
-0
No files found.
nat.go
View file @
a5aa3655
...
...
@@ -3,6 +3,7 @@ package nat
import
(
"errors"
"fmt"
"net"
"strconv"
"strings"
"sync"
...
...
@@ -125,15 +126,40 @@ func (nat *NAT) NewMapping(maddr ma.Multiaddr) (Mapping, error) {
return
nil
,
fmt
.
Errorf
(
"DialArgs failed on addr: %s"
,
maddr
.
String
())
}
var
ip
net
.
IP
switch
network
{
case
"tcp"
,
"tcp4"
,
"tcp6"
:
addr
,
err
:=
net
.
ResolveTCPAddr
(
network
,
addr
)
if
err
!=
nil
{
return
nil
,
err
}
ip
=
addr
.
IP
network
=
"tcp"
case
"udp"
,
"udp4"
,
"udp6"
:
addr
,
err
:=
net
.
ResolveUDPAddr
(
network
,
addr
)
if
err
!=
nil
{
return
nil
,
err
}
ip
=
addr
.
IP
network
=
"udp"
default
:
return
nil
,
fmt
.
Errorf
(
"transport not supported by NAT: %s"
,
network
)
}
// XXX: Known limitation: doesn't handle multiple internal addresses.
// If this applies to you, you can figure it out yourself. Ideally, the
// NAT library would allow us to handle this case but the "go way"
// appears to be to just "shrug" at edge-cases.
if
!
ip
.
IsUnspecified
()
{
internalAddr
,
err
:=
nat
.
nat
.
GetInternalAddress
()
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to discover address on nat: %s"
,
err
)
}
if
!
ip
.
Equal
(
internalAddr
)
{
return
nil
,
fmt
.
Errorf
(
"nat address is %s, refusing to map %s"
,
internalAddr
,
ip
)
}
}
intports
:=
strings
.
Split
(
addr
,
":"
)[
1
]
intport
,
err
:=
strconv
.
Atoi
(
intports
)
if
err
!=
nil
{
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment