编辑代码

#include <stdio.h>
#include <GeoIP.h>

char* get_ip_location(const char* ip) {
    // GeoIP数据库文件路径
    const char* db_file = "GeoIP2-City.mmdb";
    
    // 打开数据库文件
    GeoIP* gi = GeoIP_open(db_file, GEOIP_STANDARD);
    
    // 查询IP归属地
    GeoIPRecord* record = GeoIP_record_by_name(gi, ip);
    
    // 获取归属地信息
    const char* location = record->city;
    
    // 关闭数据库文件
    GeoIP_delete(gi);
    
    return location;
}

int main() {
    // 示例IP地址
    const char* ip_addresses[] = {
        "202.102.133.0", "202.102.133.255",
        "202.102.135.0", "202.102.136.255",
        "202.102.156.34", "202.102.157.255",
        "202.102.48.0", "202.102.48.255",
        "202.102.49.15", "202.102.51.251",
        "202.102.56.0", "202.102.56.255"
    };
    
    // 遍历IP地址并查询归属地
    int i;
    for (i = 0; i < 12; i++) {
        const char* ip = ip_addresses[i];
        char* location = get_ip_location(ip);
        printf("%s %s\n", ip, location);
    }
    
    return 0;
}