Client.hxx 5.7 KB
Newer Older
1
/*
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 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 "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
struct ConfigData;
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

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

57
	Partition *partition;
58

59
public:
60 61 62
	unsigned permission;

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

	CommandListBuilder cmd_list;

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

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

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

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

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

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

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

	Client(EventLoop &loop, Partition &partition,
101 102 103
	       UniqueSocketDescriptor fd, int uid,
	       unsigned _permission,
	       int num) noexcept;
104

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

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

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

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

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

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

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

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

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

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

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

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

		/** invalid channel name */
		INVALID,

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

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

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

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

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

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

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

		// TODO: set various idle flags?
	}

207
	gcc_pure
208
	Instance &GetInstance() noexcept;
209

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

	gcc_pure
214
	PlayerControl &GetPlayerControl() noexcept;
215

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

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

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

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

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

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

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

Warren Dukes's avatar
Warren Dukes committed
248
#endif