| 1 |
#include "canl_locl.h" |
| 2 |
|
| 3 |
#define ERR_CODE_LEN 512 |
| 4 |
|
| 5 |
static canl_err_code resolve_error_code(glb_ctx *cc, unsigned long err_code, |
| 6 |
canl_err_origin err_orig); |
| 7 |
static void get_error_string(glb_ctx *cc, char *code_str); |
| 8 |
static canl_err_code update_error_msg(canl_ctx cc, const char *new_msg); |
| 9 |
static const char *canl_strerror(const canl_err_code c_code); |
| 10 |
static int canl_err_ssl_to_canl(const unsigned long ossl_lib, |
| 11 |
const unsigned long ossl_reason); |
| 12 |
|
| 13 |
/* Save error message into err_msg |
| 14 |
* use NULL for empty err_format */ |
| 15 |
canl_err_code update_error (glb_ctx *cc, unsigned long err_code, |
| 16 |
canl_err_origin err_orig, |
| 17 |
const char *err_format, ...) |
| 18 |
{ |
| 19 |
int err_format_len = 0; |
| 20 |
va_list ap; |
| 21 |
char *new_msg = NULL; |
| 22 |
int ret = 0; |
| 23 |
|
| 24 |
if (!cc) |
| 25 |
return EINVAL; |
| 26 |
|
| 27 |
if (err_format == NULL || err_format[0] == '\0') { |
| 28 |
goto wo_msg; |
| 29 |
} |
| 30 |
|
| 31 |
va_start(ap, err_format); |
| 32 |
|
| 33 |
/*TODO if vasprintf not successful?*/ |
| 34 |
err_format_len = vasprintf(&new_msg, err_format, ap); |
| 35 |
if (!err_format_len) { |
| 36 |
va_end(ap); |
| 37 |
return EINVAL; |
| 38 |
} |
| 39 |
va_end(ap); |
| 40 |
|
| 41 |
wo_msg: |
| 42 |
ret = resolve_error_code(cc, err_code, err_orig); |
| 43 |
update_error_msg(cc, new_msg); |
| 44 |
if (new_msg) |
| 45 |
free(new_msg); |
| 46 |
|
| 47 |
return ret; |
| 48 |
} |
| 49 |
|
| 50 |
/* If there was some error message in ctx, delete it and make new */ |
| 51 |
canl_err_code set_error (glb_ctx *cc, unsigned long err_code, |
| 52 |
canl_err_origin err_orig, const char *err_format, ...) |
| 53 |
{ |
| 54 |
va_list ap; |
| 55 |
char *new_msg = NULL; |
| 56 |
int ret; |
| 57 |
int err_format_len = 0; |
| 58 |
|
| 59 |
if (!cc) |
| 60 |
return 1; |
| 61 |
/* if message already exists, delete it */ |
| 62 |
if (cc->err_msg) |
| 63 |
reset_error(cc, err_code); |
| 64 |
|
| 65 |
if (err_format == NULL || err_format[0] == '\0') { |
| 66 |
goto wo_msg; |
| 67 |
} |
| 68 |
|
| 69 |
/* make new message */ |
| 70 |
va_start(ap, err_format); |
| 71 |
err_format_len = vasprintf(&new_msg, err_format, ap); |
| 72 |
if (!err_format_len) { |
| 73 |
va_end(ap); |
| 74 |
return EINVAL; |
| 75 |
} |
| 76 |
va_end(ap); |
| 77 |
|
| 78 |
wo_msg: |
| 79 |
ret = resolve_error_code(cc, err_code, err_orig); |
| 80 |
update_error_msg(cc, new_msg); |
| 81 |
if (new_msg) |
| 82 |
free(new_msg); |
| 83 |
|
| 84 |
if (!err_code) //TODO ??? |
| 85 |
return 0; |
| 86 |
return ret; |
| 87 |
} |
| 88 |
|
| 89 |
/* Delete error message in ctx, suppose msg is not empty.Set pointer to NULL*/ |
| 90 |
void reset_error (glb_ctx *cc, unsigned long err_code) |
| 91 |
{ |
| 92 |
/*check cc*/ |
| 93 |
if (!cc ) |
| 94 |
return; |
| 95 |
if (cc->err_msg) |
| 96 |
free(cc->err_msg); |
| 97 |
cc->err_msg = NULL; |
| 98 |
cc->err_code = 0; |
| 99 |
cc->err_orig = UNKNOWN_ERROR; |
| 100 |
} |
| 101 |
|
| 102 |
/* Provide human readable information about errors */ |
| 103 |
static canl_err_code |
| 104 |
update_error_msg(canl_ctx cc, const char *new_msg) |
| 105 |
{ |
| 106 |
int error_length = 0; |
| 107 |
char *new_error = NULL; |
| 108 |
char code_str[ERR_CODE_LEN]; |
| 109 |
int code_len = 0; |
| 110 |
char *separ = ": "; |
| 111 |
char *separ_2 = "; "; |
| 112 |
int separ_len = 0; |
| 113 |
int separ_2_len = 0; |
| 114 |
int err_old_msg_len = 0; |
| 115 |
int err_new_msg_len = 0; |
| 116 |
glb_ctx *ctx = (glb_ctx*) cc; |
| 117 |
|
| 118 |
code_str[0] = '\0'; |
| 119 |
|
| 120 |
/*check cc*/ |
| 121 |
if (!ctx) { |
| 122 |
return EINVAL; |
| 123 |
} |
| 124 |
|
| 125 |
if (ctx->err_msg) |
| 126 |
err_old_msg_len = strlen(ctx->err_msg); |
| 127 |
|
| 128 |
if (new_msg) |
| 129 |
err_new_msg_len = strlen(new_msg); |
| 130 |
|
| 131 |
/* get human readable error code */ |
| 132 |
get_error_string(cc, code_str); |
| 133 |
code_len = strlen(code_str); |
| 134 |
|
| 135 |
separ_len = strlen(separ); |
| 136 |
separ_2_len = strlen(separ_2); |
| 137 |
error_length = err_new_msg_len + err_old_msg_len + code_len + |
| 138 |
(2*separ_len) + 1; |
| 139 |
new_error = (char *) malloc ((error_length) * sizeof (char)); |
| 140 |
if (!new_error) { |
| 141 |
return set_error(ctx, ENOMEM, POSIX_ERROR, "cannot get error message"); |
| 142 |
} |
| 143 |
|
| 144 |
new_error[0] = '\0'; |
| 145 |
if (new_msg) { |
| 146 |
strncpy(new_error, new_msg, err_new_msg_len + 1); |
| 147 |
strncat(new_error, separ, separ_len + 1); |
| 148 |
} |
| 149 |
strncat(new_error, code_str, code_len + 1); |
| 150 |
strncat(new_error, separ_2, separ_2_len + 1); |
| 151 |
if (ctx->err_msg) { |
| 152 |
strncat(new_error, ctx->err_msg, err_old_msg_len + 1); |
| 153 |
} |
| 154 |
|
| 155 |
if (ctx->err_msg) |
| 156 |
free(ctx->err_msg); |
| 157 |
ctx->err_msg = new_error; |
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
static void get_error_string(glb_ctx *cc, char *code_str) |
| 162 |
{ |
| 163 |
const char *new_str = NULL; |
| 164 |
|
| 165 |
switch (cc->err_orig) { |
| 166 |
case SSL_ERROR: |
| 167 |
ERR_error_string_n(cc->err_code, code_str, |
| 168 |
ERR_CODE_LEN); |
| 169 |
break; |
| 170 |
case POSIX_ERROR: |
| 171 |
new_str = strerror(cc->err_code); |
| 172 |
if (new_str) { |
| 173 |
strncpy(code_str, new_str, |
| 174 |
ERR_CODE_LEN); |
| 175 |
code_str[ERR_CODE_LEN - 1] = '\0'; |
| 176 |
} |
| 177 |
break; |
| 178 |
case NETDB_ERROR: |
| 179 |
new_str = hstrerror(cc->err_code); |
| 180 |
if (new_str) { |
| 181 |
strncpy(code_str, new_str, |
| 182 |
ERR_CODE_LEN); |
| 183 |
code_str[ERR_CODE_LEN - 1] = '\0'; |
| 184 |
} |
| 185 |
break; |
| 186 |
case CANL_ERROR: |
| 187 |
new_str = canl_strerror(cc->err_code); |
| 188 |
if (new_str) { |
| 189 |
strncpy(code_str, new_str, |
| 190 |
ERR_CODE_LEN); |
| 191 |
code_str[ERR_CODE_LEN - 1] = '\0'; |
| 192 |
} |
| 193 |
break; |
| 194 |
default: |
| 195 |
snprintf(code_str, ERR_CODE_LEN, |
| 196 |
"Unknown error origin (%u) of error %lu!", |
| 197 |
cc->err_orig, cc->err_code); |
| 198 |
break; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
static const char * |
| 203 |
canl_strerror(const canl_err_code c_code) |
| 204 |
{ |
| 205 |
const char *new_str = NULL; |
| 206 |
int k = 0; |
| 207 |
for (k = 0; k < canl_err_descs_num; k++) { |
| 208 |
if (canl_err_descs[k].code == c_code) { |
| 209 |
new_str = canl_err_descs[k].desc; |
| 210 |
} |
| 211 |
} |
| 212 |
return new_str; |
| 213 |
} |
| 214 |
|
| 215 |
/*return appropriate CANL_ERROR according to openssl error code or -1 if |
| 216 |
no one found */ |
| 217 |
static int |
| 218 |
canl_err_ssl_to_canl(const unsigned long ossl_lib, |
| 219 |
const unsigned long ossl_reason) |
| 220 |
{ |
| 221 |
int ret_err = -1; |
| 222 |
int k = 0; |
| 223 |
for (k = 0; k < canl_err_descs_num; k++) { |
| 224 |
if (canl_err_descs[k].openssl_lib == ossl_lib) { |
| 225 |
if (canl_err_descs[k].openssl_reason == ossl_reason) |
| 226 |
ret_err = canl_err_descs[k].code; |
| 227 |
} |
| 228 |
} |
| 229 |
return ret_err; |
| 230 |
} |
| 231 |
|
| 232 |
canl_err_code |
| 233 |
canl_get_error_code(canl_ctx cc) |
| 234 |
{ |
| 235 |
glb_ctx *ctx = (glb_ctx*) cc; |
| 236 |
|
| 237 |
if (ctx == NULL) |
| 238 |
return -1; |
| 239 |
|
| 240 |
return ctx->err_code; |
| 241 |
} |
| 242 |
|
| 243 |
char * |
| 244 |
canl_get_error_message(canl_ctx cc) |
| 245 |
{ |
| 246 |
glb_ctx *ctx = (glb_ctx*) cc; |
| 247 |
|
| 248 |
if (ctx == NULL) |
| 249 |
return "Context is not initialized"; |
| 250 |
|
| 251 |
return ctx->err_msg; |
| 252 |
} |
| 253 |
|
| 254 |
/*if the error code is known to canl, assign appropriate canl code |
| 255 |
TODO go through ssl errors and assign appr. canl code |
| 256 |
?preserve original one? */ |
| 257 |
static canl_err_code resolve_error_code(glb_ctx *cc, unsigned long err_code, |
| 258 |
canl_err_origin err_orig) |
| 259 |
{ |
| 260 |
int ret = 0; |
| 261 |
cc->original_err_code = err_code; |
| 262 |
cc->err_orig = err_orig; |
| 263 |
|
| 264 |
switch (err_orig) { |
| 265 |
case UNKNOWN_ERROR: |
| 266 |
cc->err_code = (err_code) ? CANL_ERR_unknown : 0; |
| 267 |
break; |
| 268 |
case POSIX_ERROR: |
| 269 |
/* We don't use that many posix-like codes, perhaps we want to |
| 270 |
* map them to CANL codes. */ |
| 271 |
cc->err_code = err_code; |
| 272 |
break; |
| 273 |
case SSL_ERROR: |
| 274 |
/* TODO What about CANL_ERR_GeneralSSLError ?*/ |
| 275 |
if ((ret = canl_err_ssl_to_canl(ERR_GET_LIB(err_code), |
| 276 |
ERR_GET_REASON(err_code))) != -1){ |
| 277 |
cc->err_code = ret; |
| 278 |
cc->err_orig = CANL_ERROR; |
| 279 |
} |
| 280 |
cc->err_code = err_code; |
| 281 |
break; |
| 282 |
case CANL_ERROR: |
| 283 |
cc->err_code = err_code; |
| 284 |
break; |
| 285 |
case NETDB_ERROR: |
| 286 |
switch (cc->err_code) { |
| 287 |
case HOST_NOT_FOUND: |
| 288 |
cc->err_code = CANL_ERR_hostNotFound; |
| 289 |
break; |
| 290 |
default: |
| 291 |
cc->err_code = CANL_ERR_resolverError; |
| 292 |
break; |
| 293 |
} |
| 294 |
break; |
| 295 |
default: |
| 296 |
cc->err_code = CANL_ERR_unknown; |
| 297 |
cc->err_orig = UNKNOWN_ERROR; |
| 298 |
} |
| 299 |
|
| 300 |
return cc->err_code; |
| 301 |
} |