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

Method MakeProxyHeaderV2

phxsqlproxy/proxy_protocol_handler.cpp:318–347  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

316}
317
318int ProxyProtocolHandler::MakeProxyHeaderV2(std::string & header) {
319 ProxyHdr hdr;
320 memcpy(hdr.sig, PP2_SIGNATURE, sizeof(hdr.sig));
321 hdr.ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
322
323 if (src_addr_.ss_family == AF_INET && dst_addr_.ss_family == AF_INET) {
324 hdr.fam = PP2_FAM_TCP4;
325 hdr.len = htons(sizeof(hdr.addr.ip4));
326 hdr.addr.ip4.src_addr = ((struct sockaddr_in *) &src_addr_)->sin_addr.s_addr;
327 hdr.addr.ip4.src_port = ((struct sockaddr_in *) &src_addr_)->sin_port;
328 hdr.addr.ip4.dst_addr = ((struct sockaddr_in *) &dst_addr_)->sin_addr.s_addr;
329 hdr.addr.ip4.dst_port = ((struct sockaddr_in *) &dst_addr_)->sin_port;
330 header = std::string((char *) &hdr, 16 + sizeof(hdr.addr.ip4));
331 } else if (src_addr_.ss_family == AF_INET6 && dst_addr_.ss_family == AF_INET6) {
332 hdr.fam = PP2_FAM_TCP6;
333 hdr.len = htons(sizeof(hdr.addr.ip6));
334 memcpy(hdr.addr.ip6.src_addr, &((struct sockaddr_in6 *) &src_addr_)->sin6_addr, 16);
335 hdr.addr.ip6.src_port = ((struct sockaddr_in6 *) &src_addr_)->sin6_port;
336 memcpy(hdr.addr.ip6.dst_addr, &((struct sockaddr_in6 *) &dst_addr_)->sin6_addr, 16);
337 hdr.addr.ip6.dst_port = ((struct sockaddr_in6 *) &dst_addr_)->sin6_port;
338 header = std::string((char *) &hdr, 16 + sizeof(hdr.addr.ip6));
339 } else {
340 hdr.fam = PP2_FAM_UNSPEC;
341 hdr.len = htons(0);
342 header = std::string((char *) &hdr, 16);
343 }
344
345 LOG_DEBUG("make v2 proxy header, size: %zu", header.size());
346 return 0;
347}
348
349}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected