使用C++编写Apache的模块 -- 命令表,保存配置信息
2008-04-09 00:56
656 查看
· 作者:laruence(http://www.laruence.com/)
· 本文地址: 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中的定义如下:
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: 看源代码,是最快的学习方法。
· 本文地址: 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中的定义如下:
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: 看源代码,是最快的学习方法。
相关文章推荐
- 使用apache mod_env模块保存php程序敏感信息
- 使用apache mod_env模块保存php程序敏感信息
- VisualStudio2012 & OpenCV & OpenNi编写运行C++代码使用配置的详细说明
- Apache使用ssl模块配置HTTPS
- Xshell使用技巧及sshd配置使用及Linux系统邮件查看清空,apache htpasswd命令
- apache 模块编写(c++)
- Apache 使用ssl模块配置HTTPS
- Apache 使用ssl模块配置HTTPS(Centos7 httpd2.4.6)
- Python性能提升一 —— 高计算模块使用C++编写
- apache 模块编写(c++)
- 如何使用 Apache 控制命令检查它的模块是否已经启用或加载
- apache之gzip模块或deflate模块的配置信息
- linux下找到apache的安装目录,配置文件,域名等信息命令
- 在CentOS6上使用saltstack配置Apache ssh模块 推荐
- 使用Apache Commons Configuration读取配置信息
- 扩展Python__在python脚本中使用其他语言(c/c++/java/c#)编写的模块
- apache中使用mod_gnutls模块实现多个SSL站点配置(多个HTTPS协议的虚拟主机)
- 使用Spring中Src下 -context.xml : 用资源文件夹保存数据库的链接信息 并在配置文件里读取
- linux 下c/c++ 使用shell命令并加入参数、保存结果
- Linux下的GNU Emacs 24命令_信息竞赛使用_C++