Skip to content

Commit 4b50467

Browse files
author
Aaron Bishop
committed
add support for asn network range
- Updates the ASN type to support the network range field. - Refactors the ASN method to utilise the LookupNetwork method of the mmdbReader in order to capture the network address and subnet. See Issue: oschwald#128
1 parent 02725dc commit 4b50467

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

reader.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ type AnonymousIP struct {
183183

184184
// The ASN struct corresponds to the data in the GeoLite2 ASN database.
185185
type ASN struct {
186+
Network string
186187
AutonomousSystemOrganization string `maxminddb:"autonomous_system_organization"`
187188
AutonomousSystemNumber uint `maxminddb:"autonomous_system_number"`
188189
}
@@ -366,12 +367,14 @@ func (r *Reader) AnonymousIP(ipAddress net.IP) (*AnonymousIP, error) {
366367
// ASN takes an IP address as a net.IP struct and returns a ASN struct and/or
367368
// an error.
368369
func (r *Reader) ASN(ipAddress net.IP) (*ASN, error) {
369-
if isASN&r.databaseType == 0 {
370-
return nil, InvalidMethodError{"ASN", r.Metadata().DatabaseType}
370+
var asn ASN
371+
network, _, err := r.mmdbReader.LookupNetwork(ipAddress, &asn)
372+
if err != nil {
373+
return nil, err
371374
}
372-
var val ASN
373-
err := r.mmdbReader.Lookup(ipAddress, &val)
374-
return &val, err
375+
mask, _ := network.Mask.Size()
376+
asn.Network = fmt.Sprintf("%s/%d", network.IP.String(), mask)
377+
return &asn, nil
375378
}
376379

377380
// ConnectionType takes an IP address as a net.IP struct and returns a

0 commit comments

Comments
 (0)