Hashing based on country throuh inline C and GeoIP

The attached package contains the source for a little library used through inline C in Varnish for converting IPs to country codes, primarily used for hashing. It uses the MaxMind? GeoIP library which can be obtained at

 http://www.maxmind.com/app/c

The site includes instructions on how to build the GeoIP library, which must be installed prior to using the library and inline C.

Building and testing the library

To build the library after having extracted the package, enter ./lib and run make:

cd lib
make

To test the library, run make test in the same directory:

make test

This should produce an output testing a couple of IPs. If you should get the message

Error: Could not load GeoIP plugin:
geoip_plugin.so: cannot open shared object file: No such file or directory

you have not installed the GeoIP library or it is not in your library path. To fix this, either install or add the GeoIP library to your library path.

When you have compiled your library successfully, copy the resulting geoip_plugin.so to a suitable place on your filesystem.

Using the library in Varnish

Included in the package is the VCL required to incorporate the library in Varnish, found in the file plugin.vcl. Before using it, make sure you alter the line

const char* plugin_name = "<insert full path to plugin directory>/geoip_plugin.so";

to be the full path to where you installed the geoip_plugin.so.

plugin.vcl contains the bare minimum of what is needed to load the library. The first C{ }C block sets up and loads the library, and you should see the line "GeoIP plugin loaded successfully." when Varnish starts up. If something goes wrong when loading the library, an error message is printed. This will most likely be caused by the path to the plugin being wrong or that the GeoIP library is not available on your system. The other C{ }C block performs the country lookup on the IP and adds it to the hash. The rest of your custom hashing, should there be one, must be added to the vcl_hash sub.

Attachments