]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengine.cpp
300a08bccca8c4c3fd2fcde7bfe020064e157a13
[user/henk/code/inspircd.git] / src / socketengine.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core */
15
16 /*********        DEFAULTS       **********/
17
18 /* $ExtraSources: socketengines/socketengine_select.cpp */
19 /* $ExtraObjects: socketengine_select.o */
20
21 /* $If: USE_POLL */
22 /* $ExtraSources: socketengines/socketengine_poll.cpp */
23 /* $ExtraObjects: socketengine_poll.o */
24 /* $EndIf */
25
26 /* $If: USE_KQUEUE */
27 /* $ExtraSources: socketengines/socketengine_kqueue.cpp */
28 /* $ExtraObjects: socketengine_kqueue.o */
29 /* $EndIf */
30
31 /* $If: USE_EPOLL */
32 /* $ExtraSources: socketengines/socketengine_epoll.cpp */
33 /* $ExtraObjects: socketengine_epoll.o */
34 /* $EndIf */
35
36 /* $If: USE_PORTS */
37 /* $ExtraSources: socketengines/socketengine_ports.cpp */
38 /* $ExtraObjects: socketengine_ports.o */
39 /* $EndIf */
40
41 #include "inspircd.h"
42 #include "socketengine.h"
43
44 EventHandler::EventHandler()
45 {
46         this->IOHook = NULL;
47 }
48
49 bool EventHandler::AddIOHook(Module *IOHooker)
50 {
51         if (this->IOHook)
52                 return false;
53
54         this->IOHook = IOHooker;
55         return true;
56 }
57
58 bool EventHandler::DelIOHook()
59 {
60         if (!this->IOHook)
61                 return false;
62
63         this->IOHook = NULL;
64         return true;
65 }
66
67 Module *EventHandler::GetIOHook()
68 {
69         return this->IOHook;
70 }
71
72 int EventHandler::GetFd()
73 {
74         return this->fd;
75 }
76
77 void EventHandler::SetFd(int FD)
78 {
79         this->fd = FD;
80 }
81
82 bool EventHandler::Readable()
83 {
84         return true;
85 }
86
87 bool EventHandler::Writeable()
88 {
89         return false;
90 }
91
92 void SocketEngine::WantWrite(EventHandler* eh)
93 {
94 }
95
96 SocketEngine::SocketEngine(InspIRCd* Instance) : ServerInstance(Instance)
97 {
98         TotalEvents = WriteEvents = ReadEvents = ErrorEvents = 0;
99         lastempty = ServerInstance->Time();
100         indata = outdata = 0;
101 }
102
103 SocketEngine::~SocketEngine()
104 {
105 }
106
107 bool SocketEngine::AddFd(EventHandler* eh)
108 {
109         return true;
110 }
111
112 bool SocketEngine::HasFd(int fd)
113 {
114         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
115                 return false;
116         return ref[fd];
117 }
118
119 EventHandler* SocketEngine::GetRef(int fd)
120 {
121         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
122                 return 0;
123         return ref[fd];
124 }
125
126 bool SocketEngine::DelFd(EventHandler* eh, bool force)
127 {
128         return true;
129 }
130
131 int SocketEngine::GetMaxFds()
132 {
133         return 0;
134 }
135
136 int SocketEngine::GetRemainingFds()
137 {
138         return 0;
139 }
140
141 int SocketEngine::DispatchEvents()
142 {
143         return 0;
144 }
145
146 std::string SocketEngine::GetName()
147 {
148         return "misconfigured";
149 }
150
151 bool SocketEngine::BoundsCheckFd(EventHandler* eh)
152 {       
153         if (!eh)
154                 return false;
155         if ((eh->GetFd() < 0) || (eh->GetFd() > MAX_DESCRIPTORS))
156                 return false;
157         return true;
158 }
159
160
161 int SocketEngine::Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen)
162 {
163         return accept(fd->GetFd(), addr, addrlen);
164 }
165
166 int SocketEngine::Close(EventHandler* fd)
167 {
168 #ifdef WINDOWS
169         return closesocket(fd->GetFd());
170 #else
171         return close(fd->GetFd());
172 #endif
173 }
174
175 int SocketEngine::Close(int fd)
176 {
177 #ifdef WINDOWS
178         return closesocket(fd);
179 #else
180         return close(fd);
181 #endif
182 }
183
184 int SocketEngine::Blocking(int fd)
185 {
186 #ifdef WINDOWS
187         unsigned long opt = 0;
188         return ioctlsocket(fd, FIONBIO, &opt);
189 #else
190         int flags = fcntl(fd, F_GETFL, 0);
191         return fcntl(fd, F_SETFL, flags ^ O_NONBLOCK);
192 #endif
193 }
194
195 int SocketEngine::NonBlocking(int fd)
196 {
197 #ifdef WINDOWS
198         unsigned long opt = 1;
199         return ioctlsocket(fd, FIONBIO, &opt);
200 #else
201         int flags = fcntl(fd, F_GETFL, 0);
202         return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
203 #endif
204 }
205
206 int SocketEngine::GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen)
207 {
208         return getsockname(fd->GetFd(), name, namelen);
209 }
210
211 int SocketEngine::RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, sockaddr *from, socklen_t *fromlen)
212 {
213         this->UpdateStats(len, 0);
214         return recvfrom(fd->GetFd(), (char*)buf, len, flags, from, fromlen);
215 }
216
217 int SocketEngine::Send(EventHandler* fd, const void *buf, size_t len, int flags)
218 {
219         this->UpdateStats(0, len);
220         return send(fd->GetFd(), (const char*)buf, len, flags);
221 }
222
223 int SocketEngine::Recv(EventHandler* fd, void *buf, size_t len, int flags)
224 {
225         this->UpdateStats(len, 0);
226         return recv(fd->GetFd(), (char*)buf, len, flags);
227 }
228
229 int SocketEngine::SendTo(EventHandler* fd, const void *buf, size_t len, int flags, const sockaddr *to, socklen_t tolen)
230 {
231         this->UpdateStats(0, len);
232         return sendto(fd->GetFd(), (const char*)buf, len, flags, to, tolen);
233 }
234
235 int SocketEngine::Connect(EventHandler* fd, const sockaddr *serv_addr, socklen_t addrlen)
236 {
237         return connect(fd->GetFd(), serv_addr, addrlen);
238 }
239
240 int SocketEngine::Shutdown(EventHandler* fd, int how)
241 {
242         return shutdown(fd->GetFd(), how);
243 }
244
245 int SocketEngine::Bind(int fd, const sockaddr *my_addr, socklen_t addrlen)
246 {
247         return bind(fd, my_addr, addrlen);
248 }
249
250 int SocketEngine::Listen(int sockfd, int backlog)
251 {
252         return listen(sockfd, backlog);
253 }
254
255 int SocketEngine::Shutdown(int fd, int how)
256 {
257         return shutdown(fd, how);
258 }
259
260 void SocketEngine::RecoverFromFork()
261 {
262 }
263
264 void SocketEngine::UpdateStats(size_t len_in, size_t len_out)
265 {
266         if (lastempty != ServerInstance->Time())
267         {
268                 lastempty = ServerInstance->Time();
269                 indata = outdata = 0;
270         }
271         indata += len_in;
272         outdata += len_out;
273 }
274
275 void SocketEngine::GetStats(float &kbitpersec_in, float &kbitpersec_out, float &kbitpersec_total)
276 {
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;
283 }