rename FORCESTART into IMMEDIATE_START (#4547b)
[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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /**
19  * @file gns/w32nsp-install.c
20  * @brief W32 integration installer for GNS
21  * @author LRN
22  */
23
24 #include <ws2spi.h>
25 #include <windows.h>
26 #include <nspapi.h>
27 #include <initguid.h>
28 #include "gnunet_w32nsp_lib.h"
29 #include <stdio.h>
30
31 int
32 main (int argc, char **argv)
33 {
34   int ret;
35   int r = 1;
36   WSADATA wsd;
37   GUID id = GNUNET_NAMESPACE_PROVIDER_DNS;
38   wchar_t *cmdl;
39   int wargc;
40   wchar_t **wargv;
41   /* Allocate a 4K buffer to retrieve all the namespace providers */
42   DWORD dwInitialBufferLen = 4096;
43   DWORD dwBufferLen;
44   WSANAMESPACE_INFO *pi;
45   int p_count;
46   int i;
47
48   if (WSAStartup (MAKEWORD (2,2), &wsd) != 0)
49   {
50     fprintf (stderr, "WSAStartup () failed: %lu\n", GetLastError ());
51     return 5;
52   }
53
54   dwBufferLen = dwInitialBufferLen;
55   pi = malloc (dwBufferLen);
56   if (NULL == pi)
57   {
58     fprintf (stderr, "malloc (%lu) failed: %d\n", dwBufferLen, errno);
59     WSACleanup ();
60     return 6;
61   }
62   p_count = WSAEnumNameSpaceProviders (&dwBufferLen, pi);
63   if (SOCKET_ERROR == p_count)
64   {
65     DWORD err = GetLastError ();
66     if (WSAEFAULT == err && dwBufferLen != dwInitialBufferLen)
67     {
68       free (pi);
69
70       pi = malloc (dwBufferLen);
71       if (pi == NULL)
72       {
73         fprintf (stderr, "malloc (%lu) failed: %d\n", dwBufferLen, errno);
74         WSACleanup ();
75         return 6;
76       }
77
78       p_count = WSAEnumNameSpaceProviders (&dwBufferLen, pi);
79       if (SOCKET_ERROR == p_count)
80       {
81         fprintf (stderr, "WSAEnumNameSpaceProviders (&%lu, %p) failed: %lu\n", dwBufferLen, pi, GetLastError ());
82         free (pi);
83         WSACleanup ();
84         return 7;
85       }
86     }
87     else
88     {
89       fprintf (stderr, "WSAEnumNameSpaceProviders (&%lu, %p) failed: %lu\n", dwBufferLen, pi, GetLastError ());
90       free (pi);
91       WSACleanup ();
92       return 8;
93     }
94   }
95   for (i= 0; i < p_count; i++)
96   {
97     if (IsEqualGUID (&pi[i].NSProviderId, &id))
98     {
99       fprintf (stderr, "GNUnet DNS provider is already installed\n");
100       free (pi);
101       WSACleanup ();
102       return 0;
103     }
104   }
105   free (pi);
106
107   cmdl = GetCommandLineW ();
108   if (cmdl == NULL)
109   {
110     WSACleanup ();
111     return 2;
112   }
113   wargv = CommandLineToArgvW (cmdl, &wargc);
114   if (wargv == NULL)
115   {
116     WSACleanup ();
117     return 3;
118   }
119   r = 4;
120
121   if (wargc == 2)
122   {
123     ret = WSCInstallNameSpace (L"GNUnet DNS provider", wargv[1], NS_DNS, 0, &id);
124     if (ret == NO_ERROR)
125     {
126       fprintf (stderr, "Installed GNUnet DNS provider\n");
127       r = 0;
128     }
129     else
130     {
131       r = 1;
132       fprintf (stderr,
133           "WSCInstallNameSpace (L\"GNUnet DNS provider\", \"%S\", %d, 0, %p) failed: %lu\n",
134           wargv[1], NS_DNS, &id, GetLastError ());
135     }
136   }
137   else
138     fprintf (stderr, "Usage: %S <path-to-libw32nsp>\n", wargv[0]);
139   WSACleanup ();
140   return r;
141 }