src/geo_functions.c
// This function builds up a code we need to lookup weather
// using Accuweather data.
// country code                     e.g. US
// city                             e.g. Beverly Hills
// if country code == US, get region e.g. CA
// And then return "Beverly HillsCAUS" if a US address or
//                    "Paris--FR" if non US
This function builds up a code we need to lookup weather
using Accuweather data.
This function builds up a code we need to lookup weather using Accuweather data .
char *
geo_lookup_weather(const char *ipstr) 
{
    if (mmdb_baddb) {
        return strdup(DEFAULT_WEATHER_CODE);
    }
    char *data;
    int ip_lookup_failed, db_status;
    MMDB_lookup_result_s result =
        MMDB_lookup_string(&mmdb_handle, ipstr, &ip_lookup_failed, &db_status);
    if (ip_lookup_failed) {
        #ifdef DEBUG
        fprintf(stderr,
                "[WARN] vmod_lookup_weathercode: Error from getaddrinfo for IP: %s Error Message: %s\n",
                ipstr, gai_strerror(ip_lookup_failed));
        #endif
        return strdup(DEFAULT_WEATHER_CODE);
    }
    if (db_status != MMDB_SUCCESS) {
        #ifdef DEBUG
        fprintf(stderr,
                "[ERROR] vmod_lookup_weathercode: libmaxminddb failure. \
Maybe there is something wrong with the file: %s libmaxmind error: %s\n",
                MMDB_CITY_PATH,
                MMDB_strerror(db_status));
        #endif
        return strdup(DEFAULT_WEATHER_CODE);
    }
    char *country = NULL;
    char *city    = NULL;
    char *state   = NULL;
    const char *country_lookup[] = {"country", "iso_code", NULL};
    const char *city_lookup[]    = {"city", "names", "en", NULL};
    const char *state_lookup[]   = {"subdivisions", "0", "iso_code", NULL};
    if (result.found_entry) {
        country = get_value(&result, country_lookup);
        city    = get_value(&result, city_lookup);
        if (country != NULL && strcmp(country,"US") == 0) {
            state = get_value(&result, state_lookup);
        } else {
            state = strdup("--");
        }
        if (country == NULL || city == NULL || state == NULL) {
            data = strdup(DEFAULT_WEATHER_CODE);
        } else {
            size_t chars = (sizeof(char)* ( strlen(country) + strlen(city) + strlen(state)) ) + 1;
            data = malloc(chars);
            sprintf(data, "%s%s%s", city, state, country);
        }
    } else {
        #ifdef DEBUG
        fprintf(
                stderr,
                "[INFO] No entry for this IP address (%s) was found\n",
                ipstr);
        #endif
        data = strdup(DEFAULT_WEATHER_CODE);
    }
    if (country != NULL)
        free(country);
    if (city != NULL)
        free(city);
    if (state != NULL)
        free(state);
    return data;
}
char * geo_lookup_weather ( const char * ipstr ) { if ( mmdb_baddb ) { return strdup ( DEFAULT_WEATHER_CODE ) ; } char * data ; int ip_lookup_failed , db_status ; MMDB_lookup_result_s result = MMDB_lookup_string ( & mmdb_handle , ipstr , & ip_lookup_failed , & db_status ) ; if ( ip_lookup_failed ) { #ifdef DEBUG fprintf ( stderr , " \n " , ipstr , gai_strerror ( ip_lookup_failed ) ) ; #endif return strdup ( DEFAULT_WEATHER_CODE ) ; } if ( db_status != MMDB_SUCCESS ) { #ifdef DEBUG fprintf ( stderr , " \
 \n " , MMDB_CITY_PATH , MMDB_strerror ( db_status ) ) ; #endif return strdup ( DEFAULT_WEATHER_CODE ) ; } char * country = NULL ; char * city = NULL ; char * state = NULL ; const char * country_lookup [ ] = { " " , " " , NULL } ; const char * city_lookup [ ] = { " " , " " , " " , NULL } ; const char * state_lookup [ ] = { " " , " " , " " , NULL } ; if ( result . found_entry ) { country = get_value ( & result , country_lookup ) ; city = get_value ( & result , city_lookup ) ; if ( country != NULL && strcmp ( country , " " ) == 0 ) { state = get_value ( & result , state_lookup ) ; } else { state = strdup ( " " ) ; } if ( country == NULL || city == NULL || state == NULL ) { data = strdup ( DEFAULT_WEATHER_CODE ) ; } else { size_t chars = ( sizeof ( char ) * ( strlen ( country ) + strlen ( city ) + strlen ( state ) ) ) + 1 ; data = malloc ( chars ) ; sprintf ( data , " " , city , state , country ) ; } } else { #ifdef DEBUG fprintf ( stderr , " \n " , ipstr ) ; #endif data = strdup ( DEFAULT_WEATHER_CODE ) ; } if ( country != NULL ) free ( country ) ; if ( city != NULL ) free ( city ) ; if ( state != NULL ) free ( state ) ; return data ; }
This function builds up a code we need to lookup weather
using Accuweather data.
This function builds up a code we need to lookup weather using Accuweather data .
// Lookup IP in the DB // these varaibles will hold our results // these are used to extract values from the mmdb // we should always return new york