Client.hxx 7.04 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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/CoarseTimerEvent.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
	CoarseTimerEvent timeout_event;
58

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 88 89 90 91 92
	/**
	 * The maximum number of bytes transmitted in a binary
	 * response.  Can be changed with the "binarylimit" command.
	 */
	size_t binary_limit = 8192;

93
private:
94 95
	static constexpr size_t MAX_SUBSCRIPTIONS = 16;

96 97 98 99 100 101 102 103 104
	/**
	 * 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.
	 */
105
	unsigned num_subscriptions = 0;
106

107 108
	static constexpr size_t MAX_MESSAGES = 64;

109 110 111 112 113
	/**
	 * A list of messages this client has received.
	 */
	std::list<ClientMessage> messages;

114 115 116 117 118 119 120 121
	/**
	 * 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;

122
public:
123
	Client(EventLoop &loop, Partition &partition,
124 125 126
	       UniqueSocketDescriptor fd, int uid,
	       unsigned _permission,
	       int num) noexcept;
127

128
	~Client() noexcept;
129

130
	using FullyBufferedSocket::GetEventLoop;
131
	using FullyBufferedSocket::GetOutputMaxSize;
132

133 134 135 136 137
	gcc_pure
	bool IsExpired() const noexcept {
		return !FullyBufferedSocket::IsDefined();
	}

138
	void Close() noexcept;
139
	void SetExpired() noexcept;
140

141
	bool Write(const void *data, size_t length) noexcept;
142

143 144 145
	/**
	 * Write a null-terminated string.
	 */
146
	bool Write(const char *data) noexcept;
147

148 149 150 151
	/**
	 * returns the uid of the client process, or a negative value
	 * if the uid is unknown
	 */
152
	int GetUID() const noexcept {
153 154 155 156 157 158 159
		return uid;
	}

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

164
	unsigned GetPermission() const noexcept {
165 166 167
		return permission;
	}

168
	void SetPermission(unsigned _permission) noexcept {
169 170 171
		permission = _permission;
	}

172 173 174
	/**
	 * Send "idle" response to this client.
	 */
175 176 177
	void IdleNotify() noexcept;
	void IdleAdd(unsigned flags) noexcept;
	bool IdleWait(unsigned flags) noexcept;
178

179 180 181 182 183 184 185 186 187 188 189 190 191
	/**
	 * 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;

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
	enum class SubscribeResult {
		/** success */
		OK,

		/** invalid channel name */
		INVALID,

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

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

	gcc_pure
207
	bool IsSubscribed(const char *channel_name) const noexcept {
208 209 210
		return subscriptions.find(channel_name) != subscriptions.end();
	}

211 212 213 214
	const auto &GetSubscriptions() const noexcept {
		return subscriptions;
	}

215 216 217 218
	SubscribeResult Subscribe(const char *channel) noexcept;
	bool Unsubscribe(const char *channel) noexcept;
	void UnsubscribeAll() noexcept;
	bool PushMessage(const ClientMessage &msg) noexcept;
219

220 221 222 223 224 225 226 227
	template<typename F>
	void ConsumeMessages(F &&f) {
		while (!messages.empty()) {
			f(messages.front());
			messages.pop_front();
		}
	}

228 229 230 231 232 233 234
	/**
	 * 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.
	 *
235 236
	 * Throws #std::runtime_error on error.
	 *
237 238
	 * @param path_fs the absolute path name in filesystem encoding
	 */
239 240
	void AllowFile(Path path_fs) const;

241
	Partition &GetPartition() const noexcept {
242
		return *partition;
243 244
	}

245
	void SetPartition(Partition &new_partition) noexcept;
246

247
	gcc_pure
248
	Instance &GetInstance() const noexcept;
249

250
	gcc_pure
251
	playlist &GetPlaylist() const noexcept;
252 253

	gcc_pure
254
	PlayerControl &GetPlayerControl() const noexcept;
255

256 257 258
	/**
	 * Wrapper for Instance::GetDatabase().
	 */
259
	gcc_pure
260
	const Database *GetDatabase() const noexcept;
261

262 263 264 265 266
	/**
	 * Wrapper for Instance::GetDatabaseOrThrow().
	 */
	const Database &GetDatabaseOrThrow() const;

267
	gcc_pure
268
	const Storage *GetStorage() const noexcept;
269

270
private:
271 272 273 274 275
	CommandResult ProcessCommandList(bool list_ok,
					 std::list<std::string> &&list) noexcept;

	CommandResult ProcessLine(char *line) noexcept;

276
	/* virtual methods from class BufferedSocket */
277 278 279
	InputResult OnSocketInput(void *data, size_t length) noexcept override;
	void OnSocketError(std::exception_ptr ep) noexcept override;
	void OnSocketClosed() noexcept override;
280

281
	/* callback for TimerEvent */
282
	void OnTimeout() noexcept;
283
};
284

285
void
286
client_new(EventLoop &loop, Partition &partition,
287 288
	   UniqueSocketDescriptor fd, SocketAddress address, int uid,
	   unsigned permission) noexcept;
289

Warren Dukes's avatar
Warren Dukes committed
290
#endif