terom@6: terom@6: #include "error.h" terom@6: terom@7: #include terom@7: #include terom@7: terom@7: #include terom@7: #include terom@7: terom@6: /* terom@6: * Helper macros terom@6: */ terom@6: #define ERROR_NAME(code, name) case code: return name terom@6: terom@6: const char *error_name (err_t code) terom@6: { terom@6: switch (code) { terom@6: ERROR_NAME( ERR_CALLOC, "calloc" ); terom@6: ERROR_NAME( ERR_GETADDRINFO, "getaddrinfo" ); terom@8: ERROR_NAME( ERR_GETADDRINFO_EMPTY, "getaddrinfo: no results" ); terom@6: ERROR_NAME( ERR_SOCKET, "socket" ); terom@6: ERROR_NAME( ERR_CONNECT, "connect" ); terom@6: ERROR_NAME( ERR_READ, "read" ); terom@8: ERROR_NAME( ERR_READ_EOF, "read: EOF" ); terom@6: ERROR_NAME( ERR_WRITE, "write" ); terom@10: ERROR_NAME( ERR_FCNTL, "fcntl" ); terom@6: ERROR_NAME( ERR_GNUTLS_CERT_ALLOC_CRED, "gnutls_certificate_allocate_credentials" ); terom@6: ERROR_NAME( ERR_GNUTLS_GLOBAL_INIT, "gnutls_global_init" ); terom@6: ERROR_NAME( ERR_GNUTLS_INIT, "gnutls_init" ); terom@6: ERROR_NAME( ERR_GNUTLS_SET_DEFAULT_PRIORITY, "gnutls_set_default_priority" ); terom@6: ERROR_NAME( ERR_GNUTLS_CRED_SET, "gnutls_credentials_set" ); terom@6: ERROR_NAME( ERR_GNUTLS_HANDSHAKE, "gnutls_handshake" ); terom@6: default: return "[unknown]"; terom@6: } terom@6: } terom@6: terom@8: const char *error_msg (const struct error_info *err) terom@7: { terom@7: static char msg[ERROR_MSG_MAXLEN]; terom@7: terom@7: // intrepret .extra terom@7: switch (err->code & _ERR_EXTRA_MASK) { terom@7: case ERR_EXTRA_NONE: terom@7: // no additional info terom@7: snprintf(msg, ERROR_MSG_MAXLEN, "%s", error_name(err->code)); terom@7: break; terom@7: terom@7: case ERR_EXTRA_ERRNO: terom@7: // strerror terom@7: snprintf(msg, ERROR_MSG_MAXLEN, "%s: %s", error_name(err->code), strerror(err->extra)); terom@7: break; terom@7: terom@7: case ERR_EXTRA_GAI: terom@7: // gai_strerror terom@7: snprintf(msg, ERROR_MSG_MAXLEN, "%s: %s", error_name(err->code), gai_strerror(err->extra)); terom@7: break; terom@7: terom@7: case ERR_EXTRA_GNUTLS: terom@7: // gnutls_strerror terom@7: snprintf(msg, ERROR_MSG_MAXLEN, "%s: %s", error_name(err->code), gnutls_strerror(err->extra)); terom@7: break; terom@7: terom@7: default: terom@7: // ??? terom@7: snprintf(msg, ERROR_MSG_MAXLEN, "%s(%#.8x): %#.8x", error_name(err->code), err->code, err->extra); terom@7: break; terom@7: } terom@7: terom@7: // return static pointer terom@7: return msg; terom@7: } terom@7: