system/Resolver: sockaddr_to_string() returns std::string()
No GLib memory allocation.
This commit is contained in:
@@ -28,8 +28,6 @@
|
|||||||
#include "util/Error.hxx"
|
#include "util/Error.hxx"
|
||||||
#include "Log.hxx"
|
#include "Log.hxx"
|
||||||
|
|
||||||
#include <glib.h>
|
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
@@ -63,15 +61,12 @@ client_new(EventLoop &loop, Partition &partition,
|
|||||||
int fd, const struct sockaddr *sa, size_t sa_length, int uid)
|
int fd, const struct sockaddr *sa, size_t sa_length, int uid)
|
||||||
{
|
{
|
||||||
static unsigned int next_client_num;
|
static unsigned int next_client_num;
|
||||||
char *remote;
|
const auto remote = sockaddr_to_string(sa, sa_length);
|
||||||
|
|
||||||
assert(fd >= 0);
|
assert(fd >= 0);
|
||||||
|
|
||||||
#ifdef HAVE_LIBWRAP
|
#ifdef HAVE_LIBWRAP
|
||||||
if (sa->sa_family != AF_UNIX) {
|
if (sa->sa_family != AF_UNIX) {
|
||||||
char *hostaddr = sockaddr_to_string(sa, sa_length,
|
|
||||||
IgnoreError());
|
|
||||||
|
|
||||||
// TODO: shall we obtain the program name from argv[0]?
|
// TODO: shall we obtain the program name from argv[0]?
|
||||||
const char *progname = "mpd";
|
const char *progname = "mpd";
|
||||||
|
|
||||||
@@ -84,14 +79,11 @@ client_new(EventLoop &loop, Partition &partition,
|
|||||||
/* tcp wrappers says no */
|
/* tcp wrappers says no */
|
||||||
FormatWarning(client_domain,
|
FormatWarning(client_domain,
|
||||||
"libwrap refused connection (libwrap=%s) from %s",
|
"libwrap refused connection (libwrap=%s) from %s",
|
||||||
progname, hostaddr);
|
progname, remote.c_str());
|
||||||
|
|
||||||
g_free(hostaddr);
|
|
||||||
close_socket(fd);
|
close_socket(fd);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
g_free(hostaddr);
|
|
||||||
}
|
}
|
||||||
#endif /* HAVE_WRAP */
|
#endif /* HAVE_WRAP */
|
||||||
|
|
||||||
@@ -109,9 +101,8 @@ client_new(EventLoop &loop, Partition &partition,
|
|||||||
|
|
||||||
client_list.Add(*client);
|
client_list.Add(*client);
|
||||||
|
|
||||||
remote = sockaddr_to_string(sa, sa_length, IgnoreError());
|
FormatInfo(client_domain, "[%u] opened from %s",
|
||||||
FormatInfo(client_domain, "[%u] opened from %s", client->num, remote);
|
client->num, remote.c_str());
|
||||||
g_free(remote);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@@ -107,7 +107,10 @@ public:
|
|||||||
using SocketMonitor::IsDefined;
|
using SocketMonitor::IsDefined;
|
||||||
using SocketMonitor::Close;
|
using SocketMonitor::Close;
|
||||||
|
|
||||||
char *ToString() const;
|
gcc_pure
|
||||||
|
std::string ToString() const {
|
||||||
|
return sockaddr_to_string(address, address_length);
|
||||||
|
}
|
||||||
|
|
||||||
void SetFD(int _fd) {
|
void SetFD(int _fd) {
|
||||||
SocketMonitor::Open(_fd);
|
SocketMonitor::Open(_fd);
|
||||||
@@ -122,18 +125,6 @@ private:
|
|||||||
|
|
||||||
static constexpr Domain server_socket_domain("server_socket");
|
static constexpr Domain server_socket_domain("server_socket");
|
||||||
|
|
||||||
/**
|
|
||||||
* Wraper for sockaddr_to_string() which never fails.
|
|
||||||
*/
|
|
||||||
char *
|
|
||||||
OneServerSocket::ToString() const
|
|
||||||
{
|
|
||||||
char *p = sockaddr_to_string(address, address_length, IgnoreError());
|
|
||||||
if (p == nullptr)
|
|
||||||
p = g_strdup("[unknown]");
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
get_remote_uid(int fd)
|
get_remote_uid(int fd)
|
||||||
{
|
{
|
||||||
@@ -243,23 +234,21 @@ ServerSocket::Open(Error &error)
|
|||||||
Error error2;
|
Error error2;
|
||||||
if (!i.Open(error2)) {
|
if (!i.Open(error2)) {
|
||||||
if (good != nullptr && good->GetSerial() == i.GetSerial()) {
|
if (good != nullptr && good->GetSerial() == i.GetSerial()) {
|
||||||
char *address_string = i.ToString();
|
const auto address_string = i.ToString();
|
||||||
char *good_string = good->ToString();
|
const auto good_string = good->ToString();
|
||||||
FormatWarning(server_socket_domain,
|
FormatWarning(server_socket_domain,
|
||||||
"bind to '%s' failed: %s "
|
"bind to '%s' failed: %s "
|
||||||
"(continuing anyway, because "
|
"(continuing anyway, because "
|
||||||
"binding to '%s' succeeded)",
|
"binding to '%s' succeeded)",
|
||||||
address_string, error2.GetMessage(),
|
address_string.c_str(),
|
||||||
good_string);
|
error2.GetMessage(),
|
||||||
g_free(address_string);
|
good_string.c_str());
|
||||||
g_free(good_string);
|
|
||||||
} else if (bad == nullptr) {
|
} else if (bad == nullptr) {
|
||||||
bad = &i;
|
bad = &i;
|
||||||
|
|
||||||
char *address_string = i.ToString();
|
const auto address_string = i.ToString();
|
||||||
error2.FormatPrefix("Failed to bind to '%s': ",
|
error2.FormatPrefix("Failed to bind to '%s': ",
|
||||||
address_string);
|
address_string.c_str());
|
||||||
g_free(address_string);
|
|
||||||
|
|
||||||
last_error = std::move(error2);
|
last_error = std::move(error2);
|
||||||
}
|
}
|
||||||
|
@@ -33,8 +33,6 @@
|
|||||||
#include "util/Domain.hxx"
|
#include "util/Domain.hxx"
|
||||||
#include "Log.hxx"
|
#include "Log.hxx"
|
||||||
|
|
||||||
#include <glib.h>
|
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@@ -199,8 +197,8 @@ HttpdOutput::OnAccept(int fd, const sockaddr &address,
|
|||||||
|
|
||||||
#ifdef HAVE_LIBWRAP
|
#ifdef HAVE_LIBWRAP
|
||||||
if (address.sa_family != AF_UNIX) {
|
if (address.sa_family != AF_UNIX) {
|
||||||
char *hostaddr = sockaddr_to_string(&address, address_length,
|
const auto hostaddr = sockaddr_to_string(&address,
|
||||||
IgnoreError());
|
address_length);
|
||||||
// TODO: shall we obtain the program name from argv[0]?
|
// TODO: shall we obtain the program name from argv[0]?
|
||||||
const char *progname = "mpd";
|
const char *progname = "mpd";
|
||||||
|
|
||||||
@@ -213,13 +211,10 @@ HttpdOutput::OnAccept(int fd, const sockaddr &address,
|
|||||||
/* tcp wrappers says no */
|
/* tcp wrappers says no */
|
||||||
FormatWarning(httpd_output_domain,
|
FormatWarning(httpd_output_domain,
|
||||||
"libwrap refused connection (libwrap=%s) from %s",
|
"libwrap refused connection (libwrap=%s) from %s",
|
||||||
progname, hostaddr);
|
progname, hostaddr.c_str());
|
||||||
g_free(hostaddr);
|
|
||||||
close_socket(fd);
|
close_socket(fd);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
g_free(hostaddr);
|
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
(void)address;
|
(void)address;
|
||||||
|
@@ -44,17 +44,17 @@
|
|||||||
|
|
||||||
const Domain resolver_domain("resolver");
|
const Domain resolver_domain("resolver");
|
||||||
|
|
||||||
char *
|
std::string
|
||||||
sockaddr_to_string(const struct sockaddr *sa, size_t length, Error &error)
|
sockaddr_to_string(const struct sockaddr *sa, size_t length)
|
||||||
{
|
{
|
||||||
#ifdef HAVE_UN
|
#ifdef HAVE_UN
|
||||||
if (sa->sa_family == AF_UNIX) {
|
if (sa->sa_family == AF_UNIX) {
|
||||||
/* return path of UNIX domain sockets */
|
/* return path of UNIX domain sockets */
|
||||||
const sockaddr_un &s_un = *(const sockaddr_un *)sa;
|
const sockaddr_un &s_un = *(const sockaddr_un *)sa;
|
||||||
if (length < sizeof(s_un) || s_un.sun_path[0] == 0)
|
if (length < sizeof(s_un) || s_un.sun_path[0] == 0)
|
||||||
return g_strdup("local");
|
return "local";
|
||||||
|
|
||||||
return g_strdup(s_un.sun_path);
|
return s_un.sun_path;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -83,17 +83,23 @@ sockaddr_to_string(const struct sockaddr *sa, size_t length, Error &error)
|
|||||||
|
|
||||||
ret = getnameinfo(sa, length, host, sizeof(host), serv, sizeof(serv),
|
ret = getnameinfo(sa, length, host, sizeof(host), serv, sizeof(serv),
|
||||||
NI_NUMERICHOST|NI_NUMERICSERV);
|
NI_NUMERICHOST|NI_NUMERICSERV);
|
||||||
if (ret != 0) {
|
if (ret != 0)
|
||||||
error.Set(resolver_domain, ret, gai_strerror(ret));
|
return "unknown";
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef HAVE_IPV6
|
#ifdef HAVE_IPV6
|
||||||
if (strchr(host, ':') != NULL)
|
if (strchr(host, ':') != NULL) {
|
||||||
return g_strconcat("[", host, "]:", serv, NULL);
|
std::string result("[");
|
||||||
|
result.append(host);
|
||||||
|
result.append("]:");
|
||||||
|
result.append(serv);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return g_strconcat(host, ":", serv, NULL);
|
std::string result(host);
|
||||||
|
result.push_back(':');
|
||||||
|
result.append(serv);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct addrinfo *
|
struct addrinfo *
|
||||||
|
@@ -22,6 +22,8 @@
|
|||||||
|
|
||||||
#include "Compiler.h"
|
#include "Compiler.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
struct sockaddr;
|
struct sockaddr;
|
||||||
@@ -33,17 +35,14 @@ extern const Domain resolver_domain;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts the specified socket address into a string in the form
|
* Converts the specified socket address into a string in the form
|
||||||
* "IP:PORT". The return value must be freed with g_free() when you
|
* "IP:PORT".
|
||||||
* don't need it anymore.
|
|
||||||
*
|
*
|
||||||
* @param sa the sockaddr struct
|
* @param sa the sockaddr struct
|
||||||
* @param length the length of #sa in bytes
|
* @param length the length of #sa in bytes
|
||||||
* @param error location to store the error occurring, or NULL to
|
|
||||||
* ignore errors
|
|
||||||
*/
|
*/
|
||||||
gcc_malloc
|
gcc_pure
|
||||||
char *
|
std::string
|
||||||
sockaddr_to_string(const sockaddr *sa, size_t length, Error &error);
|
sockaddr_to_string(const sockaddr *sa, size_t length);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolve a specification in the form "host", "host:port",
|
* Resolve a specification in the form "host", "host:port",
|
||||||
|
@@ -51,16 +51,8 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const struct addrinfo *i = ai; i != NULL; i = i->ai_next) {
|
for (const struct addrinfo *i = ai; i != NULL; i = i->ai_next) {
|
||||||
char *p = sockaddr_to_string(i->ai_addr, i->ai_addrlen,
|
const auto s = sockaddr_to_string(i->ai_addr, i->ai_addrlen);
|
||||||
error);
|
g_print("%s\n", s.c_str());
|
||||||
if (p == NULL) {
|
|
||||||
freeaddrinfo(ai);
|
|
||||||
LogError(error);
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
g_print("%s\n", p);
|
|
||||||
g_free(p);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
freeaddrinfo(ai);
|
freeaddrinfo(ai);
|
||||||
|
Reference in New Issue
Block a user