diff options
-rw-r--r-- | include/anope.h | 5 | ||||
-rw-r--r-- | include/dns.h | 2 | ||||
-rw-r--r-- | include/modules.h | 2 | ||||
-rw-r--r-- | include/services.h | 1 | ||||
-rw-r--r-- | include/sockets.h | 52 | ||||
-rw-r--r-- | modules/socketengines/m_socketengine_epoll.cpp | 14 | ||||
-rw-r--r-- | modules/socketengines/m_socketengine_select.cpp | 5 | ||||
-rw-r--r-- | src/config.cpp | 2 | ||||
-rw-r--r-- | src/dns.cpp | 16 | ||||
-rw-r--r-- | src/init.cpp | 4 | ||||
-rw-r--r-- | src/misc.cpp | 15 | ||||
-rw-r--r-- | src/modulemanager.cpp | 13 | ||||
-rw-r--r-- | src/modules.cpp | 2 | ||||
-rw-r--r-- | src/socketengines/socketengine_eventfd.cpp | 2 | ||||
-rw-r--r-- | src/socketengines/socketengine_pipe.cpp | 2 | ||||
-rw-r--r-- | src/sockets.cpp | 80 | ||||
-rw-r--r-- | src/threadengines/threadengine_pthread.cpp | 2 | ||||
-rw-r--r-- | src/threadengines/threadengine_win32.cpp | 2 | ||||
-rw-r--r-- | src/win32/windows.cpp | 12 |
19 files changed, 106 insertions, 127 deletions
diff --git a/include/anope.h b/include/anope.h index a63abb9d1..cbc87bf7b 100644 --- a/include/anope.h +++ b/include/anope.h @@ -337,6 +337,11 @@ namespace Anope */ extern CoreExport void Unhex(const Anope::string &src, Anope::string &dest); extern CoreExport void Unhex(const Anope::string &src, char *dest); + + /** Return the last error, uses errno/GetLastError() to determin this + * @return An error message + */ + extern CoreExport const Anope::string LastError(); } /** sepstream allows for splitting token seperated lists. diff --git a/include/dns.h b/include/dns.h index 51960b0f4..4f61ef909 100644 --- a/include/dns.h +++ b/include/dns.h @@ -134,7 +134,7 @@ class DNSSocket : public ClientSocket 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 nPort); + DNSSocket(const Anope::string &nTargetHost, int Port); virtual ~DNSSocket(); bool ProcessRead(); diff --git a/include/modules.h b/include/modules.h index ac76a4534..5748d4c10 100644 --- a/include/modules.h +++ b/include/modules.h @@ -27,6 +27,7 @@ # define dlsym(file, symbol) (HMODULE)GetProcAddress(file, symbol) # define dlclose(file) FreeLibrary(file) ? 0 : 1 # define ano_modclearerr() SetLastError(0) +# define ano_moderr() LastError().c_str() #else typedef void * ano_module_t; @@ -36,6 +37,7 @@ * to be cleared, POSIX-wise. -GD */ # define ano_modclearerr() dlerror() +# define ano_moderr() dlerror() #endif /** Possible return types from events. diff --git a/include/services.h b/include/services.h index 36e2f8e90..9fd9b2ce3 100644 --- a/include/services.h +++ b/include/services.h @@ -80,7 +80,6 @@ # define MARK_DEPRECATED # define EINPROGRESS WSAEWOULDBLOCK -extern CoreExport const char *dlerror(); extern CoreExport int inet_pton(int af, const char *src, void *dst); extern CoreExport const char *inet_ntop(int af, const void *src, char *dst, size_t size); #endif diff --git a/include/sockets.h b/include/sockets.h index 6caeea19c..38060ed78 100644 --- a/include/sockets.h +++ b/include/sockets.h @@ -105,10 +105,10 @@ enum SocketFlag class CoreExport Socket : public Flags<SocketFlag, 2> { protected: - /** Really recieve something from the buffer + /** Really receive something from the buffer * @param buf The buf to read to * @param sz How much to read - * @return Number of bytes recieved + * @return Number of bytes received */ virtual int RecvInternal(char *buf, size_t sz) const; @@ -120,13 +120,11 @@ class CoreExport Socket : public Flags<SocketFlag, 2> /* Socket FD */ int Sock; - /* Port we're connected to */ - int Port; /* 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 recieved */ + /* 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; @@ -180,7 +178,7 @@ class CoreExport Socket : public Flags<SocketFlag, 2> */ size_t WriteBufferLen() const; - /** Called when there is something to be recieved for this socket + /** Called when there is something to be received for this socket * @return true on success, false to drop this socket */ virtual bool ProcessRead(); @@ -195,7 +193,7 @@ class CoreExport Socket : public Flags<SocketFlag, 2> */ virtual void ProcessError(); - /** Called with a line recieved from the socket + /** Called with a line received from the socket * @param buf The line * @return true to continue reading, false to drop the socket */ @@ -248,29 +246,27 @@ class CoreExport Pipe : public Socket class CoreExport ClientSocket : public Socket { protected: - /* Target host we're connected to */ - Anope::string TargetHost; - /* Target port we're connected to */ - int Port; - /* The host to bind to */ - Anope::string BindHost; + /* Sockaddrs for bindip (if there is one) */ + sockaddrs bindaddrs; + /* Sockaddrs for connection ip/port */ + sockaddrs conaddrs; public: /** Constructor - * @param nTargetHost The target host to connect to - * @param nPort The target port to connect to - * @param nBindHost The host to bind to for connecting + * @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 &nTargetHost, int nPort, const Anope::string &nBindHost = "", bool nIPv6 = false, int type = SOCK_STREAM); + ClientSocket(const Anope::string &TargetHost, int Port, const Anope::string &BindHost = "", bool nIPv6 = false, int type = SOCK_STREAM); /** Default destructor */ virtual ~ClientSocket(); - /** Called with a line recieved from the socket + /** Called with a line received from the socket * @param buf The line * @return true to continue reading, false to drop the socket */ @@ -280,17 +276,15 @@ class CoreExport ClientSocket : public Socket class CoreExport ListenSocket : public Socket { protected: - /* Bind IP */ - Anope::string BindIP; - /* Port to bind to */ - int Port; + /* Sockaddrs for bindip/port */ + sockaddrs listenaddrs; public: /** Constructor - * @param bind The IP to bind to + * @param bindip The IP to bind to * @param port The port to listen on */ - ListenSocket(const Anope::string &bind, int port); + ListenSocket(const Anope::string &bindip, int port); /** Destructor */ @@ -306,16 +300,6 @@ class CoreExport ListenSocket : public Socket * @return true if the listen socket should remain alive */ virtual bool OnAccept(Socket *s); - - /** Get the bind IP for this socket - * @return the bind ip - */ - const Anope::string &GetBindIP() const; - - /** Get the port this socket is bound to - * @return The port - */ - int GetPort() const; }; #endif // SOCKET_H diff --git a/modules/socketengines/m_socketengine_epoll.cpp b/modules/socketengines/m_socketengine_epoll.cpp index 01908125d..7b8dbdde5 100644 --- a/modules/socketengines/m_socketengine_epoll.cpp +++ b/modules/socketengines/m_socketengine_epoll.cpp @@ -28,8 +28,8 @@ class SocketEngineEPoll : public SocketEngineBase if (EngineHandle == -1) { - Log() << "Could not initialize epoll socket engine: " << strerror(errno); - throw ModuleException(Anope::string("Could not initialize epoll socket engine: ") + strerror(errno)); + Log() << "Could not initialize epoll socket engine: " << Anope::LastError(); + throw ModuleException(Anope::string("Could not initialize epoll socket engine: ") + Anope::LastError()); } events = new epoll_event[max]; @@ -52,7 +52,7 @@ class SocketEngineEPoll : public SocketEngineBase if (epoll_ctl(EngineHandle, EPOLL_CTL_ADD, ev.data.fd, &ev) == -1) { - Log() << "Unable to add fd " << ev.data.fd << " to socketengine epoll: " << strerror(errno); + Log() << "Unable to add fd " << ev.data.fd << " to socketengine epoll: " << Anope::LastError(); return; } @@ -71,7 +71,7 @@ class SocketEngineEPoll : public SocketEngineBase if (epoll_ctl(EngineHandle, EPOLL_CTL_DEL, ev.data.fd, &ev) == -1) { - Log() << "Unable to delete fd " << ev.data.fd << " from socketengine epoll: " << strerror(errno); + Log() << "Unable to delete fd " << ev.data.fd << " from socketengine epoll: " << Anope::LastError(); return; } @@ -93,7 +93,7 @@ class SocketEngineEPoll : public SocketEngineBase ev.data.fd = s->GetSock(); if (epoll_ctl(EngineHandle, EPOLL_CTL_MOD, ev.data.fd, &ev) == -1) - Log() << "Unable to mark fd " << ev.data.fd << " as writable in socketengine epoll: " << strerror(errno); + Log() << "Unable to mark fd " << ev.data.fd << " as writable in socketengine epoll: " << Anope::LastError(); else s->SetFlag(SF_WRITABLE); } @@ -111,7 +111,7 @@ class SocketEngineEPoll : public SocketEngineBase ev.data.fd = s->GetSock(); if (epoll_ctl(EngineHandle, EPOLL_CTL_MOD, ev.data.fd, &ev) == -1) - Log() << "Unable to mark fd " << ev.data.fd << " as unwritable in socketengine epoll: " << strerror(errno); + Log() << "Unable to mark fd " << ev.data.fd << " as unwritable in socketengine epoll: " << Anope::LastError(); else s->UnsetFlag(SF_WRITABLE); } @@ -123,7 +123,7 @@ class SocketEngineEPoll : public SocketEngineBase if (total == -1) { - Log() << "SockEngine::Process(): error: " << strerror(errno); + Log() << "SockEngine::Process(): error: " << Anope::LastError(); return; } diff --git a/modules/socketengines/m_socketengine_select.cpp b/modules/socketengines/m_socketengine_select.cpp index 13791d926..733547167 100644 --- a/modules/socketengines/m_socketengine_select.cpp +++ b/modules/socketengines/m_socketengine_select.cpp @@ -69,10 +69,7 @@ class SocketEngineSelect : public SocketEngineBase if (sresult == -1) { -#ifdef WIN32 - errno = WSAGetLastError(); -#endif - Log() << "SockEngine::Process(): error: " << strerror(errno); + Log() << "SockEngine::Process(): error: " << Anope::LastError(); } else if (sresult) { diff --git a/src/config.cpp b/src/config.cpp index ac1f00295..69dc27f9b 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -404,7 +404,7 @@ void ServerConfig::ValidateNoSpaces(const Anope::string &p, const Anope::string } /* NOTE: Before anyone asks why we're not using inet_pton for this, it is because inet_pton and friends do not return so much detail, - * even in strerror(errno). They just return 'yes' or 'no' to an address without such detail as to whats WRONG with the address. + * even in LastError(). They just return 'yes' or 'no' to an address without such detail as to whats WRONG with the address. * Because ircd users arent as technical as they used to be (;)) we are going to give more of a useful error message. */ void ServerConfig::ValidateIP(const Anope::string &p, const Anope::string &tag, const Anope::string &val, bool wild) const diff --git a/src/dns.cpp b/src/dns.cpp index c613dc776..509bd877f 100644 --- a/src/dns.cpp +++ b/src/dns.cpp @@ -77,7 +77,7 @@ bool DNSPacket::AddQuestion(const Anope::string &name, const QueryType qt) in6_addr ip; if (!inet_pton(AF_INET6, working_name.c_str(), &ip)) { - Log(LOG_DEBUG_2) << "Resolver: Received an invalid IP for PTR lookup (" << working_name << "): " << strerror(errno); + Log(LOG_DEBUG_2) << "Resolver: Received an invalid IP for PTR lookup (" << working_name << "): " << Anope::LastError(); return false; } @@ -102,14 +102,14 @@ bool DNSPacket::AddQuestion(const Anope::string &name, const QueryType qt) in_addr ip; if (!inet_pton(AF_INET, working_name.c_str(), &ip)) { - Log(LOG_DEBUG_2) << "Resolver: Received an invalid IP for PTR lookup (" << working_name << "): " << strerror(errno); + Log(LOG_DEBUG_2) << "Resolver: Received an invalid IP for PTR lookup (" << working_name << "): " << Anope::LastError(); return false; } unsigned long reverse_ip = ((ip.s_addr & 0xFF) << 24) | ((ip.s_addr & 0xFF00) << 8) | ((ip.s_addr & 0xFF0000) >> 8) | ((ip.s_addr & 0xFF000000) >> 24); char ipbuf[INET_ADDRSTRLEN]; if (!inet_ntop(AF_INET, &reverse_ip, ipbuf, sizeof(ipbuf))) { - Log(LOG_DEBUG_2) << "Resolver: Reformatted IP " << working_name << " back into an invalid IP: " << strerror(errno); + Log(LOG_DEBUG_2) << "Resolver: Reformatted IP " << working_name << " back into an invalid IP: " << Anope::LastError(); return false; } @@ -197,7 +197,7 @@ inline DNSRecord::DNSRecord() this->created = Anope::CurTime; } -DNSSocket::DNSSocket(const Anope::string &nTargetHost, int nPort) : ClientSocket(nTargetHost, nPort, "", false, SOCK_DGRAM) +DNSSocket::DNSSocket(const Anope::string &TargetHost, int Port) : ClientSocket(TargetHost, Port, "", false, SOCK_DGRAM) { this->server_addr.pton(AF_INET, TargetHost, Port); } @@ -370,8 +370,8 @@ bool DNSSocket::ProcessRead() char ipbuf[INET_ADDRSTRLEN]; if (!inet_ntop(AF_INET, &ip, ipbuf, sizeof(ipbuf))) { - Log(LOG_DEBUG_2) << "Resolver: Received an invalid IP for DNS_QUERY_A: " << strerror(errno); - request->OnError(DNS_ERROR_FORMAT_ERROR, "Received an invalid IP from the nameserver: " + stringify(strerror(errno))); + Log(LOG_DEBUG_2) << "Resolver: Received an invalid IP for DNS_QUERY_A: " << Anope::LastError(); + request->OnError(DNS_ERROR_FORMAT_ERROR, "Received an invalid IP from the nameserver: " + Anope::LastError()); delete rr; rr = NULL; } @@ -389,8 +389,8 @@ bool DNSSocket::ProcessRead() char ipbuf[INET6_ADDRSTRLEN]; if (!inet_ntop(AF_INET6, &ip, ipbuf, sizeof(ipbuf))) { - Log(LOG_DEBUG_2) << "Resolver: Received an invalid IP for DNS_QUERY_A: " << strerror(errno); - request->OnError(DNS_ERROR_FORMAT_ERROR, "Received an invalid IP from the nameserver: " + stringify(strerror(errno))); + Log(LOG_DEBUG_2) << "Resolver: Received an invalid IP for DNS_QUERY_A: " << Anope::LastError(); + request->OnError(DNS_ERROR_FORMAT_ERROR, "Received an invalid IP from the nameserver: " + Anope::LastError()); delete rr; rr = NULL; } diff --git a/src/init.cpp b/src/init.cpp index 1de213a11..d882f34be 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -301,7 +301,7 @@ void Init(int ac, char **av) /* Chdir to Services data directory. */ if (chdir(services_dir.c_str()) < 0) { - throw FatalException("Unable to chdir to " + services_dir + ": " + Anope::string(strerror(errno))); + throw FatalException("Unable to chdir to " + services_dir + ": " + Anope::LastError()); } Log(LOG_TERMINAL) << "Anope " << Anope::Version() << ", " << Anope::Build(); @@ -367,7 +367,7 @@ void Init(int ac, char **av) ModuleManager::LoadModuleList(Config->DBModuleList); /* Load the socket engine */ - if (ModuleManager::LoadModule(Config->SocketEngine, NULL)) + if (ModuleManager::LoadModule(Config->SocketEngine, NULL) || !SocketEngine) throw FatalException("Unable to load socket engine " + Config->SocketEngine); try diff --git a/src/misc.cpp b/src/misc.cpp index 225aac592..aad95e528 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -1305,3 +1305,18 @@ void Anope::Unhex(const Anope::string &src, char *dest) } dest[destpos] = 0; } + +const Anope::string Anope::LastError() +{ +#ifndef _WIN32 + return LastError(); +#else + char errbuf[513]; + DWORD err = GetLastError(); + if (!err) + return NULL; + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, 0, errbuf, 512, NULL); + return errbuf; +#endif +} + diff --git a/src/modulemanager.cpp b/src/modulemanager.cpp index dc1258340..8e4329581 100644 --- a/src/modulemanager.cpp +++ b/src/modulemanager.cpp @@ -134,8 +134,8 @@ int ModuleManager::LoadModule(const Anope::string &modname, User *u) ano_modclearerr(); ano_module_t handle = dlopen(pbuf.c_str(), RTLD_LAZY); - const char *err; - if (!handle && (err = dlerror())) + const char *err = ano_moderr(); + if (!handle && err && *err) { Log() << err; return MOD_ERR_NOLOAD; @@ -143,7 +143,8 @@ int ModuleManager::LoadModule(const Anope::string &modname, User *u) ano_modclearerr(); Module *(*func)(const Anope::string &, const Anope::string &) = function_cast<Module *(*)(const Anope::string &, const Anope::string &)>(dlsym(handle, "AnopeInit")); - if (!func && (err = dlerror())) + err = ano_moderr(); + if (!func && err && *err) { Log() << "No init function found, not an Anope module"; dlclose(handle); @@ -257,8 +258,8 @@ void ModuleManager::DeleteModule(Module *m) ano_modclearerr(); void (*destroy_func)(Module *m) = function_cast<void (*)(Module *)>(dlsym(m->handle, "AnopeFini")); - const char *err; - if (!destroy_func && (err = dlerror())) + const char *err = ano_moderr(); + if (!destroy_func && err && *err) { Log() << "No destroy function found, chancing delete..."; delete m; /* we just have to chance they haven't overwrote the delete operator then... */ @@ -269,7 +270,7 @@ void ModuleManager::DeleteModule(Module *m) if (handle) { if (dlclose(handle)) - Log() << dlerror(); + Log() << ano_moderr(); } if (!filename.empty()) diff --git a/src/modules.cpp b/src/modules.cpp index d2c6d7ac3..ced2702e2 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -360,7 +360,7 @@ void ModuleRunTimeDirCleanUp() { Anope::string filebuf = dirbuf + "/" + FileData.cFileName; if (!DeleteFile(filebuf.c_str())) - Log(LOG_DEBUG) << "Error deleting file " << filebuf << " - GetLastError() reports " << dlerror(); + Log(LOG_DEBUG) << "Error deleting file " << filebuf << " - GetLastError() reports " << LastError(); } if (!FindNextFile(hList, &FileData)) { diff --git a/src/socketengines/socketengine_eventfd.cpp b/src/socketengines/socketengine_eventfd.cpp index 767a6eddb..a0efe5286 100644 --- a/src/socketengines/socketengine_eventfd.cpp +++ b/src/socketengines/socketengine_eventfd.cpp @@ -16,7 +16,7 @@ Pipe::Pipe() : Socket() { this->Sock = eventfd(0, EFD_NONBLOCK); if (this->Sock < 0) - throw CoreException(Anope::string("Could not create pipe: ") + strerror(errno)); + throw CoreException(Anope::string("Could not create pipe: ") + Anope::LastError()); this->IPv6 = false; this->Type = SOCKTYPE_CLIENT; diff --git a/src/socketengines/socketengine_pipe.cpp b/src/socketengines/socketengine_pipe.cpp index 0b319f0b0..b0be664c8 100644 --- a/src/socketengines/socketengine_pipe.cpp +++ b/src/socketengines/socketengine_pipe.cpp @@ -17,7 +17,7 @@ Pipe::Pipe() : Socket() { int fds[2]; if (pipe(fds)) - throw CoreException(Anope::string("Could not create pipe: ") + strerror(errno)); + throw CoreException(Anope::string("Could not create pipe: ") + Anope::LastError()); int flags = fcntl(fds[0], F_GETFL, 0); fcntl(fds[0], F_SETFL, flags | O_NONBLOCK); flags = fcntl(fds[1], F_GETFL, 0); diff --git a/src/sockets.cpp b/src/sockets.cpp index 7d1c2e07c..fe712357a 100644 --- a/src/sockets.cpp +++ b/src/sockets.cpp @@ -1,6 +1,6 @@ #include "services.h" -SocketEngineBase *SocketEngine; +SocketEngineBase *SocketEngine = NULL; int32 TotalRead = 0; int32 TotalWritten = 0; @@ -62,11 +62,11 @@ Anope::string sockaddrs::addr() const { case AF_INET: if (!inet_ntop(AF_INET, &sa4.sin_addr, address, sizeof(address))) - throw SocketException(strerror(errno)); + throw SocketException(Anope::LastError()); return address; case AF_INET6: if (!inet_ntop(AF_INET6, &sa6.sin6_addr, address, sizeof(address))) - throw SocketException(strerror(errno)); + throw SocketException(Anope::LastError()); return address; default: break; @@ -121,13 +121,13 @@ void sockaddrs::pton(int type, const Anope::string &address, int pport) { case AF_INET: if (inet_pton(type, address.c_str(), &sa4.sin_addr) < 1) - throw SocketException(Anope::string("Invalid host: ") + strerror(errno)); + throw SocketException(Anope::string("Invalid host: ") + Anope::LastError()); sa4.sin_family = type; sa4.sin_port = htons(pport); return; case AF_INET6: if (inet_pton(type, address.c_str(), &sa6.sin6_addr) < 1) - throw SocketException(Anope::string("Invalid host: ") + strerror(errno)); + throw SocketException(Anope::string("Invalid host: ") + Anope::LastError()); sa6.sin6_family = type; sa6.sin6_port = htons(pport); return; @@ -162,6 +162,8 @@ void sockaddrs::ntop(int type, const void *src) throw CoreException("Invalid socket type"); } +/** Default constructor + */ SocketEngineBase::SocketEngineBase() { #ifdef _WIN32 @@ -170,6 +172,8 @@ SocketEngineBase::SocketEngineBase() #endif } +/** Default destructor + */ SocketEngineBase::~SocketEngineBase() { for (std::map<int, Socket *>::const_iterator it = this->Sockets.begin(), it_end = this->Sockets.end(); it != it_end; ++it) @@ -180,6 +184,8 @@ SocketEngineBase::~SocketEngineBase() #endif } +/** Empty constructor, used for things such as the pipe socket + */ Socket::Socket() { } @@ -209,10 +215,10 @@ Socket::~Socket() CloseSocket(Sock); } -/** Really recieve something from the buffer +/** Really receive something from the buffer * @param buf The buf to read to * @param sz How much to read - * @return Number of bytes recieved + * @return Number of bytes received */ int Socket::RecvInternal(char *buf, size_t sz) const { @@ -256,7 +262,7 @@ bool Socket::SetBlocking() bool Socket::SetNonBlocking() { #ifdef _WIN32 - unsigned long opt = 0; + unsigned long opt = 1; return !ioctlsocket(this->GetSock(), FIONBIO, &opt); #else int flags = fcntl(this->GetSock(), F_GETFL, 0); @@ -288,7 +294,7 @@ size_t Socket::WriteBufferLen() const return WriteBuffer.length(); } -/** Called when there is something to be recieved for this socket +/** Called when there is something to be received for this socket * @return true on success, false to drop this socket */ bool Socket::ProcessRead() @@ -358,7 +364,7 @@ void Socket::ProcessError() { } -/** Called with a line recieved from the socket +/** Called with a line received from the socket * @param buf The line * @return true to continue reading, false to drop the socket */ @@ -396,28 +402,26 @@ void Socket::Write(const Anope::string &message) } /** Constructor - * @param nLS The listen socket this connection came from - * @param nu The user using this socket - * @param nsock The socket - * @param nIPv6 IPv6 + * @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::ClientSocket(const Anope::string &nTargetHost, int nPort, const Anope::string &nBindHost, bool nIPv6, int type) : Socket(0, nIPv6, type), TargetHost(nTargetHost), Port(nPort), BindHost(nBindHost) +ClientSocket::ClientSocket(const Anope::string &TargetHost, int Port, const Anope::string &BindHost, bool nIPv6, int type) : Socket(0, nIPv6, type) { this->SetNonBlocking(); if (!BindHost.empty()) { - sockaddrs bindaddr; - bindaddr.pton(IPv6 ? AF_INET6 : AF_INET, BindHost, 0); - if (bind(Sock, &bindaddr.sa, bindaddr.size()) == -1) - throw SocketException(Anope::string("Unable to bind to address: ") + strerror(errno)); + this->bindaddrs.pton(IPv6 ? AF_INET6 : AF_INET, BindHost, 0); + if (bind(Sock, &this->bindaddrs.sa, this->bindaddrs.size()) == -1) + throw SocketException(Anope::string("Unable to bind to address: ") + Anope::LastError()); } - sockaddrs conaddr; - conaddr.pton(IPv6 ? AF_INET6 : AF_INET, TargetHost, Port); - if (connect(Sock, &conaddr.sa, conaddr.size()) == -1 && errno != EINPROGRESS) - throw SocketException(Anope::string("Error connecting to server: ") + strerror(errno)); + this->conaddrs.pton(IPv6 ? AF_INET6 : AF_INET, TargetHost, Port); + if (connect(Sock, &this->conaddrs.sa, this->conaddrs.size()) == -1 && errno != EINPROGRESS) + throw SocketException(Anope::string("Error connecting to server: ") + Anope::LastError()); } /** Default destructor @@ -426,7 +430,7 @@ ClientSocket::~ClientSocket() { } -/** Called with a line recieved from the socket +/** Called with a line received from the socket * @param buf The line * @return true to continue reading, false to drop the socket */ @@ -436,25 +440,22 @@ bool ClientSocket::Read(const Anope::string &buf) } /** Constructor - * @param bind The IP to bind to + * @param bindip The IP to bind to * @param port The port to listen on */ ListenSocket::ListenSocket(const Anope::string &bindip, int port) : Socket(0, (bindip.find(':') != Anope::string::npos ? true : false)) { Type = SOCKTYPE_LISTEN; - BindIP = bindip; - Port = port; this->SetNonBlocking(); - sockaddrs sockaddr; - sockaddr.pton(IPv6 ? AF_INET6 : AF_INET, BindIP, Port); + this->listenaddrs.pton(IPv6 ? AF_INET6 : AF_INET, bindip, port); - if (bind(Sock, &sockaddr.sa, sockaddr.size()) == -1) - throw SocketException(Anope::string("Unable to bind to address: ") + strerror(errno)); + if (bind(Sock, &this->listenaddrs.sa, this->listenaddrs.size()) == -1) + throw SocketException(Anope::string("Unable to bind to address: ") + Anope::LastError()); if (listen(Sock, 5) == -1) - throw SocketException(Anope::string("Unable to listen: ") + strerror(errno)); + throw SocketException(Anope::string("Unable to listen: ") + Anope::LastError()); } /** Destructor @@ -488,18 +489,3 @@ bool ListenSocket::OnAccept(Socket *s) return true; } -/** Get the bind IP for this socket - * @return the bind ip - */ -const Anope::string &ListenSocket::GetBindIP() const -{ - return BindIP; -} - -/** Get the port this socket is bound to - * @return The port - */ -int ListenSocket::GetPort() const -{ - return Port; -} diff --git a/src/threadengines/threadengine_pthread.cpp b/src/threadengines/threadengine_pthread.cpp index f63cc066e..fb0b29506 100644 --- a/src/threadengines/threadengine_pthread.cpp +++ b/src/threadengines/threadengine_pthread.cpp @@ -47,7 +47,7 @@ void ThreadEngine::Start(Thread *thread) if (pthread_create(&thread->Handle, &threadengine_attr, entry_point, thread)) { delete thread; - throw CoreException(Anope::string("Unable to create thread: ") + strerror(errno)); + throw CoreException(Anope::string("Unable to create thread: ") + Anope::LastError()); } } diff --git a/src/threadengines/threadengine_win32.cpp b/src/threadengines/threadengine_win32.cpp index a07a75cce..b67f01681 100644 --- a/src/threadengines/threadengine_win32.cpp +++ b/src/threadengines/threadengine_win32.cpp @@ -43,7 +43,7 @@ void ThreadEngine::Start(Thread *thread) if (!thread->Handle) { delete thread; - throw CoreException(Anope::string("Unable to create thread: ") + dlerror()); + throw CoreException(Anope::string("Unable to create thread: ") + LastError()); } } diff --git a/src/win32/windows.cpp b/src/win32/windows.cpp index 9ea3dad09..5f0c6d5e4 100644 --- a/src/win32/windows.cpp +++ b/src/win32/windows.cpp @@ -9,19 +9,9 @@ * Based on the original code of Services by Andy Church. */ -#ifdef WIN32 +#ifdef _WIN32 #include "services.h" -const char *dlerror() -{ - static char errbuf[513]; - DWORD err = GetLastError(); - if (!err) - return NULL; - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, 0, errbuf, 512, NULL); - return errbuf; -} - /** This is inet_pton, but it works on Windows * @param af The protocol type, AF_INET or AF_INET6 * @param src The address |