vlmcsd/libkms.c

206 lines
4.0 KiB
C
Raw Permalink Normal View History

2015-11-29 10:30:52 +01:00
/*
* libkms.c
*/
#ifndef CONFIG
#define CONFIG "config.h"
#endif // CONFIG
#include CONFIG
2016-06-06 04:36:00 +02:00
#ifdef EXTERNAL
#undef EXTERNAL
#endif
2015-11-29 10:30:52 +01:00
#define EXTERNAL dllexport
2016-08-15 12:35:59 +02:00
#define DLLVERSION 0x30002
2015-11-29 10:30:52 +01:00
#include "libkms.h"
#include "shared_globals.h"
#include "network.h"
#include "helpers.h"
#ifndef _WIN32
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <netinet/in.h>
#endif // WIN32
static int_fast8_t IsServerStarted = FALSE;
2016-08-15 12:35:59 +02:00
#ifdef _WIN32
#ifndef USE_MSRPC
static int_fast8_t SocketsInitialized = FALSE;
WSADATA wsadata;
static int initializeWinSockets()
{
if (SocketsInitialized) return 0;
SocketsInitialized = TRUE;
return WSAStartup(0x0202, &wsadata);
}
#endif // USE_MSRPC
#endif // _WIN32
2015-11-29 10:30:52 +01:00
2016-08-15 12:35:59 +02:00
EXTERNC __declspec(EXTERNAL) char* __cdecl GetErrorMessage()
2015-11-29 10:30:52 +01:00
{
2016-08-15 12:35:59 +02:00
return ErrorMessage;
}
EXTERNC __declspec(EXTERNAL) SOCKET __cdecl ConnectToServer(const char* host, const char* port, const int addressFamily)
{
SOCKET sock;
*ErrorMessage = 0;
# if defined(_WIN32) && !defined(USE_MSRPC)
initializeWinSockets();
# endif // defined(_WIN32) && !defined(USE_MSRPC)
size_t adrlen = strlen(host) + 16;
char* RemoteAddr = (char*)alloca(adrlen);
2016-10-08 07:35:48 +02:00
vlmcsd_snprintf(RemoteAddr, adrlen, "[%s]:%s", host, port);
2016-08-15 12:35:59 +02:00
sock = connectToAddress(RemoteAddr, addressFamily, FALSE);
if (sock == INVALID_RPCCTX)
{
printerrorf("Fatal: Could not connect to %s\n", RemoteAddr);
return sock;
}
return sock;
}
EXTERNC __declspec(EXTERNAL) RpcStatus __cdecl BindRpc(const SOCKET sock, const int_fast8_t useMultiplexedRpc)
{
*ErrorMessage = 0;
UseMultiplexedRpc = useMultiplexedRpc;
return rpcBindClient(sock, FALSE);
}
EXTERNC __declspec(EXTERNAL) void __cdecl CloseConnection(const SOCKET sock)
{
socketclose(sock);
}
EXTERNC __declspec(EXTERNAL) DWORD __cdecl SendKMSRequest(const SOCKET sock, RESPONSE* baseResponse, REQUEST* baseRequest, RESPONSE_RESULT* result, BYTE *hwid)
{
*ErrorMessage = 0;
return SendActivationRequest(sock, baseResponse, baseRequest, result, hwid);
}
EXTERNC __declspec(EXTERNAL) int_fast8_t __cdecl IsDisconnected(const SOCKET sock)
{
return isDisconnected(sock);
2015-11-29 10:30:52 +01:00
}
EXTERNC __declspec(EXTERNAL) DWORD __cdecl StartKmsServer(const int port, RequestCallback_t requestCallback)
{
2016-06-06 04:36:00 +02:00
#ifndef SIMPLE_SOCKETS
2015-11-29 10:30:52 +01:00
char listenAddress[64];
if (IsServerStarted) return !0;
# ifdef _WIN32
2016-08-15 12:35:59 +02:00
int error = initializeWinSockets();
if (error) return error;
2015-11-29 10:30:52 +01:00
# endif // _WIN32
CreateResponseBase = requestCallback;
int maxsockets = 0;
int_fast8_t haveIPv4 = FALSE;
int_fast8_t haveIPv6 = FALSE;
if (checkProtocolStack(AF_INET)) { haveIPv4 = TRUE; maxsockets++; }
if (checkProtocolStack(AF_INET6)) { haveIPv6 = TRUE; maxsockets++; }
if(!maxsockets) return !0;
SocketList = (SOCKET*)vlmcsd_malloc(sizeof(SOCKET) * (size_t)maxsockets);
numsockets = 0;
if (haveIPv4)
{
snprintf(listenAddress, 64, "0.0.0.0:%u", (unsigned int)port);
addListeningSocket(listenAddress);
}
if (haveIPv6)
{
snprintf(listenAddress, 64, "[::]:%u", (unsigned int)port);
addListeningSocket(listenAddress);
}
if (!numsockets)
{
free(SocketList);
return !0;
}
IsServerStarted = TRUE;
runServer();
IsServerStarted = FALSE;
return 0;
2016-06-06 04:36:00 +02:00
# else // SIMPLE_SOCKETS
if (IsServerStarted) return !0;
int error;
# ifdef _WIN32
2016-08-15 12:35:59 +02:00
error = initializeWinSockets();
if (error) return error;
2016-06-06 04:36:00 +02:00
# endif // _WIN32
defaultport = vlmcsd_malloc(16);
2016-10-08 07:35:48 +02:00
vlmcsd_snprintf((char*)defaultport, (size_t)16, "%i", port);
2016-06-06 04:36:00 +02:00
CreateResponseBase = requestCallback;
error = listenOnAllAddresses();
if (error) return error;
IsServerStarted = TRUE;
runServer();
IsServerStarted = FALSE;
return 0;
# endif // SIMPLE_SOCKETS
2015-11-29 10:30:52 +01:00
}
EXTERNC __declspec(EXTERNAL) DWORD __cdecl StopKmsServer()
{
if (!IsServerStarted) return !0;
closeAllListeningSockets();
2016-06-06 04:36:00 +02:00
# ifndef SIMPLE_SOCKETS
2015-11-29 10:30:52 +01:00
if (SocketList) free(SocketList);
2016-06-06 04:36:00 +02:00
# endif
2015-11-29 10:30:52 +01:00
return 0;
}
EXTERNC __declspec(EXTERNAL) int __cdecl GetLibKmsVersion()
{
return DLLVERSION;
}
2016-06-06 04:36:00 +02:00
EXTERNC __declspec(EXTERNAL) const char* const __cdecl GetEmulatorVersion()
{
return VERSION;
}