]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengine.cpp
ba3d845121462d0be52083027a2894ed4072bc16
[user/henk/code/inspircd.git] / src / socketengine.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2005-2008 Craig Edwards <craigedwards@brainbox.cc>
7  *   Copyright (C) 2007 Burlex <???@???>
8  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
9  *
10  * This file is part of InspIRCd.  InspIRCd is free software: you can
11  * redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation, version 2.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23
24 #include "inspircd.h"
25
26 EventHandler::EventHandler()
27 {
28         fd = -1;
29         event_mask = 0;
30 }
31
32 void EventHandler::SetFd(int FD)
33 {
34         this->fd = FD;
35 }
36
37 SocketEngine::SocketEngine()
38 {
39         CurrentSetSize = 0;
40 }
41
42 SocketEngine::~SocketEngine()
43 {
44 }
45
46 void SocketEngine::SetEventMask(EventHandler* eh, int mask)
47 {
48         eh->event_mask = mask;
49 }
50
51 void SocketEngine::ChangeEventMask(EventHandler* eh, int change)
52 {
53         int old_m = eh->event_mask;
54         int new_m = old_m;
55
56         // if we are changing read/write type, remove the previously set bit
57         if (change & FD_WANT_READ_MASK)
58                 new_m &= ~FD_WANT_READ_MASK;
59         if (change & FD_WANT_WRITE_MASK)
60                 new_m &= ~FD_WANT_WRITE_MASK;
61
62         // if adding a trial read/write, insert it into the set
63         if (change & FD_TRIAL_NOTE_MASK && !(old_m & FD_TRIAL_NOTE_MASK))
64                 trials.insert(eh->GetFd());
65
66         new_m |= change;
67         if (new_m == old_m)
68                 return;
69
70         eh->event_mask = new_m;
71         OnSetEvent(eh, old_m, new_m);
72 }
73
74 void SocketEngine::DispatchTrialWrites()
75 {
76         std::vector<int> working_list;
77         working_list.reserve(trials.size());
78         working_list.assign(trials.begin(), trials.end());
79         trials.clear();
80         for(unsigned int i=0; i < working_list.size(); i++)
81         {
82                 int fd = working_list[i];
83                 EventHandler* eh = GetRef(fd);
84                 if (!eh)
85                         continue;
86                 int mask = eh->event_mask;
87                 eh->event_mask &= ~(FD_ADD_TRIAL_READ | FD_ADD_TRIAL_WRITE);
88                 if ((mask & (FD_ADD_TRIAL_READ | FD_READ_WILL_BLOCK)) == FD_ADD_TRIAL_READ)
89                         eh->HandleEvent(EVENT_READ, 0);
90                 if ((mask & (FD_ADD_TRIAL_WRITE | FD_WRITE_WILL_BLOCK)) == FD_ADD_TRIAL_WRITE)
91                         eh->HandleEvent(EVENT_WRITE, 0);
92         }
93 }
94
95 bool SocketEngine::AddFdRef(EventHandler* eh)
96 {
97         int fd = eh->GetFd();
98         if (HasFd(fd))
99                 return false;
100
101         while (static_cast<unsigned int>(fd) >= ref.size())
102                 ref.resize(ref.empty() ? 1 : (ref.size() * 2));
103         ref[fd] = eh;
104         CurrentSetSize++;
105         return true;
106 }
107
108 void SocketEngine::DelFdRef(EventHandler *eh)
109 {
110         int fd = eh->GetFd();
111         if (GetRef(fd) == eh)
112         {
113                 ref[fd] = NULL;
114                 CurrentSetSize--;
115         }
116 }
117
118 bool SocketEngine::HasFd(int fd)
119 {
120         return GetRef(fd) != NULL;
121 }
122
123 EventHandler* SocketEngine::GetRef(int fd)
124 {
125         if (fd < 0 || static_cast<unsigned int>(fd) >= ref.size())
126                 return NULL;
127         return ref[fd];
128 }
129
130 bool SocketEngine::BoundsCheckFd(EventHandler* eh)
131 {
132         if (!eh)
133                 return false;
134         if ((eh->GetFd() < 0) || (eh->GetFd() > GetMaxFds()))
135                 return false;
136         return true;
137 }
138
139
140 int SocketEngine::Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen)
141 {
142         return accept(fd->GetFd(), addr, addrlen);
143 }
144
145 int SocketEngine::Close(EventHandler* fd)
146 {
147 #ifdef _WIN32
148         return closesocket(fd->GetFd());
149 #else
150         return close(fd->GetFd());
151 #endif
152 }
153
154 int SocketEngine::Close(int fd)
155 {
156 #ifdef _WIN32
157         return closesocket(fd);
158 #else
159         return close(fd);
160 #endif
161 }
162
163 int SocketEngine::Blocking(int fd)
164 {
165 #ifdef _WIN32
166         unsigned long opt = 0;
167         return ioctlsocket(fd, FIONBIO, &opt);
168 #else
169         int flags = fcntl(fd, F_GETFL, 0);
170         return fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
171 #endif
172 }
173
174 int SocketEngine::NonBlocking(int fd)
175 {
176 #ifdef _WIN32
177         unsigned long opt = 1;
178         return ioctlsocket(fd, FIONBIO, &opt);
179 #else
180         int flags = fcntl(fd, F_GETFL, 0);
181         return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
182 #endif
183 }
184
185 void SocketEngine::SetReuse(int fd)
186 {
187         int on = 1;
188         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
189 }
190
191 int SocketEngine::RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, sockaddr *from, socklen_t *fromlen)
192 {
193         int nbRecvd = recvfrom(fd->GetFd(), (char*)buf, len, flags, from, fromlen);
194         if (nbRecvd > 0)
195                 stats.Update(nbRecvd, 0);
196         return nbRecvd;
197 }
198
199 int SocketEngine::Send(EventHandler* fd, const void *buf, size_t len, int flags)
200 {
201         int nbSent = send(fd->GetFd(), (const char*)buf, len, flags);
202         if (nbSent > 0)
203                 stats.Update(0, nbSent);
204         return nbSent;
205 }
206
207 int SocketEngine::Recv(EventHandler* fd, void *buf, size_t len, int flags)
208 {
209         int nbRecvd = recv(fd->GetFd(), (char*)buf, len, flags);
210         if (nbRecvd > 0)
211                 stats.Update(nbRecvd, 0);
212         return nbRecvd;
213 }
214
215 int SocketEngine::SendTo(EventHandler* fd, const void *buf, size_t len, int flags, const sockaddr *to, socklen_t tolen)
216 {
217         int nbSent = sendto(fd->GetFd(), (const char*)buf, len, flags, to, tolen);
218         if (nbSent > 0)
219                 stats.Update(0, nbSent);
220         return nbSent;
221 }
222
223 int SocketEngine::Connect(EventHandler* fd, const sockaddr *serv_addr, socklen_t addrlen)
224 {
225         int ret = connect(fd->GetFd(), serv_addr, addrlen);
226 #ifdef _WIN32
227         if ((ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEWOULDBLOCK))
228                 errno = EINPROGRESS;
229 #endif
230         return ret;
231 }
232
233 int SocketEngine::Shutdown(EventHandler* fd, int how)
234 {
235         return shutdown(fd->GetFd(), how);
236 }
237
238 int SocketEngine::Bind(int fd, const irc::sockets::sockaddrs& addr)
239 {
240         return bind(fd, &addr.sa, addr.sa_size());
241 }
242
243 int SocketEngine::Listen(int sockfd, int backlog)
244 {
245         return listen(sockfd, backlog);
246 }
247
248 int SocketEngine::Shutdown(int fd, int how)
249 {
250         return shutdown(fd, how);
251 }
252
253 void SocketEngine::RecoverFromFork()
254 {
255 }
256
257 void SocketEngine::Statistics::Update(size_t len_in, size_t len_out)
258 {
259         CheckFlush();
260         indata += len_in;
261         outdata += len_out;
262 }
263
264 void SocketEngine::Statistics::CheckFlush() const
265 {
266         // Reset the in/out byte counters if it has been more than a second
267         time_t now = ServerInstance->Time();
268         if (lastempty != now)
269         {
270                 lastempty = now;
271                 indata = outdata = 0;
272         }
273 }
274
275 void SocketEngine::Statistics::GetBandwidth(float& kbitpersec_in, float& kbitpersec_out, float& kbitpersec_total) const
276 {
277         CheckFlush();
278         float in_kbit = indata * 8;
279         float out_kbit = outdata * 8;
280         kbitpersec_total = ((in_kbit + out_kbit) / 1024);
281         kbitpersec_in = in_kbit / 1024;
282         kbitpersec_out = out_kbit / 1024;
283 }
284
285 std::string SocketEngine::LastError()
286 {
287 #ifndef _WIN32
288         return strerror(errno);
289 #else
290         char szErrorString[500];
291         DWORD dwErrorCode = WSAGetLastError();
292         if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)szErrorString, _countof(szErrorString), NULL) == 0)
293                 sprintf_s(szErrorString, _countof(szErrorString), "Error code: %u", dwErrorCode);
294         return szErrorString;
295 #endif
296 }
297
298 std::string SocketEngine::GetError(int errnum)
299 {
300 #ifndef _WIN32
301         return strerror(errnum);
302 #else
303         WSASetLastError(errnum);
304         return LastError();
305 #endif
306 }