]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socketengine.cpp
More stuff for allowing hostnames in <bind> and <link> again - note there is a FIXME...
[user/henk/code/inspircd.git] / src / socketengine.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #include "inspircd_config.h"
18 #include "globals.h"
19 #include "inspircd.h"
20 #ifdef USE_EPOLL
21 #include <sys/epoll.h>
22 #endif
23 #ifdef USE_KQUEUE
24 #include <sys/types.h>
25 #include <sys/event.h>
26 #include <sys/time.h>
27 #endif
28 #include <vector>
29 #include <string>
30 #include "socketengine.h"
31
32 char ref[MAX_DESCRIPTORS];
33
34 SocketEngine::SocketEngine()
35 {
36         log(DEBUG,"SocketEngine::SocketEngine()");
37 #ifdef USE_EPOLL
38         EngineHandle = epoll_create(MAX_DESCRIPTORS);
39 #endif
40 #ifdef USE_KQUEUE
41         EngineHandle = kqueue();
42 #endif
43 #ifdef USE_SELECT
44         EngineHandle = 0;
45 #endif
46         if (EngineHandle == -1)
47         {
48                 log(SPARSE,"ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
49                 log(SPARSE,"ERROR: this is a fatal error, exiting now.");
50                 printf("ERROR: Could not initialize socket engine. Your kernel probably does not have the proper features.");
51                 printf("ERROR: this is a fatal error, exiting now.");
52                 Exit(0);
53         }
54         CurrentSetSize = 0;
55 }
56
57 SocketEngine::~SocketEngine()
58 {
59         log(DEBUG,"SocketEngine::~SocketEngine()");
60 #ifdef USE_EPOLL
61         close(EngineHandle);
62 #endif
63 #ifdef USE_KQUEUE
64         close(EngineHandle);
65 #endif
66 }
67
68 char SocketEngine::GetType(int fd)
69 {
70         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
71                 return X_EMPTY_SLOT;
72         /* Mask off the top bit used for 'read/write' state */
73         return (ref[fd] & ~0x80);
74 }
75
76 bool SocketEngine::AddFd(int fd, bool readable, char type)
77 {
78         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
79         {
80                 log(DEFAULT,"ERROR: FD of %d added above max of %d",fd,MAX_DESCRIPTORS);
81                 return false;
82         }
83         if (GetRemainingFds() <= 1)
84         {
85                 log(DEFAULT,"ERROR: System out of file descriptors!");
86                 return false;
87         }
88 #ifdef USE_SELECT
89         fds[fd] = fd;
90 #endif
91         ref[fd] = type;
92         if (readable)
93         {
94                 log(DEBUG,"Set readbit");
95                 ref[fd] |= X_READBIT;
96         }
97         log(DEBUG,"Add socket %d",fd);
98 #ifdef USE_EPOLL
99         struct epoll_event ev;
100         memset(&ev,0,sizeof(struct epoll_event));
101         log(DEBUG,"epoll: Add socket to events, ep=%d socket=%d",EngineHandle,fd);
102         readable ? ev.events = EPOLLIN : ev.events = EPOLLOUT;
103         ev.data.fd = fd;
104         int i = epoll_ctl(EngineHandle, EPOLL_CTL_ADD, fd, &ev);
105         if (i < 0)
106         {
107                 log(DEBUG,"epoll: List insertion failure!");
108                 return false;
109         }
110 #endif
111 #ifdef USE_KQUEUE
112         struct kevent ke;
113         log(DEBUG,"kqueue: Add socket to events, kq=%d socket=%d",EngineHandle,fd);
114         EV_SET(&ke, fd, readable ? EVFILT_READ : EVFILT_WRITE, EV_ADD, 0, 0, NULL);
115         int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
116         if (i == -1)
117         {
118                 log(DEBUG,"kqueue: List insertion failure!");
119                 return false;
120         }
121 #endif
122         CurrentSetSize++;
123         return true;
124 }
125
126 bool SocketEngine::HasFd(int fd)
127 {
128         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
129                 return false;
130         return (ref[fd] != 0);
131 }
132
133 bool SocketEngine::DelFd(int fd)
134 {
135         log(DEBUG,"SocketEngine::DelFd(%d)",fd);
136
137         if ((fd < 0) || (fd > MAX_DESCRIPTORS))
138                 return false;
139
140 #ifdef USE_SELECT
141         std::map<int,int>::iterator t = fds.find(fd);
142         if (t != fds.end())
143         {
144                 fds.erase(t);
145                 log(DEBUG,"Deleted fd %d",fd);
146         }
147 #endif
148 #ifdef USE_KQUEUE
149         struct kevent ke;
150         EV_SET(&ke, fd, ref[fd] & X_READBIT ? EVFILT_READ : EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
151         int i = kevent(EngineHandle, &ke, 1, 0, 0, NULL);
152         if (i == -1)
153         {
154                 log(DEBUG,"kqueue: Failed to remove socket from queue!");
155                 return false;
156         }
157 #endif
158 #ifdef USE_EPOLL
159         struct epoll_event ev;
160         memset(&ev,0,sizeof(struct epoll_event));
161         ref[fd] && X_READBIT ? ev.events = EPOLLIN : ev.events = EPOLLOUT;
162         ev.data.fd = fd;
163         int i = epoll_ctl(EngineHandle, EPOLL_CTL_DEL, fd, &ev);
164         if (i < 0)
165         {
166                 log(DEBUG,"epoll: List deletion failure!");
167                 return false;
168         }
169 #endif
170         CurrentSetSize--;
171         ref[fd] = 0;
172         return true;
173 }
174
175 int SocketEngine::GetMaxFds()
176 {
177 #ifdef USE_SELECT
178         return FD_SETSIZE;
179 #endif
180 #ifdef USE_KQUEUE
181         return MAX_DESCRIPTORS;
182 #endif
183 #ifdef USE_EPOLL
184         return MAX_DESCRIPTORS;
185 #endif
186 }
187
188 int SocketEngine::GetRemainingFds()
189 {
190 #ifdef USE_SELECT
191         return FD_SETSIZE - CurrentSetSize;
192 #endif
193 #ifdef USE_KQUEUE
194         return MAX_DESCRIPTORS - CurrentSetSize;
195 #endif
196 #ifdef USE_EPOLL
197         return MAX_DESCRIPTORS - CurrentSetSize;
198 #endif
199 }
200
201 int SocketEngine::Wait(int* fdlist)
202 {
203         int result = 0;
204 #ifdef USE_SELECT
205         FD_ZERO(&wfdset);
206         FD_ZERO(&rfdset);
207         timeval tval;
208         int sresult;
209         for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
210         {
211                 if (ref[a->second] & X_READBIT)
212                 {
213                         FD_SET (a->second, &rfdset);
214                 }
215                 else
216                 {
217                         FD_SET (a->second, &wfdset);
218                 }
219                 
220         }
221         tval.tv_sec = 0;
222         tval.tv_usec = 50L;
223         sresult = select(FD_SETSIZE, &rfdset, &wfdset, NULL, &tval);
224         if (sresult > 0)
225         {
226                 for (std::map<int,int>::iterator a = fds.begin(); a != fds.end(); a++)
227                 {
228                         if ((FD_ISSET (a->second, &rfdset)) || (FD_ISSET (a->second, &wfdset)))
229                                 fdlist[result++] = a->second;
230                 }
231         }
232 #endif
233 #ifdef USE_KQUEUE
234         ts.tv_nsec = 5000L;
235         ts.tv_sec = 0;
236         int i = kevent(EngineHandle, NULL, 0, &ke_list[0], MAX_DESCRIPTORS, &ts);
237         for (int j = 0; j < i; j++)
238                 fdlist[result++] = ke_list[j].ident;
239 #endif
240 #ifdef USE_EPOLL
241         int i = epoll_wait(EngineHandle, events, MAX_DESCRIPTORS, 50);
242         for (int j = 0; j < i; j++)
243                 fdlist[result++] = events[j].data.fd;
244 #endif
245         return result;
246 }
247
248 std::string SocketEngine::GetName()
249 {
250 #ifdef USE_SELECT
251         return "select";
252 #endif
253 #ifdef USE_KQUEUE
254         return "kqueue";
255 #endif
256 #ifdef USE_EPOLL
257         return "epoll";
258 #endif
259         return "misconfigured";
260 }