libkms.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 0x30001
  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. #ifdef IS_LIBRARY
  25. char ErrorMessage[MESSAGE_BUFFER_SIZE];
  26. #endif // IS_LIBRARY
  27. static int_fast8_t IsServerStarted = FALSE;
  28. EXTERNC __declspec(EXTERNAL) DWORD __cdecl SendActivationRequest
  29. (
  30. const char* const hostname,
  31. const int port,
  32. RESPONSE* baseResponse,
  33. const REQUEST* const baseRequest,
  34. RESPONSE_RESULT* result, BYTE *hwid
  35. )
  36. {
  37. return !0; // not yet implemented
  38. }
  39. EXTERNC __declspec(EXTERNAL) DWORD __cdecl StartKmsServer(const int port, RequestCallback_t requestCallback)
  40. {
  41. #ifndef SIMPLE_SOCKETS
  42. char listenAddress[64];
  43. if (IsServerStarted) return !0;
  44. # ifdef _WIN32
  45. # ifndef USE_MSRPC
  46. // Windows Sockets must be initialized
  47. WSADATA wsadata;
  48. int error;
  49. if ((error = WSAStartup(0x0202, &wsadata)))
  50. {
  51. return error;
  52. }
  53. # endif // USE_MSRPC
  54. # endif // _WIN32
  55. CreateResponseBase = requestCallback;
  56. int maxsockets = 0;
  57. int_fast8_t haveIPv4 = FALSE;
  58. int_fast8_t haveIPv6 = FALSE;
  59. if (checkProtocolStack(AF_INET)) { haveIPv4 = TRUE; maxsockets++; }
  60. if (checkProtocolStack(AF_INET6)) { haveIPv6 = TRUE; maxsockets++; }
  61. if(!maxsockets) return !0;
  62. SocketList = (SOCKET*)vlmcsd_malloc(sizeof(SOCKET) * (size_t)maxsockets);
  63. numsockets = 0;
  64. if (haveIPv4)
  65. {
  66. snprintf(listenAddress, 64, "0.0.0.0:%u", (unsigned int)port);
  67. addListeningSocket(listenAddress);
  68. }
  69. if (haveIPv6)
  70. {
  71. snprintf(listenAddress, 64, "[::]:%u", (unsigned int)port);
  72. addListeningSocket(listenAddress);
  73. }
  74. if (!numsockets)
  75. {
  76. free(SocketList);
  77. return !0;
  78. }
  79. IsServerStarted = TRUE;
  80. runServer();
  81. IsServerStarted = FALSE;
  82. return 0;
  83. # else // SIMPLE_SOCKETS
  84. if (IsServerStarted) return !0;
  85. int error;
  86. # ifdef _WIN32
  87. # ifndef USE_MSRPC
  88. // Windows Sockets must be initialized
  89. WSADATA wsadata;
  90. if ((error = WSAStartup(0x0202, &wsadata)))
  91. {
  92. return error;
  93. }
  94. # endif // USE_MSRPC
  95. # endif // _WIN32
  96. defaultport = vlmcsd_malloc(16);
  97. snprintf((char*)defaultport, (size_t)16, "%i", port);
  98. CreateResponseBase = requestCallback;
  99. error = listenOnAllAddresses();
  100. if (error) return error;
  101. IsServerStarted = TRUE;
  102. runServer();
  103. IsServerStarted = FALSE;
  104. return 0;
  105. # endif // SIMPLE_SOCKETS
  106. }
  107. EXTERNC __declspec(EXTERNAL) DWORD __cdecl StopKmsServer()
  108. {
  109. if (!IsServerStarted) return !0;
  110. closeAllListeningSockets();
  111. # ifndef SIMPLE_SOCKETS
  112. if (SocketList) free(SocketList);
  113. # endif
  114. return 0;
  115. }
  116. EXTERNC __declspec(EXTERNAL) int __cdecl GetLibKmsVersion()
  117. {
  118. return DLLVERSION;
  119. }
  120. EXTERNC __declspec(EXTERNAL) const char* const __cdecl GetEmulatorVersion()
  121. {
  122. return VERSION;
  123. }