2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
|
|
|
|
2008-10-31 09:19:53 +01:00
|
|
|
#ifndef MPD_CLIENT_H
|
|
|
|
#define MPD_CLIENT_H
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-10-19 19:35:37 +02:00
|
|
|
#include "check.h"
|
|
|
|
#include "ClientMessage.hxx"
|
2013-10-20 13:41:04 +02:00
|
|
|
#include "command/CommandListBuilder.hxx"
|
2013-10-19 19:35:37 +02:00
|
|
|
#include "event/FullyBufferedSocket.hxx"
|
|
|
|
#include "event/TimeoutMonitor.hxx"
|
2013-10-15 09:21:13 +02:00
|
|
|
#include "Compiler.h"
|
2012-08-02 18:15:49 +02:00
|
|
|
|
2014-06-10 18:57:30 +02:00
|
|
|
#include <boost/intrusive/list.hpp>
|
|
|
|
|
2013-10-19 19:35:37 +02:00
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <list>
|
|
|
|
|
2008-10-08 10:49:29 +02:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2008-12-29 17:42:43 +01:00
|
|
|
struct sockaddr;
|
2013-01-14 23:42:06 +01:00
|
|
|
class EventLoop;
|
2014-02-02 13:59:07 +01:00
|
|
|
class Path;
|
2013-01-07 10:59:56 +01:00
|
|
|
struct Partition;
|
2014-02-01 00:26:34 +01:00
|
|
|
class Database;
|
2014-02-07 00:29:07 +01:00
|
|
|
class Storage;
|
2013-10-19 19:35:37 +02:00
|
|
|
|
2014-06-10 18:57:30 +02:00
|
|
|
class Client final
|
|
|
|
: FullyBufferedSocket, TimeoutMonitor,
|
|
|
|
public boost::intrusive::list_base_hook<boost::intrusive::link_mode<boost::intrusive::normal_link>> {
|
2013-10-19 19:35:37 +02:00
|
|
|
public:
|
|
|
|
Partition &partition;
|
|
|
|
struct playlist &playlist;
|
2013-10-28 10:12:21 +01:00
|
|
|
struct PlayerControl &player_control;
|
2013-10-19 19:35:37 +02:00
|
|
|
|
2014-06-10 18:57:30 +02:00
|
|
|
struct Disposer {
|
|
|
|
void operator()(Client *client) const {
|
|
|
|
delete client;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-19 19:35:37 +02:00
|
|
|
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);
|
|
|
|
|
2014-01-06 18:02:57 +01:00
|
|
|
~Client() {
|
|
|
|
if (FullyBufferedSocket::IsDefined())
|
|
|
|
FullyBufferedSocket::Close();
|
|
|
|
}
|
|
|
|
|
2013-10-19 19:35:37 +02:00
|
|
|
bool IsConnected() const {
|
|
|
|
return FullyBufferedSocket::IsDefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
gcc_pure
|
|
|
|
bool IsExpired() const {
|
|
|
|
return !FullyBufferedSocket::IsDefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Close();
|
|
|
|
void SetExpired();
|
|
|
|
|
|
|
|
using FullyBufferedSocket::Write;
|
|
|
|
|
2013-10-19 19:39:40 +02:00
|
|
|
/**
|
|
|
|
* 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 {
|
2014-11-18 20:56:27 +01:00
|
|
|
return uid >= 0;
|
2013-10-19 19:39:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned GetPermission() const {
|
|
|
|
return permission;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetPermission(unsigned _permission) {
|
|
|
|
permission = _permission;
|
|
|
|
}
|
|
|
|
|
2013-10-19 19:35:37 +02:00
|
|
|
/**
|
|
|
|
* Send "idle" response to this client.
|
|
|
|
*/
|
|
|
|
void IdleNotify();
|
|
|
|
void IdleAdd(unsigned flags);
|
|
|
|
bool IdleWait(unsigned flags);
|
|
|
|
|
2013-10-19 19:44:45 +02:00
|
|
|
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);
|
|
|
|
|
2014-02-02 13:59:07 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param path_fs the absolute path name in filesystem encoding
|
|
|
|
* @return true if access is allowed
|
|
|
|
*/
|
|
|
|
bool AllowFile(Path path_fs, Error &error) const;
|
|
|
|
|
2014-02-01 00:26:34 +01:00
|
|
|
/**
|
|
|
|
* Wrapper for Instance::GetDatabase().
|
|
|
|
*/
|
2014-02-07 00:29:07 +01:00
|
|
|
gcc_pure
|
2014-02-01 00:26:34 +01:00
|
|
|
const Database *GetDatabase(Error &error) const;
|
|
|
|
|
2014-02-07 00:29:07 +01:00
|
|
|
gcc_pure
|
|
|
|
const Storage *GetStorage() const;
|
|
|
|
|
2013-10-19 19:35:37 +02:00
|
|
|
private:
|
|
|
|
/* virtual methods from class BufferedSocket */
|
|
|
|
virtual InputResult OnSocketInput(void *data, size_t length) override;
|
|
|
|
virtual void OnSocketError(Error &&error) override;
|
|
|
|
virtual void OnSocketClosed() override;
|
|
|
|
|
|
|
|
/* virtual methods from class TimeoutMonitor */
|
|
|
|
virtual void OnTimeout() override;
|
|
|
|
};
|
2008-08-29 09:36:42 +02:00
|
|
|
|
2008-08-28 20:03:02 +02:00
|
|
|
void client_manager_init(void);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-01-04 23:07:11 +01:00
|
|
|
void
|
2013-01-14 23:42:06 +01:00
|
|
|
client_new(EventLoop &loop, Partition &partition,
|
2013-11-28 11:50:54 +01:00
|
|
|
int fd, const sockaddr *sa, size_t sa_length, int uid);
|
2008-08-29 09:36:40 +02:00
|
|
|
|
2008-09-07 13:24:51 +02:00
|
|
|
/**
|
|
|
|
* Write a C string to the client.
|
|
|
|
*/
|
2013-10-19 18:48:38 +02:00
|
|
|
void client_puts(Client &client, const char *s);
|
2008-09-07 13:24:51 +02:00
|
|
|
|
2008-09-07 13:25:54 +02:00
|
|
|
/**
|
|
|
|
* Write a printf-like formatted string to the client.
|
|
|
|
*/
|
2013-10-19 18:48:38 +02:00
|
|
|
void client_vprintf(Client &client, const char *fmt, va_list args);
|
2008-09-07 13:25:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a printf-like formatted string to the client.
|
|
|
|
*/
|
2013-10-15 09:13:22 +02:00
|
|
|
gcc_printf(2,3)
|
2012-08-02 18:15:49 +02:00
|
|
|
void
|
2013-10-19 18:48:38 +02:00
|
|
|
client_printf(Client &client, const char *fmt, ...);
|
2008-09-07 13:25:54 +02:00
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
#endif
|