Browse Source

update: use boolean value

master
Dnomd343 2 years ago
parent
commit
e1968ca9c7
  1. 3
      include/common.h
  2. 20
      src/load.c
  3. 2
      src/local.c
  4. 2
      src/server.c
  5. 35
      src/utils/network.c
  6. 11
      src/utils/process.c

3
include/common.h

@ -1,6 +1,9 @@
#ifndef COMMON_H_
#define COMMON_H_
#define TRUE 1
#define FALSE 0
#define VERSION "1.0.1-beta"
#define RANDOM_PORT_START 41952

20
src/load.c

@ -7,18 +7,18 @@
void init_info(bootstrap *info);
void dump_info(bootstrap *info);
char** add_extra_opts(char **opts, char *extra_opts_str);
void json_decode(char *json_content, bootstrap *info);
char** add_extra_opts(char **opts, char *extra_opts_str);
int add_field(char *field, char **target, char ***arg, char ***arg_limit);
void init_info(bootstrap *info) {
info->is_udp_proxy = 1; // enabled udp proxy
info->is_udp_proxy = TRUE; // enabled udp proxy
info->server_addr = info->client_addr = NULL;
info->server_port = info->client_port = NULL;
info->password = NULL;
info->method = NULL;
info->timeout = NULL;
info->fastopen = 0;
info->fastopen = FALSE;
info->plugin = NULL;
info->plugin_opts = NULL;
info->shadowsocks = NULL;
@ -89,9 +89,9 @@ void json_decode(char *json_content, bootstrap *info) { // decode JSON content
log_fatal("`no_udp` must be bool");
}
if (json->valueint) { // is_udp_proxy = ~(json->valueint)
info->is_udp_proxy = 0;
info->is_udp_proxy = FALSE;
} else {
info->is_udp_proxy = 1;
info->is_udp_proxy = TRUE;
}
} else if (!strcmp(json->string, "server")) { // server => server_addr
if (!cJSON_IsString(json)) {
@ -201,8 +201,8 @@ void json_decode(char *json_content, bootstrap *info) { // decode JSON content
}
int add_field(char *field, char **target, char ***arg, char ***arg_limit) {
if (strcmp(**arg, field)) { // field not match
return 0;
if (strcmp(**arg, field) != 0) { // field not match
return FALSE;
}
if (++(*arg) == *arg_limit) { // without next argument
log_fatal("`%s` require a parameter", field);
@ -211,7 +211,7 @@ int add_field(char *field, char **target, char ***arg, char ***arg_limit) {
free(*target); // override target field
}
*target = new_string(**arg);
return 1;
return TRUE;
}
bootstrap* load_info(int argc, char **argv) { // load info from input parameters
@ -231,9 +231,9 @@ bootstrap* load_info(int argc, char **argv) { // load info from input parameters
if (!strcmp(*arg, "--debug")) { // skip debug flag
continue;
} else if (!strcmp(*arg, "--fast-open")) { // --fast-open => fastopen
info->fastopen = 1;
info->fastopen = TRUE;
} else if (!strcmp(*arg, "--no-udp")) { // --no-udp => is_udp_proxy
info->is_udp_proxy = 0;
info->is_udp_proxy = FALSE;
} else if (!strcmp(*arg, "-c")) { // -c => CONFIG_JSON
if (++arg == arg_limit) {
log_fatal("Miss json file after `-c` flag");

2
src/local.c

@ -30,6 +30,6 @@ int main(int argc, char *argv[]) {
init(argc, argv, HELP_MSG);
log_info("Shadowsocks bootstrap local (%s)", VERSION);
bootstrap *info = load_info(argc, argv);
start_bootstrap(1, load_sip003("sslocal", info));
start_bootstrap(TRUE, load_sip003("sslocal", info));
return 0;
}

2
src/server.c

@ -30,6 +30,6 @@ int main(int argc, char *argv[]) {
init(argc, argv, HELP_MSG);
log_info("Shadowsocks bootstrap server (%s)", VERSION);
bootstrap *info = load_info(argc, argv);
start_bootstrap(0, load_sip003("ssserver", info));
start_bootstrap(FALSE, load_sip003("ssserver", info));
return 0;
}

35
src/utils/network.c

@ -5,6 +5,7 @@
#include <sys/time.h>
#include <arpa/inet.h>
#include "network.h"
#include "common.h"
#include "logger.h"
#define TIMEOUT 15
@ -63,7 +64,7 @@ int check_port_available(unsigned int port, int is_udp, int is_ipv6) { // test a
ipv4_tcp_addr.sin_port = htons(port);
ipv4_tcp_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(ipv4_tcp_sock, (struct sockaddr*)&ipv4_tcp_addr, sizeof(ipv4_tcp_addr)) < 0) {
return 0; // false
return FALSE;
}
close(ipv4_tcp_sock);
@ -74,13 +75,13 @@ int check_port_available(unsigned int port, int is_udp, int is_ipv6) { // test a
ipv4_udp_addr.sin_port = htons(port);
ipv4_udp_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(ipv4_udp_sock, (struct sockaddr*)&ipv4_udp_addr, sizeof(ipv4_udp_addr)) < 0) {
return 0; // false
return FALSE;
}
close(ipv4_udp_sock);
}
if (!is_ipv6) { // ipv6 ignore
return 1; // true
return TRUE;
}
ipv6_tcp_sock = socket(AF_INET6, SOCK_STREAM, 0);
@ -89,7 +90,7 @@ int check_port_available(unsigned int port, int is_udp, int is_ipv6) { // test a
ipv6_tcp_addr.sin6_port = htons(port);
ipv6_tcp_addr.sin6_addr = in6addr_any;
if (bind(ipv6_tcp_sock, (struct sockaddr*)&ipv6_tcp_addr, sizeof(ipv6_tcp_addr)) < 0) {
return 0; // false
return FALSE;
}
close(ipv6_tcp_sock);
@ -100,19 +101,19 @@ int check_port_available(unsigned int port, int is_udp, int is_ipv6) { // test a
ipv6_udp_addr.sin6_port = htons(port);
ipv6_udp_addr.sin6_addr = in6addr_any;
if (bind(ipv6_udp_sock, (struct sockaddr*)&ipv6_udp_addr, sizeof(ipv6_udp_addr)) < 0) {
return 0; // false
return FALSE;
}
close(ipv6_udp_sock);
}
return 1; // true
return TRUE;
}
int get_available_port(unsigned short range_start, unsigned short range_end) { // get a available port
unsigned short port;
for (;;) { // wait until a available port in range
port = get_random_num(range_start, range_end); // get a random port in range
if (check_port_available(port, 1, 1)) { // port available
if (check_port_available(port, TRUE, TRUE)) { // port available
return (int)port;
}
}
@ -120,24 +121,24 @@ int get_available_port(unsigned short range_start, unsigned short range_end) { /
int is_ipv4_addr(char *address) { // 判断是否为IPv4地址
if (inet_addr(address) == -1) {
return 0;
return FALSE;
}
return 1;
return TRUE;
}
int is_ipv6_addr(char *address) { // 判断是否为IPv6地址
char buf[sizeof(struct in6_addr)];
if (inet_pton(AF_INET6, address, buf) <= 0) {
return 0;
return FALSE;
}
return 1;
return TRUE;
}
int is_ip_addr(char *address) { // 判断是否为IP地址
if (is_ipv4_addr(address) || is_ipv6_addr(address)) {
return 1;
return TRUE;
}
return 0;
return FALSE;
}
int create_ipv4_udp_sock(char *address, int port) { // 创建并绑定IPv4 UDP端口
@ -295,17 +296,17 @@ void proxy(char *server_ip, int server_port, char *listen_ip, int listen_port) {
struct sockaddr_in6 ipv6_client_addr;
socklen_t ipv4_client_addr_len = sizeof(ipv4_client_addr);
socklen_t ipv6_client_addr_len = sizeof(ipv6_client_addr);
int bind_error_flag = 0;
int bind_error_flag = FALSE;
int is_listen_ipv6 = is_ipv6_addr(listen_ip); // 判断监听地址是否为IPv6
if (!is_listen_ipv6) { // IPv4客户端
ipv4_client_fd = create_ipv4_udp_sock(listen_ip, listen_port); // 监听端口描述符
if (ipv4_client_fd == -1) { // 端口监听失败
bind_error_flag = 1;
bind_error_flag = TRUE;
}
} else { // IPv6客户端
ipv6_client_fd = create_ipv6_udp_sock(listen_ip, listen_port); // 监听端口描述符
if (ipv6_client_fd == -1) { // 端口监听失败
bind_error_flag = 1;
bind_error_flag = TRUE;
}
}
if (bind_error_flag) { // 端口被占用
@ -313,7 +314,7 @@ void proxy(char *server_ip, int server_port, char *listen_ip, int listen_port) {
log_warn("UDP communication of the agent will not work properly");
return;
}
PROXY_EXIT = 0; // 重置退出标识
PROXY_EXIT = FALSE; // 重置退出标识
log_info("UDP Proxy: %s:%d -> %s:%d", listen_ip, listen_port, server_ip, server_port);
for (;;) {
if (!is_listen_ipv6) { // IPv4客户端

11
src/utils/process.c

@ -9,6 +9,7 @@
#include <sys/prctl.h>
#include "network.h"
#include "process.h"
#include "common.h"
#include "logger.h"
#include "dns.h"
@ -20,8 +21,8 @@ typedef struct {
char *plugin;
pid_t ss_pid = 0, plugin_pid = 0;
int exiting = 0; // sub process exiting
int exited = 0; // all sub process exited
int exiting = FALSE; // sub process exiting
int exited = FALSE; // all sub process exited
void error_exit();
void normal_exit();
@ -71,8 +72,8 @@ void kill_sub_process() { // kill child process
while (exiting) {
sleep(1); // exit process already working -> block
}
exiting = 1; // exit process flag
PROXY_EXIT = 1; // udp proxy cancel
exiting = TRUE; // exit process flag
PROXY_EXIT = TRUE; // udp proxy cancel
if (ss_pid != 0) {
kill(ss_pid, SIGKILL);
log_info("Kill shadowsocks process");
@ -116,7 +117,7 @@ void get_sub_exit() { // catch child process die
error_exit();
}
}
exited = 1;
exited = TRUE;
}
char** load_plugin_env(sip003 *service) { // load plugin's environment variable

Loading…
Cancel
Save