diff --git a/include/common.h b/include/common.h index 7393511..532c8ef 100644 --- a/include/common.h +++ b/include/common.h @@ -3,11 +3,22 @@ #define VERSION "1.3.0-dev" +#define TRUE 1 +#define FALSE 0 + +#define DOMESTIC_PORT 4053 +#define FOREIGN_PORT 6053 + +#define DNSPROXY_BIN "dnsproxy" +#define OVERTURE_BIN "overture" +#define ADGUARD_BIN "AdGuardHome" + + //extern char **adguard_command; //extern char **overture_command; //extern char **domestic_dnsproxy_command; //extern char **foreign_dnsproxy_command; -// + //void load_start_command(char *adguard_workdir, char *overture_config, char *upstream_config, int is_debug); #endif diff --git a/include/dnsproxy.h b/include/dnsproxy.h new file mode 100644 index 0000000..286446f --- /dev/null +++ b/include/dnsproxy.h @@ -0,0 +1,18 @@ +#ifndef _DNSPROXY_H_ +#define _DNSPROXY_H_ + +typedef struct { + int port; + int verify; // bool value + int parallel; // bool value + int optimistic; // bool value + char **bootstrap; + char **fallback; + char **primary; + char *cache; +} dnsproxy; + +dnsproxy* dnsproxy_init(int port); +void dnsproxy_dump(char *caption, dnsproxy *info); + +#endif diff --git a/include/utils/logger.h b/include/utils/logger.h index 48ecc04..e731e59 100644 --- a/include/utils/logger.h +++ b/include/utils/logger.h @@ -1,5 +1,5 @@ -#ifndef _LOG_H -#define _LOG_H +#ifndef _LOGGER_H_ +#define _LOGGER_H_ enum { LOG_DEBUG, diff --git a/include/utils/strList.h b/include/utils/strList.h new file mode 100644 index 0000000..02ede62 --- /dev/null +++ b/include/utils/strList.h @@ -0,0 +1,11 @@ +#ifndef _STRLIST_H_ +#define _STRLIST_H_ + +char* string_init(char *str); + +char** string_list_init(); +int string_list_len(char **string_list); +char* string_list_dump(char **string_list); +char** string_list_append(char **string_list, char *string); + +#endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d303d47..a2d0262 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,5 +5,5 @@ include_directories(${PROJECT_SOURCE_DIR}/include/utils) add_subdirectory(utils) -add_executable(cleardns cleardns.c) +add_executable(cleardns cleardns.c dnsproxy.c) target_link_libraries(cleardns utils) diff --git a/src/cleardns.c b/src/cleardns.c index 6cef540..01b7c43 100644 --- a/src/cleardns.c +++ b/src/cleardns.c @@ -1,5 +1,6 @@ #include "logger.h" #include "common.h" +#include "dnsproxy.h" //#include //#include @@ -27,8 +28,13 @@ int main(int argc, char *argv[]) { // ClearDNS server + LOG_LEVEL = LOG_DEBUG; + log_info("ClearDNS server start (%s)", VERSION); + dnsproxy *domestic = dnsproxy_init(DOMESTIC_PORT); + dnsproxy_dump("Domestic", domestic); + // int debug_mode = 0; // fprintf(stderr, "[ClearDNS] Server start.\n"); // for (char **p = argv; p < argv + argc; ++p) { diff --git a/src/common.c b/src/common.c index d1021d7..0803a4e 100644 --- a/src/common.c +++ b/src/common.c @@ -1,6 +1,6 @@ #include #include -#include +#include #include "cJSON.h" #include "flag.h" diff --git a/src/dnsproxy.c b/src/dnsproxy.c new file mode 100644 index 0000000..e4b9fdd --- /dev/null +++ b/src/dnsproxy.c @@ -0,0 +1,54 @@ +#include +#include "dnsproxy.h" +#include "logger.h" +#include "common.h" +#include "strList.h" + +char* show_bool(int value) { + if (value) { + return "true"; + } else { + return "false"; + } +} + +char** command_init(char *file) { + return string_list_append(string_list_init(), file); +} + +dnsproxy* dnsproxy_init(int port) { + dnsproxy *info = (dnsproxy*)malloc(sizeof(dnsproxy)); + info->port = port; + info->verify = TRUE; + info->parallel = TRUE; + info->optimistic = FALSE; + info->bootstrap = string_list_init(); + info->fallback = string_list_init(); + info->primary = string_list_init(); + info->cache = "4194304"; // 4MiB + return info; +} + +void dnsproxy_dump(char *caption, dnsproxy *info) { + log_debug("%s listen port -> %d", caption, info->port); + log_debug("%s verify -> %s", caption, show_bool(info->verify)); + log_debug("%s parallel -> %s", caption, show_bool(info->parallel)); + log_debug("%s optimistic -> %s", caption, show_bool(info->optimistic)); + + log_debug("%s bootstrap -> %s", caption, string_list_dump(info->bootstrap)); + log_debug("%s fallback -> %s", caption, string_list_dump(info->fallback)); + log_debug("%s primary -> %s", caption, string_list_dump(info->primary)); + + log_debug("%s cache -> %s", caption, info->cache); + + char **test = string_list_init(); + test = string_list_append(test, "0"); + test = string_list_append(test, "1"); + test = string_list_append(test, "2"); + +// char *test[] = {NULL}; +// char *test[] = {"dnomd343", "2333", NULL}; + log_warn("%s", string_list_dump(test)); + log_warn("%d", string_list_len(test)); + +} diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index cc3bdc1..b82db9b 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -1,3 +1,3 @@ cmake_minimum_required(VERSION 2.8.12) -add_library(utils cJSON.c logger.c) +add_library(utils logger.c strList.c) diff --git a/src/utils/cJSON.c b/src/utils/cJSON.c index 3063f74..e40e4bb 100644 --- a/src/utils/cJSON.c +++ b/src/utils/cJSON.c @@ -37,7 +37,7 @@ #pragma warning (disable : 4001) #endif -#include +#include #include #include #include diff --git a/src/utils/strList.c b/src/utils/strList.c new file mode 100644 index 0000000..a2ee359 --- /dev/null +++ b/src/utils/strList.c @@ -0,0 +1,47 @@ +#include +#include +#include "strList.h" + +char* string_init(char *str) { + return strcpy((char*)malloc(strlen(str) + 1), str); +} + +char* string_list_dump(char **string_list) { // ['a', 'b', 'c', ...] + unsigned long string_len = 0; + for (char **string = string_list; *string != NULL; ++string) { + string_len += strlen(*string) + 4; + } + if (string_len == 0) { // empty string + string_len = 2; + } + char *string_ret = (char *)malloc(sizeof(char) * (string_len + 1)); + string_ret[0] = '['; + string_ret[1] = 0x00; + for (char **string = string_list; *string != NULL; ++string) { + string_ret = strcat(strcat(string_ret, "'"), *string); + string_ret = strcat(string_ret, "', "); + } + string_ret[string_len - 1] = ']'; + string_ret[string_len] = 0x00; + return string_ret; +} + +int string_list_len(char **string_list) { + int num = 0; + while(string_list[num++] != NULL); // get string list size + return num - 1; +} + +char** string_list_init() { + char **string_list = (char**)malloc(sizeof(char*)); + *string_list = NULL; + return string_list; +} + +char** string_list_append(char **string_list, char *string) { + int len = string_list_len(string_list); + string_list = (char**)realloc(string_list, sizeof(char**) * (len + 2)); + string_list[len] = string_init(string); + string_list[len + 1] = NULL; // list end sign + return string_list; +}