· 本文地址: http://www.laruence.com/2008/04/09/112.html
· 转载请注明出处
还是和论文相关,要编写一个Apache的模块,挂在post read_request阶段,在第一时间,判断一个链接是否是而已连接请求,并在第一时间拒绝恶意连接请求。
首先遇到的第一个问题,就是需要从http.conf中读取配置信息,提供俩个配置指令,MaxNumber, TimeRange.指定在一段时间内的连接次数上限。
编写的时候,遇到的第一个问题就是,moudule申明,不能通过编译,编译器提示重复定义。查了N遍手边的apache源码分析,也没有找到原因,如:
module door_module;
......
module MODULE_VAR_EXPORT door_module = {
STANDARD_MODULE_STUFF,
NULL,
NULL,
NULL,
create_door_config,
NULL,
door_cmds,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
door_authorization,
};
最后,删除掉第一个module申明, 通过编译。
遇到的第二个问题,就是命令表中的命令处理函数在http_config.h中的定义如下:
这俩个问题这么糊涂的解决以后,接下来的工作就很简单了。
首先在server config阶段,初始化我们的全局配置结构。
int MaxNumber;
int TimeLimit;
}mod_door_config;
static void * create_door_config(pool *p, server_rec * s){
mod_door_config * cf = static_cast<mod_door_config *>(ap_palloc(p, sizeof(mod_door_config)));
cf->MaxNumber = 100;
cf->TimeLimit = 60;
return cf;
}
{"MaxRequest", maxRequest, NULL, RSRC_CONF|ACCESS_CONF, TAKE1, "Can't get MaxRequest"},
{"TimeRange", timeRange, NULL, RSRC_CONF|ACCESS_CONF, TAKE1, "Cant' get Time Range"},
{NULL}
};
server_rec * s = cmd->server;
mod_door_config *cf = static_cast<mod_door_config *>(ap_get_module_config(s->module_config, &door_module));
cf->MaxNumber = atoi(arg);
//cerr<<cf->MaxNumber<<endl;
return NULL;
}
static const char * timeRange(cmd_parms *cmd, void * dconf, const char * arg){
server_rec * s = cmd->server;
mod_door_config *cf = static_cast<mod_door_config *>(ap_get_module_config(s->module_config, &door_module));
cf->TimeLimit = atoi(arg);
//cerr<<cf->TimeLimit<<endl;
return NULL;
}
cerr << r->connection->remote_ip<<endl;
mod_door_config * cf = static_cast<mod_door_config *>(ap_get_module_config(r->server->module_config, &door_module));
//ap_rprintf(r ,"your IP : %s ", r->connection->remote_ip);
//ap_rprintf(r ,"MaxNumber : %d ", cf->MaxNumber);
//ap_rprintf(r ,"TimeRange : %s ", cf->TimeLimit);
//cerr<<cf->MaxNumber<<endl;
return OK;
}
先写这么多,等我慢慢研究这些遗留问题,留待以后补充。
ps: 看源代码,是最快的学习方法。