error message processing routines added
authorMarcel Poul <marcel.poul@cern.ch>
Sun, 6 Nov 2011 22:53:12 +0000 (22:53 +0000)
committerMarcel Poul <marcel.poul@cern.ch>
Sun, 6 Nov 2011 22:53:12 +0000 (22:53 +0000)
emi.canl.canl-c/Makefile
emi.canl.canl-c/src/canl.c
emi.canl.canl-c/src/canl.h
emi.canl.canl-c/src/canl_dns.c
emi.canl.canl-c/src/canl_err.c
emi.canl.canl-c/src/canl_err.h [new file with mode: 0644]
emi.canl.canl-c/src/canl_locl.h
emi.canl.canl-c/src/canl_sample_client.c
emi.canl.canl-c/src/canl_sample_server.c

index 07ec35a..36185c9 100644 (file)
@@ -24,7 +24,7 @@ OBJ_SER=canl_sample_server.o
 libcanl.so: canl.o canl_err.o canl_dns.o
        ${CC} canl.o canl_err.o canl_dns.o ${LFLAGS_LIB}
 
-canl.o: ${PATH_SRC}/canl.c ${HEAD_CANL}
+canl.o: ${PATH_SRC}/canl.c ${HEAD_CANL} ${PATH_SRC}/canl_err.h
        ${CC} ${PATH_SRC}/canl.c ${CFLAGS_LIB}
 
 canl_dns.o: ${PATH_SRC}/canl_dns.c ${HEAD_CANL}
@@ -36,13 +36,13 @@ canl_err.o: ${PATH_SRC}/canl_err.c ${HEAD_CANL}
 client: ${OBJ_CLI}
        ${CC} ${OBJ_CLI} ${LFLAGS_CLI}
 
-${OBJ_CLI}: ${SRC_CLI} ${HEAD_CLI}
+${OBJ_CLI}: ${SRC_CLI} ${HEAD_CLI} ./libcanl.so
        ${CC} ${SRC_CLI} ${CFLAGS_CLI} 
 
 server: ${OBJ_SER}
        ${CC} ${OBJ_SER} ${LFLAGS_SER}
 
-${OBJ_SER}: ${SRC_SER} ${HEAD_SER}
+${OBJ_SER}: ${SRC_SER} ${HEAD_SER} ./libcanl.so
        ${CC} ${SRC_SER} ${CFLAGS_SER} 
 
 clean:
index e35718b..02db50c 100644 (file)
@@ -5,14 +5,14 @@
 
 canl_ctx canl_create_ctx()
 {
-    struct glb_ctx *new_ctx = NULL;
+    glb_ctx *ctx = NULL;
     int err = 0;
 
     /*create context*/
-    new_ctx = (struct glb_ctx *) malloc(sizeof(*new_ctx));
-    if (!new_ctx) {
+    ctx = (glb_ctx *) malloc(sizeof(*ctx));
+    if (!ctx) {
         err=1; //use errno instead
-        //set_error(ctx->err_msg);
+        //set_error(ctx);
         goto end;
     }
 
@@ -24,18 +24,20 @@ canl_ctx canl_create_ctx()
      */
 
     /*initial values ...*/
-    new_ctx->io_ctx = NULL;
-    new_ctx->err_msg = NULL;
+    ctx->io_ctx = NULL;
+    ctx->err_msg = NULL;
+    ctx->err_code = no_error;
+
 end:
     if (err)
         return NULL;
     else
-        return new_ctx;
+        return ctx;
 }
 
 void canl_free_ctx(canl_ctx cc)
 {
-    struct glb_ctx *ctx = (struct glb_ctx*) cc;
+    glb_ctx *ctx = (glb_ctx*) cc;
 
     if (!cc) {
         goto end;
@@ -63,14 +65,14 @@ end:
 
 canl_io_handler canl_create_io_handler(canl_ctx cc)
 {
-    struct io_handler *new_io_h = NULL;
+    io_handler *new_io_h = NULL;
 
     if (!cc) {
         goto end;
     }
 
     /*create io handler*/
-    new_io_h = (struct io_handler *) malloc(sizeof(*new_io_h));
+    new_io_h = (io_handler *) malloc(sizeof(*new_io_h));
     if (!new_io_h)
         //set_error(ctx->err_msg);
         goto end;
@@ -85,8 +87,8 @@ int canl_io_connect(canl_ctx cc, canl_io_handler io, char * host, int port,
         int flags, cred_handler ch, struct timeval *timeout)
 {
     int err;
-    struct io_handler *io_cc = (struct io_handler*) io;
-    struct glb_ctx *glb_cc = (struct glb_ctx*) cc;
+    io_handler *io_cc = (io_handler*) io;
+    glb_ctx *glb_cc = (glb_ctx*) cc;
 
     /*check cc and io*/
     if (!cc) {
@@ -121,8 +123,8 @@ int canl_io_accept(canl_ctx cc, canl_io_handler io, int port,
         canl_io_handler *new_io)
 {
     int err;
-    struct io_handler *io_cc = (struct io_handler*) io;
-    struct glb_ctx *glb_cc = (struct glb_ctx*) cc;
+    io_handler *io_cc = (io_handler*) io;
+    glb_ctx *glb_cc = (glb_ctx*) cc;
 
     /*check cc and io*/
     if (!cc) {
@@ -239,19 +241,3 @@ size_t canl_io_write(canl_ctx cc, canl_io_handler io, void *buffer, size_t size,
 end:
     return err;
 }
-
-/* what about reason pointer? */
-size_t canl_io_get_error(canl_ctx cc, char ** reason)
-{
-    int err = 0;
-    if (!cc) {
-        err = 1;
-        goto end;
-    }
-
-    struct glb_ctx *my_ctx = (struct glb_ctx*) cc;
-    *reason = my_ctx->err_msg;
-
-end:
-    return err;
-}
index 80fe9c1..17ce298 100644 (file)
@@ -1,6 +1,8 @@
-#ifndef CANL_H
-#define CANL_H
+#ifndef _CANL_H
+#define _CANL_H
 #include <sys/time.h>
+#include "canl_err.h"
+
 typedef void *canl_io_handler;
 typedef void *canl_ctx;
 typedef void *cred_handler;
@@ -16,7 +18,7 @@ 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);
 
-size_t canl_io_get_error(canl_ctx cc, char ** reason);
+size_t 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 2110a95..84a47d0 100644 (file)
 static void free_hostent(struct hostent *h);
 static int decrement_timeout(struct timeval *timeout, struct timeval before, struct timeval after);
 
-#if ARES_VERSION >= 0x010500
+//#if ARES_VERSION >= 0x010500
 static void callback_ares_gethostbyname(void *arg, int status, int timeouts, struct hostent *h)
-#else
-static void callback_ares_gethostbyname(void *arg, int status, struct hostent *h)
-#endif
+//#else
+//static void callback_ares_gethostbyname(void *arg, int status, struct hostent *h)
+//#endif
 {
-    struct asyn_result *arp = (struct asyn_result *) arg;
+    asyn_result *arp = (asyn_result *) arg;
     int n_addr = 0;
     int i = 0;
 
@@ -98,7 +98,7 @@ static void free_hostent(struct hostent *h)
     }
 }
 
-int asyn_getservbyname(int a_family, struct asyn_result *ares_result,char const *name, 
+int asyn_getservbyname(int a_family, asyn_result *ares_result,char const *name, 
         struct timeval *timeout)
 {
     int err;
index b56fa2e..72dba1a 100644 (file)
 #include <stdlib.h>
 #include <stdarg.h>
 #include <string.h>
+#include "canl.h"
 #include "canl_locl.h"
+static int make_err_msg(char **strp, const char *fmt, va_list ap);
+
+static int make_err_msg(char **strp, const char *fmt, va_list ap)
+{
+    int err_format_len = 0;
+    int ret_val = 0;
+
+    err_format_len = vsnprintf(NULL, 0, fmt, ap );
+    if (err_format_len < 1)
+        return 0;
+    *strp = (char*) malloc ( (err_format_len +1) * sizeof(char));
+    if (!(*strp))
+        return 0;
+    ret_val = vsprintf(*strp, fmt, ap);
+    if (ret_val != err_format_len) {
+        free (*strp);
+        *strp = NULL;
+        return 0;
+    }
+    return ret_val;
+}
 
 /* Save error message into err_msg
  * use NULL for empty err_format */
-void make_err_msg (char **err_msg, const char *err_format, ...)
+void update_error (glb_ctx *cc, CANL_ERROR err_code, const char *err_format, ...)
 {
     unsigned int err_msg_len = 0;
-    va_list ap;
-    char *new_msg = NULL;
-    int err_format_len = 0;
     unsigned int err_msg_sum = 0; // sum of msg and format lengths
+    int err_format_len = 0;
+    va_list ap;
+    char *new_msg;
 
     if (err_format == NULL)
         return;
 
-    if (*err_msg != NULL)
-        err_msg_len = strlen(*err_msg);
-
-    /* make new error message */
     va_start(ap, err_format);
-    err_format_len = vsnprintf(NULL, 0, err_format, ap );
-    new_msg = (char*) malloc ( (err_format_len +1) * sizeof(char));
-    vsprintf(new_msg, err_format, ap);
-    if (err_format_len < 1)
+
+    if (!(*cc->err_msg)) {
+        make_err_msg(&cc->err_msg,err_format, ap);
+        va_end(ap);
         return;
+    }
+    err_format_len = make_err_msg(&new_msg, err_format, ap);
+
+    err_msg_len = strlen(cc->err_msg);
 
     /* Add new error message to older one */
     err_msg_sum = err_format_len + err_msg_len;
     /* separator ; and ending '\0' -> 2 bytes */
-    *err_msg = (char *) realloc (*err_msg, (err_msg_sum + 2)*sizeof(char));
-    if (*err_msg == NULL)
-    {
-        free(new_msg);
+    cc->err_msg = (char *) realloc (cc->err_msg, (err_msg_sum + 2)*sizeof(char));
+    if (cc->err_msg == NULL)
         return;
-    }
-    if (err_msg_len == 0)
-        (*err_msg)[0] = '\0';
-    strcat (*err_msg, new_msg);
-    strcat (*err_msg, ";");
+
+    strcat (cc->err_msg, ";");
+    strcat (cc->err_msg, new_msg);
+
+    cc->err_code = err_code;
 
     free(new_msg);
 }
+
+/* If there was some error message in ctx, delete it and make new */
+void set_error (glb_ctx *cc, CANL_ERROR err_code, const char *err_format, ...)
+{
+    va_list ap;
+    /*check cc*/
+    if (!cc) 
+        return;
+    /* if message already exists, delete it */
+    if (cc->err_msg)
+        reset_error(cc, err_code);
+
+    /* make new message */
+    va_start(ap, err_format);
+    make_err_msg(&cc->err_msg, err_format, ap);
+    va_end(ap);
+
+    cc->err_code = err_code;
+}
+
+/* Delete error message in ctx, suppose msg is not empty.Set pointer to NULL*/
+void reset_error (glb_ctx *cc, CANL_ERROR err_code)
+{
+    /*check cc*/
+    if (!cc )
+        return;
+    if (cc->err_msg)
+        free(cc->err_msg);
+    cc->err_msg = NULL;
+    cc->err_code = no_error;
+}
+
+/* Provide human readable information about errors */
+size_t canl_get_error(canl_ctx cc, char  **reason)
+{
+    int err = 0;
+    int error_length = 0;
+    char *new_error = NULL;
+    glb_ctx *ctx = (glb_ctx*) cc;
+
+    /*check cc*/
+    if (!ctx) {
+        err = 1;
+        goto end;
+    }
+
+    if (!ctx->err_msg) {
+        err = 1;
+        goto end;
+    }
+
+    error_length = strlen(ctx->err_msg);
+    new_error = (char *) malloc ((error_length + 1) * sizeof (char));
+    if (!new_error) {
+        err = 1; //TODO errno
+        goto end;
+    }
+
+    strncpy(new_error, ctx->err_msg, error_length + 1);
+    *reason = new_error;
+
+end:
+    return err;
+}
diff --git a/emi.canl.canl-c/src/canl_err.h b/emi.canl.canl-c/src/canl_err.h
new file mode 100644 (file)
index 0000000..4a7c525
--- /dev/null
@@ -0,0 +1,101 @@
+#ifndef CANL_ERR_H
+#define CANL_ERR_H
+
+typedef enum _CANL_ERROR
+{
+    no_error = 0,
+    emptyCertPath = 1000,
+    ncSubjectNameError,
+    notPermittedDN,
+    excludedDN,
+    subjAltNameExtError,
+    notPermittedEmail,
+    notPermittedEmailnew,
+    excludedEmail,
+/*    notPermittedDN,
+    excludedDN,
+*/    notPermittedIP,
+    excludedIP,
+    ncExtError,
+    pathLenghtExtended,
+    processLengthConstError,
+    totalPathLength,
+    certPathValidDate,
+    conflictingTrustAnchors,
+    noTrustAnchorFound,
+    trustButInvalidCert,
+    unknown,
+    trustDNInvalid,
+    trustPubKeyError,
+//    signatureNotVerified,
+    rootKeyIsValidButNotATrustAnchor,
+    signatureNotVerified,
+    NoIssuerPublicKey,
+    certificateNotYetValid,
+    certificateExpired,
+    crlDistPtExtError,
+    crlAuthInfoAccError,
+    crlDistPoint,
+    ocspLocation,
+    certWrongIssuer,
+    noCACert,
+    noBasicConstraints,
+    errorProcesingBC,
+    noCertSign,
+    pubKeyError,
+    policyExtError,
+//    policyQualifierError,
+    policyQualifierError,
+    noValidPolicyTree,
+    policyMapExtError,
+    invalidPolicyMapping,
+//    invalidPolicyMapping,
+//    policyExtError,
+//    policyQualifierError,
+    policyConstExtError,
+    policyInhibitExtError,
+//    policyConstExtError,
+    explicitPolicy,
+//    explicitPolicy,
+    invalidPolicy,
+    certPathCheckerError,
+    criticalExtensionError,
+    unknownCriticalExt,
+    QcEuCompliance,
+    QcSSCD,
+    QcLimitValueAlpha,
+    QcLimitValueNum,
+    QcUnknownStatement,
+    QcStatementExtError,
+    crlIssuerException,
+    noCrlInCertstore,
+    crlExtractionError,
+    localValidCRL,
+    localInvalidCRL,
+    onlineCRLWrongCA,
+    onlineValidCRL,
+    onlineInvalidCRL,
+    noCrlSigningPermited,
+    crlVerifyFailed,
+    crlNoIssuerPublicKey,
+    crlReasonExtError,
+    certRevoked,
+    revokedAfterValidation,
+    notRevoked,
+    crlUpdateAvailable,
+    distrPtExtError,
+    deltaCrlExtError,
+//    crlIssuerException,
+    crlNbrExtError,
+//    crlExtractionError,
+//    distrPtExtError,
+    noBaseCRL,
+    crlBCExtError,
+    crlOnlyUserCert,
+    crlOnlyCaCert,
+    crlOnlyAttrCert,
+    noValidCrlFound,
+    loadCrlDistPointError,
+    trustAnchorIssuerError
+} CANL_ERROR;
+#endif
index 5831123..53ec1be 100644 (file)
@@ -1,11 +1,14 @@
-#ifndef CANL_CBIND_H
-#define CANL_CBIND_H
-struct glb_ctx
+#ifndef CANL_LOCL_H
+#define CANL_LOCL_H
+#include "canl_err.h"
+
+typedef struct _glb_ctx
 {
     int opened_ios;
     struct io_handler * io_ctx;
     char * err_msg;
-};
+    CANL_ERROR err_code;
+} glb_ctx;
 /*
    struct ossl_ctx
    {
@@ -14,16 +17,17 @@ struct glb_ctx
    SSL ssl_conn_ctx;
    }
  */
-struct io_handler
+typedef struct _io_handler
 {
     int something;
-};
+} io_handler;
 
-struct asyn_result {
+typedef struct _asyn_result {
     struct hostent *ent;
     int err;
-};
-
-void make_err_msg (char **err_msg, const char *err_format, ...);
-
+} asyn_result;
 #endif
+
+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, ...);
index afba402..081a663 100644 (file)
@@ -47,7 +47,7 @@ int main()
     }
 
 end:
-    canl_io_get_error(my_ctx, &err_msg);
+    canl_get_error(my_ctx, &err_msg);
     if (err_msg != NULL)
         printf("%s\n", err_msg);
 
index 1609daf..3a4aec6 100644 (file)
@@ -54,7 +54,7 @@ int main()
     }
 
 end:
-    canl_io_get_error(my_ctx, &err_msg);
+    canl_get_error(my_ctx, &err_msg);
     if (err_msg != NULL)
         printf("%s\n", err_msg);