Initialize the server name

This commit is contained in:
Přemysl Eric Janouch 2014-07-13 04:59:22 +02:00
parent 475c83618a
commit ab651284a2
1 changed files with 45 additions and 0 deletions

View File

@ -314,6 +314,7 @@ struct server_context
struct connection *clients; ///< Client connections
SSL_CTX *ssl_ctx; ///< SSL context
char *server_name; ///< Our server name
struct str_map users; ///< Maps nicknames to connections
struct str_map channels; ///< Maps channel names to data
@ -332,6 +333,7 @@ server_context_init (struct server_context *self)
self->listen_fd = -1;
self->clients = NULL;
self->server_name = NULL;
str_map_init (&self->users);
// TODO: set channel_free() as the free function?
str_map_init (&self->channels);
@ -360,6 +362,7 @@ server_context_free (struct server_context *self)
free (link);
}
free (self->server_name);
str_map_free (&self->users);
str_map_free (&self->channels);
poller_free (&self->poller);
@ -783,6 +786,45 @@ error_ssl_1:
return false;
}
static bool
irc_initialize_server_name (struct server_context *ctx, struct error **e)
{
enum validation_result res;
const char *server_name = str_map_find (&ctx->config, "server_name");
if (server_name)
{
res = irc_validate_hostname (server_name);
if (res != VALIDATION_OK)
{
error_set (e, NETWORK_ERROR, NETWORK_ERROR_INVALID_CONFIGURATION,
"invalid configuration value for `%s': %s", "server_name",
irc_validate_to_str (res));
return false;
}
ctx->server_name = xstrdup (server_name);
}
else
{
char hostname[HOST_NAME_MAX];
if (gethostname (hostname, sizeof hostname))
{
error_set (e, NETWORK_ERROR, NETWORK_ERROR_INVALID_CONFIGURATION,
"%s: %s", "getting the hostname failed", strerror (errno));
return false;
}
res = irc_validate_hostname (hostname);
if (res != VALIDATION_OK)
{
error_set (e, NETWORK_ERROR, NETWORK_ERROR_INVALID_CONFIGURATION,
"`%s' is not set and the hostname (`%s') cannot be used: %s",
"server_name", hostname, irc_validate_to_str (res));
return false;
}
ctx->server_name = xstrdup (hostname);
}
return true;
}
static bool
irc_listen (struct server_context *ctx, struct error **e)
{
@ -790,6 +832,9 @@ irc_listen (struct server_context *ctx, struct error **e)
const char *bind_port = str_map_find (&ctx->config, "bind_port");
hard_assert (bind_port != NULL); // We have a default value for this
if (!irc_initialize_server_name (ctx, e))
return false;
struct addrinfo gai_hints, *gai_result, *gai_iter;
memset (&gai_hints, 0, sizeof gai_hints);