summaryrefslogtreecommitdiff
path: root/degesch.c
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2015-05-09 22:10:58 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2015-05-09 22:14:02 +0200
commit2eda3110b3bdf85f04682230ed60eb02b8a06510 (patch)
treebcc00175c7869028d0f0bf73aaf210e81309e628 /degesch.c
parent19b2eda70e02329fcd0ab7a05968711faa539837 (diff)
downloadxK-2eda3110b3bdf85f04682230ed60eb02b8a06510.tar.gz
xK-2eda3110b3bdf85f04682230ed60eb02b8a06510.tar.xz
xK-2eda3110b3bdf85f04682230ed60eb02b8a06510.zip
degesch: asynchronous connecting etc.
I'm sorry, couldn't keep the diff small. All the ZyklonB heritage code is shit anyway.
Diffstat (limited to 'degesch.c')
-rw-r--r--degesch.c965
1 files changed, 500 insertions, 465 deletions
diff --git a/degesch.c b/degesch.c
index f4e1a81..7cffb3b 100644
--- a/degesch.c
+++ b/degesch.c
@@ -977,6 +977,7 @@ buffer_destroy (struct buffer *self)
enum server_state
{
IRC_DISCONNECTED, ///< Not connected
+ IRC_CONNECTING, ///< Connecting to the server
IRC_CONNECTED, ///< Trying to register
IRC_REGISTERED ///< We can chat now
};
@@ -984,8 +985,11 @@ enum server_state
struct server
{
struct app_context *ctx; ///< Application context
+ bool reconnect; ///< Whether to reconnect on conn. fail.
+ unsigned long reconnect_delay; ///< Reconnect delay in seconds
enum server_state state; ///< Connection state
+ struct connector *connector; ///< Connection establisher
int socket; ///< Socket FD of the server
struct str read_buffer; ///< Input yet to be processed
@@ -1021,9 +1025,9 @@ struct server
struct poller_timer reconnect_tmr; ///< We should reconnect now
};
-static void on_irc_ping_timeout (void *user_data);
static void on_irc_timeout (void *user_data);
-static void on_irc_reconnect_timeout (void *user_data);
+static void on_irc_ping_timeout (void *user_data);
+static void irc_initiate_connect (struct server *s);
static void
server_init (struct server *self, struct poller *poller)
@@ -1048,13 +1052,18 @@ server_init (struct server *self, struct poller *poller)
self->ping_tmr.user_data = self;
poller_timer_init (&self->reconnect_tmr, poller);
- self->reconnect_tmr.dispatcher = on_irc_reconnect_timeout;
+ self->reconnect_tmr.dispatcher = (poller_timer_fn) irc_initiate_connect;
self->reconnect_tmr.user_data = self;
}
static void
server_free (struct server *self)
{
+ if (self->connector)
+ {
+ connector_free (self->connector);
+ free (self->connector);
+ }
if (self->socket != -1)
{
xclose (self->socket);
@@ -1086,8 +1095,6 @@ struct app_context
struct config config; ///< Program configuration
char *attrs[ATTR_COUNT]; ///< Terminal attributes
bool no_colors; ///< Colour output mode
- bool reconnect; ///< Whether to reconnect on conn. fail.
- unsigned long reconnect_delay; ///< Reconnect delay in seconds
bool isolate_buffers; ///< Isolate global/server buffers
struct server server; ///< Our only server so far
@@ -2453,11 +2460,11 @@ irc_remove_user_from_channel (struct user *user, struct channel *channel)
irc_channel_unlink_user (channel, iter);
}
-// --- Supporting code ---------------------------------------------------------
+// --- Core functionality ------------------------------------------------------
-static bool irc_connect (struct server *s, bool *should_retry, struct error **);
-static void irc_cancel_timers (struct server *s);
-static void on_irc_reconnect_timeout (void *user_data);
+// Most of the core IRC code comes from ZyklonB which is mostly blocking.
+// While it's fairly easy to follow, it also stinks. It needs to be rewritten
+// to be as asynchronous as possible. See kike.c for reference.
static bool
irc_is_connected (struct server *s)
@@ -2466,117 +2473,36 @@ irc_is_connected (struct server *s)
}
static void
-irc_shutdown (struct server *s)
+irc_cancel_timers (struct server *s)
{
- // TODO: set a timer after which we cut the connection?
- // Generally non-critical
- if (s->ssl)
- soft_assert (SSL_shutdown (s->ssl) != -1);
- else
- soft_assert (shutdown (s->socket, SHUT_WR) == 0);
+ poller_timer_reset (&s->timeout_tmr);
+ poller_timer_reset (&s->ping_tmr);
+ poller_timer_reset (&s->reconnect_tmr);
}
static void
-try_finish_quit (struct app_context *ctx)
+irc_reset_connection_timeouts (struct server *s)
{
- // TODO: multiserver
- if (ctx->quitting && !irc_is_connected (&ctx->server))
- ctx->polling = false;
+ irc_cancel_timers (s);
+ poller_timer_set (&s->timeout_tmr, 3 * 60 * 1000);
+ poller_timer_set (&s->ping_tmr, (3 * 60 + 30) * 1000);
}
static void
-initiate_quit (struct app_context *ctx)
-{
- // Destroy the user interface
- input_stop (&ctx->input);
-
- buffer_send_status (ctx, ctx->global_buffer, "Shutting down");
-
- // Initiate a connection close
- // TODO: multiserver
- struct server *s = &ctx->server;
- if (irc_is_connected (s))
- // XXX: when we go async, we'll have to flush output buffers first
- irc_shutdown (s);
-
- ctx->quitting = true;
- try_finish_quit (ctx);
-}
-
-// As of 2015, everything should be in UTF-8. And if it's not, we'll decode it
-// as ISO Latin 1. This function should not be called on the whole message.
-static char *
-irc_to_utf8 (struct app_context *ctx, const char *text)
+irc_queue_reconnect (struct server *s)
{
- size_t len = strlen (text) + 1;
- if (utf8_validate (text, len))
- return xstrdup (text);
- return iconv_xstrdup (ctx->latin1_to_utf8, (char *) text, len, NULL);
-}
+ // As long as the user wants us to, that is
+ if (!s->reconnect)
+ return;
-// This function is used to output debugging IRC traffic to the terminal.
-// It's far from ideal, as any non-UTF-8 text degrades the entire line to
-// ISO Latin 1. But it should work good enough most of the time.
-static char *
-irc_to_term (struct app_context *ctx, const char *text)
-{
- char *utf8 = irc_to_utf8 (ctx, text);
- char *term = iconv_xstrdup (ctx->term_from_utf8, utf8, -1, NULL);
- free (utf8);
- return term;
+ // TODO: exponentional backoff
+ hard_assert (s->socket == -1);
+ buffer_send_status (s->ctx, s->buffer,
+ "Trying to reconnect in %ld seconds...", s->reconnect_delay);
+ poller_timer_set (&s->reconnect_tmr, s->reconnect_delay * 1000);
}
-static bool irc_send (struct server *s,
- const char *format, ...) ATTRIBUTE_PRINTF (2, 3);
-
-static bool
-irc_send (struct server *s, const char *format, ...)
-{
- if (!soft_assert (irc_is_connected (s)))
- {
- print_debug ("tried sending a message to a dead server connection");
- return false;
- }
-
- va_list ap;
- va_start (ap, format);
- struct str str;
- str_init (&str);
- str_append_vprintf (&str, format, ap);
- va_end (ap);
-
- if (g_debug_mode)
- {
- input_hide (&s->ctx->input);
-
- char *term = irc_to_term (s->ctx, str.str);
- fprintf (stderr, "[IRC] <== \"%s\"\n", term);
- free (term);
-
- input_show (&s->ctx->input);
- }
- str_append (&str, "\r\n");
-
- bool result = true;
- if (s->ssl)
- {
- // TODO: call SSL_get_error() to detect if a clean shutdown has occured
- if (SSL_write (s->ssl, str.str, str.len) != (int) str.len)
- {
- LOG_FUNC_FAILURE ("SSL_write",
- ERR_error_string (ERR_get_error (), NULL));
- result = false;
- }
- }
- else if (write (s->socket, str.str, str.len) != (ssize_t) str.len)
- {
- LOG_LIBC_FAILURE ("write");
- result = false;
- }
-
- str_free (&str);
- return result;
-}
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static bool
irc_initialize_ssl_ctx (struct server *s, struct error **e)
@@ -2685,70 +2611,491 @@ error_ssl_1:
return false;
}
-static bool
-irc_establish_connection (struct server *s,
- const char *host, const char *port, struct error **e)
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+// As of 2015, everything should be in UTF-8. And if it's not, we'll decode it
+// as ISO Latin 1. This function should not be called on the whole message.
+static char *
+irc_to_utf8 (struct app_context *ctx, const char *text)
{
- struct addrinfo gai_hints, *gai_result, *gai_iter;
- memset (&gai_hints, 0, sizeof gai_hints);
- gai_hints.ai_socktype = SOCK_STREAM;
+ size_t len = strlen (text) + 1;
+ if (utf8_validate (text, len))
+ return xstrdup (text);
+ return iconv_xstrdup (ctx->latin1_to_utf8, (char *) text, len, NULL);
+}
- int err = getaddrinfo (host, port, &gai_hints, &gai_result);
- if (err)
+// This function is used to output debugging IRC traffic to the terminal.
+// It's far from ideal, as any non-UTF-8 text degrades the entire line to
+// ISO Latin 1. But it should work good enough most of the time.
+static char *
+irc_to_term (struct app_context *ctx, const char *text)
+{
+ char *utf8 = irc_to_utf8 (ctx, text);
+ char *term = iconv_xstrdup (ctx->term_from_utf8, utf8, -1, NULL);
+ free (utf8);
+ return term;
+}
+
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+static void irc_send (struct server *s,
+ const char *format, ...) ATTRIBUTE_PRINTF (2, 3);
+
+static void
+irc_send (struct server *s, const char *format, ...)
+{
+ if (!soft_assert (irc_is_connected (s)))
{
- error_set (e, "%s: %s: %s",
- "connection failed", "getaddrinfo", gai_strerror (err));
- return false;
+ print_debug ("tried sending a message to a dead server connection");
+ return;
}
- int sockfd;
- for (gai_iter = gai_result; gai_iter; gai_iter = gai_iter->ai_next)
+ va_list ap;
+ va_start (ap, format);
+ struct str str;
+ str_init (&str);
+ str_append_vprintf (&str, format, ap);
+ va_end (ap);
+
+ if (g_debug_mode)
{
- sockfd = socket (gai_iter->ai_family,
- gai_iter->ai_socktype, gai_iter->ai_protocol);
- if (sockfd == -1)
- continue;
- set_cloexec (sockfd);
+ input_hide (&s->ctx->input);
- int yes = 1;
- soft_assert (setsockopt (sockfd, SOL_SOCKET, SO_KEEPALIVE,
- &yes, sizeof yes) != -1);
+ char *term = irc_to_term (s->ctx, str.str);
+ fprintf (stderr, "[IRC] <== \"%s\"\n", term);
+ free (term);
- const char *real_host = host;
+ input_show (&s->ctx->input);
+ }
+ str_append (&str, "\r\n");
- // Let's try to resolve the address back into a real hostname;
- // we don't really need this, so we can let it quietly fail
- char buf[NI_MAXHOST];
- err = getnameinfo (gai_iter->ai_addr, gai_iter->ai_addrlen,
- buf, sizeof buf, NULL, 0, NI_NUMERICHOST);
- if (err)
- LOG_FUNC_FAILURE ("getnameinfo", gai_strerror (err));
- else
- real_host = buf;
+ if (s->ssl)
+ {
+ // TODO: call SSL_get_error() to detect if a clean shutdown has occured
+ if (SSL_write (s->ssl, str.str, str.len) != (int) str.len)
+ LOG_FUNC_FAILURE ("SSL_write",
+ ERR_error_string (ERR_get_error (), NULL));
+ }
+ else if (write (s->socket, str.str, str.len) != (ssize_t) str.len)
+ LOG_LIBC_FAILURE ("write");
- char *address = format_host_port_pair (real_host, port);
- buffer_send_status (s->ctx, s->buffer, "Connecting to %s...", address);
- free (address);
+ str_free (&str);
+}
+
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+static void
+irc_shutdown (struct server *s)
+{
+ // TODO: set a timer after which we cut the connection?
+ // Generally non-critical
+ if (s->ssl)
+ soft_assert (SSL_shutdown (s->ssl) != -1);
+ else
+ soft_assert (shutdown (s->socket, SHUT_WR) == 0);
+}
+
+static void
+irc_destroy_connector (struct server *s)
+{
+ connector_free (s->connector);
+ free (s->connector);
+ s->connector = NULL;
+
+ // Not connecting anymore
+ s->state = IRC_DISCONNECTED;
+}
+
+static void
+try_finish_quit (struct app_context *ctx)
+{
+ // TODO: multiserver
+ if (ctx->quitting && !irc_is_connected (&ctx->server))
+ ctx->polling = false;
+}
+
+static void
+initiate_quit (struct app_context *ctx)
+{
+ // Destroy the user interface
+ input_stop (&ctx->input);
+
+ buffer_send_status (ctx, ctx->global_buffer, "Shutting down");
+
+ // Initiate a connection close
+ // TODO: multiserver
+ struct server *s = &ctx->server;
+ if (irc_is_connected (s))
+ // XXX: when we go async, we'll have to flush output buffers first
+ irc_shutdown (s);
+ else if (s->state == IRC_CONNECTING)
+ irc_destroy_connector (s);
+
+ ctx->quitting = true;
+ try_finish_quit (ctx);
+}
+
+static void
+on_irc_disconnected (struct server *s)
+{
+ hard_assert (irc_is_connected (s));
+
+ // Get rid of the dead socket and related things
+ if (s->ssl)
+ {
+ SSL_free (s->ssl);
+ s->ssl = NULL;
+ SSL_CTX_free (s->ssl_ctx);
+ s->ssl_ctx = NULL;
+ }
+
+ xclose (s->socket);
+ s->socket = -1;
+ s->state = IRC_DISCONNECTED;
+
+ user_unref (s->irc_user);
+ s->irc_user = NULL;
+
+ free (s->irc_user_mode);
+ s->irc_user_mode = NULL;
+ free (s->irc_user_host);
+ s->irc_user_host = NULL;
+
+ s->read_event.closed = true;
+ poller_fd_reset (&s->read_event);
+
+ // All of our timers have lost their meaning now
+ irc_cancel_timers (s);
+
+ if (s->ctx->quitting)
+ try_finish_quit (s->ctx);
+ else
+ irc_queue_reconnect (s);
+}
+
+// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+static void
+on_irc_ping_timeout (void *user_data)
+{
+ struct server *s = user_data;
+ buffer_send_error (s->ctx, s->buffer, "Connection timeout");
+ on_irc_disconnected (s);
+}
+
+static void
+on_irc_timeout (void *user_data)
+{
+ // Provoke a response from the server
+ struct server *s = user_data;
+ irc_send (s, "PING :%s", get_config_string (s->ctx, s->irc_user->nickname));
+}
+
+// --- Processing server output ------------------------------------------------
+
+static void irc_process_message
+ (const struct irc_message *msg, const char *raw, void *user_data);
+
+enum irc_read_result
+{
+ IRC_READ_OK, ///< Some data were read successfully
+ IRC_READ_EOF, ///< The server has closed connection
+ IRC_READ_AGAIN, ///< No more data at the moment
+ IRC_READ_ERROR ///< General connection failure
+};
+
+static enum irc_read_result
+irc_fill_read_buffer_ssl (struct server *s, struct str *buf)
+{
+ int n_read;
+start:
+ n_read = SSL_read (s->ssl, buf->str + buf->len,
+ buf->alloc - buf->len - 1 /* null byte */);
+
+ const char *error_info = NULL;
+ switch (xssl_get_error (s->ssl, n_read, &error_info))
+ {
+ case SSL_ERROR_NONE:
+ buf->str[buf->len += n_read] = '\0';
+ return IRC_READ_OK;
+ case SSL_ERROR_ZERO_RETURN:
+ return IRC_READ_EOF;
+ case SSL_ERROR_WANT_READ:
+ return IRC_READ_AGAIN;
+ case SSL_ERROR_WANT_WRITE:
+ {
+ // Let it finish the handshake as we don't poll for writability;
+ // any errors are to be collected by SSL_read() in the next iteration
+ struct pollfd pfd = { .fd = s->socket, .events = POLLOUT };
+ soft_assert (poll (&pfd, 1, 0) > 0);
+ goto start;
+ }
+ case XSSL_ERROR_TRY_AGAIN:
+ goto start;
+ default:
+ LOG_FUNC_FAILURE ("SSL_read", error_info);
+ return IRC_READ_ERROR;
+ }
+}
+
+static enum irc_read_result
+irc_fill_read_buffer (struct server *s, struct str *buf)
+{
+ ssize_t n_read;
+start:
+ n_read = recv (s->socket, buf->str + buf->len,
+ buf->alloc - buf->len - 1 /* null byte */, 0);
+
+ if (n_read > 0)
+ {
+ buf->str[buf->len += n_read] = '\0';
+ return IRC_READ_OK;
+ }
+ if (n_read == 0)
+ return IRC_READ_EOF;
+
+ if (errno == EAGAIN)
+ return IRC_READ_AGAIN;
+ if (errno == EINTR)
+ goto start;
+
+ LOG_LIBC_FAILURE ("recv");
+ return IRC_READ_ERROR;
+}
+
+static void
+on_irc_readable (const struct pollfd *fd, struct server *s)
+{
+ if (fd->revents & ~(POLLIN | POLLHUP | POLLERR))
+ print_debug ("fd %d: unexpected revents: %d", fd->fd, fd->revents);
+
+ (void) set_blocking (s->socket, false);
- if (!connect (sockfd, gai_iter->ai_addr, gai_iter->ai_addrlen))
+ struct str *buf = &s->read_buffer;
+ enum irc_read_result (*fill_buffer)(struct server *, struct str *)
+ = s->ssl
+ ? irc_fill_read_buffer_ssl
+ : irc_fill_read_buffer;
+ bool disconnected = false;
+ while (true)
+ {
+ str_ensure_space (buf, 512);
+ switch (fill_buffer (s, buf))
+ {
+ case IRC_READ_AGAIN:
+ goto end;
+ case IRC_READ_ERROR:
+ buffer_send_error (s->ctx, s->buffer,
+ "Reading from the IRC server failed");
+ disconnected = true;
+ goto end;
+ case IRC_READ_EOF:
+ buffer_send_error (s->ctx, s->buffer,
+ "The IRC server closed the connection");
+ disconnected = true;
+ goto end;
+ case IRC_READ_OK:
break;
+ }
+
+ if (buf->len >= (1 << 20))
+ {
+ buffer_send_error (s->ctx, s->buffer,
+ "The IRC server seems to spew out data frantically");
+ irc_shutdown (s);
+ goto end;
+ }
+ }
+end:
+ (void) set_blocking (s->socket, true);
+ irc_process_buffer (buf, irc_process_message, s);
+
+ if (disconnected)
+ on_irc_disconnected (s);
+ else
+ irc_reset_connection_timeouts (s);
+}
+
+// --- Connection establishment ------------------------------------------------
+
+static void
+irc_register (struct server *s)
+{
+ const char *nickname = get_config_string (s->ctx, "server.nickname");
+ const char *username = get_config_string (s->ctx, "server.username");
+ const char *realname = get_config_string (s->ctx, "server.realname");
+
+ // These are filled automatically if needed
+ hard_assert (nickname && username && realname);
+
+ irc_send (s, "NICK %s", nickname);
+ // IRC servers may ignore the last argument if it's empty
+ irc_send (s, "USER %s 8 * :%s", username, *realname ? realname : " ");
+
+ // XXX: maybe we should wait for the first message from the server
+ // FIXME: the user may exist already after we've reconnected. Either
+ // make sure that there's no reference of this nick upon disconnection,
+ // or search in "irc_users" first... or something.
+ s->irc_user = irc_make_user (s, xstrdup (nickname));
+ s->irc_user_mode = xstrdup ("");
+ s->irc_user_host = NULL;
+}
+
+static void
+irc_finish_connection (struct server *s, int socket)
+{
+ struct app_context *ctx = s->ctx;
+
+ s->socket = socket;
- xclose (sockfd);
+ struct error *e = NULL;
+ bool use_ssl = get_config_boolean (ctx, "server.ssl");
+ if (use_ssl && !irc_initialize_ssl (s, &e))
+ {
+ buffer_send_error (ctx, s->buffer, "Connection failed: %s", e->message);
+ error_free (e);
+
+ xclose (s->socket);
+ s->socket = -1;
+
+ irc_queue_reconnect (s);
+ return;
}
- freeaddrinfo (gai_result);
+ buffer_send_status (ctx, s->buffer, "Connection established");
+ s->state = IRC_CONNECTED;
+
+ poller_fd_init (&s->read_event, &ctx->poller, s->socket);
+ s->read_event.dispatcher = (poller_fd_fn) on_irc_readable;
+ s->read_event.user_data = s;
+
+ poller_fd_set (&s->read_event, POLLIN);
+ irc_reset_connection_timeouts (s);
+
+ irc_register (s);
+}
+
+static void
+irc_on_connector_connecting (void *user_data, const char *address)
+{
+ struct server *s = user_data;
+ buffer_send_status (s->ctx, s->buffer, "Connecting to %s...", address);
+}
+
+static void
+irc_on_connector_error (void *user_data, const char *error)
+{
+ struct server *s = user_data;
+ buffer_send_error (s->ctx, s->buffer, "Connection failed: %s", error);
+}
+
+static void
+irc_on_connector_failure (void *user_data)
+{
+ struct server *s = user_data;
+ irc_destroy_connector (s);
+ irc_queue_reconnect (s);
+}
+
+static void
+irc_on_connector_connected (void *user_data, int socket)
+{
+ struct server *s = user_data;
+ irc_destroy_connector (s);
+ irc_finish_connection (s, socket);
+}
+
+static bool
+irc_setup_connector (struct server *s,
+ const char *host, const char *port, struct error **e)
+{
+ struct connector *connector = xmalloc (sizeof *connector);
+ connector_init (connector, &s->ctx->poller);
+
+ connector->user_data = s;
+ connector->on_connecting = irc_on_connector_connecting;
+ connector->on_error = irc_on_connector_error;
+ connector->on_connected = irc_on_connector_connected;
+ connector->on_failure = irc_on_connector_failure;
- if (!gai_iter)
+ s->state = IRC_CONNECTING;
+ s->connector = connector;
+
+ if (!connector_add_target (connector, host, port, e))
{
- error_set (e, "connection failed");
+ irc_destroy_connector (s);
return false;
}
- s->socket = sockfd;
+ connector_step (connector);
return true;
}
-// --- More readline funky stuff -----------------------------------------------
+static void
+irc_initiate_connect (struct server *s)
+{
+ hard_assert (s->state == IRC_DISCONNECTED);
+ struct app_context *ctx = s->ctx;
+
+ const char *irc_host = get_config_string (ctx, "server.irc_host");
+ int64_t irc_port_int = get_config_integer (ctx, "server.irc_port");
+
+ if (!get_config_string (ctx, "server.irc_host"))
+ {
+ // No sense in trying to reconnect
+ buffer_send_error (ctx, s->buffer,
+ "No hostname specified in configuration");
+ return;
+ }
+
+ const char *socks_host = get_config_string (ctx, "server.socks_host");
+ int64_t socks_port_int = get_config_integer (ctx, "server.socks_port");
+
+ const char *socks_username =
+ get_config_string (ctx, "server.socks_username");
+ const char *socks_password =
+ get_config_string (ctx, "server.socks_password");
+
+ char *irc_port = xstrdup_printf ("%" PRIi64, irc_port_int);
+ char *socks_port = xstrdup_printf ("%" PRIi64, socks_port_int);
+
+ // TODO: the SOCKS code needs a rewrite so that we don't block on it either
+ struct error *e = NULL;
+ if (socks_host)
+ {
+ char *address = format_host_port_pair (irc_host, irc_port);
+ char *socks_address = format_host_port_pair (socks_host, socks_port);
+ buffer_send_status (ctx, s->buffer,
+ "Connecting to %s via %s...", address, socks_address);
+ free (socks_address);
+ free (address);
+
+ struct error *error = NULL;
+ int fd = socks_connect (socks_host, socks_port, irc_host, irc_port,
+ socks_username, socks_password, &error);
+ if (fd != -1)
+ irc_finish_connection (s, fd);
+ else
+ {
+ error_set (&e, "%s: %s", "SOCKS connection failed", error->message);
+ error_free (error);
+ }
+ }
+ else
+ irc_setup_connector (s, irc_host, irc_port, &e);
+
+ free (irc_port);
+ free (socks_port);
+
+ if (e)
+ {
+ buffer_send_error (s->ctx, s->buffer, "%s", e->message);
+ error_free (e);
+ irc_queue_reconnect (s);
+ }
+}
+
+// --- Input prompt ------------------------------------------------------------
static char *
make_unseen_prefix (struct app_context *ctx)
@@ -4428,7 +4775,7 @@ handle_command_connect (struct app_context *ctx, char *arguments)
}
irc_cancel_timers (s);
- on_irc_reconnect_timeout (s);
+ irc_initiate_connect (s);
return true;
}
@@ -4725,318 +5072,6 @@ process_input (struct app_context *ctx, char *user_input)
free (input);
}
-// --- Supporting code (continued) ---------------------------------------------
-
-enum irc_read_result
-{
- IRC_READ_OK, ///< Some data were read successfully
- IRC_READ_EOF, ///< The server has closed connection
- IRC_READ_AGAIN, ///< No more data at the moment
- IRC_READ_ERROR ///< General connection failure
-};
-
-static enum irc_read_result
-irc_fill_read_buffer_ssl (struct server *s, struct str *buf)
-{
- int n_read;
-start:
- n_read = SSL_read (s->ssl, buf->str + buf->len,
- buf->alloc - buf->len - 1 /* null byte */);
-
- const char *error_info = NULL;
- switch (xssl_get_error (s->ssl, n_read, &error_info))
- {
- case SSL_ERROR_NONE:
- buf->str[buf->len += n_read] = '\0';
- return IRC_READ_OK;
- case SSL_ERROR_ZERO_RETURN:
- return IRC_READ_EOF;
- case SSL_ERROR_WANT_READ:
- return IRC_READ_AGAIN;
- case SSL_ERROR_WANT_WRITE:
- {
- // Let it finish the handshake as we don't poll for writability;
- // any errors are to be collected by SSL_read() in the next iteration
- struct pollfd pfd = { .fd = s->socket, .events = POLLOUT };
- soft_assert (poll (&pfd, 1, 0) > 0);
- goto start;
- }
- case XSSL_ERROR_TRY_AGAIN:
- goto start;
- default:
- LOG_FUNC_FAILURE ("SSL_read", error_info);
- return IRC_READ_ERROR;
- }
-}
-
-static enum irc_read_result
-irc_fill_read_buffer (struct server *s, struct str *buf)
-{
- ssize_t n_read;
-start:
- n_read = recv (s->socket, buf->str + buf->len,
- buf->alloc - buf->len - 1 /* null byte */, 0);
-
- if (n_read > 0)
- {
- buf->str[buf->len += n_read] = '\0';
- return IRC_READ_OK;
- }
- if (n_read == 0)
- return IRC_READ_EOF;
-
- if (errno == EAGAIN)
- return IRC_READ_AGAIN;
- if (errno == EINTR)
- goto start;
-
- LOG_LIBC_FAILURE ("recv");
- return IRC_READ_ERROR;
-}
-
-static void
-irc_cancel_timers (struct server *s)
-{
- poller_timer_reset (&s->timeout_tmr);
- poller_timer_reset (&s->ping_tmr);
- poller_timer_reset (&s->reconnect_tmr);
-}
-
-static void
-irc_queue_reconnect (struct server *s)
-{
- // TODO: exponentional backoff
- hard_assert (s->socket == -1);
- buffer_send_status (s->ctx, s->buffer,
- "Trying to reconnect in %ld seconds...", s->ctx->reconnect_delay);
- poller_timer_set (&s->reconnect_tmr, s->ctx->reconnect_delay * 1000);
-}
-
-static void
-on_irc_reconnect_timeout (void *user_data)
-{
- struct server *s = user_data;
-
- struct error *e = NULL;
- bool should_retry = false;
- if (irc_connect (s, &should_retry, &e))
- return;
-
- buffer_send_error (s->ctx, s->buffer, "%s", e->message);
- error_free (e);
-
- if (should_retry)
- irc_queue_reconnect (s);
-}
-
-static void
-on_irc_disconnected (struct server *s)
-{
- // Get rid of the dead socket and related things
- if (s->ssl)
- {
- SSL_free (s->ssl);
- s->ssl = NULL;
- SSL_CTX_free (s->ssl_ctx);
- s->ssl_ctx = NULL;
- }
-
- xclose (s->socket);
- s->socket = -1;
- s->state = IRC_DISCONNECTED;
-
- user_unref (s->irc_user);
- s->irc_user = NULL;
-
- free (s->irc_user_mode);
- s->irc_user_mode = NULL;
- free (s->irc_user_host);
- s->irc_user_host = NULL;
-
- s->read_event.closed = true;
- poller_fd_reset (&s->read_event);
-
- // All of our timers have lost their meaning now
- irc_cancel_timers (s);
-
- if (s->ctx->quitting)
- try_finish_quit (s->ctx);
- else if (!s->ctx->reconnect)
- // XXX: not sure if we want this in a client
- // FIXME: no, we don't, would need to be changed for multiserver anyway
- initiate_quit (s->ctx);
- else
- irc_queue_reconnect (s);
-}
-
-static void
-on_irc_ping_timeout (void *user_data)
-{
- struct server *s = user_data;
- buffer_send_error (s->ctx, s->buffer, "Connection timeout");
- on_irc_disconnected (s);
-}
-
-static void
-on_irc_timeout (void *user_data)
-{
- // Provoke a response from the server
- struct server *s = user_data;
- irc_send (s, "PING :%s", get_config_string (s->ctx, "server.nickname"));
-}
-
-static void
-irc_reset_connection_timeouts (struct server *s)
-{
- irc_cancel_timers (s);
- poller_timer_set (&s->timeout_tmr, 3 * 60 * 1000);
- poller_timer_set (&s->ping_tmr, (3 * 60 + 30) * 1000);
-}
-
-static void
-on_irc_readable (const struct pollfd *fd, struct server *s)
-{
- if (fd->revents & ~(POLLIN | POLLHUP | POLLERR))
- print_debug ("fd %d: unexpected revents: %d", fd->fd, fd->revents);
-
- (void) set_blocking (s->socket, false);
-
- struct str *buf = &s->read_buffer;
- enum irc_read_result (*fill_buffer)(struct server *, struct str *)
- = s->ssl
- ? irc_fill_read_buffer_ssl
- : irc_fill_read_buffer;
- bool disconnected = false;
- while (true)
- {
- str_ensure_space (buf, 512);
- switch (fill_buffer (s, buf))
- {
- case IRC_READ_AGAIN:
- goto end;
- case IRC_READ_ERROR:
- buffer_send_error (s->ctx, s->buffer,
- "Reading from the IRC server failed");
- disconnected = true;
- goto end;
- case IRC_READ_EOF:
- buffer_send_error (s->ctx, s->buffer,
- "The IRC server closed the connection");
- disconnected = true;
- goto end;
- case IRC_READ_OK:
- break;
- }
-
- if (buf->len >= (1 << 20))
- {
- buffer_send_error (s->ctx, s->buffer,
- "The IRC server seems to spew out data frantically");
- irc_shutdown (s);
- goto end;
- }
- }
-end:
- (void) set_blocking (s->socket, true);
- irc_process_buffer (buf, irc_process_message, s);
-
- if (disconnected)
- on_irc_disconnected (s);
- else
- irc_reset_connection_timeouts (s);
-}
-
-static bool
-irc_connect (struct server *s, bool *should_retry, struct error **e)
-{
- // TODO: connect asynchronously so that we don't freeze
- struct app_context *ctx = s->ctx;
- *should_retry = true;
-
- const char *irc_host = get_config_string (ctx, "server.irc_host");
- int64_t irc_port_int = get_config_integer (ctx, "server.irc_port");
-
- if (!get_config_string (ctx, "server.irc_host"))
- {
- error_set (e, "No hostname specified in configuration");
- *should_retry = false;
- return false;
- }
-
- const char *socks_host = get_config_string (ctx, "server.socks_host");
- int64_t socks_port_int = get_config_integer (ctx, "server.socks_port");
- const char *socks_username =
- get_config_string (ctx, "server.socks_username");
- const char *socks_password =
- get_config_string (ctx, "server.socks_password");
-
- // FIXME: use it as a number everywhere, there's no need for named services
- // FIXME: memory leak
- char *irc_port = xstrdup_printf ("%" PRIi64, irc_port_int);
- char *socks_port = xstrdup_printf ("%" PRIi64, socks_port_int);
-
- const char *nickname = get_config_string (ctx, "server.nickname");
- const char *username = get_config_string (ctx, "server.username");
- const char *realname = get_config_string (ctx, "server.realname");
-
- // These are filled automatically if needed
- hard_assert (nickname && username && realname);
-
- bool use_ssl = get_config_boolean (ctx, "server.ssl");
- if (socks_host)
- {
- char *address = format_host_port_pair (irc_host, irc_port);
- char *socks_address = format_host_port_pair (socks_host, socks_port);
- buffer_send_status (ctx, s->buffer,
- "Connecting to %s via %s...", address, socks_address);
- free (socks_address);
- free (address);
-
- struct error *error = NULL;
- int fd = socks_connect (socks_host, socks_port, irc_host, irc_port,
- socks_username, socks_password, &error);
- if (fd == -1)
- {
- error_set (e, "%s: %s", "SOCKS connection failed", error->message);
- error_free (error);
- return false;
- }
- s->socket = fd;
- }
- else if (!irc_establish_connection (s, irc_host, irc_port, e))
- return false;
-
- if (use_ssl && !irc_initialize_ssl (s, e))
- {
- xclose (s->socket);
- s->socket = -1;
- return false;
- }
-
- s->state = IRC_CONNECTED;
- buffer_send_status (ctx, s->buffer, "Connection established");
-
- poller_fd_init (&s->read_event, &ctx->poller, s->socket);
- s->read_event.dispatcher = (poller_fd_fn) on_irc_readable;
- s->read_event.user_data = s;
-
- poller_fd_set (&s->read_event, POLLIN);
- irc_reset_connection_timeouts (s);
-
- irc_send (s, "NICK %s", nickname);
- // IRC servers may ignore the last argument if it's empty
- irc_send (s, "USER %s 8 * :%s", username, *realname ? realname : " ");
-
- // XXX: maybe we should wait for the first message from the server
- // FIXME: the user may exist already after we've reconnected. Either
- // make sure that there's no reference of this nick upon disconnection,
- // or search in "irc_users" first... or something.
- s->irc_user = irc_make_user (s, xstrdup (nickname));
- s->irc_user_mode = xstrdup ("");
- s->irc_user_host = NULL;
- return true;
-}
-
// --- Word completion ---------------------------------------------------------
// The amount of crap that goes into this is truly insane.
@@ -5854,11 +5889,11 @@ load_configuration (struct app_context *ctx)
error_free (e);
}
- ctx->reconnect =
- get_config_boolean (ctx, "server.reconnect");
ctx->isolate_buffers =
get_config_boolean (ctx, "behaviour.isolate_buffers");
- ctx->reconnect_delay =
+ ctx->server.reconnect =
+ get_config_boolean (ctx, "server.reconnect");
+ ctx->server.reconnect_delay =
get_config_integer (ctx, "server.reconnect_delay");
}