2016-01-18 15:33:29 +00:00
|
|
|
// Copyright (c) 2011-2016 The Cryptonote developers
|
2015-04-23 16:07:22 +00:00
|
|
|
// 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
|
|
|
#include <stdexcept>
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
2015-05-27 12:08:46 +00:00
|
|
|
#include <netdb.h>
|
2015-04-06 16:13:07 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/epoll.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-07-30 15:22:07 +00:00
|
|
|
struct TcpConnectorContextExt : public OperationContext {
|
2015-04-06 16:13:07 +00:00
|
|
|
int connection;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if (other.dispatcher != nullptr) {
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(other.context == nullptr);
|
|
|
|
context = nullptr;
|
2015-04-06 16:13:07 +00:00
|
|
|
other.dispatcher = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TcpConnector::~TcpConnector() {
|
|
|
|
}
|
|
|
|
|
|
|
|
TcpConnector& TcpConnector::operator=(TcpConnector&& other) {
|
|
|
|
dispatcher = other.dispatcher;
|
|
|
|
if (other.dispatcher != nullptr) {
|
2015-05-27 12:08:46 +00:00
|
|
|
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;
|
|
|
|
int connection = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
if (connection == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "socket failed, " + lastErrorMessage();
|
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, " + lastErrorMessage();
|
2015-04-06 16:13:07 +00:00
|
|
|
} else {
|
2015-05-27 12:08:46 +00:00
|
|
|
int flags = fcntl(connection, F_GETFL, 0);
|
|
|
|
if (flags == -1 || fcntl(connection, F_SETFL, flags | O_NONBLOCK) == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "fcntl 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_addr = htonl(address.getValue());
|
|
|
|
int result = ::connect(connection, reinterpret_cast<sockaddr *>(&addressData), sizeof addressData);
|
|
|
|
if (result == -1) {
|
|
|
|
if (errno == EINPROGRESS) {
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
ContextPair contextPair;
|
2015-05-27 12:08:46 +00:00
|
|
|
TcpConnectorContextExt connectorContext;
|
|
|
|
connectorContext.interrupted = false;
|
|
|
|
connectorContext.context = dispatcher->getCurrentContext();
|
|
|
|
connectorContext.connection = connection;
|
|
|
|
|
|
|
|
contextPair.readContext = nullptr;
|
|
|
|
contextPair.writeContext = &connectorContext;
|
|
|
|
|
|
|
|
epoll_event connectEvent;
|
|
|
|
connectEvent.events = EPOLLOUT | EPOLLRDHUP | EPOLLERR | EPOLLONESHOT;
|
|
|
|
connectEvent.data.ptr = &contextPair;
|
|
|
|
if (epoll_ctl(dispatcher->getEpoll(), EPOLL_CTL_ADD, connection, &connectEvent) == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "epoll_ctl failed, " + lastErrorMessage();
|
2015-05-27 12:08:46 +00:00
|
|
|
} else {
|
|
|
|
context = &connectorContext;
|
2015-07-30 15:22:07 +00:00
|
|
|
dispatcher->getCurrentContext()->interruptProcedure = [&] {
|
|
|
|
TcpConnectorContextExt* connectorContext1 = static_cast<TcpConnectorContextExt*>(context);
|
|
|
|
if (!connectorContext1->interrupted) {
|
|
|
|
if (close(connectorContext1->connection) == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
throw std::runtime_error("TcpListener::stop, close failed, " + lastErrorMessage());
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
connectorContext1->interrupted = true;
|
|
|
|
dispatcher->pushContext(connectorContext1->context);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
dispatcher->dispatch();
|
2015-07-30 15:22:07 +00:00
|
|
|
dispatcher->getCurrentContext()->interruptProcedure = nullptr;
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(dispatcher != nullptr);
|
|
|
|
assert(connectorContext.context == dispatcher->getCurrentContext());
|
|
|
|
assert(contextPair.readContext == nullptr);
|
|
|
|
assert(context == &connectorContext);
|
|
|
|
context = nullptr;
|
|
|
|
connectorContext.context = nullptr;
|
|
|
|
if (connectorContext.interrupted) {
|
|
|
|
throw InterruptedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (epoll_ctl(dispatcher->getEpoll(), EPOLL_CTL_DEL, connection, NULL) == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "epoll_ctl failed, " + lastErrorMessage();
|
2015-04-06 16:13:07 +00:00
|
|
|
} else {
|
2015-05-27 12:08:46 +00:00
|
|
|
if((connectorContext.events & (EPOLLERR | EPOLLHUP)) != 0) {
|
|
|
|
int result = close(connection);
|
|
|
|
assert(result != -1);
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
throw std::runtime_error("TcpConnector::connect, connection failed");
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
int retval = -1;
|
|
|
|
socklen_t retValLen = sizeof(retval);
|
|
|
|
int s = getsockopt(connection, SOL_SOCKET, SO_ERROR, &retval, &retValLen);
|
|
|
|
if (s == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "getsockopt failed, " + lastErrorMessage();
|
2015-04-06 16:13:07 +00:00
|
|
|
} else {
|
2015-05-27 12:08:46 +00:00
|
|
|
if (retval != 0) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "getsockopt failed, " + lastErrorMessage();
|
2015-04-06 16:13:07 +00:00
|
|
|
} else {
|
2015-05-27 12:08:46 +00:00
|
|
|
return TcpConnection(*dispatcher, connection);
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-27 12:08:46 +00:00
|
|
|
} else {
|
|
|
|
return TcpConnection(*dispatcher, connection);
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
int result = close(connection);
|
|
|
|
assert(result != -1);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
}
|