2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
26 EventHandler::EventHandler()
32 void EventHandler::SetFd(int FD)
37 SocketEngine::SocketEngine()
39 TotalEvents = WriteEvents = ReadEvents = ErrorEvents = 0;
40 lastempty = ServerInstance->Time();
44 SocketEngine::~SocketEngine()
48 void SocketEngine::SetEventMask(EventHandler* eh, int mask)
50 eh->event_mask = mask;
53 void SocketEngine::ChangeEventMask(EventHandler* eh, int change)
55 int old_m = eh->event_mask;
58 // if we are changing read/write type, remove the previously set bit
59 if (change & FD_WANT_READ_MASK)
60 new_m &= ~FD_WANT_READ_MASK;
61 if (change & FD_WANT_WRITE_MASK)
62 new_m &= ~FD_WANT_WRITE_MASK;
64 // if adding a trial read/write, insert it into the set
65 if (change & FD_TRIAL_NOTE_MASK && !(old_m & FD_TRIAL_NOTE_MASK))
66 trials.insert(eh->GetFd());
72 eh->event_mask = new_m;
73 OnSetEvent(eh, old_m, new_m);
76 void SocketEngine::DispatchTrialWrites()
78 std::vector<int> working_list;
79 working_list.reserve(trials.size());
80 working_list.assign(trials.begin(), trials.end());
82 for(unsigned int i=0; i < working_list.size(); i++)
84 int fd = working_list[i];
85 EventHandler* eh = GetRef(fd);
88 int mask = eh->event_mask;
89 eh->event_mask &= ~(FD_ADD_TRIAL_READ | FD_ADD_TRIAL_WRITE);
90 if ((mask & (FD_ADD_TRIAL_READ | FD_READ_WILL_BLOCK)) == FD_ADD_TRIAL_READ)
91 eh->HandleEvent(EVENT_READ, 0);
92 if ((mask & (FD_ADD_TRIAL_WRITE | FD_WRITE_WILL_BLOCK)) == FD_ADD_TRIAL_WRITE)
93 eh->HandleEvent(EVENT_WRITE, 0);
97 bool SocketEngine::HasFd(int fd)
99 if ((fd < 0) || (fd > GetMaxFds()))
101 return (ref[fd] != NULL);
104 EventHandler* SocketEngine::GetRef(int fd)
106 if ((fd < 0) || (fd > GetMaxFds()))
111 bool SocketEngine::BoundsCheckFd(EventHandler* eh)
115 if ((eh->GetFd() < 0) || (eh->GetFd() > GetMaxFds()))
121 int SocketEngine::Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen)
123 return accept(fd->GetFd(), addr, addrlen);
126 int SocketEngine::Close(EventHandler* fd)
129 return closesocket(fd->GetFd());
131 return close(fd->GetFd());
135 int SocketEngine::Close(int fd)
138 return closesocket(fd);
144 int SocketEngine::Blocking(int fd)
147 unsigned long opt = 0;
148 return ioctlsocket(fd, FIONBIO, &opt);
150 int flags = fcntl(fd, F_GETFL, 0);
151 return fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
155 int SocketEngine::NonBlocking(int fd)
158 unsigned long opt = 1;
159 return ioctlsocket(fd, FIONBIO, &opt);
161 int flags = fcntl(fd, F_GETFL, 0);
162 return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
166 void SocketEngine::SetReuse(int fd)
169 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
172 int SocketEngine::RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, sockaddr *from, socklen_t *fromlen)
174 int nbRecvd = recvfrom(fd->GetFd(), (char*)buf, len, flags, from, fromlen);
176 this->UpdateStats(nbRecvd, 0);
180 int SocketEngine::Send(EventHandler* fd, const void *buf, size_t len, int flags)
182 int nbSent = send(fd->GetFd(), (const char*)buf, len, flags);
184 this->UpdateStats(0, nbSent);
188 int SocketEngine::Recv(EventHandler* fd, void *buf, size_t len, int flags)
190 int nbRecvd = recv(fd->GetFd(), (char*)buf, len, flags);
192 this->UpdateStats(nbRecvd, 0);
196 int SocketEngine::SendTo(EventHandler* fd, const void *buf, size_t len, int flags, const sockaddr *to, socklen_t tolen)
198 int nbSent = sendto(fd->GetFd(), (const char*)buf, len, flags, to, tolen);
200 this->UpdateStats(0, nbSent);
204 int SocketEngine::Connect(EventHandler* fd, const sockaddr *serv_addr, socklen_t addrlen)
206 int ret = connect(fd->GetFd(), serv_addr, addrlen);
208 if ((ret == SOCKET_ERROR) && (WSAGetLastError() == WSAEWOULDBLOCK))
214 int SocketEngine::Shutdown(EventHandler* fd, int how)
216 return shutdown(fd->GetFd(), how);
219 int SocketEngine::Bind(int fd, const irc::sockets::sockaddrs& addr)
221 return bind(fd, &addr.sa, addr.sa_size());
224 int SocketEngine::Listen(int sockfd, int backlog)
226 return listen(sockfd, backlog);
229 int SocketEngine::Shutdown(int fd, int how)
231 return shutdown(fd, how);
234 void SocketEngine::RecoverFromFork()
238 void SocketEngine::UpdateStats(size_t len_in, size_t len_out)
240 if (lastempty != ServerInstance->Time())
242 lastempty = ServerInstance->Time();
243 indata = outdata = 0;
249 void SocketEngine::GetStats(float &kbitpersec_in, float &kbitpersec_out, float &kbitpersec_total)
251 UpdateStats(0, 0); /* Forces emptying of the values if its been more than a second */
252 float in_kbit = indata * 8;
253 float out_kbit = outdata * 8;
254 kbitpersec_total = ((in_kbit + out_kbit) / 1024);
255 kbitpersec_in = in_kbit / 1024;
256 kbitpersec_out = out_kbit / 1024;
259 std::string SocketEngine::LastError()
262 return strerror(errno);
264 char szErrorString[500];
265 DWORD dwErrorCode = WSAGetLastError();
266 if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)szErrorString, _countof(szErrorString), NULL) == 0)
267 sprintf_s(szErrorString, _countof(szErrorString), "Error code: %u", dwErrorCode);
269 std::string::size_type p;
270 std::string ret = szErrorString;
271 while ((p = ret.find_last_of("\r\n")) != std::string::npos)
278 std::string SocketEngine::GetError(int errnum)
281 return strerror(errnum);
283 WSASetLastError(errnum);