2009-02-28 15:12:24 +01:00
|
|
|
/*
|
2010-01-01 05:55:13 +01:00
|
|
|
* Copyright (C) 2003-2010 The Music Player Daemon Project
|
2009-02-28 15:12:24 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2009-02-28 15:12:24 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2009-02-28 15:12:24 +01:00
|
|
|
#include "socket_util.h"
|
2009-11-07 18:55:16 +01:00
|
|
|
#include "fd_util.h"
|
2009-02-28 15:12:24 +01:00
|
|
|
|
2009-03-14 18:29:38 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2009-03-01 01:35:54 +01:00
|
|
|
#ifndef G_OS_WIN32
|
2009-02-28 15:12:24 +01:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
2009-03-01 01:35:54 +01:00
|
|
|
#else /* G_OS_WIN32 */
|
2010-03-22 02:27:29 +01:00
|
|
|
#define WINVER 0x0501
|
2009-03-01 01:35:54 +01:00
|
|
|
#include <ws2tcpip.h>
|
2009-03-14 18:29:38 +01:00
|
|
|
#include <winsock.h>
|
2009-03-01 01:35:54 +01:00
|
|
|
#endif /* G_OS_WIN32 */
|
2009-02-28 15:12:24 +01:00
|
|
|
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
2009-03-14 18:29:38 +01:00
|
|
|
static GQuark
|
|
|
|
listen_quark(void)
|
|
|
|
{
|
|
|
|
return g_quark_from_static_string("listen");
|
|
|
|
}
|
|
|
|
|
2009-02-28 15:12:24 +01:00
|
|
|
char *
|
|
|
|
sockaddr_to_string(const struct sockaddr *sa, size_t length, GError **error)
|
|
|
|
{
|
2009-03-06 10:09:10 +01:00
|
|
|
#if defined(HAVE_IPV6) && defined(IN6_IS_ADDR_V4MAPPED)
|
2009-02-28 15:20:33 +01:00
|
|
|
const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *)sa;
|
|
|
|
struct sockaddr_in a4;
|
|
|
|
#endif
|
2009-02-28 15:12:24 +01:00
|
|
|
int ret;
|
|
|
|
char host[NI_MAXHOST], serv[NI_MAXSERV];
|
|
|
|
|
2009-03-06 10:09:10 +01:00
|
|
|
#if defined(HAVE_IPV6) && defined(IN6_IS_ADDR_V4MAPPED)
|
2009-02-28 15:20:33 +01:00
|
|
|
if (sa->sa_family == AF_INET6 &&
|
|
|
|
IN6_IS_ADDR_V4MAPPED(&a6->sin6_addr)) {
|
|
|
|
/* convert "::ffff:127.0.0.1" to "127.0.0.1" */
|
|
|
|
|
|
|
|
memset(&a4, 0, sizeof(a4));
|
|
|
|
a4.sin_family = AF_INET;
|
|
|
|
memcpy(&a4.sin_addr, ((const char *)&a6->sin6_addr) + 12,
|
|
|
|
sizeof(a4.sin_addr));
|
|
|
|
a4.sin_port = a6->sin6_port;
|
|
|
|
|
|
|
|
sa = (const struct sockaddr *)&a4;
|
|
|
|
length = sizeof(a4);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-02-28 15:12:24 +01:00
|
|
|
ret = getnameinfo(sa, length, host, sizeof(host), serv, sizeof(serv),
|
|
|
|
NI_NUMERICHOST|NI_NUMERICSERV);
|
|
|
|
if (ret != 0) {
|
|
|
|
g_set_error(error, g_quark_from_static_string("netdb"), ret,
|
2009-04-01 16:45:03 +02:00
|
|
|
"%s", gai_strerror(ret));
|
2009-02-28 15:12:24 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_UN
|
|
|
|
if (sa->sa_family == AF_UNIX)
|
|
|
|
/* "serv" contains corrupt information with unix
|
|
|
|
sockets */
|
|
|
|
return g_strdup(host);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
if (strchr(host, ':') != NULL)
|
|
|
|
return g_strconcat("[", host, "]:", serv, NULL);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return g_strconcat(host, ":", serv, NULL);
|
|
|
|
}
|
2009-03-14 18:29:38 +01:00
|
|
|
|
|
|
|
int
|
|
|
|
socket_bind_listen(int domain, int type, int protocol,
|
|
|
|
const struct sockaddr *address, size_t address_length,
|
|
|
|
int backlog,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
int fd, ret;
|
|
|
|
const int reuse = 1;
|
|
|
|
#ifdef HAVE_STRUCT_UCRED
|
|
|
|
int passcred = 1;
|
|
|
|
#endif
|
|
|
|
|
2009-11-08 22:11:35 +01:00
|
|
|
fd = socket_cloexec_nonblock(domain, type, protocol);
|
2009-03-14 18:29:38 +01:00
|
|
|
if (fd < 0) {
|
|
|
|
g_set_error(error, listen_quark(), errno,
|
|
|
|
"Failed to create socket: %s", g_strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-05-18 23:12:56 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
const char *optval = (const char *)&reuse;
|
|
|
|
#else
|
|
|
|
const void *optval = &reuse;
|
|
|
|
#endif
|
|
|
|
|
2009-03-14 18:29:38 +01:00
|
|
|
ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
|
2010-05-18 23:12:56 +02:00
|
|
|
optval, sizeof(reuse));
|
2009-03-14 18:29:38 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
g_set_error(error, listen_quark(), errno,
|
|
|
|
"setsockopt() failed: %s", g_strerror(errno));
|
2011-09-19 21:04:19 +02:00
|
|
|
close_socket(fd);
|
2009-03-14 18:29:38 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = bind(fd, address, address_length);
|
|
|
|
if (ret < 0) {
|
|
|
|
g_set_error(error, listen_quark(), errno,
|
2009-03-16 11:59:26 +01:00
|
|
|
"%s", g_strerror(errno));
|
2011-09-19 21:04:19 +02:00
|
|
|
close_socket(fd);
|
2009-03-14 18:29:38 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = listen(fd, backlog);
|
|
|
|
if (ret < 0) {
|
|
|
|
g_set_error(error, listen_quark(), errno,
|
|
|
|
"listen() failed: %s", g_strerror(errno));
|
2011-09-19 21:04:19 +02:00
|
|
|
close_socket(fd);
|
2009-03-14 18:29:38 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_STRUCT_UCRED
|
|
|
|
setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &passcred, sizeof(passcred));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|