Skip to content

Conversation

@LTxAlves
Copy link
Contributor

Please sign (check) the below before submitting the Pull Request:

Describe changes:

Added function to retrieve city data for geoip when it is used

@sonarqubecloud
Copy link

@IvanNardi
Copy link
Collaborator

@LTxAlves , thanks for your patch.
Just out of curiosity, did you really test your code? I am asking only because we don't have real tests for the geolocation stuff...

@LTxAlves
Copy link
Contributor Author

Hi, @IvanNardi!

Thanks for the quick reply. I ran a simple test with some IP addresses just as a basic check.

image

Here's the test code:

#include <ndpi/ndpi_api.h>
#include <stdio.h>

int main()
{
    struct ndpi_global_context *g_ctx;

    g_ctx = ndpi_global_init();

    if (g_ctx == NULL)
        return 1;

    struct ndpi_detection_module_struct *ndpi_info_mod;

    ndpi_info_mod = ndpi_init_detection_module(g_ctx);

    if (ndpi_info_mod == NULL)
        return 1;

    ndpi_load_geoip(ndpi_info_mod, "GeoLite2-City.mmdb", "GeoLite2-ASN.mmdb");

    char country[50], continent[50], city[50];

    ndpi_get_geoip_country_continent_city(ndpi_info_mod, "24.124.1.8", country, 50, continent, 50, city, 50);
    printf("1\n\tCountry: %s\n\tContinent: %s\n\tCity: %s\n", country, continent, city);
    ndpi_get_geoip_country_continent_city(ndpi_info_mod, "8.8.8.8", country, 50, continent, 50, city, 50);
    printf("2\n\tCountry: %s\n\tContinent: %s\n\tCity: %s\n", country, continent, city);
    ndpi_get_geoip_country_continent_city(ndpi_info_mod, "161.148.164.31", country, 50, continent, 50, city, 50);
    printf("3\n\tCountry: %s\n\tContinent: %s\n\tCity: %s\n", country, continent, city);
    ndpi_get_geoip_country_continent_city(ndpi_info_mod, "184.74.73.88", country, 50, continent, 50, city, 50);
    printf("4\n\tCountry: %s\n\tContinent: %s\n\tCity: %s\n", country, continent, city);

    ndpi_free_geoip(ndpi_info_mod);

    return 0;
}

@IvanNardi
Copy link
Collaborator

@LTxAlves, could I use that code as a basic test to add to the unit tests integrated into the library?

Copy link
Collaborator

@IvanNardi IvanNardi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@LTxAlves
Copy link
Contributor Author

@IvanNardi , absolutely! Feel free to use/modify the code as you see fit

@IvanNardi IvanNardi merged commit 3d0bfc7 into ntop:dev Feb 24, 2025
30 checks passed
@IvanNardi
Copy link
Collaborator

Thanks for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants