]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/iohook.h
Add support for multiple IOHooks per StreamSocket
[user/henk/code/inspircd.git] / include / iohook.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013 Attila Molnar <attilamolnar@hush.com>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #pragma once
21
22 class StreamSocket;
23
24 class IOHookProvider : public ServiceProvider
25 {
26         const bool middlehook;
27
28  public:
29         enum Type
30         {
31                 IOH_UNKNOWN,
32                 IOH_SSL
33         };
34
35         const Type type;
36
37         IOHookProvider(Module* mod, const std::string& Name, Type hooktype = IOH_UNKNOWN, bool middle = false)
38                 : ServiceProvider(mod, Name, SERVICE_IOHOOK), middlehook(middle), type(hooktype) { }
39
40         /** Check if the IOHook provided can appear in the non-last position of a hook chain.
41          * That is the case if and only if the IOHook instances created are subclasses of IOHookMiddle.
42          * @return True if the IOHooks provided are subclasses of IOHookMiddle
43          */
44         bool IsMiddle() const { return middlehook; }
45
46         /** Called immediately after a connection is accepted. This is intended for raw socket
47          * processing (e.g. modules which wrap the tcp connection within another library) and provides
48          * no information relating to a user record as the connection has not been assigned yet.
49          * @param sock The socket in question
50          * @param client The client IP address and port
51          * @param server The server IP address and port
52          */
53         virtual void OnAccept(StreamSocket* sock, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server) = 0;
54
55         /** Called immediately upon connection of an outbound BufferedSocket which has been hooked
56          * by a module.
57          * @param sock The socket in question
58          */
59         virtual void OnConnect(StreamSocket* sock) = 0;
60 };
61
62 class IOHook : public classbase
63 {
64  public:
65         /** The IOHookProvider for this hook, contains information about the hook,
66          * such as the module providing it and the hook type.
67          */
68         IOHookProvider* const prov;
69
70         IOHook(IOHookProvider* provider)
71                 : prov(provider) { }
72
73         /**
74          * Called when a hooked stream has data to write, or when the socket
75          * engine returns it as writable
76          * @param sock The socket in question
77          * @param sendq Send queue to send data from
78          * @return 1 if the sendq has been completely emptied, 0 if there is
79          *  still data to send, and -1 if there was an error
80          */
81         virtual int OnStreamSocketWrite(StreamSocket* sock, StreamSocket::SendQueue& sendq) = 0;
82
83         /** Called immediately before any socket is closed. When this event is called, shutdown()
84          * has not yet been called on the socket.
85          * @param sock The socket in question
86          */
87         virtual void OnStreamSocketClose(StreamSocket* sock) = 0;
88
89         /**
90          * Called when the stream socket has data to read
91          * @param sock The socket that is ready
92          * @param recvq The receive queue that new data should be appended to
93          * @return 1 if new data has been read, 0 if no new data is ready (but the
94          *  socket is still connected), -1 if there was an error or close
95          */
96         virtual int OnStreamSocketRead(StreamSocket* sock, std::string& recvq) = 0;
97 };
98
99 class IOHookMiddle : public IOHook
100 {
101         /** Data already processed by the IOHook waiting to go down the chain
102          */
103         StreamSocket::SendQueue sendq;
104
105         /** Data waiting to go up the chain
106          */
107         std::string precvq;
108
109         /** Next IOHook in the chain
110          */
111         IOHook* nexthook;
112
113  protected:
114         /** Get all queued up data which has not yet been passed up the hook chain
115          * @return RecvQ containing the data
116          */
117         std::string& GetRecvQ() { return precvq; }
118
119         /** Get all queued up data which is ready to go down the hook chain
120          * @return SendQueue containing all data waiting to go down the hook chain
121          */
122         StreamSocket::SendQueue& GetSendQ() { return sendq; }
123
124  public:
125         /** Constructor
126          * @param provider IOHookProvider that creates this object
127          */
128         IOHookMiddle(IOHookProvider* provider)
129                 : IOHook(provider)
130                 , nexthook(NULL)
131         {
132         }
133
134         /** Get all queued up data which is ready to go down the hook chain
135          * @return SendQueue containing all data waiting to go down the hook chain
136          */
137         const StreamSocket::SendQueue& GetSendQ() const { return sendq; }
138
139         /** Get the next IOHook in the chain
140          * @return Next hook in the chain or NULL if this is the last hook
141          */
142         IOHook* GetNextHook() const { return nexthook; }
143
144         /** Set the next hook in the chain
145          * @param hook Hook to set as the next hook in the chain
146          */
147         void SetNextHook(IOHook* hook) { nexthook = hook; }
148
149         /** Check if a hook is capable of being the non-last hook in a hook chain and if so, cast it to an IOHookMiddle object.
150          * @param hook IOHook to check
151          * @return IOHookMiddle referring to the same hook or NULL
152          */
153         static IOHookMiddle* ToMiddleHook(IOHook* hook)
154         {
155                 if (hook->prov->IsMiddle())
156                         return static_cast<IOHookMiddle*>(hook);
157                 return NULL;
158         }
159
160         friend class StreamSocket;
161 };