-
Notifications
You must be signed in to change notification settings - Fork 124
/
error.h
95 lines (85 loc) · 2.25 KB
/
error.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef CIADPI_ERROR_H
#define CIADPI_ERROR_H
#include <stdio.h>
#include <errno.h>
#ifdef _WIN32
#include <winsock2.h>
#endif
#ifdef ANDROID_APP
#include <android/log.h>
#endif
#include "params.h"
#ifdef _WIN32
#define get_e() \
unie(WSAGetLastError())
#else
#define get_e() \
errno
#endif
#ifdef _WIN32
#define uniperror(str) \
fprintf(stderr, "%s: %ld\n", str, GetLastError())
#else
#ifdef ANDROID_APP
#define uniperror(str) \
__android_log_print(ANDROID_LOG_ERROR, "proxy", \
"%s: %s\n", str, strerror(errno))
#else
#define uniperror(str) \
perror(str)
#endif
#endif
static inline const int unie(int e)
{
#ifdef _WIN32
switch (e) {
case WSAEWOULDBLOCK:
return EAGAIN;
case WSAETIMEDOUT:
return ETIMEDOUT;
case WSAENETUNREACH:
return ENETUNREACH;
case WSAEHOSTUNREACH:
return EHOSTUNREACH;
case WSAECONNREFUSED:
return ECONNREFUSED;
case WSAECONNRESET:
return ECONNRESET;
}
#endif
return e;
}
#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__)
#endif
#define INIT_ADDR_STR(dst) \
char ADDR_STR[INET6_ADDRSTRLEN]; \
const char *p = 0; \
if (dst.sa.sa_family == AF_INET) \
p = inet_ntop(AF_INET, &dst.in.sin_addr, ADDR_STR, sizeof(ADDR_STR)); \
else \
p = inet_ntop(AF_INET6, &dst.in6.sin6_addr, ADDR_STR, sizeof(ADDR_STR)); \
if (!p) uniperror("inet_ntop");
#endif
#define INIT_HEX_STR(b, s) \
char HEX_STR[s * 2 + 1]; \
HEX_STR[sizeof(HEX_STR) - 1] = 0; \
do { \
size_t i; \
for (i = 0; i + 4 <= s; i += 4) \
snprintf(HEX_STR + i * 2, sizeof(HEX_STR) - i * 2, \
"%02x%02x%02x%02x", b[i],b[i+1],b[i+2],b[i+3]); \
for (; i < s; i++) \
snprintf(HEX_STR + i * 2, sizeof(HEX_STR) - i * 2, "%02x", b[i]); \
} while (0);