[mpd] Bind via misc.c's net_xxx methods

This commit is contained in:
ejurgensen 2021-02-28 00:08:25 +01:00
parent d4711decdc
commit d2a5f939f5

190
src/mpd.c
View File

@ -77,8 +77,9 @@ static struct commands_base *cmdbase;
static struct evhttp *evhttpd;
struct evconnlistener *mpd_listener6;
struct evconnlistener *mpd_listener;
static struct evconnlistener *mpd_listener;
static int mpd_sockfd;
// Virtual path to the default playlist directory
static char *default_pl_dir;
@ -4739,18 +4740,47 @@ artwork_cb(struct evhttp_request *req, void *arg)
}
/* Thread: main */
int mpd_init(void)
static int mpd_httpd_init(void)
{
struct sockaddr *saddr;
size_t saddr_length;
struct sockaddr_in sin;
struct sockaddr_in6 sin6;
unsigned short port;
unsigned short http_port;
int v6enabled;
const char *pl_dir;
int ret;
http_port = cfg_getint(cfg_getsec(cfg, "mpd"), "http_port");
if (http_port == 0)
return 0;
evhttpd = evhttp_new(evbase_mpd);
if (!evhttpd)
return -1;
ret = net_evhttp_bind(evhttpd, http_port, "mpd artwork");
if (ret < 0)
{
evhttp_free(evhttpd);
evhttpd = NULL;
return -1;
}
evhttp_set_gencb(evhttpd, artwork_cb, NULL);
return 0;
}
/* Thread: main */
static void mpd_httpd_deinit(void)
{
if (evhttpd)
evhttp_free(evhttpd);
evhttpd = NULL;
}
/* Thread: main */
int mpd_init(void)
{
unsigned short port;
const char *pl_dir;
int ret;
port = cfg_getint(cfg_getsec(cfg, "mpd"), "port");
if (port <= 0)
@ -4759,112 +4789,29 @@ int mpd_init(void)
return 0;
}
v6enabled = cfg_getbool(cfg_getsec(cfg, "general"), "ipv6");
CHECK_NULL(L_MPD, evbase_mpd = event_base_new());
CHECK_NULL(L_MPD, cmdbase = commands_base_new(evbase_mpd, NULL));
evbase_mpd = event_base_new();
if (!evbase_mpd)
mpd_sockfd = net_bind(&port, SOCK_STREAM | SOCK_NONBLOCK, "mpd");
if (mpd_sockfd < 0)
{
DPRINTF(E_LOG, L_MPD, "Could not create an event base\n");
goto evbase_fail;
DPRINTF(E_LOG, L_MPD, "Could not bind mpd server to port %hu\n", port);
goto bind_fail;
}
cmdbase = commands_base_new(evbase_mpd, NULL);
if (v6enabled)
{
saddr_length = sizeof(sin6);
memset(&sin6, 0, saddr_length);
sin6.sin6_family = AF_INET6;
sin6.sin6_port = htons(port);
saddr = (struct sockaddr *)&sin6;
mpd_listener6 = evconnlistener_new_bind(
evbase_mpd,
mpd_accept_conn_cb,
NULL,
LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE,
-1,
saddr,
saddr_length);
if (!mpd_listener6)
{
DPRINTF(E_LOG, L_MPD, "Could not bind to port %d, falling back to IPv4\n", port);
v6enabled = 0;
}
else
evconnlistener_set_error_cb(mpd_listener6, mpd_accept_error_cb);
}
saddr_length = sizeof(struct sockaddr_in);
memset(&sin, 0, saddr_length);
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(0);
sin.sin_port = htons(port);
saddr = (struct sockaddr *)&sin;
mpd_listener = evconnlistener_new_bind(
evbase_mpd,
mpd_accept_conn_cb,
NULL,
LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE,
-1,
saddr,
saddr_length);
mpd_listener = evconnlistener_new(evbase_mpd, mpd_accept_conn_cb, NULL, 0, -1, mpd_sockfd);
if (!mpd_listener)
{
if (!v6enabled)
{
DPRINTF(E_LOG, L_MPD, "Could not create connection listener for mpd clients on port %d\n", port);
goto connew_fail;
}
#ifndef __linux__
// Linux will listen on both ipv6 and ipv4, but FreeBSD won't
DPRINTF(E_LOG, L_MPD, "Could not bind to port %d with IPv4, listening on IPv6 only\n", port);
#endif
DPRINTF(E_LOG, L_MPD, "Could not create connection listener for mpd clients on port %d\n", port);
goto connew_fail;
}
else
evconnlistener_set_error_cb(mpd_listener, mpd_accept_error_cb);
evconnlistener_set_error_cb(mpd_listener, mpd_accept_error_cb);
http_port = cfg_getint(cfg_getsec(cfg, "mpd"), "http_port");
if (http_port > 0)
ret = mpd_httpd_init();
if (ret < 0)
{
evhttpd = evhttp_new(evbase_mpd);
if (!evhttpd)
{
DPRINTF(E_LOG, L_MPD, "Could not create HTTP artwork server\n");
goto evhttp_fail;
}
evhttp_set_gencb(evhttpd, artwork_cb, NULL);
if (v6enabled)
{
ret = evhttp_bind_socket(evhttpd, "::", http_port);
if (ret < 0)
{
DPRINTF(E_LOG, L_MPD, "Could not bind HTTP artwork server to port %d with IPv6, falling back to IPv4\n", http_port);
v6enabled = 0;
}
}
ret = evhttp_bind_socket(evhttpd, "0.0.0.0", http_port);
if (ret < 0)
{
if (!v6enabled)
{
DPRINTF(E_LOG, L_MPD, "Could not bind HTTP artwork server to port %d with IPv4\n", http_port);
goto bind_fail;
}
#ifndef __linux__
// Linux will listen on both ipv6 and ipv4, but FreeBSD won't
DPRINTF(E_LOG, L_MPD, "Could not bind HTTP artwork server to port %d with IPv4, listening on IPv6 only\n", http_port);
#endif
}
DPRINTF(E_LOG, L_MPD, "Could not initialize HTTP artwork server\n");
goto httpd_fail;
}
allow_modifying_stored_playlists = cfg_getbool(cfg_getsec(cfg, "library"), "allow_modifying_stored_playlists");
@ -4908,23 +4855,17 @@ int mpd_init(void)
return 0;
thread_fail:
bind_fail:
if (http_port > 0)
evhttp_free(evhttpd);
evhttp_fail:
// Note evconnlistener_free segfaults if you give it a null pointer, so we need the if
if (mpd_listener)
evconnlistener_free(mpd_listener);
if (mpd_listener6)
evconnlistener_free(mpd_listener6);
mpd_httpd_deinit();
httpd_fail:
evconnlistener_free(mpd_listener);
connew_fail:
close(mpd_sockfd);
bind_fail:
commands_base_free(cmdbase);
event_base_free(evbase_mpd);
evbase_mpd = NULL;
evbase_fail:
return -1;
}
@ -4932,7 +4873,6 @@ int mpd_init(void)
void mpd_deinit(void)
{
unsigned short port;
unsigned short http_port;
int ret;
port = cfg_getint(cfg_getsec(cfg, "mpd"), "port");
@ -4958,15 +4898,11 @@ void mpd_deinit(void)
free_mpd_client_ctx(mpd_clients);
}
http_port = cfg_getint(cfg_getsec(cfg, "mpd"), "http_port");
if (http_port > 0)
evhttp_free(evhttpd);
mpd_httpd_deinit();
// Note evconnlistener_free segfaults if you give it a null pointer, so we need the if
if (mpd_listener)
evconnlistener_free(mpd_listener);
if (mpd_listener6)
evconnlistener_free(mpd_listener6);
evconnlistener_free(mpd_listener);
close(mpd_sockfd);
// Free event base (should free events too)
event_base_free(evbase_mpd);