From c7d9d6ab03a14773c16e57ebe1fb63a10ff3d0f7 Mon Sep 17 00:00:00 2001 From: aixiao Date: Wed, 9 Sep 2020 21:55:48 +0800 Subject: [PATCH] Optimize gcc10 compilation error Set the maximum number of open files per process --- CProxy.conf | 2 +- Makefile | 2 +- conf.c | 2 +- http_proxy.c | 2 ++ http_proxy.h | 5 ++--- httpdns.c | 32 +++++++++++++++++--------------- httpdns.h | 4 ++-- main.c | 11 +++++++++++ main.h | 8 +++++--- 9 files changed, 42 insertions(+), 26 deletions(-) diff --git a/CProxy.conf b/CProxy.conf index 74cfc63..af983bf 100644 --- a/CProxy.conf +++ b/CProxy.conf @@ -2,7 +2,7 @@ global { uid=3004; process=2; timeout=60; - sslencoding=128; + encode=128; tcp_listen=0124; dns_listen=0125; } diff --git a/Makefile b/Makefile index 64c151f..75e1eb6 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CROSS_COMPILE ?= CC := $(CROSS_COMPILE)gcc STRIP := $(CROSS_COMPILE)strip -CFLAGS += -g -O2 -Wall -pthread -fcommon +CFLAGS += -g -O2 -Wall -pthread LIBS = OBJ := CProxy diff --git a/conf.c b/conf.c index 794ddef..215d686 100644 --- a/conf.c +++ b/conf.c @@ -114,7 +114,7 @@ static void parse_global_module(char *content, conf * p) p->process = atoi(val_begin); } else if (strcasecmp(var, "timeout") == 0) { p->timeout = atoi(val_begin); - } else if (strcasecmp(var, "sslencoding") == 0) { + } else if (strcasecmp(var, "encode") == 0) { p->sslencoding = atoi(val_begin); } else if (strcasecmp(var, "tcp_listen") == 0) { p->tcp_listen = atoi(val_begin); diff --git a/http_proxy.c b/http_proxy.c index 514d6a5..ca5974c 100644 --- a/http_proxy.c +++ b/http_proxy.c @@ -2,6 +2,8 @@ #include "main.h" int sslEncodeCode; +int remote_port; +char remote_host[128]; /* 对数据进行编码 */ void dataEncode(char *data, int data_len) diff --git a/http_proxy.h b/http_proxy.h index 985ea57..2a28450 100644 --- a/http_proxy.h +++ b/http_proxy.h @@ -7,9 +7,8 @@ #define HTTP_TYPE 0 #define OTHER_TYPE 1 -int remote_port; -char remote_host[128]; - +extern int remote_port; +extern char remote_host[128]; extern int sslEncodeCode; typedef struct conn_t { diff --git a/httpdns.c b/httpdns.c index b339a93..654e48f 100644 --- a/httpdns.c +++ b/httpdns.c @@ -13,6 +13,8 @@ char *cachePath = NULL; struct dns_cache *cache, *cache_temp; socklen_t addr_len = sizeof(dst_addr); unsigned int cache_using, cacheLimit; +dns_t dns_list[DNS_MAX_CONNECTION]; +struct epoll_event evs[DNS_MAX_CONNECTION+1], event; int read_cache_file() { @@ -152,9 +154,9 @@ void respond_clients() dns_list[i].wait_response_client = 0; } } - ev.events = EPOLLIN; - ev.data.fd = dnsListenFd; - epoll_ctl(dns_efd, EPOLL_CTL_MOD, dnsListenFd, &ev); + event.events = EPOLLIN; + event.data.fd = dnsListenFd; + epoll_ctl(dns_efd, EPOLL_CTL_MOD, dnsListenFd, &event); } /* 分析DNS请求 */ @@ -245,9 +247,9 @@ int build_dns_response(dns_t * dns) } if (respond_client(dns) == 1) { dns->wait_response_client = 1; - ev.events = EPOLLIN | EPOLLOUT; - ev.data.fd = dnsListenFd; - epoll_ctl(dns_efd, EPOLL_CTL_MOD, dnsListenFd, &ev); + event.events = EPOLLIN | EPOLLOUT; + event.data.fd = dnsListenFd; + epoll_ctl(dns_efd, EPOLL_CTL_MOD, dnsListenFd, &event); } return 0; @@ -263,9 +265,9 @@ void http_out(dns_t * out) if (write_len == out->http_request_len) { //puts("write success"); free(out->http_request); - ev.events = EPOLLIN | EPOLLET; - ev.data.ptr = out; - epoll_ctl(dns_efd, EPOLL_CTL_MOD, out->fd, &ev); + event.events = EPOLLIN | EPOLLET; + event.data.ptr = out; + epoll_ctl(dns_efd, EPOLL_CTL_MOD, out->fd, &event); } else if (write_len > 0) { //puts("write a little"); out->http_request_len -= write_len; @@ -414,9 +416,9 @@ void new_client(conf *configure) dns->http_request_len = strlen(dns->http_request); //printf("%s\n", dns->http_request); - ev.events = EPOLLOUT | EPOLLERR | EPOLLET; - ev.data.ptr = dns; - epoll_ctl(dns_efd, EPOLL_CTL_ADD, dns->fd, &ev); + event.events = EPOLLOUT | EPOLLERR | EPOLLET; + event.data.ptr = dns; + epoll_ctl(dns_efd, EPOLL_CTL_ADD, dns->fd, &event); } void *httpdns_loop(void *p) @@ -430,9 +432,9 @@ void *httpdns_loop(void *p) perror("epoll_create"); return NULL; } - ev.data.fd = dnsListenFd; - ev.events = EPOLLIN; - epoll_ctl(dns_efd, EPOLL_CTL_ADD, dnsListenFd, &ev); + event.data.fd = dnsListenFd; + event.events = EPOLLIN; + epoll_ctl(dns_efd, EPOLL_CTL_ADD, dnsListenFd, &event); memset(dns_list, 0, sizeof(dns_list)); while (1) { diff --git a/httpdns.h b/httpdns.h index b09f27b..c190b21 100644 --- a/httpdns.h +++ b/httpdns.h @@ -43,8 +43,8 @@ struct dns_cache { struct dns_cache *next; }; -dns_t dns_list[DNS_MAX_CONNECTION]; -struct epoll_event evs[DNS_MAX_CONNECTION+1], ev; +extern dns_t dns_list[DNS_MAX_CONNECTION]; +extern struct epoll_event evs[DNS_MAX_CONNECTION+1], event; void *httpdns_loop(void *p); diff --git a/main.c b/main.c index 50a5acf..17718b6 100644 --- a/main.c +++ b/main.c @@ -14,6 +14,9 @@ struct epoll_event ev, events[MAX_CONNECTION + 1]; int epollfd, server_sock; conn cts[MAX_CONNECTION]; +int local_port; +char local_host[128]; +int process; int create_connection(char *remote_host, int remote_port) { @@ -227,6 +230,7 @@ void _main(int argc, char *argv[]) char executable_filename[PATH_SIZE] = { 0 }; (void)get_executable_path(path, executable_filename, sizeof(path)); char *inifile = "/CProxy.conf"; + struct rlimit rt; inifile = strcat(path, inifile); conf *configure = (struct CONF *)malloc(sizeof(struct CONF)); memset(configure, 0, sizeof(struct CONF)); @@ -314,6 +318,12 @@ void _main(int argc, char *argv[]) } } + // 设置每个进程允许打开的最大文件数 + rt.rlim_max = rt.rlim_cur = MAX_CONNECTION * 2; + if (setrlimit(RLIMIT_NOFILE, &rt) == -1) { + perror("setrlimit"); + } + server_ini(); // 守护进程 httpdns_initialize(configure); // 初始化http_dns memset(cts, 0, sizeof(cts)); @@ -378,4 +388,5 @@ void _main(int argc, char *argv[]) int main(int argc, char *argv[]) { _main(argc, argv); + return 0; } diff --git a/main.h b/main.h index 820ecdd..4895988 100644 --- a/main.h +++ b/main.h @@ -19,15 +19,17 @@ #include #include #include +#include +#include #define MAX_CONNECTION 1020 #define BUFFER_SIZE 8192 #define PATH_SIZE 270 #define CACHE_SIZE 270 -int local_port; -char local_host[128]; -int process; +extern int local_port; +extern char local_host[128]; +extern int process; extern int epollfd; extern struct epoll_event ev, events[MAX_CONNECTION + 1];