aboutsummaryrefslogtreecommitdiffstats
path: root/src/net
diff options
context:
space:
mode:
Diffstat (limited to 'src/net')
-rw-r--r--src/net/Resolver.cxx190
-rw-r--r--src/net/Resolver.hxx59
-rw-r--r--src/net/SocketAddress.cxx38
-rw-r--r--src/net/SocketAddress.hxx103
-rw-r--r--src/net/SocketError.cxx65
-rw-r--r--src/net/SocketError.hxx139
-rw-r--r--src/net/SocketUtil.cxx87
-rw-r--r--src/net/SocketUtil.hxx54
-rw-r--r--src/net/StaticSocketAddress.cxx78
-rw-r--r--src/net/StaticSocketAddress.hxx114
10 files changed, 927 insertions, 0 deletions
diff --git a/src/net/Resolver.cxx b/src/net/Resolver.cxx
new file mode 100644
index 000000000..19df84096
--- /dev/null
+++ b/src/net/Resolver.cxx
@@ -0,0 +1,190 @@
+/*
+ * Copyright (C) 2003-2015 The Music Player Daemon Project
+ * 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.
+ */
+
+#include "config.h"
+#include "Resolver.hxx"
+#include "SocketAddress.hxx"
+#include "util/Error.hxx"
+#include "util/Domain.hxx"
+
+#include <algorithm>
+
+#ifndef WIN32
+#include <sys/socket.h>
+#include <netdb.h>
+#ifdef HAVE_TCP
+#include <netinet/in.h>
+#endif
+#else
+#include <ws2tcpip.h>
+#include <winsock.h>
+#endif
+
+#ifdef HAVE_UN
+#include <sys/un.h>
+#endif
+
+#include <string.h>
+#include <stdio.h>
+
+const Domain resolver_domain("resolver");
+
+#ifdef HAVE_UN
+
+static std::string
+LocalAddressToString(const struct sockaddr_un &s_un, size_t size)
+{
+ const size_t prefix_size = (size_t)
+ ((struct sockaddr_un *)nullptr)->sun_path;
+ assert(size >= prefix_size);
+
+ size_t result_length = size - prefix_size;
+
+ /* remove the trailing null terminator */
+ if (result_length > 0 && s_un.sun_path[result_length - 1] == 0)
+ --result_length;
+
+ if (result_length == 0)
+ return "local";
+
+ std::string result(s_un.sun_path, result_length);
+
+ /* replace all null bytes with '@'; this also handles abstract
+ addresses (Linux specific) */
+ std::replace(result.begin(), result.end(), '\0', '@');
+
+ return result;
+}
+
+#endif
+
+std::string
+sockaddr_to_string(SocketAddress address)
+{
+#ifdef HAVE_UN
+ if (address.GetFamily() == AF_UNIX)
+ /* return path of UNIX domain sockets */
+ return LocalAddressToString(*(const sockaddr_un *)address.GetAddress(),
+ address.GetSize());
+#endif
+
+#if defined(HAVE_IPV6) && defined(IN6_IS_ADDR_V4MAPPED)
+ const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *)
+ address.GetAddress();
+ struct sockaddr_in a4;
+#endif
+ int ret;
+ char host[NI_MAXHOST], serv[NI_MAXSERV];
+
+#if defined(HAVE_IPV6) && defined(IN6_IS_ADDR_V4MAPPED)
+ if (address.GetFamily() == AF_INET6 &&
+ IN6_IS_ADDR_V4MAPPED(&a6->sin6_addr)) {
+ /* convert "::ffff:127.0.0.1" to "127.0.0.1" */
+
+ memset(&a4, 0, sizeof(a4));
+ a4.sin_family = AF_INET;
+ memcpy(&a4.sin_addr, ((const char *)&a6->sin6_addr) + 12,
+ sizeof(a4.sin_addr));
+ a4.sin_port = a6->sin6_port;
+
+ address = { (const struct sockaddr *)&a4, sizeof(a4) };
+ }
+#endif
+
+ ret = getnameinfo(address.GetAddress(), address.GetSize(),
+ host, sizeof(host), serv, sizeof(serv),
+ NI_NUMERICHOST|NI_NUMERICSERV);
+ if (ret != 0)
+ return "unknown";
+
+#ifdef HAVE_IPV6
+ if (strchr(host, ':') != nullptr) {
+ std::string result("[");
+ result.append(host);
+ result.append("]:");
+ result.append(serv);
+ return result;
+ }
+#endif
+
+ std::string result(host);
+ result.push_back(':');
+ result.append(serv);
+ return result;
+}
+
+struct addrinfo *
+resolve_host_port(const char *host_port, unsigned default_port,
+ int flags, int socktype,
+ Error &error)
+{
+ std::string p(host_port);
+ const char *host = p.c_str(), *port = nullptr;
+
+ if (host_port[0] == '[') {
+ /* IPv6 needs enclosing square braces, to
+ differentiate between IP colons and the port
+ separator */
+
+ size_t q = p.find(']', 1);
+ if (q != p.npos && p[q + 1] == ':' && p[q + 2] != 0) {
+ p[q] = 0;
+ port = host + q + 2;
+ ++host;
+ }
+ }
+
+ if (port == nullptr) {
+ /* port is after the colon, but only if it's the only
+ colon (don't split IPv6 addresses) */
+
+ auto q = p.find(':');
+ if (q != p.npos && p[q + 1] != 0 &&
+ p.find(':', q + 1) == p.npos) {
+ p[q] = 0;
+ port = host + q + 1;
+ }
+ }
+
+ char buffer[32];
+ if (port == nullptr && default_port != 0) {
+ snprintf(buffer, sizeof(buffer), "%u", default_port);
+ port = buffer;
+ }
+
+ if ((flags & AI_PASSIVE) != 0 && strcmp(host, "*") == 0)
+ host = nullptr;
+
+ addrinfo hints;
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_flags = flags;
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = socktype;
+
+ struct addrinfo *ai;
+ int ret = getaddrinfo(host, port, &hints, &ai);
+ if (ret != 0) {
+ error.Format(resolver_domain, ret,
+ "Failed to look up '%s': %s",
+ host_port, gai_strerror(ret));
+ return nullptr;
+ }
+
+ return ai;
+}
diff --git a/src/net/Resolver.hxx b/src/net/Resolver.hxx
new file mode 100644
index 000000000..28d2c784b
--- /dev/null
+++ b/src/net/Resolver.hxx
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2003-2015 The Music Player Daemon Project
+ * 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.
+ */
+
+#ifndef MPD_RESOLVER_HXX
+#define MPD_RESOLVER_HXX
+
+#include "Compiler.h"
+
+#include <string>
+
+#include <stddef.h>
+
+struct addrinfo;
+class Error;
+class Domain;
+class SocketAddress;
+
+extern const Domain resolver_domain;
+
+/**
+ * Converts the specified socket address into a string in the form
+ * "IP:PORT".
+ */
+gcc_pure
+std::string
+sockaddr_to_string(SocketAddress address);
+
+/**
+ * Resolve a specification in the form "host", "host:port",
+ * "[host]:port". This is a convenience wrapper for getaddrinfo().
+ *
+ * @param default_port a default port number that will be used if none
+ * is given in the string (if applicable); pass 0 to go without a
+ * default
+ * @return an #addrinfo linked list that must be freed with
+ * freeaddrinfo(), or NULL on error
+ */
+addrinfo *
+resolve_host_port(const char *host_port, unsigned default_port,
+ int flags, int socktype,
+ Error &error);
+
+#endif
diff --git a/src/net/SocketAddress.cxx b/src/net/SocketAddress.cxx
new file mode 100644
index 000000000..38aeb8d6d
--- /dev/null
+++ b/src/net/SocketAddress.cxx
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2012-2015 Max Kellermann <max@duempel.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "SocketAddress.hxx"
+
+#include <string.h>
+
+bool
+SocketAddress::operator==(SocketAddress other) const
+{
+ return size == other.size && memcmp(address, other.address, size) == 0;
+}
diff --git a/src/net/SocketAddress.hxx b/src/net/SocketAddress.hxx
new file mode 100644
index 000000000..0577edd72
--- /dev/null
+++ b/src/net/SocketAddress.hxx
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2012-2015 Max Kellermann <max@duempel.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef SOCKET_ADDRESS_HXX
+#define SOCKET_ADDRESS_HXX
+
+#include "Compiler.h"
+
+#include <cstddef>
+
+#ifdef WIN32
+#include <winsock2.h>
+#else
+#include <sys/socket.h>
+#endif
+
+/**
+ * An OO wrapper for struct sockaddr.
+ */
+class SocketAddress {
+public:
+#ifdef WIN32
+ typedef int size_type;
+#else
+ typedef socklen_t size_type;
+#endif
+
+private:
+ const struct sockaddr *address;
+ size_type size;
+
+public:
+ SocketAddress() = default;
+
+ constexpr SocketAddress(std::nullptr_t):address(nullptr), size(0) {}
+
+ constexpr SocketAddress(const struct sockaddr *_address,
+ size_type _size)
+ :address(_address), size(_size) {}
+
+ static constexpr SocketAddress Null() {
+ return nullptr;
+ }
+
+ constexpr bool IsNull() const {
+ return address == nullptr;
+ }
+
+ const struct sockaddr *GetAddress() const {
+ return address;
+ }
+
+ constexpr size_type GetSize() const {
+ return size;
+ }
+
+ constexpr int GetFamily() const {
+ return address->sa_family;
+ }
+
+ /**
+ * Does the object have a well-defined address? Check !IsNull()
+ * before calling this method.
+ */
+ bool IsDefined() const {
+ return GetFamily() != AF_UNSPEC;
+ }
+
+ gcc_pure
+ bool operator==(const SocketAddress other) const;
+
+ bool operator!=(const SocketAddress other) const {
+ return !(*this == other);
+ }
+};
+
+#endif
diff --git a/src/net/SocketError.cxx b/src/net/SocketError.cxx
new file mode 100644
index 000000000..efa969b1f
--- /dev/null
+++ b/src/net/SocketError.cxx
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2003-2015 The Music Player Daemon Project
+ * 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.
+ */
+
+#include "config.h"
+#include "SocketError.hxx"
+#include "util/Domain.hxx"
+#include "util/Macros.hxx"
+
+#include <string.h>
+
+const Domain socket_domain("socket");
+
+#ifdef WIN32
+
+SocketErrorMessage::SocketErrorMessage(socket_error_t code)
+{
+#ifdef _UNICODE
+ wchar_t buffer[ARRAY_SIZE(msg)];
+#else
+ auto *buffer = msg;
+#endif
+
+ DWORD nbytes = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS |
+ FORMAT_MESSAGE_MAX_WIDTH_MASK,
+ nullptr, code, 0,
+ buffer, ARRAY_SIZE(msg), nullptr);
+ if (nbytes == 0) {
+ strcpy(msg, "Unknown error");
+ return;
+ }
+
+#ifdef _UNICODE
+ auto length = WideCharToMultiByte(CP_UTF8, 0, buffer, -1,
+ msg, ARRAY_SIZE(msg),
+ nullptr, nullptr);
+ if (length <= 0) {
+ strcpy(msg, "WideCharToMultiByte() error");
+ return;
+ }
+#endif
+}
+
+#else
+
+SocketErrorMessage::SocketErrorMessage(socket_error_t code)
+ :msg(strerror(code)) {}
+
+#endif
diff --git a/src/net/SocketError.hxx b/src/net/SocketError.hxx
new file mode 100644
index 000000000..c9435d6fb
--- /dev/null
+++ b/src/net/SocketError.hxx
@@ -0,0 +1,139 @@
+/*
+ * Copyright (C) 2003-2015 The Music Player Daemon Project
+ * 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.
+ */
+
+#ifndef MPD_SOCKET_ERROR_HXX
+#define MPD_SOCKET_ERROR_HXX
+
+#include "Compiler.h"
+#include "util/Error.hxx" // IWYU pragma: export
+
+#ifdef WIN32
+#include <winsock2.h>
+typedef DWORD socket_error_t;
+#else
+#include <errno.h>
+typedef int socket_error_t;
+#endif
+
+class Domain;
+
+/**
+ * A #Domain for #Error for socket I/O errors. The code is an errno
+ * value (or WSAGetLastError() on Windows).
+ */
+extern const Domain socket_domain;
+
+gcc_pure
+static inline socket_error_t
+GetSocketError()
+{
+#ifdef WIN32
+ return WSAGetLastError();
+#else
+ return errno;
+#endif
+}
+
+gcc_const
+static inline bool
+IsSocketErrorAgain(socket_error_t code)
+{
+#ifdef WIN32
+ return code == WSAEINPROGRESS;
+#else
+ return code == EAGAIN;
+#endif
+}
+
+gcc_const
+static inline bool
+IsSocketErrorInterruped(socket_error_t code)
+{
+#ifdef WIN32
+ return code == WSAEINTR;
+#else
+ return code == EINTR;
+#endif
+}
+
+gcc_const
+static inline bool
+IsSocketErrorClosed(socket_error_t code)
+{
+#ifdef WIN32
+ return code == WSAECONNRESET;
+#else
+ return code == EPIPE || code == ECONNRESET;
+#endif
+}
+
+/**
+ * Helper class that formats a socket error message into a
+ * human-readable string. On Windows, a buffer is necessary for this,
+ * and this class hosts the buffer.
+ */
+class SocketErrorMessage {
+#ifdef WIN32
+ char msg[256];
+#else
+ const char *const msg;
+#endif
+
+public:
+#ifdef WIN32
+ explicit SocketErrorMessage(socket_error_t code=GetSocketError());
+#else
+ explicit SocketErrorMessage(socket_error_t code=GetSocketError());
+#endif
+
+ operator const char *() const {
+ return msg;
+ }
+};
+
+static inline void
+SetSocketError(Error &error, socket_error_t code)
+{
+ const SocketErrorMessage msg(code);
+ error.Set(socket_domain, code, msg);
+}
+
+static inline void
+SetSocketError(Error &error)
+{
+ SetSocketError(error, GetSocketError());
+}
+
+gcc_const
+static inline Error
+NewSocketError(socket_error_t code)
+{
+ Error error;
+ SetSocketError(error, code);
+ return error;
+}
+
+gcc_pure
+static inline Error
+NewSocketError()
+{
+ return NewSocketError(GetSocketError());
+}
+
+#endif
diff --git a/src/net/SocketUtil.cxx b/src/net/SocketUtil.cxx
new file mode 100644
index 000000000..b2ea63985
--- /dev/null
+++ b/src/net/SocketUtil.cxx
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2003-2015 The Music Player Daemon Project
+ * 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.
+ */
+
+#include "config.h"
+#include "SocketUtil.hxx"
+#include "SocketAddress.hxx"
+#include "SocketError.hxx"
+#include "system/fd_util.h"
+
+#include <unistd.h>
+
+#ifdef HAVE_IPV6
+#include <string.h>
+#endif
+
+int
+socket_bind_listen(int domain, int type, int protocol,
+ SocketAddress address,
+ int backlog,
+ Error &error)
+{
+ int fd, ret;
+ const int reuse = 1;
+
+ fd = socket_cloexec_nonblock(domain, type, protocol);
+ if (fd < 0) {
+ SetSocketError(error);
+ error.AddPrefix("Failed to create socket: ");
+ return -1;
+ }
+
+ ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
+ (const char *) &reuse, sizeof(reuse));
+ if (ret < 0) {
+ SetSocketError(error);
+ error.AddPrefix("setsockopt() failed: ");
+ close_socket(fd);
+ return -1;
+ }
+
+ ret = bind(fd, address.GetAddress(), address.GetSize());
+ if (ret < 0) {
+ SetSocketError(error);
+ close_socket(fd);
+ return -1;
+ }
+
+ ret = listen(fd, backlog);
+ if (ret < 0) {
+ SetSocketError(error);
+ error.AddPrefix("listen() failed: ");
+ close_socket(fd);
+ return -1;
+ }
+
+#ifdef HAVE_STRUCT_UCRED
+ setsockopt(fd, SOL_SOCKET, SO_PASSCRED,
+ (const char *) &reuse, sizeof(reuse));
+#endif
+
+ return fd;
+}
+
+int
+socket_keepalive(int fd)
+{
+ const int reuse = 1;
+
+ return setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
+ (const char *)&reuse, sizeof(reuse));
+}
diff --git a/src/net/SocketUtil.hxx b/src/net/SocketUtil.hxx
new file mode 100644
index 000000000..e7f6b8eaa
--- /dev/null
+++ b/src/net/SocketUtil.hxx
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2003-2015 The Music Player Daemon Project
+ * 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.
+ */
+
+/*
+ * This library provides easy helper functions for working with
+ * sockets.
+ *
+ */
+
+#ifndef MPD_SOCKET_UTIL_HXX
+#define MPD_SOCKET_UTIL_HXX
+
+class SocketAddress;
+class Error;
+
+/**
+ * Creates a socket listening on the specified address. This is a
+ * shortcut for socket(), bind() and listen().
+ *
+ * @param domain the socket domain, e.g. PF_INET6
+ * @param type the socket type, e.g. SOCK_STREAM
+ * @param protocol the protocol, usually 0 to let the kernel choose
+ * @param address the address to listen on
+ * @param backlog the backlog parameter for the listen() system call
+ * @param error location to store the error occurring, or NULL to
+ * ignore errors
+ * @return the socket file descriptor or -1 on error
+ */
+int
+socket_bind_listen(int domain, int type, int protocol,
+ SocketAddress address,
+ int backlog,
+ Error &error);
+
+int
+socket_keepalive(int fd);
+
+#endif
diff --git a/src/net/StaticSocketAddress.cxx b/src/net/StaticSocketAddress.cxx
new file mode 100644
index 000000000..15e8a9e44
--- /dev/null
+++ b/src/net/StaticSocketAddress.cxx
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2012-2015 Max Kellermann <max@duempel.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "StaticSocketAddress.hxx"
+
+#include <algorithm>
+
+#include <assert.h>
+#include <string.h>
+
+#ifdef WIN32
+#include <ws2tcpip.h>
+#else
+#include <sys/un.h>
+#include <sys/socket.h>
+#endif
+
+StaticSocketAddress &
+StaticSocketAddress::operator=(SocketAddress other)
+{
+ size = std::min(size_t(other.GetSize()), GetCapacity());
+ memcpy(&address, other.GetAddress(), size);
+ return *this;
+}
+
+bool
+StaticSocketAddress::operator==(const StaticSocketAddress &other) const
+{
+ return size == other.size &&
+ memcmp(&address, &other.address, size) == 0;
+}
+
+#ifdef HAVE_UN
+
+void
+StaticSocketAddress::SetLocal(const char *path)
+{
+ auto &sun = reinterpret_cast<struct sockaddr_un &>(address);
+
+ const size_t path_length = strlen(path);
+
+ // TODO: make this a runtime check
+ assert(path_length < sizeof(sun.sun_path));
+
+ sun.sun_family = AF_LOCAL;
+ memcpy(sun.sun_path, path, path_length + 1);
+
+ /* note: Bionic doesn't provide SUN_LEN() */
+ size = SUN_LEN(&sun);
+}
+
+#endif
diff --git a/src/net/StaticSocketAddress.hxx b/src/net/StaticSocketAddress.hxx
new file mode 100644
index 000000000..4e1873a81
--- /dev/null
+++ b/src/net/StaticSocketAddress.hxx
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2012-2015 Max Kellermann <max@duempel.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef STATIC_SOCKET_ADDRESS_HXX
+#define STATIC_SOCKET_ADDRESS_HXX
+
+#include "SocketAddress.hxx"
+#include "Compiler.h"
+
+#include <assert.h>
+#include <stdint.h>
+
+#ifdef WIN32
+#include <winsock2.h>
+#else
+#include <sys/socket.h>
+#endif
+
+struct ifaddrs;
+
+/**
+ * An OO wrapper for struct sockaddr_storage.
+ */
+class StaticSocketAddress {
+ size_t size;
+ struct sockaddr_storage address;
+
+public:
+ StaticSocketAddress() = default;
+
+ StaticSocketAddress &operator=(SocketAddress other);
+
+ operator SocketAddress() const {
+ return SocketAddress(reinterpret_cast<const struct sockaddr *>(&address),
+ size);
+ }
+
+#ifdef HAVE_UN
+ /**
+ * Make this a "local" address (UNIX domain socket).
+ */
+ void SetLocal(const char *path);
+#endif
+
+ operator struct sockaddr *() {
+ return reinterpret_cast<struct sockaddr *>(&address);
+ }
+
+ operator const struct sockaddr *() const {
+ return reinterpret_cast<const struct sockaddr *>(&address);
+ }
+
+ constexpr size_t GetCapacity() const {
+ return sizeof(address);
+ }
+
+ size_t GetSize() const {
+ return size;
+ }
+
+ void SetSize(size_t _size) {
+ assert(_size > 0);
+ assert(_size <= sizeof(address));
+
+ size = _size;
+ }
+
+ int GetFamily() const {
+ return address.ss_family;
+ }
+
+ bool IsDefined() const {
+ return GetFamily() != AF_UNSPEC;
+ }
+
+ void Clear() {
+ address.ss_family = AF_UNSPEC;
+ }
+
+ gcc_pure
+ bool operator==(const StaticSocketAddress &other) const;
+
+ bool operator!=(const StaticSocketAddress &other) const {
+ return !(*this == other);
+ }
+};
+
+#endif