OSSP CVS Repository

ossp - ossp-pkg/l2/l2_ch_socket.c 1.31
Not logged in
[Honeypot]  [Browse]  [Directory]  [Home]  [Login
[Reports]  [Search]  [Ticket]  [Timeline
  [Raw

ossp-pkg/l2/l2_ch_socket.c 1.31
/*
**  L2 - OSSP Logging Library
**  Copyright (c) 2001 The OSSP Project (http://www.ossp.org/)
**  Copyright (c) 2001 Cable & Wireless Deutschland (http://www.cw.com/de/)
**
**  This file is part of OSSP L2, a flexible logging library which
**  can be found at http://www.ossp.org/pkg/l2/.
**
**  Permission to use, copy, modify, and distribute this software for
**  any purpose with or without fee is hereby granted, provided that
**  the above copyright notice and this permission notice appear in all
**  copies.
**
**  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
**  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
**  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
**  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
**  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
**  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
**  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
**  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
**  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
**  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
**  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
**  SUCH DAMAGE.
**
**  l2_ch_socket.c: socket channel implementation
*/

#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "l2.h"
#include "l2_p.h"

#define  L2_SOCKET_SABUFREAD  4096
#define  L2_SOCKET_SABUFWRITE 4096

/* declare private channel configuration */
typedef struct {
    sa_addr_t *saaServer;
    sa_t *saServer;
    long lTimeout; 

    char *szHost;            /* IP Address or name of host to connect to    */
    int  iProto;             /* Protocol to use, IPPROTO_TCP or IPPROTO_UDP */
    int  iPort;              /* TCP Port to connect to                      */
    int  iSocket;            /* Socket descriptor used during writing       */
} l2_ch_socket_t;

/* create channel */
static l2_result_t hook_create(l2_context_t *ctx, l2_channel_t *ch)
{
    l2_ch_socket_t *cfg = NULL;

    /* allocate private channel configuration */
    if ((cfg = (l2_ch_socket_t *)malloc(sizeof(l2_ch_socket_t))) == NULL)
        return L2_ERR_MEM;

    /* initialize configuration with reasonable defaults */
    cfg->saaServer = NULL;
    cfg->saServer  = NULL;
    cfg->lTimeout  = 30;

    cfg->szHost   = NULL;
    cfg->iProto    = -1;
    cfg->iPort     = 0;
    cfg->iSocket   = -1;

    /* link private channel configuration into channel context */
    ctx->vp = cfg;

    return L2_OK;
}

/* configure channel */
static l2_result_t hook_configure(l2_context_t *ctx, l2_channel_t *ch, const char *fmt, va_list ap)
{
    l2_ch_socket_t *cfg = (l2_ch_socket_t *)ctx->vp;
    char *szProtocol = NULL;
    l2_param_t pa[5];
    l2_result_t rv;

    /* feed and call generic parameter parsing engine */
    L2_PARAM_SET(pa[0], protocol,  STRING,  &szProtocol);
    L2_PARAM_SET(pa[1], timeout,   INT,     &cfg->lTimeout);
    L2_PARAM_SET(pa[2], host,      CHARPTR, &cfg->szHost);
    L2_PARAM_SET(pa[3], port,      INT,     &cfg->iPort);
    L2_PARAM_END(pa[4]);
    rv = l2_util_setparams(pa, fmt, ap);

    /* translate incoming configuration parameters */
    if (szProtocol != NULL) {
        if (strcmp(szProtocol, "IPPROTO_UDP") == 0)
            cfg->iProto = IPPROTO_UDP;
        else if (strcmp(szProtocol, "IPPROTO_TCP") == 0)
            cfg->iProto = IPPROTO_TCP;
        else
            return L2_ERR_ARG;
    }

    free(szProtocol);
    return rv;
}

/* open channel */
static l2_result_t hook_open(l2_context_t *ctx, l2_channel_t *ch)
{
    sa_rc_t rc;
    l2_ch_socket_t *cfg = (l2_ch_socket_t *)ctx->vp;

    /* make sure a path was set */
    if (cfg->szHost == NULL)
        return L2_ERR_USE;

    /* create socket address */
    if ((rc = sa_addr_create(&cfg->saaServer)) != SA_OK)
        return (rc == SA_ERR_SYS ? L2_ERR_SYS : L2_ERR_INT);
    if ((rc = sa_addr_u2a(cfg->saaServer, "inet://%s:%d",
                          cfg->szHost, cfg->iPort)) != SA_OK)
        return (rc == SA_ERR_SYS ? L2_ERR_SYS : L2_ERR_INT);

    /* create socket */
    if ((rc = sa_create(&cfg->saServer)) != SA_OK)
        return (rc == SA_ERR_SYS ? L2_ERR_SYS : L2_ERR_INT);

    /* configure socket parameters */
    sa_timeout(cfg->saServer, SA_TIMEOUT_ALL,  cfg->lTimeout, 0);
    sa_buffer (cfg->saServer, SA_BUFFER_READ,  L2_SOCKET_SABUFREAD);
    sa_buffer (cfg->saServer, SA_BUFFER_WRITE, L2_SOCKET_SABUFWRITE);

    return L2_OK;
}

/* write to channel */
static l2_result_t hook_write(l2_context_t *ctx, l2_channel_t *ch,
                              l2_level_t level, const char *buf, size_t buf_size)
{
    l2_ch_socket_t *cfg = (l2_ch_socket_t *)ctx->vp;
    size_t          sizeWrite;
    size_t          sizeRemain;
    sa_t           *sa;
    sa_rc_t         rc;
    sa_addr_t      *saa;

    /* parameter checks */
    assert(cfg->saServer != NULL);

    /* establish connection to server */
    saa = cfg->saaServer;
    sa  = cfg->saServer;
    if ((rc = sa_connect(sa, saa)) != SA_OK) {
        sa_shutdown(sa, "rw"); /* shutdown connection to server */
        return (rc == SA_ERR_SYS ? L2_ERR_SYS : L2_ERR_INT);
    }

    if (cfg->iProto == IPPROTO_TCP) {
        /* write message to channel socket, but check to make   */
        /* sure that the whole message was successfully written */
        sizeWrite  = 0;
        sizeRemain = buf_size;
        while(sizeRemain) {
            if ((rc = sa_write(sa, buf, sizeRemain, &sizeWrite)) != SA_OK)
                return (rc == SA_ERR_SYS ? L2_ERR_SYS : L2_ERR_INT);
            sizeRemain = sizeRemain - sizeWrite; /* how much is left? */
        }
    }

    return L2_OK;
}

/* close channel */
static l2_result_t hook_close(l2_context_t *ctx, l2_channel_t *ch)
{
    l2_ch_socket_t *cfg = (l2_ch_socket_t *)ctx->vp;

    /* parameter checks */
/* FIXME!    assert(cfg->saServer->fdSocket != -1); */

    /* destroy remote address */
    if (cfg->saServer != NULL) {
        sa_destroy(cfg->saServer);
        cfg->saServer = NULL;
    }
    if (cfg->saaServer != NULL) {
        sa_addr_destroy(cfg->saaServer);
        cfg->saaServer = NULL;
    }

    return L2_OK;
}

/* destroy channel */
static l2_result_t hook_destroy(l2_context_t *ctx, l2_channel_t *ch)
{
    l2_ch_socket_t *cfg = (l2_ch_socket_t *)ctx->vp;

    /* destroy channel configuration */
    free(cfg);

    return L2_OK;
}

/* exported channel handler structure */
l2_handler_t l2_handler_socket = {
    L2_CHANNEL_OUTPUT,
    hook_create,
    hook_configure,
    hook_open,
    hook_write,
    NULL,
    hook_close,
    hook_destroy
};


CVSTrac 2.0.1