From: Marcel Poul Date: Mon, 28 May 2012 23:10:37 +0000 (+0000) Subject: canl_io_read and write have to return # bytes written/read (-1 if an error occured) X-Git-Tag: merge_32_head_dst~11 X-Git-Url: http://scientific.zcu.cz/git/?a=commitdiff_plain;h=f68b745bcf717d5fecb2acbbc65158fd2ecfc4f2;p=jra1mw.git canl_io_read and write have to return # bytes written/read (-1 if an error occured) --- diff --git a/emi.canl.canl-c/examples/canl_sample_server.c b/emi.canl.canl-c/examples/canl_sample_server.c index feea7ba..a421698 100644 --- a/emi.canl.canl-c/examples/canl_sample_server.c +++ b/emi.canl.canl-c/examples/canl_sample_server.c @@ -178,6 +178,7 @@ int main(int argc, char *argv[]) printf("[SERVER] message \"%s\" sent successfully\n", buf); } + buf[0] = '\0'; err = canl_io_read (my_ctx, my_io_h, buf, sizeof(buf)-1, &timeout); if (err <= 0) { printf("[SERVER] Failed to receive reply from client: %s\n", diff --git a/emi.canl.canl-c/src/canl_locl.h b/emi.canl.canl-c/src/canl_locl.h index fe135d6..64ccc9f 100644 --- a/emi.canl.canl-c/src/canl_locl.h +++ b/emi.canl.canl-c/src/canl_locl.h @@ -104,10 +104,10 @@ typedef struct canl_mech { canl_err_code (*close) (glb_ctx *, io_handler *, void *); - canl_err_code (*read) + size_t (*read) (glb_ctx *, io_handler *, void *, void *, size_t, struct timeval *); - canl_err_code (*write) + size_t (*write) (glb_ctx *, io_handler *, void *, void *, size_t, struct timeval *); canl_err_code (*get_peer) diff --git a/emi.canl.canl-c/src/canl_ssl.c b/emi.canl.canl-c/src/canl_ssl.c index 6d8045c..37d7ad9 100644 --- a/emi.canl.canl-c/src/canl_ssl.c +++ b/emi.canl.canl-c/src/canl_ssl.c @@ -878,11 +878,10 @@ timeout->tv_sec = timeout->tv_sec - (curtime - starttime); } /* this function has to return # bytes written or ret < 0 when sth went wrong*/ -static canl_err_code +static size_t ssl_write(glb_ctx *cc, io_handler *io, void *auth_ctx, void *buffer, size_t size, struct timeval *timeout) { - int err = 0; int ret = 0, nwritten=0, ret2 = 0; const char *str; int fd = -1; @@ -892,14 +891,18 @@ ssl_write(glb_ctx *cc, io_handler *io, void *auth_ctx, SSL *ssl = (SSL *) auth_ctx; if (cc == NULL) - return EINVAL; + return -1; - if (io == NULL) - return set_error(cc, EINVAL, POSIX_ERROR, - "Connection not established"); + if (io == NULL) { + set_error(cc, EINVAL, POSIX_ERROR, + "Connection not established"); + return -1; + } - if (ssl == NULL) - return set_error(cc, EINVAL, POSIX_ERROR, "SSL not initialized"); + if (ssl == NULL) { + set_error(cc, EINVAL, POSIX_ERROR, "SSL not initialized"); + return -1; + } fd = BIO_get_fd(SSL_get_rbio(ssl), NULL); str = buffer;//TODO !!!!!! text.c_str(); @@ -935,25 +938,26 @@ end: if (timeout) timeout->tv_sec = timeout->tv_sec - (curtime - starttime); if (ret <= 0 || ret2 <= 0) { // what if ret2 == 0? conn closed? - err = -1; //TODO what to assign if (locl_timeout != -1 && (curtime - starttime >= locl_timeout)){ - timeout->tv_sec = 0; - timeout->tv_usec = 0; - return set_error(cc, ETIMEDOUT, POSIX_ERROR, "Connection stuck" - " during write: timeout reached"); + timeout->tv_sec = 0; + timeout->tv_usec = 0; + set_error(cc, ETIMEDOUT, POSIX_ERROR, "Connection stuck" + " during write: timeout reached"); + return -1; + } + else { + set_error(cc, 0, UNKNOWN_ERROR, "Error during SSL write"); + return -1; } - else - return set_error(cc, err, UNKNOWN_ERROR, "Error during SSL write"); } return ret2; } -static canl_err_code +static size_t ssl_read(glb_ctx *cc, io_handler *io, void *auth_ctx, void *buffer, size_t size, struct timeval *tout) { - int err = 0; int ret = 0, nwritten=0, ret2 = 0; char *str; int fd = -1; @@ -963,14 +967,18 @@ ssl_read(glb_ctx *cc, io_handler *io, void *auth_ctx, SSL *ssl = (SSL *) auth_ctx; if (cc == NULL) - return EINVAL; + return -1; - if (io == NULL) - return set_error(cc, EINVAL, POSIX_ERROR, - "Connection not established"); + if (io == NULL) { + set_error(cc, EINVAL, POSIX_ERROR, + "Connection not established"); + return -1; + } - if (ssl == NULL) - return set_error(cc, EINVAL, POSIX_ERROR, "SSL not initialized"); + if (ssl == NULL) { + set_error(cc, EINVAL, POSIX_ERROR, "SSL not initialized"); + return -1; + } fd = BIO_get_fd(SSL_get_rbio(ssl), NULL); str = buffer;//TODO !!!!!! text.c_str(); @@ -1000,19 +1008,20 @@ ssl_read(glb_ctx *cc, io_handler *io, void *auth_ctx, if (tout) tout->tv_sec = tout->tv_sec - (curtime - starttime); if (ret <= 0 || ret2 <= 0) { // what if ret2 == 0? conn closed? - err = -1; //TODO what to assign if (timeout != -1 && (curtime - starttime >= timeout)){ tout->tv_sec = 0; tout->tv_usec = 0; set_error(cc, ETIMEDOUT, POSIX_ERROR, "Connection stuck" " during read: timeout reached"); + return -1; + } + else { + set_error(cc, 1, UNKNOWN_ERROR, "Error during SSL read"); + return -1; } - else - set_error(cc, err, UNKNOWN_ERROR, "Error during SSL read"); } - else - err = ret2; - return err; + + return ret2; } /* ret > 1 if connection does not exist or has been closed before