diff options
author | Přemysl Janouch <p.janouch@gmail.com> | 2014-07-13 04:59:22 +0200 |
---|---|---|
committer | Přemysl Janouch <p.janouch@gmail.com> | 2014-07-13 05:05:01 +0200 |
commit | ab651284a26c1a5dd30c85cf792399df5ccd7ac2 (patch) | |
tree | 4a474818927e6284e8ab3c14867e8183141ff307 /src | |
parent | 475c83618a043c88e7414b839228c28452658166 (diff) | |
download | xK-ab651284a26c1a5dd30c85cf792399df5ccd7ac2.tar.gz xK-ab651284a26c1a5dd30c85cf792399df5ccd7ac2.tar.xz xK-ab651284a26c1a5dd30c85cf792399df5ccd7ac2.zip |
Initialize the server name
Diffstat (limited to 'src')
-rw-r--r-- | src/kike.c | 45 |
1 files changed, 45 insertions, 0 deletions
@@ -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); @@ -784,12 +787,54 @@ error_ssl_1: } 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) { const char *bind_host = str_map_find (&ctx->config, "bind_host"); 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); |