Client.hxx 5.68 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2018 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 "ClientMessage.hxx"
24
#include "command/CommandListBuilder.hxx"
25
#include "tag/Mask.hxx"
26
#include "event/FullyBufferedSocket.hxx"
27
#include "event/TimerEvent.hxx"
28
#include "util/Compiler.h"
29

30 31
#include <boost/intrusive/link_mode.hpp>
#include <boost/intrusive/list_hook.hpp>
32

33 34 35 36
#include <set>
#include <string>
#include <list>

37 38
#include <stddef.h>

39
struct ConfigData;
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
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 101 102
	       UniqueSocketDescriptor fd, int uid,
	       unsigned _permission,
	       int num) noexcept;
103

104
	~Client() noexcept {
105 106 107 108
		if (FullyBufferedSocket::IsDefined())
			FullyBufferedSocket::Close();
	}

109
	bool IsConnected() const noexcept {
110 111 112 113
		return FullyBufferedSocket::IsDefined();
	}

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

118 119
	void Close() noexcept;
	void SetExpired() noexcept;
120

121
	bool Write(const void *data, size_t length);
122

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

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

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

144
	unsigned GetPermission() const noexcept {
145 146 147
		return permission;
	}

148
	void SetPermission(unsigned _permission) noexcept {
149 150 151
		permission = _permission;
	}

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

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

		/** invalid channel name */
		INVALID,

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

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

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

178 179 180 181
	SubscribeResult Subscribe(const char *channel) noexcept;
	bool Unsubscribe(const char *channel) noexcept;
	void UnsubscribeAll() noexcept;
	bool PushMessage(const ClientMessage &msg) noexcept;
182

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

196
	Partition &GetPartition() noexcept {
197
		return *partition;
198 199
	}

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

		// TODO: set various idle flags?
	}

206
	gcc_pure
207
	Instance &GetInstance() noexcept;
208

209
	gcc_pure
210
	playlist &GetPlaylist() noexcept;
211 212

	gcc_pure
213
	PlayerControl &GetPlayerControl() noexcept;
214

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

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

226
	gcc_pure
227
	const Storage *GetStorage() const noexcept;
228

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

235
	/* callback for TimerEvent */
236
	void OnTimeout() noexcept;
237
};
238

239
void
240
client_manager_init(const ConfigData &config);
Warren Dukes's avatar
Warren Dukes committed
241

242
void
243
client_new(EventLoop &loop, Partition &partition,
244 245
	   UniqueSocketDescriptor fd, SocketAddress address, int uid,
	   unsigned permission) noexcept;
246

Warren Dukes's avatar
Warren Dukes committed
247
#endif