errors, return values; ERR_clear_error where suitable
authorMarcel Poul <marcel.poul@cern.ch>
Sun, 4 Dec 2011 16:55:50 +0000 (16:55 +0000)
committerMarcel Poul <marcel.poul@cern.ch>
Sun, 4 Dec 2011 16:55:50 +0000 (16:55 +0000)
emi.canl.canl-c/src/canl_cert.c
emi.canl.canl-c/src/canl_sample_server.c
emi.canl.canl-c/src/canl_ssl.c

index 1bed7c2..6996144 100644 (file)
@@ -57,19 +57,25 @@ end:
 //TODO cert
 int do_set_ctx_own_cert_file(glb_ctx *cc, char *cert, char *key)
 {
+    int err = 0;
     /* otherwise the private key is in cert file*/
-    if (key)
-        set_key_file(cc, key);
-
-    if (cert)
-        set_cert_file(cc, cert);
+    if (key) {
+        err = set_key_file(cc, key);
+        if (err)
+            return err;
+    }
 
+    if (cert) {
+        err = set_cert_file(cc, cert);
+        if (err)
+            return err;
+    }
     return 0;
 }
 
 static int set_key_file(glb_ctx *cc, char *key)
 {
-    int err = 0;
+    unsigned long err = 0;
     FILE * key_file = NULL;
 
     if (!cc->cert_key){
@@ -93,6 +99,9 @@ static int set_key_file(glb_ctx *cc, char *key)
                 " (set_key_file)");
         return err;
     }
+
+    ERR_clear_error();
+
     /*TODO NULL NULL, callback and user data*/
     cc->cert_key->key = PEM_read_PrivateKey(key_file, NULL, NULL, NULL);
     if (!cc->cert_key->key) {
@@ -120,7 +129,7 @@ end:
 
 static int set_cert_file(glb_ctx *cc, char *cert)
 {
-    int err = 0;
+    unsigned long err = 0;
     FILE * cert_file = NULL;
 
     if (!cc->cert_key){
@@ -144,6 +153,8 @@ static int set_cert_file(glb_ctx *cc, char *cert)
                 " (set_cert_file)");
         return err;
     }
+    
+    ERR_clear_error();
     /*TODO NULL NULL, callback and user data*/
     cc->cert_key->cert = PEM_read_X509(cert_file, NULL, NULL, NULL);
     if (!cc->cert_key->cert) {
index 1a17d15..381cf20 100644 (file)
@@ -5,6 +5,7 @@
 #include "canl.h"
 
 #define BUF_LEN 1000
+static void print_error_from_canl(canl_ctx cc);
 
 int main(int argc, char *argv[])
 {
@@ -12,7 +13,6 @@ int main(int argc, char *argv[])
     canl_io_handler my_io_h;
     canl_io_handler my_new_io_h;
     int err = 0;
-    char *err_msg = NULL;
     int opt, port = 4321;
     char *serv_cert = NULL;
     char *serv_key = NULL;
@@ -44,26 +44,28 @@ int main(int argc, char *argv[])
 
     my_ctx = canl_create_ctx();
     if (!my_ctx){
-        // set_error("context cannot be created\n");
-        goto end;
+        printf("[SERVER] canl context cannot be created\n");
+        return -1;
     }
 
     my_io_h = canl_create_io_handler(my_ctx);
     if (!my_io_h) {
-        //set_error("io handler cannot be created\n");
+        printf("[SERVER] io handler cannot be created\n");
         goto end;
     }
 
     my_new_io_h = canl_create_io_handler(my_ctx);
     if (!my_new_io_h) {
-        //set_error("io handler cannot be created\n");
+        printf("[SERVER] io handler cannot be created\n");
         goto end;
     }
+
     if (serv_cert || serv_key){
         err = canl_set_ctx_own_cert_file(my_ctx, serv_cert, serv_key, 
                 NULL, NULL);
         if (err) {
             printf("[SERVER] cannot set certificate or key to context\n");
+            goto end;
         }
     }
 
@@ -101,37 +103,57 @@ int main(int argc, char *argv[])
         printf ("[SERVER] received: %s\n", buf);
     }
     else
-    printf("[SERVER] nothing received from client\n");
+        printf("[SERVER] nothing received from client\n");
 
+end:
+    print_error_from_canl(my_ctx);
 
-    err = canl_io_close(my_ctx, my_new_io_h);
-    if (err){
-    printf("[SERVER] Cannot close connection with client\n");
+    if (my_new_io_h) {
+        err = canl_io_close(my_ctx, my_new_io_h);
+        if (err){
+            printf("[SERVER] Cannot close connection\n");
+            print_error_from_canl(my_ctx);
+        }
     }
 
-    err = canl_io_destroy(my_ctx, my_new_io_h);
-    if (err){
-    printf("[SERVER] Cannot destroy connection with client\n");
-    }
-    my_new_io_h = NULL;
-    
-    err = canl_io_close(my_ctx, my_io_h);
-    if (err){
-    printf("[SERVER] Cannoc close listening socket\n");
+    if (my_new_io_h) {
+        err = canl_io_destroy(my_ctx, my_new_io_h);
+        if (err){
+            printf("[SERVER] Cannot destroy connection\n");
+            print_error_from_canl(my_ctx);
+        }
+        my_new_io_h = NULL;
     }
 
-    err = canl_io_destroy(my_ctx, my_io_h);
-    if (err){
-    printf("[SERVER] Trying destroy listening socket\n");
+    if (my_io_h) {
+        err = canl_io_close(my_ctx, my_io_h);
+        if (err){
+            printf("[SERVER] Cannot close connection\n");
+            print_error_from_canl(my_ctx);
+        }
     }
-    my_io_h = NULL;
 
-end:
-    canl_get_error(my_ctx, &err_msg);
-    if (err_msg != NULL)
-        printf("%s\n", err_msg);
+    if (my_io_h) {
+        err = canl_io_destroy(my_ctx, my_io_h);
+        if (err){
+            printf("[SERVER] Cannot destroy connection\n");
+            print_error_from_canl(my_ctx);
+        }
+        my_io_h = NULL;
+    }
 
     canl_free_ctx(my_ctx);
 
     return err;
 }
+
+static void print_error_from_canl(canl_ctx cc)
+{
+    char *reason = NULL;
+    canl_get_error(cc, &reason);
+    if (reason != NULL) {
+        printf("%s\n", reason);
+        free (reason);
+        reason = NULL;
+    }
+}
index 34b3def..421982f 100644 (file)
@@ -86,7 +86,7 @@ int ssl_server_init(glb_ctx *cc, io_handler *io)
         err = ERR_get_error();
         e_orig = ssl_error;
         set_error(cc, err, e_orig, "Private key does not match"
-                " the certificate public key");
+                " the certificate public key (ssl_server_init)");
         return -1;
     }
     else
@@ -119,6 +119,7 @@ int ssl_client_init(glb_ctx *cc, io_handler *io)
     SSL_library_init();
     //OpenSSL_add_all_algorithms();
     //OpenSSL_add_all_ciphers();
+    ERR_clear_error();
 
     cc->ssl_ctx = SSL_CTX_new(SSL_CLIENT_METH);
     if (!cc->ssl_ctx){
@@ -334,6 +335,7 @@ static int do_ssl_connect( glb_ctx *cc, io_handler *io, struct timeval *timeout)
     else
         locl_timeout = -1;
     curtime = starttime = time(NULL);
+    ERR_clear_error();
 
     do {
         ret = do_select(io->sock, starttime, locl_timeout, expected);
@@ -461,6 +463,7 @@ int ssl_write(glb_ctx *cc, io_handler *io, void *buffer, size_t size, struct tim
         to = 0;
         locl_timeout = -1;
     }
+    ERR_clear_error();
 
     do {
         ret = do_select(fd, starttime, locl_timeout, expected);
@@ -469,7 +472,8 @@ int ssl_write(glb_ctx *cc, io_handler *io, void *buffer, size_t size, struct tim
         if (ret > 0) {
             int v;
             errno = 0;
-            ret = SSL_write(io->s_ctx->ssl_io, str + nwritten, strlen(str) - nwritten);
+            ret = SSL_write(io->s_ctx->ssl_io, str + nwritten,
+                    strlen(str) - nwritten);
             v = SSL_get_error(io->s_ctx->ssl_io, ret);
 
             switch (v) {
@@ -508,13 +512,15 @@ end:
         return -1;
     }
     if (touted){
-       errno = err = ETIMEDOUT;
-       set_error(cc, err, posix_error, "Connection stuck during write: timeout reached (ssl_write)");
+       err = ETIMEDOUT;
+       set_error(cc, err, posix_error, "Connection stuck during"
+               " write: timeout reached (ssl_write)");
        return -1;
     }
     if (ret <=0){
         err = -1;//TODO what to assign??????
-        set_error (cc, err, unknown_error, "Error during SSL write (ssl_write)");
+        set_error (cc, err, unknown_error, "Error during SSL write"
+               " (ssl_write)");
     }
     return ret;
 }
@@ -544,6 +550,8 @@ int ssl_read(glb_ctx *cc, io_handler *io, void *buffer, size_t size, struct time
     }
     else
         timeout = -1;
+    ERR_clear_error();
+
     do {
         ret = do_select(fd, starttime, timeout, expected);
         curtime = time(NULL);
@@ -558,7 +566,6 @@ int ssl_read(glb_ctx *cc, io_handler *io, void *buffer, size_t size, struct time
         }
     } while (TEST_SELECT(ret, ret2, timeout, curtime, starttime, error));
 
-end:
     if (ret <= 0 || ret2 <= 0) { // what if ret2 == 0? conn closed?
         err = -1; //TODO what to assign
         if (timeout != -1 && (curtime - starttime >= timeout)){