1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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 int EventHandler::GetFd()
49 void EventHandler::SetFd(int FD)
54 bool EventHandler::Readable()
59 bool EventHandler::Writeable()
64 void SocketEngine::WantWrite(EventHandler* eh)
68 SocketEngine::SocketEngine(InspIRCd* Instance) : ServerInstance(Instance)
70 TotalEvents = WriteEvents = ReadEvents = ErrorEvents = 0;
71 lastempty = time(NULL);
75 SocketEngine::~SocketEngine()
79 bool SocketEngine::AddFd(EventHandler* eh)
84 bool SocketEngine::HasFd(int fd)
86 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
91 EventHandler* SocketEngine::GetRef(int fd)
93 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
98 bool SocketEngine::DelFd(EventHandler* eh, bool force)
103 int SocketEngine::GetMaxFds()
108 int SocketEngine::GetRemainingFds()
113 int SocketEngine::DispatchEvents()
118 std::string SocketEngine::GetName()
120 return "misconfigured";
123 bool SocketEngine::BoundsCheckFd(EventHandler* eh)
127 if ((eh->GetFd() < 0) || (eh->GetFd() > MAX_DESCRIPTORS))
133 int SocketEngine::Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen)
135 return accept(fd->GetFd(), addr, addrlen);
138 int SocketEngine::Close(EventHandler* fd)
141 return closesocket(fd->GetFd());
143 return close(fd->GetFd());
147 int SocketEngine::Close(int fd)
150 return closesocket(fd);
156 int SocketEngine::Blocking(int fd)
159 unsigned long opt = 0;
160 return ioctlsocket(fd, FIONBIO, &opt);
162 int flags = fcntl(fd, F_GETFL, 0);
163 return fcntl(fd, F_SETFL, flags ^ O_NONBLOCK);
167 int SocketEngine::NonBlocking(int fd)
170 unsigned long opt = 1;
171 return ioctlsocket(fd, FIONBIO, &opt);
173 int flags = fcntl(fd, F_GETFL, 0);
174 return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
178 int SocketEngine::GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen)
180 return getsockname(fd->GetFd(), name, namelen);
183 int SocketEngine::RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, sockaddr *from, socklen_t *fromlen)
185 this->UpdateStats(len, 0);
186 return recvfrom(fd->GetFd(), (char*)buf, len, flags, from, fromlen);
189 int SocketEngine::Send(EventHandler* fd, const void *buf, size_t len, int flags)
191 this->UpdateStats(0, len);
192 return send(fd->GetFd(), (const char*)buf, len, flags);
195 int SocketEngine::Recv(EventHandler* fd, void *buf, size_t len, int flags)
197 this->UpdateStats(len, 0);
198 return recv(fd->GetFd(), (char*)buf, len, flags);
201 int SocketEngine::SendTo(EventHandler* fd, const void *buf, size_t len, int flags, const sockaddr *to, socklen_t tolen)
203 this->UpdateStats(0, len);
204 return sendto(fd->GetFd(), (const char*)buf, len, flags, to, tolen);
207 int SocketEngine::Connect(EventHandler* fd, const sockaddr *serv_addr, socklen_t addrlen)
209 return connect(fd->GetFd(), serv_addr, addrlen);
212 int SocketEngine::Shutdown(EventHandler* fd, int how)
214 return shutdown(fd->GetFd(), how);
217 int SocketEngine::Bind(int fd, const sockaddr *my_addr, socklen_t addrlen)
219 return bind(fd, my_addr, addrlen);
222 int SocketEngine::Listen(int sockfd, int backlog)
224 return listen(sockfd, backlog);
227 int SocketEngine::Shutdown(int fd, int how)
229 return shutdown(fd, how);
232 void SocketEngine::RecoverFromFork()
236 void SocketEngine::UpdateStats(size_t len_in, size_t len_out)
238 if (lastempty != time(NULL))
240 lastempty = time(NULL);
241 indata = outdata = 0;
247 void SocketEngine::GetStats(float &kbitpersec_in, float &kbitpersec_out, float &kbitpersec_total)
249 UpdateStats(0, 0); /* Forces emptying of the values if its been more than a second */
250 float in_kbit = indata * 8;
251 float out_kbit = outdata * 8;
252 kbitpersec_total = ((in_kbit + out_kbit) / 1024);
253 kbitpersec_in = in_kbit / 1024;
254 kbitpersec_out = out_kbit / 1024;