diff options
author | Max Kellermann <max@duempel.org> | 2015-02-10 20:30:10 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2015-02-10 22:06:06 +0100 |
commit | 4e30e747390ac6bb6a6e5ededfbcdae937579393 (patch) | |
tree | beb2d9acd687a62d476e796114efd8ea6b2f69ad /src/event | |
parent | 42890b9acf50c4406b61b3b37078c00ec79411fa (diff) |
net/SocketAddress: light wrapper for struct sockaddr
Diffstat (limited to 'src/event')
-rw-r--r-- | src/event/ServerSocket.cxx | 43 | ||||
-rw-r--r-- | src/event/ServerSocket.hxx | 7 |
2 files changed, 24 insertions, 26 deletions
diff --git a/src/event/ServerSocket.cxx b/src/event/ServerSocket.cxx index daf8517fc..d7a429f62 100644 --- a/src/event/ServerSocket.cxx +++ b/src/event/ServerSocket.cxx @@ -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; diff --git a/src/event/ServerSocket.hxx b/src/event/ServerSocket.hxx index 314889517..e5b7cffad 100644 --- a/src/event/ServerSocket.hxx +++ b/src/event/ServerSocket.hxx @@ -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 |