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 "TcpListener.h"
|
2015-05-27 12:08:46 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <stdexcept>
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netdb.h>
|
2015-04-06 16:13:07 +00:00
|
|
|
#include <sys/epoll.h>
|
|
|
|
#include <unistd.h>
|
2015-07-30 15:22:07 +00:00
|
|
|
#include <string.h>
|
2015-05-27 12:08:46 +00:00
|
|
|
|
2015-04-06 16:13:07 +00:00
|
|
|
#include "Dispatcher.h"
|
|
|
|
#include "TcpConnection.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
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
TcpListener::TcpListener() : dispatcher(nullptr) {
|
|
|
|
}
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
TcpListener::TcpListener(Dispatcher& dispatcher, const Ipv4Address& addr, uint16_t port) : dispatcher(&dispatcher) {
|
|
|
|
std::string message;
|
|
|
|
listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
if (listener == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "socket failed, " + lastErrorMessage();
|
2015-05-27 12:08:46 +00:00
|
|
|
} else {
|
|
|
|
int flags = fcntl(listener, F_GETFL, 0);
|
|
|
|
if (flags == -1 || fcntl(listener, F_SETFL, flags | O_NONBLOCK) == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "fcntl failed, " + lastErrorMessage();
|
2015-05-27 12:08:46 +00:00
|
|
|
} else {
|
|
|
|
int on = 1;
|
|
|
|
if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on) == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "setsockopt failed, " + lastErrorMessage();
|
2015-05-27 12:08:46 +00:00
|
|
|
} else {
|
|
|
|
sockaddr_in address;
|
|
|
|
address.sin_family = AF_INET;
|
|
|
|
address.sin_port = htons(port);
|
|
|
|
address.sin_addr.s_addr = htonl( addr.getValue());
|
|
|
|
if (bind(listener, reinterpret_cast<sockaddr *>(&address), sizeof address) != 0) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "bind failed, " + lastErrorMessage();
|
2015-05-27 12:08:46 +00:00
|
|
|
} else if (listen(listener, SOMAXCONN) != 0) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "listen failed, " + lastErrorMessage();
|
2015-05-27 12:08:46 +00:00
|
|
|
} else {
|
|
|
|
epoll_event listenEvent;
|
|
|
|
listenEvent.events = 0;
|
|
|
|
listenEvent.data.ptr = nullptr;
|
|
|
|
|
|
|
|
if (epoll_ctl(dispatcher.getEpoll(), EPOLL_CTL_ADD, listener, &listenEvent) == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "epoll_ctl failed, " + lastErrorMessage();
|
2015-05-27 12:08:46 +00:00
|
|
|
} else {
|
|
|
|
context = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
int result = close(listener);
|
|
|
|
assert(result != -1);
|
|
|
|
}
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
throw std::runtime_error("TcpListener::TcpListener, " + message);
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TcpListener::TcpListener(TcpListener&& other) : dispatcher(other.dispatcher) {
|
|
|
|
if (other.dispatcher != nullptr) {
|
2015-05-27 12:08:46 +00:00
|
|
|
assert(other.context == nullptr);
|
2015-04-06 16:13:07 +00:00
|
|
|
listener = other.listener;
|
2015-05-27 12:08:46 +00:00
|
|
|
context = nullptr;
|
2015-04-06 16:13:07 +00:00
|
|
|
other.dispatcher = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TcpListener::~TcpListener() {
|
|
|
|
if (dispatcher != nullptr) {
|
|
|
|
assert(context == nullptr);
|
2015-05-27 12:08:46 +00:00
|
|
|
int result = close(listener);
|
|
|
|
assert(result != -1);
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TcpListener& TcpListener::operator=(TcpListener&& other) {
|
|
|
|
if (dispatcher != nullptr) {
|
|
|
|
assert(context == nullptr);
|
|
|
|
if (close(listener) == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
throw std::runtime_error("TcpListener::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.context == nullptr);
|
2015-04-06 16:13:07 +00:00
|
|
|
listener = other.listener;
|
2015-05-27 12:08:46 +00:00
|
|
|
context = nullptr;
|
2015-04-06 16:13:07 +00:00
|
|
|
other.dispatcher = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
TcpConnection TcpListener::accept() {
|
|
|
|
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-07-30 15:22:07 +00:00
|
|
|
ContextPair contextPair;
|
|
|
|
OperationContext listenerContext;
|
2015-05-27 12:08:46 +00:00
|
|
|
listenerContext.interrupted = false;
|
|
|
|
listenerContext.context = dispatcher->getCurrentContext();
|
|
|
|
|
|
|
|
contextPair.writeContext = nullptr;
|
|
|
|
contextPair.readContext = &listenerContext;
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
epoll_event listenEvent;
|
|
|
|
listenEvent.events = EPOLLIN | EPOLLONESHOT;
|
2015-05-27 12:08:46 +00:00
|
|
|
listenEvent.data.ptr = &contextPair;
|
|
|
|
std::string message;
|
2015-04-06 16:13:07 +00:00
|
|
|
if (epoll_ctl(dispatcher->getEpoll(), EPOLL_CTL_MOD, listener, &listenEvent) == -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
|
|
|
context = &listenerContext;
|
2015-07-30 15:22:07 +00:00
|
|
|
dispatcher->getCurrentContext()->interruptProcedure = [&]() {
|
|
|
|
assert(dispatcher != nullptr);
|
|
|
|
assert(context != nullptr);
|
|
|
|
OperationContext* listenerContext = static_cast<OperationContext*>(context);
|
|
|
|
if (!listenerContext->interrupted) {
|
|
|
|
epoll_event listenEvent;
|
|
|
|
listenEvent.events = 0;
|
|
|
|
listenEvent.data.ptr = nullptr;
|
|
|
|
|
|
|
|
if (epoll_ctl(dispatcher->getEpoll(), EPOLL_CTL_MOD, listener, &listenEvent) == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
throw std::runtime_error("TcpListener::stop, epoll_ctl failed, " + lastErrorMessage() );
|
2015-07-30 15:22:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
listenerContext->interrupted = true;
|
|
|
|
dispatcher->pushContext(listenerContext->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(listenerContext.context == dispatcher->getCurrentContext());
|
|
|
|
assert(contextPair.writeContext == nullptr);
|
|
|
|
assert(context == &listenerContext);
|
2015-04-06 16:13:07 +00:00
|
|
|
context = nullptr;
|
2015-05-27 12:08:46 +00:00
|
|
|
listenerContext.context = nullptr;
|
|
|
|
if (listenerContext.interrupted) {
|
2015-04-06 16:13:07 +00:00
|
|
|
throw InterruptedException();
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
if((listenerContext.events & (EPOLLERR | EPOLLHUP)) != 0) {
|
|
|
|
throw std::runtime_error("TcpListener::accept, accepting failed");
|
|
|
|
}
|
|
|
|
|
2015-04-06 16:13:07 +00:00
|
|
|
sockaddr inAddr;
|
|
|
|
socklen_t inLen = sizeof(inAddr);
|
|
|
|
int connection = ::accept(listener, &inAddr, &inLen);
|
|
|
|
if (connection == -1) {
|
2015-08-05 13:09:05 +00:00
|
|
|
message = "accept failed, " + lastErrorMessage();
|
2015-04-06 16:13:07 +00:00
|
|
|
} else {
|
|
|
|
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 {
|
|
|
|
return TcpConnection(*dispatcher, connection);
|
|
|
|
}
|
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("TcpListener::accept, " + message);
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|