-
Notifications
You must be signed in to change notification settings - Fork 548
Expand file tree
/
Copy pathproxy_protocol_handler.cpp
More file actions
349 lines (296 loc) · 11.2 KB
/
proxy_protocol_handler.cpp
File metadata and controls
349 lines (296 loc) · 11.2 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
Tencent is pleased to support the open source community by making PhxSQL available.
Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the GNU General Public License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
https://opensource.org/licenses/GPL-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include "proxy_protocol_handler.h"
#include "phxcomm/phx_log.h"
#include "phxsqlproxyutil.h"
#include "routineutil.h"
namespace phxsqlproxy {
#define PRETTY_LOG(log, fmt, ...) \
log("%s:%d requniqid %llu " fmt, __func__, __LINE__, req_uniq_id_, ## __VA_ARGS__)
#define LOG_ERR(...) PRETTY_LOG(phxsql::LogError, __VA_ARGS__)
#define LOG_WARN(...) PRETTY_LOG(phxsql::LogWarning, __VA_ARGS__)
#define LOG_DEBUG(...) PRETTY_LOG(phxsql::LogVerbose, __VA_ARGS__)
// proxy protocol specification
struct ProxyHdr { // proxy protocol v2
uint8_t sig[12];
uint8_t ver_cmd;
uint8_t fam;
uint16_t len;
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;
} __attribute__((packed)) 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;
} __attribute__((packed)) ip6;
} addr;
} __attribute__((packed));
enum {
PP2_VERSION = 0x20
};
enum {
PP2_CMD_LOCAL = 0x00,
PP2_CMD_PROXY = 0x01
};
enum {
PP2_FAM_UNSPEC = 0x00,
PP2_FAM_TCP4 = 0x11,
PP2_FAM_TCP6 = 0x21
};
#define PP2_SIGNATURE "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A"
// proxy protocol specification end
ProxyProtocolHandler::ProxyProtocolHandler(PHXSqlProxyConfig * config,
WorkerConfig_t * worker_config,
GroupStatusCache * group_status_cache) :
config_(config),
worker_config_(worker_config),
group_status_cache_(group_status_cache) {
Clear();
}
ProxyProtocolHandler::~ProxyProtocolHandler() {
}
void ProxyProtocolHandler::SetReqUniqID(uint64_t req_uniq_id) {
req_uniq_id_ = req_uniq_id;
}
void ProxyProtocolHandler::Clear() {
req_uniq_id_ = 0;
memset(&src_addr_, 0, sizeof(src_addr_));
memset(&dst_addr_, 0, sizeof(dst_addr_));
}
int ProxyProtocolHandler::GetAddrFromFD(int fd) {
int ret;
socklen_t src_len = sizeof(src_addr_);
ret = getpeername(fd, (struct sockaddr *) &src_addr_, &src_len);
if (ret < 0) {
LOG_ERR("getpeername fd [%d] failed, ret %d, errno (%d:%s)", fd, ret, errno, strerror(errno));
return -__LINE__;
}
socklen_t dst_len = sizeof(dst_addr_);
ret = getsockname(fd, (struct sockaddr *) &dst_addr_, &dst_len);
if (ret < 0) {
LOG_ERR("getsockname fd [%d] failed, ret %d, errno (%d:%s)", fd, ret, errno, strerror(errno));
return -__LINE__;
}
return 0;
}
int ProxyProtocolHandler::IsProxyHeaderNeed() { // 1:need, 0:no need, <0:reject
std::string src_ip;
std::string dst_ip;
int src_port;
int dst_port;
if (SockAddrToIPPort((struct sockaddr *) &src_addr_, src_ip, src_port) < 0) {
return -__LINE__;
}
if (SockAddrToIPPort((struct sockaddr *) &dst_addr_, dst_ip, dst_port) < 0) {
return -__LINE__;
}
LOG_DEBUG("receive connect from [%s:%d]", src_ip.c_str(), src_port);
if (dst_port != worker_config_->proxy_port_) {
return 0;
}
if (!group_status_cache_->IsMember(src_ip)) {
LOG_ERR("non-member ip [%s] connect port [%d], reject", src_ip.c_str(), dst_port);
return -__LINE__;
}
return 1;
}
int ProxyProtocolHandler::ProcessProxyHeader(int fd) {
int ret = GetAddrFromFD(fd);
if (ret < 0) {
return ret;
}
ret = IsProxyHeaderNeed();
if (ret <= 0) {
return ret;
}
union {
char v1[108];
ProxyHdr v2;
} hdr;
int hdr_size = RoutinePeekWithTimeout(fd, (char *) &hdr, sizeof(hdr), config_->ProxyProtocolTimeoutMs());
if (hdr_size < 0) {
LOG_ERR("read proxy header from fd [%d] failed, ret %d", fd, hdr_size);
return -__LINE__;
}
if (hdr_size >= 16 && memcmp(&hdr.v2, PP2_SIGNATURE, 12) == 0 && (hdr.v2.ver_cmd & 0xF0) == PP2_VERSION) {
hdr_size = ParseProxyHeaderV2(hdr.v2, hdr_size);
} else if (hdr_size >= 8 && memcmp(hdr.v1, "PROXY ", 6) == 0) {
hdr_size = ParseProxyHeaderV1(hdr.v1, hdr_size);
} else {
return -__LINE__;
}
if (hdr_size > 0) {
ret = read(fd, &hdr, hdr_size);
if (ret != hdr_size) {
LOG_ERR("flush fd [%d] failed, ret %d hdr size %d", fd, ret, hdr_size);
return -__LINE__;
}
}
return hdr_size;
}
int ProxyProtocolHandler::ParseProxyHeaderV1(const char * hdr, int read_size) {
const char * end = (const char *) memchr(hdr, '\r', read_size - 1);
int size = end - hdr + 2;
if (!end || *(end + 1) != '\n') {
return -__LINE__;
}
std::vector<std::string> tokens = SplitStr(std::string(hdr, end), " ");
if (tokens[1] == "UNKNOWN") {
return size;
}
if (tokens.size() != 6) {
return -__LINE__;
}
const std::string & src_ip = tokens[2];
const std::string & dst_ip = tokens[3];
int src_port = atoi(tokens[4].c_str());
int dst_port = atoi(tokens[5].c_str());
if (src_port < 0 || src_port > 65535 || dst_port < 0 || dst_port > 65535) {
return -__LINE__;
}
if (tokens[1] == "TCP4") {
if (SetAddr(src_ip.c_str(), src_port, (struct sockaddr_in &) src_addr_) != 0) {
return -__LINE__;
}
if (SetAddr(dst_ip.c_str(), dst_port, (struct sockaddr_in &) dst_addr_) != 0) {
return -__LINE__;
}
} else if (tokens[1] == "TCP6") {
if (SetAddr6(src_ip.c_str(), src_port, (struct sockaddr_in6 &) src_addr_) != 0) {
return -__LINE__;
}
if (SetAddr6(dst_ip.c_str(), dst_port, (struct sockaddr_in6 &) dst_addr_) != 0) {
return -__LINE__;
}
}
return size;
}
int ProxyProtocolHandler::ParseProxyHeaderV2(const ProxyHdr & hdr, int read_size) {
int size = 16 + ntohs(hdr.len);
if (read_size < size) { // truncated or too large header
return -__LINE__;
}
switch (hdr.ver_cmd & 0xF) {
case PP2_CMD_PROXY:
switch (hdr.fam) {
case PP2_FAM_TCP4:
src_addr_.ss_family = AF_INET;
((struct sockaddr_in *) &src_addr_)->sin_addr.s_addr = hdr.addr.ip4.src_addr;
((struct sockaddr_in *) &src_addr_)->sin_port = hdr.addr.ip4.src_port;
dst_addr_.ss_family = AF_INET;
((struct sockaddr_in *) &dst_addr_)->sin_addr.s_addr = hdr.addr.ip4.dst_addr;
((struct sockaddr_in *) &dst_addr_)->sin_port = hdr.addr.ip4.dst_port;
return size;
case PP2_FAM_TCP6:
src_addr_.ss_family = AF_INET6;
memcpy(&((struct sockaddr_in6 *) &src_addr_)->sin6_addr, hdr.addr.ip6.src_addr, 16);
((struct sockaddr_in6 *) &src_addr_)->sin6_port = hdr.addr.ip6.src_port;
dst_addr_.ss_family = AF_INET6;
memcpy(&((struct sockaddr_in6 *) &dst_addr_)->sin6_addr, hdr.addr.ip6.dst_addr, 16);
((struct sockaddr_in6 *) &dst_addr_)->sin6_port = hdr.addr.ip6.dst_port;
return size;
case PP2_FAM_UNSPEC:
return size; // unknown protocol, keep local connection address
default:
return -__LINE__; // unsupport protocol
}
case PP2_CMD_LOCAL:
return size; // keep local connection address for LOCAL
default:
return -__LINE__; // unsupport command
}
return -__LINE__;
}
int ProxyProtocolHandler::SendProxyHeader(int fd) {
int ret = 0;
std::string header;
switch (config_->ProxyProtocol()) {
case 0:
return 0;
case 1:
ret = MakeProxyHeaderV1(header);
break;
case 2:
ret = MakeProxyHeaderV2(header);
break;
default:
return -__LINE__;
}
while (ret == 0) {
ret = RoutineWriteWithTimeout(fd, header.c_str(), header.size(), config_->WriteTimeoutMs());
if (ret != 0 && ret != (int) header.size()) {
LOG_ERR("write header to fd [%d] failed, ret %d, hdr size %zu", fd, ret, header.size());
return -__LINE__;
}
}
return ret;
}
int ProxyProtocolHandler::MakeProxyHeaderV1(std::string & header) {
if (src_addr_.ss_family == AF_INET && dst_addr_.ss_family == AF_INET) {
header = "PROXY TCP4 ";
} else if (src_addr_.ss_family == AF_INET6 && dst_addr_.ss_family == AF_INET6) {
header = "PROXY TCP6 ";
} else {
header = "PROXY UNKNOWN\r\n";
return 0;
}
std::string src_ip;
std::string dst_ip;
int src_port;
int dst_port;
if (SockAddrToIPPort((struct sockaddr *) &src_addr_, src_ip, src_port) < 0) {
return -__LINE__;
}
if (SockAddrToIPPort((struct sockaddr *) &dst_addr_, dst_ip, dst_port) < 0) {
return -__LINE__;
}
header += src_ip + " " + dst_ip + " " + UIntToStr(src_port) + " " + UIntToStr(dst_port) + "\r\n";
LOG_DEBUG("make v1 proxy header: %s", header.c_str());
return 0;
}
int ProxyProtocolHandler::MakeProxyHeaderV2(std::string & header) {
ProxyHdr hdr;
memcpy(hdr.sig, PP2_SIGNATURE, sizeof(hdr.sig));
hdr.ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
if (src_addr_.ss_family == AF_INET && dst_addr_.ss_family == AF_INET) {
hdr.fam = PP2_FAM_TCP4;
hdr.len = htons(sizeof(hdr.addr.ip4));
hdr.addr.ip4.src_addr = ((struct sockaddr_in *) &src_addr_)->sin_addr.s_addr;
hdr.addr.ip4.src_port = ((struct sockaddr_in *) &src_addr_)->sin_port;
hdr.addr.ip4.dst_addr = ((struct sockaddr_in *) &dst_addr_)->sin_addr.s_addr;
hdr.addr.ip4.dst_port = ((struct sockaddr_in *) &dst_addr_)->sin_port;
header = std::string((char *) &hdr, 16 + sizeof(hdr.addr.ip4));
} else if (src_addr_.ss_family == AF_INET6 && dst_addr_.ss_family == AF_INET6) {
hdr.fam = PP2_FAM_TCP6;
hdr.len = htons(sizeof(hdr.addr.ip6));
memcpy(hdr.addr.ip6.src_addr, &((struct sockaddr_in6 *) &src_addr_)->sin6_addr, 16);
hdr.addr.ip6.src_port = ((struct sockaddr_in6 *) &src_addr_)->sin6_port;
memcpy(hdr.addr.ip6.dst_addr, &((struct sockaddr_in6 *) &dst_addr_)->sin6_addr, 16);
hdr.addr.ip6.dst_port = ((struct sockaddr_in6 *) &dst_addr_)->sin6_port;
header = std::string((char *) &hdr, 16 + sizeof(hdr.addr.ip6));
} else {
hdr.fam = PP2_FAM_UNSPEC;
hdr.len = htons(0);
header = std::string((char *) &hdr, 16);
}
LOG_DEBUG("make v2 proxy header, size: %zu", header.size());
return 0;
}
}