Client.hxx 6.83 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

20 21
#ifndef MPD_CLIENT_H
#define MPD_CLIENT_H
Warren Dukes's avatar
Warren Dukes committed
22

23
#include "Message.hxx"
24
#include "command/CommandResult.hxx"
25
#include "command/CommandListBuilder.hxx"
26
#include "tag/Mask.hxx"
27
#include "event/FullyBufferedSocket.hxx"
28
#include "event/TimerEvent.hxx"
29
#include "util/Compiler.h"
30

31 32
#include <boost/intrusive/link_mode.hpp>
#include <boost/intrusive/list_hook.hpp>
33

34
#include <cstddef>
35
#include <list>
36
#include <memory>
37 38
#include <set>
#include <string>
39

40
class SocketAddress;
41
class UniqueSocketDescriptor;
42
class EventLoop;
43
class Path;
44
struct Instance;
45
struct Partition;
46
class PlayerControl;
47
struct playlist;
48
class Database;
49
class Storage;
50
class BackgroundCommand;
51

52
class Client final
53
	: FullyBufferedSocket,
54 55
	  public boost::intrusive::list_base_hook<boost::intrusive::tag<Partition>,
						  boost::intrusive::link_mode<boost::intrusive::normal_link>>,
56
	  public boost::intrusive::list_base_hook<boost::intrusive::link_mode<boost::intrusive::normal_link>> {
57 58
	TimerEvent timeout_event;

59
	Partition *partition;
60 61 62 63

	unsigned permission;

	/** the uid of the client process, or -1 if unknown */
64
	const int uid;
65 66 67

	CommandListBuilder cmd_list;

68
	const unsigned int num;	/* client number */
69 70

	/** is this client waiting for an "idle" response? */
71
	bool idle_waiting = false;
72 73 74

	/** idle flags pending on this client, to be sent as soon as
	    the client enters "idle" */
75
	unsigned idle_flags = 0;
76 77 78 79

	/** idle flags that the client wants to receive */
	unsigned idle_subscriptions;

80 81
public:
	// TODO: make this attribute "private"
82 83 84 85 86
	/**
	 * The tags this client is interested in.
	 */
	TagMask tag_mask = TagMask::All();

87
private:
88 89
	static constexpr size_t MAX_SUBSCRIPTIONS = 16;

90 91 92 93 94 95 96 97 98
	/**
	 * 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.
	 */
99
	unsigned num_subscriptions = 0;
100

101 102
	static constexpr size_t MAX_MESSAGES = 64;

103 104 105 106 107
	/**
	 * A list of messages this client has received.
	 */
	std::list<ClientMessage> messages;

108 109 110 111 112 113 114 115
	/**
	 * The command currently running in background.  If this is
	 * set, then the client is occupied and will not process any
	 * new input.  If the connection gets closed, the
	 * #BackgroundCommand will be cancelled.
	 */
	std::unique_ptr<BackgroundCommand> background_command;

116
public:
117
	Client(EventLoop &loop, Partition &partition,
118 119 120
	       UniqueSocketDescriptor fd, int uid,
	       unsigned _permission,
	       int num) noexcept;
121

122
	~Client() noexcept;
123

124 125
	using FullyBufferedSocket::GetEventLoop;

126 127 128 129 130
	gcc_pure
	bool IsExpired() const noexcept {
		return !FullyBufferedSocket::IsDefined();
	}

131
	void Close() noexcept;
132
	void SetExpired() noexcept;
133

134
	bool Write(const void *data, size_t length) noexcept;
135

136 137 138
	/**
	 * Write a null-terminated string.
	 */
139
	bool Write(const char *data) noexcept;
140

141 142 143 144
	/**
	 * returns the uid of the client process, or a negative value
	 * if the uid is unknown
	 */
145
	int GetUID() const noexcept {
146 147 148 149 150 151 152
		return uid;
	}

	/**
	 * Is this client running on the same machine, connected with
	 * a local (UNIX domain) socket?
	 */
153
	bool IsLocal() const noexcept {
154
		return uid >= 0;
155 156
	}

157
	unsigned GetPermission() const noexcept {
158 159 160
		return permission;
	}

161
	void SetPermission(unsigned _permission) noexcept {
162 163 164
		permission = _permission;
	}

165 166 167
	/**
	 * Send "idle" response to this client.
	 */
168 169 170
	void IdleNotify() noexcept;
	void IdleAdd(unsigned flags) noexcept;
	bool IdleWait(unsigned flags) noexcept;
171

172 173 174 175 176 177 178 179 180 181 182 183 184
	/**
	 * Called by a command handler to defer execution to a
	 * #BackgroundCommand.
	 */
	void SetBackgroundCommand(std::unique_ptr<BackgroundCommand> _bc) noexcept;

	/**
	 * Called by the current #BackgroundCommand when it has
	 * finished, after sending the response.  This method then
	 * deletes the #BackgroundCommand.
	 */
	void OnBackgroundCommandFinished() noexcept;

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
	enum class SubscribeResult {
		/** success */
		OK,

		/** invalid channel name */
		INVALID,

		/** already subscribed to this channel */
		ALREADY,

		/** too many subscriptions */
		FULL,
	};

	gcc_pure
200
	bool IsSubscribed(const char *channel_name) const noexcept {
201 202 203
		return subscriptions.find(channel_name) != subscriptions.end();
	}

204 205 206 207
	const auto &GetSubscriptions() const noexcept {
		return subscriptions;
	}

208 209 210 211
	SubscribeResult Subscribe(const char *channel) noexcept;
	bool Unsubscribe(const char *channel) noexcept;
	void UnsubscribeAll() noexcept;
	bool PushMessage(const ClientMessage &msg) noexcept;
212

213 214 215 216 217 218 219 220
	template<typename F>
	void ConsumeMessages(F &&f) {
		while (!messages.empty()) {
			f(messages.front());
			messages.pop_front();
		}
	}

221 222 223 224 225 226 227
	/**
	 * 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.
	 *
228 229
	 * Throws #std::runtime_error on error.
	 *
230 231
	 * @param path_fs the absolute path name in filesystem encoding
	 */
232 233
	void AllowFile(Path path_fs) const;

234
	Partition &GetPartition() const noexcept {
235
		return *partition;
236 237
	}

238
	void SetPartition(Partition &new_partition) noexcept;
239

240
	gcc_pure
241
	Instance &GetInstance() const noexcept;
242

243
	gcc_pure
244
	playlist &GetPlaylist() const noexcept;
245 246

	gcc_pure
247
	PlayerControl &GetPlayerControl() const noexcept;
248

249 250 251
	/**
	 * Wrapper for Instance::GetDatabase().
	 */
252
	gcc_pure
253
	const Database *GetDatabase() const noexcept;
254

255 256 257 258 259
	/**
	 * Wrapper for Instance::GetDatabaseOrThrow().
	 */
	const Database &GetDatabaseOrThrow() const;

260
	gcc_pure
261
	const Storage *GetStorage() const noexcept;
262

263
private:
264 265 266 267 268
	CommandResult ProcessCommandList(bool list_ok,
					 std::list<std::string> &&list) noexcept;

	CommandResult ProcessLine(char *line) noexcept;

269
	/* virtual methods from class BufferedSocket */
270 271 272
	InputResult OnSocketInput(void *data, size_t length) noexcept override;
	void OnSocketError(std::exception_ptr ep) noexcept override;
	void OnSocketClosed() noexcept override;
273

274
	/* callback for TimerEvent */
275
	void OnTimeout() noexcept;
276
};
277

278
void
279
client_new(EventLoop &loop, Partition &partition,
280 281
	   UniqueSocketDescriptor fd, SocketAddress address, int uid,
	   unsigned permission) noexcept;
282

Warren Dukes's avatar
Warren Dukes committed
283
#endif