From 4fec9f6bf36bfceeabe2cb951ef09cc48a2e62e1 Mon Sep 17 00:00:00 2001 From: Marcel Poul Date: Tue, 29 Nov 2011 11:38:16 +0000 Subject: [PATCH] update_error without error codes; set_error replaced update_error where appropriate --- emi.canl.canl-c/src/canl.c | 30 +++++++++++++++--------------- emi.canl.canl-c/src/canl_err.c | 14 ++++---------- emi.canl.canl-c/src/canl_locl.h | 3 +-- emi.canl.canl-c/src/canl_ssl.c | 35 +++++++++++++++++------------------ 4 files changed, 37 insertions(+), 45 deletions(-) diff --git a/emi.canl.canl-c/src/canl.c b/emi.canl.canl-c/src/canl.c index 5ad9ee2..83fe282 100644 --- a/emi.canl.canl-c/src/canl.c +++ b/emi.canl.canl-c/src/canl.c @@ -96,10 +96,10 @@ canl_io_handler canl_create_io_handler(canl_ctx cc) end: if (err) { - update_error(g_cc, err,"cannot create canl_io_handler" + update_error(g_cc,"cannot create canl_io_handler" "canl_create_io_handler"); if ((err = canl_io_destroy(cc, (canl_io_handler)new_io_h))) - update_error(g_cc, err, "cannot destroy canl_ctx" + update_error(g_cc, "cannot destroy canl_ctx" "canl_create_io_handler"); new_io_h = NULL; } @@ -143,7 +143,7 @@ static int init_io_content(glb_ctx *cc, io_handler *io) end: if (err) - update_error(cc, err, "failed to initialize io_handler" + update_error(cc, "failed to initialize io_handler" "(init_io_content)"); return err; } @@ -225,9 +225,9 @@ int canl_io_connect(canl_ctx cc, canl_io_handler io, char * host, int port, /*cc or io set succes*/ end: if (err) { - update_error(cc, err, "failed to connect (canl_io_connect)"); + update_error(cc, "failed to connect (canl_io_connect)"); if ((err_clear = io_clear(glb_cc, io_cc))) - update_error(cc, err, "failed to clean io_handler" + update_error(cc, "failed to clean io_handler" " (canl_io_connect)"); } return err; @@ -267,7 +267,7 @@ int canl_io_accept(canl_ctx cc, canl_io_handler io, int port, hints.ai_flags = AI_PASSIVE; // use my IP if ((err = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) { - update_error(glb_cc, 0, "getaddrinfo: %s\n", gai_strerror(err)); + update_error(glb_cc, "getaddrinfo: %s\n", gai_strerror(err)); /*TODO what kind of error return?, getaddrinfo returns its own error codes*/ goto end; @@ -295,7 +295,7 @@ int canl_io_accept(canl_ctx cc, canl_io_handler io, int port, } if (p == NULL) { - update_error(glb_cc, err, "failed to bind (canl_io_accept)"); //TODO is it there????? + update_error(glb_cc, "failed to bind (canl_io_accept)"); //TODO is it there????? freeaddrinfo(servinfo); // all done with this structure goto end; } @@ -334,7 +334,7 @@ int canl_io_accept(canl_ctx cc, canl_io_handler io, int port, end: if (err) - update_error(glb_cc, err, "cannot accept connection (canl_io_accept)"); + update_error(glb_cc, "cannot accept connection (canl_io_accept)"); return err; } @@ -362,7 +362,7 @@ int canl_io_close(canl_ctx cc, canl_io_handler io) end: if (err) - update_error(glb_cc, err, "cannot close connection (canl_io_close)"); + update_error(glb_cc, "cannot close connection (canl_io_close)"); return err; } static int io_clear(glb_ctx *cc, io_handler *io) @@ -395,7 +395,7 @@ static int io_clear(glb_ctx *cc, io_handler *io) end: if (err) - update_error(glb_cc, err, "cannot clear io_handle (io_clear)"); + update_error(glb_cc, "cannot clear io_handle (io_clear)"); return err; } @@ -426,7 +426,7 @@ int canl_io_destroy(canl_ctx cc, canl_io_handler io) } end: if (err) - update_error(glb_cc, err, "can't destroy io_handle (canl_io_destroy)"); + update_error(glb_cc, "can't destroy io_handle (canl_io_destroy)"); return err; } @@ -450,7 +450,7 @@ size_t canl_io_read(canl_ctx cc, canl_io_handler io, void *buffer, size_t size, if (!buffer || !size) { err = EINVAL; - update_error(glb_cc, err, "no memory to write into (canl_io_read)"); + update_error(glb_cc, "no memory to write into (canl_io_read)"); return -1; } @@ -462,7 +462,7 @@ size_t canl_io_read(canl_ctx cc, canl_io_handler io, void *buffer, size_t size, } end: if (err) - update_error(glb_cc, err, "can't read from connection" + update_error(glb_cc, "can't read from connection" " (canl_io_read)"); return b_recvd; } @@ -486,7 +486,7 @@ size_t canl_io_write(canl_ctx cc, canl_io_handler io, void *buffer, size_t size, if (!buffer || !size) { err = EINVAL; - update_error(glb_cc, err, "nothing to write (canl_io_write)"); + update_error(glb_cc, "nothing to write (canl_io_write)"); return -1; } @@ -499,7 +499,7 @@ size_t canl_io_write(canl_ctx cc, canl_io_handler io, void *buffer, size_t size, end: if (err) - update_error(glb_cc, err, "can't write to connection" + update_error(glb_cc, "can't write to connection" " (canl_io_write)"); return b_written; } diff --git a/emi.canl.canl-c/src/canl_err.c b/emi.canl.canl-c/src/canl_err.c index ea316d1..2f69a7d 100644 --- a/emi.canl.canl-c/src/canl_err.c +++ b/emi.canl.canl-c/src/canl_err.c @@ -8,7 +8,7 @@ /* Save error message into err_msg * use NULL for empty err_format */ -void update_error (glb_ctx *cc, CANL_ERROR err_code, const char *err_format, ...) +void update_error (glb_ctx *cc, const char *err_format, ...) { unsigned int err_msg_len = 0; unsigned int err_msg_sum = 0; // sum of msg and format lengths @@ -20,12 +20,7 @@ void update_error (glb_ctx *cc, CANL_ERROR err_code, const char *err_format, ... return; if (err_format == NULL) { - if (!err_code) - return; - else { - cc->err_code = err_code; - return; - } + return; } va_start(ap, err_format); @@ -49,8 +44,6 @@ void update_error (glb_ctx *cc, CANL_ERROR err_code, const char *err_format, ... strcat (cc->err_msg, ";"); strcat (cc->err_msg, new_msg); - cc->err_code = err_code; - free(new_msg); } @@ -98,6 +91,7 @@ int canl_get_error(canl_ctx cc, char **reason) return EINVAL; } + //TODO what to return if (!ctx->err_msg) goto end; @@ -113,6 +107,6 @@ int canl_get_error(canl_ctx cc, char **reason) end: if (err) - update_error(ctx, err, "cannot get error message (canl_get_error)"); + update_error(ctx, "cannot get error message (canl_get_error)"); return err; } diff --git a/emi.canl.canl-c/src/canl_locl.h b/emi.canl.canl-c/src/canl_locl.h index ca5b821..5cfcd23 100644 --- a/emi.canl.canl-c/src/canl_locl.h +++ b/emi.canl.canl-c/src/canl_locl.h @@ -41,8 +41,7 @@ typedef struct _io_handler void reset_error (glb_ctx *cc, CANL_ERROR err_code); void set_error (glb_ctx *cc, CANL_ERROR err_code, const char *err_format, ...); -void update_error (glb_ctx *cc, CANL_ERROR err_code, - const char *err_format, ...); +void update_error (glb_ctx *cc, 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); diff --git a/emi.canl.canl-c/src/canl_ssl.c b/emi.canl.canl-c/src/canl_ssl.c index eb316d2..6f078ce 100644 --- a/emi.canl.canl-c/src/canl_ssl.c +++ b/emi.canl.canl-c/src/canl_ssl.c @@ -22,13 +22,12 @@ int ssl_init(glb_ctx *cc, io_handler *io) io->s_ctx->ssl_ctx = SSL_CTX_new(io->s_ctx->ssl_meth); if (!io->s_ctx->ssl_ctx){ err = 1; //TODO set appropriate - update_error(cc, err, "cannot create SSL context (ssl_init)"); goto end; } end: if (err) - update_error(cc, err, ""); //TODO update error + set_error(cc, err, "cannot initialize SSL context (ssl_init)"); return err; } @@ -70,8 +69,6 @@ int ssl_connect(glb_ctx *cc, io_handler *io, struct timeval *timeout) */ end: - if (err) - update_error(cc, err, "(ssl_connect)"); //TODO update error return err; } @@ -112,8 +109,6 @@ int ssl_accept(glb_ctx *cc, io_handler *io, io_handler *new_io, */ end: - if (err) - update_error(cc, err, "(ssl_accept)"); //TODO update error return err; } @@ -207,11 +202,11 @@ static int do_ssl_connect( glb_ctx *cc, io_handler *io, struct timeval *timeout) timeout->tv_sec=0; timeout->tv_usec=0; err = ETIMEDOUT; - update_error (cc, err, "Connection stuck during handshake: timeout reached (do_ssl_connect)"); + set_error (cc, err, "Connection stuck during handshake: timeout reached (do_ssl_connect)"); } else{ err = -1; //TODO set approp. error message - update_error (cc, err, "Error during SSL handshake (do_ssl_connect)"); + set_error (cc, err, "Error during SSL handshake (do_ssl_connect)"); } return err; } @@ -241,7 +236,11 @@ static int do_ssl_accept( glb_ctx *cc, io_handler *io, struct timeval *timeout) expected = errorcode = SSL_get_error(io->s_ctx->ssl_io, ret2); } curtime = time(NULL); - } while (TEST_SELECT(ret, ret2, locl_timeout, curtime, starttime, errorcode)); + } while (ret > 0 && (ret2 <= 0 && ((locl_timeout == -1) || + ((locl_timeout != -1) && + (curtime - starttime) < locl_timeout)) && + (errorcode == SSL_ERROR_WANT_READ || + errorcode == SSL_ERROR_WANT_WRITE))); //TODO split ret2 and ret into 2 ifs to set approp. error message if (ret2 <= 0 || ret <= 0) { @@ -249,11 +248,11 @@ static int do_ssl_accept( glb_ctx *cc, io_handler *io, struct timeval *timeout) timeout->tv_sec=0; timeout->tv_usec=0; err = ETIMEDOUT; - update_error (cc, err, "Connection stuck during handshake: timeout reached (do_ssl_accept)"); + set_error (cc, err, "Connection stuck during handshake: timeout reached (do_ssl_accept)"); } else{ err = -1; //TODO set approp. error message - update_error (cc, err, "Error during SSL handshake (do_ssl_accept)"); + set_error (cc, err, "Error during SSL handshake (do_ssl_accept)"); } return err; } @@ -290,7 +289,7 @@ int ssl_write(glb_ctx *cc, io_handler *io, void *buffer, size_t size, struct tim if (!buffer) { err = EINVAL; //TODO really? - update_error(cc, err, "Nothing to write (ssl_write)"); + set_error(cc, err, "Nothing to write (ssl_write)"); errno = err; return -1; } @@ -350,17 +349,17 @@ int ssl_write(glb_ctx *cc, io_handler *io, void *buffer, size_t size, struct tim end: if (err) { errno = err; - update_error (cc, err, "Error during SSL write (ssl_write)"); + set_error (cc, err, "Error during SSL write (ssl_write)"); return -1; } if (touted){ errno = err = ETIMEDOUT; - update_error(cc, err, "Connection stuck during write: timeout reached (ssl_write)"); + set_error(cc, err, "Connection stuck during write: timeout reached (ssl_write)"); return -1; } if (ret <=0){ err = -1;//TODO what to assign?????? - update_error (cc, err, "Error during SSL write (ssl_write)"); + set_error (cc, err, "Error during SSL write (ssl_write)"); } return ret; } @@ -390,7 +389,7 @@ int ssl_read(glb_ctx *cc, io_handler *io, void *buffer, size_t size, struct time if (!buffer) { err = EINVAL; //TODO really? - update_error(cc, err, "Not enough memory to read to (ssl_read)"); + set_error(cc, err, "Not enough memory to read to (ssl_read)"); errno = err; return -1; } @@ -421,10 +420,10 @@ end: if (ret <= 0 || ret2 <= 0) { //TODO ret2 < 0 originally err = -1; //TODO what to assign if (timeout != -1 && (curtime - starttime >= timeout)){ - update_error(cc, ETIMEDOUT, "Connection stuck during read: timeout reached. (ssl_read)"); + set_error(cc, ETIMEDOUT, "Connection stuck during read: timeout reached. (ssl_read)"); } else - update_error(cc, err, "Error during SSL read: (ssl_read)"); + set_error(cc, err, "Error during SSL read: (ssl_read)"); } else err = ret2; -- 1.8.2.3