From: Vsevolod Stakhov Date: Wed, 30 Mar 2016 16:08:58 +0000 (+0100) Subject: [Fix] Improve parsing of the connect string for the client X-Git-Tag: 1.2.2~17 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7883c67c16b869e562b0fcdda103d76fafeb0f52;p=rspamd.git [Fix] Improve parsing of the connect string for the client --- diff --git a/src/client/rspamc.c b/src/client/rspamc.c index 30d1b57cc..f3d8f1fc8 100644 --- a/src/client/rspamc.c +++ b/src/client/rspamc.c @@ -1251,31 +1251,47 @@ rspamc_process_input (struct event_base *ev_base, struct rspamc_command *cmd, FILE *in, const gchar *name, GQueue *attrs) { struct rspamd_client_connection *conn; - gchar **connectv; + gchar *hostbuf = NULL, *p; guint16 port; GError *err = NULL; struct rspamc_callback_data *cbdata; - connectv = g_strsplit_set (connect_str, ":", -1); + if (connect_str[0] == '[') { + p = strrchr (connect_str, ']'); - if (connectv == NULL || connectv[0] == NULL) { - fprintf (stderr, "bad connect string: %s\n", connect_str); - exit (EXIT_FAILURE); + if (p != NULL) { + hostbuf = g_malloc (p - connect_str); + rspamd_strlcpy (hostbuf, connect_str + 1, p - connect_str); + p ++; + } + else { + p = connect_str; + } } + else { + p = connect_str; + } + + p = strrchr (p, ':'); - if (connectv[1] != NULL) { - port = strtoul (connectv[1], NULL, 10); + if (p != NULL) { + port = strtoul (p + 1, NULL, 10); } - else if (*connectv[0] != '/') { + else { port = cmd->is_controller ? DEFAULT_CONTROL_PORT : DEFAULT_PORT; } - else { - /* Unix socket */ - port = 0; + + if (!hostbuf) { + if (p != NULL) { + hostbuf = g_malloc (p - connect_str + 1); + rspamd_strlcpy (hostbuf, connect_str, p - connect_str + 1); + } + else { + hostbuf = g_strdup (connect_str); + } } - conn = rspamd_client_init (ev_base, connectv[0], port, timeout, key); - g_strfreev (connectv); + conn = rspamd_client_init (ev_base, hostbuf, port, timeout, key); if (conn != NULL) { cbdata = g_slice_alloc (sizeof (struct rspamc_callback_data)); @@ -1297,6 +1313,8 @@ rspamc_process_input (struct event_base *ev_base, struct rspamc_command *cmd, &err); } } + + g_free (hostbuf); } static void