w32nsp-install.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2012 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file gns/w32nsp-install.c
  18. * @brief W32 integration installer for GNS
  19. * @author LRN
  20. */
  21. #include <ws2spi.h>
  22. #include <windows.h>
  23. #include <nspapi.h>
  24. #include <initguid.h>
  25. #include "gnunet_w32nsp_lib.h"
  26. #include <stdio.h>
  27. int
  28. main (int argc, char **argv)
  29. {
  30. int ret;
  31. int r = 1;
  32. WSADATA wsd;
  33. GUID id = GNUNET_NAMESPACE_PROVIDER_DNS;
  34. wchar_t *cmdl;
  35. int wargc;
  36. wchar_t **wargv;
  37. /* Allocate a 4K buffer to retrieve all the namespace providers */
  38. DWORD dwInitialBufferLen = 4096;
  39. DWORD dwBufferLen;
  40. WSANAMESPACE_INFO *pi;
  41. int p_count;
  42. int i;
  43. if (WSAStartup (MAKEWORD (2,2), &wsd) != 0)
  44. {
  45. fprintf (stderr, "WSAStartup () failed: %lu\n", GetLastError ());
  46. return 5;
  47. }
  48. dwBufferLen = dwInitialBufferLen;
  49. pi = malloc (dwBufferLen);
  50. if (NULL == pi)
  51. {
  52. fprintf (stderr, "malloc (%lu) failed: %d\n", dwBufferLen, errno);
  53. WSACleanup ();
  54. return 6;
  55. }
  56. p_count = WSAEnumNameSpaceProviders (&dwBufferLen, pi);
  57. if (SOCKET_ERROR == p_count)
  58. {
  59. DWORD err = GetLastError ();
  60. if (WSAEFAULT == err && dwBufferLen != dwInitialBufferLen)
  61. {
  62. free (pi);
  63. pi = malloc (dwBufferLen);
  64. if (pi == NULL)
  65. {
  66. fprintf (stderr, "malloc (%lu) failed: %d\n", dwBufferLen, errno);
  67. WSACleanup ();
  68. return 6;
  69. }
  70. p_count = WSAEnumNameSpaceProviders (&dwBufferLen, pi);
  71. if (SOCKET_ERROR == p_count)
  72. {
  73. fprintf (stderr, "WSAEnumNameSpaceProviders (&%lu, %p) failed: %lu\n", dwBufferLen, pi, GetLastError ());
  74. free (pi);
  75. WSACleanup ();
  76. return 7;
  77. }
  78. }
  79. else
  80. {
  81. fprintf (stderr, "WSAEnumNameSpaceProviders (&%lu, %p) failed: %lu\n", dwBufferLen, pi, GetLastError ());
  82. free (pi);
  83. WSACleanup ();
  84. return 8;
  85. }
  86. }
  87. for (i= 0; i < p_count; i++)
  88. {
  89. if (IsEqualGUID (&pi[i].NSProviderId, &id))
  90. {
  91. fprintf (stderr, "GNUnet DNS provider is already installed\n");
  92. free (pi);
  93. WSACleanup ();
  94. return 0;
  95. }
  96. }
  97. free (pi);
  98. cmdl = GetCommandLineW ();
  99. if (cmdl == NULL)
  100. {
  101. WSACleanup ();
  102. return 2;
  103. }
  104. wargv = CommandLineToArgvW (cmdl, &wargc);
  105. if (wargv == NULL)
  106. {
  107. WSACleanup ();
  108. return 3;
  109. }
  110. r = 4;
  111. if (wargc == 2)
  112. {
  113. ret = WSCInstallNameSpace (L"GNUnet DNS provider", wargv[1], NS_DNS, 0, &id);
  114. if (ret == NO_ERROR)
  115. {
  116. fprintf (stderr, "Installed GNUnet DNS provider\n");
  117. r = 0;
  118. }
  119. else
  120. {
  121. r = 1;
  122. fprintf (stderr,
  123. "WSCInstallNameSpace (L\"GNUnet DNS provider\", \"%S\", %d, 0, %p) failed: %lu\n",
  124. wargv[1], NS_DNS, &id, GetLastError ());
  125. }
  126. }
  127. else
  128. fprintf (stderr, "Usage: %S <path-to-libw32nsp>\n", wargv[0]);
  129. WSACleanup ();
  130. return r;
  131. }