DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Apache IP2Location 3.0.0

01.21.2010
| 3054 views |
  • submit to reddit
        IP2Location Apache Module enables the user to identify the Country, Region, City, Latitude, Longitude, ZIP code, Time zone, ISP, Domain name, Connection type, Area code and Weather station code by IP address. It contains a demo IP2Location IP-COUNTRY BIN database which consists of real data for IP address range 0.0.0.0 to 99.255.255.255. This database contains IP address blocks as key and countries as values. The paid version of IP2Location database covers all IP address range. In general, it is more complete, faster and more accurate than using reverse DNS lookups.
Developers can download other sample databases to evaluate other features such as region, city, latitude, longitude, zip code, ISP and domain name using the same Apache module at this link: http://www.ip2location.com/apache.aspx.
Please visit http://www.ip2location.com for more information about geolocation.

static const int ENV_SET_MODE    = 0x0001;
static const int NOTES_SET_MODE  = 0x0002;
static const int ALL_SET_MODE    = 0x0003;

typedef struct {
                int enableFlag;
                int setMode; 
                char* dbFile;
                IP2Location* ip2locObj;
} ip2location_server_config;

module AP_MODULE_DECLARE_DATA IP2Location_module;

static apr_status_t ip2location_cleanup(void *cfgdata) {
   // cleanup code here, if needed
   return APR_SUCCESS;
}

static void ip2location_child_init(apr_pool_t *p, server_rec *s) {
   apr_pool_cleanup_register(p, NULL, ip2location_cleanup, ip2location_cleanup);
}

static int ip2location_post_read_request(request_rec *r) {
                char* ipaddr;
                ip2location_server_config* config;
                IP2LocationRecord* record;
                char buff[20];
                
                config = (ip2location_server_config*)
                                                                                               ap_get_module_config(r->server->module_config, &IP2Location_module);
                
                if(!config->enableFlag)
                                return OK;
                
                ipaddr = r->connection->remote_ip;
                record = IP2Location_get_all(config->ip2locObj, ipaddr);
                if(record) {
                                if(config->setMode & ENV_SET_MODE) {
                                                apr_table_set(r->subprocess_env, "IP2LOCATION_COUNTRY_SHORT", record->country_short); 
                                                apr_table_set(r->subprocess_env, "IP2LOCATION_COUNTRY_LONG", record->country_long); 
                                                apr_table_set(r->subprocess_env, "IP2LOCATION_REGION", record->region); 
                                                apr_table_set(r->subprocess_env, "IP2LOCATION_CITY", record->city); 
                                                apr_table_set(r->subprocess_env, "IP2LOCATION_ISP", record->isp); 
                                                sprintf(buff, "%f", record->latitude);
                                                apr_table_set(r->subprocess_env, "IP2LOCATION_LATITUDE", buff); 
                                                sprintf(buff, "%f", record->longitude);
                                                apr_table_set(r->subprocess_env, "IP2LOCATION_LONGITUDE", buff); 
                                                apr_table_set(r->subprocess_env, "IP2LOCATION_DOMAIN", record->domain); 
                                                apr_table_set(r->subprocess_env, "IP2LOCATION_ZIPCODE", record->zipcode); 
                                                apr_table_set(r->subprocess_env, "IP2LOCATION_TIMEZONE", record->timezone); 
                                                apr_table_set(r->subprocess_env, "IP2LOCATION_NETSPEED", record->netspeed);
                                                apr_table_set(r->subprocess_env, "IP2LOCATION_IDDCODE", record->iddcode);
                                                apr_table_set(r->subprocess_env, "IP2LOCATION_AREACODE", record->areacode);
                                                apr_table_set(r->subprocess_env, "IP2LOCATION_WEATHERSTATIONCODE", record->weatherstationcode);
                                                apr_table_set(r->subprocess_env, "IP2LOCATION_WEATHERSTATIONNAME", record->weatherstationname);
                                }
                                if(config->setMode & NOTES_SET_MODE) {
                                                apr_table_set(r->notes, "IP2LOCATION_COUNTRY_SHORT", record->country_short); 
                                                apr_table_set(r->notes, "IP2LOCATION_COUNTRY_LONG", record->country_long); 
                                                apr_table_set(r->notes, "IP2LOCATION_REGION", record->region); 
                                                apr_table_set(r->notes, "IP2LOCATION_CITY", record->city); 
                                                apr_table_set(r->notes, "IP2LOCATION_ISP", record->isp); 
                                                sprintf(buff, "%f", record->latitude);
                                                apr_table_set(r->notes, "IP2LOCATION_LATITUDE", buff); 
                                                sprintf(buff, "%f", record->longitude);
                                                apr_table_set(r->notes, "IP2LOCATION_LONGITUDE", buff); 
                                                apr_table_set(r->notes, "IP2LOCATION_DOMAIN", record->domain); 
                                                apr_table_set(r->notes, "IP2LOCATION_ZIPCODE", record->zipcode); 
                                                apr_table_set(r->notes, "IP2LOCATION_TIMEZONE", record->timezone); 
                                                apr_table_set(r->notes, "IP2LOCATION_NETSPEED", record->netspeed);
                                                apr_table_set(r->notes, "IP2LOCATION_IDDCODE", record->iddcode);
                                                apr_table_set(r->notes, "IP2LOCATION_AREACODE", record->areacode);
                                                apr_table_set(r->notes, "IP2LOCATION_WEATHERSTATIONCODE", record->weatherstationcode);
                                               apr_table_set(r->notes, "IP2LOCATION_WEATHERSTATIONNAME", record->weatherstationname);
                                }
                
                                IP2Location_free_record(record);                           
                }
                
                return OK;
}

static const char* set_ip2location_enable(cmd_parms *cmd, void *dummy, int arg) {
                ip2location_server_config* config = (ip2location_server_config*)
                                                                                                                                                                                                                                                                                                                ap_get_module_config(cmd->server->module_config, &IP2Location_module);
  if(!config) 
                return NULL;
  
  config->enableFlag = arg;
  return NULL;
}

static const char* set_ip2location_dbfile(cmd_parms* cmd, void* dummy, const char* dbFile, int arg) {
  ip2location_server_config* config = (ip2location_server_config*)
                                                                                                                                                                                                                                                                                                ap_get_module_config(cmd->server->module_config, &IP2Location_module);
                if(!config) 
                                return NULL;
                                
                config->dbFile = apr_pstrdup(cmd->pool, dbFile);
                if(config->enableFlag) {
                                config->ip2locObj = IP2Location_open(config->dbFile); 
                                if(!config->ip2locObj)
                                                return "Error opening dbFile!";
                }
                
                return NULL; 
}

static const char* set_ip2location_set_mode(cmd_parms* cmd, void* dummy, const char* mode, int arg) {
                ip2location_server_config* config = (ip2location_server_config*)
                                                                                                                                                                                                                                                                                                ap_get_module_config(cmd->server->module_config, &IP2Location_module);
                if(!config) 
                                return NULL;
                
                if(strcmp(mode, "ALL") == 0)      
                                config->setMode = ALL_SET_MODE;
                else if(strcmp(mode, "ENV") == 0)           
                                config->setMode = ENV_SET_MODE;
                else if(strcmp(mode, "NOTES") == 0)
                                config->setMode = NOTES_SET_MODE;                
                else
                                return "Invalid mode for IP2LocationSetMode";
                
                return NULL; 
}

static void* ip2location_create_svr_conf(apr_pool_t* pool, server_rec* svr) {
  ip2location_server_config* svr_cfg = apr_pcalloc(pool, sizeof(ip2location_server_config));
                svr_cfg->enableFlag = 0;
                svr_cfg->dbFile = NULL;
                svr_cfg->setMode = ALL_SET_MODE;
                svr_cfg->ip2locObj = NULL;
  return svr_cfg ;
}

static const command_rec ip2location_cmds[] = {
   AP_INIT_FLAG( "IP2LocationEnable", set_ip2location_enable, NULL, OR_FILEINFO, "Turn on mod_ip2location"),
   AP_INIT_TAKE1( "IP2LocationDBFile", set_ip2location_dbfile, NULL, OR_FILEINFO, "File path to DB file"),
   AP_INIT_TAKE1( "IP2LocationSetMode", set_ip2location_set_mode, NULL, OR_FILEINFO, "Set scope mode"),
   {NULL} 
};

static void ip2location_register_hooks(apr_pool_t *p) {
                ap_hook_post_read_request( ip2location_post_read_request, NULL, NULL, APR_HOOK_MIDDLE );
                ap_hook_child_init(        ip2location_child_init, NULL, NULL, APR_HOOK_MIDDLE );

}

// API hooks
module AP_MODULE_DECLARE_DATA IP2Location_module = {
   STANDARD20_MODULE_STUFF, 
   NULL,                        /* create per-dir    config structures */
   NULL,                        /* merge  per-dir    config structures */
   ip2location_create_svr_conf, /* create per-server config structures */
   NULL,                        /* merge  per-server config structures */
   ip2location_cmds,            /* table of config file commands       */
   ip2location_register_hooks   /* register hooks                      */
};