Client.hxx 5.54 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 24
#include "check.h"
#include "ClientMessage.hxx"
25
#include "command/CommandListBuilder.hxx"
26
#include "tag/Mask.hxx"
27
#include "event/FullyBufferedSocket.hxx"
28
#include "event/TimerEvent.hxx"
29
#include "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 47
struct PlayerControl;
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
public:
59 60 61
	unsigned permission;

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

	CommandListBuilder cmd_list;

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

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

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

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

78 79 80 81 82
	/**
	 * The tags this client is interested in.
	 */
	TagMask tag_mask = TagMask::All();

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

	/**
	 * A list of messages this client has received.
	 */
	std::list<ClientMessage> messages;

	Client(EventLoop &loop, Partition &partition,
100
	       UniqueSocketDescriptor &&fd, int uid, int num);
101

102 103 104 105 106
	~Client() {
		if (FullyBufferedSocket::IsDefined())
			FullyBufferedSocket::Close();
	}

107 108 109 110 111
	bool IsConnected() const {
		return FullyBufferedSocket::IsDefined();
	}

	gcc_pure
112
	bool IsExpired() const noexcept {
113 114 115 116 117 118
		return !FullyBufferedSocket::IsDefined();
	}

	void Close();
	void SetExpired();

119
	bool Write(const void *data, size_t length);
120

121 122 123 124 125
	/**
	 * Write a null-terminated string.
	 */
	bool Write(const char *data);

126 127 128 129 130 131 132 133 134 135 136 137 138
	/**
	 * returns the uid of the client process, or a negative value
	 * if the uid is unknown
	 */
	int GetUID() const {
		return uid;
	}

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

	unsigned GetPermission() const {
		return permission;
	}

	void SetPermission(unsigned _permission) {
		permission = _permission;
	}

150 151 152 153 154 155 156
	/**
	 * Send "idle" response to this client.
	 */
	void IdleNotify();
	void IdleAdd(unsigned flags);
	bool IdleWait(unsigned flags);

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	enum class SubscribeResult {
		/** success */
		OK,

		/** invalid channel name */
		INVALID,

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

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

	gcc_pure
172
	bool IsSubscribed(const char *channel_name) const noexcept {
173 174 175 176 177 178 179 180
		return subscriptions.find(channel_name) != subscriptions.end();
	}

	SubscribeResult Subscribe(const char *channel);
	bool Unsubscribe(const char *channel);
	void UnsubscribeAll();
	bool PushMessage(const ClientMessage &msg);

181 182 183 184 185 186 187
	/**
	 * 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.
	 *
188 189
	 * Throws #std::runtime_error on error.
	 *
190 191
	 * @param path_fs the absolute path name in filesystem encoding
	 */
192 193
	void AllowFile(Path path_fs) const;

194
	Partition &GetPartition() noexcept {
195
		return *partition;
196 197
	}

198
	void SetPartition(Partition &new_partition) noexcept {
199 200 201 202 203
		partition = &new_partition;

		// TODO: set various idle flags?
	}

204
	gcc_pure
205
	Instance &GetInstance() noexcept;
206

207
	gcc_pure
208
	playlist &GetPlaylist() noexcept;
209 210

	gcc_pure
211
	PlayerControl &GetPlayerControl() noexcept;
212

213 214 215
	/**
	 * Wrapper for Instance::GetDatabase().
	 */
216
	gcc_pure
217
	const Database *GetDatabase() const noexcept;
218

219 220 221 222 223
	/**
	 * Wrapper for Instance::GetDatabaseOrThrow().
	 */
	const Database &GetDatabaseOrThrow() const;

224
	gcc_pure
225
	const Storage *GetStorage() const noexcept;
226

227 228
private:
	/* virtual methods from class BufferedSocket */
229
	InputResult OnSocketInput(void *data, size_t length) override;
230
	void OnSocketError(std::exception_ptr ep) override;
231
	void OnSocketClosed() override;
232

233 234
	/* callback for TimerEvent */
	void OnTimeout();
235
};
236

237 238
void
client_manager_init();
Warren Dukes's avatar
Warren Dukes committed
239

240
void
241
client_new(EventLoop &loop, Partition &partition,
242
	   UniqueSocketDescriptor &&fd, SocketAddress address, int uid);
243

244 245 246
/**
 * Write a printf-like formatted string to the client.
 */
Max Kellermann's avatar
Max Kellermann committed
247
gcc_printf(2,3)
248
void
249
client_printf(Client &client, const char *fmt, ...);
250

Warren Dukes's avatar
Warren Dukes committed
251
#endif