package asnutilimport (_)//go:embed sorted-network-list.binvar dataset stringconst entrySize = 8*2 + 4// start, end 8 bytes; asn 4 bytesfunc readEntry( uint) (, uint64, uint32) { := entrySize * := dataset[ : +entrySize] = uint64([0]) | uint64([1])<<8 | uint64([2])<<16 | uint64([3])<<24 | uint64([4])<<32 | uint64([5])<<40 | uint64([6])<<48 | uint64([7])<<56 = [8:] = uint64([0]) | uint64([1])<<8 | uint64([2])<<16 | uint64([3])<<24 | uint64([4])<<32 | uint64([5])<<40 | uint64([6])<<48 | uint64([7])<<56 = [8:] = uint32([0]) | uint32([1])<<8 | uint32([2])<<16 | uint32([3])<<24return}// AsnForIPv6 returns the AS number for the given IPv6 address.// If no mapping exists for the given network, this function will return a zero ASN number.func ( net.IP) ( uint32) { = .To16()if == nil {return }returnAsnForIPv6Network(binary.BigEndian.Uint64())}func init() {iflen(dataset) > math.MaxUint/2 {panic("list is too big and would overflow in binary search") }}// AsnForIPv6Network returns the AS number for the given IPv6 network.// If no mapping exists for the given network, this function will return a zero ASN number.// network is the first 64 bits of the ip address interpreted as big endian.func ( uint64) ( uint32) { := uint(len(dataset)) / entrySizevar , uint = 0, for < { := ( + ) / 2// wont overflow since the list can't be that large , , := readEntry()if <= {if <= {return } = + 1 } else { = } }if >= {return0 } , , := readEntry()if <= && <= {return }return0}// Deprecated: use [AsnForIPv6] or [AsnForIPv6Network], they do not allocate.varStorebackwardCompattype backwardCompat 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 (backwardCompat) ( net.IP) (string, error) { = .To16()if == nil {return"", errors.New("ONLY IPv6 addresses supported") } := AsnForIPv6Network(binary.BigEndian.Uint64())if == 0 {return"", nil }returnstrconv.FormatUint(uint64(), 10), nil}func (backwardCompat) () {}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.