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
660f0205
Unverified
Commit
660f0205
authored
Aug 25, 2020
by
Adin Schmahmann
Committed by
GitHub
Aug 25, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3 from libp2p/feat/defer-init
feat: use deferred initialization of the asnStore
parents
1b967eb4
d8ddb2eb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
9 deletions
+33
-9
asn.go
asn.go
+33
-9
No files found.
asn.go
View file @
660f0205
...
@@ -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
=
newIndirectAsnStore
()
if
err
!=
nil
{
panic
(
err
)
}
Store
=
s
}
}
type
networkWithAsn
struct
{
type
networkWithAsn
struct
{
...
@@ -53,9 +49,7 @@ func (a *asnStore) AsnForIPv6(ip net.IP) (string, error) {
...
@@ -53,9 +49,7 @@ func (a *asnStore) AsnForIPv6(ip net.IP) (string, error) {
return
n
.
asn
,
nil
return
n
.
asn
,
nil
}
}
// NewAsnStore returns a `asnStore` that can be queried for the Autonomous System Numbers
func
newAsnStore
()
(
*
asnStore
,
error
)
{
// for a given IP address or a multiaddress which contains an IP address.
func
NewAsnStore
()
(
*
asnStore
,
error
)
{
cr
:=
cidranger
.
NewPCTrieRanger
()
cr
:=
cidranger
.
NewPCTrieRanger
()
for
k
,
v
:=
range
ipv6CidrToAsnMap
{
for
k
,
v
:=
range
ipv6CidrToAsnMap
{
...
@@ -71,3 +65,33 @@ func NewAsnStore() (*asnStore, error) {
...
@@ -71,3 +65,33 @@ func NewAsnStore() (*asnStore, error) {
return
&
asnStore
{
cr
},
nil
return
&
asnStore
{
cr
},
nil
}
}
type
indirectAsnStore
struct
{
store
*
asnStore
doneLoading
chan
struct
{}
}
// 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
)
{
<-
a
.
doneLoading
return
a
.
store
.
AsnForIPv6
(
ip
)
}
func
newIndirectAsnStore
()
*
indirectAsnStore
{
a
:=
&
indirectAsnStore
{
doneLoading
:
make
(
chan
struct
{}),
}
go
func
()
{
defer
close
(
a
.
doneLoading
)
store
,
err
:=
newAsnStore
()
if
err
!=
nil
{
panic
(
err
)
}
a
.
store
=
store
}()
return
a
}
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