From 6d86af329e75e7fb2a11c9ff87e6847e05eae784 Mon Sep 17 00:00:00 2001 From: Marcel Poul Date: Thu, 24 Nov 2011 19:59:46 +0000 Subject: [PATCH] canl_io_connect with ssl support (wo user credentials) --- emi.canl.canl-c/Makefile | 10 +++++++--- emi.canl.canl-c/src/canl.c | 21 +++++++++++++++------ emi.canl.canl-c/src/canl_locl.h | 23 +++++++++++++++-------- emi.canl.canl-c/src/canl_sample_client.c | 6 +++++- 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/emi.canl.canl-c/Makefile b/emi.canl.canl-c/Makefile index fb02dfa..aa74a6d 100644 --- a/emi.canl.canl-c/Makefile +++ b/emi.canl.canl-c/Makefile @@ -9,14 +9,15 @@ libdir=lib VPATH=${top_srcdir}/src LIBCARES_LIBS?=-lcares +LIBSSL_LIBS?=-lssl CC=gcc COMPILE=libtool --mode=compile ${CC} ${CFLAGS} LINK=libtool --mode=link ${CC} ${LDFLAGS} INSTALL=libtool --mode=install install -CFLAGS_LIB=-Wall -fPIC -c -g -I${top_srcdir}/src ${LIBCARES_CFLAGS} -LFLAGS_LIB=-shared ${LIBCARES_LIBS} +CFLAGS_LIB=-Wall -fPIC -c -g -I${top_srcdir}/src ${LIBCARES_CFLAGS} ${LIBSSL_CFLAGS} +LFLAGS_LIB=-shared ${LIBCARES_LIBS} ${LIBSSL_LIBS} CFLAGS_CLI=-Wall -g -I${top_srcdir}/src LFLAGS_CLI=-L. -lcanl @@ -50,7 +51,7 @@ major:=${shell \ all: libcanl.la server client -libcanl.la: canl.lo canl_err.lo canl_dns.lo +libcanl.la: canl.lo canl_err.lo canl_dns.lo canl_ssl.lo ${LINK} -rpath ${stagedir}${prefix}/${libdir} ${version_info} $+ ${LFLAGS_LIB} -o $@ canl.lo: canl.c ${HEAD_CANL} canl_err.h @@ -62,6 +63,9 @@ canl_dns.lo: canl_dns.c ${HEAD_CANL} canl_err.lo: canl_err.c ${HEAD_CANL} ${COMPILE} -c ${top_srcdir}/src/canl_err.c ${CFLAGS_LIB} -o $@ +canl_ssl.lo: canl_err.c ${HEAD_CANL} + ${COMPILE} -c ${top_srcdir}/src/canl_ssl.c ${CFLAGS_LIB} -o $@ + client: ${OBJ_CLI} ${LINK} $< ${LFLAGS_CLI} -o $@ diff --git a/emi.canl.canl-c/src/canl.c b/emi.canl.canl-c/src/canl.c index 0f58dad..a1ac28e 100644 --- a/emi.canl.canl-c/src/canl.c +++ b/emi.canl.canl-c/src/canl.c @@ -2,7 +2,6 @@ #include #include #include -#include #include "canl.h" #include "canl_locl.h" #include "sys/socket.h" @@ -136,6 +135,12 @@ static int init_io_content(glb_ctx *cc, io_handler *io) goto end; } + io->s_ctx = (ossl_ctx *) calloc(1, sizeof(*(io->s_ctx))); + if (!io->s_ctx) { + err = ENOMEM; + goto end; + } + end: if (err) update_error(cc, err, "failed to initialize io_handler" @@ -152,6 +157,7 @@ int canl_io_connect(canl_ctx cc, canl_io_handler io, char * host, int port, int sock; struct sockaddr_in *sa_in = NULL; int i = 0; + int err_clear = 0; /*check cc and io*/ if (!glb_cc) { @@ -191,7 +197,7 @@ int canl_io_connect(canl_ctx cc, canl_io_handler io, char * host, int port, sa_in->sin_family = AF_INET; sa_in->sin_port = htons(port); - //TODO loop through h_addr_list + i = 0; while (io_cc->ar->ent->h_addr_list[i]) { @@ -201,13 +207,16 @@ int canl_io_connect(canl_ctx cc, canl_io_handler io, char * host, int port, if (err) err = errno; else - goto end; //success + break; //success i++; } - /*TODO Maybe continue with select()*/ /*call openssl */ - + err = ssl_init(glb_cc, io_cc); + if (err) + goto end; + err = ssl_connect(glb_cc, io_cc, timeout); //TODO timeout + /*write succes or failure to cc, io*/ //if (err) /*cc or io set error*/ @@ -216,7 +225,7 @@ int canl_io_connect(canl_ctx cc, canl_io_handler io, char * host, int port, end: if (err) { update_error(cc, err, "failed to connect (canl_io_connect)"); - if ((err = io_clear(glb_cc, io_cc))) + if ((err_clear = io_clear(glb_cc, io_cc))) update_error(cc, err, "failed to clean io_handler" " (canl_io_connect)"); } diff --git a/emi.canl.canl-c/src/canl_locl.h b/emi.canl.canl-c/src/canl_locl.h index 0d0ee31..597d3ea 100644 --- a/emi.canl.canl-c/src/canl_locl.h +++ b/emi.canl.canl-c/src/canl_locl.h @@ -5,6 +5,9 @@ #include #include #include +#include +#include +#include typedef struct _glb_ctx { @@ -12,14 +15,15 @@ typedef struct _glb_ctx char * err_msg; CANL_ERROR err_code; } glb_ctx; -/* - struct ossl_ctx - { - SSL_METHOD ssl_meth; - SSL_CTX ssl_ctx; - SSL ssl_conn_ctx; - } - */ + +typedef struct _ossl_ctx +{ + SSL_CTX *ssl_ctx; + SSL_METHOD *ssl_meth; + SSL *ssl_io; + BIO *bio_conn; +} ossl_ctx; + typedef struct _asyn_result { struct hostent *ent; int err; @@ -30,6 +34,7 @@ typedef struct _io_handler asyn_result *ar; struct sockaddr *s_addr; int sock; + ossl_ctx * s_ctx; } io_handler; #endif @@ -40,3 +45,5 @@ void update_error (glb_ctx *cc, CANL_ERROR err_code, const char *err_format, ... void free_hostent(struct hostent *h); //TODO is there some standard funcion to free hostent? int asyn_getservbyname(int a_family, asyn_result *ares_result,char const *name, struct timeval *timeout); +int ssl_init(glb_ctx *cc, io_handler *io); +int ssl_connect(glb_ctx *cc, io_handler *io, struct timeval *timeout); diff --git a/emi.canl.canl-c/src/canl_sample_client.c b/emi.canl.canl-c/src/canl_sample_client.c index 540ac76..ea344e2 100644 --- a/emi.canl.canl-c/src/canl_sample_client.c +++ b/emi.canl.canl-c/src/canl_sample_client.c @@ -15,6 +15,7 @@ int main(int argc, char *argv[]) char *p_server = NULL; char *def_server = "www.linuxfoundation.org"; int opt, port = 80; + struct timeval timeout; while ((opt = getopt(argc, argv, "hp:s:")) != -1) { switch (opt) { @@ -50,7 +51,10 @@ int main(int argc, char *argv[]) goto end; } - err = canl_io_connect(my_ctx, my_io_h, p_server, port, 0, NULL, NULL); + timeout.tv_sec = 15; + timeout.tv_usec = 0; + + err = canl_io_connect(my_ctx, my_io_h, p_server, port, 0, NULL, &timeout); if (err) { printf("connection cannot be established\n"); goto end; -- 1.8.2.3