aboutsummaryrefslogtreecommitdiffstats
path: root/src/ServerSocket.cxx
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/ServerSocket.cxx (renamed from src/server_socket.c)323
1 files changed, 162 insertions, 161 deletions
diff --git a/src/server_socket.c b/src/ServerSocket.cxx
index 396399596..7f14168a8 100644
--- a/src/server_socket.c
+++ b/src/ServerSocket.cxx
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2003-2011 The Music Player Daemon Project
+ * Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
@@ -23,17 +23,19 @@
#define _GNU_SOURCE 1
#endif
-#include "server_socket.h"
-#include "socket_util.h"
+#include "ServerSocket.hxx"
+#include "SocketUtil.hxx"
+#include "SocketError.hxx"
+#include "event/SocketMonitor.hxx"
#include "resolver.h"
#include "fd_util.h"
-#include "glib_socket.h"
+
+#include <forward_list>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
-#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
@@ -53,27 +55,72 @@
#define DEFAULT_PORT 6600
-struct one_socket {
- struct one_socket *next;
- struct server_socket *parent;
-
- unsigned serial;
+struct OneServerSocket final : private SocketMonitor {
+ const server_socket &parent;
- int fd;
- guint source_id;
+ const unsigned serial;
char *path;
size_t address_length;
- struct sockaddr address;
+ struct sockaddr *address;
+
+ OneServerSocket(EventLoop &_loop, const server_socket &_parent,
+ unsigned _serial,
+ const struct sockaddr *_address,
+ size_t _address_length)
+ :SocketMonitor(_loop),
+ parent(_parent), serial(_serial),
+ path(nullptr),
+ address_length(_address_length),
+ address((sockaddr *)g_memdup(_address, _address_length))
+ {
+ assert(_address != nullptr);
+ assert(_address_length > 0);
+ }
+
+ OneServerSocket(const OneServerSocket &other) = delete;
+ OneServerSocket &operator=(const OneServerSocket &other) = delete;
+
+ ~OneServerSocket() {
+ g_free(path);
+ g_free(address);
+ }
+
+ bool Open(GError **error_r);
+
+ using SocketMonitor::Close;
+
+ char *ToString() const;
+
+ void SetFD(int _fd) {
+ SocketMonitor::Open(_fd);
+ SocketMonitor::ScheduleRead();
+ }
+
+ void Accept();
+
+private:
+ virtual void OnSocketReady(unsigned flags) override;
};
struct server_socket {
+ EventLoop &loop;
+
server_socket_callback_t callback;
void *callback_ctx;
- struct one_socket *sockets, **sockets_tail_r;
+ std::forward_list<OneServerSocket> sockets;
+
unsigned next_serial;
+
+ server_socket(EventLoop &_loop,
+ server_socket_callback_t _callback, void *_callback_ctx)
+ :loop(_loop),
+ callback(_callback), callback_ctx(_callback_ctx),
+ next_serial(1) {}
+
+ void Close();
};
static GQuark
@@ -83,43 +130,26 @@ server_socket_quark(void)
}
struct server_socket *
-server_socket_new(server_socket_callback_t callback, void *callback_ctx)
+server_socket_new(EventLoop &loop,
+ server_socket_callback_t callback, void *callback_ctx)
{
- struct server_socket *ss = g_new(struct server_socket, 1);
- ss->callback = callback;
- ss->callback_ctx = callback_ctx;
- ss->sockets = NULL;
- ss->sockets_tail_r = &ss->sockets;
- ss->next_serial = 1;
- return ss;
+ return new server_socket(loop, callback, callback_ctx);
}
void
server_socket_free(struct server_socket *ss)
{
- server_socket_close(ss);
-
- while (ss->sockets != NULL) {
- struct one_socket *s = ss->sockets;
- ss->sockets = s->next;
-
- assert(s->fd < 0);
-
- g_free(s->path);
- g_free(s);
- }
-
- g_free(ss);
+ delete ss;
}
/**
* Wraper for sockaddr_to_string() which never fails.
*/
-static char *
-one_socket_to_string(const struct one_socket *s)
+char *
+OneServerSocket::ToString() const
{
- char *p = sockaddr_to_string(&s->address, s->address_length, NULL);
- if (p == NULL)
+ char *p = sockaddr_to_string(address, address_length, nullptr);
+ if (p == nullptr)
p = g_strdup("[unknown]");
return p;
}
@@ -149,72 +179,83 @@ get_remote_uid(int fd)
#endif
}
-static gboolean
-server_socket_in_event(G_GNUC_UNUSED GIOChannel *source,
- G_GNUC_UNUSED GIOCondition condition,
- gpointer data)
+inline void
+OneServerSocket::Accept()
{
- struct one_socket *s = data;
+ struct sockaddr_storage peer_address;
+ size_t peer_address_length = sizeof(peer_address);
+ int peer_fd =
+ accept_cloexec_nonblock(Get(), (struct sockaddr*)&peer_address,
+ &peer_address_length);
+ if (peer_fd < 0) {
+ const SocketErrorMessage msg;
+ g_warning("accept() failed: %s", (const char *)msg);
+ return;
+ }
- struct sockaddr_storage address;
- size_t address_length = sizeof(address);
- int fd = accept_cloexec_nonblock(s->fd, (struct sockaddr*)&address,
- &address_length);
- if (fd >= 0) {
- if (socket_keepalive(fd))
- g_warning("Could not set TCP keepalive option: %s",
- g_strerror(errno));
- s->parent->callback(fd, (const struct sockaddr*)&address,
- address_length, get_remote_uid(fd),
- s->parent->callback_ctx);
- } else {
- g_warning("accept() failed: %s", g_strerror(errno));
+ if (socket_keepalive(peer_fd)) {
+ const SocketErrorMessage msg;
+ g_warning("Could not set TCP keepalive option: %s",
+ (const char *)msg);
}
- return true;
+ parent.callback(peer_fd,
+ (const struct sockaddr*)&peer_address,
+ peer_address_length, get_remote_uid(peer_fd),
+ parent.callback_ctx);
}
-static void
-set_fd(struct one_socket *s, int fd)
+void
+OneServerSocket::OnSocketReady(gcc_unused unsigned flags)
{
- assert(s != NULL);
- assert(s->fd < 0);
- assert(fd >= 0);
+ Accept();
+}
+
+inline bool
+OneServerSocket::Open(GError **error_r)
+{
+ assert(!IsDefined());
- s->fd = fd;
+ int _fd = socket_bind_listen(address->sa_family,
+ SOCK_STREAM, 0,
+ address, address_length, 5,
+ error_r);
+ if (_fd < 0)
+ return false;
+
+ /* allow everybody to connect */
+
+ if (path != nullptr)
+ chmod(path, 0666);
+
+ /* register in the GLib main loop */
- GIOChannel *channel = g_io_channel_new_socket(s->fd);
- s->source_id = g_io_add_watch(channel, G_IO_IN,
- server_socket_in_event, s);
- g_io_channel_unref(channel);
+ SetFD(_fd);
+
+ return true;
}
bool
server_socket_open(struct server_socket *ss, GError **error_r)
{
- struct one_socket *good = NULL, *bad = NULL;
- GError *last_error = NULL;
+ struct OneServerSocket *good = nullptr, *bad = nullptr;
+ GError *last_error = nullptr;
- for (struct one_socket *s = ss->sockets; s != NULL; s = s->next) {
- assert(s->serial > 0);
- assert(good == NULL || s->serial >= good->serial);
- assert(s->fd < 0);
+ for (auto &i : ss->sockets) {
+ assert(i.serial > 0);
+ assert(good == nullptr || i.serial <= good->serial);
- if (bad != NULL && s->serial != bad->serial) {
+ if (bad != nullptr && i.serial != bad->serial) {
server_socket_close(ss);
g_propagate_error(error_r, last_error);
return false;
}
- GError *error = NULL;
- int fd = socket_bind_listen(s->address.sa_family,
- SOCK_STREAM, 0,
- &s->address, s->address_length, 5,
- &error);
- if (fd < 0) {
- if (good != NULL && good->serial == s->serial) {
- char *address_string = one_socket_to_string(s);
- char *good_string = one_socket_to_string(good);
+ GError *error = nullptr;
+ if (!i.Open(&error)) {
+ if (good != nullptr && good->serial == i.serial) {
+ char *address_string = i.ToString();
+ char *good_string = good->ToString();
g_warning("bind to '%s' failed: %s "
"(continuing anyway, because "
"binding to '%s' succeeded)",
@@ -223,10 +264,10 @@ server_socket_open(struct server_socket *ss, GError **error_r)
g_free(address_string);
g_free(good_string);
g_error_free(error);
- } else if (bad == NULL) {
- bad = s;
+ } else if (bad == nullptr) {
+ bad = &i;
- char *address_string = one_socket_to_string(s);
+ char *address_string = i.ToString();
g_propagate_prefixed_error(&last_error, error,
"Failed to bind to '%s': ",
address_string);
@@ -236,28 +277,19 @@ server_socket_open(struct server_socket *ss, GError **error_r)
continue;
}
- /* allow everybody to connect */
-
- if (s->path != NULL)
- chmod(s->path, 0666);
-
- /* register in the GLib main loop */
-
- set_fd(s, fd);
-
/* mark this socket as "good", and clear previous
errors */
- good = s;
+ good = &i;
- if (bad != NULL) {
- bad = NULL;
+ if (bad != nullptr) {
+ bad = nullptr;
g_error_free(last_error);
- last_error = NULL;
+ last_error = nullptr;
}
}
- if (bad != NULL) {
+ if (bad != nullptr) {
server_socket_close(ss);
g_propagate_error(error_r, last_error);
return false;
@@ -267,85 +299,54 @@ server_socket_open(struct server_socket *ss, GError **error_r)
}
void
-server_socket_close(struct server_socket *ss)
+server_socket::Close()
{
- for (struct one_socket *s = ss->sockets; s != NULL; s = s->next) {
- if (s->fd < 0)
- continue;
+ for (auto &i : sockets)
+ i.Close();
+}
- g_source_remove(s->source_id);
- close_socket(s->fd);
- s->fd = -1;
- }
+void
+server_socket_close(struct server_socket *ss)
+{
+ ss->Close();
}
-static struct one_socket *
-one_socket_new(unsigned serial, const struct sockaddr *address,
- size_t address_length)
+static OneServerSocket &
+server_socket_add_address(struct server_socket *ss,
+ const struct sockaddr *address,
+ size_t address_length)
{
- assert(address != NULL);
- assert(address_length > 0);
-
- struct one_socket *s = g_malloc(sizeof(*s) - sizeof(s->address) +
- address_length);
- s->next = NULL;
- s->serial = serial;
- s->fd = -1;
- s->path = NULL;
- s->address_length = address_length;
- memcpy(&s->address, address, address_length);
-
- return s;
+ assert(ss != nullptr);
+
+ ss->sockets.emplace_front(ss->loop, *ss, ss->next_serial,
+ address, address_length);
+
+ return ss->sockets.front();
}
bool
server_socket_add_fd(struct server_socket *ss, int fd, GError **error_r)
{
- assert(ss != NULL);
- assert(ss->sockets_tail_r != NULL);
- assert(*ss->sockets_tail_r == NULL);
+ assert(ss != nullptr);
assert(fd >= 0);
struct sockaddr_storage address;
socklen_t address_length;
if (getsockname(fd, (struct sockaddr *)&address,
&address_length) < 0) {
- g_set_error(error_r, server_socket_quark(), errno,
- "Failed to get socket address: %s",
- g_strerror(errno));
+ SetSocketError(error_r);
+ g_prefix_error(error_r, "Failed to get socket address");
return false;
}
- struct one_socket *s = one_socket_new(ss->next_serial,
- (struct sockaddr *)&address,
- address_length);
- s->parent = ss;
- *ss->sockets_tail_r = s;
- ss->sockets_tail_r = &s->next;
-
- set_fd(s, fd);
+ OneServerSocket &s =
+ server_socket_add_address(ss, (struct sockaddr *)&address,
+ address_length);
+ s.SetFD(fd);
return true;
}
-static struct one_socket *
-server_socket_add_address(struct server_socket *ss,
- const struct sockaddr *address,
- size_t address_length)
-{
- assert(ss != NULL);
- assert(ss->sockets_tail_r != NULL);
- assert(*ss->sockets_tail_r == NULL);
-
- struct one_socket *s = one_socket_new(ss->next_serial,
- address, address_length);
- s->parent = ss;
- *ss->sockets_tail_r = s;
- ss->sockets_tail_r = &s->next;
-
- return s;
-}
-
#ifdef HAVE_TCP
/**
@@ -424,10 +425,10 @@ server_socket_add_host(struct server_socket *ss, const char *hostname,
struct addrinfo *ai = resolve_host_port(hostname, port,
AI_PASSIVE, SOCK_STREAM,
error_r);
- if (ai == NULL)
+ if (ai == nullptr)
return false;
- for (const struct addrinfo *i = ai; i != NULL; i = i->ai_next)
+ for (const struct addrinfo *i = ai; i != nullptr; i = i->ai_next)
server_socket_add_address(ss, i->ai_addr, i->ai_addrlen);
freeaddrinfo(ai);
@@ -465,10 +466,10 @@ server_socket_add_path(struct server_socket *ss, const char *path,
s_un.sun_family = AF_UNIX;
memcpy(s_un.sun_path, path, path_length + 1);
- struct one_socket *s =
+ OneServerSocket &s =
server_socket_add_address(ss, (const struct sockaddr *)&s_un,
sizeof(s_un));
- s->path = g_strdup(path);
+ s.path = g_strdup(path);
return true;
#else /* !HAVE_UN */