vlmcsd/src/libkms.c

209 lines
4.2 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-10-24 15:32:24 +02:00
#define DLLVERSION 0x40000
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;
}
2016-10-24 15:32:24 +02:00
EXTERNC __declspec(EXTERNAL)SOCKET __cdecl ConnectToServer(const char* host, const char* port, const int addressFamily)
2016-08-15 12:35:59 +02:00
{
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;
}
2016-10-24 15:32:24 +02:00
EXTERNC __declspec(EXTERNAL)RpcStatus __cdecl BindRpc(const SOCKET sock, const int_fast8_t useMultiplexedRpc, const int_fast8_t useRpcNDR64, const int_fast8_t useRpcBTFN, PRpcDiag_t rpcDiag)
2016-08-15 12:35:59 +02:00
{
*ErrorMessage = 0;
UseMultiplexedRpc = useMultiplexedRpc;
2016-10-24 15:32:24 +02:00
UseClientRpcNDR64 = useRpcNDR64;
UseClientRpcBTFN = useRpcBTFN;
return rpcBindClient(sock, FALSE, rpcDiag);
2016-08-15 12:35:59 +02:00
}
EXTERNC __declspec(EXTERNAL) void __cdecl CloseConnection(const SOCKET sock)
{
socketclose(sock);
}
2016-10-24 15:32:24 +02:00
EXTERNC __declspec(EXTERNAL)DWORD __cdecl SendKMSRequest(const SOCKET sock, RESPONSE* baseResponse, REQUEST* baseRequest, RESPONSE_RESULT* result, BYTE *hwid)
2016-08-15 12:35:59 +02:00
{
*ErrorMessage = 0;
return SendActivationRequest(sock, baseResponse, baseRequest, result, hwid);
}
2016-10-24 15:32:24 +02:00
EXTERNC __declspec(EXTERNAL)int_fast8_t __cdecl IsDisconnected(const SOCKET sock)
2016-08-15 12:35:59 +02:00
{
return isDisconnected(sock);
2015-11-29 10:30:52 +01:00
}
2016-10-24 15:32:24 +02:00
EXTERNC __declspec(EXTERNAL)DWORD __cdecl StartKmsServer(const int port, RequestCallback_t requestCallback)
2015-11-29 10:30:52 +01:00
{
2016-06-06 04:36:00 +02:00
#ifndef SIMPLE_SOCKETS
2015-11-29 10:30:52 +01:00
char listenAddress[64];
2016-10-24 15:32:24 +02:00
if (IsServerStarted) return SOCKET_EALREADY;
2015-11-29 10:30:52 +01:00
# 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++; }
2016-10-24 15:32:24 +02:00
if (!maxsockets) return SOCKET_EAFNOSUPPORT;
2015-11-29 10:30:52 +01:00
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);
2016-10-24 15:32:24 +02:00
return SOCKET_EADDRNOTAVAIL;
2015-11-29 10:30:52 +01:00
}
IsServerStarted = TRUE;
runServer();
IsServerStarted = FALSE;
return 0;
2016-06-06 04:36:00 +02:00
# else // SIMPLE_SOCKETS
2016-10-24 15:32:24 +02:00
if (IsServerStarted) return SOCKET_EALREADY;
2016-06-06 04:36:00 +02:00
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();
2016-10-24 15:32:24 +02:00
free(defaultport);
2016-06-06 04:36:00 +02:00
if (error) return error;
IsServerStarted = TRUE;
runServer();
IsServerStarted = FALSE;
return 0;
# endif // SIMPLE_SOCKETS
2015-11-29 10:30:52 +01:00
}
2016-10-24 15:32:24 +02:00
EXTERNC __declspec(EXTERNAL)DWORD __cdecl StopKmsServer()
2015-11-29 10:30:52 +01:00
{
2016-10-24 15:32:24 +02:00
if (!IsServerStarted) return VLMCSD_EPERM;
2015-11-29 10:30:52 +01:00
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;
}