2 * PROJECT: ReactOS ipconfig utility
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: apps/utils/net/ipconfig/ipconfig.c
5 * PURPOSE: Display IP info for net adapters
6 * PROGRAMMERS: Copyright 2005 - 2006 Ged Murphy (gedmurphy@gmail.com)
11 * implement flushdns, registerdns, displaydns, showclassid, setclassid
12 * allow globbing on adapter names
15 #define WIN32_LEAN_AND_MEAN
29 LPTSTR
GetNodeTypeName(UINT NodeType
)
31 static TCHAR szNode
[14];
36 if (!LoadString(hInstance
, IDS_BCAST
, szNode
, sizeof(szNode
)))
41 if (!LoadString(hInstance
, IDS_P2P
, szNode
, sizeof(szNode
)))
46 if (!LoadString(hInstance
, IDS_MIXED
, szNode
, sizeof(szNode
)))
51 if (!LoadString(hInstance
, IDS_HYBRID
, szNode
, sizeof(szNode
)))
56 if (!LoadString(hInstance
, IDS_UNKNOWN
, szNode
, sizeof(szNode
)))
65 LPTSTR
GetInterfaceTypeName(UINT InterfaceType
)
67 static TCHAR szIntType
[25];
69 switch (InterfaceType
)
71 case MIB_IF_TYPE_OTHER
:
72 if (!LoadString(hInstance
, IDS_OTHER
, szIntType
, sizeof(szIntType
)))
76 case MIB_IF_TYPE_ETHERNET
:
77 if (!LoadString(hInstance
, IDS_ETH
, szIntType
, sizeof(szIntType
)))
81 case MIB_IF_TYPE_TOKENRING
:
82 if (!LoadString(hInstance
, IDS_TOKEN
, szIntType
, sizeof(szIntType
)))
86 case MIB_IF_TYPE_FDDI
:
87 if (!LoadString(hInstance
, IDS_FDDI
, szIntType
, sizeof(szIntType
)))
92 if (!LoadString(hInstance
, IDS_PPP
, szIntType
, sizeof(szIntType
)))
96 case MIB_IF_TYPE_LOOPBACK
:
97 if (!LoadString(hInstance
, IDS_LOOP
, szIntType
, sizeof(szIntType
)))
101 case MIB_IF_TYPE_SLIP
:
102 if (!LoadString(hInstance
, IDS_SLIP
, szIntType
, sizeof(szIntType
)))
107 if (!LoadString(hInstance
, IDS_UNKNOWN
, szIntType
, sizeof(szIntType
)))
116 /* print MAC address */
117 PTCHAR
PrintMacAddr(PBYTE Mac
)
119 static TCHAR MacAddr
[20];
121 _stprintf(MacAddr
, _T("%02x-%02x-%02x-%02x-%02x-%02x"),
122 Mac
[0], Mac
[1], Mac
[2], Mac
[3], Mac
[4], Mac
[5]);
128 VOID
DoFormatMessage(LONG ErrorCode
)
134 ErrorCode
= GetLastError();
136 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|
137 FORMAT_MESSAGE_FROM_SYSTEM
|
138 FORMAT_MESSAGE_IGNORE_INSERTS
,
141 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), /* Default language */
146 _tprintf(_T("%s"), (LPTSTR
)lpMsgBuf
);
152 LPTSTR
GetConnectionType(LPTSTR lpClass
)
155 LPTSTR ConType
= NULL
;
157 LPTSTR PrePath
= _T("SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\");
158 LPTSTR PostPath
= _T("\\Connection");
163 /* don't overflow the buffer */
164 PathSize
= lstrlen(PrePath
) + lstrlen(lpClass
) + lstrlen(PostPath
) + 1;
168 wsprintf(Path
, _T("%s%s%s"), PrePath
, lpClass
, PostPath
);
170 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
174 &hKey
) == ERROR_SUCCESS
)
176 if(RegQueryValueEx(hKey
,
181 &dwDataSize
) == ERROR_SUCCESS
)
183 ConType
= (LPTSTR
)HeapAlloc(ProcessHeap
,
188 if(RegQueryValueEx(hKey
,
193 &dwDataSize
) != ERROR_SUCCESS
)
195 HeapFree(ProcessHeap
,
212 LPTSTR
GetConnectionDescription(LPTSTR lpClass
)
214 HKEY hBaseKey
= NULL
;
215 HKEY hClassKey
= NULL
;
216 LPTSTR lpKeyClass
= NULL
;
217 LPTSTR lpConDesc
= NULL
;
218 LPTSTR lpPath
= NULL
;
219 TCHAR szPrePath
[] = _T("SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}\\");
224 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
228 &hBaseKey
) != ERROR_SUCCESS
)
240 if ((Status
= RegEnumKeyEx(hBaseKey
,
247 NULL
)) != ERROR_SUCCESS
)
249 if (Status
== ERROR_NO_MORE_ITEMS
)
251 DoFormatMessage(Status
);
259 PathSize
= lstrlen(szPrePath
) + lstrlen(szName
) + 1;
260 lpPath
= (LPTSTR
)HeapAlloc(ProcessHeap
,
262 PathSize
* sizeof(TCHAR
));
266 wsprintf(lpPath
, _T("%s%s"), szPrePath
, szName
);
268 //MessageBox(NULL, lpPath, NULL, 0);
270 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
274 &hClassKey
) != ERROR_SUCCESS
)
279 HeapFree(ProcessHeap
, 0, lpPath
);
282 if(RegQueryValueEx(hClassKey
,
283 _T("NetCfgInstanceId"),
287 &dwDataSize
) == ERROR_SUCCESS
)
289 lpKeyClass
= (LPTSTR
)HeapAlloc(ProcessHeap
,
292 if (lpKeyClass
== NULL
)
295 if(RegQueryValueEx(hClassKey
,
296 _T("NetCfgInstanceId"),
300 &dwDataSize
) != ERROR_SUCCESS
)
303 HeapFree(ProcessHeap
, 0, lpKeyClass
);
310 if (!lstrcmp(lpClass
, lpKeyClass
))
312 HeapFree(ProcessHeap
, 0, lpKeyClass
);
315 if(RegQueryValueEx(hClassKey
,
320 &dwDataSize
) == ERROR_SUCCESS
)
322 lpConDesc
= (LPTSTR
)HeapAlloc(ProcessHeap
,
325 if (lpConDesc
== NULL
)
328 if(RegQueryValueEx(hClassKey
,
333 &dwDataSize
) != ERROR_SUCCESS
)
347 if (hBaseKey
!= NULL
)
348 RegCloseKey(hBaseKey
);
349 if (hClassKey
!= NULL
)
350 RegCloseKey(hClassKey
);
351 if (lpConDesc
!= NULL
)
352 HeapFree(ProcessHeap
, 0, lpPath
);
353 if (lpConDesc
!= NULL
)
354 HeapFree(ProcessHeap
, 0, lpKeyClass
);
360 VOID
ShowInfo(BOOL bAll
)
362 PIP_ADAPTER_INFO pAdapterInfo
= NULL
;
363 PIP_ADAPTER_INFO pAdapter
= NULL
;
364 ULONG adaptOutBufLen
= 0;
365 PFIXED_INFO pFixedInfo
= NULL
;
366 ULONG netOutBufLen
= 0;
369 /* call GetAdaptersInfo to obtain the adapter info */
370 ret
= GetAdaptersInfo(pAdapterInfo
, &adaptOutBufLen
);
371 if (ret
== ERROR_BUFFER_OVERFLOW
)
373 pAdapterInfo
= (IP_ADAPTER_INFO
*)HeapAlloc(ProcessHeap
, 0, adaptOutBufLen
);
374 if (pAdapterInfo
== NULL
)
377 ret
= GetAdaptersInfo(pAdapterInfo
, &adaptOutBufLen
);
381 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
387 if( ERROR_NO_DATA
!= ret
)
394 /* call GetNetworkParams to obtain the network info */
395 if(GetNetworkParams(pFixedInfo
, &netOutBufLen
) == ERROR_BUFFER_OVERFLOW
)
397 pFixedInfo
= (FIXED_INFO
*)HeapAlloc(ProcessHeap
, 0, netOutBufLen
);
398 if (pFixedInfo
== NULL
)
401 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
404 if (GetNetworkParams(pFixedInfo
, &netOutBufLen
) != NO_ERROR
)
408 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
409 HeapFree(ProcessHeap
, 0, pFixedInfo
);
416 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
421 pAdapter
= pAdapterInfo
;
423 _tprintf(_T("\nReactOS IP Configuration\n\n"));
426 _tprintf(_T("\tHost Name . . . . . . . . . . . . : %s\n"), pFixedInfo
->HostName
);
427 _tprintf(_T("\tPrimary DNS Suffix. . . . . . . . : \n"));
428 _tprintf(_T("\tNode Type . . . . . . . . . . . . : %s\n"), GetNodeTypeName(pFixedInfo
->NodeType
));
429 if (pFixedInfo
->EnableRouting
)
430 _tprintf(_T("\tIP Routing Enabled. . . . . . . . : Yes\n"));
432 _tprintf(_T("\tIP Routing Enabled. . . . . . . . : No\n"));
433 if (pAdapter
&& pAdapter
->HaveWins
)
434 _tprintf(_T("\tWINS Proxy enabled. . . . . . . . : Yes\n"));
436 _tprintf(_T("\tWINS Proxy enabled. . . . . . . . : No\n"));
437 _tprintf(_T("\tDNS Suffix Search List. . . . . . : %s\n"), pFixedInfo
->DomainName
);
442 LPTSTR IntType
, myConType
;
444 IntType
= GetInterfaceTypeName(pAdapter
->Type
);
445 myConType
= GetConnectionType(pAdapter
->AdapterName
);
447 _tprintf(_T("\n%s %s: \n\n"), IntType
, myConType
);
449 if (myConType
!= NULL
) HeapFree(ProcessHeap
, 0, myConType
);
451 /* check if the adapter is connected to the media */
452 if (_tcscmp(pAdapter
->IpAddressList
.IpAddress
.String
, "0.0.0.0") == 0)
454 _tprintf(_T("\tMedia State . . . . . . . . . . . : Media disconnected\n"));
455 pAdapter
= pAdapter
->Next
;
459 _tprintf(_T("\tConnection-specific DNS Suffix. . : %s\n"), pFixedInfo
->DomainName
);
463 LPTSTR lpDesc
= GetConnectionDescription(pAdapter
->AdapterName
);
464 _tprintf(_T("\tDescription . . . . . . . . . . . : %s\n"), lpDesc
);
465 HeapFree(ProcessHeap
, 0, lpDesc
);
466 _tprintf(_T("\tPhysical Address. . . . . . . . . : %s\n"), PrintMacAddr(pAdapter
->Address
));
467 if (pAdapter
->DhcpEnabled
)
468 _tprintf(_T("\tDHCP Enabled. . . . . . . . . . . : Yes\n"));
470 _tprintf(_T("\tDHCP Enabled. . . . . . . . . . . : No\n"));
471 _tprintf(_T("\tAutoconfiguration Enabled . . . . : \n"));
474 _tprintf(_T("\tIP Address. . . . . . . . . . . . : %s\n"), pAdapter
->IpAddressList
.IpAddress
.String
);
475 _tprintf(_T("\tSubnet Mask . . . . . . . . . . . : %s\n"), pAdapter
->IpAddressList
.IpMask
.String
);
476 if (pAdapter
->GatewayList
.IpAddress
.String
[0] != '0')
477 _tprintf(_T("\tDefault Gateway . . . . . . . . . : %s\n"), pAdapter
->GatewayList
.IpAddress
.String
);
479 _tprintf(_T("\tDefault Gateway . . . . . . . . . :\n"));
483 PIP_ADDR_STRING pIPAddr
;
485 if (pAdapter
->DhcpEnabled
)
486 _tprintf(_T("\tDHCP Server . . . . . . . . . . . : %s\n"), pAdapter
->DhcpServer
.IpAddress
.String
);
488 _tprintf(_T("\tDNS Servers . . . . . . . . . . . : "));
489 _tprintf(_T("%s\n"), pFixedInfo
->DnsServerList
.IpAddress
.String
);
490 pIPAddr
= pFixedInfo
->DnsServerList
.Next
;
493 _tprintf(_T("\t\t\t\t\t %s\n"), pIPAddr
->IpAddress
.String
);
494 pIPAddr
= pIPAddr
->Next
;
497 if (pAdapter
->HaveWins
)
499 _tprintf(_T("\tPrimary WINS Server . . . . . . . : %s\n"), pAdapter
->PrimaryWinsServer
.IpAddress
.String
);
500 _tprintf(_T("\tSecondard WINS Server . . . . . . : %s\n"), pAdapter
->SecondaryWinsServer
.IpAddress
.String
);
503 if (pAdapter
->DhcpEnabled
)
505 _tprintf(_T("\tLease Obtained. . . . . . . . . . : %s"), _tasctime(localtime(&pAdapter
->LeaseObtained
)));
506 _tprintf(_T("\tLease Expires . . . . . . . . . . : %s"), _tasctime(localtime(&pAdapter
->LeaseExpires
)));
511 pAdapter
= pAdapter
->Next
;
515 HeapFree(ProcessHeap
, 0, pFixedInfo
);
517 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
520 VOID
Release(LPTSTR Index
)
522 IP_ADAPTER_INDEX_MAP AdapterInfo
;
526 /* if interface is not given, query GetInterfaceInfo */
529 PIP_INTERFACE_INFO pInfo
= NULL
;
530 ULONG ulOutBufLen
= 0;
532 if (GetInterfaceInfo(pInfo
, &ulOutBufLen
) == ERROR_INSUFFICIENT_BUFFER
)
534 pInfo
= (IP_INTERFACE_INFO
*)HeapAlloc(ProcessHeap
, 0, ulOutBufLen
);
538 if (GetInterfaceInfo(pInfo
, &ulOutBufLen
) == NO_ERROR
)
540 for (i
= 0; i
< pInfo
->NumAdapters
; i
++)
542 CopyMemory(&AdapterInfo
, &pInfo
->Adapter
[i
], sizeof(IP_ADAPTER_INDEX_MAP
));
543 _tprintf(_T("name - %S\n"), pInfo
->Adapter
[i
].Name
);
545 /* Call IpReleaseAddress to release the IP address on the specified adapter. */
546 if ((ret
= IpReleaseAddress(&AdapterInfo
)) != NO_ERROR
)
548 _tprintf(_T("\nAn error occured while releasing interface %S : \n"), AdapterInfo
.Name
);
549 DoFormatMessage(ret
);
553 HeapFree(ProcessHeap
, 0, pInfo
);
558 HeapFree(ProcessHeap
, 0, pInfo
);
572 * we need to be able to release connections by name with support for globbing
573 * i.e. ipconfig /release Eth* will release all cards starting with Eth...
574 * ipconfig /release *con* will release all cards with 'con' in their name
582 VOID
Renew(LPTSTR Index
)
584 IP_ADAPTER_INDEX_MAP AdapterInfo
;
587 /* if interface is not given, query GetInterfaceInfo */
590 PIP_INTERFACE_INFO pInfo
;
591 ULONG ulOutBufLen
= 0;
593 pInfo
= (IP_INTERFACE_INFO
*)HeapAlloc(ProcessHeap
, 0, sizeof(IP_INTERFACE_INFO
));
596 _tprintf(_T("memory allocation error"));
600 /* Make an initial call to GetInterfaceInfo to get
601 * the necessary size into the ulOutBufLen variable */
602 if ( GetInterfaceInfo(pInfo
, &ulOutBufLen
) == ERROR_INSUFFICIENT_BUFFER
)
604 HeapFree(ProcessHeap
, 0, pInfo
);
605 pInfo
= (IP_INTERFACE_INFO
*)HeapAlloc(ProcessHeap
, 0, ulOutBufLen
);
608 _tprintf(_T("memory allocation error"));
613 /* Make a second call to GetInterfaceInfo to get the actual data we want */
614 if (GetInterfaceInfo(pInfo
, &ulOutBufLen
) == NO_ERROR
)
616 for (i
= 0; i
< pInfo
->NumAdapters
; i
++)
618 CopyMemory(&AdapterInfo
, &pInfo
->Adapter
[i
], sizeof(IP_ADAPTER_INDEX_MAP
));
619 _tprintf(_T("name - %S\n"), pInfo
->Adapter
[i
].Name
);
622 /* Call IpRenewAddress to renew the IP address on the specified adapter. */
623 if (IpRenewAddress(&AdapterInfo
) != NO_ERROR
)
625 _tprintf(_T("\nAn error occured while renew interface %s : "), _T("*name*"));
632 _tprintf(_T("\nGetInterfaceInfo failed : "));
636 HeapFree(ProcessHeap
, 0, pInfo
);
642 * we need to be able to renew connections by name with support for globbing
643 * i.e. ipconfig /renew Eth* will renew all cards starting with Eth...
644 * ipconfig /renew *con* will renew all cards with 'con' in their name
657 LPTSTR lpName
= (LPTSTR
)MAKEINTRESOURCE((IDS_USAGE
>> 4) + 1);
659 hRes
= FindResource(hInstance
,
664 if ((Size
= SizeofResource(hInstance
,
667 lpUsage
= (LPTSTR
)HeapAlloc(ProcessHeap
,
673 if (LoadString(hInstance
,
678 _tprintf(_T("%s"), lpUsage
);
686 int main(int argc
, char *argv
[])
690 BOOL DoRelease
=FALSE
;
692 BOOL DoFlushdns
=FALSE
;
693 BOOL DoRegisterdns
=FALSE
;
694 BOOL DoDisplaydns
=FALSE
;
695 BOOL DoShowclassid
=FALSE
;
696 BOOL DoSetclassid
=FALSE
;
698 hInstance
= GetModuleHandle(NULL
);
699 ProcessHeap
= GetProcessHeap();
701 /* Parse command line for options we have been given. */
702 if ( (argc
> 1)&&(argv
[1][0]=='/' || argv
[1][0]=='-') )
704 if( !_tcsicmp( &argv
[1][1], _T("?") ))
708 else if( !_tcsnicmp( &argv
[1][1], _T("ALL"), _tcslen(&argv
[1][1]) ))
712 else if( !_tcsnicmp( &argv
[1][1], _T("RELEASE"), _tcslen(&argv
[1][1]) ))
716 else if( ! _tcsnicmp( &argv
[1][1], _T("RENEW"), _tcslen(&argv
[1][1]) ))
720 else if( ! _tcsnicmp( &argv
[1][1], _T("FLUSHDNS"), _tcslen(&argv
[1][1]) ))
724 else if( ! _tcsnicmp( &argv
[1][1], _T("FLUSHREGISTERDNS"), _tcslen(&argv
[1][1]) ))
726 DoRegisterdns
= TRUE
;
728 else if( ! _tcsnicmp( &argv
[1][1], _T("DISPLAYDNS"), _tcslen(&argv
[1][1]) ))
732 else if( ! _tcsnicmp( &argv
[1][1], _T("SHOWCLASSID"), _tcslen(&argv
[1][1]) ))
734 DoShowclassid
= TRUE
;
736 else if( ! _tcsnicmp( &argv
[1][1], _T("SETCLASSID"), _tcslen(&argv
[1][1]) ))
744 case 1: /* Default behaviour if no options are given*/
747 case 2: /* Process all the options that take no parameters */
757 _tprintf(_T("\nSorry /flushdns is not implemented yet\n"));
758 else if (DoRegisterdns
)
759 _tprintf(_T("\nSorry /registerdns is not implemented yet\n"));
760 else if (DoDisplaydns
)
761 _tprintf(_T("\nSorry /displaydns is not implemented yet\n"));
765 case 3: /* Process all the options that can have 1 parameter */
767 _tprintf(_T("\nSorry /release [adapter] is not implemented yet\n"));
770 _tprintf(_T("\nSorry /renew [adapter] is not implemented yet\n"));
771 else if (DoShowclassid
)
772 _tprintf(_T("\nSorry /showclassid adapter is not implemented yet\n"));
773 else if (DoSetclassid
)
774 _tprintf(_T("\nSorry /setclassid adapter is not implemented yet\n"));
778 case 4: /* Process all the options that can have 2 parameters */
780 _tprintf(_T("\nSorry /setclassid adapter [classid]is not implemented yet\n"));