Client.hxx 7.35 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 "input/LastInputStream.hxx"
27
#include "tag/Mask.hxx"
28
#include "event/FullyBufferedSocket.hxx"
29
#include "event/CoarseTimerEvent.hxx"
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 94 95 96 97 98 99
	/**
	 * This caches the last "albumart" InputStream instance, to
	 * avoid repeating the search for each chunk requested by this
	 * client.
	 */
	LastInputStream last_album_art;

100
private:
101 102
	static constexpr size_t MAX_SUBSCRIPTIONS = 16;

103 104 105 106 107 108 109 110 111
	/**
	 * 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.
	 */
112
	unsigned num_subscriptions = 0;
113

114 115
	static constexpr size_t MAX_MESSAGES = 64;

116 117 118 119 120
	/**
	 * A list of messages this client has received.
	 */
	std::list<ClientMessage> messages;

121 122 123 124 125 126 127 128
	/**
	 * 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;

129
public:
130
	Client(EventLoop &loop, Partition &partition,
131 132 133
	       UniqueSocketDescriptor fd, int uid,
	       unsigned _permission,
	       int num) noexcept;
134

135
	~Client() noexcept;
136

137
	using FullyBufferedSocket::GetEventLoop;
138
	using FullyBufferedSocket::GetOutputMaxSize;
139

140
	[[gnu::pure]]
141 142 143 144
	bool IsExpired() const noexcept {
		return !FullyBufferedSocket::IsDefined();
	}

145
	void Close() noexcept;
146
	void SetExpired() noexcept;
147

148
	bool Write(const void *data, size_t length) noexcept;
149

150 151 152
	/**
	 * Write a null-terminated string.
	 */
153 154 155
	bool Write(std::string_view s) noexcept {
		return Write(s.data(), s.size());
	}
156

157 158 159 160
	bool WriteOK() noexcept {
		return Write("OK\n");
	}

161 162 163 164
	/**
	 * returns the uid of the client process, or a negative value
	 * if the uid is unknown
	 */
165
	int GetUID() const noexcept {
166 167 168 169 170 171 172
		return uid;
	}

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

177
	unsigned GetPermission() const noexcept {
178 179 180
		return permission;
	}

181
	void SetPermission(unsigned _permission) noexcept {
182 183 184
		permission = _permission;
	}

185 186 187
	/**
	 * Send "idle" response to this client.
	 */
188 189 190
	void IdleNotify() noexcept;
	void IdleAdd(unsigned flags) noexcept;
	bool IdleWait(unsigned flags) noexcept;
191

192 193 194 195 196 197 198 199 200 201 202 203 204
	/**
	 * 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;

205 206 207 208 209 210 211 212 213 214 215 216 217 218
	enum class SubscribeResult {
		/** success */
		OK,

		/** invalid channel name */
		INVALID,

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

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

219
	[[gnu::pure]]
220
	bool IsSubscribed(const char *channel_name) const noexcept {
221 222 223
		return subscriptions.find(channel_name) != subscriptions.end();
	}

224 225 226 227
	const auto &GetSubscriptions() const noexcept {
		return subscriptions;
	}

228 229 230 231
	SubscribeResult Subscribe(const char *channel) noexcept;
	bool Unsubscribe(const char *channel) noexcept;
	void UnsubscribeAll() noexcept;
	bool PushMessage(const ClientMessage &msg) noexcept;
232

233 234 235 236 237 238 239 240
	template<typename F>
	void ConsumeMessages(F &&f) {
		while (!messages.empty()) {
			f(messages.front());
			messages.pop_front();
		}
	}

241 242 243 244 245 246 247
	/**
	 * 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.
	 *
248 249
	 * Throws #std::runtime_error on error.
	 *
250 251
	 * @param path_fs the absolute path name in filesystem encoding
	 */
252 253
	void AllowFile(Path path_fs) const;

254
	Partition &GetPartition() const noexcept {
255
		return *partition;
256 257
	}

258
	void SetPartition(Partition &new_partition) noexcept;
259

260
	[[gnu::pure]]
261
	Instance &GetInstance() const noexcept;
262

263
	[[gnu::pure]]
264
	playlist &GetPlaylist() const noexcept;
265

266
	[[gnu::pure]]
267
	PlayerControl &GetPlayerControl() const noexcept;
268

269 270 271
	/**
	 * Wrapper for Instance::GetDatabase().
	 */
272
	[[gnu::pure]]
273
	const Database *GetDatabase() const noexcept;
274

275 276 277 278 279
	/**
	 * Wrapper for Instance::GetDatabaseOrThrow().
	 */
	const Database &GetDatabaseOrThrow() const;

280
	[[gnu::pure]]
281
	const Storage *GetStorage() const noexcept;
282

283
private:
284 285 286 287 288
	CommandResult ProcessCommandList(bool list_ok,
					 std::list<std::string> &&list) noexcept;

	CommandResult ProcessLine(char *line) noexcept;

289
	/* virtual methods from class BufferedSocket */
290 291 292
	InputResult OnSocketInput(void *data, size_t length) noexcept override;
	void OnSocketError(std::exception_ptr ep) noexcept override;
	void OnSocketClosed() noexcept override;
293

294
	/* callback for TimerEvent */
295
	void OnTimeout() noexcept;
296
};
297

298
void
299
client_new(EventLoop &loop, Partition &partition,
300 301
	   UniqueSocketDescriptor fd, SocketAddress address, int uid,
	   unsigned permission) noexcept;
302

Warren Dukes's avatar
Warren Dukes committed
303
#endif