From 3f7a08470a7024ddb1f371eccdab7735cb3662d7 Mon Sep 17 00:00:00 2001 From: Marcel Poul Date: Sun, 11 Dec 2011 23:54:08 +0000 Subject: [PATCH] to keep it simple, redundant or needless LOCs left out --- emi.canl.canl-c/src/canl.c | 67 +++++++++++------------------------------- emi.canl.canl-c/src/canl_err.c | 1 + emi.canl.canl-c/src/canl_ssl.c | 7 ++--- 3 files changed, 21 insertions(+), 54 deletions(-) diff --git a/emi.canl.canl-c/src/canl.c b/emi.canl.canl-c/src/canl.c index ada866f..af38e0f 100644 --- a/emi.canl.canl-c/src/canl.c +++ b/emi.canl.canl-c/src/canl.c @@ -14,44 +14,28 @@ static int try_connect(glb_ctx *glb_cc, io_handler *io_cc, char *addr, canl_ctx canl_create_ctx() { glb_ctx *ctx = NULL; - int err = 0; /*create context*/ ctx = (glb_ctx *) calloc(1, sizeof(*ctx)); - if (!ctx) { - err = ENOMEM; - goto end; - } - -end: - if (err) + if (!ctx) return NULL; - else - return ctx; + return ctx; } void canl_free_ctx(canl_ctx cc) { glb_ctx *ctx = (glb_ctx*) cc; - if (!cc) { - goto end; - } - + if (!cc) + return; /*delete content*/ - if (ctx->err_msg) { free(ctx->err_msg); ctx->err_msg = NULL; } free(ctx); - cc = ctx = NULL; - -end: - return; - } canl_io_handler canl_create_io_handler(canl_ctx cc) @@ -60,15 +44,13 @@ canl_io_handler canl_create_io_handler(canl_ctx cc) glb_ctx *g_cc = cc; int err = 0; - if (!g_cc) { - err = EINVAL; + if (!g_cc) return NULL; - } /*create io handler*/ new_io_h = (io_handler *) calloc(1, sizeof(*new_io_h)); if (!new_io_h){ - err = ENOMEM; + set_error(g_cc, ENOMEM, posix_error, "Not enough memory "); return NULL; } @@ -92,27 +74,12 @@ end: static int init_io_content(glb_ctx *cc, io_handler *io) { - int err = 0; - if (!cc) { - return EINVAL; - } - if (!io) { - err = EINVAL; - goto end; - } - io->s_ctx = (ossl_ctx *) calloc(1, sizeof(*(io->s_ctx))); - if (!io->s_ctx) { - err = ENOMEM; - goto end; - } + if (!io->s_ctx) + return set_error(cc, ENOMEM, posix_error, "Not enough memory"); io->sock = -1; - -end: - if (err) - update_error(cc, "failed to initialize io_handler"); - return err; + return 0; } int canl_io_connect(canl_ctx cc, canl_io_handler io, char * host, int port, @@ -123,7 +90,7 @@ int canl_io_connect(canl_ctx cc, canl_io_handler io, char * host, int port, glb_ctx *glb_cc = (glb_ctx*) cc; struct _asyn_result ar; int i = 0; - int addr_types[] = {AF_INET6, AF_INET}; //TODO ip versions policy? + int addr_types[] = {AF_INET, AF_INET6}; //TODO ip versions policy? int ipver = AF_INET6; int j = 0; @@ -152,9 +119,13 @@ int canl_io_connect(canl_ctx cc, canl_io_handler io, char * host, int port, goto end; case NETDB_INTERNAL: err = EHOSTUNREACH; //TODO check + set_error(glb_cc, err, posix_error, "Cannot resolve" + " the server hostname (%s)", host); goto end; default: err = EHOSTUNREACH; //TODO check + set_error(glb_cc, err, posix_error, "Cannot resolve" + " the server hostname (%s)", host); goto end; } @@ -284,7 +255,6 @@ end: return err; } -//TODO improve /* close connection, preserve some info for the future reuse */ int canl_io_close(canl_ctx cc, canl_io_handler io) { @@ -309,8 +279,6 @@ int canl_io_close(canl_ctx cc, canl_io_handler io) } return err; - - /*set cc and io accordingly*/ } static void io_destroy(glb_ctx *cc, io_handler *io) @@ -415,10 +383,9 @@ int canl_set_ctx_own_cert(canl_ctx cc, canl_x509 cert, if (!cc) return EINVAL; - if(!cert) { - set_error(glb_cc, EINVAL, posix_error, "invalid parameter value"); - return err; - } + if(!cert) + return set_error(glb_cc, EINVAL, posix_error, "invalid" + "parameter value"); err = do_set_ctx_own_cert(glb_cc, cert, chain, key); if(err) { diff --git a/emi.canl.canl-c/src/canl_err.c b/emi.canl.canl-c/src/canl_err.c index b671a3d..ce3c31c 100644 --- a/emi.canl.canl-c/src/canl_err.c +++ b/emi.canl.canl-c/src/canl_err.c @@ -184,6 +184,7 @@ canl_get_error_code(canl_ctx cc) return ctx->err_code; } +/* TODO why canl_get_error, neuvolnila se pamet ctx->err_msg ???*/ char * canl_get_error_message(canl_ctx cc) { diff --git a/emi.canl.canl-c/src/canl_ssl.c b/emi.canl.canl-c/src/canl_ssl.c index 704d1f0..2e7a337 100644 --- a/emi.canl.canl-c/src/canl_ssl.c +++ b/emi.canl.canl-c/src/canl_ssl.c @@ -624,19 +624,18 @@ int ssl_close(glb_ctx *cc, io_handler *io) } /* TODO set_error*/ if (ret < 0) { - set_error(cc, 0, unknown_error, "Error during SSL" - " shutdown"); + set_error(cc, 0, unknown_error, "Error during SSL shutdown"); return -1; } /* successful shutdown (uni/bi directional)*/ if (ret2 == 0 || ret2 == 1) return ret2; else { - set_error(cc, ssl_err, ssl_error, "Error during SSL" - " shutdown"); + set_error(cc, ssl_err, ssl_error, "Error during SSL shutdown"); return -1; } } + #ifdef DEBUG static void dbg_print_ssl_error(int errorcode) { -- 1.8.2.3