]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/socketengine.h
Document all this shit!
[user/henk/code/inspircd.git] / include / socketengine.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 #ifndef __SOCKETENGINE__
15 #define __SOCKETENGINE__
16
17 #include <vector>
18 #include <string>
19 #include <map>
20 #include "inspircd_config.h"
21 #include "base.h"
22
23 /** Types of event an EventHandler may receive.
24  * EVENT_READ is a readable file descriptor,
25  * and EVENT_WRITE is a writeable file descriptor.
26  * EVENT_ERROR can always occur, and indicates
27  * a write error or read error on the socket,
28  * e.g. EOF condition or broken pipe.
29  */
30 enum EventType
31 {
32         /** Read event */
33         EVENT_READ      =       0,
34         /** Write event */
35         EVENT_WRITE     =       1,
36         /** Error event */
37         EVENT_ERROR     =       2
38 };
39
40 class InspIRCd;
41
42 /** This class is a basic I/O handler class.
43  * Any object which wishes to receive basic I/O events
44  * from the socketengine must derive from this class and
45  * implement the HandleEvent() method. The derived class
46  * must then be added to SocketEngine using the method
47  * SocketEngine::AddFd(), after which point the derived
48  * class will receive events to its HandleEvent() method.
49  * The derived class should also implement one of Readable()
50  * and Writeable(). In the current implementation, only
51  * Readable() is used. If this returns true, the socketengine
52  * inserts a readable socket. If it is false, the socketengine
53  * inserts a writeable socket. The derived class should never
54  * change the value this function returns without first
55  * deleting the socket from the socket engine. The only
56  * requirement beyond this for an event handler is that it
57  * must have a file descriptor. What this file descriptor
58  * is actually attached to is completely up to you.
59  */
60 class CoreExport EventHandler : public Extensible
61 {
62  protected:
63         /** File descriptor.
64          * All events which can be handled
65          * must have a file descriptor.
66          * This allows you to add events for
67          * sockets, fifo's, pipes, and various
68          * other forms of IPC.
69          */
70         int fd;
71  public:
72         /** Get the current file descriptor
73          * @return The file descriptor of this handler
74          */
75         int GetFd();
76
77         /** Set a new file desciptor
78          * @param FD The new file descriptor. Do not
79          * call this method without first deleting the
80          * object from the SocketEngine if you have
81          * added it to a SocketEngine instance.
82          */
83         void SetFd(int FD);
84
85         /** Constructor
86          */
87         EventHandler() {}
88
89         /** Destructor
90          */
91         virtual ~EventHandler() {}
92
93         /** Override this function to indicate readability.
94          * @return This should return true if the function
95          * wishes to receive EVENT_READ events. Do not change
96          * what this function returns while the event handler
97          * is still added to a SocketEngine instance!
98          * If this function is unimplemented, the base class
99          * will return true.
100          *
101          * NOTE: You cannot set both Readable() and
102          * Writeable() to true. If you wish to receive
103          * a write event for your object, you must call
104          * SocketEngine::WantWrite() instead. This will
105          * trigger your objects next EVENT_WRITE type event.
106          */
107         virtual bool Readable();
108
109         /** Override this function to indicate writeability.
110          * @return This should return true if the function
111          * wishes to receive EVENT_WRITE events. Do not change
112          * what this function returns while the event handler
113          * is still added to a SocketEngine instance!
114          * If this function is unimplemented, the base class
115          * will return false.
116          *
117          * NOTE: You cannot set both Readable() and
118          * Writeable() to true. If you wish to receive
119          * a write event for your object, you must call
120          * SocketEngine::WantWrite() instead. This will
121          * trigger your objects next EVENT_WRITE type event.
122          */
123         virtual bool Writeable();
124
125         /** Process an I/O event.
126          * You MUST implement this function in your derived
127          * class, and it will be called whenever read or write
128          * events are received, depending on what your functions
129          * Readable() and Writeable() returns and wether you
130          * previously made a call to SocketEngine::WantWrite().
131          * @param et either one of EVENT_READ for read events,
132          * and EVENT_WRITE for write events.
133          */
134         virtual void HandleEvent(EventType et, int errornum = 0) = 0;
135 };
136
137 /** Provides basic file-descriptor-based I/O support.
138  * The actual socketengine class presents the
139  * same interface on all operating systems, but
140  * its private members and internal behaviour
141  * should be treated as blackboxed, and vary
142  * from system to system and upon the config
143  * settings chosen by the server admin. The current
144  * version supports select, epoll and kqueue.
145  * The configure script will enable a socket engine
146  * based upon what OS is detected, and will derive
147  * a class from SocketEngine based upon what it finds.
148  * The derived classes file will also implement a
149  * classfactory, SocketEngineFactory, which will
150  * create a derived instance of SocketEngine using
151  * polymorphism so that the core and modules do not
152  * have to be aware of which SocketEngine derived
153  * class they are using.
154  */
155 class CoreExport SocketEngine : public Extensible
156 {
157 protected:
158         /** Owner/Creator
159          */
160         InspIRCd* ServerInstance;
161         /** Handle to socket engine, where needed.
162          */
163         int EngineHandle;
164         /** Current number of descriptors in the engine
165          */
166         int CurrentSetSize;
167         /** Reference table, contains all current handlers
168          */
169         EventHandler* ref[MAX_DESCRIPTORS];
170 public:
171
172         /** Constructor.
173          * The constructor transparently initializes
174          * the socket engine which the ircd is using.
175          * Please note that if there is a catastrophic
176          * failure (for example, you try and enable
177          * epoll on a 2.4 linux kernel) then this
178          * function may bail back to the shell.
179          * @param Instance The creator/owner of this object
180          */
181         SocketEngine(InspIRCd* Instance);
182
183         /** Destructor.
184          * The destructor transparently tidies up
185          * any resources used by the socket engine.
186          */
187         virtual ~SocketEngine();
188
189         /** Add an EventHandler object to the engine.
190          * Use AddFd to add a file descriptor to the
191          * engine and have the socket engine monitor
192          * it. You must provide an object derived from
193          * EventHandler which implements HandleEvent()
194          * and optionally Readable() and Writeable().
195          * @param eh An event handling object to add
196          */
197         virtual bool AddFd(EventHandler* eh);
198
199         /** If you call this function and pass it an
200          * event handler, that event handler will
201          * receive the next available write event,
202          * even if the socket is a readable socket only.
203          * Developers should avoid constantly keeping
204          * an eventhandler in the writeable state,
205          * as this will consume large amounts of
206          * CPU time.
207          * @param eh An event handler which wants to
208          * receive the next writeability event.
209          */
210         virtual void WantWrite(EventHandler* eh);
211
212         /** Returns the maximum number of file descriptors
213          * you may store in the socket engine at any one time.
214          * @return The maximum fd value
215          */
216         virtual int GetMaxFds();
217
218         /** Returns the number of file descriptor slots
219          * which are available for storing fds.
220          * @return The number of remaining fd's
221          */
222         virtual int GetRemainingFds();
223
224         /** Delete an event handler from the engine.
225          * This function call deletes an EventHandler
226          * from the engine, returning true if it succeeded
227          * and false if it failed. This does not free the
228          * EventHandler pointer using delete, if this is
229          * required you must do this yourself.
230          * Note on forcing deletes. DO NOT DO THIS! This is
231          * extremely dangerous and will most likely render the
232          * socketengine dead. This was added only for handling
233          * very rare cases where broken 3rd party libs destroys
234          * the OS socket beyond our control. If you can't explain
235          * in minute details why forcing is absolutely necessary
236          * then you don't need it. That was a NO!
237          * @param eh The event handler object to remove
238          * @param force *DANGEROUS* See method description!
239          * @return True if the event handler was removed
240          */
241         virtual bool DelFd(EventHandler* eh, bool force = false);
242
243         /** Returns true if a file descriptor exists in
244          * the socket engine's list.
245          * @param fd The event handler to look for
246          * @return True if this fd has an event handler
247          */
248         virtual bool HasFd(int fd);
249
250         /** Returns the EventHandler attached to a specific fd.
251          * If the fd isnt in the socketengine, returns NULL.
252          * @param fd The event handler to look for
253          * @return A pointer to the event handler, or NULL
254          */
255         virtual EventHandler* GetRef(int fd);
256
257         /** Waits for events and dispatches them to handlers.
258          * Please note that this doesnt wait long, only
259          * a couple of milliseconds. It returns the number of
260          * events which occured during this call.
261          * This method will dispatch events to their handlers
262          * by calling their EventHandler::HandleEvent()
263          * methods with the neccessary EventType value.
264          * @return The number of events which have occured.
265          */
266         virtual int DispatchEvents();
267
268         /** Returns the socket engines name.
269          * This returns the name of the engine for use
270          * in /VERSION responses.
271          * @return The socket engine name
272          */
273         virtual std::string GetName();
274
275         /** Returns true if the file descriptors in the
276          * given event handler are within sensible ranges
277          * which can be handled by the socket engine.
278          */
279         virtual bool BoundsCheckFd(EventHandler* eh);
280
281         /** Abstraction for BSD sockets accept(2).
282          * This function should emulate its namesake system call exactly.
283          * @param fd This version of the call takes an EventHandler instead of a bare file descriptor.
284          * @return This method should return exactly the same values as the system call it emulates.
285          */
286         virtual int Accept(EventHandler* fd, sockaddr *addr, socklen_t *addrlen);
287
288         /** Abstraction for BSD sockets close(2).
289          * This function should emulate its namesake system call exactly.
290          * @param fd This version of the call takes an EventHandler instead of a bare file descriptor.
291          * @return This method should return exactly the same values as the system call it emulates.
292          */
293         virtual int Close(EventHandler* fd);
294
295         /** Abstraction for BSD sockets close(2).
296          * This function should emulate its namesake system call exactly.
297          * This function should emulate its namesake system call exactly.
298          * @return This method should return exactly the same values as the system call it emulates.
299          */
300         virtual int Close(int fd);
301
302         /** Abstraction for BSD sockets send(2).
303          * This function should emulate its namesake system call exactly.
304          * @param fd This version of the call takes an EventHandler instead of a bare file descriptor.
305          * @return This method should return exactly the same values as the system call it emulates.
306          */
307         virtual int Send(EventHandler* fd, const void *buf, size_t len, int flags);
308
309         /** Abstraction for BSD sockets recv(2).
310          * This function should emulate its namesake system call exactly.
311          * @param fd This version of the call takes an EventHandler instead of a bare file descriptor.
312          * @return This method should return exactly the same values as the system call it emulates.
313          */
314         virtual int Recv(EventHandler* fd, void *buf, size_t len, int flags);
315
316         /** Abstraction for BSD sockets recvfrom(2).
317          * This function should emulate its namesake system call exactly.
318          * @param fd This version of the call takes an EventHandler instead of a bare file descriptor.
319          * @return This method should return exactly the same values as the system call it emulates.
320          */
321         virtual int RecvFrom(EventHandler* fd, void *buf, size_t len, int flags, sockaddr *from, socklen_t *fromlen);
322
323         /** Abstraction for BSD sockets sendto(2).
324          * This function should emulate its namesake system call exactly.
325          * @param fd This version of the call takes an EventHandler instead of a bare file descriptor.
326          * @return This method should return exactly the same values as the system call it emulates.
327          */
328         virtual int SendTo(EventHandler* fd, const void *buf, size_t len, int flags, const sockaddr *to, socklen_t tolen);
329
330         /** Abstraction for BSD sockets connect(2).
331          * This function should emulate its namesake system call exactly.
332          * @param fd This version of the call takes an EventHandler instead of a bare file descriptor.
333          * @return This method should return exactly the same values as the system call it emulates.
334          */
335         virtual int Connect(EventHandler* fd, const sockaddr *serv_addr, socklen_t addrlen);
336
337         /** Make a file descriptor blocking.
338          * @param fd a file descriptor to set to blocking mode
339          * @return 0 on success, -1 on failure, errno is set appropriately.
340          */
341         virtual int Blocking(int fd);
342
343         /** Make a file descriptor nonblocking.
344          * @param fd A file descriptor to set to nonblocking mode
345          * @return 0 on success, -1 on failure, errno is set appropriately.
346          */
347         virtual int NonBlocking(int fd);
348
349         /** Abstraction for BSD sockets shutdown(2).
350          * This function should emulate its namesake system call exactly.
351          * @param fd This version of the call takes an EventHandler instead of a bare file descriptor.
352          * @return This method should return exactly the same values as the system call it emulates.
353          */
354         virtual int Shutdown(EventHandler* fd, int how);
355
356         /** Abstraction for BSD sockets shutdownt(2).
357          * This function should emulate its namesake system call exactly.
358          * @return This method should return exactly the same values as the system call it emulates.
359          */
360         virtual int Shutdown(int fd, int how);
361
362         /** Abstraction for BSD sockets bind(2).
363          * This function should emulate its namesake system call exactly.
364          * @return This method should return exactly the same values as the system call it emulates.
365          */
366         virtual int Bind(int fd, const sockaddr *my_addr, socklen_t addrlen);
367
368         /** Abstraction for BSD sockets listen(2).
369          * This function should emulate its namesake system call exactly.
370          * @return This method should return exactly the same values as the system call it emulates.
371          */
372         virtual int Listen(int sockfd, int backlog);
373
374         /** Abstraction for BSD sockets getsockname(2).
375          * This function should emulate its namesake system call exactly.
376          * @param fd This version of the call takes an EventHandler instead of a bare file descriptor.
377          * @return This method should return exactly the same values as the system call it emulates.
378          */
379         virtual int GetSockName(EventHandler* fd, sockaddr *name, socklen_t* namelen);
380
381         /** This function is called immediately after fork().
382          * Some socket engines (notably kqueue) cannot have their
383          * handles inherited by forked processes. This method
384          * allows for the socket engine to re-create its handle
385          * after the daemon forks as the socket engine is created
386          * long BEFORE the daemon forks.
387          * @return void, but it is acceptable for this function to bail back to
388          * the shell or operating system on fatal error.
389          */
390         virtual void RecoverFromFork();
391 };
392
393 #endif
394