Rework error interface

This commit is contained in:
2023-01-12 12:54:23 +01:00
parent 4814035b62
commit 5acab8144d
9 changed files with 148 additions and 100 deletions

View File

@@ -7,59 +7,23 @@
*/
#include "error.h"
#include "http.h"
#include "../logger.h"
#include <errno.h>
#include <string.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <maxminddb.h>
static const char *error_ssl_strerror(int err) {
switch (err) {
case SSL_ERROR_ZERO_RETURN:
return "closed";
case SSL_ERROR_WANT_READ:
return "want read";
case SSL_ERROR_WANT_WRITE:
return "want write";
case SSL_ERROR_WANT_CONNECT:
return "want connect";
case SSL_ERROR_WANT_ACCEPT:
return "want accept";
case SSL_ERROR_WANT_X509_LOOKUP:
return "want x509 lookup";
case SSL_ERROR_WANT_ASYNC:
return "want async";
case SSL_ERROR_WANT_ASYNC_JOB:
return "want async job";
case SSL_ERROR_WANT_CLIENT_HELLO_CB:
return "want client hello callback";
//case SSL_ERROR_WANT_RETRY_VERIFY:
// return "want retry verify";
case SSL_ERROR_SSL:
return ERR_reason_error_string(ERR_get_error());
default:
return "unknown error";
}
extern const char *sock_error_str(unsigned long err);
extern const char *http_error_str(int err);
extern const char *MMDB_strerror(int err);
extern const char *ERR_reason_error_string(unsigned long err);
static int error_compress(unsigned long err) {
if (err & 0xFF0000) warning("Lossy error code compression!");
return ((int) err & 0xFFFF) | (((int) err >> 8) & 0xFF0000);
}
static const char *error_http_strerror(int err) {
switch (err) {
case HTTP_ERROR_TOO_MANY_HEADER_FIELDS:
return "too many header fields";
case HTTP_ERROR_EOH_NOT_FOUND:
return "end of http header not found";
case HTTP_ERROR_HEADER_MALFORMED:
return "http header malformed";
case HTTP_ERROR_INVALID_VERSION:
return "invalid http version";
case HTTP_ERROR_URI_TOO_LONG:
return "uri too long";
case HTTP_ERROR_GENERAL:
default:
return "unknown error";
}
static unsigned long error_decompress(int err) {
return (err & 0xFFFF) | ((err << 8) & 0xFF000000);
}
const char *error_str(int err_no, char *buf, int buf_len) {
@@ -72,46 +36,34 @@ const char *error_str(int err_no, char *buf, int buf_len) {
return buf;
} else if (mode == 0x01) {
// ssl
return error_ssl_strerror(e);
return sock_error_str(error_decompress(e));
} else if (mode == 0x02) {
// ssl err
return ERR_reason_error_string(error_decompress(e));
} else if (mode == 0x03) {
// mmdb
return MMDB_strerror(e);
} else if (mode == 0x03) {
} else if (mode == 0x04) {
// http
return error_http_strerror(e);
return http_error_str(e);
}
return buf;
}
void error_ssl(int err) {
if (err == SSL_ERROR_NONE) {
errno = 0;
} else if (err == SSL_ERROR_SYSCALL) {
// errno already set
} else {
errno = 0x01000000 | err;
}
void error_ssl(unsigned long err) {
errno = 0x01000000 | error_compress(err);
}
void error_ssl_err(unsigned long err) {
errno = 0x02000000 | error_compress(err);
}
void error_mmdb(int err) {
if (err == MMDB_SUCCESS) {
errno = 0;
} else if (err == MMDB_IO_ERROR) {
// errno already set
} else {
errno = 0x02000000 | err;
}
errno = 0x03000000 | err;
}
int error_http(int err) {
if (err == 0) {
errno = 0;
} else if (err == HTTP_ERROR_SYSCALL) {
// errno already set
} else {
errno = 0x03000000 | err;
}
return -1;
void error_http(int err) {
errno = 0x04000000 | err;
}
static int error_get(unsigned char prefix) {
@@ -126,10 +78,14 @@ int error_get_ssl() {
return error_get(0x01);
}
int error_get_mmdb() {
int error_get_ssl_err() {
return error_get(0x02);
}
int error_get_http() {
int error_get_mmdb() {
return error_get(0x03);
}
int error_get_http() {
return error_get(0x04);
}