Commit 739c23cc authored by Max Kellermann's avatar Max Kellermann

listen: moved code to listen_add_host()

Split code from the rather large function listen_add_config_param(), part 3.
parent 33749e7e
...@@ -214,64 +214,26 @@ listen_add_port(unsigned int port, GError **error) ...@@ -214,64 +214,26 @@ listen_add_port(unsigned int port, GError **error)
#endif /* HAVE_TCP */ #endif /* HAVE_TCP */
} }
#ifdef HAVE_UN
/** /**
* Add a listener on a Unix domain socket. * Resolves a host name, and adds listeners on all addresses in the
* result set.
* *
* @param path the absolute socket path * @param hostname the host name to be resolved
* @param port the TCP port
* @param error location to store the error occuring, or NULL to ignore errors * @param error location to store the error occuring, or NULL to ignore errors
* @return true on success * @return true on success
*/ */
static bool static bool
listen_add_path(const char *path, GError **error) listen_add_host(const char *hostname, unsigned port, GError **error)
{ {
size_t path_length;
struct sockaddr_un s_un;
const struct sockaddr *addrp = (const struct sockaddr *)&s_un;
socklen_t addrlen = sizeof(s_un);
bool success;
path_length = strlen(path);
if (path_length >= sizeof(s_un.sun_path))
g_error("unix socket path is too long");
unlink(path);
s_un.sun_family = AF_UNIX;
memcpy(s_un.sun_path, path, path_length + 1);
success = listen_add_address(PF_UNIX, addrp, addrlen, error);
if (!success)
return false;
/* allow everybody to connect */
chmod(path, 0666);
return true;
}
#endif /* HAVE_UN */
static bool
listen_add_config_param(unsigned int port,
const struct config_param *param,
GError **error)
{
bool success;
if (!param || 0 == strcmp(param->value, "any")) {
return listen_add_port(port, error);
#ifdef HAVE_UN
} else if (param->value[0] == '/') {
return listen_add_path(param->value, error);
#endif /* HAVE_UN */
} else {
#ifdef HAVE_TCP #ifdef HAVE_TCP
#ifndef WIN32 #ifndef WIN32
struct addrinfo hints, *ai, *i; struct addrinfo hints, *ai, *i;
char service[20]; char service[20];
int ret; int ret;
bool success;
g_debug("binding to address for %s", param->value); g_debug("binding to address for %s", hostname);
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE; hints.ai_flags = AI_PASSIVE;
...@@ -284,10 +246,13 @@ listen_add_config_param(unsigned int port, ...@@ -284,10 +246,13 @@ listen_add_config_param(unsigned int port,
g_snprintf(service, sizeof(service), "%u", port); g_snprintf(service, sizeof(service), "%u", port);
ret = getaddrinfo(param->value, service, &hints, &ai); ret = getaddrinfo(hostname, service, &hints, &ai);
if (ret != 0) if (ret != 0) {
g_error("can't lookup host \"%s\" at line %i: %s", g_set_error(error, listen_quark(), ret,
param->value, param->line, gai_strerror(ret)); "Failed to look up host \"%s\": %s",
hostname, gai_strerror(ret));
return false;
}
for (i = ai; i != NULL; i = i->ai_next) { for (i = ai; i != NULL; i = i->ai_next) {
success = listen_add_address(i->ai_family, i->ai_addr, success = listen_add_address(i->ai_family, i->ai_addr,
...@@ -305,9 +270,11 @@ listen_add_config_param(unsigned int port, ...@@ -305,9 +270,11 @@ listen_add_config_param(unsigned int port,
g_debug("binding to address for %s", param->value); g_debug("binding to address for %s", param->value);
he = gethostbyname(param->value); he = gethostbyname(param->value);
if (he == NULL) if (he == NULL) {
g_error("can't lookup host \"%s\" at line %i", g_set_error(error, listen_quark(), 0,
param->value, param->line); "Failed to look up host \"%s\"", hostname);
return false;
}
if (he->h_addrtype != AF_INET) if (he->h_addrtype != AF_INET)
g_error("IPv4 address expected for host \"%s\" at line %i", g_error("IPv4 address expected for host \"%s\" at line %i",
...@@ -317,10 +284,63 @@ listen_add_config_param(unsigned int port, ...@@ -317,10 +284,63 @@ listen_add_config_param(unsigned int port,
error); error);
#endif /* !WIN32 */ #endif /* !WIN32 */
#else /* HAVE_TCP */ #else /* HAVE_TCP */
g_set_error(error, listen_quark(), 0, g_set_error(error, listen_quark(), 0,
"TCP support is disabled"); "TCP support is disabled");
return false; return false;
#endif /* HAVE_TCP */ #endif /* HAVE_TCP */
}
#ifdef HAVE_UN
/**
* Add a listener on a Unix domain socket.
*
* @param path the absolute socket path
* @param error location to store the error occuring, or NULL to ignore errors
* @return true on success
*/
static bool
listen_add_path(const char *path, GError **error)
{
size_t path_length;
struct sockaddr_un s_un;
const struct sockaddr *addrp = (const struct sockaddr *)&s_un;
socklen_t addrlen = sizeof(s_un);
bool success;
path_length = strlen(path);
if (path_length >= sizeof(s_un.sun_path))
g_error("unix socket path is too long");
unlink(path);
s_un.sun_family = AF_UNIX;
memcpy(s_un.sun_path, path, path_length + 1);
success = listen_add_address(PF_UNIX, addrp, addrlen, error);
if (!success)
return false;
/* allow everybody to connect */
chmod(path, 0666);
return true;
}
#endif /* HAVE_UN */
static bool
listen_add_config_param(unsigned int port,
const struct config_param *param,
GError **error)
{
if (!param || 0 == strcmp(param->value, "any")) {
return listen_add_port(port, error);
#ifdef HAVE_UN
} else if (param->value[0] == '/') {
return listen_add_path(param->value, error);
#endif /* HAVE_UN */
} else {
return listen_add_host(param->value, port, error);
} }
} }
......
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