From 80b33cd1a950022c898e1a164288bb70850b6d54 Mon Sep 17 00:00:00 2001 From: Hamish Coleman Date: Sat, 23 Oct 2021 20:36:18 +0100 Subject: [PATCH] Basic C Code lint checker and shell checker (#859) * Factor build packages out into a more maintainable list * Create a location for scripts to live * Provide a make target to return the source dir as close as reasonable to the original distributed state * Add a code lint step, checking the coding style * Change test harness as recommended by shellcheck * Ensure we actually have the linter tool installed * Use the correct directory for cmake to run the tests * Adjust for the older uncrustify in the current github ubuntu-latest * Make one file pass the linter * Integrate the lint with the existing test workflow * Add files with minimal changes needed to the linter * Add more files with minimal changes needed to the linter * Dont build binaries if we fail the lint test * Update the phony targets with the lint steps * Ensure the flake8 package is installed in the new lint workflow job * Use the makefile to drive the packages needed to install for linting * No need to add dependancies on lint, just rely on the workflow status to show failure * Update the scripts dir README to reflect current assumptions * Rename and briefly document the indent.sh script * Fix the ignore to ignore the right Makefile * Rename the test_harness script to make it clear it is a shell script * Provide a master lint make target and add a shell script lint tool * Elminate stray tabs * Drop include/auth.h from linter - there are inconsistant results with function definitions when using the current uncrustify rules --- .github/workflows/tests.yml | 39 +++++++++--- .gitignore | 7 ++- CMakeLists.txt | 2 +- Makefile.in | 59 ++++++++++++++++-- doc/Scripts.md | 8 ++- include/auth.h | 2 +- include/edge_utils_win32.h | 2 +- include/n2n_define.h | 6 +- include/pearson.h | 2 +- include/speck.h | 10 ++-- scripts/README.md | 9 +++ scripts/indent.sh | 60 +++++++++++++++++++ tools/test_harness => scripts/test_harness.sh | 8 +-- src/edge_utils_win32.c | 19 +++--- src/example_edge_embed_quick_edge_init.c | 4 +- src/header_encryption.c | 4 +- src/sn_selection.c | 14 ++--- src/transform_cc20.c | 2 +- src/tuntap_linux.c | 2 +- src/wire.c | 8 +-- tools/tests-compress.c | 35 +++++------ tools/tests-elliptic.c | 6 +- tools/tests-hashing.c | 11 ++-- tools/tests-transform.c | 27 +++++---- tools/tests-wire.c | 50 ++++++++-------- uncrustify.cfg | 34 +++++++++++ 26 files changed, 308 insertions(+), 122 deletions(-) create mode 100644 scripts/README.md create mode 100755 scripts/indent.sh rename tools/test_harness => scripts/test_harness.sh (78%) create mode 100644 uncrustify.cfg diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index dfac3c6..7ff23d4 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -12,15 +12,31 @@ jobs: steps: - uses: actions/checkout@v2 - - name: Install essential - run: | - sudo apt-get install flake8 - name: Run minimal test set run: | ./autogen.sh ./configure make test - make lint.python + + lint: + name: Code syntax + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Make the makefiles + run: | + ./autogen.sh + ./configure + + - name: Install essential + run: | + make build-dep + + - name: Run the lint tools + run: | + make lint test_linux: needs: smoketest @@ -216,8 +232,9 @@ jobs: uses: codecov/codecov-action@v2 package_dpkg: - needs: test_linux name: Package for Debian/Ubuntu + needs: + - test_linux runs-on: ubuntu-latest strategy: fail-fast: true @@ -260,8 +277,9 @@ jobs: path: packages/debian/*.deb package_rpm: - needs: test_linux name: Package for Redhat/RPM + needs: + - test_linux runs-on: ubuntu-latest steps: @@ -296,8 +314,9 @@ jobs: path: rpmbuild/RPMS/x86_64/*.rpm binaries_windows: - needs: test_windows name: Binaries for Windows (x86_64-pc-mingw64) + needs: + - test_windows runs-on: windows-latest steps: @@ -321,8 +340,9 @@ jobs: path: binaries binaries_macos: - needs: test_macos name: Binaries for MacOS (x86_64-apple-darwin) + needs: + - test_macos runs-on: macos-latest steps: @@ -351,8 +371,9 @@ jobs: path: binaries binaries_linux_crosscompile: - needs: test_linux name: Binaries for linux + needs: + - test_linux runs-on: ubuntu-latest strategy: fail-fast: true diff --git a/.gitignore b/.gitignore index 985d52d..ae2b142 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,7 @@ configure configure.ac config.* -./Makefile +/Makefile tools/Makefile autom4te.cache edge @@ -45,3 +45,8 @@ tests/*.out *.gcda *.gcov coverage/ + +# Files generated while running linting +*.indent +*.unc-backup.md5~ +*.unc-backup~ diff --git a/CMakeLists.txt b/CMakeLists.txt index 65b8244..2175d52 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -271,6 +271,6 @@ install(FILES ${PROJECT_BINARY_DIR}/doc/n2n.7.gz # TODO: # - Add the right dependancy so that the tests binaries get built first enable_testing() -add_test(tests ${PROJECT_SOURCE_DIR}/tools/test_harness ${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR}/tests) +add_test(tests ${PROJECT_SOURCE_DIR}/scripts/test_harness.sh ${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR}/tests) endif(DEFINED UNIX) diff --git a/Makefile.in b/Makefile.in index d5fb4f5..5db00f4 100644 --- a/Makefile.in +++ b/Makefile.in @@ -83,7 +83,28 @@ N2N_LIB=libn2n.a N2N_OBJS=$(patsubst src/%.c, src/%.o, $(wildcard src/*.c)) N2N_DEPS=$(wildcard include/*.h) $(wildcard src/*.c) Makefile +# As source files pass the linter, they can be added here (If all the source +# is passing the linter tests, this can be refactored) +LINT_CCODE=\ + include/edge_utils_win32.h \ + include/n2n_define.h \ + include/pearson.h \ + include/speck.h \ + src/edge_utils_win32.c \ + src/example_edge_embed_quick_edge_init.c \ + src/header_encryption.c \ + src/sn_selection.c \ + src/transform_cc20.c \ + src/tuntap_linux.c \ + src/wire.c \ + tools/tests-compress.c \ + tools/tests-elliptic.c \ + tools/tests-hashing.c \ + tools/tests-transform.c \ + tools/tests-wire.c \ + export LDLIBS + LDLIBS+=-ln2n LDLIBS+=@N2N_LIBS@ @@ -108,12 +129,26 @@ APPS+=example_sn_embed DOCS=edge.8.gz supernode.1.gz n2n.7.gz +# This is the superset of all packages that might be needed during the build. +# Mostly of use in automated build systems. +BUILD_DEP:=\ + autoconf \ + build-essential \ + flake8 \ + gcovr \ + libcap-dev \ + libzstd-dev \ + shellcheck \ + uncrustify \ + SUBDIRS+=tools COVERAGEDIR?=coverage .PHONY: $(SUBDIRS) -.PHONY: steps build push all clean install test cover gcov build-dep +.PHONY: steps build push all clean distclean install test cover gcov build-dep +.PHONY: lint lint.python lint.ccode lint.shell + all: $(APPS) $(DOCS) $(SUBDIRS) tools: $(N2N_LIB) @@ -147,11 +182,19 @@ $(N2N_LIB): $(N2N_OBJS) win32/n2n_win32.a: win32 test: tools - tools/test_harness + scripts/test_harness.sh + +lint: lint.python lint.ccode lint.shell lint.python: flake8 scripts/n2nctl scripts/n2nhttpd +lint.ccode: + scripts/indent.sh $(LINT_CCODE) + +lint.shell: + shellcheck scripts/*.sh + # To generate coverage information, run configure with # CFLAGS="-fprofile-arcs -ftest-coverage" LDFLAGS="--coverage" # and run the desired tests. Ensure that package gcovr is installed @@ -168,11 +211,10 @@ gcov: gcov $(N2N_OBJS) $(MAKE) -C tools gcov -# This is the superset of all packages that might be needed. -# It is a convinent target to use during development or from a CI/CD system +# This is a convinent target to use during development or from a CI/CD system build-dep: ifeq ($(CONFIG_TARGET),generic) - sudo apt install build-essential autoconf libcap-dev libzstd-dev gcovr flake8 + sudo apt install $(BUILD_DEP) else ifeq ($(CONFIG_TARGET),darwin) brew install automake gcovr else @@ -184,6 +226,13 @@ clean: rm -f tests/*.out src/*.gcno src/*.gcda for dir in $(SUBDIRS); do $(MAKE) -C $$dir clean; done +distclean: + rm -f tests/*.out src/*.gcno src/*.gcda src/*.indent src/*.unc-backup* + rm -rf autom4te.cache/ + rm -f config.log config.status configure configure.ac Makefile tools/Makefile include/config.h include/config.h.in + rm -f doc/edge.8.gz doc/n2n.7.gz doc/supernode.1.gz + rm -f $(addprefix src/,$(APPS)) + install: edge supernode edge.8.gz supernode.1.gz n2n.7.gz echo "MANDIR=$(MANDIR)" $(MKDIR) $(SBINDIR) $(MAN1DIR) $(MAN7DIR) $(MAN8DIR) diff --git a/doc/Scripts.md b/doc/Scripts.md index b6c02ae..f108b68 100644 --- a/doc/Scripts.md +++ b/doc/Scripts.md @@ -13,7 +13,13 @@ This shell script is used during development to help build on Windows systems. An example of how to use it is shown in the [Building document](Building.md) -## `tools/test_harness` +## `scripts/indent.sh` + +This shell script is a wrapper for the `uncrustify` C code style checker +which checks or applies a set of rules to the code. It is used during +the automated lint checks. + +## `scripts/test_harness.sh` This shell script is used to run automated tests during development. diff --git a/include/auth.h b/include/auth.h index 21a80b3..43bc588 100644 --- a/include/auth.h +++ b/include/auth.h @@ -28,7 +28,7 @@ int bin_to_ascii (char *out, uint8_t *in, size_t in_len); int ascii_to_bin (uint8_t *out, char *in); -int generate_private_key(n2n_private_public_key_t key, char *in); +int generate_private_key (n2n_private_public_key_t key, char *in); int generate_public_key (n2n_private_public_key_t pub, n2n_private_public_key_t prv); diff --git a/include/edge_utils_win32.h b/include/edge_utils_win32.h index 5a0508c..52720f7 100644 --- a/include/edge_utils_win32.h +++ b/include/edge_utils_win32.h @@ -43,7 +43,7 @@ struct tunread_arg { }; extern HANDLE startTunReadThread (struct tunread_arg *arg); -int get_best_interface_ip(n2n_edge_t * eee, dec_ip_str_t ip_addr); +int get_best_interface_ip (n2n_edge_t * eee, dec_ip_str_t ip_addr); #endif /* WIN32 */ diff --git a/include/n2n_define.h b/include/n2n_define.h index 22443e9..a9bd19d 100644 --- a/include/n2n_define.h +++ b/include/n2n_define.h @@ -92,14 +92,14 @@ /* Federation name and indicators */ #define FEDERATION_NAME "*Federation" -enum federation{IS_NO_FEDERATION = 0,IS_FEDERATION = 1}; +enum federation {IS_NO_FEDERATION = 0,IS_FEDERATION = 1}; /* (un)purgeable community indicator (supernode) */ #define COMMUNITY_UNPURGEABLE 0 #define COMMUNITY_PURGEABLE 1 /* (un)purgeable supernode indicator */ -enum sn_purge{SN_PURGEABLE = 0, SN_UNPURGEABLE = 1}; +enum sn_purge {SN_PURGEABLE = 0, SN_UNPURGEABLE = 1}; /* Header encryption indicators */ #define HEADER_ENCRYPTION_UNKNOWN 0 @@ -133,7 +133,7 @@ enum n2n_mgmt_type { /* which the socket explicitly is closed before reopening */ /* flag used in add_sn_to_list_by_mac_or_sock */ -enum skip_add{SN_ADD = 0, SN_ADD_SKIP = 1, SN_ADD_ADDED = 2}; +enum skip_add {SN_ADD = 0, SN_ADD_SKIP = 1, SN_ADD_ADDED = 2}; #define N2N_NETMASK_STR_SIZE 16 /* dotted decimal 12 numbers + 3 dots */ #define N2N_MACNAMSIZ 18 /* AA:BB:CC:DD:EE:FF + NULL*/ diff --git a/include/pearson.h b/include/pearson.h index 6a7f4de..97d2500 100644 --- a/include/pearson.h +++ b/include/pearson.h @@ -33,4 +33,4 @@ uint32_t pearson_hash_32 (const uint8_t *in, size_t len); uint16_t pearson_hash_16 (const uint8_t *in, size_t len); -void pearson_hash_init(); +void pearson_hash_init (); diff --git a/include/speck.h b/include/speck.h index 86cf66f..bf448fc 100644 --- a/include/speck.h +++ b/include/speck.h @@ -47,7 +47,7 @@ #define u512 __m512i -#define SPECK_ALIGNED_CTX 64 +#define SPECK_ALIGNED_CTX 64 typedef struct { u512 rk[34]; @@ -63,7 +63,7 @@ typedef struct { #define u256 __m256i -#define SPECK_ALIGNED_CTX 32 +#define SPECK_ALIGNED_CTX 32 typedef struct { u256 rk[34]; @@ -79,8 +79,8 @@ typedef struct { #define u128 __m128i -#define SPECK_ALIGNED_CTX 16 -#define SPECK_CTX_BYVAL 1 +#define SPECK_ALIGNED_CTX 16 +#define SPECK_CTX_BYVAL 1 typedef struct { u128 rk[34]; @@ -117,7 +117,7 @@ typedef struct { int speck_ctr (unsigned char *out, const unsigned char *in, unsigned long long inlen, const unsigned char *n, - speck_context_t *ctx); + speck_context_t *ctx); int speck_init (speck_context_t **ctx, const unsigned char *k, int keysize); diff --git a/scripts/README.md b/scripts/README.md new file mode 100644 index 0000000..380fa3c --- /dev/null +++ b/scripts/README.md @@ -0,0 +1,9 @@ +This directory contains executables that are not compiled. Some of these may +end up installed for use by end users, but many of them are for use during +development, builds and tests. + +Nothing in this directory should need compiling to use and they should be +written such that they do not need configuring (e.g: they might probe several +directories for their requirements) + +See the [Scripts Documentation](../docs/Scripts.md) for further details diff --git a/scripts/indent.sh b/scripts/indent.sh new file mode 100755 index 0000000..50d38ba --- /dev/null +++ b/scripts/indent.sh @@ -0,0 +1,60 @@ +#!/bin/sh +# +# Given one or more input source files, run a re-indenter on them. + +help() { + echo "Usage: scripts/indent [-i] [file...]" + echo " -i modify file in place with reindent results" + echo "" + echo "By default, will output a diff and exitcode if changed are needed" + echo "If modifying files, no exit code or diff is output" + exit 1 +} + +[ -z "$1" ] && help +[ "$1" = "-h" ] && help + +INPLACE=0 +if [ "$1" = "-i" ]; then + shift + INPLACE=1 +fi + +## indentOneClang() { +## rm -f "$1.indent" +## clang-format "$1" >"$1.indent" +## if [ $? -ne 0 ]; then +## echo "Error while formatting \"$1\"" +## RESULT=1 +## return +## fi +## diff -u "$1" "$1.indent" +## if [ $? -ne 0 ]; then +## RESULT=1 +## fi +## } + +indentOne() { + IFILE="$1" + if [ "$INPLACE" -eq 0 ]; then + OFILE="$1.indent" + rm -f "$OFILE" + else + OFILE="$1" + fi + if ! uncrustify -c uncrustify.cfg -f "$IFILE" -o "$OFILE"; then + echo "Error while formatting \"$1\"" + RESULT=1 + return + fi + if ! diff -u "$IFILE" "$OFILE"; then + RESULT=1 + fi +} + +RESULT=0 +while [ -n "$1" ]; do + indentOne "$1" + shift +done +exit $RESULT diff --git a/tools/test_harness b/scripts/test_harness.sh similarity index 78% rename from tools/test_harness rename to scripts/test_harness.sh index dcacef1..5f52075 100755 --- a/tools/test_harness +++ b/scripts/test_harness.sh @@ -20,11 +20,11 @@ TESTDATA=tests # Confirm we have all the tools and data for i in $TESTS; do - if [ ! -e $TOOLSDIR/$i ]; then + if [ ! -e "$TOOLSDIR/$i" ]; then echo "Could not find test $TOOLSDIR/$i" exit 1 fi - if [ ! -e $TESTDATA/$i.expected ]; then + if [ ! -e "$TESTDATA/$i.expected" ]; then echo "Could not find testdata $TESTDATA/$i.expected" exit 1 fi @@ -34,6 +34,6 @@ done set -e for i in $TESTS; do echo "$TOOLSDIR/$i >$TESTDATA/$i.out" - $TOOLSDIR/$i >$TESTDATA/$i.out - cmp $TESTDATA/$i.expected $TESTDATA/$i.out + "$TOOLSDIR/$i" >"$TESTDATA/$i.out" + cmp "$TESTDATA/$i.expected" "$TESTDATA/$i.out" done diff --git a/src/edge_utils_win32.c b/src/edge_utils_win32.c index 452a897..73ccb0b 100644 --- a/src/edge_utils_win32.c +++ b/src/edge_utils_win32.c @@ -51,7 +51,7 @@ HANDLE startTunReadThread (struct tunread_arg *arg) { -int get_best_interface_ip(n2n_edge_t * eee, dec_ip_str_t ip_addr){ +int get_best_interface_ip (n2n_edge_t * eee, dec_ip_str_t ip_addr){ DWORD interface_index = -1; DWORD dwRetVal = 0; PIP_ADAPTER_INFO pAdapterInfo = NULL, pAdapter = NULL; @@ -62,15 +62,15 @@ int get_best_interface_ip(n2n_edge_t * eee, dec_ip_str_t ip_addr){ if(dwRetVal != NO_ERROR) return -1; pAdapterInfo = (PIP_ADAPTER_INFO)malloc(ulOutBufLen); - if (pAdapterInfo == NULL) { + if(pAdapterInfo == NULL) { traceEvent(TRACE_INFO, "Error allocating memory needed to call GetAdaptersInfo\n"); return -1; } dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen); - if(dwRetVal == ERROR_BUFFER_OVERFLOW){ + if(dwRetVal == ERROR_BUFFER_OVERFLOW) { pAdapterInfo = (PIP_ADAPTER_INFO)realloc(pAdapterInfo, ulOutBufLen); - if (pAdapterInfo == NULL) { + if(pAdapterInfo == NULL) { traceEvent(TRACE_INFO, "Error allocating memory needed to call GetAdaptersInfo\n"); return -1; } @@ -78,9 +78,9 @@ int get_best_interface_ip(n2n_edge_t * eee, dec_ip_str_t ip_addr){ dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen); // hexdump((uint8_t*)pAdapterInfo, ulOutBufLen); - if (dwRetVal == NO_ERROR) { - for(pAdapter = pAdapterInfo; pAdapter != NULL; pAdapter = pAdapter->Next){ - if (pAdapter->Index != interface_index) continue; + if(dwRetVal == NO_ERROR) { + for(pAdapter = pAdapterInfo; pAdapter != NULL; pAdapter = pAdapter->Next) { + if(pAdapter->Index != interface_index) continue; traceEvent(TRACE_DEBUG, "Adapter Index: %ld\n", pAdapter->Index); traceEvent(TRACE_DEBUG, "Combo Index: %ld\n", pAdapter->ComboIndex); @@ -96,11 +96,10 @@ int get_best_interface_ip(n2n_edge_t * eee, dec_ip_str_t ip_addr){ traceEvent(TRACE_DEBUG, "Gateway: %s\n", pAdapter->GatewayList.IpAddress.String); strncpy(ip_addr, pAdapter->IpAddressList.IpAddress.String, sizeof(dec_ip_str_t)-1); } - } - else { + } else { traceEvent(TRACE_WARNING, "GetAdaptersInfo failed with error: %d\n", dwRetVal); } - if (pAdapterInfo != NULL){ + if(pAdapterInfo != NULL) { free(pAdapterInfo); pAdapterInfo = NULL; } diff --git a/src/example_edge_embed_quick_edge_init.c b/src/example_edge_embed_quick_edge_init.c index 729c964..148655a 100644 --- a/src/example_edge_embed_quick_edge_init.c +++ b/src/example_edge_embed_quick_edge_init.c @@ -31,13 +31,13 @@ int main (int argc, char* argv[]) { char *my_mac_address = (char*)"DE:AD:BE:EF:01:10"; char *my_ipv4_addr = (char*)"1.2.3.4"; char *supernode = (char*)"7.8.9.10:1234"; - int keep_on_running = 1; + int keep_on_running = 1; /* Increase tracelevel to see what's happening */ setTraceLevel(10); /* Random seed */ - n2n_srand (n2n_seed()); + n2n_srand(n2n_seed()); /* NOTE diff --git a/src/header_encryption.c b/src/header_encryption.c index 0018382..462982a 100644 --- a/src/header_encryption.c +++ b/src/header_encryption.c @@ -41,7 +41,7 @@ int packet_header_decrypt (uint8_t packet[], uint16_t packet_len, //extract header length (lower 2 bytes) uint32_t header_len = test_magic - magic; - if (header_len <= packet_len) { + if(header_len <= packet_len) { // decrypt the complete header speck_ctr(&packet[16], &packet[16], header_len - 16, packet, (speck_context_t*)ctx); @@ -130,7 +130,7 @@ void packet_header_setup_key (const char *community_name, // for all other packets, same as static by default (changed by user/pw auth scheme // calling packet_header_change_dynamic_key later) - pearson_hash_128(key, (uint8_t*)community_name, N2N_COMMUNITY_SIZE); + pearson_hash_128(key, (uint8_t*)community_name, N2N_COMMUNITY_SIZE); if(!*ctx_static) *ctx_static = (he_context_t*)calloc(1, sizeof(speck_context_t)); diff --git a/src/sn_selection.c b/src/sn_selection.c index 191c446..f011e89 100644 --- a/src/sn_selection.c +++ b/src/sn_selection.c @@ -159,7 +159,7 @@ SN_SELECTION_CRITERION_DATA_TYPE sn_selection_criterion_gather_data (n2n_sn_t *s extern char * sn_selection_criterion_str (selection_criterion_str_t out, peer_info_t *peer) { if(NULL == out) { - return NULL; + return NULL; } memset(out, 0, SN_SELECTION_CRITERION_BUF_SIZE); @@ -169,15 +169,15 @@ extern char * sn_selection_criterion_str (selection_criterion_str_t out, peer_in if(peer->selection_criterion < (UINT32_MAX >> 2)) { #ifndef SN_SELECTION_RTT - int chars = snprintf(out, SN_SELECTION_CRITERION_BUF_SIZE, "load = %8d", peer->selection_criterion); + int chars = snprintf(out, SN_SELECTION_CRITERION_BUF_SIZE, "load = %8d", peer->selection_criterion); #else - int chars = snprintf(out, SN_SELECTION_CRITERION_BUF_SIZE, "rtt = %6d ms", peer->selection_criterion); + int chars = snprintf(out, SN_SELECTION_CRITERION_BUF_SIZE, "rtt = %6d ms", peer->selection_criterion); #endif - /* this test is to make "-Wformat-truncation" less sad */ - if (chars > SN_SELECTION_CRITERION_BUF_SIZE) { - traceEvent(TRACE_INFO, "selection_criterion buffer overflow"); - } + /* this test is to make "-Wformat-truncation" less sad */ + if(chars > SN_SELECTION_CRITERION_BUF_SIZE) { + traceEvent(TRACE_INFO, "selection_criterion buffer overflow"); + } } return out; diff --git a/src/transform_cc20.c b/src/transform_cc20.c index ee7b817..fd8d372 100644 --- a/src/transform_cc20.c +++ b/src/transform_cc20.c @@ -101,7 +101,7 @@ static int transop_decode_cc20 (n2n_trans_op_t *arg, transop_cc20_t *priv = (transop_cc20_t *)arg->priv; if(((in_len - CC20_PREAMBLE_SIZE) <= N2N_PKT_BUF_SIZE) /* cipher text fits in assembly */ - && (in_len >= CC20_PREAMBLE_SIZE)) { /* has at least iv */ + && (in_len >= CC20_PREAMBLE_SIZE)) { /* has at least iv */ traceEvent(TRACE_DEBUG, "decode_cc20 %lu bytes", in_len); diff --git a/src/tuntap_linux.c b/src/tuntap_linux.c index d8f13e7..cc23fac 100644 --- a/src/tuntap_linux.c +++ b/src/tuntap_linux.c @@ -48,7 +48,7 @@ static int setup_ifname (int fd, const char *ifname, const char *ipaddr, if(ioctl(fd, SIOCSIFADDR, &ifr) == -1) { traceEvent(TRACE_ERROR, "ioctl(SIOCSIFADDR) failed [%d]: %s", errno, strerror(errno)); return -2; - } + } // netmask if(netmask && (((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr.s_addr != 0)) { diff --git a/src/wire.c b/src/wire.c index bac76e3..7265de8 100644 --- a/src/wire.c +++ b/src/wire.c @@ -71,7 +71,7 @@ int decode_uint16 (uint16_t * out, size_t * rem, size_t * idx) { - if (*rem < 2) { + if(*rem < 2) { return 0; } @@ -101,7 +101,7 @@ int decode_uint32 (uint32_t * out, size_t * rem, size_t * idx) { - if (*rem < 4) { + if(*rem < 4) { return 0; } @@ -130,7 +130,7 @@ int decode_uint64 (uint64_t * out, size_t * rem, size_t * idx) { - if (*rem < 8) { + if(*rem < 8) { return 0; } @@ -159,7 +159,7 @@ int decode_buf (uint8_t * out, size_t * rem, size_t * idx) { - if (*rem < bufsize) { + if(*rem < bufsize) { return 0; } diff --git a/tools/tests-compress.c b/tools/tests-compress.c index 2758440..f68d510 100644 --- a/tools/tests-compress.c +++ b/tools/tests-compress.c @@ -44,9 +44,10 @@ uint8_t PKT_CONTENT[]={ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, - 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }; + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 +}; -static void init_compression_for_benchmark(void) { +static void init_compression_for_benchmark (void) { if(lzo_init() != LZO_E_OK) { traceEvent(TRACE_ERROR, "LZO compression init error"); @@ -59,7 +60,7 @@ static void init_compression_for_benchmark(void) { } -static void deinit_compression_for_benchmark(void) { +static void deinit_compression_for_benchmark (void) { // lzo1x does not require de-initialization. if it were required, this would be a good place @@ -68,27 +69,27 @@ static void deinit_compression_for_benchmark(void) { #endif } -void test_lzo1x() { +void test_lzo1x () { char *test_name = "lzo1x"; uint8_t compression_buffer[N2N_PKT_BUF_SIZE]; // size allows enough of a reserve required for compression lzo_uint compression_len = sizeof(compression_buffer); - if (lzo1x_1_compress(PKT_CONTENT, sizeof(PKT_CONTENT), compression_buffer, &compression_len, wrkmem) != LZO_E_OK) { + if(lzo1x_1_compress(PKT_CONTENT, sizeof(PKT_CONTENT), compression_buffer, &compression_len, wrkmem) != LZO_E_OK) { fprintf(stderr, "%s: compression error\n", test_name); exit(1); } assert(compression_len == 47); - printf("%s: output size = 0x%"PRIx64"\n", test_name, compression_len); + printf("%s: output size = 0x%" PRIx64 "\n", test_name, compression_len); fhexdump(0, compression_buffer, compression_len, stdout); uint8_t deflation_buffer[N2N_PKT_BUF_SIZE]; lzo_uint deflated_len; - lzo1x_decompress (compression_buffer, compression_len, deflation_buffer, &deflated_len, NULL); + lzo1x_decompress(compression_buffer, compression_len, deflation_buffer, &deflated_len, NULL); assert(deflated_len == sizeof(PKT_CONTENT)); - if (memcmp(PKT_CONTENT, deflation_buffer, deflated_len)!=0) { + if(memcmp(PKT_CONTENT, deflation_buffer, deflated_len)!=0) { fprintf(stderr, "%s: round-trip buffer mismatch\n", test_name); exit(1); } @@ -97,7 +98,7 @@ void test_lzo1x() { printf("\n"); } -void test_zstd() { +void test_zstd () { char *test_name = "zstd"; #ifdef N2N_HAVE_ZSTD @@ -105,28 +106,28 @@ void test_zstd() { lzo_uint compression_len = sizeof(compression_buffer); compression_len = N2N_PKT_BUF_SIZE; - compression_len = ZSTD_compress(compression_buffer, compression_len, PKT_CONTENT, sizeof(PKT_CONTENT), ZSTD_COMPRESSION_LEVEL) ; + compression_len = ZSTD_compress(compression_buffer, compression_len, PKT_CONTENT, sizeof(PKT_CONTENT), ZSTD_COMPRESSION_LEVEL); if(ZSTD_isError(compression_len)) { fprintf(stderr, "%s: compression error\n", test_name); exit(1); } assert(compression_len == 33); - - printf("%s: output size = 0x%"PRIx64"\n", test_name, compression_len); + + printf("%s: output size = 0x%" PRIx64 "\n", test_name, compression_len); fhexdump(0, compression_buffer, compression_len, stdout); uint8_t deflation_buffer[N2N_PKT_BUF_SIZE]; int64_t deflated_len = sizeof(deflation_buffer); - deflated_len = (int32_t)ZSTD_decompress (deflation_buffer, deflated_len, compression_buffer, compression_len); + deflated_len = (int32_t)ZSTD_decompress(deflation_buffer, deflated_len, compression_buffer, compression_len); if(ZSTD_isError(deflated_len)) { fprintf(stderr, "%s: decompression error '%s'\n", - test_name, ZSTD_getErrorName(deflated_len)); + test_name, ZSTD_getErrorName(deflated_len)); exit(1); } assert(deflated_len == sizeof(PKT_CONTENT)); - if (memcmp(PKT_CONTENT, deflation_buffer, deflated_len)!=0) { + if(memcmp(PKT_CONTENT, deflation_buffer, deflated_len)!=0) { fprintf(stderr, "%s: round-trip buffer mismatch\n", test_name); exit(1); } @@ -145,12 +146,12 @@ void test_zstd() { } -int main(int argc, char * argv[]) { +int main (int argc, char * argv[]) { /* Also for compression (init moved here for ciphers get run before in case of lzo init error) */ init_compression_for_benchmark(); - printf("%s: input size = 0x%"PRIx64"\n", "original", sizeof(PKT_CONTENT)); + printf("%s: input size = 0x%" PRIx64 "\n", "original", sizeof(PKT_CONTENT)); fhexdump(0, PKT_CONTENT, sizeof(PKT_CONTENT), stdout); printf("\n"); diff --git a/tools/tests-elliptic.c b/tools/tests-elliptic.c index cf65efd..3a119bc 100644 --- a/tools/tests-elliptic.c +++ b/tools/tests-elliptic.c @@ -19,7 +19,7 @@ #include "n2n.h" #include "hexdump.h" -void test_curve25519(unsigned char *pkt_input, unsigned char *key) { +void test_curve25519 (unsigned char *pkt_input, unsigned char *key) { char *test_name = "curve25519"; unsigned char pkt_output[32]; @@ -32,7 +32,7 @@ void test_curve25519(unsigned char *pkt_input, unsigned char *key) { printf("\n"); } -int main(int argc, char * argv[]) { +int main (int argc, char * argv[]) { char *test_name = "environment"; unsigned char key[32]; @@ -40,7 +40,7 @@ int main(int argc, char * argv[]) { memset(pkt_input, 0, 31); pkt_input[31] = 9; - + memset(key, 0x55, 32); printf("%s: input\n", test_name); diff --git a/tools/tests-hashing.c b/tools/tests-hashing.c index 44e909a..51efbeb 100644 --- a/tools/tests-hashing.c +++ b/tools/tests-hashing.c @@ -38,24 +38,25 @@ uint8_t PKT_CONTENT[]={ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, - 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }; + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 +}; -void test_pearson(void *buf, unsigned int bufsize) { +void test_pearson (void *buf, unsigned int bufsize) { char *test_name = "pearson"; uint64_t hash = pearson_hash_64(buf, bufsize); - printf("%s: output = 0x%"PRIx64"\n", test_name, hash); + printf("%s: output = 0x%" PRIx64 "\n", test_name, hash); fprintf(stderr, "%s: tested\n", test_name); printf("\n"); } -int main(int argc, char * argv[]) { +int main (int argc, char * argv[]) { pearson_hash_init(); char *test_name = "environment"; - printf("%s: input size = 0x%"PRIx64"\n", test_name, sizeof(PKT_CONTENT)); + printf("%s: input size = 0x%" PRIx64 "\n", test_name, sizeof(PKT_CONTENT)); fhexdump(0, PKT_CONTENT, sizeof(PKT_CONTENT), stdout); printf("\n"); diff --git a/tools/tests-transform.c b/tools/tests-transform.c index d23280b..1ef42b5 100644 --- a/tools/tests-transform.c +++ b/tools/tests-transform.c @@ -41,14 +41,15 @@ uint8_t PKT_CONTENT[]={ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, - 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }; + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 +}; /* Prototypes */ -static ssize_t do_encode_packet( uint8_t * pktbuf, size_t bufsize, const n2n_community_t c ); -static void run_transop_benchmark(const char *op_name, n2n_trans_op_t *op_fn, n2n_edge_conf_t *conf, uint8_t *pktbuf); +static ssize_t do_encode_packet ( uint8_t * pktbuf, size_t bufsize, const n2n_community_t c ); +static void run_transop_benchmark (const char *op_name, n2n_trans_op_t *op_fn, n2n_edge_conf_t *conf, uint8_t *pktbuf); -int main(int argc, char * argv[]) { +int main (int argc, char * argv[]) { uint8_t pktbuf[N2N_PKT_BUF_SIZE]; n2n_trans_op_t transop_null, transop_tf; n2n_trans_op_t transop_aes; @@ -65,7 +66,7 @@ int main(int argc, char * argv[]) { char *test_name = "environment"; printf("%s: community_name = \"%s\"\n", test_name, conf.community_name); printf("%s: encrypt_key = \"%s\"\n", test_name, conf.encrypt_key); - printf("%s: input size = 0x%"PRIx64"\n", test_name, sizeof(PKT_CONTENT)); + printf("%s: input size = 0x%" PRIx64 "\n", test_name, sizeof(PKT_CONTENT)); fhexdump(0, PKT_CONTENT, sizeof(PKT_CONTENT), stdout); printf("\n"); @@ -96,7 +97,7 @@ int main(int argc, char * argv[]) { // --- cipher benchmark ------------------------------------------------------------------- -static void run_transop_benchmark(const char *op_name, n2n_trans_op_t *op_fn, n2n_edge_conf_t *conf, uint8_t *pktbuf) { +static void run_transop_benchmark (const char *op_name, n2n_trans_op_t *op_fn, n2n_edge_conf_t *conf, uint8_t *pktbuf) { n2n_common_t cmn; n2n_PACKET_t pkt; n2n_mac_t mac_buf; @@ -107,13 +108,13 @@ static void run_transop_benchmark(const char *op_name, n2n_trans_op_t *op_fn, n2 // encryption memset(mac_buf, 0, sizeof(mac_buf)); - + nw = do_encode_packet( pktbuf, N2N_PKT_BUF_SIZE, conf->community_name); nw += op_fn->fwd(op_fn, - pktbuf+nw, N2N_PKT_BUF_SIZE-nw, - PKT_CONTENT, sizeof(PKT_CONTENT), mac_buf); - - printf("%s: output size = 0x%"PRIx64"\n", op_name, nw); + pktbuf+nw, N2N_PKT_BUF_SIZE-nw, + PKT_CONTENT, sizeof(PKT_CONTENT), mac_buf); + + printf("%s: output size = 0x%" PRIx64 "\n", op_name, nw); fhexdump(0, pktbuf, nw, stdout); // decrpytion @@ -133,9 +134,9 @@ static void run_transop_benchmark(const char *op_name, n2n_trans_op_t *op_fn, n2 } -static ssize_t do_encode_packet( uint8_t * pktbuf, size_t bufsize, const n2n_community_t c ) +static ssize_t do_encode_packet ( uint8_t * pktbuf, size_t bufsize, const n2n_community_t c ) { - // FIXME: this is a parameter of the test environment + // FIXME: this is a parameter of the test environment n2n_mac_t destMac={0,1,2,3,4,5}; n2n_common_t cmn; diff --git a/tools/tests-wire.c b/tools/tests-wire.c index 184092f..9e14f01 100644 --- a/tools/tests-wire.c +++ b/tools/tests-wire.c @@ -21,21 +21,21 @@ #include "n2n.h" #include "hexdump.h" -void init_ip_subnet(n2n_ip_subnet_t * d) { +void init_ip_subnet (n2n_ip_subnet_t * d) { d->net_addr = 0x20212223; d->net_bitlen = 25; } -void print_ip_subnet(char *test_name, char *field, n2n_ip_subnet_t * d) { +void print_ip_subnet (char *test_name, char *field, n2n_ip_subnet_t * d) { printf("%s: %s.net_addr = 0x%08x\n", - test_name, field, d->net_addr); + test_name, field, d->net_addr); printf("%s: %s.net_bitlen = %i\n", - test_name, field, d->net_bitlen); + test_name, field, d->net_bitlen); } -void init_mac(n2n_mac_t mac, const uint8_t o0, const uint8_t o1, - const uint8_t o2, const uint8_t o3, - const uint8_t o4, const uint8_t o5) { +void init_mac (n2n_mac_t mac, const uint8_t o0, const uint8_t o1, + const uint8_t o2, const uint8_t o3, + const uint8_t o4, const uint8_t o5) { mac[0] = o0; mac[1] = o1; mac[2] = o2; @@ -44,13 +44,13 @@ void init_mac(n2n_mac_t mac, const uint8_t o0, const uint8_t o1, mac[5] = o5; } -void print_mac(char *test_name, char *field, n2n_mac_t mac) { +void print_mac (char *test_name, char *field, n2n_mac_t mac) { printf("%s: %s[] = %x:%x:%x:%x:%x:%x\n", - test_name, field, - mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + test_name, field, + mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); } -void init_auth(n2n_auth_t *auth) { +void init_auth (n2n_auth_t *auth) { auth->scheme = n2n_auth_simple_id; auth->token_size = 16; auth->token[0] = 0xfe; @@ -59,13 +59,13 @@ void init_auth(n2n_auth_t *auth) { auth->token[15] = 0xfb; } -void print_auth(char *test_name, char *field, n2n_auth_t *auth) { +void print_auth (char *test_name, char *field, n2n_auth_t *auth) { printf("%s: %s.scheme = %i\n", test_name, field, auth->scheme); printf("%s: %s.token_size = %i\n", test_name, field, auth->token_size); printf("%s: %s.token[0] = 0x%02x\n", test_name, field, auth->token[0]); } -void init_common(n2n_common_t *common, char *community) { +void init_common (n2n_common_t *common, char *community) { memset( common, 0, sizeof(*common) ); common->ttl = N2N_DEFAULT_TTL; common->flags = 0; @@ -73,13 +73,13 @@ void init_common(n2n_common_t *common, char *community) { common->community[N2N_COMMUNITY_SIZE - 1] = '\0'; } -void print_common(char *test_name, n2n_common_t *common) { +void print_common (char *test_name, n2n_common_t *common) { printf("%s: common.ttl = %i\n", test_name, common->ttl); printf("%s: common.flags = %i\n", test_name, common->flags); printf("%s: common.community = \"%s\"\n", test_name, common->community); } -void test_REGISTER(n2n_common_t *common) { +void test_REGISTER (n2n_common_t *common) { char *test_name = "REGISTER"; common->pc = n2n_register; @@ -104,8 +104,8 @@ void test_REGISTER(n2n_common_t *common) { size_t idx = 0; size_t retval = encode_REGISTER( pktbuf, &idx, common, ®); - printf("%s: output retval = 0x%"PRIx64"\n", test_name, retval); - printf("%s: output idx = 0x%"PRIx64"\n", test_name, idx); + printf("%s: output retval = 0x%" PRIx64 "\n", test_name, retval); + printf("%s: output idx = 0x%" PRIx64 "\n", test_name, idx); fhexdump(0, pktbuf, idx, stdout); // TODO: decode_REGISTER() and print @@ -114,7 +114,7 @@ void test_REGISTER(n2n_common_t *common) { printf("\n"); } -void test_REGISTER_SUPER(n2n_common_t *common) { +void test_REGISTER_SUPER (n2n_common_t *common) { char *test_name = "REGISTER_SUPER"; common->pc = n2n_register_super; @@ -136,15 +136,15 @@ void test_REGISTER_SUPER(n2n_common_t *common) { print_ip_subnet(test_name, "reg.dev_addr", ®.dev_addr); printf("%s: reg.dev_desc = \"%s\"\n", test_name, reg.dev_desc); print_auth(test_name, "reg.auth", ®.auth); - printf("%s: reg.key_time = %"PRIi32"\n", test_name, reg.key_time); + printf("%s: reg.key_time = %" PRIi32 "\n", test_name, reg.key_time); printf("\n"); uint8_t pktbuf[N2N_PKT_BUF_SIZE]; size_t idx = 0; size_t retval = encode_REGISTER_SUPER( pktbuf, &idx, common, ®); - printf("%s: output retval = 0x%"PRIx64"\n", test_name, retval); - printf("%s: output idx = 0x%"PRIx64"\n", test_name, idx); + printf("%s: output retval = 0x%" PRIx64 "\n", test_name, retval); + printf("%s: output idx = 0x%" PRIx64 "\n", test_name, idx); fhexdump(0, pktbuf, idx, stdout); // TODO: decode_REGISTER_SUPER() and print @@ -153,7 +153,7 @@ void test_REGISTER_SUPER(n2n_common_t *common) { printf("\n"); } -void test_UNREGISTER_SUPER(n2n_common_t *common) { +void test_UNREGISTER_SUPER (n2n_common_t *common) { char *test_name = "UNREGISTER_SUPER"; common->pc = n2n_unregister_super; @@ -173,8 +173,8 @@ void test_UNREGISTER_SUPER(n2n_common_t *common) { size_t idx = 0; size_t retval = encode_UNREGISTER_SUPER( pktbuf, &idx, common, &unreg); - printf("%s: output retval = 0x%"PRIx64"\n", test_name, retval); - printf("%s: output idx = 0x%"PRIx64"\n", test_name, idx); + printf("%s: output retval = 0x%" PRIx64 "\n", test_name, retval); + printf("%s: output idx = 0x%" PRIx64 "\n", test_name, idx); fhexdump(0, pktbuf, idx, stdout); // TODO: decode_UNREGISTER_SUPER() and print @@ -183,7 +183,7 @@ void test_UNREGISTER_SUPER(n2n_common_t *common) { printf("\n"); } -int main(int argc, char * argv[]) { +int main (int argc, char * argv[]) { char *test_name = "environment"; n2n_common_t common; diff --git a/uncrustify.cfg b/uncrustify.cfg new file mode 100644 index 0000000..baef9cd --- /dev/null +++ b/uncrustify.cfg @@ -0,0 +1,34 @@ +# Initial rules taken from a quick discussion +# (See https://github.com/ntop/n2n/commit/00159d0d012c6836fd972af1748833eeaf50fa22#commitcomment-57137247) + +# 4 space indention (never use tabs) +indent_columns = 4 +indent_with_tabs = 0 +indent_switch_case = 4 + +# space between name and bracket during function define +sp_func_def_paren = force +sp_func_proto_paren = force + +# no space between name and bracket during call +sp_func_call_paren = remove + +# no space after if and while +sp_before_sparen = remove +#sp_while_paren_open = remove # only in newer uncrustify + +# block-braces as seen above +nl_if_brace = remove +nl_brace_else = remove +nl_elseif_brace = remove +nl_else_brace = remove +#nl_before_opening_brace_func_class_def = remove # only in newer uncrustify +nl_for_brace = remove +nl_while_brace = remove + +# multi-line parameters with indentation under the opening bracket +# looks like this is the default, but might be the following: +#indent_func_call_param = false ? + +# Want to keep var definition alignment +#align_keep_extra_space = true