I am trying to convert IP addresses to their ASN. I grabbed a network to ASN mapping from:
http://thyme.apnic.net/current/data-raw-table
and converted the following PHP script to Lua for use in OpenResty:
http://quaxio.com/bgp/
My probelm is that the script works for some IPs, but not for others, for example:
# curl -s localhost/asn?ip=8.8.8.8
IP is: 134744072
ASN: 15169
# curl -s localhost/asn?ip=222.242.105.93
IP is: 3740428637
The actual code was a bit long for an email so I posted it here: http://pastebin.com/Kfwa1nYY
For testing you can simply add the following to redis:
redis-cli SET 134744064 15169
redis-cli SET 3740270592 4134
I believe I have a bug in the bitwise conversion of the long IP. Can someone help explain why the script sometimes returns the correct ASN and sometimes does not?
thanks!
- G