2013-05-24 19:34:25 +02:00
|
|
|
/*
|
|
|
|
* InspIRCd -- Internet Relay Chat Daemon
|
|
|
|
*
|
2024-06-07 10:37:56 +01:00
|
|
|
* Copyright (C) 2021 Dominic Hamon
|
|
|
|
* Copyright (C) 2020-2023 Sadie Powell <sadie@witchery.services>
|
2020-01-11 22:02:47 +00:00
|
|
|
* Copyright (C) 2019 Robby <robby@chatbelgie.be>
|
2024-06-07 10:37:56 +01:00
|
|
|
* Copyright (C) 2013, 2016 Attila Molnar <attilamolnar@hush.com>
|
2013-05-24 19:34:25 +02:00
|
|
|
*
|
|
|
|
* This file is part of InspIRCd. InspIRCd is free software: you can
|
|
|
|
* redistribute it and/or modify it under the terms of the GNU General Public
|
|
|
|
* License as published by the Free Software Foundation, version 2.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
class StreamSocket;
|
|
|
|
|
2021-04-09 01:03:30 +01:00
|
|
|
class IOHookProvider
|
|
|
|
: public std::enable_shared_from_this<IOHookProvider>
|
|
|
|
, public ServiceProvider
|
2013-05-24 19:34:25 +02:00
|
|
|
{
|
2021-04-27 16:36:40 +01:00
|
|
|
const bool middlehook;
|
2016-08-08 15:02:28 +02:00
|
|
|
|
2022-01-25 13:59:42 +00:00
|
|
|
public:
|
2013-05-24 19:34:25 +02:00
|
|
|
enum Type
|
|
|
|
{
|
|
|
|
IOH_UNKNOWN,
|
|
|
|
IOH_SSL
|
|
|
|
};
|
|
|
|
|
|
|
|
const Type type;
|
|
|
|
|
2016-08-08 16:30:04 +02:00
|
|
|
/** Constructor
|
|
|
|
* @param mod Module that owns the IOHookProvider
|
|
|
|
* @param Name Name of the provider
|
|
|
|
* @param hooktype One of IOHookProvider::Type
|
|
|
|
* @param middle True if the IOHook instances created by this hook are subclasses of IOHookMiddle, false otherwise
|
|
|
|
*/
|
2016-08-08 15:02:28 +02:00
|
|
|
IOHookProvider(Module* mod, const std::string& Name, Type hooktype = IOH_UNKNOWN, bool middle = false)
|
2022-12-01 05:14:58 +00:00
|
|
|
: ServiceProvider(mod, Name, SERVICE_IOHOOK)
|
|
|
|
, middlehook(middle)
|
|
|
|
, type(hooktype)
|
|
|
|
{
|
|
|
|
}
|
2016-08-08 15:02:28 +02:00
|
|
|
|
|
|
|
/** Check if the IOHook provided can appear in the non-last position of a hook chain.
|
|
|
|
* That is the case if and only if the IOHook instances created are subclasses of IOHookMiddle.
|
|
|
|
* @return True if the IOHooks provided are subclasses of IOHookMiddle
|
|
|
|
*/
|
|
|
|
bool IsMiddle() const { return middlehook; }
|
2013-05-24 19:34:25 +02:00
|
|
|
|
2019-02-17 15:58:31 +01:00
|
|
|
/** Called when the provider should hook an incoming connection and act as being on the server-side of the connection.
|
2016-08-08 16:30:04 +02:00
|
|
|
* This occurs when a bind block has a hook configured and the listener accepts a connection.
|
|
|
|
* @param sock Socket to hook
|
|
|
|
* @param client Client IP address and port
|
|
|
|
* @param server Server IP address and port
|
2013-05-24 19:34:25 +02:00
|
|
|
*/
|
2022-12-25 13:06:50 +00:00
|
|
|
virtual void OnAccept(StreamSocket* sock, const irc::sockets::sockaddrs& client, const irc::sockets::sockaddrs& server) = 0;
|
2013-09-24 20:40:20 +02:00
|
|
|
|
2016-08-08 16:30:04 +02:00
|
|
|
/** Called when the provider should hook an outgoing connection and act as being on the client side of the connection.
|
|
|
|
* @param sock Socket to hook
|
2013-09-24 20:40:20 +02:00
|
|
|
*/
|
|
|
|
virtual void OnConnect(StreamSocket* sock) = 0;
|
|
|
|
};
|
|
|
|
|
2021-12-20 20:00:03 +00:00
|
|
|
class IOHook
|
|
|
|
: public Cullable
|
2013-09-24 20:40:20 +02:00
|
|
|
{
|
2022-01-25 13:59:42 +00:00
|
|
|
public:
|
2013-09-24 20:40:20 +02:00
|
|
|
/** The IOHookProvider for this hook, contains information about the hook,
|
|
|
|
* such as the module providing it and the hook type.
|
|
|
|
*/
|
2021-04-09 01:03:30 +01:00
|
|
|
std::shared_ptr<IOHookProvider> prov;
|
2013-09-24 20:40:20 +02:00
|
|
|
|
2016-08-08 16:30:04 +02:00
|
|
|
/** Constructor
|
|
|
|
* @param provider IOHookProvider that creates this object
|
|
|
|
*/
|
2023-01-10 21:25:28 +00:00
|
|
|
IOHook(const std::shared_ptr<IOHookProvider>& provider)
|
|
|
|
: prov(provider)
|
|
|
|
{
|
|
|
|
}
|
2013-05-24 19:34:25 +02:00
|
|
|
|
2021-08-17 12:06:22 +01:00
|
|
|
/** Determines whether this I/O hook is ready to send and receive data. */
|
|
|
|
virtual bool IsHookReady() const { return true; }
|
|
|
|
|
2013-05-24 19:34:25 +02:00
|
|
|
/**
|
2016-08-08 16:30:04 +02:00
|
|
|
* Called when the hooked socket has data to write, or when the socket engine returns it as writable
|
|
|
|
* @param sock Hooked socket
|
2016-08-08 14:28:02 +02:00
|
|
|
* @param sendq Send queue to send data from
|
2013-05-24 19:34:25 +02:00
|
|
|
* @return 1 if the sendq has been completely emptied, 0 if there is
|
|
|
|
* still data to send, and -1 if there was an error
|
|
|
|
*/
|
2021-05-30 20:37:54 +01:00
|
|
|
virtual ssize_t OnStreamSocketWrite(StreamSocket* sock, StreamSocket::SendQueue& sendq) = 0;
|
2013-05-24 19:34:25 +02:00
|
|
|
|
2016-08-08 16:30:04 +02:00
|
|
|
/** Called immediately before the hooked socket is closed. When this event is called, shutdown()
|
2013-05-24 19:34:25 +02:00
|
|
|
* has not yet been called on the socket.
|
2016-08-08 16:30:04 +02:00
|
|
|
* @param sock Hooked socket
|
2013-05-24 19:34:25 +02:00
|
|
|
*/
|
|
|
|
virtual void OnStreamSocketClose(StreamSocket* sock) = 0;
|
|
|
|
|
|
|
|
/**
|
2016-08-08 16:30:04 +02:00
|
|
|
* Called when the hooked socket has data to read
|
|
|
|
* @param sock Hooked socket
|
2013-05-24 19:34:25 +02:00
|
|
|
* @param recvq The receive queue that new data should be appended to
|
|
|
|
* @return 1 if new data has been read, 0 if no new data is ready (but the
|
|
|
|
* socket is still connected), -1 if there was an error or close
|
|
|
|
*/
|
2023-01-24 23:02:35 +00:00
|
|
|
virtual ssize_t OnStreamSocketRead(StreamSocket* sock, std::string& recvq) = 0;
|
2023-01-21 12:54:07 +00:00
|
|
|
|
|
|
|
/** Sends a ping to the remote client to check whether it is still connected.
|
|
|
|
* @return True if the client was pinged; otherwise, false.
|
|
|
|
*/
|
|
|
|
virtual bool Ping() { return false; }
|
2013-05-24 19:34:25 +02:00
|
|
|
};
|
2016-08-08 15:02:28 +02:00
|
|
|
|
2021-12-20 20:00:03 +00:00
|
|
|
class IOHookMiddle
|
|
|
|
: public IOHook
|
2016-08-08 15:02:28 +02:00
|
|
|
{
|
|
|
|
/** Data already processed by the IOHook waiting to go down the chain
|
|
|
|
*/
|
|
|
|
StreamSocket::SendQueue sendq;
|
|
|
|
|
|
|
|
/** Data waiting to go up the chain
|
|
|
|
*/
|
|
|
|
std::string precvq;
|
|
|
|
|
|
|
|
/** Next IOHook in the chain
|
|
|
|
*/
|
2020-02-06 11:25:42 +00:00
|
|
|
IOHook* nexthook = nullptr;
|
2016-08-08 15:02:28 +02:00
|
|
|
|
2022-01-25 13:59:42 +00:00
|
|
|
protected:
|
2016-08-08 15:02:28 +02:00
|
|
|
/** Get all queued up data which has not yet been passed up the hook chain
|
|
|
|
* @return RecvQ containing the data
|
|
|
|
*/
|
|
|
|
std::string& GetRecvQ() { return precvq; }
|
|
|
|
|
|
|
|
/** Get all queued up data which is ready to go down the hook chain
|
|
|
|
* @return SendQueue containing all data waiting to go down the hook chain
|
|
|
|
*/
|
|
|
|
StreamSocket::SendQueue& GetSendQ() { return sendq; }
|
|
|
|
|
2022-01-25 13:59:42 +00:00
|
|
|
public:
|
2016-08-08 15:02:28 +02:00
|
|
|
/** Constructor
|
|
|
|
* @param provider IOHookProvider that creates this object
|
|
|
|
*/
|
2023-01-10 21:25:28 +00:00
|
|
|
IOHookMiddle(const std::shared_ptr<IOHookProvider>& provider)
|
2016-08-08 15:02:28 +02:00
|
|
|
: IOHook(provider)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-21 13:22:33 +00:00
|
|
|
/** @copydoc IOHook::OnStreamSocketClose */
|
|
|
|
void OnStreamSocketClose(StreamSocket* sock) override { }
|
|
|
|
|
2016-08-08 15:02:28 +02:00
|
|
|
/** Get all queued up data which is ready to go down the hook chain
|
|
|
|
* @return SendQueue containing all data waiting to go down the hook chain
|
|
|
|
*/
|
|
|
|
const StreamSocket::SendQueue& GetSendQ() const { return sendq; }
|
|
|
|
|
|
|
|
/** Get the next IOHook in the chain
|
|
|
|
* @return Next hook in the chain or NULL if this is the last hook
|
|
|
|
*/
|
|
|
|
IOHook* GetNextHook() const { return nexthook; }
|
|
|
|
|
|
|
|
/** Set the next hook in the chain
|
|
|
|
* @param hook Hook to set as the next hook in the chain
|
|
|
|
*/
|
|
|
|
void SetNextHook(IOHook* hook) { nexthook = hook; }
|
|
|
|
|
|
|
|
/** 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.
|
|
|
|
* @param hook IOHook to check
|
|
|
|
* @return IOHookMiddle referring to the same hook or NULL
|
|
|
|
*/
|
|
|
|
static IOHookMiddle* ToMiddleHook(IOHook* hook)
|
|
|
|
{
|
|
|
|
if (hook->prov->IsMiddle())
|
|
|
|
return static_cast<IOHookMiddle*>(hook);
|
2022-07-22 18:33:38 +01:00
|
|
|
return nullptr;
|
2016-08-08 15:02:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
friend class StreamSocket;
|
|
|
|
};
|