]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - win/inspircd_win32wrapper.cpp
Change Windows libraries to be dynamically linked
[user/henk/code/inspircd.git] / win / inspircd_win32wrapper.cpp
index 26d1548986b7745399c607f24fe8fb92ec7e0d61..7707dea0c1b0d58799dff4b686d120588711172e 100644 (file)
 #include "inspircd_win32wrapper.h"
 #include "inspircd.h"
 #include "configreader.h"
-#include "colors.h"
 #include <string>
 #include <errno.h>
 #include <assert.h>
-#include <mmsystem.h>
-#pragma comment(lib, "Winmm.lib")
 
 CoreExport const char *insp_inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
 {
@@ -58,18 +55,38 @@ CoreExport const char *insp_inet_ntop(int af, const void *src, char *dst, sockle
 
 CoreExport int insp_inet_pton(int af, const char *src, void *dst)
 {
-       sockaddr_in sa;
-       int len = sizeof(SOCKADDR);
-       int rv = WSAStringToAddressA((LPSTR)src, af, NULL, (LPSOCKADDR)&sa, &len);
-       if(rv >= 0)
+       int address_length;
+       sockaddr_storage sa;
+       sockaddr_in* sin = reinterpret_cast<sockaddr_in*>(&sa);
+       sockaddr_in6* sin6 = reinterpret_cast<sockaddr_in6*>(&sa);
+
+       switch (af)
        {
-               if(WSAGetLastError() == WSAEINVAL)
-                       rv = 0;
-               else
-                       rv = 1;
+               case AF_INET:
+                       address_length = sizeof(sockaddr_in);
+                       break;
+               case AF_INET6:
+                       address_length = sizeof(sockaddr_in6);
+                       break;
+               default:
+                       return -1;
        }
-       memcpy(dst, &sa.sin_addr, sizeof(struct in_addr));
-       return rv;
+
+       if (!WSAStringToAddress(static_cast<LPSTR>(const_cast<char *>(src)), af, NULL, reinterpret_cast<LPSOCKADDR>(&sa), &address_length))
+       {
+               switch (af)
+               {
+                       case AF_INET:
+                               memcpy(dst, &sin->sin_addr, sizeof(in_addr));
+                               break;
+                       case AF_INET6:
+                               memcpy(dst, &sin6->sin6_addr, sizeof(in6_addr));
+                               break;
+               }
+               return 1;
+       }
+       
+       return 0;
 }
 
 CoreExport DIR * opendir(const char * path)
@@ -183,9 +200,29 @@ int getopt_long(int ___argc, char *const *___argv, const char *__shortopts, cons
        return 1;
 }
 
-#include "../src/modules/m_spanningtree/link.h"
-#include "../src/modules/ssl.h"
-template class reference<Link>;
-template class reference<Autoconnect>;
-template class reference<ssl_cert>;
-template class reference<OperInfo>;
+CWin32Exception::CWin32Exception() : exception()
+{
+       dwErrorCode = GetLastError();
+       if( FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)szErrorString, _countof(szErrorString), NULL) == 0 )
+               sprintf_s(szErrorString, _countof(szErrorString), "Error code: %u", dwErrorCode);
+       for (size_t i = 0; i < _countof(szErrorString); i++)
+       {
+               if ((szErrorString[i] == '\r') || (szErrorString[i] == '\n'))
+                       szErrorString[i] = 0;
+       }
+}
+
+CWin32Exception::CWin32Exception(const CWin32Exception& other)
+{
+       strcpy_s(szErrorString, _countof(szErrorString), other.szErrorString);
+}
+
+const char* CWin32Exception::what() const throw()
+{
+       return szErrorString;
+}
+
+DWORD CWin32Exception::GetErrorCode()
+{
+       return dwErrorCode;
+}