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
mf
go-multiaddr
Commits
c2b139aa
Commit
c2b139aa
authored
Oct 16, 2018
by
vyzo
Committed by
Steven Allen
Oct 18, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test all known unroutable address ranges in IsPublicAddr
parent
2945cfc2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
8 deletions
+27
-8
private.go
private.go
+27
-8
No files found.
private.go
View file @
c2b139aa
...
...
@@ -7,7 +7,6 @@ import (
)
// Private4 and Private6 are well-known private networks
// These are exported to allow overriding for testing
var
Private4
,
Private6
[]
*
net
.
IPNet
var
privateCIDR4
=
[]
string
{
// localhost
...
...
@@ -29,12 +28,32 @@ var privateCIDR6 = []string{
"fe80::/10"
,
}
// Unroutable4 and Unroutable6 are well known unroutable address ranges
var
Unroutable4
,
Unroutable6
[]
*
net
.
IPNet
var
unroutableCIDR4
=
[]
string
{
"0.0.0.0/8"
,
"192.0.0.0/26"
,
"192.0.2.0/24"
,
"192.88.99.0/24"
,
"198.18.0.0/15"
,
"198.51.100.0/24"
,
"203.0.113.0/24"
,
"224.0.0.0/4"
,
"240.0.0.0/4"
,
"255.255.255.255/32"
,
}
var
unroutableCIDR6
=
[]
string
{
"ff00::/8"
,
}
func
init
()
{
Private4
=
parsePrivateCIDR
(
privateCIDR4
)
Private6
=
parsePrivateCIDR
(
privateCIDR6
)
Private4
=
parseCIDR
(
privateCIDR4
)
Private6
=
parseCIDR
(
privateCIDR6
)
Unroutable4
=
parseCIDR
(
unroutableCIDR4
)
Unroutable6
=
parseCIDR
(
unroutableCIDR6
)
}
func
parse
Private
CIDR
(
cidrs
[]
string
)
[]
*
net
.
IPNet
{
func
parseCIDR
(
cidrs
[]
string
)
[]
*
net
.
IPNet
{
ipnets
:=
make
([]
*
net
.
IPNet
,
len
(
cidrs
))
for
i
,
cidr
:=
range
cidrs
{
_
,
ipnet
,
err
:=
net
.
ParseCIDR
(
cidr
)
...
...
@@ -46,22 +65,22 @@ func parsePrivateCIDR(cidrs []string) []*net.IPNet {
return
ipnets
}
// IsPublicAddr retruns true if the IP part of the multiaddr is
not in a private network
// IsPublicAddr retruns true if the IP part of the multiaddr is
a publically routable address
func
IsPublicAddr
(
a
ma
.
Multiaddr
)
bool
{
ip
,
err
:=
a
.
ValueForProtocol
(
ma
.
P_IP4
)
if
err
==
nil
{
return
!
inAddrRange
(
ip
,
Private4
)
return
!
inAddrRange
(
ip
,
Private4
)
&&
!
inAddrRange
(
ip
,
Unroutable4
)
}
ip
,
err
=
a
.
ValueForProtocol
(
ma
.
P_IP6
)
if
err
==
nil
{
return
!
inAddrRange
(
ip
,
Private6
)
return
!
inAddrRange
(
ip
,
Private6
)
&&
!
inAddrRange
(
ip
,
Unroutable6
)
}
return
false
}
// IsPrivateAddr returns true if the IP part of the mutiadr is in a private network
// IsPrivateAddr returns true if the IP part of the mutiad
d
r is in a private network
func
IsPrivateAddr
(
a
ma
.
Multiaddr
)
bool
{
ip
,
err
:=
a
.
ValueForProtocol
(
ma
.
P_IP4
)
if
err
==
nil
{
...
...
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