OSSP CVS Repository

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

ossp-pkg/sio/sio_test.c 1.5
#include <stdio.h>

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

#include "sa.h"
#include <openssl/ssl.h>
#include <openssl/bio.h>
extern BIO_METHOD *BIO_s_socket();

extern sio_module_t sio_module_bio;
extern sio_module_t sio_module_hello;
extern sio_module_t sio_module_buffer;
#ifndef SINK
extern sio_module_t sio_module_sa;
#endif

#define e(f) rc = f; printf("%s = %s\n",#f, sio_error(rc)); fflush(stdout);
#define s(f) src = f; printf("%s = %s\n",#f, sa_error(src)); fflush(stdout);
#define n(f) rn = f; printf("%s = %d\n",#f, rn); fflush(stdout);
#define p(f) rp = f; printf("%s = %p\n",#f, rp); fflush(stdout);

int main(int argc, char *argv[])
{
    int rn;
    void *rp;
    sio_rc_t rc;
    sio_t *sio;
#ifndef SINK
    sio_stage_t *sios_sa;
#endif
    sio_stage_t *sios_bio, *sios_hello, *sios_buffer;

    sa_rc_t src;
    sa_addr_t *saa;
    sa_t *msa, *sa;
    char *uri;

    int fd;
    SSL_CTX *ctx;
#ifdef SINK
    BIO *bio;
#endif
    BIO *sbio;

    char buf[] = "Hello world\n";

    size_t actual;
    size_t buflen;
    int no  = 0;
    int yes = 1;

    s(sa_create(&msa));
    s(sa_option(msa, SA_OPTION_REUSEADDR, 1));

    s(sa_addr_create(&saa));
    uri = "inet://localhost:25001#tcp";
    s(sa_addr_u2a(saa, uri));
    s(sa_bind(msa,saa));
    s(sa_addr_destroy(saa));
    uri = NULL;

    ERR_load_BIO_strings();
    OpenSSL_add_ssl_algorithms();
    ctx = SSL_CTX_new(SSLv23_server_method());

    if (!SSL_CTX_use_certificate_file(ctx, "/u/mlelstv/ssl/server.crt", SSL_FILETYPE_PEM))
        exit(98);
    if (!SSL_CTX_use_PrivateKey_file(ctx, "/u/mlelstv/ssl/server.key", SSL_FILETYPE_PEM))
        exit(99);

    e(sio_create(&sio));
    e(sio_create_stage(sio, &sio_module_bio,    &sios_bio));
    e(sio_create_stage(sio, &sio_module_hello,  &sios_hello));
    e(sio_create_stage(sio, &sio_module_buffer, &sios_buffer));
#ifndef SINK
    e(sio_create_stage(sio, &sio_module_sa,     &sios_sa));
#endif

    buflen = 256;
    e(sio_configure_stage(sio, sios_bio, "inputsize", &buflen));

    buflen = 1000;
    e(sio_configure_stage(sio, sios_buffer, "outputsize", &buflen));

    s(sa_listen(msa, 5));

    for(;;) {

        s(sa_accept(msa, &saa, &sa));
        s(sa_addr_a2u(saa, &uri));
        printf("Connection from %s\n",uri);
        s(sa_addr_destroy(saa));

        p(sbio = BIO_new_ssl(ctx,0));
        e(sio_configure_stage(sio, sios_bio, "bio", sbio));

#ifdef SINK
        s(sa_getfd(sa, &fd));
        p(bio = BIO_new_socket(fd, 0));
        p(BIO_push(sbio,bio));
#else
        e(sio_configure_stage(sio, sios_sa, "sa", sa));
        buflen = 256;
        e(sio_configure_stage(sio, sios_sa, "buflen", &buflen));
        e(sio_configure_stage(sio, sios_bio, "issink", &no));
#endif
        e(sio_configure_stage(sio, sios_bio, "freebio", &yes));


#ifndef SINK
        e(sio_attach(sio, sios_sa, SIO_MODE_READWRITE));
#endif
        e(sio_attach(sio, sios_bio, SIO_MODE_READWRITE));
        e(sio_attach(sio, sios_hello, SIO_MODE_READWRITE));
        e(sio_attach(sio, sios_buffer, SIO_MODE_WRITE));

        e(sio_write(sio, buf, sizeof(buf)-1, &actual));
        e(sio_push(sio));

        e(sio_detach(sio, sios_buffer));
        e(sio_detach(sio, sios_hello));
        e(sio_detach(sio, sios_bio));
#ifndef SINK
        e(sio_detach(sio, sios_sa));
#endif

#ifdef SINK
        BIO_pop(bio);
#endif
        /* BIO_free(sbio); */
#ifdef SINK
        BIO_free(bio);
#endif

        sa_destroy(sa);
    }


#ifndef SINK
    e(sio_destroy_stage(sio, sios_sa));
#endif
    e(sio_destroy_stage(sio, sios_buffer));
    e(sio_destroy_stage(sio, sios_hello));
    e(sio_destroy_stage(sio, sios_bio));

    e(sio_destroy(sio));

    sa_destroy(msa);

    return 0;
}

CVSTrac 2.0.1