Commit bc66dc45 authored by Max Kellermann's avatar Max Kellermann

server_socket: convert to C++

parent 70879356
...@@ -88,7 +88,6 @@ mpd_headers = \ ...@@ -88,7 +88,6 @@ mpd_headers = \
src/despotify_utils.h \ src/despotify_utils.h \
src/text_input_stream.h \ src/text_input_stream.h \
src/icy_server.h \ src/icy_server.h \
src/server_socket.h \
src/ls.h \ src/ls.h \
src/mixer_api.h \ src/mixer_api.h \
src/mixer_control.h \ src/mixer_control.h \
...@@ -233,7 +232,7 @@ src_mpd_SOURCES = \ ...@@ -233,7 +232,7 @@ src_mpd_SOURCES = \
src/ClientMessage.cxx src/ClientMessage.hxx \ src/ClientMessage.cxx src/ClientMessage.hxx \
src/ClientSubscribe.cxx src/ClientSubscribe.hxx \ src/ClientSubscribe.cxx src/ClientSubscribe.hxx \
src/ClientFile.cxx src/ClientFile.hxx \ src/ClientFile.cxx src/ClientFile.hxx \
src/server_socket.c \ src/ServerSocket.cxx src/ServerSocket.hxx \
src/Listen.cxx src/Listen.hxx \ src/Listen.cxx src/Listen.hxx \
src/Log.cxx src/Log.hxx \ src/Log.cxx src/Log.hxx \
src/ls.cxx \ src/ls.cxx \
...@@ -1301,7 +1300,7 @@ test_run_output_SOURCES = test/run_output.cxx \ ...@@ -1301,7 +1300,7 @@ test_run_output_SOURCES = test/run_output.cxx \
src/AudioCompress/compress.c \ src/AudioCompress/compress.c \
src/ReplayGainInfo.cxx \ src/ReplayGainInfo.cxx \
src/fd_util.c \ src/fd_util.c \
src/server_socket.c src/ServerSocket.cxx
test_read_mixer_LDADD = \ test_read_mixer_LDADD = \
libpcm.a \ libpcm.a \
......
...@@ -22,10 +22,7 @@ ...@@ -22,10 +22,7 @@
#include "Main.hxx" #include "Main.hxx"
#include "Client.hxx" #include "Client.hxx"
#include "conf.h" #include "conf.h"
#include "ServerSocket.hxx"
extern "C" {
#include "server_socket.h"
}
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
......
/* /*
* Copyright (C) 2003-2011 The Music Player Daemon Project * Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org * http://www.musicpd.org
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
...@@ -23,12 +23,14 @@ ...@@ -23,12 +23,14 @@
#define _GNU_SOURCE 1 #define _GNU_SOURCE 1
#endif #endif
#include "server_socket.h" #include "ServerSocket.hxx"
#include "socket_util.h" #include "socket_util.h"
#include "resolver.h" #include "resolver.h"
#include "fd_util.h" #include "fd_util.h"
#include "glib_socket.h" #include "glib_socket.h"
#include <forward_list>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
...@@ -53,11 +55,10 @@ ...@@ -53,11 +55,10 @@
#define DEFAULT_PORT 6600 #define DEFAULT_PORT 6600
struct one_socket { struct OneServerSocket {
struct one_socket *next; const server_socket &parent;
struct server_socket *parent;
unsigned serial; const unsigned serial;
int fd; int fd;
guint source_id; guint source_id;
...@@ -65,15 +66,52 @@ struct one_socket { ...@@ -65,15 +66,52 @@ struct one_socket {
char *path; char *path;
size_t address_length; size_t address_length;
struct sockaddr address; struct sockaddr *address;
OneServerSocket(const server_socket &_parent, unsigned _serial,
const struct sockaddr *_address,
size_t _address_length)
:parent(_parent), serial(_serial),
fd(-1),
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() {
Close();
g_free(path);
g_free(address);
}
bool Open(GError **error_r);
void Close();
char *ToString() const;
void SetFD(int fd);
}; };
struct server_socket { struct server_socket {
server_socket_callback_t callback; server_socket_callback_t callback;
void *callback_ctx; void *callback_ctx;
struct one_socket *sockets, **sockets_tail_r; std::forward_list<OneServerSocket> sockets;
unsigned next_serial; unsigned next_serial;
server_socket(server_socket_callback_t _callback, void *_callback_ctx)
:callback(_callback), callback_ctx(_callback_ctx),
next_serial(1) {}
void Close();
}; };
static GQuark static GQuark
...@@ -85,41 +123,23 @@ server_socket_quark(void) ...@@ -85,41 +123,23 @@ server_socket_quark(void)
struct server_socket * struct server_socket *
server_socket_new(server_socket_callback_t callback, void *callback_ctx) server_socket_new(server_socket_callback_t callback, void *callback_ctx)
{ {
struct server_socket *ss = g_new(struct server_socket, 1); return new server_socket(callback, callback_ctx);
ss->callback = callback;
ss->callback_ctx = callback_ctx;
ss->sockets = NULL;
ss->sockets_tail_r = &ss->sockets;
ss->next_serial = 1;
return ss;
} }
void void
server_socket_free(struct server_socket *ss) server_socket_free(struct server_socket *ss)
{ {
server_socket_close(ss); delete 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);
} }
/** /**
* Wraper for sockaddr_to_string() which never fails. * Wraper for sockaddr_to_string() which never fails.
*/ */
static char * char *
one_socket_to_string(const struct one_socket *s) OneServerSocket::ToString() const
{ {
char *p = sockaddr_to_string(&s->address, s->address_length, NULL); char *p = sockaddr_to_string(address, address_length, nullptr);
if (p == NULL) if (p == nullptr)
p = g_strdup("[unknown]"); p = g_strdup("[unknown]");
return p; return p;
} }
...@@ -154,7 +174,7 @@ server_socket_in_event(G_GNUC_UNUSED GIOChannel *source, ...@@ -154,7 +174,7 @@ server_socket_in_event(G_GNUC_UNUSED GIOChannel *source,
G_GNUC_UNUSED GIOCondition condition, G_GNUC_UNUSED GIOCondition condition,
gpointer data) gpointer data)
{ {
struct one_socket *s = data; OneServerSocket *s = (OneServerSocket *)data;
struct sockaddr_storage address; struct sockaddr_storage address;
size_t address_length = sizeof(address); size_t address_length = sizeof(address);
...@@ -164,9 +184,9 @@ server_socket_in_event(G_GNUC_UNUSED GIOChannel *source, ...@@ -164,9 +184,9 @@ server_socket_in_event(G_GNUC_UNUSED GIOChannel *source,
if (socket_keepalive(fd)) if (socket_keepalive(fd))
g_warning("Could not set TCP keepalive option: %s", g_warning("Could not set TCP keepalive option: %s",
g_strerror(errno)); g_strerror(errno));
s->parent->callback(fd, (const struct sockaddr*)&address, s->parent.callback(fd, (const struct sockaddr*)&address,
address_length, get_remote_uid(fd), address_length, get_remote_uid(fd),
s->parent->callback_ctx); s->parent.callback_ctx);
} else { } else {
g_warning("accept() failed: %s", g_strerror(errno)); g_warning("accept() failed: %s", g_strerror(errno));
} }
...@@ -174,47 +194,65 @@ server_socket_in_event(G_GNUC_UNUSED GIOChannel *source, ...@@ -174,47 +194,65 @@ server_socket_in_event(G_GNUC_UNUSED GIOChannel *source,
return true; return true;
} }
static void void
set_fd(struct one_socket *s, int fd) OneServerSocket::SetFD(int _fd)
{ {
assert(s != NULL); assert(fd < 0);
assert(s->fd < 0); assert(_fd >= 0);
assert(fd >= 0);
s->fd = fd; fd = _fd;
GIOChannel *channel = g_io_channel_new_socket(s->fd); GIOChannel *channel = g_io_channel_new_socket(fd);
s->source_id = g_io_add_watch(channel, G_IO_IN, source_id = g_io_add_watch(channel, G_IO_IN,
server_socket_in_event, s); server_socket_in_event, this);
g_io_channel_unref(channel); g_io_channel_unref(channel);
} }
inline bool
OneServerSocket::Open(GError **error_r)
{
assert(fd < 0);
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 */
SetFD(_fd);
return true;
}
bool bool
server_socket_open(struct server_socket *ss, GError **error_r) server_socket_open(struct server_socket *ss, GError **error_r)
{ {
struct one_socket *good = NULL, *bad = NULL; struct OneServerSocket *good = nullptr, *bad = nullptr;
GError *last_error = NULL; GError *last_error = nullptr;
for (struct one_socket *s = ss->sockets; s != NULL; s = s->next) { for (auto &i : ss->sockets) {
assert(s->serial > 0); assert(i.serial > 0);
assert(good == NULL || s->serial >= good->serial); assert(good == nullptr || i.serial <= good->serial);
assert(s->fd < 0);
if (bad != NULL && s->serial != bad->serial) { if (bad != nullptr && i.serial != bad->serial) {
server_socket_close(ss); server_socket_close(ss);
g_propagate_error(error_r, last_error); g_propagate_error(error_r, last_error);
return false; return false;
} }
GError *error = NULL; GError *error = nullptr;
int fd = socket_bind_listen(s->address.sa_family, if (!i.Open(&error)) {
SOCK_STREAM, 0, if (good != nullptr && good->serial == i.serial) {
&s->address, s->address_length, 5, char *address_string = i.ToString();
&error); char *good_string = good->ToString();
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);
g_warning("bind to '%s' failed: %s " g_warning("bind to '%s' failed: %s "
"(continuing anyway, because " "(continuing anyway, because "
"binding to '%s' succeeded)", "binding to '%s' succeeded)",
...@@ -223,10 +261,10 @@ server_socket_open(struct server_socket *ss, GError **error_r) ...@@ -223,10 +261,10 @@ server_socket_open(struct server_socket *ss, GError **error_r)
g_free(address_string); g_free(address_string);
g_free(good_string); g_free(good_string);
g_error_free(error); g_error_free(error);
} else if (bad == NULL) { } else if (bad == nullptr) {
bad = s; bad = &i;
char *address_string = one_socket_to_string(s); char *address_string = i.ToString();
g_propagate_prefixed_error(&last_error, error, g_propagate_prefixed_error(&last_error, error,
"Failed to bind to '%s': ", "Failed to bind to '%s': ",
address_string); address_string);
...@@ -236,28 +274,19 @@ server_socket_open(struct server_socket *ss, GError **error_r) ...@@ -236,28 +274,19 @@ server_socket_open(struct server_socket *ss, GError **error_r)
continue; 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 /* mark this socket as "good", and clear previous
errors */ errors */
good = s; good = &i;
if (bad != NULL) { if (bad != nullptr) {
bad = NULL; bad = nullptr;
g_error_free(last_error); g_error_free(last_error);
last_error = NULL; last_error = nullptr;
} }
} }
if (bad != NULL) { if (bad != nullptr) {
server_socket_close(ss); server_socket_close(ss);
g_propagate_error(error_r, last_error); g_propagate_error(error_r, last_error);
return false; return false;
...@@ -267,43 +296,46 @@ server_socket_open(struct server_socket *ss, GError **error_r) ...@@ -267,43 +296,46 @@ server_socket_open(struct server_socket *ss, GError **error_r)
} }
void void
server_socket_close(struct server_socket *ss) OneServerSocket::Close()
{ {
for (struct one_socket *s = ss->sockets; s != NULL; s = s->next) { if (fd < 0)
if (s->fd < 0) return;
continue;
g_source_remove(s->source_id); g_source_remove(source_id);
close_socket(s->fd); close_socket(fd);
s->fd = -1; fd = -1;
} }
void
server_socket::Close()
{
for (auto &i : sockets)
i.Close();
}
void
server_socket_close(struct server_socket *ss)
{
ss->Close();
} }
static struct one_socket * static OneServerSocket &
one_socket_new(unsigned serial, const struct sockaddr *address, server_socket_add_address(struct server_socket *ss,
size_t address_length) const struct sockaddr *address,
size_t address_length)
{ {
assert(address != NULL); assert(ss != nullptr);
assert(address_length > 0);
ss->sockets.emplace_front(*ss, ss->next_serial,
struct one_socket *s = g_malloc(sizeof(*s) - sizeof(s->address) + address, address_length);
address_length);
s->next = NULL; return ss->sockets.front();
s->serial = serial;
s->fd = -1;
s->path = NULL;
s->address_length = address_length;
memcpy(&s->address, address, address_length);
return s;
} }
bool bool
server_socket_add_fd(struct server_socket *ss, int fd, GError **error_r) server_socket_add_fd(struct server_socket *ss, int fd, GError **error_r)
{ {
assert(ss != NULL); assert(ss != nullptr);
assert(ss->sockets_tail_r != NULL);
assert(*ss->sockets_tail_r == NULL);
assert(fd >= 0); assert(fd >= 0);
struct sockaddr_storage address; struct sockaddr_storage address;
...@@ -316,36 +348,14 @@ server_socket_add_fd(struct server_socket *ss, int fd, GError **error_r) ...@@ -316,36 +348,14 @@ server_socket_add_fd(struct server_socket *ss, int fd, GError **error_r)
return false; return false;
} }
struct one_socket *s = one_socket_new(ss->next_serial, OneServerSocket &s =
(struct sockaddr *)&address, server_socket_add_address(ss, (struct sockaddr *)&address,
address_length); address_length);
s->parent = ss; s.SetFD(fd);
*ss->sockets_tail_r = s;
ss->sockets_tail_r = &s->next;
set_fd(s, fd);
return true; 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 #ifdef HAVE_TCP
/** /**
...@@ -424,10 +434,10 @@ server_socket_add_host(struct server_socket *ss, const char *hostname, ...@@ -424,10 +434,10 @@ server_socket_add_host(struct server_socket *ss, const char *hostname,
struct addrinfo *ai = resolve_host_port(hostname, port, struct addrinfo *ai = resolve_host_port(hostname, port,
AI_PASSIVE, SOCK_STREAM, AI_PASSIVE, SOCK_STREAM,
error_r); error_r);
if (ai == NULL) if (ai == nullptr)
return false; 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); server_socket_add_address(ss, i->ai_addr, i->ai_addrlen);
freeaddrinfo(ai); freeaddrinfo(ai);
...@@ -465,10 +475,10 @@ server_socket_add_path(struct server_socket *ss, const char *path, ...@@ -465,10 +475,10 @@ server_socket_add_path(struct server_socket *ss, const char *path,
s_un.sun_family = AF_UNIX; s_un.sun_family = AF_UNIX;
memcpy(s_un.sun_path, path, path_length + 1); 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, server_socket_add_address(ss, (const struct sockaddr *)&s_un,
sizeof(s_un)); sizeof(s_un));
s->path = g_strdup(path); s.path = g_strdup(path);
return true; return true;
#else /* !HAVE_UN */ #else /* !HAVE_UN */
......
/* /*
* Copyright (C) 2003-2011 The Music Player Daemon Project * Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org * http://www.musicpd.org
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
...@@ -17,12 +17,11 @@ ...@@ -17,12 +17,11 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/ */
#ifndef MPD_SERVER_SOCKET_H #ifndef MPD_SERVER_SOCKET_HXX
#define MPD_SERVER_SOCKET_H #define MPD_SERVER_SOCKET_HXX
#include "gerror.h" #include "gerror.h"
#include <stdbool.h>
#include <stddef.h> #include <stddef.h>
struct sockaddr; struct sockaddr;
...@@ -32,10 +31,6 @@ typedef void (*server_socket_callback_t)(int fd, ...@@ -32,10 +31,6 @@ typedef void (*server_socket_callback_t)(int fd,
size_t address_length, int uid, size_t address_length, int uid,
void *ctx); void *ctx);
#ifdef __cplusplus
extern "C" {
#endif
struct server_socket * struct server_socket *
server_socket_new(server_socket_callback_t callback, void *callback_ctx); server_socket_new(server_socket_callback_t callback, void *callback_ctx);
...@@ -94,8 +89,4 @@ bool ...@@ -94,8 +89,4 @@ bool
server_socket_add_path(struct server_socket *ss, const char *path, server_socket_add_path(struct server_socket *ss, const char *path,
GError **error_r); GError **error_r);
#ifdef __cplusplus
}
#endif
#endif #endif
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
#include "page.h" #include "page.h"
#include "icy_server.h" #include "icy_server.h"
#include "fd_util.h" #include "fd_util.h"
#include "server_socket.h" #include "ServerSocket.hxx"
#include <assert.h> #include <assert.h>
......
...@@ -32,6 +32,10 @@ ...@@ -32,6 +32,10 @@
struct sockaddr; struct sockaddr;
#ifdef __cplusplus
extern "C" {
#endif
/** /**
* Creates a socket listening on the specified address. This is a * Creates a socket listening on the specified address. This is a
* shortcut for socket(), bind() and listen(). * shortcut for socket(), bind() and listen().
...@@ -55,4 +59,8 @@ socket_bind_listen(int domain, int type, int protocol, ...@@ -55,4 +59,8 @@ socket_bind_listen(int domain, int type, int protocol,
int int
socket_keepalive(int fd); socket_keepalive(int fd);
#ifdef __cplusplus
}
#endif
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment