changeset 38:d9f5caa13898

Added support for NetBSD, added command line parsing to provide configuration options
author Ivo Smits <Ivo@UCIS.nl>
date Mon, 06 May 2013 22:53:20 +0200
parents bb4bbf380938
children 47a34fe75c57
files build.sh src/common.c src/proto.nacl0.c src/proto.nacltai.c src/proto.raw.c src/run.combined.c
diffstat 6 files changed, 46 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/build.sh	Mon May 06 21:28:45 2013 +0200
+++ b/build.sh	Mon May 06 22:53:20 2013 +0200
@@ -1,6 +1,6 @@
 #!/bin/sh
 
-if [ "$(uname -s)" = "OpenBSD" -o "$(uname -s)" = "FreeBSD" ]; then
+if [ "$(uname -s)" = "OpenBSD" -o "$(uname -s)" = "FreeBSD" -o "$(uname -s)" = "NetBSD" ]; then
 	echo "Detected *BSD"
 	tar="gtar"
 elif [ "$(uname -s)" = "SunOS" ]; then
@@ -53,6 +53,8 @@
 	export CPATH="/usr/include/nacl/:${CPATH}"
 fi
 
+CFLAGS="$CFLAGS -DQT_VERSION=\"`cat version`\""
+
 echo Building combined binary...
 gcc $CFLAGS -c -DCOMBINED_BINARY	src/proto.raw.c		-o obj/proto.raw.o
 gcc $CFLAGS -c -DCOMBINED_BINARY	src/proto.nacl0.c	-o obj/proto.nacl0.o
--- a/src/common.c	Mon May 06 21:28:45 2013 +0200
+++ b/src/common.c	Mon May 06 22:53:20 2013 +0200
@@ -84,6 +84,8 @@
 
 char* (*getconf)(const char*) = getenv;
 int debug = 0;
+static int gargc = 0;
+static char** gargv = NULL;
 
 int errorexit(const char* text) {
 	fprintf(stderr, "%s\n", text);
@@ -95,7 +97,7 @@
 }
 
 void print_header() {
-	fprintf(stderr, "UCIS QuickTun (c) 2010 Ivo Smits <Ivo@UCIS.nl>\n");
+	fprintf(stderr, "UCIS QuickTun "QT_VERSION" (c) 2010-2013 Ivo Smits <Ivo@UCIS.nl>\n");
 	fprintf(stderr, "More information: http://wiki.ucis.nl/QuickTun\n");
 }
 
@@ -312,5 +314,34 @@
 	}
 	return 0;
 }
+
+char* getconfcmdargs(const char* name) {
+	int i;
+	for (i = 1; i < gargc - 2; i++) {
+		if (strcmp(gargv[i], "-c")) continue;
+		if (strcmp(gargv[i + 1], name)) continue;
+		return gargv[i + 2];
+	}
+	return NULL;
+}
+
+int qtprocessargs(int argc, char** argv) {
+	int i;
+	for (i = 1; i < argc; i++) {
+		char* a = argv[i];
+		if (!strcmp(a, "-h") || !strcmp(a, "--help")) {
+			return errorexit("Please read the documentation at http://wiki.ucis.nl/QuickTun");
+		} else if (!strcmp(a, "-v") || !strcmp(a, "--version")) {
+			return errorexit("UCIS QuickTun "QT_VERSION);
+		} else if (!strcmp(a, "-c")) {
+			gargc = argc;
+			gargv = argv;
+			getconf = getconfcmdargs;
+			i += 2;
+		} else {
+			return errorexit("Unexpected command line argument");
+		}
+	}
+}
 #endif
 
--- a/src/proto.nacl0.c	Mon May 06 21:28:45 2013 +0200
+++ b/src/proto.nacl0.c	Mon May 06 22:53:20 2013 +0200
@@ -98,8 +98,9 @@
 };
 
 #ifndef COMBINED_BINARY
-int main() {
+int main(int argc, char** argv) {
 	print_header();
+	if (qtprocessargs(argc, argv) < 0) return -1;
 	return qtrun(&qtproto_nacl0);
 }
 #endif
--- a/src/proto.nacltai.c	Mon May 06 21:28:45 2013 +0200
+++ b/src/proto.nacltai.c	Mon May 06 22:53:20 2013 +0200
@@ -218,8 +218,9 @@
 };
 
 #ifndef COMBINED_BINARY
-int main() {
+int main(int argc, char** argv) {
 	print_header();
+	if (qtprocessargs(argc, argv) < 0) return -1;
 	return qtrun(&qtproto_nacltai);
 }
 #endif
--- a/src/proto.raw.c	Mon May 06 21:28:45 2013 +0200
+++ b/src/proto.raw.c	Mon May 06 22:53:20 2013 +0200
@@ -48,8 +48,9 @@
 };
 
 #ifndef COMBINED_BINARY
-int main() {
+int main(int argc, char** argv) {
 	print_header();
+	if (qtprocessargs(argc, argv) < 0) return -1;
 	return qtrun(&qtproto_raw);
 }
 #endif
--- a/src/run.combined.c	Mon May 06 21:28:45 2013 +0200
+++ b/src/run.combined.c	Mon May 06 22:53:20 2013 +0200
@@ -42,13 +42,14 @@
 }
 #endif
 
-int main() {
+int main(int argc, char** argv) {
 	print_header();
 #ifdef DEBIAN_BINARY
 	getconf = getenvdeb;
 #else
 	getconf = getenv;
 #endif
+	if (qtprocessargs(argc, argv) < 0) return -1;
 	char* envval;
 	if (envval = getconf("PROTOCOL")) {
 		if (strcmp(envval, "raw") == 0) {
@@ -58,12 +59,13 @@
 		} else if (strcmp(envval, "nacltai") == 0) {
 			return qtrun(&qtproto_nacltai);
 		} else {
-			fprintf(stderr, "Unknown protocol specified: %s\n", envval);
-			return -1;
+			return errorexit("Unknown PROTOCOL specified");
 		}
 	} else if (getconf("PRIVATE_KEY")) {
+		fprintf(stderr, "Warning: PROTOCOL not specified, using insecure nacl0 protocol\n");
 		return qtrun(&qtproto_nacl0);
 	} else {
+		fprintf(stderr, "Warning: PROTOCOL not specified, using insecure raw protocol\n");
 		return qtrun(&qtproto_raw);
 	}
 }