| 2130 | } |
| 2131 | |
| 2132 | std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const { |
| 2133 | std::vector<AddedNodeInfo> ret; |
| 2134 | |
| 2135 | std::list<std::string> lAddresses(0); |
| 2136 | { |
| 2137 | LOCK(m_added_nodes_mutex); |
| 2138 | ret.reserve(m_added_nodes.size()); |
| 2139 | std::copy(m_added_nodes.cbegin(), m_added_nodes.cend(), |
| 2140 | std::back_inserter(lAddresses)); |
| 2141 | } |
| 2142 | |
| 2143 | // Build a map of all already connected addresses (by IP:port and by name) |
| 2144 | // to inbound/outbound and resolved CService |
| 2145 | std::map<CService, bool> mapConnected; |
| 2146 | std::map<std::string, std::pair<bool, CService>> mapConnectedByName; |
| 2147 | { |
| 2148 | LOCK(m_nodes_mutex); |
| 2149 | for (const CNode *pnode : m_nodes) { |
| 2150 | if (pnode->addr.IsValid()) { |
| 2151 | mapConnected[pnode->addr] = pnode->IsInboundConn(); |
| 2152 | } |
| 2153 | std::string addrName{pnode->m_addr_name}; |
| 2154 | if (!addrName.empty()) { |
| 2155 | mapConnectedByName[std::move(addrName)] = |
| 2156 | std::make_pair(pnode->IsInboundConn(), |
| 2157 | static_cast<const CService &>(pnode->addr)); |
| 2158 | } |
| 2159 | } |
| 2160 | } |
| 2161 | |
| 2162 | for (const std::string &strAddNode : lAddresses) { |
| 2163 | CService service( |
| 2164 | LookupNumeric(strAddNode, Params().GetDefaultPort(strAddNode))); |
| 2165 | AddedNodeInfo addedNode{strAddNode, CService(), false, false}; |
| 2166 | if (service.IsValid()) { |
| 2167 | // strAddNode is an IP:port |
| 2168 | auto it = mapConnected.find(service); |
| 2169 | if (it != mapConnected.end()) { |
| 2170 | addedNode.resolvedAddress = service; |
| 2171 | addedNode.fConnected = true; |
| 2172 | addedNode.fInbound = it->second; |
| 2173 | } |
| 2174 | } else { |
| 2175 | // strAddNode is a name |
| 2176 | auto it = mapConnectedByName.find(strAddNode); |
| 2177 | if (it != mapConnectedByName.end()) { |
| 2178 | addedNode.resolvedAddress = it->second.second; |
| 2179 | addedNode.fConnected = true; |
| 2180 | addedNode.fInbound = it->second.first; |
| 2181 | } |
| 2182 | } |
| 2183 | ret.emplace_back(std::move(addedNode)); |
| 2184 | } |
| 2185 | |
| 2186 | return ret; |
| 2187 | } |
| 2188 | |
| 2189 | void CConnman::ThreadOpenAddedConnections() { |