SocketUtil.cxx 1.53 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
 * 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.
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.
18 19
 */

20
#include "config.h"
21
#include "SocketUtil.hxx"
22
#include "SocketAddress.hxx"
23
#include "SocketError.hxx"
24
#include "UniqueSocketDescriptor.hxx"
25

26
UniqueSocketDescriptor
27
socket_bind_listen(int domain, int type, int protocol,
28
		   SocketAddress address,
29
		   int backlog)
30
{
31 32
	UniqueSocketDescriptor fd;
	if (!fd.CreateNonBlock(domain, type, protocol))
33
		throw MakeSocketError("Failed to create socket");
34

35 36
	if (!fd.SetReuseAddress())
		throw MakeSocketError("setsockopt() failed");
37

38 39
	if (!fd.Bind(address))
		throw MakeSocketError("Failed to bind socket");
40

41 42
	if (!fd.Listen(backlog))
		throw MakeSocketError("Failed to listen on socket");
43

44
#if defined(HAVE_STRUCT_UCRED) && defined(SO_PASSCRED)
45
	fd.SetBoolOption(SOL_SOCKET, SO_PASSCRED, true);
46 47 48 49
#endif

	return fd;
}