2017-02-21 20:49:29 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_SERVERSOCKET_HPP
|
|
|
|
#define INCLUDED_SERVERSOCKET_HPP
|
|
|
|
|
|
|
|
#include "memory"
|
|
|
|
|
|
|
|
#include "Socket.hpp"
|
2017-02-25 09:54:51 -06:00
|
|
|
#include "Log.hpp"
|
2017-02-21 20:49:29 -06:00
|
|
|
|
2017-03-16 12:43:33 -05:00
|
|
|
#include <Poco/Net/SocketAddress.h>
|
|
|
|
|
2017-02-21 20:49:29 -06:00
|
|
|
class SocketFactory
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual std::shared_ptr<Socket> create(const int fd) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// A non-blocking, streaming socket.
|
|
|
|
class ServerSocket : public Socket
|
|
|
|
{
|
|
|
|
public:
|
2018-01-09 08:02:02 -06:00
|
|
|
ServerSocket(Socket::Type type, SocketPoll& clientPoller, std::shared_ptr<SocketFactory> sockFactory) :
|
|
|
|
Socket(type),
|
|
|
|
_type(type),
|
2017-02-21 20:49:29 -06:00
|
|
|
_clientPoller(clientPoller),
|
|
|
|
_sockFactory(std::move(sockFactory))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-01-09 08:02:02 -06:00
|
|
|
enum Type { Local, Public };
|
|
|
|
|
2017-02-21 20:49:29 -06:00
|
|
|
/// Binds to a local address (Servers only).
|
|
|
|
/// Does not retry on error.
|
2018-01-09 08:02:02 -06:00
|
|
|
/// Returns true only on success.
|
|
|
|
bool bind(Type type, int port);
|
2017-02-21 20:49:29 -06:00
|
|
|
|
|
|
|
/// Listen to incoming connections (Servers only).
|
|
|
|
/// Does not retry on error.
|
|
|
|
/// Returns true on success only.
|
|
|
|
bool listen(const int backlog = 64)
|
|
|
|
{
|
|
|
|
const int rc = ::listen(getFD(), backlog);
|
2018-01-09 08:02:02 -06:00
|
|
|
|
|
|
|
if (rc)
|
|
|
|
LOG_SYS("Failed to listen");
|
2017-03-30 04:15:28 -05:00
|
|
|
return rc == 0;
|
2017-02-21 20:49:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Accepts an incoming connection (Servers only).
|
|
|
|
/// Does not retry on error.
|
|
|
|
/// Returns a valid Socket shared_ptr on success only.
|
|
|
|
std::shared_ptr<Socket> accept()
|
|
|
|
{
|
|
|
|
// Accept a connection (if any) and set it to non-blocking.
|
2017-12-12 23:31:29 -06:00
|
|
|
// There still need the client's address to filter request from POST(call from REST) here.
|
2018-07-10 14:50:11 -05:00
|
|
|
struct sockaddr_in6 clientInfo;
|
|
|
|
socklen_t addrlen = sizeof(clientInfo);
|
2017-12-12 23:31:29 -06:00
|
|
|
const int rc = ::accept4(getFD(), (struct sockaddr *)&clientInfo, &addrlen, SOCK_NONBLOCK);
|
2017-03-04 11:06:23 -06:00
|
|
|
LOG_DBG("Accepted socket #" << rc << ", creating socket object.");
|
|
|
|
try
|
|
|
|
{
|
2017-03-07 01:00:51 -06:00
|
|
|
// Create a socket object using the factory.
|
2017-12-12 23:31:29 -06:00
|
|
|
if (rc != -1)
|
|
|
|
{
|
2018-05-13 06:32:05 -05:00
|
|
|
char addrstr[INET6_ADDRSTRLEN];
|
|
|
|
|
|
|
|
const void *inAddr;
|
2018-07-10 14:50:11 -05:00
|
|
|
if (clientInfo.sin6_family == AF_INET)
|
2018-05-13 06:32:05 -05:00
|
|
|
{
|
2018-07-10 14:50:11 -05:00
|
|
|
auto ipv4 = (struct sockaddr_in *)&clientInfo;
|
2018-05-13 06:32:05 -05:00
|
|
|
inAddr = &(ipv4->sin_addr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-10 14:50:11 -05:00
|
|
|
auto ipv6 = (struct sockaddr_in6 *)&clientInfo;
|
2018-05-13 06:32:05 -05:00
|
|
|
inAddr = &(ipv6->sin6_addr);
|
|
|
|
}
|
|
|
|
|
2018-07-10 14:50:11 -05:00
|
|
|
inet_ntop(clientInfo.sin6_family, inAddr, addrstr, sizeof(addrstr));
|
2017-12-12 23:31:29 -06:00
|
|
|
std::shared_ptr<Socket> _socket = _sockFactory->create(rc);
|
2018-05-13 06:32:05 -05:00
|
|
|
_socket->_clientAddress = addrstr;
|
2018-07-10 14:50:11 -05:00
|
|
|
LOG_DBG("Accepted socket has family " << clientInfo.sin6_family <<
|
2018-05-13 06:32:05 -05:00
|
|
|
" address " << _socket->_clientAddress);
|
2017-12-12 23:31:29 -06:00
|
|
|
return _socket;
|
|
|
|
}
|
|
|
|
return std::shared_ptr<Socket>(nullptr);
|
2017-03-04 11:06:23 -06:00
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
LOG_SYS("Failed to create client socket #" << rc << ". Error: " << ex.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2017-02-21 20:49:29 -06:00
|
|
|
}
|
|
|
|
|
2017-03-17 16:59:09 -05:00
|
|
|
int getPollEvents(std::chrono::steady_clock::time_point /* now */,
|
|
|
|
int & /* timeoutMaxMs */) override
|
2017-02-21 20:49:29 -06:00
|
|
|
{
|
|
|
|
return POLLIN;
|
|
|
|
}
|
|
|
|
|
2017-03-12 18:03:45 -05:00
|
|
|
void dumpState(std::ostream& os) override;
|
2017-03-06 09:45:34 -06:00
|
|
|
|
2017-05-05 05:51:43 -05:00
|
|
|
void handlePoll(SocketDisposition &,
|
|
|
|
std::chrono::steady_clock::time_point /* now */,
|
|
|
|
int events) override
|
2017-02-21 20:49:29 -06:00
|
|
|
{
|
|
|
|
if (events & POLLIN)
|
|
|
|
{
|
|
|
|
std::shared_ptr<Socket> clientSocket = accept();
|
|
|
|
if (!clientSocket)
|
|
|
|
{
|
|
|
|
const std::string msg = "Failed to accept. (errno: ";
|
|
|
|
throw std::runtime_error(msg + std::strerror(errno) + ")");
|
|
|
|
}
|
|
|
|
|
2017-02-27 20:40:44 -06:00
|
|
|
LOG_DBG("Accepted client #" << clientSocket->getFD());
|
2017-02-21 20:49:29 -06:00
|
|
|
_clientPoller.insertNewSocket(clientSocket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-01-09 08:02:02 -06:00
|
|
|
Socket::Type _type;
|
2017-02-21 20:49:29 -06:00
|
|
|
SocketPoll& _clientPoller;
|
2017-03-06 13:50:06 -06:00
|
|
|
std::shared_ptr<SocketFactory> _sockFactory;
|
2017-02-21 20:49:29 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|