2009-02-28 15:12:24 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2009-02-28 15:12:24 +01:00
|
|
|
* 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.
|
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.
|
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
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
}
|