libkms.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * libkms.c
  3. */
  4. #ifndef CONFIG
  5. #define CONFIG "config.h"
  6. #endif // CONFIG
  7. #include CONFIG
  8. #ifdef EXTERNAL
  9. #undef EXTERNAL
  10. #endif
  11. #define EXTERNAL dllexport
  12. #define DLLVERSION 0x30002
  13. #include "libkms.h"
  14. #include "shared_globals.h"
  15. #include "network.h"
  16. #include "helpers.h"
  17. #ifndef _WIN32
  18. #include <signal.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <errno.h>
  22. #include <netinet/in.h>
  23. #endif // WIN32
  24. static int_fast8_t IsServerStarted = FALSE;
  25. #ifdef _WIN32
  26. #ifndef USE_MSRPC
  27. static int_fast8_t SocketsInitialized = FALSE;
  28. WSADATA wsadata;
  29. static int initializeWinSockets()
  30. {
  31. if (SocketsInitialized) return 0;
  32. SocketsInitialized = TRUE;
  33. return WSAStartup(0x0202, &wsadata);
  34. }
  35. #endif // USE_MSRPC
  36. #endif // _WIN32
  37. EXTERNC __declspec(EXTERNAL) char* __cdecl GetErrorMessage()
  38. {
  39. return ErrorMessage;
  40. }
  41. EXTERNC __declspec(EXTERNAL) SOCKET __cdecl ConnectToServer(const char* host, const char* port, const int addressFamily)
  42. {
  43. SOCKET sock;
  44. *ErrorMessage = 0;
  45. # if defined(_WIN32) && !defined(USE_MSRPC)
  46. initializeWinSockets();
  47. # endif // defined(_WIN32) && !defined(USE_MSRPC)
  48. size_t adrlen = strlen(host) + 16;
  49. char* RemoteAddr = (char*)alloca(adrlen);
  50. snprintf(RemoteAddr, adrlen, "[%s]:%s", host, port);
  51. sock = connectToAddress(RemoteAddr, addressFamily, FALSE);
  52. if (sock == INVALID_RPCCTX)
  53. {
  54. printerrorf("Fatal: Could not connect to %s\n", RemoteAddr);
  55. return sock;
  56. }
  57. return sock;
  58. }
  59. EXTERNC __declspec(EXTERNAL) RpcStatus __cdecl BindRpc(const SOCKET sock, const int_fast8_t useMultiplexedRpc)
  60. {
  61. *ErrorMessage = 0;
  62. UseMultiplexedRpc = useMultiplexedRpc;
  63. return rpcBindClient(sock, FALSE);
  64. }
  65. EXTERNC __declspec(EXTERNAL) void __cdecl CloseConnection(const SOCKET sock)
  66. {
  67. socketclose(sock);
  68. }
  69. EXTERNC __declspec(EXTERNAL) DWORD __cdecl SendKMSRequest(const SOCKET sock, RESPONSE* baseResponse, REQUEST* baseRequest, RESPONSE_RESULT* result, BYTE *hwid)
  70. {
  71. *ErrorMessage = 0;
  72. return SendActivationRequest(sock, baseResponse, baseRequest, result, hwid);
  73. }
  74. EXTERNC __declspec(EXTERNAL) int_fast8_t __cdecl IsDisconnected(const SOCKET sock)
  75. {
  76. return isDisconnected(sock);
  77. }
  78. EXTERNC __declspec(EXTERNAL) DWORD __cdecl StartKmsServer(const int port, RequestCallback_t requestCallback)
  79. {
  80. #ifndef SIMPLE_SOCKETS
  81. char listenAddress[64];
  82. if (IsServerStarted) return !0;
  83. # ifdef _WIN32
  84. int error = initializeWinSockets();
  85. if (error) return error;
  86. # endif // _WIN32
  87. CreateResponseBase = requestCallback;
  88. int maxsockets = 0;
  89. int_fast8_t haveIPv4 = FALSE;
  90. int_fast8_t haveIPv6 = FALSE;
  91. if (checkProtocolStack(AF_INET)) { haveIPv4 = TRUE; maxsockets++; }
  92. if (checkProtocolStack(AF_INET6)) { haveIPv6 = TRUE; maxsockets++; }
  93. if(!maxsockets) return !0;
  94. SocketList = (SOCKET*)vlmcsd_malloc(sizeof(SOCKET) * (size_t)maxsockets);
  95. numsockets = 0;
  96. if (haveIPv4)
  97. {
  98. snprintf(listenAddress, 64, "0.0.0.0:%u", (unsigned int)port);
  99. addListeningSocket(listenAddress);
  100. }
  101. if (haveIPv6)
  102. {
  103. snprintf(listenAddress, 64, "[::]:%u", (unsigned int)port);
  104. addListeningSocket(listenAddress);
  105. }
  106. if (!numsockets)
  107. {
  108. free(SocketList);
  109. return !0;
  110. }
  111. IsServerStarted = TRUE;
  112. runServer();
  113. IsServerStarted = FALSE;
  114. return 0;
  115. # else // SIMPLE_SOCKETS
  116. if (IsServerStarted) return !0;
  117. int error;
  118. # ifdef _WIN32
  119. error = initializeWinSockets();
  120. if (error) return error;
  121. # endif // _WIN32
  122. defaultport = vlmcsd_malloc(16);
  123. snprintf((char*)defaultport, (size_t)16, "%i", port);
  124. CreateResponseBase = requestCallback;
  125. error = listenOnAllAddresses();
  126. if (error) return error;
  127. IsServerStarted = TRUE;
  128. runServer();
  129. IsServerStarted = FALSE;
  130. return 0;
  131. # endif // SIMPLE_SOCKETS
  132. }
  133. EXTERNC __declspec(EXTERNAL) DWORD __cdecl StopKmsServer()
  134. {
  135. if (!IsServerStarted) return !0;
  136. closeAllListeningSockets();
  137. # ifndef SIMPLE_SOCKETS
  138. if (SocketList) free(SocketList);
  139. # endif
  140. return 0;
  141. }
  142. EXTERNC __declspec(EXTERNAL) int __cdecl GetLibKmsVersion()
  143. {
  144. return DLLVERSION;
  145. }
  146. EXTERNC __declspec(EXTERNAL) const char* const __cdecl GetEmulatorVersion()
  147. {
  148. return VERSION;
  149. }