-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy_ptc.h
102 lines (81 loc) · 2.62 KB
/
proxy_ptc.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
96
97
98
99
100
101
102
#ifndef _PROXY_PTC_H_
#define _PROXY_PTC_H_
typedef union {
struct { /* for TCP/UDP over IPv4, len = 12 */
uint32_t src_addr;
uint32_t dst_addr;
uint16_t src_port;
uint16_t dst_port;
} ip4;
struct { /* for TCP/UDP over IPv6, len = 36 */
uint8_t src_addr[16];
uint8_t dst_addr[16];
uint16_t src_port;
uint16_t dst_port;
} ip6;
struct { /* for AF_UNIX sockets, len = 216 */
uint8_t src_addr[108];
uint8_t dst_addr[108];
} unx;
} pproxy_v2_addr_t;
typedef struct {
char line[108];
} pproxy_v1_t;
typedef struct {
uint8_t sig[12]; /* hex 0D 0A 0D 0A 00 0D 0A 51 55 49 54 0A */
uint8_t ver_cmd; /* protocol version and command (V2/V1) */
uint8_t fam; /* protocol family and address (DGRAM/STREAM) */
uint16_t len; /* number of following bytes part of the header */
pproxy_v2_addr_t addr;
} pproxy_v2_t;
typedef union {
pproxy_v1_t v1;
pproxy_v2_t v2;
} pproxy_hdr_t;
typedef enum {
PPHDRERR = -7,
PPSENDERR, // failure to send to sock fd
PPREADERR, // failure to read from sock fd
PPINVALADDR, // invalid address
PPINVALFAM, // invalid family value
PPTRUNCATED, // truncated header
PPINVALCMD, // invalid cmd
PPNOERR = 0, // no error
PPLOCALCMD, // local command in vercmd
} pp_ret_t;
typedef enum {
PPROXY_V1 = 1,
PPROXY_V2
} pproxy_ver_t;
#define IPV4_ADDR_LEN 12
#define IPV6_ADDR_LEN 36
#define UNX_ADDR_LEN 216
#define V2_HDR_LEN 16
#define MAX_PP_LEN sizeof(pproxy_hdr_t)
#define SOCKADDR_STORAGE(addr) (struct sockaddr_storage *)addr
static const char v2sig[12] = "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A";
static const uint8_t vercmd = 0x21;
pp_ret_t proxy_ptc_v1_encode(char *buf, int8_t *len,
struct sockaddr_storage *src,
struct sockaddr_storage *dst);
pp_ret_t proxy_ptc_v2_encode(char *buf, int8_t *len,
struct sockaddr_storage *src,
struct sockaddr_storage *dst);
pp_ret_t proxy_ptc_send(int fd,
pproxy_ver_t ppver,
struct sockaddr_storage *src,
struct sockaddr_storage *dst);
pp_ret_t proxy_ptc_decode(char *buf, int len,
pproxy_ver_t *ppver,
struct sockaddr_storage *src,
struct sockaddr_storage *dst);
pp_ret_t proxy_ptc_read(int fd,
pproxy_ver_t *ppver,
struct sockaddr_storage *src,
struct sockaddr_storage *dst);
uint8_t get_addr_family(struct sockaddr_storage *addr);
struct in_addr get_inet4_ip(struct sockaddr_storage *addr);
struct in6_addr get_inet6_ip(struct sockaddr_storage *addr);
uint8_t get_inet4_port(struct sockaddr_storage *addr);
uint16_t get_inet6_port(struct sockaddr_storage *addr);
#endif // _PROXY_PTC_H_