2015-04-23 16:07:22 +00:00
|
|
|
// Copyright (c) 2011-2015 The Cryptonote developers
|
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
#include "TcpConnector.h"
|
|
|
|
#include <cassert>
|
2015-05-27 12:08:46 +00:00
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
2015-04-06 16:13:07 +00:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2015-05-27 12:08:46 +00:00
|
|
|
#endif
|
2015-04-06 16:13:07 +00:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <mswsock.h>
|
2015-05-27 12:08:46 +00:00
|
|
|
#include <System/InterruptedException.h>
|
|
|
|
#include <System/Ipv4Address.h>
|
2015-04-06 16:13:07 +00:00
|
|
|
#include "Dispatcher.h"
|
2015-08-05 13:09:05 +00:00
|
|
|
#include "ErrorMessage.h"
|
2015-04-06 16:13:07 +00:00
|
|
|
#include "TcpConnection.h"
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
namespace System {
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
struct TcpConnectorContext : public OVERLAPPED {
|
2015-07-30 15:22:07 +00:00
|
|
|
NativeContext* context;
|
|
|
|
size_t connection;
|
2015-04-06 16:13:07 +00:00
|
|
|
bool interrupted;
|
|
|
|
};
|
|
|
|
|
|
|
|
LPFN_CONNECTEX connectEx = nullptr;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TcpConnector::TcpConnector() : dispatcher(nullptr) {
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
TcpConnector::TcpConnector(Dispatcher& dispatcher) : dispatcher(&dispatcher), context(nullptr) {
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TcpConnector::TcpConnector(TcpConnector&& other) : dispatcher(other.dispatcher) {
|
2015-05-27 12:08:46 +00:00
|
|
|
if (dispatcher != nullptr) {
|
|
|
|
assert(other.context == nullptr);
|
|
|
|
context = nullptr;
|
2015-04-06 16:13:07 +00:00
|
|
|
other.dispatcher = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TcpConnector::~TcpConnector() {
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(dispatcher == nullptr || context == nullptr);
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TcpConnector& TcpConnector::operator=(TcpConnector&& other) {
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(dispatcher == nullptr || context == nullptr);
|
2015-04-06 16:13:07 +00:00
|
|
|
dispatcher = other.dispatcher;
|
2015-05-27 12:08:46 +00:00
|
|
|
if (dispatcher != nullptr) {
|
|
|
|
assert(other.context == nullptr);
|
|
|
|
context = nullptr;
|
2015-04-06 16:13:07 +00:00
|
|
|
other.dispatcher = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
TcpConnection TcpConnector::connect(const Ipv4Address& address, uint16_t port) {
|
2015-04-06 16:13:07 +00:00
|
|
|
assert(dispatcher != nullptr);
|
|
|
|
assert(context == nullptr);
|
2015-07-30 15:22:07 +00:00
|
|
|
if (dispatcher->interrupted()) {
|
2015-04-06 16:13:07 +00:00
|
|
|
throw InterruptedException();
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
std::string message;
|
|
|
|
SOCKET connection = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
if (connection == INVALID_SOCKET) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "socket failed, " + errorMessage(WSAGetLastError());
|
2015-04-06 16:13:07 +00:00
|
|
|
} else {
|
2015-05-27 12:08:46 +00:00
|
|
|
sockaddr_in bindAddress;
|
|
|
|
bindAddress.sin_family = AF_INET;
|
|
|
|
bindAddress.sin_port = 0;
|
|
|
|
bindAddress.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
if (bind(connection, reinterpret_cast<sockaddr*>(&bindAddress), sizeof bindAddress) != 0) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "bind failed, " + errorMessage(WSAGetLastError());
|
2015-04-06 16:13:07 +00:00
|
|
|
} else {
|
2015-05-27 12:08:46 +00:00
|
|
|
GUID guidConnectEx = WSAID_CONNECTEX;
|
|
|
|
DWORD read = sizeof connectEx;
|
|
|
|
if (connectEx == nullptr && WSAIoctl(connection, SIO_GET_EXTENSION_FUNCTION_POINTER, &guidConnectEx, sizeof guidConnectEx, &connectEx, sizeof connectEx, &read, NULL, NULL) != 0) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "WSAIoctl failed, " + errorMessage(WSAGetLastError());
|
2015-04-06 16:13:07 +00:00
|
|
|
} else {
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(read == sizeof connectEx);
|
|
|
|
if (CreateIoCompletionPort(reinterpret_cast<HANDLE>(connection), dispatcher->getCompletionPort(), 0, 0) != dispatcher->getCompletionPort()) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "CreateIoCompletionPort failed, " + lastErrorMessage();
|
2015-04-06 16:13:07 +00:00
|
|
|
} else {
|
2015-05-27 12:08:46 +00:00
|
|
|
sockaddr_in addressData;
|
|
|
|
addressData.sin_family = AF_INET;
|
|
|
|
addressData.sin_port = htons(port);
|
|
|
|
addressData.sin_addr.S_un.S_addr = htonl(address.getValue());
|
|
|
|
TcpConnectorContext context2;
|
|
|
|
context2.hEvent = NULL;
|
|
|
|
if (connectEx(connection, reinterpret_cast<sockaddr*>(&addressData), sizeof addressData, NULL, 0, NULL, &context2) == TRUE) {
|
|
|
|
message = "ConnectEx returned immediately, which is not supported.";
|
2015-04-06 16:13:07 +00:00
|
|
|
} else {
|
2015-05-27 12:08:46 +00:00
|
|
|
int lastError = WSAGetLastError();
|
|
|
|
if (lastError != WSA_IO_PENDING) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "ConnectEx failed, " + errorMessage(lastError);
|
2015-04-06 16:13:07 +00:00
|
|
|
} else {
|
2015-07-30 15:22:07 +00:00
|
|
|
context2.context = dispatcher->getCurrentContext();
|
2015-05-27 12:08:46 +00:00
|
|
|
context2.connection = connection;
|
|
|
|
context2.interrupted = false;
|
|
|
|
context = &context2;
|
2015-07-30 15:22:07 +00:00
|
|
|
dispatcher->getCurrentContext()->interruptProcedure = [&]() {
|
2015-04-06 16:13:07 +00:00
|
|
|
assert(dispatcher != nullptr);
|
2015-07-30 15:22:07 +00:00
|
|
|
assert(context != nullptr);
|
|
|
|
TcpConnectorContext* context2 = static_cast<TcpConnectorContext*>(context);
|
|
|
|
if (!context2->interrupted) {
|
|
|
|
if (CancelIoEx(reinterpret_cast<HANDLE>(context2->connection), context2) != TRUE) {
|
|
|
|
DWORD lastError = GetLastError();
|
|
|
|
if (lastError != ERROR_NOT_FOUND) {
|
2015-08-05 13:09:05 +00:00
|
|
|
throw std::runtime_error("TcpConnector::stop, CancelIoEx failed, " + lastErrorMessage());
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
context2->context->interrupted = true;
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
context2->interrupted = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
dispatcher->dispatch();
|
2015-07-30 15:22:07 +00:00
|
|
|
dispatcher->getCurrentContext()->interruptProcedure = nullptr;
|
|
|
|
assert(context2.context == dispatcher->getCurrentContext());
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(context2.connection == connection);
|
|
|
|
assert(dispatcher != nullptr);
|
|
|
|
assert(context == &context2);
|
|
|
|
context = nullptr;
|
|
|
|
DWORD transferred;
|
|
|
|
DWORD flags;
|
|
|
|
if (WSAGetOverlappedResult(connection, &context2, &transferred, FALSE, &flags) != TRUE) {
|
|
|
|
lastError = WSAGetLastError();
|
|
|
|
if (lastError != ERROR_OPERATION_ABORTED) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "ConnectEx failed, " + errorMessage(lastError);
|
2015-05-27 12:08:46 +00:00
|
|
|
} else {
|
|
|
|
assert(context2.interrupted);
|
|
|
|
if (closesocket(connection) != 0) {
|
2015-08-05 13:09:05 +00:00
|
|
|
throw std::runtime_error("TcpConnector::connect, closesocket failed, " + errorMessage(WSAGetLastError()));
|
2015-05-27 12:08:46 +00:00
|
|
|
} else {
|
2015-04-06 16:13:07 +00:00
|
|
|
throw InterruptedException();
|
|
|
|
}
|
2015-05-27 12:08:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(transferred == 0);
|
|
|
|
assert(flags == 0);
|
|
|
|
DWORD value = 1;
|
|
|
|
if (setsockopt(connection, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, reinterpret_cast<char*>(&value), sizeof(value)) != 0) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "setsockopt failed, " + errorMessage(WSAGetLastError());
|
2015-04-06 16:13:07 +00:00
|
|
|
} else {
|
|
|
|
return TcpConnection(*dispatcher, connection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-27 12:08:46 +00:00
|
|
|
|
|
|
|
int result = closesocket(connection);
|
|
|
|
assert(result == 0);
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
throw std::runtime_error("TcpConnector::connect, " + message);
|
|
|
|
}
|
|
|
|
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|