byedpi/error.h

82 lines
1.8 KiB
C
Raw Normal View History

2024-08-07 16:05:14 +03:00
#ifndef CIADPI_ERROR_H
#define CIADPI_ERROR_H
2024-08-07 14:25:26 +03:00
2024-02-19 12:51:34 +03:00
#include <stdio.h>
#include <errno.h>
#ifdef _WIN32
2024-02-24 20:44:54 +03:00
#include <winsock2.h>
#endif
#ifdef ANDROID_APP
#include <android/log.h>
2024-02-19 12:51:34 +03:00
#endif
#ifdef _WIN32
#define get_e() \
unie(WSAGetLastError())
#else
#define get_e() \
errno
#endif
#ifdef _WIN32
2024-02-24 20:44:54 +03:00
#define uniperror(str) \
2024-08-07 14:25:26 +03:00
fprintf(stderr, "%s: %ld\n", str, GetLastError())
2024-02-19 12:51:34 +03:00
#else
2024-02-24 20:44:54 +03:00
#ifdef ANDROID_APP
#define uniperror(str) \
2024-02-24 23:13:25 +03:00
__android_log_print(ANDROID_LOG_ERROR, "proxy", \
2024-02-24 20:44:54 +03:00
"%s: %s\n", str, strerror(errno))
#else
#define uniperror(str) \
perror(str)
#endif
2024-02-19 12:51:34 +03:00
#endif
2024-05-02 19:36:29 +03:00
static inline const int unie(int e)
2024-02-19 12:51:34 +03:00
{
#ifdef _WIN32
switch (e) {
case WSAEWOULDBLOCK:
return EAGAIN;
case WSAETIMEDOUT:
return ETIMEDOUT;
case WSAENETUNREACH:
return ENETUNREACH;
case WSAEHOSTUNREACH:
return EHOSTUNREACH;
case WSAECONNREFUSED:
return ECONNREFUSED;
2024-03-08 04:48:35 +03:00
case WSAECONNRESET:
return ECONNRESET;
2024-02-19 12:51:34 +03:00
}
#endif
return e;
}
2024-02-24 20:44:54 +03:00
#ifdef ANDROID_APP
#define LOG_E ANDROID_LOG_ERROR
#define LOG_S ANDROID_LOG_DEBUG
#define LOG_L ANDROID_LOG_VERBOSE
#define LOG(s, str, ...) \
__android_log_print(s, "proxy", str, ##__VA_ARGS__)
#else
#define LOG_E -1
#define LOG_S 1
#define LOG_L 2
#define LOG(s, str, ...) \
if (params.debug >= s) \
fprintf(stderr, str, ##__VA_ARGS__)
2024-07-18 23:59:44 +03:00
#endif
#define INIT_ADDR_STR(dst) \
2024-08-20 18:22:59 +03:00
char ADDR_STR[INET6_ADDRSTRLEN]; \
const char *p = 0; \
2024-07-18 23:59:44 +03:00
if (dst.sa.sa_family == AF_INET) \
2024-08-20 18:22:59 +03:00
p = inet_ntop(AF_INET, &dst.in.sin_addr, ADDR_STR, sizeof(ADDR_STR)); \
2024-07-18 23:59:44 +03:00
else \
2024-08-20 18:22:59 +03:00
p = inet_ntop(AF_INET6, &dst.in6.sin6_addr, ADDR_STR, sizeof(ADDR_STR)); \
if (!p) uniperror("inet_ntop");
2024-08-07 14:25:26 +03:00
#endif