Client.hxx 5.27 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 27
#include "event/FullyBufferedSocket.hxx"
#include "event/TimeoutMonitor.hxx"
28
#include "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 39
#include <stddef.h>
#include <stdarg.h>

40
class SocketAddress;
41
class EventLoop;
42
class Path;
43
struct Partition;
44
class Database;
45
class Storage;
46

47 48 49
class Client final
	: FullyBufferedSocket, TimeoutMonitor,
	  public boost::intrusive::list_base_hook<boost::intrusive::link_mode<boost::intrusive::normal_link>> {
50 51 52
public:
	Partition &partition;
	struct playlist &playlist;
53
	struct PlayerControl &player_control;
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

	unsigned permission;

	/** the uid of the client process, or -1 if unknown */
	int uid;

	CommandListBuilder cmd_list;

	unsigned int num;	/* client number */

	/** is this client waiting for an "idle" response? */
	bool idle_waiting;

	/** idle flags pending on this client, to be sent as soon as
	    the client enters "idle" */
	unsigned idle_flags;

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

	/**
	 * 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.
	 */
	unsigned num_subscriptions;

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

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

93 94 95 96 97
	~Client() {
		if (FullyBufferedSocket::IsDefined())
			FullyBufferedSocket::Close();
	}

98 99 100 101 102 103 104 105 106 107 108 109
	bool IsConnected() const {
		return FullyBufferedSocket::IsDefined();
	}

	gcc_pure
	bool IsExpired() const {
		return !FullyBufferedSocket::IsDefined();
	}

	void Close();
	void SetExpired();

110
	bool Write(const void *data, size_t length);
111

112 113 114 115 116
	/**
	 * Write a null-terminated string.
	 */
	bool Write(const char *data);

117 118 119 120 121 122 123 124 125 126 127 128 129
	/**
	 * 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 {
130
		return uid >= 0;
131 132 133 134 135 136 137 138 139 140
	}

	unsigned GetPermission() const {
		return permission;
	}

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

141 142 143 144 145 146 147
	/**
	 * Send "idle" response to this client.
	 */
	void IdleNotify();
	void IdleAdd(unsigned flags);
	bool IdleWait(unsigned flags);

148 149 150 151 152 153 154 155 156 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
	bool IsSubscribed(const char *channel_name) const {
		return subscriptions.find(channel_name) != subscriptions.end();
	}

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

172 173 174 175 176 177 178
	/**
	 * 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.
	 *
179 180
	 * Throws #std::runtime_error on error.
	 *
181 182
	 * @param path_fs the absolute path name in filesystem encoding
	 */
183 184
	void AllowFile(Path path_fs) const;

185 186 187
	/**
	 * Wrapper for Instance::GetDatabase().
	 */
188
	gcc_pure
189
	const Database *GetDatabase() const;
190

191 192 193 194 195 196
	/**
	 * Wrapper for Instance::GetDatabaseOrThrow().
	 */
	gcc_pure
	const Database &GetDatabaseOrThrow() const;

197 198 199
	gcc_pure
	const Storage *GetStorage() const;

200 201 202
private:
	/* virtual methods from class BufferedSocket */
	virtual InputResult OnSocketInput(void *data, size_t length) override;
203
	void OnSocketError(std::exception_ptr ep) override;
204 205 206 207 208
	virtual void OnSocketClosed() override;

	/* virtual methods from class TimeoutMonitor */
	virtual void OnTimeout() override;
};
209

210 211
void
client_manager_init();
Warren Dukes's avatar
Warren Dukes committed
212

213
void
214
client_new(EventLoop &loop, Partition &partition,
215
	   int fd, SocketAddress address, int uid);
216

217 218 219
/**
 * Write a C string to the client.
 */
220
void client_puts(Client &client, const char *s);
221

222 223 224
/**
 * Write a printf-like formatted string to the client.
 */
225
void client_vprintf(Client &client, const char *fmt, va_list args);
226 227 228 229

/**
 * Write a printf-like formatted string to the client.
 */
Max Kellermann's avatar
Max Kellermann committed
230
gcc_printf(2,3)
231
void
232
client_printf(Client &client, const char *fmt, ...);
233

Warren Dukes's avatar
Warren Dukes committed
234
#endif