还是和论文相关,要编写一个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中的定义如下:
const char * (*cmd_func)();
但是,《Aapache源码分析》和网上的资料中,命令处理都是带有参数的,如下:
static const char * maxRequest(cmd_parms *cmd, void * dconf, const char * arg);
真是,奇怪的很,我使用的是Apache 1.3的dev包,不知道是不是因为版本太低的缘故? 最后,修改了http_config.h, 通过编译,并且工作正常。真的很奇怪,留待以后解决。
这俩个问题这么糊涂的解决以后,接下来的工作就很简单了。
首先在server config阶段,初始化我们的全局配置结构。

typedef struct ...{
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;
}

通过ap_palloc为全局配置结构分配资源,并初始化配置结构。然后定义命令表。

static const command_rec door_cmds[] = ...{

...{"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}
};

接着定义命令处理函数:

static const char * maxRequest(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->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;
}

最后在验证阶段,来获取配置信息:

static int door_authorization(request_rec * r)...{
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;
}

经过编译,测试,一切works well。
先写这么多,等我慢慢研究这些遗留问题,留待以后补充。
ps: 看源代码,是最快的学习方法。
收获颇多
@whut_avatar 不好意思, N年前搞的了, 现在对这块已经不熟悉了, 爱莫能助.
请指教,小弟先表示谢意!
您好 !
我一直使用C语言来编写Apache模块的,现在想在我的handler中生成SAML断言,发送给后台的APP,因此想到了用OPENSaml,可是这个库只有C++的 ,因此就想在C中怎么调用C++的库。看到你这篇文章后,想问下你,面对这种情况我该怎么搞 ?Apache模块编写中怎么去调用一个C++的库,再说在用Apache编译工具apxs编译的时候,它根本就不能处理C++函数。
《Aapache源码分析》??
《Aapache源码分析》??
这个不算源码分析,只是我当时记录的一点心得.:)
《Aapache源码分析》??