-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerTCP.cpp
More file actions
219 lines (202 loc) · 7.02 KB
/
Copy pathServerTCP.cpp
File metadata and controls
219 lines (202 loc) · 7.02 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#ifdef DEBUGFASTCGITCP
#include "ServerTCP.hpp"
#include "Client.hpp"
#include "Dns.hpp"
#include <sys/socket.h>
#include <sys/un.h>
#include <iostream>
#include <unistd.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
ServerTCP::ServerTCP(const char* const ip,const char* const port)
{
this->kind=EpollObject::Kind::Kind_ServerTCP;
if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
{
std::cerr << "Can't create the unix socket: " << errno << std::endl;
abort();
}
if(!tryListenInternal(ip,port))
abort();
}
bool ServerTCP::tryListenInternal(const char* const ip,const char* const port)
{
if(strlen(port)==0)
{
std::cout << "ServerTCP::tryListenInternal() port can't be empty (abort)" << std::endl;
abort();
}
std::cout << "ServerTCP::tryListenInternal() on port: " << port << std::endl;
struct addrinfo hints;
struct addrinfo *result, *rp;
int s;
memset(&hints, 0, sizeof (struct addrinfo));
hints.ai_family = AF_UNSPEC; /* Return IPv4 and IPv6 choices */
hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
hints.ai_flags = AI_PASSIVE; /* All interfaces */
if(ip==NULL || ip[0]=='\0')
s = getaddrinfo(NULL, port, &hints, &result);
else
s = getaddrinfo(ip, port, &hints, &result);
if (s != 0)
{
std::cerr << "getaddrinfo:" << gai_strerror(s) << std::endl;
return false;
}
rp = result;
if(rp == NULL)
{
std::cerr << "rp == NULL, can't bind" << std::endl;
return false;
}
unsigned int bindSuccess=0,bindFailed=0;
while(rp != NULL)
{
fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if(fd == -1)
{
std::cerr
<< "unable to create the socket: familly: " << rp->ai_family
<< ", rp->ai_socktype: " << rp->ai_socktype
<< ", rp->ai_protocol: " << rp->ai_protocol
<< ", rp->ai_addr: " << rp->ai_addr
<< ", rp->ai_addrlen: " << rp->ai_addrlen
<< std::endl;
continue;
}
int one=1;
if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one)!=0)
std::cerr << "Unable to apply SO_REUSEADDR" << std::endl;
one=1;
if(setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof one)!=0)
std::cerr << "Unable to apply SO_REUSEPORT" << std::endl;
s = bind(fd, rp->ai_addr, rp->ai_addrlen);
if(s!=0)
{
//unable to bind
::close(fd);
std::cerr
<< "unable to bind: familly: " << rp->ai_family
<< ", rp->ai_socktype: " << rp->ai_socktype
<< ", rp->ai_protocol: " << rp->ai_protocol
<< ", rp->ai_addr: " << rp->ai_addr
<< ", rp->ai_addrlen: " << rp->ai_addrlen
<< ", errno: " << std::to_string(errno)
<< std::endl;
bindFailed++;
}
else
{
if(fd==-1)
{
std::cerr << "Leave without bind but s!=0" << std::endl;
return false;
}
int flags = fcntl(fd, F_GETFL, 0);
if(flags == -1)
{
std::cerr << "fcntl get flags error" << std::endl;
return false;
}
flags |= O_NONBLOCK;
int s = fcntl(fd, F_SETFL, flags);
if(s == -1)
{
std::cerr << "fcntl set flags error" << std::endl;
return false;
}
s = listen(fd, SOMAXCONN);
if(s == -1)
{
::close(s);
std::cerr << "Unable to listen" << std::endl;
return false;
}
epoll_event event;
event.data.ptr = this;
#ifdef DEBUGFASTCGI
std::cerr << "EPOLL_CTL_ADD: " << event.data.ptr << " " << __FILE__ << ":" << __LINE__ << std::endl;
#endif
event.events = EPOLLIN | EPOLLOUT | EPOLLET | EPOLLRDHUP | EPOLLHUP | EPOLLERR;
if((uint64_t)event.data.ptr<100)
{
std::cerr << "EPOLL_CTL_ADD: " << event.data.ptr << " " << __FILE__ << ":" << __LINE__ << " (abort)" << std::endl;
abort();
}
s = epoll_ctl(epollfd,EPOLL_CTL_ADD, fd, &event);
if(s == -1)
{
::close(s);
std::cerr << "epoll_ctl error" << std::endl;
return false;
}
std::cout
<< "correctly bind: familly: " << rp->ai_family
<< ", rp->ai_socktype: " << rp->ai_socktype
<< ", rp->ai_protocol: " << rp->ai_protocol
<< ", rp->ai_addr: " << rp->ai_addr
<< ", rp->ai_addrlen: " << rp->ai_addrlen
<< ", port: " << port
<< ", rp->ai_flags: " << rp->ai_flags
//<< ", rp->ai_canonname: " << rp->ai_canonname-> corrupt the output
<< std::endl;
char hbuf[NI_MAXHOST];
if(!getnameinfo(rp->ai_addr, rp->ai_addrlen, hbuf, sizeof(hbuf),NULL, 0, NI_NAMEREQD))
std::cout << "getnameinfo: " << hbuf << std::endl;
bindSuccess++;
}
rp = rp->ai_next;
}
freeaddrinfo (result);
return bindSuccess>0;
}
void ServerTCP::parseEvent(const epoll_event &)
{
while(1)
{
sockaddr in_addr;
socklen_t in_len = sizeof(in_addr);
const int &infd = ::accept4(fd, &in_addr, &in_len, SOCK_NONBLOCK | SOCK_CLOEXEC);
if(infd == -1)
{
if((errno != EAGAIN) &&
(errno != EWOULDBLOCK))
std::cout << "connexion accepted" << std::endl;
return;
}
if(Dns::dns->requestCountMerged()>1000)
{
#ifdef DEBUGFILEOPEN
std::cerr << "Server::parseEvent(), fd: " << infd << std::endl;
#endif
::close(infd);
return;
}
//do the stuff
Client *client=new Client(infd);
//setup unix socket non blocking and listen
epoll_event event;
event.data.ptr = client;
event.events = EPOLLIN | EPOLLOUT | EPOLLET | EPOLLRDHUP | EPOLLHUP | EPOLLERR;// | EPOLLONESHOT: broke
//std::cerr << "EPOLL_CTL_ADD: " << infd << std::endl;
if(epoll_ctl(epollfd,EPOLL_CTL_ADD, infd, &event) == -1)
{
printf("epoll_ctl failed to add server: %s", strerror(errno));
abort();
}
//try read request
client->readyToRead();
if(!client->isValid())
{
#ifdef DEBUGFASTCGI
std::cerr << __FILE__ << ":" << __LINE__ << std::endl;
#endif
client->disconnect();
//delete client; -> generate use after free bug
}
}
}
#endif