2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2009-02-28 15:12:24 +01:00
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-15 22:39:51 +01:00
|
|
|
#include "SocketUtil.hxx"
|
2015-02-10 20:30:10 +01:00
|
|
|
#include "SocketAddress.hxx"
|
2013-01-15 22:27:24 +01:00
|
|
|
#include "SocketError.hxx"
|
2017-08-10 09:53:09 +02:00
|
|
|
#include "UniqueSocketDescriptor.hxx"
|
2009-02-28 15:12:24 +01:00
|
|
|
|
2018-08-07 22:01:13 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2017-08-10 09:53:09 +02:00
|
|
|
UniqueSocketDescriptor
|
2009-03-14 18:29:38 +01:00
|
|
|
socket_bind_listen(int domain, int type, int protocol,
|
2015-02-10 20:30:10 +01:00
|
|
|
SocketAddress address,
|
2016-10-28 10:36:05 +02:00
|
|
|
int backlog)
|
2009-03-14 18:29:38 +01:00
|
|
|
{
|
2017-08-10 09:53:09 +02:00
|
|
|
UniqueSocketDescriptor fd;
|
|
|
|
if (!fd.CreateNonBlock(domain, type, protocol))
|
2016-10-28 10:36:05 +02:00
|
|
|
throw MakeSocketError("Failed to create socket");
|
2009-03-14 18:29:38 +01:00
|
|
|
|
2018-08-07 22:01:13 +02:00
|
|
|
#ifdef HAVE_UN
|
2019-03-20 12:57:26 +01:00
|
|
|
if (domain == AF_LOCAL) {
|
2018-08-14 22:53:19 +02:00
|
|
|
/* Prevent access until right permissions are set */
|
|
|
|
fchmod(fd.Get(), 0);
|
2018-08-07 22:01:13 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-08-10 09:53:09 +02:00
|
|
|
if (!fd.SetReuseAddress())
|
|
|
|
throw MakeSocketError("setsockopt() failed");
|
2009-03-14 18:29:38 +01:00
|
|
|
|
2017-08-10 09:53:09 +02:00
|
|
|
if (!fd.Bind(address))
|
|
|
|
throw MakeSocketError("Failed to bind socket");
|
2009-03-14 18:29:38 +01:00
|
|
|
|
2017-08-10 09:53:09 +02:00
|
|
|
if (!fd.Listen(backlog))
|
|
|
|
throw MakeSocketError("Failed to listen on socket");
|
2009-03-14 18:29:38 +01:00
|
|
|
|
2015-09-17 22:18:09 +02:00
|
|
|
#if defined(HAVE_STRUCT_UCRED) && defined(SO_PASSCRED)
|
2017-08-10 19:30:46 +02:00
|
|
|
fd.SetBoolOption(SOL_SOCKET, SO_PASSCRED, true);
|
2009-03-14 18:29:38 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|