Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3313386
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:06:34+00:00 2026-05-17T22:06:34+00:00

I have created a simple mod_perl module, which writes a 0-terminated string to the

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:06:35+00:00Added an answer on May 17, 2026 at 10:06 pm

    For content handlers the convention is
    to use SetHandler XXX in httpd.conf and
    then at the runtime they check for that string with

    if (!r->handler || (strcmp(r->handler, "XXX") != 0))
       return DECLINED;
    

    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:

       if (conn->base_server->port != 843)
               return DECLINED;
    

    Also below is the working module for the same purpose, but using bucket brigades, which is better than writing directly to client socket:

    /*
    LoadModule socket_policy_module modules/mod_socket_policy.so
    Listen 843
    <VirtualHost _default_:843>
    </VirtualHost>
    */
    #include <httpd.h>
    #include <http_protocol.h>
    #include <http_connection.h>
    #include <http_config.h>
    #include <http_log.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>\n"
    
    static int socket_policy_handler(conn_rec *conn) {
            apr_bucket_brigade *bb;
            apr_bucket *b;
            apr_status_t rv;
    
            if (conn->base_server->port != 843)
                    return DECLINED;
    
            bb = apr_brigade_create(conn->pool, conn->bucket_alloc);
            /* this will send the terminating 0 as well */
            b = apr_bucket_immortal_create(POLICY, sizeof(POLICY), bb->bucket_alloc);
            APR_BRIGADE_INSERT_TAIL(bb, b);
            b = apr_bucket_eos_create(bb->bucket_alloc);
            APR_BRIGADE_INSERT_TAIL(bb, b);
            rv = ap_pass_brigade(conn->output_filters, bb);
            if (rv != APR_SUCCESS) {
                    ap_log_error(APLOG_MARK, APLOG_ERR, 0, conn->base_server, "output error");
                    return DECLINED;
            }
    
            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
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have created a simple public ref class in the vc++ project, which is
Could someone help me on this, I have created simple web services using axis2
How to manually create Friendly URLs? (PHP) So I have created simple php file
I am new to Repository concept and get some questions. I have created simple
I have created the simple web service. Code: [ServiceContract] public interface ITsdxService { [OperationContract]
I have created this simple program to learn shared_ptr using namespace std; #define Yes
I have created very simple app with persistence context (hibernate as provider) to read
I have created a simple test form with FormBorderStyle = FixedToolWindow by default and
I have created a simple JQuery script with hovering effect on some links. The
I have created a simple WordPress plugin that automatically sets my new sites up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.