Browse Source

update: enhance structure module

dev
dnomd343 2 years ago
parent
commit
3232f2ffdb
  1. 8
      include/common.h
  2. 15
      include/dnsproxy.h
  3. 15
      include/overture.h
  4. 12
      include/utils/structure.h
  5. 42
      src/cleardns.c
  6. 26
      src/common.c
  7. 2
      src/dnsproxy.c
  8. 10
      src/overture.c
  9. 101
      src/utils/structure.c

8
include/common.h

@ -1,6 +1,13 @@
#ifndef _COMMON_H_
#define _COMMON_H_
#include <stdlib.h>
typedef u_int8_t uint8_t;
typedef u_int16_t uint16_t;
typedef u_int32_t uint32_t;
typedef u_int64_t uint64_t;
#define VERSION "1.3.0-dev"
#define TRUE 1
@ -24,6 +31,7 @@
//void load_start_command(char *adguard_workdir, char *overture_config, char *upstream_config, int is_debug);
char* show_bool(int value);
char* read_file(char *file);
void save_file(char *file, char *content);
#endif

15
include/dnsproxy.h

@ -2,20 +2,21 @@
#define _DNSPROXY_H_
#include "process.h"
#include "common.h"
typedef struct {
int port;
int cache;
int debug; // bool value
int verify; // bool value
int parallel; // bool value
int optimistic; // bool value
uint16_t port;
uint32_t cache;
uint8_t debug; // bool value
uint8_t verify; // bool value
uint8_t parallel; // bool value
uint8_t optimistic; // bool value
char **bootstrap;
char **fallback;
char **primary;
} dnsproxy;
dnsproxy* dnsproxy_init(int port);
dnsproxy* dnsproxy_init(uint16_t port);
void dnsproxy_add_primary(dnsproxy *info, const char *server);
void dnsproxy_add_fallback(dnsproxy *info, const char *server);
void dnsproxy_add_bootstrap(dnsproxy *info, const char *server);

15
include/overture.h

@ -2,23 +2,24 @@
#define _OVERTURE_H_
#include "process.h"
#include "common.h"
typedef struct {
int port;
int debug;
int timeout;
uint16_t port;
uint8_t debug; // bool value
uint32_t timeout;
char *ttl_file;
char *host_file;
int foreign_port;
int domestic_port;
int **reject_type;
uint16_t foreign_port;
uint16_t domestic_port;
uint32_t **reject_type;
char *foreign_ip_file;
char *domestic_ip_file;
char *foreign_domain_file;
char *domestic_domain_file;
} overture;
overture* overture_init(int port);
overture* overture_init(uint16_t port);
process* overture_load(overture *info, const char *file);
#endif

12
include/utils/structure.h

@ -1,16 +1,18 @@
#ifndef _STR_H_
#define _STR_H_
#include "common.h"
char* string_init(const char *str);
char* string_join(const char *base, const char *add);
int** int_list_init();
int int_list_len(int **int_list);
char* int_list_dump(int **int_list);
int** int_list_append(int **int_list, int number);
uint32_t** uint32_list_init();
uint32_t uint32_list_len(uint32_t **int_list);
char* uint32_list_dump(uint32_t **int_list);
uint32_t** uint32_list_append(uint32_t **int_list, uint32_t number);
char** string_list_init();
int string_list_len(char **string_list);
uint32_t string_list_len(char **string_list);
char* string_list_dump(char **string_list);
char** string_list_append(char **string_list, const char *string);

42
src/cleardns.c

@ -36,6 +36,16 @@ int main(int argc, char *argv[]) { // ClearDNS server
log_info("ClearDNS server start (%s)", VERSION);
char **temp = string_list_init();
// temp = string_list_append(temp, "123");
// temp = string_list_append(temp, "abc");
// temp = string_list_append(temp, "ok");
char *str = string_list_dump(temp);
log_info("`%s`", str);
free(str);
// dnsproxy *domestic = dnsproxy_init(DOMESTIC_PORT);
//
// dnsproxy_add_bootstrap(domestic, "1.1.1.1");
@ -59,22 +69,22 @@ int main(int argc, char *argv[]) { // ClearDNS server
// log_info("cwd -> %s", p->cwd);
overture *diverter = overture_init(DIVERTER_PORT);
diverter->timeout = 8;
diverter->domestic_ip_file = "china-ip.txt";
diverter->domestic_domain_file = "chinalist.txt";
diverter->foreign_domain_file = "gfwlist.txt";
diverter->debug = TRUE;
diverter->ttl_file = "domain_ttl.txt";
diverter->host_file = "hosts.txt";
diverter->reject_type = int_list_append(diverter->reject_type, 255);
process *p = overture_load(diverter, "overture.json");
log_info("cmd -> %s", string_list_dump(p->cmd));
log_info("env -> %s", string_list_dump(p->env));
log_info("cwd -> %s", p->cwd);
// overture *diverter = overture_init(DIVERTER_PORT);
//
// diverter->timeout = 8;
// diverter->domestic_ip_file = "china-ip.txt";
// diverter->domestic_domain_file = "chinalist.txt";
// diverter->foreign_domain_file = "gfwlist.txt";
//
// diverter->debug = TRUE;
// diverter->ttl_file = "domain_ttl.txt";
// diverter->host_file = "hosts.txt";
// diverter->reject_type = int_list_append(diverter->reject_type, 255);
//
// process *p = overture_load(diverter, "overture.json");
// log_info("cmd -> %s", string_list_dump(p->cmd));
// log_info("env -> %s", string_list_dump(p->env));
// log_info("cwd -> %s", p->cwd);
// int debug_mode = 0;

26
src/common.c

@ -1,9 +1,9 @@
#include <stdio.h>
#include <stdlib.h>
#include "common.h"
#include "logger.h"
#include "structure.h"
char* show_bool(int value) {
char* show_bool(int value) { // return `true` or `false`
if (value) {
return "true";
} else {
@ -11,7 +11,7 @@ char* show_bool(int value) {
}
}
void save_file(char *file, char *content) {
void save_file(char *file, char *content) { // save into file
log_debug("Write into `%s` -> \n%s", file, content);
FILE* fp = fopen(file , "w");
if (fp == NULL) {
@ -21,3 +21,23 @@ void save_file(char *file, char *content) {
fclose(fp);
log_debug("Save `%s` success", file);
}
char* read_file(char *file) { // read file content
log_debug("Read file -> %s", file);
FILE *fp = fopen(file, "rb");
if (fp == NULL) { // file open failed
log_fatal("File `%s` open failed", file);
}
fseek(fp, 0, SEEK_END);
long length = ftell(fp); // get file length
char *content = (char*)malloc(length + 1); // malloc new memory
if (content == NULL) {
log_fatal("No enough memory for reading file"); // file too large
}
rewind(fp);
fread(content, 1, length, fp); // read file stream
content[length] = '\0'; // set end flag
fclose(fp);
log_debug("File `%s` read success ->\n%s", file, content);
return content;
}

2
src/dnsproxy.c

@ -21,7 +21,7 @@ void dnsproxy_add_bootstrap(dnsproxy *info, const char *server) { // add bootstr
info->bootstrap = string_list_append(info->bootstrap, server);
}
dnsproxy* dnsproxy_init(int port) { // init dnsproxy options
dnsproxy* dnsproxy_init(uint16_t port) { // init dnsproxy options
dnsproxy *info = (dnsproxy*)malloc(sizeof(dnsproxy));
info->port = port;
info->cache = 0; // disable cache in default

10
src/overture.c

@ -10,7 +10,7 @@
void overture_dump(overture *info);
char* overture_gen_config(overture *info);
overture* overture_init(int port) { // init overture options
overture* overture_init(uint16_t port) { // init overture options
overture *info = (overture*)malloc(sizeof(overture));
info->port = port;
info->debug = FALSE;
@ -19,7 +19,7 @@ overture* overture_init(int port) { // init overture options
info->host_file = NULL;
info->foreign_port = FOREIGN_PORT;
info->domestic_port = DOMESTIC_PORT;
info->reject_type = int_list_init();
info->reject_type = uint32_list_init();
info->foreign_ip_file = "/dev/null";
info->domestic_ip_file = "/dev/null";
info->foreign_domain_file = "/dev/null";
@ -28,7 +28,7 @@ overture* overture_init(int port) { // init overture options
}
void overture_dump(overture *info) { // show overture info in debug log
char *reject_type = int_list_dump(info->reject_type);
char *reject_type = uint32_list_dump(info->reject_type);
log_debug("Overture port -> %d", info->port);
log_debug("Overture debug -> %s", show_bool(info->debug));
log_debug("Overture timeout -> %d", info->timeout);
@ -120,9 +120,9 @@ char* overture_gen_config(overture *info) { // generate json configure from over
cJSON_AddStringToObject(config, "domainTTLFile", info->ttl_file);
}
if (int_list_len(info->reject_type)) {
if (uint32_list_len(info->reject_type)) {
cJSON *reject_type = cJSON_CreateArray();
for (int **rr_num = info->reject_type; *rr_num != NULL; ++rr_num) {
for (uint32_t **rr_num = info->reject_type; *rr_num != NULL; ++rr_num) {
cJSON_AddItemToArray(reject_type, cJSON_CreateNumber(**rr_num));
}
cJSON_AddItemToObject(config, "rejectQType", reject_type);

101
src/utils/structure.c

@ -3,94 +3,97 @@
#include <string.h>
#include "structure.h"
int* int_init(int number) {
int *data = (int*)malloc(sizeof(int));
*data = number;
return data;
}
char* string_init(const char *str) {
char* string_init(const char *str) { // new string
return strcpy((char*)malloc(strlen(str) + 1), str);
}
char* string_join(const char *base, const char *add) {
char* string_join(const char *base, const char *add) { // combine string
char *ret = (char*)malloc(sizeof(char) * (strlen(base) + strlen(add) + 1));
return strcat(strcpy(ret, base), add);
}
char** string_list_init() {
char** string_list_init() { // init string list
char **string_list = (char**)malloc(sizeof(char*));
*string_list = NULL;
*string_list = NULL; // list end sign
return string_list;
}
int string_list_len(char **string_list) {
int num = 0;
uint32_t string_list_len(char **string_list) { // get len of string list
uint32_t num = 0;
while(string_list[num++] != NULL); // get string list size
return num - 1;
}
char** string_list_append(char **string_list, const char *string) {
int len = string_list_len(string_list);
string_list = (char**)realloc(string_list, sizeof(char*) * (len + 2));
char** string_list_append(char **string_list, const char *string) { // add new string at the end of list
uint32_t len = string_list_len(string_list);
string_list = (char **)realloc(string_list, sizeof(char *) * (len + 2)); // extend string list
string_list[len] = string_init(string);
string_list[len + 1] = NULL; // list end sign
return string_list;
}
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;
// TODO: string list free
void string_list_free(char **string_list) { // free string list
// free every string
// free list itself
}
if (string_len == 0) { // empty string
string_len = 2;
char* string_list_dump(char **string_list) { // ['a', 'b', 'c', ...]
if (string_list_len(string_list) == 0) {
return string_init("[]"); // empty string list
}
char *string_ret = (char*)malloc(sizeof(char) * (string_len + 1));
string_ret[0] = '[';
string_ret[1] = 0x00;
char *string_ret = (char *)malloc(2);
strcpy(string_ret, "[");
for (char **string = string_list; *string != NULL; ++string) {
string_ret = strcat(strcat(string_ret, "'"), *string);
string_ret = strcat(string_ret, "', ");
string_ret = (char *)realloc(string_ret, strlen(string_ret) + strlen(*string) + 5);
strcat(strcat(strcat(string_ret, "'"), *string), "', ");
}
string_ret[string_len - 1] = ']';
string_ret[string_len] = 0x00;
return string_ret;
string_ret[strlen(string_ret) - 2] = '\0';
return strcat(string_ret, "]");
}
int** int_list_init() {
int **int_list = (int**)malloc(sizeof(int*));
uint32_t** uint32_list_init() {
uint32_t **int_list = (uint32_t **)malloc(sizeof(uint32_t *));
*int_list = NULL;
return int_list;
}
int int_list_len(int **int_list) {
int num = 0;
uint32_t* uint32_init(uint32_t number) {
uint32_t *data = (uint32_t *)malloc(sizeof(uint32_t));
*data = number;
return data;
}
uint32_t uint32_list_len(uint32_t **int_list) {
uint32_t num = 0;
while(int_list[num++] != NULL); // get int list size
return num - 1;
}
int** int_list_append(int **int_list, int number) {
int len = int_list_len(int_list);
int_list = (int**)realloc(int_list, sizeof(int*) * (len + 2));
int_list[len] = int_init(number);
int_list[len + 1] = NULL; // list end sign
return int_list;
uint32_t** uint32_list_append(uint32_t **uint32_list, uint32_t number) {
uint32_t len = uint32_list_len(uint32_list);
uint32_list = (uint32_t **)realloc(uint32_list, sizeof(uint32_t *) * (len + 2));
uint32_list[len] = uint32_init(number);
uint32_list[len + 1] = NULL; // list end sign
return uint32_list;
}
char* int_list_dump(int **int_list) { // [1, 2, 3, ...]
if (int_list_len(int_list) == 0) {
char* uint32_list_dump(uint32_t **uint32_list) { // [1, 2, 3, ...]
if (uint32_list_len(uint32_list) == 0) {
return string_init("[]"); // empty int list
}
char int_str[12];
char *string_ret = (char*)malloc(sizeof(char) * 2);
string_ret[0] = '['; string_ret[1] = 0x00;
for (int **number = int_list; *number != NULL; ++number) {
sprintf(int_str, "%d", **number);
string_ret = (char*)realloc(string_ret, sizeof(char) * (strlen(string_ret) + 15));
string_ret = strcat(strcat(string_ret, int_str), ", ");
char uint32_str[12];
char *string_ret = (char *)malloc(2);
string_ret[0] = '[';
string_ret[1] = '\0';
for (uint32_t **number = uint32_list; *number != NULL; ++number) {
sprintf(uint32_str, "%d", **number);
string_ret = (char*)realloc(string_ret, strlen(string_ret) + 15);
string_ret = strcat(strcat(string_ret, uint32_str), ", ");
}
string_ret[strlen(string_ret) - 2] = ']';
string_ret[strlen(string_ret) - 1] = 0x00;
string_ret[strlen(string_ret) - 1] = '\0';
return string_ret;
}
// TODO: uint32 list free

Loading…
Cancel
Save