changeset 6:cf9b44b46be5

Use stderr for output instead of stdout, added debugging code to nacltai
author root <root@Really.UFO-Net.nl>
date Fri, 08 Oct 2010 23:29:56 +0000
parents 9d449e899402
children fd7c60905b13
files src/common.c src/proto.nacl0.c src/proto.nacltai.c
diffstat 3 files changed, 20 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/src/common.c	Sat Oct 09 01:28:25 2010 +0200
+++ b/src/common.c	Fri Oct 08 23:29:56 2010 +0000
@@ -81,13 +81,13 @@
 }
 
 void print_header() {
-	printf("UCIS QuickTun (c) 2010 Ivo Smits <Ivo@UCIS.nl>\n");
-	printf("More information: http://wiki.qontrol.nl/QuickTun\n");
+	fprintf(stderr, "UCIS QuickTun (c) 2010 Ivo Smits <Ivo@UCIS.nl>\n");
+	fprintf(stderr, "More information: http://wiki.qontrol.nl/QuickTun\n");
 }
 
 int init_udp(struct qtsession* session) {
 	char* envval;
-	printf("Initializing UDP socket...\n");
+	fprintf(stderr, "Initializing UDP socket...\n");
 	int sfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
 	if (sfd < 0) return errorexitp("Could not create UDP socket");
 	struct sockaddr_in udpaddr;
@@ -132,7 +132,7 @@
 
 int init_tuntap() {
 	char* envval;
-	printf("Initializing tap device...\n");
+	fprintf(stderr, "Initializing tap device...\n");
 	int ttfd; //Tap device file descriptor
 	struct ifreq ifr; //required for tun/tap setup
 	memset(&ifr, 0, sizeof(ifr));
@@ -161,18 +161,20 @@
 int qtrun(struct qtproto* p) {
 	struct qtsession session;
 	session.protocol = *p;
+
 	init_udp(&session);
+	int sfd = session.fd_socket;
+	if (sfd == -1) return -1;
+
 	session.fd_dev = init_tuntap();
+	int ttfd = session.fd_dev;
+	if (ttfd == -1) return -1;
 
 	char protocol_data[p->protocol_data_size];
 	session.protocol_data = &protocol_data;
 	if (p->init) p->init(&session);
 
-	int sfd = session.fd_socket;
-	int ttfd = session.fd_dev;
-	if (sfd == -1) return -1;
-	if (ttfd == -1) return -1;
-	printf("The tunnel is now operational!\n");
+	fprintf(stderr, "The tunnel is now operational!\n");
 
 	struct pollfd fds[2];
 	fds[0].fd = ttfd;
--- a/src/proto.nacl0.c	Sat Oct 09 01:28:25 2010 +0200
+++ b/src/proto.nacl0.c	Fri Oct 08 23:29:56 2010 +0000
@@ -72,6 +72,7 @@
 	if (strlen(envval) != 2*crypto_box_PUBLICKEYBYTES) return errorexit("PRIVATE_KEY length");
 	hex2bin(csecretkey, envval, crypto_box_SECRETKEYBYTES);
 	crypto_box_beforenm(d->cbefore, cpublickey, csecretkey);
+	return 0;
 }
 
 #ifdef COMBINED_BINARY
--- a/src/proto.nacltai.c	Sat Oct 09 01:28:25 2010 +0200
+++ b/src/proto.nacltai.c	Fri Oct 08 23:29:56 2010 +0000
@@ -45,7 +45,7 @@
 	struct taia cdtaip, cdtaic;
 };
 
-#define noncelength sizeof(struct taia)
+#define noncelength 16
 #define nonceoffset (crypto_box_NONCEBYTES - noncelength)
 /*static unsigned char cbefore[crypto_box_BEFORENMBYTES];
 static unsigned char buffer1[MAX_PACKET_LEN+crypto_box_ZEROBYTES], buffer2[MAX_PACKET_LEN+crypto_box_ZEROBYTES];
@@ -119,6 +119,7 @@
 extern crypto_scalarmult_curve25519_base(unsigned char *pk, unsigned char *sk);
 
 static int encode(struct qtsession* sess, char* raw, char* enc, int len) {
+	fprintf(stderr, "Encoding packet of %d bytes from %d to %d\n", len, raw, enc);
 	struct qt_proto_data_nacltai* d = (struct qt_proto_data_nacltai*)sess->protocol_data;
 	memset(raw, 0, crypto_box_ZEROBYTES);
 	taia_now(&d->cdtaic);
@@ -126,10 +127,12 @@
 	if (crypto_box_afternm(enc, raw, len + crypto_box_ZEROBYTES, d->cenonce, d->cbefore)) return errorexit("Crypto failed");
 	memcpy((void*)(enc + crypto_box_BOXZEROBYTES - noncelength), d->cenonce + nonceoffset, noncelength);
 	len += overhead;
+	fprintf(stderr, "Encoded packet of %d bytes from %d to %d\n", len, raw, enc);
 	return len;
 }
 
 static int decode(struct qtsession* sess, char* enc, char* raw, int len) {
+	fprintf(stderr, "Decoding packet of %d bytes from %d to %d\n", len, enc, raw);
 	struct qt_proto_data_nacltai* d = (struct qt_proto_data_nacltai*)sess->protocol_data;
 	int i;
 	if (len < overhead) {
@@ -137,7 +140,7 @@
 		return 0;
 	}
 	len -= overhead;
-	taia_unpack((char*)(enc + crypto_box_BOXZEROBYTES - noncelength), &(d->cdtaic));
+	taia_unpack((char*)(enc + crypto_box_BOXZEROBYTES - noncelength), &d->cdtaic);
 	if (d->cdtaic.sec.x <= d->cdtaip.sec.x || d->cdtaic.nano <= d->cdtaip.nano || d->cdtaic.atto <= d->cdtaip.atto) { 
 		fprintf(stderr, "Timestamp going back, ignoring packet\n");
 		return 0;
@@ -149,6 +152,7 @@
 		return 0;
 	}
 	d->cdtaip = d->cdtaic;
+	fprintf(stderr, "Decoded packet of %d bytes from %d to %d\n", len, enc, raw);
 	return len;
 }
 
@@ -180,9 +184,10 @@
 	if (envval = getenv("ROLE")) {
 		d->cenonce[nonceoffset-1] = atoi(envval) ? 1 : 0;
 	} else {
-		d->cenonce[nonceoffset-1] = memcmp(cpublickey, cownpublickey, crypto_box_PUBLICKEYBYTES) ? 1 : 0;
+		d->cenonce[nonceoffset-1] = memcmp(cownpublickey, cpublickey, crypto_box_PUBLICKEYBYTES) > 0 ? 1 : 0;
 	}
 	d->cdnonce[nonceoffset-1] = d->cenonce[nonceoffset-1] ? 0 : 1;
+	return 0;
 }
 
 #ifdef COMBINED_BINARY