Add files via upload

This commit is contained in:
mmmdbybyd 2018-10-27 22:30:57 +08:00 committed by GitHub
parent 80a25f7740
commit 5c0f9df53a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

32
main.c
View File

@ -3,7 +3,7 @@
#include "dns.h" #include "dns.h"
#include <pthread.h> #include <pthread.h>
#define VERSION "0.1" #define VERSION "0.2"
#define DEFAULT_DNS_IP "114.114.114.114" #define DEFAULT_DNS_IP "114.114.114.114"
struct epoll_event evs[MAX_CONNECTION + 1], ev; struct epoll_event evs[MAX_CONNECTION + 1], ev;
@ -67,10 +67,12 @@ static void initializate(int argc, char **argv)
{ {
struct sockaddr_in dnsAddr; struct sockaddr_in dnsAddr;
char *p; char *p;
int opt, i, workers = 1; int opt, i, workers;
/* 初始化部分变量值 */
addr_len = sizeof(addr); addr_len = sizeof(addr);
lisFd = -1; lisFd = -1;
workers = 1;
dnsAddr.sin_family = addr.sin_family = AF_INET; dnsAddr.sin_family = addr.sin_family = AF_INET;
//默认dns地址 //默认dns地址
dnsAddr.sin_addr.s_addr = inet_addr(DEFAULT_DNS_IP); dnsAddr.sin_addr.s_addr = inet_addr(DEFAULT_DNS_IP);
@ -83,8 +85,8 @@ static void initializate(int argc, char **argv)
proxy_header = (char *)"\nHost:"; proxy_header = (char *)"\nHost:";
proxy_header_len = strlen(proxy_header); 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) while ((opt = getopt(argc, argv, "d:l:p:s:w:u:L:ah")) != -1)
{ {
switch (opt) switch (opt)
{ {
@ -151,11 +153,25 @@ static void initializate(int argc, char **argv)
workers = atoi(optarg); workers = atoi(optarg);
break; break;
case 'u':
if (setgid(atoi(optarg)) != 0))
{
perror("setgid");
exit(1);
}
if (setuid(atoi(optarg)) != 0))
{
perror("setuid");
exit(1);
}
break;
default: default:
usage(); usage();
break; break;
} }
} }
/* 初始化剩下的变量值 */
if (lisFd < 0) if (lisFd < 0)
{ {
fputs("no listen address\n", stderr); fputs("no listen address\n", stderr);
@ -174,6 +190,7 @@ static void initializate(int argc, char **argv)
exit(1); exit(1);
} }
} }
//设置dns请求头首部
memset(dns_list, 0, sizeof(dns_list)); memset(dns_list, 0, sizeof(dns_list));
for (i = MAX_CONNECTION / 2; i--; ) for (i = MAX_CONNECTION / 2; i--; )
{ {
@ -189,9 +206,9 @@ static void initializate(int argc, char **argv)
dns_list[i].request[10] = 0; dns_list[i].request[10] = 0;
dns_list[i].request[11] = 0; dns_list[i].request[11] = 0;
} }
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN); //忽略PIPE信号
while (workers-- > 1 && fork() == 0)
//子进程中的dnsFd必须重新申请不然epoll监听可能读取到其他进程得到的数据 //子进程中的dnsFd必须重新申请不然epoll监听可能读取到其他进程得到的数据
while (workers-- > 1 && fork() == 0)
dns_connect(&dnsAddr); dns_connect(&dnsAddr);
efd = epoll_create(MAX_CONNECTION + 1); efd = epoll_create(MAX_CONNECTION + 1);
if (efd < 0) if (efd < 0)
@ -205,8 +222,7 @@ static void initializate(int argc, char **argv)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
initializate(argc, argv); initializate(argc, argv);
if (daemon(1, 1)) if (daemon(1, 0))
//if (daemon(1, 0))
{ {
perror("daemon"); perror("daemon");
return 1; return 1;