C 中的 Apache 模块:如何将数据写入客户端套接字?
我创建了一个简单的 mod_perl 模块,它将一个以 0 结尾的字符串写入 Flash 客户端 连接到端口 843。它工作正常,但在我的 CentOS 5 Linux 机器上每个 httpd-child 使用 20m。
因此,我尝试用 C 重写我的模块,但我不确定如何通过我的协议处理程序接收的 conn_rec 结构访问客户端套接字。
我在邮件列表中询问并尝试添加 #define CORE_PRIVATE 并使用 ap_get_module_config(conn->conn_config, &core_module) 但这会破坏我的网络服务器:字符串既可以发送到端口 843(可以),也可以发送到端口 80(可以)。
有人在这里提出建议吗?
这是我的 SocketPolicy.pm (工作正常,但需要大量内存):
package SocketPolicy;
# Listen 843
# <VirtualHost _default_:843>
# PerlModule SocketPolicy
# PerlProcessConnectionHandler SocketPolicy
# </VirtualHost>
use strict;
use warnings FATAL => 'all';
use APR::Const(-compile => 'SO_NONBLOCK');
use APR::Socket();
use Apache2::ServerRec();
use Apache2::Connection();
use Apache2::Const(-compile => qw(OK DECLINED));
use constant POLICY =>
qq{<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM
"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*" to-ports="8080"/>
</cross-domain-policy>
\0};
sub handler {
my $conn = shift;
my $socket = $conn->client_socket();
my $offset = 0;
# set the socket to the blocking mode
$socket->opt_set(APR::Const::SO_NONBLOCK => 0);
do {
my $nbytes = $socket->send(substr(POLICY, $offset), length(POLICY) - $offset);
# client connection closed or interrupted
return Apache2::Const::DECLINED unless $nbytes;
$offset += $nbytes;
} while ($offset < length(POLICY));
my $slog = $conn->base_server()->log();
$slog->warn('served socket policy to: ', $conn->remote_ip());
return Apache2::Const::OK;
}
1;
这是我损坏的 mod_socket_policy.c (劫持端口 80):
/*
LoadModule socket_policy_module modules/mod_socket_policy.so
Listen 843
<VirtualHost _default_:843>
SetHandler socket_policy
</VirtualHost>
*/
#include <httpd.h>
#include <http_protocol.h>
#include <http_connection.h>
#include <http_config.h>
#include <http_log.h>
#define CORE_PRIVATE
#include <http_core.h>
#define POLICY "<?xml version=\"1.0\"?>\n" \
"<!DOCTYPE cross-domain-policy SYSTEM\n" \
"\"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\">\n" \
"<cross-domain-policy>\n" \
"<allow-access-from domain=\"*\" to-ports=\"8080\"/>\n" \
"</cross-domain-policy>\0"
static int socket_policy_handler(conn_rec *conn) {
apr_socket_t *socket = ap_get_module_config(conn->conn_config, &core_module);
apr_size_t len = strlen(POLICY);
apr_socket_send(socket, POLICY, &len);
ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, conn->base_server,
"served socket policy to %s", conn->remote_ip);
return OK;
}
static void register_hooks(apr_pool_t *pool) {
ap_hook_process_connection(socket_policy_handler, NULL, NULL, APR_HOOK_MIDDLE);
}
module AP_MODULE_DECLARE_DATA socket_policy_module = {
STANDARD20_MODULE_STUFF,
NULL,
NULL,
NULL,
NULL,
NULL,
register_hooks
};
另外,我查看了 mod_perl 源 他们似乎使用相同的方法来访问客户端套接字:
static MP_INLINE
apr_socket_t *mpxs_Apache2__Connection_client_socket(pTHX_ conn_rec *c,
apr_socket_t *s)
{
apr_socket_t *socket =
ap_get_module_config(c->conn_config, &core_module);
if (s) {
ap_set_module_config(c->conn_config, &core_module, s);
}
return socket;
}
那么为什么我的 Perl模块可以工作,而 C 模块不行吗?
I have created a simple mod_perl module, which writes a 0-terminated string to the Flash-clients connecting to the port 843. It works ok, but uses 20m per httpd-child at my CentOS 5 Linux machine.
So I'm trying to rewrite my module in C, but I'm not sure how to access the client socket through the conn_rec struct that my protocol handler receives.
I've asked at a mailing list and tried adding #define CORE_PRIVATE and using ap_get_module_config(conn->conn_config, &core_module) but this breaks my web server: the string is served both to port 843 (which is ok), but also to the port 80 (which is not ok).
Does anybody please have a suggestion here?
Here is my SocketPolicy.pm (works ok, but needs mucho memory):
package SocketPolicy;
# Listen 843
# <VirtualHost _default_:843>
# PerlModule SocketPolicy
# PerlProcessConnectionHandler SocketPolicy
# </VirtualHost>
use strict;
use warnings FATAL => 'all';
use APR::Const(-compile => 'SO_NONBLOCK');
use APR::Socket();
use Apache2::ServerRec();
use Apache2::Connection();
use Apache2::Const(-compile => qw(OK DECLINED));
use constant POLICY =>
qq{<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM
"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*" to-ports="8080"/>
</cross-domain-policy>
\0};
sub handler {
my $conn = shift;
my $socket = $conn->client_socket();
my $offset = 0;
# set the socket to the blocking mode
$socket->opt_set(APR::Const::SO_NONBLOCK => 0);
do {
my $nbytes = $socket->send(substr(POLICY, $offset), length(POLICY) - $offset);
# client connection closed or interrupted
return Apache2::Const::DECLINED unless $nbytes;
$offset += $nbytes;
} while ($offset < length(POLICY));
my $slog = $conn->base_server()->log();
$slog->warn('served socket policy to: ', $conn->remote_ip());
return Apache2::Const::OK;
}
1;
Here is my broken mod_socket_policy.c (hijacks the port 80):
/*
LoadModule socket_policy_module modules/mod_socket_policy.so
Listen 843
<VirtualHost _default_:843>
SetHandler socket_policy
</VirtualHost>
*/
#include <httpd.h>
#include <http_protocol.h>
#include <http_connection.h>
#include <http_config.h>
#include <http_log.h>
#define CORE_PRIVATE
#include <http_core.h>
#define POLICY "<?xml version=\"1.0\"?>\n" \
"<!DOCTYPE cross-domain-policy SYSTEM\n" \
"\"http://www.adobe.com/xml/dtds/cross-domain-policy.dtd\">\n" \
"<cross-domain-policy>\n" \
"<allow-access-from domain=\"*\" to-ports=\"8080\"/>\n" \
"</cross-domain-policy>\0"
static int socket_policy_handler(conn_rec *conn) {
apr_socket_t *socket = ap_get_module_config(conn->conn_config, &core_module);
apr_size_t len = strlen(POLICY);
apr_socket_send(socket, POLICY, &len);
ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, conn->base_server,
"served socket policy to %s", conn->remote_ip);
return OK;
}
static void register_hooks(apr_pool_t *pool) {
ap_hook_process_connection(socket_policy_handler, NULL, NULL, APR_HOOK_MIDDLE);
}
module AP_MODULE_DECLARE_DATA socket_policy_module = {
STANDARD20_MODULE_STUFF,
NULL,
NULL,
NULL,
NULL,
NULL,
register_hooks
};
Also, I've looked at mod_perl source and they seemingly use the same method to access the client socket:
static MP_INLINE
apr_socket_t *mpxs_Apache2__Connection_client_socket(pTHX_ conn_rec *c,
apr_socket_t *s)
{
apr_socket_t *socket =
ap_get_module_config(c->conn_config, &core_module);
if (s) {
ap_set_module_config(c->conn_config, &core_module, s);
}
return socket;
}
So why does my Perl module work and the C one not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于内容处理程序,约定是
在 httpd.conf 中使用 SetHandler XXX 并
然后在运行时他们检查该字符串
但对于协议处理程序没有这样的约定。
你必须为 httpd.conf 引入一些关键字
并检查它。例如 mod_echo (Apache 附带的源代码)引入了 ProtocolEcho开。或者在我的情况下,您可以只检查端口:
下面也是用于相同目的的工作模块,但使用 bucket brigades,这比直接写入客户端套接字要好:
For content handlers the convention is
to use SetHandler XXX in httpd.conf and
then at the runtime they check for that string with
But for protocol handlers there is no such convention.
You have to introduce some keyword for httpd.conf
and check for it. For example mod_echo (source code included with Apache) introduces ProtocolEcho On. Or in my case you could just check the port:
Also below is the working module for the same purpose, but using bucket brigades, which is better than writing directly to client socket: