diff --git a/CMakeLists_Headers.txt b/CMakeLists_Headers.txt index e6581ebb..396efcc0 100644 --- a/CMakeLists_Headers.txt +++ b/CMakeLists_Headers.txt @@ -77,7 +77,6 @@ set(INCLUDE_HEADERS src/util/LRUCache.h src/util/StringUtil.h src/util/URIParser.h - src/util/MD5Util.h src/factory/WFConnection.h src/factory/WFTask.h src/factory/WFTask.inl diff --git a/src/include/workflow/MD5Util.h b/src/include/workflow/MD5Util.h deleted file mode 120000 index 8d8eb0df..00000000 --- a/src/include/workflow/MD5Util.h +++ /dev/null @@ -1 +0,0 @@ -../../util/MD5Util.h \ No newline at end of file diff --git a/src/manager/RouteManager.cc b/src/manager/RouteManager.cc index 35c499bc..ffb58c04 100644 --- a/src/manager/RouteManager.cc +++ b/src/manager/RouteManager.cc @@ -17,6 +17,7 @@ */ #include +#include #include #include #include @@ -32,7 +33,6 @@ #include "list.h" #include "rbtree.h" #include "WFGlobal.h" -#include "MD5Util.h" #include "CommScheduler.h" #include "EndpointParams.h" #include "RouteManager.h" @@ -105,7 +105,7 @@ struct RouteParams { TransportType transport_type; const struct addrinfo *addrinfo; - uint64_t md5_16; + uint64_t md5_64; SSL_CTX *ssl_ctx; int connect_timeout; int ssl_connect_timeout; @@ -124,7 +124,7 @@ public: std::mutex mutex; std::vector targets; struct list_head breaker_list; - uint64_t md5_16; + uint64_t md5_64; int nleft; int nbreak; @@ -214,7 +214,7 @@ int RouteResultEntry::init(const struct RouteParams *params) { this->targets.push_back(target); this->request_object = target; - this->md5_16 = params->md5_16; + this->md5_64 = params->md5_64; return 0; } @@ -227,7 +227,7 @@ int RouteResultEntry::init(const struct RouteParams *params) if (this->add_group_targets(params) >= 0) { this->request_object = this->group; - this->md5_16 = params->md5_16; + this->md5_64 = params->md5_64; return 0; } @@ -390,19 +390,18 @@ static uint64_t __generate_key(TransportType type, const struct EndpointParams *endpoint_params, const std::string& hostname) { - std::string str(std::to_string(type)); + uint64_t md5[2]; + MD5_CTX ctx; - str += '\n'; + MD5_Init(&ctx); + MD5_Update(&ctx, &type, sizeof type); if (!other_info.empty()) - { - str += other_info; - str += '\n'; - } + MD5_Update(&ctx, other_info.c_str(), other_info.size() + 1); if (type == TT_TCP_SSL && endpoint_params->use_tls_sni) { - str += hostname; - str += '\n'; + MD5_Update(&ctx, "\n", 1); + MD5_Update(&ctx, hostname.c_str(), hostname.size() + 1); } if (addrinfo->ai_next) @@ -419,15 +418,13 @@ static uint64_t __generate_key(TransportType type, std::sort(sorted_addr.begin(), sorted_addr.end(), __addr_less); for (const struct addrinfo *p : sorted_addr) - { - str += std::string((char *)p->ai_addr, p->ai_addrlen); - str += '\n'; - } + MD5_Update(&ctx, p->ai_addr, p->ai_addrlen); } else - str += std::string((char *)addrinfo->ai_addr, addrinfo->ai_addrlen); + MD5_Update(&ctx, addrinfo->ai_addr, addrinfo->ai_addrlen); - return MD5Util::md5_integer_16(str); + MD5_Final((unsigned char *)md5, &ctx); + return md5[0]; } RouteManager::~RouteManager() @@ -450,7 +447,7 @@ int RouteManager::get(TransportType type, const std::string& hostname, RouteResult& result) { - uint64_t md5_16 = __generate_key(type, addrinfo, other_info, + uint64_t md5_64 = __generate_key(type, addrinfo, other_info, endpoint_params, hostname); struct rb_node **p = &cache_.rb_node; struct rb_node *parent = NULL; @@ -462,7 +459,7 @@ int RouteManager::get(TransportType type, { parent = *p; entry = rb_entry(*p, RouteResultEntry, rb); - if (md5_16 <= entry->md5_16) + if (md5_64 <= entry->md5_64) { bound = entry; p = &(*p)->rb_left; @@ -471,7 +468,7 @@ int RouteManager::get(TransportType type, p = &(*p)->rb_right; } - if (bound && bound->md5_16 == md5_16) + if (bound && bound->md5_64 == md5_64) { entry = bound; entry->check_breaker(); @@ -492,7 +489,7 @@ int RouteManager::get(TransportType type, struct RouteParams params = { .transport_type = type, .addrinfo = addrinfo, - .md5_16 = md5_16, + .md5_64 = md5_64, .ssl_ctx = ssl_ctx, .connect_timeout = endpoint_params->connect_timeout, .ssl_connect_timeout = ssl_connect_timeout, diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index 94b8bcd6..74d4f190 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -6,7 +6,6 @@ set(SRC EncodeStream.cc StringUtil.cc URIParser.cc - MD5Util.cc ) add_library(${PROJECT_NAME} OBJECT ${SRC}) diff --git a/src/util/MD5Util.cc b/src/util/MD5Util.cc deleted file mode 100644 index 9deeb33a..00000000 --- a/src/util/MD5Util.cc +++ /dev/null @@ -1,95 +0,0 @@ -/* - Copyright (c) 2019 Sogou, Inc. - - Licensed under the Apache 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 - - http://www.apache.org/licenses/LICENSE-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. - - Authors: Wu Jiaxu (wujiaxu@sogou-inc.com) -*/ - -#include -#include -#include -#include "MD5Util.h" - -static inline void __md5(const std::string& str, unsigned char *md) -{ - MD5_CTX ctx; - MD5_Init(&ctx); - MD5_Update(&ctx, str.c_str(), str.size()); - MD5_Final(md, &ctx); -} - -std::string MD5Util::md5_bin(const std::string& str) -{ - unsigned char md[16]; - - __md5(str, md); - return std::string((const char *)md, 16); -} - -static inline char __hex_char(int v) -{ - return v < 10 ? '0' + v : 'a' + v - 10; -} - -static inline void __plain_hex(char *s, int ch) -{ - *s = __hex_char(ch / 16); - *(s + 1) = __hex_char(ch % 16); -} - -std::string MD5Util::md5_string_32(const std::string& str) -{ - unsigned char md[16]; - char out[32]; - - __md5(str, md); - for (int i = 0; i < 16; i++) - __plain_hex(out + (i * 2), md[i]); - - return std::string((const char *)out, 32); -} - -std::string MD5Util::md5_string_16(const std::string& str) -{ - unsigned char md[16]; - char out[16]; - - __md5(str, md); - for (int i = 0; i < 8; i++) - __plain_hex(out + (i * 2), md[i + 4]); - - return std::string((const char *)out, 16); -} - -std::pair MD5Util::md5_integer_32(const std::string& str) -{ - unsigned char md[16]; - std::pair res; - - __md5(str, md); - memcpy(&res.first, md, sizeof (uint64_t)); - memcpy(&res.second, md + 8, sizeof (uint64_t)); - return res; -} - -uint64_t MD5Util::md5_integer_16(const std::string& str) -{ - unsigned char md[16]; - uint64_t res; - - __md5(str, md); - memcpy(&res, md + 4, sizeof (uint64_t)); - return res; -} - diff --git a/src/util/MD5Util.h b/src/util/MD5Util.h deleted file mode 100644 index 5dc356a2..00000000 --- a/src/util/MD5Util.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - Copyright (c) 2019 Sogou, Inc. - - Licensed under the Apache 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 - - http://www.apache.org/licenses/LICENSE-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. - - Authors: Wu Jiaxu (wujiaxu@sogou-inc.com) -*/ - -#ifndef _MD5UTIL_H_ -#define _MD5UTIL_H_ - -#include -#include -#include - -/** - * @file MD5Util.h - * @brief MD5 toolbox - */ - -// static class -class MD5Util -{ -public: - //128 bit binary data - static std::string md5_bin(const std::string& str); - //128 bit hex string style, lower case - static std::string md5_string_32(const std::string& str); - //64 bit hex string style, lower case - static std::string md5_string_16(const std::string& str); - - //128 bit integer style - static std::pair md5_integer_32(const std::string& str); - //64 bit integer style - static uint64_t md5_integer_16(const std::string& str); -}; - -#endif -