Client.hxx 6.13 KB
Newer Older
1
/*
2
 * Copyright 2003-2019 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 37
#include <set>
#include <string>
#include <list>

38 39
#include <stddef.h>

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

51
class Client final
52
	: FullyBufferedSocket,
53
	  public boost::intrusive::list_base_hook<boost::intrusive::link_mode<boost::intrusive::normal_link>> {
54 55
	TimerEvent timeout_event;

56
	Partition *partition;
57 58 59 60

	unsigned permission;

	/** the uid of the client process, or -1 if unknown */
61
	const int uid;
62 63 64

	CommandListBuilder cmd_list;

65
	const unsigned int num;	/* client number */
66 67

	/** is this client waiting for an "idle" response? */
68
	bool idle_waiting = false;
69 70 71

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

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

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

84
private:
85 86
	static constexpr size_t MAX_SUBSCRIPTIONS = 16;

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

98 99
	static constexpr size_t MAX_MESSAGES = 64;

100 101 102 103 104
	/**
	 * A list of messages this client has received.
	 */
	std::list<ClientMessage> messages;

105
public:
106
	Client(EventLoop &loop, Partition &partition,
107 108 109
	       UniqueSocketDescriptor fd, int uid,
	       unsigned _permission,
	       int num) noexcept;
110

111
	~Client() noexcept;
112

113 114
	using FullyBufferedSocket::GetEventLoop;

115
	bool IsConnected() const noexcept {
116 117 118 119
		return FullyBufferedSocket::IsDefined();
	}

	gcc_pure
120
	bool IsExpired() const noexcept {
121 122 123
		return !FullyBufferedSocket::IsDefined();
	}

124 125
	void Close() noexcept;
	void SetExpired() noexcept;
126

127
	bool Write(const void *data, size_t length) noexcept;
128

129 130 131
	/**
	 * Write a null-terminated string.
	 */
132
	bool Write(const char *data) noexcept;
133

134 135 136 137
	/**
	 * returns the uid of the client process, or a negative value
	 * if the uid is unknown
	 */
138
	int GetUID() const noexcept {
139 140 141 142 143 144 145
		return uid;
	}

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

150
	unsigned GetPermission() const noexcept {
151 152 153
		return permission;
	}

154
	void SetPermission(unsigned _permission) noexcept {
155 156 157
		permission = _permission;
	}

158 159 160
	/**
	 * Send "idle" response to this client.
	 */
161 162 163
	void IdleNotify() noexcept;
	void IdleAdd(unsigned flags) noexcept;
	bool IdleWait(unsigned flags) noexcept;
164

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	enum class SubscribeResult {
		/** success */
		OK,

		/** invalid channel name */
		INVALID,

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

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

	gcc_pure
180
	bool IsSubscribed(const char *channel_name) const noexcept {
181 182 183
		return subscriptions.find(channel_name) != subscriptions.end();
	}

184 185 186 187
	const auto &GetSubscriptions() const noexcept {
		return subscriptions;
	}

188 189 190 191
	SubscribeResult Subscribe(const char *channel) noexcept;
	bool Unsubscribe(const char *channel) noexcept;
	void UnsubscribeAll() noexcept;
	bool PushMessage(const ClientMessage &msg) noexcept;
192

193 194 195 196 197 198 199 200
	template<typename F>
	void ConsumeMessages(F &&f) {
		while (!messages.empty()) {
			f(messages.front());
			messages.pop_front();
		}
	}

201 202 203 204 205 206 207
	/**
	 * 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.
	 *
208 209
	 * Throws #std::runtime_error on error.
	 *
210 211
	 * @param path_fs the absolute path name in filesystem encoding
	 */
212 213
	void AllowFile(Path path_fs) const;

214
	Partition &GetPartition() noexcept {
215
		return *partition;
216 217
	}

218
	void SetPartition(Partition &new_partition) noexcept {
219 220 221 222 223
		partition = &new_partition;

		// TODO: set various idle flags?
	}

224
	gcc_pure
225
	Instance &GetInstance() noexcept;
226

227
	gcc_pure
228
	playlist &GetPlaylist() noexcept;
229 230

	gcc_pure
231
	PlayerControl &GetPlayerControl() noexcept;
232

233 234 235
	/**
	 * Wrapper for Instance::GetDatabase().
	 */
236
	gcc_pure
237
	const Database *GetDatabase() const noexcept;
238

239 240 241 242 243
	/**
	 * Wrapper for Instance::GetDatabaseOrThrow().
	 */
	const Database &GetDatabaseOrThrow() const;

244
	gcc_pure
245
	const Storage *GetStorage() const noexcept;
246

247
private:
248 249 250 251 252
	CommandResult ProcessCommandList(bool list_ok,
					 std::list<std::string> &&list) noexcept;

	CommandResult ProcessLine(char *line) noexcept;

253
	/* virtual methods from class BufferedSocket */
254 255 256
	InputResult OnSocketInput(void *data, size_t length) noexcept override;
	void OnSocketError(std::exception_ptr ep) noexcept override;
	void OnSocketClosed() noexcept override;
257

258
	/* callback for TimerEvent */
259
	void OnTimeout() noexcept;
260
};
261

262
void
263
client_new(EventLoop &loop, Partition &partition,
264 265
	   UniqueSocketDescriptor fd, SocketAddress address, int uid,
	   unsigned permission) noexcept;
266

Warren Dukes's avatar
Warren Dukes committed
267
#endif