2015-05-27 12:08:46 +00:00
|
|
|
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
|
2015-04-06 16:13:07 +00:00
|
|
|
//
|
|
|
|
// This file is part of Bytecoin.
|
|
|
|
//
|
|
|
|
// Bytecoin is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Bytecoin is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#include "TcpListener.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 <netinet/in.h>
|
|
|
|
#include <sys/errno.h>
|
2015-04-06 16:13:07 +00:00
|
|
|
#include <sys/event.h>
|
|
|
|
#include <sys/socket.h>
|
2015-05-27 12:08:46 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-04-06 16:13:07 +00:00
|
|
|
#include "Dispatcher.h"
|
|
|
|
#include "TcpConnection.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) {
|
|
|
|
message = "socket() failed, errno=" + std::to_string(errno);
|
|
|
|
} else {
|
|
|
|
int flags = fcntl(listener, F_GETFL, 0);
|
|
|
|
if (flags == -1 || (fcntl(listener, F_SETFL, flags | O_NONBLOCK) == -1)) {
|
|
|
|
message = "fcntl() failed errno=" + std::to_string(errno);
|
|
|
|
} else {
|
|
|
|
int on = 1;
|
|
|
|
if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on) == -1) {
|
|
|
|
message = "setsockopt failed, errno=" + std::to_string(errno);
|
|
|
|
} 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) {
|
|
|
|
message = "bind failed, errno=" + std::to_string(errno);
|
|
|
|
} else if (listen(listener, SOMAXCONN) != 0) {
|
|
|
|
message = "listen failed, errno=" + std::to_string(errno);
|
|
|
|
} else {
|
|
|
|
struct kevent event;
|
|
|
|
EV_SET(&event, listener, EVFILT_READ, EV_ADD | EV_DISABLE, 0, SOMAXCONN, NULL);
|
|
|
|
|
|
|
|
if (kevent(dispatcher.getKqueue(), &event, 1, NULL, 0, NULL) == -1) {
|
|
|
|
message = "kevent() failed, errno=" + std::to_string(errno);
|
|
|
|
} else {
|
|
|
|
stopped = false;
|
|
|
|
context = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
if (close(listener) == -1) {
|
|
|
|
message = "close failed, errno=" + std::to_string(errno);
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
stopped = other.stopped;
|
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-05-27 12:08:46 +00:00
|
|
|
throw std::runtime_error("TcpListener::operator=, close failed, errno=" + std::to_string(errno));
|
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;
|
|
|
|
stopped = other.stopped;
|
2015-05-27 12:08:46 +00:00
|
|
|
context = nullptr;
|
2015-04-06 16:13:07 +00:00
|
|
|
other.dispatcher = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TcpListener::start() {
|
|
|
|
assert(dispatcher != nullptr);
|
|
|
|
assert(stopped);
|
|
|
|
stopped = false;
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
void TcpListener::stop() {
|
|
|
|
assert(dispatcher != nullptr);
|
|
|
|
assert(!stopped);
|
|
|
|
if (context != nullptr) {
|
|
|
|
Dispatcher::OperationContext* listenerContext = static_cast<Dispatcher::OperationContext*>(context);
|
|
|
|
if (!listenerContext->interrupted) {
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
struct kevent event;
|
|
|
|
EV_SET(&event, listener, EVFILT_READ, EV_DELETE | EV_DISABLE, 0, 0, NULL);
|
|
|
|
|
|
|
|
if (kevent(dispatcher->getKqueue(), &event, 1, NULL, 0, NULL) == -1) {
|
|
|
|
throw std::runtime_error("TcpListener::stop, kevent() failed, errno=" + std::to_string(errno));
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
listenerContext->interrupted = true;
|
|
|
|
dispatcher->pushContext(listenerContext->context);
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
stopped = true;
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TcpConnection TcpListener::accept() {
|
|
|
|
assert(dispatcher != nullptr);
|
|
|
|
assert(context == nullptr);
|
|
|
|
if (stopped) {
|
|
|
|
throw InterruptedException();
|
|
|
|
}
|
|
|
|
|
2015-05-27 12:08:46 +00:00
|
|
|
std::string message;
|
|
|
|
Dispatcher::OperationContext listenerContext;
|
|
|
|
listenerContext.context = dispatcher->getCurrentContext();
|
|
|
|
listenerContext.interrupted = false;
|
2015-04-06 16:13:07 +00:00
|
|
|
struct kevent event;
|
2015-05-27 12:08:46 +00:00
|
|
|
EV_SET(&event, listener, EVFILT_READ, EV_ADD | EV_ENABLE | EV_ONESHOT, 0, SOMAXCONN, &listenerContext);
|
2015-04-06 16:13:07 +00:00
|
|
|
if (kevent(dispatcher->getKqueue(), &event, 1, NULL, 0, NULL) == -1) {
|
2015-05-27 12:08:46 +00:00
|
|
|
message = "kevent() failed, errno=" + std::to_string(errno);
|
2015-04-06 16:13:07 +00:00
|
|
|
} else {
|
2015-05-27 12:08:46 +00:00
|
|
|
context = &listenerContext;
|
|
|
|
dispatcher->dispatch();
|
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(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
|
|
|
sockaddr inAddr;
|
|
|
|
socklen_t inLen = sizeof(inAddr);
|
|
|
|
int connection = ::accept(listener, &inAddr, &inLen);
|
|
|
|
if (connection == -1) {
|
|
|
|
message = "accept() failed, errno=" + std::to_string(errno);
|
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) {
|
|
|
|
message = "fcntl() failed errno=" + std::to_string(errno);
|
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
|
|
|
throw std::runtime_error("TcpListener::accept, " + message);
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|