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 * ---------------------------------------------------
14 /* $Core: libIRCDsocketengine */
16 /********* DEFAULTS **********/
17 /* $ExtraSources: socketengines/socketengine_select.cpp */
18 /* $ExtraObjects: socketengine_select.o */
21 /* $ExtraSources: socketengines/socketengine_kqueue.cpp */
22 /* $ExtraObjects: socketengine_kqueue.o */
26 /* $ExtraSources: socketengines/socketengine_epoll.cpp */
27 /* $ExtraObjects: socketengine_epoll.o */
31 /* $ExtraSources: socketengines/socketengine_ports.cpp */
32 /* $ExtraObjects: socketengine_ports.o */
36 #include "socketengine.h"
38 int EventHandler::GetFd()
43 void EventHandler::SetFd(int FD)
48 bool EventHandler::Readable()
53 bool EventHandler::Writeable()
58 void SocketEngine::WantWrite(EventHandler* eh)
62 SocketEngine::SocketEngine(InspIRCd* Instance) : ServerInstance(Instance)
64 TotalEvents = WriteEvents = ReadEvents = ErrorEvents = 0;
65 lastempty = time(NULL);
69 SocketEngine::~SocketEngine()
73 bool SocketEngine::AddFd(EventHandler* eh)
78 bool SocketEngine::HasFd(int fd)
80 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
85 EventHandler* SocketEngine::GetRef(int fd)
87 if ((fd < 0) || (fd > MAX_DESCRIPTORS))
92 bool SocketEngine::DelFd(EventHandler* eh, bool force)
97 int SocketEngine::GetMaxFds()
102 int SocketEngine::GetRemainingFds()
107 int SocketEngine::DispatchEvents()
112 std::string SocketEngine::GetName()
114 return "misconfigured";
117 bool SocketEngine::BoundsCheckFd(EventHandler* eh)
121 if ((eh->GetFd() < 0) || (eh->GetFd() > MAX_DESCRIPTORS))
127 int SocketEngine::Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen)
129 return accept(fd->GetFd(), addr, addrlen);
132 int SocketEngine::Close(EventHandler* fd)
135 return closesocket(fd->GetFd());
137 return close(fd->GetFd());
141 int SocketEngine::Close(int fd)
144 return closesocket(fd);
150 int SocketEngine::Blocking(int fd)
153 unsigned long opt = 0;
154 return ioctlsocket(fd, FIONBIO, &opt);
156 int flags = fcntl(fd, F_GETFL, 0);
157 return fcntl(fd, F_SETFL, flags ^ O_NONBLOCK);
161 int SocketEngine::NonBlocking(int fd)
164 unsigned long opt = 1;
165 return ioctlsocket(fd, FIONBIO, &opt);
167 int flags = fcntl(fd, F_GETFL, 0);
168 return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
172 int SocketEngine::GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen)
174 return getsockname(fd->GetFd(), name, namelen);
177 int SocketEngine::RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, sockaddr *from, socklen_t *fromlen)
179 this->UpdateStats(len, 0);
180 return recvfrom(fd->GetFd(), (char*)buf, len, flags, from, fromlen);
183 int SocketEngine::Send(EventHandler* fd, const void *buf, size_t len, int flags)
185 this->UpdateStats(0, len);
186 return send(fd->GetFd(), (const char*)buf, len, flags);
189 int SocketEngine::Recv(EventHandler* fd, void *buf, size_t len, int flags)
191 this->UpdateStats(len, 0);
192 return recv(fd->GetFd(), (char*)buf, len, flags);
195 int SocketEngine::SendTo(EventHandler* fd, const void *buf, size_t len, int flags, const sockaddr *to, socklen_t tolen)
197 this->UpdateStats(0, len);
198 return sendto(fd->GetFd(), (const char*)buf, len, flags, to, tolen);
201 int SocketEngine::Connect(EventHandler* fd, const sockaddr *serv_addr, socklen_t addrlen)
203 return connect(fd->GetFd(), serv_addr, addrlen);
206 int SocketEngine::Shutdown(EventHandler* fd, int how)
208 return shutdown(fd->GetFd(), how);
211 int SocketEngine::Bind(int fd, const sockaddr *my_addr, socklen_t addrlen)
213 return bind(fd, my_addr, addrlen);
216 int SocketEngine::Listen(int sockfd, int backlog)
218 return listen(sockfd, backlog);
221 int SocketEngine::Shutdown(int fd, int how)
223 return shutdown(fd, how);
226 void SocketEngine::RecoverFromFork()
230 void SocketEngine::UpdateStats(size_t len_in, size_t len_out)
232 if (lastempty != time(NULL))
234 lastempty = time(NULL);
235 indata = outdata = 0;
241 void SocketEngine::GetStats(float &kbitpersec_in, float &kbitpersec_out, float &kbitpersec_total)
243 UpdateStats(0, 0); /* Forces emptying of the values if its been more than a second */
244 float in_kbit = indata * 8;
245 float out_kbit = outdata * 8;
246 kbitpersec_total = ((in_kbit + out_kbit) / 1024);
247 kbitpersec_in = in_kbit / 1024;
248 kbitpersec_out = out_kbit / 1024;