| 220 | } |
| 221 | |
| 222 | bool Session::Connect(const CService& to, Connection& conn, bool& proxy_error) |
| 223 | { |
| 224 | // Refuse connecting to arbitrary ports. We don't specify any destination port to the SAM proxy |
| 225 | // when connecting (SAM 3.1 does not use ports) and it forces/defaults it to I2P_SAM31_PORT. |
| 226 | if (to.GetPort() != I2P_SAM31_PORT) { |
| 227 | LogDebug(BCLog::I2P, "Error connecting to %s, connection refused due to arbitrary port %s\n", to.ToStringAddrPort(), to.GetPort()); |
| 228 | proxy_error = false; |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | proxy_error = true; |
| 233 | |
| 234 | std::string session_id; |
| 235 | std::unique_ptr<Sock> sock; |
| 236 | conn.peer = to; |
| 237 | |
| 238 | try { |
| 239 | { |
| 240 | LOCK(m_mutex); |
| 241 | CreateIfNotCreatedAlready(); |
| 242 | session_id = m_session_id; |
| 243 | conn.me = m_my_addr; |
| 244 | sock = Hello(); |
| 245 | } |
| 246 | |
| 247 | const Reply& lookup_reply = |
| 248 | SendRequestAndGetReply(*sock, strprintf("NAMING LOOKUP NAME=%s", to.ToStringAddr())); |
| 249 | |
| 250 | const std::string& dest = lookup_reply.Get("VALUE"); |
| 251 | |
| 252 | const Reply& connect_reply = SendRequestAndGetReply( |
| 253 | *sock, strprintf("STREAM CONNECT ID=%s DESTINATION=%s SILENT=false", session_id, dest), |
| 254 | false); |
| 255 | |
| 256 | const std::string& result = connect_reply.Get("RESULT"); |
| 257 | |
| 258 | if (result == "OK") { |
| 259 | conn.sock = std::move(sock); |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | if (result == "INVALID_ID") { |
| 264 | LOCK(m_mutex); |
| 265 | Disconnect(); |
| 266 | throw std::runtime_error("Invalid session id"); |
| 267 | } |
| 268 | |
| 269 | if (result == "CANT_REACH_PEER" || result == "TIMEOUT") { |
| 270 | proxy_error = false; |
| 271 | } |
| 272 | |
| 273 | throw std::runtime_error(strprintf("\"%s\"", connect_reply.full)); |
| 274 | } catch (const std::runtime_error& e) { |
| 275 | LogDebug(BCLog::I2P, "Error connecting to %s: %s\n", to.ToStringAddrPort(), e.what()); |
| 276 | CheckControlSock(); |
| 277 | return false; |
| 278 | } |
| 279 | } |
no test coverage detected