├── .gitignore ├── .gitmodules ├── build.sh └── build └── cmake ├── CMakeLists.txt ├── headers.cmake ├── iptables ├── CMakeLists.txt └── iptables.cmake └── lib ├── CMakeLists.txt ├── filter_init ├── gen.cmake ├── gen_init ├── libextensions.cmake ├── libiptc.cmake └── libxtables.cmake /.gitignore: -------------------------------------------------------------------------------- 1 | .* 2 | *~ 3 | *.[ao] 4 | *.diff 5 | *.la 6 | *.lo 7 | *.mod.c 8 | *.orig 9 | *.patch 10 | *.rej 11 | *.so 12 | *.so.dbg 13 | *.tar.* 14 | 15 | # 16 | # Generated files 17 | # 18 | aclocal.m4 19 | autom4te.cache 20 | config.* 21 | Makefile 22 | Makefile.in 23 | config/ 24 | m4/ 25 | configure 26 | configure.scan 27 | libtool 28 | stamp-h 29 | stamp-h1 30 | *.bak 31 | CMakeCache.txt 32 | CMakeFiles 33 | cmake_install.cmake 34 | out/ 35 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "src/iptables"] 2 | path = src/iptables 3 | url = https://android.googlesource.com/platform/external/iptables 4 | branch = master 5 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | BASE_DIR="$(pwd)" 2 | BUILD_CMAKE_DIR="${BASE_DIR}/build/cmake" 3 | OUT="${BASE_DIR}/out" 4 | BIN_DIR="${OUT}/iptables" 5 | rm *[.zip] > /dev/null 2>&1 6 | 7 | cmake_build() 8 | { 9 | local TARGET_PLAT=$1 10 | local COMPILE_ABI=$2 11 | local COMPILE_METHOD=$3 12 | local ANDROID_PLATFORM_VER=$4 13 | 14 | local BUILD_METHOD="" 15 | local MAKE_CMD="" 16 | if [ "$COMPILE_METHOD" == "Ninja" ]; then 17 | BUILD_METHOD="-G Ninja" 18 | echo ${CMAKE_CMD} 19 | MAKE_CMD="time -p ninja -C $OUT" 20 | elif [ "$COMPILE_METHOD" == "make" ]; then 21 | MAKE_CMD="time -p make -C $OUT -j$(nproc)" 22 | fi; 23 | 24 | if [ "$TARGET_PLAT" == "Android" ]; then 25 | cmake -S ${BUILD_CMAKE_DIR} -B $OUT ${BUILD_METHOD} \ 26 | -DNDK_CCACHE="ccache" \ 27 | -DCMAKE_BUILD_TYPE="Release" \ 28 | -DANDROID_PLATFORM="${ANDROID_PLATFORM_VER}" \ 29 | -DANDROID_ABI="${COMPILE_ABI}" \ 30 | -DANDROID_STL="c++_static" \ 31 | -DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK_HOME}/build/cmake/android.toolchain.cmake" \ 32 | -DANDROID_USE_LEGACY_TOOLCHAIN_FILE="OFF" 33 | elif [ "$TARGET_PLAT" == "Linux" ]; then 34 | ##指定第三方clang 路径:CLANG_PATH="" 35 | CLANG_PATH="" 36 | cmake -S ${BUILD_CMAKE_DIR} -B ${OUT} ${BUILD_METHOD} \ 37 | -DCMAKE_C_COMPILER_LAUNCHER="ccache" \ 38 | -DCMAKE_CXX_COMPILER_LAUNCHER="ccache" \ 39 | -DCMAKE_C_COMPILER="${CLANG_PATH}clang" \ 40 | -DCMAKE_CXX_COMPILER="${CLANG_PATH}clang++" 41 | fi 42 | 43 | ${MAKE_CMD} 44 | } 45 | 46 | build() 47 | { 48 | clear 49 | local TARGET_PLAT=$1 50 | local COMPILE_ABI=$2 51 | local ANDROID_PLATFORM=$3 52 | 53 | rm -r $OUT > /dev/null 2>&1 54 | 55 | local NINJA=`which ninja` 56 | local COMPILE_METHOD 57 | if [[ -f $NINJA ]]; then 58 | COMPILE_METHOD="Ninja" 59 | else 60 | COMPILE_METHOD="make" 61 | fi 62 | echo "skkk: TARGET_PLAT=${TARGET_PLAT}" 63 | echo "skkk: COMPILE_ABI=${COMPILE_ABI}" 64 | echo "skkk: ANDROID_PLATFORM=${ANDROID_PLATFORM}" 65 | time cmake_build "${TARGET_PLAT}" "${COMPILE_ABI}" "${COMPILE_METHOD}" "${ANDROID_PLATFORM}" 66 | 67 | local IPTABLES_BIN="$BIN_DIR/iptables" 68 | 69 | local RET=0 70 | [ -f "$IPTABLES_BIN" ] && RET=1 71 | 72 | if [ $RET -eq 1 ]; then 73 | echo "打包中..." 74 | touch -c -d "2009-01-01 08:00:00" ${BIN_DIR}/* 75 | rm $BIN_DIR/*.a > /dev/null 2>&1 76 | rm $BIN_DIR/*.cmake > /dev/null 2>&1 77 | zip -9 -jy "iptables-${TARGET_PLAT}_${COMPILE_ABI}-$(TZ=UTC-8 date +%y%m%d%H%M).zip" $BIN_DIR/* > /dev/null 2>&1 78 | echo "打包完成!" 79 | else 80 | echo "error" 81 | exit -1 82 | fi 83 | } 84 | 85 | build "Android" "arm64-v8a" "android-29" 86 | #build "Android" "armeabi-v7a" "android-29" 87 | build "Android" "x86_64" "android-29" 88 | #build "Android" "x86" "android-29" 89 | build "Linux" "x86_64" 90 | -------------------------------------------------------------------------------- /build/cmake/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.24) 2 | project(iptables LANGUAGES C CXX) 3 | 4 | set(CMAKE_C_STANDARD 11) 5 | set(CMAKE_CXX_STANDARD 20) 6 | 7 | set(CMAKE_C_FLAGS "") 8 | set(CMAKE_CXX_FLAGS "") 9 | 10 | set(CMAKE_VERBOSE_MAKEFILE ON) 11 | set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 12 | 13 | # File options 14 | add_definitions("-DNDEBUG") 15 | 16 | # Compile flags 17 | set(GLOBAL_CFLAGS 18 | "-Os" 19 | "-fdata-sections" 20 | "-ffunction-sections" 21 | "-funwind-tables" 22 | "-fstack-protector-strong" 23 | "-D_FORTIFY_SOURCE=2" 24 | "-no-canonical-prefixes" 25 | "-fvisibility=hidden" 26 | "-fvisibility-inlines-hidden" 27 | "-fno-exceptions" 28 | "-fno-rtti" 29 | ) 30 | 31 | # Linker flags 32 | set(GLOBAL_LDFLAGS 33 | "-fstack-protector-strong" 34 | "-Wl,--fatal-warnings" 35 | "-Qunused-arguments" 36 | "-Wl,--no-undefined" 37 | "-Wl,--gc-sections" 38 | "-static" 39 | "-s" 40 | ) 41 | 42 | if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") 43 | list(APPEND GLOBAL_CFLAGS "-fdiagnostics-color=always") 44 | elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") 45 | list(APPEND GLOBAL_CFLAGS "-fcolor-diagnostics") 46 | endif() 47 | 48 | if (CMAKE_SYSTEM_NAME MATCHES "Android") 49 | list(APPEND GLOBAL_CFLAGS "-mllvm" "-polly") 50 | list(APPEND GLOBAL_LDFLAGS "-Wl,--build-id=none") 51 | endif() 52 | 53 | add_compile_options("$<$:${GLOBAL_CFLAGS}>" "$<$:${GLOBAL_CFLAGS}>") 54 | add_link_options("$<$:${GLOBAL_LDFLAGS}>" "$<$:${GLOBAL_LDFLAGS}>") 55 | 56 | # Git submodule 57 | execute_process(COMMAND git submodule init) 58 | execute_process(COMMAND git submodule update) 59 | 60 | set(SRC_DIR "${PROJECT_SOURCE_DIR}/../../src") 61 | 62 | # Project headers 63 | include(headers.cmake) 64 | 65 | set(iptables_default_cflags 66 | "-D_LARGEFILE_SOURCE=1" 67 | "-D_LARGE_FILES" 68 | "-D_FILE_OFFSET_BITS=64" 69 | "-D_REENTRANT" 70 | "-DENABLE_IPV4" 71 | "-DENABLE_IPV6" 72 | "-Wall" 73 | "-Werror" 74 | "-Wno-pointer-arith" 75 | "-Wno-sign-compare" 76 | "-Wno-unused-parameter" 77 | ) 78 | 79 | # Start building 80 | add_subdirectory(lib) 81 | add_subdirectory(iptables) 82 | -------------------------------------------------------------------------------- /build/cmake/headers.cmake: -------------------------------------------------------------------------------- 1 | # Headers 2 | set(iptables_headers "${SRC_DIR}/iptables/include" CACHE INTERNAL "iptables_headers") 3 | set(iptables_config_header "${SRC_DIR}/iptables" CACHE INTERNAL "iptables_config_header") 4 | set(iptables_iptables_headers "${SRC_DIR}/iptables/iptables" CACHE INTERNAL "iptables_iptables_headers") 5 | -------------------------------------------------------------------------------- /build/cmake/iptables/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include(iptables.cmake) 2 | -------------------------------------------------------------------------------- /build/cmake/iptables/iptables.cmake: -------------------------------------------------------------------------------- 1 | set(TARGET_iptables iptables) 2 | 3 | set(TARGET_SRC_DIR "${SRC_DIR}/iptables/iptables") 4 | 5 | set(TARGET_CFLAGS 6 | ${iptables_default_cflags} 7 | "-Wno-missing-field-initializers" 8 | "-Wno-parentheses-equality" 9 | "-DNO_SHARED_LIBS=1" 10 | "-DALL_INCLUSIVE" 11 | "-DXTABLES_INTERNAL" 12 | ) 13 | 14 | set(iptables_default_srcs 15 | "${TARGET_SRC_DIR}/xtables-legacy-multi.c" 16 | "${TARGET_SRC_DIR}/iptables-xml.c" 17 | "${TARGET_SRC_DIR}/xshared.c" 18 | ) 19 | 20 | set(iptables_srcs 21 | ${iptables_default_srcs} 22 | "${TARGET_SRC_DIR}/iptables-save.c" 23 | "${TARGET_SRC_DIR}/iptables-restore.c" 24 | "${TARGET_SRC_DIR}/iptables-standalone.c" 25 | "${TARGET_SRC_DIR}/iptables.c" 26 | "${TARGET_SRC_DIR}/ip6tables-standalone.c" 27 | "${TARGET_SRC_DIR}/ip6tables.c" 28 | ) 29 | 30 | set(static_link_lib 31 | -Wl,--start-group 32 | ext_static 33 | ext4_static 34 | ext6_static 35 | xtables_static 36 | ip4tc_static 37 | ip6tc_static 38 | -lm 39 | -Wl,--end-group 40 | ) 41 | 42 | set(iptables_symlink 43 | "iptables-save" 44 | "iptables-restore" 45 | "ip6tables-save" 46 | "ip6tables-restore" 47 | ) 48 | 49 | foreach(symlink ${iptables_symlink}) 50 | add_custom_target(${symlink} ALL 51 | ${CMAKE_COMMAND} -E create_symlink ${TARGET_iptables} ${symlink} 52 | DEPENDS ${TARGET_iptables} 53 | COMMENT "Creating ${symlink} symlink" 54 | ) 55 | endforeach(symlink) 56 | 57 | add_executable(${TARGET_iptables} ${iptables_srcs}) 58 | target_include_directories(${TARGET_iptables} PRIVATE 59 | ${iptables_headers} 60 | ${iptables_config_header} 61 | ) 62 | target_link_libraries(${TARGET_iptables} ${static_link_lib}) 63 | if (CMAKE_SYSTEM_NAME MATCHES "Linux") 64 | target_link_options(${TARGET_iptables} PRIVATE "-Wl,--no-fatal-warnings") 65 | endif() 66 | target_compile_options(${TARGET_iptables} PRIVATE ${TARGET_CFLAGS}) 67 | -------------------------------------------------------------------------------- /build/cmake/lib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | include(libextensions.cmake) 2 | include(libxtables.cmake) 3 | include(libiptc.cmake) 4 | -------------------------------------------------------------------------------- /build/cmake/lib/filter_init: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # This is for working around Android.mk's incapability to handle $* in CFLAGS, 3 | # even with SECONDEXPNASION. 4 | # LOCAL_CFLAGS:=-D_INIT=$*_init 5 | f=${1##*/} 6 | f=${f%%.*} 7 | sed "s/\([ ]*\)\(${f}.*_init\|_init\)\(([ ]*void\)/\1${f}_init\3/" $1 8 | -------------------------------------------------------------------------------- /build/cmake/lib/gen.cmake: -------------------------------------------------------------------------------- 1 | function(listRemoveAll source_list remove_list) 2 | set (new_source_list ${${source_list}}) 3 | foreach (item ${${remove_list}}) 4 | list(REMOVE_ITEM new_source_list ${item}) 5 | #message("exc:=${item}") 6 | endforeach(item) 7 | #[[foreach (item ${new_source_list}) 8 | message("list:=${item}") 9 | endforeach(item)]] 10 | set(${source_list} ${new_source_list} PARENT_SCOPE) 11 | endfunction(listRemoveAll) 12 | 13 | set(gen_init "${CMAKE_CURRENT_SOURCE_DIR}/gen_init") 14 | set(filter_init "${CMAKE_CURRENT_SOURCE_DIR}/filter_init") 15 | 16 | function(genrule flag target_srcs out_src) 17 | set(_target_srcs) 18 | foreach (item ${${target_srcs}}) 19 | STRING(REGEX REPLACE ".+/(.+)" "\\1" FILE_NAME ${item}) 20 | #message("skkk=${FILE_NAME}") 21 | list(APPEND _target_srcs ${FILE_NAME}) 22 | endforeach(item) 23 | string (REPLACE ";" " " files "${_target_srcs}") 24 | execute_process(COMMAND sh -c "${gen_init} '${flag}' ${files} > ${out_src}") 25 | set(${target_srcs} 26 | ${${target_srcs}} 27 | ${out_src} 28 | PARENT_SCOPE 29 | ) 30 | endfunction(genrule) 31 | 32 | function(gensrcs output_extension target_srcs) 33 | foreach (item ${${target_srcs}}) 34 | string(REGEX REPLACE "\\.[^.]*$" "" FILE_NAME ${item}) 35 | #message("skkk=${FILE_NAME}") 36 | execute_process(COMMAND 37 | sh -c 38 | "${filter_init} ${item}" 39 | OUTPUT_VARIABLE OUTBUF 40 | ) 41 | file(WRITE "${FILE_NAME}.${output_extension}" "${OUTBUF}") 42 | endforeach(item) 43 | endfunction(gensrcs) 44 | -------------------------------------------------------------------------------- /build/cmake/lib/gen_init: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | # 3 | # Generate init_extensions* functions to call all the _init functions from 4 | # filter_init 5 | # 6 | # Usage: gen_init filename... 7 | # 8 | # Example output: 9 | # 10 | # void libxt_tcp_init(void); 11 | # void libxt_udp_init(void); 12 | # void init_extensions(void); 13 | # void init_extensions(void) { 14 | # libxt_tcp_init(); 15 | # libxt_udp_init(); 16 | # } 17 | 18 | EXT=$1 19 | shift 20 | 21 | for i in "$@"; do 22 | f=${i##*/} 23 | f=${f%%.*} 24 | echo "void ${f}_init(void);" 25 | done 26 | 27 | echo "void init_extensions${EXT}(void);" 28 | echo "void init_extensions${EXT}(void) {" 29 | 30 | for i in "$@"; do 31 | f=${i##*/} 32 | f=${f%%.*} 33 | echo " ${f}_init();" 34 | done 35 | 36 | echo "}" 37 | -------------------------------------------------------------------------------- /build/cmake/lib/libextensions.cmake: -------------------------------------------------------------------------------- 1 | include(gen.cmake) 2 | 3 | set(TARGET_SRC_DIR "${SRC_DIR}/iptables/extensions") 4 | 5 | set(libext_default_cflags 6 | ${iptables_default_cflags} 7 | "-DNO_SHARED_LIBS=1" 8 | "-DXTABLES_INTERNAL" 9 | "-Wno-format" 10 | "-Wno-missing-field-initializers" 11 | # libxt_recent.c:202:11: error: address of array 'info->name' will always evaluate to 'true' [-Werror,-Wpointer-bool-conversion] 12 | "-Wno-pointer-bool-conversion" 13 | "-Wno-tautological-pointer-compare" 14 | ) 15 | 16 | ###########################################ext_static############################################ 17 | set(TARGET ext_static) 18 | 19 | file(GLOB libext_static_srcs "${TARGET_SRC_DIR}/libxt_*.c") 20 | 21 | set(libext_static_excluse_srcs 22 | # Exclude some modules that are problematic to compile (types/headers) 23 | "${TARGET_SRC_DIR}/libxt_TCPOPTSTRIP.c" 24 | "${TARGET_SRC_DIR}/libxt_connlabel.c" 25 | "${TARGET_SRC_DIR}/libxt_cgroup.c" 26 | "${TARGET_SRC_DIR}/libxt_dccp.c" 27 | "${TARGET_SRC_DIR}/libxt_ipvs.c" 28 | ) 29 | listRemoveAll(libext_static_srcs libext_static_excluse_srcs) 30 | gensrcs("c" libext_static_srcs) 31 | genrule("" libext_static_srcs "${TARGET_SRC_DIR}/initext.c") 32 | 33 | add_library(${TARGET} STATIC ${libext_static_srcs}) 34 | 35 | target_include_directories(${TARGET} PRIVATE 36 | ${iptables_headers} 37 | ${iptables_config_header} 38 | ) 39 | 40 | target_compile_options(${TARGET} PRIVATE 41 | "$<$:${libext_default_cflags}>" 42 | "$<$:${libext_default_cflags}>" 43 | ) 44 | ################################################################################################## 45 | 46 | ###########################################ext4_static############################################ 47 | set(TARGET ext4_static) 48 | 49 | file(GLOB libext4_static_srcs "${TARGET_SRC_DIR}/libipt_*.c") 50 | 51 | gensrcs("c" libext4_static_srcs) 52 | genrule("4" libext4_static_srcs "${TARGET_SRC_DIR}/initext4.c") 53 | 54 | add_library(${TARGET} STATIC ${libext4_static_srcs}) 55 | 56 | target_include_directories(${TARGET} PRIVATE 57 | ${iptables_headers} 58 | ${iptables_config_header} 59 | ) 60 | 61 | target_compile_options(${TARGET} PRIVATE 62 | "$<$:${libext_default_cflags}>" 63 | "$<$:${libext_default_cflags}>" 64 | ) 65 | ################################################################################################## 66 | 67 | ###########################################ext6_static############################################ 68 | 69 | set(TARGET ext6_static) 70 | 71 | file(GLOB libext6_static_srcs "${TARGET_SRC_DIR}/libip6t_*.c") 72 | 73 | gensrcs("c" libext6_static_srcs) 74 | genrule("6" libext6_static_srcs "${TARGET_SRC_DIR}/initext6.c") 75 | 76 | add_library(${TARGET} STATIC ${libext6_static_srcs}) 77 | 78 | target_include_directories(${TARGET} PRIVATE 79 | ${iptables_headers} 80 | ${iptables_config_header} 81 | ) 82 | 83 | target_compile_options(${TARGET} PRIVATE 84 | "$<$:${libext_default_cflags}>" 85 | "$<$:${libext_default_cflags}>" 86 | ) 87 | ################################################################################################## 88 | -------------------------------------------------------------------------------- /build/cmake/lib/libiptc.cmake: -------------------------------------------------------------------------------- 1 | set(TARGET_SRC_DIR "${SRC_DIR}/iptables/libiptc") 2 | 3 | set(TARGET_CFLAGS 4 | ${iptables_default_cflags} 5 | "-Wno-pointer-sign" 6 | ) 7 | 8 | ###########################################ip4tc_static############################################ 9 | set(TARGET ip4tc_static) 10 | set(libip4tc_static_srcs "${TARGET_SRC_DIR}/libip4tc.c") 11 | add_library(${TARGET} STATIC ${libip4tc_static_srcs}) 12 | target_include_directories(${TARGET} PRIVATE ${iptables_headers}) 13 | target_compile_options(${TARGET} PRIVATE 14 | "$<$:${TARGET_CFLAGS}>" 15 | "$<$:${TARGET_CFLAGS}>" 16 | ) 17 | ################################################################################################## 18 | 19 | ###########################################ip6tc_static############################################ 20 | set(TARGET ip6tc_static) 21 | set(libip6tc_static_srcs "${TARGET_SRC_DIR}/libip6tc.c") 22 | add_library(${TARGET} STATIC ${libip6tc_static_srcs}) 23 | target_include_directories(${TARGET} PRIVATE ${iptables_headers}) 24 | target_compile_options(${TARGET} PRIVATE 25 | "$<$:${TARGET_CFLAGS}>" 26 | "$<$:${TARGET_CFLAGS}>" 27 | ) 28 | ################################################################################################## 29 | -------------------------------------------------------------------------------- /build/cmake/lib/libxtables.cmake: -------------------------------------------------------------------------------- 1 | set(TARGET xtables_static) 2 | 3 | set(TARGET_SRC_DIR "${SRC_DIR}/iptables/libxtables") 4 | 5 | set(TARGET_CFLAGS 6 | ${iptables_default_cflags} 7 | "-DNO_SHARED_LIBS=1" 8 | "-DXTABLES_INTERNAL" 9 | "-DXTABLES_LIBDIR=\"xtables_libdir_not_used\"" 10 | "-Wno-missing-field-initializers" 11 | ) 12 | 13 | set(libxtables_static_srcs 14 | "${TARGET_SRC_DIR}/xtables.c" 15 | "${TARGET_SRC_DIR}/xtoptions.c" 16 | ) 17 | 18 | add_library(${TARGET} STATIC ${libxtables_static_srcs}) 19 | 20 | target_include_directories(${TARGET} PRIVATE 21 | ${iptables_headers} 22 | ${iptables_iptables_headers} 23 | ${iptables_config_header} 24 | ) 25 | 26 | target_compile_options(${TARGET} PRIVATE 27 | "$<$:${TARGET_CFLAGS}>" 28 | "$<$:${TARGET_CFLAGS}>" 29 | ) 30 | --------------------------------------------------------------------------------