diff options
Diffstat (limited to 'dep/include/sockets')
26 files changed, 573 insertions, 0 deletions
diff --git a/dep/include/sockets/Base64.h b/dep/include/sockets/Base64.h index 7db41c27d36..d4323aaa019 100644 --- a/dep/include/sockets/Base64.h +++ b/dep/include/sockets/Base64.h @@ -4,49 +4,63 @@ **/ /* Copyright (C) 2004-2007 Anders Hedstrom + This library is made available under the terms of the GNU GPL. + If you would like to use this library in a closed-source application, a separate license agreement is available. For information about the closed-source license agreement for the C++ sockets library, please visit http://www.alhem.net/Sockets/license.html and/or email license@alhem.net. + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef _SOCKETS_Base64_H #define _SOCKETS_Base64_H + #include "sockets-config.h" #ifdef _MSC_VER #pragma warning(disable:4514) #endif + #include <stdio.h> #include <string> + #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif + /** \defgroup util Utilities */ + /** Base64 encode/decode. \ingroup util */ class Base64 { public: Base64(); + void encode(FILE *, std::string& , bool add_crlf = true); void encode(const std::string&, std::string& , bool add_crlf = true); void encode(const char *, size_t, std::string& , bool add_crlf = true); void encode(const unsigned char *, size_t, std::string& , bool add_crlf = true); + void decode(const std::string&, std::string& ); void decode(const std::string&, unsigned char *, size_t&); + size_t decode_length(const std::string& ); + private: Base64(const Base64& ) {} Base64& operator=(const Base64& ) { return *this; } @@ -57,5 +71,7 @@ static const char rstr[128]; #ifdef SOCKETS_NAMESPACE } #endif + #endif // _SOCKETS_Base64_H + diff --git a/dep/include/sockets/Exception.h b/dep/include/sockets/Exception.h index ac33ea1c6d6..bb881b2d74f 100644 --- a/dep/include/sockets/Exception.h +++ b/dep/include/sockets/Exception.h @@ -5,21 +5,26 @@ **/ /* Copyright (C) 2007 Anders Hedstrom + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef _Sockets_Exception_H #define _Sockets_Exception_H + #include <string> + #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif @@ -29,15 +34,22 @@ class Exception public: Exception(const std::string& description); virtual ~Exception() {} + virtual const std::string ToString() const; + Exception(const Exception& ) {} // copy constructor + Exception& operator=(const Exception& ) { return *this; } // assignment operator + private: std::string m_description; + }; #ifdef SOCKETS_NAMESPACE } // namespace SOCKETS_NAMESPACE { #endif + #endif // _Sockets_Exception_H + diff --git a/dep/include/sockets/File.h b/dep/include/sockets/File.h index 104f79b4d13..ed322efa2d8 100644 --- a/dep/include/sockets/File.h +++ b/dep/include/sockets/File.h @@ -4,29 +4,36 @@ **/ /* Copyright (C) 2004-2007 Anders Hedstrom + This library is made available under the terms of the GNU GPL. + If you would like to use this library in a closed-source application, a separate license agreement is available. For information about the closed-source license agreement for the C++ sockets library, please visit http://www.alhem.net/Sockets/license.html and/or email license@alhem.net. + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef _SOCKETS_File_H #define _SOCKETS_File_H + #include "sockets-config.h" #include "IFile.h" #include <stdio.h> + #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif @@ -38,19 +45,26 @@ class File : public IFile public: File(); ~File(); + bool fopen(const std::string&, const std::string&); void fclose(); + size_t fread(char *, size_t, size_t) const; size_t fwrite(const char *, size_t, size_t); + char *fgets(char *, int) const; void fprintf(const char *format, ...); + off_t size() const; bool eof() const; + void reset_read() const; void reset_write(); + private: File(const File& ) {} // copy constructor File& operator=(const File& ) { return *this; } // assignment operator + std::string m_path; std::string m_mode; FILE *m_fil; @@ -62,5 +76,7 @@ private: #ifdef SOCKETS_NAMESPACE } #endif + #endif // _SOCKETS_File_H + diff --git a/dep/include/sockets/IFile.h b/dep/include/sockets/IFile.h index ef4c859427e..657c8a4b1d9 100644 --- a/dep/include/sockets/IFile.h +++ b/dep/include/sockets/IFile.h @@ -4,31 +4,39 @@ **/ /* Copyright (C) 2004-2007 Anders Hedstrom + This library is made available under the terms of the GNU GPL. + If you would like to use this library in a closed-source application, a separate license agreement is available. For information about the closed-source license agreement for the C++ sockets library, please visit http://www.alhem.net/Sockets/license.html and/or email license@alhem.net. + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef _SOCKETS_IFile_H #define _SOCKETS_IFile_H + #include "sockets-config.h" #include <string> + #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif + /** \defgroup file File handling */ /** Pure virtual file I/O interface. \ingroup file */ @@ -36,20 +44,28 @@ class IFile { public: virtual ~IFile() {} + virtual bool fopen(const std::string&, const std::string&) = 0; virtual void fclose() = 0; + virtual size_t fread(char *, size_t, size_t) const = 0; virtual size_t fwrite(const char *, size_t, size_t) = 0; + virtual char *fgets(char *, int) const = 0; virtual void fprintf(const char *format, ...) = 0; + virtual off_t size() const = 0; virtual bool eof() const = 0; + virtual void reset_read() const = 0; virtual void reset_write() = 0; + }; #ifdef SOCKETS_NAMESPACE } #endif + #endif // _SOCKETS_IFile_H + diff --git a/dep/include/sockets/ISocketHandler.h b/dep/include/sockets/ISocketHandler.h index 4bc72949e5e..940783c104b 100644 --- a/dep/include/sockets/ISocketHandler.h +++ b/dep/include/sockets/ISocketHandler.h @@ -4,20 +4,25 @@ **/ /* Copyright (C) 2004-2007 Anders Hedstrom + This library is made available under the terms of the GNU GPL. + If you would like to use this library in a closed-source application, a separate license agreement is available. For information about the closed-source license agreement for the C++ sockets library, please visit http://www.alhem.net/Sockets/license.html and/or email license@alhem.net. + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. @@ -25,13 +30,17 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifndef _SOCKETS_ISocketHandler_H #define _SOCKETS_ISocketHandler_H #include "sockets-config.h" + #include <list> + #include "socket_include.h" #include "Socket.h" #include "StdLog.h" + #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif + typedef enum { LIST_CALLONCONNECT = 0, #ifdef ENABLE_DETACH @@ -41,6 +50,7 @@ typedef enum { LIST_RETRY, LIST_CLOSE } list_t; + class SocketAddress; class Mutex; @@ -49,6 +59,7 @@ class Mutex; class ISocketHandler { friend class Socket; + public: /** Connection pool class for internal use by the ISocketHandler. \ingroup internal */ @@ -60,22 +71,29 @@ public: CopyConnection( src ); SetIsClient(); } + void OnRead() { Handler().LogError(this, "OnRead", 0, "data on hibernating socket", LOG_LEVEL_FATAL); SetCloseAndDelete(); } void OnOptions(int,int,int,SOCKET) {} + }; #endif + public: virtual ~ISocketHandler() {} + /** Get mutex reference for threadsafe operations. */ virtual Mutex& GetMutex() const = 0; + /** Register StdLog object for error callback. \param log Pointer to log class */ virtual void RegStdLog(StdLog *log) = 0; + /** Log error to log class for print out / storage. */ virtual void LogError(Socket *p,const std::string& user_text,int err,const std::string& sys_err,loglevel_t t = LOG_LEVEL_WARNING) = 0; + // ------------------------------------------------------------------------- // Socket stuff // ------------------------------------------------------------------------- @@ -89,21 +107,26 @@ public: virtual void Get(SOCKET s,bool& r,bool& w,bool& e) = 0; /** Set read/write/exception file descriptor sets (fd_set). */ virtual void Set(SOCKET s,bool bRead,bool bWrite,bool bException = true) = 0; + /** Wait for events, generate callbacks. */ virtual int Select(long sec,long usec) = 0; /** This method will not return until an event has been detected. */ virtual int Select() = 0; /** Wait for events, generate callbacks. */ virtual int Select(struct timeval *tsel) = 0; + /** Check that a socket really is handled by this socket handler. */ virtual bool Valid(Socket *) = 0; /** Return number of sockets handled by this handler. */ virtual size_t GetCount() = 0; + /** Override and return false to deny all incoming connections. \param p ListenSocket class pointer (use GetPort to identify which one) */ virtual bool OkToAccept(Socket *p) = 0; + /** Called by Socket when a socket changes state. */ virtual void AddList(SOCKET s,list_t which_one,bool add) = 0; + // ------------------------------------------------------------------------- // Connection pool // ------------------------------------------------------------------------- @@ -116,6 +139,7 @@ public: \return true if connection pool is enabled */ virtual bool PoolEnabled() = 0; #endif // ENABLE_POOL + // ------------------------------------------------------------------------- // Socks4 // ------------------------------------------------------------------------- @@ -143,6 +167,7 @@ public: \return true if direct connection should be tried if connection to socks4 server fails */ virtual bool Socks4TryDirect() = 0; #endif // ENABLE_SOCKS4 + // ------------------------------------------------------------------------- // DNS resolve server // ------------------------------------------------------------------------- @@ -172,6 +197,7 @@ public: /** Returns true if socket waiting for a resolve event. */ virtual bool Resolving(Socket *) = 0; #endif // ENABLE_RESOLVER + #ifdef ENABLE_TRIGGERS /** Fetch unique trigger id. */ virtual int TriggerID(Socket *src) = 0; @@ -186,16 +212,20 @@ public: Leave them in place if 'false' - if a trigger should be called many times */ virtual void Trigger(int id, Socket::TriggerData& data, bool erase = true) = 0; #endif // ENABLE_TRIGGERS + #ifdef ENABLE_DETACH /** Indicates that the handler runs under SocketThread. */ virtual void SetSlave(bool x = true) = 0; /** Indicates that the handler runs under SocketThread. */ virtual bool IsSlave() = 0; #endif // ENABLE_DETACH + }; #ifdef SOCKETS_NAMESPACE } #endif + #endif // _SOCKETS_ISocketHandler_H + diff --git a/dep/include/sockets/Ipv4Address.h b/dep/include/sockets/Ipv4Address.h index 5a8d9a6ce20..71d925254e9 100644 --- a/dep/include/sockets/Ipv4Address.h +++ b/dep/include/sockets/Ipv4Address.h @@ -5,20 +5,24 @@ **/ /* Copyright (C) 2007 Anders Hedstrom + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef _SOCKETS_Ipv4Address_H #define _SOCKETS_Ipv4Address_H + #include "sockets-config.h" #include "SocketAddress.h" @@ -48,25 +52,33 @@ public: Ipv4Address(const std::string& host,port_t port); Ipv4Address(struct sockaddr_in&); ~Ipv4Address(); + // SocketAddress implementation + operator struct sockaddr *(); operator socklen_t(); bool operator==(SocketAddress&); + void SetPort(port_t port); port_t GetPort(); + void SetAddress(struct sockaddr *sa); int GetFamily(); + bool IsValid(); std::auto_ptr<SocketAddress> GetCopy(); + /** Convert address struct to text. */ std::string Convert(bool include_port = false); std::string Reverse(); + /** Resolve hostname. */ static bool Resolve(const std::string& hostname,struct in_addr& a); /** Reverse resolve (IP to hostname). */ static bool Reverse(struct in_addr& a,std::string& name); /** Convert address struct to text. */ static std::string Convert(struct in_addr& a); + private: Ipv4Address(const Ipv4Address& ) {} // copy constructor Ipv4Address& operator=(const Ipv4Address& ) { return *this; } // assignment operator @@ -80,3 +92,4 @@ private: #endif #endif // _SOCKETS_Ipv4Address_H + diff --git a/dep/include/sockets/Ipv6Address.h b/dep/include/sockets/Ipv6Address.h index b1a6afa98c6..20c68d8c92d 100644 --- a/dep/include/sockets/Ipv6Address.h +++ b/dep/include/sockets/Ipv6Address.h @@ -5,14 +5,17 @@ **/ /* Copyright (C) 2007 Anders Hedstrom + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. @@ -21,11 +24,13 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define _SOCKETS_Ipv6Address_H #include "sockets-config.h" #ifdef ENABLE_IPV6 + #include "SocketAddress.h" #ifdef IPPROTO_IPV6 #if defined( _WIN32) && !defined(__CYGWIN__) typedef unsigned __int32 uint32_t; #endif + #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif @@ -48,31 +53,40 @@ public: Ipv6Address(const std::string& host,port_t port); Ipv6Address(struct sockaddr_in6&); ~Ipv6Address(); + // SocketAddress implementation + operator struct sockaddr *(); operator socklen_t(); bool operator==(SocketAddress&); + void SetPort(port_t port); port_t GetPort(); + void SetAddress(struct sockaddr *sa); int GetFamily(); + bool IsValid(); std::auto_ptr<SocketAddress> GetCopy(); + /** Convert address struct to text. */ std::string Convert(bool include_port = false); std::string Reverse(); + /** Resolve hostname. */ static bool Resolve(const std::string& hostname,struct in6_addr& a); /** Reverse resolve (IP to hostname). */ static bool Reverse(struct in6_addr& a,std::string& name); /** Convert address struct to text. */ static std::string Convert(struct in6_addr& a,bool mixed = false); + void SetFlowinfo(uint32_t); uint32_t GetFlowinfo(); #ifndef _WIN32 void SetScopeId(uint32_t); uint32_t GetScopeId(); #endif + private: Ipv6Address(const Ipv6Address& ) {} // copy constructor Ipv6Address& operator=(const Ipv6Address& ) { return *this; } // assignment operator @@ -88,3 +102,4 @@ private: #endif // ENABLE_IPV6 #endif // _SOCKETS_Ipv6Address_H + diff --git a/dep/include/sockets/ListenSocket.h b/dep/include/sockets/ListenSocket.h index aeb5bf028f2..8934a809d0e 100644 --- a/dep/include/sockets/ListenSocket.h +++ b/dep/include/sockets/ListenSocket.h @@ -4,20 +4,25 @@ **/ /* Copyright (C) 2004-2007 Anders Hedstrom + This library is made available under the terms of the GNU GPL. + If you would like to use this library in a closed-source application, a separate license agreement is available. For information about the closed-source license agreement for the C++ sockets library, please visit http://www.alhem.net/Sockets/license.html and/or email license@alhem.net. + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. @@ -25,11 +30,13 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifndef _SOCKETS_ListenSocket_H #define _SOCKETS_ListenSocket_H #include "sockets-config.h" + #ifdef _WIN32 #include <stdlib.h> #else #include <errno.h> #endif + #include "ISocketHandler.h" #include "Socket.h" #include "Utility.h" @@ -39,6 +46,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifdef ENABLE_EXCEPTIONS #include "Exception.h" #endif + #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif @@ -75,6 +83,7 @@ public: delete m_creator; } } + /** Close file descriptor. */ int Close() { if (GetSocket() != INVALID_SOCKET) @@ -83,6 +92,7 @@ public: } return 0; } + /** Bind and listen to any interface. \param port Port (0 is random) \param depth Listen queue depth */ @@ -102,6 +112,7 @@ public: return Bind(ad, depth); } } + int Bind(SocketAddress& ad,int depth) { #ifdef USE_SCTP if (dynamic_cast<SctpSocket *>(m_creator)) @@ -111,6 +122,7 @@ public: #endif return Bind(ad, "tcp", depth); } + /** Bind and listen to any interface, with optional protocol. \param port Port (0 is random) \param protocol Network protocol @@ -131,6 +143,7 @@ public: return Bind(ad, protocol, depth); } } + /** Bind and listen to specific interface. \param intf Interface hostname \param port Port (0 is random) @@ -161,6 +174,7 @@ public: return -1; } } + /** Bind and listen to specific interface. \param intf Interface hostname \param port Port (0 is random) @@ -192,6 +206,7 @@ public: return -1; } } + /** Bind and listen to ipv4 interface. \param a Ipv4 interface address \param port Port (0 is random) @@ -215,6 +230,7 @@ public: Ipv4Address ad(a, port); return Bind(ad, protocol, depth); } + #ifdef ENABLE_IPV6 #ifdef IPPROTO_IPV6 /** Bind and listen to ipv6 interface. @@ -242,6 +258,7 @@ public: } #endif #endif + /** Bind and listen to network interface. \param ad Interface address \param protocol Network protocol @@ -274,22 +291,26 @@ public: Attach(s); return 0; } + /** Return assigned port number. */ port_t GetPort() { return GetSockPort(); } + /** Return listen queue depth. */ int GetDepth() { return m_depth; } + /** OnRead on a ListenSocket receives an incoming connection. */ void OnRead() { struct sockaddr sa; socklen_t sa_len = sizeof(struct sockaddr); SOCKET a_s = accept(GetSocket(), &sa, &sa_len); + if (a_s == INVALID_SOCKET) { Handler().LogError(this, "accept", Errno, StrError(Errno), LOG_LEVEL_ERROR); @@ -365,16 +386,20 @@ public: tmp -> OnAccept(); } } + /** Please don't use this method. "accept()" is handled automatically in the OnRead() method. */ virtual SOCKET Accept(SOCKET socket, struct sockaddr *saptr, socklen_t *lenptr) { return accept(socket, saptr, lenptr); } + bool HasCreator() { return m_bHasCreate; } + void OnOptions(int,int,int,SOCKET) { SetSoReuseaddr(true); } + protected: ListenSocket(const ListenSocket& s) : Socket(s) {} private: @@ -387,5 +412,7 @@ private: #ifdef SOCKETS_NAMESPACE } #endif + #endif // _SOCKETS_ListenSocket_H + diff --git a/dep/include/sockets/Lock.h b/dep/include/sockets/Lock.h index f2d9e6fb713..f3bb9273920 100644 --- a/dep/include/sockets/Lock.h +++ b/dep/include/sockets/Lock.h @@ -4,31 +4,39 @@ **/ /* Copyright (C) 2005,2007 Anders Hedstrom + This library is made available under the terms of the GNU GPL. + If you would like to use this library in a closed-source application, a separate license agreement is available. For information about the closed-source license agreement for the C++ sockets library, please visit http://www.alhem.net/Sockets/license.html and/or email license@alhem.net. + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef _SOCKETS_Lock_H #define _SOCKETS_Lock_H + #include "sockets-config.h" #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif + class Mutex; + /** Mutex encapsulation class. \ingroup threading */ class Lock @@ -36,6 +44,7 @@ class Lock public: Lock(Mutex&); ~Lock(); + private: Mutex& m_mutex; }; @@ -46,3 +55,4 @@ private: #endif #endif // _SOCKETS_Lock_H + diff --git a/dep/include/sockets/Mutex.h b/dep/include/sockets/Mutex.h index 4dba5e7b401..e42a57c3262 100644 --- a/dep/include/sockets/Mutex.h +++ b/dep/include/sockets/Mutex.h @@ -4,35 +4,43 @@ **/ /* Copyright (C) 2004-2007 Anders Hedstrom + This library is made available under the terms of the GNU GPL. + If you would like to use this library in a closed-source application, a separate license agreement is available. For information about the closed-source license agreement for the C++ sockets library, please visit http://www.alhem.net/Sockets/license.html and/or email license@alhem.net. + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef _SOCKETS_Mutex_H #define _SOCKETS_Mutex_H + #include "sockets-config.h" #ifndef _WIN32 #include <pthread.h> #else #include <windows.h> #endif + #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif + /** Mutex container class, used by Lock. \ingroup threading */ class Mutex @@ -41,6 +49,7 @@ class Mutex public: Mutex(); ~Mutex(); + void Lock(); void Unlock(); private: @@ -56,3 +65,4 @@ private: #endif #endif // _SOCKETS_Mutex_H + diff --git a/dep/include/sockets/Parse.h b/dep/include/sockets/Parse.h index 6ad4d44290c..52bd9327e28 100644 --- a/dep/include/sockets/Parse.h +++ b/dep/include/sockets/Parse.h @@ -2,39 +2,50 @@ ** ** Written: 1999-Feb-10 grymse@alhem.net **/ + /* Copyright (C) 1999-2007 Anders Hedstrom + This library is made available under the terms of the GNU GPL. + If you would like to use this library in a closed-source application, a separate license agreement is available. For information about the closed-source license agreement for the C++ sockets library, please visit http://www.alhem.net/Sockets/license.html and/or email license@alhem.net. + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + #ifndef _SOCKETS_Parse_H #define _SOCKETS_Parse_H + #include "sockets-config.h" #ifdef _MSC_VER #pragma warning(disable:4514) #endif + #include <string> + #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif /***************************************************/ /* interface of class Parse */ + /** Splits a string whatever way you want. \ingroup util */ class Parse @@ -67,6 +78,7 @@ public: void getline(std::string&); size_t getptr() { return pa_the_ptr; } void EnableQuote(bool b) { pa_quote = b; } + private: std::string pa_the_str; std::string pa_splits; @@ -82,5 +94,7 @@ private: #ifdef SOCKETS_NAMESPACE } #endif + #endif // _SOCKETS_Parse_H + diff --git a/dep/include/sockets/ResolvServer.h b/dep/include/sockets/ResolvServer.h index fb3f5ec1527..409c9b7a619 100644 --- a/dep/include/sockets/ResolvServer.h +++ b/dep/include/sockets/ResolvServer.h @@ -4,20 +4,25 @@ **/ /* Copyright (C) 2004-2007 Anders Hedstrom + This library is made available under the terms of the GNU GPL. + If you would like to use this library in a closed-source application, a separate license agreement is available. For information about the closed-source license agreement for the C++ sockets library, please visit http://www.alhem.net/Sockets/license.html and/or email license@alhem.net. + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. @@ -28,9 +33,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifdef ENABLE_RESOLVER #include "socket_include.h" #include "Thread.h" + #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif + /** \defgroup async Asynchronous DNS */ /** Async DNS resolver thread. \ingroup async */ @@ -39,12 +46,16 @@ class ResolvServer : public Thread public: ResolvServer(port_t); ~ResolvServer(); + void Run(); void Quit(); + bool Ready(); + private: ResolvServer(const ResolvServer& ) {} // copy constructor ResolvServer& operator=(const ResolvServer& ) { return *this; } // assignment operator + bool m_quit; port_t m_port; bool m_ready; @@ -54,6 +65,8 @@ private: #ifdef SOCKETS_NAMESPACE } #endif + #endif // ENABLE_RESOLVER #endif // _SOCKETS_ResolvServer_H + diff --git a/dep/include/sockets/ResolvSocket.h b/dep/include/sockets/ResolvSocket.h index 2347f040a7a..60743736e08 100644 --- a/dep/include/sockets/ResolvSocket.h +++ b/dep/include/sockets/ResolvSocket.h @@ -4,20 +4,25 @@ **/ /* Copyright (C) 2004-2007 Anders Hedstrom + This library is made available under the terms of the GNU GPL. + If you would like to use this library in a closed-source application, a separate license agreement is available. For information about the closed-source license agreement for the C++ sockets library, please visit http://www.alhem.net/Sockets/license.html and/or email license@alhem.net. + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. @@ -28,10 +33,13 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifdef ENABLE_RESOLVER #include "TcpSocket.h" #include <map> + #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif + class Mutex; + /** Async DNS resolver socket. \ingroup async */ class ResolvSocket : public TcpSocket @@ -40,6 +48,7 @@ class ResolvSocket : public TcpSocket std::map<std::string, std::string> > cache_t; /* host, result */ typedef std::map<std::string, /* type */ std::map<std::string, time_t> > timeout_t; /* host, time */ + public: ResolvSocket(ISocketHandler&); ResolvSocket(ISocketHandler&, Socket *parent, const std::string& host, port_t port, bool ipv6 = false); @@ -48,19 +57,25 @@ public: ResolvSocket(ISocketHandler&, Socket *parent, in6_addr&); #endif ~ResolvSocket(); + void OnAccept() { m_bServer = true; } void OnLine(const std::string& line); void OnDetached(); void OnDelete(); + void SetId(int x) { m_resolv_id = x; } int GetId() { return m_resolv_id; } + void OnConnect(); + #ifdef ENABLE_IPV6 void SetResolveIpv6(bool x = true) { m_resolve_ipv6 = x; } #endif + private: ResolvSocket(const ResolvSocket& s) : TcpSocket(s) {} // copy constructor ResolvSocket& operator=(const ResolvSocket& ) { return *this; } // assignment operator + std::string m_query; std::string m_data; bool m_bServer; @@ -83,6 +98,8 @@ private: #ifdef SOCKETS_NAMESPACE } #endif + #endif // ENABLE_RESOLVER #endif // _SOCKETS_ResolvSocket_H + diff --git a/dep/include/sockets/SctpSocket.h b/dep/include/sockets/SctpSocket.h index b3fdd9ae1ab..ed507fb1880 100644 --- a/dep/include/sockets/SctpSocket.h +++ b/dep/include/sockets/SctpSocket.h @@ -5,14 +5,17 @@ **/ /* Copyright (C) 2007 Anders Hedstrom + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. @@ -20,13 +23,17 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifndef _SOCKETS_SctpSocket_H #define _SOCKETS_SctpSocket_H #include "sockets-config.h" + #include "StreamSocket.h" #ifdef USE_SCTP #include <netinet/sctp.h> + #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif + #define SCTP_BUFSIZE_READ 16400 + class SocketAddress; class SctpSocket : public StreamSocket @@ -37,6 +44,7 @@ public: \param type SCTP_STREAM or SCTP_SEQPACKET */ SctpSocket(ISocketHandler& h,int type); ~SctpSocket(); + /** bind() */ int Bind(const std::string&,port_t); int Bind(SocketAddress&); @@ -46,34 +54,44 @@ public: /** sctp_bindx() */ int RemoveAddress(const std::string&,port_t); int RemoveAddress(SocketAddress&); + /** connect() */ int Open(const std::string&,port_t); int Open(SocketAddress&); + /** Connect timeout callback. */ void OnConnectTimeout(); #ifdef _WIN32 /** Connection failed reported as exception on win32 */ void OnException(); #endif + #ifndef SOLARIS /** sctp_connectx() */ int AddConnection(const std::string&,port_t); int AddConnection(SocketAddress&); #endif + /** Get peer addresses of an association. */ int getpaddrs(sctp_assoc_t id,std::list<std::string>&); /** Get all bound addresses of an association. */ int getladdrs(sctp_assoc_t id,std::list<std::string>&); + /** sctp_peeloff */ int PeelOff(sctp_assoc_t id); + /** recvmsg callback */ virtual void OnReceiveMessage(const char *buf,size_t sz,struct sockaddr *sa,socklen_t sa_len,struct sctp_sndrcvinfo *sinfo,int msg_flags) = 0; + void OnOptions(int,int,int,SOCKET) {} + virtual int Protocol(); + protected: SctpSocket(const SctpSocket& s) : StreamSocket(s) {} void OnRead(); void OnWrite(); + private: SctpSocket& operator=(const SctpSocket& s) { return *this; } int m_type; ///< SCTP_STREAM or SCTP_SEQPACKET @@ -83,6 +101,8 @@ private: #ifdef SOCKETS_NAMESPACE } // namespace SOCKETS_NAMESPACE #endif + #endif // USE_SCTP #endif // _SOCKETS_SctpSocket_H + diff --git a/dep/include/sockets/Socket.h b/dep/include/sockets/Socket.h index cb1c2fb3c29..23a806b5ea1 100644 --- a/dep/include/sockets/Socket.h +++ b/dep/include/sockets/Socket.h @@ -4,20 +4,25 @@ **/ /* Copyright (C) 2004-2007 Anders Hedstrom + This software is made available under the terms of the GNU GPL. + If you would like to use this library in a closed-source application, a separate license agreement is available. For information about the closed-source license agreement for the C++ sockets library, please visit http://www.alhem.net/Sockets/license.html and/or email license@alhem.net. + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. @@ -25,12 +30,14 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifndef _SOCKETS_Socket_H #define _SOCKETS_Socket_H #include "sockets-config.h" + #include <string> #include <vector> #include <list> #ifdef HAVE_OPENSSL #include <openssl/ssl.h> #endif + #include "socket_include.h" #include <time.h> #include "SocketAddress.h" @@ -58,7 +65,9 @@ class Socket public: SocketThread(Socket *p); ~SocketThread(); + void Run(); + private: Socket *GetSocket() const { return m_socket; } SocketThread(const SocketThread& s) : m_socket(s.GetSocket()) {} @@ -66,6 +75,7 @@ class Socket Socket *m_socket; }; #endif // ENABLE_DETACH + #ifdef ENABLE_TRIGGERS public: /** Data pass class from source to destination. */ @@ -74,12 +84,15 @@ public: public: TriggerData() : m_src(NULL) {} virtual ~TriggerData() {} + Socket *GetSource() const { return m_src; } void SetSource(Socket *x) { m_src = x; } + private: Socket *m_src; }; #endif // ENABLE_TRIGGERS + /** Socket mode flags. */ /* enum { @@ -88,17 +101,20 @@ public: SOCK_CLOSE = 0x02, ///< Close and delete flag SOCK_DISABLE_READ = 0x04, ///< Disable checking for read events SOCK_CONNECTED = 0x08, ///< Socket is connected (tcp/udp) + SOCK_ERASED_BY_HANDLER = 0x10, ///< Set by handler before delete // HAVE_OPENSSL SOCK_ENABLE_SSL = 0x20, ///< Enable SSL for this TcpSocket SOCK_SSL = 0x40, ///< ssl negotiation mode (TcpSocket) SOCK_SSL_SERVER = 0x80, ///< True if this is an incoming ssl TcpSocket connection + // ENABLE_IPV6 SOCK_IPV6 = 0x0100, ///< This is an ipv6 socket if this one is true // ENABLE_POOL SOCK_CLIENT = 0x0200, ///< only client connections are pooled SOCK_RETAIN = 0x0400, ///< keep connection on close SOCK_LOST = 0x0800, ///< connection lost + // ENABLE_SOCKS4 SOCK_SOCKS4 = 0x1000, ///< socks4 negotiation mode (TcpSocket) // ENABLE_DETACH @@ -106,30 +122,38 @@ public: SOCK_DETACHED = 0x4000, ///< Socket has been detached // StreamSocket STREAMSOCK_CONNECTING = 0x8000, ///< Flag indicating connection in progress + STREAMSOCK_FLUSH_BEFORE_CLOSE = 0x010000L, ///< Send all data before closing (default true) STREAMSOCK_CALL_ON_CONNECT = 0x020000L, ///< OnConnect will be called next ISocketHandler cycle if true STREAMSOCK_RETRY_CONNECT = 0x040000L, ///< Try another connection attempt next ISocketHandler cycle STREAMSOCK_LINE_PROTOCOL = 0x080000L, ///< Line protocol mode flag + }; */ + public: /** "Default" constructor */ Socket(ISocketHandler&); + virtual ~Socket(); + /** Socket class instantiation method. Used when a "non-standard" constructor * needs to be used for the socket class. Note: the socket class still needs * the "default" constructor with one ISocketHandler& as input parameter. */ virtual Socket *Create() { return NULL; } + /** Returns reference to sockethandler that owns the socket. If the socket is detached, this is a reference to the slave sockethandler. */ ISocketHandler& Handler() const; + /** Returns reference to sockethandler that owns the socket. This one always returns the reference to the original sockethandler, even if the socket is detached. */ ISocketHandler& MasterHandler() const; + /** Called by ListenSocket after accept but before socket is added to handler. * CTcpSocket uses this to create its ICrypt member variable. * The ICrypt member variable is created by a virtual method, therefore @@ -138,59 +162,83 @@ public: * or ssl (port 443). */ virtual void Init(); + /** Create a socket file descriptor. \param af Address family AF_INET / AF_INET6 / ... \param type SOCK_STREAM / SOCK_DGRAM / ... \param protocol "tcp" / "udp" / ... */ SOCKET CreateSocket(int af,int type,const std::string& protocol = ""); + /** Assign this socket a file descriptor created by a call to socket() or otherwise. */ void Attach(SOCKET s); + /** Return file descriptor assigned to this socket. */ SOCKET GetSocket(); + /** Close connection immediately - internal use. \sa SetCloseAndDelete */ virtual int Close(); + /** Add file descriptor to sockethandler fd_set's. */ void Set(bool bRead,bool bWrite,bool bException = true); + /** Returns true when socket file descriptor is valid and socket is not about to be closed. */ virtual bool Ready(); + /** Returns pointer to ListenSocket that created this instance * on an incoming connection. */ Socket *GetParent(); + /** Used by ListenSocket to set parent pointer of newly created * socket instance. */ void SetParent(Socket *); + /** Get listening port from ListenSocket<>. */ virtual port_t GetPort(); + /** Set socket non-block operation. */ bool SetNonblocking(bool); + /** Set socket non-block operation. */ bool SetNonblocking(bool, SOCKET); + /** Total lifetime of instance. */ time_t Uptime(); + /** Set address/port of last connect() call. */ void SetClientRemoteAddress(SocketAddress&); + /** Get address/port of last connect() call. */ std::auto_ptr<SocketAddress> GetClientRemoteAddress(); + /** Common interface for SendBuf used by Tcp and Udp sockets. */ virtual void SendBuf(const char *,size_t,int = 0); + /** Common interface for Send used by Tcp and Udp sockets. */ virtual void Send(const std::string&,int = 0); + /** Outgoing traffic counter. */ virtual uint64_t GetBytesSent(bool clear = false); + /** Incoming traffic counter. */ virtual uint64_t GetBytesReceived(bool clear = false); + // LIST_TIMEOUT + /** Enable timeout control. 0=disable timeout check. */ void SetTimeout(time_t secs); + /** Check timeout. \return true if time limit reached */ bool Timeout(time_t tnow); + /** Used by ListenSocket. ipv4 and ipv6 */ void SetRemoteAddress(SocketAddress&); + /** \name Event callbacks */ //@{ + /** Called when there is something to be read from the file descriptor. */ virtual void OnRead(); /** Called when there is room for another write on the file descriptor. */ @@ -228,6 +276,7 @@ public: /** Connection timeout. */ virtual void OnConnectTimeout(); //@} + /** \name Socket mode flags, set/reset */ //@{ /** Set delete by handler true when you want the sockethandler to @@ -236,34 +285,43 @@ public: /** Check delete by handler flag. \return true if this instance should be deleted by the sockethandler */ bool DeleteByHandler(); + // LIST_CLOSE - conditional event queue + /** Set close and delete to terminate the connection. */ void SetCloseAndDelete(bool = true); /** Check close and delete flag. \return true if this socket should be closed and the instance removed */ bool CloseAndDelete(); + /** Return number of seconds since socket was ordered to close. \sa SetCloseAndDelete */ time_t TimeSinceClose(); + /** Ignore read events for an output only socket. */ void DisableRead(bool x = true); /** Check ignore read events flag. \return true if read events should be ignored */ bool IsDisableRead(); + /** Set connected status. */ void SetConnected(bool = true); /** Check connected status. \return true if connected */ bool IsConnected(); + /** Connection lost - error while reading/writing from a socket - TcpSocket only. */ void SetLost(); /** Check connection lost status flag, used by TcpSocket only. \return true if there was an error while r/w causing the socket to close */ bool Lost(); + /** Set flag indicating the socket is being actively deleted by the sockethandler. */ void SetErasedByHandler(bool x = true); /** Get value of flag indicating socket is deleted by sockethandler. */ bool ErasedByHandler(); + //@} + /** \name Information about remote connection */ //@{ /** Returns address of remote end. */ @@ -283,6 +341,7 @@ public: /** ipv4 and ipv6(not implemented) */ std::string GetRemoteHostname(); //@} + /** Returns local port number for bound socket file descriptor. */ port_t GetSockPort(); /** Returns local ipv4 address for bound socket file descriptor. */ @@ -304,8 +363,10 @@ public: checked with an #ifdef below. This might cause a compile error on other operating systems. */ // -------------------------------------------------------------------------- + // IP options //@{ + bool SetIpOptions(const void *p, socklen_t len); bool SetIpTOS(unsigned char tos); unsigned char IpTOS(); @@ -317,6 +378,7 @@ public: bool SetMulticastLoop(bool x = true); bool IpAddMembership(struct ip_mreq&); bool IpDropMembership(struct ip_mreq&); + #ifdef IP_PKTINFO bool SetIpPktinfo(bool x = true); #endif @@ -351,9 +413,11 @@ public: bool IpDropMembership(struct ip_mreqn&); #endif //@} + // SOCKET options /** @name Socket Options */ //@{ + bool SoAcceptconn(); bool SetSoBroadcast(bool x = true); bool SetSoDebug(bool x = true); @@ -372,6 +436,7 @@ public: int SoType(); bool SetSoReuseaddr(bool x = true); bool SetSoKeepalive(bool x = true); + #ifdef SO_BSDCOMPAT bool SetSoBsdcompat(bool x = true); #endif @@ -400,6 +465,7 @@ public: bool SetSoNosigpipe(bool x = true); #endif //@} + // TCP options in TcpSocket.h/TcpSocket.cpp #ifdef HAVE_OPENSSL @@ -436,6 +502,7 @@ public: virtual SSL *GetSsl() { return NULL; } //@} #endif // HAVE_OPENSSL + #ifdef ENABLE_IPV6 /** Enable ipv6 for this socket. */ void SetIpv6(bool x = true); @@ -443,6 +510,7 @@ public: \return true if this is an ipv6 socket */ bool IsIpv6(); #endif + #ifdef ENABLE_POOL /** @name Connection Pool */ //@{ @@ -470,6 +538,7 @@ public: void CopyConnection(Socket *sock); //@} #endif // ENABLE_POOL + #ifdef ENABLE_SOCKS4 /** \name Socks4 support */ //@{ @@ -485,6 +554,7 @@ public: bool Socks4(); /** Set flag indicating Socks4 handshaking in progress */ void SetSocks4(bool x = true); + /** Set socks4 server host address to use */ void SetSocks4Host(ipaddr_t a); /** Set socks4 server hostname to use. */ @@ -504,6 +574,7 @@ public: const std::string& GetSocks4Userid(); //@} #endif // ENABLE_SOCKS4 + #ifdef ENABLE_RESOLVER /** \name Asynchronous Resolver */ //@{ @@ -538,6 +609,7 @@ public: virtual void OnResolveFailed(int id); //@} #endif // ENABLE_RESOLVER + #ifdef ENABLE_DETACH /** \name Thread Support */ //@{ @@ -545,12 +617,15 @@ public: socket is ready for operation again. \sa ResolvSocket */ virtual void OnDetached(); + // LIST_DETACH + /** Internal use. */ void SetDetach(bool x = true); /** Check detach flag. \return true if the socket should detach to its own thread */ bool IsDetach(); + /** Internal use. */ void SetDetached(bool x = true); /** Check detached flag. @@ -565,8 +640,10 @@ public: void DetachSocket(); //@} #endif // ENABLE_DETACH + /** Write traffic to an IFile. Socket will not delete this object. */ void SetTrafficMonitor(IFile *p) { m_traffic_monitor = p; } + #ifdef ENABLE_TRIGGERS /** \name Triggers */ //@{ @@ -580,16 +657,21 @@ public: virtual void OnCancelled(int id); //@} #endif + protected: /** default constructor not available */ Socket() : m_handler(m_handler) {} /** copy constructor not available */ Socket(const Socket& s) : m_handler(s.m_handler) {} + /** assignment operator not available. */ Socket& operator=(const Socket& ) { return *this; } + /** All traffic will be written to this IFile, if set. */ IFile *GetTrafficMonitor() { return m_traffic_monitor; } + // unsigned long m_flags; ///< boolean flags, replacing old 'bool' members + private: ISocketHandler& m_handler; ///< Reference of ISocketHandler in control of this socket SOCKET m_socket; ///< File descriptor @@ -607,29 +689,35 @@ private: time_t m_timeout_start; ///< Set by SetTimeout time_t m_timeout_limit; ///< Defined by SetTimeout bool m_bLost; ///< connection lost + #ifdef _WIN32 static WSAInitializer m_winsock_init; ///< Winsock initialization singleton class #endif + #ifdef HAVE_OPENSSL bool m_b_enable_ssl; ///< Enable SSL for this TcpSocket bool m_b_ssl; ///< ssl negotiation mode (TcpSocket) bool m_b_ssl_server; ///< True if this is an incoming ssl TcpSocket connection #endif + #ifdef ENABLE_IPV6 bool m_ipv6; ///< This is an ipv6 socket if this one is true #endif + #ifdef ENABLE_POOL int m_socket_type; ///< Type of socket, from socket() call std::string m_socket_protocol; ///< Protocol, from socket() call bool m_bClient; ///< only client connections are pooled bool m_bRetain; ///< keep connection on close #endif + #ifdef ENABLE_SOCKS4 bool m_bSocks4; ///< socks4 negotiation mode (TcpSocket) ipaddr_t m_socks4_host; ///< socks4 server address port_t m_socks4_port; ///< socks4 server port number std::string m_socks4_userid; ///< socks4 server usedid #endif + #ifdef ENABLE_DETACH bool m_detach; ///< Socket ordered to detach flag bool m_detached; ///< Socket has been detached @@ -637,9 +725,11 @@ static WSAInitializer m_winsock_init; ///< Winsock initialization singleton cla ISocketHandler *m_slave_handler; ///< Actual sockethandler while detached #endif }; + #ifdef SOCKETS_NAMESPACE } #endif #endif // _SOCKETS_Socket_H + diff --git a/dep/include/sockets/SocketAddress.h b/dep/include/sockets/SocketAddress.h index af7aaa3a03d..abdbbfd2cf6 100644 --- a/dep/include/sockets/SocketAddress.h +++ b/dep/include/sockets/SocketAddress.h @@ -5,24 +5,29 @@ **/ /* Copyright (C) 2007 Anders Hedstrom + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef _SOCKETS_SocketAddress_H #define _SOCKETS_SocketAddress_H + #include "sockets-config.h" #include <string> #include <memory> #include "socket_include.h" + #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif @@ -41,29 +46,40 @@ class SocketAddress { public: virtual ~SocketAddress() {} + /** Get a pointer to the address struct. */ virtual operator struct sockaddr *() = 0; + /** Get length of address struct. */ virtual operator socklen_t() = 0; + /** Compare two addresses. */ virtual bool operator==(SocketAddress&) = 0; + /** Set port number. \param port Port number in host byte order */ virtual void SetPort(port_t port) = 0; + /** Get port number. \return Port number in host byte order. */ virtual port_t GetPort() = 0; + /** Set socket address. \param sa Pointer to either 'struct sockaddr_in' or 'struct sockaddr_in6'. */ virtual void SetAddress(struct sockaddr *sa) = 0; + /** Convert address to text. */ virtual std::string Convert(bool include_port) = 0; + /** Reverse lookup of address. */ virtual std::string Reverse() = 0; + /** Get address family. */ virtual int GetFamily() = 0; + /** Address structure is valid. */ virtual bool IsValid() = 0; + /** Get a copy of this SocketAddress object. */ virtual std::auto_ptr<SocketAddress> GetCopy() = 0; }; @@ -74,3 +90,4 @@ public: #endif #endif // _SOCKETS_SocketAddress_H + diff --git a/dep/include/sockets/SocketHandler.h b/dep/include/sockets/SocketHandler.h index e187de5f675..5598ec4249b 100644 --- a/dep/include/sockets/SocketHandler.h +++ b/dep/include/sockets/SocketHandler.h @@ -4,31 +4,39 @@ **/ /* Copyright (C) 2004-2007 Anders Hedstrom + This library is made available under the terms of the GNU GPL. + If you would like to use this library in a closed-source application, a separate license agreement is available. For information about the closed-source license agreement for the C++ sockets library, please visit http://www.alhem.net/Sockets/license.html and/or email license@alhem.net. + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef _SOCKETS_SocketHandler_H #define _SOCKETS_SocketHandler_H + #include "sockets-config.h" #include <map> #include <list> + #include "socket_include.h" #include "ISocketHandler.h" + #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif @@ -38,6 +46,7 @@ class Socket; class ResolvServer; #endif class Mutex; + /** Socket container class, event generator. \ingroup basic */ class SocketHandler : public ISocketHandler @@ -45,43 +54,60 @@ class SocketHandler : public ISocketHandler protected: /** Map type for holding file descriptors/socket object pointers. */ typedef std::map<SOCKET,Socket *> socket_m; + public: /** SocketHandler constructor. \param log Optional log class pointer */ SocketHandler(StdLog *log = NULL); + /** SocketHandler threadsafe constructor. \param mutex Externally declared mutex variable \param log Optional log class pointer */ SocketHandler(Mutex& mutex,StdLog *log = NULL); + ~SocketHandler(); + /** Get mutex reference for threadsafe operations. */ Mutex& GetMutex() const; + /** Register StdLog object for error callback. \param log Pointer to log class */ void RegStdLog(StdLog *log); + /** Log error to log class for print out / storage. */ void LogError(Socket *p,const std::string& user_text,int err,const std::string& sys_err,loglevel_t t = LOG_LEVEL_WARNING); + /** Add socket instance to socket map. Removal is always automatic. */ void Add(Socket *); + /** Get status of read/write/exception file descriptor set for a socket. */ void Get(SOCKET s,bool& r,bool& w,bool& e); + /** Set read/write/exception file descriptor sets (fd_set). */ void Set(SOCKET s,bool bRead,bool bWrite,bool bException = true); + /** Wait for events, generate callbacks. */ int Select(long sec,long usec); + /** This method will not return until an event has been detected. */ int Select(); + /** Wait for events, generate callbacks. */ int Select(struct timeval *tsel); + /** Check that a socket really is handled by this socket handler. */ bool Valid(Socket *); + /** Return number of sockets handled by this handler. */ size_t GetCount(); + /** Override and return false to deny all incoming connections. \param p ListenSocket class pointer (use GetPort to identify which one) */ bool OkToAccept(Socket *p); + /** Called by Socket when a socket changes state. */ void AddList(SOCKET s,list_t which_one,bool add); + // Connection pool #ifdef ENABLE_POOL /** Find available open connection (used by connection pool). */ @@ -92,6 +118,7 @@ public: \return true if connection pool is enabled */ bool PoolEnabled(); #endif // ENABLE_POOL + // Socks4 #ifdef ENABLE_SOCKS4 /** Set socks4 server ip that all new tcp sockets should use. */ @@ -117,6 +144,7 @@ public: \return true if direct connection should be tried if connection to socks4 server fails */ bool Socks4TryDirect(); #endif // ENABLE_SOCKS4 + // DNS resolve server #ifdef ENABLE_RESOLVER /** Enable asynchronous DNS. @@ -144,6 +172,7 @@ public: /** Returns true if the socket is waiting for a resolve event. */ bool Resolving(Socket *); #endif // ENABLE_RESOLVER + #ifdef ENABLE_TRIGGERS /** Fetch unique trigger id. */ int TriggerID(Socket *src); @@ -158,22 +187,27 @@ public: Leave them in place if 'false' - if a trigger should be called many times */ void Trigger(int id, Socket::TriggerData& data, bool erase = true); #endif // ENABLE_TRIGGERS + #ifdef ENABLE_DETACH /** Indicates that the handler runs under SocketThread. */ void SetSlave(bool x = true); /** Indicates that the handler runs under SocketThread. */ bool IsSlave(); #endif + /** Sanity check of those accursed lists. */ void CheckSanity(); + protected: socket_m m_sockets; ///< Active sockets map socket_m m_add; ///< Sockets to be added to sockets map std::list<Socket *> m_delete; ///< Sockets to be deleted (failed when Add) + protected: StdLog *m_stdlog; ///< Registered log class, or NULL Mutex& m_mutex; ///< Thread safety mutex bool m_b_use_mutex; ///< Mutex correctly initialized + private: void CheckList(socket_v&,const std::string&); ///< Used by CheckSanity /** Remove socket from socket map, used by Socket class. */ @@ -185,6 +219,7 @@ private: int m_preverror; ///< debug select() error int m_errcnt; ///< debug select() error time_t m_tlast; ///< timeout control + // state lists socket_v m_fds; ///< Active file descriptor list socket_v m_fds_erase; ///< File descriptors that are to be erased from m_sockets @@ -195,6 +230,7 @@ private: socket_v m_fds_timeout; ///< checklist timeout socket_v m_fds_retry; ///< checklist retry client connect socket_v m_fds_close; ///< checklist close and delete + #ifdef ENABLE_SOCKS4 ipaddr_t m_socks4_host; ///< Socks4 server host ip port_t m_socks4_port; ///< Socks4 server port number @@ -223,5 +259,7 @@ private: #ifdef SOCKETS_NAMESPACE } #endif + #endif // _SOCKETS_SocketHandler_H + diff --git a/dep/include/sockets/StdLog.h b/dep/include/sockets/StdLog.h index 65d91081338..3ff68d6e9ea 100644 --- a/dep/include/sockets/StdLog.h +++ b/dep/include/sockets/StdLog.h @@ -4,31 +4,39 @@ **/ /* Copyright (C) 2004-2007 Anders Hedstrom + This library is made available under the terms of the GNU GPL. + If you would like to use this library in a closed-source application, a separate license agreement is available. For information about the closed-source license agreement for the C++ sockets library, please visit http://www.alhem.net/Sockets/license.html and/or email license@alhem.net. + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef _SOCKETS_StdLog_H #define _SOCKETS_StdLog_H + #include "sockets-config.h" #include <string> + #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif + /** error level enum. */ typedef enum { @@ -40,6 +48,7 @@ typedef enum class ISocketHandler; class Socket; + /** \defgroup logging Log help classes */ /** Log class interface. \ingroup logging */ @@ -47,6 +56,7 @@ class StdLog { public: virtual ~StdLog() {} + virtual void error(ISocketHandler *,Socket *, const std::string& user_text, int err, @@ -57,5 +67,7 @@ public: #ifdef SOCKETS_NAMESPACE } #endif + #endif // _SOCKETS_StdLog_H + diff --git a/dep/include/sockets/StdoutLog.h b/dep/include/sockets/StdoutLog.h index 8e97700a2ff..aeb25b3e6e6 100644 --- a/dep/include/sockets/StdoutLog.h +++ b/dep/include/sockets/StdoutLog.h @@ -4,28 +4,35 @@ **/ /* Copyright (C) 2004-2007 Anders Hedstrom + This library is made available under the terms of the GNU GPL. + If you would like to use this library in a closed-source application, a separate license agreement is available. For information about the closed-source license agreement for the C++ sockets library, please visit http://www.alhem.net/Sockets/license.html and/or email license@alhem.net. + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef _SOCKETS_StdoutLog_H #define _SOCKETS_StdoutLog_H + #include "sockets-config.h" #include "StdLog.h" + #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif @@ -42,5 +49,7 @@ public: #ifdef SOCKETS_NAMESPACE } #endif + #endif // _SOCKETS_StdoutLog_H + diff --git a/dep/include/sockets/StreamSocket.h b/dep/include/sockets/StreamSocket.h index 6abb936b6cd..bcce10ffbc5 100644 --- a/dep/include/sockets/StreamSocket.h +++ b/dep/include/sockets/StreamSocket.h @@ -1,5 +1,6 @@ #ifndef _StreamSocket_H #define _StreamSocket_H + #include "Socket.h" #ifdef SOCKETS_NAMESPACE @@ -13,70 +14,96 @@ class StreamSocket : public Socket public: StreamSocket(ISocketHandler& ); ~StreamSocket(); + /** Socket should Check Connect on next write event from select(). */ void SetConnecting(bool = true); + /** Check connecting flag. \return true if the socket is still trying to connect */ bool Connecting(); + /** Returns true when socket file descriptor is valid, socket connection is established, and socket is not about to be closed. */ bool Ready(); + /** Set timeout to use for connection attempt. \param x Timeout in seconds */ void SetConnectTimeout(int x); + /** Return number of seconds to wait for a connection. \return Connection timeout (seconds) */ int GetConnectTimeout(); + /** Set flush before close to make a tcp socket completely empty its output buffer before closing the connection. */ void SetFlushBeforeClose(bool = true); + /** Check flush before status. \return true if the socket should send all data before closing */ bool GetFlushBeforeClose(); + /** Define number of connection retries (tcp only). n = 0 - no retry n > 0 - number of retries n = -1 - unlimited retries */ void SetConnectionRetry(int n); + /** Get number of maximum connection retries (tcp only). */ int GetConnectionRetry(); + /** Increase number of actual connection retries (tcp only). */ void IncreaseConnectionRetries(); + /** Get number of actual connection retries (tcp only). */ int GetConnectionRetries(); + /** Reset actual connection retries (tcp only). */ void ResetConnectionRetries(); + // LIST_CALLONCONNECT + /** Instruct socket to call OnConnect callback next sockethandler cycle. */ void SetCallOnConnect(bool x = true); + /** Check call on connect flag. \return true if OnConnect() should be called a.s.a.p */ bool CallOnConnect(); + // LIST_RETRY + /** Set flag to initiate a connection attempt after a connection timeout. */ void SetRetryClientConnect(bool x = true); + /** Check if a connection attempt should be made. \return true when another attempt should be made */ bool RetryClientConnect(); + /** Called after OnRead if socket is in line protocol mode. \sa SetLineProtocol */ /** Enable the OnLine callback. Do not create your own OnRead * callback when using this. */ virtual void SetLineProtocol(bool = true); + /** Check line protocol mode. \return true if socket is in line protocol mode */ bool LineProtocol(); + /** Set shutdown status. */ void SetShutdown(int); + /** Get shutdown status. */ int GetShutdown(); + /** Returns IPPROTO_TCP or IPPROTO_SCTP */ virtual int Protocol() = 0; + protected: StreamSocket(const StreamSocket& ) {} // copy constructor + private: StreamSocket& operator=(const StreamSocket& ) { return *this; } // assignment operator + bool m_bConnecting; ///< Flag indicating connection in progress int m_connect_timeout; ///< Connection timeout (seconds) bool m_flush_before_close; ///< Send all data before closing (default true) @@ -94,3 +121,4 @@ private: #endif // _StreamSocket_H + diff --git a/dep/include/sockets/TcpSocket.h b/dep/include/sockets/TcpSocket.h index 34561f59cdc..de1be8bd8b9 100644 --- a/dep/include/sockets/TcpSocket.h +++ b/dep/include/sockets/TcpSocket.h @@ -4,20 +4,25 @@ **/ /* Copyright (C) 2004-2007 Anders Hedstrom + This library is made available under the terms of the GNU GPL. + If you would like to use this library in a closed-source application, a separate license agreement is available. For information about the closed-source license agreement for the C++ sockets library, please visit http://www.alhem.net/Sockets/license.html and/or email license@alhem.net. + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. @@ -30,13 +35,16 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include <openssl/ssl.h> #include "SSLInitializer.h" #endif + #include <string.h> + #define TCP_BUFSIZE_READ 16400 #define TCP_OUTPUT_CAPACITY 1024000 #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif + class SocketAddress; /** Socket implementation for TCP. @@ -52,6 +60,7 @@ protected: public: CircularBuffer(size_t size); ~CircularBuffer(); + /** append l bytes from p to buffer */ bool Write(const char *p,size_t l); /** copy l bytes from buffer to dest */ @@ -62,6 +71,7 @@ protected: bool Remove(size_t l); /** read l bytes from buffer, returns as string. */ std::string ReadString(size_t l); + /** total buffer length */ size_t GetLength(); /** pointer to circular buffer beginning */ @@ -70,8 +80,10 @@ protected: size_t GetL(); /** return free space in buffer, number of bytes until buffer overrun */ size_t Space(); + /** return total number of bytes written to this buffer, ever */ unsigned long ByteCounter(bool clear = false); + private: CircularBuffer(const CircularBuffer& /*s*/) {} CircularBuffer& operator=(const CircularBuffer& ) { return *this; } @@ -114,6 +126,7 @@ protected: char _buf[TCP_OUTPUT_CAPACITY]; }; typedef std::list<OUTPUT *> output_l; + public: /** Constructor with standard values on input/output buffers. */ TcpSocket(ISocketHandler& ); @@ -123,6 +136,7 @@ public: \param osize Output buffer size */ TcpSocket(ISocketHandler& h,size_t isize,size_t osize); ~TcpSocket(); + /** Open a connection to a remote server. If you want your socket to connect to a server, always call Open before Add'ing a socket to the sockethandler. @@ -147,15 +161,18 @@ public: \param host Hostname \param port Port number */ bool Open(const std::string &host,port_t port); + /** Connect timeout callback. */ void OnConnectTimeout(); #ifdef _WIN32 /** Connection failed reported as exception on win32 */ void OnException(); #endif + /** Close file descriptor - internal use only. \sa SetCloseAndDelete */ int Close(); + /** Send a string. \param s String to send \param f Dummy flags -- not used */ @@ -171,6 +188,7 @@ public: \param buf Pointer to the data \param len Length of the data */ virtual void OnRawData(const char *buf,size_t len); + /** Called when output buffer has been sent. Note: Will only be called IF the output buffer has been used. Send's that was successful without needing the output buffer @@ -180,6 +198,7 @@ public: size_t GetInputLength(); /** Number of bytes in output buffer. */ size_t GetOutputLength(); + /** Callback fires when a socket in line protocol has read one full line. \param line Line read */ void OnLine(const std::string& line); @@ -187,6 +206,7 @@ public: uint64_t GetBytesReceived(bool clear = false); /** Get counter of number of bytes sent. */ uint64_t GetBytesSent(bool clear = false); + /** Socks4 specific callback. */ void OnSocks4Connect(); /** Socks4 specific callback. */ @@ -194,6 +214,7 @@ public: /** Socks4 specific callback. \return 'need_more' */ bool OnSocks4Read(); + #ifdef ENABLE_RESOLVER /** Callback executed when resolver thread has finished a resolve request. */ void OnResolved(int id,ipaddr_t a,port_t port); @@ -213,6 +234,7 @@ public: the ssl context for an incoming connection. */ virtual void InitSSLServer(); #endif + #ifdef ENABLE_RECONNECT /** Flag that says a broken connection will try to reconnect. */ void SetReconnect(bool = true); @@ -223,17 +245,24 @@ public: /** Socket is reconnecting. */ bool IsReconnect(); #endif + void DisableInputBuffer(bool = true); + void OnOptions(int,int,int,SOCKET); + void SetLineProtocol(bool = true); + // TCP options bool SetTcpNodelay(bool = true); + virtual int Protocol(); + /** Trigger limit for callback OnTransferLimit. */ void SetTransferLimit(size_t sz); /** This callback fires when the output buffer drops below the value set by SetTransferLimit. Default: 0 (disabled). */ virtual void OnTransferLimit(); + protected: TcpSocket(const TcpSocket& ); void OnRead(); @@ -265,13 +294,17 @@ static int SSL_password_cb(char *buf,int num,int rwflag,void *userdata); /** SSL; Get ssl password. */ const std::string& GetPassword(); #endif + CircularBuffer ibuf; ///< Circular input buffer + private: TcpSocket& operator=(const TcpSocket& ) { return *this; } + /** the actual send() */ int TryWrite(const char *buf, size_t len); /** add data to output buffer top */ void Buffer(const char *buf, size_t len); + // bool m_b_input_buffer_disabled; uint64_t m_bytes_sent; @@ -286,6 +319,7 @@ private: OUTPUT *m_obuf_top; ///< output buffer on top size_t m_transfer_limit; size_t m_output_length; + #ifdef HAVE_OPENSSL static SSLInitializer m_ssl_init; SSL_CTX *m_ssl_ctx; ///< ssl context @@ -293,6 +327,7 @@ static SSLInitializer m_ssl_init; BIO *m_sbio; ///< ssl bio std::string m_password; ///< ssl password #endif + #ifdef ENABLE_SOCKS4 int m_socks4_state; ///< socks4 support char m_socks4_vn; ///< socks4 support, temporary variable @@ -300,17 +335,22 @@ static SSLInitializer m_ssl_init; unsigned short m_socks4_dstport; ///< socks4 support unsigned long m_socks4_dstip; ///< socks4 support #endif + #ifdef ENABLE_RESOLVER int m_resolver_id; ///< Resolver id (if any) for current Open call #endif + #ifdef ENABLE_RECONNECT bool m_b_reconnect; ///< Reconnect on lost connection flag bool m_b_is_reconnect; ///< Trying to reconnect #endif + }; #ifdef SOCKETS_NAMESPACE } #endif + #endif // _SOCKETS_TcpSocket_H + diff --git a/dep/include/sockets/Thread.h b/dep/include/sockets/Thread.h index 4c649ed14d5..efb766e9ee6 100644 --- a/dep/include/sockets/Thread.h +++ b/dep/include/sockets/Thread.h @@ -4,30 +4,37 @@ **/ /* Copyright (C) 2004-2007 Anders Hedstrom + This library is made available under the terms of the GNU GPL. + If you would like to use this library in a closed-source application, a separate license agreement is available. For information about the closed-source license agreement for the C++ sockets library, please visit http://www.alhem.net/Sockets/license.html and/or email license@alhem.net. + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef _SOCKETS_Thread_H #define _SOCKETS_Thread_H + #include "sockets-config.h" #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif + #ifdef _WIN32 // to be //typedef DWORD threadfunc_t; @@ -38,10 +45,12 @@ typedef void * threadparam_t; #define STDPREFIX __stdcall #else #include <pthread.h> + typedef void * threadfunc_t; typedef void * threadparam_t; #define STDPREFIX #endif + /** \defgroup threading Threading */ /** Thread base class. The Thread class is used by the resolver (ResolvServer) and running a detached socket (SocketThread). @@ -54,8 +63,11 @@ class Thread public: Thread(bool release = true); virtual ~Thread(); + static threadfunc_t STDPREFIX StartThread(threadparam_t); + virtual void Run() = 0; + bool IsRunning(); void SetRunning(bool x); bool IsReleased(); @@ -63,6 +75,7 @@ public: bool DeleteOnExit(); void SetDeleteOnExit(bool x = true); bool IsDestructor(); + private: Thread(const Thread& ) {} Thread& operator=(const Thread& ) { return *this; } @@ -81,5 +94,7 @@ private: #ifdef SOCKETS_NAMESPACE } #endif + #endif // _SOCKETS_Thread_H + diff --git a/dep/include/sockets/UdpSocket.h b/dep/include/sockets/UdpSocket.h index 010a7b23143..3b06c6955bd 100644 --- a/dep/include/sockets/UdpSocket.h +++ b/dep/include/sockets/UdpSocket.h @@ -4,31 +4,39 @@ **/ /* Copyright (C) 2004-2007 Anders Hedstrom + This library is made available under the terms of the GNU GPL. + If you would like to use this library in a closed-source application, a separate license agreement is available. For information about the closed-source license agreement for the C++ sockets library, please visit http://www.alhem.net/Sockets/license.html and/or email license@alhem.net. + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef _SOCKETS_UdpSocket_H #define _SOCKETS_UdpSocket_H + #include "sockets-config.h" #include "Socket.h" + #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif + /** Socket implementation for UDP. \ingroup basic */ class UdpSocket : public Socket @@ -40,12 +48,14 @@ public: \param ipv6 'true' if this is an ipv6 socket */ UdpSocket(ISocketHandler& h,int ibufsz = 16384,bool ipv6 = false, int retries = 0); ~UdpSocket(); + /** Called when incoming data has been received. \param buf Pointer to data \param len Length of data \param sa Pointer to sockaddr struct of sender \param sa_len Length of sockaddr struct */ virtual void OnRawData(const char *buf,size_t len,struct sockaddr *sa,socklen_t sa_len); + /** Called when incoming data has been received and read timestamp is enabled. \param buf Pointer to data \param len Length of data @@ -53,6 +63,7 @@ public: \param sa_len Length of sockaddr struct \param ts Timestamp from message */ virtual void OnRawData(const char *buf,size_t len,struct sockaddr *sa,socklen_t sa_len,struct timeval *ts); + /** To receive incoming data, call Bind to setup an incoming port. \param port Incoming port number \param range Port range to try if ports already in use @@ -85,6 +96,7 @@ public: \param range Port range \return 0 if bind succeeded */ int Bind(SocketAddress& ad,int range = 1); + /** Define remote host. \param l Address of remote host \param port Port of remote host @@ -108,6 +120,7 @@ public: \param ad Socket address \return true if successful */ bool Open(SocketAddress& ad); + /** Send to specified host */ void SendToBuf(const std::string& ,port_t,const char *data,int len,int flags = 0); /** Send to specified address */ @@ -120,6 +133,7 @@ public: #endif /** Send to specified socket address */ void SendToBuf(SocketAddress& ad,const char *data,int len,int flags = 0); + /** Send string to specified host */ void SendTo(const std::string&,port_t,const std::string&,int flags = 0); /** Send string to specified address */ @@ -132,15 +146,18 @@ public: #endif /** Send string to specified socket address */ void SendTo(SocketAddress& ad,const std::string&,int flags = 0); + /** Send to connected address */ void SendBuf(const char *data,size_t,int flags = 0); /** Send string to connected address. */ void Send(const std::string& ,int flags = 0); + /** Set broadcast */ void SetBroadcast(bool b = true); /** Check broadcast flag. \return true broadcast is enabled. */ bool IsBroadcast(); + /** multicast */ void SetMulticastTTL(int ttl = 1); int GetMulticastTTL(); @@ -160,10 +177,14 @@ public: bool IsBound(); /** Return Bind port number */ port_t GetPort(); + void OnOptions(int,int,int,SOCKET) {} + int GetLastSizeWritten(); + /** Also read timestamp information from incoming message */ void SetTimestamp(bool = true); + protected: UdpSocket(const UdpSocket& s) : Socket(s) {} void OnRead(); @@ -171,6 +192,7 @@ protected: /** This method emulates socket recvfrom, but uses messages so we can get the timestamp */ int ReadTS(char *ioBuf, int inBufSize, struct sockaddr *from, socklen_t fromlen, struct timeval *ts); #endif + private: UdpSocket& operator=(const UdpSocket& ) { return *this; } /** create before using sendto methods */ @@ -187,5 +209,7 @@ private: #ifdef SOCKETS_NAMESPACE } #endif + #endif // _SOCKETS_UdpSocket_H + diff --git a/dep/include/sockets/Utility.h b/dep/include/sockets/Utility.h index 8f1c15ab260..724a94e4b32 100644 --- a/dep/include/sockets/Utility.h +++ b/dep/include/sockets/Utility.h @@ -4,26 +4,32 @@ **/ /* Copyright (C) 2004-2007 Anders Hedstrom + This library is made available under the terms of the GNU GPL. + If you would like to use this library in a closed-source application, a separate license agreement is available. For information about the closed-source license agreement for the C++ sockets library, please visit http://www.alhem.net/Sockets/license.html and/or email license@alhem.net. + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef _SOCKETS_Utility_H #define _SOCKETS_Utility_H + #include "sockets-config.h" #include <ctype.h> #include <string.h> @@ -31,11 +37,15 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "socket_include.h" #include <map> #include <string> + #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif + #define TWIST_LEN 624 + class SocketAddress; + /** Conversion utilities. \ingroup util */ class Utility @@ -47,7 +57,9 @@ class Utility class Rng { public: Rng(unsigned long seed); + unsigned long Get(); + private: int m_value; unsigned long m_tmp[TWIST_LEN]; @@ -72,13 +84,16 @@ public: static unsigned int hex2unsigned(const std::string& str); static std::string rfc1738_encode(const std::string& src); static std::string rfc1738_decode(const std::string& src); + /** Checks whether a string is a valid ipv4/ipv6 ip number. */ static bool isipv4(const std::string&); /** Checks whether a string is a valid ipv4/ipv6 ip number. */ static bool isipv6(const std::string&); + /** Hostname to ip resolution ipv4, not asynchronous. */ static bool u2ip(const std::string&, ipaddr_t&); static bool u2ip(const std::string&, struct sockaddr_in& sa, int ai_flags = 0); + #ifdef ENABLE_IPV6 #ifdef IPPROTO_IPV6 /** Hostname to ip resolution ipv6, not asynchronous. */ @@ -86,10 +101,13 @@ public: static bool u2ip(const std::string&, struct sockaddr_in6& sa, int ai_flags = 0); #endif #endif + /** Reverse lookup of address to hostname */ static bool reverse(struct sockaddr *sa, socklen_t sa_len, std::string&, int flags = 0); static bool reverse(struct sockaddr *sa, socklen_t sa_len, std::string& hostname, std::string& service, int flags = 0); + static bool u2service(const std::string& name, int& service, int ai_flags = 0); + /** Convert binary ip address to string: ipv4. */ static void l2ip(const ipaddr_t,std::string& ); static void l2ip(const in_addr&,std::string& ); @@ -97,6 +115,7 @@ public: #ifdef IPPROTO_IPV6 /** Convert binary ip address to string: ipv6. */ static void l2ip(const struct in6_addr&,std::string& ,bool mixed = false); + /** ipv6 address compare. */ static int in6_addr_compare(in6_addr,in6_addr); #endif @@ -129,15 +148,22 @@ public: /** Convert sockaddr struct to human readable string. \param sa Ptr to sockaddr struct */ static std::string Sa2String(struct sockaddr *sa); + /** Get current time in sec/microseconds. */ static void GetTime(struct timeval *); + static std::auto_ptr<SocketAddress> CreateAddress(struct sockaddr *,socklen_t); + static unsigned long ThreadID(); + static std::string ToLower(const std::string& str); static std::string ToUpper(const std::string& str); + static std::string ToString(double d); + /** Returns a random 32-bit integer */ static unsigned long Rnd(); + private: static std::string m_host; ///< local hostname static ipaddr_t m_ip; ///< local ip address @@ -154,5 +180,7 @@ private: #ifdef SOCKETS_NAMESPACE } #endif + #endif // _SOCKETS_Utility_H + diff --git a/dep/include/sockets/socket_include.h b/dep/include/sockets/socket_include.h index e5a216432b5..89855a54108 100644 --- a/dep/include/sockets/socket_include.h +++ b/dep/include/sockets/socket_include.h @@ -4,20 +4,25 @@ **/ /* Copyright (C) 2004-2007 Anders Hedstrom + This library is made available under the terms of the GNU GPL. + If you would like to use this library in a closed-source application, a separate license agreement is available. For information about the closed-source license agreement for the C++ sockets library, please visit http://www.alhem.net/Sockets/license.html and/or email license@alhem.net. + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. @@ -25,10 +30,13 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifndef _SOCKETS_socket_include_H #define _SOCKETS_socket_include_H #include "sockets-config.h" + #ifdef _MSC_VER #pragma warning(disable:4514) #endif + // common defines affecting library and applications using library + /* Define SOCKETS_DYNAMIC_TEMP to use dynamically allocated buffers in read operations - helps on ECOS */ #define SOCKETS_DYNAMIC_TEMP @@ -38,6 +46,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include <sys/param.h> #endif #include <list> + // int64 #ifdef _WIN32 typedef unsigned __int64 uint64_t; @@ -49,6 +58,7 @@ typedef unsigned __int64 uint64_t; # include <stdint.h> #endif #endif + #ifndef _WIN32 // ---------------------------------------- // common unix includes / defines @@ -59,11 +69,13 @@ typedef unsigned __int64 uint64_t; #include <netinet/in.h> #include <arpa/inet.h> //#include <netdb.h> + // all typedefs in this file will be declared outside the sockets namespace, // because some os's will already have one or more of the type defined. typedef int SOCKET; #define Errno errno #define StrError strerror + #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif @@ -72,12 +84,15 @@ namespace SOCKETS_NAMESPACE { #define closesocket close #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 + #ifndef INADDR_NONE #define INADDR_NONE ((unsigned long) -1) #endif // INADDR_NONE + #ifdef SOCKETS_NAMESPACE } #endif + #endif // !_WIN32 // ---------------------------------------- @@ -88,6 +103,7 @@ namespace SOCKETS_NAMESPACE { // ---------------------------------------- // OS specific adaptions + #ifdef SOLARIS // ---------------------------------------- // Solaris @@ -96,11 +112,14 @@ typedef unsigned short port_t; namespace SOCKETS_NAMESPACE { #endif // no defs + #ifdef SOCKETS_NAMESPACE } #endif + #define s6_addr16 _S6_un._S6_u8 #define MSG_NOSIGNAL 0 + #elif defined __FreeBSD__ // ---------------------------------------- // FreeBSD @@ -116,14 +135,17 @@ typedef in_port_t port_t; namespace SOCKETS_NAMESPACE { #endif // no defs + #ifdef SOCKETS_NAMESPACE } #endif + # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP # define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP # else # error FreeBSD versions prior to 400014 does not support ipv6 # endif + #elif defined (__NetBSD__) || defined (__OpenBSD__) # if !defined(MSG_NOSIGNAL) # define MSG_NOSIGNAL 0 @@ -145,13 +167,16 @@ typedef unsigned long ipaddr_t; namespace SOCKETS_NAMESPACE { #endif // no defs + #ifdef SOCKETS_NAMESPACE } #endif + #define s6_addr16 __u6_addr.__u6_addr16 #define MSG_NOSIGNAL 0 // oops - thanks Derek #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP + #elif defined _WIN32 // ---------------------------------------- // Win32 @@ -159,6 +184,7 @@ namespace SOCKETS_NAMESPACE { #pragma comment(lib, "wsock32.lib") #endif #define strcasecmp _stricmp + typedef unsigned long ipaddr_t; typedef unsigned short port_t; typedef int socklen_t; @@ -166,12 +192,15 @@ typedef int socklen_t; namespace SOCKETS_NAMESPACE { #endif // no defs + #ifdef SOCKETS_NAMESPACE } #endif + // 1.8.6: define FD_SETSIZE to something bigger than 64 if there are a lot of // simultaneous connections (must be done before including winsock.h) #define FD_SETSIZE 1024 + // windows 2000 with ipv6 preview installed: // http://msdn.microsoft.com/downloads/sdks/platform/tpipv6.asp // see the FAQ on how to install @@ -189,8 +218,10 @@ namespace SOCKETS_NAMESPACE { #define MSG_NOSIGNAL 0 //#define SHUT_RDWR 2 #define SHUT_WR 1 + #define Errno WSAGetLastError() const char *StrError(int x); + #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif @@ -213,9 +244,11 @@ public: private: WSADATA m_wsadata; }; + #ifdef SOCKETS_NAMESPACE } #endif + #else // ---------------------------------------- // LINUX @@ -225,11 +258,13 @@ typedef unsigned short port_t; namespace SOCKETS_NAMESPACE { #endif // no defs + #ifdef SOCKETS_NAMESPACE } #endif #endif + #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif @@ -252,3 +287,4 @@ namespace SOCKETS_NAMESPACE { #endif // _SOCKETS_socket_include_H + diff --git a/dep/include/sockets/sockets-config.h b/dep/include/sockets/sockets-config.h index f2de433756c..1c8dc439092 100644 --- a/dep/include/sockets/sockets-config.h +++ b/dep/include/sockets/sockets-config.h @@ -5,20 +5,24 @@ **/ /* Copyright (C) 2007 Anders Hedstrom + This program 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; either version 2 of the License, or (at your option) any later version. + 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, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef _SOCKETS_CONFIG_H #define _SOCKETS_CONFIG_H + #ifndef _RUN_DP /* First undefine symbols if already defined. */ #undef HAVE_OPENSSL @@ -33,10 +37,12 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #undef ENABLE_TRIGGERS #undef ENABLE_EXCEPTIONS #endif // _RUN_DP + // define MACOSX for internal socket library checks #if defined(__APPLE__) && defined(__MACH__) && !defined(MACOSX) #define MACOSX #endif + /* OpenSSL support. */ //#define HAVE_OPENSSL @@ -81,3 +87,4 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #endif // _SOCKETS_CONFIG_H + |
