mirror of
https://github.com/anope/anope.git
synced 2026-07-10 13:03:12 +02:00
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.
This commit is contained in:
@@ -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.
|
||||
|
||||
+1
-1
@@ -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();
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
+18
-34
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+8
-8
@@ -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;
|
||||
}
|
||||
|
||||
+2
-2
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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())
|
||||
|
||||
+1
-1
@@ -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))
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
+33
-47
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-11
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user