-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_proxy.cpp
More file actions
93 lines (71 loc) · 2.03 KB
/
client_proxy.cpp
File metadata and controls
93 lines (71 loc) · 2.03 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <thread>
#include <netdb.h>
#include <signal.h>
#include "TcpServer.h"
#include "UdpClient.h"
class ClientProxy {
public:
explicit ClientProxy() : server() {
}
virtual ~ClientProxy() {
}
void run() {
while (true) {
int client_fd = server.listen();
std::thread serve_thread(&ClientProxy::serve, this, client_fd);
serve_thread.detach();
}
}
int getServerSocketFd() const {
return server.getSocketFd();
}
private:
void serve(int clientFd) {
std::string request = server.getRequest(clientFd);
std::string ip = getIp(request);
if (ip.empty()) {
close(clientFd);
return;
}
UdpClient client(ip);
client.sendRequest(request);
std::string response = client.getResponse();
server.sendResponse(clientFd, response);
close(clientFd);
}
std::string getIp(std::string request) {
std::size_t colon = request.find(':', request.find('\n')),
enter = request.find('\n', colon);
std::string host = request.substr(colon + 2, enter - colon - 2);
if (host.back() == '\r')
host.pop_back();
if (host.front() >= '0' and host.front() <= '9') // is IP
return host;
hostent *he;
if ((he = gethostbyname(host.c_str())) == NULL) {
herror("resolve failed");
return "";
}
in_addr **addr_list = (in_addr **)he->h_addr_list;
if (addr_list[0] == NULL)
return "";
return std::string(inet_ntoa(*addr_list[0]));
}
TcpServer server;
};
int socketFd;
void sig_handler(int sigNo) {
shutdown(socketFd, SHUT_RDWR);
close(socketFd);
std::cerr << "Closing server socket" << std::endl;
exit(0);
}
int main() {
ClientProxy clientProxy;
socketFd = clientProxy.getServerSocketFd();
signal(SIGINT, sig_handler);
signal(SIGTERM, sig_handler);
clientProxy.run();
return 1;
}