summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdam <Adam@anope.org>2010-09-17 19:20:07 -0400
committerAdam <Adam@anope.org>2010-09-17 19:20:07 -0400
commit227909e2cf0039737dc52872651837fe5f1702b5 (patch)
treeb8a7072c7d938ea66d9710a48d717b3eb76343de /src
parentf71fb6e8133da955a58b1cca00013ce20c0b65cc (diff)
Rejig of some of the socket stuff. Fixed marking sockets as nonblocking on Windows. Added in a LastError function to keep having to use strerror/GetLastError everywhere.
Diffstat (limited to 'src')
-rw-r--r--src/config.cpp2
-rw-r--r--src/dns.cpp16
-rw-r--r--src/init.cpp4
-rw-r--r--src/misc.cpp15
-rw-r--r--src/modulemanager.cpp13
-rw-r--r--src/modules.cpp2
-rw-r--r--src/socketengines/socketengine_eventfd.cpp2
-rw-r--r--src/socketengines/socketengine_pipe.cpp2
-rw-r--r--src/sockets.cpp80
-rw-r--r--src/threadengines/threadengine_pthread.cpp2
-rw-r--r--src/threadengines/threadengine_win32.cpp2
-rw-r--r--src/win32/windows.cpp12
12 files changed, 72 insertions, 80 deletions
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