├── .gitattributes ├── .gitignore ├── LICENSE ├── Makefile.Darwin ├── Makefile.freebsd ├── Makefile.inc ├── Makefile.linux ├── Makefile.mingw_x86_64 ├── Makefile.openbsd ├── Makefile.openwrt ├── README.md ├── README_CN.md ├── docs ├── disclaimer_cn.md ├── gnb_diagnose_cn.md ├── gnb_user_manual_cn.md └── gnb_user_manual_en.md ├── examples └── node_config_example │ ├── address.conf │ ├── node.conf │ ├── route.conf │ └── scripts │ ├── if_down_darwin.sh │ ├── if_down_freebsd.sh │ ├── if_down_linux.sh │ ├── if_down_openbsd.sh │ ├── if_up_darwin.sh │ ├── if_up_freebsd.sh │ ├── if_up_linux.sh │ └── if_up_openbsd.sh ├── libs ├── README.md ├── ed25519 │ ├── add_scalar.c │ ├── ed25519.h │ ├── fe.c │ ├── fe.h │ ├── fixedint.h │ ├── ge.c │ ├── ge.h │ ├── key_exchange.c │ ├── keypair.c │ ├── license.txt │ ├── precomp_data.h │ ├── sc.c │ ├── sc.h │ ├── seed.c │ ├── sha512.c │ ├── sha512.h │ ├── sign.c │ └── verify.c ├── hash │ └── murmurhash.c ├── libnatpmp │ ├── LICENSE │ ├── declspec.h │ ├── getgateway.c │ ├── getgateway.h │ ├── natpmp.c │ ├── natpmp.h │ ├── wingettimeofday.c │ └── wingettimeofday.h ├── miniupnpc │ ├── LICENSE │ ├── README │ ├── VERSION │ ├── codelength.h │ ├── connecthostport.c │ ├── connecthostport.h │ ├── igd_desc_parse.c │ ├── igd_desc_parse.h │ ├── minisoap.c │ ├── minisoap.h │ ├── minissdpc.c │ ├── minissdpc.h │ ├── miniupnpc.c │ ├── miniupnpc.h │ ├── miniupnpc_declspec.h │ ├── miniupnpc_socketdef.h │ ├── miniupnpcstrings.h │ ├── miniupnpcstrings.h.in │ ├── miniupnpctypes.h │ ├── miniwget.c │ ├── miniwget.h │ ├── miniwget_private.h │ ├── minixml.c │ ├── minixml.h │ ├── portlistingparse.c │ ├── portlistingparse.h │ ├── receivedata.c │ ├── receivedata.h │ ├── upnpcommands.c │ ├── upnpcommands.h │ ├── upnpdev.c │ ├── upnpdev.h │ ├── upnperrors.c │ ├── upnperrors.h │ ├── upnpreplyparse.c │ └── upnpreplyparse.h ├── protocol │ └── network_protocol.h ├── tap-windows │ └── tap-windows.h ├── wintun │ └── wintun.h └── zlib │ ├── LICENSE │ ├── adler32.c │ ├── deflate.c │ ├── deflate.h │ ├── gzguts.h │ ├── inffast.c │ ├── inffast.h │ ├── inffixed.h │ ├── inflate.c │ ├── inflate.h │ ├── inftrees.c │ ├── inftrees.h │ ├── trees.c │ ├── trees.h │ ├── zconf.h │ ├── zlib.h │ ├── zutil.c │ └── zutil.h ├── scripts └── opengnb@.service ├── src ├── Darwin │ └── gnb_drv_darwin.c ├── cli │ ├── gnb.c │ ├── gnb_crypto.c │ ├── gnb_ctl.c │ └── gnb_es.c ├── crypto │ ├── arc4 │ │ ├── arc4.c │ │ └── arc4.h │ ├── random │ │ ├── gnb_random.c │ │ └── gnb_random.h │ └── xor │ │ ├── xor.c │ │ └── xor.h ├── ctl │ └── gnb_ctl_dump.c ├── es │ ├── gnb_discover_in_lan.c │ ├── gnb_environment_service.c │ ├── gnb_es_broadcast_address.c │ ├── gnb_es_dump.c │ ├── gnb_es_platform.c │ ├── gnb_es_resolv.c │ ├── gnb_es_type.h │ └── gnb_es_upnp.c ├── freebsd │ └── gnb_drv_freebsd.c ├── gnb.h ├── gnb_address.c ├── gnb_address.h ├── gnb_address_type.h ├── gnb_alloc.c ├── gnb_alloc.h ├── gnb_arg_list.c ├── gnb_arg_list.h ├── gnb_argv.c ├── gnb_binary.c ├── gnb_binary.h ├── gnb_block.h ├── gnb_conf.c ├── gnb_conf_file.c ├── gnb_conf_file.h ├── gnb_conf_type.h ├── gnb_config_lite.c ├── gnb_config_lite.h ├── gnb_core.c ├── gnb_core.h ├── gnb_core_frame_type_defs.h ├── gnb_ctl_block.c ├── gnb_ctl_block.h ├── gnb_ctl_block_set.c ├── gnb_daemon.c ├── gnb_detect_worker.c ├── gnb_dir.c ├── gnb_dir.h ├── gnb_discover_in_lan_frame_type.h ├── gnb_doubly_linked_list.c ├── gnb_doubly_linked_list.h ├── gnb_exec.h ├── gnb_fixed_pool.c ├── gnb_fixed_pool.h ├── gnb_hash32.c ├── gnb_hash32.h ├── gnb_index_frame_type.h ├── gnb_index_service_worker.c ├── gnb_index_worker.c ├── gnb_keys.c ├── gnb_keys.h ├── gnb_log.c ├── gnb_log.h ├── gnb_log_type.h ├── gnb_lru32.c ├── gnb_lru32.h ├── gnb_mmap.c ├── gnb_mmap.h ├── gnb_mod_register.c ├── gnb_node.c ├── gnb_node.h ├── gnb_node_type.h ├── gnb_node_worker.c ├── gnb_nodeid.c ├── gnb_nodeid.h ├── gnb_payload16.c ├── gnb_payload16.h ├── gnb_pf.c ├── gnb_pf.h ├── gnb_pf_worker.c ├── gnb_pingpong_frame_type.h ├── gnb_platform.h ├── gnb_primary_worker.c ├── gnb_ring_buffer_fixed.c ├── gnb_ring_buffer_fixed.h ├── gnb_secure_index_service_worker.c ├── gnb_secure_index_worker.c ├── gnb_time.c ├── gnb_time.h ├── gnb_tun_drv.h ├── gnb_type.h ├── gnb_udp.c ├── gnb_udp.h ├── gnb_uf_node_frame_type.h ├── gnb_unified_forwarding.c ├── gnb_unified_forwarding.h ├── gnb_ur1_frame_type.h ├── gnb_worker.c ├── gnb_worker.h ├── gnb_worker_queue_data.h ├── gnb_worker_type.h ├── linux │ └── gnb_drv_linux.c ├── mingw │ ├── gnb.exe.manifest │ ├── gnb_drv_win32.c │ ├── gnb_drv_wintun.c │ ├── gnb_res.rc │ └── windows_platform.c ├── openbsd │ └── gnb_drv_openbsd.c ├── packet_filter │ ├── gnb_pf_crypto_arc4.c │ ├── gnb_pf_crypto_xor.c │ ├── gnb_pf_dump.c │ ├── gnb_pf_route.c │ └── gnb_pf_zip.c └── unix │ └── unix_platform.c └── version /.gitattributes: -------------------------------------------------------------------------------- 1 | *.sh linguist-language=c 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | #*.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | #*.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | 54 | #vscode 55 | .vscode 56 | -------------------------------------------------------------------------------- /Makefile.Darwin: -------------------------------------------------------------------------------- 1 | CFLAGS=-O2 -I./src -I./libs -I./libs/miniupnpc -I./libs/libnatpmp -I./libs/zlib -D NO_GZIP=1 -Wno-deprecated-non-prototype 2 | 3 | #-D Z_SOLO=0 4 | CLI_LDFLAGS=-L/usr/lib -pthread 5 | GNB_ES_LDFLAGS=-L/usr/lib -pthread 6 | 7 | 8 | GNB_CRYPTO=gnb_crypto 9 | GNB_CTL=gnb_ctl 10 | GNB_ES=gnb_es 11 | GNB_CLI=gnb 12 | 13 | 14 | include Makefile.inc 15 | 16 | 17 | GNB_CLI_OBJS = \ 18 | ./src/cli/gnb.o \ 19 | ./src/gnb_argv.o \ 20 | ./src/unix/unix_platform.o \ 21 | ./src/Darwin/gnb_drv_darwin.o 22 | 23 | GNB_ES_OBJS += ./src/unix/unix_platform.o 24 | 25 | all:${GNB_CLI} ${GNB_CRYPTO} ${GNB_ES} ${GNB_CTL} 26 | 27 | 28 | ${GNB_CTL}: ${GNB_CTL_OBJS} 29 | ${CC} -o ${GNB_CTL} ${GNB_CTL_OBJS} ${CLI_LDFLAGS} 30 | strip ${GNB_CTL} 31 | 32 | 33 | ${GNB_ES}: ${GNB_ES_OBJS} ${CRYPTO_OBJS} ${MINIUPNP_OBJS} ${LIBNATPMP_OBJS} 34 | ${CC} -o ${GNB_ES} ${GNB_ES_OBJS} ${CRYPTO_OBJS} ${MINIUPNP_OBJS} ${LIBNATPMP_OBJS} ${GNB_ES_LDFLAGS} 35 | strip ${GNB_ES} 36 | 37 | 38 | ${GNB_CRYPTO}: ${CRYPTO_OBJS} ./src/cli/gnb_crypto.o 39 | ${CC} -o ${GNB_CRYPTO} ./src/cli/gnb_crypto.o ${CRYPTO_OBJS} ${CLI_LDFLAGS} 40 | strip ${GNB_CRYPTO} 41 | 42 | 43 | ${GNB_CLI}: ${GNB_OBJS} ${GNB_CLI_OBJS} ${GNB_PF_OBJS} ${CRYPTO_OBJS} ${ZLIB_OBJS} 44 | ${CC} -o ${GNB_CLI} ${GNB_OBJS} ${GNB_CLI_OBJS} ${GNB_PF_OBJS} ${CRYPTO_OBJS} ${ZLIB_OBJS} ${CLI_LDFLAGS} 45 | strip ${GNB_CLI} 46 | 47 | 48 | %.o:%.c 49 | ${CC} ${CFLAGS} -c -o $@ $< 50 | 51 | 52 | install:${GNB_CLI} ${GNB_CRYPTO} ${GNB_ES} ${GNB_CTL} 53 | mkdir -p ./bin/ 54 | cp ${GNB_CLI} ./bin/ 55 | cp ${GNB_CTL} ./bin/ 56 | cp ${GNB_CRYPTO} ./bin/ 57 | cp ${GNB_ES} ./bin/ 58 | 59 | clean: 60 | find . -name "*.o" -exec rm -f {} \; 61 | rm -f ${GNB_CLI} ${GNB_CRYPTO} ${GNB_ES} ${GNB_CTL} 62 | rm -f core 63 | rm -f *.exe 64 | 65 | -------------------------------------------------------------------------------- /Makefile.freebsd: -------------------------------------------------------------------------------- 1 | CFLAGS=-O2 -I./src -I./libs -I./libs/miniupnpc -I./libs/libnatpmp -I./libs/zlib -D NO_GZIP=1 2 | CLI_LDFLAGS=-s -L/usr/lib -pthread 3 | GNB_ES_LDFLAGS=-s -L/usr/lib -pthread 4 | 5 | 6 | ifeq ($(DEBUG),1) 7 | CFLAGS += -g 8 | else 9 | CLI_LDFLAGS += -s 10 | GNB_ES_LDFLAGS += -s 11 | endif 12 | 13 | 14 | GNB_CRYPTO=gnb_crypto 15 | GNB_CTL=gnb_ctl 16 | GNB_ES=gnb_es 17 | GNB_CLI=gnb 18 | 19 | 20 | include Makefile.inc 21 | 22 | 23 | GNB_CLI_OBJS = \ 24 | ./src/cli/gnb.o \ 25 | ./src/gnb_argv.o \ 26 | ./src/unix/unix_platform.o \ 27 | ./src/freebsd/gnb_drv_freebsd.o 28 | 29 | GNB_ES_OBJS += ./src/unix/unix_platform.o 30 | 31 | all:${GNB_CLI} ${GNB_CRYPTO} ${GNB_ES} ${GNB_CTL} 32 | 33 | 34 | $(GNB_CTL): $(GNB_CTL_OBJS) 35 | ${CC} -o ${GNB_CTL} ${GNB_CTL_OBJS} ${CLI_LDFLAGS} 36 | 37 | 38 | $(GNB_ES): $(GNB_ES_OBJS) ${CRYPTO_OBJS} ${MINIUPNP_OBJS} ${LIBNATPMP_OBJS} 39 | ${CC} -o ${GNB_ES} ${GNB_ES_OBJS} ${CRYPTO_OBJS} ${MINIUPNP_OBJS} ${LIBNATPMP_OBJS} ${GNB_ES_LDFLAGS} 40 | 41 | 42 | $(GNB_CRYPTO): $(CRYPTO_OBJS) ./src/cli/gnb_crypto.o 43 | ${CC} -o ${GNB_CRYPTO} ./src/cli/gnb_crypto.o ${CRYPTO_OBJS} ${CLI_LDFLAGS} 44 | 45 | 46 | $(GNB_CLI): $(GNB_OBJS) $(GNB_CLI_OBJS) $(GNB_PF_OBJS) ${CRYPTO_OBJS} ${ZLIB_OBJS} 47 | ${CC} -o ${GNB_CLI} ${GNB_OBJS} ${GNB_CLI_OBJS} ${GNB_PF_OBJS} ${CRYPTO_OBJS} ${ZLIB_OBJS} ${CLI_LDFLAGS} 48 | 49 | 50 | %.o:%.c 51 | ${CC} ${CFLAGS} -c -o $@ $< 52 | 53 | 54 | install:${GNB_CLI} ${GNB_CRYPTO} ${GNB_ES} ${GNB_CTL} 55 | mkdir -p ./bin/ 56 | cp ${GNB_CLI} ./bin/ 57 | cp ${GNB_CTL} ./bin/ 58 | cp ${GNB_CRYPTO} ./bin/ 59 | cp ${GNB_ES} ./bin/ 60 | 61 | 62 | clean: 63 | find . -name "*.o" -exec rm -f {} \; 64 | rm -f ${GNB_CLI} ${GNB_CRYPTO} ${GNB_ES} ${GNB_CTL} 65 | rm -f core 66 | rm -f *.exe 67 | 68 | -------------------------------------------------------------------------------- /Makefile.linux: -------------------------------------------------------------------------------- 1 | CFLAGS=-ffunction-sections -fdata-sections -Wno-unused-result -I./src -I./libs -I./libs/miniupnpc -I./libs/libnatpmp -I./libs/zlib -D NO_GZIP=1 -D GNB_LINUX_BUILD=1 2 | CLI_LDFLAGS=-Wl,--gc-sections -L/usr/lib -pthread 3 | GNB_ES_LDFLAGS=-Wl,--gc-sections -L/usr/lib -pthread 4 | 5 | 6 | ifeq ($(DEBUG),1) 7 | CFLAGS += -g 8 | else 9 | CFLAGS += -O2 10 | CLI_LDFLAGS += -s 11 | GNB_ES_LDFLAGS += -s 12 | endif 13 | 14 | 15 | GNB_CRYPTO=gnb_crypto 16 | GNB_CTL=gnb_ctl 17 | GNB_ES=gnb_es 18 | GNB_CLI=gnb 19 | 20 | 21 | include Makefile.inc 22 | 23 | 24 | GNB_CLI_OBJS = \ 25 | ./src/cli/gnb.o \ 26 | ./src/gnb_argv.o \ 27 | ./src/unix/unix_platform.o \ 28 | ./src/linux/gnb_drv_linux.o 29 | 30 | GNB_ES_OBJS += ./src/unix/unix_platform.o 31 | 32 | all:${GNB_CLI} ${GNB_CRYPTO} ${GNB_ES} ${GNB_CTL} 33 | 34 | 35 | $(GNB_CTL): $(GNB_CTL_OBJS) 36 | ${CC} -o ${GNB_CTL} ${GNB_CTL_OBJS} ${CLI_LDFLAGS} 37 | 38 | 39 | $(GNB_ES): $(GNB_ES_OBJS) ${CRYPTO_OBJS} ${MINIUPNP_OBJS} ${LIBNATPMP_OBJS} 40 | ${CC} -o ${GNB_ES} ${GNB_ES_OBJS} ${CRYPTO_OBJS} ${MINIUPNP_OBJS} ${LIBNATPMP_OBJS} ${GNB_ES_LDFLAGS} 41 | 42 | 43 | $(GNB_CRYPTO): $(CRYPTO_OBJS) ./src/cli/gnb_crypto.o 44 | ${CC} -o ${GNB_CRYPTO} ./src/cli/gnb_crypto.o ${CRYPTO_OBJS} ${CLI_LDFLAGS} 45 | 46 | 47 | $(GNB_CLI): $(GNB_OBJS) $(GNB_CLI_OBJS) $(GNB_PF_OBJS) ${CRYPTO_OBJS} ${ZLIB_OBJS} 48 | ${CC} -o ${GNB_CLI} ${GNB_OBJS} ${GNB_CLI_OBJS} ${GNB_PF_OBJS} ${CRYPTO_OBJS} ${ZLIB_OBJS} ${CLI_LDFLAGS} 49 | 50 | 51 | %.o:%.c 52 | ${CC} ${CFLAGS} -c -o $@ $< 53 | 54 | 55 | install:${GNB_CLI} ${GNB_CRYPTO} ${GNB_ES} ${GNB_CTL} 56 | mkdir -p ./bin/ 57 | cp ${GNB_CLI} ./bin/ 58 | cp ${GNB_CTL} ./bin/ 59 | cp ${GNB_CRYPTO} ./bin/ 60 | cp ${GNB_ES} ./bin/ 61 | 62 | 63 | clean: 64 | find . -name "*.o" -exec rm -f {} \; 65 | rm -f ${GNB_CLI} ${GNB_CRYPTO} ${GNB_ES} ${GNB_CTL} 66 | rm -f core core.* 67 | rm -f *.exe 68 | -------------------------------------------------------------------------------- /Makefile.mingw_x86_64: -------------------------------------------------------------------------------- 1 | CFLAGS=-O2 -I./src -I/usr/x86_64-w64-mingw32/include -I./libs -I./libs/wintun -I./libs/miniupnpc -I./libs/libnatpmp -I./libs/zlib -D NO_GZIP=1 -DMINIUPNP_STATICLIB -DNATPMP_STATICLIB 2 | 3 | CLI_LDFLAGS=-s -static -L/usr/x86_64-w64-mingw32/lib/ -lwsock32 -lws2_32 -liphlpapi -lpthread -lmingwex 4 | GNB_ES_LDFLAGS=-s -static -L/usr/x86_64-w64-mingw32/lib/ -lwsock32 -lws2_32 -liphlpapi -lpthread -lmingwex 5 | 6 | 7 | GNB_CRYPTO=gnb_crypto.exe 8 | GNB_CTL=gnb_ctl.exe 9 | GNB_ES=gnb_es.exe 10 | GNB_CLI=gnb.exe 11 | 12 | 13 | include Makefile.inc 14 | 15 | 16 | GNB_CLI_OBJS = \ 17 | ./src/cli/gnb.o \ 18 | ./src/gnb_argv.o \ 19 | ./src/mingw/gnb_res.o \ 20 | ./src/mingw/windows_platform.o \ 21 | ./src/mingw/gnb_drv_win32.o \ 22 | ./src/mingw/gnb_drv_wintun.o 23 | 24 | GNB_ES_OBJS += ./src/mingw/windows_platform.o 25 | 26 | all:${GNB_CLI} ${GNB_CRYPTO} ${GNB_ES} ${GNB_CTL} 27 | 28 | 29 | ${GNB_CTL}: ${GNB_CTL_OBJS} ./src/mingw/gnb_res.o 30 | ${CC} -o ${GNB_CTL} ${GNB_CTL_OBJS} ./src/mingw/gnb_res.o ${CLI_LDFLAGS} 31 | 32 | 33 | ${GNB_ES}: ${GNB_ES_OBJS} ${CRYPTO_OBJS} ${MINIUPNP_OBJS} ${LIBNATPMP_OBJS} 34 | ${CC} -o ${GNB_ES} ${GNB_ES_OBJS} ${CRYPTO_OBJS} ${MINIUPNP_OBJS} ${LIBNATPMP_OBJS} ./src/mingw/gnb_res.o ${GNB_ES_LDFLAGS} 35 | 36 | 37 | $(GNB_CRYPTO): $(CRYPTO_OBJS) ./src/cli/gnb_crypto.o 38 | ${CC} -o ${GNB_CRYPTO} ./src/cli/gnb_crypto.o ${CRYPTO_OBJS} ${CLI_LDFLAGS} 39 | 40 | 41 | ${GNB_CLI}: ${GNB_OBJS} ${GNB_CLI_OBJS} ${GNB_PF_OBJS} ${CRYPTO_OBJS} ${ZLIB_OBJS} 42 | ${CC} -o ${GNB_CLI} ${GNB_OBJS} ${GNB_CLI_OBJS} ${GNB_PF_OBJS} ${CRYPTO_OBJS} ${ZLIB_OBJS} ${CLI_LDFLAGS} 43 | 44 | 45 | %.o:%.c 46 | ${CC} ${CFLAGS} -c -o $@ $< 47 | 48 | ./src/mingw/gnb_res.o: ./src/mingw/gnb_res.rc 49 | ${WINDRES} ./src/mingw/gnb_res.rc -o ./src/mingw/gnb_res.o 50 | 51 | 52 | install:${GNB_CLI} ${GNB_CRYPTO} ${GNB_ES} ${GNB_CTL} 53 | mkdir -p ./bin/ 54 | cp ${GNB_CLI} ./bin/ 55 | cp ${GNB_CTL} ./bin/ 56 | cp ${GNB_CRYPTO} ./bin/ 57 | cp ${GNB_ES} ./bin/ 58 | 59 | clean: 60 | find . -name "*.o" -exec rm -f {} \; 61 | rm -f ${GNB_CLI} ${GNB_CRYPTO} ${GNB_ES} ${GNB_CTL} 62 | rm -f core 63 | rm -f *.exe 64 | 65 | -------------------------------------------------------------------------------- /Makefile.openbsd: -------------------------------------------------------------------------------- 1 | CFLAGS=-O2 -I./src -I./libs -I./libs/miniupnpc -I./libs/libnatpmp -I./libs/zlib -D NO_GZIP=1 2 | CLI_LDFLAGS=-s -L/usr/lib -pthread 3 | GNB_ES_LDFLAGS=-s -L/usr/lib -pthread 4 | 5 | GNB_CRYPTO=gnb_crypto 6 | GNB_CTL=gnb_ctl 7 | GNB_ES=gnb_es 8 | GNB_CLI=gnb 9 | 10 | 11 | include Makefile.inc 12 | 13 | 14 | GNB_CLI_OBJS = \ 15 | ./src/cli/gnb.o \ 16 | ./src/gnb_argv.o \ 17 | ./src/unix/unix_platform.o \ 18 | ./src/openbsd/gnb_drv_openbsd.o 19 | 20 | GNB_ES_OBJS += ./src/unix/unix_platform.o 21 | 22 | all:${GNB_CLI} ${GNB_CRYPTO} ${GNB_ES} ${GNB_CTL} 23 | 24 | 25 | $(GNB_CTL): $(GNB_CTL_OBJS) 26 | ${CC} -o ${GNB_CTL} ${GNB_CTL_OBJS} ${CLI_LDFLAGS} 27 | 28 | 29 | $(GNB_ES): $(GNB_ES_OBJS) ${CRYPTO_OBJS} ${MINIUPNP_OBJS} ${LIBNATPMP_OBJS} 30 | ${CC} -o ${GNB_ES} ${GNB_ES_OBJS} ${CRYPTO_OBJS} ${MINIUPNP_OBJS} ${LIBNATPMP_OBJS} ${GNB_ES_LDFLAGS} 31 | 32 | 33 | $(GNB_CRYPTO): $(CRYPTO_OBJS) ./src/cli/gnb_crypto.o 34 | ${CC} -o ${GNB_CRYPTO} ./src/cli/gnb_crypto.o ${CRYPTO_OBJS} ${CLI_LDFLAGS} 35 | 36 | 37 | $(GNB_CLI): $(GNB_OBJS) $(GNB_CLI_OBJS) $(GNB_PF_OBJS) ${CRYPTO_OBJS} ${ZLIB_OBJS} 38 | ${CC} -o ${GNB_CLI} ${GNB_OBJS} ${GNB_CLI_OBJS} ${GNB_PF_OBJS} ${CRYPTO_OBJS} ${ZLIB_OBJS} ${CLI_LDFLAGS} 39 | 40 | 41 | %.o:%.c 42 | ${CC} ${CFLAGS} -c -o $@ $< 43 | 44 | 45 | install:${GNB_CLI} ${GNB_CRYPTO} ${GNB_ES} ${GNB_CTL} 46 | mkdir -p ./bin/ 47 | cp ${GNB_CLI} ./bin/ 48 | cp ${GNB_CTL} ./bin/ 49 | cp ${GNB_CRYPTO} ./bin/ 50 | cp ${GNB_ES} ./bin/ 51 | 52 | 53 | clean: 54 | find . -name "*.o" -exec rm -f {} \; 55 | rm -f ${GNB_CLI} ${GNB_CRYPTO} ${GNB_ES} ${GNB_CTL} 56 | rm -f core 57 | rm -f *.exe 58 | 59 | -------------------------------------------------------------------------------- /Makefile.openwrt: -------------------------------------------------------------------------------- 1 | CFLAGS=-ffunction-sections -fdata-sections -Wno-unused-result -I./src -I./libs -I./libs/miniupnpc -I./libs/libnatpmp -I./libs/zlib -D NO_GZIP=1 -D GNB_OPENWRT_BUILD=1 2 | CLI_LDFLAGS=-Wl,--gc-sections -L/usr/lib -pthread 3 | GNB_ES_LDFLAGS=-Wl,--gc-sections -L/usr/lib -pthread 4 | 5 | 6 | ifeq ($(DEBUG),1) 7 | CFLAGS += -g 8 | else 9 | CLI_LDFLAGS += -s 10 | GNB_ES_LDFLAGS += -s 11 | endif 12 | 13 | 14 | GNB_CRYPTO=gnb_crypto 15 | GNB_CTL=gnb_ctl 16 | GNB_ES=gnb_es 17 | GNB_CLI=gnb 18 | 19 | 20 | include Makefile.inc 21 | 22 | 23 | GNB_CLI_OBJS = \ 24 | ./src/cli/gnb.o \ 25 | ./src/gnb_argv.o \ 26 | ./src/unix/unix_platform.o \ 27 | ./src/linux/gnb_drv_linux.o 28 | 29 | GNB_ES_OBJS += ./src/unix/unix_platform.o 30 | 31 | all:${GNB_CLI} ${GNB_CRYPTO} ${GNB_ES} ${GNB_CTL} 32 | 33 | 34 | $(GNB_CTL): $(GNB_CTL_OBJS) 35 | ${CC} -o ${GNB_CTL} ${GNB_CTL_OBJS} ${CLI_LDFLAGS} 36 | 37 | 38 | $(GNB_ES): $(GNB_ES_OBJS) ${CRYPTO_OBJS} ${MINIUPNP_OBJS} ${LIBNATPMP_OBJS} 39 | ${CC} -o ${GNB_ES} ${GNB_ES_OBJS} ${CRYPTO_OBJS} ${MINIUPNP_OBJS} ${LIBNATPMP_OBJS} ${GNB_ES_LDFLAGS} 40 | 41 | 42 | $(GNB_CRYPTO): $(CRYPTO_OBJS) ./src/cli/gnb_crypto.o 43 | ${CC} -o ${GNB_CRYPTO} ./src/cli/gnb_crypto.o ${CRYPTO_OBJS} ${CLI_LDFLAGS} 44 | 45 | 46 | $(GNB_CLI): $(GNB_OBJS) $(GNB_CLI_OBJS) $(GNB_PF_OBJS) ${CRYPTO_OBJS} ${ZLIB_OBJS} 47 | ${CC} -o ${GNB_CLI} ${GNB_OBJS} ${GNB_CLI_OBJS} ${GNB_PF_OBJS} ${CRYPTO_OBJS} ${ZLIB_OBJS} ${CLI_LDFLAGS} 48 | 49 | 50 | %.o:%.c 51 | ${CC} ${CFLAGS} -c -o $@ $< 52 | 53 | 54 | install:${GNB_CLI} ${GNB_CRYPTO} ${GNB_ES} ${GNB_CTL} 55 | mkdir -p ./bin/ 56 | cp ${GNB_CLI} ./bin/ 57 | cp ${GNB_CTL} ./bin/ 58 | cp ${GNB_CRYPTO} ./bin/ 59 | cp ${GNB_ES} ./bin/ 60 | 61 | 62 | clean: 63 | find . -name "*.o" -exec rm -f {} \; 64 | rm -f ${GNB_CLI} ${GNB_CRYPTO} ${GNB_ES} ${GNB_CTL} 65 | rm -f core 66 | rm -f *.exe 67 | 68 | -------------------------------------------------------------------------------- /docs/disclaimer_cn.md: -------------------------------------------------------------------------------- 1 | # 免责声明 2 | 3 | [OpenGNB](https://github.com/gnbdev/opengnb "OpenGNB")是一个开源的去中心化的具有极致内网穿透能力的通过 P2P 进行三层网络交换的虚拟组网系统。 4 | 5 | [gnb_udp_over_tcp](https://gitee.com/gnbdev/gnb_udp_over_tcp "gnb_udp_over_tcp")是一个为 GNB 开发的通过 tcp 链路中转 UDP 分组转发的服务,也可以为其他基于 UDP 协议的服务中转数据。 6 | 7 | 用户使用本项目的软件及相关项目源码、脚本、配置文件即表示用户并同意以下免责条款。 8 | 9 | 在一个计算机及计算机所在网络中部署运行 GNB 及衍生项目的软件会涉及该计算机及所在网络的安全,请用户在部署运行 GNB 及衍生项目的软件之前务必确认用户的计算机及所在的计算机网络的管理者是否允许用户这样做,并且要严格遵循用户所在地的法律法规。 10 | 11 | GNB 开发组织不对用户部署运行 GNB 及衍生项目的软件的行为造成的任何后果负责。 12 | -------------------------------------------------------------------------------- /docs/gnb_diagnose_cn.md: -------------------------------------------------------------------------------- 1 | [OpenGNB](https://github.com/gnbdev/opengnb "OpenGNB")是一个开源的去中心化的具有极致内网穿透能力的通过P2P进行三层网络交换的虚拟组网系统。 2 | 3 | 一些运行GNB的节点处于的网络可能非常复杂,虽然GNB尽最大可能让这些节点通过NAT穿透互连,但是总是难以避免会遇到一些节点在NAT时会失败,这在P2P通讯中是不可避免的。 4 | 5 | 造成一些节点无法互连的原因还可能有: 6 | 7 | 节点之间由于没有正确的交换公钥,如果节点采用时钟同步更新密钥但一些节点的时钟没有和其他节点的时钟同步也会导致互连失败。 8 | 9 | 为了诊断当前GNB与其他节点互连的情况,提供了一个叫`gnb_ctl`的工具来查看当前GNB进程内部的状态。 10 | 11 | 在介绍`gnb_ctl`之前,要先介绍一下gnb内部的一个机制,gnb在设计之初就考虑到如何将gnb进程内部大部分的核心的状态暴露出来,这样用户不需要通过观察gnb的日志就能了解到gnb进程的工作状况,例如当前节点与哪些节点是成功建立了连接,也可以根据这些信息判断某些节点没有互连成功的原因。 12 | 13 | 这个机制就是共享内存,gnb内部几乎所有的重要的数据结构所使用的内存都不是从堆上获得,而是通过mmap一块内存与 `gnb_ctl` `gnb_es` 共享,通常情况是gnb进行负责更新这块共享内存,`gnb_ctl` `gnb_es` 多数时候是读取这块内存。 14 | 15 | 如果启动gnb时不通过参数指定,这块mmap内存对应的文件 gnb.map 将在节点配置目录中创建。 16 | 17 | Unix系的mmap系统调用几乎都是一样,除了个别平台特性相关的参数,唯独Windows创建共享内存的API是独有的,因此gnb特地对Unix系和Windows的共享内存做了一个封装,具体代码在 `gnb_mmap.h gnb_mmap.c`,现在这部分代码已经公开。 18 | 19 | 由于启动 gnb 通常是root用户,因此通过`gnb_ctl`打开这块共享内存也需要用root用户。 20 | 21 | 当gnb启动后,执行下面的命令就能看到gnb内部的状态, 22 | 23 | `./gnb_ctl -b ../../conf/1001/gnb.map -c` 24 | 25 | 输出的信息中会列出所有节点的连通状态,如果一个节点的信息中有 26 | `ipv6 REACHABL` 27 | 或在 28 | `ipv4 REACHABL` 29 | 就说明这个节点与当前节点当然成功连通的。 30 | 31 | 用户可以通过这些信息排查节点无法连通的原因,其中包括当前节点与其他节点通讯时使用的密钥,这些信息涉及到通讯的安全不应当透露给无关的他人。 32 | 33 | 当确实遇到无法连通的节点,这样可能就需要通过`forward`节点去为这些节点提供数据中转服务,这样这些节点就从点对点网络转变为中心化网络。 34 | 35 | 如名字的含义,`gnb_ctl`将来还可以做更多的事情。 36 | 37 | 需要了解更多细节可以执行`gnb_ctl -h` 了解。 38 | 39 | -------------------------------------------------------------------------------- /examples/node_config_example/address.conf: -------------------------------------------------------------------------------- 1 | i|0|101.32.178.3|9001 2 | 3 | 4 | -------------------------------------------------------------------------------- /examples/node_config_example/node.conf: -------------------------------------------------------------------------------- 1 | #setup uuid for node 2 | #nodeid $nodeid 3 | 4 | #setup listen udp port 5 | #listen $port 6 | 7 | #use multi socket to increase gnb nat traversal success rate 8 | #multi-socket on 9 | 10 | #pass-through argv "--upnp" to gnb_es try to set upnp 11 | #es-argv --upnp 12 | 13 | #set maximum transmission unit(MTU) for the tun device 14 | #mtu 1450 15 | -------------------------------------------------------------------------------- /examples/node_config_example/route.conf: -------------------------------------------------------------------------------- 1 | 1001|10.1.0.1|255.255.255.0 2 | 1002|10.1.0.2|255.255.255.0 3 | 1003|10.1.0.3|255.255.255.0 4 | 1004|10.1.0.4|255.255.255.0 5 | 1005|10.1.0.5|255.255.255.0 6 | 7 | -------------------------------------------------------------------------------- /examples/node_config_example/scripts/if_down_darwin.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ -n "$GNB_IF_NAME" ] ; then 4 | 5 | echo $GNB_MTU > "/sys/class/net/$GNB_IF_NAME/mtu" 6 | 7 | fi 8 | 9 | 10 | #if [ -n "$GNB_TUN_IPV6" ] ; then 11 | 12 | # ip -6 address add "$GNB_TUN_IPV6/96" dev $GNB_IF_NAME 13 | 14 | #fi 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /examples/node_config_example/scripts/if_down_freebsd.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | #GNB ENV $GNB_IF_NAME $GNB_MTU $GNB_TUN_IPV4 $GNB_TUN_IPV6 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /examples/node_config_example/scripts/if_down_linux.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # if_down_linux.sh 3 | 4 | #GNB ENV $GNB_IF_NAME $GNB_MTU $GNB_TUN_IPV4 $GNB_TUN_IPV6 5 | -------------------------------------------------------------------------------- /examples/node_config_example/scripts/if_down_openbsd.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | #GNB ENV $GNB_IF_NAME $GNB_MTU $GNB_TUN_IPV4 $GNB_TUN_IPV6 4 | 5 | -------------------------------------------------------------------------------- /examples/node_config_example/scripts/if_up_darwin.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | #GNB ENV $GNB_IF_NAME $GNB_MTU $GNB_TUN_IPV4 $GNB_TUN_IPV6 4 | 5 | 6 | -------------------------------------------------------------------------------- /examples/node_config_example/scripts/if_up_freebsd.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | #GNB ENV $GNB_IF_NAME $GNB_MTU $GNB_TUN_IPV4 $GNB_TUN_IPV6 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /examples/node_config_example/scripts/if_up_linux.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # if_up_linux.sh 3 | 4 | #GNB ENV $GNB_IF_NAME $GNB_MTU $GNB_TUN_IPV4 $GNB_TUN_IPV6 5 | -------------------------------------------------------------------------------- /examples/node_config_example/scripts/if_up_openbsd.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | #GNB ENV $GNB_IF_NAME $GNB_MTU $GNB_TUN_IPV4 $GNB_TUN_IPV6 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /libs/README.md: -------------------------------------------------------------------------------- 1 | # OpenGNB 2 | 3 | [OpenGNB](https://github.com/gnbdev/opengnb "OpenGNB")是一个开源的去中心化的具有极致内网穿透能力的通过P2P进行三层网络交换的虚拟组网系统。 4 | 5 | 编译链接OpenGNB的源码时不需要安装第三方库文件和头文件,这使得OpenGNB可以轻易在许多操作系统上构建;但必须要明确说明的是,OpenGNB使用了一些第三方源代码,这个目录下存放的正是OpenGNB项目所使用到所有第三方源代码,这些源代码版权归原作者所有。 6 | 7 | [ed25519](https://github.com/orlp/ed25519 "ed25519") 8 | 9 | [miniupnp](https://github.com/miniupnp/miniupnp "miniupnp") 10 | 11 | [tap-windows6](https://github.com/OpenVPN/tap-windows6 "tap-windows6") 12 | 13 | [nginx](https://github.com/nginx/nginx "nginx") 14 | 15 | [zlib](https://www.zlib.net/ "zlib") 16 | 17 | -------------------------------------------------------------------------------- /libs/ed25519/add_scalar.c: -------------------------------------------------------------------------------- 1 | #include "ed25519.h" 2 | #include "ge.h" 3 | #include "sc.h" 4 | #include "sha512.h" 5 | 6 | 7 | /* see http://crypto.stackexchange.com/a/6215/4697 */ 8 | void ed25519_add_scalar(unsigned char *public_key, unsigned char *private_key, const unsigned char *scalar) { 9 | const unsigned char SC_1[32] = {1}; /* scalar with value 1 */ 10 | 11 | unsigned char n[32]; 12 | ge_p3 nB; 13 | ge_p1p1 A_p1p1; 14 | ge_p3 A; 15 | ge_p3 public_key_unpacked; 16 | ge_cached T; 17 | 18 | sha512_context hash; 19 | unsigned char hashbuf[64]; 20 | 21 | int i; 22 | 23 | /* copy the scalar and clear highest bit */ 24 | for (i = 0; i < 31; ++i) { 25 | n[i] = scalar[i]; 26 | } 27 | n[31] = scalar[31] & 127; 28 | 29 | /* private key: a = n + t */ 30 | if (private_key) { 31 | sc_muladd(private_key, SC_1, n, private_key); 32 | 33 | // https://github.com/orlp/ed25519/issues/3 34 | sha512_init(&hash); 35 | sha512_update(&hash, private_key + 32, 32); 36 | sha512_update(&hash, scalar, 32); 37 | sha512_final(&hash, hashbuf); 38 | for (i = 0; i < 32; ++i) { 39 | private_key[32 + i] = hashbuf[i]; 40 | } 41 | } 42 | 43 | /* public key: A = nB + T */ 44 | if (public_key) { 45 | /* if we know the private key we don't need a point addition, which is faster */ 46 | /* using a "timing attack" you could find out wether or not we know the private 47 | key, but this information seems rather useless - if this is important pass 48 | public_key and private_key seperately in 2 function calls */ 49 | if (private_key) { 50 | ge_scalarmult_base(&A, private_key); 51 | } else { 52 | /* unpack public key into T */ 53 | ge_frombytes_negate_vartime(&public_key_unpacked, public_key); 54 | fe_neg(public_key_unpacked.X, public_key_unpacked.X); /* undo negate */ 55 | fe_neg(public_key_unpacked.T, public_key_unpacked.T); /* undo negate */ 56 | ge_p3_to_cached(&T, &public_key_unpacked); 57 | 58 | /* calculate n*B */ 59 | ge_scalarmult_base(&nB, n); 60 | 61 | /* A = n*B + T */ 62 | ge_add(&A_p1p1, &nB, &T); 63 | ge_p1p1_to_p3(&A, &A_p1p1); 64 | } 65 | 66 | /* pack public key */ 67 | ge_p3_tobytes(public_key, &A); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /libs/ed25519/ed25519.h: -------------------------------------------------------------------------------- 1 | #ifndef ED25519_H 2 | #define ED25519_H 3 | 4 | #include 5 | 6 | #if defined(_WIN32) 7 | #if defined(ED25519_BUILD_DLL) 8 | #define ED25519_DECLSPEC __declspec(dllexport) 9 | #elif defined(ED25519_DLL) 10 | #define ED25519_DECLSPEC __declspec(dllimport) 11 | #else 12 | #define ED25519_DECLSPEC 13 | #endif 14 | #else 15 | #define ED25519_DECLSPEC 16 | #endif 17 | 18 | 19 | #ifdef __cplusplus 20 | extern "C" { 21 | #endif 22 | 23 | #ifndef ED25519_NO_SEED 24 | int ED25519_DECLSPEC ed25519_create_seed(unsigned char *seed); 25 | #endif 26 | 27 | void ED25519_DECLSPEC ed25519_create_keypair(unsigned char *public_key, unsigned char *private_key, const unsigned char *seed); 28 | void ED25519_DECLSPEC ed25519_sign(unsigned char *signature, const unsigned char *message, size_t message_len, const unsigned char *public_key, const unsigned char *private_key); 29 | int ED25519_DECLSPEC ed25519_verify(const unsigned char *signature, const unsigned char *message, size_t message_len, const unsigned char *public_key); 30 | void ED25519_DECLSPEC ed25519_add_scalar(unsigned char *public_key, unsigned char *private_key, const unsigned char *scalar); 31 | void ED25519_DECLSPEC ed25519_key_exchange(unsigned char *shared_secret, const unsigned char *public_key, const unsigned char *private_key); 32 | 33 | 34 | #ifdef __cplusplus 35 | } 36 | #endif 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /libs/ed25519/fe.h: -------------------------------------------------------------------------------- 1 | #ifndef FE_H 2 | #define FE_H 3 | 4 | #include "fixedint.h" 5 | 6 | 7 | /* 8 | fe means field element. 9 | Here the field is \Z/(2^255-19). 10 | An element t, entries t[0]...t[9], represents the integer 11 | t[0]+2^26 t[1]+2^51 t[2]+2^77 t[3]+2^102 t[4]+...+2^230 t[9]. 12 | Bounds on each t[i] vary depending on context. 13 | */ 14 | 15 | 16 | typedef int32_t fe[10]; 17 | 18 | 19 | void fe_0(fe h); 20 | void fe_1(fe h); 21 | 22 | void fe_frombytes(fe h, const unsigned char *s); 23 | void fe_tobytes(unsigned char *s, const fe h); 24 | 25 | void fe_copy(fe h, const fe f); 26 | int fe_isnegative(const fe f); 27 | int fe_isnonzero(const fe f); 28 | void fe_cmov(fe f, const fe g, unsigned int b); 29 | void fe_cswap(fe f, fe g, unsigned int b); 30 | 31 | void fe_neg(fe h, const fe f); 32 | void fe_add(fe h, const fe f, const fe g); 33 | void fe_invert(fe out, const fe z); 34 | void fe_sq(fe h, const fe f); 35 | void fe_sq2(fe h, const fe f); 36 | void fe_mul(fe h, const fe f, const fe g); 37 | void fe_mul121666(fe h, fe f); 38 | void fe_pow22523(fe out, const fe z); 39 | void fe_sub(fe h, const fe f, const fe g); 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /libs/ed25519/fixedint.h: -------------------------------------------------------------------------------- 1 | /* 2 | Portable header to provide the 32 and 64 bits type. 3 | 4 | Not a compatible replacement for , do not blindly use it as such. 5 | */ 6 | 7 | #if ((defined(__STDC__) && __STDC__ && __STDC_VERSION__ >= 199901L) || (defined(__WATCOMC__) && (defined(_STDINT_H_INCLUDED) || __WATCOMC__ >= 1250)) || (defined(__GNUC__) && (defined(_STDINT_H) || defined(_STDINT_H_) || defined(__UINT_FAST64_TYPE__)) )) && !defined(FIXEDINT_H_INCLUDED) 8 | #include 9 | #define FIXEDINT_H_INCLUDED 10 | 11 | #if defined(__WATCOMC__) && __WATCOMC__ >= 1250 && !defined(UINT64_C) 12 | #include 13 | #define UINT64_C(x) (x + (UINT64_MAX - UINT64_MAX)) 14 | #endif 15 | #endif 16 | 17 | 18 | #ifndef FIXEDINT_H_INCLUDED 19 | #define FIXEDINT_H_INCLUDED 20 | 21 | #include 22 | 23 | /* (u)int32_t */ 24 | #ifndef uint32_t 25 | #if (ULONG_MAX == 0xffffffffUL) 26 | typedef unsigned long uint32_t; 27 | #elif (UINT_MAX == 0xffffffffUL) 28 | typedef unsigned int uint32_t; 29 | #elif (USHRT_MAX == 0xffffffffUL) 30 | typedef unsigned short uint32_t; 31 | #endif 32 | #endif 33 | 34 | 35 | #ifndef int32_t 36 | #if (LONG_MAX == 0x7fffffffL) 37 | typedef signed long int32_t; 38 | #elif (INT_MAX == 0x7fffffffL) 39 | typedef signed int int32_t; 40 | #elif (SHRT_MAX == 0x7fffffffL) 41 | typedef signed short int32_t; 42 | #endif 43 | #endif 44 | 45 | 46 | /* (u)int64_t */ 47 | #if (defined(__STDC__) && defined(__STDC_VERSION__) && __STDC__ && __STDC_VERSION__ >= 199901L) 48 | typedef long long int64_t; 49 | typedef unsigned long long uint64_t; 50 | 51 | #define UINT64_C(v) v ##ULL 52 | #define INT64_C(v) v ##LL 53 | #elif defined(__GNUC__) 54 | __extension__ typedef long long int64_t; 55 | __extension__ typedef unsigned long long uint64_t; 56 | 57 | #define UINT64_C(v) v ##ULL 58 | #define INT64_C(v) v ##LL 59 | #elif defined(__MWERKS__) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) || defined(__APPLE_CC__) || defined(_LONG_LONG) || defined(_CRAYC) 60 | typedef long long int64_t; 61 | typedef unsigned long long uint64_t; 62 | 63 | #define UINT64_C(v) v ##ULL 64 | #define INT64_C(v) v ##LL 65 | #elif (defined(__WATCOMC__) && defined(__WATCOM_INT64__)) || (defined(_MSC_VER) && _INTEGRAL_MAX_BITS >= 64) || (defined(__BORLANDC__) && __BORLANDC__ > 0x460) || defined(__alpha) || defined(__DECC) 66 | typedef __int64 int64_t; 67 | typedef unsigned __int64 uint64_t; 68 | 69 | #define UINT64_C(v) v ##UI64 70 | #define INT64_C(v) v ##I64 71 | #endif 72 | #endif 73 | -------------------------------------------------------------------------------- /libs/ed25519/ge.h: -------------------------------------------------------------------------------- 1 | #ifndef GE_H 2 | #define GE_H 3 | 4 | #include "fe.h" 5 | 6 | 7 | /* 8 | ge means group element. 9 | 10 | Here the group is the set of pairs (x,y) of field elements (see fe.h) 11 | satisfying -x^2 + y^2 = 1 + d x^2y^2 12 | where d = -121665/121666. 13 | 14 | Representations: 15 | ge_p2 (projective): (X:Y:Z) satisfying x=X/Z, y=Y/Z 16 | ge_p3 (extended): (X:Y:Z:T) satisfying x=X/Z, y=Y/Z, XY=ZT 17 | ge_p1p1 (completed): ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T 18 | ge_precomp (Duif): (y+x,y-x,2dxy) 19 | */ 20 | 21 | typedef struct { 22 | fe X; 23 | fe Y; 24 | fe Z; 25 | } ge_p2; 26 | 27 | typedef struct { 28 | fe X; 29 | fe Y; 30 | fe Z; 31 | fe T; 32 | } ge_p3; 33 | 34 | typedef struct { 35 | fe X; 36 | fe Y; 37 | fe Z; 38 | fe T; 39 | } ge_p1p1; 40 | 41 | typedef struct { 42 | fe yplusx; 43 | fe yminusx; 44 | fe xy2d; 45 | } ge_precomp; 46 | 47 | typedef struct { 48 | fe YplusX; 49 | fe YminusX; 50 | fe Z; 51 | fe T2d; 52 | } ge_cached; 53 | 54 | void ge_p3_tobytes(unsigned char *s, const ge_p3 *h); 55 | void ge_tobytes(unsigned char *s, const ge_p2 *h); 56 | int ge_frombytes_negate_vartime(ge_p3 *h, const unsigned char *s); 57 | 58 | void ge_add(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q); 59 | void ge_sub(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q); 60 | void ge_double_scalarmult_vartime(ge_p2 *r, const unsigned char *a, const ge_p3 *A, const unsigned char *b); 61 | void ge_madd(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q); 62 | void ge_msub(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q); 63 | void ge_scalarmult_base(ge_p3 *h, const unsigned char *a); 64 | 65 | void ge_p1p1_to_p2(ge_p2 *r, const ge_p1p1 *p); 66 | void ge_p1p1_to_p3(ge_p3 *r, const ge_p1p1 *p); 67 | void ge_p2_0(ge_p2 *h); 68 | void ge_p2_dbl(ge_p1p1 *r, const ge_p2 *p); 69 | void ge_p3_0(ge_p3 *h); 70 | void ge_p3_dbl(ge_p1p1 *r, const ge_p3 *p); 71 | void ge_p3_to_cached(ge_cached *r, const ge_p3 *p); 72 | void ge_p3_to_p2(ge_p2 *r, const ge_p3 *p); 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /libs/ed25519/key_exchange.c: -------------------------------------------------------------------------------- 1 | #include "ed25519.h" 2 | #include "fe.h" 3 | 4 | void ed25519_key_exchange(unsigned char *shared_secret, const unsigned char *public_key, const unsigned char *private_key) { 5 | unsigned char e[32]; 6 | unsigned int i; 7 | 8 | fe x1; 9 | fe x2; 10 | fe z2; 11 | fe x3; 12 | fe z3; 13 | fe tmp0; 14 | fe tmp1; 15 | 16 | int pos; 17 | unsigned int swap; 18 | unsigned int b; 19 | 20 | /* copy the private key and make sure it's valid */ 21 | for (i = 0; i < 32; ++i) { 22 | e[i] = private_key[i]; 23 | } 24 | 25 | e[0] &= 248; 26 | e[31] &= 63; 27 | e[31] |= 64; 28 | 29 | /* unpack the public key and convert edwards to montgomery */ 30 | /* due to CodesInChaos: montgomeryX = (edwardsY + 1)*inverse(1 - edwardsY) mod p */ 31 | fe_frombytes(x1, public_key); 32 | fe_1(tmp1); 33 | fe_add(tmp0, x1, tmp1); 34 | fe_sub(tmp1, tmp1, x1); 35 | fe_invert(tmp1, tmp1); 36 | fe_mul(x1, tmp0, tmp1); 37 | 38 | fe_1(x2); 39 | fe_0(z2); 40 | fe_copy(x3, x1); 41 | fe_1(z3); 42 | 43 | swap = 0; 44 | for (pos = 254; pos >= 0; --pos) { 45 | b = e[pos / 8] >> (pos & 7); 46 | b &= 1; 47 | swap ^= b; 48 | fe_cswap(x2, x3, swap); 49 | fe_cswap(z2, z3, swap); 50 | swap = b; 51 | 52 | /* from montgomery.h */ 53 | fe_sub(tmp0, x3, z3); 54 | fe_sub(tmp1, x2, z2); 55 | fe_add(x2, x2, z2); 56 | fe_add(z2, x3, z3); 57 | fe_mul(z3, tmp0, x2); 58 | fe_mul(z2, z2, tmp1); 59 | fe_sq(tmp0, tmp1); 60 | fe_sq(tmp1, x2); 61 | fe_add(x3, z3, z2); 62 | fe_sub(z2, z3, z2); 63 | fe_mul(x2, tmp1, tmp0); 64 | fe_sub(tmp1, tmp1, tmp0); 65 | fe_sq(z2, z2); 66 | fe_mul121666(z3, tmp1); 67 | fe_sq(x3, x3); 68 | fe_add(tmp0, tmp0, z3); 69 | fe_mul(z3, x1, z2); 70 | fe_mul(z2, tmp1, tmp0); 71 | } 72 | 73 | fe_cswap(x2, x3, swap); 74 | fe_cswap(z2, z3, swap); 75 | 76 | fe_invert(z2, z2); 77 | fe_mul(x2, x2, z2); 78 | fe_tobytes(shared_secret, x2); 79 | } 80 | -------------------------------------------------------------------------------- /libs/ed25519/keypair.c: -------------------------------------------------------------------------------- 1 | #include "ed25519.h" 2 | #include "sha512.h" 3 | #include "ge.h" 4 | 5 | 6 | void ed25519_create_keypair(unsigned char *public_key, unsigned char *private_key, const unsigned char *seed) { 7 | ge_p3 A; 8 | 9 | sha512(seed, 32, private_key); 10 | private_key[0] &= 248; 11 | private_key[31] &= 63; 12 | private_key[31] |= 64; 13 | 14 | ge_scalarmult_base(&A, private_key); 15 | ge_p3_tobytes(public_key, &A); 16 | } 17 | -------------------------------------------------------------------------------- /libs/ed25519/license.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Orson Peters 2 | 3 | This software is provided 'as-is', without any express or implied warranty. In no event will the 4 | authors be held liable for any damages arising from the use of this software. 5 | 6 | Permission is granted to anyone to use this software for any purpose, including commercial 7 | applications, and to alter it and redistribute it freely, subject to the following restrictions: 8 | 9 | 1. The origin of this software must not be misrepresented; you must not claim that you wrote the 10 | original software. If you use this software in a product, an acknowledgment in the product 11 | documentation would be appreciated but is not required. 12 | 13 | 2. Altered source versions must be plainly marked as such, and must not be misrepresented as 14 | being the original software. 15 | 16 | 3. This notice may not be removed or altered from any source distribution. 17 | -------------------------------------------------------------------------------- /libs/ed25519/sc.h: -------------------------------------------------------------------------------- 1 | #ifndef SC_H 2 | #define SC_H 3 | 4 | /* 5 | The set of scalars is \Z/l 6 | where l = 2^252 + 27742317777372353535851937790883648493. 7 | */ 8 | 9 | void sc_reduce(unsigned char *s); 10 | void sc_muladd(unsigned char *s, const unsigned char *a, const unsigned char *b, const unsigned char *c); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /libs/ed25519/seed.c: -------------------------------------------------------------------------------- 1 | #include "ed25519.h" 2 | 3 | #ifndef ED25519_NO_SEED 4 | 5 | #ifdef _WIN32 6 | #include 7 | #include 8 | #else 9 | #include 10 | #endif 11 | 12 | int ed25519_create_seed(unsigned char *seed) { 13 | #ifdef _WIN32 14 | HCRYPTPROV prov; 15 | 16 | if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { 17 | return 1; 18 | } 19 | 20 | if (!CryptGenRandom(prov, 32, seed)) { 21 | CryptReleaseContext(prov, 0); 22 | return 1; 23 | } 24 | 25 | CryptReleaseContext(prov, 0); 26 | #else 27 | FILE *f = fopen("/dev/urandom", "rb"); 28 | 29 | if (f == NULL) { 30 | return 1; 31 | } 32 | 33 | fread(seed, 1, 32, f); 34 | fclose(f); 35 | #endif 36 | 37 | return 0; 38 | } 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /libs/ed25519/sha512.h: -------------------------------------------------------------------------------- 1 | #ifndef SHA512_H 2 | #define SHA512_H 3 | 4 | #include 5 | 6 | #include "fixedint.h" 7 | 8 | /* state */ 9 | typedef struct sha512_context_ { 10 | uint64_t length, state[8]; 11 | size_t curlen; 12 | unsigned char buf[128]; 13 | } sha512_context; 14 | 15 | 16 | int sha512_init(sha512_context * md); 17 | int sha512_final(sha512_context * md, unsigned char *out); 18 | int sha512_update(sha512_context * md, const unsigned char *in, size_t inlen); 19 | int sha512(const unsigned char *message, size_t message_len, unsigned char *out); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /libs/ed25519/sign.c: -------------------------------------------------------------------------------- 1 | #include "ed25519.h" 2 | #include "sha512.h" 3 | #include "ge.h" 4 | #include "sc.h" 5 | 6 | 7 | void ed25519_sign(unsigned char *signature, const unsigned char *message, size_t message_len, const unsigned char *public_key, const unsigned char *private_key) { 8 | sha512_context hash; 9 | unsigned char hram[64]; 10 | unsigned char r[64]; 11 | ge_p3 R; 12 | 13 | 14 | sha512_init(&hash); 15 | sha512_update(&hash, private_key + 32, 32); 16 | sha512_update(&hash, message, message_len); 17 | sha512_final(&hash, r); 18 | 19 | sc_reduce(r); 20 | ge_scalarmult_base(&R, r); 21 | ge_p3_tobytes(signature, &R); 22 | 23 | sha512_init(&hash); 24 | sha512_update(&hash, signature, 32); 25 | sha512_update(&hash, public_key, 32); 26 | sha512_update(&hash, message, message_len); 27 | sha512_final(&hash, hram); 28 | 29 | sc_reduce(hram); 30 | sc_muladd(signature + 32, hram, private_key, r); 31 | } 32 | -------------------------------------------------------------------------------- /libs/ed25519/verify.c: -------------------------------------------------------------------------------- 1 | #include "ed25519.h" 2 | #include "sha512.h" 3 | #include "ge.h" 4 | #include "sc.h" 5 | 6 | static int consttime_equal(const unsigned char *x, const unsigned char *y) { 7 | unsigned char r = 0; 8 | 9 | r = x[0] ^ y[0]; 10 | #define F(i) r |= x[i] ^ y[i] 11 | F(1); 12 | F(2); 13 | F(3); 14 | F(4); 15 | F(5); 16 | F(6); 17 | F(7); 18 | F(8); 19 | F(9); 20 | F(10); 21 | F(11); 22 | F(12); 23 | F(13); 24 | F(14); 25 | F(15); 26 | F(16); 27 | F(17); 28 | F(18); 29 | F(19); 30 | F(20); 31 | F(21); 32 | F(22); 33 | F(23); 34 | F(24); 35 | F(25); 36 | F(26); 37 | F(27); 38 | F(28); 39 | F(29); 40 | F(30); 41 | F(31); 42 | #undef F 43 | 44 | return !r; 45 | } 46 | 47 | int ed25519_verify(const unsigned char *signature, const unsigned char *message, size_t message_len, const unsigned char *public_key) { 48 | unsigned char h[64]; 49 | unsigned char checker[32]; 50 | sha512_context hash; 51 | ge_p3 A; 52 | ge_p2 R; 53 | 54 | if (signature[63] & 224) { 55 | return 0; 56 | } 57 | 58 | if (ge_frombytes_negate_vartime(&A, public_key) != 0) { 59 | return 0; 60 | } 61 | 62 | sha512_init(&hash); 63 | sha512_update(&hash, signature, 32); 64 | sha512_update(&hash, public_key, 32); 65 | sha512_update(&hash, message, message_len); 66 | sha512_final(&hash, h); 67 | 68 | sc_reduce(h); 69 | ge_double_scalarmult_vartime(&R, h, &A, signature + 32); 70 | ge_tobytes(checker, &R); 71 | 72 | if (!consttime_equal(checker, signature)) { 73 | return 0; 74 | } 75 | 76 | return 1; 77 | } 78 | -------------------------------------------------------------------------------- /libs/hash/murmurhash.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | //from nginx source code 5 | uint32_t murmurhash_hash(unsigned char *data, size_t len){ 6 | 7 | uint32_t h, k; 8 | 9 | h = 0 ^ (uint32_t)len; 10 | 11 | while (len >= 4) { 12 | k = data[0]; 13 | k |= data[1] << 8; 14 | k |= data[2] << 16; 15 | k |= data[3] << 24; 16 | 17 | k *= 0x5bd1e995; 18 | k ^= k >> 24; 19 | k *= 0x5bd1e995; 20 | 21 | h *= 0x5bd1e995; 22 | h ^= k; 23 | 24 | data += 4; 25 | len -= 4; 26 | } 27 | 28 | switch (len) { 29 | case 3: 30 | h ^= data[2] << 16; 31 | /* fall through */ 32 | case 2: 33 | h ^= data[1] << 8; 34 | /* fall through */ 35 | case 1: 36 | h ^= data[0]; 37 | h *= 0x5bd1e995; 38 | } 39 | 40 | h ^= h >> 13; 41 | h *= 0x5bd1e995; 42 | h ^= h >> 15; 43 | 44 | return h; 45 | } 46 | -------------------------------------------------------------------------------- /libs/libnatpmp/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2007-2011, Thomas BERNARD 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, 8 | this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright notice, 10 | this list of conditions and the following disclaimer in the documentation 11 | and/or other materials provided with the distribution. 12 | * The name of the author may not be used to endorse or promote products 13 | derived from this software without specific prior written permission. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | POSSIBILITY OF SUCH DAMAGE. 26 | 27 | -------------------------------------------------------------------------------- /libs/libnatpmp/declspec.h: -------------------------------------------------------------------------------- 1 | #ifndef DECLSPEC_H_INCLUDED 2 | #define DECLSPEC_H_INCLUDED 3 | 4 | #if defined(WIN32) && !defined(NATPMP_STATICLIB) 5 | /* for windows dll */ 6 | #ifdef NATPMP_EXPORTS 7 | #define LIBSPEC __declspec(dllexport) 8 | #else 9 | #define LIBSPEC __declspec(dllimport) 10 | #endif 11 | #else 12 | #if defined(__GNUC__) && __GNUC__ >= 4 13 | /* fix dynlib for OS X 10.9.2 and Apple LLVM version 5.0 */ 14 | #define LIBSPEC __attribute__ ((visibility ("default"))) 15 | #else 16 | #define LIBSPEC 17 | #endif 18 | #endif 19 | 20 | #endif 21 | 22 | -------------------------------------------------------------------------------- /libs/libnatpmp/getgateway.h: -------------------------------------------------------------------------------- 1 | /* $Id: getgateway.h,v 1.8 2014/04/22 09:15:40 nanard Exp $ */ 2 | /* libnatpmp 3 | Copyright (c) 2007-2014, Thomas BERNARD 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, 10 | this list of conditions and the following disclaimer. 11 | * Redistributions in binary form must reproduce the above copyright notice, 12 | this list of conditions and the following disclaimer in the documentation 13 | and/or other materials provided with the distribution. 14 | * The name of the author may not be used to endorse or promote products 15 | derived from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | #ifndef __GETGATEWAY_H__ 30 | #define __GETGATEWAY_H__ 31 | 32 | #ifdef WIN32 33 | #if !defined(_MSC_VER) || _MSC_VER >= 1600 34 | #include 35 | #else 36 | typedef unsigned long uint32_t; 37 | typedef unsigned short uint16_t; 38 | #endif 39 | #define in_addr_t uint32_t 40 | #endif 41 | /* #include "declspec.h" */ 42 | 43 | /* getdefaultgateway() : 44 | * return value : 45 | * 0 : success 46 | * -1 : failure */ 47 | /* LIBSPEC */int getdefaultgateway(in_addr_t * addr); 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /libs/libnatpmp/wingettimeofday.c: -------------------------------------------------------------------------------- 1 | /* $Id: wingettimeofday.c,v 1.6 2013/09/10 20:13:26 nanard Exp $ */ 2 | /* libnatpmp 3 | Copyright (c) 2007-2013, Thomas BERNARD 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, 10 | this list of conditions and the following disclaimer. 11 | * Redistributions in binary form must reproduce the above copyright notice, 12 | this list of conditions and the following disclaimer in the documentation 13 | and/or other materials provided with the distribution. 14 | * The name of the author may not be used to endorse or promote products 15 | derived from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | #ifdef WIN32 30 | #if defined(_MSC_VER) 31 | struct timeval { 32 | long tv_sec; 33 | long tv_usec; 34 | }; 35 | #else 36 | #include 37 | #endif 38 | 39 | typedef struct _FILETIME { 40 | unsigned long dwLowDateTime; 41 | unsigned long dwHighDateTime; 42 | } FILETIME; 43 | 44 | void __stdcall GetSystemTimeAsFileTime(FILETIME*); 45 | 46 | int natpmp_gettimeofday(struct timeval* p, void* tz /* IGNORED */) { 47 | union { 48 | long long ns100; /*time since 1 Jan 1601 in 100ns units */ 49 | FILETIME ft; 50 | } _now; 51 | 52 | if(!p) 53 | return -1; 54 | GetSystemTimeAsFileTime( &(_now.ft) ); 55 | p->tv_usec =(long)((_now.ns100 / 10LL) % 1000000LL ); 56 | p->tv_sec = (long)((_now.ns100-(116444736000000000LL))/10000000LL); 57 | return 0; 58 | } 59 | #endif 60 | 61 | -------------------------------------------------------------------------------- /libs/libnatpmp/wingettimeofday.h: -------------------------------------------------------------------------------- 1 | /* $Id: wingettimeofday.h,v 1.5 2013/09/11 07:22:25 nanard Exp $ */ 2 | /* libnatpmp 3 | Copyright (c) 2007-2013, Thomas BERNARD 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, 10 | this list of conditions and the following disclaimer. 11 | * Redistributions in binary form must reproduce the above copyright notice, 12 | this list of conditions and the following disclaimer in the documentation 13 | and/or other materials provided with the distribution. 14 | * The name of the author may not be used to endorse or promote products 15 | derived from this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | #ifndef __WINGETTIMEOFDAY_H__ 30 | #define __WINGETTIMEOFDAY_H__ 31 | #ifdef WIN32 32 | #if defined(_MSC_VER) 33 | #include 34 | #else 35 | #include 36 | #endif 37 | int natpmp_gettimeofday(struct timeval* p, void* tz /* IGNORED */); 38 | #endif 39 | #endif 40 | -------------------------------------------------------------------------------- /libs/miniupnpc/LICENSE: -------------------------------------------------------------------------------- 1 | MiniUPnPc 2 | Copyright (c) 2005-2019, Thomas BERNARD 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, 9 | this list of conditions and the following disclaimer. 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | * The name of the author may not be used to endorse or promote products 14 | derived from this software without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 20 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 | POSSIBILITY OF SUCH DAMAGE. 27 | 28 | -------------------------------------------------------------------------------- /libs/miniupnpc/README: -------------------------------------------------------------------------------- 1 | Project: miniupnp 2 | Project web page: http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/ 3 | github: https://github.com/miniupnp/miniupnp 4 | Author: Thomas Bernard 5 | Copyright (c) 2005-2019 Thomas Bernard 6 | This software is subject to the conditions detailed in the 7 | LICENSE file provided within this distribution. 8 | 9 | 10 | * miniUPnP Client - miniUPnPc * 11 | 12 | To compile, simply run 'gmake' (could be 'make' on your system). 13 | Under win32, to compile with MinGW, type "mingw32make.bat". 14 | MS Visual C solution and project files are supplied in the msvc/ subdirectory. 15 | The miniupnpc library is available as a static library or as a DLL : 16 | define MINIUPNP_STATICLIB if you want to link against the static library. 17 | 18 | The compilation is known to work under linux, FreeBSD, 19 | OpenBSD, MacOS X, AmigaOS and cygwin. 20 | The official AmigaOS4.1 SDK was used for AmigaOS4 and GeekGadgets for AmigaOS3. 21 | upx (http://upx.sourceforge.net) is used to compress the win32 .exe files. 22 | 23 | To install the library and headers on the system use : 24 | > su 25 | > make install 26 | > exit 27 | 28 | alternatively, to install into a specific location, use : 29 | > INSTALLPREFIX=/usr/local make install 30 | 31 | upnpc.c is a sample client using the libminiupnpc. 32 | To use the libminiupnpc in your application, link it with 33 | libminiupnpc.a (or .so) and use the following functions found in miniupnpc.h, 34 | upnpcommands.h and miniwget.h : 35 | - upnpDiscover() 36 | - UPNP_GetValidIGD() 37 | - miniwget() 38 | - parserootdesc() 39 | - GetUPNPUrls() 40 | - UPNP_* (calling UPNP methods) 41 | 42 | Note : use #include etc... for the includes 43 | and -lminiupnpc for the link 44 | 45 | Discovery process is speeded up when MiniSSDPd is running on the machine. 46 | 47 | 48 | * Python module * 49 | 50 | you can build a python module with 'make pythonmodule' 51 | and install it with 'make installpythonmodule'. 52 | setup.py (and setupmingw32.py) are included in the distribution. 53 | 54 | 55 | Feel free to contact me if you have any problem : 56 | e-mail : miniupnp@free.fr 57 | 58 | If you are using libminiupnpc in your application, please 59 | send me an email ! 60 | 61 | For any question, you can use the web forum : 62 | https://miniupnp.tuxfamily.org/forum/ 63 | 64 | Bugs should be reported on github : 65 | https://github.com/miniupnp/miniupnp/issues 66 | 67 | * Linux firewall configuration for UPnP clients * 68 | 69 | Due to how UPnP protocol is designed, unicast responses to UPnP multicast client 70 | requests are not tracked by Linux netfilter. And therefore netfilter executes 71 | default action for them (which is in most cases DROP response packet). 72 | 73 | To workaround this limitation, custom ipset hash table can be used. It is 74 | supported since Linux kernel >= 2.6.39. 75 | 76 | Rules for IPv4: 77 | $ ipset create upnp hash:ip,port timeout 3 78 | $ iptables -A OUTPUT -d 239.255.255.250/32 -p udp -m udp --dport 1900 -j SET --add-set upnp src,src --exist 79 | $ iptables -A INPUT -p udp -m set --match-set upnp dst,dst -j ACCEPT 80 | $ iptables -A INPUT -d 239.255.255.250/32 -p udp -m udp --dport 1900 -j ACCEPT 81 | 82 | Rules for IPv6: 83 | $ ipset create upnp6 hash:ip,port timeout 3 family inet6 84 | $ ip6tables -A OUTPUT -d ff02::c/128 -p udp -m udp --dport 1900 -j SET --add-set upnp6 src,src --exist 85 | $ ip6tables -A OUTPUT -d ff05::c/128 -p udp -m udp --dport 1900 -j SET --add-set upnp6 src,src --exist 86 | $ ip6tables -A INPUT -p udp -m set --match-set upnp6 dst,dst -j ACCEPT 87 | $ ip6tables -A INPUT -d ff02::c/128 -p udp -m udp --dport 1900 -j ACCEPT 88 | $ ip6tables -A INPUT -d ff05::c/128 -p udp -m udp --dport 1900 -j ACCEPT 89 | 90 | Detailed description is available on: 91 | https://serverfault.com/a/911286 92 | https://unix.stackexchange.com/a/444804 93 | -------------------------------------------------------------------------------- /libs/miniupnpc/VERSION: -------------------------------------------------------------------------------- 1 | 2.1 2 | -------------------------------------------------------------------------------- /libs/miniupnpc/codelength.h: -------------------------------------------------------------------------------- 1 | /* $Id: codelength.h,v 1.5 2015/07/09 12:40:18 nanard Exp $ */ 2 | /* Project : miniupnp 3 | * Author : Thomas BERNARD 4 | * copyright (c) 2005-2015 Thomas Bernard 5 | * This software is subjet to the conditions detailed in the 6 | * provided LICENCE file. */ 7 | #ifndef CODELENGTH_H_INCLUDED 8 | #define CODELENGTH_H_INCLUDED 9 | 10 | /* Encode length by using 7bit per Byte : 11 | * Most significant bit of each byte specifies that the 12 | * following byte is part of the code */ 13 | 14 | /* n : unsigned 15 | * p : unsigned char * 16 | */ 17 | #define DECODELENGTH(n, p) n = 0; \ 18 | do { n = (n << 7) | (*p & 0x7f); } \ 19 | while((*(p++)&0x80) && (n<(1<<25))); 20 | 21 | /* n : unsigned 22 | * READ : function/macro to read one byte (unsigned char) 23 | */ 24 | #define DECODELENGTH_READ(n, READ) \ 25 | n = 0; \ 26 | do { \ 27 | unsigned char c; \ 28 | READ(c); \ 29 | n = (n << 7) | (c & 0x07f); \ 30 | if(!(c&0x80)) break; \ 31 | } while(n<(1<<25)); 32 | 33 | /* n : unsigned 34 | * p : unsigned char * 35 | * p_limit : unsigned char * 36 | */ 37 | #define DECODELENGTH_CHECKLIMIT(n, p, p_limit) \ 38 | n = 0; \ 39 | do { \ 40 | if((p) >= (p_limit)) break; \ 41 | n = (n << 7) | (*(p) & 0x7f); \ 42 | } while((*((p)++)&0x80) && (n<(1<<25))); 43 | 44 | 45 | /* n : unsigned 46 | * p : unsigned char * 47 | */ 48 | #define CODELENGTH(n, p) if(n>=268435456) *(p++) = (n >> 28) | 0x80; \ 49 | if(n>=2097152) *(p++) = (n >> 21) | 0x80; \ 50 | if(n>=16384) *(p++) = (n >> 14) | 0x80; \ 51 | if(n>=128) *(p++) = (n >> 7) | 0x80; \ 52 | *(p++) = n & 0x7f; 53 | 54 | #endif /* CODELENGTH_H_INCLUDED */ 55 | -------------------------------------------------------------------------------- /libs/miniupnpc/connecthostport.h: -------------------------------------------------------------------------------- 1 | /* $Id: connecthostport.h,v 1.4 2018/04/06 10:53:13 nanard Exp $ */ 2 | /* Project: miniupnp 3 | * http://miniupnp.free.fr/ 4 | * Author: Thomas Bernard 5 | * Copyright (c) 2010-2018 Thomas Bernard 6 | * This software is subjects to the conditions detailed 7 | * in the LICENCE file provided within this distribution */ 8 | #ifndef CONNECTHOSTPORT_H_INCLUDED 9 | #define CONNECTHOSTPORT_H_INCLUDED 10 | 11 | #include "miniupnpc_socketdef.h" 12 | 13 | /* connecthostport() 14 | * return a socket connected (TCP) to the host and port 15 | * or INVALID_SOCKET in case of error */ 16 | SOCKET connecthostport(const char * host, unsigned short port, 17 | unsigned int scope_id); 18 | 19 | #endif 20 | 21 | -------------------------------------------------------------------------------- /libs/miniupnpc/igd_desc_parse.h: -------------------------------------------------------------------------------- 1 | /* $Id: igd_desc_parse.h,v 1.12 2014/11/17 17:19:13 nanard Exp $ */ 2 | /* Project : miniupnp 3 | * http://miniupnp.free.fr/ 4 | * Author : Thomas Bernard 5 | * Copyright (c) 2005-2014 Thomas Bernard 6 | * This software is subject to the conditions detailed in the 7 | * LICENCE file provided in this distribution. 8 | * */ 9 | #ifndef IGD_DESC_PARSE_H_INCLUDED 10 | #define IGD_DESC_PARSE_H_INCLUDED 11 | 12 | /* Structure to store the result of the parsing of UPnP 13 | * descriptions of Internet Gateway Devices */ 14 | #define MINIUPNPC_URL_MAXSIZE (128) 15 | struct IGDdatas_service { 16 | char controlurl[MINIUPNPC_URL_MAXSIZE]; 17 | char eventsuburl[MINIUPNPC_URL_MAXSIZE]; 18 | char scpdurl[MINIUPNPC_URL_MAXSIZE]; 19 | char servicetype[MINIUPNPC_URL_MAXSIZE]; 20 | /*char devicetype[MINIUPNPC_URL_MAXSIZE];*/ 21 | }; 22 | 23 | struct IGDdatas { 24 | char cureltname[MINIUPNPC_URL_MAXSIZE]; 25 | char urlbase[MINIUPNPC_URL_MAXSIZE]; 26 | char presentationurl[MINIUPNPC_URL_MAXSIZE]; 27 | int level; 28 | /*int state;*/ 29 | /* "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1" */ 30 | struct IGDdatas_service CIF; 31 | /* "urn:schemas-upnp-org:service:WANIPConnection:1" 32 | * "urn:schemas-upnp-org:service:WANPPPConnection:1" */ 33 | struct IGDdatas_service first; 34 | /* if both WANIPConnection and WANPPPConnection are present */ 35 | struct IGDdatas_service second; 36 | /* "urn:schemas-upnp-org:service:WANIPv6FirewallControl:1" */ 37 | struct IGDdatas_service IPv6FC; 38 | /* tmp */ 39 | struct IGDdatas_service tmp; 40 | }; 41 | 42 | void IGDstartelt(void *, const char *, int); 43 | void IGDendelt(void *, const char *, int); 44 | void IGDdata(void *, const char *, int); 45 | #ifdef DEBUG 46 | void printIGD(struct IGDdatas *); 47 | #endif /* DEBUG */ 48 | 49 | #endif /* IGD_DESC_PARSE_H_INCLUDED */ 50 | -------------------------------------------------------------------------------- /libs/miniupnpc/minisoap.c: -------------------------------------------------------------------------------- 1 | /* $Id: minisoap.c,v 1.28 2019/02/10 12:29:23 nanard Exp $ */ 2 | /* vim: tabstop=4 shiftwidth=4 noexpandtab 3 | * Project : miniupnp 4 | * Author : Thomas Bernard 5 | * Copyright (c) 2005-2018 Thomas Bernard 6 | * This software is subject to the conditions detailed in the 7 | * LICENCE file provided in this distribution. 8 | * 9 | * Minimal SOAP implementation for UPnP protocol. 10 | */ 11 | #include 12 | #include 13 | #ifdef _WIN32 14 | #include 15 | #include 16 | #define snprintf _snprintf 17 | #else 18 | #include 19 | #include 20 | #include 21 | #endif 22 | #include "minisoap.h" 23 | #include "miniupnpcstrings.h" 24 | 25 | /* only for malloc */ 26 | #include 27 | 28 | /* httpWrite sends the headers and the body to the socket 29 | * and returns the number of bytes sent */ 30 | static int 31 | httpWrite(SOCKET fd, const char * body, int bodysize, 32 | const char * headers, int headerssize) 33 | { 34 | int n = 0; 35 | /*n = write(fd, headers, headerssize);*/ 36 | /*if(bodysize>0) 37 | n += write(fd, body, bodysize);*/ 38 | /* Note : my old linksys router only took into account 39 | * soap request that are sent into only one packet */ 40 | char * p; 41 | /* TODO: AVOID MALLOC, we could use writev() for that */ 42 | p = malloc(headerssize+bodysize); 43 | if(!p) 44 | return -1; 45 | memcpy(p, headers, headerssize); 46 | memcpy(p+headerssize, body, bodysize); 47 | /*n = write(fd, p, headerssize+bodysize);*/ 48 | n = send(fd, p, headerssize+bodysize, 0); 49 | if(n<0) { 50 | PRINT_SOCKET_ERROR("send"); 51 | } 52 | /* disable send on the socket */ 53 | /* draytek routers don't seem to like that... */ 54 | #if 0 55 | #ifdef _WIN32 56 | if(shutdown(fd, SD_SEND)<0) { 57 | #else 58 | if(shutdown(fd, SHUT_WR)<0) { /*SD_SEND*/ 59 | #endif 60 | PRINT_SOCKET_ERROR("shutdown"); 61 | } 62 | #endif 63 | free(p); 64 | return n; 65 | } 66 | 67 | /* self explanatory */ 68 | int soapPostSubmit(SOCKET fd, 69 | const char * url, 70 | const char * host, 71 | unsigned short port, 72 | const char * action, 73 | const char * body, 74 | const char * httpversion) 75 | { 76 | char headerbuf[512]; 77 | int headerssize; 78 | char portstr[8]; 79 | int bodysize = (int)strlen(body); 80 | /* We are not using keep-alive HTTP connections. 81 | * HTTP/1.1 needs the header Connection: close to do that. 82 | * This is the default with HTTP/1.0 83 | * Using HTTP/1.1 means we need to support chunked transfer-encoding : 84 | * When using HTTP/1.1, the router "BiPAC 7404VNOX" always use chunked 85 | * transfer encoding. */ 86 | /* Connection: Close is normally there only in HTTP/1.1 but who knows */ 87 | portstr[0] = '\0'; 88 | if(port != 80) 89 | snprintf(portstr, sizeof(portstr), ":%hu", port); 90 | headerssize = snprintf(headerbuf, sizeof(headerbuf), 91 | "POST %s HTTP/%s\r\n" 92 | "Host: %s%s\r\n" 93 | "User-Agent: " OS_STRING ", " UPNP_VERSION_STRING ", MiniUPnPc/" MINIUPNPC_VERSION_STRING "\r\n" 94 | "Content-Length: %d\r\n" 95 | "Content-Type: text/xml\r\n" 96 | "SOAPAction: \"%s\"\r\n" 97 | "Connection: Close\r\n" 98 | "Cache-Control: no-cache\r\n" /* ??? */ 99 | "Pragma: no-cache\r\n" 100 | "\r\n", 101 | url, httpversion, host, portstr, bodysize, action); 102 | if ((unsigned int)headerssize >= sizeof(headerbuf)) 103 | return -1; 104 | #ifdef DEBUG 105 | /*printf("SOAP request : headersize=%d bodysize=%d\n", 106 | headerssize, bodysize); 107 | */ 108 | printf("SOAP request : POST %s HTTP/%s - Host: %s%s\n", 109 | url, httpversion, host, portstr); 110 | printf("SOAPAction: \"%s\" - Content-Length: %d\n", action, bodysize); 111 | printf("Headers :\n%s", headerbuf); 112 | printf("Body :\n%s\n", body); 113 | #endif 114 | return httpWrite(fd, body, bodysize, headerbuf, headerssize); 115 | } 116 | 117 | 118 | -------------------------------------------------------------------------------- /libs/miniupnpc/minisoap.h: -------------------------------------------------------------------------------- 1 | /* $Id: minisoap.h,v 1.6 2018/04/06 10:53:13 nanard Exp $ */ 2 | /* Project : miniupnp 3 | * Author : Thomas Bernard 4 | * Copyright (c) 2005-2018 Thomas Bernard 5 | * This software is subject to the conditions detailed in the 6 | * LICENCE file provided in this distribution. */ 7 | #ifndef MINISOAP_H_INCLUDED 8 | #define MINISOAP_H_INCLUDED 9 | 10 | #include "miniupnpc_socketdef.h" 11 | 12 | /*int httpWrite(int, const char *, int, const char *);*/ 13 | int soapPostSubmit(SOCKET, const char *, const char *, unsigned short, 14 | const char *, const char *, const char *); 15 | 16 | #endif 17 | 18 | -------------------------------------------------------------------------------- /libs/miniupnpc/minissdpc.h: -------------------------------------------------------------------------------- 1 | /* $Id: minissdpc.h,v 1.8 2019/02/10 12:29:23 nanard Exp $ */ 2 | /* Project: miniupnp 3 | * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ 4 | * Author: Thomas Bernard 5 | * Copyright (c) 2005-2015 Thomas Bernard 6 | * This software is subjects to the conditions detailed 7 | * in the LICENCE file provided within this distribution */ 8 | #ifndef MINISSDPC_H_INCLUDED 9 | #define MINISSDPC_H_INCLUDED 10 | 11 | #include "miniupnpc_declspec.h" 12 | #include "upnpdev.h" 13 | 14 | /* error codes : */ 15 | #define MINISSDPC_SUCCESS (0) 16 | #define MINISSDPC_UNKNOWN_ERROR (-1) 17 | #define MINISSDPC_SOCKET_ERROR (-101) 18 | #define MINISSDPC_MEMORY_ERROR (-102) 19 | #define MINISSDPC_INVALID_INPUT (-103) 20 | #define MINISSDPC_INVALID_SERVER_REPLY (-104) 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | #if !(defined(_WIN32) || defined(__amigaos__) || defined(__amigaos4__)) 27 | 28 | MINIUPNP_LIBSPEC struct UPNPDev * 29 | getDevicesFromMiniSSDPD(const char * devtype, const char * socketpath, int * error); 30 | 31 | MINIUPNP_LIBSPEC int 32 | connectToMiniSSDPD(const char * socketpath); 33 | 34 | MINIUPNP_LIBSPEC int 35 | disconnectFromMiniSSDPD(int s); 36 | 37 | MINIUPNP_LIBSPEC int 38 | requestDevicesFromMiniSSDPD(int s, const char * devtype); 39 | 40 | MINIUPNP_LIBSPEC struct UPNPDev * 41 | receiveDevicesFromMiniSSDPD(int s, int * error); 42 | 43 | #endif /* !(defined(_WIN32) || defined(__amigaos__) || defined(__amigaos4__)) */ 44 | 45 | MINIUPNP_LIBSPEC struct UPNPDev * 46 | ssdpDiscoverDevices(const char * const deviceTypes[], 47 | int delay, const char * multicastif, 48 | int localport, 49 | int ipv6, unsigned char ttl, 50 | int * error, 51 | int searchalltypes); 52 | 53 | #ifdef __cplusplus 54 | } 55 | #endif 56 | 57 | #endif 58 | 59 | -------------------------------------------------------------------------------- /libs/miniupnpc/miniupnpc_declspec.h: -------------------------------------------------------------------------------- 1 | #ifndef MINIUPNPC_DECLSPEC_H_INCLUDED 2 | #define MINIUPNPC_DECLSPEC_H_INCLUDED 3 | 4 | #if defined(_WIN32) && !defined(MINIUPNP_STATICLIB) 5 | /* for windows dll */ 6 | #ifdef MINIUPNP_EXPORTS 7 | #define MINIUPNP_LIBSPEC __declspec(dllexport) 8 | #else 9 | #define MINIUPNP_LIBSPEC __declspec(dllimport) 10 | #endif 11 | #else 12 | #if defined(__GNUC__) && __GNUC__ >= 4 13 | /* fix dynlib for OS X 10.9.2 and Apple LLVM version 5.0 */ 14 | #define MINIUPNP_LIBSPEC __attribute__ ((visibility ("default"))) 15 | #else 16 | #define MINIUPNP_LIBSPEC 17 | #endif 18 | #endif 19 | 20 | #endif /* MINIUPNPC_DECLSPEC_H_INCLUDED */ 21 | 22 | -------------------------------------------------------------------------------- /libs/miniupnpc/miniupnpc_socketdef.h: -------------------------------------------------------------------------------- 1 | /* $Id: miniupnpc_socketdef.h,v 1.3 2019/02/10 13:24:52 nanard Exp $ */ 2 | /* Miniupnp project : http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/ 3 | * Author : Thomas Bernard 4 | * Copyright (c) 2018 Thomas Bernard 5 | * This software is subject to the conditions detailed in the 6 | * LICENCE file provided within this distribution */ 7 | #ifndef MINIUPNPC_SOCKETDEF_H_INCLUDED 8 | #define MINIUPNPC_SOCKETDEF_H_INCLUDED 9 | 10 | #ifdef _MSC_VER 11 | 12 | #define ISINVALID(s) (INVALID_SOCKET==(s)) 13 | 14 | #else 15 | 16 | #ifndef SOCKET 17 | #define SOCKET int 18 | #endif 19 | #ifndef SSIZE_T 20 | #define SSIZE_T ssize_t 21 | #endif 22 | #ifndef INVALID_SOCKET 23 | #define INVALID_SOCKET (-1) 24 | #endif 25 | #ifndef ISINVALID 26 | #define ISINVALID(s) ((s)<0) 27 | #endif 28 | 29 | #endif 30 | 31 | #ifdef _MSC_VER 32 | #define MSC_CAST_INT (int) 33 | #else 34 | #define MSC_CAST_INT 35 | #endif 36 | 37 | /* definition of PRINT_SOCKET_ERROR */ 38 | #ifdef _WIN32 39 | #define PRINT_SOCKET_ERROR(x) fprintf(stderr, "Socket error: %s, %d\n", x, WSAGetLastError()); 40 | #else 41 | #define PRINT_SOCKET_ERROR(x) perror(x) 42 | #endif 43 | 44 | #endif /* MINIUPNPC_SOCKETDEF_H_INCLUDED */ 45 | -------------------------------------------------------------------------------- /libs/miniupnpc/miniupnpcstrings.h: -------------------------------------------------------------------------------- 1 | /* $Id: miniupnpcstrings.h.in,v 1.6 2014/11/04 22:31:55 nanard Exp $ */ 2 | /* Project: miniupnp 3 | * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ 4 | * Author: Thomas Bernard 5 | * Copyright (c) 2005-2014 Thomas Bernard 6 | * This software is subjects to the conditions detailed 7 | * in the LICENCE file provided within this distribution */ 8 | #ifndef MINIUPNPCSTRINGS_H_INCLUDED 9 | #define MINIUPNPCSTRINGS_H_INCLUDED 10 | 11 | #define OS_STRING "Ubuntu/18.04" 12 | #define MINIUPNPC_VERSION_STRING "2.1" 13 | 14 | #if 0 15 | /* according to "UPnP Device Architecture 1.0" */ 16 | #define UPNP_VERSION_STRING "UPnP/1.0" 17 | #else 18 | /* according to "UPnP Device Architecture 1.1" */ 19 | #define UPNP_VERSION_STRING "UPnP/1.1" 20 | #endif 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /libs/miniupnpc/miniupnpcstrings.h.in: -------------------------------------------------------------------------------- 1 | /* $Id: miniupnpcstrings.h.in,v 1.6 2014/11/04 22:31:55 nanard Exp $ */ 2 | /* Project: miniupnp 3 | * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ 4 | * Author: Thomas Bernard 5 | * Copyright (c) 2005-2014 Thomas Bernard 6 | * This software is subjects to the conditions detailed 7 | * in the LICENCE file provided within this distribution */ 8 | #ifndef MINIUPNPCSTRINGS_H_INCLUDED 9 | #define MINIUPNPCSTRINGS_H_INCLUDED 10 | 11 | #define OS_STRING "OS/version" 12 | #define MINIUPNPC_VERSION_STRING "version" 13 | 14 | #if 0 15 | /* according to "UPnP Device Architecture 1.0" */ 16 | #define UPNP_VERSION_STRING "UPnP/1.0" 17 | #else 18 | /* according to "UPnP Device Architecture 1.1" */ 19 | #define UPNP_VERSION_STRING "UPnP/1.1" 20 | #endif 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /libs/miniupnpc/miniupnpctypes.h: -------------------------------------------------------------------------------- 1 | /* $Id: miniupnpctypes.h,v 1.2 2012/09/27 15:42:10 nanard Exp $ */ 2 | /* Miniupnp project : http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org 3 | * Author : Thomas Bernard 4 | * Copyright (c) 2011 Thomas Bernard 5 | * This software is subject to the conditions detailed in the 6 | * LICENCE file provided within this distribution */ 7 | #ifndef MINIUPNPCTYPES_H_INCLUDED 8 | #define MINIUPNPCTYPES_H_INCLUDED 9 | 10 | #if (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L) 11 | #define UNSIGNED_INTEGER unsigned long long 12 | #define STRTOUI strtoull 13 | #else 14 | #define UNSIGNED_INTEGER unsigned int 15 | #define STRTOUI strtoul 16 | #endif 17 | 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /libs/miniupnpc/miniwget.h: -------------------------------------------------------------------------------- 1 | /* $Id: miniwget.h,v 1.13 2018/04/06 10:53:15 nanard Exp $ */ 2 | /* Project : miniupnp 3 | * Author : Thomas Bernard 4 | * Copyright (c) 2005-2016 Thomas Bernard 5 | * This software is subject to the conditions detailed in the 6 | * LICENCE file provided in this distribution. 7 | * */ 8 | #ifndef MINIWGET_H_INCLUDED 9 | #define MINIWGET_H_INCLUDED 10 | 11 | #include "miniupnpc_declspec.h" 12 | 13 | #ifdef __cplusplus 14 | extern "C" { 15 | #endif 16 | 17 | MINIUPNP_LIBSPEC void * miniwget(const char *, int *, unsigned int, int *); 18 | 19 | MINIUPNP_LIBSPEC void * miniwget_getaddr(const char *, int *, char *, int, unsigned int, int *); 20 | 21 | int parseURL(const char *, char *, unsigned short *, char * *, unsigned int *); 22 | 23 | #ifdef __cplusplus 24 | } 25 | #endif 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /libs/miniupnpc/miniwget_private.h: -------------------------------------------------------------------------------- 1 | /* $Id: miniwget_private.h,v 1.1 2018/04/06 10:17:58 nanard Exp $ */ 2 | /* Project : miniupnp 3 | * Author : Thomas Bernard 4 | * Copyright (c) 2018 Thomas Bernard 5 | * This software is subject to the conditions detailed in the 6 | * LICENCE file provided in this distribution. 7 | * */ 8 | #ifndef MINIWGET_INTERNAL_H_INCLUDED 9 | #define MINIWGET_INTERNAL_H_INCLUDED 10 | 11 | #include "miniupnpc_socketdef.h" 12 | 13 | void * getHTTPResponse(SOCKET s, int * size, int * status_code); 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /libs/miniupnpc/minixml.h: -------------------------------------------------------------------------------- 1 | /* $Id: minixml.h,v 1.8 2019/02/10 12:29:25 nanard Exp $ */ 2 | /* minimal xml parser 3 | * 4 | * Project : miniupnp 5 | * Website : http://miniupnp.free.fr/ 6 | * Author : Thomas Bernard 7 | * Copyright (c) 2005 Thomas Bernard 8 | * This software is subject to the conditions detailed in the 9 | * LICENCE file provided in this distribution. 10 | * */ 11 | #ifndef MINIXML_H_INCLUDED 12 | #define MINIXML_H_INCLUDED 13 | #define IS_WHITE_SPACE(c) ((c)==' ' || (c)=='\t' || (c)=='\r' || (c)=='\n') 14 | 15 | /* if a callback function pointer is set to NULL, 16 | * the function is not called */ 17 | struct xmlparser { 18 | const char *xmlstart; 19 | const char *xmlend; 20 | const char *xml; /* pointer to current character */ 21 | int xmlsize; 22 | void * data; 23 | void (*starteltfunc) (void *, const char *, int); 24 | void (*endeltfunc) (void *, const char *, int); 25 | void (*datafunc) (void *, const char *, int); 26 | void (*attfunc) (void *, const char *, int, const char *, int); 27 | }; 28 | 29 | /* parsexml() 30 | * the xmlparser structure must be initialized before the call 31 | * the following structure members have to be initialized : 32 | * xmlstart, xmlsize, data, *func 33 | * xml is for internal usage, xmlend is computed automatically */ 34 | void parsexml(struct xmlparser *); 35 | 36 | #endif 37 | 38 | -------------------------------------------------------------------------------- /libs/miniupnpc/portlistingparse.c: -------------------------------------------------------------------------------- 1 | /* $Id: portlistingparse.c,v 1.10 2016/12/16 08:53:21 nanard Exp $ */ 2 | /* MiniUPnP project 3 | * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ 4 | * (c) 2011-2016 Thomas Bernard 5 | * This software is subject to the conditions detailed 6 | * in the LICENCE file provided within the distribution */ 7 | #include 8 | #include 9 | #ifdef DEBUG 10 | #include 11 | #endif /* DEBUG */ 12 | #include "portlistingparse.h" 13 | #include "minixml.h" 14 | 15 | /* list of the elements */ 16 | static const struct { 17 | const portMappingElt code; 18 | const char * const str; 19 | } elements[] = { 20 | { PortMappingEntry, "PortMappingEntry"}, 21 | { NewRemoteHost, "NewRemoteHost"}, 22 | { NewExternalPort, "NewExternalPort"}, 23 | { NewProtocol, "NewProtocol"}, 24 | { NewInternalPort, "NewInternalPort"}, 25 | { NewInternalClient, "NewInternalClient"}, 26 | { NewEnabled, "NewEnabled"}, 27 | { NewDescription, "NewDescription"}, 28 | { NewLeaseTime, "NewLeaseTime"}, 29 | { PortMappingEltNone, NULL} 30 | }; 31 | 32 | /* Helper function */ 33 | static UNSIGNED_INTEGER 34 | atoui(const char * p, int l) 35 | { 36 | UNSIGNED_INTEGER r = 0; 37 | while(l > 0 && *p) 38 | { 39 | if(*p >= '0' && *p <= '9') 40 | r = r*10 + (*p - '0'); 41 | else 42 | break; 43 | p++; 44 | l--; 45 | } 46 | return r; 47 | } 48 | 49 | /* Start element handler */ 50 | static void 51 | startelt(void * d, const char * name, int l) 52 | { 53 | int i; 54 | struct PortMappingParserData * pdata = (struct PortMappingParserData *)d; 55 | pdata->curelt = PortMappingEltNone; 56 | for(i = 0; elements[i].str; i++) 57 | { 58 | if(strlen(elements[i].str) == (size_t)l && memcmp(name, elements[i].str, l) == 0) 59 | { 60 | pdata->curelt = elements[i].code; 61 | break; 62 | } 63 | } 64 | if(pdata->curelt == PortMappingEntry) 65 | { 66 | struct PortMapping * pm; 67 | pm = calloc(1, sizeof(struct PortMapping)); 68 | if(pm == NULL) 69 | { 70 | /* malloc error */ 71 | #ifdef DEBUG 72 | fprintf(stderr, "%s: error allocating memory", 73 | "startelt"); 74 | #endif /* DEBUG */ 75 | return; 76 | } 77 | pm->l_next = pdata->l_head; /* insert in list */ 78 | pdata->l_head = pm; 79 | } 80 | } 81 | 82 | /* End element handler */ 83 | static void 84 | endelt(void * d, const char * name, int l) 85 | { 86 | struct PortMappingParserData * pdata = (struct PortMappingParserData *)d; 87 | (void)name; 88 | (void)l; 89 | pdata->curelt = PortMappingEltNone; 90 | } 91 | 92 | /* Data handler */ 93 | static void 94 | data(void * d, const char * data, int l) 95 | { 96 | struct PortMapping * pm; 97 | struct PortMappingParserData * pdata = (struct PortMappingParserData *)d; 98 | pm = pdata->l_head; 99 | if(!pm) 100 | return; 101 | if(l > 63) 102 | l = 63; 103 | switch(pdata->curelt) 104 | { 105 | case NewRemoteHost: 106 | memcpy(pm->remoteHost, data, l); 107 | pm->remoteHost[l] = '\0'; 108 | break; 109 | case NewExternalPort: 110 | pm->externalPort = (unsigned short)atoui(data, l); 111 | break; 112 | case NewProtocol: 113 | if(l > 3) 114 | l = 3; 115 | memcpy(pm->protocol, data, l); 116 | pm->protocol[l] = '\0'; 117 | break; 118 | case NewInternalPort: 119 | pm->internalPort = (unsigned short)atoui(data, l); 120 | break; 121 | case NewInternalClient: 122 | memcpy(pm->internalClient, data, l); 123 | pm->internalClient[l] = '\0'; 124 | break; 125 | case NewEnabled: 126 | pm->enabled = (unsigned char)atoui(data, l); 127 | break; 128 | case NewDescription: 129 | memcpy(pm->description, data, l); 130 | pm->description[l] = '\0'; 131 | break; 132 | case NewLeaseTime: 133 | pm->leaseTime = atoui(data, l); 134 | break; 135 | default: 136 | break; 137 | } 138 | } 139 | 140 | 141 | /* Parse the PortMappingList XML document for IGD version 2 142 | */ 143 | void 144 | ParsePortListing(const char * buffer, int bufsize, 145 | struct PortMappingParserData * pdata) 146 | { 147 | struct xmlparser parser; 148 | 149 | memset(pdata, 0, sizeof(struct PortMappingParserData)); 150 | /* init xmlparser */ 151 | parser.xmlstart = buffer; 152 | parser.xmlsize = bufsize; 153 | parser.data = pdata; 154 | parser.starteltfunc = startelt; 155 | parser.endeltfunc = endelt; 156 | parser.datafunc = data; 157 | parser.attfunc = 0; 158 | parsexml(&parser); 159 | } 160 | 161 | void 162 | FreePortListing(struct PortMappingParserData * pdata) 163 | { 164 | struct PortMapping * pm; 165 | while((pm = pdata->l_head) != NULL) 166 | { 167 | /* remove from list */ 168 | pdata->l_head = pm->l_next; 169 | free(pm); 170 | } 171 | } 172 | 173 | -------------------------------------------------------------------------------- /libs/miniupnpc/portlistingparse.h: -------------------------------------------------------------------------------- 1 | /* $Id: portlistingparse.h,v 1.11 2015/07/21 13:16:55 nanard Exp $ */ 2 | /* MiniUPnP project 3 | * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ 4 | * (c) 2011-2015 Thomas Bernard 5 | * This software is subject to the conditions detailed 6 | * in the LICENCE file provided within the distribution */ 7 | #ifndef PORTLISTINGPARSE_H_INCLUDED 8 | #define PORTLISTINGPARSE_H_INCLUDED 9 | 10 | #include "miniupnpc_declspec.h" 11 | /* for the definition of UNSIGNED_INTEGER */ 12 | #include "miniupnpctypes.h" 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | /* sample of PortMappingEntry : 19 | 20 | 202.233.2.1 21 | 2345 22 | TCP 23 | 2345 24 | 192.168.1.137 25 | 1 26 | dooom 27 | 345 28 | 29 | */ 30 | typedef enum { PortMappingEltNone, 31 | PortMappingEntry, NewRemoteHost, 32 | NewExternalPort, NewProtocol, 33 | NewInternalPort, NewInternalClient, 34 | NewEnabled, NewDescription, 35 | NewLeaseTime } portMappingElt; 36 | 37 | struct PortMapping { 38 | struct PortMapping * l_next; /* list next element */ 39 | UNSIGNED_INTEGER leaseTime; 40 | unsigned short externalPort; 41 | unsigned short internalPort; 42 | char remoteHost[64]; 43 | char internalClient[64]; 44 | char description[64]; 45 | char protocol[4]; 46 | unsigned char enabled; 47 | }; 48 | 49 | struct PortMappingParserData { 50 | struct PortMapping * l_head; /* list head */ 51 | portMappingElt curelt; 52 | }; 53 | 54 | MINIUPNP_LIBSPEC void 55 | ParsePortListing(const char * buffer, int bufsize, 56 | struct PortMappingParserData * pdata); 57 | 58 | MINIUPNP_LIBSPEC void 59 | FreePortListing(struct PortMappingParserData * pdata); 60 | 61 | #ifdef __cplusplus 62 | } 63 | #endif 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /libs/miniupnpc/receivedata.c: -------------------------------------------------------------------------------- 1 | /* $Id: receivedata.c,v 1.9 2018/04/06 10:53:15 nanard Exp $ */ 2 | /* Project : miniupnp 3 | * Website : http://miniupnp.free.fr/ 4 | * Author : Thomas Bernard 5 | * Copyright (c) 2011-2014 Thomas Bernard 6 | * This software is subject to the conditions detailed in the 7 | * LICENCE file provided in this distribution. */ 8 | 9 | #include 10 | #include 11 | #ifdef _WIN32 12 | #include 13 | #include 14 | #else /* _WIN32 */ 15 | #include 16 | #if defined(__amigaos__) && !defined(__amigaos4__) 17 | #define socklen_t int 18 | #else /* #if defined(__amigaos__) && !defined(__amigaos4__) */ 19 | #include 20 | #endif /* #else defined(__amigaos__) && !defined(__amigaos4__) */ 21 | #include 22 | #include 23 | #if !defined(__amigaos__) && !defined(__amigaos4__) 24 | #include 25 | #endif /* !defined(__amigaos__) && !defined(__amigaos4__) */ 26 | #include 27 | #define MINIUPNPC_IGNORE_EINTR 28 | #endif /* _WIN32 */ 29 | 30 | #include "receivedata.h" 31 | 32 | int 33 | receivedata(SOCKET socket, 34 | char * data, int length, 35 | int timeout, unsigned int * scope_id) 36 | { 37 | #ifdef MINIUPNPC_GET_SRC_ADDR 38 | struct sockaddr_storage src_addr; 39 | socklen_t src_addr_len = sizeof(src_addr); 40 | #endif /* MINIUPNPC_GET_SRC_ADDR */ 41 | int n; 42 | #if !defined(_WIN32) && !defined(__amigaos__) && !defined(__amigaos4__) 43 | /* using poll */ 44 | struct pollfd fds[1]; /* for the poll */ 45 | #ifdef MINIUPNPC_IGNORE_EINTR 46 | do { 47 | #endif /* MINIUPNPC_IGNORE_EINTR */ 48 | fds[0].fd = socket; 49 | fds[0].events = POLLIN; 50 | n = poll(fds, 1, timeout); 51 | #ifdef MINIUPNPC_IGNORE_EINTR 52 | } while(n < 0 && errno == EINTR); 53 | #endif /* MINIUPNPC_IGNORE_EINTR */ 54 | if(n < 0) { 55 | PRINT_SOCKET_ERROR("poll"); 56 | return -1; 57 | } else if(n == 0) { 58 | /* timeout */ 59 | return 0; 60 | } 61 | #else /* !defined(_WIN32) && !defined(__amigaos__) && !defined(__amigaos4__) */ 62 | /* using select under _WIN32 and amigaos */ 63 | fd_set socketSet; 64 | TIMEVAL timeval; 65 | FD_ZERO(&socketSet); 66 | FD_SET(socket, &socketSet); 67 | timeval.tv_sec = timeout / 1000; 68 | timeval.tv_usec = (timeout % 1000) * 1000; 69 | n = select(FD_SETSIZE, &socketSet, NULL, NULL, &timeval); 70 | if(n < 0) { 71 | PRINT_SOCKET_ERROR("select"); 72 | return -1; 73 | } else if(n == 0) { 74 | return 0; 75 | } 76 | #endif /* !defined(_WIN32) && !defined(__amigaos__) && !defined(__amigaos4__) */ 77 | #ifdef MINIUPNPC_GET_SRC_ADDR 78 | memset(&src_addr, 0, sizeof(src_addr)); 79 | n = recvfrom(socket, data, length, 0, 80 | (struct sockaddr *)&src_addr, &src_addr_len); 81 | #else /* MINIUPNPC_GET_SRC_ADDR */ 82 | n = recv(socket, data, length, 0); 83 | #endif /* MINIUPNPC_GET_SRC_ADDR */ 84 | if(n<0) { 85 | PRINT_SOCKET_ERROR("recv"); 86 | } 87 | #ifdef MINIUPNPC_GET_SRC_ADDR 88 | if (src_addr.ss_family == AF_INET6) { 89 | const struct sockaddr_in6 * src_addr6 = (struct sockaddr_in6 *)&src_addr; 90 | #ifdef DEBUG 91 | printf("scope_id=%u\n", src_addr6->sin6_scope_id); 92 | #endif /* DEBUG */ 93 | if(scope_id) 94 | *scope_id = src_addr6->sin6_scope_id; 95 | } 96 | #endif /* MINIUPNPC_GET_SRC_ADDR */ 97 | return n; 98 | } 99 | 100 | -------------------------------------------------------------------------------- /libs/miniupnpc/receivedata.h: -------------------------------------------------------------------------------- 1 | /* $Id: receivedata.h,v 1.5 2018/04/06 10:53:15 nanard Exp $ */ 2 | /* Project: miniupnp 3 | * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ 4 | * Author: Thomas Bernard 5 | * Copyright (c) 2011-2018 Thomas Bernard 6 | * This software is subjects to the conditions detailed 7 | * in the LICENCE file provided within this distribution */ 8 | #ifndef RECEIVEDATA_H_INCLUDED 9 | #define RECEIVEDATA_H_INCLUDED 10 | 11 | #include "miniupnpc_socketdef.h" 12 | 13 | /* Reads data from the specified socket. 14 | * Returns the number of bytes read if successful, zero if no bytes were 15 | * read or if we timed out. Returns negative if there was an error. */ 16 | int receivedata(SOCKET socket, 17 | char * data, int length, 18 | int timeout, unsigned int * scope_id); 19 | 20 | #endif 21 | 22 | -------------------------------------------------------------------------------- /libs/miniupnpc/upnpdev.c: -------------------------------------------------------------------------------- 1 | /* $Id: upnpdev.c,v 1.1 2015/08/28 12:14:19 nanard Exp $ */ 2 | /* Project : miniupnp 3 | * Web : http://miniupnp.free.fr/ 4 | * Author : Thomas BERNARD 5 | * copyright (c) 2005-2015 Thomas Bernard 6 | * This software is subjet to the conditions detailed in the 7 | * provided LICENSE file. */ 8 | #include 9 | #include "upnpdev.h" 10 | 11 | /* freeUPNPDevlist() should be used to 12 | * free the chained list returned by upnpDiscover() */ 13 | void freeUPNPDevlist(struct UPNPDev * devlist) 14 | { 15 | struct UPNPDev * next; 16 | while(devlist) 17 | { 18 | next = devlist->pNext; 19 | free(devlist); 20 | devlist = next; 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /libs/miniupnpc/upnpdev.h: -------------------------------------------------------------------------------- 1 | /* $Id: upnpdev.h,v 1.2 2018/04/06 10:53:15 nanard Exp $ */ 2 | /* Project : miniupnp 3 | * Web : http://miniupnp.free.fr/ 4 | * Author : Thomas BERNARD 5 | * copyright (c) 2005-2018 Thomas Bernard 6 | * This software is subjet to the conditions detailed in the 7 | * provided LICENSE file. */ 8 | #ifndef UPNPDEV_H_INCLUDED 9 | #define UPNPDEV_H_INCLUDED 10 | 11 | #include "miniupnpc_declspec.h" 12 | 13 | #ifdef __cplusplus 14 | extern "C" { 15 | #endif 16 | 17 | struct UPNPDev { 18 | struct UPNPDev * pNext; 19 | char * descURL; 20 | char * st; 21 | char * usn; 22 | unsigned int scope_id; 23 | char buffer[3]; 24 | }; 25 | 26 | /* freeUPNPDevlist() 27 | * free list returned by upnpDiscover() */ 28 | MINIUPNP_LIBSPEC void freeUPNPDevlist(struct UPNPDev * devlist); 29 | 30 | 31 | #ifdef __cplusplus 32 | } 33 | #endif 34 | 35 | 36 | #endif /* UPNPDEV_H_INCLUDED */ 37 | -------------------------------------------------------------------------------- /libs/miniupnpc/upnperrors.c: -------------------------------------------------------------------------------- 1 | /* $Id: upnperrors.c,v 1.10 2019/08/24 08:49:53 nanard Exp $ */ 2 | /* vim: tabstop=4 shiftwidth=4 noexpandtab 3 | * Project : miniupnp 4 | * Author : Thomas BERNARD 5 | * copyright (c) 2007-2019 Thomas Bernard 6 | * All Right reserved. 7 | * http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/ 8 | * This software is subjet to the conditions detailed in the 9 | * provided LICENCE file. */ 10 | #include 11 | #include "upnperrors.h" 12 | #include "upnpcommands.h" 13 | #include "miniupnpc.h" 14 | 15 | const char * strupnperror(int err) 16 | { 17 | const char * s = NULL; 18 | switch(err) { 19 | case UPNPCOMMAND_SUCCESS: 20 | s = "Success"; 21 | break; 22 | case UPNPCOMMAND_UNKNOWN_ERROR: 23 | s = "Miniupnpc Unknown Error"; 24 | break; 25 | case UPNPCOMMAND_INVALID_ARGS: 26 | s = "Miniupnpc Invalid Arguments"; 27 | break; 28 | case UPNPCOMMAND_INVALID_RESPONSE: 29 | s = "Miniupnpc Invalid response"; 30 | break; 31 | case UPNPCOMMAND_HTTP_ERROR: 32 | s = "Miniupnpc HTTP error"; 33 | break; 34 | case UPNPDISCOVER_SOCKET_ERROR: 35 | s = "Miniupnpc Socket error"; 36 | break; 37 | case UPNPDISCOVER_MEMORY_ERROR: 38 | case UPNPCOMMAND_MEM_ALLOC_ERROR: 39 | s = "Miniupnpc Memory allocation error"; 40 | break; 41 | case 401: 42 | s = "Invalid Action"; 43 | break; 44 | case 402: 45 | s = "Invalid Args"; 46 | break; 47 | case 501: 48 | s = "Action Failed"; 49 | break; 50 | case 606: 51 | s = "Action not authorized"; 52 | break; 53 | case 701: 54 | s = "PinholeSpaceExhausted"; 55 | break; 56 | case 702: 57 | s = "FirewallDisabled"; 58 | break; 59 | case 703: 60 | s = "InboundPinholeNotAllowed"; 61 | break; 62 | case 704: 63 | s = "NoSuchEntry"; 64 | break; 65 | case 705: 66 | s = "ProtocolNotSupported"; 67 | break; 68 | case 706: 69 | s = "InternalPortWildcardingNotAllowed"; 70 | break; 71 | case 707: 72 | s = "ProtocolWildcardingNotAllowed"; 73 | break; 74 | case 708: 75 | s = "InvalidLayer2Address"; 76 | break; 77 | case 709: 78 | s = "NoPacketSent"; 79 | break; 80 | case 713: 81 | s = "SpecifiedArrayIndexInvalid"; 82 | break; 83 | case 714: 84 | s = "NoSuchEntryInArray"; 85 | break; 86 | case 715: 87 | s = "WildCardNotPermittedInSrcIP"; 88 | break; 89 | case 716: 90 | s = "WildCardNotPermittedInExtPort"; 91 | break; 92 | case 718: 93 | s = "ConflictInMappingEntry"; 94 | break; 95 | case 724: 96 | s = "SamePortValuesRequired"; 97 | break; 98 | case 725: 99 | s = "OnlyPermanentLeasesSupported"; 100 | break; 101 | case 726: 102 | s = "RemoteHostOnlySupportsWildcard"; 103 | break; 104 | case 727: 105 | s = "ExternalPortOnlySupportsWildcard"; 106 | break; 107 | default: 108 | s = "UnknownError"; 109 | break; 110 | } 111 | return s; 112 | } 113 | -------------------------------------------------------------------------------- /libs/miniupnpc/upnperrors.h: -------------------------------------------------------------------------------- 1 | /* $Id: upnperrors.h,v 1.6 2015/07/21 13:16:55 nanard Exp $ */ 2 | /* (c) 2007-2015 Thomas Bernard 3 | * All rights reserved. 4 | * MiniUPnP Project. 5 | * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ 6 | * This software is subjet to the conditions detailed in the 7 | * provided LICENCE file. */ 8 | #ifndef UPNPERRORS_H_INCLUDED 9 | #define UPNPERRORS_H_INCLUDED 10 | 11 | #include "miniupnpc_declspec.h" 12 | 13 | #ifdef __cplusplus 14 | extern "C" { 15 | #endif 16 | 17 | /* strupnperror() 18 | * Return a string description of the UPnP error code 19 | * or NULL for undefinded errors */ 20 | MINIUPNP_LIBSPEC const char * strupnperror(int err); 21 | 22 | #ifdef __cplusplus 23 | } 24 | #endif 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /libs/miniupnpc/upnpreplyparse.h: -------------------------------------------------------------------------------- 1 | /* $Id: upnpreplyparse.h,v 1.19 2014/10/27 16:33:19 nanard Exp $ */ 2 | /* MiniUPnP project 3 | * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ 4 | * (c) 2006-2013 Thomas Bernard 5 | * This software is subject to the conditions detailed 6 | * in the LICENCE file provided within the distribution */ 7 | 8 | #ifndef UPNPREPLYPARSE_H_INCLUDED 9 | #define UPNPREPLYPARSE_H_INCLUDED 10 | 11 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | struct NameValue { 16 | struct NameValue * l_next; 17 | char name[64]; 18 | char value[128]; 19 | }; 20 | 21 | struct NameValueParserData { 22 | struct NameValue * l_head; 23 | char curelt[64]; 24 | char * portListing; 25 | int portListingLength; 26 | int topelt; 27 | const char * cdata; 28 | int cdatalen; 29 | }; 30 | 31 | /* ParseNameValue() */ 32 | void 33 | ParseNameValue(const char * buffer, int bufsize, 34 | struct NameValueParserData * data); 35 | 36 | /* ClearNameValueList() */ 37 | void 38 | ClearNameValueList(struct NameValueParserData * pdata); 39 | 40 | /* GetValueFromNameValueList() */ 41 | char * 42 | GetValueFromNameValueList(struct NameValueParserData * pdata, 43 | const char * Name); 44 | 45 | #if 0 46 | /* GetValueFromNameValueListIgnoreNS() */ 47 | char * 48 | GetValueFromNameValueListIgnoreNS(struct NameValueParserData * pdata, 49 | const char * Name); 50 | #endif 51 | 52 | /* DisplayNameValueList() */ 53 | #ifdef DEBUG 54 | void 55 | DisplayNameValueList(char * buffer, int bufsize); 56 | #endif 57 | 58 | #ifdef __cplusplus 59 | } 60 | #endif 61 | 62 | #endif 63 | 64 | -------------------------------------------------------------------------------- /libs/protocol/network_protocol.h: -------------------------------------------------------------------------------- 1 | #ifndef NETWORK_PROTOCOL_H 2 | #define NETWORK_PROTOCOL_H 3 | 4 | #include 5 | 6 | #if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__) 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #endif 21 | 22 | 23 | #if defined(_WIN32) 24 | #include 25 | #endif 26 | 27 | 28 | #ifndef ETH_ALEN 29 | #define ETH_ALEN 6 30 | #endif 31 | 32 | #ifndef ARPHRD_ETHER 33 | #define ARPHRD_ETHER 1 34 | #endif 35 | 36 | #ifndef ETH_P_IP 37 | #define ETH_P_IP 0x0800 38 | #endif 39 | 40 | #ifndef ETH_P_ARP 41 | #define ETH_P_ARP 0x0806 42 | #endif 43 | 44 | #ifndef ETH_P_IPV6 45 | #define ETH_P_IPV6 0x86DD 46 | #endif 47 | 48 | #ifndef ETH_P_8021Q 49 | #define ETH_P_8021Q 0x8100 50 | #endif 51 | 52 | struct iphdr { 53 | 54 | #if __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__ 55 | u_char ihl:4, /* header length */ 56 | version:4; /* version */ 57 | #endif 58 | 59 | #if __BYTE_ORDER__==__ORDER_BIG_ENDIAN__ 60 | u_char version:4, /* version */ 61 | ihl:4; /* header length */ 62 | #endif 63 | 64 | u_char tos; /* type of service */ 65 | short tot_len; /* total length */ 66 | u_short id; /* identification */ 67 | short off; /* fragment offset field */ 68 | u_char ttl; /* time to live */ 69 | u_char protocol; /* protocol */ 70 | u_short check; /* checksum */ 71 | struct in_addr saddr; 72 | struct in_addr daddr; /* source and dest address */ 73 | 74 | }; 75 | 76 | 77 | #pragma pack(push, 1) 78 | 79 | struct gnb_in6_addr { 80 | 81 | union { 82 | uint8_t __u6_addr8[16]; 83 | uint16_t __u6_addr16[8]; 84 | uint32_t __u6_addr32[4]; 85 | } __in6_u; 86 | 87 | }__attribute__ ((__packed__)); 88 | 89 | struct ip6_hdr { 90 | 91 | union { 92 | 93 | struct ip6_hdrctl { 94 | uint32_t ip6_un1_flow; /* 20 bits of flow-ID */ 95 | uint16_t ip6_un1_plen; /* payload length */ 96 | uint8_t ip6_un1_nxt; /* next header */ 97 | uint8_t ip6_un1_hlim; /* hop limit */ 98 | } ip6_un1; 99 | 100 | uint8_t ip6_un2_vfc; /* 4 bits version, top 4 bits class */ 101 | 102 | } ip6_ctlun; 103 | 104 | struct gnb_in6_addr ip6_src; /* source address */ 105 | struct gnb_in6_addr ip6_dst; /* destination address */ 106 | 107 | } __attribute__ ((__packed__)); 108 | 109 | #pragma pack(pop) 110 | 111 | #endif 112 | -------------------------------------------------------------------------------- /libs/tap-windows/tap-windows.h: -------------------------------------------------------------------------------- 1 | /* 2 | * TAP-Windows -- A kernel driver to provide virtual tap 3 | * device functionality on Windows. 4 | * 5 | * This code was inspired by the CIPE-Win32 driver by Damion K. Wilson. 6 | * 7 | * This source code is Copyright (C) 2002-2014 OpenVPN Technologies, Inc., 8 | * and is released under the GPL version 2 (see below). This particular file 9 | * (tap-windows.h) is also licensed using the MIT license (see COPYRIGHT.MIT). 10 | * 11 | * This program is free software; you can redistribute it and/or modify 12 | * it under the terms of the GNU General Public License version 2 13 | * as published by the Free Software Foundation. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program (see the file COPYING included with this 22 | * distribution); if not, write to the Free Software Foundation, Inc., 23 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 24 | */ 25 | #ifndef __TAP_WIN_H 26 | #define __TAP_WIN_H 27 | 28 | /* 29 | * ============= 30 | * TAP IOCTLs 31 | * ============= 32 | */ 33 | 34 | #define TAP_WIN_CONTROL_CODE(request,method) \ 35 | CTL_CODE (FILE_DEVICE_UNKNOWN, request, method, FILE_ANY_ACCESS) 36 | 37 | /* Present in 8.1 */ 38 | 39 | #define TAP_WIN_IOCTL_GET_MAC TAP_WIN_CONTROL_CODE (1, METHOD_BUFFERED) 40 | #define TAP_WIN_IOCTL_GET_VERSION TAP_WIN_CONTROL_CODE (2, METHOD_BUFFERED) 41 | #define TAP_WIN_IOCTL_GET_MTU TAP_WIN_CONTROL_CODE (3, METHOD_BUFFERED) 42 | #define TAP_WIN_IOCTL_GET_INFO TAP_WIN_CONTROL_CODE (4, METHOD_BUFFERED) 43 | #define TAP_WIN_IOCTL_CONFIG_POINT_TO_POINT TAP_WIN_CONTROL_CODE (5, METHOD_BUFFERED) 44 | #define TAP_WIN_IOCTL_SET_MEDIA_STATUS TAP_WIN_CONTROL_CODE (6, METHOD_BUFFERED) 45 | #define TAP_WIN_IOCTL_CONFIG_DHCP_MASQ TAP_WIN_CONTROL_CODE (7, METHOD_BUFFERED) 46 | #define TAP_WIN_IOCTL_GET_LOG_LINE TAP_WIN_CONTROL_CODE (8, METHOD_BUFFERED) 47 | #define TAP_WIN_IOCTL_CONFIG_DHCP_SET_OPT TAP_WIN_CONTROL_CODE (9, METHOD_BUFFERED) 48 | 49 | /* Added in 8.2 */ 50 | 51 | /* obsoletes TAP_WIN_IOCTL_CONFIG_POINT_TO_POINT */ 52 | #define TAP_WIN_IOCTL_CONFIG_TUN TAP_WIN_CONTROL_CODE (10, METHOD_BUFFERED) 53 | 54 | /* Control whether 802.1Q headers are added for priority */ 55 | #define TAP_WIN_IOCTL_PRIORITY_BEHAVIOR TAP_WIN_CONTROL_CODE (11, METHOD_BUFFERED) 56 | #define TAP_PRIORITY_BEHAVIOR_NOPRIORITY 0 57 | #define TAP_PRIORITY_BEHAVIOR_ENABLED 1 58 | #define TAP_PRIORITY_BEHAVIOR_ADDALWAYS 2 59 | #define TAP_PRIORITY_BEHAVIOR_MAX 2 60 | 61 | /* 62 | * ================= 63 | * Registry keys 64 | * ================= 65 | */ 66 | 67 | #define ADAPTER_KEY "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}" 68 | 69 | #define NETWORK_CONNECTIONS_KEY "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}" 70 | 71 | /* 72 | * ====================== 73 | * Filesystem prefixes 74 | * ====================== 75 | */ 76 | 77 | #define USERMODEDEVICEDIR "\\\\.\\Global\\" 78 | #define SYSDEVICEDIR "\\Device\\" 79 | #define USERDEVICEDIR "\\DosDevices\\Global\\" 80 | #define TAP_WIN_SUFFIX ".tap" 81 | 82 | #endif // __TAP_WIN_H 83 | -------------------------------------------------------------------------------- /libs/zlib/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright notice: 2 | 3 | (C) 1995-2022 Jean-loup Gailly and Mark Adler 4 | 5 | This software is provided 'as-is', without any express or implied 6 | warranty. In no event will the authors be held liable for any damages 7 | arising from the use of this software. 8 | 9 | Permission is granted to anyone to use this software for any purpose, 10 | including commercial applications, and to alter it and redistribute it 11 | freely, subject to the following restrictions: 12 | 13 | 1. The origin of this software must not be misrepresented; you must not 14 | claim that you wrote the original software. If you use this software 15 | in a product, an acknowledgment in the product documentation would be 16 | appreciated but is not required. 17 | 2. Altered source versions must be plainly marked as such, and must not be 18 | misrepresented as being the original software. 19 | 3. This notice may not be removed or altered from any source distribution. 20 | 21 | Jean-loup Gailly Mark Adler 22 | jloup@gzip.org madler@alumni.caltech.edu 23 | -------------------------------------------------------------------------------- /libs/zlib/inffast.h: -------------------------------------------------------------------------------- 1 | /* inffast.h -- header to use inffast.c 2 | * Copyright (C) 1995-2003, 2010 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | void ZLIB_INTERNAL inflate_fast(z_streamp strm, unsigned start); 12 | -------------------------------------------------------------------------------- /libs/zlib/inftrees.h: -------------------------------------------------------------------------------- 1 | /* inftrees.h -- header to use inftrees.c 2 | * Copyright (C) 1995-2005, 2010 Mark Adler 3 | * For conditions of distribution and use, see copyright notice in zlib.h 4 | */ 5 | 6 | /* WARNING: this file should *not* be used by applications. It is 7 | part of the implementation of the compression library and is 8 | subject to change. Applications should only use zlib.h. 9 | */ 10 | 11 | /* Structure for decoding tables. Each entry provides either the 12 | information needed to do the operation requested by the code that 13 | indexed that table entry, or it provides a pointer to another 14 | table that indexes more bits of the code. op indicates whether 15 | the entry is a pointer to another table, a literal, a length or 16 | distance, an end-of-block, or an invalid code. For a table 17 | pointer, the low four bits of op is the number of index bits of 18 | that table. For a length or distance, the low four bits of op 19 | is the number of extra bits to get after the code. bits is 20 | the number of bits in this code or part of the code to drop off 21 | of the bit buffer. val is the actual byte to output in the case 22 | of a literal, the base length or distance, or the offset from 23 | the current table to the next table. Each entry is four bytes. */ 24 | typedef struct { 25 | unsigned char op; /* operation, extra bits, table bits */ 26 | unsigned char bits; /* bits in this part of the code */ 27 | unsigned short val; /* offset in table or code value */ 28 | } code; 29 | 30 | /* op values as set by inflate_table(): 31 | 00000000 - literal 32 | 0000tttt - table link, tttt != 0 is the number of table index bits 33 | 0001eeee - length or distance, eeee is the number of extra bits 34 | 01100000 - end of block 35 | 01000000 - invalid code 36 | */ 37 | 38 | /* Maximum size of the dynamic table. The maximum number of code structures is 39 | 1444, which is the sum of 852 for literal/length codes and 592 for distance 40 | codes. These values were found by exhaustive searches using the program 41 | examples/enough.c found in the zlib distribution. The arguments to that 42 | program are the number of symbols, the initial root table size, and the 43 | maximum bit length of a code. "enough 286 9 15" for literal/length codes 44 | returns 852, and "enough 30 6 15" for distance codes returns 592. The 45 | initial root table size (9 or 6) is found in the fifth argument of the 46 | inflate_table() calls in inflate.c and infback.c. If the root table size is 47 | changed, then these maximum sizes would be need to be recalculated and 48 | updated. */ 49 | #define ENOUGH_LENS 852 50 | #define ENOUGH_DISTS 592 51 | #define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS) 52 | 53 | /* Type of code to build for inflate_table() */ 54 | typedef enum { 55 | CODES, 56 | LENS, 57 | DISTS 58 | } codetype; 59 | 60 | int ZLIB_INTERNAL inflate_table(codetype type, unsigned short FAR *lens, 61 | unsigned codes, code FAR * FAR *table, 62 | unsigned FAR *bits, unsigned short FAR *work); 63 | -------------------------------------------------------------------------------- /scripts/opengnb@.service: -------------------------------------------------------------------------------- 1 | ######################### 2 | #(1)发行版软件包应该将此单元模板应放置在 /usr/lib/systemd/system/ 目录中 3 | #(2)可以使用例如 systemctl start|stop opengnb@xxx.service 这样的命令启动/停止"xxx"服务实例("xxx"表示使用"/etc/opengnb/xxx"配置目录) 4 | #(3)可以使用例如 systemctl enable|disable opengnb@xxx.service 这样的命令开启/关闭"xxx"服务实例的自动启动 5 | # 上述命令等价于创建/删除 /etc/systemd/system/multi-user.target.wants/opengnb@xxx.service 软连接(指向 /usr/lib/systemd/system/opengnb@.service ) 6 | #(4)配置目录的名称(也就是"/etc/opengnb/xxx"中的"xxx")只能使用ASCII字母/数字/下划线(也就是只能使用 "a-z" "A-Z" "0-9" "_" 字符) 7 | ######################### 8 | 9 | [Unit] 10 | Description="GNB NAT Traversal P2P VPN" 11 | Documentation=https://github.com/gnbdev/opengnb/blob/master/docs/gnb_config_manual_cn.md \ 12 | https://github.com/gnbdev/opengnb/blob/master/docs/gnb_user_manual_en.md \ 13 | https://github.com/gnbdev/opengnb/blob/master/README_EN.md \ 14 | https://github.com/gnbdev/opengnb/blob/master/README.md 15 | #开机时,确保在网络接通之后才会启动 16 | Wants=network-online.target 17 | After=network-online.target 18 | #开机时,确保在DNS解析就绪之后才会启动 19 | #Wants=nss-lookup.target 20 | #After=nss-lookup.target 21 | #关机时,确保在关闭网络之前已经停止 22 | After=network.target 23 | 24 | [Service] 25 | #为兼容 systemd 老版本所做的妥协(v240 以上版本建议设置为 Type=exec ) 26 | Type=simple 27 | PIDFile=/run/opengnb-%I.pid 28 | ExecStartPre=/bin/mkdir -p /var/log/opengnb/%I 29 | ExecStartPre=/bin/mkdir -p /var/cache/opengnb 30 | ExecStart=/usr/sbin/gnb --systemd --conf=/etc/opengnb/%I --pid-file=/run/opengnb-%I.pid --ctl-block=/var/cache/opengnb/%I.map --node-cache-file=/var/cache/opengnb/%I.dump --log-file-path=/var/log/opengnb/%I --ifname=gnb_tun_%I 31 | 32 | [Install] 33 | WantedBy=multi-user.target 34 | #默认实例名称(仅当 systemctl enable 命令没有指定实例名称时有意义) 35 | DefaultInstance=default 36 | -------------------------------------------------------------------------------- /src/cli/gnb_crypto.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include 27 | #include 28 | 29 | #include "gnb.h" 30 | #include "gnb_keys.h" 31 | 32 | #include "gnb_binary.h" 33 | #include "crypto/random/gnb_random.h" 34 | #include "ed25519/ed25519.h" 35 | 36 | 37 | #ifndef GNB_SKIP_BUILD_TIME 38 | #define GNB_BUILD_STRING "Build Time ["__DATE__","__TIME__"]" 39 | #else 40 | #define GNB_BUILD_STRING "Build Time [Hidden]" 41 | #endif 42 | 43 | 44 | static void show_useage(int argc,char *argv[]){ 45 | 46 | printf("%s\n", GNB_BUILD_STRING); 47 | 48 | printf("usage: %s -c -p private_key_file -k public_key_file\n",argv[0]); 49 | printf("example:\n"); 50 | printf("%s -c -p 1001.private -k 1001.public\n",argv[0]); 51 | 52 | } 53 | 54 | static void create_keypair(gnb_uuid_t uuid64, const char *private_key_file, const char *public_key_file){ 55 | 56 | int private_file_fd; 57 | int public_file_fd; 58 | 59 | unsigned char seed[32]; 60 | 61 | unsigned char ed25519_private_key[64]; 62 | unsigned char ed25519_public_key[32]; 63 | 64 | char hex_string[128]; 65 | 66 | void *p; 67 | 68 | ssize_t rlen; 69 | ssize_t wlen; 70 | gnb_random_data(seed, 32); 71 | 72 | ed25519_create_keypair(ed25519_public_key, ed25519_private_key, (const unsigned char *)seed); 73 | 74 | gnb_bin2hex(ed25519_public_key, 32, hex_string); 75 | 76 | public_file_fd = open(public_key_file, O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR); 77 | 78 | if ( -1 == public_file_fd ) { 79 | perror("create public key file"); 80 | exit(0); 81 | } 82 | 83 | wlen = write(public_file_fd,hex_string,64); 84 | 85 | if ( -1 == wlen ) { 86 | perror("write public key file"); 87 | } 88 | 89 | close(public_file_fd); 90 | 91 | gnb_bin2hex(ed25519_private_key, 64, hex_string); 92 | 93 | private_file_fd = open(private_key_file, O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR); 94 | 95 | if ( -1 == private_file_fd ) { 96 | perror("create private key file"); 97 | exit(0); 98 | } 99 | 100 | wlen = write(private_file_fd,hex_string,128); 101 | 102 | if ( -1 == wlen ) { 103 | perror("write private key file"); 104 | } 105 | 106 | close(private_file_fd); 107 | 108 | } 109 | 110 | 111 | int main (int argc,char *argv[]){ 112 | 113 | static struct option long_options[] = { 114 | { "create", required_argument, 0, 'c' }, 115 | { "private_key", required_argument, 0, 'p' }, 116 | { "public_key", required_argument, 0, 'k' }, 117 | { 0, 0, 0, 0 } 118 | }; 119 | 120 | int opt; 121 | 122 | gnb_uuid_t uuid64 = 0; 123 | 124 | char *public_key_file = NULL; 125 | char *private_key_file = NULL; 126 | 127 | char *cmd = NULL; 128 | 129 | while (1) { 130 | 131 | int option_index = 0; 132 | 133 | opt = getopt_long (argc, argv, "ck:p:h",long_options, &option_index); 134 | 135 | if ( -1 == opt ) { 136 | break; 137 | } 138 | 139 | switch (opt) { 140 | 141 | case 'c': 142 | cmd = "create"; 143 | break; 144 | 145 | case 'p': 146 | private_key_file = optarg; 147 | break; 148 | 149 | case 'k': 150 | public_key_file = optarg; 151 | break; 152 | 153 | case 'h': 154 | break; 155 | 156 | default: 157 | break; 158 | } 159 | 160 | } 161 | 162 | if ( NULL==cmd || NULL==private_key_file || NULL==public_key_file ) { 163 | show_useage(argc, argv); 164 | exit(0); 165 | } 166 | 167 | create_keypair(uuid64, private_key_file, public_key_file); 168 | 169 | return 0; 170 | 171 | } 172 | -------------------------------------------------------------------------------- /src/crypto/arc4/arc4.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include "arc4.h" 19 | 20 | //参考来自这里的信息 21 | //https://en.wikipedia.org/wiki/RC4 22 | 23 | void arc4_init(struct arc4_sbox *sbox, unsigned char *key, unsigned int len) { 24 | 25 | register unsigned int x = 0; 26 | register unsigned int y = 0; 27 | 28 | sbox->x = 0; 29 | sbox->y = 0; 30 | 31 | for ( x=0; x<256; x++ ) { 32 | sbox->data[x] = x; 33 | } 34 | 35 | for ( x=0; x<256; x++ ) { 36 | 37 | y = ( y + sbox->data[x] + key[x%len]) % 256; 38 | 39 | sbox->data[x] ^= sbox->data[y]; 40 | sbox->data[y] ^= sbox->data[x]; 41 | sbox->data[x] ^= sbox->data[y]; 42 | 43 | } 44 | 45 | } 46 | 47 | void arc4_crypt(struct arc4_sbox *sbox, unsigned char*data, unsigned int len) { 48 | 49 | register unsigned int idx; 50 | register unsigned char k; 51 | 52 | register unsigned int x; 53 | register unsigned int y; 54 | 55 | x = sbox->x; 56 | y = sbox->y; 57 | 58 | for( idx=0; idxdata[x]) % 256; 62 | 63 | sbox->data[x] ^= sbox->data[y]; 64 | sbox->data[y] ^= sbox->data[x]; 65 | sbox->data[x] ^= sbox->data[y]; 66 | 67 | k = sbox->data[ ( sbox->data[x] + sbox->data[y] ) % 256]; 68 | 69 | data[idx] ^= k; 70 | 71 | } 72 | 73 | sbox->x = x; 74 | sbox->y = y; 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/crypto/arc4/arc4.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef ARC4_H 19 | #define ARC4_H 20 | 21 | struct arc4_sbox { 22 | unsigned int x; 23 | unsigned int y; 24 | unsigned char data[256]; 25 | }; 26 | 27 | 28 | void arc4_init(struct arc4_sbox *sbox, unsigned char *key, unsigned int len); 29 | void arc4_crypt(struct arc4_sbox *sbox, unsigned char*data, unsigned int len); 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /src/crypto/random/gnb_random.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include 19 | #include 20 | 21 | 22 | #ifdef _WIN32 23 | #include 24 | //unistd.h 定义了 _POSIX_THREAD_SAFE_FUNCTIONS 使得 localtime_r 有效 25 | #include 26 | #endif 27 | 28 | #include 29 | 30 | #include "gnb_random.h" 31 | #include "gnb_binary.h" 32 | 33 | static uint64_t comput_random_seed(){ 34 | 35 | int ret; 36 | 37 | struct timeval cur_time; 38 | 39 | ret = gettimeofday(&cur_time,NULL); 40 | 41 | if (0!=ret) { 42 | return 0; 43 | } 44 | 45 | return cur_time.tv_usec; 46 | 47 | } 48 | 49 | 50 | unsigned char *gnb_random_data(unsigned char *buffer, size_t buffer_size){ 51 | 52 | uint64_t seed_u64; 53 | 54 | seed_u64 = comput_random_seed(); 55 | 56 | int seed = (int)seed_u64; 57 | 58 | srand(seed); 59 | 60 | int r; 61 | int i; 62 | for (i = 0; i < buffer_size; i++) { 63 | r = rand(); 64 | buffer[i] = r % 256; 65 | } 66 | 67 | return buffer; 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/crypto/random/gnb_random.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_RANDOM_H 19 | #define GNB_RANDOM_H 20 | 21 | #include 22 | #include 23 | 24 | unsigned char *gnb_random_data(unsigned char *buffer, size_t buffer_size); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /src/crypto/xor/xor.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | void xor_crypto(unsigned char *crypto_key, unsigned char *data, unsigned int len) { 19 | 20 | int i; 21 | int j = 0; 22 | 23 | for ( i=0; i= 64 ) { 32 | j = 0; 33 | } 34 | 35 | } 36 | 37 | } 38 | 39 | 40 | void xor_crypto_copy(unsigned char *crypto_key, unsigned char *dest, unsigned char *src, unsigned int len) { 41 | 42 | int i; 43 | int j = 0; 44 | 45 | for ( i=0; i= 64 ) { 55 | j = 0; 56 | } 57 | 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/crypto/xor/xor.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef XOR_H 19 | #define XOR_H 20 | 21 | void xor_crypto(unsigned char *crypto_key, unsigned char *data, unsigned int len); 22 | void xor_crypto_copy(unsigned char *crypto_key, unsigned char *dest, unsigned char *src, unsigned int len); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /src/es/gnb_es_platform.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | #ifdef _WIN32 23 | #include 24 | #include 25 | #endif 26 | 27 | #include "gnb_es_type.h" 28 | 29 | #ifdef _WIN32 30 | 31 | static void windows_if_up(gnb_es_ctx *es_ctx){ 32 | 33 | gnb_conf_t *conf; 34 | gnb_ctl_core_zone_t *core_zone; 35 | 36 | conf = &es_ctx->ctl_block->conf_zone->conf_st; 37 | core_zone = es_ctx->ctl_block->core_zone; 38 | 39 | char cmd[1024]; 40 | 41 | char *devicename; 42 | char *if_device_string; 43 | 44 | devicename = core_zone->ifname; 45 | if_device_string = core_zone->if_device_string; 46 | 47 | //修改tun设备的MTU 48 | snprintf(cmd,1024,"netsh interface ipv4 set subinterface interface=\"%s\" mtu=%d store=active >NUL 2>&1", devicename, conf->mtu); 49 | system(cmd); 50 | 51 | } 52 | 53 | 54 | static void windows_if_down(gnb_es_ctx *es_ctx){ 55 | 56 | 57 | } 58 | 59 | #endif 60 | 61 | 62 | void gnb_es_if_up(gnb_es_ctx *es_ctx){ 63 | 64 | #ifdef _WIN32 65 | windows_if_up(es_ctx); 66 | #endif 67 | 68 | } 69 | 70 | 71 | void gnb_es_if_down(gnb_es_ctx *es_ctx){ 72 | 73 | #ifdef _WIN32 74 | windows_if_down(es_ctx); 75 | #endif 76 | 77 | } 78 | 79 | void gnb_es_if_loop(gnb_es_ctx *es_ctx){ 80 | 81 | } 82 | -------------------------------------------------------------------------------- /src/es/gnb_es_type.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef gnb_es_type_h 19 | #define gnb_es_type_h 20 | 21 | #if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__) || defined(__OpenBSD__) 22 | #define __UNIX_LIKE_OS__ 1 23 | #endif 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | #include "gnb_alloc.h" 30 | #include "gnb_conf_type.h" 31 | #include "gnb_ctl_block.h" 32 | #include "gnb_hash32.h" 33 | #include "gnb_node_type.h" 34 | #include "gnb_worker_type.h" 35 | #include "gnb_log.h" 36 | 37 | 38 | typedef struct _gnb_es_ctx{ 39 | 40 | gnb_heap_t *heap; 41 | 42 | struct timeval now_timeval; 43 | 44 | uint64_t now_time_sec; 45 | uint64_t now_time_usec; 46 | 47 | int udp_socket4; 48 | int udp_socket6; 49 | 50 | int udp_discover_recv_socket4; 51 | 52 | gnb_ctl_block_t *ctl_block; 53 | 54 | gnb_hash32_map_t *uuid_node_map; 55 | 56 | gnb_node_t *local_node; 57 | 58 | gnb_worker_t *discover_in_lan_worker; 59 | 60 | char *pid_file; 61 | 62 | char *wan_address6_file; 63 | 64 | int service_opt; 65 | 66 | int upnp_opt; 67 | char *upnp_multicase_if; 68 | char *upnp_gateway4; 69 | 70 | int resolv_opt; 71 | 72 | int broadcast_address_opt; 73 | int discover_in_lan_opt; 74 | int dump_address_opt; 75 | 76 | int if_up_opt; 77 | int if_down_opt; 78 | int if_loop_opt; 79 | 80 | gnb_log_ctx_t *log; 81 | 82 | int daemon; 83 | 84 | }gnb_es_ctx; 85 | 86 | 87 | #define GNB_LOG_ID_ES_CORE 0 88 | #define GNB_LOG_ID_ES_UPNP 1 89 | #define GNB_LOG_ID_ES_RESOLV 2 90 | #define GNB_LOG_ID_ES_BROADCAST 3 91 | #define GNB_LOG_ID_ES_DISCOVER_IN_LAN 4 92 | 93 | 94 | #define DISCOVER_LAN_IN_BROADCAST_PORT 8998 95 | 96 | #endif 97 | -------------------------------------------------------------------------------- /src/gnb_address_type.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_ADDRESS_TYPE_H 19 | #define GNB_ADDRESS_TYPE_H 20 | 21 | #include "gnb_platform.h" 22 | 23 | #ifdef __UNIX_LIKE_OS__ 24 | #include 25 | #include 26 | #endif 27 | 28 | 29 | #ifdef _WIN32 30 | #include 31 | #include 32 | #endif 33 | 34 | #include 35 | #include 36 | 37 | #define GNB_IP6_PORT_STRING_SIZE ( 46 + 2 + sizeof("65535") + 1 ) 38 | #define GNB_IP4_PORT_STRING_SIZE ( 16 + 1 + sizeof("65535") + 1 ) 39 | 40 | #pragma pack(push, 1) 41 | typedef struct _gnb_address_t{ 42 | 43 | int type; //AF_INET AF_INET6 44 | 45 | //更新该地址的 socket 索引 46 | uint8_t socket_idx; 47 | 48 | //最后更新时间 49 | uint64_t ts_sec; 50 | 51 | //延时,经过 ping pong 后算出 52 | uint64_t latency_usec; 53 | union{ 54 | uint8_t addr4[4]; 55 | uint8_t addr6[16]; 56 | }address; 57 | 58 | #define m_address4 address.addr4 59 | #define m_address6 address.addr6 60 | 61 | //网络字节序 62 | uint16_t port; 63 | 64 | }gnb_address_t; 65 | #pragma pack(pop) 66 | 67 | 68 | typedef struct _gnb_address_list_t{ 69 | 70 | size_t size; 71 | size_t num; 72 | uint64_t update_sec; 73 | gnb_address_t array[0]; 74 | 75 | }gnb_address_list_t; 76 | 77 | 78 | #define GNB_MAX_ADDR_RING 128 79 | 80 | typedef struct _gnb_address_ring_t{ 81 | 82 | int cur_index; 83 | 84 | gnb_address_list_t *address_list; 85 | 86 | }gnb_address_ring_t; 87 | 88 | 89 | typedef struct _gnb_sockaddress_t{ 90 | 91 | int addr_type; // AF_INET AF_INET6 92 | int protocol; //SOCK_STREAM SOCK_DGRAM 93 | 94 | union{ 95 | struct sockaddr_in in; 96 | struct sockaddr_in6 in6; 97 | }addr; 98 | #define m_in4 addr.in 99 | #define m_in6 addr.in6 100 | 101 | //在确定 addr_type 是 AF_INET 或 AF_INET6 后,就能确定这个长度了,可以去掉这个成员 102 | socklen_t socklen; 103 | 104 | }gnb_sockaddress_t; 105 | 106 | 107 | #endif 108 | 109 | -------------------------------------------------------------------------------- /src/gnb_alloc.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_ALLOC_H 19 | #define GNB_ALLOC_H 20 | 21 | #include 22 | #include 23 | 24 | typedef struct _gnb_heap_fragment_t gnb_heap_fragment_t; 25 | 26 | typedef struct _gnb_heap_t{ 27 | 28 | uint32_t max_fragment; 29 | uint32_t fragment_nums; 30 | uint32_t alloc_byte; 31 | uint32_t ralloc_byte; 32 | gnb_heap_fragment_t *fragment_list[0]; 33 | 34 | }gnb_heap_t; 35 | 36 | gnb_heap_t* gnb_heap_create(uint32_t max_fragment); 37 | 38 | void* gnb_heap_alloc(gnb_heap_t *gnb_heap, uint32_t size); 39 | 40 | void gnb_heap_free(gnb_heap_t *gnb_heap, void *p); 41 | 42 | void gnb_heap_clean(gnb_heap_t *gnb_heap); 43 | 44 | void gnb_heap_release(gnb_heap_t *gnb_heap); 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /src/gnb_arg_list.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_ARG_LIST_H 19 | #define GNB_ARG_LIST_H 20 | 21 | #include 22 | 23 | #define GNB_ARG_STRING_MAX_SIZE 1024*4 24 | #define GNB_ARG_MAX_SIZE 1024 25 | 26 | typedef struct _gnb_arg_list_t { 27 | 28 | size_t size; 29 | 30 | int argc; 31 | 32 | char *argv[0]; 33 | 34 | }gnb_arg_list_t; 35 | 36 | 37 | gnb_arg_list_t *gnb_arg_list_init(int size); 38 | 39 | void gnb_arg_list_release(gnb_arg_list_t *arg_list); 40 | 41 | int gnb_arg_append(gnb_arg_list_t *arg_list,const char *arg); 42 | 43 | int gnb_arg_list_to_string(gnb_arg_list_t *arg_list, char *string, size_t string_len); 44 | 45 | gnb_arg_list_t *gnb_arg_string_to_list(char *string, int num); 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /src/gnb_binary.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_BINARY_H 19 | #define GNB_BINARY_H 20 | 21 | #include 22 | 23 | #define FE_BIN2HEX_LOWERCASE 0 24 | #define FE_BIN2HEX_CAPITALS 1 25 | 26 | char *gnb_bin2hex_case_string(void *bin, size_t bin_size, char *hex_string, int letter_case); 27 | char *gnb_bin2hex_string(void *bin, size_t bin_size, char *hex_string); 28 | 29 | 30 | char *gnb_bin2hex_case(void *bin, size_t bin_size, char *hex_string, int letter_case); 31 | char *gnb_bin2hex(void *bin, size_t bin_size, char *hex_string); 32 | 33 | void *gnb_hex2bin(char *hex_string, void *bin, size_t bin_size); 34 | 35 | 36 | typedef struct _gnb_hex_string8_t{ 37 | char value[8+1]; 38 | }gnb_hex_string8_t; 39 | 40 | 41 | typedef struct _gnb_hex_string16_t{ 42 | char value[16+1]; 43 | }gnb_hex_string16_t; 44 | 45 | 46 | typedef struct _gnb_hex_string32_t{ 47 | char value[32+1]; 48 | }gnb_hex_string32_t; 49 | 50 | 51 | typedef struct _gnb_hex_string64_t{ 52 | char value[64+1]; 53 | }gnb_hex_string64_t; 54 | 55 | 56 | typedef struct _gnb_hex_string128_t{ 57 | char value[128+1]; 58 | }gnb_hex_string128_t; 59 | 60 | 61 | typedef struct _gnb_hex_string256_t{ 62 | char value[256+1]; 63 | }gnb_hex_string256_t; 64 | 65 | 66 | char * gnb_get_hex_string8(void *byte4, char *dest); 67 | char * gnb_get_hex_string16(void *byte8, char *dest); 68 | char * gnb_get_hex_string32(void *byte16, char *dest); 69 | char * gnb_get_hex_string64(void *byte32, char *dest); 70 | char * gnb_get_hex_string128(void *byte64, char *dest); 71 | char * gnb_get_hex_string256(void *byte128, char *dest); 72 | 73 | 74 | static char gnb_hex1_string256[256+1]; 75 | static char gnb_hex2_string256[256+1]; 76 | 77 | #define GNB_HEX1_BYTE8(bytes) gnb_get_hex_string8(bytes, gnb_hex1_string256) 78 | #define GNB_HEX1_BYTE16(bytes) gnb_get_hex_string16(bytes, gnb_hex1_string256) 79 | #define GNB_HEX1_BYTE32(bytes) gnb_get_hex_string32(bytes, gnb_hex1_string256) 80 | #define GNB_HEX1_BYTE64(bytes) gnb_get_hex_string64(bytes, gnb_hex1_string256) 81 | #define GNB_HEX1_BYTE128(bytes) gnb_get_hex_string128(bytes, gnb_hex1_string256) 82 | #define GNB_HEX1_BYTE256(bytes) gnb_get_hex_string256(bytes, gnb_hex1_string256) 83 | 84 | #define GNB_HEX2_BYTE8(bytes) gnb_get_hex_string8(bytes, gnb_hex2_string256) 85 | #define GNB_HEX2_BYTE16(bytes) gnb_get_hex_string16(bytes, gnb_hex2_string256) 86 | #define GNB_HEX2_BYTE32(bytes) gnb_get_hex_string32(bytes, gnb_hex2_string256) 87 | #define GNB_HEX2_BYTE64(bytes) gnb_get_hex_string64(bytes, gnb_hex2_string256) 88 | #define GNB_HEX2_BYTE128(bytes) gnb_get_hex_string128(bytes, gnb_hex2_string256) 89 | #define GNB_HEX2_BYTE256(bytes) gnb_get_hex_string256(bytes, gnb_hex2_string256) 90 | 91 | #endif 92 | -------------------------------------------------------------------------------- /src/gnb_block.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_BLOCK_H 19 | #define GNB_BLOCK_H 20 | 21 | #include 22 | 23 | 24 | typedef struct _gnb_block32_t { 25 | uint32_t size; 26 | unsigned char data[0]; 27 | }gnb_block32_t; 28 | 29 | 30 | #define GNB_BLOCK_VOID(block) *(void **)&block->data 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /src/gnb_conf.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | #include "gnb_conf_type.h" 23 | #include "gnb.h" 24 | 25 | 26 | char * check_domain_name(char *host_string){ 27 | 28 | if ( NULL != strchr(host_string, ':') ) { 29 | return NULL; 30 | } 31 | 32 | int i; 33 | 34 | for( i=0; i= 'a' && host_string[i] <= 'z' ) { 45 | return host_string; 46 | } 47 | 48 | } 49 | 50 | return NULL; 51 | } 52 | 53 | /*判断 配置行第二列是ip地址 还是node id*/ 54 | char * check_node_route(char *config_line_string){ 55 | 56 | if ( NULL != strchr(config_line_string, ':') || NULL != strchr(config_line_string, '.') ) { 57 | return NULL; 58 | } 59 | 60 | return config_line_string; 61 | 62 | } 63 | 64 | 65 | int gnb_test_field_separator(char *config_string){ 66 | 67 | int i; 68 | 69 | for ( i=0; i. 16 | */ 17 | 18 | #ifndef GNB_CONF_FILE_H 19 | #define GNB_CONF_FILE_H 20 | 21 | #include "gnb.h" 22 | 23 | 24 | void local_node_file_config(gnb_conf_t *conf); 25 | 26 | void gnb_config_safe(gnb_core_t *gnb_core); 27 | 28 | size_t gnb_get_node_num_from_file(gnb_conf_t *conf); 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /src/gnb_config_lite.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_CONF_LITE_H 19 | #define GNB_CONF_LITE_H 20 | 21 | #include "gnb.h" 22 | 23 | void gnb_config_lite(gnb_core_t *gnb_core); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /src/gnb_core.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_CORE_H 19 | #define GNB_CORE_H 20 | 21 | #include "gnb.h" 22 | 23 | gnb_core_t* gnb_core_create(gnb_conf_t *conf); 24 | 25 | void gnb_core_start(gnb_core_t *gnb_core); 26 | 27 | void gnb_core_stop(gnb_core_t *gnb_core); 28 | 29 | gnb_core_t * gnb_core_index_service_create(gnb_conf_t *conf); 30 | 31 | void gnb_core_index_service_start(gnb_core_t *gnb_core); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /src/gnb_core_frame_type_defs.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_CORE_FRAME_TYPE_DEFS_H 19 | #define GNB_CORE_FRAME_TYPE_DEFS_H 20 | 21 | #define GNB_PAYLOAD_TYPE_UR0 (0x3) 22 | 23 | 24 | #define GNB_PAYLOAD_TYPE_IPFRAME (0x4) 25 | #define GNB_PAYLOAD_SUB_TYPE_IPFRAME_INIT (0x0) 26 | #define GNB_PAYLOAD_SUB_TYPE_IPFRAME_STD (0x1) 27 | #define GNB_PAYLOAD_SUB_TYPE_IPFRAME_RELAY (0x1 << 1) 28 | #define GNB_PAYLOAD_SUB_TYPE_IPFRAME_UNIFIED (0x1 << 2) 29 | #define GNB_PAYLOAD_SUB_TYPE_IPFRAME_UNIFIED_MULTI_PATH (0x1 << 3) 30 | #define GNB_PAYLOAD_SUB_TYPE_IPFRAME_ZIP (0x1 << 4) 31 | 32 | 33 | #define GNB_PAYLOAD_TYPE_INDEX (0x8) 34 | #define PAYLOAD_SUB_TYPE_POST_ADDR (0x1) 35 | #define PAYLOAD_SUB_TYPE_ECHO_ADDR (0x2) 36 | #define PAYLOAD_SUB_TYPE_REQUEST_ADDR (0x3) 37 | #define PAYLOAD_SUB_TYPE_PUSH_ADDR (0x4) 38 | #define PAYLOAD_SUB_TYPE_DETECT_ADDR (0x5) 39 | 40 | 41 | #define GNB_PAYLOAD_TYPE_NODE (0x9) 42 | #define PAYLOAD_SUB_TYPE_PING (0x1) 43 | #define PAYLOAD_SUB_TYPE_PONG (0x2) 44 | #define PAYLOAD_SUB_TYPE_PONG2 (0x3) 45 | #define PAYLOAD_SUB_TYPE_LAN_PING (0x4) 46 | #define PAYLOAD_SUB_TYPE_NODE_UNIFIED_NOTIFY (0x5) 47 | 48 | 49 | #define GNB_PAYLOAD_TYPE_LAN_DISCOVER (0x43) 50 | 51 | 52 | #define GNB_PAYLOAD_TYPE_UDPLOG (0x44) 53 | #define GNB_ES_PAYLOAD_TYPE_UDPLOG (0x45) 54 | 55 | 56 | #define GNB_PAYLOAD_TYPE_UR1 (0x46) 57 | 58 | #define GNB_PAYLOAD_SUB_TYPE_UR1_UNSET (0x0) 59 | 60 | //IPV6:bit=0,IPV4:bit=1 61 | #define GNB_PAYLOAD_SUB_TYPE_UR1_SRC_ADDRESS6 (0x0) 62 | #define GNB_PAYLOAD_SUB_TYPE_UR1_SRC_ADDRESS4 (0x1) 63 | 64 | //IPV6:bit=0,IPV4:bit=1 65 | #define GNB_PAYLOAD_SUB_TYPE_UR1_DST_ADDRESS6 (0x0) 66 | #define GNB_PAYLOAD_SUB_TYPE_UR1_DST_ADDRESS4 (0x1 << 1) 67 | 68 | //不转发header:bit=0,转发header:bit=1 69 | #define GNB_PAYLOAD_SUB_TYPE_UR1_NOHEADER (0x0) 70 | #define GNB_PAYLOAD_SUB_TYPE_UR1_HEADER (0x1 << 2) 71 | 72 | #define GNB_PAYLOAD_SUB_TYPE_UR1_STD (0x1 << 3) 73 | #define GNB_PAYLOAD_SUB_TYPE_UR1_UNIFIED (0x1 << 4) 74 | 75 | 76 | #define ED25519_SIGN_SIZE 64 77 | 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /src/gnb_ctl_block.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_CTL_BLOCK_H 19 | #define GNB_CTL_BLOCK_H 20 | 21 | #include 22 | 23 | #include "gnb_mmap.h" 24 | #include "gnb_payload16.h" 25 | #include "gnb_conf_type.h" 26 | #include "gnb_node_type.h" 27 | #include "gnb_log_type.h" 28 | 29 | 30 | #define GNB_MAX_PAYLOAD_BLOCK_SIZE 1024*64 31 | 32 | #define GNB_PAYLOAD_BUFFER_PADDING_SIZE 1024 33 | 34 | #define CTL_BLOCK_ES_MAGIC_IDX 3 35 | #define CTL_BLOCK_VT_MAGIC_IDX 4 36 | 37 | typedef struct _gnb_ctl_magic_number_t { 38 | 39 | unsigned char data[16]; 40 | 41 | }gnb_ctl_magic_number_t; 42 | 43 | 44 | typedef struct _gnb_ctl_conf_zone_t { 45 | 46 | unsigned char name[8]; 47 | gnb_conf_t conf_st; 48 | 49 | }gnb_ctl_conf_zone_t; 50 | 51 | 52 | typedef struct _gnb_ctl_core_zone_t { 53 | 54 | unsigned char name[8]; 55 | 56 | gnb_uuid_t local_uuid; 57 | 58 | unsigned char ed25519_private_key[64]; 59 | unsigned char ed25519_public_key[32]; 60 | 61 | uint32_t tun_if_id; 62 | unsigned char ifname[256]; 63 | unsigned char if_device_string[256]; 64 | 65 | unsigned char wan6_addr[16]; 66 | uint16_t wan6_port; //网络字节序 67 | 68 | unsigned char wan4_addr[4]; 69 | uint16_t wan4_port; //网络字节序 70 | 71 | gnb_log_ctx_t log_ctx_st; 72 | 73 | unsigned char index_address_block[ sizeof(gnb_address_list_t) + sizeof(gnb_address_t) * GNB_MAX_ADDR_RING ]; 74 | 75 | unsigned char ufwd_address_block[ sizeof(gnb_address_list_t) + sizeof(gnb_address_t) * 16 ]; 76 | 77 | unsigned char tun_payload_block[ GNB_PAYLOAD_BUFFER_PADDING_SIZE + sizeof(gnb_payload16_t) + GNB_MAX_PAYLOAD_BLOCK_SIZE ]; 78 | unsigned char inet_payload_block[ GNB_PAYLOAD_BUFFER_PADDING_SIZE + sizeof(gnb_payload16_t) + GNB_MAX_PAYLOAD_BLOCK_SIZE ]; 79 | 80 | unsigned char pf_worker_payload_blocks[0]; 81 | 82 | }gnb_ctl_core_zone_t; 83 | 84 | 85 | typedef struct _gnb_ctl_status_zone_t { 86 | 87 | uint64_t keep_alive_ts_sec; 88 | 89 | }gnb_ctl_status_zone_t; 90 | 91 | 92 | typedef struct _gnb_ctl_node_zone_t { 93 | 94 | unsigned char name[8]; 95 | int node_num; 96 | gnb_node_t node[0]; 97 | 98 | }gnb_ctl_node_zone_t; 99 | 100 | 101 | typedef struct _gnb_ctl_block_t { 102 | 103 | uint32_t *entry_table256; 104 | 105 | gnb_ctl_magic_number_t *magic_number; 106 | 107 | gnb_ctl_conf_zone_t *conf_zone; 108 | 109 | gnb_ctl_core_zone_t *core_zone; 110 | 111 | gnb_ctl_status_zone_t *status_zone; 112 | 113 | gnb_ctl_node_zone_t *node_zone; 114 | 115 | gnb_mmap_block_t *mmap_block; 116 | 117 | }gnb_ctl_block_t; 118 | 119 | 120 | ssize_t gnb_ctl_file_size(const char *filename); 121 | 122 | 123 | gnb_ctl_block_t *gnb_ctl_block_build(void *memory, uint32_t payload_block_size, size_t node_num, uint8_t pf_worker_num); 124 | 125 | void gnb_ctl_block_build_finish(void *memory); 126 | 127 | void gnb_ctl_block_setup(gnb_ctl_block_t *ctl_block, void *memory); 128 | 129 | gnb_ctl_block_t *gnb_get_ctl_block(const char *ctl_block_file, int flag); 130 | 131 | #define MIN_CTL_BLOCK_FILE_SIZE (sizeof(uint32_t)*256 + sizeof(gnb_ctl_magic_number_t) + sizeof(gnb_ctl_conf_zone_t) + sizeof(gnb_ctl_core_zone_t) + sizeof(gnb_ctl_status_zone_t) + sizeof(gnb_ctl_node_zone_t) + sizeof(gnb_node_t)) 132 | 133 | #define GNB_CTL_KEEP_ALIVE_TS 15 134 | 135 | #endif 136 | -------------------------------------------------------------------------------- /src/gnb_ctl_block_set.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "gnb_conf_type.h" 24 | #include "gnb_node_type.h" 25 | #include "gnb_address.h" 26 | #include "gnb_binary.h" 27 | #include "gnb_time.h" 28 | #include "gnb_udp.h" 29 | #include "gnb_ctl_block.h" 30 | #include "ed25519/sha512.h" 31 | 32 | 33 | #ifdef _WIN32 34 | #include 35 | #include 36 | #include 37 | #endif 38 | 39 | -------------------------------------------------------------------------------- /src/gnb_daemon.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "gnb_platform.h" 24 | 25 | #ifdef __UNIX_LIKE_OS__ 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #endif 33 | 34 | 35 | #ifdef __UNIX_LIKE_OS__ 36 | 37 | int gnb_daemon(){ 38 | 39 | int ret; 40 | 41 | int fd; 42 | 43 | pid_t pid; 44 | 45 | pid = fork(); 46 | 47 | if ( 0 != pid ) { 48 | exit(0); 49 | } 50 | 51 | setsid(); 52 | 53 | signal(SIGHUP,SIG_IGN); 54 | 55 | pid = fork(); 56 | 57 | if ( 0 != pid ) { 58 | exit(0); 59 | } 60 | 61 | ret = chdir("/"); 62 | 63 | umask(0); 64 | 65 | fd = open("/dev/null", O_RDWR); 66 | 67 | if ( -1 == fd ) { 68 | return -1; 69 | } 70 | 71 | ret = dup2(fd, STDIN_FILENO); 72 | 73 | if ( -1 == ret ) { 74 | return -2; 75 | } 76 | 77 | ret = dup2(fd, STDOUT_FILENO); 78 | 79 | if ( -1 == ret ) { 80 | return -3; 81 | } 82 | 83 | return 0; 84 | 85 | } 86 | 87 | 88 | void save_pid(const char *pid_file){ 89 | 90 | FILE *file; 91 | 92 | file = fopen(pid_file,"w"); 93 | 94 | if ( NULL==file ) { 95 | printf("open pid file[%s] err\n",pid_file); 96 | exit(1); 97 | } 98 | 99 | int pid = getpid(); 100 | 101 | fprintf(file,"%d",pid); 102 | 103 | fclose(file); 104 | 105 | } 106 | 107 | #endif 108 | -------------------------------------------------------------------------------- /src/gnb_dir.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2019 gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_DIR_H 19 | #define GNB_DIR_H 20 | 21 | #include 22 | #include 23 | 24 | char *gnb_get_file_dir(char *file_name, char *file_dir); 25 | char *gnb_get_file_dir_dup(char *file_name); 26 | char *gnb_realpath_dup(char *path); 27 | char *gnb_make_realpath_dup(char *base_path, char *sub_path); 28 | 29 | #define GNB_MAX_FILE_NAME_LEN 1024 30 | #define GNB_MAX_DIR_TOKEN_ARRAY_SIZE 1024 31 | 32 | typedef struct _gnb_file_info_t{ 33 | char *name; 34 | //绝对路径 35 | char *abs_name; 36 | 37 | #define GNB_FILE_TYPE_INIT 0 38 | #define GNB_FILE_TYPE_REG 1 39 | #define GNB_FILE_TYPE_DIR 2 40 | #define GNB_FILE_TYPE_LNK 3 41 | uint8_t type; 42 | }gnb_file_info_t; 43 | 44 | int gnb_get_dir_file_names(char *path, gnb_file_info_t **sub_file_info_lst, uint32_t *lst_len_ptr); 45 | /* 遍历目录下所有的目录和文件,文件/目录的数量 由 *lst_len_ptr 限制 */ 46 | int gnb_scan_dir(char *path, gnb_file_info_t **file_info_lst, uint32_t *lst_len_ptr); 47 | char *gnb_file_path_cut(char *filename, size_t len); 48 | char *gnb_file_path_dup(const char *filename, size_t len); 49 | 50 | /* 从传入的文件路径中得到文件的目录和文件名,通过 realpath(Unix 平台) 或 _fullpath (Windows平台) 获得文件目录的绝对路径,与文件名合并成一个字符串返回 */ 51 | char *gnb_realpath(char *in_path, char *resolved_path); 52 | int gnb_mkdirs(char *path,mode_t mode); 53 | int gnb_remove_dirs(char *path); 54 | void gnb_release_file_info_lst(gnb_file_info_t **file_info_lst, int lst_len); 55 | int gnb_mkdirs(char *path,mode_t mode); 56 | 57 | /*检查一个src文件或目录,是否在dst目录下,dst 必须是存在的目录*/ 58 | int gnb_inspect_in_directory(char *dst, char *src); 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /src/gnb_discover_in_lan_frame_type.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_DISCOVER_IN_LAN_FRAME_TYPE_H 19 | #define GNB_DISCOVER_IN_LAN_FRAME_TYPE_H 20 | 21 | #include "stdint.h" 22 | #include "gnb_core_frame_type_defs.h" 23 | 24 | #pragma pack(push, 1) 25 | 26 | typedef struct _discover_lan_in_frame_t { 27 | 28 | struct __attribute__((__packed__)) discover_lan_in_data { 29 | 30 | unsigned char arg0; 31 | unsigned char arg1; 32 | unsigned char arg2; 33 | unsigned char arg3; 34 | 35 | unsigned char src_key512[64]; 36 | gnb_uuid_t src_uuid64; 37 | 38 | uint8_t src_addr6_a[16]; 39 | uint16_t src_port6_a; 40 | 41 | uint8_t src_addr4[4]; 42 | uint16_t src_port4; 43 | 44 | uint64_t src_ts_usec; 45 | 46 | char text[256]; 47 | char attachment[128]; 48 | 49 | }data; 50 | 51 | unsigned char src_sign[ED25519_SIGN_SIZE]; 52 | 53 | }__attribute__ ((__packed__)) discover_lan_in_frame_t; 54 | 55 | #pragma pack(pop) 56 | 57 | #define PAYLOAD_DISCOVER_LAN_IN_FRAME_PAYLOAD_SIZE (sizeof(gnb_payload16_t) + sizeof(discover_lan_in_frame_t)) 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /src/gnb_doubly_linked_list.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_DOUBLY_LINKED_LIST_H 19 | #define GNB_DOUBLY_LINKED_LIST_H 20 | 21 | #include 22 | 23 | #include "gnb_alloc.h" 24 | 25 | typedef struct _gnb_doubly_linked_list_node_t gnb_doubly_linked_list_node_t; 26 | 27 | typedef struct _gnb_doubly_linked_list_t gnb_doubly_linked_list_t; 28 | 29 | typedef struct _gnb_doubly_linked_list_node_t{ 30 | 31 | gnb_doubly_linked_list_node_t *pre; 32 | 33 | gnb_doubly_linked_list_node_t *nex; 34 | 35 | void *data; 36 | 37 | }gnb_doubly_linked_list_node_t; 38 | 39 | 40 | typedef struct _gnb_doubly_linked_list_t{ 41 | 42 | gnb_heap_t *heap; 43 | 44 | gnb_doubly_linked_list_node_t *head; 45 | 46 | gnb_doubly_linked_list_node_t *tail; 47 | 48 | uint32_t num; 49 | 50 | }gnb_doubly_linked_list_t; 51 | 52 | 53 | void gnb_doubly_linked_list_node_set(gnb_doubly_linked_list_node_t *dl_node,void *data); 54 | 55 | 56 | gnb_doubly_linked_list_t* gnb_doubly_linked_list_create(gnb_heap_t *heap); 57 | void gnb_doubly_linked_list_release(gnb_doubly_linked_list_t *doubly_linked_list); 58 | 59 | 60 | int gnb_doubly_linked_list_add(gnb_doubly_linked_list_t *doubly_linked_list, gnb_doubly_linked_list_node_t *dl_node); 61 | 62 | gnb_doubly_linked_list_node_t* gnb_doubly_linked_list_pop_head(gnb_doubly_linked_list_t *doubly_linked_list); 63 | gnb_doubly_linked_list_node_t* gnb_doubly_linked_list_pop_tail(gnb_doubly_linked_list_t *doubly_linked_list); 64 | 65 | 66 | int gnb_doubly_linked_list_pop(gnb_doubly_linked_list_t *doubly_linked_list, gnb_doubly_linked_list_node_t *dl_node); 67 | int gnb_doubly_linked_list_move_head(gnb_doubly_linked_list_t *doubly_linked_list, gnb_doubly_linked_list_node_t *dl_node); 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /src/gnb_exec.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_EXEC_H 19 | #define GNB_EXEC_H 20 | 21 | #include "gnb_platform.h" 22 | 23 | #include "gnb_arg_list.h" 24 | 25 | #define GNB_EXEC_FOREGROUND (0x1) 26 | #define GNB_EXEC_BACKGROUND (0x1 << 1) 27 | #define GNB_EXEC_WAIT (0x1 << 2) 28 | 29 | #ifdef __UNIX_LIKE_OS__ 30 | 31 | #include 32 | #include 33 | 34 | pid_t gnb_exec(char *app_filename, char *current_path, gnb_arg_list_t *arg_list, int flag); 35 | void gnb_kill(pid_t pid); 36 | 37 | #endif 38 | 39 | 40 | 41 | #ifdef _WIN32 42 | 43 | #ifndef NAME_MAX 44 | #define NAME_MAX 255 45 | #endif 46 | 47 | #include 48 | 49 | int gnb_exec(char *app_filename, char *current_path, gnb_arg_list_t *gnb_arg_list, int flag); 50 | int gnb_kill(DWORD pid); 51 | 52 | #endif 53 | 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /src/gnb_fixed_pool.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include 19 | #include 20 | 21 | #include "gnb_fixed_pool.h" 22 | 23 | typedef struct _gnb_fixed_pool_t{ 24 | 25 | uint32_t num; 26 | 27 | uint32_t bsize; 28 | 29 | void *block; 30 | 31 | uint32_t array_len; 32 | 33 | void *array[0]; 34 | 35 | }gnb_fixed_pool_t; 36 | 37 | 38 | gnb_fixed_pool_t* gnb_fixed_pool_create(gnb_heap_t *heap, uint32_t array_len, uint32_t bsize){ 39 | 40 | gnb_fixed_pool_t *fixed_pool; 41 | 42 | fixed_pool = (gnb_fixed_pool_t *)gnb_heap_alloc(heap, sizeof(gnb_fixed_pool_t) + sizeof(void *) * array_len ); 43 | 44 | fixed_pool->num = array_len; 45 | fixed_pool->bsize = bsize; 46 | fixed_pool->block = gnb_heap_alloc(heap,bsize * array_len); 47 | 48 | memset(fixed_pool->block, 0, bsize * array_len); 49 | 50 | fixed_pool->array_len = array_len; 51 | 52 | int i; 53 | 54 | void *p; 55 | 56 | p = fixed_pool->block; 57 | 58 | for ( i=0; iarray[i] = p; 60 | p += bsize; 61 | } 62 | 63 | return fixed_pool; 64 | 65 | } 66 | 67 | 68 | void* gnb_fixed_pool_pop(gnb_fixed_pool_t *fixed_pool){ 69 | 70 | if ( 0 == fixed_pool->num ) { 71 | return NULL; 72 | } 73 | 74 | void *block; 75 | 76 | block = fixed_pool->array[fixed_pool->num-1]; 77 | fixed_pool->array[fixed_pool->num-1] = NULL; 78 | fixed_pool->num--; 79 | 80 | return block; 81 | 82 | } 83 | 84 | 85 | uint32_t gnb_fixed_pool_push(gnb_fixed_pool_t *fixed_pool, void *block){ 86 | 87 | if ( fixed_pool->array_len == fixed_pool->num ) { 88 | return 0; 89 | } 90 | 91 | if ( NULL != fixed_pool->array[fixed_pool->num] ) { 92 | return -1; 93 | } 94 | 95 | fixed_pool->array[fixed_pool->num] = block; 96 | fixed_pool->num++; 97 | 98 | return fixed_pool->num; 99 | 100 | } 101 | 102 | void gnb_fixed_pool_release(gnb_heap_t *heap,gnb_fixed_pool_t *fixed_pool){ 103 | gnb_heap_free(heap, fixed_pool->block); 104 | gnb_heap_free(heap, fixed_pool); 105 | } 106 | -------------------------------------------------------------------------------- /src/gnb_fixed_pool.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_FIXED_POOL_H 19 | #define GNB_FIXED_POOL_H 20 | 21 | #include 22 | 23 | #include "gnb_alloc.h" 24 | 25 | typedef struct _gnb_fixed_pool_t gnb_fixed_pool_t; 26 | 27 | gnb_fixed_pool_t* gnb_fixed_pool_create(gnb_heap_t *heap, uint32_t array_len, uint32_t bsize); 28 | 29 | void* gnb_fixed_pool_pop(gnb_fixed_pool_t *fixed_pool); 30 | 31 | uint32_t gnb_fixed_pool_push(gnb_fixed_pool_t *fixed_pool, void *block); 32 | 33 | void gnb_fixed_pool_release(gnb_heap_t *heap,gnb_fixed_pool_t *fixed_pool); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /src/gnb_keys.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_KEYS_H 19 | #define GNB_KEYS_H 20 | 21 | #include "gnb.h" 22 | 23 | int gnb_load_keypair(gnb_core_t *gnb_core); 24 | 25 | int gnb_load_public_key(gnb_core_t *gnb_core, gnb_uuid_t uuid64, unsigned char *public_key); 26 | 27 | void gnb_update_time_seed(gnb_core_t *gnb_core, uint64_t now_sec); 28 | 29 | int gnb_verify_seed_time(gnb_core_t *gnb_core, uint64_t now_sec); 30 | 31 | void gnb_build_crypto_key(gnb_core_t *gnb_core, gnb_node_t *node); 32 | 33 | void gnb_build_passcode(void *passcode_bin, char *string_in); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /src/gnb_log_type.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_LOG_TYPE_H 19 | #define GNB_LOG_TYPE_H 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | #ifdef _WIN32 26 | 27 | #ifndef NAME_MAX 28 | #define NAME_MAX 255 29 | #endif 30 | 31 | #endif 32 | 33 | typedef struct _gnb_log_config_t { 34 | 35 | char log_name[20]; 36 | 37 | uint8_t console_level; 38 | 39 | uint8_t file_level; 40 | 41 | uint8_t udp_level; 42 | 43 | }gnb_log_config_t; 44 | 45 | #define GNB_MAX_LOG_ID 128 46 | 47 | typedef struct _gnb_log_ctx_t { 48 | 49 | #define GNB_LOG_OUTPUT_NONE (0x0) 50 | #define GNB_LOG_OUTPUT_STDOUT (0x1) 51 | #define GNB_LOG_OUTPUT_FILE (0x1 << 1) 52 | #define GNB_LOG_OUTPUT_UDP (0x1 << 2) 53 | 54 | unsigned char output_type; 55 | 56 | char log_file_path[PATH_MAX]; 57 | 58 | //char log_file_prefix[32]; 59 | 60 | char log_file_name_std[PATH_MAX+NAME_MAX]; 61 | char log_file_name_debug[PATH_MAX+NAME_MAX]; 62 | char log_file_name_error[PATH_MAX+NAME_MAX]; 63 | 64 | 65 | int std_fd; 66 | int debug_fd; 67 | int error_fd; 68 | 69 | 70 | int pre_std_fd; 71 | int pre_debug_fd; 72 | int pre_error_fd; 73 | 74 | 75 | int pre_mday; 76 | 77 | uint8_t addr4[4]; 78 | uint16_t port4; 79 | 80 | uint8_t addr6[16]; 81 | uint16_t port6; 82 | 83 | int socket6_fd; 84 | int socket4_fd; 85 | 86 | 87 | #define GNB_LOG_UDP_TYPE_TEXT 0 88 | #define GNB_LOG_UDP_TYPE_BINARY 1 89 | uint8_t log_udp_type; 90 | 91 | //如果 log_udp_type 为 GNB_LOG_UDP_TYPE_BINARY,就需要提供gnb_payload的类型 92 | char log_payload_type; 93 | 94 | gnb_log_config_t config_table[GNB_MAX_LOG_ID]; 95 | 96 | }gnb_log_ctx_t; 97 | 98 | #endif 99 | -------------------------------------------------------------------------------- /src/gnb_mmap.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_MMAP_H 19 | #define GNB_MMAP_H 20 | 21 | #include 22 | 23 | typedef struct _gnb_mmap_block_t gnb_mmap_block_t; 24 | 25 | 26 | #define GNB_MMAP_TYPE_READONLY (0x0) 27 | #define GNB_MMAP_TYPE_READWRITE (0x1) 28 | #define GNB_MMAP_TYPE_CREATE (0x1 << 1) 29 | #define GNB_MMAP_TYPE_CLEANEXIT (0x1 << 2) 30 | 31 | 32 | gnb_mmap_block_t* gnb_mmap_create(const char *filename, size_t block_size, int mmap_type); 33 | 34 | void gnb_mmap_release(gnb_mmap_block_t *mmap_block); 35 | 36 | void* gnb_mmap_get_block(gnb_mmap_block_t *mmap_block); 37 | 38 | size_t gnb_mmap_get_size(gnb_mmap_block_t *mmap_block); 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /src/gnb_mod_register.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | /* 19 | 改名为 pf_register 20 | */ 21 | 22 | 23 | #include "gnb.h" 24 | #include "gnb_pf.h" 25 | 26 | 27 | extern gnb_pf_t gnb_pf_dump; 28 | extern gnb_pf_t gnb_pf_route; 29 | extern gnb_pf_t gnb_pf_crypto_xor; 30 | extern gnb_pf_t gnb_pf_crypto_arc4; 31 | extern gnb_pf_t gnb_pf_zip; 32 | 33 | 34 | gnb_pf_t *gnb_pf_mods[] = { 35 | &gnb_pf_dump, 36 | &gnb_pf_route, 37 | &gnb_pf_crypto_xor, 38 | &gnb_pf_crypto_arc4, 39 | &gnb_pf_zip, 40 | 0 41 | }; 42 | 43 | gnb_pf_t* gnb_find_pf_mod_by_name(const char *name){ 44 | 45 | int num = sizeof(gnb_pf_mods)/sizeof(gnb_pf_t *); 46 | 47 | int i; 48 | 49 | for ( i=0; iname,name,128) ) { 56 | return gnb_pf_mods[i]; 57 | } 58 | 59 | } 60 | 61 | return NULL; 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/gnb_node.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_NODE_H 19 | #define GNB_NODE_H 20 | 21 | #include "gnb.h" 22 | 23 | void gnb_init_node_key512(gnb_core_t *gnb_core); 24 | 25 | void gnb_add_forward_node_ring(gnb_core_t *gnb_core, gnb_uuid_t uuid64); 26 | void gnb_add_index_node_ring(gnb_core_t *gnb_core, gnb_uuid_t uuid64); 27 | gnb_node_t* gnb_select_forward_node(gnb_core_t *gnb_core); 28 | 29 | int gnb_node_sign_verify(gnb_core_t *gnb_core, gnb_uuid_t uuid64, unsigned char *sign, void *data, size_t data_size); 30 | 31 | void gnb_send_to_address(gnb_core_t *gnb_core, gnb_address_t *address, gnb_payload16_t *payload); 32 | void gnb_send_udata_to_address(gnb_core_t *gnb_core, gnb_address_t *address, void *udata, size_t udata_size); 33 | 34 | void gnb_send_address_list(gnb_core_t *gnb_core, gnb_address_list_t *address_list, gnb_payload16_t *payload); 35 | 36 | void gnb_send_to_address_through_all_sockets(gnb_core_t *gnb_core, gnb_address_t *address, gnb_payload16_t *payload, uint32_t interval_usec); 37 | void gnb_send_address_list_through_all_sockets(gnb_core_t *gnb_core, gnb_address_list_t *address_list, gnb_payload16_t *payload, uint32_t interval_usec); 38 | 39 | void gnb_send_available_address_list(gnb_core_t *gnb_core, gnb_address_list_t *address_list, gnb_payload16_t *payload, uint64_t now_sec); 40 | 41 | gnb_address_t* gnb_select_index_address(gnb_core_t *gnb_core, uint64_t now_sec); 42 | 43 | gnb_node_t* gnb_select_index_nodes(gnb_core_t *gnb_core); 44 | 45 | gnb_address_t* gnb_select_available_address4(gnb_core_t *gnb_core, gnb_node_t *node); 46 | 47 | int gnb_send_to_node(gnb_core_t *gnb_core, gnb_node_t *node, gnb_payload16_t *payload, unsigned char addr_type_bits); 48 | 49 | int gnb_p2p_forward_payload_to_node(gnb_core_t *gnb_core, gnb_node_t *node, gnb_payload16_t *payload); 50 | void gnb_std_uf_forward_payload_to_node(gnb_core_t *gnb_core, gnb_node_t *node, gnb_payload16_t *payload); 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /src/gnb_nodeid.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include "gnb_nodeid.h" 25 | 26 | gnb_uuid_t gnb_str2nodeid(char *nodeidstr){ 27 | 28 | gnb_uuid_t nodeid; 29 | char *endptr; 30 | char *p; 31 | int is_hex = 0; 32 | int i; 33 | 34 | p = nodeidstr; 35 | 36 | if ( '$' == *nodeidstr ) { 37 | is_hex = 1; 38 | p = nodeidstr+1; 39 | goto convert_begin; 40 | } 41 | 42 | if ( '0' == *nodeidstr && ( 'x' == *(nodeidstr+1) || 'X' == *(nodeidstr+1) ) ) { 43 | is_hex = 1; 44 | p = nodeidstr+2; 45 | goto convert_begin; 46 | } 47 | 48 | for ( i=0; i<16; i++ ) { 49 | 50 | if ( *p == '\0' ) { 51 | break; 52 | } 53 | 54 | if ( (*p >='A' && *p <='F') || (*p >='a' && *p <='f') ) { 55 | is_hex = 1; 56 | p = nodeidstr; 57 | goto convert_begin; 58 | } 59 | 60 | p++; 61 | 62 | } 63 | 64 | convert_begin: 65 | 66 | if ( is_hex ) { 67 | nodeid = strtoull(p, &endptr, 16); 68 | } else { 69 | nodeid = strtoull(nodeidstr, &endptr, 10); 70 | } 71 | 72 | if ( ERANGE == errno ) { 73 | nodeid = 0xFFFFFFFFFFFFFFFF; 74 | goto convert_end; 75 | } 76 | 77 | if ( endptr == p ) { 78 | nodeid = 0xFFFFFFFFFFFFFFFF; 79 | goto convert_end; 80 | } 81 | 82 | convert_end: 83 | 84 | return nodeid; 85 | 86 | } 87 | 88 | 89 | char *gnb_nodeid2str(gnb_uuid_t nodeid, char *nodeidstr, int fmt){ 90 | #if 0 91 | char buf[GNB_MAX_NODEID_STRING_SIZE]; 92 | 93 | switch (fmt) { 94 | case GNB_NODEIS2STR_FMT_DEC|GNB_NODEIS2STR_FMT_HEX: 95 | snprintf(nodeidstr, GNB_MAX_NODEID_STRING_SIZE, "%016"PRIX64"(%"PRIu64")", nodeid, nodeid); 96 | break; 97 | case GNB_NODEIS2STR_FMT_DEC: 98 | snprintf(nodeidstr, GNB_MAX_NODEID_STRING_SIZE, "%"PRIu64"", nodeid); 99 | break; 100 | case GNB_NODEIS2STR_FMT_HEX: 101 | snprintf(nodeidstr, GNB_MAX_NODEID_STRING_SIZE, "%016"PRIX64"", nodeid); 102 | break; 103 | default: 104 | break; 105 | 106 | } 107 | #endif 108 | return nodeidstr; 109 | 110 | } 111 | -------------------------------------------------------------------------------- /src/gnb_nodeid.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_NODEID_H 19 | #define GNB_NODEID_H 20 | 21 | #include "gnb_node_type.h" 22 | 23 | #define GNB_MAX_NODEID_DEC_STRING_SIZE (sizeof("18446744073709551615") - 1) 24 | #define GNB_MAX_NODEID_HEX_STRING_SIZE (sizeof("$FFFFFFFFFFFFFFFF") - 1) 25 | #define GNB_MAX_NODEID_STRING_SIZE (GNB_MAX_NODEID_DEC_STRING_SIZE+GNB_MAX_NODEID_HEX_STRING_SIZE+3) 26 | 27 | #define GNB_NODEIS2STR_FMT_DEC (0x1) 28 | #define GNB_NODEIS2STR_FMT_HEX (0x1 << 1) 29 | 30 | gnb_uuid_t gnb_str2nodeid(char *nodeidstr); 31 | 32 | char *gnb_nodeid2str(gnb_uuid_t nodeid, char *nodeidstr, int fmt); 33 | 34 | 35 | static char gnb_static_nodeid_string_buffer1[GNB_MAX_NODEID_STRING_SIZE]; 36 | static char gnb_static_nodeid_string_buffer2[GNB_MAX_NODEID_STRING_SIZE]; 37 | static char gnb_static_nodeid_string_buffer3[GNB_MAX_NODEID_STRING_SIZE]; 38 | static char gnb_static_nodeid_string_buffer4[GNB_MAX_NODEID_STRING_SIZE]; 39 | 40 | #define GNB_NODEID_DEC_STR1(nodeid) gnb_nodeid2str(nodeid, gnb_static_nodeid_string_buffer1, GNB_NODEIS2STR_FMT_DEC) 41 | #define GNB_NODEID_DEC_STR2(nodeid) gnb_nodeid2str(nodeid, gnb_static_nodeid_string_buffer2, GNB_NODEIS2STR_FMT_DEC) 42 | #define GNB_NODEID_DEC_STR3(nodeid) gnb_nodeid2str(nodeid, gnb_static_nodeid_string_buffer3, GNB_NODEIS2STR_FMT_DEC) 43 | #define GNB_NODEID_DEC_STR4(nodeid) gnb_nodeid2str(nodeid, gnb_static_nodeid_string_buffer4, GNB_NODEIS2STR_FMT_DEC) 44 | 45 | 46 | #define GNB_NODEID_HEX_STR1(nodeid) gnb_nodeid2str(nodeid, gnb_static_nodeid_string_buffer1, GNB_NODEIS2STR_FMT_HEX) 47 | #define GNB_NODEID_HEX_STR2(nodeid) gnb_nodeid2str(nodeid, gnb_static_nodeid_string_buffer2, GNB_NODEIS2STR_FMT_HEX) 48 | #define GNB_NODEID_HEX_STR3(nodeid) gnb_nodeid2str(nodeid, gnb_static_nodeid_string_buffer3, GNB_NODEIS2STR_FMT_HEX) 49 | #define GNB_NODEID_HEX_STR4(nodeid) gnb_nodeid2str(nodeid, gnb_static_nodeid_string_buffer4, GNB_NODEIS2STR_FMT_HEX) 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /src/gnb_payload16.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_PAYLOAD16_H 19 | #define GNB_PAYLOAD16_H 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #pragma pack(push, 1) 28 | typedef struct _gnb_payload16_t{ 29 | uint16_t size; 30 | unsigned char type; 31 | unsigned char sub_type; 32 | unsigned char data[0]; 33 | } __attribute__ ((packed)) gnb_payload16_t; 34 | #pragma pack(pop) 35 | 36 | #define GNB_PAYLOAD16_HEAD_SIZE 4 37 | 38 | #define GNB_MAX_PAYLOAD_SIZE 65535 39 | 40 | gnb_payload16_t* gnb_payload16_init(char type,uint16_t data_size); 41 | 42 | gnb_payload16_t* gnb_payload16_create(char type, void *data, uint16_t data_size); 43 | 44 | gnb_payload16_t *gnb_payload16_dup(gnb_payload16_t *gnb_payload16_in); 45 | 46 | void gnb_payload16_free(gnb_payload16_t *gnb_payload16); 47 | 48 | uint16_t gnb_payload16_set_size(gnb_payload16_t *gnb_payload16, uint16_t new_size); 49 | 50 | uint16_t gnb_payload16_size(gnb_payload16_t *gnb_payload16); 51 | 52 | uint16_t gnb_payload16_set_data_len(gnb_payload16_t *gnb_payload16, uint16_t new_len); 53 | 54 | uint16_t gnb_payload16_data_len(gnb_payload16_t *gnb_payload16); 55 | 56 | typedef struct _gnb_payload16_ctx_t{ 57 | 58 | //当前 当前payload(frame) 已收到的字节数 59 | //r_len足2字节时,接收的数据存 gnb_payload 60 | //r_len 不足2字节时,接收的数据存 buffer 61 | int r_len; 62 | 63 | unsigned char buffer[2]; 64 | 65 | //传入payload 66 | gnb_payload16_t *gnb_payload16; 67 | //传入的 payload 内存块大小 68 | uint32_t max_payload_size; 69 | 70 | void *udata; 71 | 72 | }gnb_payload16_ctx_t; 73 | 74 | gnb_payload16_ctx_t* gnb_payload16_ctx_init(uint16_t max_payload_size); 75 | 76 | void gnb_payload16_ctx_free(gnb_payload16_ctx_t *gnb_payload16_ctx); 77 | 78 | typedef int (*gnb_payload16_handle_cb_t)(gnb_payload16_t *gnb_payload16, void *ctx); 79 | 80 | int gnb_payload16_handle(void *data, size_t data_size, gnb_payload16_ctx_t *gnb_payload16_ctx, gnb_payload16_handle_cb_t cb); 81 | 82 | #define GNB_PAYLOAD16_FRAME_SIZE(payload) gnb_payload16_size(payload) 83 | #define GNB_PAYLOAD16_DATA_SIZE(payload) gnb_payload16_data_len(payload) 84 | 85 | #endif 86 | -------------------------------------------------------------------------------- /src/gnb_pingpong_frame_type.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_PINGPONG_FRAME_TYPE_H 19 | #define GNB_PINGPONG_FRAME_TYPE_H 20 | 21 | #include "stdint.h" 22 | #include "gnb_core_frame_type_defs.h" 23 | 24 | #pragma pack(push, 1) 25 | 26 | typedef struct _node_ping_frame_t { 27 | 28 | struct __attribute__((__packed__)) ping_frame_data { 29 | gnb_uuid_t src_uuid64; //发送方的uuid64 30 | gnb_uuid_t dst_uuid64; //接收方的uuid64 31 | uint64_t src_ts_usec; //发送方的时间戳 32 | /*让 dst 看到自己的 ip 地址,暂时还没启用*/ 33 | uint8_t dst_addr4[4]; 34 | uint16_t dst_port4; 35 | uint8_t dst_addr6[16]; 36 | uint16_t dst_port6; 37 | unsigned char crypto_seed[64]; 38 | unsigned char attachment[128+64]; 39 | unsigned char text[32]; 40 | }data; 41 | 42 | unsigned char src_sign[ED25519_SIGN_SIZE]; 43 | 44 | }__attribute__ ((__packed__)) node_ping_frame_t; 45 | 46 | #define NODE_PING_FRAME_PAYLOAD_SIZE (sizeof(gnb_payload16_t) + sizeof(node_ping_frame_t)) 47 | 48 | typedef struct _node_pong_frame_t { 49 | 50 | struct __attribute__((__packed__)) pong_frame_data { 51 | gnb_uuid_t src_uuid64; //发送方的uuid64 52 | gnb_uuid_t dst_uuid64; //接收方的uuid64 53 | uint64_t src_ts_usec; //发送方的时间戳 54 | uint64_t dst_ts_usec; //接收方上一个ping frame带来的时间戳 55 | 56 | uint8_t dst_addr4[4]; 57 | uint16_t dst_port4; 58 | uint8_t dst_addr6[16]; 59 | uint16_t dst_port6; 60 | 61 | unsigned char crypto_seed[64]; 62 | 63 | unsigned char attachment[128+64]; 64 | 65 | unsigned char text[32]; 66 | }data; 67 | 68 | unsigned char src_sign[ED25519_SIGN_SIZE]; 69 | 70 | }__attribute__ ((__packed__)) node_pong_frame_t; 71 | 72 | #define NODE_PONG_FRAME_PAYLOAD_SIZE (sizeof(gnb_payload16_t) + sizeof(node_pong_frame_t)) 73 | 74 | #define GNB_NODE_ATTACHMENT_TYPE_TUN_EMPTY 0x0 75 | #define GNB_NODE_ATTACHMENT_TYPE_TUN_SOCKADDRESS 0x1 76 | 77 | typedef struct _node_attachment_tun_sockaddress_t { 78 | 79 | struct in_addr tun_addr4; 80 | uint16_t tun_sin_port4; 81 | 82 | struct in6_addr tun_ipv6_addr; 83 | uint16_t tun_sin_port6; 84 | 85 | uint16_t es_sin_port4; 86 | uint16_t es_sin_port6; 87 | 88 | }__attribute__ ((__packed__)) node_attachment_tun_sockaddress_t; 89 | 90 | 91 | #pragma pack(pop) 92 | 93 | #endif 94 | -------------------------------------------------------------------------------- /src/gnb_platform.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_PLATFORM_H 19 | #define GNB_PLATFORM_H 20 | 21 | #if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__) || defined(__OpenBSD__) 22 | #define __UNIX_LIKE_OS__ 1 23 | #endif 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /src/gnb_ring_buffer_fixed.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | 19 | #include 20 | #include 21 | 22 | #include "gnb_ring_buffer_fixed.h" 23 | 24 | size_t gnb_ring_buffer_fixed_sum_size(size_t block_size, unsigned short block_num_mask){ 25 | 26 | size_t memory_size; 27 | 28 | size_t block_num = 0xFF+1; 29 | 30 | switch (block_num_mask) { 31 | 32 | case 0xFFFF : //65535 33 | case 0x7FFF : //32767 34 | case 0x3FFF : //16383 35 | case 0x1FFF : //8191 36 | case 0xFFF : //4095 37 | case 0x7FF : //2047 38 | case 0x3FF : //1023 39 | case 0x1FF : //511 40 | case 0xFF : //255 41 | case 0x7F : //127 42 | case 0x3F : //63 43 | case 0x1F : //31 44 | case 0xF : //15 45 | case 0x7 : //7 46 | case 0x3 : //3 47 | block_num = block_num_mask+1; 48 | break; 49 | 50 | default: 51 | block_num = 0xFF+1; 52 | break; 53 | 54 | } 55 | 56 | memory_size = sizeof(gnb_ring_buffer_fixed_t) + block_size * block_num; 57 | 58 | return memory_size; 59 | 60 | } 61 | 62 | gnb_ring_buffer_fixed_t* gnb_ring_buffer_fixed_init(void *memory, size_t block_size, unsigned short block_num_mask){ 63 | 64 | gnb_ring_buffer_fixed_t *gnb_ring_buffer_fixed; 65 | 66 | size_t memory_size; 67 | 68 | gnb_ring_buffer_fixed = (gnb_ring_buffer_fixed_t*)memory; 69 | 70 | memory_size = gnb_ring_buffer_fixed_sum_size(block_size, block_num_mask); 71 | 72 | memset(memory, 0, memory_size); 73 | 74 | gnb_ring_buffer_fixed->block_num_mask = block_num_mask; 75 | 76 | gnb_ring_buffer_fixed->block_size = block_size; 77 | 78 | gnb_ring_buffer_fixed->memory_size = memory_size; 79 | 80 | return gnb_ring_buffer_fixed; 81 | 82 | } 83 | 84 | void* gnb_ring_buffer_fixed_push(gnb_ring_buffer_fixed_t *ring_buffer_fixed){ 85 | 86 | int tail_next_idx = (ring_buffer_fixed->tail_idx + 1) & ring_buffer_fixed->block_num_mask; 87 | 88 | if ( tail_next_idx == ring_buffer_fixed->head_idx ) { 89 | return NULL; 90 | } 91 | 92 | void *buffer_header = ring_buffer_fixed->blocks + ring_buffer_fixed->block_size * ring_buffer_fixed->tail_idx; 93 | 94 | return buffer_header; 95 | 96 | } 97 | 98 | void gnb_ring_buffer_fixed_push_submit(gnb_ring_buffer_fixed_t *ring_buffer_fixed){ 99 | 100 | int tail_next_idx = (ring_buffer_fixed->tail_idx + 1) & ring_buffer_fixed->block_num_mask; 101 | 102 | ring_buffer_fixed->tail_idx = tail_next_idx; 103 | 104 | } 105 | 106 | void* gnb_ring_buffer_fixed_pop(gnb_ring_buffer_fixed_t *ring_buffer_fixed){ 107 | 108 | if ( ring_buffer_fixed->head_idx == ring_buffer_fixed->tail_idx ) { 109 | return NULL; 110 | } 111 | 112 | void *buffer_header = ring_buffer_fixed->blocks + ring_buffer_fixed->block_size * ring_buffer_fixed->head_idx; 113 | 114 | return buffer_header; 115 | 116 | } 117 | 118 | void gnb_ring_buffer_fixed_pop_submit(gnb_ring_buffer_fixed_t *ring_buffer_fixed){ 119 | 120 | int head_next_idx = (ring_buffer_fixed->head_idx + 1) & ring_buffer_fixed->block_num_mask; 121 | 122 | ring_buffer_fixed->head_idx = head_next_idx; 123 | 124 | } 125 | -------------------------------------------------------------------------------- /src/gnb_ring_buffer_fixed.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef gnb_ring_buffer_fixed_h 19 | #define gnb_ring_buffer_fixed_h 20 | 21 | #include 22 | 23 | typedef struct _gnb_ring_buffer_fixed_t{ 24 | 25 | unsigned short block_num_mask; 26 | 27 | size_t block_size; 28 | 29 | size_t memory_size; 30 | 31 | unsigned int head_idx; 32 | unsigned int tail_idx; 33 | 34 | unsigned char blocks[0]; 35 | 36 | } __attribute__ ((aligned (4))) gnb_ring_buffer_fixed_t; 37 | 38 | 39 | /* 40 | block_num_mask must be: 41 | 42 | 0xFFFF 65535 43 | 0x7FFF 32767 44 | 0x3FFF 16383 45 | 0x1FFF 8191 46 | 0xFFF 4095 47 | 0x7FF 2047 48 | 0x3FF 1023 49 | 0x1FF 511 50 | 0xFF 255 51 | 0x7F 127 52 | 0x3F 63 53 | 0x1F 31 54 | 0xF 15 55 | 0x7 7 56 | 0x3 3 57 | */ 58 | size_t gnb_ring_buffer_fixed_sum_size(size_t block_size, unsigned short block_num_mask); 59 | 60 | gnb_ring_buffer_fixed_t* gnb_ring_buffer_fixed_init(void *memory, size_t block_size, unsigned short block_num_mask); 61 | 62 | void* gnb_ring_buffer_fixed_push(gnb_ring_buffer_fixed_t *ring_buffer_fixed); 63 | 64 | void gnb_ring_buffer_fixed_push_submit(gnb_ring_buffer_fixed_t *ring_buffer_fixed); 65 | 66 | void* gnb_ring_buffer_fixed_pop(gnb_ring_buffer_fixed_t *ring_buffer_fixed); 67 | 68 | void gnb_ring_buffer_fixed_pop_submit(gnb_ring_buffer_fixed_t *ring_buffer_fixed); 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /src/gnb_time.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include 19 | #include 20 | 21 | #ifdef _WIN32 22 | #include 23 | #include 24 | 25 | #include 26 | 27 | #define _POSIX_C_SOURCE 1 28 | //使得 localtime_r 等函数有效 29 | 30 | #endif 31 | 32 | #include 33 | #include 34 | 35 | #include "gnb_time.h" 36 | 37 | 38 | uint64_t gnb_timestamp_sec(){ 39 | 40 | int ret; 41 | struct timeval cur_time; 42 | uint64_t u64; 43 | 44 | ret = gettimeofday(&cur_time,NULL); 45 | 46 | if (0!=ret) { 47 | return 0; 48 | } 49 | 50 | u64 = (uint64_t)cur_time.tv_sec; 51 | 52 | return u64; 53 | 54 | } 55 | 56 | 57 | uint64_t gnb_timestamp_usec(){ 58 | 59 | int ret; 60 | struct timeval cur_time; 61 | uint64_t u64; 62 | 63 | ret = gettimeofday(&cur_time,NULL); 64 | 65 | if (0!=ret) { 66 | return 0; 67 | } 68 | 69 | u64 = (uint64_t)cur_time.tv_sec*1000000 + cur_time.tv_usec; 70 | 71 | return u64; 72 | } 73 | 74 | 75 | void gnb_now_timef(const char *format, char *buffer, size_t buffer_size){ 76 | 77 | time_t t; 78 | 79 | struct tm ltm; 80 | 81 | time (&t); 82 | 83 | localtime_r(&t, <m); 84 | 85 | strftime (buffer,buffer_size,format,<m); 86 | 87 | } 88 | 89 | 90 | void gnb_timef(const char *format, time_t t, char *buffer, size_t buffer_size){ 91 | 92 | struct tm ltm; 93 | 94 | localtime_r(&t, <m); 95 | 96 | strftime (buffer,buffer_size,format,<m); 97 | 98 | } 99 | 100 | 101 | int gnb_now_mday(){ 102 | 103 | time_t t; 104 | 105 | struct tm ltm; 106 | 107 | time (&t); 108 | 109 | localtime_r(&t, <m); 110 | 111 | return ltm.tm_mday; 112 | 113 | } 114 | 115 | 116 | int gnb_now_yday(){ 117 | 118 | time_t t; 119 | 120 | struct tm ltm; 121 | 122 | time (&t); 123 | 124 | localtime_r(&t, <m); 125 | 126 | return ltm.tm_yday; 127 | 128 | } 129 | -------------------------------------------------------------------------------- /src/gnb_time.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_TIME_H 19 | #define GNB_TIME_H 20 | 21 | #if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__) || defined(__OpenBSD__) 22 | #define __UNIX_LIKE_OS__ 1 23 | #endif 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | uint64_t gnb_timestamp_sec(); 30 | 31 | /*微秒*/ 32 | uint64_t gnb_timestamp_usec(); 33 | 34 | 35 | //format:"%Y_%m_%d_%H.%M.%S" 36 | void gnb_now_timef(const char *format, char *buffer, size_t buffer_size); 37 | 38 | void gnb_timef(const char *format, time_t t, char *buffer, size_t buffer_size); 39 | 40 | /* 41 | 获得当前月的当天数字 42 | */ 43 | int gnb_now_mday(); 44 | 45 | int gnb_now_yday(); 46 | 47 | #define GNB_TIME_STRING_MAX 64 48 | 49 | #ifdef __UNIX_LIKE_OS__ 50 | #define GNB_SLEEP_MILLISECOND(millisecond) usleep(1000 * millisecond) 51 | #endif 52 | 53 | #if defined(_WIN32) 54 | #define GNB_SLEEP_MILLISECOND(millisecond) Sleep(millisecond) 55 | #endif 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /src/gnb_tun_drv.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_TUN_DRV_H 19 | #define GNB_TUN_DRV_H 20 | 21 | typedef struct _gnb_core_t gnb_core_t; 22 | 23 | typedef int (*init_tun_func_t)(gnb_core_t *gnb_core); 24 | 25 | typedef int (*open_tun_func_t)(gnb_core_t *gnb_core); 26 | 27 | typedef int (*read_tun_func_t)(gnb_core_t *gnb_core, void *buf, size_t buf_size); 28 | 29 | typedef int (*write_tun_func_t)(gnb_core_t *gnb_core, void *buf, size_t buf_size); 30 | 31 | typedef int (*close_tun_func_t)(gnb_core_t *gnb_core); 32 | 33 | typedef int (*loop_tun_func_t)(gnb_core_t *gnb_core); 34 | 35 | typedef int (*release_tun_func_t)(gnb_core_t *gnb_core); 36 | 37 | typedef struct _gnb_tun_drv_t { 38 | 39 | init_tun_func_t init_tun; 40 | 41 | open_tun_func_t open_tun; 42 | 43 | read_tun_func_t read_tun; 44 | 45 | write_tun_func_t write_tun; 46 | 47 | close_tun_func_t close_tun; 48 | 49 | release_tun_func_t release_tun; 50 | 51 | }gnb_tun_drv_t; 52 | 53 | 54 | #if defined(__FreeBSD__) 55 | extern gnb_tun_drv_t gnb_tun_drv_freebsd; 56 | #endif 57 | 58 | 59 | #if defined(__OpenBSD__) 60 | extern gnb_tun_drv_t gnb_tun_drv_openbsd; 61 | #endif 62 | 63 | 64 | #if defined(__APPLE__) 65 | extern gnb_tun_drv_t gnb_tun_drv_darwin; 66 | #endif 67 | 68 | #if defined(__linux__) 69 | extern gnb_tun_drv_t gnb_tun_drv_linux; 70 | #endif 71 | 72 | 73 | #if defined(_WIN32) 74 | extern gnb_tun_drv_t gnb_tun_drv_win32; 75 | extern gnb_tun_drv_t gnb_tun_drv_wintun; 76 | #endif 77 | 78 | #endif 79 | -------------------------------------------------------------------------------- /src/gnb_type.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_TYPE_H 19 | #define GNB_TYPE_H 20 | 21 | typedef unsigned long long int gnb_uint64_t; 22 | 23 | /* 64 bits node id*/ 24 | typedef unsigned long long int gnb_uuid_t; 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /src/gnb_udp.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__) || defined(__OpenBSD__) 24 | #include 25 | #include 26 | #include 27 | 28 | #endif 29 | 30 | #ifdef _WIN32 31 | 32 | #define _POSIX 33 | 34 | #undef _WIN32_WINNT 35 | #define _WIN32_WINNT 0x0600 36 | 37 | #include 38 | #include 39 | 40 | #endif 41 | 42 | #include "gnb_udp.h" 43 | 44 | int gnb_bind_udp_socket_ipv4(int socketfd,const char *host, int port) { 45 | struct sockaddr_in svr_addr; 46 | memset(&svr_addr, 0, sizeof(struct sockaddr_in)); 47 | svr_addr.sin_family = AF_INET; 48 | svr_addr.sin_port = htons(port); 49 | if ( NULL != host ) { 50 | svr_addr.sin_addr.s_addr = inet_addr(host); 51 | } else { 52 | svr_addr.sin_addr.s_addr = htonl(INADDR_ANY); 53 | } 54 | 55 | int on = 1; 56 | setsockopt( socketfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on) ); 57 | 58 | if ( bind(socketfd, (struct sockaddr *)&svr_addr, sizeof(struct sockaddr_in)) < 0 ) { 59 | perror("bind"); 60 | return -1; 61 | } 62 | return 0; 63 | } 64 | 65 | 66 | int gnb_bind_udp_socket_ipv6(int socketfd,const char *host, int port) { 67 | struct sockaddr_in6 svr_addr; 68 | memset(&svr_addr,0, sizeof(struct sockaddr_in6)); 69 | 70 | svr_addr.sin6_family = AF_INET6; 71 | svr_addr.sin6_port = htons(port); 72 | 73 | if ( NULL != host ) { 74 | inet_pton( AF_INET6, host, &svr_addr.sin6_addr); 75 | } else { 76 | svr_addr.sin6_addr = in6addr_any; 77 | } 78 | 79 | int on; 80 | on = 1; 81 | setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR,(const char *)&on, sizeof(on) ); 82 | on = 1; 83 | setsockopt(socketfd, IPPROTO_IPV6, IPV6_V6ONLY,(char *)&on, sizeof(on) ); 84 | if ( bind(socketfd, (struct sockaddr *)&svr_addr, sizeof(struct sockaddr_in6))<0 ) { 85 | perror("bind"); 86 | return -1; 87 | } 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /src/gnb_udp.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_UDP_H 19 | #define GNB_UDP_H 20 | 21 | int gnb_bind_udp_socket_ipv4(int socketfd,const char *host, int port); 22 | int gnb_bind_udp_socket_ipv6(int socketfd,const char *host, int port); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /src/gnb_uf_node_frame_type.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_UF_NODE_FRAME_TYPE_H 19 | #define GNB_UF_NODE_FRAME_TYPE_H 20 | 21 | #pragma pack(push, 1) 22 | 23 | typedef struct _uf_node_notify_frame_t { 24 | 25 | struct __attribute__((__packed__)) uf_node_notify_frame_data { 26 | gnb_uuid_t src_uuid64; //发送方的uuid64 27 | gnb_uuid_t dst_uuid64; //接收方的uuid64 28 | gnb_uuid_t df_uuid64; //direct forwarding node uuid64 29 | int64_t addr6_ping_latency_usec; 30 | int64_t addr4_ping_latency_usec; 31 | unsigned char attachment[128]; 32 | unsigned char text[64]; 33 | }data; 34 | 35 | unsigned char src_sign[ED25519_SIGN_SIZE]; 36 | 37 | }__attribute__ ((__packed__)) uf_node_notify_frame_t; 38 | 39 | #define UF_NODE_NOTIFY_FRAME_PAYLOAD_SIZE (sizeof(gnb_payload16_t) + sizeof(uf_node_notify_frame_t)) 40 | 41 | #pragma pack(pop) 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /src/gnb_unified_forwarding.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_UNIFIED_FORWARDING_H 19 | #define GNB_UNIFIED_FORWARDING_H 20 | 21 | #include 22 | 23 | typedef struct _gnb_core_t gnb_core_t; 24 | 25 | typedef struct _gnb_payload16_t gnb_payload16_t; 26 | 27 | typedef struct _gnb_node_t gnb_node_t; 28 | 29 | typedef struct _gnb_sockaddress_t gnb_sockaddress_t; 30 | 31 | int gnb_unified_forwarding_tun(gnb_core_t *gnb_core, gnb_pf_ctx_t *pf_ctx); 32 | int gnb_unified_forwarding_with_multi_path_tun(gnb_core_t *gnb_core, gnb_pf_ctx_t *pf_ctx); 33 | 34 | #define UNIFIED_FORWARDING_DROP -2 35 | #define UNIFIED_FORWARDING_ERROR -1 36 | #define UNIFIED_FORWARDING_TO_TUN 0 37 | #define UNIFIED_FORWARDING_TO_INET 1 38 | 39 | void gnb_setup_unified_forwarding_nodeid(gnb_core_t *gnb_core, gnb_node_t *dst_node); 40 | int gnb_unified_forwarding_inet(gnb_core_t *gnb_core, gnb_payload16_t *payload); 41 | int gnb_unified_forwarding_multi_path_inet(gnb_core_t *gnb_core, gnb_payload16_t *payload); 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /src/gnb_ur1_frame_type.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_UR1_FRAME_TYPE_H 19 | #define GNB_UR1_FRAME_TYPE_H 20 | 21 | #include "stdint.h" 22 | #include "gnb_type.h" 23 | #include "gnb_core_frame_type_defs.h" 24 | 25 | #pragma pack(push, 1) 26 | 27 | typedef struct _gnb_ur1_frame_head_t{ 28 | 29 | gnb_uuid_t src_uuid64; //由第一个转发的node设置,应用构造时设置为0, 网络字节序 30 | gnb_uuid_t dst_uuid64; //由应用构造时设置,网络字节序 31 | 32 | uint8_t src_addr[16]; //src_addr,src_port 由转发的node设置 33 | uint16_t src_port; //网络字节序 34 | 35 | uint8_t dst_addr[16]; //由应用构造时设置 36 | uint16_t dst_port; //网络字节序 37 | 38 | unsigned char passcode[4]; 39 | unsigned char verifycode[4]; //由第一个转发的node设置 40 | 41 | uint8_t ttl; //由第一个转发的node设置为2, 应用构造时设置为0, 42 | 43 | }__attribute__ ((__packed__)) gnb_ur1_frame_head_t; 44 | 45 | #pragma pack(pop) 46 | 47 | #define GNB_MAX_UR1_FRAME_SIZE (32*1024) 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /src/gnb_worker.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #include "gnb.h" 25 | #include "gnb_worker.h" 26 | #include "gnb_worker_queue_data.h" 27 | 28 | /* 29 | 每个 worker 都是一个独立线程,设计上允许多个线程并发处理payload,但对于处理 node 的数据来说不是很必要。 30 | 如果把 pf 也设计为 worker 的形式,对于加密运算比较重的分组,可以发挥多核的特性 31 | */ 32 | extern gnb_worker_t gnb_primary_worker_mod; 33 | extern gnb_worker_t gnb_node_worker_mod; 34 | extern gnb_worker_t gnb_index_worker_mod; 35 | extern gnb_worker_t gnb_secure_index_worker_mod; 36 | extern gnb_worker_t gnb_detect_worker_mod; 37 | 38 | extern gnb_worker_t gnb_index_service_worker_mod; 39 | extern gnb_worker_t gnb_secure_index_service_worker_mod; 40 | 41 | extern gnb_worker_t gnb_pf_worker_mod; 42 | 43 | static gnb_worker_t *gnb_worker_array[] = { 44 | &gnb_primary_worker_mod, 45 | &gnb_index_worker_mod, 46 | &gnb_secure_index_worker_mod, 47 | &gnb_index_service_worker_mod, 48 | &gnb_secure_index_service_worker_mod, 49 | &gnb_detect_worker_mod, 50 | &gnb_node_worker_mod, 51 | &gnb_pf_worker_mod, 52 | NULL, 53 | }; 54 | 55 | static gnb_worker_t* find_worker_mod_by_name(const char *name){ 56 | 57 | int num = sizeof(gnb_worker_array)/sizeof(gnb_worker_t *); 58 | 59 | int i; 60 | 61 | for ( i=0; iname,name,128) ) { 68 | return gnb_worker_array[i]; 69 | } 70 | 71 | } 72 | 73 | return NULL; 74 | 75 | } 76 | 77 | gnb_worker_t *gnb_worker_init(const char *name, void *ctx){ 78 | 79 | gnb_worker_t *gnb_worker_mod = find_worker_mod_by_name(name); 80 | 81 | if ( NULL==gnb_worker_mod ) { 82 | printf("find_worker_by_name name[%s] is NULL\n",name); 83 | return NULL; 84 | } 85 | 86 | gnb_worker_t *gnb_worker = (gnb_worker_t *) malloc(sizeof(gnb_worker_t)); 87 | 88 | *gnb_worker = *gnb_worker_mod; 89 | 90 | gnb_worker->thread_worker_flag = 0; 91 | gnb_worker->thread_worker_run_flag = 0; 92 | 93 | gnb_worker->init(gnb_worker, ctx); 94 | 95 | return gnb_worker; 96 | 97 | } 98 | 99 | 100 | void gnb_worker_wait_primary_worker_started(gnb_core_t *gnb_core){ 101 | 102 | do{ 103 | 104 | if ( 1==gnb_core->primary_worker->thread_worker_run_flag ) { 105 | break; 106 | } 107 | 108 | }while(1); 109 | 110 | } 111 | 112 | 113 | void gnb_worker_sync_time(uint64_t *now_time_sec_ptr, uint64_t *now_time_usec_ptr){ 114 | 115 | struct timeval now_timeval; 116 | 117 | gettimeofday(&now_timeval, NULL); 118 | 119 | *now_time_sec_ptr = (uint64_t)now_timeval.tv_sec; 120 | *now_time_usec_ptr = (uint64_t)now_timeval.tv_sec *1000000 + now_timeval.tv_usec; 121 | 122 | } 123 | 124 | void gnb_worker_release(gnb_worker_t *gnb_worker){ 125 | 126 | gnb_worker->release(gnb_worker); 127 | 128 | free(gnb_worker); 129 | 130 | return; 131 | 132 | } 133 | -------------------------------------------------------------------------------- /src/gnb_worker.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_WORKER_H 19 | #define GNB_WORKER_H 20 | 21 | #include "gnb_worker_type.h" 22 | 23 | gnb_worker_t *gnb_worker_init(const char *name, void *ctx); 24 | 25 | void gnb_worker_wait_primary_worker_started(gnb_core_t *gnb_core); 26 | 27 | void gnb_worker_sync_time(uint64_t *now_time_sec_ptr, uint64_t *now_time_usec_ptr); 28 | 29 | void gnb_worker_release(gnb_worker_t *gnb_worker); 30 | 31 | #endif 32 | 33 | -------------------------------------------------------------------------------- /src/gnb_worker_queue_data.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_NODE_WORKER_QUEUE_DATA_H 19 | #define GNB_NODE_WORKER_QUEUE_DATA_H 20 | 21 | #include 22 | 23 | typedef struct _gnb_node_t gnb_node_t; 24 | 25 | typedef struct _gnb_payload16_t gnb_payload16_t; 26 | 27 | typedef struct _gnb_sockaddress_t gnb_sockaddress_t; 28 | 29 | 30 | typedef struct _gnb_node_worker_in_data_t { 31 | 32 | gnb_sockaddress_t node_addr_st; 33 | 34 | uint8_t socket_idx; 35 | 36 | gnb_payload16_t payload_st; 37 | 38 | }gnb_worker_in_data_t; 39 | 40 | 41 | typedef struct _gnb_worker_queue_data_t { 42 | 43 | #define GNB_WORKER_QUEUE_DATA_TYPE_NODE_IN 0x1 44 | #define GNB_WORKER_QUEUE_DATA_TYPE_NODE_OUT 0x2 45 | int type; 46 | 47 | union worker_data { 48 | gnb_worker_in_data_t node_in; 49 | }data; 50 | 51 | //这里可以定义宏方便操作union 52 | 53 | }gnb_worker_queue_data_t; 54 | 55 | //这个块不能太大,在嵌入设备上,这里是占用内存的大头 56 | #define GNB_NODE_WORKER_QUEUE_BLOCK_SIZE 512 57 | #define GNB_INDEX_WORKER_QUEUE_BLOCK_SIZE 512 58 | #define GNB_INDEX_SERVICE_WORKER_QUEUE_BLOCK_SIZE 512 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /src/gnb_worker_type.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #ifndef GNB_WORKER_TYPE_H 19 | #define GNB_WORKER_TYPE_H 20 | 21 | #include 22 | #include "gnb_ring_buffer_fixed.h" 23 | 24 | 25 | typedef struct _gnb_worker_t gnb_worker_t; 26 | 27 | typedef struct _gnb_ring_buffer_t gnb_ring_buffer_t; 28 | 29 | typedef void(*gnb_worker_init_func_t)(gnb_worker_t *gnb_worker, void *ctx); 30 | 31 | typedef void(*gnb_worker_release_func_t)(gnb_worker_t *gnb_worker); 32 | 33 | typedef int(*gnb_worker_start_func_t)(gnb_worker_t *gnb_worker); 34 | 35 | typedef int(*gnb_worker_stop_func_t)(gnb_worker_t *gnb_worker); 36 | 37 | typedef int(*gnb_worker_notify_func_t)(gnb_worker_t *gnb_worker); 38 | 39 | typedef int(*gnb_worker_notify_func_t)(gnb_worker_t *gnb_worker); 40 | 41 | typedef struct _gnb_worker_t { 42 | 43 | char *name; 44 | 45 | gnb_worker_init_func_t init; 46 | 47 | gnb_worker_release_func_t release; 48 | 49 | gnb_worker_start_func_t start; 50 | 51 | gnb_worker_stop_func_t stop; 52 | 53 | gnb_worker_notify_func_t notify; 54 | 55 | gnb_ring_buffer_fixed_t *ring_buffer_in; 56 | gnb_ring_buffer_fixed_t *ring_buffer_out; 57 | 58 | volatile int thread_worker_flag; 59 | 60 | volatile int thread_worker_ready_flag; 61 | 62 | volatile int thread_worker_run_flag; 63 | 64 | void *ctx; 65 | 66 | }gnb_worker_t; 67 | 68 | 69 | typedef struct _gnb_worker_ring_t { 70 | 71 | uint8_t size; 72 | uint8_t cur_idx; 73 | gnb_worker_t *worker[0]; 74 | 75 | }gnb_worker_ring_t; 76 | 77 | #endif 78 | 79 | 80 | -------------------------------------------------------------------------------- /src/mingw/gnb.exe.manifest: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/mingw/gnb_res.rc: -------------------------------------------------------------------------------- 1 | 1 24 "gnb.exe.manifest" 2 | 3 | -------------------------------------------------------------------------------- /src/mingw/windows_platform.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "gnb_exec.h" 24 | #include "gnb_arg_list.h" 25 | 26 | 27 | int gnb_get_pid(){ 28 | int pid; 29 | pid = GetCurrentProcessId(); 30 | return pid; 31 | } 32 | 33 | 34 | void gnb_set_env(const char *name, const char *value){ 35 | 36 | _putenv_s(name, value); 37 | 38 | } 39 | 40 | 41 | int gnb_exec(char *app_filename, char *current_path, gnb_arg_list_t *gnb_arg_list, int flag){ 42 | 43 | int rc; 44 | 45 | wchar_t app_filename_w[PATH_MAX+NAME_MAX]; 46 | wchar_t gnb_arg_string_w[PATH_MAX+NAME_MAX]; 47 | wchar_t current_path_w[PATH_MAX+NAME_MAX]; 48 | 49 | STARTUPINFOW si; 50 | PROCESS_INFORMATION pi = {0}; 51 | DWORD creation_flags; 52 | 53 | ZeroMemory(&si,sizeof(si)); 54 | 55 | si.cb = sizeof(si); 56 | 57 | char gnb_arg_string[GNB_ARG_STRING_MAX_SIZE]; 58 | 59 | rc = gnb_arg_list_to_string(gnb_arg_list, gnb_arg_string, GNB_ARG_STRING_MAX_SIZE); 60 | 61 | if ( flag & GNB_EXEC_BACKGROUND ) { 62 | 63 | si.wShowWindow = SW_HIDE; 64 | si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; 65 | creation_flags = CREATE_NO_WINDOW; 66 | 67 | } else { 68 | 69 | si.dwFlags = STARTF_USESHOWWINDOW; 70 | si.wShowWindow = SW_SHOW; 71 | creation_flags = CREATE_NEW_CONSOLE; 72 | } 73 | 74 | memset(app_filename_w, 0, PATH_MAX+NAME_MAX); 75 | MultiByteToWideChar(CP_ACP, 0, app_filename, -1, app_filename_w, PATH_MAX+NAME_MAX); 76 | 77 | memset(current_path_w, 0, PATH_MAX+NAME_MAX); 78 | MultiByteToWideChar(CP_ACP, 0, current_path, -1, current_path_w, PATH_MAX+NAME_MAX); 79 | 80 | memset(gnb_arg_string_w, 0, PATH_MAX+NAME_MAX); 81 | MultiByteToWideChar(CP_ACP, 0, gnb_arg_string, -1, gnb_arg_string_w, PATH_MAX+NAME_MAX); 82 | 83 | rc = CreateProcessW(app_filename_w, gnb_arg_string_w, NULL,NULL,TRUE, creation_flags, NULL, current_path_w, &si,&pi); 84 | 85 | if ( !rc ) { 86 | return -1; 87 | } 88 | 89 | if ( flag & GNB_EXEC_WAIT ) { 90 | 91 | WaitForSingleObject(pi.hProcess, INFINITE); 92 | 93 | DWORD exitcode; 94 | 95 | rc = GetExitCodeProcess(pi.hProcess, &exitcode); 96 | 97 | } 98 | 99 | if ( pi.hProcess ) { 100 | CloseHandle(pi.hProcess); 101 | pi.hProcess = NULL; 102 | } 103 | 104 | if ( pi.hThread ) { 105 | CloseHandle(pi.hThread); 106 | pi.hThread = NULL; 107 | } 108 | 109 | return pi.dwProcessId; 110 | 111 | } 112 | 113 | 114 | int gnb_kill(DWORD pid){ 115 | 116 | HANDLE hProcess=OpenProcess(PROCESS_TERMINATE,FALSE, pid); 117 | 118 | TerminateProcess(hProcess, 0); 119 | 120 | return 0; 121 | } 122 | 123 | -------------------------------------------------------------------------------- /src/unix/unix_platform.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) gnbdev 3 | 4 | This program is free software; you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation; either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include 19 | #include 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | 26 | #include "gnb_exec.h" 27 | #include "gnb_arg_list.h" 28 | 29 | #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) 30 | extern char **__environ; 31 | #endif 32 | 33 | #if defined(GNB_OPENWRT_BUILD) 34 | extern char **environ; 35 | #endif 36 | 37 | #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) 38 | extern char **environ; 39 | #endif 40 | 41 | 42 | int gnb_get_pid(){ 43 | int pid = getpid(); 44 | return pid; 45 | } 46 | 47 | 48 | void gnb_set_env(const char *name, const char *value){ 49 | 50 | setenv(name, value, 1); 51 | 52 | } 53 | 54 | 55 | pid_t gnb_exec(char *app_filename, char *current_path, gnb_arg_list_t *arg_list, int flag){ 56 | 57 | pid_t pid; 58 | 59 | int fd; 60 | int ret; 61 | int argc = 0; 62 | 63 | char *argv[arg_list->argc]; 64 | 65 | pid = fork(); 66 | 67 | if ( 0 != pid ) { 68 | 69 | if ( -1 == pid ) { 70 | return pid; 71 | } 72 | 73 | if ( flag & GNB_EXEC_WAIT ) { 74 | waitpid(pid, NULL, 0); 75 | } 76 | 77 | return pid; 78 | 79 | } 80 | 81 | int i; 82 | 83 | for (i=0; iargc; i++) { 84 | argv[i] = arg_list->argv[i]; 85 | } 86 | 87 | argv[i] = NULL; 88 | 89 | ret = chdir(current_path); 90 | 91 | if ( 0 != ret ) { 92 | goto finish; 93 | } 94 | 95 | if ( flag & GNB_EXEC_FOREGROUND ) { 96 | goto do_exec; 97 | } 98 | 99 | fd = open("/dev/null", O_RDWR); 100 | 101 | if ( 0 != fd ){ 102 | ret = dup2(fd, STDIN_FILENO); 103 | ret = dup2(fd, STDOUT_FILENO); 104 | ret = dup2(fd, STDERR_FILENO); 105 | } 106 | 107 | do_exec: 108 | 109 | #if defined(__linux__) 110 | 111 | #if defined(GNB_LINUX_BUILD) 112 | ret = execve(app_filename, argv, __environ); 113 | #endif 114 | 115 | #if defined(GNB_OPENWRT_BUILD) 116 | 117 | ret = execve(app_filename, argv, environ); 118 | #endif 119 | 120 | #if defined(GNB_ANDROID_BUILD) 121 | ret = execve(app_filename, argv, environ); 122 | #endif 123 | 124 | #endif 125 | 126 | 127 | #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) 128 | ret = execve(app_filename, argv, environ); 129 | #endif 130 | 131 | if ( -1==ret ) { 132 | goto finish; 133 | } 134 | 135 | finish: 136 | 137 | exit(0); 138 | 139 | return 0; 140 | 141 | } 142 | 143 | 144 | void gnb_kill(pid_t pid){ 145 | 146 | kill(pid, SIGKILL); 147 | 148 | } 149 | -------------------------------------------------------------------------------- /version: -------------------------------------------------------------------------------- 1 | 1.6.0.a 2 | --------------------------------------------------------------------------------