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-asn-util
Commits
38b74d19
Commit
38b74d19
authored
Aug 25, 2020
by
Adin Schmahmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: use deferred initialization of the asnStore
parent
1b967eb4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
6 deletions
+32
-6
asn.go
asn.go
+32
-6
No files found.
asn.go
View file @
38b74d19
...
@@ -8,14 +8,10 @@ import (
...
@@ -8,14 +8,10 @@ import (
"github.com/libp2p/go-cidranger"
"github.com/libp2p/go-cidranger"
)
)
var
Store
*
a
snStore
var
Store
*
indirectA
snStore
func
init
()
{
func
init
()
{
s
,
err
:=
NewAsnStore
()
Store
=
&
indirectAsnStore
{}
if
err
!=
nil
{
panic
(
err
)
}
Store
=
s
}
}
type
networkWithAsn
struct
{
type
networkWithAsn
struct
{
...
@@ -56,6 +52,17 @@ func (a *asnStore) AsnForIPv6(ip net.IP) (string, error) {
...
@@ -56,6 +52,17 @@ func (a *asnStore) AsnForIPv6(ip net.IP) (string, error) {
// NewAsnStore returns a `asnStore` that can be queried for the Autonomous System Numbers
// NewAsnStore returns a `asnStore` that can be queried for the Autonomous System Numbers
// for a given IP address or a multiaddress which contains an IP address.
// for a given IP address or a multiaddress which contains an IP address.
func
NewAsnStore
()
(
*
asnStore
,
error
)
{
func
NewAsnStore
()
(
*
asnStore
,
error
)
{
if
Store
.
store
==
nil
{
store
,
err
:=
newAsnStore
()
if
err
!=
nil
{
return
nil
,
err
}
Store
.
store
=
store
}
return
Store
.
store
,
nil
}
func
newAsnStore
()
(
*
asnStore
,
error
)
{
cr
:=
cidranger
.
NewPCTrieRanger
()
cr
:=
cidranger
.
NewPCTrieRanger
()
for
k
,
v
:=
range
ipv6CidrToAsnMap
{
for
k
,
v
:=
range
ipv6CidrToAsnMap
{
...
@@ -71,3 +78,22 @@ func NewAsnStore() (*asnStore, error) {
...
@@ -71,3 +78,22 @@ func NewAsnStore() (*asnStore, error) {
return
&
asnStore
{
cr
},
nil
return
&
asnStore
{
cr
},
nil
}
}
type
indirectAsnStore
struct
{
store
*
asnStore
}
// AsnForIPv6 returns the AS number for the given IPv6 address.
// If no mapping exists for the given IP, this function will
// return an empty ASN and a nil error.
func
(
a
*
indirectAsnStore
)
AsnForIPv6
(
ip
net
.
IP
)
(
string
,
error
)
{
if
a
.
store
==
nil
{
store
,
err
:=
newAsnStore
()
if
err
!=
nil
{
panic
(
err
)
}
a
.
store
=
store
}
return
a
.
store
.
AsnForIPv6
(
ip
)
}
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