GeoIP API for Chicken Scheme
By Arto on Mon, 2006-06-19 21:05. API | Chicken | DNS | GeoIP | Lisp | projects | SchemeGeoIP is a IP geolocation library and database developed by MaxMind. They provide a freely available GeoLite Country database that maps IP address blocks to country codes (with a claimed 97% accuracy), and sell several subscription-based commercial databases that provide more fine-grained geolocation services such as determining the exact region or city based on an IP address.
Geolocation is useful in a number of areas. For example, geolocation services can be used in web applications to customize content or target ads regionally, to select the geographically closest mirror server, or to analyze web server logs to determine the countries that visitors originate from.
As part of a distributed, geographically load-balanced DNS daemon I’ve been developing in Scheme, I’ve put together a set of GeoIP API bindings for Chicken Scheme that I’m hereby making available under the MIT License.
This was the first egg I’ve developed for Chicken — not to mention the first time I’d needed to make any real use of Chicken’s FFI — and I have to say I was pleasantly surprised at how easy it was to create.
It was gratifying to be able to go from zero to completed egg in only a couple hours simply by looking at some other eggs for examples on how to use foreign-lambda , create a setup spec, and write the eggdoc documentation.
The GeoIP API subset provided by the egg is straightforward to use. Basically, you feed in a hostname or an IP address, and you get back a country code or country name. Country codes can be returned as either the 2-letter or 3-letter codes defined in ISO-3166-1.
Here’s a usage example that you can try out in the Chicken interpreter once you’ve installed the GeoIP egg:
(use geoip)
; A GeoIP database can be explicitly opened, as follows:
(define chicken "www.call-with-current-continuation.org")
(let ((db (geoip:open-type GEOIP-COUNTRY-EDITION)))
(geoip:country-code-by-name chicken db)) ; => "DE"
; ...but this is not necessary for working with the freely
; available GeoIP Standard Country database:
(geoip:country-code-by-name chicken) ; => "DE"
(geoip:country-code-by-addr "194.97.107.133") ; => "DE"
(geoip:country-code3-by-name "www.google.com") ; => "USA"
(geoip:country-name-by-name "www.bbc.co.uk") ; => "United Kingdom"
Please have a look at the GeoIP egg documentation for more information. Note that the extended region-specific API procedures meant for use with MaxMind’s commercial databases are not implemented at present, but could be easily added provided the possibility to test them out.
You can download the egg from http://bendiken.net/code/chicken/eggs/geoip.egg (6 KB). Installation is performed in the normal Chicken fashion with chicken-setup. Note that you will need to have the GeoIP C library installed in order for the egg to compile successfully.
If you are running OS X and have Chicken and DarwinPorts installed, here’s what you would do to get the GeoIP C API and the Chicken bindings installed:
$ sudo port install libgeoip
$ wget http://bendiken.net/code/chicken/eggs/geoip.egg
$ sudo chicken-setup geoip
Installation should be similarly facile on other Unix-like platforms.
Currently, the GeoIP egg has been tested with Chicken 2.312 and version 1.3.14 of the GeoIP C API (as installed from DarwinPorts). Please report any problems to the Chicken mailing list.
