glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / gns / w32nsp-install.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14 */
15 /**
16  * @file gns/w32nsp-install.c
17  * @brief W32 integration installer for GNS
18  * @author LRN
19  */
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
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
45   if (WSAStartup (MAKEWORD (2,2), &wsd) != 0)
46   {
47     fprintf (stderr, "WSAStartup () failed: %lu\n", GetLastError ());
48     return 5;
49   }
50
51   dwBufferLen = dwInitialBufferLen;
52   pi = malloc (dwBufferLen);
53   if (NULL == pi)
54   {
55     fprintf (stderr, "malloc (%lu) failed: %d\n", dwBufferLen, errno);
56     WSACleanup ();
57     return 6;
58   }
59   p_count = WSAEnumNameSpaceProviders (&dwBufferLen, pi);
60   if (SOCKET_ERROR == p_count)
61   {
62     DWORD err = GetLastError ();
63     if (WSAEFAULT == err && dwBufferLen != dwInitialBufferLen)
64     {
65       free (pi);
66
67       pi = malloc (dwBufferLen);
68       if (pi == NULL)
69       {
70         fprintf (stderr, "malloc (%lu) failed: %d\n", dwBufferLen, errno);
71         WSACleanup ();
72         return 6;
73       }
74
75       p_count = WSAEnumNameSpaceProviders (&dwBufferLen, pi);
76       if (SOCKET_ERROR == p_count)
77       {
78         fprintf (stderr, "WSAEnumNameSpaceProviders (&%lu, %p) failed: %lu\n", dwBufferLen, pi, GetLastError ());
79         free (pi);
80         WSACleanup ();
81         return 7;
82       }
83     }
84     else
85     {
86       fprintf (stderr, "WSAEnumNameSpaceProviders (&%lu, %p) failed: %lu\n", dwBufferLen, pi, GetLastError ());
87       free (pi);
88       WSACleanup ();
89       return 8;
90     }
91   }
92   for (i= 0; i < p_count; i++)
93   {
94     if (IsEqualGUID (&pi[i].NSProviderId, &id))
95     {
96       fprintf (stderr, "GNUnet DNS provider is already installed\n");
97       free (pi);
98       WSACleanup ();
99       return 0;
100     }
101   }
102   free (pi);
103
104   cmdl = GetCommandLineW ();
105   if (cmdl == NULL)
106   {
107     WSACleanup ();
108     return 2;
109   }
110   wargv = CommandLineToArgvW (cmdl, &wargc);
111   if (wargv == NULL)
112   {
113     WSACleanup ();
114     return 3;
115   }
116   r = 4;
117
118   if (wargc == 2)
119   {
120     ret = WSCInstallNameSpace (L"GNUnet DNS provider", wargv[1], NS_DNS, 0, &id);
121     if (ret == NO_ERROR)
122     {
123       fprintf (stderr, "Installed GNUnet DNS provider\n");
124       r = 0;
125     }
126     else
127     {
128       r = 1;
129       fprintf (stderr,
130           "WSCInstallNameSpace (L\"GNUnet DNS provider\", \"%S\", %d, 0, %p) failed: %lu\n",
131           wargv[1], NS_DNS, &id, GetLastError ());
132     }
133   }
134   else
135     fprintf (stderr, "Usage: %S <path-to-libw32nsp>\n", wargv[0]);
136   WSACleanup ();
137   return r;
138 }