update_error can be be passed err codes, too
authorDaniel Kouřil <kouril@ics.muni.cz>
Tue, 17 Jan 2012 20:10:56 +0000 (20:10 +0000)
committerDaniel Kouřil <kouril@ics.muni.cz>
Tue, 17 Jan 2012 20:10:56 +0000 (20:10 +0000)
emi.canl.canl-c/src/canl.c
emi.canl.canl-c/src/canl_cert.c
emi.canl.canl-c/src/canl_err.c
emi.canl.canl-c/src/canl_locl.h
emi.canl.canl-c/src/canl_ssl.c

index 528e834..60cd888 100644 (file)
@@ -118,10 +118,8 @@ canl_io_connect(canl_ctx cc, canl_io_handler io, const char *host, const char *s
                 err = 0;
                 break;
             case TRY_AGAIN:
-                update_error(glb_cc, ETIMEDOUT, posix_error,
+                err = update_error(glb_cc, ETIMEDOUT, posix_error,
                         "Cannot resolve the server hostname (%s)", host);
-               err = set_error(glb_cc, ECONNREFUSED, posix_error,
-                       "Failed to make network connection to server %s", host);
                goto end;
             case NETDB_INTERNAL:
                err = update_error(glb_cc, errno, posix_error,
@@ -151,15 +149,16 @@ canl_io_connect(canl_ctx cc, canl_io_handler io, const char *host, const char *s
            break;
     }
 
-    if (err) {
-        err = set_error(glb_cc, ECONNREFUSED, posix_error,
-                "Failed to make network connection to server %s", host);
+    if (err)
        goto end;
-    }
 
     err = 0;
 
 end:
+    if (err) /* XXX: rather invent own error */
+       err = update_error(glb_cc, ECONNREFUSED, posix_error,
+               "Failed to make network connection to server %s", host);
+
     if (ar.ent != NULL)
         free_hostent(ar.ent);
 
@@ -315,6 +314,7 @@ canl_io_destroy(canl_ctx cc, canl_io_handler io)
     return err;
 }
 
+/* XXX: 0 returned returned by ssl_read() means error or EOF ? */
 size_t canl_io_read(canl_ctx cc, canl_io_handler io, void *buffer, size_t size, struct timeval *timeout)
 {
     io_handler *io_cc = (io_handler*) io;
@@ -335,9 +335,7 @@ size_t canl_io_read(canl_ctx cc, canl_io_handler io, void *buffer, size_t size,
     }
 
     b_recvd = ssl_read(glb_cc, io_cc, buffer, size, timeout);
-    if (b_recvd <= 0) {
-       update_error(glb_cc, "Can't read from connection");
-    }
+
     return b_recvd;
 }
 
@@ -361,9 +359,7 @@ size_t canl_io_write(canl_ctx cc, canl_io_handler io, void *buffer, size_t size,
     }
 
     b_written = ssl_write(glb_cc, io_cc, buffer, size, timeout);
-    if (b_written <= 0) {
-        update_error(glb_cc, "Can't write to connection");
-    }
+
     return b_written;
 }
 
index 2d834e1..2dfd018 100644 (file)
@@ -121,7 +121,7 @@ static int set_key_file(glb_ctx *cc, char *key)
 end:
     if (fclose(key_file)){
         err = errno;
-        update_error(cc, "cannot close file with key");
+        update_error(cc, errno, posix_error, "cannot close file with key");
     }
     return 1;
 }
@@ -164,7 +164,7 @@ static int set_cert_file(glb_ctx *cc, char *cert)
 end:
     if (fclose(cert_file)){
         err = errno;
-        update_error(cc, "cannot close file with certificate");
+        update_error(cc, errno, posix_error, "cannot close file with certificate");
     }
     return 1;
 }
index 08683ce..97c0f50 100644 (file)
@@ -6,9 +6,11 @@ static CANL_ERROR resolve_error(glb_ctx *cc, unsigned long err_code,
         CANL_ERROR_ORIGIN err_orig);
 static void get_error_string(glb_ctx *cc, char *code_str);
 
+/* TODO: produce error messages immediately (to chain them) */
 /* Save error message into err_msg
  * use NULL for empty err_format */
-void update_error (glb_ctx *cc,  const char *err_format, ...)
+int update_error (glb_ctx *cc, unsigned long err_code, CANL_ERROR_ORIGIN err_orig,
+                  const char *err_format, ...)
 {
     unsigned int err_msg_len = 0;
     unsigned int err_msg_sum = 0; // sum of msg and format lengths
@@ -20,10 +22,10 @@ void update_error (glb_ctx *cc,  const char *err_format, ...)
     char *old_msg = NULL;
 
     if (!cc)
-        return;
+        return EINVAL;
 
     if (err_format == NULL) {
-        return;
+        return EINVAL;
     }
     separator_len = strlen(separator);
 
@@ -32,7 +34,7 @@ void update_error (glb_ctx *cc,  const char *err_format, ...)
     if (!(cc->err_msg)) {
         vasprintf(&cc->err_msg, err_format, ap);
         va_end(ap);
-        return;
+        return resolve_error(cc, err_code, err_orig);
     }
     err_format_len = vasprintf(&new_msg, err_format, ap);
 
@@ -43,7 +45,7 @@ void update_error (glb_ctx *cc,  const char *err_format, ...)
     err_msg_sum = err_format_len + err_msg_len + separator_len + 1;
     cc->err_msg = (char *) malloc ((err_msg_sum)*sizeof(char));
     if (cc->err_msg == NULL)
-        return;
+        return ENOMEM;
 
     strncpy(cc->err_msg, new_msg, err_format_len + 1);
     strncat (cc->err_msg, separator, separator_len + 1);
@@ -51,6 +53,8 @@ void update_error (glb_ctx *cc,  const char *err_format, ...)
 
     free(new_msg);
     free(old_msg);
+
+    return resolve_error(cc, err_code, err_orig);
 }
 
 /* If there was some error message in ctx, delete it and make new */
index 4c8d405..7b40cf5 100644 (file)
@@ -126,7 +126,8 @@ extern struct canl_mech canl_mech_ssl;
 void reset_error (glb_ctx *cc, unsigned long err_code);
 int set_error (glb_ctx *cc, unsigned long err_code, CANL_ERROR_ORIGIN err_orig,
         const char *err_format, ...);
-void update_error (glb_ctx *cc, const char *err_format, ...);
+int update_error (glb_ctx *cc, unsigned long err_code, CANL_ERROR_ORIGIN err_orig,
+       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);
index f687c36..ea21047 100644 (file)
@@ -765,7 +765,7 @@ canl_ctx_set_ssl_cred(canl_ctx cc, char *cert, char *key,
 
     err = do_set_ctx_own_cert_file(glb_cc, cert, key);
     if(err) {
-        update_error(glb_cc, "can't set cert or key to context");
+//        update_error(glb_cc, "can't set cert or key to context");
     }
     return err;
 }