C语言解析17monipdb.dat(http://www.ipip.net/)免费数据库
官方给的链接打不开,而且里面的逻辑,每次都会打开文件,所以自己做了点个修改,发上来,借大家参考:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#include <fstream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h> class IP17MON {
public:
static bool init(const std::string strPathToDataFile = "./17monipdb.dat") {
if(is_init) {
return true;
} printf("Opening %s\n", strPathToDataFile.c_str());
std::ifstream ifDataFile(strPathToDataFile.c_str(), std::ios::binary);
if (ifDataFile.is_open() == false) {
printf("%m\n");
return false;
}
// std vector reserve
vecDataFile.assign(std::istreambuf_iterator<char>(ifDataFile), std::istreambuf_iterator<char>());
printf("Load %lu bytes success\n", vecDataFile.size()); unsigned int uiIndexLen = 0;
memcpy(&uiIndexLen, &vecDataFile[0], 4);
uiIndexLen = ntohl(uiIndexLen);
printf("uiIndexLen = %d\n", uiIndexLen);
pIPIndex = &vecDataFile[4];
pIPData = &vecDataFile[uiIndexLen];
is_init = 1;
return true;
} static std::string find(const std::string strIP) {
struct sockaddr_in stSockAddrInet; memset(&stSockAddrInet, 0, sizeof(struct sockaddr_in));
if(inet_aton(strIP.c_str(), &stSockAddrInet.sin_addr) == 0) {
printf("convert error\n");
return "";
}
unsigned int uiIP = ntohl(stSockAddrInet.sin_addr.s_addr); // atoi The string can contain additional characters after those
// that form the integral number, which are ignored and have no
// effect on the behavior of this function.
int iFirst = atoi(strIP.c_str());
int iStart = 0;
memcpy(&iStart, pIPIndex+(iFirst*4), 4);
printf("iStart = %d\n", iStart);
int iMaxComLen = pIPData - pIPIndex - 1024 - 4; int iIndexOffset = -1;
unsigned char ucIndexLength = 0;
for (iStart = iStart * 8 + 1024; iStart < iMaxComLen; iStart += 8) {
unsigned int uiCurrIP = 0;
memcpy(&uiCurrIP, pIPIndex+iStart, 4);
uiCurrIP = ntohl(uiCurrIP);
if (uiCurrIP >= uiIP) {
iIndexOffset = 0;
memcpy(&iIndexOffset, pIPIndex+iStart+4, 3);
memcpy(&ucIndexLength, pIPIndex+iStart+7, 1);
break;
}
} if (iIndexOffset == -1) {
return "";
} std::string strRegion(pIPData + iIndexOffset - 1024, ucIndexLength);
return strRegion;
}
public:
static std::vector<char> vecDataFile;
static char *pIPIndex, *pIPData;
static int is_init;
};
char *IP17MON::pIPIndex;
char *IP17MON::pIPData;
int IP17MON::is_init = 0;
std::vector<char> IP17MON::vecDataFile; extern "C" const char *find_monip(const char *ip)
{
if (IP17MON::init() == false) {
return "";
}
return IP17MON::find(ip).c_str();
}
C语言解析17monipdb.dat(http://www.ipip.net/)免费数据库的更多相关文章
- atitit.java解析sql语言解析器解释器的实现
atitit.java解析sql语言解析器解释器的实现 1. 解析sql的本质:实现一个4gl dsl编程语言的编译器 1 2. 解析sql的主要的流程,词法分析,而后进行语法分析,语义分析,构建sq ...
- C语言解析WAV音频文件
C语言解析WAV音频文件 代码地址: Github : https://github.com/CasterWx/c-wave-master 目录 前言 了解WAV音频文件 什么是二进制文件 WAV的二 ...
- 纯真IP数据库(qqwry.dat)转换成最新的IP数据库格式(ipwry.dat)
纯真IP数据库(qqwry.dat)转换成最新的IP数据库格式(ipwry.dat) 转载自:http://blog.cafeboy.org/2011/02/25/qqwry-to-ipwry/ ht ...
- VBA中使用JavaScript脚本语言解析JSON数据
JSON:JavaScript 对象表示法(JavaScript Object Notation) 和xml相似,都是文本形式(保存在文本文件中或字符串等形式),比如: jsstr = {" ...
- cJSON_json包的C语言解析库
cJSON库描述 CJSON是一个用于解析JSON包的C语言库,库文件为cJSON.c和cJSON.h, 所有的实现都在这两个文件中.原作者的地址cJSON. JSON包的解析 例如有一个JSON的数 ...
- NOIP2014-提高组初赛C语言解析(选择填空题)
第二十届全国青少年信息学奥林匹克联赛初赛 一.单项选择题(共 20 题,每题 1.5 分,共计 30 分.每题有且仅有一个正确选项) 1. 以下哪个是面向对象的高级语言( B ) A.汇编语言 B ...
- 使用go语言解析xml
操作系统: CentOS 6.9_x64 go语言版本: 1.8.3 问题描述 现有一个自动报障程序,如果服务出错会自动给指定人发送邮件,配置文件内容如下(default.xml): <?xml ...
- go语言解析网页利器goquery使用教程(爬虫必备)
某些时候需要爬取网页中指定信息时,通常需要一些框架解析网页行成dom模型,然后来操作节点来获取相应的信息.在java中很显然就是Jsoup,而在Golang里,应该就是这个goquery了吧. goq ...
- 书籍推荐《以C语言解析电脑》
这本书要想买到,在大陆看起来比较难,理出个目录,看个大概: 另外在这个地方可以预览前20页:http://openebook.hyread.com.tw/ebookservice/hyviewer/o ...
随机推荐
- 个人作业2——英语学习APP案例分析
一.个人体验 1.下载并使用,描述最简单直观的个人第一次上手体验. ①入眼界面华丽,有正能量的名言警句配上很有意境的图片,界面美观. ②内容丰富,有许多精选英文文章,同时配有中文翻译,便于理解. ③能 ...
- 简单实用的JQuery弹出层
效果: 初始状态时滚动条是可以滚动的 弹出层出现之后:1.弹窗始终居于整个窗口的中间 2.滚动条不可滚动 实现代码: HTML代码: <div class="container&quo ...
- 老李分享:大数据测试之HDFS文件系统
poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908821478,咨询电话010-845052 ...
- [Linux] PHP程序员玩转Linux系列-Linux和Windows安装nginx
1.PHP程序员玩转Linux系列-怎么安装使用CentOS 2.PHP程序员玩转Linux系列-lnmp环境的搭建 3.PHP程序员玩转Linux系列-搭建FTP代码开发环境 4.PHP程序员玩转L ...
- 用 config drive 配置网络 - 每天5分钟玩转 OpenStack(173)
上一节最后问了大家一个问题:如果 subnet 没有开 DHCP,会是怎样一个情况? 在其他条件不变的情况下,cloud-init 依然会完成那 3 个步骤,也就是说网卡还是会被配置成 dhcp 模式 ...
- input是否checked与使用jquery的attr或prop方法无关
最近在项目中有这样一个需求,用户在下单时可以选择优惠券,也可取消选择,并且可以多次选择,取消. 这是一个典型的input标签checked功能,博主使用radio元素实现此需求,但是优惠券只能选中,不 ...
- 云计算之路-阿里云上:数据库连接数过万的真相,从阿里云RDS到微软.NET Core
在昨天的博文中,我们坚持认为数据库连接数过万是阿里云RDS的问题,但后来阿里云提供了当时的数据库连接情况,让我们动摇了自己的想法. 帐户 连接数 A 4077 B 3995 C 741 D 698 E ...
- 基于Spring开发——自定义标签及其解析
1. XML Schema 1.1 最简单的标签 一个最简单的标签,形式如: <bf:head-routing key="1" value="1" to= ...
- Use “error_messages” in Rails 3.2? (raises “undefined method” error)
I am getting the following error in my Rails 3.2 functional tests: ActionView::Template::Error: unde ...
- 跟着刚哥梳理java知识点——枚举和注解(十四)
enum Season{ SPRING("spring","春暖花开"), SUMMER("summer","夏日炎炎" ...