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 ...
随机推荐
- ajax使用及代码表示
最近学习了ajax,记录一下学习写过的代码和一些问题 一.原生ajax var xhr = null; if(window.XMLHttpRequest) { xhr = new XMLHttpReq ...
- 老李推荐: 第3章1节《MonkeyRunner源码剖析》脚本编写示例: MonkeyRunner API使用示例
老李推荐: 第3章1节<MonkeyRunner源码剖析>脚本编写示例: MonkeyRunner API使用示例 MonkeyRunner这个类可以说是编写monkeyrunner脚 ...
- get你想象不到的技能
1.取消选取.防止复制 <body selectStart="return false"> </body> 2.不允许粘贴 <body onpaste ...
- ORACLE JOB创建
Connected Connected as focususer SQL> SQL> --JOB 需要在命令行执行: SQL> --抽数job SQL> CREATE OR R ...
- 基于WebForm和Bootstrap的权限框架解决方案 一.PQGRID的使用
天天打游戏也不是个事,就写一套权限框架吧,我的要求是即漂亮美观大方上档次,又要实用易用接地气. 按理来说应该先设计数据库在来秀的,但是人生就是这么随意,先搭个框子吧, 这一篇的重点是pqgrid的介绍 ...
- iOS项目中常见的文件
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...
- Java Script 数组操作
常用几种数组操作方法: concat() join() toString() pop() push() shift() unshift() slice() splice() sort() revers ...
- 深入tornado中的IOStream
IOStream对tornado的高效起了很大的作用,他封装了socket的非阻塞IO的读写操作.大体上可以这么说,当连接建立后,服务端与客户端的请求响应都是基于IOStream的,也就是说:IOSt ...
- 记录参加QCon的心得
如有侵权,请告知作者删除.scottzg@126.com 很荣幸参加QCon全球软件开发大会,这里特别感谢我们部门的总经理,也是<互联网广告算法和系统实践>此书的作者王勇睿.因为他我才有这 ...
- webmagic源码学习(一)
最近工作主要是一些爬虫相关的东西,由于公司需要构建自己的爬虫框架,在调研过程中参考了许多优秀的开源作品,包括webmagic,webcollector,Spiderman等,通过学习这些优秀的源码获益 ...