]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengines/socketengine_poll.cpp
Merge pull request #274 from ChrisTX/insp20+gnutlsdeprecation
[user/henk/code/inspircd.git] / src / socketengines / socketengine_poll.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2009 Uli Schlachter <psychon@znc.in>
6  *   Copyright (C) 2009 Craig Edwards <craigedwards@brainbox.cc>
7  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24 #include "exitcodes.h"
25
26 #ifndef SOCKETENGINE_POLL
27 #define SOCKETENGINE_POLL
28
29 #include <iostream>
30 #include <vector>
31 #include <string>
32 #include <map>
33 #include "inspircd_config.h"
34 #include "inspircd.h"
35 #include "socketengine.h"
36
37 #ifndef _WIN32
38         #ifndef __USE_XOPEN
39             #define __USE_XOPEN /* fuck every fucking OS ever made. needed by poll.h to work.*/
40         #endif
41         #include <poll.h>
42         #include <sys/poll.h>
43 #else
44         /* *grumble* */
45         #define struct pollfd WSAPOLLFD
46         #define poll WSAPoll
47 #endif
48
49 class InspIRCd;
50
51 /** A specialisation of the SocketEngine class, designed to use poll().
52  */
53 class PollEngine : public SocketEngine
54 {
55 private:
56         /** These are used by poll() to hold socket events
57          */
58         struct pollfd *events;
59         /** This map maps fds to an index in the events array.
60          */
61         std::map<int, unsigned int> fd_mappings;
62 public:
63         /** Create a new PollEngine
64          */
65         PollEngine();
66         /** Delete a PollEngine
67          */
68         virtual ~PollEngine();
69         virtual bool AddFd(EventHandler* eh, int event_mask);
70         virtual void OnSetEvent(EventHandler* eh, int old_mask, int new_mask);
71         virtual EventHandler* GetRef(int fd);
72         virtual void DelFd(EventHandler* eh);
73         virtual int DispatchEvents();
74         virtual std::string GetName();
75 };
76
77 #endif
78
79 #include <ulimit.h>
80 #ifdef __FreeBSD__
81         #include <sys/sysctl.h>
82 #endif
83
84 PollEngine::PollEngine()
85 {
86         CurrentSetSize = 0;
87 #ifndef __FreeBSD__
88         int max = ulimit(4, 0);
89         if (max > 0)
90         {
91                 MAX_DESCRIPTORS = max;
92         }
93         else
94         {
95                 ServerInstance->Logs->Log("SOCKET", DEFAULT, "ERROR: Can't determine maximum number of open sockets: %s", strerror(errno));
96                 std::cout << "ERROR: Can't determine maximum number of open sockets: " << strerror(errno) << std::endl;
97                 ServerInstance->Exit(EXIT_STATUS_SOCKETENGINE);
98         }
99 #else
100         int mib[2];
101         size_t len;
102
103         mib[0] = CTL_KERN;
104         mib[1] = KERN_MAXFILES;
105         len = sizeof(MAX_DESCRIPTORS);
106         sysctl(mib, 2, &MAX_DESCRIPTORS, &len, NULL, 0);
107 #endif
108
109         ref = new EventHandler* [GetMaxFds()];
110         events = new struct pollfd[GetMaxFds()];
111
112         memset(events, 0, GetMaxFds() * sizeof(struct pollfd));
113         memset(ref, 0, GetMaxFds() * sizeof(EventHandler*));
114 }
115
116 PollEngine::~PollEngine()
117 {
118         // No destruction required, either.
119         delete[] ref;
120         delete[] events;
121 }
122
123 static int mask_to_poll(int event_mask)
124 {
125         int rv = 0;
126         if (event_mask & (FD_WANT_POLL_READ | FD_WANT_FAST_READ))
127                 rv |= POLLIN;
128         if (event_mask & (FD_WANT_POLL_WRITE | FD_WANT_FAST_WRITE | FD_WANT_SINGLE_WRITE))
129                 rv |= POLLOUT;
130         return rv;
131 }
132
133 bool PollEngine::AddFd(EventHandler* eh, int event_mask)
134 {
135         int fd = eh->GetFd();
136         if ((fd < 0) || (fd > GetMaxFds() - 1))
137         {
138                 ServerInstance->Logs->Log("SOCKET",DEBUG,"AddFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
139                 return false;
140         }
141
142         if (fd_mappings.find(fd) != fd_mappings.end())
143         {
144                 ServerInstance->Logs->Log("SOCKET",DEBUG,"Attempt to add duplicate fd: %d", fd);
145                 return false;
146         }
147
148         unsigned int index = CurrentSetSize;
149
150         fd_mappings[fd] = index;
151         ref[index] = eh;
152         events[index].fd = fd;
153         events[index].events = mask_to_poll(event_mask);
154
155         ServerInstance->Logs->Log("SOCKET", DEBUG,"New file descriptor: %d (%d; index %d)", fd, events[fd].events, index);
156         SocketEngine::SetEventMask(eh, event_mask);
157         CurrentSetSize++;
158         return true;
159 }
160
161 EventHandler* PollEngine::GetRef(int fd)
162 {
163         std::map<int, unsigned int>::iterator it = fd_mappings.find(fd);
164         if (it == fd_mappings.end())
165                 return NULL;
166         return ref[it->second];
167 }
168
169 void PollEngine::OnSetEvent(EventHandler* eh, int old_mask, int new_mask)
170 {
171         std::map<int, unsigned int>::iterator it = fd_mappings.find(eh->GetFd());
172         if (it == fd_mappings.end())
173         {
174                 ServerInstance->Logs->Log("SOCKET",DEBUG,"SetEvents() on unknown fd: %d", eh->GetFd());
175                 return;
176         }
177
178         events[it->second].events = mask_to_poll(new_mask);
179 }
180
181 void PollEngine::DelFd(EventHandler* eh)
182 {
183         int fd = eh->GetFd();
184         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
185         {
186                 ServerInstance->Logs->Log("SOCKET", DEBUG, "DelFd out of range: (fd: %d, max: %d)", fd, GetMaxFds());
187                 return;
188         }
189
190         std::map<int, unsigned int>::iterator it = fd_mappings.find(fd);
191         if (it == fd_mappings.end())
192         {
193                 ServerInstance->Logs->Log("SOCKET",DEBUG,"DelFd() on unknown fd: %d", fd);
194                 return;
195         }
196
197         unsigned int index = it->second;
198         unsigned int last_index = CurrentSetSize - 1;
199         int last_fd = events[last_index].fd;
200
201         if (index != last_index)
202         {
203                 // We need to move the last fd we got into this gap (gaps are evil!)
204
205                 // So update the mapping for the last fd to its new position
206                 fd_mappings[last_fd] = index;
207
208                 // move last_fd from last_index into index
209                 events[index].fd = last_fd;
210                 events[index].events = events[last_index].events;
211
212                 ref[index] = ref[last_index];
213         }
214
215         // Now remove all data for the last fd we got into out list.
216         // Above code made sure this always is right
217         fd_mappings.erase(it);
218         events[last_index].fd = 0;
219         events[last_index].events = 0;
220         ref[last_index] = NULL;
221
222         CurrentSetSize--;
223
224         ServerInstance->Logs->Log("SOCKET", DEBUG, "Remove file descriptor: %d (index: %d) "
225                         "(Filled gap with: %d (index: %d))", fd, index, last_fd, last_index);
226 }
227
228 int PollEngine::DispatchEvents()
229 {
230         int i = poll(events, CurrentSetSize, 1000);
231         int index;
232         socklen_t codesize = sizeof(int);
233         int errcode;
234         int processed = 0;
235         ServerInstance->UpdateTime();
236
237         if (i > 0)
238         {
239                 for (index = 0; index < CurrentSetSize && processed != i; index++)
240                 {
241                         if (events[index].revents)
242                                 processed++;
243                         EventHandler* eh = ref[index];
244                         if (!eh)
245                                 continue;
246
247                         if (events[index].revents & POLLHUP)
248                         {
249                                 eh->HandleEvent(EVENT_ERROR, 0);
250                                 continue;
251                         }
252
253                         if (events[index].revents & POLLERR)
254                         {
255                                 // Get fd
256                                 int fd = events[index].fd;
257
258                                 // Get error number
259                                 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &errcode, &codesize) < 0)
260                                         errcode = errno;
261                                 eh->HandleEvent(EVENT_ERROR, errcode);
262                                 continue;
263                         }
264
265                         if (events[index].revents & POLLIN)
266                         {
267                                 SetEventMask(eh, eh->GetEventMask() & ~FD_READ_WILL_BLOCK);
268                                 eh->HandleEvent(EVENT_READ);
269                                 if (eh != ref[index])
270                                         // whoops, deleted out from under us
271                                         continue;
272                         }
273                         
274                         if (events[index].revents & POLLOUT)
275                         {
276                                 int mask = eh->GetEventMask();
277                                 mask &= ~(FD_WRITE_WILL_BLOCK | FD_WANT_SINGLE_WRITE);
278                                 SetEventMask(eh, mask);
279                                 events[index].events = mask_to_poll(mask);
280                                 eh->HandleEvent(EVENT_WRITE);
281                         }
282                 }
283         }
284
285         return i;
286 }
287
288 std::string PollEngine::GetName()
289 {
290         return "poll";
291 }
292
293 SocketEngine* CreateSocketEngine()
294 {
295         return new PollEngine;
296 }