Error handling improvements
authorDaniel Kouřil <kouril@ics.muni.cz>
Tue, 6 Dec 2011 14:21:08 +0000 (14:21 +0000)
committerDaniel Kouřil <kouril@ics.muni.cz>
Tue, 6 Dec 2011 14:21:08 +0000 (14:21 +0000)
emi.canl.canl-c/src/canl.h
emi.canl.canl-c/src/canl_err.c
emi.canl.canl-c/src/canl_sample_server.c
emi.canl.canl-c/src/canl_ssl.c

index f79d3c2..a0a97f6 100644 (file)
@@ -23,12 +23,13 @@ int canl_io_accept(canl_ctx cc, canl_io_handler io, int port, int flags, cred_ha
 size_t canl_io_read(canl_ctx cc, canl_io_handler io, void *buffer, size_t size, struct timeval *timeout);
 size_t canl_io_write(canl_ctx cc, canl_io_handler io, void *buffer, size_t size, struct timeval *timeout);
 
-int
+long
 canl_get_error_code(canl_ctx cc);
 
 char *
 canl_get_error_message(canl_ctx);
 
+int canl_get_error(canl_ctx cc, char ** reason);
 int canl_io_close(canl_ctx cc, canl_io_handler io);
 int canl_io_destroy(canl_ctx cc, canl_io_handler io);
 
index 2b21762..fd10f4f 100644 (file)
@@ -173,7 +173,7 @@ static void get_error_string(glb_ctx *cc, char *code_str)
     }
 }
 
-unsigned long
+long
 canl_get_error_code(canl_ctx cc)
 {
     glb_ctx *ctx = (glb_ctx*) cc;
@@ -185,7 +185,7 @@ canl_get_error_code(canl_ctx cc)
 }
 
 char * 
-canl_get_error_message(canl_ctx)
+canl_get_error_message(canl_ctx cc)
 {
     glb_ctx *ctx = (glb_ctx*) cc;
 
index 381cf20..18b2c23 100644 (file)
@@ -50,13 +50,15 @@ int main(int argc, char *argv[])
 
     my_io_h = canl_create_io_handler(my_ctx);
     if (!my_io_h) {
-        printf("[SERVER] io handler cannot be created\n");
+        printf("[SERVER] io handler cannot be created: %s\n",
+              canl_get_error_message(my_ctx));
         goto end;
     }
 
     my_new_io_h = canl_create_io_handler(my_ctx);
     if (!my_new_io_h) {
-        printf("[SERVER] io handler cannot be created\n");
+        printf("[SERVER] io handler cannot be created: %s\n",
+              canl_get_error_message(my_ctx));
         goto end;
     }
 
@@ -64,7 +66,8 @@ int main(int argc, char *argv[])
         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");
+            printf("[SERVER] cannot set certificate or key to context: %s\n",
+                  canl_get_error_message(my_ctx));
             goto end;
         }
     }
@@ -76,20 +79,20 @@ int main(int argc, char *argv[])
     /* TODO timeout in this function?*/
     err = canl_io_accept(my_ctx, my_io_h, port, 0, NULL, &timeout, &my_new_io_h);
     if (err) {
-        printf("[SERVER] connection cannot be established\n");
+        printf("[SERVER] connection cannot be established: %s\n",
+              canl_get_error_message(my_ctx));
         goto end;
     }
-    else {
-        printf("[SERVER] connection established\n");
-    }
+    printf("[SERVER] connection established\n");
 
-    strcpy(buf, "This is the testing message to send");
+    strncpy(buf, "This is a testing message to send", sizeof(buf));
     buf_len = strlen(buf) + 1;
 
     printf("[SERVER] Trying to send sth to the client\n");
     err = canl_io_write (my_ctx, my_new_io_h, buf, buf_len, &timeout);
     if (err <= 0) {
-        printf("[SERVER] cannot send message to the client\n");
+        printf("[SERVER] cannot send message to the client: %s\n",
+              canl_get_error_message(my_ctx));
         goto end;
     }
     else {
@@ -103,11 +106,10 @@ int main(int argc, char *argv[])
         printf ("[SERVER] received: %s\n", buf);
     }
     else
-        printf("[SERVER] nothing received from client\n");
+        printf("[SERVER] Failed to receive reply from client: %s\n",
+              canl_get_error_message(my_ctx));
 
 end:
-    print_error_from_canl(my_ctx);
-
     if (my_new_io_h) {
         err = canl_io_close(my_ctx, my_new_io_h);
         if (err){
index 3052d4b..280fb8a 100644 (file)
@@ -462,8 +462,7 @@ int ssl_write(glb_ctx *cc, io_handler *io, void *buffer, size_t size, struct tim
     int to = 0; // bool
 
     if (!io->s_ctx || !io->s_ctx->ssl_io) {
-        err = EINVAL;
-        set_error(cc, err, posix_error, "wrong ssl handler (ssl_read)");
+        set_error(cc, EINVAL, posix_error, "SSL not initialized");
         return -1;
     }