InspIRCd  4.0
iohook.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2019 Robby <[email protected]>
5  * Copyright (C) 2013, 2016-2017 Attila Molnar <[email protected]>
6  *
7  * This file is part of InspIRCd. InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 
21 #pragma once
22 
23 class StreamSocket;
24 
26  : public std::enable_shared_from_this<IOHookProvider>
27  , public ServiceProvider
28 {
29  const bool middlehook;
30 
31  public:
32  enum Type
33  {
34  IOH_UNKNOWN,
35  IOH_SSL
36  };
37 
38  const Type type;
39 
46  IOHookProvider(Module* mod, const std::string& Name, Type hooktype = IOH_UNKNOWN, bool middle = false)
47  : ServiceProvider(mod, Name, SERVICE_IOHOOK), middlehook(middle), type(hooktype) { }
48 
53  bool IsMiddle() const { return middlehook; }
54 
61  virtual void OnAccept(StreamSocket* sock, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server) = 0;
62 
66  virtual void OnConnect(StreamSocket* sock) = 0;
67 };
68 
69 class IOHook : public Cullable
70 {
71  public:
75  std::shared_ptr<IOHookProvider> prov;
76 
80  IOHook(std::shared_ptr<IOHookProvider> provider)
81  : prov(provider) { }
82 
90  virtual ssize_t OnStreamSocketWrite(StreamSocket* sock, StreamSocket::SendQueue& sendq) = 0;
91 
96  virtual void OnStreamSocketClose(StreamSocket* sock) = 0;
97 
105  virtual int OnStreamSocketRead(StreamSocket* sock, std::string& recvq) = 0;
106 };
107 
108 class IOHookMiddle : public IOHook
109 {
113 
116  std::string precvq;
117 
120  IOHook* nexthook = nullptr;
121 
122  protected:
126  std::string& GetRecvQ() { return precvq; }
127 
131  StreamSocket::SendQueue& GetSendQ() { return sendq; }
132 
133  public:
137  IOHookMiddle(std::shared_ptr<IOHookProvider> provider)
138  : IOHook(provider)
139  {
140  }
141 
145  const StreamSocket::SendQueue& GetSendQ() const { return sendq; }
146 
150  IOHook* GetNextHook() const { return nexthook; }
151 
155  void SetNextHook(IOHook* hook) { nexthook = hook; }
156 
162  {
163  if (hook->prov->IsMiddle())
164  return static_cast<IOHookMiddle*>(hook);
165  return NULL;
166  }
167 
168  friend class StreamSocket;
169 };
Definition: cull.h:30
Definition: iohook.h:70
virtual int OnStreamSocketRead(StreamSocket *sock, std::string &recvq)=0
IOHook(std::shared_ptr< IOHookProvider > provider)
Definition: iohook.h:80
virtual ssize_t OnStreamSocketWrite(StreamSocket *sock, StreamSocket::SendQueue &sendq)=0
virtual void OnStreamSocketClose(StreamSocket *sock)=0
std::shared_ptr< IOHookProvider > prov
Definition: iohook.h:75
Definition: iohook.h:109
void SetNextHook(IOHook *hook)
Definition: iohook.h:155
StreamSocket::SendQueue & GetSendQ()
Definition: iohook.h:131
IOHookMiddle(std::shared_ptr< IOHookProvider > provider)
Definition: iohook.h:137
const StreamSocket::SendQueue & GetSendQ() const
Definition: iohook.h:145
std::string & GetRecvQ()
Definition: iohook.h:126
IOHook * GetNextHook() const
Definition: iohook.h:150
static IOHookMiddle * ToMiddleHook(IOHook *hook)
Definition: iohook.h:161
Definition: iohook.h:28
IOHookProvider(Module *mod, const std::string &Name, Type hooktype=IOH_UNKNOWN, bool middle=false)
Definition: iohook.h:46
bool IsMiddle() const
Definition: iohook.h:53
virtual void OnAccept(StreamSocket *sock, irc::sockets::sockaddrs *client, irc::sockets::sockaddrs *server)=0
virtual void OnConnect(StreamSocket *sock)=0
Definition: modules.h:286
Definition: base.h:204
Definition: inspsocket.h:120
Definition: inspsocket.h:115
Definition: socket.h:63