]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengine.cpp
Modify aliases to use a multimap rather than a vector for storage + a map for quick...
[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 int EventHandler::GetFd()
45 {
46         return this->fd;
47 }
48
49 void EventHandler::SetFd(int FD)
50 {
51         this->fd = FD;
52 }
53
54 bool EventHandler::Readable()
55 {
56         return true;
57 }
58
59 bool EventHandler::Writeable()
60 {
61         return false;
62 }
63
64 void SocketEngine::WantWrite(EventHandler* eh)
65 {
66 }
67
68 SocketEngine::SocketEngine(InspIRCd* Instance) : ServerInstance(Instance)
69 {
70         TotalEvents = WriteEvents = ReadEvents = ErrorEvents = 0;
71         lastempty = time(NULL);
72         indata = outdata = 0;
73 }
74
75 SocketEngine::~SocketEngine()
76 {
77 }
78
79 bool SocketEngine::AddFd(EventHandler* eh)
80 {
81         return true;
82 }
83
84 bool SocketEngine::HasFd(int fd)
85 {
86         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
87                 return false;
88         return ref[fd];
89 }
90
91 EventHandler* SocketEngine::GetRef(int fd)
92 {
93         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
94                 return 0;
95         return ref[fd];
96 }
97
98 bool SocketEngine::DelFd(EventHandler* eh, bool force)
99 {
100         return true;
101 }
102
103 int SocketEngine::GetMaxFds()
104 {
105         return 0;
106 }
107
108 int SocketEngine::GetRemainingFds()
109 {
110         return 0;
111 }
112
113 int SocketEngine::DispatchEvents()
114 {
115         return 0;
116 }
117
118 std::string SocketEngine::GetName()
119 {
120         return "misconfigured";
121 }
122
123 bool SocketEngine::BoundsCheckFd(EventHandler* eh)
124 {       
125         if (!eh)
126                 return false;
127         if ((eh->GetFd() < 0) || (eh->GetFd() > MAX_DESCRIPTORS))
128                 return false;
129         return true;
130 }
131
132
133 int SocketEngine::Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen)
134 {
135         return accept(fd->GetFd(), addr, addrlen);
136 }
137
138 int SocketEngine::Close(EventHandler* fd)
139 {
140 #ifdef WINDOWS
141         return closesocket(fd->GetFd());
142 #else
143         return close(fd->GetFd());
144 #endif
145 }
146
147 int SocketEngine::Close(int fd)
148 {
149 #ifdef WINDOWS
150         return closesocket(fd);
151 #else
152         return close(fd);
153 #endif
154 }
155
156 int SocketEngine::Blocking(int fd)
157 {
158 #ifdef WINDOWS
159         unsigned long opt = 0;
160         return ioctlsocket(fd, FIONBIO, &opt);
161 #else
162         int flags = fcntl(fd, F_GETFL, 0);
163         return fcntl(fd, F_SETFL, flags ^ O_NONBLOCK);
164 #endif
165 }
166
167 int SocketEngine::NonBlocking(int fd)
168 {
169 #ifdef WINDOWS
170         unsigned long opt = 1;
171         return ioctlsocket(fd, FIONBIO, &opt);
172 #else
173         int flags = fcntl(fd, F_GETFL, 0);
174         return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
175 #endif
176 }
177
178 int SocketEngine::GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen)
179 {
180         return getsockname(fd->GetFd(), name, namelen);
181 }
182
183 int SocketEngine::RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, sockaddr *from, socklen_t *fromlen)
184 {
185         this->UpdateStats(len, 0);
186         return recvfrom(fd->GetFd(), (char*)buf, len, flags, from, fromlen);
187 }
188
189 int SocketEngine::Send(EventHandler* fd, const void *buf, size_t len, int flags)
190 {
191         this->UpdateStats(0, len);
192         return send(fd->GetFd(), (const char*)buf, len, flags);
193 }
194
195 int SocketEngine::Recv(EventHandler* fd, void *buf, size_t len, int flags)
196 {
197         this->UpdateStats(len, 0);
198         return recv(fd->GetFd(), (char*)buf, len, flags);
199 }
200
201 int SocketEngine::SendTo(EventHandler* fd, const void *buf, size_t len, int flags, const sockaddr *to, socklen_t tolen)
202 {
203         this->UpdateStats(0, len);
204         return sendto(fd->GetFd(), (const char*)buf, len, flags, to, tolen);
205 }
206
207 int SocketEngine::Connect(EventHandler* fd, const sockaddr *serv_addr, socklen_t addrlen)
208 {
209         return connect(fd->GetFd(), serv_addr, addrlen);
210 }
211
212 int SocketEngine::Shutdown(EventHandler* fd, int how)
213 {
214         return shutdown(fd->GetFd(), how);
215 }
216
217 int SocketEngine::Bind(int fd, const sockaddr *my_addr, socklen_t addrlen)
218 {
219         return bind(fd, my_addr, addrlen);
220 }
221
222 int SocketEngine::Listen(int sockfd, int backlog)
223 {
224         return listen(sockfd, backlog);
225 }
226
227 int SocketEngine::Shutdown(int fd, int how)
228 {
229         return shutdown(fd, how);
230 }
231
232 void SocketEngine::RecoverFromFork()
233 {
234 }
235
236 void SocketEngine::UpdateStats(size_t len_in, size_t len_out)
237 {
238         if (lastempty != time(NULL))
239         {
240                 lastempty = time(NULL);
241                 indata = outdata = 0;
242         }
243         indata += len_in;
244         outdata += len_out;
245 }
246
247 void SocketEngine::GetStats(float &kbitpersec_in, float &kbitpersec_out, float &kbitpersec_total)
248 {
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;
255 }