diff --git a/dns.c b/dns.c index 2be62dc..c4ba7f3 100644 --- a/dns.c +++ b/dns.c @@ -1,7 +1,7 @@ #include "dns.h" #include "http.h" -struct dns dns_list[MAX_CONNECTION / 2]; //一个客户端 + 一个服务端 占用一个dns结构体 +struct dns dns_list[MAX_CONNECTION / 2]; //涓涓鎴风 + 涓涓湇鍔$ 鍗犵敤涓涓猟ns缁撴瀯浣 int dnsFd; void read_dns_rsp() @@ -17,10 +17,10 @@ void read_dns_rsp() memcpy(&dns_flag, rsp_data, 2); dns = dns_list + dns_flag; client = cts + (dns_flag << 1); - //判断是否是正常DNS回应,是否已关闭连接 + //鍒ゆ柇鏄惁鏄甯窪NS鍥炲簲锛屾槸鍚﹀凡鍏抽棴杩炴帴 if (dns_flag > MAX_CONNECTION >> 1 || client->fd < 0) continue; - if (dns->request_len + 12 > len || (unsigned char)rsp_data[3] != 128) //char只有7位可用,则正数最高为127 + if (dns->request_len + 12 > len || (unsigned char)rsp_data[3] != 128) //char鍙湁7浣嶅彲鐢紝鍒欐鏁版渶楂樹负127 { close_connection(client); continue; @@ -58,7 +58,7 @@ void read_dns_rsp() } } -/* 完全发送返回0,发送部分返回1,出错返回-1 */ +/* 瀹屽叏鍙戦佽繑鍥0锛屽彂閫侀儴鍒嗚繑鍥1锛屽嚭閿欒繑鍥-1 */ static int8_t send_dns_req(struct dns *dns) { static int write_len; @@ -145,12 +145,12 @@ int8_t build_dns_req(struct dns *dns, char *domain) void dns_connect(struct sockaddr_in *dnsAddr) { - dnsFd = socket(AF_INET, SOCK_DGRAM, 0); + dnsFd = socket(AF_INET, SOCK_DGRAM, 0); if (dnsFd < 0) { perror("socket"); exit(1); } - connect(dnsFd, (struct sockaddr *)dnsAddr, sizeof(struct sockaddr_in)); + connect(dnsFd, (struct sockaddr *)dnsAddr, sizeof(struct sockaddr_in)); fcntl(dnsFd, F_SETFL, O_NONBLOCK); } diff --git a/dns.h b/dns.h index ff42ef0..9f7c01c 100644 --- a/dns.h +++ b/dns.h @@ -4,7 +4,7 @@ #include "main.h" struct dns { - char request[512]; //UDP的DNS请求不超512字节 + char request[512]; //UDP鐨凞NS璇锋眰涓嶈秴512瀛楄妭 uint16_t request_len, sent_len; }; diff --git a/http.c b/http.c index 4af5c2e..fcf692f 100644 --- a/http.c +++ b/http.c @@ -226,10 +226,10 @@ static char *build_request(char *client_data, int *data_len, char *host) static char *uri, *url, *p, *lf, *header, *new_data, *proxy_host; static int len; - del_hdr(client_data, data_len); header = client_data; proxy_host = host; do { + del_hdr(client_data, data_len); /* 灏嗗畬鏁磚rl杞崲涓簎ri */ url = strchr(header, ' '); lf = strchr(header, '\n'); @@ -435,7 +435,7 @@ void tcp_in(conn_t *in) if (in->fd < 0) return; - //濡傛灉in - cts鏄鏁帮紝閭d箞鏄湇鍔$瑙﹀彂浜嬩欢 + //濡傛灉in - cts鏄鏁帮紝閭d箞鏄湇鍔$瑙﹀彂浜嬩欢 if ((in - cts) & 1) { if (in->ready_data_len == 0) @@ -537,28 +537,28 @@ void *accept_loop(void *ptr) { conn_t *client; - while (1) - { - /* 鍋舵暟涓哄鎴风锛屽鏁颁负鏈嶅姟绔 */ - for (client = cts; client - cts < MAX_CONNECTION; client += 2) - if (client->fd < 0) - break; - if (client - cts >= MAX_CONNECTION) - { - sleep(3); - continue; - } - client->fd = accept(lisFd, (struct sockaddr *)&addr, &addr_len); - if (client->fd >= 0) - { - fcntl(client->fd, F_SETFL, fcntl(client->fd, F_GETFL)|O_NONBLOCK); - ev.data.ptr = client; - ev.events = EPOLLIN|EPOLLET; - epoll_ctl(efd, EPOLL_CTL_ADD, client->fd, &ev); - } - } - - return NULL; + while (1) + { + /* 鍋舵暟涓哄鎴风锛屽鏁颁负鏈嶅姟绔 */ + for (client = cts; client - cts < MAX_CONNECTION; client += 2) + if (client->fd < 0) + break; + if (client - cts >= MAX_CONNECTION) + { + sleep(3); + continue; + } + client->fd = accept(lisFd, (struct sockaddr *)&addr, &addr_len); + if (client->fd >= 0) + { + fcntl(client->fd, F_SETFL, fcntl(client->fd, F_GETFL)|O_NONBLOCK); + ev.data.ptr = client; + ev.events = EPOLLIN|EPOLLET; + epoll_ctl(efd, EPOLL_CTL_ADD, client->fd, &ev); + } + } + + return NULL; } void create_listen(char *ip, int port) diff --git a/main.c b/main.c index 801af77..6a905fa 100644 --- a/main.c +++ b/main.c @@ -27,11 +27,11 @@ static void usage() static void server_loop() { - pthread_t thread_id; + pthread_t thread_id; int n; - //单独进程accept多进程并发环境下不会惊群 - pthread_create(&thread_id, NULL, accept_loop, NULL); + //鍗曠嫭杩涚▼accept澶氳繘绋嬪苟鍙戠幆澧冧笅涓嶄細鎯婄兢 + pthread_create(&thread_id, NULL, accept_loop, NULL); ev.events = EPOLLIN; ev.data.fd = dnsFd; epoll_ctl(efd, EPOLL_CTL_ADD, dnsFd, &ev); @@ -39,7 +39,7 @@ static void server_loop() { n = epoll_wait(efd, evs, MAX_CONNECTION + 2, -1); while (n-- > 0) - { + { if (evs[n].data.fd == dnsFd) { if (evs[n].events & EPOLLIN) @@ -60,7 +60,7 @@ static void server_loop() static void initializate(int argc, char **argv) { - struct sockaddr_in dnsAddr; + struct sockaddr_in dnsAddr; char *p; int opt, i, workers = 1; @@ -73,22 +73,22 @@ static void initializate(int argc, char **argv) exit(1); } dnsAddr.sin_family = addr.sin_family = AF_INET; - //默认dns地址 + //榛樿dns鍦板潃 dnsAddr.sin_addr.s_addr = inet_addr(DEFAULT_DNS_IP); dnsAddr.sin_port = htons(53); - dns_connect(&dnsAddr); //主进程中的fd + dns_connect(&dnsAddr); //涓昏繘绋嬩腑鐨刦d strict_spilce = 0; local_header = NULL; ssl_proxy = (char *)"CONNECT"; local_header = (char *)"\nLocal:"; proxy_header = (char *)"\nHost:"; proxy_header_len = strlen(proxy_header); - local_header_len = strlen(local_header); - /* 读取命令行参数 */ + local_header_len = strlen(local_header); + /* 璇诲彇鍛戒护琛屽弬鏁 */ while ((opt = getopt(argc, argv, "d:l:p:s:w:L:ah")) != -1) { switch (opt) - { + { case 'd': p = strchr(optarg, ':'); if (p) @@ -101,7 +101,7 @@ static void initializate(int argc, char **argv) dnsAddr.sin_port = htons(53); } dnsAddr.sin_addr.s_addr = inet_addr(optarg); - connect(dnsFd, (struct sockaddr *)&dnsAddr, sizeof(dnsAddr)); + connect(dnsFd, (struct sockaddr *)&dnsAddr, sizeof(dnsAddr)); break; case 'l': @@ -118,7 +118,7 @@ static void initializate(int argc, char **argv) break; case 'p': - //假如选项值为 "Proxy", proxy_header设置为 "\nProxy:" + //鍋囧閫夐」鍊间负 "Proxy", proxy_header璁剧疆涓 "\nProxy:" proxy_header_len = strlen(optarg) + 2; if (optarg[proxy_header_len] == ':') optarg[proxy_header_len--] = '\0'; @@ -169,7 +169,7 @@ static void initializate(int argc, char **argv) memset(cts, 0, sizeof(cts)); for (i = MAX_CONNECTION; i--; ) cts[i].fd = -1; - //为服务端的结构体分配内存 + //涓烘湇鍔$鐨勭粨鏋勪綋鍒嗛厤鍐呭瓨 for (i = 1; i < MAX_CONNECTION; i += 2) { cts[i].ready_data = (char *)malloc(BUFFER_SIZE); @@ -195,9 +195,9 @@ static void initializate(int argc, char **argv) dns_list[i].request[11] = 0; } signal(SIGPIPE, SIG_IGN); - while (workers-- > 1 && fork() == 0) - //子进程中的dnsFd必须重新申请,不然epoll监听可能读取到其他进程得到的数据 - dns_connect(&dnsAddr); + while (workers-- > 1 && fork() == 0) + //瀛愯繘绋嬩腑鐨刣nsFd蹇呴』閲嶆柊鐢宠锛屼笉鐒秂poll鐩戝惉鍙兘璇诲彇鍒板叾浠栬繘绋嬪緱鍒扮殑鏁版嵁 + dns_connect(&dnsAddr); } int main(int argc, char **argv)