[TOOLS] repair ndinstall (installsp.c) return a proper exit code; removed unused...
[mirror/winof/.git] / tools / ndinstall / user / installsp.c
1 /*\r
2  * Copyright (c) 2005 SilverStorm Technologies.  All rights reserved.\r
3  * Portions Copyright (c) 2008 Microsoft Corporation.  All rights reserved.\r
4  * Copyright (c) 2009 Intel Corp, Inc.  All rights reserved.\r
5  *\r
6  * This software is available to you under the OpenIB.org BSD license\r
7  * below:\r
8  *\r
9  *     Redistribution and use in source and binary forms, with or\r
10  *     without modification, are permitted provided that the following\r
11  *     conditions are met:\r
12  *\r
13  *      - Redistributions of source code must retain the above\r
14  *        copyright notice, this list of conditions and the following\r
15  *        disclaimer.\r
16  *\r
17  *      - Redistributions in binary form must reproduce the above\r
18  *        copyright notice, this list of conditions and the following\r
19  *        disclaimer in the documentation and/or other materials\r
20  *        provided with the distribution.\r
21  *\r
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
25  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\r
26  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\r
27  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
28  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
29  * SOFTWARE.\r
30  *\r
31  * $Id$\r
32  */\r
33 \r
34 /*\r
35  *      Module Name: installsp.c\r
36  *      Description: This module installs/removes the NetworkDirect provider for infiniband.\r
37  *      execute:\r
38  *      To install the service provider\r
39  *              installsp -i\r
40  *      To remove the service provider\r
41  *              installsp -r\r
42  */\r
43 \r
44 #include <winsock2.h>\r
45 #include <ws2tcpip.h>\r
46 #include <ws2spi.h>\r
47 #include <stdio.h>\r
48 #include <string.h>\r
49 #include "..\..\..\..\etc\user\getopt.c"\r
50 \r
51 #ifndef PFL_NETWORKDIRECT_PROVIDER\r
52 #define PFL_NETWORKDIRECT_PROVIDER          0x00000010\r
53 #endif\r
54 \r
55 /* Initialize the LSP's provider path for Infiband Service Provider dll */\r
56 #define IBAL_INDEX                      0\r
57 #define IBAL_DEBUG_INDEX        1\r
58 #define WV_INDEX                        2\r
59 #define WV_DEBUG_INDEX          3\r
60 #define MAX_INDEX                       4\r
61 \r
62 int index;\r
63 \r
64 static const char * const provider_name[MAX_INDEX] = {\r
65         "IBAL", "IBAL (debug)", "WinVerbs", "WinVerbs (debug)"\r
66 };\r
67 \r
68 static const WCHAR * const provider_path[MAX_INDEX] = {\r
69         L"%SYSTEMROOT%\\system32\\ibndprov.dll",\r
70         L"%SYSTEMROOT%\\system32\\ibndprov.dll",\r
71         L"%SYSTEMROOT%\\system32\\wvndprov.dll",\r
72         L"%SYSTEMROOT%\\system32\\wvndprovd.dll"\r
73 };\r
74 \r
75 static const WCHAR * const provider_desc[MAX_INDEX] = {\r
76         L"OpenFabrics Network Direct Provider",\r
77         L"OpenFabrics Network Direct Provider (Debug)",\r
78         L"OpenFabrics Winverbs Network Direct Provider",\r
79         L"OpenFabrics Winverbs Network Direct Provider (Debug)"\r
80 };\r
81 \r
82 static GUID provider_guid[MAX_INDEX] = {\r
83         // {52CDAA00-29D0-46be-8FC6-E51D7075C338}\r
84         { 0x52CDAA00, 0x29D0, 0x46be, { 0x8f, 0xc6, 0xe5, 0x1d, 0x70, 0x75, 0xc3, 0x38 } },\r
85         // {52CDAA00-29D0-46be-8FC6-E51D7075C338}\r
86         { 0x52CDAA00, 0x29D0, 0x46be, { 0x8f, 0xc6, 0xe5, 0x1d, 0x70, 0x75, 0xc3, 0x38 } },\r
87         // {854DCE83-C872-4462-A3EB-C961C40E59D0}\r
88         { 0x854dce83, 0xc872, 0x4462, { 0xa3, 0xeb, 0xc9, 0x61, 0xc4, 0x0e, 0x59, 0xd0 } },\r
89         // {1B8F1692-EDD9-4153-A159-605A73BCFFCF}\r
90         { 0x1b8f1692, 0xedd9, 0x4153, { 0xa1, 0x59, 0x60, 0x5a, 0x73, 0xbc, 0xff, 0xcf } }\r
91 };\r
92 \r
93 #ifdef _WIN64\r
94 #define WSCInstallProvider      WSCInstallProvider64_32\r
95 #endif  /* _WIN64 */\r
96 \r
97 static void\r
98 show_usage (char *progname)\r
99 {\r
100         printf("usage: %s\n", progname);\r
101         printf("\tRemove, install, and list OFA NetworkDirect providers\n\n");\r
102         printf("\t[-[i|r] provider_name]  Install/remove the specified provider\n");\r
103         printf("\t[-d]                    Install/remove debug version of provider\n");\r
104         printf("\t[-l]                    list OFA ND providers\n");\r
105         printf("\tprovider_name must be one of the following:\n");\r
106         printf("\t\tibal\n");\r
107         printf("\t\twinverbs\n");\r
108 }\r
109 \r
110 \r
111 static void print_providers(void)\r
112 {\r
113         WSAPROTOCOL_INFOW *protocol_info;\r
114         unsigned int protocol_count;\r
115         unsigned int i;\r
116         DWORD protocol_size;\r
117         INT err_no;\r
118         int rc;\r
119 \r
120         /* Find the size of the buffer */\r
121         protocol_size = 0;\r
122         rc = WSCEnumProtocols (NULL, NULL, &protocol_size, &err_no);\r
123         if (rc == SOCKET_ERROR && err_no != WSAENOBUFS) {\r
124                 printf("WSCEnumProtocols() returned error (%d)\n", err_no);\r
125                 return;\r
126         }\r
127 \r
128         protocol_info = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, protocol_size);\r
129         if (protocol_info == NULL) {\r
130                 printf("HeapAlloc() failed\n");\r
131                 return;\r
132         }\r
133 \r
134         /* Enumerate the catalog for real */\r
135         rc = WSCEnumProtocols (NULL, protocol_info, &protocol_size, &err_no);\r
136         if (rc == SOCKET_ERROR) {\r
137                 printf("WSCEnumProtocols returned error for real enumeration (%d)\n",\r
138                          err_no);\r
139                 HeapFree (GetProcessHeap (), 0, protocol_info);\r
140                 return;\r
141         }\r
142 \r
143         protocol_count = rc;\r
144 \r
145         printf("\nCurrent providers:\n");\r
146         for (i = 0; i < protocol_count; i++) {\r
147                 printf ("\t%010d - %S\n", protocol_info[i].dwCatalogEntryId,\r
148                                 protocol_info[i].szProtocol);\r
149         }\r
150 \r
151         HeapFree (GetProcessHeap (), 0, protocol_info);\r
152 \r
153         return;\r
154 }\r
155 \r
156 /*\r
157  * Function: install_provider\r
158  *   Description: installs the service provider\r
159  *\r
160  * Note: most of the information setup here comes from "MSDN Home >\r
161  * MSDN Library > Windows Development > Network Devices and\r
162  * Protocols > Design Guide > System Area Networks > Windows Sockets\r
163  * Direct > Windows Sockets Direct Component Operation > Installing\r
164  * Windows Sockets Direct Components".\r
165  * The direct link is http://msdn.microsoft.com/library/default.asp?url=/library/en-us/network/hh/network/wsdp_2xrb.asp\r
166  */\r
167 static int install_provider(void)\r
168 {\r
169         int rc, err_no;\r
170         WSAPROTOCOL_INFOW provider;\r
171 \r
172         /* Setup the values in PROTOCOL_INFO */\r
173         provider.dwServiceFlags1 = \r
174                 XP1_GUARANTEED_DELIVERY | \r
175                 XP1_GUARANTEED_ORDER | \r
176                 XP1_MESSAGE_ORIENTED |\r
177                 XP1_CONNECT_DATA;  /*XP1_GRACEFUL_CLOSE;*/\r
178         provider.dwServiceFlags2 = 0;   /* Reserved */\r
179         provider.dwServiceFlags3 = 0;   /* Reserved */\r
180         provider.dwServiceFlags4 = 0;   /* Reserved */\r
181         provider.dwProviderFlags = PFL_HIDDEN | PFL_NETWORKDIRECT_PROVIDER;\r
182         provider.ProviderId = provider_guid[index];\r
183         provider.dwCatalogEntryId = 0;\r
184         provider.ProtocolChain.ChainLen = 1;    /* Base Protocol Service Provider */\r
185         provider.iVersion = 1;\r
186         if (index == IBAL_INDEX || index == IBAL_DEBUG_INDEX) {\r
187                 provider.iAddressFamily = AF_INET;\r
188                 provider.iMaxSockAddr = sizeof(SOCKADDR_IN);\r
189         } else {\r
190                 provider.iAddressFamily = AF_INET6;\r
191                 provider.iMaxSockAddr = sizeof(SOCKADDR_IN6);\r
192         }\r
193         provider.iMinSockAddr = 16;\r
194         provider.iSocketType = -1;\r
195         provider.iProtocol = 0;\r
196         provider.iProtocolMaxOffset = 0;\r
197         provider.iNetworkByteOrder = BIGENDIAN;\r
198         provider.iSecurityScheme = SECURITY_PROTOCOL_NONE;\r
199         provider.dwMessageSize = 0xFFFFFFFF; /* IB supports 32-bit lengths for data transfers on RC */\r
200         provider.dwProviderReserved = 0;\r
201         wcscpy( provider.szProtocol, provider_desc[index] );\r
202 \r
203         printf("\nInstalling %s provider: ", provider_name[index]);\r
204         rc = WSCInstallProvider(\r
205                 &provider_guid[index], provider_path[index], &provider, 1, &err_no );\r
206         if( rc == SOCKET_ERROR )\r
207         {\r
208                 if( err_no == WSANO_RECOVERY )\r
209                         printf("already installed\n");\r
210                 else\r
211                         printf("WSCInstallProvider failed: %d\n", err_no);\r
212                         rc = err_no;\r
213         } else {\r
214                 printf("successful\n");\r
215                 rc = 0;\r
216         }\r
217         return rc;\r
218 }\r
219 \r
220 /*\r
221  * Function: remove_provider\r
222  *   Description: removes our provider.\r
223  */\r
224 static int remove_provider(void)\r
225 {\r
226         int rc=0, err_no, rc1;\r
227 \r
228         /* Remove from the catalog */\r
229         printf("\nRemoving %s provider: ", provider_name[index]);\r
230         rc1 = WSCDeinstallProvider(&provider_guid[index], &err_no);\r
231         if (rc1 == SOCKET_ERROR) {\r
232                 printf ("WSCDeinstallProvider failed: %d\n", err_no);\r
233                 rc1 = err_no;\r
234         } else {\r
235                 printf ("successful\n");\r
236                 rc1 = 0;\r
237         }\r
238 \r
239 #ifdef _WIN64\r
240         /* Remove from the 32-bit catalog too! */\r
241         printf("Removing 32-bit %s provider: ", provider_name[index]);\r
242         rc = WSCDeinstallProvider32(&provider_guid[index], &err_no);\r
243         if (rc == SOCKET_ERROR) {\r
244                 printf ("WSCDeinstallProvider32 failed: %d\n", err_no);\r
245                 rc = err_no;\r
246         } else {\r
247                 printf ("successful\n");\r
248                 rc = 0;\r
249         }\r
250 #endif  /* _WIN64 */\r
251         if ( rc || rc1 )\r
252                 return (rc ? rc : rc1);\r
253         return 0;\r
254 }\r
255 \r
256 static int get_prov_index(char *name)\r
257 {\r
258         int i;\r
259 \r
260         for (i = 0; i < MAX_INDEX; i++) {\r
261                 if (!_stricmp(provider_name[i], name)) {\r
262                         index = i;\r
263                         return 0;\r
264                 }\r
265         }\r
266         return -1;\r
267 }\r
268 \r
269 int __cdecl main (int argc, char *argv[])\r
270 {\r
271         int ret=0, op;\r
272         int install = 0, remove = 0, debug = 0;\r
273         WSADATA wsd;\r
274         char *prov;\r
275 \r
276         while ((op = getopt(argc, argv, "i:r:dl")) != -1) {\r
277                 switch (op) {\r
278                 case 'i':\r
279                         if (install || remove || get_prov_index(optarg)) {\r
280                                 goto usage;\r
281                         }\r
282                         install = 1;\r
283                         break;\r
284                 case 'r':\r
285                         if (install || remove || get_prov_index(optarg)) {\r
286                                 goto usage;\r
287                         }\r
288                         remove = 1;\r
289                         break;\r
290                 case 'd':\r
291                         debug = 1;\r
292                         break;\r
293                 case 'l':\r
294                         break;\r
295                 default:\r
296                         goto usage;\r
297                 }\r
298         }\r
299 \r
300         index += debug;\r
301 \r
302         if (WSAStartup (MAKEWORD (2, 2), &wsd) != 0) {\r
303                 printf ("InstallSP: Unable to load Winsock: %d\n", GetLastError ());\r
304                 return -1;\r
305         }\r
306 \r
307         if (LOBYTE (wsd.wVersion) != 2 || HIBYTE (wsd.wVersion) != 2) {\r
308                 WSACleanup ();\r
309                 printf("InstallSP: Unable to find a usable version of Winsock DLL\n");\r
310                 ret = -1;\r
311                 goto exit;\r
312         }\r
313 \r
314         if (install) {\r
315                 ret = install_provider();\r
316         } else if (remove) {\r
317                 ret = remove_provider();\r
318         }\r
319 \r
320         print_providers();\r
321 \r
322 exit:\r
323         WSACleanup();\r
324         return ret;\r
325 \r
326 usage:\r
327         show_usage(argv[0]);\r
328         return -1;\r
329 }\r