Throw GAIException() for getaddrinfo errors

Created a new subclass of Exception called GAIException() that will
handle error messages from getaddrinfo() instead of letting Exception()
handle it. GAIException() will make use of gai_strerror() to map the
error code to text. On Windows, gai_strerrorW() must be used if the text
is encoded with UTF-8.
pull/8/head
Alex Tanskanen 5 years ago committed by Lauri Kasanen
parent 209712b18f
commit 03639406dc

@ -151,8 +151,7 @@ TcpSocket::TcpSocket(const char *host, int port)
hints.ai_next = NULL; hints.ai_next = NULL;
if ((result = getaddrinfo(host, NULL, &hints, &ai)) != 0) { if ((result = getaddrinfo(host, NULL, &hints, &ai)) != 0) {
throw Exception("unable to resolve host by name: %s", throw GAIException("unable to resolve host by name", result);
gai_strerror(result));
} }
sock = -1; sock = -1;
@ -603,8 +602,7 @@ void network::createTcpListeners(std::list<SocketListener*> *listeners,
snprintf (service, sizeof (service) - 1, "%d", port); snprintf (service, sizeof (service) - 1, "%d", port);
service[sizeof (service) - 1] = '\0'; service[sizeof (service) - 1] = '\0';
if ((result = getaddrinfo(addr, service, &hints, &ai)) != 0) if ((result = getaddrinfo(addr, service, &hints, &ai)) != 0)
throw rdr::Exception("unable to resolve listening address: %s", throw GAIException("unable to resolve listening address", result);
gai_strerror(result));
try { try {
createTcpListeners(listeners, ai); createTcpListeners(listeners, ai);
@ -913,8 +911,7 @@ TcpFilter::Pattern TcpFilter::parsePattern(const char* p) {
} }
if ((result = getaddrinfo (p, NULL, &hints, &ai)) != 0) { if ((result = getaddrinfo (p, NULL, &hints, &ai)) != 0) {
throw Exception("unable to resolve host by name: %s", throw GAIException("unable to resolve host by name", result);
gai_strerror(result));
} }
memcpy (&pattern.address.u.sa, ai->ai_addr, ai->ai_addrlen); memcpy (&pattern.address.u.sa, ai->ai_addr, ai->ai_addrlen);

@ -31,6 +31,9 @@
#include <tchar.h> #include <tchar.h>
#include <winsock2.h> #include <winsock2.h>
#include <windows.h> #include <windows.h>
#include <ws2tcpip.h>
#else
#include <netdb.h>
#endif #endif
#include <string.h> #include <string.h>
@ -49,6 +52,21 @@ Exception::Exception(const char *format, ...) {
va_end(ap); va_end(ap);
} }
GAIException::GAIException(const char* s, int err)
: Exception("%s", s)
{
strncat(str_, ": ", len-1-strlen(str_));
#ifdef _WIN32
wchar_t currStr[len-strlen(str_)];
wcsncpy(currStr, gai_strerrorW(err), len-1-strlen(str_));
WideCharToMultiByte(CP_UTF8, 0, currStr, -1, str_+strlen(str_),
len-1-strlen(str_), 0, 0);
#else
//FIXME: perhaps print the error number (NNNN)
strncat(str_, gai_strerror(err), len-1-strlen(str_));
#endif
}
SystemException::SystemException(const char* s, int err_) SystemException::SystemException(const char* s, int err_)
: Exception("%s", s), err(err_) : Exception("%s", s), err(err_)
{ {

@ -42,6 +42,11 @@ namespace rdr {
SystemException(const char* s, int err_); SystemException(const char* s, int err_);
}; };
struct GAIException : public Exception {
int err;
GAIException(const char* s, int err_);
};
struct TimedOut : public Exception { struct TimedOut : public Exception {
TimedOut() : Exception("Timed out") {} TimedOut() : Exception("Timed out") {}
}; };

Loading…
Cancel
Save