ClientNew.cxx 2.95 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * 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.
 *
 * 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.
 */

20
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21
#include "ClientInternal.hxx"
22
#include "ClientList.hxx"
23
#include "Partition.hxx"
24
#include "Instance.hxx"
25
#include "system/fd_util.h"
26
#include "net/SocketAddress.hxx"
27
#include "net/ToString.hxx"
28
#include "Permission.hxx"
29
#include "Log.hxx"
30 31

#include <assert.h>
32 33 34 35 36
#ifdef WIN32
#include <winsock2.h>
#else
#include <sys/socket.h>
#endif
37

38 39 40 41
#ifdef HAVE_LIBWRAP
#include <tcpd.h>
#endif

42 43
static const char GREETING[] = "OK MPD " PROTOCOL_VERSION "\n";

44 45
Client::Client(EventLoop &_loop, Partition &_partition,
	       int _fd, int _uid, int _num)
46
	:FullyBufferedSocket(_fd, _loop, 16384, client_max_output_buffer_size),
47
	 TimeoutMonitor(_loop),
48
	 partition(_partition),
49
	 playlist(partition.playlist), player_control(partition.pc),
50 51 52
	 permission(getDefaultPermissions()),
	 uid(_uid),
	 num(_num),
53 54
	 idle_waiting(false), idle_flags(0),
	 num_subscriptions(0)
55
{
56
	TimeoutMonitor::Schedule(client_timeout);
57 58
}

59
void
60
client_new(EventLoop &loop, Partition &partition,
61
	   int fd, SocketAddress address, int uid)
62 63
{
	static unsigned int next_client_num;
64
	const auto remote = ToString(address);
65 66 67

	assert(fd >= 0);

68
#ifdef HAVE_LIBWRAP
69
	if (address.GetFamily() != AF_UNIX) {
70 71
		// TODO: shall we obtain the program name from argv[0]?
		const char *progname = "mpd";
72 73 74 75 76 77 78 79

		struct request_info req;
		request_init(&req, RQ_FILE, fd, RQ_DAEMON, progname, 0);

		fromhost(&req);

		if (!hosts_access(&req)) {
			/* tcp wrappers says no */
80 81
			FormatWarning(client_domain,
				      "libwrap refused connection (libwrap=%s) from %s",
82
				      progname, remote.c_str());
83

84
			close_socket(fd);
85 86 87 88 89
			return;
		}
	}
#endif	/* HAVE_WRAP */

90 91
	ClientList &client_list = *partition.instance.client_list;
	if (client_list.IsFull()) {
92
		LogWarning(client_domain, "Max connections reached");
93
		close_socket(fd);
94 95 96
		return;
	}

97
	Client *client = new Client(loop, partition, fd, uid,
98
				    next_client_num++);
99

100
	(void)send(fd, GREETING, sizeof(GREETING) - 1, 0);
101

102
	client_list.Add(*client);
103

104 105
	FormatInfo(client_domain, "[%u] opened from %s",
		   client->num, remote.c_str());
106 107 108
}

void
109
Client::Close()
110
{
111
	partition.instance.client_list->Remove(*this);
112

113
	SetExpired();
114

115
	FormatInfo(client_domain, "[%u] closed", num);
116
	delete this;
117
}