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
28 int LoadStringAndOem(HINSTANCE hInst
,
35 int res
= LoadString(hInst
, uID
, szTmp
, sizeof(szTmp
));
36 CharToOem(szTmp
, szNode
);
40 LPTSTR
GetNodeTypeName(UINT NodeType
)
42 static TCHAR szNode
[14];
47 if (!LoadStringAndOem(hInstance
, IDS_BCAST
, szNode
, sizeof(szNode
)))
52 if (!LoadStringAndOem(hInstance
, IDS_P2P
, szNode
, sizeof(szNode
)))
57 if (!LoadStringAndOem(hInstance
, IDS_MIXED
, szNode
, sizeof(szNode
)))
62 if (!LoadStringAndOem(hInstance
, IDS_HYBRID
, szNode
, sizeof(szNode
)))
67 if (!LoadStringAndOem(hInstance
, IDS_UNKNOWN
, szNode
, sizeof(szNode
)))
76 LPTSTR
GetInterfaceTypeName(UINT InterfaceType
)
78 static TCHAR szIntType
[25];
80 switch (InterfaceType
)
82 case MIB_IF_TYPE_OTHER
:
83 if (!LoadStringAndOem(hInstance
, IDS_OTHER
, szIntType
, sizeof(szIntType
)))
87 case MIB_IF_TYPE_ETHERNET
:
88 if (!LoadStringAndOem(hInstance
, IDS_ETH
, szIntType
, sizeof(szIntType
)))
92 case MIB_IF_TYPE_TOKENRING
:
93 if (!LoadStringAndOem(hInstance
, IDS_TOKEN
, szIntType
, sizeof(szIntType
)))
97 case MIB_IF_TYPE_FDDI
:
98 if (!LoadStringAndOem(hInstance
, IDS_FDDI
, szIntType
, sizeof(szIntType
)))
102 case MIB_IF_TYPE_PPP
:
103 if (!LoadStringAndOem(hInstance
, IDS_PPP
, szIntType
, sizeof(szIntType
)))
107 case MIB_IF_TYPE_LOOPBACK
:
108 if (!LoadStringAndOem(hInstance
, IDS_LOOP
, szIntType
, sizeof(szIntType
)))
112 case MIB_IF_TYPE_SLIP
:
113 if (!LoadStringAndOem(hInstance
, IDS_SLIP
, szIntType
, sizeof(szIntType
)))
118 if (!LoadStringAndOem(hInstance
, IDS_UNKNOWN
, szIntType
, sizeof(szIntType
)))
127 /* print MAC address */
128 PTCHAR
PrintMacAddr(PBYTE Mac
)
130 static TCHAR MacAddr
[20];
132 _stprintf(MacAddr
, _T("%02x-%02x-%02x-%02x-%02x-%02x"),
133 Mac
[0], Mac
[1], Mac
[2], Mac
[3], Mac
[4], Mac
[5]);
139 VOID
DoFormatMessage(LONG ErrorCode
)
145 ErrorCode
= GetLastError();
147 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|
148 FORMAT_MESSAGE_FROM_SYSTEM
|
149 FORMAT_MESSAGE_IGNORE_INSERTS
,
152 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), /* Default language */
157 _tprintf(_T("%s"), (LPTSTR
)lpMsgBuf
);
163 LPTSTR
GetConnectionType(LPTSTR lpClass
)
166 LPTSTR ConType
= NULL
;
167 LPTSTR ConTypeTmp
= NULL
;
169 LPTSTR PrePath
= _T("SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\");
170 LPTSTR PostPath
= _T("\\Connection");
175 /* don't overflow the buffer */
176 PathSize
= lstrlen(PrePath
) + lstrlen(lpClass
) + lstrlen(PostPath
) + 1;
180 wsprintf(Path
, _T("%s%s%s"), PrePath
, lpClass
, PostPath
);
182 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
186 &hKey
) == ERROR_SUCCESS
)
188 if(RegQueryValueEx(hKey
,
193 &dwDataSize
) == ERROR_SUCCESS
)
195 ConTypeTmp
= (LPTSTR
)HeapAlloc(ProcessHeap
,
199 ConType
= (LPTSTR
)HeapAlloc(ProcessHeap
,
202 if (ConType
&& ConTypeTmp
)
204 if(RegQueryValueEx(hKey
,
209 &dwDataSize
) != ERROR_SUCCESS
)
211 HeapFree(ProcessHeap
,
215 HeapFree(ProcessHeap
,
222 if (ConType
) CharToOem(ConTypeTmp
, ConType
);
234 LPTSTR
GetConnectionDescription(LPTSTR lpClass
)
236 HKEY hBaseKey
= NULL
;
237 HKEY hClassKey
= NULL
;
238 LPTSTR lpKeyClass
= NULL
;
239 LPTSTR lpConDesc
= NULL
;
240 LPTSTR lpPath
= NULL
;
241 TCHAR szPrePath
[] = _T("SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}\\");
246 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
250 &hBaseKey
) != ERROR_SUCCESS
)
262 if ((Status
= RegEnumKeyEx(hBaseKey
,
269 NULL
)) != ERROR_SUCCESS
)
271 if (Status
== ERROR_NO_MORE_ITEMS
)
273 DoFormatMessage(Status
);
281 PathSize
= lstrlen(szPrePath
) + lstrlen(szName
) + 1;
282 lpPath
= (LPTSTR
)HeapAlloc(ProcessHeap
,
284 PathSize
* sizeof(TCHAR
));
288 wsprintf(lpPath
, _T("%s%s"), szPrePath
, szName
);
290 //MessageBox(NULL, lpPath, NULL, 0);
292 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
296 &hClassKey
) != ERROR_SUCCESS
)
301 HeapFree(ProcessHeap
, 0, lpPath
);
304 if(RegQueryValueEx(hClassKey
,
305 _T("NetCfgInstanceId"),
309 &dwDataSize
) == ERROR_SUCCESS
)
311 lpKeyClass
= (LPTSTR
)HeapAlloc(ProcessHeap
,
314 if (lpKeyClass
== NULL
)
317 if(RegQueryValueEx(hClassKey
,
318 _T("NetCfgInstanceId"),
322 &dwDataSize
) != ERROR_SUCCESS
)
325 HeapFree(ProcessHeap
, 0, lpKeyClass
);
332 if (!lstrcmp(lpClass
, lpKeyClass
))
334 HeapFree(ProcessHeap
, 0, lpKeyClass
);
337 if(RegQueryValueEx(hClassKey
,
342 &dwDataSize
) == ERROR_SUCCESS
)
344 lpConDesc
= (LPTSTR
)HeapAlloc(ProcessHeap
,
347 if (lpConDesc
== NULL
)
350 if(RegQueryValueEx(hClassKey
,
355 &dwDataSize
) != ERROR_SUCCESS
)
369 if (hBaseKey
!= NULL
)
370 RegCloseKey(hBaseKey
);
371 if (hClassKey
!= NULL
)
372 RegCloseKey(hClassKey
);
373 if (lpConDesc
!= NULL
)
374 HeapFree(ProcessHeap
, 0, lpPath
);
375 if (lpConDesc
!= NULL
)
376 HeapFree(ProcessHeap
, 0, lpKeyClass
);
382 VOID
ShowInfo(BOOL bAll
)
385 PIP_ADAPTER_INFO pAdapterInfo
= NULL
;
386 PIP_ADAPTER_INFO pAdapter
= NULL
;
387 ULONG adaptOutBufLen
= 0;
388 PFIXED_INFO pFixedInfo
= NULL
;
389 ULONG netOutBufLen
= 0;
392 /* call GetAdaptersInfo to obtain the adapter info */
393 ret
= GetAdaptersInfo(pAdapterInfo
, &adaptOutBufLen
);
394 if (ret
== ERROR_BUFFER_OVERFLOW
)
396 pAdapterInfo
= (IP_ADAPTER_INFO
*)HeapAlloc(ProcessHeap
, 0, adaptOutBufLen
);
397 if (pAdapterInfo
== NULL
)
400 ret
= GetAdaptersInfo(pAdapterInfo
, &adaptOutBufLen
);
404 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
410 if( ERROR_NO_DATA
!= ret
)
417 /* call GetNetworkParams to obtain the network info */
418 if(GetNetworkParams(pFixedInfo
, &netOutBufLen
) == ERROR_BUFFER_OVERFLOW
)
420 pFixedInfo
= (FIXED_INFO
*)HeapAlloc(ProcessHeap
, 0, netOutBufLen
);
421 if (pFixedInfo
== NULL
)
424 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
427 if (GetNetworkParams(pFixedInfo
, &netOutBufLen
) != NO_ERROR
)
431 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
432 HeapFree(ProcessHeap
, 0, pFixedInfo
);
439 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
444 pAdapter
= pAdapterInfo
;
446 _tprintf(_T("\nReactOS IP Configuration\n\n"));
449 _tprintf(_T("\tHost Name . . . . . . . . . . . . : %s\n"), pFixedInfo
->HostName
);
450 _tprintf(_T("\tPrimary DNS Suffix. . . . . . . . : \n"));
451 _tprintf(_T("\tNode Type . . . . . . . . . . . . : %s\n"), GetNodeTypeName(pFixedInfo
->NodeType
));
452 if (pFixedInfo
->EnableRouting
)
453 _tprintf(_T("\tIP Routing Enabled. . . . . . . . : Yes\n"));
455 _tprintf(_T("\tIP Routing Enabled. . . . . . . . : No\n"));
456 if (pAdapter
&& pAdapter
->HaveWins
)
457 _tprintf(_T("\tWINS Proxy enabled. . . . . . . . : Yes\n"));
459 _tprintf(_T("\tWINS Proxy enabled. . . . . . . . : No\n"));
460 _tprintf(_T("\tDNS Suffix Search List. . . . . . : %s\n"), pFixedInfo
->DomainName
);
465 LPTSTR IntType
, myConType
;
467 mibEntry
.dwIndex
= pAdapter
->Index
;
468 GetIfEntry(&mibEntry
);
470 IntType
= GetInterfaceTypeName(pAdapter
->Type
);
471 myConType
= GetConnectionType(pAdapter
->AdapterName
);
473 _tprintf(_T("\n%s %s: \n\n"), IntType
, myConType
);
475 if (myConType
!= NULL
) HeapFree(ProcessHeap
, 0, myConType
);
477 /* check if the adapter is connected to the media */
478 if (mibEntry
.dwOperStatus
!= MIB_IF_OPER_STATUS_CONNECTED
&& mibEntry
.dwOperStatus
!= MIB_IF_OPER_STATUS_OPERATIONAL
)
480 _tprintf(_T("\tMedia State . . . . . . . . . . . : Media disconnected\n"));
481 pAdapter
= pAdapter
->Next
;
485 _tprintf(_T("\tConnection-specific DNS Suffix. . : %s\n"), pFixedInfo
->DomainName
);
489 LPTSTR lpDesc
= GetConnectionDescription(pAdapter
->AdapterName
);
490 _tprintf(_T("\tDescription . . . . . . . . . . . : %s\n"), lpDesc
);
491 HeapFree(ProcessHeap
, 0, lpDesc
);
492 _tprintf(_T("\tPhysical Address. . . . . . . . . : %s\n"), PrintMacAddr(pAdapter
->Address
));
493 if (pAdapter
->DhcpEnabled
)
494 _tprintf(_T("\tDHCP Enabled. . . . . . . . . . . : Yes\n"));
496 _tprintf(_T("\tDHCP Enabled. . . . . . . . . . . : No\n"));
497 _tprintf(_T("\tAutoconfiguration Enabled . . . . : \n"));
500 _tprintf(_T("\tIP Address. . . . . . . . . . . . : %s\n"), pAdapter
->IpAddressList
.IpAddress
.String
);
501 _tprintf(_T("\tSubnet Mask . . . . . . . . . . . : %s\n"), pAdapter
->IpAddressList
.IpMask
.String
);
502 if (pAdapter
->GatewayList
.IpAddress
.String
[0] != '0')
503 _tprintf(_T("\tDefault Gateway . . . . . . . . . : %s\n"), pAdapter
->GatewayList
.IpAddress
.String
);
505 _tprintf(_T("\tDefault Gateway . . . . . . . . . :\n"));
509 PIP_ADDR_STRING pIPAddr
;
511 if (pAdapter
->DhcpEnabled
)
512 _tprintf(_T("\tDHCP Server . . . . . . . . . . . : %s\n"), pAdapter
->DhcpServer
.IpAddress
.String
);
514 _tprintf(_T("\tDNS Servers . . . . . . . . . . . : "));
515 _tprintf(_T("%s\n"), pFixedInfo
->DnsServerList
.IpAddress
.String
);
516 pIPAddr
= pFixedInfo
->DnsServerList
.Next
;
519 _tprintf(_T("\t\t\t\t\t %s\n"), pIPAddr
->IpAddress
.String
);
520 pIPAddr
= pIPAddr
->Next
;
523 if (pAdapter
->HaveWins
)
525 _tprintf(_T("\tPrimary WINS Server . . . . . . . : %s\n"), pAdapter
->PrimaryWinsServer
.IpAddress
.String
);
526 _tprintf(_T("\tSecondard WINS Server . . . . . . : %s\n"), pAdapter
->SecondaryWinsServer
.IpAddress
.String
);
529 if (pAdapter
->DhcpEnabled
)
531 _tprintf(_T("\tLease Obtained. . . . . . . . . . : %s"), _tasctime(localtime(&pAdapter
->LeaseObtained
)));
532 _tprintf(_T("\tLease Expires . . . . . . . . . . : %s"), _tasctime(localtime(&pAdapter
->LeaseExpires
)));
537 pAdapter
= pAdapter
->Next
;
541 HeapFree(ProcessHeap
, 0, pFixedInfo
);
543 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
546 VOID
Release(LPTSTR Index
)
548 IP_ADAPTER_INDEX_MAP AdapterInfo
;
552 /* if interface is not given, query GetInterfaceInfo */
555 PIP_INTERFACE_INFO pInfo
= NULL
;
556 ULONG ulOutBufLen
= 0;
558 if (GetInterfaceInfo(pInfo
, &ulOutBufLen
) == ERROR_INSUFFICIENT_BUFFER
)
560 pInfo
= (IP_INTERFACE_INFO
*)HeapAlloc(ProcessHeap
, 0, ulOutBufLen
);
564 if (GetInterfaceInfo(pInfo
, &ulOutBufLen
) == NO_ERROR
)
566 for (i
= 0; i
< pInfo
->NumAdapters
; i
++)
568 CopyMemory(&AdapterInfo
, &pInfo
->Adapter
[i
], sizeof(IP_ADAPTER_INDEX_MAP
));
569 _tprintf(_T("name - %S\n"), pInfo
->Adapter
[i
].Name
);
571 /* Call IpReleaseAddress to release the IP address on the specified adapter. */
572 if ((ret
= IpReleaseAddress(&AdapterInfo
)) != NO_ERROR
)
574 _tprintf(_T("\nAn error occured while releasing interface %S : \n"), AdapterInfo
.Name
);
575 DoFormatMessage(ret
);
579 HeapFree(ProcessHeap
, 0, pInfo
);
584 HeapFree(ProcessHeap
, 0, pInfo
);
598 * we need to be able to release connections by name with support for globbing
599 * i.e. ipconfig /release Eth* will release all cards starting with Eth...
600 * ipconfig /release *con* will release all cards with 'con' in their name
608 VOID
Renew(LPTSTR Index
)
610 IP_ADAPTER_INDEX_MAP AdapterInfo
;
613 /* if interface is not given, query GetInterfaceInfo */
616 PIP_INTERFACE_INFO pInfo
;
617 ULONG ulOutBufLen
= 0;
619 pInfo
= (IP_INTERFACE_INFO
*)HeapAlloc(ProcessHeap
, 0, sizeof(IP_INTERFACE_INFO
));
622 _tprintf(_T("memory allocation error"));
626 /* Make an initial call to GetInterfaceInfo to get
627 * the necessary size into the ulOutBufLen variable */
628 if ( GetInterfaceInfo(pInfo
, &ulOutBufLen
) == ERROR_INSUFFICIENT_BUFFER
)
630 HeapFree(ProcessHeap
, 0, pInfo
);
631 pInfo
= (IP_INTERFACE_INFO
*)HeapAlloc(ProcessHeap
, 0, ulOutBufLen
);
634 _tprintf(_T("memory allocation error"));
639 /* Make a second call to GetInterfaceInfo to get the actual data we want */
640 if (GetInterfaceInfo(pInfo
, &ulOutBufLen
) == NO_ERROR
)
642 for (i
= 0; i
< pInfo
->NumAdapters
; i
++)
644 CopyMemory(&AdapterInfo
, &pInfo
->Adapter
[i
], sizeof(IP_ADAPTER_INDEX_MAP
));
645 _tprintf(_T("name - %S\n"), pInfo
->Adapter
[i
].Name
);
648 /* Call IpRenewAddress to renew the IP address on the specified adapter. */
649 if (IpRenewAddress(&AdapterInfo
) != NO_ERROR
)
651 _tprintf(_T("\nAn error occured while renew interface %s : "), _T("*name*"));
658 _tprintf(_T("\nGetInterfaceInfo failed : "));
662 HeapFree(ProcessHeap
, 0, pInfo
);
668 * we need to be able to renew connections by name with support for globbing
669 * i.e. ipconfig /renew Eth* will renew all cards starting with Eth...
670 * ipconfig /renew *con* will renew all cards with 'con' in their name
683 LPTSTR lpName
= (LPTSTR
)MAKEINTRESOURCE((IDS_USAGE
>> 4) + 1);
685 hRes
= FindResource(hInstance
,
690 if ((Size
= SizeofResource(hInstance
,
693 lpUsage
= (LPTSTR
)HeapAlloc(ProcessHeap
,
699 if (LoadStringAndOem(hInstance
,
704 _tprintf(_T("%s"), lpUsage
);
712 int main(int argc
, char *argv
[])
716 BOOL DoRelease
=FALSE
;
718 BOOL DoFlushdns
=FALSE
;
719 BOOL DoRegisterdns
=FALSE
;
720 BOOL DoDisplaydns
=FALSE
;
721 BOOL DoShowclassid
=FALSE
;
722 BOOL DoSetclassid
=FALSE
;
724 hInstance
= GetModuleHandle(NULL
);
725 ProcessHeap
= GetProcessHeap();
727 /* Parse command line for options we have been given. */
728 if ( (argc
> 1)&&(argv
[1][0]=='/' || argv
[1][0]=='-') )
730 if( !_tcsicmp( &argv
[1][1], _T("?") ))
734 else if( !_tcsnicmp( &argv
[1][1], _T("ALL"), _tcslen(&argv
[1][1]) ))
738 else if( !_tcsnicmp( &argv
[1][1], _T("RELEASE"), _tcslen(&argv
[1][1]) ))
742 else if( ! _tcsnicmp( &argv
[1][1], _T("RENEW"), _tcslen(&argv
[1][1]) ))
746 else if( ! _tcsnicmp( &argv
[1][1], _T("FLUSHDNS"), _tcslen(&argv
[1][1]) ))
750 else if( ! _tcsnicmp( &argv
[1][1], _T("FLUSHREGISTERDNS"), _tcslen(&argv
[1][1]) ))
752 DoRegisterdns
= TRUE
;
754 else if( ! _tcsnicmp( &argv
[1][1], _T("DISPLAYDNS"), _tcslen(&argv
[1][1]) ))
758 else if( ! _tcsnicmp( &argv
[1][1], _T("SHOWCLASSID"), _tcslen(&argv
[1][1]) ))
760 DoShowclassid
= TRUE
;
762 else if( ! _tcsnicmp( &argv
[1][1], _T("SETCLASSID"), _tcslen(&argv
[1][1]) ))
770 case 1: /* Default behaviour if no options are given*/
773 case 2: /* Process all the options that take no parameters */
783 _tprintf(_T("\nSorry /flushdns is not implemented yet\n"));
784 else if (DoRegisterdns
)
785 _tprintf(_T("\nSorry /registerdns is not implemented yet\n"));
786 else if (DoDisplaydns
)
787 _tprintf(_T("\nSorry /displaydns is not implemented yet\n"));
791 case 3: /* Process all the options that can have 1 parameter */
793 _tprintf(_T("\nSorry /release [adapter] is not implemented yet\n"));
796 _tprintf(_T("\nSorry /renew [adapter] is not implemented yet\n"));
797 else if (DoShowclassid
)
798 _tprintf(_T("\nSorry /showclassid adapter is not implemented yet\n"));
799 else if (DoSetclassid
)
800 _tprintf(_T("\nSorry /setclassid adapter is not implemented yet\n"));
804 case 4: /* Process all the options that can have 2 parameters */
806 _tprintf(_T("\nSorry /setclassid adapter [classid]is not implemented yet\n"));