├── tests ├── x_8888 ├── google.com ├── taobao.com ├── facebook.com ├── twitter.com ├── www.facebook.com ├── iplist.py └── test.py ├── Makefile.am ├── src ├── Makefile.am ├── local_ns_parser.h ├── local_ns_parser.c └── chinadns.c ├── autogen.sh ├── .travis.yml ├── .gitignore ├── packaging ├── release.sh └── build_all.sh ├── openwrt ├── files │ └── chinadns.init └── Makefile ├── CHANGES ├── configure.ac ├── .jenkins.sh ├── config.h.in ├── README.md ├── iplist.txt └── COPYING /tests/x_8888: -------------------------------------------------------------------------------- 1 | dig -x 8.8.8.8 @127.0.0.1 2 | -------------------------------------------------------------------------------- /tests/google.com: -------------------------------------------------------------------------------- 1 | dig @127.0.0.1 a google.com 2 | -------------------------------------------------------------------------------- /tests/taobao.com: -------------------------------------------------------------------------------- 1 | dig @127.0.0.1 a taobao.com 2 | -------------------------------------------------------------------------------- /tests/facebook.com: -------------------------------------------------------------------------------- 1 | dig @127.0.0.1 a facebook.com 2 | -------------------------------------------------------------------------------- /tests/twitter.com: -------------------------------------------------------------------------------- 1 | dig @127.0.0.1 a twitter.com 2 | -------------------------------------------------------------------------------- /tests/www.facebook.com: -------------------------------------------------------------------------------- 1 | dig @127.0.0.1 a www.facebook.com 2 | -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS = src 2 | dist_data_DATA = iplist.txt chnroute.txt 3 | -------------------------------------------------------------------------------- /src/Makefile.am: -------------------------------------------------------------------------------- 1 | bin_PROGRAMS = chinadns 2 | 3 | chinadns_SOURCES = chinadns.c local_ns_parser.c local_ns_parser.h 4 | -------------------------------------------------------------------------------- /autogen.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | aclocal && \ 4 | automake --add-missing --force-missing --include-deps && \ 5 | autoconf 6 | -------------------------------------------------------------------------------- /src/local_ns_parser.h: -------------------------------------------------------------------------------- 1 | #ifndef LOCAL_NS_PARSER_H 2 | #define LOCAL_NS_PARSER_H 3 | 4 | int local_ns_initparse(const unsigned char *msg, int msglen, ns_msg *handle); 5 | int local_ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr); 6 | 7 | #endif 8 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | 3 | os: 4 | - linux 5 | - osx 6 | 7 | compiler: 8 | - clang 9 | - gcc 10 | 11 | before_script: 12 | - sudo apt-get update -qq 13 | - sudo apt-get install dnsutils 14 | - sudo pip install gcovr 15 | - ./autogen.sh 16 | - ./configure 17 | 18 | script: 19 | - ./.jenkins.sh 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.cmake 2 | *.dSYM 3 | *.exp 4 | *.la 5 | *.lo 6 | *.log 7 | *.o 8 | *.plist 9 | *.scan 10 | *.sdf 11 | *.status 12 | *.tar.* 13 | *.gcov 14 | *.gcda 15 | *.gcno 16 | *.html 17 | *~ 18 | .DS_Store 19 | .deps 20 | .dirstamp 21 | .done 22 | .libs 23 | Build 24 | Makefile 25 | Makefile.in 26 | aclocal.m4 27 | autom4te.cache 28 | build 29 | compile 30 | confdefs.h 31 | config.h 32 | configure 33 | depcomp 34 | android-toolchain 35 | install-sh 36 | libtool 37 | ltmain.sh 38 | m4/argz.m4 39 | m4/libtool.m4 40 | m4/ltoptions.m4 41 | m4/ltsugar.m4 42 | m4/ltversion.m4 43 | m4/lt~obsolete.m4 44 | src/chinadns 45 | missing 46 | stamp-h1 47 | -------------------------------------------------------------------------------- /tests/iplist.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import random 4 | import sys 5 | from subprocess import Popen, PIPE 6 | 7 | if __name__ == '__main__': 8 | result = set() 9 | start = random.randint(0, 1000000) 10 | for i in range(start, start + 1000): 11 | p = Popen((('dig +short @114.114.114.114 a ' 12 | 'r%d-1.googlevideo.com') % i).split(), 13 | stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) 14 | output = p.stdout.read() 15 | if output: 16 | print >>sys.stderr, output.strip() 17 | result.add(output.strip()) 18 | print 19 | for r in result: 20 | print r 21 | -------------------------------------------------------------------------------- /packaging/release.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ $# -ne 1 ]; then 4 | echo usage: release.sh version 5 | exit 1 6 | fi 7 | 8 | VERSION=$1 9 | 10 | git tag $1 11 | git push origin $1 12 | ./autogen.sh && ./configure && make dist 13 | 14 | API_JSON=$(printf '{"tag_name": "%s","target_commitish": "master","name": "%s","body": "","draft": false,"prerelease": false}' $VERSION $VERSION) 15 | ID=`curl -L -v --data "$API_JSON" https://api.github.com/repos/clowwindy/ChinaDNS/releases?access_token=$GITHUB_TOKEN | python -c 'import json,sys;d = json.load(sys.stdin);print >>sys.stderr, d;print d["id"]'` 16 | 17 | curl -v -L -H "Content-Type: application/x-tar" \ 18 | -H "Authorization: token $GITHUB_TOKEN" \ 19 | --data-binary "@chinadns-$VERSION.tar.gz" \ 20 | https://uploads.github.com/repos/clowwindy/ChinaDNS/releases/$ID/assets?name=chinadns-$VERSION.tar.gz 21 | -------------------------------------------------------------------------------- /openwrt/files/chinadns.init: -------------------------------------------------------------------------------- 1 | #!/bin/sh /etc/rc.common 2 | # chinadns init script 3 | 4 | START=90 5 | STOP=15 6 | EXTRA_COMMANDS="restart" 7 | PIDFILE='/tmp/chinadns.pid' 8 | 9 | start() 10 | { 11 | if [ -f $PIDFILE ] 12 | then 13 | echo "already started: $PIDFILE exists" 14 | exit 1 15 | fi 16 | chinadns \ 17 | -l /etc/chinadns_iplist.txt \ 18 | -c /etc/chinadns_chnroute.txt \ 19 | -p 5353 \ 20 | 1> /tmp/log/chinadns.log \ 21 | 2> /tmp/log/chinadns.err.log & 22 | echo $! > $PIDFILE 23 | iptables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 5353 24 | } 25 | 26 | stop() 27 | { 28 | iptables -t nat -D PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 5353 29 | kill `cat $PIDFILE` 30 | rm $PIDFILE 31 | } 32 | 33 | restart() 34 | { 35 | stop 36 | start 37 | } 38 | -------------------------------------------------------------------------------- /packaging/build_all.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | VERSION=$1 4 | 5 | pushd OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2 && \ 6 | pushd package/ChinaDNS && \ 7 | git fetch origin && \ 8 | git merge origin/dev --ff-only && \ 9 | popd && \ 10 | make V=s && \ 11 | rsync --progress -e ssh bin/ar71xx/packages/ChinaDNS_$1_ar71xx.ipk frs.sourceforge.net:/home/frs/project/chinadns/dist/ && \ 12 | popd && \ 13 | 14 | pushd OpenWrt-SDK-brcm47xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2 && \ 15 | make V=s && \ 16 | rsync --progress -e ssh bin/brcm47xx/packages/base/ChinaDNS_$1_brcm47xx.ipk frs.sourceforge.net:/home/frs/project/chinadns/dist/ && \ 17 | popd && \ 18 | 19 | pushd OpenWrt-SDK-brcm63xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2 && \ 20 | make V=s && \ 21 | rsync --progress -e ssh bin/brcm63xx/packages/base/ChinaDNS_$1_brcm63xx.ipk frs.sourceforge.net:/home/frs/project/chinadns/dist/ && \ 22 | popd && \ 23 | 24 | 25 | pushd OpenWrt-SDK-ramips-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2 && \ 26 | make V=s && \ 27 | rsync --progress -e ssh bin/ramips/packages/base/ChinaDNS_$1_ramips_24kec.ipk frs.sourceforge.net:/home/frs/project/chinadns/dist/ && \ 28 | popd 29 | 30 | 31 | -------------------------------------------------------------------------------- /tests/test.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # -*- coding: utf-8 -*- 3 | 4 | import sys 5 | import os 6 | import signal 7 | import time 8 | import argparse 9 | from subprocess import Popen 10 | 11 | parser = argparse.ArgumentParser(description='test ChinaDNS') 12 | parser.add_argument('-a', '--arguments', type=str, default=[]) 13 | parser.add_argument('-t', '--test-command', type=str, default=None) 14 | 15 | config = parser.parse_args() 16 | 17 | arguments = config.arguments 18 | chinadns = ['src/chinadns', '-p', '15353', '-v'] + arguments.split() 19 | 20 | print chinadns 21 | p1 = Popen(chinadns, shell=False, bufsize=0, close_fds=True) 22 | try: 23 | 24 | with open(config.test_command) as f: 25 | dig_cmd = f.read() 26 | 27 | time.sleep(1) 28 | 29 | p2 = Popen(dig_cmd.split() + ['-p', '15353'], shell=False, 30 | bufsize=0, close_fds=True) 31 | 32 | if p2 is not None: 33 | r = p2.wait() 34 | if r == 0: 35 | print 'test passed' 36 | 37 | sys.exit(r) 38 | finally: 39 | for p in [p1]: 40 | try: 41 | os.kill(p.pid, signal.SIGTERM) 42 | os.waitpid(p.pid, 0) 43 | except OSError: 44 | pass 45 | 46 | -------------------------------------------------------------------------------- /openwrt/Makefile: -------------------------------------------------------------------------------- 1 | include $(TOPDIR)/rules.mk 2 | 3 | PKG_NAME:=ChinaDNS 4 | PKG_VERSION:=1.3.2 5 | PKG_RELEASE=$(PKG_SOURCE_VERSION) 6 | 7 | PKG_SOURCE_URL:=https://github.com/clowwindy/ChinaDNS/releases/download/$(PKG_VERSION) 8 | PKG_SOURCE:=chinadns-$(PKG_VERSION).tar.gz 9 | PKG_MAINTAINER:=clowwindy 10 | 11 | PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(BUILD_VARIANT)/chinadns-$(PKG_VERSION) 12 | 13 | PKG_INSTALL:=1 14 | PKG_FIXUP:=autoreconf 15 | PKG_USE_MIPS16:=0 16 | PKG_BUILD_PARALLEL:=1 17 | 18 | include $(INCLUDE_DIR)/package.mk 19 | 20 | define Package/ChinaDNS/Default 21 | SECTION:=net 22 | CATEGORY:=Network 23 | TITLE:=ChinaDNS 24 | URL:=https://github.com/clowwindy/ChinaDNS 25 | endef 26 | 27 | define Package/ChinaDNS 28 | $(call Package/ChinaDNS/Default) 29 | endef 30 | 31 | define Package/ChinaDNS/description 32 | A DNS forwarder that ignores incorrect(you knew it) responses. 33 | endef 34 | 35 | define Package/ChinaDNS/conffiles 36 | /etc/chinadns_iplist.txt 37 | /etc/chinadns_chnroute.txt 38 | endef 39 | 40 | define Package/ChinaDNS/install 41 | $(INSTALL_DIR) $(1)/etc/init.d 42 | $(INSTALL_CONF) $(PKG_BUILD_DIR)/iplist.txt $(1)/etc/chinadns_iplist.txt 43 | $(INSTALL_CONF) $(PKG_BUILD_DIR)/chnroute.txt $(1)/etc/chinadns_chnroute.txt 44 | $(INSTALL_BIN) ./files/chinadns.init $(1)/etc/init.d/chinadns 45 | $(INSTALL_DIR) $(1)/usr/bin 46 | $(INSTALL_BIN) $(PKG_BUILD_DIR)/src/chinadns $(1)/usr/bin 47 | endef 48 | 49 | $(eval $(call BuildPackage,ChinaDNS)) 50 | -------------------------------------------------------------------------------- /CHANGES: -------------------------------------------------------------------------------- 1 | 1.3.3 2019-08-13 2 | - Cumulative Update 3 | 4 | 1.3.2 2015-08-05 5 | - Fix compatibility issues 6 | 7 | 1.3.1 2015-03-14 8 | - Update strategy 9 | 10 | 1.3.0 2015-01-25 11 | - Support DNS compression pointer mutation 12 | 13 | 1.2.1 2015-01-01 14 | - Fix high CPU usage 15 | 16 | 1.2.0 2015-01-01 17 | - Happy new year and add a new filter strategy 18 | 19 | 1.1.9 2014-12-26 20 | - One Chinese DNS and one foreign DNS requirement is only a warning now 21 | - Update iplist 22 | 23 | 1.1.8 2014-11-19 24 | - Add -d option: also filter results inside China from foreign DNS servers 25 | 26 | 1.1.7 2014-09-16 27 | - Add 443 port of OpenDNS as default DNS 28 | 29 | 1.1.6 2014-09-16 30 | - Use fgets() for better portability 31 | - Fix logs format 32 | 33 | 1.1.5 2014-09-08 34 | - Support single IP address 35 | - Update IP list and CHNRoute 36 | 37 | 1.1.4 2014-08-27 38 | - Fix logs with stderr 39 | - Fix remote socket not in nonblocking mode 40 | - Fix restart function in OpenWRT init script 41 | 42 | 1.1.3 2014-08-11 43 | - Make CHNRoute an option 44 | - Output logs to stderr 45 | 46 | 1.1.2 2014-08-11 47 | - Fix mask > 0xFFFF with chnroute 48 | 49 | 1.1.1 2014-08-08 50 | - Use CIDR format in chnroute.txt 51 | 52 | 1.1.0 2014-08-07 53 | - Support chnroute 54 | 55 | 1.0.4 2014-08-05 56 | - Fix IP filter random failures 57 | 58 | 1.0.3 2014-07-29 59 | - Fix segment fault when queue is full 60 | 61 | 1.0.2 2014-07-29 62 | - Support -s hostname:port 63 | 64 | 1.0.1 2014-07-28 65 | - Use autotools 66 | 67 | 1.0.0 2014-07-25 68 | - Initial version 69 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | # -*- Autoconf -*- 2 | # Process this file with autoconf to produce a configure script. 3 | 4 | AC_PREREQ([2.67]) 5 | AC_INIT([ChinaDNS], [1.3.3], [clowwindy42@gmail.com]) 6 | AC_CONFIG_SRCDIR([src/chinadns.c]) 7 | AC_CONFIG_HEADERS([config.h]) 8 | 9 | AM_INIT_AUTOMAKE([foreign -Wall]) 10 | 11 | # Checks for programs. 12 | AC_PROG_CC 13 | AC_PROG_INSTALL 14 | 15 | # Checks for libraries. 16 | AC_CHECK_LIB(resolv, res_query, [], 17 | [AC_CHECK_LIB(resolv, __res_query, [], 18 | [AC_MSG_ERROR([libresolv not found.])])]) 19 | 20 | # Checks for header files. 21 | AC_HEADER_RESOLV 22 | AC_CHECK_HEADERS([arpa/inet.h fcntl.h netdb.h stdlib.h string.h sys/socket.h unistd.h]) 23 | 24 | # Checks for typedefs, structures, and compiler characteristics. 25 | AC_TYPE_SIZE_T 26 | AC_TYPE_SSIZE_T 27 | AC_TYPE_UINT16_T 28 | 29 | # Checks for library functions. 30 | # To fix rpl_malloc undefined error in mips cross-compile enviroment. 31 | AC_CHECK_FUNCS([malloc realloc]) 32 | AC_CHECK_FUNCS([inet_ntoa memset select socket strchr strdup strrchr]) 33 | 34 | AC_ARG_ENABLE([debug], 35 | [ --enable-debug build with additional debugging code], 36 | [CFLAGS='-g -DDEBUG -fprofile-arcs -ftest-coverage -O0']) 37 | 38 | AM_CONDITIONAL(DEBUG, test x"$debug" = x"true") 39 | 40 | # Add a option to force static link the target. 41 | AC_ARG_ENABLE([static], 42 | [ --enable-static build with static linking], 43 | [LDFLAGS='-static']) 44 | 45 | AM_CONDITIONAL(STATIC, test x"$static" = x"true") 46 | 47 | AC_CONFIG_FILES([Makefile src/Makefile]) 48 | AC_OUTPUT 49 | -------------------------------------------------------------------------------- /.jenkins.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | result=0 4 | 5 | function run_test { 6 | printf '\e[0;36m' 7 | echo "running test: $command $@" 8 | printf '\e[0m' 9 | 10 | $command "$@" 11 | status=$? 12 | if [ $status -ne 0 ]; then 13 | printf '\e[0;31m' 14 | echo "test failed: $command $@" 15 | printf '\e[0m' 16 | echo 17 | result=1 18 | else 19 | printf '\e[0;32m' 20 | echo OK 21 | printf '\e[0m' 22 | echo 23 | fi 24 | return 0 25 | } 26 | 27 | rm src/*.gc* 28 | run_test ./autogen.sh 29 | run_test ./configure --enable-debug 30 | make clean 31 | run_test make 32 | 33 | run_test src/chinadns -h 34 | run_test src/chinadns -V 35 | 36 | run_test tests/test.py -a '-c chnroute.txt -l iplist.txt' -t tests/google.com 37 | run_test tests/test.py -a '-c chnroute.txt -l iplist.txt' -t tests/facebook.com 38 | run_test tests/test.py -a '-c chnroute.txt -s 114.114.114.114,8.8.8.8 -l iplist.txt' -t tests/google.com 39 | run_test tests/test.py -a '-c chnroute.txt -y 0.5 -l iplist.txt' -t tests/google.com 40 | run_test tests/test.py -a '-c chnroute.txt -b 0.0.0.0 -l iplist.txt' -t tests/google.com 41 | 42 | run_test tests/test.py -a '-c chnroute.txt -l iplist.txt' -t tests/twitter.com 43 | run_test tests/test.py -a '-d -c chnroute.txt -l iplist.txt' -t tests/twitter.com 44 | run_test tests/test.py -a '-c chnroute.txt' -t tests/twitter.com 45 | run_test tests/test.py -a '-m -c chnroute.txt' -t tests/twitter.com 46 | 47 | run_test tests/test.py -a '-c chnroute.txt -l iplist.txt' -t tests/taobao.com 48 | run_test tests/test.py -a '-d -c chnroute.txt -l iplist.txt' -t tests/taobao.com 49 | run_test tests/test.py -a '-c chnroute.txt' -t tests/taobao.com 50 | run_test tests/test.py -a '-m -c chnroute.txt' -t tests/taobao.com 51 | 52 | run_test tests/test.py -a '-c chnroute.txt -l iplist.txt' -t tests/x_8888 53 | run_test tests/test.py -a '-d -c chnroute.txt -l iplist.txt' -t tests/x_8888 54 | run_test tests/test.py -a '-c chnroute.txt' -t tests/x_8888 55 | run_test tests/test.py -a '-m -c chnroute.txt' -t tests/x_8888 56 | 57 | gcov src/*.c 58 | rm src/*.html 59 | cd src && gcovr -r . --html --html-details -o index.html 60 | gcovr -r . | grep TOTAL | rev | cut -d' ' -f 1 | rev > /tmp/chinadns-coverage 61 | 62 | exit $result 63 | -------------------------------------------------------------------------------- /config.h.in: -------------------------------------------------------------------------------- 1 | /* config.h.in. Generated from configure.ac by autoheader. */ 2 | 3 | /* Define to 1 if you have the header file. */ 4 | #undef HAVE_ARPA_INET_H 5 | 6 | /* Define to 1 if you have the header file. */ 7 | #undef HAVE_ARPA_NAMESER_H 8 | 9 | /* Define to 1 if you have the header file. */ 10 | #undef HAVE_FCNTL_H 11 | 12 | /* Define to 1 if you have the `inet_ntoa' function. */ 13 | #undef HAVE_INET_NTOA 14 | 15 | /* Define to 1 if you have the header file. */ 16 | #undef HAVE_INTTYPES_H 17 | 18 | /* Define to 1 if you have the `resolv' library (-lresolv). */ 19 | #undef HAVE_LIBRESOLV 20 | 21 | /* Define to 1 if you have the `malloc' function. */ 22 | #undef HAVE_MALLOC 23 | 24 | /* Define to 1 if you have the header file. */ 25 | #undef HAVE_MEMORY_H 26 | 27 | /* Define to 1 if you have the `memset' function. */ 28 | #undef HAVE_MEMSET 29 | 30 | /* Define to 1 if you have the header file. */ 31 | #undef HAVE_NETDB_H 32 | 33 | /* Define to 1 if you have the header file. */ 34 | #undef HAVE_NETINET_IN_H 35 | 36 | /* Define to 1 if you have the `realloc' function. */ 37 | #undef HAVE_REALLOC 38 | 39 | /* Define to 1 if you have the header file. */ 40 | #undef HAVE_RESOLV_H 41 | 42 | /* Define to 1 if you have the `select' function. */ 43 | #undef HAVE_SELECT 44 | 45 | /* Define to 1 if you have the `socket' function. */ 46 | #undef HAVE_SOCKET 47 | 48 | /* Define to 1 if you have the header file. */ 49 | #undef HAVE_STDINT_H 50 | 51 | /* Define to 1 if you have the header file. */ 52 | #undef HAVE_STDLIB_H 53 | 54 | /* Define to 1 if you have the `strchr' function. */ 55 | #undef HAVE_STRCHR 56 | 57 | /* Define to 1 if you have the `strdup' function. */ 58 | #undef HAVE_STRDUP 59 | 60 | /* Define to 1 if you have the header file. */ 61 | #undef HAVE_STRINGS_H 62 | 63 | /* Define to 1 if you have the header file. */ 64 | #undef HAVE_STRING_H 65 | 66 | /* Define to 1 if you have the `strrchr' function. */ 67 | #undef HAVE_STRRCHR 68 | 69 | /* Define to 1 if you have the header file. */ 70 | #undef HAVE_SYS_SOCKET_H 71 | 72 | /* Define to 1 if you have the header file. */ 73 | #undef HAVE_SYS_STAT_H 74 | 75 | /* Define to 1 if you have the header file. */ 76 | #undef HAVE_SYS_TYPES_H 77 | 78 | /* Define to 1 if you have the header file. */ 79 | #undef HAVE_UNISTD_H 80 | 81 | /* Name of package */ 82 | #undef PACKAGE 83 | 84 | /* Define to the address where bug reports for this package should be sent. */ 85 | #undef PACKAGE_BUGREPORT 86 | 87 | /* Define to the full name of this package. */ 88 | #undef PACKAGE_NAME 89 | 90 | /* Define to the full name and version of this package. */ 91 | #undef PACKAGE_STRING 92 | 93 | /* Define to the one symbol short name of this package. */ 94 | #undef PACKAGE_TARNAME 95 | 96 | /* Define to the home page for this package. */ 97 | #undef PACKAGE_URL 98 | 99 | /* Define to the version of this package. */ 100 | #undef PACKAGE_VERSION 101 | 102 | /* Define to 1 if you have the ANSI C header files. */ 103 | #undef STDC_HEADERS 104 | 105 | /* Version number of package */ 106 | #undef VERSION 107 | 108 | /* Define to `unsigned int' if does not define. */ 109 | #undef size_t 110 | 111 | /* Define to `int' if does not define. */ 112 | #undef ssize_t 113 | 114 | /* Define to the type of an unsigned integer type of width exactly 16 bits if 115 | such a type exists and the standard includes do not define it. */ 116 | #undef uint16_t 117 | -------------------------------------------------------------------------------- /src/local_ns_parser.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "local_ns_parser.h" 5 | 6 | static void local_ns_setsection(ns_msg *msg, ns_sect sect); 7 | static int local_ns_skiprr(const unsigned char *ptr, const unsigned char *eom, ns_sect section, int count); 8 | static int local_ns_dn_skipname(const unsigned char *ptr, const unsigned char *eom); 9 | static int local_ns_name_skip(const unsigned char **ptrptr, const unsigned char *eom); 10 | static int local_ns_labellen(const unsigned char *lp); 11 | #define LOCAL_NS_TYPE_ELT 0x40 /*%< EDNS0 extended label type */ 12 | #define LOCAL_DNS_LABELTYPE_BITSTRING 0x41 13 | #ifdef __UCLIBC__ 14 | #define LOCAL_NS_MSG_PTR _ptr 15 | #else 16 | #define LOCAL_NS_MSG_PTR _msg_ptr 17 | #endif 18 | 19 | int local_ns_initparse(const unsigned char *msg, int msglen, ns_msg *handle) 20 | { 21 | const unsigned char *eom = msg + msglen; 22 | int i; 23 | 24 | handle->_msg = msg; 25 | handle->_eom = eom; 26 | if (msg + NS_INT16SZ > eom) { 27 | errno = EMSGSIZE; 28 | return -1; 29 | } 30 | 31 | NS_GET16(handle->_id, msg); 32 | if (msg + NS_INT16SZ > eom) { 33 | errno = EMSGSIZE; 34 | return -1; 35 | } 36 | 37 | NS_GET16(handle->_flags, msg); 38 | for (i = 0; i < ns_s_max; i++) { 39 | if (msg + NS_INT16SZ > eom) { 40 | errno = EMSGSIZE; 41 | return -1; 42 | } 43 | 44 | NS_GET16(handle->_counts[i], msg); 45 | } 46 | for (i = 0; i < ns_s_max; i++) 47 | if (handle->_counts[i] == 0) 48 | handle->_sections[i] = NULL; 49 | else { 50 | int b = local_ns_skiprr(msg, eom, (ns_sect)i, 51 | handle->_counts[i]); 52 | 53 | if (b < 0) 54 | return -1; 55 | handle->_sections[i] = msg; 56 | msg += b; 57 | } 58 | 59 | if (msg != eom) { 60 | errno = EMSGSIZE; 61 | return -1; 62 | } 63 | 64 | local_ns_setsection(handle, ns_s_max); 65 | return 0; 66 | } 67 | int local_ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) 68 | { 69 | int b; 70 | int tmp; 71 | 72 | /* Make section right. */ 73 | tmp = section; 74 | if (tmp < 0 || section >= ns_s_max) { 75 | errno = ENODEV; 76 | return -1; 77 | } 78 | 79 | if (section != handle->_sect) 80 | local_ns_setsection(handle, section); 81 | 82 | /* Make rrnum right. */ 83 | if (rrnum == -1) 84 | rrnum = handle->_rrnum; 85 | if (rrnum < 0 || rrnum >= handle->_counts[(int)section]) { 86 | errno = ENODEV; 87 | return -1; 88 | } 89 | if (rrnum < handle->_rrnum) 90 | local_ns_setsection(handle, section); 91 | if (rrnum > handle->_rrnum) { 92 | b = local_ns_skiprr(handle->LOCAL_NS_MSG_PTR, handle->_eom, section, 93 | rrnum - handle->_rrnum); 94 | 95 | if (b < 0) 96 | return (-1); 97 | handle->LOCAL_NS_MSG_PTR += b; 98 | handle->_rrnum = rrnum; 99 | } 100 | 101 | /* Do the parse. */ 102 | b = dn_expand(handle->_msg, handle->_eom, 103 | handle->LOCAL_NS_MSG_PTR, rr->name, NS_MAXDNAME); 104 | if (b < 0) 105 | return (-1); 106 | handle->LOCAL_NS_MSG_PTR += b; 107 | if (handle->LOCAL_NS_MSG_PTR + NS_INT16SZ + NS_INT16SZ > handle->_eom) { 108 | errno = EMSGSIZE; 109 | return -1; 110 | } 111 | NS_GET16(rr->type, handle->LOCAL_NS_MSG_PTR); 112 | NS_GET16(rr->rr_class, handle->LOCAL_NS_MSG_PTR); 113 | if (section == ns_s_qd) { 114 | rr->ttl = 0; 115 | rr->rdlength = 0; 116 | rr->rdata = NULL; 117 | } else { 118 | if (handle->LOCAL_NS_MSG_PTR + NS_INT32SZ + NS_INT16SZ > handle->_eom) { 119 | errno = EMSGSIZE; 120 | return -1; 121 | } 122 | NS_GET32(rr->ttl, handle->LOCAL_NS_MSG_PTR); 123 | NS_GET16(rr->rdlength, handle->LOCAL_NS_MSG_PTR); 124 | if (handle->LOCAL_NS_MSG_PTR + rr->rdlength > handle->_eom) { 125 | errno = EMSGSIZE; 126 | return -1; 127 | } 128 | rr->rdata = handle->LOCAL_NS_MSG_PTR; 129 | handle->LOCAL_NS_MSG_PTR += rr->rdlength; 130 | } 131 | if (++handle->_rrnum > handle->_counts[(int)section]) 132 | local_ns_setsection(handle, (ns_sect)((int)section + 1)); 133 | 134 | return 0; 135 | } 136 | static void local_ns_setsection(ns_msg *msg, ns_sect sect) 137 | { 138 | msg->_sect = sect; 139 | if (sect == ns_s_max) { 140 | msg->_rrnum = -1; 141 | msg->LOCAL_NS_MSG_PTR = NULL; 142 | } else { 143 | msg->_rrnum = 0; 144 | msg->LOCAL_NS_MSG_PTR = msg->_sections[(int)sect]; 145 | } 146 | } 147 | static int local_ns_skiprr(const unsigned char *ptr, const unsigned char *eom, ns_sect section, int count) 148 | { 149 | const unsigned char *optr = ptr; 150 | 151 | for (; count > 0; count--) { 152 | int b, rdlength; 153 | 154 | b = local_ns_dn_skipname(ptr, eom); 155 | if (b < 0) { 156 | errno = EMSGSIZE; 157 | return -1; 158 | } 159 | ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/; 160 | if (section != ns_s_qd) { 161 | if (ptr + NS_INT32SZ + NS_INT16SZ > eom) { 162 | errno = EMSGSIZE; 163 | return -1; 164 | } 165 | 166 | ptr += NS_INT32SZ/*TTL*/; 167 | NS_GET16(rdlength, ptr); 168 | ptr += rdlength/*RData*/; 169 | } 170 | } 171 | 172 | if (ptr > eom) { 173 | errno = EMSGSIZE; 174 | return -1; 175 | } 176 | 177 | return ptr - optr; 178 | } 179 | static int local_ns_dn_skipname(const unsigned char *ptr, const unsigned char *eom) 180 | { 181 | const unsigned char *saveptr = ptr; 182 | 183 | if (local_ns_name_skip(&ptr, eom) == -1) 184 | return -1; 185 | 186 | return ptr - saveptr; 187 | } 188 | static int local_ns_name_skip(const unsigned char **ptrptr, const unsigned char *eom) 189 | { 190 | const unsigned char *cp; 191 | unsigned int n; 192 | int l; 193 | 194 | cp = *ptrptr; 195 | while (cp < eom && (n = *cp++) != 0) { 196 | /* Check for indirection. */ 197 | switch (n & NS_CMPRSFLGS) { 198 | case 0: /*%< normal case, n == len */ 199 | cp += n; 200 | continue; 201 | case LOCAL_NS_TYPE_ELT: /*%< EDNS0 extended label */ 202 | if ((l = local_ns_labellen(cp - 1)) < 0) { 203 | errno = EMSGSIZE; /*%< XXX */ 204 | return -1; 205 | } 206 | cp += l; 207 | continue; 208 | case NS_CMPRSFLGS: /*%< indirection */ 209 | cp++; 210 | break; 211 | default: /*%< illegal type */ 212 | errno = EMSGSIZE; 213 | return -1; 214 | } 215 | 216 | break; 217 | } 218 | 219 | if (cp > eom) { 220 | errno = EMSGSIZE; 221 | return -1; 222 | } 223 | 224 | *ptrptr = cp; 225 | 226 | return 0; 227 | } 228 | static int local_ns_labellen(const unsigned char *lp) 229 | { 230 | int bitlen; 231 | unsigned char l = *lp; 232 | 233 | if ((l & NS_CMPRSFLGS) == NS_CMPRSFLGS) { 234 | /* should be avoided by the caller */ 235 | return -1; 236 | } 237 | 238 | if ((l & NS_CMPRSFLGS) == LOCAL_NS_TYPE_ELT) { 239 | if (l == LOCAL_DNS_LABELTYPE_BITSTRING) { 240 | if ((bitlen = *(lp + 1)) == 0) 241 | bitlen = 256; 242 | return ((bitlen + 7 ) / 8 + 1); 243 | } 244 | 245 | return -1; /*%< unknwon ELT */ 246 | } 247 | 248 | return l; 249 | } 250 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ChinaDNS 2 | ======== 3 | 4 | [![Build Status]][Travis CI] 5 | [![Coverage Status]][Coverage] 6 | 7 | Traditional way to bypass DNS poisoning is to send all queries to 8 | a foreign DNS server via VPN. However some Chinese websites will get 9 | bad results if they have CDNs outside the country. 10 | 11 | The second way is to maintain a list of domains of which you want to 12 | resolve from local DNS or foreign DNS. This list changes too often, 13 | taking too much effort to maintain. 14 | 15 | ChinaDNS automatically queries local DNS servers to resolve Chinese domains 16 | and queries foreign DNS servers to resolve foreign domains. It is smart 17 | enough to work only with a Chinese IP range file, which doesn't change often. 18 | 19 | In order to bypass IP blocking, you SHOULD use VPN software like [ShadowVPN]. 20 | 21 | Install 22 | ------- 23 | 24 | * Linux / Unix 25 | 26 | [Download a release]. 27 | 28 | ./configure && make 29 | src/chinadns -m -c chnroute.txt 30 | 31 | * OpenWRT 32 | 33 | * [Download precompiled] for OpenWRT trunk and CPU: ar71xx, brcm63xx, 34 | brcm47xx, ramips_24kec. Open an issue if you think your CPU is a popular 35 | one but not listed here. 36 | * If you use other CPU or other OpenWRT versions, build yourself: 37 | cd into [SDK] root, then 38 | 39 | pushd package 40 | git clone https://github.com/clowwindy/ChinaDNS.git 41 | popd 42 | make menuconfig # select Network/ChinaDNS 43 | make -j 44 | make V=99 package/ChinaDNS/openwrt/compile 45 | 46 | * Tomoto 47 | 48 | * Download [Tomato toolchain], build by yourself. 49 | * Uncompress the downloaded file to `~/`. 50 | * Copy the `brcm` directory under 51 | `~/WRT54GL-US_v4.30.11_11/tools/` to `/opt`, then 52 | 53 | export PATH=/opt/brcm/hndtools-mipsel-uclibc/bin/:/opt/brcm/hndtools-mipsel-linux/bin/:$PATH 54 | git clone https://github.com/clowwindy/ChinaDNS.git 55 | cd ChinaDNS 56 | ./autogen.sh && ./configure --host=mipsel-linux --enable-static && make 57 | 58 | * Windows 59 | 60 | [Download] Python exe version. 61 | 62 | Usage 63 | ----- 64 | 65 | * Linux / Unix 66 | Recommand using with option "-m" ([DNS pointer mutation method]) 67 | Run `sudo chinadns -m -c chnroute.txt` on your local machine. ChinaDNS creates a 68 | UDP DNS Server at `0.0.0.0:53`. 69 | 70 | * OpenWRT 71 | 72 | opkg install ChinaDNS_1.x.x_ar71xx.ipk 73 | /etc/init.d/chinadns start 74 | /etc/init.d/chinadns enable 75 | 76 | Invoke the "enable" command to run the initscript on boot 77 | 78 | (Optional) We strongly recommend you to set ChinaDNS as a upstream DNS 79 | server for dnsmasq instead of using ChinaDNS directly: 80 | 81 | 1. Run `/etc/init.d/chinadns stop` 82 | 2. Remove the 2 lines containing `iptables` in `/etc/init.d/chinadns`. 83 | 3. Update `/etc/dnsmasq.conf` to use only 127.0.0.1#5353: 84 | 85 | no-resolv 86 | server=127.0.0.1#5353 87 | 88 | 4. Restart chinadns and dnsmasq 89 | 90 | Test if it works correctly: 91 | 92 | $ dig @192.168.1.1 www.youtube.com -p5353 93 | ; <<>> DiG 9.8.3-P1 <<>> @127.0.0.1 www.google.com -p5353 94 | ; (1 server found) 95 | ;; global options: +cmd 96 | ;; Got answer: 97 | ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 29845 98 | ;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0 99 | 100 | ;; QUESTION SECTION: 101 | ;www.youtube.com. IN A 102 | 103 | ;; ANSWER SECTION: 104 | www.youtube.com. 21569 IN CNAME youtube-ui.l.google.com. 105 | youtube-ui.l.google.com. 269 IN A 216.58.220.174 106 | 107 | ;; Query time: 74 msec 108 | ;; SERVER: 127.0.0.1#5353(127.0.0.1) 109 | ;; WHEN: Fri Jan 30 18:37:57 2015 110 | ;; MSG SIZE rcvd: 83 111 | 112 | Currently ChinaDNS only supports UDP. Builtin OpenWRT init script works with 113 | dnsmasq, which handles TCP. If you use it directly without dnsmasq, you need to 114 | add a redirect rule for TCP: 115 | 116 | iptables -t nat -A PREROUTING -p tcp --dport 53 -j DNAT --to-destination 8.8.8.8:53 117 | 118 | Advanced 119 | -------- 120 | 121 | usage: chinadns [-h] [-l IPLIST_FILE] [-b BIND_ADDR] [-p BIND_PORT] 122 | [-c CHNROUTE_FILE] [-s DNS] [-v] 123 | Forward DNS requests. 124 | 125 | -h, --help show this help message and exit 126 | -l IPLIST_FILE path to ip blacklist file 127 | -c CHNROUTE_FILE path to china route file 128 | if not specified, CHNRoute will be turned off 129 | -d enable bi-directional CHNRoute filter 130 | -y delay time for suspects, default: 0.3 131 | -b BIND_ADDR address that listens, default: 127.0.0.1 132 | -p BIND_PORT port that listens, default: 53 133 | -s DNS DNS servers to use, default: 134 | 114.114.114.114,208.67.222.222:443,8.8.8.8 135 | -m Using DNS compression pointer mutation 136 | (backlist and delaying would be disabled) 137 | -v verbose logging 138 | 139 | About chnroute 140 | -------------- 141 | 142 | You can generate latest chnroute.txt using this command: 143 | 144 | curl 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' | grep ipv4 | grep CN | awk -F\| '{ printf("%s/%d\n", $4, 32-log($5)/log(2)) }' > chnroute.txt 145 | 146 | 147 | License 148 | ------- 149 | 150 | Copyright (C) 2015 clowwindy 151 | 152 | This program is free software: you can redistribute it and/or modify 153 | it under the terms of the GNU General Public License as published by 154 | the Free Software Foundation, either version 3 of the License, or 155 | (at your option) any later version. 156 | 157 | This program is distributed in the hope that it will be useful, 158 | but WITHOUT ANY WARRANTY; without even the implied warranty of 159 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 160 | GNU General Public License for more details. 161 | 162 | You should have received a copy of the GNU General Public License 163 | along with this program. If not, see . 164 | 165 | Bugs and Issues 166 | ---------------- 167 | Please visit [Issue Tracker] 168 | 169 | Mailing list: http://groups.google.com/group/shadowsocks 170 | 171 | 172 | [Build Status]: https://travis-ci.org/clowwindy/ChinaDNS.svg?branch=master 173 | [ChinaDNS]: https://github.com/clowwindy/ChinaDNS 174 | [Coverage Status]: https://jenkins.shadowvpn.org/result/chinadns 175 | [Coverage]: https://jenkins.shadowvpn.org/job/ChinaDNS/ws/src/index.html 176 | [Download]: https://github.com/clowwindy/ChinaDNS/releases 177 | [Issue Tracker]: https://github.com/clowwindy/ChinaDNS/issues?state=open 178 | [Download precompiled]: https://github.com/clowwindy/ChinaDNS/releases 179 | [Download a release]: https://github.com/clowwindy/ChinaDNS/releases 180 | [SDK]: http://wiki.openwrt.org/doc/howto/obtain.firmware.sdk 181 | [ShadowVPN]: https://github.com/clowwindy/ShadowVPN 182 | [Tomato toolchain]: http://downloads.linksysbycisco.com/downloads/WRT54GL_v4.30.11_11_US.tgz 183 | [Travis CI]: https://travis-ci.org/clowwindy/ChinaDNS 184 | [DNS pointer mutation method]: https://gist.github.com/klzgrad/f124065c0616022b65e5 185 | -------------------------------------------------------------------------------- /iplist.txt: -------------------------------------------------------------------------------- 1 | 1.1.127.45 2 | 1.1.67.51 3 | 1.2.3.4 4 | 1.209.208.200 5 | 1.226.83.147 6 | 1.234.21.83 7 | 1.234.29.40 8 | 1.234.39.14 9 | 1.234.4.91 10 | 1.234.70.80 11 | 1.234.83.104 12 | 1.244.115.172 13 | 1.33.170.68 14 | 1.33.188.62 15 | 1.33.190.228 16 | 1.33.190.70 17 | 1.33.191.58 18 | 104.200.31.226 19 | 104.28.1.22 20 | 104.28.14.112 21 | 104.28.20.14 22 | 104.28.30.59 23 | 106.186.120.157 24 | 106.187.39.80 25 | 107.6.34.101 26 | 108.168.215.230 27 | 108.168.250.3 28 | 108.179.196.77 29 | 108.179.250.106 30 | 108.61.250.218 31 | 109.123.115.205 32 | 109.206.173.212 33 | 109.234.159.38 34 | 109.71.81.130 35 | 110.173.154.142 36 | 110.74.163.40 37 | 112.175.60.31 38 | 113.11.194.190 39 | 113.160.102.90 40 | 118.145.17.184 41 | 118.219.253.245 42 | 118.5.49.6 43 | 119.18.62.130 44 | 119.235.57.82 45 | 119.245.217.155 46 | 119.9.94.83 47 | 12.87.133.0 48 | 120.89.93.248 49 | 122.214.2.171 50 | 122.218.101.190 51 | 123.126.249.238 52 | 123.30.175.29 53 | 123.50.49.171 54 | 125.230.148.48 55 | 127.0.0.2 56 | 128.121.126.139 57 | 128.199.180.162 58 | 133.192.181.66 59 | 133.242.165.24 60 | 133.42.48.3 61 | 137.135.129.175 62 | 14.102.249.18 63 | 141.101.118.102 64 | 141.8.195.47 65 | 141.8.195.78 66 | 141.8.225.80 67 | 142.4.5.109 68 | 144.76.106.232 69 | 144.76.127.114 70 | 144.76.21.13 71 | 145.253.183.23 72 | 147.87.244.32 73 | 155.92.182.118 74 | 157.205.32.64 75 | 157.7.143.209 76 | 159.106.121.75 77 | 159.253.20.179 78 | 159.50.88.77 79 | 16.63.155.0 80 | 162.159.243.101 81 | 162.243.137.163 82 | 162.253.33.134 83 | 164.109.96.232 84 | 164.138.221.68 85 | 168.156.168.21 86 | 169.132.13.103 87 | 171.17.130.53 88 | 171.25.204.141 89 | 173.192.219.59 90 | 173.194.127.144 91 | 173.201.216.6 92 | 173.224.209.14 93 | 173.236.228.108 94 | 173.244.184.10 95 | 173.255.194.174 96 | 173.255.230.196 97 | 174.142.113.142 98 | 174.142.22.25 99 | 176.10.37.81 100 | 176.57.216.145 101 | 178.18.82.216 102 | 178.236.177.77 103 | 178.32.111.136 104 | 178.32.156.59 105 | 178.32.247.82 106 | 178.33.212.162 107 | 178.49.132.135 108 | 178.62.242.156 109 | 178.62.75.99 110 | 178.79.182.248 111 | 180.153.225.168 112 | 180.179.171.121 113 | 180.87.182.227 114 | 181.224.155.41 115 | 183.111.141.95 116 | 184.154.10.146 117 | 184.169.132.244 118 | 184.72.253.232 119 | 185.25.150.45 120 | 185.53.61.50 121 | 188.132.250.186 122 | 188.165.31.24 123 | 188.226.207.251 124 | 188.40.108.13 125 | 188.5.4.96 126 | 189.163.17.5 127 | 192.104.44.6 128 | 192.121.151.106 129 | 192.67.198.6 130 | 192.95.98.202 131 | 193.105.145.158 132 | 193.169.66.88 133 | 193.203.48.18 134 | 193.234.233.149 135 | 193.238.151.98 136 | 193.239.132.44 137 | 193.48.96.218 138 | 193.57.244.117 139 | 193.91.26.132 140 | 194.149.250.20 141 | 194.185.115.1 142 | 194.187.94.6 143 | 194.67.144.70 144 | 195.146.235.33 145 | 195.149.210.211 146 | 195.154.243.151 147 | 195.191.149.103 148 | 195.2.88.68 149 | 195.211.72.200 150 | 195.43.82.170 151 | 195.49.201.30 152 | 195.50.195.15 153 | 195.74.38.62 154 | 195.74.78.21 155 | 195.77.241.242 156 | 195.8.125.64 157 | 197.4.4.12 158 | 198.143.143.36 159 | 198.57.205.133 160 | 198.57.222.88 161 | 198.58.124.68 162 | 199.167.31.142 163 | 199.21.68.222 164 | 199.79.63.83 165 | 2.1.1.2 166 | 2.187.253.121 167 | 2.228.123.7 168 | 2.228.154.8 169 | 20.139.56.0 170 | 200.229.206.115 171 | 200.98.234.14 172 | 201.77.211.143 173 | 202.106.1.2 174 | 202.181.7.85 175 | 202.218.219.10 176 | 202.6.96.25 177 | 203.113.173.22 178 | 203.133.238.172 179 | 203.161.230.171 180 | 203.199.57.81 181 | 203.98.7.65 182 | 206.108.51.91 183 | 206.113.150.70 184 | 207.12.88.98 185 | 207.126.59.27 186 | 207.140.149.247 187 | 207.58.177.166 188 | 208.109.138.55 189 | 208.109.205.232 190 | 208.112.102.122 191 | 208.43.134.107 192 | 208.43.33.194 193 | 208.56.31.43 194 | 208.73.211.164 195 | 208.86.154.112 196 | 208.93.0.150 197 | 209.116.71.109 198 | 209.126.106.182 199 | 209.141.48.35 200 | 209.145.54.50 201 | 209.188.7.186 202 | 209.204.148.22 203 | 209.220.30.174 204 | 209.235.224.25 205 | 209.36.73.33 206 | 209.43.1.130 207 | 209.56.158.42 208 | 209.62.154.94 209 | 209.85.229.138 210 | 210.175.255.154 211 | 210.209.110.199 212 | 210.230.192.183 213 | 211.43.203.33 214 | 211.5.133.18 215 | 211.8.69.27 216 | 211.94.66.147 217 | 212.227.98.130 218 | 212.45.52.219 219 | 212.68.42.67 220 | 212.77.104.29 221 | 213.108.66.21 222 | 213.133.111.102 223 | 213.169.251.35 224 | 213.174.158.108 225 | 213.186.33.5 226 | 213.19.161.141 227 | 213.207.85.148 228 | 213.238.166.227 229 | 216.12.205.2 230 | 216.139.213.144 231 | 216.178.241.101 232 | 216.198.246.103 233 | 216.221.188.182 234 | 216.234.179.13 235 | 216.250.115.144 236 | 216.38.0.92 237 | 216.70.88.29 238 | 216.92.58.37 239 | 217.160.42.85 240 | 217.172.183.9 241 | 217.30.184.161 242 | 218.44.251.212 243 | 220.110.150.90 244 | 220.247.224.8 245 | 221.213.49.149 246 | 221.8.69.27 247 | 222.122.56.219 248 | 23.23.14.192 249 | 23.89.5.60 250 | 24.51.184.0 251 | 243.185.187.30 252 | 243.185.187.39 253 | 249.129.46.48 254 | 253.157.14.165 255 | 28.121.126.139 256 | 28.13.216.0 257 | 31.169.90.4 258 | 31.170.8.8 259 | 31.210.156.212 260 | 31.22.4.60 261 | 31.222.185.202 262 | 31.25.191.134 263 | 34.254.247.151 264 | 37.1.205.21 265 | 37.1.207.129 266 | 37.140.238.35 267 | 37.187.134.150 268 | 37.187.149.129 269 | 37.187.251.35 270 | 37.252.122.184 271 | 37.58.78.79 272 | 37.59.25.95 273 | 37.61.54.158 274 | 37.99.194.148 275 | 38.117.98.231 276 | 4.17.143.131 277 | 4.193.80.0 278 | 4.21.70.9 279 | 4.30.13.168 280 | 4.30.187.9 281 | 4.30.235.229 282 | 4.31.139.146 283 | 4.34.180.178 284 | 4.35.100.20 285 | 4.35.234.200 286 | 4.36.66.178 287 | 4.53.17.215 288 | 4.59.79.206 289 | 4.78.167.196 290 | 4.79.129.122 291 | 41.79.20.9 292 | 43.253.199.12 293 | 46.137.219.7 294 | 46.165.231.144 295 | 46.20.126.252 296 | 46.20.13.100 297 | 46.229.175.95 298 | 46.243.6.170 299 | 46.30.212.198 300 | 46.38.24.209 301 | 46.82.174.68 302 | 49.2.123.56 303 | 49.212.153.128 304 | 5.10.105.41 305 | 5.10.68.187 306 | 5.10.68.188 307 | 5.10.69.29 308 | 5.10.77.72 309 | 5.100.152.24 310 | 5.100.225.204 311 | 5.100.228.206 312 | 5.100.231.27 313 | 5.100.248.208 314 | 5.144.129.20 315 | 5.35.251.108 316 | 5.9.118.111 317 | 5.9.120.140 318 | 5.9.136.210 319 | 5.9.242.232 320 | 5.9.5.26 321 | 5.9.65.105 322 | 50.116.6.162 323 | 50.18.183.233 324 | 50.57.11.12 325 | 50.63.202.13 326 | 50.87.148.140 327 | 50.87.169.77 328 | 50.93.207.101 329 | 50.97.134.91 330 | 54.174.40.182 331 | 54.187.136.30 332 | 54.187.39.38 333 | 54.191.193.138 334 | 54.200.3.32 335 | 54.206.98.127 336 | 54.209.238.28 337 | 54.209.87.186 338 | 54.218.38.198 339 | 54.229.147.183 340 | 54.235.199.154 341 | 54.244.22.77 342 | 54.246.169.32 343 | 54.246.202.250 344 | 54.68.166.130 345 | 54.76.135.1 346 | 54.83.51.191 347 | 54.86.21.64 348 | 54.86.223.202 349 | 54.88.252.91 350 | 59.124.74.28 351 | 59.24.3.173 352 | 61.54.28.6 353 | 62.138.115.35 354 | 62.75.221.31 355 | 62.92.17.213 356 | 64.14.72.41 357 | 64.150.184.98 358 | 64.22.110.34 359 | 64.33.88.161 360 | 64.33.99.47 361 | 64.34.161.142 362 | 64.50.179.133 363 | 64.66.163.251 364 | 64.79.69.250 365 | 64.79.84.141 366 | 64.91.254.97 367 | 65.104.202.252 368 | 65.160.219.113 369 | 65.183.39.139 370 | 66.146.2.241 371 | 66.187.204.50 372 | 66.206.11.194 373 | 66.39.61.161 374 | 66.45.252.237 375 | 66.55.151.148 376 | 66.85.134.186 377 | 66.96.147.160 378 | 67.137.227.11 379 | 67.225.220.248 380 | 68.71.58.18 381 | 69.16.196.113 382 | 69.167.172.162 383 | 69.171.13.49 384 | 69.174.244.221 385 | 69.175.75.202 386 | 69.195.124.90 387 | 69.30.23.10 388 | 69.50.192.218 389 | 69.61.60.122 390 | 70.42.243.33 391 | 72.14.205.104 392 | 72.14.205.99 393 | 72.167.32.10 394 | 72.20.110.50 395 | 72.29.94.240 396 | 72.32.4.243 397 | 72.47.228.79 398 | 72.5.1.109 399 | 72.52.244.56 400 | 74.117.117.122 401 | 74.117.57.138 402 | 74.124.195.73 403 | 74.125.127.102 404 | 74.125.155.102 405 | 74.125.204.121 406 | 74.125.39.102 407 | 74.125.39.113 408 | 74.207.236.174 409 | 74.208.125.184 410 | 74.220.215.67 411 | 74.82.166.166 412 | 75.98.175.166 413 | 76.164.217.116 414 | 77.4.7.92 415 | 78.108.178.26 416 | 78.140.172.33 417 | 78.16.49.15 418 | 78.24.135.99 419 | 79.127.127.68 420 | 79.136.125.49 421 | 79.98.34.60 422 | 8.105.84.0 423 | 8.34.161.150 424 | 8.7.198.45 425 | 80.190.96.26 426 | 80.241.209.19 427 | 80.241.92.180 428 | 80.245.171.70 429 | 80.70.184.118 430 | 80.72.41.146 431 | 80.82.117.209 432 | 80.82.201.154 433 | 80.92.117.132 434 | 82.145.47.117 435 | 83.125.118.122 436 | 83.222.124.187 437 | 83.222.5.171 438 | 84.124.59.165 439 | 85.111.18.138 440 | 85.190.0.110 441 | 85.25.171.103 442 | 85.92.134.229 443 | 87.106.57.209 444 | 87.230.46.50 445 | 88.198.69.101 446 | 88.214.195.67 447 | 89.108.118.129 448 | 89.111.181.74 449 | 89.186.95.11 450 | 89.30.125.204 451 | 89.31.55.106 452 | 90.156.201.42 453 | 91.121.245.154 454 | 91.186.28.41 455 | 91.198.129.47 456 | 91.217.73.22 457 | 91.221.37.35 458 | 91.223.175.25 459 | 91.238.30.54 460 | 91.239.201.16 461 | 92.53.106.175 462 | 92.53.96.9 463 | 92.63.110.174 464 | 93.115.240.148 465 | 93.158.121.72 466 | 93.187.205.2 467 | 93.46.8.89 468 | 93.93.187.49 469 | 94.136.188.30 470 | 94.141.31.140 471 | 94.23.147.142 472 | 94.23.156.11 473 | 94.23.193.224 474 | 94.23.199.144 475 | 95.163.95.47 476 | 95.211.150.70 477 | 95.211.229.156 478 | 95.211.58.97 479 | 95.85.22.163 480 | 96.126.97.15 481 | 96.127.172.221 482 | 96.30.51.148 483 | 97.74.80.22 484 | 98.129.229.202 485 | 98.158.152.159 486 | 98.158.178.141 487 | -------------------------------------------------------------------------------- /src/chinadns.c: -------------------------------------------------------------------------------- 1 | /* ChinaDNS 2 | Copyright (C) 2015 clowwindy 3 | 4 | This program is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU General Public License for more details. 13 | 14 | You should have received a copy of the GNU General Public License 15 | along with this program. If not, see . 16 | */ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | #include "local_ns_parser.h" 35 | 36 | #include "config.h" 37 | 38 | typedef struct { 39 | uint16_t id; 40 | struct timeval ts; 41 | char *buf; 42 | size_t buflen; 43 | struct sockaddr *addr; 44 | socklen_t addrlen; 45 | } delay_buf_t; 46 | 47 | typedef struct { 48 | uint16_t id; 49 | uint16_t old_id; 50 | struct sockaddr *addr; 51 | socklen_t addrlen; 52 | } id_addr_t; 53 | 54 | typedef struct { 55 | int entries; 56 | struct in_addr *ips; 57 | } ip_list_t; 58 | 59 | typedef struct { 60 | struct in_addr net; 61 | in_addr_t mask; 62 | } net_mask_t; 63 | 64 | typedef struct { 65 | int entries; 66 | net_mask_t *nets; 67 | } net_list_t; 68 | 69 | 70 | // default max EDNS.0 UDP packet from RFC5625 71 | #define BUF_SIZE 4096 72 | static char global_buf[BUF_SIZE]; 73 | static char compression_buf[BUF_SIZE]; 74 | static int verbose = 0; 75 | static int compression = 0; 76 | static int bidirectional = 0; 77 | 78 | #define CHN_DNS 0 79 | #define FOREIGN_DNS 1 80 | #define TRUSTED_DNS 2 81 | static const char *default_dns_servers = 82 | "114.114.114.114,223.5.5.5,8.8.8.8,8.8.4.4,208.67.222.222:443,208.67.222.222:5353"; 83 | static char *dns_servers = NULL; 84 | static int dns_servers_len; 85 | static id_addr_t *dns_server_addrs; 86 | 87 | static int test_dns_server_type(struct sockaddr *addr); 88 | 89 | static int has_trusted_dns; 90 | static ip_list_t trusted_dns_list; 91 | 92 | static int parse_args(int argc, char **argv); 93 | 94 | static int setnonblock(int sock); 95 | 96 | static int resolve_dns_servers(); 97 | 98 | static const char *default_listen_addr = "0.0.0.0"; 99 | static const char *default_listen_port = "53"; 100 | 101 | static char *listen_addr = NULL; 102 | static char *listen_port = NULL; 103 | 104 | static char *ip_list_file = NULL; 105 | static ip_list_t ip_list; 106 | 107 | static int parse_ip_list(); 108 | 109 | #define NETMASK_MIN 0 110 | static char *chnroute_file = NULL; 111 | static net_list_t chnroute_list; 112 | 113 | static int parse_chnroute(); 114 | 115 | static int test_ip_in_list(struct in_addr ip, const net_list_t *netlist); 116 | 117 | static int dns_init_sockets(); 118 | 119 | static void dns_handle_local(); 120 | 121 | static void dns_handle_remote(); 122 | 123 | static const char *hostname_from_question(ns_msg msg); 124 | 125 | static int should_filter_query(ns_msg msg, struct sockaddr *dns_addr); 126 | 127 | static void queue_add(id_addr_t id_addr); 128 | 129 | static id_addr_t *queue_lookup(uint16_t id); 130 | 131 | #define ID_ADDR_QUEUE_LEN 128 132 | // use a queue instead of hash here since it's not long 133 | static id_addr_t id_addr_queue[ID_ADDR_QUEUE_LEN]; 134 | static int id_addr_queue_pos = 0; 135 | 136 | #define EMPTY_RESULT_DELAY 0.3f 137 | #define DELAY_QUEUE_LEN 128 138 | static delay_buf_t delay_queue[DELAY_QUEUE_LEN]; 139 | 140 | static void schedule_delay(uint16_t query_id, const char *buf, size_t buflen, 141 | struct sockaddr *addr, socklen_t addrlen); 142 | 143 | static void check_and_send_delay(); 144 | 145 | static void free_delay(int pos); 146 | 147 | // next position for first, not used 148 | static int delay_queue_first = 0; 149 | // current position for last, used 150 | static int delay_queue_last = 0; 151 | static float empty_result_delay = EMPTY_RESULT_DELAY; 152 | 153 | static int local_sock; 154 | static int remote_sock; 155 | 156 | static void usage(void); 157 | 158 | #define __LOG(o, t, v, s...) do { \ 159 | time_t now; \ 160 | time(&now); \ 161 | char *time_str = ctime(&now); \ 162 | time_str[strlen(time_str) - 1] = '\0'; \ 163 | if (t == 0) { \ 164 | if (stdout != o || verbose) { \ 165 | fprintf(o, "%s ", time_str); \ 166 | fprintf(o, s); \ 167 | fflush(o); \ 168 | } \ 169 | } else if (t == 1) { \ 170 | fprintf(o, "%s %s:%d ", time_str, __FILE__, __LINE__); \ 171 | perror(v); \ 172 | } \ 173 | } while (0) 174 | 175 | #define LOG(s...) __LOG(stdout, 0, "_", s) 176 | #define ERR(s) __LOG(stderr, 1, s, "_") 177 | #define VERR(s...) __LOG(stderr, 0, "_", s) 178 | 179 | #ifdef DEBUG 180 | #define DLOG(s...) LOG(s) 181 | void __gcov_flush(void); 182 | static void gcov_handler(int signum) 183 | { 184 | __gcov_flush(); 185 | exit(1); 186 | } 187 | #else 188 | #define DLOG(s...) 189 | #endif 190 | 191 | int main(int argc, char **argv) { 192 | fd_set readset, errorset; 193 | int max_fd, retval; 194 | 195 | #ifdef DEBUG 196 | signal(SIGTERM, gcov_handler); 197 | #endif 198 | 199 | memset(&id_addr_queue, 0, sizeof(id_addr_queue)); 200 | if (0 != parse_args(argc, argv)) 201 | return EXIT_FAILURE; 202 | if (0 != parse_ip_list()) 203 | return EXIT_FAILURE; 204 | if (0 != parse_chnroute()) 205 | return EXIT_FAILURE; 206 | if (0 != resolve_dns_servers()) 207 | return EXIT_FAILURE; 208 | if (0 != dns_init_sockets()) 209 | return EXIT_FAILURE; 210 | if (!compression) 211 | memset(&delay_queue, 0, sizeof(delay_queue)); 212 | 213 | max_fd = MAX(local_sock, remote_sock) + 1; 214 | while (1) { 215 | FD_ZERO(&readset); 216 | FD_ZERO(&errorset); 217 | FD_SET(local_sock, &readset); 218 | FD_SET(local_sock, &errorset); 219 | FD_SET(remote_sock, &readset); 220 | FD_SET(remote_sock, &errorset); 221 | struct timeval timeout = { 222 | .tv_sec = 0, 223 | .tv_usec = 50 * 1000, 224 | }; 225 | retval = select(max_fd, &readset, NULL, &errorset, &timeout); 226 | if (-1 == retval) { 227 | ERR("select"); 228 | return EXIT_FAILURE; 229 | } 230 | check_and_send_delay(); 231 | if (0 == retval) { 232 | continue; 233 | } 234 | if (FD_ISSET(local_sock, &errorset)) { 235 | // TODO getsockopt(..., SO_ERROR, ...); 236 | VERR("local_sock error\n"); 237 | return EXIT_FAILURE; 238 | } 239 | if (FD_ISSET(remote_sock, &errorset)) { 240 | // TODO getsockopt(..., SO_ERROR, ...); 241 | VERR("remote_sock error\n"); 242 | return EXIT_FAILURE; 243 | } 244 | if (FD_ISSET(local_sock, &readset)) 245 | dns_handle_local(); 246 | if (FD_ISSET(remote_sock, &readset)) 247 | dns_handle_remote(); 248 | } 249 | return EXIT_SUCCESS; 250 | } 251 | 252 | static int setnonblock(int sock) { 253 | int flags; 254 | flags = fcntl(sock, F_GETFL, 0); 255 | if (flags == -1) { 256 | ERR("fcntl"); 257 | return -1; 258 | } 259 | if (-1 == fcntl(sock, F_SETFL, flags | O_NONBLOCK)) { 260 | ERR("fcntl"); 261 | return -1; 262 | } 263 | return 0; 264 | } 265 | 266 | static int parse_args(int argc, char **argv) { 267 | int ch; 268 | while ((ch = getopt(argc, argv, "hb:p:s:l:c:y:dmvV")) != -1) { 269 | switch (ch) { 270 | case 'h': 271 | usage(); 272 | exit(0); 273 | case 'b': 274 | listen_addr = strdup(optarg); 275 | break; 276 | case 'p': 277 | listen_port = strdup(optarg); 278 | break; 279 | case 's': 280 | dns_servers = strdup(optarg); 281 | break; 282 | case 'c': 283 | chnroute_file = strdup(optarg); 284 | break; 285 | case 'l': 286 | ip_list_file = strdup(optarg); 287 | break; 288 | case 'y': 289 | empty_result_delay = atof(optarg); 290 | break; 291 | case 'd': 292 | bidirectional = 1; 293 | break; 294 | case 'm': 295 | compression = 1; 296 | break; 297 | case 'v': 298 | verbose = 1; 299 | break; 300 | case 'V': 301 | printf("ChinaDNS %s\n", PACKAGE_VERSION); 302 | exit(0); 303 | default: 304 | usage(); 305 | exit(1); 306 | } 307 | } 308 | if (dns_servers == NULL) { 309 | dns_servers = strdup(default_dns_servers); 310 | } 311 | if (listen_addr == NULL) { 312 | listen_addr = strdup(default_listen_addr); 313 | } 314 | if (listen_port == NULL) { 315 | listen_port = strdup(default_listen_port); 316 | } 317 | argc -= optind; 318 | argv += optind; 319 | return 0; 320 | } 321 | 322 | static int cmp_in_addr(const void *a, const void *b) { 323 | struct in_addr *ina = (struct in_addr *) a; 324 | struct in_addr *inb = (struct in_addr *) b; 325 | if (ina->s_addr == inb->s_addr) 326 | return 0; 327 | if (ntohl(ina->s_addr) > ntohl(inb->s_addr)) 328 | return 1; 329 | return -1; 330 | } 331 | 332 | static int resolve_dns_servers() { 333 | struct addrinfo hints; 334 | struct addrinfo *addr_ip; 335 | char *token; 336 | int r; 337 | int i = 0; 338 | has_trusted_dns = 0; 339 | int has_chn_dns = 0; 340 | int has_foreign_dns = 0; 341 | trusted_dns_list.entries = 0; 342 | dns_servers_len = 1; 343 | 344 | char *pch = strchr(dns_servers, ','); 345 | while (pch != NULL) { 346 | dns_servers_len++; 347 | pch = strchr(pch + 1, ','); 348 | } 349 | char *tch = strchr(dns_servers, '#'); 350 | while (tch != NULL) { 351 | trusted_dns_list.entries++; 352 | tch = strchr(tch + 1, '#'); 353 | } 354 | dns_server_addrs = calloc(dns_servers_len, sizeof(id_addr_t)); 355 | if (trusted_dns_list.entries) { 356 | trusted_dns_list.ips = calloc(trusted_dns_list.entries, 357 | sizeof(struct in_addr)); 358 | } 359 | 360 | memset(&hints, 0, sizeof(hints)); 361 | hints.ai_family = AF_INET; 362 | hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */ 363 | token = strtok(dns_servers, ","); 364 | while (token) { 365 | char *port; 366 | int is_trusted_dns = 0; 367 | memset(global_buf, 0, BUF_SIZE); 368 | strncpy(global_buf, token, BUF_SIZE - 1); 369 | if ((port = (strrchr(global_buf, '#')))) { 370 | *port = '\0'; 371 | port++; 372 | is_trusted_dns = 1; 373 | } else if ((port = (strrchr(global_buf, ':')))) { 374 | *port = '\0'; 375 | port++; 376 | } else { 377 | port = "53"; 378 | } 379 | if (0 != (r = getaddrinfo(global_buf, port, &hints, &addr_ip))) { 380 | VERR("%s:%s\n", gai_strerror(r), token); 381 | return -1; 382 | } 383 | dns_server_addrs[i].addr = addr_ip->ai_addr; 384 | dns_server_addrs[i++].addrlen = addr_ip->ai_addrlen; 385 | if (is_trusted_dns) { 386 | inet_aton(global_buf, &trusted_dns_list.ips[has_trusted_dns++]); 387 | } 388 | token = strtok(0, ","); 389 | } 390 | 391 | qsort(trusted_dns_list.ips, trusted_dns_list.entries, sizeof(struct in_addr), 392 | cmp_in_addr); 393 | 394 | for (i = 0; i < dns_servers_len; i++) { 395 | switch (test_dns_server_type(dns_server_addrs[i].addr)) { 396 | case CHN_DNS: 397 | has_chn_dns = 1; 398 | break; 399 | case FOREIGN_DNS: 400 | has_foreign_dns = 1; 401 | break; 402 | case TRUSTED_DNS: 403 | compression = 0; 404 | break; 405 | } 406 | } 407 | if (chnroute_file) { 408 | if (has_chn_dns) { 409 | if (compression) { 410 | if (!has_foreign_dns) { 411 | VERR("You should have at least one Chinese DNS and one foreign DNS " 412 | "when using DNS compression pointer mutation\n"); 413 | return -1; 414 | } 415 | } else if (!has_foreign_dns && !has_trusted_dns) { 416 | VERR("You should have at least one Chinese DNS and one trusted DNS or " 417 | "foreign DNS when chnroutes is enabled\n"); 418 | return -1; 419 | } 420 | } 421 | } else { 422 | if (compression) { 423 | VERR( 424 | "Chnroutes is necessary when using DNS compression pointer mutation\n"); 425 | return -1; 426 | } 427 | if (has_trusted_dns) { 428 | VERR("Chnroutes is necessary when specify the trusted DNS\n"); 429 | return -1; 430 | } 431 | } 432 | return 0; 433 | } 434 | 435 | static int test_dns_server_type(struct sockaddr *addr) { 436 | void *r; 437 | r = bsearch(&(((struct sockaddr_in *) addr)->sin_addr), 438 | trusted_dns_list.ips, trusted_dns_list.entries, 439 | sizeof(struct in_addr), cmp_in_addr); 440 | if (r) { 441 | return TRUSTED_DNS; 442 | } else { 443 | if (test_ip_in_list(((struct sockaddr_in *) addr)->sin_addr, 444 | &chnroute_list)) { 445 | return CHN_DNS; 446 | } else { 447 | return FOREIGN_DNS; 448 | } 449 | } 450 | } 451 | 452 | static int parse_ip_list() { 453 | FILE *fp; 454 | char line_buf[32]; 455 | char *line = NULL; 456 | size_t len = sizeof(line_buf); 457 | ssize_t read; 458 | ip_list.entries = 0; 459 | int i = 0; 460 | 461 | if (ip_list_file == NULL) 462 | return 0; 463 | 464 | fp = fopen(ip_list_file, "rb"); 465 | if (fp == NULL) { 466 | ERR("fopen"); 467 | VERR("Can't open ip list: %s\n", ip_list_file); 468 | return -1; 469 | } 470 | while ((line = fgets(line_buf, len, fp))) { 471 | ip_list.entries++; 472 | } 473 | 474 | ip_list.ips = calloc(ip_list.entries, sizeof(struct in_addr)); 475 | if (0 != fseek(fp, 0, SEEK_SET)) { 476 | VERR("fseek"); 477 | return -1; 478 | } 479 | while ((line = fgets(line_buf, len, fp))) { 480 | char *sp_pos; 481 | sp_pos = strchr(line, '\r'); 482 | if (sp_pos) *sp_pos = 0; 483 | sp_pos = strchr(line, '\n'); 484 | if (sp_pos) *sp_pos = 0; 485 | inet_aton(line, &ip_list.ips[i]); 486 | i++; 487 | } 488 | 489 | qsort(ip_list.ips, ip_list.entries, sizeof(struct in_addr), cmp_in_addr); 490 | fclose(fp); 491 | return 0; 492 | } 493 | 494 | static int cmp_net_mask(const void *a, const void *b) { 495 | net_mask_t *neta = (net_mask_t *) a; 496 | net_mask_t *netb = (net_mask_t *) b; 497 | if (neta->net.s_addr == netb->net.s_addr) 498 | return 0; 499 | if (ntohl(neta->net.s_addr) > ntohl(netb->net.s_addr)) 500 | return 1; 501 | return -1; 502 | } 503 | 504 | static int parse_chnroute() { 505 | FILE *fp; 506 | char line_buf[32]; 507 | char *line; 508 | size_t len = sizeof(line_buf); 509 | ssize_t read; 510 | char net[32]; 511 | chnroute_list.entries = 0; 512 | int i = 0; 513 | int cidr; 514 | 515 | if (chnroute_file == NULL) { 516 | VERR("CHNROUTE_FILE not specified, CHNRoute is disabled\n"); 517 | return 0; 518 | } 519 | 520 | fp = fopen(chnroute_file, "rb"); 521 | if (fp == NULL) { 522 | ERR("fopen"); 523 | VERR("Can't open chnroute: %s\n", chnroute_file); 524 | return -1; 525 | } 526 | while ((line = fgets(line_buf, len, fp))) { 527 | chnroute_list.entries++; 528 | } 529 | 530 | chnroute_list.nets = calloc(chnroute_list.entries, sizeof(net_mask_t)); 531 | if (0 != fseek(fp, 0, SEEK_SET)) { 532 | VERR("fseek"); 533 | return -1; 534 | } 535 | while ((line = fgets(line_buf, len, fp))) { 536 | char *sp_pos; 537 | sp_pos = strchr(line, '\r'); 538 | if (sp_pos) *sp_pos = 0; 539 | sp_pos = strchr(line, '\n'); 540 | if (sp_pos) *sp_pos = 0; 541 | sp_pos = strchr(line, '/'); 542 | if (sp_pos) { 543 | *sp_pos = 0; 544 | cidr = atoi(sp_pos + 1); 545 | if (cidr > 0) { 546 | chnroute_list.nets[i].mask = (1 << (32 - cidr)) - 1; 547 | } else { 548 | chnroute_list.nets[i].mask = UINT32_MAX; 549 | } 550 | } else { 551 | chnroute_list.nets[i].mask = NETMASK_MIN; 552 | } 553 | if (0 == inet_aton(line, &chnroute_list.nets[i].net)) { 554 | VERR("invalid addr %s in %s:%d\n", line, chnroute_file, i + 1); 555 | return 1; 556 | } 557 | i++; 558 | } 559 | 560 | qsort(chnroute_list.nets, chnroute_list.entries, sizeof(net_mask_t), 561 | cmp_net_mask); 562 | 563 | fclose(fp); 564 | return 0; 565 | } 566 | 567 | static int test_ip_in_list(struct in_addr ip, const net_list_t *netlist) { 568 | // binary search 569 | int l = 0, r = netlist->entries - 1; 570 | int m, cmp; 571 | if (netlist->entries == 0) 572 | return 0; 573 | net_mask_t ip_net; 574 | ip_net.net = ip; 575 | while (l <= r) { 576 | m = (l + r) >> 1; 577 | cmp = cmp_net_mask(&netlist->nets[m], &ip_net); 578 | if (cmp < 0) 579 | l = m + 1; 580 | else if (cmp > 0) 581 | r = m - 1; 582 | else 583 | return 1; 584 | #ifdef DEBUG 585 | DLOG("l=%d, r=%d\n", l, r); 586 | DLOG("%s, %d\n", inet_ntoa(netlist->nets[m].net), netlist->nets[m].mask); 587 | #endif 588 | } 589 | #ifdef DEBUG 590 | DLOG("nets: %x <-> %x\n", ntohl(netlist->nets[l - 1].net.s_addr, ntohl(ip.s_addr)); 591 | DLOG("mask: %x\n", netlist->nets[l - 1].mask); 592 | #endif 593 | if (0 == l || (ntohl(ip.s_addr) > (ntohl(netlist->nets[l - 1].net.s_addr) 594 | | netlist->nets[l - 1].mask))) { 595 | return 0; 596 | } 597 | return 1; 598 | } 599 | 600 | static int dns_init_sockets() { 601 | struct addrinfo hints; 602 | struct addrinfo *addr_ip; 603 | int r; 604 | 605 | local_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 606 | if (0 != setnonblock(local_sock)) 607 | return -1; 608 | memset(&hints, 0, sizeof(hints)); 609 | hints.ai_family = AF_INET; 610 | hints.ai_socktype = SOCK_DGRAM; 611 | if (0 != (r = getaddrinfo(listen_addr, listen_port, &hints, &addr_ip))) { 612 | VERR("%s:%s:%s\n", gai_strerror(r), listen_addr, listen_port); 613 | return -1; 614 | } 615 | if (0 != bind(local_sock, addr_ip->ai_addr, addr_ip->ai_addrlen)) { 616 | ERR("bind"); 617 | VERR("Can't bind address %s:%s\n", listen_addr, listen_port); 618 | return -1; 619 | } 620 | freeaddrinfo(addr_ip); 621 | remote_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 622 | if (0 != setnonblock(remote_sock)) 623 | return -1; 624 | return 0; 625 | } 626 | 627 | static int send_request(id_addr_t id_addr, char *buf, ssize_t len) { 628 | if (verbose) 629 | printf(" %s:%d", inet_ntoa(((struct sockaddr_in *) id_addr.addr)->sin_addr), 630 | htons(((struct sockaddr_in *) id_addr.addr)->sin_port)); 631 | if (-1 == sendto(remote_sock, buf, len, 0, id_addr.addr, id_addr.addrlen)) 632 | ERR("sendto"); 633 | } 634 | 635 | static void dns_handle_local() { 636 | struct sockaddr *src_addr = malloc(sizeof(struct sockaddr)); 637 | socklen_t src_addrlen = sizeof(struct sockaddr); 638 | uint16_t query_id; 639 | ssize_t len; 640 | int i; 641 | int ended = 0; 642 | const char *question_hostname; 643 | ns_msg msg; 644 | len = recvfrom(local_sock, global_buf, BUF_SIZE, 0, src_addr, &src_addrlen); 645 | if (len > 0) { 646 | if (local_ns_initparse((const u_char *) global_buf, len, &msg) < 0) { 647 | ERR("local_ns_initparse"); 648 | free(src_addr); 649 | return; 650 | } 651 | // parse DNS query id 652 | // TODO generate id for each request to avoid conflicts 653 | query_id = ns_msg_id(msg); 654 | question_hostname = hostname_from_question(msg); 655 | LOG("request %s from", question_hostname); 656 | 657 | // assign a new id 658 | uint16_t new_id; 659 | do { 660 | struct timeval tv; 661 | gettimeofday(&tv, 0); 662 | int randombits = (tv.tv_sec << 8) ^tv.tv_usec; 663 | new_id = randombits & 0xffff; 664 | } while (queue_lookup(new_id)); 665 | 666 | uint16_t ns_new_id = htons(new_id); 667 | memcpy(global_buf, &ns_new_id, 2); 668 | 669 | id_addr_t id_addr; 670 | id_addr.id = new_id; 671 | id_addr.old_id = query_id; 672 | id_addr.addr = src_addr; 673 | id_addr.addrlen = src_addrlen; 674 | queue_add(id_addr); 675 | 676 | if (compression && len > 16) { 677 | size_t off = 12; 678 | while (off < len - 4) { 679 | if (global_buf[off] & 0xc0) 680 | break; 681 | if (global_buf[off] == 0) { 682 | ended = 1; 683 | off++; 684 | break; 685 | } 686 | off += 1 + global_buf[off]; 687 | } 688 | if (ended) { 689 | memcpy(compression_buf, global_buf, off - 1); 690 | memcpy(compression_buf + off + 1, global_buf + off, len - off); 691 | compression_buf[off - 1] = '\xc0'; 692 | compression_buf[off] = '\x04'; 693 | } 694 | } 695 | for (i = 0; i < dns_servers_len; i++) { 696 | switch (test_dns_server_type(dns_server_addrs[i].addr)) { 697 | case CHN_DNS: 698 | case TRUSTED_DNS: 699 | send_request(dns_server_addrs[i], global_buf, len); 700 | break; 701 | case FOREIGN_DNS: 702 | if (ended) 703 | send_request(dns_server_addrs[i], compression_buf, len + 1); 704 | else if (!has_trusted_dns) 705 | send_request(dns_server_addrs[i], global_buf, len); 706 | break; 707 | } 708 | } 709 | if (verbose) 710 | printf("\n"); 711 | } else 712 | ERR("recvfrom"); 713 | } 714 | 715 | static void dns_handle_remote() { 716 | struct sockaddr *src_addr = malloc(sizeof(struct sockaddr)); 717 | socklen_t src_len = sizeof(struct sockaddr); 718 | uint16_t query_id; 719 | ssize_t len; 720 | const char *question_hostname; 721 | int r; 722 | ns_msg msg; 723 | len = recvfrom(remote_sock, global_buf, BUF_SIZE, 0, src_addr, &src_len); 724 | if (len > 0) { 725 | if (local_ns_initparse((const u_char *) global_buf, len, &msg) < 0) { 726 | ERR("local_ns_initparse"); 727 | free(src_addr); 728 | return; 729 | } 730 | // parse DNS query id 731 | query_id = ns_msg_id(msg); 732 | question_hostname = hostname_from_question(msg); 733 | if (question_hostname) { 734 | LOG("response %s from %s:%d - ", question_hostname, 735 | inet_ntoa(((struct sockaddr_in *) src_addr)->sin_addr), 736 | htons(((struct sockaddr_in *) src_addr)->sin_port)); 737 | } 738 | id_addr_t *id_addr = queue_lookup(query_id); 739 | if (id_addr) { 740 | id_addr->addr->sa_family = AF_INET; 741 | uint16_t ns_old_id = htons(id_addr->old_id); 742 | memcpy(global_buf, &ns_old_id, 2); 743 | r = should_filter_query(msg, src_addr); 744 | if (r == 0) { 745 | if (verbose) 746 | printf("pass\n"); 747 | if (-1 == sendto(local_sock, global_buf, len, 0, id_addr->addr, 748 | id_addr->addrlen)) 749 | ERR("sendto"); 750 | } else if (r == -1) { 751 | schedule_delay(query_id, global_buf, len, id_addr->addr, 752 | id_addr->addrlen); 753 | if (verbose) 754 | printf("delay\n"); 755 | } else { 756 | if (verbose) 757 | printf("filter\n"); 758 | } 759 | } else { 760 | if (verbose) 761 | printf("skip\n"); 762 | } 763 | free(src_addr); 764 | } else 765 | ERR("recvfrom"); 766 | } 767 | 768 | static void queue_add(id_addr_t id_addr) { 769 | id_addr_queue_pos = (id_addr_queue_pos + 1) % ID_ADDR_QUEUE_LEN; 770 | // free next hole 771 | id_addr_t old_id_addr = id_addr_queue[id_addr_queue_pos]; 772 | free(old_id_addr.addr); 773 | id_addr_queue[id_addr_queue_pos] = id_addr; 774 | } 775 | 776 | static id_addr_t *queue_lookup(uint16_t id) { 777 | int i; 778 | for (i = 0; i < ID_ADDR_QUEUE_LEN; i++) { 779 | if (id_addr_queue[i].id == id) 780 | return id_addr_queue + i; 781 | } 782 | return NULL; 783 | } 784 | 785 | static char *hostname_buf = NULL; 786 | static size_t hostname_buflen = 0; 787 | 788 | static const char *hostname_from_question(ns_msg msg) { 789 | ns_rr rr; 790 | int rrnum, rrmax; 791 | const char *result; 792 | int result_len; 793 | rrmax = ns_msg_count(msg, ns_s_qd); 794 | if (rrmax == 0) 795 | return NULL; 796 | for (rrnum = 0; rrnum < rrmax; rrnum++) { 797 | if (local_ns_parserr(&msg, ns_s_qd, rrnum, &rr)) { 798 | ERR("local_ns_parserr"); 799 | return NULL; 800 | } 801 | result = ns_rr_name(rr); 802 | result_len = strlen(result) + 1; 803 | if (result_len > hostname_buflen) { 804 | hostname_buflen = result_len << 1; 805 | hostname_buf = realloc(hostname_buf, hostname_buflen); 806 | } 807 | memcpy(hostname_buf, result, result_len); 808 | return hostname_buf; 809 | } 810 | return NULL; 811 | } 812 | 813 | static int should_filter_query(ns_msg msg, struct sockaddr *dns_addr) { 814 | ns_rr rr; 815 | int rrnum, rrmax; 816 | int ns_t_a_num = 0; 817 | void *r; 818 | // TODO cache result for each dns server 819 | int dns_is_chn = 0; 820 | int dns_is_foreign = 0; 821 | if (chnroute_file && (dns_servers_len > 1)) { 822 | dns_is_chn = (CHN_DNS == test_dns_server_type(dns_addr)); 823 | dns_is_foreign = !dns_is_chn; 824 | } 825 | rrmax = ns_msg_count(msg, ns_s_an); 826 | for (rrnum = 0; rrnum < rrmax; rrnum++) { 827 | if (local_ns_parserr(&msg, ns_s_an, rrnum, &rr)) { 828 | ERR("local_ns_parserr"); 829 | return 0; 830 | } 831 | u_int type; 832 | type = ns_rr_type(rr); 833 | if (type == ns_t_a) { 834 | ns_t_a_num++; 835 | const u_char *rd; 836 | rd = ns_rr_rdata(rr); 837 | if (verbose) 838 | printf("%s, ", inet_ntoa(*(struct in_addr *) rd)); 839 | if (!compression && !has_trusted_dns) { 840 | r = bsearch(rd, ip_list.ips, ip_list.entries, sizeof(struct in_addr), 841 | cmp_in_addr); 842 | if (r) { 843 | return 1; 844 | } 845 | } 846 | if (test_ip_in_list(*(struct in_addr *) rd, &chnroute_list)) { 847 | // result is chn 848 | if (dns_is_foreign) { 849 | if (bidirectional) { 850 | // filter DNS result from foreign dns if result is inside chn 851 | return 1; 852 | } 853 | } 854 | } else { 855 | // result is foreign 856 | if (dns_is_chn) { 857 | // filter DNS result from chn dns if result is outside chn 858 | return 1; 859 | } 860 | } 861 | } else if (type == ns_t_aaaa || type == ns_t_ptr) { 862 | // if we've got an IPv6 result or a PTR result, pass 863 | return 0; 864 | } 865 | } 866 | if (ns_t_a_num == 0) { 867 | if (compression || has_trusted_dns) { 868 | // Wait for foreign dns 869 | if (dns_is_chn) { 870 | return 1; 871 | } else { 872 | return 0; 873 | } 874 | } 875 | return -1; 876 | } 877 | return 0; 878 | } 879 | 880 | static void schedule_delay(uint16_t query_id, const char *buf, size_t buflen, 881 | struct sockaddr *addr, socklen_t addrlen) { 882 | int i; 883 | int found = 0; 884 | struct timeval now; 885 | gettimeofday(&now, 0); 886 | 887 | delay_buf_t *delay_buf = &delay_queue[delay_queue_last]; 888 | 889 | // first search for existed item with query_id and replace it 890 | for (i = delay_queue_first; 891 | i != delay_queue_last; 892 | i = (i + 1) % DELAY_QUEUE_LEN) { 893 | delay_buf_t *delay_buf2 = &delay_queue[i]; 894 | if (delay_buf2->id == query_id) { 895 | free_delay(i); 896 | delay_buf = &delay_queue[i]; 897 | found = 1; 898 | } 899 | } 900 | 901 | delay_buf->id = query_id; 902 | delay_buf->ts = now; 903 | delay_buf->buf = malloc(buflen); 904 | memcpy(delay_buf->buf, buf, buflen); 905 | delay_buf->buflen = buflen; 906 | delay_buf->addr = malloc(addrlen); 907 | memcpy(delay_buf->addr, addr, addrlen); 908 | delay_buf->addrlen = addrlen; 909 | 910 | // then append to queue 911 | if (!found) { 912 | delay_queue_last = (delay_queue_last + 1) % DELAY_QUEUE_LEN; 913 | if (delay_queue_last == delay_queue_first) { 914 | free_delay(delay_queue_first); 915 | delay_queue_first = (delay_queue_first + 1) % DELAY_QUEUE_LEN; 916 | } 917 | } 918 | } 919 | 920 | float time_diff(struct timeval t0, struct timeval t1) { 921 | return (t1.tv_sec - t0.tv_sec) + (t1.tv_usec - t0.tv_usec) / 1000000.0f; 922 | } 923 | 924 | static void check_and_send_delay() { 925 | struct timeval now; 926 | int i; 927 | gettimeofday(&now, 0); 928 | for (i = delay_queue_first; 929 | i != delay_queue_last; 930 | i = (i + 1) % DELAY_QUEUE_LEN) { 931 | delay_buf_t *delay_buf = &delay_queue[i]; 932 | if (time_diff(delay_buf->ts, now) > empty_result_delay) { 933 | if (-1 == sendto(local_sock, delay_buf->buf, delay_buf->buflen, 0, 934 | delay_buf->addr, delay_buf->addrlen)) 935 | ERR("sendto"); 936 | free_delay(i); 937 | delay_queue_first = (delay_queue_first + 1) % DELAY_QUEUE_LEN; 938 | } else { 939 | break; 940 | } 941 | } 942 | } 943 | 944 | static void free_delay(int pos) { 945 | free(delay_queue[pos].buf); 946 | free(delay_queue[pos].addr); 947 | } 948 | 949 | static void usage() { 950 | printf("%s\n", "\ 951 | usage: chinadns [-h] [-l IPLIST_FILE] [-b BIND_ADDR] [-p BIND_PORT]\n\ 952 | [-c CHNROUTE_FILE] [-s DNS] [-m] [-v] [-V]\n\ 953 | Forward DNS requests.\n\ 954 | \n\ 955 | -l IPLIST_FILE path to ip blacklist file\n\ 956 | -c CHNROUTE_FILE path to china route file\n\ 957 | if not specified, CHNRoute will be turned\n\ 958 | -d off enable bi-directional CHNRoute filter\n\ 959 | -y delay time for suspects, default: 0.3\n\ 960 | -b BIND_ADDR address that listens, default: 0.0.0.0\n\ 961 | -p BIND_PORT port that listens, default: 53\n\ 962 | -s DNS DNS servers to use, default:\n\ 963 | 114.114.114.114,208.67.222.222:443,8.8.8.8\n\ 964 | -m use DNS compression pointer mutation\n\ 965 | (backlist and delaying would be disabled)\n\ 966 | -v verbose logging\n\ 967 | -h show this help message and exit\n\ 968 | -V print version and exit\n\ 969 | \n\ 970 | Online help: \n"); 971 | } 972 | 973 | 974 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------