From ab651284a26c1a5dd30c85cf792399df5ccd7ac2 Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Sun, 13 Jul 2014 04:59:22 +0200 Subject: Initialize the server name --- src/kike.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'src') diff --git a/src/kike.c b/src/kike.c index 0ce7092..fdb87bc 100644 --- a/src/kike.c +++ b/src/kike.c @@ -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); -- cgit v1.2.3-70-g09d2