OSSP CVS Repository

ossp - ossp-pkg/sio/sio_fd.c 1.1
Not logged in
[Honeypot]  [Browse]  [Directory]  [Home]  [Login
[Reports]  [Search]  [Ticket]  [Timeline
  [Raw

ossp-pkg/sio/sio_fd.c 1.1
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>

#include "al.h"
#include "sio.h"
#include "sio_module.h"

typedef struct {
    int fd;
    size_t buflen;
    char *buf;
    size_t actual;
} private_t;

/*
 * create stage
 *
 * allocate private instance data
 */
static
sio_rc_t fd_init(sio_t *sio, void **u)
{
    private_t *mydata;
    
    mydata = (private_t *)malloc(sizeof(private_t));
    if (mydata == NULL)
        return SIO_ERR_MEM;

    mydata->fd     = -1;
    mydata->buflen = 0;
    mydata->buf    = NULL;

    *u = mydata;

    return SIO_OK;
}

/*
 * configure stage
 *
 */
static
sio_rc_t fd_configure(sio_t *sio, void *u, void *obj, void *value)
{
    private_t *mydata = (private_t *)u;
    const char *name = (const char *)obj;
    char *newbuf;

    if (!strcmp(name, "fd")) {
        mydata->fd = *(int *)value;
    } else if (!strcmp(name, "buflen")) {
        mydata->buflen = *(int *)value;
        newbuf = realloc(mydata->buf, mydata->buflen);
        if (newbuf == NULL)
            return SIO_ERR_MEM;
        mydata->buf = newbuf;
    } else {
        return SIO_ERR_ARG;
    }

    return SIO_OK;
}

/*
 * destroy stage
 */
static
sio_rc_t fd_cleanup(sio_t *sio, void *u)
{
    private_t *mydata = (private_t *)u;

    if (mydata->buf) free(mydata->buf);
    free(mydata);

    return SIO_OK;
}

static
sio_rc_t fd_open(sio_t *sio, void *u)
{
    return SIO_OK;
}

static
sio_rc_t fd_close(sio_t *sio, void *u)
{
    return SIO_OK;
}

static
sio_rc_t fd_input(sio_t *sio, al_t *al, void *u)
{
    private_t *mydata = (private_t *)u;
    size_t actual;

    if (mydata->buf == NULL)
        return SIO_ERR_ARG;

    actual = read(mydata->fd, mydata->buf, mydata->buflen);
    if (actual < 0)
        return SIO_ERR_SYS;

    al_append_bytes(al, mydata->buf, actual);

    return SIO_OK;
}

static
al_rc_t fd_output_chunk(al_chunk_t *alc, void *u)
{
    private_t *mydata = (private_t *)u;
    char   *p;
    size_t n, actual;

    p = al_chunk_ptr(alc, 0);
    n = al_chunk_len(alc);

    actual = write(mydata->fd, p, n);
    if (actual < 0)
        return AL_ERR_EOF;

    mydata->actual += actual;

    return AL_OK;
}

static
sio_rc_t fd_output(sio_t *sio, al_t *al, void *u)
{
    private_t *mydata = (private_t *)u;
    al_rc_t arc;
    size_t n = al_bytes(al);

    mydata->actual = 0;

    arc = al_traverse_cb(al, 0, n, AL_FORWARD, fd_output_chunk, u);
    if (arc != AL_OK) return SIO_ERR_INT;

    arc = al_splice(al, 0, mydata->actual, NULL, NULL);
    if (arc != AL_OK) return SIO_ERR_INT;

    return SIO_OK;
}

sio_module_t sio_fd_module = {
    "fd",
    fd_init,
    fd_configure,
    fd_cleanup,
    fd_open,
    fd_close,
    fd_input,
    fd_output
};



CVSTrac 2.0.1