-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocks_server.cpp
More file actions
51 lines (42 loc) · 1.16 KB
/
socks_server.cpp
File metadata and controls
51 lines (42 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "socks_server.hpp"
using namespace std;
void sigchildHandler(int signo)
{
pid_t child;
while((child = waitpid(-1, NULL, WNOHANG)) > 0)
{
d_trace("#child[%d] has sent sigchild", child);
}
}
void server(int listenfd)
{
int connfd;
socklen_t client_len;
struct sockaddr_in client_addr;
char client_ip[INET_ADDRSTRLEN];
vector<Firewall> firewalls;
struct sigaction act_child;
act_child.sa_handler = sigchildHandler;
act_child.sa_flags = SA_RESTART;
sigaction(SIGCHLD, &act_child, 0);
while(true)
{
connfd = accept(listenfd, (struct sockaddr*) &client_addr, &client_len);
inet_ntop(AF_INET, &(client_addr.sin_addr), client_ip, INET_ADDRSTRLEN);
d_server("New client from: %s:%d", client_ip, ntohs(client_addr.sin_port));
pid_t pid = fork();
if(pid < 0)
{
perror("fork error");
close(connfd);
continue;
}
if(pid == 0)
{
close(listenfd);
SOCKSHandler handler(connfd, client_ip, ntohs(client_addr.sin_port));
exit(EXIT_SUCCESS);
}
close(connfd);
}
}