changeset 27:5ba185ca7102

Fixed error checking during initialization, restructured code a bit to make it even simpler
author Ivo Smits <Ivo@UCIS.nl>
date Sat, 09 Apr 2011 17:36:48 +0200
parents 68c67c6d2080
children e77af6acb559
files src/common.c src/proto.nacl0.c src/proto.nacltai.c src/proto.raw.c src/run.combined.c
diffstat 5 files changed, 47 insertions(+), 70 deletions(-) [+]
line wrap: on
line diff
--- a/src/common.c	Fri Apr 08 17:08:04 2011 +0200
+++ b/src/common.c	Sat Apr 09 17:36:48 2011 +0200
@@ -206,7 +206,7 @@
 	char protocol_data[p->protocol_data_size];
 	memset(protocol_data, 0, p->protocol_data_size);
 	session.protocol_data = &protocol_data;
-	if (p->init) p->init(&session);
+	if (p->init && p->init(&session) < 0) return -1;
 
 	fprintf(stderr, "The tunnel is now operational!\n");
 
--- a/src/proto.nacl0.c	Fri Apr 08 17:08:04 2011 +0200
+++ b/src/proto.nacl0.c	Sat Apr 09 17:36:48 2011 +0200
@@ -30,12 +30,6 @@
 	unsigned char cnonce[crypto_box_curve25519xsalsa20poly1305_NONCEBYTES], cbefore[crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES];
 };
 
-/*static unsigned char cnonce[crypto_box_curve25519xsalsa20poly1305_NONCEBYTES], cbefore[crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES];
-static unsigned char buffer1[MAX_PACKET_LEN+crypto_box_curve25519xsalsa20poly1305_ZEROBYTES], buffer2[MAX_PACKET_LEN+crypto_box_curve25519xsalsa20poly1305_ZEROBYTES];
-static const unsigned char* buffer1offset = buffer1 + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES;
-static const unsigned char* buffer2offset = buffer2 + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES;
-static const int overhead                 = crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES;*/
-
 static int encode(struct qtsession* sess, char* raw, char* enc, int len) {
 	struct qt_proto_data_nacl0* d = (struct qt_proto_data_nacl0*)sess->protocol_data;
 	memset(raw, 0, crypto_box_curve25519xsalsa20poly1305_ZEROBYTES);
@@ -75,28 +69,21 @@
 	return 0;
 }
 
-#ifdef COMBINED_BINARY
-	int tunmain_nacl0() {
-#else
-	int tunmain() {
-#endif
-	struct qtproto p = {
-		1,
-		MAX_PACKET_LEN + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES,
-		MAX_PACKET_LEN + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES,
-		crypto_box_curve25519xsalsa20poly1305_ZEROBYTES,
-		crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES,
-		encode,
-		decode,
-		init,
-		sizeof(struct qt_proto_data_nacl0),
-	};
-	return qtrun(&p);
-}
+struct qtproto qtproto_nacl0 = {
+	1,
+	MAX_PACKET_LEN + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES,
+	MAX_PACKET_LEN + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES + crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES,
+	crypto_box_curve25519xsalsa20poly1305_ZEROBYTES,
+	crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES,
+	encode,
+	decode,
+	init,
+	sizeof(struct qt_proto_data_nacl0),
+};
 
 #ifndef COMBINED_BINARY
 int main() {
 	print_header();
-	return tunmain();
+	return qtrun(&qtproto_nacl0);
 }
 #endif
--- a/src/proto.nacltai.c	Fri Apr 08 17:08:04 2011 +0200
+++ b/src/proto.nacltai.c	Sat Apr 09 17:36:48 2011 +0200
@@ -189,28 +189,21 @@
 	return 0;
 }
 
-#ifdef COMBINED_BINARY
-	int tunmain_nacltai() {
-#else
-	int tunmain() {
-#endif
-	struct qtproto p = {
-		1,
-		MAX_PACKET_LEN + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES,
-		MAX_PACKET_LEN + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES,
-		crypto_box_curve25519xsalsa20poly1305_ZEROBYTES,
-		crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES - noncelength,
-		encode,
-		decode,
-		init,
-		sizeof(struct qt_proto_data_nacltai),
-	};
-	return qtrun(&p);
-}
+struct qtproto qtproto_nacltai = {
+	1,
+	MAX_PACKET_LEN + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES,
+	MAX_PACKET_LEN + crypto_box_curve25519xsalsa20poly1305_ZEROBYTES,
+	crypto_box_curve25519xsalsa20poly1305_ZEROBYTES,
+	crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES - noncelength,
+	encode,
+	decode,
+	init,
+	sizeof(struct qt_proto_data_nacltai),
+};
 
 #ifndef COMBINED_BINARY
 int main() {
 	print_header();
-	return tunmain();
+	return qtrun(&qtproto_nacltai);
 }
 #endif
--- a/src/proto.raw.c	Fri Apr 08 17:08:04 2011 +0200
+++ b/src/proto.raw.c	Sat Apr 09 17:36:48 2011 +0200
@@ -35,28 +35,21 @@
 	return len;
 }
 
-#ifdef COMBINED_BINARY
-	int tunmain_raw() {
-#else
-	int tunmain() {
-#endif
-	struct qtproto p = {
-		0,
-		MAX_PACKET_LEN,
-		MAX_PACKET_LEN,
-		0,
-		0,
-		decode,
-		encode,
-		NULL,
-		0,
-	};
-	return qtrun(&p);
-}
+struct qtproto qtproto_raw = {
+	0,
+	MAX_PACKET_LEN,
+	MAX_PACKET_LEN,
+	0,
+	0,
+	decode,
+	encode,
+	NULL,
+	0,
+};
 
 #ifndef COMBINED_BINARY
 int main() {
 	print_header();
-	return tunmain();
+	return qtrun(&qtproto_raw);
 }
 #endif
--- a/src/run.combined.c	Fri Apr 08 17:08:04 2011 +0200
+++ b/src/run.combined.c	Sat Apr 09 17:36:48 2011 +0200
@@ -25,6 +25,10 @@
 
 #include "common.c"
 
+struct qtproto qtproto_raw;
+struct qtproto qtproto_nacl0;
+struct qtproto qtproto_nacltai;
+
 #ifdef DEBIAN_BINARY
 char* getenvdeb(const char* name) {
 	char tmp[1024] = "IF_QT_";
@@ -48,18 +52,18 @@
 	char* envval;
 	if (envval = getconf("PROTOCOL")) {
 		if (strcmp(envval, "raw") == 0) {
-			return tunmain_raw();
+			return qtrun(&qtproto_raw);
 		} else if (strcmp(envval, "nacl0") == 0) {
-			return tunmain_nacl0();
+			return qtrun(&qtproto_nacl0);
 		} else if (strcmp(envval, "nacltai") == 0) {
-			return tunmain_nacltai();
+			return qtrun(&qtproto_nacltai);
 		} else {
 			fprintf(stderr, "Unknown protocol specified: %s\n", envval);
 			return -1;
 		}
 	} else if (getconf("PRIVATE_KEY")) {
-		return tunmain_nacl0();
+		return qtrun(&qtproto_nacl0);
 	} else {
-		return tunmain_raw();
+		return qtrun(&qtproto_raw);
 	}
-}
\ No newline at end of file
+}