Set property svn:keywords "id" on all repository
[mirror/winof/.git] / tests / ibat / user / PrintIp.c
1 /*\r
2 * Copyright (c) 2005 Mellanox Technologies.  All rights reserved.\r
3 * Copyright (c) 2005 SilverStorm Technologies.  All rights reserved.\r
4 *\r
5 * This software is available to you under the OpenIB.org BSD license\r
6 * below:\r
7 *\r
8 *     Redistribution and use in source and binary forms, with or\r
9 *     without modification, are permitted provided that the following\r
10 *     conditions are met:\r
11 *\r
12 *      - Redistributions of source code must retain the above\r
13 *        copyright notice, this list of conditions and the following\r
14 *        disclaimer.\r
15 *\r
16 *      - Redistributions in binary form must reproduce the above\r
17 *        copyright notice, this list of conditions and the following\r
18 *        disclaimer in the documentation and/or other materials\r
19 *        provided with the distribution.\r
20 *\r
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\r
25 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\r
26 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
28 * SOFTWARE.\r
29 *\r
30 * $Id$\r
31 */\r
32 \r
33 \r
34 #include <windows.h>\r
35 #include <devioctl.h>\r
36 #include <stdio.h>\r
37 #include <Iphlpapi.h>\r
38 #include "iba\ib_types.h"\r
39 #include <iba\ib_at_ioctl.h>\r
40 \r
41 \r
42 // Print all ips that are related to infiniband on this computer\r
43 int print_ips()\r
44 {\r
45         HANDLE hKernelLib;\r
46         HRESULT hr = S_OK;\r
47         char temp [1000];\r
48         char temp1 [1000];\r
49         IOCTL_IBAT_PORTS_IN ipoib_ports_in;\r
50         IOCTL_IBAT_PORTS_OUT *p_ipoib_ports_out;\r
51         IBAT_PORT_RECORD *ports_records;\r
52 \r
53         IOCTL_IBAT_IP_ADDRESSES_IN addresses_in;\r
54         IOCTL_IBAT_IP_ADDRESSES_OUT *addresses_out;\r
55         IP_ADDRESS       *ip_addreses;\r
56 \r
57         BOOL ret;\r
58         int i,j;\r
59         DWORD BytesReturned = 0;\r
60 \r
61         printf("Adapters that are known to the ipoib modules are:\n\n");\r
62 \r
63         hKernelLib =\r
64                 CreateFileW(\r
65                 IBAT_WIN32_NAME,\r
66                 GENERIC_READ | GENERIC_WRITE,\r
67                 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode none\r
68                 NULL,                               // no security\r
69                 OPEN_EXISTING,\r
70                 FILE_ATTRIBUTE_NORMAL,\r
71                 NULL                                // no template\r
72                 );\r
73 \r
74         if (hKernelLib == INVALID_HANDLE_VALUE) {\r
75                 hr = HRESULT_FROM_WIN32(GetLastError());\r
76                 printf("failed to open the kernel device hr=0x%x\n", hr);\r
77                 return 1;\r
78         }\r
79 \r
80         ipoib_ports_in.Version = IBAT_IOCTL_VERSION;\r
81 \r
82         p_ipoib_ports_out = (IOCTL_IBAT_PORTS_OUT *)temp;\r
83 \r
84         ret = DeviceIoControl(\r
85                 hKernelLib,\r
86                 IOCTL_IBAT_PORTS,\r
87                 &ipoib_ports_in,\r
88                 sizeof(ipoib_ports_in),\r
89                 p_ipoib_ports_out,\r
90                 sizeof(temp),\r
91                 &BytesReturned,\r
92                 NULL\r
93                 );\r
94 \r
95         if (ret == 0) {\r
96                 hr = HRESULT_FROM_WIN32(GetLastError());\r
97                 printf("DeviceIoControl failed for IOCTL_IBAT_PORTS hr=0x%x\n", hr);\r
98                 return 1;\r
99         }\r
100         if (p_ipoib_ports_out->Size > sizeof(temp)) {\r
101                 printf("Data truncated, please call again with a buffer of %d bytes", p_ipoib_ports_out->Size);\r
102         }\r
103 \r
104         ports_records = p_ipoib_ports_out->Ports;\r
105         printf("Number of devices %d\n", p_ipoib_ports_out->NumPorts);\r
106         for (i = 0 ; i < p_ipoib_ports_out->NumPorts; i++)\r
107         {\r
108                 printf("%d: ca guid = 0x%I64x port guid=0x%I64x\n", i, CL_NTOH64(ports_records[i].CaGuid), CL_NTOH64(ports_records[i].PortGuid));\r
109 \r
110                 // print the ip adresses of this port\r
111                 addresses_in.Version = IBAT_IOCTL_VERSION;\r
112                 addresses_in.PortGuid = ports_records[i].PortGuid;\r
113 \r
114                 addresses_out = (IOCTL_IBAT_IP_ADDRESSES_OUT *)temp1;\r
115 \r
116                 ret = DeviceIoControl(\r
117                         hKernelLib,\r
118                         IOCTL_IBAT_IP_ADDRESSES,\r
119                         &addresses_in,\r
120                         sizeof(addresses_in),\r
121                         addresses_out,\r
122                         sizeof(temp1),\r
123                         &BytesReturned,\r
124                         NULL\r
125                         );\r
126 \r
127                 if (ret == 0)\r
128                 {\r
129                         hr = HRESULT_FROM_WIN32(GetLastError());\r
130                         printf("DeviceIoControl failed for IOCTL_IBAT_IP_ADDRESSES hr=0x%x\n", hr);\r
131                         return 1;\r
132                 }\r
133                 if (addresses_out->Size > sizeof(temp1) )\r
134                 {\r
135                         printf("Data truncated, please call again with a buffer of %d bytes", p_ipoib_ports_out->Size);\r
136                         return 1;\r
137                 }\r
138 \r
139                 printf("   found %d ips:", addresses_out->AddressCount);\r
140                 ip_addreses = addresses_out->Address;\r
141                 for (j = 0; j < addresses_out->AddressCount; j++)\r
142                 {\r
143                         printf("    %d.%d.%d.%d   ",\r
144                                 ip_addreses[j].Address[12],\r
145                                 ip_addreses[j].Address[13],\r
146                                 ip_addreses[j].Address[14],\r
147                                 ip_addreses[j].Address[15]);\r
148                 }\r
149                 printf("\n");\r
150         }\r
151 \r
152         return 0;\r
153 };\r
154 \r
155 void print_usage(char *argv[])\r
156 {\r
157         printf("This program is used to print ip adapters and their addresses or to do arp\n");\r
158         printf("Usage is: %s <print_ips> \n",argv[0]);\r
159         printf("or %s <remoteip> <ip>  (for example %s remoteip 1.2.3.4)\n", argv[0],argv[0]);\r
160 }\r
161 \r
162 int remote_ip(char *remote_ip)\r
163 {\r
164         HANDLE hKernelLib;\r
165         HRESULT hr = S_OK;\r
166         IPAddr ip;\r
167         char *pIp = (char *)&ip;\r
168         int b1,b2,b3,b4;\r
169         DWORD  ret;\r
170         IOCTL_IBAT_MAC_TO_GID_IN mac;\r
171         IOCTL_IBAT_MAC_TO_GID_OUT gid;\r
172         DWORD BytesReturned = 0;\r
173 \r
174         ULONG pMacAddr[2], PhyAddrLen ;\r
175         unsigned char *pMac = (unsigned char *)&pMacAddr;\r
176         PhyAddrLen = sizeof(pMacAddr);\r
177 \r
178         hKernelLib =\r
179                 CreateFileW(\r
180                 IBAT_WIN32_NAME,\r
181                 GENERIC_READ | GENERIC_WRITE,\r
182                 FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode none\r
183                 NULL,                               // no security\r
184                 OPEN_EXISTING,\r
185                 FILE_ATTRIBUTE_NORMAL,\r
186                 NULL                                // no template\r
187                 );\r
188 \r
189         if (hKernelLib == INVALID_HANDLE_VALUE)\r
190         {\r
191                 hr = HRESULT_FROM_WIN32(GetLastError());\r
192                 printf("failed to open the kernel device hr=0x%x\n", hr);\r
193                 return 1;\r
194         }\r
195 \r
196         sscanf(remote_ip, "%d.%d.%d.%d", &b1, &b2, &b3, &b4);\r
197         printf("Calling arp for addresses %d.%d.%d.%d\n", b1, b2, b3, b4);\r
198 \r
199         pIp[0] = (char)b1;\r
200         pIp[1] = (char)b2;\r
201         pIp[2] = (char)b3;\r
202         pIp[3] = (char)b4;\r
203 \r
204         ret = SendARP(ip ,0 ,pMacAddr, &PhyAddrLen );\r
205         if (ret != NO_ERROR)\r
206         {\r
207                 printf("Error in SendARP");\r
208                 return 1;\r
209         }\r
210 \r
211         printf("Mac of the remote addresses is %x-%x-%x-%x-%x-%x\n",\r
212                 pMac[0], pMac[1], pMac[2], pMac[3], pMac[4], pMac[5] );\r
213 \r
214         // query for the gid\r
215         memcpy(mac.DestMac, pMac, 6);\r
216 \r
217         ret = DeviceIoControl(\r
218                 hKernelLib,\r
219                 IOCTL_IBAT_MAC_TO_GID,\r
220                 &mac,\r
221                 sizeof(mac),\r
222                 &gid,\r
223                 sizeof(gid),\r
224                 &BytesReturned,\r
225                 NULL );\r
226 \r
227         if (ret == 0)\r
228         {\r
229                 hr = HRESULT_FROM_WIN32(GetLastError());\r
230                 printf("DeviceIoControl failed for IOCTL_IBAT_IP_ADDRESSES hr=0x%x\n", hr);\r
231         }\r
232 \r
233         printf("lid of remote ip is = 0x%I64x : 0x%I64x\n", CL_NTOH64(gid.DestGid.unicast.prefix), CL_NTOH64(gid.DestGid.unicast.interface_id));\r
234 \r
235         return 0;\r
236 }\r
237 \r
238 \r
239 int __cdecl main(int argc, char *argv[])\r
240 {\r
241         if (argc < 2) {\r
242                 print_usage(argv);\r
243                 return 1;\r
244         }\r
245         if (!strcmp(argv[1], "print_ips")) {\r
246                 return print_ips();\r
247         }\r
248         if (!strcmp(argv[1], "remoteip")) {\r
249                 return remote_ip(argv[2]);\r
250         }\r
251         print_usage(argv);\r
252         return 1;\r
253 }\r