1. comm_lists.c 中加入
int QQwry();
{"Wry" , QQwry,0},
2. menu.ini加入
@Wry 0,0,PERM_POST, "Wry", "(W) IP查询"
3. xyz.c最后加入
// add for QQwry
// 感谢cnss的数据结构
// 代码由 cacti@smth 编写
// mok@smth 移植到Firebird系统中 2003.7
#define QQWRYFILENAME "/home/bbs/etc/QQwry.dat"
#define BYTE3INT(X) ( ( X[0] & 0x000000FF ) \
| ( ( X[1] & 0x000000FF ) << 8 ) \
| ( ( X[2] & 0x000000FF ) << 16 ) )
#define BYTE4INT(X) ( ( X[0] & 0x000000FF ) \
| ( ( X[1] & 0x000000FF ) << 8 ) \
| ( ( X[2] & 0x000000FF ) << 16 ) \
| ( ( X[3] & 0x000000FF ) << 24 ) )
struct INDEXITEM {
unsigned char ip[4];
unsigned char offset[3];
};
int compare(unsigned char pA[4], unsigned char pB[4])
{
unsigned int a = BYTE4INT(pA);
unsigned int b = BYTE4INT(pB);
if ( a > b )
return 1;
else if ( a < b )
return -1;
else
return 0;
}
struct INDEXITEM LookForIndexItem(struct INDEXITEM* const pAimItem, FILE* pFil
e, unsigned
int indexBasePos, unsigned int indexAmount )
{
struct INDEXITEM tmp;
int i = 0;
int j = indexAmount;
int s = (int)sizeof(struct INDEXITEM);
while ( i < j - 1 ) {
int k = (int) (i+j)/2;
int offset = (int)( k * s );
fseek(pFile, indexBasePos+offset, SEEK_SET);
fread(&tmp, s, 1, pFile);
int c = compare( tmp.ip, pAimItem->ip );
if ( c > 0 )
j = k;
else if ( c < 0 )
i = k;
else {
i = k;
j = k;
}
}
fseek(pFile, indexBasePos+i*s, SEEK_SET);
fread(&tmp, s, 1, pFile);
return tmp;
}
void getData(unsigned char* str, FILE* pFile, int max)
{
int i = 0;
while ( (*(str+i)=fgetc(pFile)) && (i<(max-2)) )
i++;
str[i] = 0;
}
void RetrieveContent(struct INDEXITEM* const pIndexItem, unsigned char ip[4],
FILE* pFile, char* content)
{
// to get the pos from the offset array
long tmp = 0;
unsigned char buf[80];
int pos = BYTE3INT(pIndexItem->offset);
fseek(pFile, pos, SEEK_SET);
fread(buf, 4, 1, pFile);
if ( compare(ip, buf) > 0 ) {
strcat(content, "未知数据");
//printf("未知数据\n");
return ;
}
sprintf((char*)(buf+40), "%u.%u.%u.%u\n", buf[3], buf[2], buf[1],
buf[0]);
sprintf((char*)buf, "%u.%u.%u.%u ", pIndexItem->ip[3],
pIndexItem->ip[2], pIndexItem->ip[1], pIndexItem->ip[0]);
strcat(content, (char*)buf);
strcat(content, (char*)buf+40);
// 获取资料
fread(buf, 1, 1, pFile);
if ( buf[0] == 0x01 ) { // 国家地区均重复, 跳转至新地址
fread(buf, 3, 1, pFile);
pos = BYTE3INT(buf);
fseek(pFile, pos, SEEK_SET);
fread(buf, 1, 1, pFile);
}
// 获取国家
if ( buf[0] == 0x02 ) {
// 获取国家偏移
fread(buf, 3, 1, pFile);
// 保存地区信息
tmp = ftell(pFile);
pos = BYTE3INT(buf);
fseek(pFile, pos, SEEK_SET);
fread(buf, 1, 1, pFile);
}
if ( buf[0] == 0x01 || buf[0] == 0x02 ) {
strcat(content, "There is something wrong....\n");
//printf("There is something wrong....\n");
return;
}
if ( buf[0] )
getData(buf+1, pFile, 40);
strcat(content, (char*)buf);
//printf("%s\t", buf);
// 获取地区
if ( tmp )
fseek(pFile, tmp, SEEK_SET);
fread(buf, 1, 1, pFile);
while ( buf[0] == 0x02 ) {
// 获取地区偏移
fread(buf, 3, 1, pFile);
pos = BYTE3INT(buf);
fseek(pFile, pos, SEEK_SET);
fread(buf, 1, 1, pFile);
}
if ( buf[0] == 0x01 || buf[0] == 0x02 ) {
strcat(content, "There is something wrong....\n");
//printf("There is something wrong....\n");
return;
}
if ( buf[0] )
getData(buf+1, pFile, 40);
strcat(content," ");
strcat(content, (char*)buf);
return;
}
void SearchIP(unsigned int ip[4])
{
struct INDEXITEM target;
unsigned int indexHeadPos = 0;
unsigned int indexTailPos = 0;
char fileName[] = QQWRYFILENAME;
target.ip[0] = ip[3];
target.ip[1] = ip[2];
target.ip[2] = ip[1];
target.ip[3] = ip[0];
FILE* pf = fopen(fileName,"rb");
if ( pf == 0 ) {
prints("打开数据文件出错! 请联系程序管理员\n");
sprintf(genbuf, "QQwry查询 打开%s出错",fileName);
report(genbuf);
pressanykey();
return ;
}
fread(&indexHeadPos, sizeof(indexHeadPos), 1, pf);
fread(&indexTailPos, sizeof(indexTailPos), 1, pf);
int amount = (indexTailPos - indexHeadPos)/sizeof(struct INDEXITEM);
struct INDEXITEM start = LookForIndexItem(&target, pf, indexHeadPos, amoun
t);
char result[255] = {0};
RetrieveContent(&start, target.ip, pf, result);
prints("\n%s\n", result);
fclose(pf);
pressanykey();
}
unsigned long GetIPAdress(const char* name)
{
struct hostent *he;
unsigned int addr = 0;
// if ( (he = gethostbyname(name) ) != NULL)
// addr=*(unsigned int*) he->h_addr;
// else
addr=inet_addr(name);
return addr;
}
int QQwry()
{
unsigned int addr;
unsigned int ip2srch[4];
char content[256];
int maxlen=80;
modify_user_mode( FREEIP );
clear();
refresh();
//pressanykey();
//move(5,10);
getdata(10,0,"请输入要查询的IP:",content,240,DOECHO,YEA);
if(!content[0]) return;
sprintf(genbuf, "QQwry查询 %s", content);
report(genbuf);
if (strlen(content)>maxlen) {
prints("查询的内容超长!\n");
pressanykey();
return;
}
addr = GetIPAdress(content);
ip2srch[0] = (unsigned char) (addr);
ip2srch[1] = (unsigned char) (addr >> 8);
ip2srch[2] = (unsigned char) (addr >> 16);
ip2srch[3] = (unsigned char) (addr >> 24);
SearchIP(ip2srch);
return 0;
}
// end for QQwry
4. make update; 重启bbsd
5. 找一个QQwry.dat 放到/home/bbs/etc/
目前有一个问题就是gethostbyname那部分会出错,被我注释掉了,谁指点一下?
--
FROM 202.198.173.6