net/SocketAddress: add method GetLocalRaw()
This commit is contained in:
parent
4c6ae4e9e8
commit
11396d4fba
@ -29,9 +29,14 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "SocketAddress.hxx"
|
#include "SocketAddress.hxx"
|
||||||
|
#include "util/StringView.hxx"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#ifdef HAVE_UN
|
||||||
|
#include <sys/un.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef HAVE_TCP
|
#ifdef HAVE_TCP
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
@ -46,6 +51,28 @@ SocketAddress::operator==(SocketAddress other) const noexcept
|
|||||||
return size == other.size && memcmp(address, other.address, size) == 0;
|
return size == other.size && memcmp(address, other.address, size) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef HAVE_UN
|
||||||
|
|
||||||
|
StringView
|
||||||
|
SocketAddress::GetLocalRaw() const noexcept
|
||||||
|
{
|
||||||
|
if (IsNull() || GetFamily() != AF_LOCAL)
|
||||||
|
/* not applicable */
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
const auto sun = (const struct sockaddr_un *)GetAddress();
|
||||||
|
const auto start = (const char *)sun;
|
||||||
|
const auto path = sun->sun_path;
|
||||||
|
const size_t header_size = path - start;
|
||||||
|
if (size < header_size)
|
||||||
|
/* malformed address */
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
return {path, size - header_size};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef HAVE_TCP
|
#ifdef HAVE_TCP
|
||||||
|
|
||||||
bool
|
bool
|
||||||
|
@ -41,6 +41,8 @@
|
|||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
struct StringView;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An OO wrapper for struct sockaddr.
|
* An OO wrapper for struct sockaddr.
|
||||||
*/
|
*/
|
||||||
@ -94,6 +96,17 @@ public:
|
|||||||
return GetFamily() != AF_UNSPEC;
|
return GetFamily() != AF_UNSPEC;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef HAVE_UN
|
||||||
|
/**
|
||||||
|
* Extract the local socket path (which may begin with a null
|
||||||
|
* byte, denoting an "abstract" socket). The return value's
|
||||||
|
* "size" attribute includes the null terminator. Returns
|
||||||
|
* nullptr if not applicable.
|
||||||
|
*/
|
||||||
|
gcc_pure
|
||||||
|
StringView GetLocalRaw() const noexcept;
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef HAVE_TCP
|
#ifdef HAVE_TCP
|
||||||
/**
|
/**
|
||||||
* Is this the IPv6 wildcard address (in6addr_any)?
|
* Is this the IPv6 wildcard address (in6addr_any)?
|
||||||
|
Loading…
Reference in New Issue
Block a user