diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/compat.h | 2 | ||||
-rw-r--r-- | include/inspsocket.h | 38 | ||||
-rw-r--r-- | include/iohook.h | 117 | ||||
-rw-r--r-- | include/socket.h | 21 | ||||
-rw-r--r-- | include/typedefs.h | 1 |
5 files changed, 152 insertions, 27 deletions
diff --git a/include/compat.h b/include/compat.h index e7719bcd7..1e6fc3d45 100644 --- a/include/compat.h +++ b/include/compat.h @@ -27,10 +27,12 @@ */ #if defined _LIBCPP_VERSION || defined _WIN32 # define TR1NS std +# include <array> # include <unordered_map> # include <type_traits> #else # define TR1NS std::tr1 +# include <tr1/array> # include <tr1/unordered_map> # include <tr1/type_traits> #endif diff --git a/include/inspsocket.h b/include/inspsocket.h index 53eca2e91..751374fdf 100644 --- a/include/inspsocket.h +++ b/include/inspsocket.h @@ -198,6 +198,13 @@ class CoreExport StreamSocket : public EventHandler nbytes = 0; } + void moveall(SendQueue& other) + { + nbytes += other.bytes(); + data.insert(data.end(), other.data.begin(), other.data.end()); + other.clear(); + } + private: /** Private send queue. Note that individual strings may be shared. */ @@ -228,6 +235,28 @@ class CoreExport StreamSocket : public EventHandler */ void DoRead(); + /** Send as much data contained in a SendQueue object as possible. + * All data which successfully sent will be removed from the SendQueue. + * @param sq SendQueue to flush + */ + void FlushSendQ(SendQueue& sq); + + /** Read incoming data into a receive queue. + * @param rq Receive queue to put incoming data into + * @return < 0 on error or close, 0 if no new data is ready (but the socket is still connected), > 0 if data was read from the socket and put into the recvq + */ + int ReadToRecvQ(std::string& rq); + + /** Read data from a hook chain recursively, starting at 'hook'. + * If 'hook' is NULL, the recvq is filled with data from SocketEngine::Recv(), otherwise it is filled with data from the + * next hook in the chain. + * @param hook Next IOHook in the chain, can be NULL + * @param rq Receive queue to put incoming data into + * @return < 0 on error or close, 0 if no new data is ready (but the socket is still connected), > 0 if data was read from + the socket and put into the recvq + */ + int HookChainRead(IOHook* hook, std::string& rq); + protected: std::string recvq; public: @@ -274,7 +303,7 @@ class CoreExport StreamSocket : public EventHandler */ bool GetNextLine(std::string& line, char delim = '\n'); /** Useful for implementing sendq exceeded */ - size_t getSendQSize() const { return sendq.size(); } + size_t getSendQSize() const; SendQueue& GetSendQ() { return sendq; } @@ -284,6 +313,12 @@ class CoreExport StreamSocket : public EventHandler virtual void Close(); /** This ensures that close is called prior to destructor */ virtual CullResult cull(); + + /** Get the IOHook of a module attached to this socket + * @param mod Module whose IOHook to return + * @return IOHook belonging to the module or NULL if the module haven't attached an IOHook to this socket + */ + IOHook* GetModHook(Module* mod) const; }; /** * BufferedSocket is an extendable socket class which modules @@ -358,5 +393,4 @@ class CoreExport BufferedSocket : public StreamSocket }; inline IOHook* StreamSocket::GetIOHook() const { return iohook; } -inline void StreamSocket::AddIOHook(IOHook* hook) { iohook = hook; } inline void StreamSocket::DelIOHook() { iohook = NULL; } diff --git a/include/iohook.h b/include/iohook.h index cf27fcb0c..e99316b99 100644 --- a/include/iohook.h +++ b/include/iohook.h @@ -23,6 +23,8 @@ class StreamSocket; class IOHookProvider : public ServiceProvider { + const bool middlehook; + public: enum Type { @@ -32,21 +34,31 @@ class IOHookProvider : public ServiceProvider const Type type; - IOHookProvider(Module* mod, const std::string& Name, Type hooktype = IOH_UNKNOWN) - : ServiceProvider(mod, Name, SERVICE_IOHOOK), type(hooktype) { } + /** 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 + */ + IOHookProvider(Module* mod, const std::string& Name, Type hooktype = IOH_UNKNOWN, bool middle = false) + : ServiceProvider(mod, Name, SERVICE_IOHOOK), middlehook(middle), type(hooktype) { } + + /** 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; } - /** Called immediately after a connection is accepted. This is intended for raw socket - * processing (e.g. modules which wrap the tcp connection within another library) and provides - * no information relating to a user record as the connection has not been assigned yet. - * @param sock The socket in question - * @param client The client IP address and port - * @param server The server IP address and port + /** Called when the provider should hook an incoming connection and act as being on the server side of the connection. + * 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 */ virtual void OnAccept(StreamSocket* sock, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server) = 0; - /** Called immediately upon connection of an outbound BufferedSocket which has been hooked - * by a module. - * @param sock The socket in question + /** 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 */ virtual void OnConnect(StreamSocket* sock) = 0; }; @@ -59,30 +71,97 @@ class IOHook : public classbase */ IOHookProvider* const prov; + /** Constructor + * @param provider IOHookProvider that creates this object + */ IOHook(IOHookProvider* provider) : prov(provider) { } /** - * Called when a hooked stream has data to write, or when the socket - * engine returns it as writable - * @param sock The socket in question + * Called when the hooked socket has data to write, or when the socket engine returns it as writable + * @param sock Hooked socket + * @param sendq Send queue to send data from * @return 1 if the sendq has been completely emptied, 0 if there is * still data to send, and -1 if there was an error */ - virtual int OnStreamSocketWrite(StreamSocket* sock) = 0; + virtual int OnStreamSocketWrite(StreamSocket* sock, StreamSocket::SendQueue& sendq) = 0; - /** Called immediately before any socket is closed. When this event is called, shutdown() + /** Called immediately before the hooked socket is closed. When this event is called, shutdown() * has not yet been called on the socket. - * @param sock The socket in question + * @param sock Hooked socket */ virtual void OnStreamSocketClose(StreamSocket* sock) = 0; /** - * Called when the stream socket has data to read - * @param sock The socket that is ready + * Called when the hooked socket has data to read + * @param sock Hooked socket * @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 */ virtual int OnStreamSocketRead(StreamSocket* sock, std::string& recvq) = 0; }; + +class IOHookMiddle : public IOHook +{ + /** 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 + */ + IOHook* nexthook; + + protected: + /** 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; } + + public: + /** Constructor + * @param provider IOHookProvider that creates this object + */ + IOHookMiddle(IOHookProvider* provider) + : IOHook(provider) + , nexthook(NULL) + { + } + + /** 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); + return NULL; + } + + friend class StreamSocket; +}; diff --git a/include/socket.h b/include/socket.h index 9d69b5d22..427ee9fe7 100644 --- a/include/socket.h +++ b/include/socket.h @@ -127,7 +127,6 @@ namespace irc } } -#include "iohook.h" #include "socketengine.h" /** This class handles incoming connections on client ports. * It will create a new User for every valid connection @@ -142,10 +141,21 @@ class CoreExport ListenSocket : public EventHandler /** Human-readable bind description */ std::string bind_desc; - /** The IOHook provider which handles connections on this socket, - * NULL if there is none. + class IOHookProvRef : public dynamic_reference_nocheck<IOHookProvider> + { + public: + IOHookProvRef() + : dynamic_reference_nocheck<IOHookProvider>(NULL, std::string()) + { + } + }; + + typedef TR1NS::array<IOHookProvRef, 2> IOHookProvList; + + /** IOHook providers for handling connections on this socket, + * may be empty. */ - dynamic_reference_nocheck<IOHookProvider> iohookprov; + IOHookProvList iohookprovs; /** Create a new listening socket */ @@ -160,7 +170,6 @@ class CoreExport ListenSocket : public EventHandler /** Inspects the bind block belonging to this socket to set the name of the IO hook * provider which this socket will use for incoming connections. - * @return True if the IO hook provider was found or none was given, false otherwise. */ - bool ResetIOHookProvider(); + void ResetIOHookProvider(); }; diff --git a/include/typedefs.h b/include/typedefs.h index 17c05d704..48842ccf0 100644 --- a/include/typedefs.h +++ b/include/typedefs.h @@ -31,6 +31,7 @@ class Extensible; class FakeUser; class InspIRCd; class Invitation; +class IOHookProvider; class LocalUser; class Membership; class Module; |