2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2019-04-03 20:03:17 +02:00
|
|
|
* Copyright 2003-2019 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
|
|
|
|
2008-10-31 09:19:53 +01:00
|
|
|
#ifndef MPD_CLIENT_H
|
|
|
|
#define MPD_CLIENT_H
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-10-19 19:35:37 +02:00
|
|
|
#include "ClientMessage.hxx"
|
2019-04-03 20:03:17 +02:00
|
|
|
#include "command/CommandResult.hxx"
|
2013-10-20 13:41:04 +02:00
|
|
|
#include "command/CommandListBuilder.hxx"
|
2017-02-07 16:52:59 +01:00
|
|
|
#include "tag/Mask.hxx"
|
2013-10-19 19:35:37 +02:00
|
|
|
#include "event/FullyBufferedSocket.hxx"
|
2017-08-29 16:22:01 +02:00
|
|
|
#include "event/TimerEvent.hxx"
|
2018-08-20 16:19:17 +02:00
|
|
|
#include "util/Compiler.h"
|
2012-08-02 18:15:49 +02:00
|
|
|
|
2016-03-01 22:08:13 +01:00
|
|
|
#include <boost/intrusive/link_mode.hpp>
|
|
|
|
#include <boost/intrusive/list_hook.hpp>
|
2014-06-10 18:57:30 +02:00
|
|
|
|
2013-10-19 19:35:37 +02:00
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <list>
|
|
|
|
|
2008-10-08 10:49:29 +02:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2018-07-17 23:07:50 +02:00
|
|
|
struct ConfigData;
|
2015-02-10 20:30:10 +01:00
|
|
|
class SocketAddress;
|
2017-08-10 19:13:18 +02:00
|
|
|
class UniqueSocketDescriptor;
|
2013-01-14 23:42:06 +01:00
|
|
|
class EventLoop;
|
2014-02-02 13:59:07 +01:00
|
|
|
class Path;
|
2017-02-25 10:00:05 +01:00
|
|
|
struct Instance;
|
2013-01-07 10:59:56 +01:00
|
|
|
struct Partition;
|
2018-09-21 18:00:23 +02:00
|
|
|
class PlayerControl;
|
2017-02-20 12:44:09 +01:00
|
|
|
struct playlist;
|
2014-02-01 00:26:34 +01:00
|
|
|
class Database;
|
2014-02-07 00:29:07 +01:00
|
|
|
class Storage;
|
2013-10-19 19:35:37 +02:00
|
|
|
|
2014-06-10 18:57:30 +02:00
|
|
|
class Client final
|
2017-08-29 16:22:01 +02:00
|
|
|
: FullyBufferedSocket,
|
2014-06-10 18:57:30 +02:00
|
|
|
public boost::intrusive::list_base_hook<boost::intrusive::link_mode<boost::intrusive::normal_link>> {
|
2017-08-29 16:22:01 +02:00
|
|
|
TimerEvent timeout_event;
|
|
|
|
|
2017-02-25 10:23:23 +01:00
|
|
|
Partition *partition;
|
2013-10-19 19:35:37 +02:00
|
|
|
|
2017-02-25 09:58:58 +01:00
|
|
|
public:
|
2013-10-19 19:35:37 +02:00
|
|
|
unsigned permission;
|
|
|
|
|
|
|
|
/** the uid of the client process, or -1 if unknown */
|
2017-02-03 20:41:31 +01:00
|
|
|
const int uid;
|
2013-10-19 19:35:37 +02:00
|
|
|
|
|
|
|
CommandListBuilder cmd_list;
|
|
|
|
|
2017-02-03 20:41:31 +01:00
|
|
|
const unsigned int num; /* client number */
|
2013-10-19 19:35:37 +02:00
|
|
|
|
|
|
|
/** is this client waiting for an "idle" response? */
|
2017-08-11 09:21:29 +02:00
|
|
|
bool idle_waiting = false;
|
2013-10-19 19:35:37 +02:00
|
|
|
|
|
|
|
/** idle flags pending on this client, to be sent as soon as
|
|
|
|
the client enters "idle" */
|
2017-08-11 09:21:29 +02:00
|
|
|
unsigned idle_flags = 0;
|
2013-10-19 19:35:37 +02:00
|
|
|
|
|
|
|
/** idle flags that the client wants to receive */
|
|
|
|
unsigned idle_subscriptions;
|
|
|
|
|
2017-02-07 16:52:59 +01:00
|
|
|
/**
|
|
|
|
* The tags this client is interested in.
|
|
|
|
*/
|
|
|
|
TagMask tag_mask = TagMask::All();
|
|
|
|
|
2013-10-19 19:35:37 +02:00
|
|
|
/**
|
|
|
|
* A list of channel names this client is subscribed to.
|
|
|
|
*/
|
|
|
|
std::set<std::string> subscriptions;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of subscriptions in #subscriptions. Used to
|
|
|
|
* limit the number of subscriptions.
|
|
|
|
*/
|
2017-08-11 09:21:29 +02:00
|
|
|
unsigned num_subscriptions = 0;
|
2013-10-19 19:35:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A list of messages this client has received.
|
|
|
|
*/
|
|
|
|
std::list<ClientMessage> messages;
|
|
|
|
|
|
|
|
Client(EventLoop &loop, Partition &partition,
|
2018-07-13 18:20:47 +02:00
|
|
|
UniqueSocketDescriptor fd, int uid,
|
|
|
|
unsigned _permission,
|
|
|
|
int num) noexcept;
|
2013-10-19 19:35:37 +02:00
|
|
|
|
2019-04-03 14:44:01 +02:00
|
|
|
~Client() noexcept;
|
2014-01-06 18:02:57 +01:00
|
|
|
|
2019-04-03 14:32:25 +02:00
|
|
|
using FullyBufferedSocket::GetEventLoop;
|
|
|
|
|
2017-12-20 10:38:29 +01:00
|
|
|
bool IsConnected() const noexcept {
|
2013-10-19 19:35:37 +02:00
|
|
|
return FullyBufferedSocket::IsDefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
gcc_pure
|
2017-06-03 21:33:44 +02:00
|
|
|
bool IsExpired() const noexcept {
|
2013-10-19 19:35:37 +02:00
|
|
|
return !FullyBufferedSocket::IsDefined();
|
|
|
|
}
|
|
|
|
|
2017-12-20 10:38:29 +01:00
|
|
|
void Close() noexcept;
|
|
|
|
void SetExpired() noexcept;
|
2013-10-19 19:35:37 +02:00
|
|
|
|
2015-08-06 10:31:53 +02:00
|
|
|
bool Write(const void *data, size_t length);
|
2013-10-19 19:35:37 +02:00
|
|
|
|
2016-04-12 22:32:35 +02:00
|
|
|
/**
|
|
|
|
* Write a null-terminated string.
|
|
|
|
*/
|
|
|
|
bool Write(const char *data);
|
|
|
|
|
2013-10-19 19:39:40 +02:00
|
|
|
/**
|
|
|
|
* returns the uid of the client process, or a negative value
|
|
|
|
* if the uid is unknown
|
|
|
|
*/
|
2017-12-20 10:38:29 +01:00
|
|
|
int GetUID() const noexcept {
|
2013-10-19 19:39:40 +02:00
|
|
|
return uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is this client running on the same machine, connected with
|
|
|
|
* a local (UNIX domain) socket?
|
|
|
|
*/
|
2017-12-20 10:38:29 +01:00
|
|
|
bool IsLocal() const noexcept {
|
2014-11-18 20:56:27 +01:00
|
|
|
return uid >= 0;
|
2013-10-19 19:39:40 +02:00
|
|
|
}
|
|
|
|
|
2017-12-20 10:38:29 +01:00
|
|
|
unsigned GetPermission() const noexcept {
|
2013-10-19 19:39:40 +02:00
|
|
|
return permission;
|
|
|
|
}
|
|
|
|
|
2017-12-20 10:38:29 +01:00
|
|
|
void SetPermission(unsigned _permission) noexcept {
|
2013-10-19 19:39:40 +02:00
|
|
|
permission = _permission;
|
|
|
|
}
|
|
|
|
|
2013-10-19 19:35:37 +02:00
|
|
|
/**
|
|
|
|
* Send "idle" response to this client.
|
|
|
|
*/
|
2017-12-20 10:38:29 +01:00
|
|
|
void IdleNotify() noexcept;
|
|
|
|
void IdleAdd(unsigned flags) noexcept;
|
|
|
|
bool IdleWait(unsigned flags) noexcept;
|
2013-10-19 19:35:37 +02:00
|
|
|
|
2013-10-19 19:44:45 +02:00
|
|
|
enum class SubscribeResult {
|
|
|
|
/** success */
|
|
|
|
OK,
|
|
|
|
|
|
|
|
/** invalid channel name */
|
|
|
|
INVALID,
|
|
|
|
|
|
|
|
/** already subscribed to this channel */
|
|
|
|
ALREADY,
|
|
|
|
|
|
|
|
/** too many subscriptions */
|
|
|
|
FULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
gcc_pure
|
2017-05-08 14:44:49 +02:00
|
|
|
bool IsSubscribed(const char *channel_name) const noexcept {
|
2013-10-19 19:44:45 +02:00
|
|
|
return subscriptions.find(channel_name) != subscriptions.end();
|
|
|
|
}
|
|
|
|
|
2017-12-20 10:38:29 +01:00
|
|
|
SubscribeResult Subscribe(const char *channel) noexcept;
|
|
|
|
bool Unsubscribe(const char *channel) noexcept;
|
|
|
|
void UnsubscribeAll() noexcept;
|
|
|
|
bool PushMessage(const ClientMessage &msg) noexcept;
|
2013-10-19 19:44:45 +02:00
|
|
|
|
2014-02-02 13:59:07 +01:00
|
|
|
/**
|
|
|
|
* Is this client allowed to use the specified local file?
|
|
|
|
*
|
|
|
|
* Note that this function is vulnerable to timing/symlink attacks.
|
|
|
|
* We cannot fix this as long as there are plugins that open a file by
|
|
|
|
* its name, and not by file descriptor / callbacks.
|
|
|
|
*
|
2016-10-27 22:04:38 +02:00
|
|
|
* Throws #std::runtime_error on error.
|
|
|
|
*
|
2014-02-02 13:59:07 +01:00
|
|
|
* @param path_fs the absolute path name in filesystem encoding
|
|
|
|
*/
|
2016-10-27 22:04:38 +02:00
|
|
|
void AllowFile(Path path_fs) const;
|
|
|
|
|
2017-05-15 23:05:45 +02:00
|
|
|
Partition &GetPartition() noexcept {
|
2017-02-25 10:23:23 +01:00
|
|
|
return *partition;
|
2017-02-25 09:58:58 +01:00
|
|
|
}
|
|
|
|
|
2017-05-15 23:05:45 +02:00
|
|
|
void SetPartition(Partition &new_partition) noexcept {
|
2017-02-25 10:25:16 +01:00
|
|
|
partition = &new_partition;
|
|
|
|
|
|
|
|
// TODO: set various idle flags?
|
|
|
|
}
|
|
|
|
|
2017-02-25 10:00:05 +01:00
|
|
|
gcc_pure
|
2017-05-15 23:05:45 +02:00
|
|
|
Instance &GetInstance() noexcept;
|
2017-02-25 10:00:05 +01:00
|
|
|
|
2017-02-20 12:44:09 +01:00
|
|
|
gcc_pure
|
2017-05-15 23:05:45 +02:00
|
|
|
playlist &GetPlaylist() noexcept;
|
2017-02-20 12:44:09 +01:00
|
|
|
|
|
|
|
gcc_pure
|
2017-05-15 23:05:45 +02:00
|
|
|
PlayerControl &GetPlayerControl() noexcept;
|
2017-02-20 12:44:09 +01:00
|
|
|
|
2014-02-01 00:26:34 +01:00
|
|
|
/**
|
|
|
|
* Wrapper for Instance::GetDatabase().
|
|
|
|
*/
|
2014-02-07 00:29:07 +01:00
|
|
|
gcc_pure
|
2017-05-08 14:44:49 +02:00
|
|
|
const Database *GetDatabase() const noexcept;
|
2014-02-01 00:26:34 +01:00
|
|
|
|
2016-10-26 18:47:19 +02:00
|
|
|
/**
|
|
|
|
* Wrapper for Instance::GetDatabaseOrThrow().
|
|
|
|
*/
|
|
|
|
const Database &GetDatabaseOrThrow() const;
|
|
|
|
|
2014-02-07 00:29:07 +01:00
|
|
|
gcc_pure
|
2017-05-08 14:44:49 +02:00
|
|
|
const Storage *GetStorage() const noexcept;
|
2014-02-07 00:29:07 +01:00
|
|
|
|
2013-10-19 19:35:37 +02:00
|
|
|
private:
|
2019-04-03 20:03:17 +02:00
|
|
|
CommandResult ProcessCommandList(bool list_ok,
|
|
|
|
std::list<std::string> &&list) noexcept;
|
|
|
|
|
|
|
|
CommandResult ProcessLine(char *line) noexcept;
|
|
|
|
|
2013-10-19 19:35:37 +02:00
|
|
|
/* virtual methods from class BufferedSocket */
|
2017-12-20 10:42:17 +01:00
|
|
|
InputResult OnSocketInput(void *data, size_t length) noexcept override;
|
|
|
|
void OnSocketError(std::exception_ptr ep) noexcept override;
|
|
|
|
void OnSocketClosed() noexcept override;
|
2013-10-19 19:35:37 +02:00
|
|
|
|
2017-08-29 16:22:01 +02:00
|
|
|
/* callback for TimerEvent */
|
2017-12-20 10:38:29 +01:00
|
|
|
void OnTimeout() noexcept;
|
2013-10-19 19:35:37 +02:00
|
|
|
};
|
2008-08-29 09:36:42 +02:00
|
|
|
|
2015-03-03 20:05:08 +01:00
|
|
|
void
|
2018-07-17 23:07:50 +02:00
|
|
|
client_manager_init(const ConfigData &config);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-01-04 23:07:11 +01:00
|
|
|
void
|
2013-01-14 23:42:06 +01:00
|
|
|
client_new(EventLoop &loop, Partition &partition,
|
2018-07-13 18:20:47 +02:00
|
|
|
UniqueSocketDescriptor fd, SocketAddress address, int uid,
|
|
|
|
unsigned permission) noexcept;
|
2008-08-29 09:36:40 +02:00
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
#endif
|