ClientNew.cxx 2.93 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 "net/UniqueSocketDescriptor.hxx"
26
#include "net/SocketAddress.hxx"
27
#include "net/ToString.hxx"
28
#include "Permission.hxx"
29
#include "Log.hxx"
30 31

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

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

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

44
Client::Client(EventLoop &_loop, Partition &_partition,
45
	       UniqueSocketDescriptor _fd, int _uid, int _num) noexcept
46 47
	:FullyBufferedSocket(_fd.Release(), _loop,
			     16384, client_max_output_buffer_size),
48
	 timeout_event(_loop, BIND_THIS_METHOD(OnTimeout)),
49
	 partition(&_partition),
50 51
	 permission(getDefaultPermissions()),
	 uid(_uid),
52
	 num(_num)
53
{
54
	timeout_event.Schedule(client_timeout);
55 56
}

57
void
58
client_new(EventLoop &loop, Partition &partition,
59
	   UniqueSocketDescriptor fd, SocketAddress address, int uid) noexcept
60 61
{
	static unsigned int next_client_num;
62
	const auto remote = ToString(address);
63

64
	assert(fd.IsDefined());
65

66
#ifdef HAVE_LIBWRAP
67
	if (address.GetFamily() != AF_LOCAL) {
68 69
		// TODO: shall we obtain the program name from argv[0]?
		const char *progname = "mpd";
70 71

		struct request_info req;
72
		request_init(&req, RQ_FILE, fd.Get(), RQ_DAEMON, progname, 0);
73 74 75 76 77

		fromhost(&req);

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

			return;
		}
	}
#endif	/* HAVE_WRAP */

87 88
	ClientList &client_list = *partition.instance.client_list;
	if (client_list.IsFull()) {
89
		LogWarning(client_domain, "Max connections reached");
90 91 92
		return;
	}

93
	(void)fd.Write(GREETING, sizeof(GREETING) - 1);
94

95 96 97
	Client *client = new Client(loop, partition, std::move(fd), uid,
				    next_client_num++);

98
	client_list.Add(*client);
99

100 101
	FormatInfo(client_domain, "[%u] opened from %s",
		   client->num, remote.c_str());
102 103 104
}

void
105
Client::Close() noexcept
106
{
107
	partition->instance.client_list->Remove(*this);
108

109
	SetExpired();
110

111
	FormatInfo(client_domain, "[%u] closed", num);
112
	delete this;
113
}