From 996cbb5f2bc4a6dc2a9168052efb730ade59f902 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Tue, 24 Jul 2018 18:09:18 +0200 Subject: [PATCH] tools: support ancient NDKs Signed-off-by: Jason A. Donenfeld --- app/tools/CMakeLists.txt | 8 ++-- app/tools/ndk-compat/compat.c | 77 +++++++++++++++++++++++++++++++++++ app/tools/ndk-compat/compat.h | 16 ++++++++ app/tools/wireguard | 2 +- 4 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 app/tools/ndk-compat/compat.c create mode 100644 app/tools/ndk-compat/compat.h diff --git a/app/tools/CMakeLists.txt b/app/tools/CMakeLists.txt index 840e9f59..f390163f 100644 --- a/app/tools/CMakeLists.txt +++ b/app/tools/CMakeLists.txt @@ -8,12 +8,12 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}") # Work around https://github.com/android-ndk/ndk/issues/602 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold") -add_executable(libwg-quick.so wireguard/src/tools/wg-quick/android.c) -target_compile_options(libwg-quick.so PUBLIC -O3 -std=gnu11 -Wall -pedantic -Wno-missing-field-initializers -DWG_PACKAGE_NAME=\"${ANDROID_PACKAGE_NAME}\") +add_executable(libwg-quick.so wireguard/src/tools/wg-quick/android.c ndk-compat/compat.c) +target_compile_options(libwg-quick.so PUBLIC -O3 -std=gnu11 -Wall -pedantic -Wno-missing-field-initializers -include ${CMAKE_CURRENT_SOURCE_DIR}/ndk-compat/compat.h -DWG_PACKAGE_NAME=\"${ANDROID_PACKAGE_NAME}\") -file(GLOB WG_SOURCES wireguard/src/tools/*.c libmnl/src/*.c) +file(GLOB WG_SOURCES wireguard/src/tools/*.c libmnl/src/*.c ndk-compat/compat.c) add_executable(libwg.so ${WG_SOURCES}) -target_compile_options(libwg.so PUBLIC -idirafter "${CMAKE_CURRENT_SOURCE_DIR}/libmnl/include/" -I "${CMAKE_CURRENT_SOURCE_DIR}/wireguard/src/tools/" -O3 -std=gnu11 -D_GNU_SOURCE -DHAVE_VISIBILITY_HIDDEN -DRUNSTATEDIR=\"/data/data/${ANDROID_PACKAGE_NAME}/cache\" -Wno-pointer-arith -Wno-unused-parameter) +target_compile_options(libwg.so PUBLIC -idirafter "${CMAKE_CURRENT_SOURCE_DIR}/libmnl/include/" -I "${CMAKE_CURRENT_SOURCE_DIR}/wireguard/src/tools/" -O3 -std=gnu11 -D_GNU_SOURCE -include ${CMAKE_CURRENT_SOURCE_DIR}/ndk-compat/compat.h -DHAVE_VISIBILITY_HIDDEN -DRUNSTATEDIR=\"/data/data/${ANDROID_PACKAGE_NAME}/cache\" -Wno-pointer-arith -Wno-unused-parameter) add_custom_target(libwg-go.so WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/libwg-go" COMMENT "Building wireguard-go" VERBATIM COMMAND make ANDROID_ARCH_NAME=${ANDROID_ARCH_NAME} diff --git a/app/tools/ndk-compat/compat.c b/app/tools/ndk-compat/compat.c new file mode 100644 index 00000000..ecf653f8 --- /dev/null +++ b/app/tools/ndk-compat/compat.c @@ -0,0 +1,77 @@ +/* SPDX-License-Identifier: BSD + * + * Copyright (C) 2015-2018 Jason A. Donenfeld . All Rights Reserved. + * + */ + +#define FILE_IS_EMPTY + +#if defined(__ANDROID_API__) && __ANDROID_API__ < 18 +#undef FILE_IS_EMPTY +#include +#include + +ssize_t getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp) +{ + char *ptr, *eptr; + + if (*buf == NULL || *bufsiz == 0) { + *bufsiz = BUFSIZ; + if ((*buf = malloc(*bufsiz)) == NULL) + return -1; + } + + for (ptr = *buf, eptr = *buf + *bufsiz;;) { + int c = fgetc(fp); + if (c == -1) { + if (feof(fp)) { + ssize_t diff = (ssize_t)(ptr - *buf); + if (diff != 0) { + *ptr = '\0'; + return diff; + } + } + return -1; + } + *ptr++ = c; + if (c == delimiter) { + *ptr = '\0'; + return ptr - *buf; + } + if (ptr + 2 >= eptr) { + char *nbuf; + size_t nbufsiz = *bufsiz * 2; + ssize_t d = ptr - *buf; + if ((nbuf = realloc(*buf, nbufsiz)) == NULL) + return -1; + *buf = nbuf; + *bufsiz = nbufsiz; + eptr = nbuf + nbufsiz; + ptr = nbuf + d; + } + } +} + +ssize_t getline(char **buf, size_t *bufsiz, FILE *fp) +{ + return getdelim(buf, bufsiz, '\n', fp); +} +#endif + +#if defined(__ANDROID_API__) && __ANDROID_API__ < 24 +#undef FILE_IS_EMPTY +#include + +char *strchrnul(const char *s, int c) +{ + char *x = strchr(s, c); + if (!x) + return (char *)s + strlen(s); + return x; +} +#endif + +#ifdef FILE_IS_EMPTY +#undef FILE_IS_EMPTY +static char ____x __attribute__((unused)); +#endif diff --git a/app/tools/ndk-compat/compat.h b/app/tools/ndk-compat/compat.h new file mode 100644 index 00000000..91f5e7c9 --- /dev/null +++ b/app/tools/ndk-compat/compat.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: BSD + * + * Copyright (C) 2015-2018 Jason A. Donenfeld . All Rights Reserved. + * + */ + +#if defined(__ANDROID_API__) && __ANDROID_API__ < 18 +#include +ssize_t getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp); +ssize_t getline(char **buf, size_t *bufsiz, FILE *fp); +#endif + +#if defined(__ANDROID_API__) && __ANDROID_API__ < 24 +char *strchrnul(const char *s, int c); +#endif + diff --git a/app/tools/wireguard b/app/tools/wireguard index be3e2a91..f52f9d04 160000 --- a/app/tools/wireguard +++ b/app/tools/wireguard @@ -1 +1 @@ -Subproject commit be3e2a9134e1833c313e700132018de12fe2839d +Subproject commit f52f9d045f5a6dceea2bbe70e22405ea07c62960