├── dist-build ├── android-mips.sh ├── android-x86.sh ├── android-mips32.sh ├── android-arm.sh ├── android-armv7-a.sh └── android-build.sh ├── src ├── acl.h ├── daemon.h ├── udprelay.h ├── crypto.h ├── util.h ├── packet.h ├── logger.h ├── resolver.h ├── udprelay.c ├── common.h ├── cache.h ├── socks.h ├── xTunnel.h ├── signal.c ├── android.c ├── xTproxy.h ├── xSocks.h ├── packet.c ├── xForwarder.h ├── xSocksd.h ├── daemon.c ├── common.c ├── crypto.c ├── acl.c ├── consumer.c ├── logger.c ├── xForwarder_client.c └── cache.c ├── valgrind.supp ├── .gitmodules ├── 3rd ├── libcork │ ├── include │ │ └── libcork │ │ │ ├── cli.h │ │ │ ├── config.h │ │ │ ├── threads.h │ │ │ ├── os.h │ │ │ ├── ds.h │ │ │ ├── os │ │ │ └── process.h │ │ │ ├── config │ │ │ ├── version.h.in │ │ │ ├── bsd.h │ │ │ ├── linux.h │ │ │ ├── macosx.h │ │ │ ├── arch.h │ │ │ ├── config.h │ │ │ └── gcc.h │ │ │ ├── core.h │ │ │ ├── core │ │ │ ├── id.h │ │ │ ├── callbacks.h │ │ │ ├── api.h │ │ │ ├── gc.h │ │ │ ├── mempool.h │ │ │ ├── types.h │ │ │ ├── timestamp.h │ │ │ ├── error.h │ │ │ └── net-addresses.h │ │ │ ├── helpers │ │ │ ├── gc.h │ │ │ ├── posix.h │ │ │ └── errors.h │ │ │ ├── cli │ │ │ └── commands.h │ │ │ ├── ds │ │ │ ├── ring-buffer.h │ │ │ ├── stream.h │ │ │ ├── bitset.h │ │ │ └── managed-buffer.h │ │ │ └── threads │ │ │ └── atomics.h │ └── src │ │ ├── core │ │ ├── hash.c │ │ ├── version.c │ │ └── u128.c │ │ ├── ds │ │ ├── bitset.c │ │ ├── dllist.c │ │ └── ring-buffer.c │ │ └── posix │ │ ├── process.c │ │ └── directory-walker.c ├── libipset │ ├── src │ │ ├── general.c │ │ ├── set │ │ │ ├── allocation.c │ │ │ ├── ipv4_set.c │ │ │ ├── ipv6_set.c │ │ │ ├── inspection.c │ │ │ ├── storage.c │ │ │ └── inspection-template.c.in │ │ ├── map │ │ │ ├── allocation.c │ │ │ ├── ipv4_map.c │ │ │ ├── ipv6_map.c │ │ │ ├── inspection.c │ │ │ ├── inspection-template.c.in │ │ │ └── storage.c │ │ └── bdd │ │ │ ├── reachable.c │ │ │ ├── assignments.c │ │ │ ├── expanded.c │ │ │ └── bdd-iterator.c │ └── include │ │ └── ipset │ │ ├── errors.h │ │ ├── logging.h │ │ └── bits.h └── libancillary │ ├── COPYING │ ├── Makefile │ ├── fd_send.c │ ├── test.c │ ├── fd_recv.c │ └── ancillary.h ├── .gitignore ├── openwrt ├── Makefile └── files │ └── xSocks.init ├── xSocks.sln ├── config.mk ├── CHANGES.md ├── xSocks.vcxproj.filters └── .ycm_extra_conf.py /dist-build/android-mips.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | export CFLAGS="-Os" 3 | TARGET_ARCH=mips HOST_COMPILER=mipsel-linux-android "$(dirname "$0")/android-build.sh" 4 | -------------------------------------------------------------------------------- /src/acl.h: -------------------------------------------------------------------------------- 1 | #ifndef ACL_H 2 | #define ACL_H 3 | 4 | int acl_init(const char *path); 5 | void acl_free(void); 6 | int acl_contains_ip(const char * ip); 7 | 8 | #endif // for #ifndef ACL_H 9 | -------------------------------------------------------------------------------- /dist-build/android-x86.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | export TARGET_ARCH=i686 3 | export CFLAGS="-Os -march=${TARGET_ARCH}" 4 | ARCH=x86 HOST_COMPILER=i686-linux-android "$(dirname "$0")/android-build.sh" 5 | -------------------------------------------------------------------------------- /dist-build/android-mips32.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | export TARGET_ARCH=mips32 3 | export CFLAGS="-Os -march=${TARGET_ARCH}" 4 | ARCH=mips HOST_COMPILER=mipsel-linux-android "$(dirname "$0")/android-build.sh" 5 | -------------------------------------------------------------------------------- /dist-build/android-arm.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | export TARGET_ARCH=armv6 3 | export CFLAGS="-Os -mthumb -marm -march=${TARGET_ARCH}" 4 | ARCH=arm HOST_COMPILER=arm-linux-androideabi "$(dirname "$0")/android-build.sh" 5 | -------------------------------------------------------------------------------- /dist-build/android-armv7-a.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | export TARGET_ARCH=armv7-a 3 | export CFLAGS="-Os -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -marm -march=${TARGET_ARCH}" 4 | ARCH=arm HOST_COMPILER=arm-linux-androideabi "$(dirname "$0")/android-build.sh" 5 | -------------------------------------------------------------------------------- /valgrind.supp: -------------------------------------------------------------------------------- 1 | { 2 | 3 | Memcheck:Leak 4 | match-leak-kinds: possible 5 | fun:calloc 6 | fun:_dl_allocate_tls 7 | fun:pthread_create@@GLIBC_2.2.5 8 | fun:uv_thread_create 9 | fun:main 10 | } 11 | -------------------------------------------------------------------------------- /src/daemon.h: -------------------------------------------------------------------------------- 1 | #ifndef DAEMON_H 2 | #define DAEMON_H 3 | 4 | int daemonize(void); 5 | int already_running(const char *pidfile); 6 | void create_pidfile(const char *pidfile); 7 | void delete_pidfile(const char *pidfile); 8 | 9 | #endif // for #ifndef DAEMON_H 10 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "3rd/libuv"] 2 | path = 3rd/libuv 3 | url = https://github.com/libuv/libuv 4 | [submodule "3rd/libsodium"] 5 | path = 3rd/libsodium 6 | url = https://github.com/jedisct1/libsodium 7 | [submodule "3rd/c-ares"] 8 | path = 3rd/c-ares 9 | url = https://github.com/bagder/c-ares 10 | -------------------------------------------------------------------------------- /src/udprelay.h: -------------------------------------------------------------------------------- 1 | #ifndef UDPRELAY_H 2 | #define UDPRELAY_H 3 | 4 | #include "uv.h" 5 | #include "common.h" 6 | 7 | int udprelay_init(); 8 | int udprelay_start(uv_loop_t *loop, struct server_context *server); 9 | void udprelay_close(struct server_context *server); 10 | void udprelay_destroy(); 11 | 12 | #endif // for #ifndef UDPRELAY_H 13 | -------------------------------------------------------------------------------- /src/crypto.h: -------------------------------------------------------------------------------- 1 | #ifndef CRYPTO_H 2 | #define CRYPTO_H 3 | 4 | #include 5 | #include 6 | 7 | int crypto_init(const char *password); 8 | int crypto_generickey(uint8_t *out, size_t outlen, uint8_t *in, size_t inlen, uint8_t *key, size_t keylen); 9 | int crypto_encrypt(uint8_t *c, const uint8_t *m, const uint32_t mlen); 10 | int crypto_decrypt(uint8_t *m, const uint8_t *c, const uint32_t clen); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/cli.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef LIBCORK_CLI_H 12 | #define LIBCORK_CLI_H 13 | 14 | /*** include all of the parts ***/ 15 | 16 | #include 17 | 18 | #endif /* LIBCORK_CLI_H */ 19 | -------------------------------------------------------------------------------- /3rd/libipset/src/general.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2009-2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the LICENSE.txt file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #include 12 | 13 | #include "ipset/bdd/nodes.h" 14 | #include "ipset/ipset.h" 15 | 16 | 17 | int 18 | ipset_init_library() 19 | { 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/config.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2011, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef LIBCORK_CONFIG_H 12 | #define LIBCORK_CONFIG_H 13 | 14 | /*** include all of the parts ***/ 15 | 16 | #include 17 | 18 | #endif /* LIBCORK_CONFIG_H */ 19 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/threads.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef LIBCORK_THREADS_H 12 | #define LIBCORK_THREADS_H 13 | 14 | /*** include all of the parts ***/ 15 | 16 | #include 17 | #include 18 | 19 | #endif /* LIBCORK_THREADS_H */ 20 | -------------------------------------------------------------------------------- /src/util.h: -------------------------------------------------------------------------------- 1 | #ifndef UTIL_H 2 | #define UTIL_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "uv.h" 9 | 10 | #define container_of(ptr, type, member) ((type*)(((char*)(ptr)) - offsetof(type, member))) 11 | 12 | int resolve_addr(const char *buf, struct sockaddr_storage *addr); 13 | int read_size(uint8_t *buffer); 14 | void write_size(uint8_t *buffer, int len); 15 | int ip_name(const struct sockaddr *ip, char *name, size_t size); 16 | uv_os_sock_t create_socket(int type, int reuse); 17 | void dump_hex(const void *data, uint32_t len, char *title); 18 | 19 | #endif // for #ifndef UTIL_H 20 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/os.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2012-2013, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef LIBCORK_OS_H 12 | #define LIBCORK_OS_H 13 | 14 | /*** include all of the parts ***/ 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | #endif /* LIBCORK_OS_H */ 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Libraries 12 | *.lib 13 | *.a 14 | *.la 15 | *.lo 16 | 17 | # Shared objects (inc. Windows DLLs) 18 | *.dll 19 | *.so 20 | *.so.* 21 | *.dylib 22 | 23 | # Executables 24 | *.exe 25 | *.out 26 | *.app 27 | *.i*86 28 | *.x86_64 29 | *.hex 30 | 31 | # Debug files 32 | *.dSYM/ 33 | 34 | tags 35 | *.swp 36 | *.pid 37 | /core 38 | /vgcore* 39 | 40 | /Debug 41 | /Release 42 | .vscode 43 | *.opensdf 44 | *.sdf 45 | *.suo 46 | *.user 47 | .ycm_extra_conf.pyc 48 | 49 | /xSocksd 50 | /xSocks 51 | /xTproxy 52 | /xForwarder 53 | /xTunnel 54 | -------------------------------------------------------------------------------- /3rd/libcork/src/core/hash.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2011, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #define CORK_HASH_ATTRIBUTES CORK_API 12 | 13 | #include "libcork/core/hash.h" 14 | #include "libcork/core/types.h" 15 | 16 | /* All of the following functions will be defined for us by libcork/core/hash.h: 17 | * cork_hash_buffer 18 | * cork_big_hash_buffer 19 | * cork_stable_hash_buffer 20 | */ 21 | -------------------------------------------------------------------------------- /src/packet.h: -------------------------------------------------------------------------------- 1 | #ifndef PACKET_H 2 | #define PACKET_H 3 | 4 | #include "uv.h" 5 | 6 | 7 | #define HEADER_BYTES 2 8 | #define PRIMITIVE_BYTES 24 9 | #define OVERHEAD_BYTES 26 10 | #define MAX_PACKET_SIZE 2048 11 | 12 | #define PACKET_COMPLETED 0 13 | #define PACKET_INVALID 1 14 | #define PACKET_UNCOMPLETE 2 15 | 16 | 17 | struct packet { 18 | int read; 19 | uint16_t offset; 20 | uint16_t size; 21 | uint16_t max; 22 | uint8_t buf[MAX_PACKET_SIZE]; 23 | }; 24 | 25 | void packet_alloc(struct packet *packet, uv_buf_t *buf); 26 | int packet_filter(struct packet *packet, const char *buf, ssize_t buflen); 27 | void packet_reset(struct packet *packet); 28 | 29 | #endif // for #ifndef PACKET_H 30 | -------------------------------------------------------------------------------- /3rd/libcork/src/core/version.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2015, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license details. 7 | * ---------------------------------------------------------------------- 8 | */ 9 | 10 | #include "libcork/config.h" 11 | #include "libcork/core/api.h" 12 | 13 | 14 | /*----------------------------------------------------------------------- 15 | * Library version 16 | */ 17 | 18 | const char * 19 | cork_version_string(void) 20 | { 21 | return CORK_CONFIG_VERSION_STRING; 22 | } 23 | 24 | const char * 25 | cork_revision_string(void) 26 | { 27 | return CORK_CONFIG_REVISION; 28 | } 29 | -------------------------------------------------------------------------------- /src/logger.h: -------------------------------------------------------------------------------- 1 | #ifndef LOGGER_H 2 | #define LOGGER_H 3 | 4 | #include 5 | 6 | #if !defined(_WIN32) 7 | #include 8 | #else 9 | #define LOG_EMERG 0 /* system is unusable */ 10 | #define LOG_ALERT 1 /* action must be taken immediately */ 11 | #define LOG_CRIT 2 /* critical conditions */ 12 | #define LOG_ERR 3 /* error conditions */ 13 | #define LOG_WARNING 4 /* warning conditions */ 14 | #define LOG_NOTICE 5 /* normal but significant condition */ 15 | #define LOG_INFO 6 /* informational */ 16 | #define LOG_DEBUG 7 /* debug-level messages */ 17 | #endif 18 | 19 | int logger_init(int syslog, int level); 20 | void logger_exit(void); 21 | void logger_stderr(const char *msg, ...); 22 | void logger_log(uint32_t level, const char *msg, ...); 23 | 24 | #endif // for #ifndef LOGGER_H 25 | -------------------------------------------------------------------------------- /3rd/libipset/include/ipset/errors.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the LICENSE.txt file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef IPSET_ERRORS_H 12 | #define IPSET_ERRORS_H 13 | 14 | 15 | #include 16 | 17 | 18 | /*----------------------------------------------------------------------- 19 | * Error reporting 20 | */ 21 | 22 | /* Hash of "ipset.h" */ 23 | #define IPSET_ERROR 0xf2000181 24 | 25 | enum ipset_error { 26 | IPSET_IO_ERROR, 27 | IPSET_PARSE_ERROR 28 | }; 29 | 30 | 31 | #endif /* IPSET_ERRORS_H */ 32 | -------------------------------------------------------------------------------- /3rd/libipset/include/ipset/logging.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2010-2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the LICENSE.txt file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef IPSET_LOGGING_H 12 | #define IPSET_LOGGING_H 13 | 14 | 15 | #if !defined(IPSET_DEBUG) 16 | #define IPSET_DEBUG 0 17 | #endif 18 | 19 | #if IPSET_DEBUG 20 | #include 21 | #define DEBUG(...) \ 22 | do { \ 23 | fprintf(stderr, __VA_ARGS__); \ 24 | fprintf(stderr, "\n"); \ 25 | } while (0) 26 | #else 27 | #define DEBUG(...) /* no debug messages */ 28 | #endif 29 | 30 | 31 | #endif /* IPSET_LOGGING_H */ 32 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/ds.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2011, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef LIBCORK_DS_H 12 | #define LIBCORK_DS_H 13 | 14 | /*** include all of the parts ***/ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #endif /* LIBCORK_DS_H */ 27 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/os/process.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2013, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef LIBCORK_CORE_PROCESS_H 12 | #define LIBCORK_CORE_PROCESS_H 13 | 14 | #include 15 | 16 | 17 | typedef void 18 | (*cork_cleanup_function)(void); 19 | 20 | CORK_API void 21 | cork_cleanup_at_exit_named(const char *name, int priority, 22 | cork_cleanup_function function); 23 | 24 | #define cork_cleanup_at_exit(priority, function) \ 25 | cork_cleanup_at_exit_named(#function, priority, function) 26 | 27 | 28 | #endif /* LIBCORK_CORE_PROCESS_H */ 29 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/config/version.h.in: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2015, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license details. 7 | * ---------------------------------------------------------------------- 8 | */ 9 | 10 | #ifndef LIBCORK_CONFIG_VERSION_H 11 | #define LIBCORK_CONFIG_VERSION_H 12 | 13 | 14 | /*----------------------------------------------------------------------- 15 | * Library version 16 | */ 17 | 18 | #define CORK_CONFIG_VERSION_MAJOR @VERSION_MAJOR@ 19 | #define CORK_CONFIG_VERSION_MINOR @VERSION_MINOR@ 20 | #define CORK_CONFIG_VERSION_PATCH @VERSION_PATCH@ 21 | #define CORK_CONFIG_VERSION_STRING "@VERSION@" 22 | #define CORK_CONFIG_REVISION "@GIT_SHA1@" 23 | 24 | 25 | #endif /* LIBCORK_CONFIG_VERSION_H */ 26 | -------------------------------------------------------------------------------- /src/resolver.h: -------------------------------------------------------------------------------- 1 | #ifndef RESOLVER_H 2 | #define RESOLVER_H 3 | 4 | #include 5 | #include "uv.h" 6 | 7 | 8 | struct resolver_query; 9 | struct resolver_context; 10 | 11 | enum resolver_mode { 12 | MODE_IPV4, 13 | MODE_IPV6, 14 | MODE_IPV4_FIRST, 15 | MODE_IPV6_FIRST, 16 | }; 17 | 18 | typedef void (*dns_host_callback)(struct sockaddr *addr, void *data); 19 | 20 | void resolver_prepare(int nameserver_num); 21 | struct resolver_context * resolver_init(uv_loop_t *loop, int mode, char **nameservers, int nameserver_num); 22 | struct resolver_query * resolver_query(struct resolver_context *ctx, const char *host, uint16_t port, dns_host_callback cb, void *data); 23 | void resolver_cancel(struct resolver_query *); 24 | void resolver_shutdown(struct resolver_context *ctx); 25 | void resolver_destroy(struct resolver_context *ctx); 26 | const char * resolver_error(struct resolver_query *query); 27 | 28 | #endif // for #ifndef RESOLVER_H 29 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/core.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2011-2013, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license details. 7 | * ---------------------------------------------------------------------- 8 | */ 9 | 10 | #ifndef LIBCORK_CORE_H 11 | #define LIBCORK_CORE_H 12 | 13 | /*** include all of the parts ***/ 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #endif /* LIBCORK_CORE_H */ 30 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/config/bsd.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2013, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef LIBCORK_CONFIG_BSD_H 12 | #define LIBCORK_CONFIG_BSD_H 13 | 14 | /*----------------------------------------------------------------------- 15 | * Endianness 16 | */ 17 | 18 | #include 19 | 20 | #if BYTE_ORDER == BIG_ENDIAN 21 | #define CORK_CONFIG_IS_BIG_ENDIAN 1 22 | #define CORK_CONFIG_IS_LITTLE_ENDIAN 0 23 | #elif BYTE_ORDER == LITTLE_ENDIAN 24 | #define CORK_CONFIG_IS_BIG_ENDIAN 0 25 | #define CORK_CONFIG_IS_LITTLE_ENDIAN 1 26 | #else 27 | #error "Cannot determine system endianness" 28 | #endif 29 | 30 | #define CORK_HAVE_REALLOCF 1 31 | #define CORK_HAVE_PTHREADS 1 32 | 33 | 34 | #endif /* LIBCORK_CONFIG_BSD_H */ 35 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/config/linux.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2011, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef LIBCORK_CONFIG_LINUX_H 12 | #define LIBCORK_CONFIG_LINUX_H 13 | 14 | /*----------------------------------------------------------------------- 15 | * Endianness 16 | */ 17 | 18 | #include 19 | 20 | #if __BYTE_ORDER == __BIG_ENDIAN 21 | #define CORK_CONFIG_IS_BIG_ENDIAN 1 22 | #define CORK_CONFIG_IS_LITTLE_ENDIAN 0 23 | #elif __BYTE_ORDER == __LITTLE_ENDIAN 24 | #define CORK_CONFIG_IS_BIG_ENDIAN 0 25 | #define CORK_CONFIG_IS_LITTLE_ENDIAN 1 26 | #else 27 | #error "Cannot determine system endianness" 28 | #endif 29 | 30 | #define CORK_HAVE_REALLOCF 0 31 | #define CORK_HAVE_PTHREADS 1 32 | 33 | 34 | #endif /* LIBCORK_CONFIG_LINUX_H */ 35 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/config/macosx.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2011, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef LIBCORK_CONFIG_MACOSX_H 12 | #define LIBCORK_CONFIG_MACOSX_H 13 | 14 | /*----------------------------------------------------------------------- 15 | * Endianness 16 | */ 17 | 18 | #include 19 | 20 | #if BYTE_ORDER == BIG_ENDIAN 21 | #define CORK_CONFIG_IS_BIG_ENDIAN 1 22 | #define CORK_CONFIG_IS_LITTLE_ENDIAN 0 23 | #elif BYTE_ORDER == LITTLE_ENDIAN 24 | #define CORK_CONFIG_IS_BIG_ENDIAN 0 25 | #define CORK_CONFIG_IS_LITTLE_ENDIAN 1 26 | #else 27 | #error "Cannot determine system endianness" 28 | #endif 29 | 30 | #define CORK_HAVE_REALLOCF 1 31 | #define CORK_HAVE_PTHREADS 1 32 | 33 | 34 | #endif /* LIBCORK_CONFIG_MACOSX_H */ 35 | -------------------------------------------------------------------------------- /dist-build/android-build.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | if [ -z "$ANDROID_NDK_HOME" ]; then 4 | echo "You should probably set ANDROID_NDK_HOME to the directory containing" 5 | echo "the Android NDK" 6 | exit 7 | fi 8 | 9 | if [ "x$TARGET_ARCH" = 'x' ] || [ "x$ARCH" = 'x' ] || [ "x$HOST_COMPILER" = 'x' ]; then 10 | echo "You shouldn't use android-build.sh directly, use android-[arch].sh instead" 11 | exit 1 12 | fi 13 | 14 | export MAKE_TOOLCHAIN="${ANDROID_NDK_HOME}/build/tools/make-standalone-toolchain.sh" 15 | export TOOLCHAIN_DIR="$(pwd)/android-toolchain-${TARGET_ARCH}" 16 | export PREFIX="$(pwd)/xSocks-android-${TARGET_ARCH}" 17 | export PATH="${PATH}:${TOOLCHAIN_DIR}/bin" 18 | 19 | if [ ! -d $TOOLCHAIN_DIR ]; then 20 | bash $MAKE_TOOLCHAIN \ 21 | --arch=$ARCH \ 22 | --install-dir=$TOOLCHAIN_DIR \ 23 | --platform=android-9 24 | fi 25 | 26 | make CROSS="${HOST_COMPILER}-" O="${PREFIX}" android 27 | ${HOST_COMPILER}-strip --strip-unneeded $PREFIX/xSocks 28 | ${HOST_COMPILER}-strip --strip-unneeded $PREFIX/xForwarder 29 | echo "xSocks has been installed into $PREFIX" 30 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/core/id.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2013, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license details. 7 | * ---------------------------------------------------------------------- 8 | */ 9 | 10 | #ifndef LIBCORK_CORE_ID_H 11 | #define LIBCORK_CORE_ID_H 12 | 13 | #include 14 | 15 | 16 | struct cork_uid { 17 | const char *name; 18 | }; 19 | 20 | typedef const struct cork_uid *cork_uid; 21 | 22 | #define CORK_UID_NONE ((cork_uid) NULL) 23 | 24 | #define cork_uid_define_named(c_name, name) \ 25 | static const struct cork_uid c_name##__id = { name }; \ 26 | static cork_uid c_name = &c_name##__id; 27 | #define cork_uid_define(c_name) \ 28 | cork_uid_define_named(c_name, #c_name) 29 | 30 | #define cork_uid_equal(id1, id2) ((id1) == (id2)) 31 | #define cork_uid_hash(id) ((cork_hash) (uintptr_t) (id)) 32 | #define cork_uid_name(id) ((id)->name) 33 | 34 | 35 | #endif /* LIBCORK_CORE_ID_H */ 36 | -------------------------------------------------------------------------------- /3rd/libipset/src/set/allocation.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2009-2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the LICENSE.txt file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #include 12 | 13 | #include "ipset/bdd/nodes.h" 14 | #include "ipset/ipset.h" 15 | 16 | 17 | void 18 | ipset_init(struct ip_set *set) 19 | { 20 | /* The set starts empty, so every value assignment should yield 21 | * false. */ 22 | set->cache = ipset_node_cache_new(); 23 | set->set_bdd = ipset_terminal_node_id(false); 24 | } 25 | 26 | 27 | struct ip_set * 28 | ipset_new(void) 29 | { 30 | struct ip_set *result = cork_new(struct ip_set); 31 | ipset_init(result); 32 | return result; 33 | } 34 | 35 | 36 | void 37 | ipset_done(struct ip_set *set) 38 | { 39 | ipset_node_decref(set->cache, set->set_bdd); 40 | ipset_node_cache_free(set->cache); 41 | } 42 | 43 | 44 | void 45 | ipset_free(struct ip_set *set) 46 | { 47 | ipset_done(set); 48 | free(set); 49 | } 50 | -------------------------------------------------------------------------------- /openwrt/Makefile: -------------------------------------------------------------------------------- 1 | include $(TOPDIR)/rules.mk 2 | 3 | PKG_NAME:=xSocks 4 | PKG_VERSION:=0.5.1 5 | PKG_RELEASE= 6 | 7 | PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz 8 | PKG_SOURCE_PROTO:=git 9 | PKG_SOURCE_URL:=https://github.com/lparam/xSocks.git 10 | PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION) 11 | PKG_SOURCE_VERSION:=v$(PKG_VERSION) 12 | 13 | PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION) 14 | 15 | PKG_MAINTAINER:=lparam 16 | 17 | PKG_INSTALL:=1 18 | 19 | include $(INCLUDE_DIR)/package.mk 20 | 21 | define Package/xSocks/Default 22 | SECTION:=net 23 | CATEGORY:=Network 24 | TITLE:=A secure and fast proxy for protect your network traffic 25 | URL:=https://github.com/lparam/xSocks 26 | endef 27 | 28 | define Package/xSocks 29 | $(call Package/xSocks/Default) 30 | DEPENDS:=+libpthread +librt 31 | endef 32 | 33 | define Package/xSocks/description 34 | xSocks is a secure and fast proxy for protect your network traffic 35 | endef 36 | 37 | define Package/xSocks/install 38 | $(INSTALL_DIR) $(1)/etc/init.d 39 | $(INSTALL_BIN) ./files/xSocks.init $(1)/etc/init.d/xSocks 40 | $(INSTALL_DIR) $(1)/usr/bin 41 | $(INSTALL_BIN) $(PKG_BUILD_DIR)/x{Socks,Tproxy,Forwarder,Tunnel} $(1)/usr/bin 42 | endef 43 | 44 | $(eval $(call BuildPackage,xSocks)) 45 | -------------------------------------------------------------------------------- /src/udprelay.c: -------------------------------------------------------------------------------- 1 | #ifndef _UDPRELAY_C 2 | #define _UDPRELAY_C 3 | 4 | static uv_mutex_t mutex; 5 | static struct cache *cache; 6 | 7 | int 8 | udprelay_init() { 9 | uv_mutex_init(&mutex); 10 | cache_create(&cache, 1024, free_cb); 11 | return 0; 12 | } 13 | 14 | int 15 | udprelay_start(uv_loop_t *loop, struct server_context *server) { 16 | int rc; 17 | 18 | uv_udp_init(loop, &server->udp); 19 | 20 | if ((rc = uv_udp_open(&server->udp, server->udp_fd))) { 21 | logger_stderr("udp open error: %s", uv_strerror(rc)); 22 | return 1; 23 | } 24 | 25 | rc = uv_udp_bind(&server->udp, server->local_addr, UV_UDP_REUSEADDR); 26 | if (rc) { 27 | logger_stderr("bind error: %s", uv_strerror(rc)); 28 | return 1; 29 | } 30 | 31 | uv_udp_recv_start(&server->udp, client_alloc_cb, client_recv_cb); 32 | 33 | return 0; 34 | } 35 | 36 | void 37 | udprelay_close(struct server_context *server) { 38 | uv_close((uv_handle_t *)&server->udp, NULL); 39 | uv_mutex_lock(&mutex); 40 | cache_removeall(cache, server->udp.loop, select_cb); 41 | uv_mutex_unlock(&mutex); 42 | } 43 | 44 | void 45 | udprelay_destroy() { 46 | uv_mutex_destroy(&mutex); 47 | free(cache); 48 | } 49 | #endif // for #ifndef _UDPRELAY_C 50 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/config/arch.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2012-2013, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef LIBCORK_CONFIG_ARCH_H 12 | #define LIBCORK_CONFIG_ARCH_H 13 | 14 | 15 | /*----------------------------------------------------------------------- 16 | * Platform 17 | */ 18 | 19 | #if defined(__i386__) || defined(_M_IX86) 20 | #define CORK_CONFIG_ARCH_X86 1 21 | #else 22 | #define CORK_CONFIG_ARCH_X86 0 23 | #endif 24 | 25 | #if defined(__x86_64__) || defined(_M_X64) 26 | #define CORK_CONFIG_ARCH_X64 1 27 | #else 28 | #define CORK_CONFIG_ARCH_X64 0 29 | #endif 30 | 31 | #if defined(__powerpc__) || defined(__ppc__) 32 | /* GCC-ish compiler */ 33 | #define CORK_CONFIG_ARCH_PPC 1 34 | #elif defined(_M_PPC) 35 | /* VS-ish compiler */ 36 | #define CORK_CONFIG_ARCH_PPC 1 37 | #elif defined(_ARCH_PPC) 38 | /* Something called XL C/C++? */ 39 | #define CORK_CONFIG_ARCH_PPC 1 40 | #else 41 | #define CORK_CONFIG_ARCH_PPC 0 42 | #endif 43 | 44 | 45 | #endif /* LIBCORK_CONFIG_ARCH_H */ 46 | -------------------------------------------------------------------------------- /3rd/libipset/src/map/allocation.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2009-2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the LICENSE.txt file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #include 12 | 13 | #include "ipset/bdd/nodes.h" 14 | #include "ipset/ipset.h" 15 | 16 | 17 | void 18 | ipmap_init(struct ip_map *map, int default_value) 19 | { 20 | /* The map starts empty, so every value assignment should yield the 21 | * default. */ 22 | map->cache = ipset_node_cache_new(); 23 | map->default_bdd = ipset_terminal_node_id(default_value); 24 | map->map_bdd = map->default_bdd; 25 | } 26 | 27 | 28 | struct ip_map * 29 | ipmap_new(int default_value) 30 | { 31 | struct ip_map *result = cork_new(struct ip_map); 32 | ipmap_init(result, default_value); 33 | return result; 34 | } 35 | 36 | 37 | void 38 | ipmap_done(struct ip_map *map) 39 | { 40 | ipset_node_decref(map->cache, map->map_bdd); 41 | ipset_node_cache_free(map->cache); 42 | } 43 | 44 | 45 | void 46 | ipmap_free(struct ip_map *map) 47 | { 48 | ipmap_done(map); 49 | free(map); 50 | } 51 | -------------------------------------------------------------------------------- /src/common.h: -------------------------------------------------------------------------------- 1 | #ifndef COMMON_H 2 | #define COMMON_H 3 | 4 | #include "uv.h" 5 | #include "socks.h" 6 | 7 | #ifdef VERSION 8 | #define xSocks_VER VERSION 9 | #define xSocks_BUILD_TIME BUILD_TIME 10 | #else 11 | #define xSocks_VER "undefined" 12 | #define xSocks_BUILD_TIME "" 13 | #endif 14 | 15 | struct server_context { 16 | int index; 17 | int udprelay; 18 | int tcp_fd; 19 | int udp_fd; 20 | uv_tcp_t tcp; 21 | uv_udp_t udp; 22 | uv_poll_t watcher; 23 | uv_sem_t semaphore; 24 | uv_async_t async_handle; 25 | uv_thread_t thread_id; 26 | int resolver; 27 | int nameserver_num; 28 | char **nameservers; 29 | uv_connection_cb accept_cb; 30 | struct sockaddr *dest_addr; 31 | struct sockaddr *local_addr; 32 | struct sockaddr *server_addr; 33 | }; 34 | 35 | struct signal_ctx { 36 | int signum; 37 | uv_signal_t sig; 38 | }; 39 | 40 | int signal_process(char *signal, const char *pidfile); 41 | void consumer_start(void *arg); 42 | int parse_target_address(const struct xSocks_request *req, struct sockaddr *addr, char *host); 43 | void cache_log(uint8_t atyp, const struct sockaddr *src_addr, const struct sockaddr *dst_addr, const char *host, uint16_t port, int hit); 44 | int protect_socket(int fd); 45 | 46 | #endif // for #ifndef COMMON_H 47 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/core/callbacks.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2013-2014, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license details. 7 | * ---------------------------------------------------------------------- 8 | */ 9 | 10 | #ifndef LIBCORK_CORE_CALLBACKS_H 11 | #define LIBCORK_CORE_CALLBACKS_H 12 | 13 | 14 | #include 15 | 16 | 17 | typedef int 18 | (*cork_copy_f)(void *user_data, void *dest, const void *src); 19 | 20 | typedef void 21 | (*cork_done_f)(void *user_data, void *value); 22 | 23 | typedef void 24 | (*cork_free_f)(void *value); 25 | 26 | typedef cork_hash 27 | (*cork_hash_f)(void *user_data, const void *value); 28 | 29 | typedef bool 30 | (*cork_equals_f)(void *user_data, const void *value1, const void *value2); 31 | 32 | typedef void 33 | (*cork_init_f)(void *user_data, void *value); 34 | 35 | #define cork_free_user_data(parent) \ 36 | ((parent)->free_user_data == NULL? (void) 0: \ 37 | (parent)->free_user_data((parent)->user_data)) 38 | 39 | typedef void * 40 | (*cork_new_f)(void *user_data); 41 | 42 | typedef int 43 | (*cork_run_f)(void *user_data); 44 | 45 | 46 | #endif /* LIBCORK_CORE_CALLBACKS_H */ 47 | -------------------------------------------------------------------------------- /src/cache.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Original Author: Oliver Lorenz (ol), olli@olorenz.org, https://olorenz.org 3 | * License: This is licensed under the same terms as uthash itself 4 | */ 5 | 6 | #ifndef CACHE_H 7 | #define CACHE_H 8 | 9 | #include "uthash.h" 10 | 11 | #define KEY_MAX_LENGTH 32 12 | 13 | /** 14 | * A cache entry 15 | */ 16 | struct cache_entry { 17 | char *key; /** 16 | #include 17 | 18 | 19 | #define _free_(name) \ 20 | static void \ 21 | name##__free(void *obj) 22 | 23 | 24 | #define _recurse_(name) \ 25 | static void \ 26 | name##__recurse(struct cork_gc *gc, void *obj, \ 27 | cork_gc_recurser recurse, void *ud) 28 | 29 | 30 | #define _gc_(name) \ 31 | static struct cork_gc_obj_iface name##__gc = { \ 32 | name##__free, name##__recurse \ 33 | }; 34 | 35 | #define _gc_no_free_(name) \ 36 | static struct cork_gc_obj_iface name##__gc = { \ 37 | NULL, name##__recurse \ 38 | }; 39 | 40 | #define _gc_no_recurse_(name) \ 41 | static struct cork_gc_obj_iface name##__gc = { \ 42 | name##__free, NULL \ 43 | }; 44 | 45 | #define _gc_leaf_(name) \ 46 | static struct cork_gc_obj_iface name##__gc = { \ 47 | NULL, NULL \ 48 | }; 49 | 50 | 51 | #endif /* LIBCORK_HELPERS_REFCOUNT_H */ 52 | -------------------------------------------------------------------------------- /3rd/libipset/src/map/ipv4_map.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2009-2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the LICENSE.txt file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | /* 12 | * The IPv4 and IPv6 map types are basically identical, except for the 13 | * names of the functions, and the size of the values that are being 14 | * stored. Rather than having two mostly duplicate definitions of each 15 | * function, we define “template functions” where anything that depends 16 | * on the size of the IP address is defined using the following macros. 17 | */ 18 | 19 | 20 | /* The name of the cork_ipvX type. */ 21 | #define CORK_IP struct cork_ipv4 22 | 23 | /* The number of bits in an IPvX address. */ 24 | #define IP_BIT_SIZE 32 25 | 26 | /* The value of the discriminator variable for an IPvX address. */ 27 | #define IP_DISCRIMINATOR_VALUE true 28 | 29 | /* Creates a identifier of the form “ipset_ipv4_”. */ 30 | #define IPSET_NAME(basename) ipset_ipv4_##basename 31 | 32 | /* Creates a identifier of the form “ipmap_ipv4_”. */ 33 | #define IPMAP_NAME(basename) ipmap_ipv4_##basename 34 | 35 | 36 | /* Now include all of the templates. */ 37 | #include "inspection-template.c.in" 38 | -------------------------------------------------------------------------------- /3rd/libipset/src/map/ipv6_map.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2009-2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the LICENSE.txt file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | /* 12 | * The IPv4 and IPv6 map types are basically identical, except for the 13 | * names of the functions, and the size of the values that are being 14 | * stored. Rather than having two mostly duplicate definitions of each 15 | * function, we define “template functions” where anything that depends 16 | * on the size of the IP address is defined using the following macros. 17 | */ 18 | 19 | 20 | /* The name of the cork_ipvX type. */ 21 | #define CORK_IP struct cork_ipv6 22 | 23 | /* The number of bits in an IPvX address. */ 24 | #define IP_BIT_SIZE 128 25 | 26 | /* The value of the discriminator variable for an IPvX address. */ 27 | #define IP_DISCRIMINATOR_VALUE false 28 | 29 | /* Creates a identifier of the form “ipset_ipv6_”. */ 30 | #define IPSET_NAME(basename) ipset_ipv6_##basename 31 | 32 | /* Creates a identifier of the form “ipmap_ipv6_”. */ 33 | #define IPMAP_NAME(basename) ipmap_ipv6_##basename 34 | 35 | 36 | /* Now include all of the templates. */ 37 | #include "inspection-template.c.in" 38 | -------------------------------------------------------------------------------- /3rd/libipset/src/set/ipv4_set.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2009-2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the LICENSE.txt file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | /* 12 | * The IPv4 and IPv6 set types are basically identical, except for the 13 | * names of the functions, and the size of the values that are being 14 | * stored. Rather than having two mostly duplicate definitions of each 15 | * function, we define “template functions” where anything that depends 16 | * on the size of the IP address is defined using the following macros. 17 | */ 18 | 19 | 20 | /* The name of the cork_ipvX type. */ 21 | #define CORK_IP struct cork_ipv4 22 | 23 | /* The number of bits in an IPvX address. */ 24 | #define IP_BIT_SIZE 32 25 | 26 | /* The value of the discriminator variable for an IPvX address. */ 27 | #define IP_DISCRIMINATOR_VALUE true 28 | 29 | /* Creates a identifier of the form “ipset_ipv4_”. */ 30 | #define IPSET_NAME(basename) ipset_ipv4_##basename 31 | 32 | /* Creates a identifier of the form “ipset__ipv4”. */ 33 | #define IPSET_PRENAME(basename) ipset_##basename##_ipv4 34 | 35 | 36 | /* Now include all of the templates. */ 37 | #include "inspection-template.c.in" 38 | -------------------------------------------------------------------------------- /3rd/libancillary/COPYING: -------------------------------------------------------------------------------- 1 | Redistribution and use in source and binary forms, with or without 2 | modification, are permitted provided that the following conditions are met: 3 | 4 | 1. Redistributions of source code must retain the above copyright notice, 5 | this list of conditions and the following disclaimer. 6 | 2. Redistributions in binary form must reproduce the above copyright 7 | notice, this list of conditions and the following disclaimer in the 8 | documentation and/or other materials provided with the distribution. 9 | 3. The name of the author may not be used to endorse or promote products 10 | derived from this software without specific prior written permission. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 13 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 14 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 15 | EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 16 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 17 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 18 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 19 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 20 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 21 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22 | -------------------------------------------------------------------------------- /3rd/libipset/src/set/ipv6_set.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2009-2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the LICENSE.txt file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | /* 12 | * The IPv4 and IPv6 set types are basically identical, except for the 13 | * names of the functions, and the size of the values that are being 14 | * stored. Rather than having two mostly duplicate definitions of 15 | * each function, we define “template functions” where anything that 16 | * depends on the size of the IP address is defined using the 17 | * following macros. 18 | */ 19 | 20 | 21 | /* The name of the cork_ipvX type. */ 22 | #define CORK_IP struct cork_ipv6 23 | 24 | /* The number of bits in an IPvX address. */ 25 | #define IP_BIT_SIZE 128 26 | 27 | /* The value of the discriminator variable for an IPvX address. */ 28 | #define IP_DISCRIMINATOR_VALUE false 29 | 30 | /* Creates a identifier of the form “ipset_ipv6_”. */ 31 | #define IPSET_NAME(basename) ipset_ipv6_##basename 32 | 33 | /* Creates a identifier of the form “ipset__ipv6”. */ 34 | #define IPSET_PRENAME(basename) ipset_##basename##_ipv6 35 | 36 | 37 | /* Now include all of the templates. */ 38 | #include "inspection-template.c.in" 39 | -------------------------------------------------------------------------------- /3rd/libcork/src/ds/bitset.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2013-2014, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license details. 7 | * ---------------------------------------------------------------------- 8 | */ 9 | 10 | #include 11 | 12 | #include "libcork/core/allocator.h" 13 | #include "libcork/core/api.h" 14 | #include "libcork/core/types.h" 15 | #include "libcork/ds/bitset.h" 16 | 17 | 18 | static size_t 19 | bytes_needed(size_t bit_count) 20 | { 21 | /* We need one byte for every bit... */ 22 | size_t bytes_needed = bit_count / 8; 23 | /* Plus one extra for the leftovers that don't fit into a whole byte. */ 24 | bytes_needed += ((bit_count % 8) > 0); 25 | return bytes_needed; 26 | } 27 | 28 | struct cork_bitset * 29 | cork_bitset_new(size_t bit_count) 30 | { 31 | struct cork_bitset *set = cork_new(struct cork_bitset); 32 | set->bit_count = bit_count; 33 | set->byte_count = bytes_needed(bit_count); 34 | set->bits = cork_malloc(set->byte_count); 35 | memset(set->bits, 0, set->byte_count); 36 | return set; 37 | } 38 | 39 | void 40 | cork_bitset_free(struct cork_bitset *set) 41 | { 42 | cork_free(set->bits, set->byte_count); 43 | cork_delete(struct cork_bitset, set); 44 | } 45 | 46 | void 47 | cork_bitset_clear(struct cork_bitset *set) 48 | { 49 | memset(set->bits, 0, set->byte_count); 50 | } 51 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/cli/commands.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef LIBCORK_COMMANDS_H 12 | #define LIBCORK_COMMANDS_H 13 | 14 | #include 15 | 16 | 17 | typedef void 18 | (*cork_leaf_command_run)(int argc, char **argv); 19 | 20 | typedef int 21 | (*cork_option_parser)(int argc, char **argv); 22 | 23 | enum cork_command_type { 24 | CORK_COMMAND_SET, 25 | CORK_LEAF_COMMAND 26 | }; 27 | 28 | struct cork_command { 29 | enum cork_command_type type; 30 | const char *name; 31 | const char *short_desc; 32 | const char *usage_suffix; 33 | const char *full_help; 34 | 35 | int 36 | (*parse_options)(int argc, char **argv); 37 | 38 | struct cork_command **set; 39 | cork_leaf_command_run run; 40 | }; 41 | 42 | #define cork_command_set(name, sd, parse_options, set) \ 43 | { \ 44 | CORK_COMMAND_SET, name, sd, NULL, NULL, \ 45 | parse_options, set, NULL \ 46 | } 47 | 48 | #define cork_leaf_command(name, sd, us, fh, parse_options, run) \ 49 | { \ 50 | CORK_LEAF_COMMAND, name, sd, us, fh, \ 51 | parse_options, NULL, run \ 52 | } 53 | 54 | CORK_API void 55 | cork_command_show_help(struct cork_command *command, const char *message); 56 | 57 | CORK_API int 58 | cork_command_main(struct cork_command *root, int argc, char **argv); 59 | 60 | 61 | #endif /* LIBCORK_COMMANDS_H */ 62 | -------------------------------------------------------------------------------- /xSocks.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.31101.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "xSocks", "xSocks.vcxproj", "{5DFFAC1E-81E2-4F03-AFBE-2984BF91300D}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Debug|Mixed Platforms = Debug|Mixed Platforms 12 | Debug|Win32 = Debug|Win32 13 | Release|Any CPU = Release|Any CPU 14 | Release|Mixed Platforms = Release|Mixed Platforms 15 | Release|Win32 = Release|Win32 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {5DFFAC1E-81E2-4F03-AFBE-2984BF91300D}.Debug|Any CPU.ActiveCfg = Debug|Win32 19 | {5DFFAC1E-81E2-4F03-AFBE-2984BF91300D}.Debug|Mixed Platforms.ActiveCfg = Debug|Win32 20 | {5DFFAC1E-81E2-4F03-AFBE-2984BF91300D}.Debug|Mixed Platforms.Build.0 = Debug|Win32 21 | {5DFFAC1E-81E2-4F03-AFBE-2984BF91300D}.Debug|Win32.ActiveCfg = Debug|Win32 22 | {5DFFAC1E-81E2-4F03-AFBE-2984BF91300D}.Debug|Win32.Build.0 = Debug|Win32 23 | {5DFFAC1E-81E2-4F03-AFBE-2984BF91300D}.Release|Any CPU.ActiveCfg = Release|Win32 24 | {5DFFAC1E-81E2-4F03-AFBE-2984BF91300D}.Release|Mixed Platforms.ActiveCfg = Release|Win32 25 | {5DFFAC1E-81E2-4F03-AFBE-2984BF91300D}.Release|Mixed Platforms.Build.0 = Release|Win32 26 | {5DFFAC1E-81E2-4F03-AFBE-2984BF91300D}.Release|Win32.ActiveCfg = Release|Win32 27 | {5DFFAC1E-81E2-4F03-AFBE-2984BF91300D}.Release|Win32.Build.0 = Release|Win32 28 | EndGlobalSection 29 | GlobalSection(SolutionProperties) = preSolution 30 | HideSolutionNode = FALSE 31 | EndGlobalSection 32 | EndGlobal 33 | -------------------------------------------------------------------------------- /3rd/libcork/src/ds/dllist.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2011-2014, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license details. 7 | * ---------------------------------------------------------------------- 8 | */ 9 | 10 | #include "libcork/core/api.h" 11 | #include "libcork/core/types.h" 12 | #include "libcork/ds/dllist.h" 13 | 14 | 15 | /* Include a linkable (but deprecated) version of this to maintain ABI 16 | * compatibility. */ 17 | #undef cork_dllist_init 18 | CORK_API void 19 | cork_dllist_init(struct cork_dllist *list) 20 | { 21 | list->head.next = &list->head; 22 | list->head.prev = &list->head; 23 | } 24 | 25 | 26 | void 27 | cork_dllist_map(struct cork_dllist *list, 28 | cork_dllist_map_func func, void *user_data) 29 | { 30 | struct cork_dllist_item *curr; 31 | struct cork_dllist_item *next; 32 | cork_dllist_foreach_void(list, curr, next) { 33 | func(curr, user_data); 34 | } 35 | } 36 | 37 | int 38 | cork_dllist_visit(struct cork_dllist *list, void *ud, 39 | cork_dllist_visit_f *visit) 40 | { 41 | struct cork_dllist_item *curr; 42 | struct cork_dllist_item *next; 43 | cork_dllist_foreach_void(list, curr, next) { 44 | int rc = visit(ud, curr); 45 | if (rc != 0) { 46 | return rc; 47 | } 48 | } 49 | return 0; 50 | } 51 | 52 | 53 | size_t 54 | cork_dllist_size(const struct cork_dllist *list) 55 | { 56 | size_t size = 0; 57 | struct cork_dllist_item *curr; 58 | struct cork_dllist_item *next; 59 | cork_dllist_foreach_void(list, curr, next) { 60 | size++; 61 | } 62 | return size; 63 | } 64 | -------------------------------------------------------------------------------- /src/socks.h: -------------------------------------------------------------------------------- 1 | #ifndef SOCKS_H 2 | #define SOCKS_H 3 | 4 | #pragma pack(push,1) 5 | 6 | struct socks5_method_request { 7 | char ver; 8 | char nmethods; 9 | char methods[255]; 10 | }; 11 | 12 | struct socks5_method_response { 13 | char ver; 14 | char method; 15 | }; 16 | 17 | struct xSocks_request { 18 | char atyp; 19 | char addr[0]; 20 | }; 21 | 22 | struct socks5_request { 23 | char ver; 24 | char cmd; 25 | char rsv; 26 | char atyp; 27 | char addr[0]; 28 | }; 29 | 30 | struct socks5_response { 31 | char ver; 32 | char rep; 33 | char rsv; 34 | char atyp; 35 | }; 36 | 37 | #pragma pack(pop) 38 | 39 | enum s5_auth_method { 40 | S5_AUTH_NONE = 0x00, 41 | S5_AUTH_GSSAPI = 0x01, 42 | S5_AUTH_PASSWD = 0x02, 43 | }; 44 | 45 | enum s5_auth_result { 46 | S5_AUTH_ALLOW = 0x00, 47 | S5_AUTH_DENY = 0x01, 48 | }; 49 | 50 | enum xSocks_atyp { 51 | ATYP_IPV4 = 0x01, 52 | ATYP_HOST = 0x03, 53 | ATYP_IPV6 = 0x04, 54 | }; 55 | 56 | enum s5_cmd { 57 | S5_CMD_CONNECT = 0x01, 58 | S5_CMD_BIND = 0x02, 59 | S5_CMD_UDP_ASSOCIATE = 0x03, 60 | }; 61 | 62 | enum s5_rep { 63 | S5_REP_SUCCESSED = 0X00, 64 | S5_REP_SOCKS_FAILURE = 0X01, 65 | S5_REP_RULESET_DENY = 0X02, 66 | S5_REP_NETWORK_UNREACHABLE = 0X03, 67 | S5_REP_HOST_UNREACHABLE = 0X04, 68 | S5_REP_CONNECTION_REFUSED = 0X05, 69 | S5_REP_TTL_EXPIRED = 0X06, 70 | S5_REP_CMD_NOT_SUPPORTED = 0X07, 71 | S5_REP_ADDRESS_TYPE_NOT_SUPPORTED = 0X08, 72 | S5_REP_UNASSIGNED = 0X09, 73 | }; 74 | 75 | enum xstage { 76 | XSTAGE_HANDSHAKE, 77 | XSTAGE_REQUEST, 78 | XSTAGE_RESOLVE, 79 | XSTAGE_CONNECT, 80 | XSTAGE_UDP_RELAY, 81 | XSTAGE_FORWARD, 82 | XSTAGE_TERMINATE, 83 | XSTAGE_DEAD, 84 | }; 85 | 86 | #endif // for #ifndef SOCKS_H 87 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/core/api.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2012-2015, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license details. 7 | * ---------------------------------------------------------------------- 8 | */ 9 | 10 | #ifndef LIBCORK_CORE_API_H 11 | #define LIBCORK_CORE_API_H 12 | 13 | #include 14 | #include 15 | 16 | 17 | /*----------------------------------------------------------------------- 18 | * Calling conventions 19 | */ 20 | 21 | /* If you're using libcork as a shared library, you don't need to do anything 22 | * special; the following will automatically set things up so that libcork's 23 | * public symbols are imported from the library. When we build the shared 24 | * library, we define this ourselves to export the symbols. */ 25 | 26 | #if !defined(CORK_API) 27 | #define CORK_API CORK_IMPORT 28 | #endif 29 | 30 | 31 | /*----------------------------------------------------------------------- 32 | * Library version 33 | */ 34 | 35 | #define CORK_VERSION_MAJOR CORK_CONFIG_VERSION_MAJOR 36 | #define CORK_VERSION_MINOR CORK_CONFIG_VERSION_MINOR 37 | #define CORK_VERSION_PATCH CORK_CONFIG_VERSION_PATCH 38 | 39 | #define CORK_MAKE_VERSION(major, minor, patch) \ 40 | ((major * 1000000) + (minor * 1000) + patch) 41 | 42 | #define CORK_VERSION \ 43 | CORK_MAKE_VERSION(CORK_VERSION_MAJOR, \ 44 | CORK_VERSION_MINOR, \ 45 | CORK_VERSION_PATCH) 46 | 47 | CORK_API const char * 48 | cork_version_string(void) 49 | CORK_ATTR_CONST; 50 | 51 | CORK_API const char * 52 | cork_revision_string(void) 53 | CORK_ATTR_CONST; 54 | 55 | 56 | #endif /* LIBCORK_CORE_API_H */ 57 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/ds/ring-buffer.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2011, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef LIBCORK_DS_RING_BUFFER_H 12 | #define LIBCORK_DS_RING_BUFFER_H 13 | 14 | #include 15 | #include 16 | 17 | 18 | struct cork_ring_buffer { 19 | /* The elements of the ring buffer */ 20 | void **elements; 21 | /* The number of elements that can be stored in this ring 22 | * buffer. */ 23 | size_t allocated_size; 24 | /* The actual number of elements currently in the ring buffer. */ 25 | size_t size; 26 | /* The index of the next element to read from the buffer */ 27 | size_t read_index; 28 | /* The index of the next element to write into the buffer */ 29 | size_t write_index; 30 | }; 31 | 32 | 33 | CORK_API int 34 | cork_ring_buffer_init(struct cork_ring_buffer *buf, size_t size); 35 | 36 | CORK_API struct cork_ring_buffer * 37 | cork_ring_buffer_new(size_t size); 38 | 39 | CORK_API void 40 | cork_ring_buffer_done(struct cork_ring_buffer *buf); 41 | 42 | CORK_API void 43 | cork_ring_buffer_free(struct cork_ring_buffer *buf); 44 | 45 | 46 | #define cork_ring_buffer_is_empty(buf) ((buf)->size == 0) 47 | #define cork_ring_buffer_is_full(buf) ((buf)->size == (buf)->allocated_size) 48 | 49 | 50 | CORK_API int 51 | cork_ring_buffer_add(struct cork_ring_buffer *buf, void *element); 52 | 53 | CORK_API void * 54 | cork_ring_buffer_pop(struct cork_ring_buffer *buf); 55 | 56 | CORK_API void * 57 | cork_ring_buffer_peek(struct cork_ring_buffer *buf); 58 | 59 | 60 | #endif /* LIBCORK_DS_RING_BUFFER_H */ 61 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/core/gc.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2011, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef LIBCORK_GC_REFCOUNT_H 12 | #define LIBCORK_GC_REFCOUNT_H 13 | 14 | 15 | #include 16 | #include 17 | 18 | 19 | struct cork_gc; 20 | 21 | /* A callback for recursing through the children of a garbage-collected 22 | * object. */ 23 | typedef void 24 | (*cork_gc_recurser)(struct cork_gc *gc, void *obj, void *ud); 25 | 26 | typedef void 27 | (*cork_gc_free_func)(void *obj); 28 | 29 | typedef void 30 | (*cork_gc_recurse_func)(struct cork_gc *gc, void *self, 31 | cork_gc_recurser recurser, void *ud); 32 | 33 | /* An interface that each garbage-collected object must implement. */ 34 | struct cork_gc_obj_iface { 35 | /* Perform additional cleanup; does *NOT* need to deallocate the 36 | * object itself, or release any child references */ 37 | cork_gc_free_func free; 38 | cork_gc_recurse_func recurse; 39 | }; 40 | 41 | 42 | CORK_API void 43 | cork_gc_init(void); 44 | 45 | CORK_API void 46 | cork_gc_done(void); 47 | 48 | 49 | CORK_API void * 50 | cork_gc_alloc(size_t instance_size, struct cork_gc_obj_iface *iface); 51 | 52 | #define cork_gc_new_iface(obj_type, iface) \ 53 | ((obj_type *) \ 54 | (cork_gc_alloc(sizeof(obj_type), (iface)))) 55 | 56 | #define cork_gc_new(struct_name) \ 57 | (cork_gc_new_iface(struct struct_name, &struct_name##__gc)) 58 | 59 | 60 | CORK_API void * 61 | cork_gc_incref(void *obj); 62 | 63 | CORK_API void 64 | cork_gc_decref(void *obj); 65 | 66 | 67 | #endif /* LIBCORK_GC_REFCOUNT_H */ 68 | -------------------------------------------------------------------------------- /src/xTunnel.h: -------------------------------------------------------------------------------- 1 | #ifndef XTUNNEL_H 2 | #define XTUNNEL_H 3 | 4 | #include "uv.h" 5 | #include "socks.h" 6 | #include "packet.h" 7 | 8 | 9 | #ifdef VERSION 10 | #define xTunnel_VER VERSION 11 | #define xTunnel_BUILD_TIME BUILD_TIME 12 | #else 13 | #define xTunnel_VER "undefined" 14 | #define xTunnel_BUILD_TIME "" 15 | #endif 16 | 17 | struct source_context { 18 | int stage; 19 | union { 20 | uv_handle_t handle; 21 | uv_stream_t stream; 22 | uv_tcp_t tcp; 23 | uv_udp_t udp; 24 | } handle; 25 | uv_write_t write_req; 26 | struct packet packet; 27 | struct target_context *target; 28 | }; 29 | 30 | struct target_context { 31 | union { 32 | uv_handle_t handle; 33 | uv_stream_t stream; 34 | uv_tcp_t tcp; 35 | uv_udp_t udp; 36 | } handle; 37 | uv_write_t write_req; 38 | uv_connect_t connect_req; 39 | struct packet packet; 40 | struct source_context *source; 41 | }; 42 | 43 | enum tunnel_mode { 44 | TUNNEL_MODE_CLIENT, 45 | TUNNEL_MODE_SERVER, 46 | }; 47 | 48 | enum tunnel_stage { 49 | TUNNEL_STAGE_FORWARD, 50 | TUNNEL_STAGE_DEAD, 51 | }; 52 | 53 | struct source_context * new_source(); 54 | void close_source(struct source_context *source); 55 | void source_accept_cb(uv_stream_t *server, int status); 56 | void forward_to_source(struct source_context *source, uint8_t *buf, int buflen); 57 | void receive_from_source(struct source_context *source); 58 | 59 | struct target_context * new_target(); 60 | void close_target(struct target_context *target); 61 | void connect_to_target(struct target_context *target); 62 | void forward_to_target(struct target_context *target, uint8_t *buf, int buflen); 63 | void receive_from_target(struct target_context *target); 64 | 65 | int verbose; 66 | enum tunnel_mode mode; 67 | struct sockaddr_storage target_addr; 68 | 69 | #endif // for #ifndef XTUNNEL_H 70 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/ds/stream.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2011, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef LIBCORK_DS_STREAM_H 12 | #define LIBCORK_DS_STREAM_H 13 | 14 | #include 15 | 16 | #include 17 | #include 18 | 19 | 20 | struct cork_stream_consumer { 21 | int 22 | (*data)(struct cork_stream_consumer *consumer, 23 | const void *buf, size_t size, bool is_first_chunk); 24 | 25 | int 26 | (*eof)(struct cork_stream_consumer *consumer); 27 | 28 | void 29 | (*free)(struct cork_stream_consumer *consumer); 30 | }; 31 | 32 | 33 | #define cork_stream_consumer_data(consumer, buf, size, is_first) \ 34 | ((consumer)->data((consumer), (buf), (size), (is_first))) 35 | 36 | #define cork_stream_consumer_eof(consumer) \ 37 | ((consumer)->eof((consumer))) 38 | 39 | #define cork_stream_consumer_free(consumer) \ 40 | ((consumer)->free((consumer))) 41 | 42 | 43 | CORK_API int 44 | cork_consume_fd(struct cork_stream_consumer *consumer, int fd); 45 | 46 | CORK_API int 47 | cork_consume_file(struct cork_stream_consumer *consumer, FILE *fp); 48 | 49 | CORK_API int 50 | cork_consume_file_from_path(struct cork_stream_consumer *consumer, 51 | const char *path, int flags); 52 | 53 | 54 | CORK_API struct cork_stream_consumer * 55 | cork_fd_consumer_new(int fd); 56 | 57 | CORK_API struct cork_stream_consumer * 58 | cork_file_consumer_new(FILE *fp); 59 | 60 | CORK_API struct cork_stream_consumer * 61 | cork_file_from_path_consumer_new(const char *path, int flags); 62 | 63 | 64 | #endif /* LIBCORK_DS_STREAM_H */ 65 | -------------------------------------------------------------------------------- /3rd/libipset/include/ipset/bits.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the LICENSE.txt file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef IPSET_BITS_H 12 | #define IPSET_BITS_H 13 | 14 | #include 15 | 16 | /*----------------------------------------------------------------------- 17 | * Bit arrays 18 | */ 19 | 20 | /** 21 | * Extract the byte that contains a particular bit in an array. 22 | */ 23 | #define IPSET_BIT_GET_BYTE(array, i) \ 24 | (((uint8_t *) (array))[(i) / 8]) 25 | 26 | /** 27 | * Create a bit mask that extracts a particular bit from the byte that 28 | * it lives in. 29 | */ 30 | #define IPSET_BIT_ON_MASK(i) \ 31 | (0x80 >> ((i) % 8)) 32 | 33 | /** 34 | * Create a bit mask that extracts everything except for a particular 35 | * bit from the byte that it lives in. 36 | */ 37 | #define IPSET_BIT_NEG_MASK(i) \ 38 | (~IPSET_BIT_ON_MASK(i)) 39 | 40 | /** 41 | * Return whether a particular bit is set in a byte array. Bits are 42 | * numbered from 0, in a big-endian order. 43 | */ 44 | #define IPSET_BIT_GET(array, i) \ 45 | ((IPSET_BIT_GET_BYTE(array, i) & \ 46 | IPSET_BIT_ON_MASK(i)) != 0) 47 | 48 | /** 49 | * Set (or unset) a particular bit is set in a byte array. Bits are 50 | * numbered from 0, in a big-endian order. 51 | */ 52 | #define IPSET_BIT_SET(array, i, val) \ 53 | (IPSET_BIT_GET_BYTE(array, i) = \ 54 | (IPSET_BIT_GET_BYTE(array, i) & IPSET_BIT_NEG_MASK(i)) \ 55 | | ((val)? IPSET_BIT_ON_MASK(i): 0)) 56 | 57 | 58 | #endif /* IPSET_BITS_H */ 59 | -------------------------------------------------------------------------------- /src/signal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "logger.h" 10 | 11 | 12 | #define LF (uint8_t) 10 13 | #define CR (uint8_t) 13 14 | 15 | 16 | static ssize_t 17 | read_file(int fd, uint8_t *buf, size_t size, off_t offset) { 18 | #ifdef CROSS_COMPILE 19 | extern ssize_t pread(int fd, void *buf, size_t count, off_t offset); 20 | #endif 21 | ssize_t n = pread(fd, buf, size, offset); 22 | if (n == -1) { 23 | return -1; 24 | } 25 | return n; 26 | } 27 | 28 | static int32_t 29 | get_pid(const char *pidfile) { 30 | uint8_t buf[10] = {0}; 31 | ssize_t n; 32 | 33 | int fd = open(pidfile, O_RDONLY); 34 | 35 | if (fd == -1) { 36 | return -1; 37 | } 38 | 39 | n = read_file(fd, buf, 10, 0); 40 | 41 | close(fd); 42 | 43 | if (n == -1) { 44 | return -1; 45 | } 46 | 47 | while (n-- && (buf[n] == CR || buf[n] == LF)) { /* void */ } 48 | 49 | return atoi((const char*)buf); 50 | } 51 | 52 | int 53 | signal_process(char *signal, const char *pidfile) { 54 | int32_t pid; 55 | 56 | pid = get_pid(pidfile); 57 | if (pid == -1) { 58 | logger_stderr("open \"%s\" failed (%d: %s)", pidfile, errno, strerror(errno)); 59 | return 1; 60 | } 61 | 62 | if (strcmp(signal, "stop") == 0) { 63 | if (kill(pid, SIGTERM) != -1) { 64 | unlink(pidfile); 65 | return 0; 66 | } else { 67 | logger_stderr("stop failed (%d: %s)", errno, strerror(errno)); 68 | } 69 | } 70 | if (strcmp(signal, "quit") == 0) { 71 | if (kill(pid, SIGQUIT) != -1) { 72 | return 0; 73 | } else { 74 | logger_stderr("quit failed (%d: %s)", errno, strerror(errno)); 75 | } 76 | } 77 | 78 | return 1; 79 | } 80 | -------------------------------------------------------------------------------- /src/android.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #include "util.h" 17 | #include "logger.h" 18 | #include "ancillary.h" 19 | 20 | 21 | int 22 | protect_socket(int fd) { 23 | int sock; 24 | struct sockaddr_un addr; 25 | 26 | if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { 27 | logger_log(LOG_ERR, "[android] socket() failed: %s (socket fd = %d)\n", strerror(errno), sock); 28 | return -1; 29 | } 30 | 31 | // Set timeout to 100us 32 | struct timeval tv; 33 | tv.tv_sec = 1; /* 0 Secs Timeout */ 34 | tv.tv_usec = 0; // Not init'ing this can cause strange errors 35 | setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)); 36 | setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval)); 37 | 38 | const char path[] = "/data/data/io.github.xSocks/protect_path"; 39 | 40 | memset(&addr, 0, sizeof(addr)); 41 | addr.sun_family = AF_UNIX; 42 | strncpy(addr.sun_path, path, sizeof(addr.sun_path)-1); 43 | 44 | if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) { 45 | logger_log(LOG_ERR, "[android] connect() failed: %s (socket fd = %d)\n", strerror(errno), sock); 46 | close(sock); 47 | return -1; 48 | } 49 | 50 | if (ancil_send_fd(sock, fd)) { 51 | logger_log(LOG_ERR, "[android] ancil_send_fd: %d", fd); 52 | close(sock); 53 | return -1; 54 | } 55 | 56 | char ret = 0; 57 | 58 | if (recv(sock, &ret, 1, 0) == -1) { 59 | logger_log(LOG_ERR, "[android] recv"); 60 | close(sock); 61 | return -1; 62 | } 63 | 64 | close(sock); 65 | return ret; 66 | } 67 | -------------------------------------------------------------------------------- /3rd/libipset/src/map/inspection.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2009-2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the LICENSE.txt file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #include 12 | 13 | #include "ipset/bdd/nodes.h" 14 | #include "ipset/ipset.h" 15 | 16 | bool 17 | ipmap_is_empty(const struct ip_map *map) 18 | { 19 | /* Since BDDs are unique, any map that maps all addresses to the 20 | * default value is “empty”. */ 21 | return (map->map_bdd == map->default_bdd); 22 | } 23 | 24 | bool 25 | ipmap_is_equal(const struct ip_map *map1, const struct ip_map *map2) 26 | { 27 | return ipset_node_cache_nodes_equal 28 | (map1->cache, map1->map_bdd, map2->cache, map2->map_bdd); 29 | } 30 | 31 | size_t 32 | ipmap_memory_size(const struct ip_map *map) 33 | { 34 | return ipset_node_memory_size(map->cache, map->map_bdd); 35 | } 36 | 37 | 38 | void 39 | ipmap_ip_set(struct ip_map *map, struct cork_ip *addr, int value) 40 | { 41 | if (addr->version == 4) { 42 | ipmap_ipv4_set(map, &addr->ip.v4, value); 43 | } else { 44 | ipmap_ipv6_set(map, &addr->ip.v6, value); 45 | } 46 | } 47 | 48 | 49 | void 50 | ipmap_ip_set_network(struct ip_map *map, struct cork_ip *addr, 51 | unsigned int cidr_prefix, int value) 52 | { 53 | if (addr->version == 4) { 54 | ipmap_ipv4_set_network(map, &addr->ip.v4, cidr_prefix, value); 55 | } else { 56 | ipmap_ipv6_set_network(map, &addr->ip.v6, cidr_prefix, value); 57 | } 58 | } 59 | 60 | 61 | int 62 | ipmap_ip_get(struct ip_map *map, struct cork_ip *addr) 63 | { 64 | if (addr->version == 4) { 65 | return ipmap_ipv4_get(map, &addr->ip.v4); 66 | } else { 67 | return ipmap_ipv6_get(map, &addr->ip.v6); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/threads/atomics.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef LIBCORK_THREADS_ATOMICS_H 12 | #define LIBCORK_THREADS_ATOMICS_H 13 | 14 | #include 15 | #include 16 | 17 | /*----------------------------------------------------------------------- 18 | * GCC intrinsics 19 | */ 20 | 21 | /* Ideally we can use GCC's intrinsics to define everything */ 22 | #if defined(CORK_CONFIG_HAVE_GCC_ATOMICS) 23 | 24 | #define cork_int_atomic_add __sync_add_and_fetch 25 | #define cork_uint_atomic_add __sync_add_and_fetch 26 | #define cork_size_atomic_add __sync_add_and_fetch 27 | #define cork_int_atomic_pre_add __sync_fetch_and_add 28 | #define cork_uint_atomic_pre_add __sync_fetch_and_add 29 | #define cork_size_atomic_pre_add __sync_fetch_and_add 30 | #define cork_int_atomic_sub __sync_sub_and_fetch 31 | #define cork_uint_atomic_sub __sync_sub_and_fetch 32 | #define cork_size_atomic_sub __sync_sub_and_fetch 33 | #define cork_int_atomic_pre_sub __sync_fetch_and_sub 34 | #define cork_uint_atomic_pre_sub __sync_fetch_and_sub 35 | #define cork_size_atomic_pre_sub __sync_fetch_and_sub 36 | #define cork_int_cas __sync_val_compare_and_swap 37 | #define cork_uint_cas __sync_val_compare_and_swap 38 | #define cork_size_cas __sync_val_compare_and_swap 39 | #define cork_ptr_cas __sync_val_compare_and_swap 40 | 41 | 42 | /*----------------------------------------------------------------------- 43 | * End of atomic implementations 44 | */ 45 | #else 46 | #error "No atomics implementation!" 47 | #endif 48 | 49 | 50 | #endif /* LIBCORK_THREADS_ATOMICS_H */ 51 | -------------------------------------------------------------------------------- /src/xTproxy.h: -------------------------------------------------------------------------------- 1 | #ifndef XTPROXY_H 2 | #define XTPROXY_H 3 | 4 | #include "uv.h" 5 | #include "socks.h" 6 | #include "packet.h" 7 | #include "packet.h" 8 | 9 | 10 | #ifdef VERSION 11 | #define xTProxy_VER VERSION 12 | #define xTProxy_BUILD_TIME BUILD_TIME 13 | #else 14 | #define xTProxy_VER "undefined" 15 | #define xTProxy_BUILD_TIME "" 16 | #endif 17 | 18 | 19 | struct client_context { 20 | int stage; 21 | union { 22 | uv_handle_t handle; 23 | uv_stream_t stream; 24 | uv_tcp_t tcp; 25 | uv_udp_t udp; 26 | } handle; 27 | struct sockaddr target_addr; 28 | uv_write_t write_req; 29 | struct remote_context *remote; 30 | uint8_t buf[MAX_PACKET_SIZE]; 31 | }; 32 | 33 | struct remote_context { 34 | int stage; 35 | union { 36 | uv_handle_t handle; 37 | uv_stream_t stream; 38 | uv_tcp_t tcp; 39 | uv_udp_t udp; 40 | } handle; 41 | uv_write_t write_req; 42 | uv_timer_t *timer; 43 | uv_connect_t connect_req; 44 | struct sockaddr *server_addr; 45 | struct dns_query *addr_query; 46 | struct client_context *client; 47 | struct packet packet; 48 | uint16_t idle_timeout; 49 | }; 50 | 51 | struct client_context * new_client(); 52 | void close_client(struct client_context *client); 53 | void receive_from_client(struct client_context *client); 54 | void forward_to_client(struct client_context *client, uint8_t *buf, int buflen); 55 | void client_accept_cb(uv_stream_t *server, int status); 56 | 57 | struct remote_context * new_remote(uint16_t timeout); 58 | void close_remote(struct remote_context *remote); 59 | void connect_to_remote(struct remote_context *remote); 60 | void receive_from_remote(struct remote_context *remote); 61 | void forward_to_remote(struct remote_context *remote, uint8_t *buf, int buflen); 62 | void reset_timer(struct remote_context *remote); 63 | 64 | void close_loop(uv_loop_t *loop); 65 | 66 | int verbose; 67 | uint16_t idle_timeout; 68 | 69 | #endif // for #ifndef XTPROXY_H 70 | -------------------------------------------------------------------------------- /src/xSocks.h: -------------------------------------------------------------------------------- 1 | #ifndef XSOCKS_H 2 | #define XSOCKS_H 3 | 4 | #include "uv.h" 5 | #include "socks.h" 6 | #include "packet.h" 7 | 8 | 9 | #define XSOCKS_VER "xSocks/" XSOCKS_VERSION 10 | 11 | 12 | struct client_context { 13 | int stage; 14 | union { 15 | uv_handle_t handle; 16 | uv_stream_t stream; 17 | uv_tcp_t tcp; 18 | uv_udp_t udp; 19 | } handle; 20 | struct sockaddr addr; 21 | struct remote_context *remote; 22 | uint8_t buf[MAX_PACKET_SIZE]; 23 | uint8_t buflen; 24 | uint8_t cmd; 25 | char target_addr[256]; 26 | }; 27 | 28 | struct remote_context { 29 | int stage; 30 | int direct; 31 | uv_os_sock_t tcp_fd; 32 | union { 33 | uv_handle_t handle; 34 | uv_stream_t stream; 35 | uv_tcp_t tcp; 36 | uv_udp_t udp; 37 | } handle; 38 | uv_timer_t *timer; 39 | uv_connect_t connect_req; 40 | struct sockaddr addr; 41 | struct client_context *client; 42 | struct packet packet; 43 | uint16_t idle_timeout; 44 | }; 45 | 46 | struct client_context * new_client(); 47 | void close_client(struct client_context *client); 48 | int receive_from_client(struct client_context *client); 49 | void forward_to_client(struct client_context *client, uint8_t *buf, int buflen); 50 | void client_accept_cb(uv_stream_t *server, int status); 51 | void request_ack(struct client_context *client, enum s5_rep rep); 52 | 53 | struct remote_context * new_remote(uint16_t timeout, struct sockaddr *addr); 54 | void close_remote(struct remote_context *remote); 55 | void connect_to_remote(struct remote_context *remote); 56 | int receive_from_remote(struct remote_context *remote); 57 | void forward_to_remote(struct remote_context *remote, uint8_t *buf, int buflen); 58 | void reset_timer(struct remote_context *remote); 59 | 60 | #ifdef ANDROID 61 | int vpn; 62 | #endif 63 | int acl; 64 | int verbose; 65 | uint16_t idle_timeout; 66 | struct sockaddr_storage bind_addr; 67 | struct sockaddr_storage server_addr; 68 | 69 | #endif // for #ifndef XSOCKS_H 70 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/config/config.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2011-2015, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license details. 7 | * ---------------------------------------------------------------------- 8 | */ 9 | 10 | #ifndef LIBCORK_CONFIG_CONFIG_H 11 | #define LIBCORK_CONFIG_CONFIG_H 12 | 13 | 14 | /* If you want to skip autodetection, define this to 1, and provide a 15 | * libcork/config/custom.h header file. */ 16 | 17 | #if !defined(CORK_CONFIG_SKIP_AUTODETECT) 18 | #define CORK_CONFIG_SKIP_AUTODETECT 0 19 | #endif 20 | 21 | 22 | #if CORK_CONFIG_SKIP_AUTODETECT 23 | /* The user has promised that they'll define everything themselves. */ 24 | #include 25 | 26 | #else 27 | /* Otherwise autodetect! */ 28 | 29 | 30 | /**** VERSION ****/ 31 | 32 | // #include 33 | 34 | 35 | /**** ARCHITECTURES ****/ 36 | 37 | #include 38 | 39 | 40 | /**** PLATFORMS ****/ 41 | #if (defined(__unix__) || defined(unix)) && !defined(USG) 42 | /* We need this to test for BSD, but it's a good idea to have for 43 | * any brand of Unix.*/ 44 | #include 45 | #endif 46 | 47 | #if defined(__linux) 48 | /* Do some Linux-specific autodetection. */ 49 | #include 50 | 51 | #elif defined(__APPLE__) && defined(__MACH__) 52 | /* Do some Mac OS X-specific autodetection. */ 53 | #include 54 | 55 | #elif defined(BSD) && (BSD >= 199103) 56 | /* Do some BSD (4.3 code base or newer)specific autodetection. */ 57 | #include 58 | 59 | #endif /* platforms */ 60 | 61 | 62 | /**** COMPILERS ****/ 63 | 64 | #if defined(__GNUC__) 65 | /* Do some GCC-specific autodetection. */ 66 | #include 67 | 68 | #endif /* compilers */ 69 | 70 | 71 | #endif /* autodetect or not */ 72 | 73 | 74 | #endif /* LIBCORK_CONFIG_CONFIG_H */ 75 | -------------------------------------------------------------------------------- /src/packet.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "util.h" 5 | #include "packet.h" 6 | 7 | 8 | void 9 | packet_alloc(struct packet *packet, uv_buf_t *buf) { 10 | if (packet->size) { 11 | buf->base = (char *) packet->buf + packet->offset; 12 | buf->len = packet->size - packet->offset; 13 | } else { 14 | buf->base = (char *) packet->buf + (packet->read ? 1 : 0); 15 | buf->len = packet->read ? 1 : HEADER_BYTES; 16 | } 17 | } 18 | 19 | int 20 | packet_filter(struct packet *packet, const char *buf, ssize_t buflen) { 21 | int rc = PACKET_INVALID; 22 | 23 | if (packet->size == 0) { 24 | if (packet->read == 0) { 25 | if (buflen == HEADER_BYTES) { 26 | packet->size = read_size((uint8_t *) buf); 27 | if (packet->size > PRIMITIVE_BYTES && packet->size <= packet->max) { 28 | rc = PACKET_UNCOMPLETE; 29 | } else { 30 | rc = PACKET_INVALID; 31 | } 32 | 33 | } else { 34 | assert(buflen == 1); 35 | packet->read = 1; 36 | rc = PACKET_UNCOMPLETE; 37 | } 38 | 39 | } else { 40 | assert(packet->read == 1); 41 | packet->size = read_size((uint8_t *) packet->buf); 42 | if (packet->size > PRIMITIVE_BYTES && packet->size <= packet->max) { 43 | rc = PACKET_UNCOMPLETE; 44 | } else { 45 | rc = PACKET_INVALID; 46 | } 47 | } 48 | 49 | } else { 50 | if (buflen + packet->offset == packet->size) { 51 | rc = PACKET_COMPLETED; 52 | 53 | } else { 54 | assert(buflen + packet->offset < packet->size); 55 | packet->offset += buflen; 56 | rc = PACKET_UNCOMPLETE; 57 | } 58 | } 59 | 60 | return rc; 61 | } 62 | 63 | void 64 | packet_reset(struct packet *packet) { 65 | packet->read = 0; 66 | packet->offset = 0; 67 | packet->size = 0; 68 | memset(packet->buf, 0, packet->max); 69 | } 70 | -------------------------------------------------------------------------------- /src/xForwarder.h: -------------------------------------------------------------------------------- 1 | #ifndef XFORWARDER_H 2 | #define XFORWARDER_H 3 | 4 | #include "uv.h" 5 | #include "socks.h" 6 | #include "packet.h" 7 | 8 | 9 | #ifdef VERSION 10 | #define xForwarder_VER VERSION 11 | #define xForwarder_BUILD_TIME BUILD_TIME 12 | #else 13 | #define xForwarder_VER "undefined" 14 | #define xForwarder_BUILD_TIME "" 15 | #endif 16 | 17 | struct client_context { 18 | int stage; 19 | union { 20 | uv_handle_t handle; 21 | uv_stream_t stream; 22 | uv_tcp_t tcp; 23 | uv_udp_t udp; 24 | } handle; 25 | uv_write_t write_req; 26 | struct remote_context *remote; 27 | uint8_t buf[MAX_PACKET_SIZE]; 28 | }; 29 | 30 | struct remote_context { 31 | int id; 32 | int stage; 33 | union { 34 | uv_handle_t handle; 35 | uv_stream_t stream; 36 | uv_tcp_t tcp; 37 | uv_udp_t udp; 38 | } handle; 39 | uv_write_t write_req; 40 | uv_timer_t *timer; 41 | uv_connect_t connect_req; 42 | struct client_context *client; 43 | struct packet packet; 44 | uint16_t idle_timeout; 45 | }; 46 | 47 | struct client_context * new_client(); 48 | void close_client(struct client_context *client); 49 | void receive_from_client(struct client_context *client); 50 | void forward_to_client(struct client_context *client, uint8_t *buf, int buflen); 51 | void client_accept_cb(uv_stream_t *server, int status); 52 | void request_ack(struct client_context *client, enum s5_rep rep); 53 | 54 | struct remote_context * new_remote(uint16_t timeout); 55 | void close_remote(struct remote_context *remote); 56 | void connect_to_remote(struct remote_context *remote); 57 | void receive_from_remote(struct remote_context *remote); 58 | void forward_to_remote(struct remote_context *remote, uint8_t *buf, int buflen); 59 | void reset_timer(struct remote_context *remote); 60 | 61 | void close_loop(uv_loop_t *loop); 62 | 63 | int verbose; 64 | uint16_t idle_timeout; 65 | char *dest_addr_buf; 66 | struct sockaddr_storage bind_addr; 67 | struct sockaddr_storage dest_addr; 68 | struct sockaddr_storage server_addr; 69 | 70 | #endif // for #ifndef XFORWARDER_H 71 | -------------------------------------------------------------------------------- /src/xSocksd.h: -------------------------------------------------------------------------------- 1 | #ifndef XSOCKSD_H 2 | #define XSOCKSD_H 3 | 4 | #include "uv.h" 5 | #include "socks.h" 6 | #include "packet.h" 7 | #include "resolver.h" 8 | 9 | 10 | #ifdef VERSION 11 | #define xSocksd_VER VERSION 12 | #define xSocksd_BUILD_TIME BUILD_TIME 13 | #else 14 | #define xSocksd_VER "undefined" 15 | #define xSocksd_BUILD_TIME "" 16 | #endif 17 | 18 | 19 | struct client_context { 20 | int stage; 21 | union { 22 | uv_handle_t handle; 23 | uv_stream_t stream; 24 | uv_tcp_t tcp; 25 | uv_udp_t udp; 26 | } handle; 27 | uv_write_t write_req; 28 | struct sockaddr addr; 29 | char target_addr[256]; 30 | struct remote_context *remote; 31 | struct packet packet; 32 | }; 33 | 34 | struct remote_context { 35 | int stage; 36 | union { 37 | uv_handle_t handle; 38 | uv_stream_t stream; 39 | uv_tcp_t tcp; 40 | uv_udp_t udp; 41 | } handle; 42 | uv_timer_t *timer; 43 | uv_connect_t connect_req; 44 | uint16_t idle_timeout; 45 | uint8_t buf[MAX_PACKET_SIZE]; 46 | struct sockaddr addr; 47 | struct client_context *client; 48 | struct resolver_query *host_query; 49 | }; 50 | 51 | struct client_context * new_client(); 52 | void close_client(struct client_context *client); 53 | void receive_from_client(struct client_context *client); 54 | void forward_to_client(struct client_context *client, uint8_t *buf, int buflen); 55 | void client_accept_cb(uv_stream_t *server, int status); 56 | 57 | struct remote_context * new_remote(uint16_t timeout); 58 | void close_remote(struct remote_context *remote); 59 | void resolve_remote(struct remote_context *remote, char *host, uint16_t port); 60 | void connect_to_remote(struct remote_context *remote); 61 | void receive_from_remote(struct remote_context *remote); 62 | void forward_to_remote(struct remote_context *remote, uint8_t *buf, int buflen); 63 | void reset_timer(struct remote_context *remote); 64 | 65 | void close_loop(uv_loop_t *loop); 66 | 67 | int verbose; 68 | uint16_t idle_timeout; 69 | uv_key_t thread_resolver_key; 70 | 71 | #endif // for #ifndef XSOCKSD_H 72 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/ds/bitset.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2013, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license details. 7 | * ---------------------------------------------------------------------- 8 | */ 9 | 10 | #ifndef LIBCORK_DS_BITS_H 11 | #define LIBCORK_DS_BITS_H 12 | 13 | 14 | #include 15 | #include 16 | 17 | 18 | /*----------------------------------------------------------------------- 19 | * Bit sets 20 | */ 21 | 22 | struct cork_bitset { 23 | uint8_t *bits; 24 | size_t bit_count; 25 | size_t byte_count; 26 | }; 27 | 28 | CORK_API struct cork_bitset * 29 | cork_bitset_new(size_t bit_count); 30 | 31 | CORK_API void 32 | cork_bitset_free(struct cork_bitset *set); 33 | 34 | CORK_API void 35 | cork_bitset_clear(struct cork_bitset *set); 36 | 37 | /* Extract the byte that contains a particular bit in an array. */ 38 | #define cork_bitset_byte_for_bit(set, i) \ 39 | ((set)->bits[(i) / 8]) 40 | 41 | /* Create a bit mask that extracts a particular bit from the byte that it lives 42 | * in. */ 43 | #define cork_bitset_pos_mask_for_bit(i) \ 44 | (0x80 >> ((i) % 8)) 45 | 46 | /* Create a bit mask that extracts everything except for a particular bit from 47 | * the byte that it lives in. */ 48 | #define cork_bitset_neg_mask_for_bit(i) \ 49 | (~cork_bitset_pos_mask_for_bit(i)) 50 | 51 | /* Return whether a particular bit is set in a byte array. Bits are numbered 52 | * from 0, in a big-endian order. */ 53 | #define cork_bitset_get(set, i) \ 54 | ((cork_bitset_byte_for_bit(set, i) & cork_bitset_pos_mask_for_bit(i)) != 0) 55 | 56 | /* Set (or unset) a particular bit is set in a byte array. Bits are numbered 57 | * from 0, in a big-endian order. */ 58 | #define cork_bitset_set(set, i, val) \ 59 | (cork_bitset_byte_for_bit(set, i) = \ 60 | (cork_bitset_byte_for_bit(set, i) & cork_bitset_neg_mask_for_bit(i)) \ 61 | | ((val)? cork_bitset_pos_mask_for_bit(i): 0)) 62 | 63 | 64 | #endif /* LIBCORK_DS_BITS_H */ 65 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/core/mempool.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2012-2015, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license details. 7 | * ---------------------------------------------------------------------- 8 | */ 9 | 10 | #ifndef LIBCORK_CORK_MEMPOOL_H 11 | #define LIBCORK_CORK_MEMPOOL_H 12 | 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | 21 | #define CORK_MEMPOOL_DEFAULT_BLOCK_SIZE 4096 22 | 23 | 24 | struct cork_mempool; 25 | 26 | 27 | CORK_API struct cork_mempool * 28 | cork_mempool_new_size_ex(size_t element_size, size_t block_size); 29 | 30 | #define cork_mempool_new_size(element_size) \ 31 | (cork_mempool_new_size_ex \ 32 | ((element_size), CORK_MEMPOOL_DEFAULT_BLOCK_SIZE)) 33 | 34 | #define cork_mempool_new_ex(type, block_size) \ 35 | (cork_mempool_new_size_ex(sizeof(type), (block_size))) 36 | 37 | #define cork_mempool_new(type) \ 38 | (cork_mempool_new_size(sizeof(type))) 39 | 40 | CORK_API void 41 | cork_mempool_free(struct cork_mempool *mp); 42 | 43 | 44 | CORK_API void 45 | cork_mempool_set_user_data(struct cork_mempool *mp, 46 | void *user_data, cork_free_f free_user_data); 47 | 48 | CORK_API void 49 | cork_mempool_set_init_object(struct cork_mempool *mp, cork_init_f init_object); 50 | 51 | CORK_API void 52 | cork_mempool_set_done_object(struct cork_mempool *mp, cork_done_f done_object); 53 | 54 | /* Deprecated; you should now use separate calls to cork_mempool_set_user_data, 55 | * cork_mempool_set_init_object, and cork_mempool_set_done_object. */ 56 | CORK_API void 57 | cork_mempool_set_callbacks(struct cork_mempool *mp, 58 | void *user_data, cork_free_f free_user_data, 59 | cork_init_f init_object, 60 | cork_done_f done_object); 61 | 62 | 63 | CORK_API void * 64 | cork_mempool_new_object(struct cork_mempool *mp); 65 | 66 | 67 | CORK_API void 68 | cork_mempool_free_object(struct cork_mempool *mp, void *ptr); 69 | 70 | 71 | #endif /* LIBCORK_CORK_MEMPOOL_H */ 72 | -------------------------------------------------------------------------------- /3rd/libcork/src/ds/ring-buffer.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2011-2014, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license details. 7 | * ---------------------------------------------------------------------- 8 | */ 9 | 10 | #include 11 | 12 | #include "libcork/core/allocator.h" 13 | #include "libcork/core/types.h" 14 | #include "libcork/ds/ring-buffer.h" 15 | 16 | 17 | int 18 | cork_ring_buffer_init(struct cork_ring_buffer *self, size_t size) 19 | { 20 | self->elements = cork_calloc(size, sizeof(void *)); 21 | self->allocated_size = size; 22 | self->size = 0; 23 | self->read_index = 0; 24 | self->write_index = 0; 25 | return 0; 26 | } 27 | 28 | struct cork_ring_buffer * 29 | cork_ring_buffer_new(size_t size) 30 | { 31 | struct cork_ring_buffer *buf = cork_new(struct cork_ring_buffer); 32 | cork_ring_buffer_init(buf, size); 33 | return buf; 34 | } 35 | 36 | void 37 | cork_ring_buffer_done(struct cork_ring_buffer *self) 38 | { 39 | cork_cfree(self->elements, self->allocated_size, sizeof(void *)); 40 | } 41 | 42 | void 43 | cork_ring_buffer_free(struct cork_ring_buffer *buf) 44 | { 45 | cork_ring_buffer_done(buf); 46 | cork_delete(struct cork_ring_buffer, buf); 47 | } 48 | 49 | int 50 | cork_ring_buffer_add(struct cork_ring_buffer *self, void *element) 51 | { 52 | if (cork_ring_buffer_is_full(self)) { 53 | return -1; 54 | } 55 | 56 | self->elements[self->write_index++] = element; 57 | self->size++; 58 | if (self->write_index == self->allocated_size) { 59 | self->write_index = 0; 60 | } 61 | return 0; 62 | } 63 | 64 | void * 65 | cork_ring_buffer_pop(struct cork_ring_buffer *self) 66 | { 67 | if (cork_ring_buffer_is_empty(self)) { 68 | return NULL; 69 | } else { 70 | void *result = self->elements[self->read_index++]; 71 | self->size--; 72 | if (self->read_index == self->allocated_size) { 73 | self->read_index = 0; 74 | } 75 | return result; 76 | } 77 | } 78 | 79 | void * 80 | cork_ring_buffer_peek(struct cork_ring_buffer *self) 81 | { 82 | if (cork_ring_buffer_is_empty(self)) { 83 | return NULL; 84 | } else { 85 | return self->elements[self->read_index]; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/ds/managed-buffer.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2011, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef LIBCORK_DS_MANAGED_BUFFER_H 12 | #define LIBCORK_DS_MANAGED_BUFFER_H 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | 19 | /*----------------------------------------------------------------------- 20 | * Managed buffers 21 | */ 22 | 23 | struct cork_managed_buffer; 24 | 25 | struct cork_managed_buffer_iface { 26 | /* Free the contents of a managed buffer, and the managed buffer 27 | * object itself. */ 28 | void 29 | (*free)(struct cork_managed_buffer *buf); 30 | }; 31 | 32 | 33 | struct cork_managed_buffer { 34 | /* The buffer that this instance manages */ 35 | const void *buf; 36 | /* The size of buf */ 37 | size_t size; 38 | /* A reference count for the buffer. If this drops to 0, the buffer 39 | * will be finalized. */ 40 | volatile int ref_count; 41 | /* The managed buffer implementation for this instance. */ 42 | struct cork_managed_buffer_iface *iface; 43 | }; 44 | 45 | 46 | CORK_API struct cork_managed_buffer * 47 | cork_managed_buffer_new_copy(const void *buf, size_t size); 48 | 49 | 50 | typedef void 51 | (*cork_managed_buffer_freer)(void *buf, size_t size); 52 | 53 | CORK_API struct cork_managed_buffer * 54 | cork_managed_buffer_new(const void *buf, size_t size, 55 | cork_managed_buffer_freer free); 56 | 57 | 58 | CORK_API struct cork_managed_buffer * 59 | cork_managed_buffer_ref(struct cork_managed_buffer *buf); 60 | 61 | CORK_API void 62 | cork_managed_buffer_unref(struct cork_managed_buffer *buf); 63 | 64 | 65 | CORK_API int 66 | cork_managed_buffer_slice(struct cork_slice *dest, 67 | struct cork_managed_buffer *buffer, 68 | size_t offset, size_t length); 69 | 70 | CORK_API int 71 | cork_managed_buffer_slice_offset(struct cork_slice *dest, 72 | struct cork_managed_buffer *buffer, 73 | size_t offset); 74 | 75 | 76 | #endif /* LIBCORK_DS_MANAGED_BUFFER_H */ 77 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/core/types.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2011, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef LIBCORK_CORE_TYPES_H 12 | #define LIBCORK_CORE_TYPES_H 13 | 14 | /* For now, we assume that the C99 integer types are available using the 15 | * standard headers. */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | 24 | /* Define preprocessor macros that contain the size of several built-in 25 | * types. Again, we assume that we have the C99 definitions available. */ 26 | 27 | #if SHRT_MAX == INT8_MAX 28 | #define CORK_SIZEOF_SHORT 1 29 | #elif SHRT_MAX == INT16_MAX 30 | #define CORK_SIZEOF_SHORT 2 31 | #elif SHRT_MAX == INT32_MAX 32 | #define CORK_SIZEOF_SHORT 4 33 | #elif SHRT_MAX == INT64_MAX 34 | #define CORK_SIZEOF_SHORT 8 35 | #else 36 | #error "Cannot determine size of short" 37 | #endif 38 | 39 | #if INT_MAX == INT8_MAX 40 | #define CORK_SIZEOF_INT 1 41 | #elif INT_MAX == INT16_MAX 42 | #define CORK_SIZEOF_INT 2 43 | #elif INT_MAX == INT32_MAX 44 | #define CORK_SIZEOF_INT 4 45 | #elif INT_MAX == INT64_MAX 46 | #define CORK_SIZEOF_INT 8 47 | #else 48 | #error "Cannot determine size of int" 49 | #endif 50 | 51 | #if LONG_MAX == INT8_MAX 52 | #define CORK_SIZEOF_LONG 1 53 | #elif LONG_MAX == INT16_MAX 54 | #define CORK_SIZEOF_LONG 2 55 | #elif LONG_MAX == INT32_MAX 56 | #define CORK_SIZEOF_LONG 4 57 | #elif LONG_MAX == INT64_MAX 58 | #define CORK_SIZEOF_LONG 8 59 | #else 60 | #error "Cannot determine size of long" 61 | #endif 62 | 63 | #if INTPTR_MAX == INT8_MAX 64 | #define CORK_SIZEOF_POINTER 1 65 | #elif INTPTR_MAX == INT16_MAX 66 | #define CORK_SIZEOF_POINTER 2 67 | #elif INTPTR_MAX == INT32_MAX 68 | #define CORK_SIZEOF_POINTER 4 69 | #elif INTPTR_MAX == INT64_MAX 70 | #define CORK_SIZEOF_POINTER 8 71 | #else 72 | #error "Cannot determine size of void *" 73 | #endif 74 | 75 | 76 | /* Return a pointer to a @c struct, given a pointer to one of its 77 | * fields. */ 78 | #define cork_container_of(field, struct_type, field_name) \ 79 | ((struct_type *) (- offsetof(struct_type, field_name) + \ 80 | (void *) (field))) 81 | 82 | #endif /* LIBCORK_CORE_TYPES_H */ 83 | -------------------------------------------------------------------------------- /src/daemon.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "logger.h" 11 | 12 | 13 | #define LOCKMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) 14 | 15 | 16 | static int 17 | lockfile(int fd) { 18 | struct flock fl; 19 | 20 | fl.l_type = F_WRLCK; 21 | fl.l_start = 0; 22 | fl.l_whence = SEEK_SET; 23 | fl.l_len = 0; 24 | return(fcntl(fd, F_SETLK, &fl)); 25 | } 26 | 27 | int 28 | already_running(const char *pidfile) { 29 | int fd; 30 | char buf[16]; 31 | 32 | fd = open(pidfile, O_RDWR | O_CREAT, LOCKMODE); 33 | if (fd < 0) { 34 | logger_stderr("open \"%s\" failed (%d: %s)", pidfile, errno, strerror(errno)); 35 | exit(1); 36 | } 37 | if (lockfile(fd) < 0) { 38 | if (errno == EACCES || errno == EAGAIN) { 39 | close(fd); 40 | return(1); 41 | } 42 | logger_stderr("can't lock %s: %s", pidfile, strerror(errno)); 43 | exit(1); 44 | } 45 | 46 | /* 47 | * create pid file 48 | */ 49 | if (ftruncate(fd, 0)) { 50 | logger_stderr("can't truncate %s: %s", pidfile, strerror(errno)); 51 | exit(1); 52 | } 53 | sprintf(buf, "%ld\n", (long)getpid()); 54 | if (write(fd, buf, strlen(buf)+1) == -1) { 55 | logger_stderr("can't write %s: %s", pidfile, strerror(errno)); 56 | exit(1); 57 | } 58 | 59 | return 0; 60 | } 61 | 62 | void 63 | create_pidfile(const char *pidfile) { 64 | FILE *fp = fopen(pidfile, "w"); 65 | if (fp) { 66 | fprintf(fp, "%ld\n", (long)getpid()); 67 | fclose(fp); 68 | } 69 | } 70 | 71 | void 72 | delete_pidfile(const char *pidfile) { 73 | unlink(pidfile); 74 | } 75 | 76 | int 77 | daemonize(void) { 78 | int fd; 79 | pid_t pid; 80 | 81 | switch (pid = fork()) { 82 | case -1: 83 | fprintf(stderr, "fork() failed.\n"); 84 | return -1; 85 | 86 | case 0: 87 | break; 88 | 89 | default: 90 | exit(0); 91 | } 92 | 93 | setsid(); 94 | 95 | if ((fd = open("/dev/null", O_RDWR, 0)) == -1) { 96 | logger_stderr("open [/dev/null] failed (%d: %s)", errno, strerror(errno)); 97 | return -1; 98 | 99 | } else { 100 | dup2(fd, STDIN_FILENO); 101 | dup2(fd, STDOUT_FILENO); 102 | /* dup2(fd, STDERR_FILENO); */ 103 | } 104 | 105 | return 0; 106 | } 107 | 108 | -------------------------------------------------------------------------------- /src/common.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "uv.h" 5 | #include "socks.h" 6 | #include "util.h" 7 | #include "logger.h" 8 | 9 | int 10 | parse_target_address(const struct xSocks_request *req, struct sockaddr *addr, char *host) { 11 | int addrlen; 12 | uint16_t portlen = 2; // network byte order port number, 2 bytes 13 | union { 14 | struct sockaddr addr; 15 | struct sockaddr_in addr4; 16 | struct sockaddr_in6 addr6; 17 | } dest; 18 | 19 | memset(&dest, 0, sizeof(dest)); 20 | 21 | if (req->atyp == ATYP_IPV4) { 22 | size_t in_addr_len = sizeof(struct in_addr); // 4 bytes for IPv4 address 23 | dest.addr4.sin_family = AF_INET; 24 | memcpy(&dest.addr4.sin_addr, req->addr, in_addr_len); 25 | memcpy(&dest.addr4.sin_port, req->addr + in_addr_len, portlen); 26 | addrlen = 4 + portlen; 27 | 28 | } else if (req->atyp == ATYP_HOST) { 29 | uint8_t namelen = *(uint8_t *)(req->addr); // 1 byte of name length 30 | if (namelen > 0xFF) { 31 | return -1; 32 | } 33 | memcpy(&dest.addr4.sin_port, req->addr + 1 + namelen, portlen); 34 | memcpy(host, req->addr + 1, namelen); 35 | host[namelen] = '\0'; 36 | addrlen = 1 + namelen + portlen; 37 | 38 | } else if (req->atyp == ATYP_IPV6) { 39 | size_t in6_addr_len = sizeof(struct in6_addr); // 16 bytes for IPv6 address 40 | memcpy(&dest.addr6.sin6_addr, req->addr, in6_addr_len); 41 | memcpy(&dest.addr6.sin6_port, req->addr + in6_addr_len, portlen); 42 | addrlen = 16 + portlen; 43 | 44 | } else { 45 | return 0; 46 | } 47 | 48 | memcpy(addr, &dest.addr, sizeof(*addr)); 49 | return addrlen; 50 | } 51 | 52 | void 53 | cache_log(uint8_t atyp, const struct sockaddr *src_addr, const struct sockaddr *dst_addr, 54 | const char *host, uint16_t port, int hit) { 55 | char src[INET6_ADDRSTRLEN + 1] = {0}; 56 | char dst[INET6_ADDRSTRLEN + 1] = {0}; 57 | uint16_t src_port = 0, dst_port = 0; 58 | char *hint = hit ? "hit" : "miss"; 59 | src_port = ip_name(src_addr, src, sizeof src); 60 | if (atyp == ATYP_HOST) { 61 | logger_log(hit ? LOG_INFO : LOG_WARNING, "[udp] cache %s: %s:%d -> %s:%d", 62 | hint, src, src_port, host, ntohs(port)); 63 | } else { 64 | dst_port = ip_name(dst_addr, dst, sizeof dst); 65 | logger_log(hit ? LOG_INFO : LOG_WARNING, "[udp] cache %s: %s:%d -> %s:%d", 66 | hint, src, src_port, dst, dst_port); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/crypto.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "sodium.h" 3 | 4 | 5 | /* 6 | * 7 | * Cipher Text 8 | * +------+-----+----------+ 9 | * |NONCE | TAG | DATA | 10 | * +------+-----+----------+ 11 | * | 8 | 16 | Variable | 12 | * +------+-----+----------+ 13 | * 14 | */ 15 | 16 | #define COB crypto_onetimeauth_BYTES // 16U 17 | #define COKB crypto_onetimeauth_KEYBYTES // 32U 18 | #define CSSNB crypto_stream_chacha20_NONCEBYTES // 8U 19 | #define CSSKB crypto_stream_chacha20_KEYBYTES //32U 20 | 21 | static uint8_t secret_key[crypto_generichash_BYTES]; 22 | 23 | 24 | static int 25 | chacha20poly1305_encrypt(uint8_t *c, const uint8_t *m, const uint32_t mlen, 26 | const uint8_t *n, const uint8_t *k) { 27 | uint8_t cok[COKB]; 28 | 29 | crypto_stream_chacha20(cok, COKB, n, k); 30 | crypto_stream_chacha20_xor(c + COB, m, mlen, n, k); 31 | crypto_onetimeauth_poly1305(c, c + COB, mlen, cok); 32 | 33 | return 0; 34 | } 35 | 36 | static int 37 | chacha20poly1305_decrypt(uint8_t *m, const uint8_t *c, const uint32_t clen, 38 | const uint8_t *n, const uint8_t *k) { 39 | uint8_t cok[COKB]; 40 | 41 | if (clen < COB) { 42 | return -1; 43 | } 44 | 45 | int mlen = clen - COB; 46 | 47 | crypto_stream_chacha20(cok, COKB, n, k); 48 | if (crypto_onetimeauth_poly1305_verify(c, c + COB, mlen, cok) == 0) { 49 | return crypto_stream_chacha20_xor(m, c + COB, mlen, n, k); 50 | } 51 | 52 | return -1; 53 | } 54 | 55 | int 56 | crypto_init(const char *password) { 57 | if (sodium_init() == -1) { 58 | return 1; 59 | } 60 | 61 | randombytes_set_implementation(&randombytes_internal_implementation); 62 | randombytes_stir(); 63 | 64 | return crypto_generichash(secret_key, sizeof secret_key, (uint8_t*)password, strlen(password), NULL, 0); 65 | } 66 | 67 | int 68 | crypto_generickey(uint8_t *out, size_t outlen, uint8_t *in, size_t inlen, uint8_t *key, size_t keylen) { 69 | return crypto_generichash(out, outlen, in, inlen, key, keylen); 70 | } 71 | 72 | int 73 | crypto_encrypt(uint8_t *c, const uint8_t *m, const uint32_t mlen) { 74 | uint8_t nonce[CSSNB]; 75 | randombytes_buf(nonce, CSSNB); 76 | memcpy(c, nonce, CSSNB); 77 | return chacha20poly1305_encrypt(c + CSSNB, m, mlen, nonce, secret_key); 78 | } 79 | 80 | int 81 | crypto_decrypt(uint8_t *m, const uint8_t *c, const uint32_t clen) { 82 | uint8_t nonce[CSSNB]; 83 | if (clen <= CSSNB + COB) { 84 | return -1; 85 | } 86 | memcpy(nonce, c, CSSNB); 87 | return chacha20poly1305_decrypt(m, c + CSSNB, clen - CSSNB, nonce, secret_key); 88 | } 89 | -------------------------------------------------------------------------------- /3rd/libipset/src/set/inspection.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2009-2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the LICENSE.txt file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #include 12 | 13 | #include "ipset/bdd/nodes.h" 14 | #include "ipset/ipset.h" 15 | 16 | bool 17 | ipset_is_empty(const struct ip_set *set) 18 | { 19 | /* Since BDDs are unique, the only empty set is the “false” BDD. */ 20 | return (set->set_bdd == ipset_terminal_node_id(false)); 21 | } 22 | 23 | bool 24 | ipset_is_equal(const struct ip_set *set1, const struct ip_set *set2) 25 | { 26 | return ipset_node_cache_nodes_equal 27 | (set1->cache, set1->set_bdd, set2->cache, set2->set_bdd); 28 | } 29 | 30 | size_t 31 | ipset_memory_size(const struct ip_set *set) 32 | { 33 | return ipset_node_memory_size(set->cache, set->set_bdd); 34 | } 35 | 36 | 37 | bool 38 | ipset_ip_add(struct ip_set *set, struct cork_ip *addr) 39 | { 40 | if (addr->version == 4) { 41 | return ipset_ipv4_add(set, &addr->ip.v4); 42 | } else { 43 | return ipset_ipv6_add(set, &addr->ip.v6); 44 | } 45 | } 46 | 47 | 48 | bool 49 | ipset_ip_add_network(struct ip_set *set, struct cork_ip *addr, 50 | unsigned int cidr_prefix) 51 | { 52 | if (addr->version == 4) { 53 | return ipset_ipv4_add_network(set, &addr->ip.v4, cidr_prefix); 54 | } else { 55 | return ipset_ipv6_add_network(set, &addr->ip.v6, cidr_prefix); 56 | } 57 | } 58 | 59 | 60 | bool 61 | ipset_ip_remove(struct ip_set *set, struct cork_ip *addr) 62 | { 63 | if (addr->version == 4) { 64 | return ipset_ipv4_remove(set, &addr->ip.v4); 65 | } else { 66 | return ipset_ipv6_remove(set, &addr->ip.v6); 67 | } 68 | } 69 | 70 | 71 | bool 72 | ipset_ip_remove_network(struct ip_set *set, struct cork_ip *addr, 73 | unsigned int cidr_prefix) 74 | { 75 | if (addr->version == 4) { 76 | return ipset_ipv4_remove_network(set, &addr->ip.v4, cidr_prefix); 77 | } else { 78 | return ipset_ipv6_remove_network(set, &addr->ip.v6, cidr_prefix); 79 | } 80 | } 81 | 82 | 83 | bool 84 | ipset_contains_ip(const struct ip_set *set, struct cork_ip *addr) 85 | { 86 | if (addr->version == 4) { 87 | return ipset_contains_ipv4(set, &addr->ip.v4); 88 | } else { 89 | return ipset_contains_ipv6(set, &addr->ip.v6); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /3rd/libipset/src/map/inspection-template.c.in: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2009-2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the LICENSE.txt file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #include 12 | 13 | #include "ipset/bdd/nodes.h" 14 | #include "ipset/bits.h" 15 | #include "ipset/errors.h" 16 | #include "ipset/ipset.h" 17 | 18 | 19 | /** 20 | * Given a BDD variable number, return the index of the corresponding 21 | * bit in an IP address. IPv4 addresses use variables 1-32; IPv6 22 | * addresses use 1-128. (Variable 0 is used to identify the kind of 23 | * address — TRUE for IPv4, FALSE for IPv6.) 24 | */ 25 | 26 | static unsigned int 27 | IPMAP_NAME(bit_for_var)(ipset_variable var) 28 | { 29 | return (var - 1); 30 | } 31 | 32 | 33 | /** 34 | * An assignment function that can be used to evaluate an IP map BDD. 35 | */ 36 | 37 | static bool 38 | IPMAP_NAME(assignment)(const void *addr, ipset_variable var) 39 | { 40 | if (var == 0) { 41 | return IP_DISCRIMINATOR_VALUE; 42 | } else { 43 | unsigned int bit = IPMAP_NAME(bit_for_var)(var); 44 | return IPSET_BIT_GET(addr, bit); 45 | } 46 | } 47 | 48 | 49 | int 50 | IPMAP_NAME(get)(struct ip_map *map, CORK_IP *elem) 51 | { 52 | return ipset_node_evaluate 53 | (map->cache, map->map_bdd, IPMAP_NAME(assignment), elem); 54 | } 55 | 56 | 57 | void 58 | IPMAP_NAME(set_network)(struct ip_map *map, CORK_IP *elem, 59 | unsigned int cidr_prefix, int value) 60 | { 61 | /* Special case — the BDD for a netmask that's out of range never 62 | * evaluates to true. */ 63 | if (cidr_prefix > IP_BIT_SIZE) { 64 | cork_error_set 65 | (IPSET_ERROR, IPSET_PARSE_ERROR, 66 | "CIDR block %u out of range [0..%u]", cidr_prefix, IP_BIT_SIZE); 67 | return; 68 | } 69 | 70 | ipset_node_id new_bdd = 71 | ipset_node_insert 72 | (map->cache, map->map_bdd, 73 | IPMAP_NAME(assignment), elem, cidr_prefix + 1, value); 74 | ipset_node_decref(map->cache, map->map_bdd); 75 | map->map_bdd = new_bdd; 76 | } 77 | 78 | 79 | void 80 | IPMAP_NAME(set)(struct ip_map *map, CORK_IP *elem, int value) 81 | { 82 | ipset_node_id new_bdd = 83 | ipset_node_insert 84 | (map->cache, map->map_bdd, 85 | IPMAP_NAME(assignment), elem, IP_BIT_SIZE + 1, value); 86 | ipset_node_decref(map->cache, map->map_bdd); 87 | map->map_bdd = new_bdd; 88 | } 89 | -------------------------------------------------------------------------------- /3rd/libcork/src/core/u128.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2013, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license details. 7 | * ---------------------------------------------------------------------- 8 | */ 9 | 10 | #include 11 | #include 12 | 13 | #include "libcork/core/types.h" 14 | #include "libcork/core/u128.h" 15 | 16 | 17 | /* From http://stackoverflow.com/questions/8023414/how-to-convert-a-128-bit-integer-to-a-decimal-ascii-string-in-c */ 18 | 19 | const char * 20 | cork_u128_to_decimal(char *dest, cork_u128 val) 21 | { 22 | uint32_t n[4]; 23 | char *s = dest; 24 | char *p = dest; 25 | unsigned int i; 26 | 27 | /* This algorithm assumes that n[3] is the MSW. */ 28 | n[3] = cork_u128_be32(val, 0); 29 | n[2] = cork_u128_be32(val, 1); 30 | n[1] = cork_u128_be32(val, 2); 31 | n[0] = cork_u128_be32(val, 3); 32 | 33 | memset(s, '0', CORK_U128_DECIMAL_LENGTH - 1); 34 | s[CORK_U128_DECIMAL_LENGTH - 1] = '\0'; 35 | 36 | for (i = 0; i < 128; i++) { 37 | unsigned int j; 38 | unsigned int carry; 39 | 40 | carry = (n[3] >= 0x80000000); 41 | /* Shift n[] left, doubling it */ 42 | n[3] = ((n[3] << 1) & 0xFFFFFFFF) + (n[2] >= 0x80000000); 43 | n[2] = ((n[2] << 1) & 0xFFFFFFFF) + (n[1] >= 0x80000000); 44 | n[1] = ((n[1] << 1) & 0xFFFFFFFF) + (n[0] >= 0x80000000); 45 | n[0] = ((n[0] << 1) & 0xFFFFFFFF); 46 | 47 | /* Add s[] to itself in decimal, doubling it */ 48 | for (j = CORK_U128_DECIMAL_LENGTH - 1; j-- > 0; ) { 49 | s[j] += s[j] - '0' + carry; 50 | carry = (s[j] > '9'); 51 | if (carry) { 52 | s[j] -= 10; 53 | } 54 | } 55 | } 56 | 57 | while ((p[0] == '0') && (p < &s[CORK_U128_DECIMAL_LENGTH - 2])) { 58 | p++; 59 | } 60 | 61 | return p; 62 | } 63 | 64 | 65 | const char * 66 | cork_u128_to_hex(char *buf, cork_u128 val) 67 | { 68 | uint64_t hi = val._.be64.hi; 69 | uint64_t lo = val._.be64.lo; 70 | if (hi == 0) { 71 | snprintf(buf, CORK_U128_HEX_LENGTH, "%" PRIx64, lo); 72 | } else { 73 | snprintf(buf, CORK_U128_HEX_LENGTH, "%" PRIx64 "%016" PRIx64, hi, lo); 74 | } 75 | return buf; 76 | } 77 | 78 | const char * 79 | cork_u128_to_padded_hex(char *buf, cork_u128 val) 80 | { 81 | uint64_t hi = val._.be64.hi; 82 | uint64_t lo = val._.be64.lo; 83 | snprintf(buf, CORK_U128_HEX_LENGTH, "%016" PRIx64 "%016" PRIx64, hi, lo); 84 | return buf; 85 | } 86 | -------------------------------------------------------------------------------- /3rd/libancillary/Makefile: -------------------------------------------------------------------------------- 1 | ########################################################################### 2 | # libancillary - black magic on Unix domain sockets 3 | # (C) Nicolas George 4 | # Makefile - guess what 5 | ########################################################################### 6 | 7 | # Redistribution and use in source and binary forms, with or without 8 | # modification, are permitted provided that the following conditions are met: 9 | # 10 | # 1. Redistributions of source code must retain the above copyright notice, 11 | # this list of conditions and the following disclaimer. 12 | # 2. Redistributions in binary form must reproduce the above copyright 13 | # notice, this list of conditions and the following disclaimer in the 14 | # documentation and/or other materials provided with the distribution. 15 | # 3. The name of the author may not be used to endorse or promote products 16 | # derived from this software without specific prior written permission. 17 | # 18 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 19 | # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 20 | # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 21 | # EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 24 | # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25 | # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26 | # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 27 | # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | 29 | CC=gcc 30 | CFLAGS=-Wall -g -O2 31 | LDFLAGS= 32 | LIBS= 33 | AR=ar 34 | RANLIB=ranlib 35 | RM=rm 36 | CP=cp 37 | MKDIR=mkdir 38 | TAR=tar 39 | GZIP=gzip -9 40 | 41 | NAME=libancillary 42 | DISTRIBUTION=API COPYING Makefile ancillary.h fd_send.c fd_recv.c test.c 43 | VERSION=0.9.1 44 | 45 | OBJECTS=fd_send.o fd_recv.o 46 | 47 | TUNE_OPTS=-DNDEBUG 48 | #TUNE_OPTS=-DNDEBUG \ 49 | -DSPARE_SEND_FDS -DSPARE_SEND_FD -DSPARE_RECV_FDS -DSPARE_RECV_FD 50 | 51 | .c.o: 52 | $(CC) -c $(CFLAGS) $(TUNE_OPTS) $< 53 | 54 | all: libancillary.a 55 | 56 | libancillary.a: $(OBJECTS) 57 | $(AR) cr $@ $(OBJECTS) 58 | $(RANLIB) $@ 59 | 60 | fd_send.o: ancillary.h 61 | fd_recv.o: ancillary.h 62 | 63 | test: test.c libancillary.a 64 | $(CC) -o $@ $(CFLAGS) $(LDFLAGS) -L. test.c -lancillary $(LIBS) 65 | 66 | clean: 67 | -$(RM) -f *.o *.a test 68 | 69 | dist: 70 | $(MKDIR) $(NAME)-$(VERSION) 71 | $(CP) $(DISTRIBUTION) $(NAME)-$(VERSION) 72 | $(TAR) -cf - $(NAME)-$(VERSION) | $(GZIP) > $(NAME)-$(VERSION).tar.gz 73 | $(RM) -rf $(NAME)-$(VERSION) 74 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/core/timestamp.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2011, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef LIBCORK_CORE_TIMESTAMP_H 12 | #define LIBCORK_CORE_TIMESTAMP_H 13 | 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | 21 | typedef uint64_t cork_timestamp; 22 | 23 | 24 | #define cork_timestamp_init_sec(ts, sec) \ 25 | do { \ 26 | *(ts) = (((uint64_t) (sec)) << 32); \ 27 | } while (0) 28 | 29 | #define cork_timestamp_init_gsec(ts, sec, gsec) \ 30 | do { \ 31 | *(ts) = (((uint64_t) (sec)) << 32) | \ 32 | (((uint64_t) (gsec)) & 0xffffffff); \ 33 | } while (0) 34 | 35 | #define cork_timestamp_init_msec(ts, sec, msec) \ 36 | do { \ 37 | *(ts) = (((uint64_t) (sec)) << 32) | \ 38 | ((((uint64_t) (msec)) << 32) / 1000); \ 39 | } while (0) 40 | 41 | #define cork_timestamp_init_usec(ts, sec, usec) \ 42 | do { \ 43 | *(ts) = (((uint64_t) (sec)) << 32) | \ 44 | ((((uint64_t) (usec)) << 32) / 1000000); \ 45 | } while (0) 46 | 47 | #define cork_timestamp_init_nsec(ts, sec, nsec) \ 48 | do { \ 49 | *(ts) = (((uint64_t) (sec)) << 32) | \ 50 | ((((uint64_t) (nsec)) << 32) / 1000000000); \ 51 | } while (0) 52 | 53 | 54 | CORK_API void 55 | cork_timestamp_init_now(cork_timestamp *ts); 56 | 57 | 58 | #define cork_timestamp_sec(ts) ((uint32_t) ((ts) >> 32)) 59 | #define cork_timestamp_gsec(ts) ((uint32_t) ((ts) & 0xffffffff)) 60 | 61 | CORK_ATTR_UNUSED 62 | static inline uint64_t 63 | cork_timestamp_gsec_to_units(const cork_timestamp ts, uint64_t denom) 64 | { 65 | uint64_t half = ((uint64_t) 1 << 31) / denom; 66 | uint64_t gsec = cork_timestamp_gsec(ts); 67 | gsec += half; 68 | gsec *= denom; 69 | gsec >>= 32; 70 | return gsec; 71 | } 72 | 73 | #define cork_timestamp_msec(ts) cork_timestamp_gsec_to_units(ts, 1000) 74 | #define cork_timestamp_usec(ts) cork_timestamp_gsec_to_units(ts, 1000000) 75 | #define cork_timestamp_nsec(ts) cork_timestamp_gsec_to_units(ts, 1000000000) 76 | 77 | 78 | CORK_API int 79 | cork_timestamp_format_utc(const cork_timestamp ts, const char *format, 80 | struct cork_buffer *dest); 81 | 82 | CORK_API int 83 | cork_timestamp_format_local(const cork_timestamp ts, const char *format, 84 | struct cork_buffer *dest); 85 | 86 | 87 | #endif /* LIBCORK_CORE_TIMESTAMP_H */ 88 | -------------------------------------------------------------------------------- /config.mk: -------------------------------------------------------------------------------- 1 | ifneq ($(OBJTREE),$(SRCTREE)) 2 | ifeq ($(CURDIR),$(SRCTREE)) 3 | dir := 4 | else 5 | dir := $(subst $(SRCTREE)/,,$(CURDIR)) 6 | endif 7 | obj := $(if $(dir),$(OBJTREE)/$(dir)/,$(OBJTREE)/) 8 | src := $(if $(dir),$(SRCTREE)/$(dir)/,$(SRCTREE)/) 9 | $(shell mkdir -p $(obj)) 10 | else 11 | # current object directory 12 | obj := 13 | src := 14 | endif 15 | 16 | ######################################################################### 17 | 18 | # 19 | # Include the make variables (CC, etc...) 20 | # 21 | AS = $(CROSS_COMPILE)as 22 | CC = $(CROSS_COMPILE)gcc 23 | LD = $(CROSS_COMPILE)ld 24 | CPP = $(CC) -E 25 | AR = $(CROSS_COMPILE)ar 26 | NM = $(CROSS_COMPILE)nm 27 | LDR = $(CROSS_COMPILE)ldr 28 | STRIP = $(CROSS_COMPILE)strip 29 | OBJCOPY = $(CROSS_COMPILE)objcopy 30 | OBJDUMP = $(CROSS_COMPILE)objdump 31 | RANLIB = $(CROSS_COMPILE)ranlib 32 | 33 | ######################################################################### 34 | 35 | export CROSS_COMPILE \ 36 | AS LD CC CPP AR NM STRIP OBJCOPY OBJDUMP MAKE 37 | 38 | ######################################################################### 39 | 40 | MAKEFLAGS += -rR --no-print-directory 41 | 42 | ifndef V 43 | Q = @ 44 | endif 45 | 46 | FINAL_CFLAGS = $(CPPFLAGS) $(CFLAGS) $(EXTRA_CFLAGS) 47 | FINAL_LDFLAGS = 48 | 49 | CCC=$(QUIET_CC)$(CC) $(FINAL_CFLAGS) 50 | LINK=$(QUIET_LINK)$(CC) $(FINAL_LDFLAGS) 51 | BUILD_AR=$(QUIET_AR)$(AR) 52 | BUILD_RANLIB=$(QUIET_RANLIB)$(RANLIB) 53 | INSTALL=$(QUIET_INSTALL) 54 | 55 | CCCOLOR="\033[34m" 56 | LINKCOLOR="\033[34;1m" 57 | SRCCOLOR="\033[33m" 58 | BINCOLOR="\033[32;1m" 59 | MAKECOLOR="\033[32;1m" 60 | ECHOCOLOR="\033[32;1m" 61 | ENDCOLOR="\033[0m" 62 | 63 | ifndef V 64 | QUIET_CC = @printf ' %b %b\n' $(CCCOLOR)CC$(ENDCOLOR) $(SRCCOLOR)$(subst $(OBJTREE)/,,$@)$(ENDCOLOR) 1>&2; 65 | QUIET_LINK = @printf ' %b %b\n' $(LINKCOLOR)LINK$(ENDCOLOR) $(BINCOLOR)$(subst $(OBJTREE)/,,$@)$(ENDCOLOR) 1>&2; 66 | QUIET_AR = @printf ' %b %b\n' $(LINKCOLOR)AR$(ENDCOLOR) $(BINCOLOR)$(subst $(OBJTREE)/,,$@)$(ENDCOLOR) 1>&2; 67 | QUIET_RANLIB = @printf ' %b %b\n' $(LINKCOLOR)RANLIB$(ENDCOLOR) $(BINCOLOR)$(subst $(OBJTREE)/,,$@)$(ENDCOLOR) 1>&2; 68 | QUIET_INSTALL = @printf ' %b %b\n' $(LINKCOLOR)INSTALL$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR) 1>&2; 69 | QUIET_STRIP_OPTION = > /dev/null 70 | endif 71 | 72 | ifneq ($(OBJTREE),$(SRCTREE)) 73 | define nicename 74 | @echo $(subst $(OBJTREE)/,,$1) 75 | endef 76 | else 77 | define nicename 78 | @echo $(subst $(OBJTREE)/,,$(CURDIR)/$1) 79 | endef 80 | endif 81 | 82 | ifneq ($(OBJTREE),$(SRCTREE)) 83 | cobj = $(subst $(OBJTREE)/,,$@) 84 | else 85 | cobj = $(subst $(OBJTREE)/,,$(CURDIR)/$@) 86 | endif 87 | 88 | %.o: %.c 89 | $(shell [ -d $(dir $(OBJTREE)/$@) ] || mkdir -p $(dir $@)) 90 | $(CCC) -c $< -o $(obj)$@ 91 | 92 | $(obj)%.o: %.c 93 | $(shell [ -d $(dir $(OBJTREE)/$@) ] || mkdir -p $(dir $@)) 94 | $(CCC) -c $< -o $@ 95 | -------------------------------------------------------------------------------- /CHANGES.md: -------------------------------------------------------------------------------- 1 | v0.5.1 (2020-10-07) 2 | ----------- 3 | * Feature: macOS support 4 | 5 | 6 | v0.5.0 (2020-10-06) 7 | ----------- 8 | * Fix: ipv6 sockaddr 9 | * Fix: crypto overlap 10 | * Change: chacha20 instead of salsa20 11 | 12 | 13 | v0.4.6 (2020-8-17) 14 | ----------- 15 | * Fix: don't use uv_write_t 16 | * Feature: log level 17 | 18 | 19 | v0.4.5 (2016-7-23) 20 | ----------- 21 | * Fix: Package avalible 22 | 23 | 24 | v0.4.4 (2015-10-21) 25 | ----------- 26 | * Fix: Openwrt start script 27 | 28 | 29 | v0.4.3 (2015-10-21) 30 | ----------- 31 | * Hotfix: Android build script 32 | 33 | 34 | v0.4.2 (2015-10-20) 35 | ----------- 36 | * Change: Rename program name 37 | 38 | 39 | v0.4.1 (2015-10-18) 40 | ----------- 41 | * Hotfix: Openwrt build script 42 | 43 | 44 | v0.4.0 (2015-10-18) 45 | ----------- 46 | * Feature: Protect Android VpnService 47 | * Update: Openwrt Script 48 | * Update: Build Script 49 | 50 | 51 | v0.3.1 (2015-10-8) 52 | ----------- 53 | * Fix: Close remote 54 | 55 | 56 | v0.3.0 (2015-10-1) 57 | ----------- 58 | * Feature: Support ACL 59 | 60 | 61 | v0.2.6 (2015-9-16) 62 | ----------- 63 | * Hotfix: Crypto init once only 64 | 65 | 66 | v0.2.5 (2015-9-15) 67 | ----------- 68 | * Fix: Crypto init once only 69 | 70 | 71 | v0.2.4 (2015-9-01) 72 | ----------- 73 | * Change: Update xforwarder's arguments 74 | 75 | 76 | v0.2.3 (2015-9-01) 77 | ----------- 78 | * Fix: Don't use pthread_setname_p under non linux platform 79 | 80 | 81 | v0.2.2 (2015-9-01) 82 | ----------- 83 | * Feature: Add Connection Timeout 84 | * Feature: Add UDP relay option 85 | * Fix: TLS key 86 | * Fix: UDP association 87 | * Change: Openwrt scripts 88 | 89 | 90 | v0.2.1 (2015-6-09) 91 | ----------- 92 | * Change: Build native xSocks with WinDDK for windows 93 | * Feature: Support mingw-w64 94 | 95 | 96 | v0.2.0 (2015-6-07) 97 | ----------- 98 | * Feature: Support WIN32 99 | 100 | 101 | v0.1.7 (2015-5-21) 102 | ----------- 103 | * Fix: Some minor bug 104 | 105 | 106 | v0.1.6 (2015-5-17) 107 | ----------- 108 | * Feature: Add Android build script 109 | 110 | 111 | v0.1.5 (2015-4-15) 112 | ----------- 113 | * Fix: Openwrt PKG_VERSION 114 | 115 | 116 | v0.1.4 (2015-4-15) 117 | ----------- 118 | * Feature: Add tproxy function to OpenWrt init script 119 | * Change: Pid file path 120 | 121 | 122 | v0.1.3 (2015-4-11) 123 | ----------- 124 | * Fix: Don't support MultiThreading if SO_REUSEPORT undeclared 125 | * Change: Default listen port 126 | 127 | 128 | v0.1.2 (2015-4-10) 129 | ----------- 130 | * Fix: Issue 1# that unknown type in clang 131 | * Fix: OpenWrt init script 132 | 133 | 134 | v0.1.1 (2015-4-03) 135 | ----------- 136 | * Fix: OpenWrt build script 137 | 138 | 139 | v0.1.0 (2015-4-02) 140 | ----------- 141 | * The first public version. 142 | 143 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/config/gcc.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2011-2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef LIBCORK_CONFIG_GCC_H 12 | #define LIBCORK_CONFIG_GCC_H 13 | 14 | /* Figure out the GCC version */ 15 | 16 | #if defined(__GNUC_PATCHLEVEL__) 17 | #define CORK_CONFIG_GCC_VERSION (__GNUC__ * 10000 \ 18 | + __GNUC_MINOR__ * 100 \ 19 | + __GNUC_PATCHLEVEL__) 20 | #else 21 | #define CORK_CONFIG_GCC_VERSION (__GNUC__ * 10000 \ 22 | + __GNUC_MINOR__ * 100) 23 | #endif 24 | 25 | 26 | /*----------------------------------------------------------------------- 27 | * Compiler attributes 28 | */ 29 | 30 | /* The GCC assembly syntax has been available basically forever. */ 31 | 32 | #if defined(CORK_CONFIG_GCC_VERSION) 33 | #define CORK_CONFIG_HAVE_GCC_ASM 1 34 | #else 35 | #define CORK_CONFIG_HAVE_GCC_ASM 0 36 | #endif 37 | 38 | /* The GCC atomic instrinsics are available as of GCC 4.1.0. */ 39 | 40 | #if CORK_CONFIG_GCC_VERSION >= 40100 41 | #define CORK_CONFIG_HAVE_GCC_ATOMICS 1 42 | #else 43 | #define CORK_CONFIG_HAVE_GCC_ATOMICS 0 44 | #endif 45 | 46 | /* The attributes we want to use are available as of GCC 2.96. */ 47 | 48 | #if CORK_CONFIG_GCC_VERSION >= 29600 49 | #define CORK_CONFIG_HAVE_GCC_ATTRIBUTES 1 50 | #else 51 | #define CORK_CONFIG_HAVE_GCC_ATTRIBUTES 0 52 | #endif 53 | 54 | /* __int128 seems to be available on 64-bit platforms as of GCC 4.6. The 55 | * attribute((mode(TI))) syntax seems to be available as of 4.1. */ 56 | 57 | #if CORK_CONFIG_ARCH_X64 && CORK_CONFIG_GCC_VERSION >= 40600 58 | #define CORK_CONFIG_HAVE_GCC_INT128 1 59 | #else 60 | #define CORK_CONFIG_HAVE_GCC_INT128 0 61 | #endif 62 | 63 | #if CORK_CONFIG_ARCH_X64 && CORK_CONFIG_GCC_VERSION >= 40100 64 | #define CORK_CONFIG_HAVE_GCC_MODE_ATTRIBUTE 1 65 | #else 66 | #define CORK_CONFIG_HAVE_GCC_MODE_ATTRIBUTE 0 67 | #endif 68 | 69 | /* Statement expressions have been available since GCC 3.1. */ 70 | 71 | #if CORK_CONFIG_GCC_VERSION >= 30100 72 | #define CORK_CONFIG_HAVE_GCC_STATEMENT_EXPRS 1 73 | #else 74 | #define CORK_CONFIG_HAVE_GCC_STATEMENT_EXPRS 0 75 | #endif 76 | 77 | /* Thread-local storage has been available since GCC 3.3, but not on Mac 78 | * OS X. */ 79 | 80 | #if !(defined(__APPLE__) && defined(__MACH__)) 81 | #if CORK_CONFIG_GCC_VERSION >= 30300 82 | #define CORK_CONFIG_HAVE_THREAD_STORAGE_CLASS 1 83 | #else 84 | #define CORK_CONFIG_HAVE_THREAD_STORAGE_CLASS 0 85 | #endif 86 | #else 87 | #define CORK_CONFIG_HAVE_THREAD_STORAGE_CLASS 0 88 | #endif 89 | 90 | 91 | #endif /* LIBCORK_CONFIG_GCC_H */ 92 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/helpers/posix.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2013, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license details. 7 | * ---------------------------------------------------------------------- 8 | */ 9 | 10 | #ifndef LIBCORK_HELPERS_POSIX_H 11 | #define LIBCORK_HELPERS_POSIX_H 12 | 13 | /* This header is *not* automatically included when you include 14 | * libcork/core.h, since we define some macros that don't include a 15 | * cork_ or CORK_ prefix. Don't want to pollute your namespace unless 16 | * you ask for it! */ 17 | 18 | #include 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | 25 | #if !defined(CORK_PRINT_ERRORS) 26 | #define CORK_PRINT_ERRORS 0 27 | #endif 28 | 29 | #if !defined(CORK_PRINT_ERROR) 30 | #if CORK_PRINT_ERRORS 31 | #include 32 | #define CORK_PRINT_ERROR_(func, file, line) \ 33 | fprintf(stderr, "---\nError in %s (%s:%u)\n %s\n", \ 34 | (func), (file), (unsigned int) (line), \ 35 | cork_error_message()); 36 | #define CORK_PRINT_ERROR() CORK_PRINT_ERROR_(__func__, __FILE__, __LINE__) 37 | #else 38 | #define CORK_PRINT_ERROR() /* do nothing */ 39 | #endif 40 | #endif 41 | 42 | 43 | #define xi_check_posix(call, on_error) \ 44 | do { \ 45 | while (true) { \ 46 | if ((call) == -1) { \ 47 | if (errno == EINTR) { \ 48 | continue; \ 49 | } else { \ 50 | cork_system_error_set(); \ 51 | CORK_PRINT_ERROR(); \ 52 | on_error; \ 53 | } \ 54 | } else { \ 55 | break; \ 56 | } \ 57 | } \ 58 | } while (0) 59 | 60 | #define xp_check_posix(call, on_error) \ 61 | do { \ 62 | while (true) { \ 63 | if ((call) == NULL) { \ 64 | if (errno == EINTR) { \ 65 | continue; \ 66 | } else { \ 67 | cork_system_error_set(); \ 68 | CORK_PRINT_ERROR(); \ 69 | on_error; \ 70 | } \ 71 | } else { \ 72 | break; \ 73 | } \ 74 | } \ 75 | } while (0) 76 | 77 | 78 | #define ei_check_posix(call) xi_check_posix(call, goto error) 79 | #define rii_check_posix(call) xi_check_posix(call, return -1) 80 | #define rpi_check_posix(call) xi_check_posix(call, return NULL) 81 | 82 | #define ep_check_posix(call) xp_check_posix(call, goto error) 83 | #define rip_check_posix(call) xp_check_posix(call, return -1) 84 | #define rpp_check_posix(call) xp_check_posix(call, return NULL) 85 | 86 | 87 | #endif /* LIBCORK_HELPERS_POSIX_H */ 88 | -------------------------------------------------------------------------------- /src/acl.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "util.h" 5 | #include "logger.h" 6 | #include "ipset/ipset.h" 7 | 8 | static struct ip_set acl_ipv4_set; 9 | static struct ip_set acl_ipv6_set; 10 | 11 | static void 12 | parse_addr_cidr(const char *str, char *host, int *cidr) { 13 | int ret = -1, n = 0; 14 | char *pch; 15 | 16 | pch = strchr(str, '/'); 17 | while (pch != NULL) { 18 | n++; 19 | ret = pch - str; 20 | pch = strchr(pch + 1, '/'); 21 | } 22 | if (ret == -1) { 23 | strcpy(host, str); 24 | *cidr = -1; 25 | } else { 26 | memcpy(host, str, ret); 27 | host[ret] = '\0'; 28 | *cidr = atoi(str + ret + 1); 29 | } 30 | } 31 | 32 | int 33 | acl_init(const char *path) { 34 | // initialize ipset 35 | ipset_init_library(); 36 | ipset_init(&acl_ipv4_set); 37 | ipset_init(&acl_ipv6_set); 38 | 39 | FILE *f = fopen(path, "r"); 40 | if (f == NULL) { 41 | logger_stderr("Invalid acl path"); 42 | return -1; 43 | } 44 | 45 | char line[256]; 46 | while (!feof(f)) { 47 | if (fgets(line, 256, f)) { 48 | // Trim the newline 49 | int len = strlen(line); 50 | if (len > 0 && line[len - 1] == '\n') { 51 | line[len - 1] = '\0'; 52 | } 53 | 54 | char host[256]; 55 | int cidr; 56 | parse_addr_cidr(line, host, &cidr); 57 | 58 | struct cork_ip addr; 59 | int err = cork_ip_init(&addr, host); 60 | if (!err) { 61 | if (addr.version == 4) { 62 | if (cidr >= 0) { 63 | ipset_ipv4_add_network(&acl_ipv4_set, &(addr.ip.v4), cidr); 64 | } else { 65 | ipset_ipv4_add(&acl_ipv4_set, &(addr.ip.v4)); 66 | } 67 | } else if (addr.version == 6) { 68 | if (cidr >= 0) { 69 | ipset_ipv6_add_network(&acl_ipv6_set, &(addr.ip.v6), cidr); 70 | } else { 71 | ipset_ipv6_add(&acl_ipv6_set, &(addr.ip.v6)); 72 | } 73 | } 74 | } 75 | } 76 | } 77 | 78 | fclose(f); 79 | 80 | return 0; 81 | } 82 | 83 | void 84 | acl_free(void) { 85 | ipset_done(&acl_ipv4_set); 86 | ipset_done(&acl_ipv6_set); 87 | } 88 | 89 | int 90 | acl_contains_ip(const char * host) { 91 | struct cork_ip addr; 92 | int err = cork_ip_init(&addr, host); 93 | if (err) { 94 | return 0; 95 | } 96 | 97 | if (addr.version == 4) { 98 | return ipset_contains_ipv4(&acl_ipv4_set, &(addr.ip.v4)); 99 | } else if (addr.version == 6) { 100 | return ipset_contains_ipv6(&acl_ipv6_set, &(addr.ip.v6)); 101 | } 102 | 103 | return 0; 104 | } 105 | -------------------------------------------------------------------------------- /openwrt/files/xSocks.init: -------------------------------------------------------------------------------- 1 | #!/bin/sh /etc/rc.common 2 | # Copyright (C) 2006-2014 OpenWrt.org 3 | 4 | START=72 5 | STOP=30 6 | FIREWALL_RELOAD=0 7 | 8 | SERVER=IP:PORT 9 | PASSWORD=PASSWORD 10 | 11 | LISTEN_PORT=1070 12 | IP_ROUTE_TABLE_NUMBER=100 13 | FWMARK="0x01/0x01" 14 | SETNAME=wall 15 | CHAIN=xSocks 16 | DNS=8.8.8.8 17 | BLACK_LIST=/etc/black_list 18 | 19 | 20 | start() { 21 | tproxy_start 22 | acl add 23 | mkdir -p /var/run/xSocks 24 | xSocks -s $SERVER -k $PASSWORD 25 | xTproxy -s $SERVER -k $PASSWORD 26 | xForwarder -l 0.0.0.0:5533 -d $DNS:53 -s $SERVER -k $PASSWORD 27 | } 28 | 29 | stop() { 30 | tproxy_stop 31 | acl del 32 | xSocks --signal stop 33 | xTproxy --signal stop 34 | xForwarder --signal stop 35 | } 36 | 37 | shutdown() { 38 | tproxy_stop 39 | acl del 40 | xSocks --signal quit 41 | xTproxy --signal quit 42 | xForwarder --signal quit 43 | } 44 | 45 | tproxy_start() { 46 | iptables -t nat -D PREROUTING -p tcp -j $CHAIN > /dev/null 2>&1 47 | iptables -t nat -F $CHAIN > /dev/null 2>&1 48 | iptables -t nat -X $CHAIN > /dev/null 2>&1 49 | 50 | iptables -t mangle -D PREROUTING -j $CHAIN > /dev/null 2>&1 51 | iptables -t mangle -F $CHAIN > /dev/null 2>&1 52 | iptables -t mangle -X $CHAIN > /dev/null 2>&1 53 | 54 | iptables -t nat -N $CHAIN 55 | iptables -t mangle -N $CHAIN 56 | 57 | ipset -N $SETNAME iphash -exist 58 | 59 | ### TCP 60 | iptables -t nat -A $CHAIN -p tcp -m set --match-set $SETNAME dst -j REDIRECT --to-port $LISTEN_PORT 61 | iptables -t nat -A PREROUTING -p tcp -j $CHAIN 62 | 63 | ### UDP 64 | ip rule del fwmark $FWMARK table $IP_ROUTE_TABLE_NUMBER > /dev/null 2>&1 65 | ip route del local 0.0.0.0/0 dev lo table $IP_ROUTE_TABLE_NUMBER > /dev/null 2>&1 66 | 67 | ip rule add fwmark $FWMARK table $IP_ROUTE_TABLE_NUMBER 68 | ip route add local 0.0.0.0/0 dev lo table $IP_ROUTE_TABLE_NUMBER 69 | 70 | iptables -t mangle -A $CHAIN -p udp -m set --match-set $SETNAME dst -j TPROXY \ 71 | --on-port $LISTEN_PORT --tproxy-mark $FWMARK 72 | iptables -t mangle -A PREROUTING -j $CHAIN 73 | } 74 | 75 | tproxy_stop() { 76 | iptables -t nat -D PREROUTING -p tcp -j $CHAIN > /dev/null 2>&1 77 | iptables -t nat -F $CHAIN > /dev/null 2>&1 78 | iptables -t nat -X $CHAIN > /dev/null 2>&1 79 | 80 | iptables -t mangle -D PREROUTING -j $CHAIN > /dev/null 2>&1 81 | iptables -t mangle -F $CHAIN > /dev/null 2>&1 82 | iptables -t mangle -X $CHAIN > /dev/null 2>&1 83 | 84 | ip rule del fwmark $FWMARK table $IP_ROUTE_TABLE_NUMBER > /dev/null 2>&1 85 | ip route del local 0.0.0.0/0 dev lo table $IP_ROUTE_TABLE_NUMBER > /dev/null 2>&1 86 | } 87 | 88 | acl() { 89 | if [ ! -f $BLACK_LIST ]; then 90 | return 91 | fi 92 | 93 | while read line;do 94 | [ -z "$line" ] && continue 95 | case "$line" in \#*) continue ;; esac 96 | ipset $1 $SETNAME $line --exist 97 | done < $BLACK_LIST 98 | } 99 | -------------------------------------------------------------------------------- /3rd/libipset/src/map/storage.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2009-2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the LICENSE.txt file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #include 17 | 18 | #include "ipset/bdd/nodes.h" 19 | #include "ipset/errors.h" 20 | #include "ipset/ipset.h" 21 | 22 | 23 | static void 24 | create_errno_error(FILE *stream) 25 | { 26 | if (ferror(stream)) { 27 | cork_error_set(IPSET_ERROR, IPSET_IO_ERROR, "%s", strerror(errno)); 28 | } else { 29 | cork_unknown_error(); 30 | } 31 | } 32 | 33 | struct file_consumer { 34 | /* file_consumer is a subclass of cork_stream_consumer */ 35 | struct cork_stream_consumer parent; 36 | /* the file to write the data into */ 37 | FILE *fp; 38 | }; 39 | 40 | static int 41 | file_consumer_data(struct cork_stream_consumer *vself, 42 | const void *buf, size_t size, bool is_first) 43 | { 44 | struct file_consumer *self = 45 | cork_container_of(vself, struct file_consumer, parent); 46 | size_t bytes_written = fwrite(buf, 1, size, self->fp); 47 | /* If there was an error writing to the file, then signal this to 48 | * the producer */ 49 | if (bytes_written == size) { 50 | return 0; 51 | } else { 52 | create_errno_error(self->fp); 53 | return -1; 54 | } 55 | } 56 | 57 | static int 58 | file_consumer_eof(struct cork_stream_consumer *vself) 59 | { 60 | /* We don't close the file, so there's nothing special to do at 61 | * end-of-stream. */ 62 | return 0; 63 | } 64 | 65 | 66 | int 67 | ipmap_save_to_stream(struct cork_stream_consumer *stream, 68 | const struct ip_map *map) 69 | { 70 | return ipset_node_cache_save(stream, map->cache, map->map_bdd); 71 | } 72 | 73 | int 74 | ipmap_save(FILE *fp, const struct ip_map *map) 75 | { 76 | struct file_consumer stream = { 77 | { file_consumer_data, file_consumer_eof, NULL }, fp 78 | }; 79 | return ipmap_save_to_stream(&stream.parent, map); 80 | } 81 | 82 | 83 | struct ip_map * 84 | ipmap_load(FILE *stream) 85 | { 86 | struct ip_map *map; 87 | ipset_node_id new_bdd; 88 | 89 | /* It doesn't matter what default value we use here, because we're 90 | * going to replace it with the default BDD we load in from the 91 | * file. */ 92 | map = ipmap_new(0); 93 | new_bdd = ipset_node_cache_load(stream, map->cache); 94 | if (cork_error_occurred()) { 95 | ipmap_free(map); 96 | return NULL; 97 | } 98 | 99 | map->map_bdd = new_bdd; 100 | return map; 101 | } 102 | -------------------------------------------------------------------------------- /3rd/libipset/src/bdd/reachable.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2010-2013, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the LICENSE.txt file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | #include "ipset/bdd/nodes.h" 15 | #include "ipset/logging.h" 16 | 17 | 18 | size_t 19 | ipset_node_reachable_count(const struct ipset_node_cache *cache, 20 | ipset_node_id node) 21 | { 22 | /* Create a set to track when we've visited a given node. */ 23 | struct cork_hash_table *visited = cork_pointer_hash_table_new(0, 0); 24 | 25 | /* And a queue of nodes to check. */ 26 | cork_array(ipset_node_id) queue; 27 | cork_array_init(&queue); 28 | 29 | if (ipset_node_get_type(node) == IPSET_NONTERMINAL_NODE) { 30 | DEBUG("Adding node %u to queue", node); 31 | cork_array_append(&queue, node); 32 | } 33 | 34 | /* And somewhere to store the result. */ 35 | size_t node_count = 0; 36 | 37 | /* Check each node in turn. */ 38 | while (!cork_array_is_empty(&queue)) { 39 | ipset_node_id curr = cork_array_at(&queue, --queue.size); 40 | 41 | /* We don't have to do anything if this node is already in the 42 | * visited set. */ 43 | if (cork_hash_table_get(visited, (void *) (uintptr_t) curr) == NULL) { 44 | DEBUG("Visiting node %u for the first time", curr); 45 | 46 | /* Add the node to the visited set. */ 47 | cork_hash_table_put 48 | (visited, (void *) (uintptr_t) curr, 49 | (void *) (uintptr_t) true, NULL, NULL, NULL); 50 | 51 | /* Increase the node count. */ 52 | node_count++; 53 | 54 | /* And add the node's nonterminal children to the visit 55 | * queue. */ 56 | struct ipset_node *node = 57 | ipset_node_cache_get_nonterminal(cache, curr); 58 | 59 | if (ipset_node_get_type(node->low) == IPSET_NONTERMINAL_NODE) { 60 | DEBUG("Adding node %u to queue", node->low); 61 | cork_array_append(&queue, node->low); 62 | } 63 | 64 | if (ipset_node_get_type(node->high) == IPSET_NONTERMINAL_NODE) { 65 | DEBUG("Adding node %u to queue", node->high); 66 | cork_array_append(&queue, node->high); 67 | } 68 | } 69 | } 70 | 71 | /* Return the result, freeing everything before we go. */ 72 | cork_hash_table_free(visited); 73 | cork_array_done(&queue); 74 | return node_count; 75 | } 76 | 77 | 78 | size_t 79 | ipset_node_memory_size(const struct ipset_node_cache *cache, 80 | ipset_node_id node) 81 | { 82 | return ipset_node_reachable_count(cache, node) * sizeof(struct ipset_node); 83 | } 84 | -------------------------------------------------------------------------------- /xSocks.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {2d83fb11-3237-4121-8998-70bd2cfc6e2a} 6 | 7 | 8 | {059595f4-122f-4e48-882a-a376b82dc373} 9 | 10 | 11 | {c449a96e-2803-4242-80b7-e91d0224276e} 12 | 13 | 14 | 15 | 16 | Header Files 17 | 18 | 19 | Header Files 20 | 21 | 22 | Header Files 23 | 24 | 25 | Header Files 26 | 27 | 28 | Header Files 29 | 30 | 31 | Header Files 32 | 33 | 34 | Header Files 35 | 36 | 37 | Header Files 38 | 39 | 40 | Header Files 41 | 42 | 43 | Header Files 44 | 45 | 46 | Header Files 47 | 48 | 49 | 50 | 51 | Source Files 52 | 53 | 54 | Source Files 55 | 56 | 57 | Source Files 58 | 59 | 60 | Source Files 61 | 62 | 63 | Source Files 64 | 65 | 66 | Source Files 67 | 68 | 69 | Source Files 70 | 71 | 72 | Source Files 73 | 74 | 75 | Source Files 76 | 77 | 78 | Source Files 79 | 80 | 81 | Source Files 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /3rd/libipset/src/set/storage.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2010-2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the LICENSE.txt file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #include 17 | 18 | #include "ipset/bdd/nodes.h" 19 | #include "ipset/errors.h" 20 | #include "ipset/ipset.h" 21 | 22 | 23 | static void 24 | create_errno_error(FILE *stream) 25 | { 26 | if (ferror(stream)) { 27 | cork_error_set(IPSET_ERROR, IPSET_IO_ERROR, "%s", strerror(errno)); 28 | } else { 29 | cork_unknown_error(); 30 | } 31 | } 32 | 33 | struct file_consumer { 34 | /* file_consumer is a subclass of cork_stream_consumer */ 35 | struct cork_stream_consumer parent; 36 | /* the file to write the data into */ 37 | FILE *fp; 38 | }; 39 | 40 | static int 41 | file_consumer_data(struct cork_stream_consumer *vself, 42 | const void *buf, size_t size, bool is_first) 43 | { 44 | struct file_consumer *self = 45 | cork_container_of(vself, struct file_consumer, parent); 46 | size_t bytes_written = fwrite(buf, 1, size, self->fp); 47 | /* If there was an error writing to the file, then signal this to 48 | * the producer */ 49 | if (bytes_written == size) { 50 | return 0; 51 | } else { 52 | create_errno_error(self->fp); 53 | return -1; 54 | } 55 | } 56 | 57 | static int 58 | file_consumer_eof(struct cork_stream_consumer *vself) 59 | { 60 | /* We don't close the file, so there's nothing special to do at 61 | * end-of-stream. */ 62 | return 0; 63 | } 64 | 65 | int 66 | ipset_save_to_stream(struct cork_stream_consumer *stream, 67 | const struct ip_set *set) 68 | { 69 | return ipset_node_cache_save(stream, set->cache, set->set_bdd); 70 | } 71 | 72 | int 73 | ipset_save(FILE *fp, const struct ip_set *set) 74 | { 75 | struct file_consumer stream = { 76 | { file_consumer_data, file_consumer_eof, NULL }, fp 77 | }; 78 | return ipset_save_to_stream(&stream.parent, set); 79 | } 80 | 81 | 82 | int 83 | ipset_save_dot(FILE *fp, const struct ip_set *set) 84 | { 85 | struct file_consumer stream = { 86 | { file_consumer_data, file_consumer_eof, NULL }, fp 87 | }; 88 | return ipset_node_cache_save_dot(&stream.parent, set->cache, set->set_bdd); 89 | } 90 | 91 | 92 | struct ip_set * 93 | ipset_load(FILE *stream) 94 | { 95 | struct ip_set *set; 96 | ipset_node_id new_bdd; 97 | 98 | set = ipset_new(); 99 | new_bdd = ipset_node_cache_load(stream, set->cache); 100 | if (cork_error_occurred()) { 101 | ipset_free(set); 102 | return NULL; 103 | } 104 | 105 | set->set_bdd = new_bdd; 106 | return set; 107 | } 108 | -------------------------------------------------------------------------------- /src/consumer.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "uv.h" 8 | #include "logger.h" 9 | #include "util.h" 10 | #include "common.h" 11 | 12 | 13 | struct resolver_context * 14 | resolver_init(uv_loop_t *loop, int mode, char **nameservers, int nameserver_num) __attribute__((weak)); 15 | void resolver_shutdown(struct resolver_context *rctx) __attribute__((weak)); 16 | void resolver_destroy(struct resolver_context *ctx) __attribute__((weak)); 17 | uv_key_t thread_resolver_key __attribute__((weak)); 18 | 19 | int udprelay_start(uv_loop_t *loop, struct server_context *server) __attribute__((weak)); 20 | void udprelay_close(struct server_context *server) __attribute__((weak)); 21 | 22 | extern void close_loop(uv_loop_t *loop); 23 | 24 | 25 | static void 26 | consumer_close(uv_async_t *handle) { 27 | struct server_context *server = container_of(handle, struct server_context, async_handle); 28 | 29 | uv_close((uv_handle_t*) &server->tcp, NULL); 30 | uv_close((uv_handle_t*) &server->async_handle, NULL); 31 | 32 | if (server->udprelay) { 33 | udprelay_close(server); 34 | } 35 | 36 | if (server->resolver) { 37 | struct resolver_context *dns = uv_key_get(&thread_resolver_key); 38 | resolver_shutdown(dns); 39 | } 40 | } 41 | 42 | static void 43 | tcp_bind(uv_loop_t *loop, struct server_context *server) { 44 | int rc; 45 | 46 | uv_tcp_init(loop, &server->tcp); 47 | 48 | rc = uv_tcp_open(&server->tcp, server->tcp_fd); 49 | if (rc) { 50 | logger_stderr("tcp open error: %s", uv_strerror(rc)); 51 | } 52 | 53 | uv_async_init(loop, &server->async_handle, consumer_close); 54 | 55 | rc = uv_tcp_bind(&server->tcp, server->local_addr, 0); 56 | if (rc || errno) { 57 | logger_stderr("bind error: %s", rc ? uv_strerror(rc) : strerror(errno)); 58 | exit(1); 59 | } 60 | 61 | rc = uv_listen((uv_stream_t*)&server->tcp, SOMAXCONN, server->accept_cb); 62 | if (rc) { 63 | logger_stderr("listen error: %s", rc ? uv_strerror(rc) : strerror(errno)); 64 | exit(1); 65 | } 66 | } 67 | 68 | void 69 | consumer_start(void *arg) { 70 | uv_loop_t loop; 71 | struct server_context *server = arg; 72 | 73 | #ifndef SO_REUSEPORT 74 | logger_stderr("don't support SO_REUSEPORT"); 75 | exit(1); 76 | #endif 77 | 78 | #ifdef __linux__ 79 | #ifndef CROSS_COMPILE 80 | char name[24] = {0}; 81 | sprintf(name, "consumer-%d", server->index + 1); 82 | pthread_setname_np(pthread_self(), name); 83 | #endif 84 | #endif 85 | 86 | uv_loop_init(&loop); 87 | 88 | struct resolver_context *dns = NULL; 89 | if (server->resolver) { 90 | char **servers = server->nameserver_num == 0 ? NULL : server->nameservers; 91 | dns = resolver_init(&loop, 0, servers, server->nameserver_num); 92 | uv_key_set(&thread_resolver_key, dns); 93 | } 94 | 95 | tcp_bind(&loop, server); 96 | 97 | if (server->udprelay) { 98 | udprelay_start(&loop, server); 99 | } 100 | 101 | uv_run(&loop, UV_RUN_DEFAULT); 102 | 103 | close_loop(&loop); 104 | 105 | if (server->resolver) { 106 | resolver_destroy(dns); 107 | } 108 | 109 | uv_sem_post(&server->semaphore); 110 | } 111 | -------------------------------------------------------------------------------- /.ycm_extra_conf.py: -------------------------------------------------------------------------------- 1 | 2 | import os 3 | import ycm_core 4 | 5 | flags = [ 6 | '-Wall', 7 | '-std=gnu99', 8 | '-x', 9 | 'c', 10 | '-isystem', 11 | '/usr/include', 12 | '-isystem', 13 | '/usr/local/include', 14 | '-Isrc', 15 | '-I3rd/c-ares', 16 | '-I3rd/libuv/include', 17 | '-I3rd/libsodium/src/libsodium/include', 18 | '-I3rd/libancillary', 19 | '-I3rd/libcork/include', 20 | '-I3rd/libipset/include', 21 | ] 22 | 23 | compilation_database_folder = '' 24 | 25 | if os.path.exists( compilation_database_folder ): 26 | database = ycm_core.CompilationDatabase( compilation_database_folder ) 27 | else: 28 | database = None 29 | 30 | SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ] 31 | 32 | def DirectoryOfThisScript(): 33 | return os.path.dirname( os.path.abspath( __file__ ) ) 34 | 35 | def MakeRelativePathsInFlagsAbsolute( flags, working_directory ): 36 | if not working_directory: 37 | return list( flags ) 38 | new_flags = [] 39 | make_next_absolute = False 40 | path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ] 41 | for flag in flags: 42 | new_flag = flag 43 | 44 | if make_next_absolute: 45 | make_next_absolute = False 46 | if not flag.startswith( '/' ): 47 | new_flag = os.path.join( working_directory, flag ) 48 | 49 | for path_flag in path_flags: 50 | if flag == path_flag: 51 | make_next_absolute = True 52 | break 53 | 54 | if flag.startswith( path_flag ): 55 | path = flag[ len( path_flag ): ] 56 | new_flag = path_flag + os.path.join( working_directory, path ) 57 | break 58 | 59 | if new_flag: 60 | new_flags.append( new_flag ) 61 | return new_flags 62 | 63 | 64 | def IsHeaderFile( filename ): 65 | extension = os.path.splitext( filename )[ 1 ] 66 | return extension in [ '.h', '.hxx', '.hpp', '.hh' ] 67 | 68 | 69 | def GetCompilationInfoForFile( filename ): 70 | # The compilation_commands.json file generated by CMake does not have entries 71 | # for header files. So we do our best by asking the db for flags for a 72 | # corresponding source file, if any. If one exists, the flags for that file 73 | # should be good enough. 74 | if IsHeaderFile( filename ): 75 | basename = os.path.splitext( filename )[ 0 ] 76 | for extension in SOURCE_EXTENSIONS: 77 | replacement_file = basename + extension 78 | if os.path.exists( replacement_file ): 79 | compilation_info = database.GetCompilationInfoForFile( 80 | replacement_file ) 81 | if compilation_info.compiler_flags_: 82 | return compilation_info 83 | return None 84 | return database.GetCompilationInfoForFile( filename ) 85 | 86 | 87 | # This is the entry point; this function is called by ycmd to produce flags for a file. 88 | def FlagsForFile( filename, **kwargs ): 89 | if database: 90 | # Bear in mind that compilation_info.compiler_flags_ does NOT return a 91 | # python list, but a "list-like" StringVec object 92 | compilation_info = GetCompilationInfoForFile( filename ) 93 | if not compilation_info: 94 | return None 95 | 96 | final_flags = MakeRelativePathsInFlagsAbsolute( 97 | compilation_info.compiler_flags_, 98 | compilation_info.compiler_working_dir_ ) 99 | 100 | else: 101 | relative_to = DirectoryOfThisScript() 102 | final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to ) 103 | 104 | return { 105 | 'flags': final_flags, 106 | 'do_cache': True 107 | } 108 | -------------------------------------------------------------------------------- /3rd/libancillary/fd_send.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * libancillary - black magic on Unix domain sockets 3 | * (C) Nicolas George 4 | * fd_send.c - sending file descriptors 5 | ***************************************************************************/ 6 | 7 | /* 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 3. The name of the author may not be used to endorse or promote products 17 | * derived from this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 20 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 22 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 24 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 25 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 26 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 27 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 28 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | #ifndef _XPG4_2 /* Solaris sucks */ 32 | # define _XPG4_2 33 | #endif 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #if defined(__FreeBSD__) 41 | # include /* FreeBSD sucks */ 42 | #endif 43 | 44 | #include "ancillary.h" 45 | 46 | int 47 | ancil_send_fds_with_buffer(int sock, const int *fds, unsigned n_fds, void *buffer) 48 | { 49 | struct msghdr msghdr; 50 | char nothing = '!'; 51 | struct iovec nothing_ptr; 52 | struct cmsghdr *cmsg; 53 | int i; 54 | 55 | nothing_ptr.iov_base = ¬hing; 56 | nothing_ptr.iov_len = 1; 57 | msghdr.msg_name = NULL; 58 | msghdr.msg_namelen = 0; 59 | msghdr.msg_iov = ¬hing_ptr; 60 | msghdr.msg_iovlen = 1; 61 | msghdr.msg_flags = 0; 62 | msghdr.msg_control = buffer; 63 | msghdr.msg_controllen = sizeof(struct cmsghdr) + sizeof(int) * n_fds; 64 | cmsg = CMSG_FIRSTHDR(&msghdr); 65 | cmsg->cmsg_len = msghdr.msg_controllen; 66 | cmsg->cmsg_level = SOL_SOCKET; 67 | cmsg->cmsg_type = SCM_RIGHTS; 68 | for(i = 0; i < n_fds; i++) 69 | ((int *)CMSG_DATA(cmsg))[i] = fds[i]; 70 | return(sendmsg(sock, &msghdr, 0) >= 0 ? 0 : -1); 71 | } 72 | 73 | #ifndef SPARE_SEND_FDS 74 | int 75 | ancil_send_fds(int sock, const int *fds, unsigned n_fds) 76 | { 77 | ANCIL_FD_BUFFER(ANCIL_MAX_N_FDS) buffer; 78 | 79 | assert(n_fds <= ANCIL_MAX_N_FDS); 80 | return(ancil_send_fds_with_buffer(sock, fds, n_fds, &buffer)); 81 | } 82 | #endif /* SPARE_SEND_FDS */ 83 | 84 | #ifndef SPARE_SEND_FD 85 | int 86 | ancil_send_fd(int sock, int fd) 87 | { 88 | ANCIL_FD_BUFFER(1) buffer; 89 | 90 | return(ancil_send_fds_with_buffer(sock, &fd, 1, &buffer)); 91 | } 92 | #endif /* SPARE_SEND_FD */ 93 | -------------------------------------------------------------------------------- /3rd/libancillary/test.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * libancillary - black magic on Unix domain sockets 3 | * (C) Nicolas George 4 | * test.c - testing and example program 5 | ***************************************************************************/ 6 | 7 | /* 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 3. The name of the author may not be used to endorse or promote products 17 | * derived from this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 20 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 22 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 24 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 25 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 26 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 27 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 28 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include "ancillary.h" 39 | 40 | void child_process(int sock) 41 | { 42 | int fd; 43 | int fds[3], nfds; 44 | char b[] = "This is on the received fd!\n"; 45 | 46 | if(ancil_recv_fd(sock, &fd)) { 47 | perror("ancil_recv_fd"); 48 | exit(1); 49 | } else { 50 | printf("Received fd: %d\n", fd); 51 | } 52 | write(fd, b, sizeof(b)); 53 | close(fd); 54 | sleep(2); 55 | 56 | nfds = ancil_recv_fds(sock, fds, 3); 57 | if(nfds < 0) { 58 | perror("ancil_recv_fds"); 59 | exit(1); 60 | } else { 61 | printf("Received %d/3 fds : %d %d %d.\n", nfds, 62 | fds[0], fds[1], fds[2]); 63 | } 64 | } 65 | 66 | void parent_process(int sock) 67 | { 68 | int fds[2] = { 1, 2 }; 69 | 70 | if(ancil_send_fd(sock, 1)) { 71 | perror("ancil_send_fd"); 72 | exit(1); 73 | } else { 74 | printf("Sent fd.\n"); 75 | } 76 | sleep(1); 77 | 78 | if(ancil_send_fds(sock, fds, 2)) { 79 | perror("ancil_send_fds"); 80 | exit(1); 81 | } else { 82 | printf("Sent two fds.\n"); 83 | } 84 | } 85 | 86 | int main(void) 87 | { 88 | int sock[2]; 89 | 90 | if(socketpair(PF_UNIX, SOCK_STREAM, 0, sock)) { 91 | perror("socketpair"); 92 | exit(1); 93 | } else { 94 | printf("Established socket pair: (%d, %d)\n", sock[0], sock[1]); 95 | } 96 | 97 | switch(fork()) { 98 | case 0: 99 | close(sock[0]); 100 | child_process(sock[1]); 101 | break; 102 | case -1: 103 | perror("fork"); 104 | exit(1); 105 | default: 106 | close(sock[1]); 107 | parent_process(sock[0]); 108 | wait(NULL); 109 | break; 110 | } 111 | return(0); 112 | } 113 | -------------------------------------------------------------------------------- /3rd/libancillary/fd_recv.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * libancillary - black magic on Unix domain sockets 3 | * (C) Nicolas George 4 | * fd_send.c - receiving file descriptors 5 | ***************************************************************************/ 6 | 7 | /* 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 3. The name of the author may not be used to endorse or promote products 17 | * derived from this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 20 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 22 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 24 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 25 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 26 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 27 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 28 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | #ifndef _XPG4_2 /* Solaris sucks */ 32 | # define _XPG4_2 33 | #endif 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #if defined(__FreeBSD__) 41 | # include /* FreeBSD sucks */ 42 | #endif 43 | 44 | #include "ancillary.h" 45 | 46 | int 47 | ancil_recv_fds_with_buffer(int sock, int *fds, unsigned n_fds, void *buffer) 48 | { 49 | struct msghdr msghdr; 50 | char nothing; 51 | struct iovec nothing_ptr; 52 | struct cmsghdr *cmsg; 53 | int i; 54 | 55 | nothing_ptr.iov_base = ¬hing; 56 | nothing_ptr.iov_len = 1; 57 | msghdr.msg_name = NULL; 58 | msghdr.msg_namelen = 0; 59 | msghdr.msg_iov = ¬hing_ptr; 60 | msghdr.msg_iovlen = 1; 61 | msghdr.msg_flags = 0; 62 | msghdr.msg_control = buffer; 63 | msghdr.msg_controllen = sizeof(struct cmsghdr) + sizeof(int) * n_fds; 64 | cmsg = CMSG_FIRSTHDR(&msghdr); 65 | cmsg->cmsg_len = msghdr.msg_controllen; 66 | cmsg->cmsg_level = SOL_SOCKET; 67 | cmsg->cmsg_type = SCM_RIGHTS; 68 | for(i = 0; i < n_fds; i++) 69 | ((int *)CMSG_DATA(cmsg))[i] = -1; 70 | 71 | if(recvmsg(sock, &msghdr, 0) < 0) 72 | return(-1); 73 | for(i = 0; i < n_fds; i++) 74 | fds[i] = ((int *)CMSG_DATA(cmsg))[i]; 75 | n_fds = (msghdr.msg_controllen - sizeof(struct cmsghdr)) / sizeof(int); 76 | return(n_fds); 77 | } 78 | 79 | #ifndef SPARE_RECV_FDS 80 | int 81 | ancil_recv_fds(int sock, int *fd, unsigned n_fds) 82 | { 83 | ANCIL_FD_BUFFER(ANCIL_MAX_N_FDS) buffer; 84 | 85 | assert(n_fds <= ANCIL_MAX_N_FDS); 86 | return(ancil_recv_fds_with_buffer(sock, fd, n_fds, &buffer)); 87 | } 88 | #endif /* SPARE_RECV_FDS */ 89 | 90 | #ifndef SPARE_RECV_FD 91 | int 92 | ancil_recv_fd(int sock, int *fd) 93 | { 94 | ANCIL_FD_BUFFER(1) buffer; 95 | 96 | return(ancil_recv_fds_with_buffer(sock, fd, 1, &buffer) == 1 ? 0 : -1); 97 | } 98 | #endif /* SPARE_RECV_FD */ 99 | -------------------------------------------------------------------------------- /src/logger.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #ifndef _WIN32 8 | #include 9 | #endif 10 | #ifdef ANDROID 11 | #include 12 | #endif 13 | 14 | #include "uv.h" 15 | #include "logger.h" 16 | 17 | 18 | #define LOG_MESSAGE_SIZE 256 19 | 20 | static int _syslog = 0; 21 | static int _level = LOG_INFO; 22 | 23 | #ifdef _WIN32 24 | static uv_tty_t _tty; 25 | static uv_tty_t _ttyerr; 26 | #endif 27 | 28 | #ifdef _MSC_VER 29 | #define vsnprintf _vsnprintf 30 | #endif 31 | 32 | static const char *levels[] = { 33 | "EMERG", "ALERT", "CRIT", "ERRO", "WARN", "NOTICE", "INFO", "DEBG" 34 | }; 35 | 36 | #ifndef ANDROID 37 | static const char *colors[] = { 38 | "\033[01;31m", "\033[01;31m", "\033[01;31m", "\033[01;31m", "\033[01;33m", "\033[01;33m", "\033[01;32m", "\033[01;36m" 39 | }; 40 | #endif 41 | 42 | 43 | #ifdef _WIN32 44 | static void 45 | syslog(int priority, const char *format, ...) { 46 | } 47 | 48 | static void 49 | tty_send_cb(uv_write_t *req, int status) { 50 | free(req); 51 | } 52 | 53 | static void 54 | log2tty(uv_tty_t *tty, char *msg) { 55 | uv_write_t *req = malloc(sizeof(*req)); 56 | uv_buf_t buf = uv_buf_init(msg, strlen(msg)); 57 | 58 | if (uv_guess_handle(1) == UV_TTY) { 59 | uv_write(req, (uv_stream_t*)tty, &buf, 1, tty_send_cb); 60 | } 61 | } 62 | 63 | #else 64 | static void 65 | log2std(FILE *file, const char *msg) { 66 | fprintf(file, "%s", msg); 67 | } 68 | #endif 69 | 70 | void 71 | logger_log(uint32_t level, const char *msg, ...) { 72 | char tmp[LOG_MESSAGE_SIZE]; 73 | 74 | if (level > _level) { 75 | return; 76 | } 77 | 78 | va_list ap; 79 | va_start(ap, msg); 80 | vsnprintf(tmp, LOG_MESSAGE_SIZE, msg, ap); 81 | va_end(ap); 82 | 83 | if (_syslog) { 84 | syslog(level, "[%s] %s", levels[level], tmp); 85 | 86 | } else { 87 | #ifdef ANDROID 88 | if (level <= LOG_ERR) { 89 | level = ANDROID_LOG_ERROR; 90 | } else if (level == LOG_WARNING) { 91 | level = ANDROID_LOG_WARN; 92 | } else if (level == LOG_DEBUG) { 93 | level = ANDROID_LOG_DEBUG; 94 | } else { 95 | level = ANDROID_LOG_INFO; 96 | } 97 | __android_log_print(level, "xSocks", tmp); 98 | #else 99 | time_t curtime = time(NULL); 100 | struct tm *loctime = localtime(&curtime); 101 | char timestr[20]; 102 | strftime(timestr, 20, "%Y/%m/%d %H:%M:%S", loctime); 103 | char m[300] = { 0 }; 104 | sprintf(m, "%s %s%s\033[0m %s\n", timestr, colors[level], levels[level], tmp); 105 | #ifdef _WIN32 106 | log2tty(&_tty, m); 107 | #else 108 | log2std(stdout, m); 109 | #endif 110 | #endif 111 | } 112 | } 113 | 114 | void 115 | logger_stderr(const char *msg, ...) { 116 | char timestr[20]; 117 | time_t curtime = time(NULL); 118 | struct tm *loctime = localtime(&curtime); 119 | 120 | char tmp[LOG_MESSAGE_SIZE]; 121 | 122 | va_list ap; 123 | va_start(ap, msg); 124 | vsnprintf(tmp, LOG_MESSAGE_SIZE, msg, ap); 125 | va_end(ap); 126 | 127 | strftime(timestr, 20, "%Y/%m/%d %H:%M:%S", loctime); 128 | char m[300] = { 0 }; 129 | sprintf(m, "\033[01;31m%s [%s]\033[0m: %s\n", timestr, levels[LOG_ERR], tmp); 130 | 131 | #ifdef _WIN32 132 | log2tty(&_ttyerr, m); 133 | #else 134 | log2std(stderr, m); 135 | #endif 136 | } 137 | 138 | int 139 | logger_init(int syslog, int level) { 140 | #ifndef _WIN32 141 | _syslog = syslog; 142 | #else 143 | uv_tty_init(uv_default_loop(), &_tty, 1, 0); 144 | uv_tty_init(uv_default_loop(), &_ttyerr, 2, 0); 145 | uv_tty_set_mode(&_tty, UV_TTY_MODE_NORMAL); 146 | uv_tty_set_mode(&_ttyerr, UV_TTY_MODE_NORMAL); 147 | #endif 148 | _level = level; 149 | 150 | return 0; 151 | } 152 | 153 | void 154 | logger_exit() { 155 | #ifdef _WIN32 156 | uv_tty_reset_mode(); 157 | #endif 158 | } 159 | -------------------------------------------------------------------------------- /3rd/libipset/src/set/inspection-template.c.in: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the LICENSE.txt file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #include 12 | 13 | #include "ipset/bdd/nodes.h" 14 | #include "ipset/bits.h" 15 | #include "ipset/errors.h" 16 | #include "ipset/ipset.h" 17 | 18 | 19 | /** 20 | * Given a BDD variable number, return the index of the corresponding 21 | * bit in an IP address. IPv4 addresses use variables 1-32; IPv6 22 | * addresses use 1-128. (Variable 0 is used to identify the kind of 23 | * address — TRUE for IPv4, FALSE for IPv6.) 24 | */ 25 | 26 | static unsigned int 27 | IPSET_NAME(bit_for_var)(ipset_variable var) 28 | { 29 | return (var - 1); 30 | } 31 | 32 | 33 | /** 34 | * An assignment function that can be used to evaluate an IP set BDD. 35 | */ 36 | 37 | static bool 38 | IPSET_NAME(assignment)(const void *addr, ipset_variable var) 39 | { 40 | if (var == 0) { 41 | return IP_DISCRIMINATOR_VALUE; 42 | } else { 43 | unsigned int bit = IPSET_NAME(bit_for_var)(var); 44 | return IPSET_BIT_GET(addr, bit); 45 | } 46 | } 47 | 48 | 49 | bool 50 | IPSET_PRENAME(contains)(const struct ip_set *set, CORK_IP *elem) 51 | { 52 | return ipset_node_evaluate 53 | (set->cache, set->set_bdd, IPSET_NAME(assignment), elem); 54 | } 55 | 56 | 57 | bool 58 | IPSET_NAME(add_network)(struct ip_set *set, CORK_IP *elem, 59 | unsigned int cidr_prefix) 60 | { 61 | /* Special case — the BDD for a netmask that's out of range never 62 | * evaluates to true. */ 63 | if (cidr_prefix > IP_BIT_SIZE) { 64 | cork_error_set 65 | (IPSET_ERROR, IPSET_PARSE_ERROR, 66 | "CIDR block %u out of range [0..%u]", cidr_prefix, IP_BIT_SIZE); 67 | return false; 68 | } 69 | 70 | ipset_node_id new_bdd = 71 | ipset_node_insert 72 | (set->cache, set->set_bdd, 73 | IPSET_NAME(assignment), elem, cidr_prefix + 1, 1); 74 | bool result = (new_bdd == set->set_bdd); 75 | ipset_node_decref(set->cache, set->set_bdd); 76 | set->set_bdd = new_bdd; 77 | return result; 78 | } 79 | 80 | 81 | bool 82 | IPSET_NAME(add)(struct ip_set *set, CORK_IP *elem) 83 | { 84 | ipset_node_id new_bdd = 85 | ipset_node_insert 86 | (set->cache, set->set_bdd, 87 | IPSET_NAME(assignment), elem, IP_BIT_SIZE + 1, 1); 88 | bool result = (new_bdd == set->set_bdd); 89 | ipset_node_decref(set->cache, set->set_bdd); 90 | set->set_bdd = new_bdd; 91 | return result; 92 | } 93 | 94 | 95 | bool 96 | IPSET_NAME(remove)(struct ip_set *set, CORK_IP *elem) 97 | { 98 | ipset_node_id new_bdd = 99 | ipset_node_insert 100 | (set->cache, set->set_bdd, 101 | IPSET_NAME(assignment), elem, IP_BIT_SIZE + 1, 0); 102 | bool result = (new_bdd == set->set_bdd); 103 | ipset_node_decref(set->cache, set->set_bdd); 104 | set->set_bdd = new_bdd; 105 | return result; 106 | } 107 | 108 | 109 | bool 110 | IPSET_NAME(remove_network)(struct ip_set *set, CORK_IP *elem, 111 | unsigned int cidr_prefix) 112 | { 113 | /* Special case — the BDD for a netmask that's out of range never 114 | * evaluates to true. */ 115 | if (cidr_prefix > IP_BIT_SIZE) { 116 | cork_error_set 117 | (IPSET_ERROR, IPSET_PARSE_ERROR, 118 | "CIDR block %u out of range [0..%u]", cidr_prefix, IP_BIT_SIZE); 119 | return false; 120 | } 121 | 122 | ipset_node_id new_bdd = 123 | ipset_node_insert 124 | (set->cache, set->set_bdd, 125 | IPSET_NAME(assignment), elem, cidr_prefix + 1, 0); 126 | bool result = (new_bdd == set->set_bdd); 127 | ipset_node_decref(set->cache, set->set_bdd); 128 | set->set_bdd = new_bdd; 129 | return result; 130 | } 131 | -------------------------------------------------------------------------------- /3rd/libcork/src/posix/process.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2013-2014, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license details. 7 | * ---------------------------------------------------------------------- 8 | */ 9 | 10 | #include 11 | 12 | #include "libcork/core.h" 13 | #include "libcork/ds.h" 14 | #include "libcork/os/process.h" 15 | #include "libcork/helpers/errors.h" 16 | 17 | 18 | #if !defined(CORK_DEBUG_PROCESS) 19 | #define CORK_DEBUG_PROCESS 0 20 | #endif 21 | 22 | #if CORK_DEBUG_PROCESS 23 | #include 24 | #define DEBUG(...) fprintf(stderr, __VA_ARGS__) 25 | #else 26 | #define DEBUG(...) /* no debug messages */ 27 | #endif 28 | 29 | 30 | struct cork_cleanup_entry { 31 | struct cork_dllist_item item; 32 | int priority; 33 | const char *name; 34 | cork_cleanup_function function; 35 | }; 36 | 37 | static struct cork_cleanup_entry * 38 | cork_cleanup_entry_new(const char *name, int priority, 39 | cork_cleanup_function function) 40 | { 41 | struct cork_cleanup_entry *self = cork_new(struct cork_cleanup_entry); 42 | self->priority = priority; 43 | self->name = cork_strdup(name); 44 | self->function = function; 45 | return self; 46 | } 47 | 48 | static void 49 | cork_cleanup_entry_free(struct cork_cleanup_entry *self) 50 | { 51 | cork_strfree(self->name); 52 | cork_delete(struct cork_cleanup_entry, self); 53 | } 54 | 55 | static struct cork_dllist cleanup_entries = CORK_DLLIST_INIT(cleanup_entries); 56 | static bool cleanup_registered = false; 57 | 58 | static void 59 | cork_cleanup_call_one(struct cork_dllist_item *item, void *user_data) 60 | { 61 | struct cork_cleanup_entry *entry = 62 | cork_container_of(item, struct cork_cleanup_entry, item); 63 | cork_cleanup_function function = entry->function; 64 | DEBUG("Call cleanup function [%d] %s\n", entry->priority, entry->name); 65 | /* We need to free the entry before calling the entry's function, since one 66 | * of the functions that libcork registers frees the allocator instance that 67 | * we'd use to free the entry. If we called the function first, the 68 | * allocator would be freed before we could use it to free the entry. */ 69 | cork_cleanup_entry_free(entry); 70 | function(); 71 | } 72 | 73 | static void 74 | cork_cleanup_call_all(void) 75 | { 76 | cork_dllist_map(&cleanup_entries, cork_cleanup_call_one, NULL); 77 | } 78 | 79 | static void 80 | cork_cleanup_entry_add(struct cork_cleanup_entry *entry) 81 | { 82 | struct cork_dllist_item *curr; 83 | 84 | if (CORK_UNLIKELY(!cleanup_registered)) { 85 | atexit(cork_cleanup_call_all); 86 | cleanup_registered = true; 87 | } 88 | 89 | /* Linear search through the list of existing cleanup functions. When we 90 | * find the first existing function with a higher priority, we've found 91 | * where to insert the new function. */ 92 | for (curr = cork_dllist_start(&cleanup_entries); 93 | !cork_dllist_is_end(&cleanup_entries, curr); curr = curr->next) { 94 | struct cork_cleanup_entry *existing = 95 | cork_container_of(curr, struct cork_cleanup_entry, item); 96 | if (existing->priority > entry->priority) { 97 | cork_dllist_add_before(&existing->item, &entry->item); 98 | return; 99 | } 100 | } 101 | 102 | /* If we fall through the loop, then the new function should be appended to 103 | * the end of the list. */ 104 | cork_dllist_add(&cleanup_entries, &entry->item); 105 | } 106 | 107 | 108 | CORK_API void 109 | cork_cleanup_at_exit_named(const char *name, int priority, 110 | cork_cleanup_function function) 111 | { 112 | struct cork_cleanup_entry *entry = 113 | cork_cleanup_entry_new(name, priority, function); 114 | DEBUG("Register cleanup function [%d] %s\n", priority, name); 115 | cork_cleanup_entry_add(entry); 116 | } 117 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/core/error.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2011-2013, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license details. 7 | * ---------------------------------------------------------------------- 8 | */ 9 | 10 | #ifndef LIBCORK_CORE_ERROR_H 11 | #define LIBCORK_CORE_ERROR_H 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | 23 | /* Should be a hash of a string representing the error code. */ 24 | typedef uint32_t cork_error; 25 | 26 | /* An error code that represents “no error”. */ 27 | #define CORK_ERROR_NONE ((cork_error) 0) 28 | 29 | CORK_API bool 30 | cork_error_occurred(void); 31 | 32 | CORK_API cork_error 33 | cork_error_code(void); 34 | 35 | CORK_API const char * 36 | cork_error_message(void); 37 | 38 | 39 | CORK_API void 40 | cork_error_clear(void); 41 | 42 | CORK_API void 43 | cork_error_set_printf(cork_error code, const char *format, ...) 44 | CORK_ATTR_PRINTF(2,3); 45 | 46 | CORK_API void 47 | cork_error_set_string(cork_error code, const char *str); 48 | 49 | CORK_API void 50 | cork_error_set_vprintf(cork_error code, const char *format, va_list args) 51 | CORK_ATTR_PRINTF(2,0); 52 | 53 | CORK_API void 54 | cork_error_prefix_printf(const char *format, ...) 55 | CORK_ATTR_PRINTF(1,2); 56 | 57 | CORK_API void 58 | cork_error_prefix_string(const char *str); 59 | 60 | CORK_API void 61 | cork_error_prefix_vprintf(const char *format, va_list arg) 62 | CORK_ATTR_PRINTF(1,0); 63 | 64 | 65 | /* deprecated */ 66 | CORK_API void 67 | cork_error_set(uint32_t error_class, unsigned int error_code, 68 | const char *format, ...) 69 | CORK_ATTR_PRINTF(3,4); 70 | 71 | /* deprecated */ 72 | CORK_API void 73 | cork_error_prefix(const char *format, ...) 74 | CORK_ATTR_PRINTF(1,2); 75 | 76 | 77 | /*----------------------------------------------------------------------- 78 | * Built-in errors 79 | */ 80 | 81 | #define CORK_PARSE_ERROR 0x95dfd3c8 82 | #define CORK_REDEFINED 0x171629cb 83 | #define CORK_UNDEFINED 0xedc3d7d9 84 | #define CORK_UNKNOWN_ERROR 0x8cb0880d 85 | 86 | #define cork_parse_error(...) \ 87 | cork_error_set_printf(CORK_PARSE_ERROR, __VA_ARGS__) 88 | #define cork_redefined(...) \ 89 | cork_error_set_printf(CORK_REDEFINED, __VA_ARGS__) 90 | #define cork_undefined(...) \ 91 | cork_error_set_printf(CORK_UNDEFINED, __VA_ARGS__) 92 | 93 | CORK_API void 94 | cork_system_error_set(void); 95 | 96 | CORK_API void 97 | cork_system_error_set_explicit(int err); 98 | 99 | CORK_API void 100 | cork_unknown_error_set_(const char *location); 101 | 102 | #define cork_unknown_error() \ 103 | cork_unknown_error_set_(__func__) 104 | 105 | 106 | /*----------------------------------------------------------------------- 107 | * Abort on failure 108 | */ 109 | 110 | #define cork_abort_(func, file, line, fmt, ...) \ 111 | do { \ 112 | fprintf(stderr, fmt "\n in %s (%s:%u)\n", \ 113 | __VA_ARGS__, (func), (file), (unsigned int) (line)); \ 114 | abort(); \ 115 | } while (0) 116 | 117 | #define cork_abort(fmt, ...) \ 118 | cork_abort_(__func__, __FILE__, __LINE__, fmt, __VA_ARGS__) 119 | 120 | CORK_ATTR_UNUSED 121 | static void * 122 | cork_abort_if_null_(void *ptr, const char *msg, const char *func, 123 | const char *file, unsigned int line) 124 | { 125 | if (CORK_UNLIKELY(ptr == NULL)) { 126 | cork_abort_(func, file, line, "%s", msg); 127 | } else { 128 | return ptr; 129 | } 130 | } 131 | 132 | #define cork_abort_if_null(ptr, msg) \ 133 | (cork_abort_if_null_(ptr, msg, __func__, __FILE__, __LINE__)) 134 | 135 | #define cork_unreachable() \ 136 | cork_abort("%s", "Code should not be reachable") 137 | 138 | 139 | #endif /* LIBCORK_CORE_ERROR_H */ 140 | -------------------------------------------------------------------------------- /src/xForwarder_client.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "uv.h" 7 | 8 | #include "util.h" 9 | #include "logger.h" 10 | #include "crypto.h" 11 | #include "socks.h" 12 | #include "xForwarder.h" 13 | 14 | 15 | static void client_alloc_cb(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf); 16 | static void client_send_cb(uv_write_t *req, int status); 17 | static void client_recv_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf); 18 | 19 | 20 | struct client_context * 21 | new_client() { 22 | struct client_context *client = malloc(sizeof(*client)); 23 | memset(client, 0, sizeof(*client)); 24 | client->stage = XSTAGE_REQUEST; 25 | return client; 26 | } 27 | 28 | static void 29 | free_client(struct client_context *client) { 30 | if (client->remote != NULL) { 31 | client->remote = NULL; 32 | } 33 | free(client); 34 | } 35 | 36 | static void 37 | client_close_cb(uv_handle_t *handle) { 38 | struct client_context *client = (struct client_context *)handle->data; 39 | free_client(client); 40 | } 41 | 42 | void 43 | close_client(struct client_context *client) { 44 | client->stage = XSTAGE_DEAD; 45 | client->handle.handle.data = client; 46 | uv_close(&client->handle.handle, client_close_cb); 47 | } 48 | 49 | void 50 | receive_from_client(struct client_context *client) { 51 | client->handle.stream.data = client; 52 | uv_read_start(&client->handle.stream, client_alloc_cb, client_recv_cb); 53 | } 54 | 55 | void 56 | forward_to_client(struct client_context *client, uint8_t *buf, int buflen) { 57 | uv_buf_t reply = uv_buf_init((char*)buf, buflen); 58 | client->write_req.data = client; 59 | uv_write(&client->write_req, &client->handle.stream, &reply, 1, client_send_cb); 60 | } 61 | 62 | static void 63 | client_alloc_cb(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) { 64 | struct client_context *client = handle->data; 65 | buf->base = (char*)(client->buf + OVERHEAD_BYTES); 66 | buf->len = sizeof(client->buf) - OVERHEAD_BYTES; 67 | } 68 | 69 | static void 70 | client_send_cb(uv_write_t *req, int status) { 71 | struct client_context *client = req->data; 72 | struct remote_context *remote = client->remote; 73 | 74 | if (status == 0) { 75 | if (client->stage == XSTAGE_FORWARD) { 76 | receive_from_remote(remote); 77 | } 78 | 79 | } else { 80 | if (verbose) { 81 | logger_log(LOG_ERR, "forward to client failed: %s", uv_strerror(status)); 82 | } 83 | } 84 | } 85 | 86 | static void 87 | client_recv_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) { 88 | struct client_context *client = stream->data; 89 | struct remote_context *remote = client->remote; 90 | 91 | if (nread > 0) { 92 | uv_read_stop(&client->handle.stream); 93 | int clen = nread + PRIMITIVE_BYTES; 94 | uint8_t *c = client->buf + HEADER_BYTES; 95 | int rc = crypto_encrypt(c, (uint8_t*)buf->base, nread); 96 | if (rc) { 97 | logger_log(LOG_ERR, "invalid packet"); 98 | close_client(client); 99 | close_remote(remote); 100 | } else { 101 | forward_to_remote(remote, c, clen); 102 | } 103 | 104 | } else if (nread < 0) { 105 | if (nread != UV_EOF) { 106 | logger_log(LOG_ERR, "receive from server failed: %s", uv_strerror(nread)); 107 | } 108 | close_client(client); 109 | close_remote(remote); 110 | } 111 | } 112 | 113 | void 114 | client_accept_cb(uv_stream_t *server, int status) { 115 | struct client_context *client = new_client(); 116 | struct remote_context *remote = new_remote(idle_timeout); 117 | 118 | client->remote = remote; 119 | remote->client = client; 120 | 121 | uv_timer_init(server->loop, remote->timer); 122 | 123 | uv_tcp_init(server->loop, &client->handle.tcp); 124 | uv_tcp_init(server->loop, &remote->handle.tcp); 125 | 126 | int rc = uv_accept(server, &client->handle.stream); 127 | 128 | if (rc == 0) { 129 | reset_timer(remote); 130 | connect_to_remote(remote); 131 | } else { 132 | logger_log(LOG_ERR, "accept error: %s", uv_strerror(rc)); 133 | close_client(client); 134 | close_remote(remote); 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/helpers/errors.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2011-2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #ifndef LIBCORK_HELPERS_ERRORS_H 12 | #define LIBCORK_HELPERS_ERRORS_H 13 | 14 | 15 | /* This header is *not* automatically included when you include 16 | * libcork/core.h, since we define some macros that don't include a 17 | * cork_ or CORK_ prefix. Don't want to pollute your namespace unless 18 | * you ask for it! */ 19 | 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | 26 | #if !defined(CORK_PRINT_ERRORS) 27 | #define CORK_PRINT_ERRORS 0 28 | #endif 29 | 30 | #if !defined(CORK_PRINT_ERROR) 31 | #if CORK_PRINT_ERRORS 32 | #include 33 | #define CORK_PRINT_ERROR_(func, file, line) \ 34 | fprintf(stderr, "---\nError in %s (%s:%u)\n %s\n", \ 35 | (func), (file), (unsigned int) (line), \ 36 | cork_error_message()); 37 | #define CORK_PRINT_ERROR() CORK_PRINT_ERROR_(__func__, __FILE__, __LINE__) 38 | #else 39 | #define CORK_PRINT_ERROR() /* do nothing */ 40 | #endif 41 | #endif 42 | 43 | 44 | /* A bunch of macros for calling a function that returns an error. If 45 | * an error occurs, it will automatically be propagated out as the 46 | * result of your own function. With these macros, you won't have a 47 | * check to check or modify the error condition; it's returned as-is. 48 | * 49 | * XZ_check 50 | * 51 | * where: 52 | * 53 | * X = what happens if an error occurs 54 | * "e" = jump to the "error" label 55 | * "rY" = return a default error result (Y defined below) 56 | * "x" = return an error result that you specify 57 | * 58 | * Y = your return type 59 | * "i" = int 60 | * "p" = some pointer type 61 | * 62 | * Z = the return type of the function you're calling 63 | * "e" = use cork_error_occurred() to check 64 | * "i" = int 65 | * "p" = some pointer type 66 | * 67 | * In all cases, we assume that your function has a cork_error parameter 68 | * called "err". 69 | */ 70 | 71 | 72 | /* jump to "error" label */ 73 | 74 | #define ee_check(call) \ 75 | do { \ 76 | (call); \ 77 | if (CORK_UNLIKELY(cork_error_occurred())) { \ 78 | CORK_PRINT_ERROR(); \ 79 | goto error; \ 80 | } \ 81 | } while (0) 82 | 83 | #define ei_check(call) \ 84 | do { \ 85 | int __rc = (call); \ 86 | if (CORK_UNLIKELY(__rc != 0)) { \ 87 | CORK_PRINT_ERROR(); \ 88 | goto error; \ 89 | } \ 90 | } while (0) 91 | 92 | #define ep_check(call) \ 93 | do { \ 94 | const void *__result = (call); \ 95 | if (CORK_UNLIKELY(__result == NULL)) { \ 96 | CORK_PRINT_ERROR(); \ 97 | goto error; \ 98 | } \ 99 | } while (0) 100 | 101 | 102 | /* return specific error code */ 103 | 104 | #define xe_check(result, call) \ 105 | do { \ 106 | (call); \ 107 | if (CORK_UNLIKELY(cork_error_occurred())) { \ 108 | CORK_PRINT_ERROR(); \ 109 | return result; \ 110 | } \ 111 | } while (0) 112 | 113 | #define xi_check(result, call) \ 114 | do { \ 115 | int __rc = (call); \ 116 | if (CORK_UNLIKELY(__rc != 0)) { \ 117 | CORK_PRINT_ERROR(); \ 118 | return result; \ 119 | } \ 120 | } while (0) 121 | 122 | #define xp_check(result, call) \ 123 | do { \ 124 | const void *__result = (call); \ 125 | if (CORK_UNLIKELY(__result == NULL)) { \ 126 | CORK_PRINT_ERROR(); \ 127 | return result; \ 128 | } \ 129 | } while (0) 130 | 131 | 132 | /* return default error code */ 133 | 134 | #define rie_check(call) xe_check(-1, call) 135 | #define rii_check(call) xi_check(__rc, call) 136 | #define rip_check(call) xp_check(-1, call) 137 | #define rpe_check(call) xe_check(NULL, call) 138 | #define rpi_check(call) xi_check(NULL, call) 139 | #define rpp_check(call) xp_check(NULL, call) 140 | 141 | 142 | #endif /* LIBCORK_HELPERS_ERRORS_H */ 143 | -------------------------------------------------------------------------------- /3rd/libipset/src/bdd/assignments.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2010-2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the LICENSE.txt file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #include 12 | 13 | #include "ipset/bdd/nodes.h" 14 | 15 | 16 | struct ipset_assignment * 17 | ipset_assignment_new() 18 | { 19 | struct ipset_assignment *assignment = cork_new(struct ipset_assignment); 20 | cork_array_init(&assignment->values); 21 | return assignment; 22 | } 23 | 24 | 25 | void 26 | ipset_assignment_free(struct ipset_assignment *assignment) 27 | { 28 | cork_array_done(&assignment->values); 29 | free(assignment); 30 | } 31 | 32 | 33 | bool 34 | ipset_assignment_equal(const struct ipset_assignment *assignment1, 35 | const struct ipset_assignment *assignment2) 36 | { 37 | /* Identical pointers are trivially equal. */ 38 | if (assignment1 == assignment2) { 39 | return true; 40 | } 41 | 42 | /* Otherwise we compare the assignments piecewise up through the end 43 | * of the smaller vector. */ 44 | unsigned int size1 = cork_array_size(&assignment1->values); 45 | unsigned int size2 = cork_array_size(&assignment2->values); 46 | unsigned int smaller_size = (size1 < size2)? size1: size2; 47 | 48 | unsigned int i; 49 | for (i = 0; i < smaller_size; i++) { 50 | if (cork_array_at(&assignment1->values, i) != 51 | cork_array_at(&assignment2->values, i)) { 52 | return false; 53 | } 54 | } 55 | 56 | /* If one of the assignment vectors is longer, any remaining 57 | * elements must be indeterminate. */ 58 | if (size1 > smaller_size) { 59 | for (i = smaller_size; i < size1; i++) { 60 | if (cork_array_at(&assignment1->values, i) != IPSET_EITHER) { 61 | return false; 62 | } 63 | } 64 | } 65 | 66 | if (size2 > smaller_size) { 67 | for (i = smaller_size; i < size2; i++) { 68 | if (cork_array_at(&assignment2->values, i) != IPSET_EITHER) { 69 | return false; 70 | } 71 | } 72 | } 73 | 74 | /* If we make it through all of that, the two assignments are equal. */ 75 | return true; 76 | } 77 | 78 | 79 | void 80 | ipset_assignment_cut(struct ipset_assignment *assignment, 81 | ipset_variable var) 82 | { 83 | if (var < cork_array_size(&assignment->values)) { 84 | assignment->values.size = var; 85 | } 86 | } 87 | 88 | 89 | void 90 | ipset_assignment_clear(struct ipset_assignment *assignment) 91 | { 92 | ipset_assignment_cut(assignment, 0); 93 | } 94 | 95 | 96 | enum ipset_tribool 97 | ipset_assignment_get(struct ipset_assignment *assignment, ipset_variable var) 98 | { 99 | if (var < cork_array_size(&assignment->values)) { 100 | /* If the requested variable is in the range of the values 101 | * array, return whatever is stored there. */ 102 | return cork_array_at(&assignment->values, var); 103 | } else { 104 | /* Variables htat aren't in the values array are always EITHER. */ 105 | return IPSET_EITHER; 106 | } 107 | } 108 | 109 | 110 | void 111 | ipset_assignment_set(struct ipset_assignment *assignment, 112 | ipset_variable var, enum ipset_tribool value) 113 | { 114 | /* Ensure that the vector is big enough to hold this variable 115 | * assignment, inserting new EITHERs if needed. */ 116 | if (var >= cork_array_size(&assignment->values)) { 117 | unsigned int old_len = cork_array_size(&assignment->values); 118 | 119 | /* Expand the array. */ 120 | cork_array_ensure_size(&assignment->values, var+1); 121 | assignment->values.size = var+1; 122 | 123 | /* Fill in EITHERs in the newly allocated elements. */ 124 | if (var != old_len) { 125 | unsigned int i; 126 | for (i = old_len; i < var; i++) { 127 | cork_array_at(&assignment->values, i) = IPSET_EITHER; 128 | } 129 | } 130 | } 131 | 132 | /* Assign the desired value. */ 133 | cork_array_at(&assignment->values, var) = value; 134 | } 135 | -------------------------------------------------------------------------------- /3rd/libcork/src/posix/directory-walker.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include "libcork/core/attributes.h" 20 | #include "libcork/core/error.h" 21 | #include "libcork/core/types.h" 22 | #include "libcork/ds/buffer.h" 23 | #include "libcork/helpers/errors.h" 24 | #include "libcork/helpers/posix.h" 25 | #include "libcork/os/files.h" 26 | 27 | 28 | static int 29 | cork_walk_one_directory(struct cork_dir_walker *w, struct cork_buffer *path, 30 | size_t root_path_size) 31 | { 32 | DIR *dir = NULL; 33 | struct dirent *entry; 34 | size_t dir_path_size; 35 | 36 | rip_check_posix(dir = opendir(path->buf)); 37 | 38 | cork_buffer_append(path, "/", 1); 39 | dir_path_size = path->size; 40 | errno = 0; 41 | while ((entry = readdir(dir)) != NULL) { 42 | struct stat info; 43 | 44 | /* Skip the "." and ".." entries */ 45 | if (strcmp(entry->d_name, ".") == 0 || 46 | strcmp(entry->d_name, "..") == 0) { 47 | continue; 48 | } 49 | 50 | /* Stat the directory entry */ 51 | cork_buffer_append_string(path, entry->d_name); 52 | ei_check_posix(stat(path->buf, &info)); 53 | 54 | /* If the entry is a subdirectory, recurse into it. */ 55 | if (S_ISDIR(info.st_mode)) { 56 | int rc = cork_dir_walker_enter_directory 57 | (w, path->buf, path->buf + root_path_size, 58 | path->buf + dir_path_size); 59 | if (rc != CORK_SKIP_DIRECTORY) { 60 | ei_check(cork_walk_one_directory(w, path, root_path_size)); 61 | ei_check(cork_dir_walker_leave_directory 62 | (w, path->buf, path->buf + root_path_size, 63 | path->buf + dir_path_size)); 64 | } 65 | } else if (S_ISREG(info.st_mode)) { 66 | ei_check(cork_dir_walker_file 67 | (w, path->buf, path->buf + root_path_size, 68 | path->buf + dir_path_size)); 69 | } 70 | 71 | /* Remove this entry name from the path buffer. */ 72 | cork_buffer_truncate(path, dir_path_size); 73 | 74 | /* We have to reset errno to 0 because of the ambiguous way 75 | * readdir uses a return value of NULL. Other functions may 76 | * return normally yet set errno to a non-zero value. dlopen 77 | * on Mac OS X is an ogreish example. Since an error readdir 78 | * is indicated by returning NULL and setting errno to indicate 79 | * the error, then we need to reset it to zero before each call. 80 | * We shall assume, perhaps to our great misery, that functions 81 | * within this loop do proper error checking and act accordingly. 82 | */ 83 | errno = 0; 84 | } 85 | 86 | /* Check errno immediately after the while loop terminates */ 87 | if (CORK_UNLIKELY(errno != 0)) { 88 | cork_system_error_set(); 89 | goto error; 90 | } 91 | 92 | /* Remove the trailing '/' from the path buffer. */ 93 | cork_buffer_truncate(path, dir_path_size - 1); 94 | rii_check_posix(closedir(dir)); 95 | return 0; 96 | 97 | error: 98 | if (dir != NULL) { 99 | rii_check_posix(closedir(dir)); 100 | } 101 | return -1; 102 | } 103 | 104 | int 105 | cork_walk_directory(const char *path, struct cork_dir_walker *w) 106 | { 107 | int rc; 108 | char *p; 109 | struct cork_buffer buf = CORK_BUFFER_INIT(); 110 | 111 | /* Seed the buffer with the directory's path, ensuring that there's no 112 | * trailing '/' */ 113 | cork_buffer_append_string(&buf, path); 114 | p = buf.buf; 115 | while (p[buf.size-1] == '/') { 116 | buf.size--; 117 | p[buf.size] = '\0'; 118 | } 119 | rc = cork_walk_one_directory(w, &buf, buf.size + 1); 120 | cork_buffer_done(&buf); 121 | return rc; 122 | } 123 | -------------------------------------------------------------------------------- /3rd/libcork/include/libcork/core/net-addresses.h: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2011-2013, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the COPYING file in this distribution for license details. 7 | * ---------------------------------------------------------------------- 8 | */ 9 | 10 | #ifndef LIBCORK_CORE_NET_ADDRESSES_H 11 | #define LIBCORK_CORE_NET_ADDRESSES_H 12 | 13 | 14 | #include 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | 21 | /*----------------------------------------------------------------------- 22 | * IP addresses 23 | */ 24 | 25 | struct cork_ipv4 { 26 | union { 27 | uint8_t u8[4]; 28 | uint16_t u16[2]; 29 | uint32_t u32; 30 | } _; 31 | }; 32 | 33 | struct cork_ipv6 { 34 | union { 35 | uint8_t u8[16]; 36 | uint16_t u16[8]; 37 | uint32_t u32[4]; 38 | uint64_t u64[2]; 39 | } _; 40 | }; 41 | 42 | struct cork_ip { 43 | /* Which version of IP address this is. */ 44 | unsigned int version; 45 | union { 46 | struct cork_ipv4 v4; 47 | struct cork_ipv6 v6; 48 | } ip; 49 | }; 50 | 51 | 52 | #define CORK_IPV4_STRING_LENGTH (sizeof "xxx.xxx.xxx.xxx") 53 | #define CORK_IPV6_STRING_LENGTH \ 54 | (sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") 55 | #define CORK_IP_STRING_LENGTH CORK_IPV6_STRING_LENGTH 56 | 57 | 58 | /*** IPv4 ***/ 59 | 60 | /* src must be well-formed: 4 bytes, big-endian */ 61 | #define cork_ipv4_copy(addr, src) \ 62 | (memcpy((addr), (src), sizeof(struct cork_ipv4))) 63 | 64 | #define cork_ipv4_equal(a1, a2) \ 65 | ((a1)->_.u32 == (a2)->_.u32) 66 | 67 | CORK_API int 68 | cork_ipv4_init(struct cork_ipv4 *addr, const char *str); 69 | 70 | CORK_API bool 71 | cork_ipv4_equal_(const struct cork_ipv4 *addr1, const struct cork_ipv4 *addr2); 72 | 73 | CORK_API void 74 | cork_ipv4_to_raw_string(const struct cork_ipv4 *addr, char *dest); 75 | 76 | CORK_API bool 77 | cork_ipv4_is_valid_network(const struct cork_ipv4 *addr, 78 | unsigned int cidr_prefix); 79 | 80 | 81 | /*** IPv6 ***/ 82 | 83 | /* src must be well-formed: 16 bytes, big-endian */ 84 | #define cork_ipv6_copy(addr, src) \ 85 | (memcpy((addr), (src), sizeof(struct cork_ipv6))) 86 | 87 | #define cork_ipv6_equal(a1, a2) \ 88 | ((a1)->_.u64[0] == (a2)->_.u64[0] && \ 89 | (a1)->_.u64[1] == (a2)->_.u64[1]) 90 | 91 | CORK_API int 92 | cork_ipv6_init(struct cork_ipv6 *addr, const char *str); 93 | 94 | CORK_API bool 95 | cork_ipv6_equal_(const struct cork_ipv6 *addr1, const struct cork_ipv6 *addr2); 96 | 97 | CORK_API void 98 | cork_ipv6_to_raw_string(const struct cork_ipv6 *addr, char *dest); 99 | 100 | CORK_API bool 101 | cork_ipv6_is_valid_network(const struct cork_ipv6 *addr, 102 | unsigned int cidr_prefix); 103 | 104 | 105 | /*** Generic IP ***/ 106 | 107 | #define cork_ip_equal(a1, a2) \ 108 | ((a1)->version == 4? \ 109 | ((a2)->version == 4 && cork_ipv4_equal(&(a1)->ip.v4, &(a2)->ip.v4)): \ 110 | ((a2)->version == 6 && cork_ipv6_equal(&(a1)->ip.v6, &(a2)->ip.v6))) 111 | 112 | /* src must be well-formed: 4 bytes, big-endian */ 113 | #define cork_ip_from_ipv4(addr, src) \ 114 | do { \ 115 | (addr)->version = 4; \ 116 | cork_ipv4_copy(&(addr)->ip.v4, (src)); \ 117 | } while (0) 118 | 119 | /* src must be well-formed: 16 bytes, big-endian */ 120 | #define cork_ip_from_ipv6(addr, src) \ 121 | do { \ 122 | (addr)->version = 6; \ 123 | cork_ipv6_copy(&(addr)->ip.v6, (src)); \ 124 | } while (0) 125 | 126 | /* src must be well-formed: 4 bytes, big-endian */ 127 | CORK_API void 128 | cork_ip_from_ipv4_(struct cork_ip *addr, const void *src); 129 | 130 | /* src must be well-formed: 16 bytes, big-endian */ 131 | CORK_API void 132 | cork_ip_from_ipv6_(struct cork_ip *addr, const void *src); 133 | 134 | CORK_API int 135 | cork_ip_init(struct cork_ip *addr, const char *str); 136 | 137 | CORK_API bool 138 | cork_ip_equal_(const struct cork_ip *addr1, const struct cork_ip *addr2); 139 | 140 | CORK_API void 141 | cork_ip_to_raw_string(const struct cork_ip *addr, char *dest); 142 | 143 | CORK_API bool 144 | cork_ip_is_valid_network(const struct cork_ip *addr, unsigned int cidr_prefix); 145 | 146 | 147 | #endif /* LIBCORK_CORE_NET_ADDRESSES_H */ 148 | -------------------------------------------------------------------------------- /src/cache.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | /* 3 | * Original Author: Oliver Lorenz (ol), olli@olorenz.org, https://olorenz.org 4 | * License: This is licensed under the same terms as uthash itself 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include "cache.h" 11 | 12 | int 13 | cache_create(struct cache **dst, const size_t capacity, 14 | void (*free_cb)(void *element)) { 15 | struct cache *new = NULL; 16 | 17 | if (!dst) { 18 | return EINVAL; 19 | } 20 | 21 | if ((new = malloc(sizeof(*new))) == NULL) { 22 | return ENOMEM; 23 | } 24 | 25 | new->max_entries = capacity; 26 | new->entries = NULL; 27 | new->free_cb = free_cb; 28 | *dst = new; 29 | return 0; 30 | 31 | } 32 | 33 | int 34 | cache_delete(struct cache *cache, int keep_data) { 35 | struct cache_entry *entry, *tmp; 36 | 37 | if (!cache) { 38 | return EINVAL; 39 | } 40 | 41 | if (keep_data) { 42 | HASH_CLEAR(hh, cache->entries); 43 | } else { 44 | HASH_ITER(hh, cache->entries, entry, tmp){ 45 | HASH_DEL(cache->entries, entry); 46 | if (cache->free_cb) { 47 | cache->free_cb(entry->data); 48 | } 49 | free(entry); 50 | } 51 | } 52 | 53 | free(cache); 54 | cache = NULL; 55 | return 0; 56 | } 57 | 58 | int 59 | cache_remove(struct cache *cache, char *key) { 60 | struct cache_entry *tmp; 61 | 62 | if (!cache || !key) { 63 | return EINVAL; 64 | } 65 | 66 | HASH_FIND_STR(cache->entries, key, tmp); 67 | 68 | if (tmp) { 69 | HASH_DEL(cache->entries, tmp); 70 | if (cache->free_cb) { 71 | cache->free_cb(tmp->data); 72 | } 73 | free(tmp); 74 | } 75 | 76 | return 0; 77 | } 78 | 79 | int 80 | cache_removeall(struct cache *cache, void *opaque, int (*select_cb)(void *element, void *opaque)) { 81 | struct cache_entry *entry, *tmp; 82 | 83 | if (!cache) { 84 | return EINVAL; 85 | } 86 | 87 | HASH_ITER(hh, cache->entries, entry, tmp){ 88 | if (select_cb(entry->data, opaque)) { 89 | HASH_DEL(cache->entries, entry); 90 | if (cache->free_cb) { 91 | cache->free_cb(entry->data); 92 | } 93 | free(entry); 94 | } 95 | } 96 | 97 | return 0; 98 | } 99 | 100 | /** Checks if a given key is in the cache 101 | 102 | @param cache 103 | The cache object 104 | 105 | @param key 106 | The key to look-up 107 | 108 | @param result 109 | Where to store the result if key is found. 110 | 111 | A warning: Even though result is just a pointer, 112 | you have to call this function with a **ptr, 113 | otherwise this will blow up in your face. 114 | 115 | @return EINVAL if cache is NULL, 0 otherwise 116 | */ 117 | int 118 | cache_lookup(struct cache *cache, char *key, void *result) 119 | { 120 | struct cache_entry *tmp = NULL; 121 | char **dirty_hack = result; 122 | 123 | if (!cache || !key || !result) { 124 | return EINVAL; 125 | } 126 | 127 | HASH_FIND_STR(cache->entries, key, tmp); 128 | if (tmp) { 129 | size_t key_len = strnlen(tmp->key, KEY_MAX_LENGTH); 130 | HASH_DELETE(hh, cache->entries, tmp); 131 | HASH_ADD_KEYPTR(hh, cache->entries, tmp->key, key_len, tmp); 132 | *dirty_hack = tmp->data; 133 | } else { 134 | *dirty_hack = result = NULL; 135 | } 136 | 137 | return 0; 138 | } 139 | 140 | int 141 | cache_insert(struct cache *cache, char *key, void *data) { 142 | struct cache_entry *entry = NULL; 143 | struct cache_entry *tmp_entry = NULL; 144 | size_t key_len = 0; 145 | 146 | if (!cache || !data) { 147 | return EINVAL; 148 | } 149 | 150 | if ((entry = malloc(sizeof(*entry))) == NULL) { 151 | return ENOMEM; 152 | } 153 | 154 | entry->key = key; 155 | entry->data = data; 156 | key_len = strnlen(entry->key, KEY_MAX_LENGTH); 157 | HASH_ADD_KEYPTR(hh, cache->entries, entry->key, key_len, entry); 158 | 159 | if (HASH_COUNT(cache->entries) >= cache->max_entries) { 160 | HASH_ITER(hh, cache->entries, entry, tmp_entry){ 161 | HASH_DELETE(hh, cache->entries, entry); 162 | if (cache->free_cb) { 163 | cache->free_cb(entry->data); 164 | } else { 165 | free(entry->data); 166 | } 167 | /* free(key->key) if data has been copied */ 168 | free(entry); 169 | break; 170 | } 171 | } 172 | 173 | return 0; 174 | } 175 | -------------------------------------------------------------------------------- /3rd/libancillary/ancillary.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************** 2 | * libancillary - black magic on Unix domain sockets 3 | * (C) Nicolas George 4 | * ancillary.c - public header 5 | ***************************************************************************/ 6 | 7 | /* 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 3. The name of the author may not be used to endorse or promote products 17 | * derived from this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 20 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 22 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 24 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 25 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 26 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 27 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 28 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | #ifndef ANCILLARY_H__ 32 | #define ANCILLARY_H__ 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | /*************************************************************************** 39 | * Start of the readable part. 40 | ***************************************************************************/ 41 | 42 | #define ANCIL_MAX_N_FDS 960 43 | /* 44 | * Maximum number of fds that can be sent or received using the "esay" 45 | * functions; this is so that all can fit in one page. 46 | */ 47 | 48 | extern int 49 | ancil_send_fds_with_buffer(int, const int *, unsigned, void *); 50 | /* 51 | * ancil_send_fds_with_buffer(sock, n_fds, fds, buffer) 52 | * 53 | * Sends the file descriptors in the array pointed by fds, of length n_fds 54 | * on the socket sock. 55 | * buffer is a writeable memory area large enough to hold the required data 56 | * structures. 57 | * Returns: -1 and errno in case of error, 0 in case of success. 58 | */ 59 | 60 | extern int 61 | ancil_recv_fds_with_buffer(int, int *, unsigned, void *); 62 | /* 63 | * ancil_recv_fds_with_buffer(sock, n_fds, fds, buffer) 64 | * 65 | * Receives *n_fds file descriptors into the array pointed by fds 66 | * from the socket sock. 67 | * buffer is a writeable memory area large enough to hold the required data 68 | * structures. 69 | * Returns: -1 and errno in case of error, the actual number of received fd 70 | * in case of success 71 | */ 72 | 73 | #define ANCIL_FD_BUFFER(n) \ 74 | struct { \ 75 | struct cmsghdr h; \ 76 | int fd[n]; \ 77 | } 78 | /* ANCIL_FD_BUFFER(n) 79 | * 80 | * A structure type suitable to be used as buffer for n file descriptors. 81 | * Requires . 82 | * Example: 83 | * ANCIL_FD_BUFFER(42) buffer; 84 | * ancil_recv_fds_with_buffer(sock, 42, my_fds, &buffer); 85 | */ 86 | 87 | extern int 88 | ancil_send_fds(int, const int *, unsigned); 89 | /* 90 | * ancil_send_fds(sock, n_fds, fds) 91 | * 92 | * Sends the file descriptors in the array pointed by fds, of length n_fds 93 | * on the socket sock. 94 | * n_fds must not be greater than ANCIL_MAX_N_FDS. 95 | * Returns: -1 and errno in case of error, 0 in case of success. 96 | */ 97 | 98 | extern int 99 | ancil_recv_fds(int, int *, unsigned); 100 | /* 101 | * ancil_recv_fds(sock, n_fds, fds) 102 | * 103 | * Receives *n_fds file descriptors into the array pointed by fds 104 | * from the socket sock. 105 | * *n_fds must not be greater than ANCIL_MAX_N_FDS. 106 | * Returns: -1 and errno in case of error, the actual number of received fd 107 | * in case of success. 108 | */ 109 | 110 | 111 | extern int 112 | ancil_send_fd(int, int); 113 | /* ancil_recv_fd(sock, fd); 114 | * 115 | * Sends the file descriptor fd on the socket sock. 116 | * Returns : -1 and errno in case of error, 0 in case of success. 117 | */ 118 | 119 | extern int 120 | ancil_recv_fd(int, int *); 121 | /* ancil_send_fd(sock, &fd); 122 | * 123 | * Receives the file descriptor fd from the socket sock. 124 | * Returns : -1 and errno in case of error, 0 in case of success. 125 | */ 126 | 127 | #ifdef __cplusplus 128 | } 129 | #endif 130 | 131 | #endif /* ANCILLARY_H__ */ 132 | -------------------------------------------------------------------------------- /3rd/libipset/src/bdd/expanded.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2010-2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the LICENSE.txt file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #include 12 | 13 | #include 14 | 15 | #include "ipset/bdd/nodes.h" 16 | #include "ipset/bits.h" 17 | #include "ipset/logging.h" 18 | 19 | 20 | static void 21 | initialize(struct ipset_expanded_assignment *exp, 22 | const struct ipset_assignment *assignment, 23 | ipset_variable var_count) 24 | { 25 | /* First loop through all of the variables in the assignment vector, 26 | * making sure not to go further than the caller requested. */ 27 | 28 | ipset_variable last_assignment = cork_array_size(&assignment->values); 29 | if (var_count < last_assignment) { 30 | last_assignment = var_count; 31 | } 32 | 33 | ipset_variable var; 34 | for (var = 0; var < last_assignment; var++) { 35 | enum ipset_tribool curr_value = 36 | cork_array_at(&assignment->values, var); 37 | 38 | if (curr_value == IPSET_EITHER) { 39 | /* If this variable is EITHER, start it off as FALSE, and 40 | * add it to the eithers list. */ 41 | DEBUG("Variable %u is EITHER", var); 42 | 43 | IPSET_BIT_SET(exp->values.buf, var, false); 44 | cork_array_append(&exp->eithers, var); 45 | } else { 46 | /* Otherwise set the variable to the same value in the 47 | * expanded assignment as it is in the non-expanded one. */ 48 | 49 | DEBUG("Variable %u is %s", var, curr_value? "true": "false"); 50 | IPSET_BIT_SET(exp->values.buf, var, curr_value); 51 | } 52 | } 53 | 54 | /* If the caller requested more variables than there are in the 55 | * assignment vector, add them to the eithers list. */ 56 | for (var = last_assignment; var < var_count; var++) { 57 | DEBUG("Variable %u is implicitly EITHER", var); 58 | cork_array_append(&exp->eithers, var); 59 | } 60 | } 61 | 62 | 63 | struct ipset_expanded_assignment * 64 | ipset_assignment_expand(const struct ipset_assignment *assignment, 65 | ipset_variable var_count) 66 | { 67 | /* First allocate the iterator itself, and all of its contained 68 | * fields. */ 69 | 70 | struct ipset_expanded_assignment *exp; 71 | unsigned int values_size = (var_count / 8) + ((var_count % 8) != 0); 72 | 73 | exp = cork_new(struct ipset_expanded_assignment); 74 | exp->finished = false; 75 | cork_buffer_init(&exp->values); 76 | cork_buffer_ensure_size(&exp->values, values_size); 77 | memset(exp->values.buf, 0, values_size); 78 | cork_array_init(&exp->eithers); 79 | 80 | /* Then initialize the values and eithers fields. */ 81 | initialize(exp, assignment, var_count); 82 | return exp; 83 | } 84 | 85 | 86 | void 87 | ipset_expanded_assignment_free(struct ipset_expanded_assignment *exp) 88 | { 89 | if (exp == NULL) { 90 | return; 91 | } 92 | 93 | cork_buffer_done(&exp->values); 94 | cork_array_done(&exp->eithers); 95 | free(exp); 96 | } 97 | 98 | 99 | void 100 | ipset_expanded_assignment_advance(struct ipset_expanded_assignment *exp) 101 | { 102 | /* If we're already at the end of the iterator, don't do anything. */ 103 | if (CORK_UNLIKELY(exp->finished)) { 104 | return; 105 | } 106 | 107 | DEBUG("Advancing iterator"); 108 | 109 | /* Look at the last EITHER bit in the assignment. If it's 0, then 110 | * set it to 1 and return. Otherwise we set it to 0 and carry up to 111 | * the previous indeterminate bit. */ 112 | 113 | size_t i; 114 | for (i = cork_array_size(&exp->eithers); i > 0; i--) { 115 | size_t idx = i - 1; 116 | ipset_variable either_var = cork_array_at(&exp->eithers, idx); 117 | DEBUG("Checking EITHER variable %u", either_var); 118 | 119 | if (IPSET_BIT_GET(exp->values.buf, either_var)) { 120 | /* This variable is currently true, so set it back to false 121 | * and carry. */ 122 | DEBUG(" Variable %u is true, changing to false and carrying", 123 | either_var); 124 | IPSET_BIT_SET(exp->values.buf, either_var, false); 125 | } else { 126 | /* This variable is currently false, so set it to true and 127 | * return. */ 128 | DEBUG(" Variable %u is false, changing to true", 129 | either_var); 130 | IPSET_BIT_SET(exp->values.buf, either_var, true); 131 | return; 132 | } 133 | } 134 | 135 | /* If we fall through then we've made it through all of the expanded 136 | * assignments. */ 137 | exp->finished = true; 138 | } 139 | -------------------------------------------------------------------------------- /3rd/libipset/src/bdd/bdd-iterator.c: -------------------------------------------------------------------------------- 1 | /* -*- coding: utf-8 -*- 2 | * ---------------------------------------------------------------------- 3 | * Copyright © 2010-2012, RedJack, LLC. 4 | * All rights reserved. 5 | * 6 | * Please see the LICENSE.txt file in this distribution for license 7 | * details. 8 | * ---------------------------------------------------------------------- 9 | */ 10 | 11 | #include 12 | 13 | #include "ipset/bdd/nodes.h" 14 | #include "ipset/logging.h" 15 | 16 | 17 | /** 18 | * Add the given node ID to the node stack, and trace down from it 19 | * until we find a terminal node. Assign values to the variables for 20 | * each nonterminal that encounter along the way. We check low edges 21 | * first, so each new variable we encounter will be assigned FALSE. 22 | * (The high edges will be checked eventually by a call to the 23 | * ipset_bdd_iterator_advance() function.) 24 | */ 25 | static void 26 | add_node(struct ipset_bdd_iterator *iterator, ipset_node_id node_id) 27 | { 28 | /* Keep tracing down low edges until we reach a terminal. */ 29 | while (ipset_node_get_type(node_id) == IPSET_NONTERMINAL_NODE) { 30 | /* Add this nonterminal node to the stack, and trace down 31 | * further into the tree. We check low edges first, so set the 32 | * node's variable to FALSE in the assignment. */ 33 | struct ipset_node *node = 34 | ipset_node_cache_get_nonterminal(iterator->cache, node_id); 35 | 36 | cork_array_append(&iterator->stack, node_id); 37 | ipset_assignment_set(iterator->assignment, node->variable, false); 38 | 39 | node_id = node->low; 40 | } 41 | 42 | /* Once we find a terminal node, save it away in the iterator result 43 | * and return. */ 44 | iterator->value = ipset_terminal_value(node_id); 45 | } 46 | 47 | 48 | struct ipset_bdd_iterator * 49 | ipset_node_iterate(struct ipset_node_cache *cache, ipset_node_id root) 50 | { 51 | /* First allocate the iterator itself, and all of its contained 52 | * fields. */ 53 | 54 | struct ipset_bdd_iterator *iterator = 55 | cork_new(struct ipset_bdd_iterator); 56 | iterator->finished = false; 57 | iterator->cache = cache; 58 | cork_array_init(&iterator->stack); 59 | iterator->assignment = ipset_assignment_new(); 60 | 61 | /* Then add the root node to the iterator, tracing down until we 62 | * find the first terminal node. */ 63 | add_node(iterator, root); 64 | return iterator; 65 | } 66 | 67 | 68 | void 69 | ipset_bdd_iterator_free(struct ipset_bdd_iterator *iterator) 70 | { 71 | cork_array_done(&iterator->stack); 72 | ipset_assignment_free(iterator->assignment); 73 | free(iterator); 74 | } 75 | 76 | 77 | void 78 | ipset_bdd_iterator_advance(struct ipset_bdd_iterator *iterator) 79 | { 80 | /* If we're already at the end of the iterator, don't do anything. */ 81 | if (CORK_UNLIKELY(iterator->finished)) { 82 | return; 83 | } 84 | 85 | /* We look at the last node in the stack. If it's currently 86 | * assigned a false value, then we track down its true branch. If 87 | * it's got a true branch, then we pop it off and check the next to 88 | * last node. */ 89 | 90 | DEBUG("Advancing BDD iterator"); 91 | 92 | while (cork_array_size(&iterator->stack) > 0) { 93 | ipset_node_id last_node_id = 94 | cork_array_at 95 | (&iterator->stack, cork_array_size(&iterator->stack) - 1); 96 | 97 | struct ipset_node *last_node = 98 | ipset_node_cache_get_nonterminal(iterator->cache, last_node_id); 99 | 100 | enum ipset_tribool current_value = 101 | ipset_assignment_get(iterator->assignment, last_node->variable); 102 | 103 | /* The current value can't be EITHER, because we definitely 104 | * assign a TRUE or FALSE to the variables of the nodes that we 105 | * encounter. */ 106 | if (current_value == IPSET_TRUE) { 107 | /* We've checked both outgoing edges for this node, so pop 108 | * it off and look at its parent. */ 109 | iterator->stack.size--; 110 | 111 | /* Before continuing, reset this node's variable to 112 | * indeterminate in the assignment. */ 113 | ipset_assignment_set 114 | (iterator->assignment, last_node->variable, IPSET_EITHER); 115 | } else { 116 | /* We've checked this node's low edge, but not its high 117 | * edge. Set the variable to TRUE in the assignment, and 118 | * add the high edge's node to the node stack. */ 119 | ipset_assignment_set 120 | (iterator->assignment, last_node->variable, IPSET_TRUE); 121 | add_node(iterator, last_node->high); 122 | return; 123 | } 124 | } 125 | 126 | /* If we fall through then we ran out of nodes to check. That means 127 | * the iterator is done! */ 128 | iterator->finished = true; 129 | } 130 | --------------------------------------------------------------------------------