| 171 | } |
| 172 | |
| 173 | bool Session::Connect(const CService& to, Connection& conn, bool& proxy_error) |
| 174 | { |
| 175 | // Refuse connecting to arbitrary ports. We don't specify any destination port to the SAM proxy |
| 176 | // when connecting (SAM 3.1 does not use ports) and it forces/defaults it to I2P_SAM31_PORT. |
| 177 | if (to.GetPort() != I2P_SAM31_PORT) { |
| 178 | proxy_error = false; |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | proxy_error = true; |
| 183 | |
| 184 | std::string session_id; |
| 185 | std::unique_ptr<Sock> sock; |
| 186 | conn.peer = to; |
| 187 | |
| 188 | try { |
| 189 | { |
| 190 | LOCK(m_mutex); |
| 191 | CreateIfNotCreatedAlready(); |
| 192 | session_id = m_session_id; |
| 193 | conn.me = m_my_addr; |
| 194 | sock = Hello(); |
| 195 | } |
| 196 | |
| 197 | const Reply& lookup_reply = |
| 198 | SendRequestAndGetReply(*sock, strprintf("NAMING LOOKUP NAME=%s", to.ToStringIP())); |
| 199 | |
| 200 | const std::string& dest = lookup_reply.Get("VALUE"); |
| 201 | |
| 202 | const Reply& connect_reply = SendRequestAndGetReply( |
| 203 | *sock, strprintf("STREAM CONNECT ID=%s DESTINATION=%s SILENT=false", session_id, dest), |
| 204 | false); |
| 205 | |
| 206 | const std::string& result = connect_reply.Get("RESULT"); |
| 207 | |
| 208 | if (result == "OK") { |
| 209 | conn.sock = std::move(sock); |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | if (result == "INVALID_ID") { |
| 214 | LOCK(m_mutex); |
| 215 | Disconnect(); |
| 216 | throw std::runtime_error("Invalid session id"); |
| 217 | } |
| 218 | |
| 219 | if (result == "CANT_REACH_PEER" || result == "TIMEOUT") { |
| 220 | proxy_error = false; |
| 221 | } |
| 222 | |
| 223 | throw std::runtime_error(strprintf("\"%s\"", connect_reply.full)); |
| 224 | } catch (const std::runtime_error& e) { |
| 225 | Log("Error connecting to %s: %s", to.ToString(), e.what()); |
| 226 | CheckControlSock(); |
| 227 | return false; |
| 228 | } |
| 229 | } |
| 230 |
no test coverage detected