net/SocketAddress: light wrapper for struct sockaddr

This commit is contained in:
Max Kellermann 2015-02-10 20:30:10 +01:00
parent 42890b9acf
commit 4e30e74739
15 changed files with 201 additions and 67 deletions

View File

@ -421,6 +421,7 @@ libthread_a_SOURCES = \
libnet_a_SOURCES = \
src/net/Resolver.cxx src/net/Resolver.hxx \
src/net/SocketAddress.cxx src/net/SocketAddress.hxx \
src/net/SocketUtil.cxx src/net/SocketUtil.hxx \
src/net/SocketError.cxx src/net/SocketError.hxx

View File

@ -23,6 +23,7 @@
#include "config/Param.hxx"
#include "config/ConfigGlobal.hxx"
#include "config/ConfigOption.hxx"
#include "net/SocketAddress.hxx"
#include "event/ServerSocket.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
@ -48,10 +49,9 @@ public:
:ServerSocket(_loop), partition(_partition) {}
private:
void OnAccept(int fd, const sockaddr &address,
size_t address_length, int uid) override {
void OnAccept(int fd, SocketAddress address, int uid) override {
client_new(GetEventLoop(), partition,
fd, &address, address_length, uid);
fd, address, uid);
}
};

View File

@ -36,7 +36,7 @@
#include <stddef.h>
#include <stdarg.h>
struct sockaddr;
class SocketAddress;
class EventLoop;
class Path;
struct Partition;
@ -204,7 +204,7 @@ void client_manager_init(void);
void
client_new(EventLoop &loop, Partition &partition,
int fd, const sockaddr *sa, size_t sa_length, int uid);
int fd, SocketAddress address, int uid);
/**
* Write a C string to the client.

View File

@ -23,6 +23,7 @@
#include "Partition.hxx"
#include "Instance.hxx"
#include "system/fd_util.h"
#include "net/SocketAddress.hxx"
#include "net/Resolver.hxx"
#include "Permission.hxx"
#include "util/Error.hxx"
@ -58,15 +59,15 @@ Client::Client(EventLoop &_loop, Partition &_partition,
void
client_new(EventLoop &loop, Partition &partition,
int fd, const struct sockaddr *sa, size_t sa_length, int uid)
int fd, SocketAddress address, int uid)
{
static unsigned int next_client_num;
const auto remote = sockaddr_to_string(sa, sa_length);
const auto remote = sockaddr_to_string(address);
assert(fd >= 0);
#ifdef HAVE_LIBWRAP
if (sa->sa_family != AF_UNIX) {
if (address.GetFamily() != AF_UNIX) {
// TODO: shall we obtain the program name from argv[0]?
const char *progname = "mpd";

View File

@ -19,6 +19,7 @@
#include "config.h"
#include "ServerSocket.hxx"
#include "net/SocketAddress.hxx"
#include "net/SocketUtil.hxx"
#include "net/SocketError.hxx"
#include "net/Resolver.hxx"
@ -59,29 +60,28 @@ class OneServerSocket final : private SocketMonitor {
AllocatedPath path;
size_t address_length;
struct sockaddr *address;
SocketAddress address;
public:
OneServerSocket(EventLoop &_loop, ServerSocket &_parent,
unsigned _serial,
const struct sockaddr *_address,
size_t _address_length)
SocketAddress _address)
:SocketMonitor(_loop),
parent(_parent), serial(_serial),
path(AllocatedPath::Null()),
address_length(_address_length),
address((sockaddr *)xmemdup(_address, _address_length))
address((sockaddr *)xmemdup(_address.GetAddress(),
_address.GetSize()),
_address.GetSize())
{
assert(_address != nullptr);
assert(_address_length > 0);
assert(!_address.IsNull());
assert(_address.GetSize() > 0);
}
OneServerSocket(const OneServerSocket &other) = delete;
OneServerSocket &operator=(const OneServerSocket &other) = delete;
~OneServerSocket() {
free(address);
free(const_cast<struct sockaddr *>(address.GetAddress()));
if (IsDefined())
Close();
@ -104,7 +104,7 @@ public:
gcc_pure
std::string ToString() const {
return sockaddr_to_string(address, address_length);
return sockaddr_to_string(address);
}
void SetFD(int _fd) {
@ -168,8 +168,8 @@ OneServerSocket::Accept()
}
parent.OnAccept(peer_fd,
(const sockaddr &)peer_address,
peer_address_length, get_remote_uid(peer_fd));
{ (const sockaddr *)&peer_address, socklen_t(peer_address_length) },
get_remote_uid(peer_fd));
}
bool
@ -184,9 +184,9 @@ OneServerSocket::Open(Error &error)
{
assert(!IsDefined());
int _fd = socket_bind_listen(address->sa_family,
int _fd = socket_bind_listen(address.GetFamily(),
SOCK_STREAM, 0,
address, address_length, 5,
address, 5,
error);
if (_fd < 0)
return false;
@ -280,10 +280,10 @@ ServerSocket::Close()
}
OneServerSocket &
ServerSocket::AddAddress(const sockaddr &address, size_t address_length)
ServerSocket::AddAddress(SocketAddress address)
{
sockets.emplace_back(loop, *this, next_serial,
&address, address_length);
address);
return sockets.back();
}
@ -302,8 +302,7 @@ ServerSocket::AddFD(int fd, Error &error)
return false;
}
OneServerSocket &s = AddAddress((const sockaddr &)address,
address_length);
OneServerSocket &s = AddAddress({(const sockaddr *)&address, address_length});
s.SetFD(fd);
return true;
@ -320,7 +319,7 @@ ServerSocket::AddPortIPv4(unsigned port)
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
AddAddress((const sockaddr &)sin, sizeof(sin));
AddAddress({(const sockaddr *)&sin, sizeof(sin)});
}
#ifdef HAVE_IPV6
@ -333,7 +332,7 @@ ServerSocket::AddPortIPv6(unsigned port)
sin.sin6_port = htons(port);
sin.sin6_family = AF_INET6;
AddAddress((const sockaddr &)sin, sizeof(sin));
AddAddress({(const sockaddr *)&sin, sizeof(sin)});
}
/**
@ -392,7 +391,7 @@ ServerSocket::AddHost(const char *hostname, unsigned port, Error &error)
return false;
for (const struct addrinfo *i = ai; i != nullptr; i = i->ai_next)
AddAddress(*i->ai_addr, i->ai_addrlen);
AddAddress(SocketAddress(i->ai_addr, i->ai_addrlen));
freeaddrinfo(ai);
@ -426,7 +425,7 @@ ServerSocket::AddPath(AllocatedPath &&path, Error &error)
s_un.sun_family = AF_UNIX;
memcpy(s_un.sun_path, path.c_str(), path_length + 1);
OneServerSocket &s = AddAddress((const sockaddr &)s_un, sizeof(s_un));
OneServerSocket &s = AddAddress({(const sockaddr *)&s_un, sizeof(s_un)});
s.SetPath(std::move(path));
return true;

View File

@ -24,7 +24,7 @@
#include <stddef.h>
struct sockaddr;
class SocketAddress;
class EventLoop;
class Error;
class AllocatedPath;
@ -51,7 +51,7 @@ public:
}
private:
OneServerSocket &AddAddress(const sockaddr &address, size_t length);
OneServerSocket &AddAddress(SocketAddress address);
/**
* Add a listener on a port on all IPv4 interfaces.
@ -111,8 +111,7 @@ public:
void Close();
protected:
virtual void OnAccept(int fd, const sockaddr &address,
size_t address_length, int uid) = 0;
virtual void OnAccept(int fd, SocketAddress address, int uid) = 0;
};
#endif

View File

@ -19,6 +19,7 @@
#include "config.h"
#include "Resolver.hxx"
#include "SocketAddress.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
@ -43,13 +44,14 @@
const Domain resolver_domain("resolver");
std::string
sockaddr_to_string(const struct sockaddr *sa, size_t length)
sockaddr_to_string(SocketAddress address)
{
#ifdef HAVE_UN
if (sa->sa_family == AF_UNIX) {
if (address.GetFamily() == AF_UNIX) {
/* return path of UNIX domain sockets */
const sockaddr_un &s_un = *(const sockaddr_un *)sa;
if (length < sizeof(s_un) || s_un.sun_path[0] == 0)
const sockaddr_un &s_un = *(const sockaddr_un *)
address.GetAddress();
if (address.GetSize() < sizeof(s_un) || s_un.sun_path[0] == 0)
return "local";
return s_un.sun_path;
@ -57,14 +59,15 @@ sockaddr_to_string(const struct sockaddr *sa, size_t length)
#endif
#if defined(HAVE_IPV6) && defined(IN6_IS_ADDR_V4MAPPED)
const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *)sa;
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 (sa->sa_family == AF_INET6 &&
if (address.GetFamily() == AF_INET6 &&
IN6_IS_ADDR_V4MAPPED(&a6->sin6_addr)) {
/* convert "::ffff:127.0.0.1" to "127.0.0.1" */
@ -74,12 +77,12 @@ sockaddr_to_string(const struct sockaddr *sa, size_t length)
sizeof(a4.sin_addr));
a4.sin_port = a6->sin6_port;
sa = (const struct sockaddr *)&a4;
length = sizeof(a4);
address = { (const struct sockaddr *)&a4, sizeof(a4) };
}
#endif
ret = getnameinfo(sa, length, host, sizeof(host), serv, sizeof(serv),
ret = getnameinfo(address.GetAddress(), address.GetSize(),
host, sizeof(host), serv, sizeof(serv),
NI_NUMERICHOST|NI_NUMERICSERV);
if (ret != 0)
return "unknown";

View File

@ -26,10 +26,10 @@
#include <stddef.h>
struct sockaddr;
struct addrinfo;
class Error;
class Domain;
class SocketAddress;
extern const Domain resolver_domain;
@ -42,7 +42,7 @@ extern const Domain resolver_domain;
*/
gcc_pure
std::string
sockaddr_to_string(const sockaddr *sa, size_t length);
sockaddr_to_string(SocketAddress address);
/**
* Resolve a specification in the form "host", "host:port",

38
src/net/SocketAddress.cxx Normal file
View File

@ -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;
}

103
src/net/SocketAddress.hxx Normal file
View File

@ -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

View File

@ -19,25 +19,19 @@
#include "config.h"
#include "SocketUtil.hxx"
#include "SocketAddress.hxx"
#include "SocketError.hxx"
#include "system/fd_util.h"
#include <unistd.h>
#ifndef WIN32
#include <sys/socket.h>
#else
#include <ws2tcpip.h>
#include <winsock.h>
#endif
#ifdef HAVE_IPV6
#include <string.h>
#endif
int
socket_bind_listen(int domain, int type, int protocol,
const struct sockaddr *address, size_t address_length,
SocketAddress address,
int backlog,
Error &error)
{
@ -60,7 +54,7 @@ socket_bind_listen(int domain, int type, int protocol,
return -1;
}
ret = bind(fd, address, address_length);
ret = bind(fd, address.GetAddress(), address.GetSize());
if (ret < 0) {
SetSocketError(error);
close_socket(fd);

View File

@ -26,9 +26,7 @@
#ifndef MPD_SOCKET_UTIL_HXX
#define MPD_SOCKET_UTIL_HXX
#include <stddef.h>
struct sockaddr;
class SocketAddress;
class Error;
/**
@ -47,7 +45,7 @@ class Error;
*/
int
socket_bind_listen(int domain, int type, int protocol,
const struct sockaddr *address, size_t address_length,
SocketAddress address,
int backlog,
Error &error);

View File

@ -259,8 +259,7 @@ public:
private:
virtual void RunDeferred() override;
virtual void OnAccept(int fd, const sockaddr &address,
size_t address_length, int uid) override;
void OnAccept(int fd, SocketAddress address, int uid) override;
};
extern const class Domain httpd_output_domain;

View File

@ -26,6 +26,7 @@
#include "encoder/EncoderPlugin.hxx"
#include "encoder/EncoderList.hxx"
#include "net/Resolver.hxx"
#include "net/SocketAddress.hxx"
#include "Page.hxx"
#include "IcyMetaDataServer.hxx"
#include "system/fd_util.h"
@ -201,16 +202,14 @@ HttpdOutput::RunDeferred()
}
void
HttpdOutput::OnAccept(int fd, const sockaddr &address,
size_t address_length, gcc_unused int uid)
HttpdOutput::OnAccept(int fd, SocketAddress address, gcc_unused int uid)
{
/* the listener socket has become readable - a client has
connected */
#ifdef HAVE_LIBWRAP
if (address.sa_family != AF_UNIX) {
const auto hostaddr = sockaddr_to_string(&address,
address_length);
if (address.GetFamily() != AF_UNIX) {
const auto hostaddr = sockaddr_to_string(address);
// TODO: shall we obtain the program name from argv[0]?
const char *progname = "mpd";
@ -230,7 +229,6 @@ HttpdOutput::OnAccept(int fd, const sockaddr &address,
}
#else
(void)address;
(void)address_length;
#endif /* HAVE_WRAP */
const ScopeLock protect(mutex);

View File

@ -19,6 +19,7 @@
#include "config.h"
#include "net/Resolver.hxx"
#include "net/SocketAddress.hxx"
#include "util/Error.hxx"
#include "Log.hxx"
@ -50,7 +51,7 @@ int main(int argc, char **argv)
}
for (const struct addrinfo *i = ai; i != NULL; i = i->ai_next) {
const auto s = sockaddr_to_string(i->ai_addr, i->ai_addrlen);
const auto s = sockaddr_to_string({i->ai_addr, i->ai_addrlen});
printf("%s\n", s.c_str());
}