dovecot-2.0: Added version_string_verify() and removed code dupl...

dovecot at dovecot.org dovecot at dovecot.org
Wed Jun 2 20:27:22 EEST 2010


details:   http://hg.dovecot.org/dovecot-2.0/rev/4274d1549fa0
changeset: 11458:4274d1549fa0
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Jun 02 18:27:08 2010 +0100
description:
Added version_string_verify() and removed code duplication with it.

diffstat:

 src/anvil/anvil-connection.c      |  10 ++++------
 src/auth/auth-worker-client.c     |   3 +--
 src/config/config-connection.c    |   3 +--
 src/director/doveadm-connection.c |   8 ++++----
 src/doveadm/doveadm-director.c    |   7 +++----
 src/lib-master/master-service.c   |  25 +++++++++++++++++++++++--
 src/lib-master/master-service.h   |   6 ++++++
 7 files changed, 42 insertions(+), 20 deletions(-)

diffs (171 lines):

diff -r addb2c6c1dfb -r 4274d1549fa0 src/anvil/anvil-connection.c
--- a/src/anvil/anvil-connection.c	Wed Jun 02 17:21:21 2010 +0100
+++ b/src/anvil/anvil-connection.c	Wed Jun 02 18:27:08 2010 +0100
@@ -131,7 +131,7 @@
 static void anvil_connection_input(void *context)
 {
 	struct anvil_connection *conn = context;
-	const char *const *args, *error;
+	const char *line, *const *args, *error;
 
 	switch (i_stream_read(conn->input)) {
 	case -2:
@@ -144,13 +144,11 @@
 	}
 
 	if (!conn->version_received) {
-		if ((args = anvil_connection_next_line(conn)) == NULL)
+		if ((line = i_stream_next_line(conn->input)) == NULL)
 			return;
 
-		if (str_array_length(args) < 4 ||
-		    strcmp(args[0], "VERSION") != 0 ||
-		    strcmp(args[1], "anvil") != 0 ||
-		    !str_uint_equals(args[2], ANVIL_CLIENT_PROTOCOL_MAJOR_VERSION)) {
+		if (!version_string_verify(line, "anvil",
+				ANVIL_CLIENT_PROTOCOL_MAJOR_VERSION)) {
 			i_error("Anvil client not compatible with this server "
 				"(mixed old and new binaries?)");
 			anvil_connection_destroy(conn);
diff -r addb2c6c1dfb -r 4274d1549fa0 src/auth/auth-worker-client.c
--- a/src/auth/auth-worker-client.c	Wed Jun 02 17:21:21 2010 +0100
+++ b/src/auth/auth-worker-client.c	Wed Jun 02 18:27:08 2010 +0100
@@ -584,8 +584,7 @@
 		if (line == NULL)
 			return;
 
-		if (strncmp(line, "VERSION\tauth-worker\t", 20) != 0 ||
-		    !str_uint_equals(t_strcut(line + 20, '\t'),
+		if (!version_string_verify(line, "auth-worker",
 				     AUTH_WORKER_PROTOCOL_MAJOR_VERSION)) {
 			i_error("Auth worker not compatible with this server "
 				"(mixed old and new binaries?)");
diff -r addb2c6c1dfb -r 4274d1549fa0 src/config/config-connection.c
--- a/src/config/config-connection.c	Wed Jun 02 17:21:21 2010 +0100
+++ b/src/config/config-connection.c	Wed Jun 02 18:27:08 2010 +0100
@@ -162,8 +162,7 @@
 		if (line == NULL)
 			return;
 
-		if (strncmp(line, "VERSION\tconfig\t", 15) != 0 ||
-		    !str_uint_equals(t_strcut(line + 15, '\t'),
+		if (!version_string_verify(line, "config",
 				     CONFIG_CLIENT_PROTOCOL_MAJOR_VERSION)) {
 			i_error("Config client not compatible with this server "
 				"(mixed old and new binaries?)");
diff -r addb2c6c1dfb -r 4274d1549fa0 src/director/doveadm-connection.c
--- a/src/director/doveadm-connection.c	Wed Jun 02 17:21:21 2010 +0100
+++ b/src/director/doveadm-connection.c	Wed Jun 02 18:27:08 2010 +0100
@@ -18,8 +18,8 @@
 
 #include <unistd.h>
 
-#define DOVEADM_HANDSHAKE_EXPECTED "VERSION\tdirector-doveadm\t1\t"
-#define DOVEADM_HANDSHAKE DOVEADM_HANDSHAKE_EXPECTED"0\n"
+#define DOVEADM_PROTOCOL_VERSION_MAJOR 1
+#define DOVEADM_HANDSHAKE "VERSION\tdirector-doveadm\t1\t0\n"
 
 #define MAX_VALID_VHOST_COUNT 1000
 
@@ -199,8 +199,8 @@
 		if ((line = i_stream_read_next_line(conn->input)) == NULL)
 			return;
 
-		if (strncmp(line, DOVEADM_HANDSHAKE_EXPECTED,
-			    strlen(DOVEADM_HANDSHAKE_EXPECTED)) != 0) {
+		if (!version_string_verify(line, "director-doveadm",
+					   DOVEADM_PROTOCOL_VERSION_MAJOR)) {
 			i_error("doveadm not compatible with this server "
 				"(mixed old and new binaries?)");
 			doveadm_connection_deinit(&conn);
diff -r addb2c6c1dfb -r 4274d1549fa0 src/doveadm/doveadm-director.c
--- a/src/doveadm/doveadm-director.c	Wed Jun 02 17:21:21 2010 +0100
+++ b/src/doveadm/doveadm-director.c	Wed Jun 02 18:27:08 2010 +0100
@@ -3,6 +3,7 @@
 #include "lib.h"
 #include "network.h"
 #include "istream.h"
+#include "master-service.h"
 #include "doveadm.h"
 
 #include <stdio.h>
@@ -25,8 +26,7 @@
 
 static void director_connect(struct director_context *ctx)
 {
-#define DIRECTOR_HANDSHAKE_EXPECTED "VERSION\tdirector-doveadm\t1\t"
-#define DIRECTOR_HANDSHAKE DIRECTOR_HANDSHAKE_EXPECTED"0\n"
+#define DIRECTOR_HANDSHAKE "VERSION\tdirector-doveadm\t1\t0\n"
 	const char *line;
 	int fd;
 
@@ -41,8 +41,7 @@
 	line = i_stream_read_next_line(ctx->input);
 	if (line == NULL)
 		i_fatal("%s disconnected", ctx->socket_path);
-	if (strncmp(line, DIRECTOR_HANDSHAKE_EXPECTED,
-		    strlen(DIRECTOR_HANDSHAKE_EXPECTED)) != 0) {
+	if (!version_string_verify(line, "director-doveadm", 1)) {
 		i_fatal("%s not a compatible director-doveadm socket",
 			ctx->socket_path);
 	}
diff -r addb2c6c1dfb -r 4274d1549fa0 src/lib-master/master-service.c
--- a/src/lib-master/master-service.c	Wed Jun 02 17:21:21 2010 +0100
+++ b/src/lib-master/master-service.c	Wed Jun 02 18:27:08 2010 +0100
@@ -77,7 +77,7 @@
 	master_service_refresh_login_state(service);
 }
 
-static void master_service_verify_version(struct master_service *service)
+static void master_service_verify_version_string(struct master_service *service)
 {
 	if (service->version_string != NULL &&
 	    strcmp(service->version_string, PACKAGE_VERSION) != 0) {
@@ -175,7 +175,7 @@
 		i_set_failure_prefix(t_strdup_printf("%s: ", name));
 	}
 
-	master_service_verify_version(service);
+	master_service_verify_version_string(service);
 	return service;
 }
 
@@ -872,3 +872,24 @@
 		}
 	}
 }
+
+bool version_string_verify(const char *line, const char *service_name,
+			   unsigned major_version)
+{
+	unsigned int service_name_len = strlen(service_name);
+	bool ret;
+
+	if (strncmp(line, "VERSION\t", 8) != 0)
+		return FALSE;
+	line += 8;
+
+	if (strncmp(line, service_name, service_name_len) != 0 ||
+	    line[service_name_len] != '\t')
+		return FALSE;
+	line += service_name_len + 1;
+
+	T_BEGIN {
+		ret = str_uint_equals(t_strcut(line, '\t'), major_version);
+	} T_END;
+	return ret;
+}
diff -r addb2c6c1dfb -r 4274d1549fa0 src/lib-master/master-service.h
--- a/src/lib-master/master-service.h	Wed Jun 02 17:21:21 2010 +0100
+++ b/src/lib-master/master-service.h	Wed Jun 02 18:27:08 2010 +0100
@@ -119,4 +119,10 @@
 /* Deinitialize the service. */
 void master_service_deinit(struct master_service **service);
 
+/* Returns TRUE if line contains compatible service name and major version.
+   The line is expected to be in format:
+   VERSION <tab> service_name <tab> major version <tab> minor version */
+bool version_string_verify(const char *line, const char *service_name,
+			   unsigned major_version);
+
 #endif


More information about the dovecot-cvs mailing list