X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=include%2Fsocketengine.h;h=26d31c168d1c9ff77d9847ddc8f20d7644240e79;hb=635cb9d65f6d7f6758ae8ed874da00c8d94b6e39;hp=895457b89191034666b11ba9bcc89dce367c4882;hpb=3a3ff949670c61a4a8856e1391222e156eb1cd17;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/include/socketengine.h b/include/socketengine.h index 895457b89..26d31c168 100644 --- a/include/socketengine.h +++ b/include/socketengine.h @@ -1,10 +1,15 @@ /* * InspIRCd -- Internet Relay Chat Daemon * - * Copyright (C) 2009 Daniel De Graaf - * Copyright (C) 2007-2008 Robin Burchell - * Copyright (C) 2005-2007 Craig Edwards + * Copyright (C) 2013-2016 Attila Molnar + * Copyright (C) 2013-2014 Adam + * Copyright (C) 2012-2013, 2017-2020 Sadie Powell + * Copyright (C) 2012 Robby + * Copyright (C) 2009-2010 Daniel De Graaf + * Copyright (C) 2009 Uli Schlachter + * Copyright (C) 2007-2008, 2017 Robin Burchell * Copyright (C) 2007 Dennis Friis + * Copyright (C) 2005-2008 Craig Edwards * * 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 @@ -22,29 +27,17 @@ #pragma once -#include #include -#include -#include "config.h" #include "socket.h" #include "base.h" -/** Types of event an EventHandler may receive. - * EVENT_READ is a readable file descriptor, - * and EVENT_WRITE is a writeable file descriptor. - * EVENT_ERROR can always occur, and indicates - * a write error or read error on the socket, - * e.g. EOF condition or broken pipe. - */ -enum EventType -{ - /** Read event */ - EVENT_READ = 0, - /** Write event */ - EVENT_WRITE = 1, - /** Error event */ - EVENT_ERROR = 2 -}; +#ifndef _WIN32 +#include +#endif + +#ifndef IOV_MAX +#define IOV_MAX 1024 +#endif /** * Event mask for SocketEngine events @@ -93,7 +86,7 @@ enum EventMask * EINPROGRESS. An event MAY be sent at any time that writes will not * block. * - * Before calling HandleEvent, a socket engine MAY change the state of + * Before calling OnEventHandler*(), a socket engine MAY change the state of * the FD back to FD_WANT_EDGE_WRITE if it is simpler (for example, if a * one-shot notification was registered). If further writes are needed, * it is the responsibility of the event handler to change the state to @@ -107,7 +100,7 @@ enum EventMask */ FD_WANT_EDGE_WRITE = 0x80, /** Request a one-shot poll-style write notification. The socket will - * return to the FD_WANT_NO_WRITE state before HandleEvent is called. + * return to the FD_WANT_NO_WRITE state before OnEventHandler*() is called. */ FD_WANT_SINGLE_WRITE = 0x100, @@ -115,17 +108,17 @@ enum EventMask FD_WANT_WRITE_MASK = 0x1F0, /** Add a trial read. During the next DispatchEvents invocation, this - * will call HandleEvent with EVENT_READ unless reads are known to be + * will call OnEventHandlerRead() unless reads are known to be * blocking. */ FD_ADD_TRIAL_READ = 0x1000, /** Assert that reads are known to block. This cancels FD_ADD_TRIAL_READ. - * Reset by SE before running EVENT_READ + * Reset by SE before running OnEventHandlerRead(). */ FD_READ_WILL_BLOCK = 0x2000, /** Add a trial write. During the next DispatchEvents invocation, this - * will call HandleEvent with EVENT_WRITE unless writes are known to be + * will call OnEventHandlerWrite() unless writes are known to be * blocking. * * This could be used to group several writes together into a single @@ -134,7 +127,7 @@ enum EventMask */ FD_ADD_TRIAL_WRITE = 0x4000, /** Assert that writes are known to block. This cancels FD_ADD_TRIAL_WRITE. - * Reset by SE before running EVENT_WRITE + * Reset by SE before running OnEventHandlerWrite(). */ FD_WRITE_WILL_BLOCK = 0x8000, @@ -145,17 +138,14 @@ enum EventMask /** This class is a basic I/O handler class. * Any object which wishes to receive basic I/O events * from the socketengine must derive from this class and - * implement the HandleEvent() method. The derived class + * implement the OnEventHandler*() methods. The derived class * must then be added to SocketEngine using the method * SocketEngine::AddFd(), after which point the derived - * class will receive events to its HandleEvent() method. - * The derived class should also implement one of Readable() - * and Writeable(). In the current implementation, only - * Readable() is used. If this returns true, the socketengine - * inserts a readable socket. If it is false, the socketengine - * inserts a writeable socket. The derived class should never - * change the value this function returns without first - * deleting the socket from the socket engine. The only + * class will receive events to its OnEventHandler*() methods. + * The event mask passed to SocketEngine::AddFd() determines + * what events the EventHandler gets notified about and with + * what semantics. SocketEngine::ChangeEventMask() can be + * called to update the event mask later. The only * requirement beyond this for an event handler is that it * must have a file descriptor. What this file descriptor * is actually attached to is completely up to you. @@ -176,15 +166,24 @@ class CoreExport EventHandler : public classbase * registered with the SocketEngine */ int fd; + + /** Swaps the internals of this EventHandler with another one. + * @param other A EventHandler to swap internals with. + */ + void SwapInternals(EventHandler& other); + public: /** Get the current file descriptor * @return The file descriptor of this handler */ inline int GetFd() const { return fd; } + /** Checks if this event handler has a fd associated with it. */ + inline bool HasFd() const { return fd >= 0; } + inline int GetEventMask() const { return event_mask; } - /** Set a new file desciptor + /** Set a new file descriptor * @param FD The new file descriptor. Do not call this method without * first deleting the object from the SocketEngine if you have * added it to a SocketEngine instance. @@ -199,16 +198,20 @@ class CoreExport EventHandler : public classbase */ virtual ~EventHandler() {} - /** Process an I/O event. - * You MUST implement this function in your derived - * class, and it will be called whenever read or write - * events are received. - * @param et either one of EVENT_READ for read events, - * EVENT_WRITE for write events and EVENT_ERROR for - * error events. - * @param errornum The error code which goes with an EVENT_ERROR. + /** Called by the socket engine in case of a read event + */ + virtual void OnEventHandlerRead() = 0; + + /** Called by the socket engine in case of a write event. + * The default implementation does nothing. + */ + virtual void OnEventHandlerWrite(); + + /** Called by the socket engine in case of an error event. + * The default implementation does nothing. + * @param errornum Error code */ - virtual void HandleEvent(EventType et, int errornum = 0) = 0; + virtual void OnEventHandlerError(int errornum); friend class SocketEngine; }; @@ -219,17 +222,7 @@ class CoreExport EventHandler : public classbase * its private members and internal behaviour * should be treated as blackboxed, and vary * from system to system and upon the config - * settings chosen by the server admin. The current - * version supports select, epoll and kqueue. - * The configure script will enable a socket engine - * based upon what OS is detected, and will derive - * a class from SocketEngine based upon what it finds. - * The derived classes file will also implement a - * classfactory, SocketEngineFactory, which will - * create a derived instance of SocketEngine using - * polymorphism so that the core and modules do not - * have to be aware of which SocketEngine derived - * class they are using. + * settings chosen by the server admin. */ class CoreExport SocketEngine { @@ -252,16 +245,24 @@ class CoreExport SocketEngine */ Statistics() : lastempty(0), TotalEvents(0), ReadEvents(0), WriteEvents(0), ErrorEvents(0) { } - /** Increase the counters for bytes sent/received in this second. - * @param len_in Bytes received, 0 if updating number of bytes written. - * @param len_out Bytes sent, 0 if updating number of bytes read. + /** Update counters for network data received. + * This should be called after every read-type syscall. + * @param len_in Number of bytes received, or -1 for error, as typically + * returned by a read-style syscall. */ - void Update(size_t len_in, size_t len_out); + void UpdateReadCounters(int len_in); + + /** Update counters for network data sent. + * This should be called after every write-type syscall. + * @param len_out Number of bytes sent, or -1 for error, as typically + * returned by a read-style syscall. + */ + void UpdateWriteCounters(int len_out); /** Get data transfer statistics. - * @param kbitspersec_in Filled with incoming traffic in this second in kbit/s. - * @param kbitspersec_out Filled with outgoing traffic in this second in kbit/s. - * @param kbitspersec_total Filled with total traffic in this second in kbit/s. + * @param kbitpersec_in Filled with incoming traffic in this second in kbit/s. + * @param kbitpersec_out Filled with outgoing traffic in this second in kbit/s. + * @param kbitpersec_total Filled with total traffic in this second in kbit/s. */ void CoreExport GetBandwidth(float& kbitpersec_in, float& kbitpersec_out, float& kbitpersec_total) const; @@ -276,20 +277,26 @@ class CoreExport SocketEngine **/ static std::vector ref; - protected: - /** Current number of descriptors in the engine - */ + /** Current number of descriptors in the engine. */ static size_t CurrentSetSize; + + /** The maximum number of descriptors in the engine. */ + static size_t MaxSetSize; + /** List of handlers that want a trial read/write */ static std::set trials; - static int MAX_DESCRIPTORS; - /** Socket engine statistics: count of various events, bandwidth usage */ static Statistics stats; + /** Look up the fd limit using rlimit. */ + static void LookupMaxFds(); + + /** Terminates the program when the socket engine fails to initialize. */ + static void InitError(); + static void OnSetEvent(EventHandler* eh, int old_mask, int new_mask); /** Add an event handler to the base socket engine. AddFd(EventHandler*, int) should call this. @@ -302,10 +309,16 @@ class CoreExport SocketEngine static void ResizeDouble(std::vector& vect) { if (SocketEngine::CurrentSetSize > vect.size()) - vect.resize(vect.size() * 2); + vect.resize(SocketEngine::CurrentSetSize * 2); } public: +#ifndef _WIN32 + typedef iovec IOVector; +#else + typedef WindowsIOVec IOVector; +#endif + /** Constructor. * The constructor transparently initializes * the socket engine which the ircd is using. @@ -327,7 +340,7 @@ public: /** Add an EventHandler object to the engine. Use AddFd to add a file * descriptor to the engine and have the socket engine monitor it. You * must provide an object derived from EventHandler which implements - * HandleEvent(). + * the required OnEventHandler*() methods. * @param eh An event handling object to add * @param event_mask The initial event mask for the object */ @@ -348,10 +361,10 @@ public: /** Returns the number of file descriptors reported by the system this program may use * when it was started. - * @return If positive, the number of file descriptors that the system reported that we - * may use. Otherwise (<= 0) this number could not be determined. + * @return If non-zero the number of file descriptors that the system reported that we + * may use. */ - static int GetMaxFds() { return MAX_DESCRIPTORS; } + static size_t GetMaxFds() { return MaxSetSize; } /** Returns the number of file descriptors being queried * @return The set size @@ -376,7 +389,7 @@ public: static bool HasFd(int fd); /** Returns the EventHandler attached to a specific fd. - * If the fd isnt in the socketengine, returns NULL. + * If the fd isn't in the socketengine, returns NULL. * @param fd The event handler to look for * @return A pointer to the event handler, or NULL */ @@ -386,9 +399,8 @@ public: * this doesn't wait long, only a couple of milliseconds. It returns the * number of events which occurred during this call. This method will * dispatch events to their handlers by calling their - * EventHandler::HandleEvent() methods with the necessary EventType - * value. - * @return The number of events which have occured. + * EventHandler::OnEventHandler*() methods. + * @return The number of events which have occurred. */ static int DispatchEvents(); @@ -434,6 +446,27 @@ public: */ static int Send(EventHandler* fd, const void *buf, size_t len, int flags); + /** Abstraction for vector write function writev(). + * This function should emulate its namesake system call exactly. + * @param fd EventHandler to send data with + * @param iov Array of IOVectors containing the buffers to send and their lengths in the platform's + * native format. + * @param count Number of elements in iov. + * @return This method should return exactly the same values as the system call it emulates. + */ + static int WriteV(EventHandler* fd, const IOVector* iov, int count); + +#ifdef _WIN32 + /** Abstraction for vector write function writev() that accepts a POSIX format iovec. + * This function should emulate its namesake system call exactly. + * @param fd EventHandler to send data with + * @param iov Array of iovecs containing the buffers to send and their lengths in POSIX format. + * @param count Number of elements in iov. + * @return This method should return exactly the same values as the system call it emulates. + */ + static int WriteV(EventHandler* fd, const iovec* iov, int count); +#endif + /** Abstraction for BSD sockets recv(2). * This function should emulate its namesake system call exactly. * @param fd This version of the call takes an EventHandler instead of a bare file descriptor. @@ -462,20 +495,18 @@ public: * @param buf The buffer in which the data that is sent is stored. * @param len The size of the buffer. * @param flags A flag value that controls the sending of the data. - * @param to The remote IP address and port. - * @param tolen The size of the to parameter. + * @param address The remote IP address and port. * @return This method should return exactly the same values as the system call it emulates. */ - static int SendTo(EventHandler* fd, const void *buf, size_t len, int flags, const sockaddr *to, socklen_t tolen); + static int SendTo(EventHandler* fd, const void* buf, size_t len, int flags, const irc::sockets::sockaddrs& address); /** Abstraction for BSD sockets connect(2). * This function should emulate its namesake system call exactly. * @param fd This version of the call takes an EventHandler instead of a bare file descriptor. - * @param serv_addr The server IP address and port. - * @param addrlen The size of the sockaddr parameter. + * @param address The server IP address and port. * @return This method should return exactly the same values as the system call it emulates. */ - static int Connect(EventHandler* fd, const sockaddr *serv_addr, socklen_t addrlen); + static int Connect(EventHandler* fd, const irc::sockets::sockaddrs& address); /** Make a file descriptor blocking. * @param fd a file descriptor to set to blocking mode