From c7a46d7d4f3a0aba41c7e014c50c976fe8d1439e Mon Sep 17 00:00:00 2001 From: Andreas Rottmann Date: Fri, 17 Jun 2016 20:27:02 +0200 Subject: [PATCH 1/2] Make use of a header file Extract the interface of common.c into a dedicated header, common.h, and use that instead of including the C file. --- build.sh | 10 ++--- src/common.c | 58 +--------------------------- src/common.h | 93 +++++++++++++++++++++++++++++++++++++++++++++ src/keypair.c | 2 +- src/proto.nacl0.c | 2 +- src/proto.nacltai.c | 2 +- src/proto.raw.c | 2 +- src/proto.salty.c | 2 +- src/run.combined.c | 2 +- 9 files changed, 106 insertions(+), 67 deletions(-) create mode 100644 src/common.h diff --git a/build.sh b/build.sh index 1467c9a..fe8f6ea 100755 --- a/build.sh +++ b/build.sh @@ -82,11 +82,11 @@ $cc $CFLAGS -o out/quicktun.combined obj/common.o obj/run.combined.o obj/proto.r ln out/quicktun.combined out/quicktun echo Building single protocol binaries... -$cc $CFLAGS -o out/quicktun.raw src/proto.raw.c $LDFLAGS -$cc $CFLAGS -o out/quicktun.nacl0 src/proto.nacl0.c -l$CRYPTLIB $LDFLAGS -$cc $CFLAGS -o out/quicktun.nacltai src/proto.nacltai.c -l$CRYPTLIB $LDFLAGS -$cc $CFLAGS -o out/quicktun.salty src/proto.salty.c -l$CRYPTLIB $LDFLAGS -$cc $CFLAGS -o out/quicktun.keypair src/keypair.c -l$CRYPTLIB $LDFLAGS +$cc $CFLAGS -o out/quicktun.raw src/proto.raw.c obj/common.o $LDFLAGS +$cc $CFLAGS -o out/quicktun.nacl0 src/proto.nacl0.c obj/common.o -l$CRYPTLIB $LDFLAGS +$cc $CFLAGS -o out/quicktun.nacltai src/proto.nacltai.c obj/common.o -l$CRYPTLIB $LDFLAGS +$cc $CFLAGS -o out/quicktun.salty src/proto.salty.c obj/common.o -l$CRYPTLIB $LDFLAGS +$cc $CFLAGS -o out/quicktun.keypair src/keypair.c obj/common.o -l$CRYPTLIB $LDFLAGS if [ -f /etc/network/interfaces ]; then echo Building debian binary... diff --git a/src/common.c b/src/common.c index dad5a39..fcce33f 100755 --- a/src/common.c +++ b/src/common.c @@ -23,67 +23,14 @@ authors and should not be interpreted as representing official policies, either expressed or implied, of Ivo Smits.*/ -#include -#include -#include -#include -#include +#include "common.h" + #include #include -#ifndef HAVE_NETINET_IN_H -#include -#endif #include -#include #include #include #include -#include -#include -#ifdef linux - #include - #include -#else - #define ETH_FRAME_LEN 1514 - #include - #ifdef SOLARIS - #include - #include - #endif -#endif - -#define MAX_PACKET_LEN (ETH_FRAME_LEN+4) //Some space for optional packet information - -typedef union { - struct sockaddr any; - struct sockaddr_in ip4; - struct sockaddr_in6 ip6; -} sockaddr_any; - -struct qtsession; -struct qtproto { - int encrypted; - int buffersize_raw; - int buffersize_enc; - int offset_raw; - int offset_enc; - int (*encode)(struct qtsession* sess, char* raw, char* enc, int len); - int (*decode)(struct qtsession* sess, char* enc, char* raw, int len); - int (*init)(struct qtsession* sess); - int protocol_data_size; - void (*idle)(struct qtsession* sess); -}; -struct qtsession { - struct qtproto protocol; - void* protocol_data; - int fd_socket; - int fd_dev; - int remote_float; - sockaddr_any remote_addr; - int use_pi; - int poll_timeout; - void (*sendnetworkpacket)(struct qtsession* sess, char* msg, int len); -}; #ifdef COMBINED_BINARY extern char* (*getconf)(const char*); @@ -436,4 +383,3 @@ int qtprocessargs(int argc, char** argv) { return 0; } #endif - diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..1c98714 --- /dev/null +++ b/src/common.h @@ -0,0 +1,93 @@ +/* Copyright 2010 Ivo Smits . All rights reserved. + Redistribution and use in source and binary forms, with or without modification, are + permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, this list of + conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, this list + of conditions and the following disclaimer in the documentation and/or other materials + provided with the distribution. + + THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + The views and conclusions contained in the software and documentation are those of the + authors and should not be interpreted as representing official policies, either expressed + or implied, of Ivo Smits.*/ + +#ifndef QT_COMMON_H_ +#define QT_COMMON_H_ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#ifdef linux + #include + #include +#else + #define ETH_FRAME_LEN 1514 + #include + #ifdef SOLARIS + #include + #include + #endif +#endif + +#define MAX_PACKET_LEN (ETH_FRAME_LEN+4) //Some space for optional packet information + +typedef union { + struct sockaddr any; + struct sockaddr_in ip4; + struct sockaddr_in6 ip6; +} sockaddr_any; + +struct qtsession; +struct qtproto { + int encrypted; + int buffersize_raw; + int buffersize_enc; + int offset_raw; + int offset_enc; + int (*encode)(struct qtsession* sess, char* raw, char* enc, int len); + int (*decode)(struct qtsession* sess, char* enc, char* raw, int len); + int (*init)(struct qtsession* sess); + int protocol_data_size; + void (*idle)(struct qtsession* sess); +}; +struct qtsession { + struct qtproto protocol; + void* protocol_data; + int fd_socket; + int fd_dev; + int remote_float; + sockaddr_any remote_addr; + int use_pi; + int poll_timeout; + void (*sendnetworkpacket)(struct qtsession* sess, char* msg, int len); +}; + +char* (*getconf)(const char*); +int errorexit(const char*); +int errorexitp(const char*); +void print_header(); +void hex2bin(unsigned char*, const char*, const int); +int debug; +int qtrun(struct qtproto* p); +int qtprocessargs(int argc, char** argv); + +#endif diff --git a/src/keypair.c b/src/keypair.c index 3c1854e..e05d3fb 100644 --- a/src/keypair.c +++ b/src/keypair.c @@ -23,7 +23,7 @@ authors and should not be interpreted as representing official policies, either expressed or implied, of Ivo Smits.*/ -#include "common.c" +#include "common.h" #include "crypto_box_curve25519xsalsa20poly1305.h" #include "crypto_scalarmult_curve25519.h" #include diff --git a/src/proto.nacl0.c b/src/proto.nacl0.c index 36a888d..3370a42 100644 --- a/src/proto.nacl0.c +++ b/src/proto.nacl0.c @@ -23,7 +23,7 @@ authors and should not be interpreted as representing official policies, either expressed or implied, of Ivo Smits.*/ -#include "common.c" +#include "common.h" #include "crypto_box_curve25519xsalsa20poly1305.h" struct qt_proto_data_nacl0 { diff --git a/src/proto.nacltai.c b/src/proto.nacltai.c index ceb5ff9..98e3c2f 100755 --- a/src/proto.nacltai.c +++ b/src/proto.nacltai.c @@ -23,7 +23,7 @@ authors and should not be interpreted as representing official policies, either expressed or implied, of Ivo Smits.*/ -#include "common.c" +#include "common.h" #include "crypto_box_curve25519xsalsa20poly1305.h" #include "crypto_scalarmult_curve25519.h" #include diff --git a/src/proto.raw.c b/src/proto.raw.c index e6a7a2b..359b068 100644 --- a/src/proto.raw.c +++ b/src/proto.raw.c @@ -23,7 +23,7 @@ authors and should not be interpreted as representing official policies, either expressed or implied, of Ivo Smits.*/ -#include "common.c" +#include "common.h" static int encode(struct qtsession* sess, char* raw, char* enc, int len) { memcpy(enc, raw, len); diff --git a/src/proto.salty.c b/src/proto.salty.c index 6981b33..079ad57 100755 --- a/src/proto.salty.c +++ b/src/proto.salty.c @@ -114,7 +114,7 @@ When receiving packet: Write packet to tunnel */ -#include "common.c" +#include "common.h" #include "crypto_box_curve25519xsalsa20poly1305.h" #include "crypto_scalarmult_curve25519.h" #include diff --git a/src/run.combined.c b/src/run.combined.c index 934d4bf..dd289a9 100755 --- a/src/run.combined.c +++ b/src/run.combined.c @@ -23,7 +23,7 @@ authors and should not be interpreted as representing official policies, either expressed or implied, of Ivo Smits.*/ -#include "common.c" +#include "common.h" extern struct qtproto qtproto_raw; extern struct qtproto qtproto_nacl0; From ed4e061375d0d3db513418cbcfc15b674a8f2615 Mon Sep 17 00:00:00 2001 From: Andreas Rottmann Date: Fri, 17 Jun 2016 20:27:02 +0200 Subject: [PATCH 2/2] Eliminate the per-protocol binaries Remove the distinction between "combined binary" and per-protocol binary; only a the "combined" variant is built. For backward compatibility, the quicktun binary dispatches on argv[0], and uses the part after the last dot, if present, as a protocol name. If both are present, the protocol derived from the path takes precedence over the PROTOCOL setting, and a warning is printed to stderr. The build scripts now just creates compatibility symlinks instead of actually compiling code for each protocol. --- build.sh | 30 +++++++++++++-------------- src/common.c | 12 ----------- src/{run.combined.c => main.c} | 38 +++++++++++++++++++++++++++------- src/proto.nacl0.c | 8 ------- src/proto.nacltai.c | 8 ------- src/proto.raw.c | 8 ------- src/proto.salty.c | 8 ------- 7 files changed, 45 insertions(+), 67 deletions(-) rename src/{run.combined.c => main.c} (76%) diff --git a/build.sh b/build.sh index fe8f6ea..e83a324 100755 --- a/build.sh +++ b/build.sh @@ -71,26 +71,24 @@ fi CFLAGS="$CFLAGS -DQT_VERSION=\"`cat version`\"" -echo Building combined binary... -$cc $CFLAGS -c -DCOMBINED_BINARY src/proto.raw.c -o obj/proto.raw.o -$cc $CFLAGS -c -DCOMBINED_BINARY src/proto.nacl0.c -o obj/proto.nacl0.o -$cc $CFLAGS -c -DCOMBINED_BINARY src/proto.nacltai.c -o obj/proto.nacltai.o -$cc $CFLAGS -c -DCOMBINED_BINARY src/proto.salty.c -o obj/proto.salty.o -$cc $CFLAGS -c -DCOMBINED_BINARY src/run.combined.c -o obj/run.combined.o -$cc $CFLAGS -c src/common.c -o obj/common.o -$cc $CFLAGS -o out/quicktun.combined obj/common.o obj/run.combined.o obj/proto.raw.o obj/proto.nacl0.o obj/proto.nacltai.o obj/proto.salty.o -l$CRYPTLIB $LDFLAGS -ln out/quicktun.combined out/quicktun - -echo Building single protocol binaries... -$cc $CFLAGS -o out/quicktun.raw src/proto.raw.c obj/common.o $LDFLAGS -$cc $CFLAGS -o out/quicktun.nacl0 src/proto.nacl0.c obj/common.o -l$CRYPTLIB $LDFLAGS -$cc $CFLAGS -o out/quicktun.nacltai src/proto.nacltai.c obj/common.o -l$CRYPTLIB $LDFLAGS -$cc $CFLAGS -o out/quicktun.salty src/proto.salty.c obj/common.o -l$CRYPTLIB $LDFLAGS +echo Building binaries... +$cc $CFLAGS -c src/proto.raw.c -o obj/proto.raw.o +$cc $CFLAGS -c src/proto.nacl0.c -o obj/proto.nacl0.o +$cc $CFLAGS -c src/proto.nacltai.c -o obj/proto.nacltai.o +$cc $CFLAGS -c src/proto.salty.c -o obj/proto.salty.o +$cc $CFLAGS -c src/main.c -o obj/main.o +$cc $CFLAGS -c src/common.c -o obj/common.o +$cc $CFLAGS -o out/quicktun obj/common.o obj/main.o obj/proto.raw.o obj/proto.nacl0.o obj/proto.nacltai.o obj/proto.salty.o -l$CRYPTLIB $LDFLAGS $cc $CFLAGS -o out/quicktun.keypair src/keypair.c obj/common.o -l$CRYPTLIB $LDFLAGS +echo Creating compatibility symlinks... +for proto in combined raw nacl0 nacltai salty; do + ln -s quicktun out/quicktun.$proto +done + if [ -f /etc/network/interfaces ]; then echo Building debian binary... - $cc $CFLAGS -c -DCOMBINED_BINARY -DDEBIAN_BINARY src/run.combined.c -o obj/run.debian.o + $cc $CFLAGS -c -DDEBIAN_BINARY src/main.c -o obj/run.debian.o $cc $CFLAGS -o out/quicktun.debian obj/common.o obj/run.debian.o obj/proto.raw.o obj/proto.nacl0.o obj/proto.nacltai.o obj/proto.salty.o -l$CRYPTLIB $LDFLAGS if [ -x /usr/bin/dpkg-deb -a -x /usr/bin/fakeroot ]; then echo -n Building debian package... diff --git a/src/common.c b/src/common.c index fcce33f..d0da17e 100755 --- a/src/common.c +++ b/src/common.c @@ -32,17 +32,6 @@ #include #include -#ifdef COMBINED_BINARY - extern char* (*getconf)(const char*); - extern int errorexit(const char*); - extern int errorexitp(const char*); - extern void print_header(); - extern void hex2bin(unsigned char*, const char*, const int); - extern int debug; - extern int qtrun(struct qtproto* p); - extern int qtprocessargs(int argc, char** argv); -#else - char* (*getconf)(const char*) = getenv; int debug = 0; static int gargc = 0; @@ -382,4 +371,3 @@ int qtprocessargs(int argc, char** argv) { } return 0; } -#endif diff --git a/src/run.combined.c b/src/main.c similarity index 76% rename from src/run.combined.c rename to src/main.c index dd289a9..7ab771f 100755 --- a/src/run.combined.c +++ b/src/main.c @@ -43,6 +43,24 @@ char* getenvdeb(const char* name) { } #endif +const char *execproto(const char *path) +{ + const char* last_dot; + const char* last_slash; + + if (!path) return NULL; + + last_dot = strrchr(path, '.'); + last_slash = strrchr(path, '/'); + if (last_dot && (!last_slash || last_slash < last_dot)) { + if (strcmp(last_dot + 1, "combined") == 0) { + return NULL; + } + return last_dot + 1; + } + return NULL; +} + int main(int argc, char** argv) { print_header(); #ifdef DEBIAN_BINARY @@ -51,15 +69,22 @@ int main(int argc, char** argv) { getconf = getenv; #endif if (qtprocessargs(argc, argv) < 0) return -1; - char* envval; - if ((envval = getconf("PROTOCOL"))) { - if (strcmp(envval, "raw") == 0) { + const char* proto = execproto(argv[0]); + const char* envval = getconf("PROTOCOL"); + if (proto && envval) { + fprintf(stderr, "Warning: ignoring PROTOCOL setting '%s' in favor single-protocol compatibility '%s'\n", + envval, proto); + } else if (!proto) { + proto = envval; + } + if (proto) { + if (strcmp(proto, "raw") == 0) { return qtrun(&qtproto_raw); - } else if (strcmp(envval, "nacl0") == 0) { + } else if (strcmp(proto, "nacl0") == 0) { return qtrun(&qtproto_nacl0); - } else if (strcmp(envval, "nacltai") == 0) { + } else if (strcmp(proto, "nacltai") == 0) { return qtrun(&qtproto_nacltai); - } else if (strcmp(envval, "salty") == 0) { + } else if (strcmp(proto, "salty") == 0) { return qtrun(&qtproto_salty); } else { return errorexit("Unknown PROTOCOL specified"); @@ -72,4 +97,3 @@ int main(int argc, char** argv) { return qtrun(&qtproto_raw); } } - diff --git a/src/proto.nacl0.c b/src/proto.nacl0.c index 3370a42..9676319 100644 --- a/src/proto.nacl0.c +++ b/src/proto.nacl0.c @@ -95,11 +95,3 @@ struct qtproto qtproto_nacl0 = { init, sizeof(struct qt_proto_data_nacl0), }; - -#ifndef COMBINED_BINARY -int main(int argc, char** argv) { - print_header(); - if (qtprocessargs(argc, argv) < 0) return -1; - return qtrun(&qtproto_nacl0); -} -#endif diff --git a/src/proto.nacltai.c b/src/proto.nacltai.c index 98e3c2f..60c22e1 100755 --- a/src/proto.nacltai.c +++ b/src/proto.nacltai.c @@ -172,11 +172,3 @@ struct qtproto qtproto_nacltai = { init, sizeof(struct qt_proto_data_nacltai), }; - -#ifndef COMBINED_BINARY -int main(int argc, char** argv) { - print_header(); - if (qtprocessargs(argc, argv) < 0) return -1; - return qtrun(&qtproto_nacltai); -} -#endif diff --git a/src/proto.raw.c b/src/proto.raw.c index 359b068..babab1c 100644 --- a/src/proto.raw.c +++ b/src/proto.raw.c @@ -46,11 +46,3 @@ struct qtproto qtproto_raw = { NULL, 0, }; - -#ifndef COMBINED_BINARY -int main(int argc, char** argv) { - print_header(); - if (qtprocessargs(argc, argv) < 0) return -1; - return qtrun(&qtproto_raw); -} -#endif diff --git a/src/proto.salty.c b/src/proto.salty.c index 079ad57..d608d36 100755 --- a/src/proto.salty.c +++ b/src/proto.salty.c @@ -460,11 +460,3 @@ struct qtproto qtproto_salty = { sizeof(struct qt_proto_data_salty), idle, }; - -#ifndef COMBINED_BINARY -int main(int argc, char** argv) { - print_header(); - if (qtprocessargs(argc, argv) < 0) return -1; - return qtrun(&qtproto_salty); -} -#endif