forked from Tencent/phxsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphxsqlproxyutil.cpp
More file actions
235 lines (206 loc) · 6.62 KB
/
Copy pathphxsqlproxyutil.cpp
File metadata and controls
235 lines (206 loc) · 6.62 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
/*
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 <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/time.h>
#include <poll.h>
#include <stack>
#include <netinet/tcp.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <string>
#include <cstring>
#include <errno.h>
#include "phxsqlproxyutil.h"
#include "phxcomm/phx_log.h"
using namespace std;
using phxsql::LogError;
namespace phxsqlproxy {
int SetNoDelay(int sock_fd) {
int one = 1;
return setsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
}
bool IsNonBlock(int sock_fd) {
int flags;
flags = fcntl(sock_fd, F_GETFL, 0);
if (flags & O_NONBLOCK) {
return true;
}
return false;
}
int SetNonBlock(int sock_fd) {
int flags;
flags = fcntl(sock_fd, F_GETFL, 0);
flags |= O_NONBLOCK;
flags |= O_NDELAY;
int ret = fcntl(sock_fd, F_SETFL, flags);
return ret;
}
void SetAddr(const char *ip_string, const unsigned short port, struct sockaddr_in &addr) {
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
int ip = 0;
if (!ip_string || '\0' == *ip_string || 0 == strcmp(ip_string, "0") || 0 == strcmp(ip_string, "0.0.0.0")
|| 0 == strcmp(ip_string, "*")) {
ip = htonl(INADDR_ANY);
} else {
ip = inet_addr(ip_string);
}
addr.sin_addr.s_addr = ip;
}
int CreateTcpSocket(const unsigned short port /* = 0 */, const char *ip /* = "*" */, bool is_reuse /* = false */) {
int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd >= 0) {
if (port != 0) {
if (is_reuse) {
int reuse_addr = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_addr, sizeof(reuse_addr));
}
struct sockaddr_in addr;
SetAddr(ip, port, addr);
int ret = bind(fd, (struct sockaddr*) &addr, sizeof(addr));
if (ret != 0) {
close(fd);
return -1;
}
}
}
return fd;
}
bool IsAuthReqPkg(const char * buf, int len) {
if (len < 5) {
return false;
}
return buf[4] == 0xA;
}
bool IsAuthFinishPkg(const char * buf, int len) {
if (len < 5) {
return false;
}
return buf[4] == 0x0;
}
uint64_t GetTimestampMS() {
uint64_t u = 0;
struct timeval now;
gettimeofday(&now, NULL);
u += now.tv_sec;
u *= 1000;
u += now.tv_usec / 1000;
return u;
}
void GetMysqlBufDebugString(const char * buf, int len, std::string & debug_str) {
debug_str = "";
for (int i = 0; i < len; ++i) {
char debug_buf[16] = { 0 };
snprintf(debug_buf, sizeof(debug_buf), "(%u,0x%X)", (unsigned int) ((unsigned char) buf[i]), buf[i]);
debug_str = debug_str + " " + string(debug_buf);
}
}
int GetSockName(int fd, std::string & ip, int & port) {
struct sockaddr_in socket_address;
socklen_t sock_len = sizeof(socket_address);
int ret = getsockname(fd, (struct sockaddr*) &socket_address, &sock_len);
if (ret == -1) {
LogError("getsockname fd [%d] failed, ret %d, errno (%d:%s)", fd, ret, errno, strerror(errno));
return -__LINE__;
}
char buf[INET6_ADDRSTRLEN] = { 0 };
if (inet_ntop(socket_address.sin_family, &socket_address.sin_addr, buf, INET6_ADDRSTRLEN) == NULL) {
LogError("inet_ntop failed, ret NULL, errno(%d:%s)", errno, strerror(errno));
return -__LINE__;
}
ip = string(buf);
port = ntohs(socket_address.sin_port);
return 0;
}
int GetPeerName(int fd, std::string & ip, int & port) {
struct sockaddr_in socket_address;
socklen_t sock_len = sizeof(socket_address);
int ret = getpeername(fd, (struct sockaddr*) &socket_address, &sock_len);
if (ret == -1) {
LogError("getpeername fd [%d] failed, ret %d, errno (%d:%s)", fd, ret, errno, strerror(errno));
return -__LINE__;
}
char buf[INET6_ADDRSTRLEN] = { 0 };
if (inet_ntop(socket_address.sin_family, &socket_address.sin_addr, buf, INET6_ADDRSTRLEN) == NULL) {
LogError("inet_ntop failed, ret NULL, errno(%d:%s)", errno, strerror(errno));
return -__LINE__;
}
ip = string(buf);
port = ntohs(socket_address.sin_port);
return 0;
}
uint64_t DecodedLengthBinary(const char * buf, int len, int & len_field_size) {
uint8_t first_bit = (uint8_t)(buf[0]);
uint64_t package_len = 0;
switch (first_bit) {
case 251: {
package_len = 0;
len_field_size = 1;
break;
}
case 252: {
if (len < 3) {
package_len = -1;
} else {
memcpy(&package_len, buf + 1, sizeof(char) * 2);
len_field_size = 3;
}
break;
}
case 253: {
if (len < 4) {
package_len = -1;
} else {
memcpy(&package_len, buf + 1, sizeof(char) * 3);
len_field_size = 4;
}
break;
}
case 254: {
if (len < 9) {
package_len = -1;
} else {
memcpy(&package_len, buf + 1, sizeof(char) * 8);
len_field_size = 9;
}
break;
}
default:
package_len = first_bit;
len_field_size = 1;
break;
}
return package_len;
}
std::string UIntToStr(uint32_t i) {
char buf[16];
snprintf(buf, sizeof(buf), "%u", i);
return std::string(buf);
}
std::vector<std::string> SplitStr(const std::string & str, const std::string & delim) {
std::vector<std::string> vec;
for (size_t pos = 0; pos < str.size();) {
size_t next = str.find(delim, pos);
if (next == std::string::npos) {
vec.push_back(str.substr(pos));
break;
}
vec.push_back(str.substr(pos, next - pos));
pos = next + delim.size();
}
return vec;
}
}