1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 /********* DEFAULTS **********/
18 /* $ExtraSources: socketengines/socketengine_select.cpp */
19 /* $ExtraObjects: socketengine_select.o */
22 /* $ExtraSources: socketengines/socketengine_poll.cpp */
23 /* $ExtraObjects: socketengine_poll.o */
27 /* $ExtraSources: socketengines/socketengine_kqueue.cpp */
28 /* $ExtraObjects: socketengine_kqueue.o */
32 /* $ExtraSources: socketengines/socketengine_epoll.cpp */
33 /* $ExtraObjects: socketengine_epoll.o */
37 /* $ExtraSources: socketengines/socketengine_ports.cpp */
38 /* $ExtraObjects: socketengine_ports.o */
42 #include "socketengine.h"
44 EventHandler::EventHandler()
49 bool EventHandler::AddIOHook(Module *IOHooker)
54 this->IOHook = IOHooker;
58 bool EventHandler::DelIOHook()
67 Module *EventHandler::GetIOHook()
72 int EventHandler::GetFd()
77 void EventHandler::SetFd(int FD)
82 bool EventHandler::Readable()
87 bool EventHandler::Writeable()
92 void SocketEngine::WantWrite(EventHandler* eh)
96 SocketEngine::SocketEngine(InspIRCd* Instance) : ServerInstance(Instance)
98 TotalEvents = WriteEvents = ReadEvents = ErrorEvents = 0;
99 lastempty = ServerInstance->Time();
100 indata = outdata = 0;
103 SocketEngine::~SocketEngine()
107 bool SocketEngine::AddFd(EventHandler* eh)
112 bool SocketEngine::HasFd(int fd)
114 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
119 EventHandler* SocketEngine::GetRef(int fd)
121 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
126 bool SocketEngine::DelFd(EventHandler* eh, bool force)
131 int SocketEngine::GetMaxFds()
136 int SocketEngine::GetRemainingFds()
141 int SocketEngine::DispatchEvents()
146 std::string SocketEngine::GetName()
148 return "misconfigured";
151 bool SocketEngine::BoundsCheckFd(EventHandler* eh)
155 if ((eh->GetFd() < 0) || (eh->GetFd() > MAX_DESCRIPTORS))
161 int SocketEngine::Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen)
163 return accept(fd->GetFd(), addr, addrlen);
166 int SocketEngine::Close(EventHandler* fd)
169 return closesocket(fd->GetFd());
171 return close(fd->GetFd());
175 int SocketEngine::Close(int fd)
178 return closesocket(fd);
184 int SocketEngine::Blocking(int fd)
187 unsigned long opt = 0;
188 return ioctlsocket(fd, FIONBIO, &opt);
190 int flags = fcntl(fd, F_GETFL, 0);
191 return fcntl(fd, F_SETFL, flags ^ O_NONBLOCK);
195 int SocketEngine::NonBlocking(int fd)
198 unsigned long opt = 1;
199 return ioctlsocket(fd, FIONBIO, &opt);
201 int flags = fcntl(fd, F_GETFL, 0);
202 return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
206 int SocketEngine::GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen)
208 return getsockname(fd->GetFd(), name, namelen);
211 int SocketEngine::RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, sockaddr *from, socklen_t *fromlen)
213 this->UpdateStats(len, 0);
214 return recvfrom(fd->GetFd(), (char*)buf, len, flags, from, fromlen);
217 int SocketEngine::Send(EventHandler* fd, const void *buf, size_t len, int flags)
219 this->UpdateStats(0, len);
220 return send(fd->GetFd(), (const char*)buf, len, flags);
223 int SocketEngine::Recv(EventHandler* fd, void *buf, size_t len, int flags)
225 this->UpdateStats(len, 0);
226 return recv(fd->GetFd(), (char*)buf, len, flags);
229 int SocketEngine::SendTo(EventHandler* fd, const void *buf, size_t len, int flags, const sockaddr *to, socklen_t tolen)
231 this->UpdateStats(0, len);
232 return sendto(fd->GetFd(), (const char*)buf, len, flags, to, tolen);
235 int SocketEngine::Connect(EventHandler* fd, const sockaddr *serv_addr, socklen_t addrlen)
237 return connect(fd->GetFd(), serv_addr, addrlen);
240 int SocketEngine::Shutdown(EventHandler* fd, int how)
242 return shutdown(fd->GetFd(), how);
245 int SocketEngine::Bind(int fd, const sockaddr *my_addr, socklen_t addrlen)
247 return bind(fd, my_addr, addrlen);
250 int SocketEngine::Listen(int sockfd, int backlog)
252 return listen(sockfd, backlog);
255 int SocketEngine::Shutdown(int fd, int how)
257 return shutdown(fd, how);
260 void SocketEngine::RecoverFromFork()
264 void SocketEngine::UpdateStats(size_t len_in, size_t len_out)
266 if (lastempty != ServerInstance->Time())
268 lastempty = ServerInstance->Time();
269 indata = outdata = 0;
275 void SocketEngine::GetStats(float &kbitpersec_in, float &kbitpersec_out, float &kbitpersec_total)
277 UpdateStats(0, 0); /* Forces emptying of the values if its been more than a second */
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;