]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
dsync: Added handshaking to proxy protocol.
authorTimo Sirainen <tss@iki.fi>
Wed, 18 Nov 2009 20:19:39 +0000 (15:19 -0500)
committerTimo Sirainen <tss@iki.fi>
Wed, 18 Nov 2009 20:19:39 +0000 (15:19 -0500)
--HG--
branch : HEAD

src/dsync/dsync-proxy-client.c
src/dsync/dsync-proxy-server.c
src/dsync/dsync-proxy-server.h
src/dsync/dsync-proxy.h

index 390b8137cde5bc425b24b5ef1737bfef3cc8eb8a..67fc6bc17f894cead0caeb285d3dc5456efb86e1 100644 (file)
@@ -64,6 +64,7 @@ struct proxy_client_dsync_worker {
        ARRAY_DEFINE(request_array, struct proxy_client_request);
        struct aqueue *request_queue;
 
+       unsigned int handshake_received:1;
        unsigned int finished:1;
 };
 
@@ -101,7 +102,19 @@ proxy_client_worker_read_line(struct proxy_client_dsync_worker *worker,
                        return -1;
                }
        }
-       return *line_r != NULL ? 1 : 0;
+       if (*line_r == NULL)
+               return 0;
+
+       if (!worker->handshake_received) {
+               if (strcmp(*line_r, DSYNC_PROXY_SERVER_GREETING_LINE) != 0) {
+                       i_error("Invalid server handshake: %s", *line_r);
+                       dsync_worker_set_failure(&worker->worker);
+                       return -1;
+               }
+               worker->handshake_received = TRUE;
+               return proxy_client_worker_read_line(worker, line_r);
+       }
+       return 1;
 }
 
 static void
@@ -283,6 +296,7 @@ struct dsync_worker *dsync_worker_init_proxy_client(int fd_in, int fd_out)
        worker->io = io_add(fd_in, IO_READ, proxy_client_worker_input, worker);
        worker->input = i_stream_create_fd(fd_in, (size_t)-1, FALSE);
        worker->output = o_stream_create_fd(fd_out, (size_t)-1, FALSE);
+       o_stream_send_str(worker->output, DSYNC_PROXY_CLIENT_GREETING_LINE"\n");
        /* we'll keep the output corked until flush is needed */
        o_stream_cork(worker->output);
        o_stream_set_flush_callback(worker->output, proxy_client_worker_output,
index e82817206726bdb543c7727cae954280efd47326..2d27da6681cdcfa86df3ca38ec472406ef078242 100644 (file)
@@ -31,7 +31,19 @@ proxy_server_read_line(struct dsync_proxy_server *server,
                        return -1;
                }
        }
-       return *line_r != NULL ? 1 : 0;
+       if (*line_r == NULL)
+               return 0;
+
+       if (!server->handshake_received) {
+               if (strcmp(*line_r, DSYNC_PROXY_CLIENT_GREETING_LINE) != 0) {
+                       i_error("Invalid client handshake: %s", *line_r);
+                       master_service_stop(master_service);
+                       return -1;
+               }
+               server->handshake_received = TRUE;
+               return proxy_server_read_line(server, line_r);
+       }
+       return 1;
 }
 
 static int proxy_server_run_cmd(struct dsync_proxy_server *server)
@@ -162,6 +174,7 @@ dsync_proxy_server_init(int fd_in, int fd_out, struct dsync_worker *worker)
                                 dsync_proxy_server_timeout, NULL);
        o_stream_set_flush_callback(server->output, proxy_server_output,
                                    server);
+       o_stream_send_str(server->output, DSYNC_PROXY_SERVER_GREETING_LINE"\n");
        fd_set_nonblock(fd_in, TRUE);
        fd_set_nonblock(fd_out, TRUE);
        return server;
index 4958a7c0a6f2b98ae0234ca9a879c362882ba25b..727ca33532df0108c8b989e8a4fd05360748d9f1 100644 (file)
@@ -30,6 +30,7 @@ struct dsync_proxy_server {
        bool get_input_last_lf;
        uint32_t get_uid;
 
+       unsigned int handshake_received:1;
        unsigned int subs_sending_unsubscriptions:1;
        unsigned int finished:1;
 };
index e970f6ea44299f3c292cb2ef0bf86ddc8855c6c8..f05c2355bc168e4261c94107bf7dc59c487b0a67 100644 (file)
@@ -4,6 +4,8 @@
 #include "dsync-data.h"
 
 #define DSYNC_PROXY_TIMEOUT_MSECS (15*60*1000)
+#define DSYNC_PROXY_CLIENT_GREETING_LINE "dsync-client\t1"
+#define DSYNC_PROXY_SERVER_GREETING_LINE "dsync-server\t1"
 
 struct dsync_message;
 struct dsync_mailbox;