1 /** 2 * Copyright 2020 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_CCSRC_PS_CORE_COMM_UTIL_H_ 18 #define MINDSPORE_CCSRC_PS_CORE_COMM_UTIL_H_ 19 20 #include <unistd.h> 21 #ifdef _MSC_VER 22 #include <tchar.h> 23 #include <winsock2.h> 24 #include <windows.h> 25 #include <iphlpapi.h> 26 #else 27 #include <net/if.h> 28 #include <arpa/inet.h> 29 #include <ifaddrs.h> 30 #include <netinet/in.h> 31 #endif 32 33 #include <event2/buffer.h> 34 #include <event2/event.h> 35 #include <event2/http.h> 36 #include <event2/keyvalq_struct.h> 37 #include <event2/listener.h> 38 #include <event2/util.h> 39 40 #include <openssl/ssl.h> 41 #include <openssl/rand.h> 42 #include <openssl/err.h> 43 #include <openssl/evp.h> 44 #include <assert.h> 45 #include <openssl/pkcs12.h> 46 #include <openssl/bio.h> 47 48 #include <cstdio> 49 #include <cstdlib> 50 #include <cstring> 51 #include <functional> 52 #include <random> 53 #include <sstream> 54 #include <string> 55 #include <utility> 56 #include <thread> 57 #include <fstream> 58 #include <iostream> 59 #include <vector> 60 #include <algorithm> 61 62 #include "proto/comm.pb.h" 63 #include "proto/ps.pb.h" 64 #include "ps/core/cluster_metadata.h" 65 #include "ps/core/cluster_config.h" 66 #include "utils/log_adapter.h" 67 #include "ps/ps_context.h" 68 #include "utils/convert_utils_base.h" 69 #include "ps/core/configuration.h" 70 71 namespace mindspore { 72 namespace ps { 73 namespace core { 74 constexpr int kGroup1RandomLength = 8; 75 constexpr int kGroup2RandomLength = 4; 76 constexpr int kGroup3RandomLength = 4; 77 constexpr int kGroup4RandomLength = 4; 78 constexpr int kGroup5RandomLength = 12; 79 80 // The size of the buffer for sending and receiving data is 4096 bytes. 81 constexpr int kMessageChunkLength = 4096; 82 // The timeout period for the http client to connect to the http server is 120 seconds. 83 constexpr int kConnectionTimeout = 120; 84 constexpr char kLibeventLogPrefix[] = "[libevent log]:"; 85 86 // Find the corresponding string style of cluster state through the subscript of the enum:ClusterState 87 const std::vector<std::string> kClusterState = { 88 "ClUSTER_STARTING", // Initialization state when the cluster is just started. 89 "CLUSTER_READY", // The state after all nodes are successfully registered. 90 "CLUSTER_EXIT", // The state after the cluster exits successfully. 91 "NODE_TIMEOUT", // When a node has a heartbeat timeout 92 "CLUSTER_SCALE_OUT", // When the cluster is scale out. 93 "CLUSTER_SCALE_IN" // When the cluster is scale in. 94 }; 95 96 class CommUtil { 97 public: 98 static bool CheckIpWithRegex(const std::string &ip); 99 static bool CheckIp(const std::string &ip); 100 static bool CheckPort(const uint16_t &port); 101 static void GetAvailableInterfaceAndIP(std::string *interface, std::string *ip); 102 static std::string GenerateUUID(); 103 static std::string NodeRoleToString(const NodeRole &role); 104 static bool ValidateRankId(const enum NodeRole &node_role, const uint32_t &rank_id, const int32_t &total_worker_num, 105 const int32_t &total_server_num); 106 static bool Retry(const std::function<bool()> &func, size_t max_attempts, size_t interval_milliseconds); 107 static void LogCallback(int severity, const char *msg); 108 109 // Check if the file exists. 110 static bool IsFileExists(const std::string &file); 111 // Convert cluster state to string when response the http request. 112 static std::string ClusterStateToString(const ClusterState &state); 113 114 // Parse the configuration file according to the key. 115 static std::string ParseConfig(const Configuration &config, const std::string &data); 116 117 // verify valid of certificate time 118 static bool VerifyCertTime(const X509 *cert, int64_t time = 0); 119 // verify valid of equip certificate with CRL 120 static bool VerifyCRL(const X509 *cert, const std::string &crl_path); 121 // Check the common name of the certificate 122 static bool VerifyCommonName(const X509 *cert, const std::string &ca_path); 123 // The string is divided according to delim 124 static std::vector<std::string> Split(const std::string &s, char delim); 125 // Check the cipher list of the certificate 126 static bool VerifyCipherList(const std::vector<std::string> &list); 127 static void InitOpenSSLEnv(); 128 129 private: 130 static std::random_device rd; 131 static std::mt19937_64 gen; 132 static std::uniform_int_distribution<> dis; 133 static std::uniform_int_distribution<> dis2; 134 }; 135 } // namespace core 136 } // namespace ps 137 } // namespace mindspore 138 #endif // MINDSPORE_CCSRC_PS_CORE_COMM_UTIL_H_ 139