MCPcopy Create free account
hub / github.com/Tencent/phxsql / ParseProxyHeaderV1

Method ParseProxyHeaderV1

phxsqlproxy/proxy_protocol_handler.cpp:185–225  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

183}
184
185int ProxyProtocolHandler::ParseProxyHeaderV1(const char * hdr, int read_size) {
186 const char * end = (const char *) memchr(hdr, '\r', read_size - 1);
187 int size = end - hdr + 2;
188 if (!end || *(end + 1) != '\n') {
189 return -__LINE__;
190 }
191
192 std::vector<std::string> tokens = SplitStr(std::string(hdr, end), " ");
193 if (tokens[1] == "UNKNOWN") {
194 return size;
195 }
196 if (tokens.size() != 6) {
197 return -__LINE__;
198 }
199
200 const std::string & src_ip = tokens[2];
201 const std::string & dst_ip = tokens[3];
202 int src_port = atoi(tokens[4].c_str());
203 int dst_port = atoi(tokens[5].c_str());
204 if (src_port < 0 || src_port > 65535 || dst_port < 0 || dst_port > 65535) {
205 return -__LINE__;
206 }
207
208 if (tokens[1] == "TCP4") {
209 if (SetAddr(src_ip.c_str(), src_port, (struct sockaddr_in &) src_addr_) != 0) {
210 return -__LINE__;
211 }
212 if (SetAddr(dst_ip.c_str(), dst_port, (struct sockaddr_in &) dst_addr_) != 0) {
213 return -__LINE__;
214 }
215 } else if (tokens[1] == "TCP6") {
216 if (SetAddr6(src_ip.c_str(), src_port, (struct sockaddr_in6 &) src_addr_) != 0) {
217 return -__LINE__;
218 }
219 if (SetAddr6(dst_ip.c_str(), dst_port, (struct sockaddr_in6 &) dst_addr_) != 0) {
220 return -__LINE__;
221 }
222 }
223
224 return size;
225}
226
227int ProxyProtocolHandler::ParseProxyHeaderV2(const ProxyHdr & hdr, int read_size) {
228 int size = 16 + ntohs(hdr.len);

Callers

nothing calls this directly

Calls 3

SplitStrFunction · 0.85
SetAddrFunction · 0.85
SetAddr6Function · 0.85

Tested by

no test coverage detected