summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorAdam <Adam@anope.org>2010-10-01 21:01:49 -0400
committerAdam <Adam@anope.org>2010-10-01 21:01:49 -0400
commitd44f7971b129aa7ba80999f16f17b8c7499686e1 (patch)
treea86d08c3e641ed6b499b53b3bbb74e2a7f5b0dfb /include
parent70056dd4689eeab4f7a9b31a921e0d7e40d5ed0d (diff)
Rewrote some of the socket code to allow m_ssl to be a service.
This allows modules (xmlrpc) to create and accept SSL connections. Also fixed unloading m_mysql at certain times and made the threading engine always work correctly on Windows.
Diffstat (limited to 'include')
-rw-r--r--include/dns.h5
-rw-r--r--include/extern.h14
-rw-r--r--include/modules.h8
-rw-r--r--include/sockets.h260
-rw-r--r--include/threadengine.h7
5 files changed, 190 insertions, 104 deletions
diff --git a/include/dns.h b/include/dns.h
index 8a7d7a4a2..7bfdac59a 100644
--- a/include/dns.h
+++ b/include/dns.h
@@ -126,13 +126,14 @@ struct DNSRecord
/** The socket used to talk to the nameserver, uses UDP
*/
-class DNSSocket : public ClientSocket
+class DNSSocket : public ConnectionSocket
{
private:
int SendTo(const unsigned char *buf, size_t len) const;
int RecvFrom(char *buf, size_t size, sockaddrs &addrs) const;
+
public:
- DNSSocket(const Anope::string &nTargetHost, int Port);
+ DNSSocket();
virtual ~DNSSocket();
bool ProcessRead();
diff --git a/include/extern.h b/include/extern.h
index 249ac6dfb..12fbcbbc5 100644
--- a/include/extern.h
+++ b/include/extern.h
@@ -183,13 +183,24 @@ E Anope::string quitmsg;
E bool save_data;
E time_t start_time;
-E Socket *UplinkSock;
+E ConnectionSocket *UplinkSock;
E void save_databases();
E void expire_all();
E void sighandler(int signum);
E void do_restart_services();
+/* The socket to our uplink */
+class UplinkSocket : public ConnectionSocket
+{
+ public:
+ UplinkSocket(bool ipv6 = false);
+
+ virtual ~UplinkSocket();
+
+ bool Read(const Anope::string &buf);
+};
+
/**** memory.c ****/
E void *scalloc(long elsize, long els);
@@ -334,6 +345,7 @@ E int exception_add(User *u, const Anope::string &mask, int limit, const Anope::
E SocketEngineBase *SocketEngine;
E int32 TotalRead;
E int32 TotalWritten;
+E SocketIO normalSocketIO;
/**** users.c ****/
diff --git a/include/modules.h b/include/modules.h
index 53cd81340..ab3472132 100644
--- a/include/modules.h
+++ b/include/modules.h
@@ -1236,11 +1236,10 @@ class Service : public virtual Base
template<typename T>
class service_reference : public dynamic_reference<T>
{
- Module *owner;
Anope::string name;
public:
- service_reference(Module *o, const Anope::string &n) : dynamic_reference<T>(static_cast<T *>(ModuleManager::GetService(this->name))), owner(o), name(n)
+ service_reference(const Anope::string &n) : dynamic_reference<T>(static_cast<T *>(ModuleManager::GetService(n))), name(n)
{
}
@@ -1263,11 +1262,6 @@ class service_reference : public dynamic_reference<T>
}
return this->ref;
}
-
- inline T *operator->()
- {
- return this->ref;
- }
};
struct Message
diff --git a/include/sockets.h b/include/sockets.h
index 38060ed78..847e91309 100644
--- a/include/sockets.h
+++ b/include/sockets.h
@@ -30,6 +30,14 @@ union CoreExport sockaddrs
sockaddr_in sa4;
sockaddr_in6 sa6;
+ /** Construct the object, sets everything to 0
+ */
+ sockaddrs();
+
+ /** Memset the object to 0
+ */
+ void clear();
+
/** Get the size of the sockaddr we represent
* @return The size
*/
@@ -45,10 +53,6 @@ union CoreExport sockaddrs
*/
Anope::string addr() const;
- /** Construct the object, sets everything to 0
- */
- sockaddrs();
-
/** Check if this sockaddr has data in it
*/
bool operator()() const;
@@ -92,6 +96,9 @@ class SocketException : public CoreException
enum SocketType
{
+ SOCKTYPE_BASE,
+ SOCKTYPE_BUFFERED,
+ SOCKTYPE_CONNECTION,
SOCKTYPE_CLIENT,
SOCKTYPE_LISTEN
};
@@ -102,34 +109,59 @@ enum SocketFlag
SF_WRITABLE
};
-class CoreExport Socket : public Flags<SocketFlag, 2>
+class Socket;
+class ClientSocket;
+class ListenSocket;
+class ConnectionSocket;
+
+class SocketIO
{
- protected:
- /** Really receive something from the buffer
+ public:
+ /** Receive something from the buffer
+ * @param s The socket
* @param buf The buf to read to
* @param sz How much to read
* @return Number of bytes received
*/
- virtual int RecvInternal(char *buf, size_t sz) const;
+ virtual int Recv(Socket *s, char *buf, size_t sz) const;
- /** Really write something to the socket
+ /** Write something to the socket
+ * @param s The socket
* @param buf What to write
* @return Number of bytes written
*/
- virtual int SendInternal(const Anope::string &buf) const;
+ virtual int Send(Socket *s, const Anope::string &buf) const;
+
+ /** Accept a connection from a socket
+ * @param s The socket
+ */
+ virtual void Accept(ListenSocket *s);
+
+ /** Connect the socket
+ * @param s THe socket
+ * @param target IP to connect to
+ * @param port to connect to
+ * @param bindip IP to bind to, if any
+ */
+ virtual void Connect(ConnectionSocket *s, const Anope::string &target, int port, const Anope::string &bindip = "");
+ /** Called when the socket is destructing
+ */
+ virtual void Destroy() { }
+};
+
+class CoreExport Socket : public Flags<SocketFlag, 2>, public virtual Base
+{
+ protected:
/* Socket FD */
int Sock;
/* Is this an IPv6 socket? */
bool IPv6;
- /* Things to be written to the socket */
- std::string WriteBuffer;
- /* Part of a message sent from the server, but not totally received */
- std::string extrabuf;
- /* How much data was received from this socket */
- size_t RecvLen;
public:
+ /* I/O functions used for this socket */
+ SocketIO *IO;
+
/* Type this socket is */
SocketType Type;
@@ -138,11 +170,11 @@ class CoreExport Socket : public Flags<SocketFlag, 2>
Socket();
/** Default constructor
- * @param nsock The socket to use, 0 if we need to create our own
- * @param nIPv6 true if using ipv6
+ * @param sock The socket to use, 0 if we need to create our own
+ * @param ipv6 true if using ipv6
* @param type The socket type, defaults to SOCK_STREAM
*/
- Socket(int nsock, bool nIPv6, int type = SOCK_STREAM);
+ Socket(int sock, bool ipv6, int type = SOCK_STREAM);
/** Default destructor
*/
@@ -151,7 +183,12 @@ class CoreExport Socket : public Flags<SocketFlag, 2>
/** Get the socket FD for this socket
* @return the fd
*/
- int GetSock() const;
+ int GetFD() const;
+
+ /** Check if this socket is IPv6
+ * @return true or false
+ */
+ bool IsIPv6() const;
/** Mark a socket as blockig
* @return true if the socket is now blocking
@@ -163,21 +200,6 @@ class CoreExport Socket : public Flags<SocketFlag, 2>
*/
bool SetNonBlocking();
- /** Check if this socket is IPv6
- * @return true or false
- */
- bool IsIPv6() const;
-
- /** Get the length of the read buffer
- * @return The length of the read buffer
- */
- size_t ReadBufferLen() const;
-
- /** Get the length of the write buffer
- * @return The length of the write buffer
- */
- size_t WriteBufferLen() const;
-
/** Called when there is something to be received for this socket
* @return true on success, false to drop this socket
*/
@@ -192,6 +214,43 @@ class CoreExport Socket : public Flags<SocketFlag, 2>
* @return true on success, false to drop this socket
*/
virtual void ProcessError();
+};
+
+class CoreExport BufferedSocket : public Socket
+{
+ protected:
+ /* Things to be written to the socket */
+ std::string WriteBuffer;
+ /* Part of a message sent from the server, but not totally received */
+ std::string extrabuf;
+ /* How much data was received from this socket */
+ size_t RecvLen;
+
+ public:
+ /** Blank constructor
+ */
+ BufferedSocket();
+
+ /** Constructor
+ * @param fd FD to use
+ * @param ipv6 true for ipv6
+ * @param type socket type, defaults to SOCK_STREAM
+ */
+ BufferedSocket(int fd, bool ipv6, int type = SOCK_STREAM);
+
+ /** Default destructor
+ */
+ virtual ~BufferedSocket();
+
+ /** Called when there is something to be received for this socket
+ * @return true on success, false to drop this socket
+ */
+ bool ProcessRead();
+
+ /** Called when the socket is ready to be written to
+ * @return true on success, false to drop this socket
+ */
+ bool ProcessWrite();
/** Called with a line received from the socket
* @param buf The line
@@ -200,106 +259,119 @@ class CoreExport Socket : public Flags<SocketFlag, 2>
virtual bool Read(const Anope::string &buf);
/** Write to the socket
- * @param message The message
- */
+ * @param message The message
+ */
void Write(const char *message, ...);
void Write(const Anope::string &message);
-};
-class CoreExport Pipe : public Socket
-{
- private:
- /** The FD of the write pipe (if this isn't evenfd)
- * this->Sock is the readfd
+ /** Get the length of the read buffer
+ * @return The length of the read buffer
*/
- int WritePipe;
+ size_t ReadBufferLen() const;
- /** Our overloaded RecvInternal call
+ /** Get the length of the write buffer
+ * @return The length of the write buffer
*/
- int RecvInternal(char *buf, size_t sz) const;
+ size_t WriteBufferLen() const;
+};
- /** Our overloaded SendInternal call
- */
- int SendInternal(const Anope::string &buf) const;
- public:
- /** Constructor
- */
- Pipe();
+class CoreExport ListenSocket : public Socket
+{
+ protected:
+ /* Sockaddrs for bindip/port */
+ sockaddrs listenaddrs;
- /** Called when data is to be read
+ public:
+ /** Constructor
+ * @param bindip The IP to bind to
+ * @param port The port to listen on
+ * @param ipv6 true for ipv6
*/
- bool ProcessRead();
+ ListenSocket(const Anope::string &bindip, int port, bool ipv6);
- /** Function that calls OnNotify
+ /** Destructor
*/
- bool Read(const Anope::string &);
+ virtual ~ListenSocket();
- /** Called when this pipe needs to be woken up
+ /** Process what has come in from the connection
+ * @return false to destory this socket
*/
- void Notify();
+ bool ProcessRead();
- /** Should be overloaded to do something useful
+ /** Called when a connection is accepted
+ * @param fd The FD for the new connection
+ * @param addr The sockaddr for where the connection came from
+ * @return The new socket
*/
- virtual void OnNotify();
+ virtual ClientSocket *OnAccept(int fd, const sockaddrs &addr);
};
-class CoreExport ClientSocket : public Socket
+class ConnectionSocket : public BufferedSocket
{
- protected:
+ public:
/* Sockaddrs for bindip (if there is one) */
- sockaddrs bindaddrs;
+ sockaddrs bindaddr;
/* Sockaddrs for connection ip/port */
- sockaddrs conaddrs;
-
- public:
+ sockaddrs conaddr;
/** Constructor
+ * @param ipv6 true to use IPv6
+ * @param type The socket type, defaults to SOCK_STREAM
+ */
+ ConnectionSocket(bool ipv6 = false, int type = SOCK_STREAM);
+
+ /** Connect the socket
* @param TargetHost The target host to connect to
* @param Port The target port to connect to
* @param BindHost The host to bind to for connecting
- * @param nIPv6 true to use IPv6
- * @param type The socket type, defaults to SOCK_STREAM
*/
- ClientSocket(const Anope::string &TargetHost, int Port, const Anope::string &BindHost = "", bool nIPv6 = false, int type = SOCK_STREAM);
+ void Connect(const Anope::string &TargetHost, int Port, const Anope::string &BindHost = "");
+};
- /** Default destructor
- */
- virtual ~ClientSocket();
+class ClientSocket : public BufferedSocket
+{
+ /* Listen socket this connection came from */
+ ListenSocket *LS;
+ /* Clients address */
+ sockaddrs clientaddr;
+ public:
- /** Called with a line received from the socket
- * @param buf The line
- * @return true to continue reading, false to drop the socket
+ /** Constructor
+ * @param ls Listen socket this connection is from
+ * @param fd New FD for this socket
+ * @param addr Address the connection came from
*/
- virtual bool Read(const Anope::string &buf);
+ ClientSocket(ListenSocket *ls, int fd, const sockaddrs &addr);
};
-class CoreExport ListenSocket : public Socket
+class CoreExport Pipe : public BufferedSocket
{
- protected:
- /* Sockaddrs for bindip/port */
- sockaddrs listenaddrs;
+ private:
+ /** The FD of the write pipe (if this isn't evenfd)
+ * this->Sock is the readfd
+ */
+ int WritePipe;
public:
- /** Constructor
- * @param bindip The IP to bind to
- * @param port The port to listen on
+ /** Constructor
*/
- ListenSocket(const Anope::string &bindip, int port);
+ Pipe();
- /** Destructor
+ /** Called when data is to be read
*/
- virtual ~ListenSocket();
+ bool ProcessRead();
- /** Process what has come in from the connection
- * @return false to destory this socket
+ /** Function that calls OnNotify
*/
- bool ProcessRead();
+ bool Read(const Anope::string &);
- /** Called when a connection is accepted
- * @param s The socket for the new connection
- * @return true if the listen socket should remain alive
+ /** Called when this pipe needs to be woken up
*/
- virtual bool OnAccept(Socket *s);
+ void Notify();
+
+ /** Should be overloaded to do something useful
+ */
+ virtual void OnNotify();
};
#endif // SOCKET_H
diff --git a/include/threadengine.h b/include/threadengine.h
index 1337f0c61..764c26242 100644
--- a/include/threadengine.h
+++ b/include/threadengine.h
@@ -20,6 +20,9 @@ extern CoreExport ThreadEngine threadEngine;
class ThreadEngine
{
public:
+ /* Vector of threads */
+ std::vector<Thread *> threads;
+
/** Threadengines constructor
*/
ThreadEngine();
@@ -32,6 +35,10 @@ class ThreadEngine
* @param thread A pointer to a newley allocated thread
*/
void Start(Thread *thread);
+
+ /** Check for finished threads
+ */
+ void Process();
};
class Thread : public Extensible