libkms.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * libkms.c
  3. */
  4. #ifndef CONFIG
  5. #define CONFIG "config.h"
  6. #endif // CONFIG
  7. #include CONFIG
  8. #define EXTERNAL dllexport
  9. #define DLLVERSION 0x30000
  10. #include "libkms.h"
  11. #include "shared_globals.h"
  12. #include "network.h"
  13. #include "helpers.h"
  14. #ifndef _WIN32
  15. #include <signal.h>
  16. #include <unistd.h>
  17. #include <fcntl.h>
  18. #include <errno.h>
  19. #include <netinet/in.h>
  20. #endif // WIN32
  21. static int_fast8_t IsServerStarted = FALSE;
  22. EXTERNC __declspec(EXTERNAL) DWORD __cdecl SendActivationRequest
  23. (
  24. const char* const hostname,
  25. const int port,
  26. RESPONSE* baseResponse,
  27. const REQUEST* const baseRequest,
  28. RESPONSE_RESULT* result, BYTE *hwid
  29. )
  30. {
  31. return !0; // not yet implemented
  32. }
  33. EXTERNC __declspec(EXTERNAL) DWORD __cdecl StartKmsServer(const int port, RequestCallback_t requestCallback)
  34. {
  35. char listenAddress[64];
  36. if (IsServerStarted) return !0;
  37. # ifdef _WIN32
  38. # ifndef USE_MSRPC
  39. // Windows Sockets must be initialized
  40. WSADATA wsadata;
  41. int error;
  42. if ((error = WSAStartup(0x0202, &wsadata)))
  43. {
  44. return error;
  45. }
  46. # endif // USE_MSRPC
  47. # endif // _WIN32
  48. CreateResponseBase = requestCallback;
  49. int maxsockets = 0;
  50. int_fast8_t haveIPv4 = FALSE;
  51. int_fast8_t haveIPv6 = FALSE;
  52. if (checkProtocolStack(AF_INET)) { haveIPv4 = TRUE; maxsockets++; }
  53. if (checkProtocolStack(AF_INET6)) { haveIPv6 = TRUE; maxsockets++; }
  54. if(!maxsockets) return !0;
  55. SocketList = (SOCKET*)vlmcsd_malloc(sizeof(SOCKET) * (size_t)maxsockets);
  56. numsockets = 0;
  57. if (haveIPv4)
  58. {
  59. snprintf(listenAddress, 64, "0.0.0.0:%u", (unsigned int)port);
  60. addListeningSocket(listenAddress);
  61. }
  62. if (haveIPv6)
  63. {
  64. snprintf(listenAddress, 64, "[::]:%u", (unsigned int)port);
  65. addListeningSocket(listenAddress);
  66. }
  67. if (!numsockets)
  68. {
  69. free(SocketList);
  70. return !0;
  71. }
  72. IsServerStarted = TRUE;
  73. runServer();
  74. IsServerStarted = FALSE;
  75. return 0;
  76. }
  77. EXTERNC __declspec(EXTERNAL) DWORD __cdecl StopKmsServer()
  78. {
  79. if (!IsServerStarted) return !0;
  80. closeAllListeningSockets();
  81. if (SocketList) free(SocketList);
  82. return 0;
  83. }
  84. EXTERNC __declspec(EXTERNAL) int __cdecl GetLibKmsVersion()
  85. {
  86. return DLLVERSION;
  87. }