w32nsp-install.c 3.5 KB

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