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
)
363 PIP_ADAPTER_INFO pAdapterInfo
= NULL
;
364 PIP_ADAPTER_INFO pAdapter
= NULL
;
365 ULONG adaptOutBufLen
= 0;
366 PFIXED_INFO pFixedInfo
= NULL
;
367 ULONG netOutBufLen
= 0;
370 /* call GetAdaptersInfo to obtain the adapter info */
371 ret
= GetAdaptersInfo(pAdapterInfo
, &adaptOutBufLen
);
372 if (ret
== ERROR_BUFFER_OVERFLOW
)
374 pAdapterInfo
= (IP_ADAPTER_INFO
*)HeapAlloc(ProcessHeap
, 0, adaptOutBufLen
);
375 if (pAdapterInfo
== NULL
)
378 ret
= GetAdaptersInfo(pAdapterInfo
, &adaptOutBufLen
);
382 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
388 if( ERROR_NO_DATA
!= ret
)
395 /* call GetNetworkParams to obtain the network info */
396 if(GetNetworkParams(pFixedInfo
, &netOutBufLen
) == ERROR_BUFFER_OVERFLOW
)
398 pFixedInfo
= (FIXED_INFO
*)HeapAlloc(ProcessHeap
, 0, netOutBufLen
);
399 if (pFixedInfo
== NULL
)
402 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
405 if (GetNetworkParams(pFixedInfo
, &netOutBufLen
) != NO_ERROR
)
409 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
410 HeapFree(ProcessHeap
, 0, pFixedInfo
);
417 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
422 pAdapter
= pAdapterInfo
;
424 _tprintf(_T("\nReactOS IP Configuration\n\n"));
427 _tprintf(_T("\tHost Name . . . . . . . . . . . . : %s\n"), pFixedInfo
->HostName
);
428 _tprintf(_T("\tPrimary DNS Suffix. . . . . . . . : \n"));
429 _tprintf(_T("\tNode Type . . . . . . . . . . . . : %s\n"), GetNodeTypeName(pFixedInfo
->NodeType
));
430 if (pFixedInfo
->EnableRouting
)
431 _tprintf(_T("\tIP Routing Enabled. . . . . . . . : Yes\n"));
433 _tprintf(_T("\tIP Routing Enabled. . . . . . . . : No\n"));
434 if (pAdapter
&& pAdapter
->HaveWins
)
435 _tprintf(_T("\tWINS Proxy enabled. . . . . . . . : Yes\n"));
437 _tprintf(_T("\tWINS Proxy enabled. . . . . . . . : No\n"));
438 _tprintf(_T("\tDNS Suffix Search List. . . . . . : %s\n"), pFixedInfo
->DomainName
);
443 LPTSTR IntType
, myConType
;
445 mibEntry
.dwIndex
= pAdapter
->Index
;
446 GetIfEntry(&mibEntry
);
448 IntType
= GetInterfaceTypeName(pAdapter
->Type
);
449 myConType
= GetConnectionType(pAdapter
->AdapterName
);
451 _tprintf(_T("\n%s %s: \n\n"), IntType
, myConType
);
453 if (myConType
!= NULL
) HeapFree(ProcessHeap
, 0, myConType
);
455 /* check if the adapter is connected to the media */
456 if (mibEntry
.dwOperStatus
!= MIB_IF_OPER_STATUS_CONNECTED
&& mibEntry
.dwOperStatus
!= MIB_IF_OPER_STATUS_OPERATIONAL
)
458 _tprintf(_T("\tMedia State . . . . . . . . . . . : Media disconnected\n"));
459 pAdapter
= pAdapter
->Next
;
463 _tprintf(_T("\tConnection-specific DNS Suffix. . : %s\n"), pFixedInfo
->DomainName
);
467 LPTSTR lpDesc
= GetConnectionDescription(pAdapter
->AdapterName
);
468 _tprintf(_T("\tDescription . . . . . . . . . . . : %s\n"), lpDesc
);
469 HeapFree(ProcessHeap
, 0, lpDesc
);
470 _tprintf(_T("\tPhysical Address. . . . . . . . . : %s\n"), PrintMacAddr(pAdapter
->Address
));
471 if (pAdapter
->DhcpEnabled
)
472 _tprintf(_T("\tDHCP Enabled. . . . . . . . . . . : Yes\n"));
474 _tprintf(_T("\tDHCP Enabled. . . . . . . . . . . : No\n"));
475 _tprintf(_T("\tAutoconfiguration Enabled . . . . : \n"));
478 _tprintf(_T("\tIP Address. . . . . . . . . . . . : %s\n"), pAdapter
->IpAddressList
.IpAddress
.String
);
479 _tprintf(_T("\tSubnet Mask . . . . . . . . . . . : %s\n"), pAdapter
->IpAddressList
.IpMask
.String
);
480 if (pAdapter
->GatewayList
.IpAddress
.String
[0] != '0')
481 _tprintf(_T("\tDefault Gateway . . . . . . . . . : %s\n"), pAdapter
->GatewayList
.IpAddress
.String
);
483 _tprintf(_T("\tDefault Gateway . . . . . . . . . :\n"));
487 PIP_ADDR_STRING pIPAddr
;
489 if (pAdapter
->DhcpEnabled
)
490 _tprintf(_T("\tDHCP Server . . . . . . . . . . . : %s\n"), pAdapter
->DhcpServer
.IpAddress
.String
);
492 _tprintf(_T("\tDNS Servers . . . . . . . . . . . : "));
493 _tprintf(_T("%s\n"), pFixedInfo
->DnsServerList
.IpAddress
.String
);
494 pIPAddr
= pFixedInfo
->DnsServerList
.Next
;
497 _tprintf(_T("\t\t\t\t\t %s\n"), pIPAddr
->IpAddress
.String
);
498 pIPAddr
= pIPAddr
->Next
;
501 if (pAdapter
->HaveWins
)
503 _tprintf(_T("\tPrimary WINS Server . . . . . . . : %s\n"), pAdapter
->PrimaryWinsServer
.IpAddress
.String
);
504 _tprintf(_T("\tSecondard WINS Server . . . . . . : %s\n"), pAdapter
->SecondaryWinsServer
.IpAddress
.String
);
507 if (pAdapter
->DhcpEnabled
)
509 _tprintf(_T("\tLease Obtained. . . . . . . . . . : %s"), _tasctime(localtime(&pAdapter
->LeaseObtained
)));
510 _tprintf(_T("\tLease Expires . . . . . . . . . . : %s"), _tasctime(localtime(&pAdapter
->LeaseExpires
)));
515 pAdapter
= pAdapter
->Next
;
519 HeapFree(ProcessHeap
, 0, pFixedInfo
);
521 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
524 VOID
Release(LPTSTR Index
)
526 IP_ADAPTER_INDEX_MAP AdapterInfo
;
530 /* if interface is not given, query GetInterfaceInfo */
533 PIP_INTERFACE_INFO pInfo
= NULL
;
534 ULONG ulOutBufLen
= 0;
536 if (GetInterfaceInfo(pInfo
, &ulOutBufLen
) == ERROR_INSUFFICIENT_BUFFER
)
538 pInfo
= (IP_INTERFACE_INFO
*)HeapAlloc(ProcessHeap
, 0, ulOutBufLen
);
542 if (GetInterfaceInfo(pInfo
, &ulOutBufLen
) == NO_ERROR
)
544 for (i
= 0; i
< pInfo
->NumAdapters
; i
++)
546 CopyMemory(&AdapterInfo
, &pInfo
->Adapter
[i
], sizeof(IP_ADAPTER_INDEX_MAP
));
547 _tprintf(_T("name - %S\n"), pInfo
->Adapter
[i
].Name
);
549 /* Call IpReleaseAddress to release the IP address on the specified adapter. */
550 if ((ret
= IpReleaseAddress(&AdapterInfo
)) != NO_ERROR
)
552 _tprintf(_T("\nAn error occured while releasing interface %S : \n"), AdapterInfo
.Name
);
553 DoFormatMessage(ret
);
557 HeapFree(ProcessHeap
, 0, pInfo
);
562 HeapFree(ProcessHeap
, 0, pInfo
);
576 * we need to be able to release connections by name with support for globbing
577 * i.e. ipconfig /release Eth* will release all cards starting with Eth...
578 * ipconfig /release *con* will release all cards with 'con' in their name
586 VOID
Renew(LPTSTR Index
)
588 IP_ADAPTER_INDEX_MAP AdapterInfo
;
591 /* if interface is not given, query GetInterfaceInfo */
594 PIP_INTERFACE_INFO pInfo
;
595 ULONG ulOutBufLen
= 0;
597 pInfo
= (IP_INTERFACE_INFO
*)HeapAlloc(ProcessHeap
, 0, sizeof(IP_INTERFACE_INFO
));
600 _tprintf(_T("memory allocation error"));
604 /* Make an initial call to GetInterfaceInfo to get
605 * the necessary size into the ulOutBufLen variable */
606 if ( GetInterfaceInfo(pInfo
, &ulOutBufLen
) == ERROR_INSUFFICIENT_BUFFER
)
608 HeapFree(ProcessHeap
, 0, pInfo
);
609 pInfo
= (IP_INTERFACE_INFO
*)HeapAlloc(ProcessHeap
, 0, ulOutBufLen
);
612 _tprintf(_T("memory allocation error"));
617 /* Make a second call to GetInterfaceInfo to get the actual data we want */
618 if (GetInterfaceInfo(pInfo
, &ulOutBufLen
) == NO_ERROR
)
620 for (i
= 0; i
< pInfo
->NumAdapters
; i
++)
622 CopyMemory(&AdapterInfo
, &pInfo
->Adapter
[i
], sizeof(IP_ADAPTER_INDEX_MAP
));
623 _tprintf(_T("name - %S\n"), pInfo
->Adapter
[i
].Name
);
626 /* Call IpRenewAddress to renew the IP address on the specified adapter. */
627 if (IpRenewAddress(&AdapterInfo
) != NO_ERROR
)
629 _tprintf(_T("\nAn error occured while renew interface %s : "), _T("*name*"));
636 _tprintf(_T("\nGetInterfaceInfo failed : "));
640 HeapFree(ProcessHeap
, 0, pInfo
);
646 * we need to be able to renew connections by name with support for globbing
647 * i.e. ipconfig /renew Eth* will renew all cards starting with Eth...
648 * ipconfig /renew *con* will renew all cards with 'con' in their name
661 LPTSTR lpName
= (LPTSTR
)MAKEINTRESOURCE((IDS_USAGE
>> 4) + 1);
663 hRes
= FindResource(hInstance
,
668 if ((Size
= SizeofResource(hInstance
,
671 lpUsage
= (LPTSTR
)HeapAlloc(ProcessHeap
,
677 if (LoadString(hInstance
,
682 _tprintf(_T("%s"), lpUsage
);
690 int main(int argc
, char *argv
[])
694 BOOL DoRelease
=FALSE
;
696 BOOL DoFlushdns
=FALSE
;
697 BOOL DoRegisterdns
=FALSE
;
698 BOOL DoDisplaydns
=FALSE
;
699 BOOL DoShowclassid
=FALSE
;
700 BOOL DoSetclassid
=FALSE
;
702 hInstance
= GetModuleHandle(NULL
);
703 ProcessHeap
= GetProcessHeap();
705 /* Parse command line for options we have been given. */
706 if ( (argc
> 1)&&(argv
[1][0]=='/' || argv
[1][0]=='-') )
708 if( !_tcsicmp( &argv
[1][1], _T("?") ))
712 else if( !_tcsnicmp( &argv
[1][1], _T("ALL"), _tcslen(&argv
[1][1]) ))
716 else if( !_tcsnicmp( &argv
[1][1], _T("RELEASE"), _tcslen(&argv
[1][1]) ))
720 else if( ! _tcsnicmp( &argv
[1][1], _T("RENEW"), _tcslen(&argv
[1][1]) ))
724 else if( ! _tcsnicmp( &argv
[1][1], _T("FLUSHDNS"), _tcslen(&argv
[1][1]) ))
728 else if( ! _tcsnicmp( &argv
[1][1], _T("FLUSHREGISTERDNS"), _tcslen(&argv
[1][1]) ))
730 DoRegisterdns
= TRUE
;
732 else if( ! _tcsnicmp( &argv
[1][1], _T("DISPLAYDNS"), _tcslen(&argv
[1][1]) ))
736 else if( ! _tcsnicmp( &argv
[1][1], _T("SHOWCLASSID"), _tcslen(&argv
[1][1]) ))
738 DoShowclassid
= TRUE
;
740 else if( ! _tcsnicmp( &argv
[1][1], _T("SETCLASSID"), _tcslen(&argv
[1][1]) ))
748 case 1: /* Default behaviour if no options are given*/
751 case 2: /* Process all the options that take no parameters */
761 _tprintf(_T("\nSorry /flushdns is not implemented yet\n"));
762 else if (DoRegisterdns
)
763 _tprintf(_T("\nSorry /registerdns is not implemented yet\n"));
764 else if (DoDisplaydns
)
765 _tprintf(_T("\nSorry /displaydns is not implemented yet\n"));
769 case 3: /* Process all the options that can have 1 parameter */
771 _tprintf(_T("\nSorry /release [adapter] is not implemented yet\n"));
774 _tprintf(_T("\nSorry /renew [adapter] is not implemented yet\n"));
775 else if (DoShowclassid
)
776 _tprintf(_T("\nSorry /showclassid adapter is not implemented yet\n"));
777 else if (DoSetclassid
)
778 _tprintf(_T("\nSorry /setclassid adapter is not implemented yet\n"));
782 case 4: /* Process all the options that can have 2 parameters */
784 _tprintf(_T("\nSorry /setclassid adapter [classid]is not implemented yet\n"));