only display host info once when more than one adapter is present.
[reactos.git] / reactos / apps / utils / net / ipconfig / ipconfig.c
1 /*
2 * ReactOS Win32 Applications
3 * Copyright (C) 2005 ReactOS Team
4 *
5 * COPYRIGHT: See COPYING in the top level directory
6 * PROJECT: ReactOS arp utility
7 * FILE: apps/utils/net/ipconfig/ipconfig.c
8 * PURPOSE:
9 * PROGRAMMERS: Ged Murphy (gedmurphy@gmail.com)
10 * REVISIONS:
11 * GM 14/09/05 Created
12 *
13 */
14 /*
15 * TODO:
16 * fix renew / release
17 * implement flushdns, registerdns, displaydns, showclassid, setclassid
18 * allow globbing on adapter names
19 */
20 #define WIN32_LEAN_AND_MEAN
21 #include <windows.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <time.h>
25 #include <tchar.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <winsock2.h>
29 #include <iphlpapi.h>
30
31 #define UNICODE
32 #define _UNICODE
33
34
35
36 LPCTSTR GetNodeTypeName(UINT NodeType)
37 {
38 switch (NodeType) {
39 case 1: return _T("Broadcast");
40 case 2: return _T("Peer To Peer");
41 case 4: return _T("Mixed");
42 case 8: return _T("Hybrid");
43 default : return _T("unknown");
44 }
45 }
46
47 LPCTSTR GetInterfaceTypeName(UINT InterfaceType)
48 {
49 switch (InterfaceType) {
50 case MIB_IF_TYPE_OTHER: return _T("Other Type Of Adapter");
51 case MIB_IF_TYPE_ETHERNET: return _T("Ethernet Adapter");
52 case MIB_IF_TYPE_TOKENRING: return _T("Token Ring Adapter");
53 case MIB_IF_TYPE_FDDI: return _T("FDDI Adapter");
54 case MIB_IF_TYPE_PPP: return _T("PPP Adapter");
55 case MIB_IF_TYPE_LOOPBACK: return _T("Loopback Adapter");
56 case MIB_IF_TYPE_SLIP: return _T("SLIP Adapter");
57 default: return _T("unknown");
58 }
59 }
60
61 /* print MAC address */
62 PTCHAR PrintMacAddr(PBYTE Mac)
63 {
64 static TCHAR MacAddr[20];
65
66 _stprintf(MacAddr, _T("%02x-%02x-%02x-%02x-%02x-%02x"),
67 Mac[0], Mac[1], Mac[2], Mac[3], Mac[4], Mac[5]);
68
69 return MacAddr;
70 }
71
72 DWORD DoFormatMessage(DWORD ErrorCode)
73 {
74 LPVOID lpMsgBuf;
75 DWORD RetVal;
76
77 if ((RetVal = FormatMessage(
78 FORMAT_MESSAGE_ALLOCATE_BUFFER |
79 FORMAT_MESSAGE_FROM_SYSTEM |
80 FORMAT_MESSAGE_IGNORE_INSERTS,
81 NULL,
82 ErrorCode,
83 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
84 (LPTSTR) &lpMsgBuf,
85 0,
86 NULL ))) {
87 _tprintf(_T("%s"), (LPTSTR)lpMsgBuf);
88
89 LocalFree(lpMsgBuf);
90 /* return number of TCHAR's stored in output buffer
91 * excluding '\0' - as FormatMessage does*/
92 return RetVal;
93 }
94 else
95 return 0;
96 }
97
98 INT ShowInfo(BOOL bAll)
99 {
100 PIP_ADAPTER_INFO pAdapterInfo = NULL;
101 PIP_ADAPTER_INFO pAdapter = NULL;
102 ULONG adaptOutBufLen;
103
104 PFIXED_INFO pFixedInfo;
105 ULONG netOutBufLen;
106 PIP_ADDR_STRING pIPAddr = NULL;
107
108 DWORD ErrRet = 0;
109
110 /* assign memory for call to GetNetworkParams */
111 pFixedInfo = (FIXED_INFO *) GlobalAlloc( GPTR, sizeof( FIXED_INFO ) );
112 netOutBufLen = sizeof(FIXED_INFO);
113
114 /* assign memory for call to GetAdapterInfo */
115 pAdapterInfo = (IP_ADAPTER_INFO *) malloc( sizeof(IP_ADAPTER_INFO) );
116 adaptOutBufLen = sizeof(IP_ADAPTER_INFO);
117
118 /* set required buffer size */
119 if(GetNetworkParams(pFixedInfo, &netOutBufLen) == ERROR_BUFFER_OVERFLOW)
120 {
121 GlobalFree(pFixedInfo);
122 pFixedInfo = (FIXED_INFO *) GlobalAlloc(GPTR, netOutBufLen);
123 }
124
125 /* set required buffer size */
126 if (GetAdaptersInfo( pAdapterInfo, &adaptOutBufLen) == ERROR_BUFFER_OVERFLOW)
127 {
128 free(pAdapterInfo);
129 pAdapterInfo = (IP_ADAPTER_INFO *) malloc (adaptOutBufLen);
130 }
131
132 if ((ErrRet = GetAdaptersInfo(pAdapterInfo, &adaptOutBufLen)) != NO_ERROR)
133 {
134 _tprintf(_T("GetAdaptersInfo failed : "));
135 DoFormatMessage(ErrRet);
136 return EXIT_FAILURE;
137 }
138
139 if ((ErrRet = GetNetworkParams(pFixedInfo, &netOutBufLen)) != NO_ERROR)
140 {
141 _tprintf(_T("GetNetworkParams failed : "));
142 DoFormatMessage(ErrRet);
143 return EXIT_FAILURE;
144 }
145
146 pAdapter = pAdapterInfo;
147 //HKEY hKey;
148 //LPCTSTR lpSubKey = _T("SYSTEM\\ControlSet\\Control\\Network");
149
150 _tprintf(_T("\nReactOS IP Configuration\n\n"));
151
152
153 if (bAll)
154 {
155 _tprintf(_T("\tHost Name . . . . . . . . . . . . : %s\n"), pFixedInfo->HostName);
156 _tprintf(_T("\tPrimary DNS Suffix. . . . . . . . : \n"));
157 _tprintf(_T("\tNode Type . . . . . . . . . . . . : %s\n"), GetNodeTypeName(pFixedInfo->NodeType));
158 if (pFixedInfo->EnableRouting)
159 _tprintf(_T("\tIP Routing Enabled. . . . . . . . : Yes\n"));
160 else
161 _tprintf(_T("\tIP Routing Enabled. . . . . . . . : No\n"));
162 if (pAdapter->HaveWins)
163 _tprintf(_T("\tWINS Proxy enabled. . . . . . . . : Yes\n"));
164 else
165 _tprintf(_T("\tWINS Proxy enabled. . . . . . . . : No\n"));
166 _tprintf(_T("\tDNS Suffix Search List. . . . . . : %s\n"), pFixedInfo->DomainName);
167 }
168
169 while (pAdapter)
170 {
171
172 _tprintf(_T("\n%s ...... : \n\n"), GetInterfaceTypeName(pAdapter->Type));
173
174 /* check if the adapter is connected to the media */
175 if (_tcscmp(pAdapter->IpAddressList.IpAddress.String, "0.0.0.0") == 0)
176 {
177 _tprintf(_T("\tMedia State . . . . . . . . . . . : Media disconnected\n"));
178 pAdapter = pAdapter->Next;
179 continue;
180 }
181
182 _tprintf(_T("\tConnection-specific DNS Suffix. . : %s\n"), pFixedInfo->DomainName);
183
184 if (bAll)
185 {
186 _tprintf(_T("\tDescription . . . . . . . . . . . : %s\n"), pAdapter->Description);
187 _tprintf(_T("\tPhysical Address. . . . . . . . . : %s\n"), PrintMacAddr(pAdapter->Address));
188 if (pAdapter->DhcpEnabled)
189 _tprintf(_T("\tDHCP Enabled. . . . . . . . . . . : Yes\n"));
190 else
191 _tprintf(_T("\tDHCP Enabled. . . . . . . . . . . : No\n"));
192 _tprintf(_T("\tAutoconfiguration Enabled . . . . : \n"));
193 }
194
195 _tprintf(_T("\tIP Address. . . . . . . . . . . . : %s\n"), pAdapter->IpAddressList.IpAddress.String);
196 _tprintf(_T("\tSubnet Mask . . . . . . . . . . . : %s\n"), pAdapter->IpAddressList.IpMask.String);
197 _tprintf(_T("\tDefault Gateway . . . . . . . . . : %s\n"), pAdapter->GatewayList.IpAddress.String);
198
199 if (bAll)
200 {
201 if (pAdapter->DhcpEnabled)
202 _tprintf(_T("\tDHCP Server . . . . . . . . . . . : %s\n"), pAdapter->DhcpServer.IpAddress.String);
203
204 _tprintf(_T("\tDNS Servers . . . . . . . . . . . : "));
205 _tprintf(_T("%s\n"), pFixedInfo->DnsServerList.IpAddress.String);
206 pIPAddr = pFixedInfo -> DnsServerList.Next;
207 while (pIPAddr)
208 {
209 _tprintf(_T("\t\t\t\t\t %s\n"), pIPAddr ->IpAddress.String );
210 pIPAddr = pIPAddr ->Next;
211 }
212 if (pAdapter->HaveWins)
213 {
214 _tprintf(_T("\tPrimary WINS Server . . . . . . . : %s\n"), pAdapter->PrimaryWinsServer.IpAddress.String);
215 _tprintf(_T("\tSecondard WINS Server . . . . . . : %s\n"), pAdapter->SecondaryWinsServer.IpAddress.String);
216 }
217 if (pAdapter->DhcpEnabled)
218 {
219 _tprintf(_T("\tLease Obtained. . . . . . . . . . : %s"), _tasctime(localtime(&pAdapter->LeaseObtained)));
220 _tprintf(_T("\tLease Expires . . . . . . . . . . : %s"), _tasctime(localtime(&pAdapter->LeaseExpires)));
221 }
222 }
223 _tprintf(_T("\n"));
224
225 pAdapter = pAdapter->Next;
226
227 }
228
229 return 0;
230 }
231
232 INT Release(TCHAR Index)
233 {
234 IP_ADAPTER_INDEX_MAP AdapterInfo;
235 DWORD dwRetVal = 0;
236
237 /* if interface is not given, query GetInterfaceInfo */
238 if (Index == (TCHAR)NULL)
239 {
240 PIP_INTERFACE_INFO pInfo;
241 ULONG ulOutBufLen;
242 pInfo = (IP_INTERFACE_INFO *) malloc(sizeof(IP_INTERFACE_INFO));
243 ulOutBufLen = 0;
244
245 /* Make an initial call to GetInterfaceInfo to get
246 * the necessary size into the ulOutBufLen variable */
247 if ( GetInterfaceInfo(pInfo, &ulOutBufLen) == ERROR_INSUFFICIENT_BUFFER)
248 {
249 GlobalFree(pInfo);
250 pInfo = (IP_INTERFACE_INFO *) malloc (ulOutBufLen);
251 }
252
253 /* Make a second call to GetInterfaceInfo to get the actual data we want */
254 if ((dwRetVal = GetInterfaceInfo(pInfo, &ulOutBufLen)) == NO_ERROR )
255 {
256 AdapterInfo = pInfo->Adapter[0];
257 _tprintf(_T("name - %S\n"), pInfo->Adapter[0].Name);
258 }
259 else
260 {
261 _tprintf(_T("\nGetInterfaceInfo failed : "));
262 DoFormatMessage(dwRetVal);
263 }
264 }
265 else
266 {
267 ;
268 /* we need to be able to release connections by name with support for globbing
269 * i.e. ipconfig /release Eth* will release all cards starting with Eth...
270 * ipconfig /release *con* will release all cards with 'con' in their name
271 */
272 }
273
274
275 /* Call IpReleaseAddress to release the IP address on the specified adapter. */
276 if ((dwRetVal = IpReleaseAddress(&AdapterInfo)) != NO_ERROR)
277 {
278 _tprintf(_T("\nAn error occured while releasing interface %s : "), _T("*name*"));
279 DoFormatMessage(dwRetVal);
280 }
281 return 0;
282 }
283
284
285
286
287 INT Renew(TCHAR Index)
288 {
289 IP_ADAPTER_INDEX_MAP AdapterInfo;
290 DWORD dwRetVal = 0;
291
292 /* if interface is not given, query GetInterfaceInfo */
293 if (Index == (TCHAR)NULL)
294 {
295 PIP_INTERFACE_INFO pInfo;
296 ULONG ulOutBufLen;
297 pInfo = (IP_INTERFACE_INFO *) malloc(sizeof(IP_INTERFACE_INFO));
298 ulOutBufLen = 0;
299
300 /* Make an initial call to GetInterfaceInfo to get
301 * the necessary size into the ulOutBufLen variable */
302 if ( GetInterfaceInfo(pInfo, &ulOutBufLen) == ERROR_INSUFFICIENT_BUFFER)
303 {
304 GlobalFree(pInfo);
305 pInfo = (IP_INTERFACE_INFO *) malloc (ulOutBufLen);
306 }
307
308 /* Make a second call to GetInterfaceInfo to get the actual data we want */
309 if ((dwRetVal = GetInterfaceInfo(pInfo, &ulOutBufLen)) == NO_ERROR )
310 {
311 AdapterInfo = pInfo->Adapter[0];
312 _tprintf(_T("name - %S\n"), pInfo->Adapter[0].Name);
313 } else {
314 _tprintf(_T("\nGetInterfaceInfo failed : "));
315 DoFormatMessage(dwRetVal);
316 }
317 }
318 else
319 {
320 ;
321 /* we need to be able to renew connections by name with support for globbing
322 * i.e. ipconfig /renew Eth* will renew all cards starting with Eth...
323 * ipconfig /renew *con* will renew all cards with 'con' in their name
324 */
325 }
326
327
328 /* Call IpRenewAddress to renew the IP address on the specified adapter. */
329 if ((dwRetVal = IpRenewAddress(&AdapterInfo)) != NO_ERROR)
330 {
331 _tprintf(_T("\nAn error occured while renew interface %s : "), _T("*name*"));
332 DoFormatMessage(dwRetVal);
333 }
334 return 0;
335 }
336
337 /* temp func for testing purposes */
338 VOID Info()
339 {
340 // Declare and initialize variables
341 PIP_INTERFACE_INFO pInfo;
342 ULONG ulOutBufLen;
343 DWORD dwRetVal;
344
345 pInfo = (IP_INTERFACE_INFO *) malloc( sizeof(IP_INTERFACE_INFO) );
346 ulOutBufLen = sizeof(IP_INTERFACE_INFO);
347 dwRetVal = 0;
348
349
350 // Make an initial call to GetInterfaceInfo to get
351 // the necessary size in the ulOutBufLen variable
352 if ( GetInterfaceInfo(pInfo, &ulOutBufLen) == ERROR_INSUFFICIENT_BUFFER)
353 {
354 free(pInfo);
355 pInfo = (IP_INTERFACE_INFO *) malloc (ulOutBufLen);
356 }
357
358 // Make a second call to GetInterfaceInfo to get
359 // the actual data we need
360 if ((dwRetVal = GetInterfaceInfo(pInfo, &ulOutBufLen)) == NO_ERROR )
361 {
362 int i;
363 for (i=0; i<pInfo->NumAdapters; i++)
364 {
365 printf("\tAdapter Name: %S\n", pInfo->Adapter[i].Name);
366 printf("\tAdapter Index: %ld\n", pInfo->Adapter[i].Index);
367 printf("\tNum Adapters: %ld\n", pInfo->NumAdapters);
368 }
369 }
370 else
371 {
372 printf("GetInterfaceInfo failed.\n");
373 DoFormatMessage(dwRetVal);
374 }
375 }
376
377
378 VOID Usage(VOID)
379 {
380 _tprintf(_T("\nUSAGE:\n"
381 " ipconfig [/? | /all | /renew [adapter] | /release [adapter] |\n"
382 " /flushdns | /displaydns | /registerdns |\n"
383 " /showclassid adapter |\n"
384 " /setclassid adapter [classid] ]\n"
385 "\n"
386 "where\n"
387 " adapter Connection name\n"
388 " (wildcard characters * and ? allowed, see examples)\n"
389 "\n"
390 " Options:\n"
391 " /? Display this help message\n"
392 " /all Display full configuration information.\n"
393 " /release Release the IP address for the specified adapter.\n"
394 " /renew Renew the IP address for the specified adapter.\n"
395 " /flushdns Purges the DNS Resolver cache.\n"
396 " /registerdns Refreshes all DHCP leases and re-registers DNS names.\n"
397 " /displaydns Display the contents of the DNS Resolver Cache.\n"
398 " /showclassid Displays all the dhcp class IDs allowed for adapter.\n"
399 " /setclassid Modifies the dhcp class id.\n"
400 "\n"
401 "The default is to display only the IP address, subnet mask and\n"
402 "default gateway for each adapter bound to TCP/IP.\n"
403 "\n"
404 "For Release and Renew, if no adapter name is specified, then the IP address\n"
405 "leases for all adapters bound to TCP/IP will be released or renewed.\n"
406 "\n"
407 "For Setclassid, if no ClassId is specified, then the ClassId is removed.\n"
408 "\n"
409 "Examples:\n"
410 " > ipconfig ... Show information.\n"
411 " > ipconfig /all ... Show detailed information\n"
412 " > ipconfig /renew ... renew all adapters\n"
413 " > ipconfig /renew EL* ... renew any connection that has its\n"
414 " name starting with EL\n"
415 " > ipconfig /release *Con* ... release all matching connections,\n"
416 " eg. \"Local Area Connection 1\" or\n"
417 " \"Local Area Connection 2\"\n"));
418 }
419
420 int main(int argc, char *argv[])
421 {
422 BOOL DoUsage=FALSE;
423 BOOL DoAll=FALSE;
424 BOOL DoRelease=FALSE;
425 BOOL DoRenew=FALSE;
426 BOOL DoFlushdns=FALSE;
427 BOOL DoRegisterdns=FALSE;
428 BOOL DoDisplaydns=FALSE;
429 BOOL DoShowclassid=FALSE;
430 BOOL DoSetclassid=FALSE;
431
432 /* Parse command line for options we have been given. */
433 if ( (argc > 1)&&(argv[1][0]=='/') )
434 {
435 if( !_tcsicmp( &argv[1][1], _T("?") ))
436 {
437 DoUsage = TRUE;
438 }
439 else if( !_tcsnicmp( &argv[1][1], _T("ALL"), _tcslen(&argv[1][1]) ))
440 {
441 DoAll = TRUE;
442 }
443 else if( !_tcsnicmp( &argv[1][1], _T("RELEASE"), _tcslen(&argv[1][1]) ))
444 {
445 DoRelease = TRUE;
446 }
447 else if( ! _tcsnicmp( &argv[1][1], _T("RENEW"), _tcslen(&argv[1][1]) ))
448 {
449 DoRenew = TRUE;
450 }
451 else if( ! _tcsnicmp( &argv[1][1], _T("FLUSHDNS"), _tcslen(&argv[1][1]) ))
452 {
453 DoFlushdns = TRUE;
454 }
455 else if( ! _tcsnicmp( &argv[1][1], _T("FLUSHREGISTERDNS"), _tcslen(&argv[1][1]) ))
456 {
457 DoRegisterdns = TRUE;
458 }
459 else if( ! _tcsnicmp( &argv[1][1], _T("DISPLAYDNS"), _tcslen(&argv[1][1]) ))
460 {
461 DoDisplaydns = TRUE;
462 }
463 else if( ! _tcsnicmp( &argv[1][1], _T("SHOWCLASSID"), _tcslen(&argv[1][1]) ))
464 {
465 DoShowclassid = TRUE;
466 }
467 else if( ! _tcsnicmp( &argv[1][1], _T("SETCLASSID"), _tcslen(&argv[1][1]) ))
468 {
469 DoSetclassid = TRUE;
470 }
471 }
472
473 switch (argc)
474 {
475 case 1: /* Default behaviour if no options are given*/
476 ShowInfo(FALSE);
477 break;
478 case 2: /* Process all the options that take no paramiters */
479 if (DoUsage)
480 Usage();
481 else if (DoAll)
482 ShowInfo(TRUE);
483 else if (DoRelease)
484 Release((TCHAR)NULL);
485 else if (DoRenew)
486 Renew((TCHAR)NULL);
487 else if (DoFlushdns)
488 _tprintf(_T("\nSorry /flushdns is not implemented yet\n"));
489 else if (DoRegisterdns)
490 _tprintf(_T("\nSorry /registerdns is not implemented yet\n"));
491 else if (DoDisplaydns)
492 _tprintf(_T("\nSorry /displaydns is not implemented yet\n"));
493 else
494 Usage();
495 break;
496 case 3: /* Process all the options that can have 1 paramiters */
497 if (DoRelease)
498 _tprintf(_T("\nSorry /release [adapter] is not implemented yet\n"));
499 //Release(argv[2]);
500 else if (DoRenew)
501 _tprintf(_T("\nSorry /renew [adapter] is not implemented yet\n"));
502 else if (DoShowclassid)
503 _tprintf(_T("\nSorry /showclassid adapter is not implemented yet\n"));
504 else if (DoSetclassid)
505 _tprintf(_T("\nSorry /setclassid adapter is not implemented yet\n"));
506 else
507 Usage();
508 break;
509 case 4: /* Process all the options that can have 2 paramiters */
510 if (DoSetclassid)
511 _tprintf(_T("\nSorry /setclassid adapter [classid]is not implemented yet\n"));
512 else
513 Usage();
514 break;
515 default:
516 Usage();
517 }
518
519 return 0;
520 }