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 "TcpConnection.h"
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
#include <arpa/inet.h>
|
2015-07-30 15:22:07 +00:00
|
|
|
#include <cassert>
|
2015-04-06 16:13:07 +00:00
|
|
|
#include <sys/epoll.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-08-05 13:09:05 +00:00
|
|
|
#include <System/ErrorMessage.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
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
namespace System {
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
TcpConnection::TcpConnection() : dispatcher(nullptr) {
|
|
|
|
}
|
|
|
|
|
|
|
|
TcpConnection::TcpConnection(TcpConnection&& other) : dispatcher(other.dispatcher) {
|
|
|
|
if (other.dispatcher != nullptr) {
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(other.contextPair.writeContext == nullptr);
|
|
|
|
assert(other.contextPair.readContext == nullptr);
|
2015-04-06 16:13:07 +00:00
|
|
|
connection = other.connection;
|
2015-05-27 12:08:46 +00:00
|
|
|
contextPair = other.contextPair;
|
2015-04-06 16:13:07 +00:00
|
|
|
other.dispatcher = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TcpConnection::~TcpConnection() {
|
|
|
|
if (dispatcher != nullptr) {
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(contextPair.readContext == nullptr);
|
|
|
|
assert(contextPair.writeContext == nullptr);
|
|
|
|
int result = close(connection);
|
|
|
|
assert(result != -1);
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TcpConnection& TcpConnection::operator=(TcpConnection&& other) {
|
|
|
|
if (dispatcher != nullptr) {
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(contextPair.readContext == nullptr);
|
|
|
|
assert(contextPair.writeContext == nullptr);
|
2015-04-06 16:13:07 +00:00
|
|
|
if (close(connection) == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
throw std::runtime_error("TcpConnection::operator=, close failed, " + lastErrorMessage());
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatcher = other.dispatcher;
|
|
|
|
if (other.dispatcher != nullptr) {
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(other.contextPair.readContext == nullptr);
|
|
|
|
assert(other.contextPair.writeContext == nullptr);
|
2015-04-06 16:13:07 +00:00
|
|
|
connection = other.connection;
|
2015-05-27 12:08:46 +00:00
|
|
|
contextPair = other.contextPair;
|
2015-04-06 16:13:07 +00:00
|
|
|
other.dispatcher = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t TcpConnection::read(uint8_t* data, size_t size) {
|
|
|
|
assert(dispatcher != nullptr);
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(contextPair.readContext == 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;
|
2015-04-06 16:13:07 +00:00
|
|
|
ssize_t transferred = ::recv(connection, (void *)data, size, 0);
|
|
|
|
if (transferred == -1) {
|
|
|
|
if (errno != EAGAIN && errno != EWOULDBLOCK) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "recv failed, " + lastErrorMessage();
|
2015-04-06 16:13:07 +00:00
|
|
|
} else {
|
|
|
|
epoll_event connectionEvent;
|
2015-07-30 15:22:07 +00:00
|
|
|
OperationContext operationContext;
|
2015-05-27 12:08:46 +00:00
|
|
|
operationContext.interrupted = false;
|
|
|
|
operationContext.context = dispatcher->getCurrentContext();
|
|
|
|
contextPair.readContext = &operationContext;
|
|
|
|
connectionEvent.data.ptr = &contextPair;
|
|
|
|
|
|
|
|
if(contextPair.writeContext != nullptr) {
|
2015-04-06 16:13:07 +00:00
|
|
|
connectionEvent.events = EPOLLIN | EPOLLOUT | EPOLLONESHOT;
|
2015-05-27 12:08:46 +00:00
|
|
|
} else {
|
|
|
|
connectionEvent.events = EPOLLIN | EPOLLONESHOT;
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (epoll_ctl(dispatcher->getEpoll(), EPOLL_CTL_MOD, connection, &connectionEvent) == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "epoll_ctl failed, " + lastErrorMessage();
|
2015-04-06 16:13:07 +00:00
|
|
|
} else {
|
2015-07-30 15:22:07 +00:00
|
|
|
dispatcher->getCurrentContext()->interruptProcedure = [&]() {
|
|
|
|
assert(dispatcher != nullptr);
|
|
|
|
assert(contextPair.readContext != nullptr);
|
|
|
|
epoll_event connectionEvent;
|
|
|
|
connectionEvent.events = 0;
|
|
|
|
connectionEvent.data.ptr = nullptr;
|
|
|
|
|
|
|
|
if (epoll_ctl(dispatcher->getEpoll(), EPOLL_CTL_MOD, connection, &connectionEvent) == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
throw std::runtime_error("TcpConnection::stop, epoll_ctl failed, " + lastErrorMessage());
|
2015-07-30 15:22:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
contextPair.readContext->interrupted = true;
|
|
|
|
dispatcher->pushContext(contextPair.readContext->context);
|
|
|
|
};
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
dispatcher->dispatch();
|
2015-07-30 15:22:07 +00:00
|
|
|
dispatcher->getCurrentContext()->interruptProcedure = nullptr;
|
2015-04-06 16:13:07 +00:00
|
|
|
assert(dispatcher != nullptr);
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(operationContext.context == dispatcher->getCurrentContext());
|
|
|
|
assert(contextPair.readContext == &operationContext);
|
|
|
|
|
|
|
|
if (operationContext.interrupted) {
|
|
|
|
contextPair.readContext = nullptr;
|
2015-04-06 16:13:07 +00:00
|
|
|
throw InterruptedException();
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
contextPair.readContext = nullptr;
|
|
|
|
if(contextPair.writeContext != nullptr) { //write is presented, rearm
|
2015-04-06 16:13:07 +00:00
|
|
|
epoll_event connectionEvent;
|
|
|
|
connectionEvent.events = EPOLLOUT | EPOLLONESHOT;
|
2015-05-27 12:08:46 +00:00
|
|
|
connectionEvent.data.ptr = &contextPair;
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
if (epoll_ctl(dispatcher->getEpoll(), EPOLL_CTL_MOD, connection, &connectionEvent) == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "epoll_ctl failed, " + lastErrorMessage();
|
2015-04-06 16:13:07 +00:00
|
|
|
throw std::runtime_error("TcpConnection::read");
|
|
|
|
}
|
2015-05-27 12:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if((operationContext.events & (EPOLLERR | EPOLLHUP)) != 0) {
|
|
|
|
throw std::runtime_error("TcpConnection::read");
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t transferred = ::recv(connection, (void *)data, size, 0);
|
|
|
|
if (transferred == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "recv failed, " + lastErrorMessage();
|
2015-04-06 16:13:07 +00:00
|
|
|
} else {
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(transferred <= static_cast<ssize_t>(size));
|
2015-04-06 16:13:07 +00:00
|
|
|
return transferred;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
throw std::runtime_error("TcpConnection::read, "+ message);
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(transferred <= static_cast<ssize_t>(size));
|
2015-04-06 16:13:07 +00:00
|
|
|
return transferred;
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
std::size_t TcpConnection::write(const uint8_t* data, size_t size) {
|
2015-04-06 16:13:07 +00:00
|
|
|
assert(dispatcher != nullptr);
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(contextPair.writeContext == 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;
|
|
|
|
if(size == 0) {
|
|
|
|
if(shutdown(connection, SHUT_WR) == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
throw std::runtime_error("TcpConnection::write, shutdown failed, " + lastErrorMessage());
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
return 0;
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
ssize_t transferred = ::send(connection, (void *)data, size, MSG_NOSIGNAL);
|
2015-04-06 16:13:07 +00:00
|
|
|
if (transferred == -1) {
|
|
|
|
if (errno != EAGAIN && errno != EWOULDBLOCK) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "send failed, " + lastErrorMessage();
|
2015-04-06 16:13:07 +00:00
|
|
|
} else {
|
|
|
|
epoll_event connectionEvent;
|
2015-07-30 15:22:07 +00:00
|
|
|
OperationContext operationContext;
|
2015-05-27 12:08:46 +00:00
|
|
|
operationContext.interrupted = false;
|
|
|
|
operationContext.context = dispatcher->getCurrentContext();
|
|
|
|
contextPair.writeContext = &operationContext;
|
|
|
|
connectionEvent.data.ptr = &contextPair;
|
|
|
|
|
|
|
|
if(contextPair.readContext != nullptr) {
|
2015-04-06 16:13:07 +00:00
|
|
|
connectionEvent.events = EPOLLIN | EPOLLOUT | EPOLLONESHOT;
|
2015-05-27 12:08:46 +00:00
|
|
|
} else {
|
|
|
|
connectionEvent.events = EPOLLOUT | EPOLLONESHOT;
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (epoll_ctl(dispatcher->getEpoll(), EPOLL_CTL_MOD, connection, &connectionEvent) == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "epoll_ctl failed, " + lastErrorMessage();
|
2015-04-06 16:13:07 +00:00
|
|
|
} else {
|
2015-07-30 15:22:07 +00:00
|
|
|
dispatcher->getCurrentContext()->interruptProcedure = [&]() {
|
|
|
|
assert(dispatcher != nullptr);
|
|
|
|
assert(contextPair.writeContext != nullptr);
|
|
|
|
epoll_event connectionEvent;
|
|
|
|
connectionEvent.events = 0;
|
|
|
|
connectionEvent.data.ptr = nullptr;
|
|
|
|
|
|
|
|
if (epoll_ctl(dispatcher->getEpoll(), EPOLL_CTL_MOD, connection, &connectionEvent) == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
throw std::runtime_error("TcpConnection::stop, epoll_ctl failed, " + lastErrorMessage());
|
2015-07-30 15:22:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
contextPair.writeContext->interrupted = true;
|
|
|
|
dispatcher->pushContext(contextPair.writeContext->context);
|
|
|
|
};
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
dispatcher->dispatch();
|
2015-07-30 15:22:07 +00:00
|
|
|
dispatcher->getCurrentContext()->interruptProcedure = nullptr;
|
2015-04-06 16:13:07 +00:00
|
|
|
assert(dispatcher != nullptr);
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(operationContext.context == dispatcher->getCurrentContext());
|
|
|
|
assert(contextPair.writeContext == &operationContext);
|
|
|
|
|
|
|
|
if (operationContext.interrupted) {
|
|
|
|
contextPair.writeContext = nullptr;
|
2015-04-06 16:13:07 +00:00
|
|
|
throw InterruptedException();
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
contextPair.writeContext = nullptr;
|
|
|
|
if(contextPair.readContext != nullptr) { //read is presented, rearm
|
2015-04-06 16:13:07 +00:00
|
|
|
epoll_event connectionEvent;
|
|
|
|
connectionEvent.events = EPOLLIN | EPOLLONESHOT;
|
2015-05-27 12:08:46 +00:00
|
|
|
connectionEvent.data.ptr = &contextPair;
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
if (epoll_ctl(dispatcher->getEpoll(), EPOLL_CTL_MOD, connection, &connectionEvent) == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "epoll_ctl failed, " + lastErrorMessage();
|
2015-07-30 15:22:07 +00:00
|
|
|
throw std::runtime_error("TcpConnection::write, " + message);
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
2015-05-27 12:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if((operationContext.events & (EPOLLERR | EPOLLHUP)) != 0) {
|
2015-08-05 13:09:05 +00:00
|
|
|
throw std::runtime_error("TcpConnection::write, events & (EPOLLERR | EPOLLHUP) != 0");
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t transferred = ::send(connection, (void *)data, size, 0);
|
|
|
|
if (transferred == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "send failed, " + lastErrorMessage();
|
2015-04-06 16:13:07 +00:00
|
|
|
} else {
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(transferred <= static_cast<ssize_t>(size));
|
|
|
|
return transferred;
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
throw std::runtime_error("TcpConnection::write, " + message);
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
2015-05-27 12:08:46 +00:00
|
|
|
|
|
|
|
assert(transferred <= static_cast<ssize_t>(size));
|
|
|
|
return transferred;
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
std::pair<Ipv4Address, uint16_t> TcpConnection::getPeerAddressAndPort() const {
|
2015-05-27 12:08:46 +00:00
|
|
|
sockaddr_in addr;
|
|
|
|
socklen_t size = sizeof(addr);
|
|
|
|
if (getpeername(connection, reinterpret_cast<sockaddr*>(&addr), &size) != 0) {
|
2015-08-05 13:09:05 +00:00
|
|
|
throw std::runtime_error("TcpConnection::getPeerAddress, getpeername failed, " + lastErrorMessage());
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
2015-05-27 12:08:46 +00:00
|
|
|
|
|
|
|
assert(size == sizeof(sockaddr_in));
|
|
|
|
return std::make_pair(Ipv4Address(htonl(addr.sin_addr.s_addr)), htons(addr.sin_port));
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
TcpConnection::TcpConnection(Dispatcher& dispatcher, int socket) : dispatcher(&dispatcher), connection(socket) {
|
2015-05-27 12:08:46 +00:00
|
|
|
contextPair.readContext = nullptr;
|
|
|
|
contextPair.writeContext = nullptr;
|
|
|
|
epoll_event connectionEvent;
|
|
|
|
connectionEvent.events = EPOLLONESHOT;
|
|
|
|
connectionEvent.data.ptr = nullptr;
|
|
|
|
|
|
|
|
if (epoll_ctl(dispatcher.getEpoll(), EPOLL_CTL_ADD, socket, &connectionEvent) == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
throw std::runtime_error("TcpConnection::TcpConnection, epoll_ctl failed, " + lastErrorMessage());
|
2015-05-27 12:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|