2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2013-01-15 21:42:55 +01:00
|
|
|
#ifndef MPD_SERVER_SOCKET_HXX
|
|
|
|
#define MPD_SERVER_SOCKET_HXX
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2022-01-10 17:59:38 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <cassert>
|
2013-11-04 20:10:46 +01:00
|
|
|
#include <list>
|
2013-01-30 12:56:23 +01:00
|
|
|
|
2015-02-10 20:30:10 +01:00
|
|
|
class SocketAddress;
|
2015-07-22 11:05:08 +02:00
|
|
|
class AllocatedSocketAddress;
|
2017-08-10 19:13:18 +02:00
|
|
|
class UniqueSocketDescriptor;
|
2013-01-15 22:50:49 +01:00
|
|
|
class EventLoop;
|
2013-10-19 17:40:56 +02:00
|
|
|
class AllocatedPath;
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2013-11-24 19:28:04 +01:00
|
|
|
/**
|
|
|
|
* A socket that accepts incoming stream connections (e.g. TCP).
|
|
|
|
*/
|
2013-01-30 12:56:23 +01:00
|
|
|
class ServerSocket {
|
2018-10-30 20:16:26 +01:00
|
|
|
class OneServerSocket;
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2013-01-30 12:56:23 +01:00
|
|
|
EventLoop &loop;
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2013-11-04 20:10:46 +01:00
|
|
|
std::list<OneServerSocket> sockets;
|
2012-02-13 20:58:57 +01:00
|
|
|
|
2022-01-10 17:59:38 +01:00
|
|
|
#ifdef HAVE_TCP
|
|
|
|
/**
|
2022-01-11 20:10:42 +01:00
|
|
|
* A non-negative value sets the IPPROTO_IP/IP_TOS or
|
|
|
|
* IPPROTO_IPV6/IPV6_TCLASS socket option.
|
2022-01-10 17:59:38 +01:00
|
|
|
*/
|
2022-01-11 20:10:42 +01:00
|
|
|
int dscp_class = -1;
|
2022-01-10 17:59:38 +01:00
|
|
|
#endif
|
|
|
|
|
2018-10-30 20:11:58 +01:00
|
|
|
unsigned next_serial = 1;
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2013-01-30 12:56:23 +01:00
|
|
|
public:
|
2018-10-30 19:57:39 +01:00
|
|
|
ServerSocket(EventLoop &_loop) noexcept;
|
|
|
|
~ServerSocket() noexcept;
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2018-10-30 19:57:39 +01:00
|
|
|
EventLoop &GetEventLoop() const noexcept {
|
2013-01-30 14:22:58 +01:00
|
|
|
return loop;
|
|
|
|
}
|
|
|
|
|
2022-01-10 17:59:38 +01:00
|
|
|
#ifdef HAVE_TCP
|
2022-01-11 20:10:42 +01:00
|
|
|
void SetDscpClass(int _dscp_class) noexcept {
|
2022-01-10 17:59:38 +01:00
|
|
|
assert(sockets.empty());
|
|
|
|
|
2022-01-11 20:10:42 +01:00
|
|
|
dscp_class = _dscp_class;
|
2022-01-10 17:59:38 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-01-30 12:56:23 +01:00
|
|
|
private:
|
2018-10-30 20:43:52 +01:00
|
|
|
template<typename A>
|
|
|
|
OneServerSocket &AddAddress(A &&address) noexcept;
|
2013-01-30 12:56:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a listener on a port on all IPv4 interfaces.
|
|
|
|
*
|
|
|
|
* @param port the TCP port
|
|
|
|
*/
|
2018-10-30 19:57:39 +01:00
|
|
|
void AddPortIPv4(unsigned port) noexcept;
|
2013-01-30 12:56:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a listener on a port on all IPv6 interfaces.
|
|
|
|
*
|
|
|
|
* @param port the TCP port
|
|
|
|
*/
|
2018-10-30 19:57:39 +01:00
|
|
|
void AddPortIPv6(unsigned port) noexcept;
|
2013-01-30 12:56:23 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Add a listener on a port on all interfaces.
|
|
|
|
*
|
2016-10-28 10:36:05 +02:00
|
|
|
* Throws #std::runtime_error on error.
|
|
|
|
*
|
2013-01-30 12:56:23 +01:00
|
|
|
* @param port the TCP port
|
|
|
|
*/
|
2016-10-28 10:36:05 +02:00
|
|
|
void AddPort(unsigned port);
|
2013-01-30 12:56:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves a host name, and adds listeners on all addresses in the
|
|
|
|
* result set.
|
|
|
|
*
|
2016-10-28 10:36:05 +02:00
|
|
|
* Throws #std::runtime_error on error.
|
|
|
|
*
|
2013-01-30 12:56:23 +01:00
|
|
|
* @param hostname the host name to be resolved
|
|
|
|
* @param port the TCP port
|
|
|
|
*/
|
2016-10-28 10:36:05 +02:00
|
|
|
void AddHost(const char *hostname, unsigned port);
|
2013-01-30 12:56:23 +01:00
|
|
|
|
|
|
|
/**
|
2019-03-20 12:57:26 +01:00
|
|
|
* Add a listener on a local socket.
|
2013-01-30 12:56:23 +01:00
|
|
|
*
|
2016-10-28 10:36:05 +02:00
|
|
|
* Throws #std::runtime_error on error.
|
|
|
|
*
|
2013-01-30 12:56:23 +01:00
|
|
|
* @param path the absolute socket path
|
|
|
|
*/
|
2016-10-28 10:36:05 +02:00
|
|
|
void AddPath(AllocatedPath &&path);
|
2013-01-30 12:56:23 +01:00
|
|
|
|
2019-02-25 13:01:42 +01:00
|
|
|
/**
|
|
|
|
* Add a listener on an abstract local socket (Linux specific).
|
|
|
|
*
|
|
|
|
* Throws on error.
|
|
|
|
*
|
|
|
|
* @param name the abstract socket name, starting with a '@'
|
|
|
|
* instead of a null byte
|
|
|
|
*/
|
|
|
|
void AddAbstract(const char *name);
|
|
|
|
|
2013-01-30 12:56:23 +01:00
|
|
|
/**
|
|
|
|
* Add a socket descriptor that is accepting connections. After this
|
|
|
|
* has been called, don't call server_socket_open(), because the
|
|
|
|
* socket is already open.
|
2016-10-28 10:36:05 +02:00
|
|
|
*
|
|
|
|
* Throws #std::runtime_error on error.
|
|
|
|
*/
|
2018-10-30 20:05:57 +01:00
|
|
|
void AddFD(UniqueSocketDescriptor fd);
|
2016-10-28 10:36:05 +02:00
|
|
|
|
2018-10-30 20:44:56 +01:00
|
|
|
void AddFD(UniqueSocketDescriptor fd,
|
|
|
|
AllocatedSocketAddress &&address) noexcept;
|
|
|
|
|
2018-07-15 21:35:14 +02:00
|
|
|
bool IsEmpty() const noexcept {
|
|
|
|
return sockets.empty();
|
|
|
|
}
|
|
|
|
|
2016-10-28 10:36:05 +02:00
|
|
|
/**
|
|
|
|
* Throws #std::runtime_error on error.
|
2013-01-30 12:56:23 +01:00
|
|
|
*/
|
2016-10-28 10:36:05 +02:00
|
|
|
void Open();
|
2013-01-30 12:56:23 +01:00
|
|
|
|
2018-10-30 19:57:39 +01:00
|
|
|
void Close() noexcept;
|
2013-01-30 13:20:27 +01:00
|
|
|
|
|
|
|
protected:
|
2017-11-10 20:43:14 +01:00
|
|
|
virtual void OnAccept(UniqueSocketDescriptor fd,
|
2018-10-30 20:13:07 +01:00
|
|
|
SocketAddress address, int uid) noexcept = 0;
|
2013-01-30 12:56:23 +01:00
|
|
|
};
|
2010-10-05 20:29:41 +02:00
|
|
|
|
|
|
|
#endif
|