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 35 36
#include <set>
#include <string>
#include <list>
37
#include <memory>
38

39 40
#include <stddef.h>

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

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

60
	Partition *partition;
61 62 63 64

	unsigned permission;

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

	CommandListBuilder cmd_list;

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

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

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

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

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

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

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

102 103
	static constexpr size_t MAX_MESSAGES = 64;

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

109 110 111 112 113 114 115 116
	/**
	 * 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;

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

123
	~Client() noexcept;
124

125 126
	using FullyBufferedSocket::GetEventLoop;

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

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

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

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

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

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

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

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

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

173 174 175 176 177 178 179 180 181 182 183 184 185
	/**
	 * 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;

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

		/** invalid channel name */
		INVALID,

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

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

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

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

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

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

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

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

239
	void SetPartition(Partition &new_partition) noexcept;
240

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

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

	gcc_pure
248
	PlayerControl &GetPlayerControl() const noexcept;
249

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

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

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

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

	CommandResult ProcessLine(char *line) noexcept;

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

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

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

Warren Dukes's avatar
Warren Dukes committed
284
#endif