4.12.1.18 Geolocation functions

The geolocation functions allow to identify the country where the given IP address or host name is located. These functions are available only if the ‘GeoIP’ library is installed and mailfromd is compiled with the ‘GeoIP’ support. The ‘GeoIP’ is a geolocational package distributed by ‘MaxMind’ under the terms of the GNU Lesser General Public License. The library is available from http://www.maxmind.com/app/c.

Built-in Function: string geoip_country_code_by_addr (  string ip [, bool tlc])

Look up the ‘ISO 3166-1’ country code corresponding to the IP address ip. If tlc is given and is not zero, return the 3 letter code, otherwise return the 2 letter code.

Built-in Function: string geoip_country_code_by_name (  string name [, bool tlc])

Look up the ‘ISO 3166-1’ country code corresponding to the host name name. If tlc is given and is not zero, return the 3 letter code, otherwise return the 2 letter code.

If it is impossible to locate the country, both functions raise the e_not_found exception. If an error internal to the ‘GeoIP’ library occurs, they raise the e_failure exception.

Applications may test whether the GeoIP support is present and enable corresponding code blocks conditionally by testing if the ‘WITH_GEOIP’ m4 macro is defined. For example, the following code adds to the message the ‘X-Originator-Country’ header, containing the 2 letter code of the country where the client machine is located. If mailfromd is compiled without ‘GeoIP’ support, it does nothing:

 
m4_ifdef(`WITH_GEOIP',`
  add "X-Originator-Country" geoip_country_code_by_addr($client_addr)
')