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
,
37 szTmp
= (LPTSTR
)HeapAlloc(ProcessHeap
, 0, byteSize
);
42 res
= LoadString(hInst
, uID
, szTmp
, byteSize
);
43 CharToOem(szTmp
, szNode
);
44 HeapFree(ProcessHeap
, 0, szTmp
);
48 LPTSTR
GetNodeTypeName(UINT NodeType
)
50 static TCHAR szNode
[14];
55 if (!LoadStringAndOem(hInstance
, IDS_BCAST
, szNode
, sizeof(szNode
)))
60 if (!LoadStringAndOem(hInstance
, IDS_P2P
, szNode
, sizeof(szNode
)))
65 if (!LoadStringAndOem(hInstance
, IDS_MIXED
, szNode
, sizeof(szNode
)))
70 if (!LoadStringAndOem(hInstance
, IDS_HYBRID
, szNode
, sizeof(szNode
)))
75 if (!LoadStringAndOem(hInstance
, IDS_UNKNOWN
, szNode
, sizeof(szNode
)))
84 LPTSTR
GetInterfaceTypeName(UINT InterfaceType
)
86 static TCHAR szIntType
[25];
88 switch (InterfaceType
)
90 case MIB_IF_TYPE_OTHER
:
91 if (!LoadStringAndOem(hInstance
, IDS_OTHER
, szIntType
, sizeof(szIntType
)))
95 case MIB_IF_TYPE_ETHERNET
:
96 if (!LoadStringAndOem(hInstance
, IDS_ETH
, szIntType
, sizeof(szIntType
)))
100 case MIB_IF_TYPE_TOKENRING
:
101 if (!LoadStringAndOem(hInstance
, IDS_TOKEN
, szIntType
, sizeof(szIntType
)))
105 case MIB_IF_TYPE_FDDI
:
106 if (!LoadStringAndOem(hInstance
, IDS_FDDI
, szIntType
, sizeof(szIntType
)))
110 case MIB_IF_TYPE_PPP
:
111 if (!LoadStringAndOem(hInstance
, IDS_PPP
, szIntType
, sizeof(szIntType
)))
115 case MIB_IF_TYPE_LOOPBACK
:
116 if (!LoadStringAndOem(hInstance
, IDS_LOOP
, szIntType
, sizeof(szIntType
)))
120 case MIB_IF_TYPE_SLIP
:
121 if (!LoadStringAndOem(hInstance
, IDS_SLIP
, szIntType
, sizeof(szIntType
)))
126 if (!LoadStringAndOem(hInstance
, IDS_UNKNOWN
, szIntType
, sizeof(szIntType
)))
135 /* print MAC address */
136 PTCHAR
PrintMacAddr(PBYTE Mac
)
138 static TCHAR MacAddr
[20];
140 _stprintf(MacAddr
, _T("%02x-%02x-%02x-%02x-%02x-%02x"),
141 Mac
[0], Mac
[1], Mac
[2], Mac
[3], Mac
[4], Mac
[5]);
147 VOID
DoFormatMessage(LONG ErrorCode
)
153 ErrorCode
= GetLastError();
155 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|
156 FORMAT_MESSAGE_FROM_SYSTEM
|
157 FORMAT_MESSAGE_IGNORE_INSERTS
,
160 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), /* Default language */
165 _tprintf(_T("%s"), (LPTSTR
)lpMsgBuf
);
171 LPTSTR
GetConnectionType(LPTSTR lpClass
)
174 LPTSTR ConType
= NULL
;
175 LPTSTR ConTypeTmp
= NULL
;
177 LPTSTR PrePath
= _T("SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\");
178 LPTSTR PostPath
= _T("\\Connection");
183 /* don't overflow the buffer */
184 PathSize
= lstrlen(PrePath
) + lstrlen(lpClass
) + lstrlen(PostPath
) + 1;
188 wsprintf(Path
, _T("%s%s%s"), PrePath
, lpClass
, PostPath
);
190 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
194 &hKey
) == ERROR_SUCCESS
)
196 if(RegQueryValueEx(hKey
,
201 &dwDataSize
) == ERROR_SUCCESS
)
203 ConTypeTmp
= (LPTSTR
)HeapAlloc(ProcessHeap
,
207 ConType
= (LPTSTR
)HeapAlloc(ProcessHeap
,
210 if (ConType
&& ConTypeTmp
)
212 if(RegQueryValueEx(hKey
,
217 &dwDataSize
) != ERROR_SUCCESS
)
219 HeapFree(ProcessHeap
,
223 HeapFree(ProcessHeap
,
230 if (ConType
) CharToOem(ConTypeTmp
, ConType
);
242 LPTSTR
GetConnectionDescription(LPTSTR lpClass
)
244 HKEY hBaseKey
= NULL
;
245 HKEY hClassKey
= NULL
;
246 LPTSTR lpKeyClass
= NULL
;
247 LPTSTR lpConDesc
= NULL
;
248 LPTSTR lpPath
= NULL
;
249 TCHAR szPrePath
[] = _T("SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}\\");
254 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
258 &hBaseKey
) != ERROR_SUCCESS
)
270 if ((Status
= RegEnumKeyEx(hBaseKey
,
277 NULL
)) != ERROR_SUCCESS
)
279 if (Status
== ERROR_NO_MORE_ITEMS
)
281 DoFormatMessage(Status
);
289 PathSize
= lstrlen(szPrePath
) + lstrlen(szName
) + 1;
290 lpPath
= (LPTSTR
)HeapAlloc(ProcessHeap
,
292 PathSize
* sizeof(TCHAR
));
296 wsprintf(lpPath
, _T("%s%s"), szPrePath
, szName
);
298 //MessageBox(NULL, lpPath, NULL, 0);
300 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
304 &hClassKey
) != ERROR_SUCCESS
)
309 HeapFree(ProcessHeap
, 0, lpPath
);
312 if(RegQueryValueEx(hClassKey
,
313 _T("NetCfgInstanceId"),
317 &dwDataSize
) == ERROR_SUCCESS
)
319 lpKeyClass
= (LPTSTR
)HeapAlloc(ProcessHeap
,
322 if (lpKeyClass
== NULL
)
325 if(RegQueryValueEx(hClassKey
,
326 _T("NetCfgInstanceId"),
330 &dwDataSize
) != ERROR_SUCCESS
)
333 HeapFree(ProcessHeap
, 0, lpKeyClass
);
340 if (!lstrcmp(lpClass
, lpKeyClass
))
342 HeapFree(ProcessHeap
, 0, lpKeyClass
);
345 if(RegQueryValueEx(hClassKey
,
350 &dwDataSize
) == ERROR_SUCCESS
)
352 lpConDesc
= (LPTSTR
)HeapAlloc(ProcessHeap
,
355 if (lpConDesc
== NULL
)
358 if(RegQueryValueEx(hClassKey
,
363 &dwDataSize
) != ERROR_SUCCESS
)
377 if (hBaseKey
!= NULL
)
378 RegCloseKey(hBaseKey
);
379 if (hClassKey
!= NULL
)
380 RegCloseKey(hClassKey
);
381 if (lpConDesc
!= NULL
)
382 HeapFree(ProcessHeap
, 0, lpPath
);
383 if (lpConDesc
!= NULL
)
384 HeapFree(ProcessHeap
, 0, lpKeyClass
);
390 VOID
ShowInfo(BOOL bAll
)
393 PIP_ADAPTER_INFO pAdapterInfo
= NULL
;
394 PIP_ADAPTER_INFO pAdapter
= NULL
;
395 ULONG adaptOutBufLen
= 0;
396 PFIXED_INFO pFixedInfo
= NULL
;
397 ULONG netOutBufLen
= 0;
400 /* call GetAdaptersInfo to obtain the adapter info */
401 ret
= GetAdaptersInfo(pAdapterInfo
, &adaptOutBufLen
);
402 if (ret
== ERROR_BUFFER_OVERFLOW
)
404 pAdapterInfo
= (IP_ADAPTER_INFO
*)HeapAlloc(ProcessHeap
, 0, adaptOutBufLen
);
405 if (pAdapterInfo
== NULL
)
408 ret
= GetAdaptersInfo(pAdapterInfo
, &adaptOutBufLen
);
412 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
418 if( ERROR_NO_DATA
!= ret
)
425 /* call GetNetworkParams to obtain the network info */
426 if(GetNetworkParams(pFixedInfo
, &netOutBufLen
) == ERROR_BUFFER_OVERFLOW
)
428 pFixedInfo
= (FIXED_INFO
*)HeapAlloc(ProcessHeap
, 0, netOutBufLen
);
429 if (pFixedInfo
== NULL
)
432 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
435 if (GetNetworkParams(pFixedInfo
, &netOutBufLen
) != NO_ERROR
)
439 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
440 HeapFree(ProcessHeap
, 0, pFixedInfo
);
447 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
452 pAdapter
= pAdapterInfo
;
454 _tprintf(_T("\nReactOS IP Configuration\n\n"));
457 _tprintf(_T("\tHost Name . . . . . . . . . . . . : %s\n"), pFixedInfo
->HostName
);
458 _tprintf(_T("\tPrimary DNS Suffix. . . . . . . . : \n"));
459 _tprintf(_T("\tNode Type . . . . . . . . . . . . : %s\n"), GetNodeTypeName(pFixedInfo
->NodeType
));
460 if (pFixedInfo
->EnableRouting
)
461 _tprintf(_T("\tIP Routing Enabled. . . . . . . . : Yes\n"));
463 _tprintf(_T("\tIP Routing Enabled. . . . . . . . : No\n"));
464 if (pAdapter
&& pAdapter
->HaveWins
)
465 _tprintf(_T("\tWINS Proxy enabled. . . . . . . . : Yes\n"));
467 _tprintf(_T("\tWINS Proxy enabled. . . . . . . . : No\n"));
468 _tprintf(_T("\tDNS Suffix Search List. . . . . . : %s\n"), pFixedInfo
->DomainName
);
473 LPTSTR IntType
, myConType
;
475 mibEntry
.dwIndex
= pAdapter
->Index
;
476 GetIfEntry(&mibEntry
);
478 IntType
= GetInterfaceTypeName(pAdapter
->Type
);
479 myConType
= GetConnectionType(pAdapter
->AdapterName
);
481 _tprintf(_T("\n%s %s: \n\n"), IntType
, myConType
);
483 if (myConType
!= NULL
) HeapFree(ProcessHeap
, 0, myConType
);
485 /* check if the adapter is connected to the media */
486 if (mibEntry
.dwOperStatus
!= MIB_IF_OPER_STATUS_CONNECTED
&& mibEntry
.dwOperStatus
!= MIB_IF_OPER_STATUS_OPERATIONAL
)
488 _tprintf(_T("\tMedia State . . . . . . . . . . . : Media disconnected\n"));
489 pAdapter
= pAdapter
->Next
;
493 _tprintf(_T("\tConnection-specific DNS Suffix. . : %s\n"), pFixedInfo
->DomainName
);
497 LPTSTR lpDesc
= GetConnectionDescription(pAdapter
->AdapterName
);
498 _tprintf(_T("\tDescription . . . . . . . . . . . : %s\n"), lpDesc
);
499 HeapFree(ProcessHeap
, 0, lpDesc
);
500 _tprintf(_T("\tPhysical Address. . . . . . . . . : %s\n"), PrintMacAddr(pAdapter
->Address
));
501 if (pAdapter
->DhcpEnabled
)
502 _tprintf(_T("\tDHCP Enabled. . . . . . . . . . . : Yes\n"));
504 _tprintf(_T("\tDHCP Enabled. . . . . . . . . . . : No\n"));
505 _tprintf(_T("\tAutoconfiguration Enabled . . . . : \n"));
508 _tprintf(_T("\tIP Address. . . . . . . . . . . . : %s\n"), pAdapter
->IpAddressList
.IpAddress
.String
);
509 _tprintf(_T("\tSubnet Mask . . . . . . . . . . . : %s\n"), pAdapter
->IpAddressList
.IpMask
.String
);
510 if (pAdapter
->GatewayList
.IpAddress
.String
[0] != '0')
511 _tprintf(_T("\tDefault Gateway . . . . . . . . . : %s\n"), pAdapter
->GatewayList
.IpAddress
.String
);
513 _tprintf(_T("\tDefault Gateway . . . . . . . . . :\n"));
517 PIP_ADDR_STRING pIPAddr
;
519 if (pAdapter
->DhcpEnabled
)
520 _tprintf(_T("\tDHCP Server . . . . . . . . . . . : %s\n"), pAdapter
->DhcpServer
.IpAddress
.String
);
522 _tprintf(_T("\tDNS Servers . . . . . . . . . . . : "));
523 _tprintf(_T("%s\n"), pFixedInfo
->DnsServerList
.IpAddress
.String
);
524 pIPAddr
= pFixedInfo
->DnsServerList
.Next
;
527 _tprintf(_T("\t\t\t\t\t %s\n"), pIPAddr
->IpAddress
.String
);
528 pIPAddr
= pIPAddr
->Next
;
531 if (pAdapter
->HaveWins
)
533 _tprintf(_T("\tPrimary WINS Server . . . . . . . : %s\n"), pAdapter
->PrimaryWinsServer
.IpAddress
.String
);
534 _tprintf(_T("\tSecondard WINS Server . . . . . . : %s\n"), pAdapter
->SecondaryWinsServer
.IpAddress
.String
);
537 if (pAdapter
->DhcpEnabled
)
539 _tprintf(_T("\tLease Obtained. . . . . . . . . . : %s"), _tasctime(localtime(&pAdapter
->LeaseObtained
)));
540 _tprintf(_T("\tLease Expires . . . . . . . . . . : %s"), _tasctime(localtime(&pAdapter
->LeaseExpires
)));
545 pAdapter
= pAdapter
->Next
;
549 HeapFree(ProcessHeap
, 0, pFixedInfo
);
551 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
554 VOID
Release(LPTSTR Index
)
556 IP_ADAPTER_INDEX_MAP AdapterInfo
;
560 /* if interface is not given, query GetInterfaceInfo */
563 PIP_INTERFACE_INFO pInfo
= NULL
;
564 ULONG ulOutBufLen
= 0;
566 if (GetInterfaceInfo(pInfo
, &ulOutBufLen
) == ERROR_INSUFFICIENT_BUFFER
)
568 pInfo
= (IP_INTERFACE_INFO
*)HeapAlloc(ProcessHeap
, 0, ulOutBufLen
);
572 if (GetInterfaceInfo(pInfo
, &ulOutBufLen
) == NO_ERROR
)
574 for (i
= 0; i
< pInfo
->NumAdapters
; i
++)
576 CopyMemory(&AdapterInfo
, &pInfo
->Adapter
[i
], sizeof(IP_ADAPTER_INDEX_MAP
));
577 _tprintf(_T("name - %S\n"), pInfo
->Adapter
[i
].Name
);
579 /* Call IpReleaseAddress to release the IP address on the specified adapter. */
580 if ((ret
= IpReleaseAddress(&AdapterInfo
)) != NO_ERROR
)
582 _tprintf(_T("\nAn error occured while releasing interface %S : \n"), AdapterInfo
.Name
);
583 DoFormatMessage(ret
);
587 HeapFree(ProcessHeap
, 0, pInfo
);
592 HeapFree(ProcessHeap
, 0, pInfo
);
606 * we need to be able to release connections by name with support for globbing
607 * i.e. ipconfig /release Eth* will release all cards starting with Eth...
608 * ipconfig /release *con* will release all cards with 'con' in their name
616 VOID
Renew(LPTSTR Index
)
618 IP_ADAPTER_INDEX_MAP AdapterInfo
;
621 /* if interface is not given, query GetInterfaceInfo */
624 PIP_INTERFACE_INFO pInfo
;
625 ULONG ulOutBufLen
= 0;
627 pInfo
= (IP_INTERFACE_INFO
*)HeapAlloc(ProcessHeap
, 0, sizeof(IP_INTERFACE_INFO
));
630 _tprintf(_T("memory allocation error"));
634 /* Make an initial call to GetInterfaceInfo to get
635 * the necessary size into the ulOutBufLen variable */
636 if ( GetInterfaceInfo(pInfo
, &ulOutBufLen
) == ERROR_INSUFFICIENT_BUFFER
)
638 HeapFree(ProcessHeap
, 0, pInfo
);
639 pInfo
= (IP_INTERFACE_INFO
*)HeapAlloc(ProcessHeap
, 0, ulOutBufLen
);
642 _tprintf(_T("memory allocation error"));
647 /* Make a second call to GetInterfaceInfo to get the actual data we want */
648 if (GetInterfaceInfo(pInfo
, &ulOutBufLen
) == NO_ERROR
)
650 for (i
= 0; i
< pInfo
->NumAdapters
; i
++)
652 CopyMemory(&AdapterInfo
, &pInfo
->Adapter
[i
], sizeof(IP_ADAPTER_INDEX_MAP
));
653 _tprintf(_T("name - %S\n"), pInfo
->Adapter
[i
].Name
);
656 /* Call IpRenewAddress to renew the IP address on the specified adapter. */
657 if (IpRenewAddress(&AdapterInfo
) != NO_ERROR
)
659 _tprintf(_T("\nAn error occured while renew interface %s : "), _T("*name*"));
666 _tprintf(_T("\nGetInterfaceInfo failed : "));
670 HeapFree(ProcessHeap
, 0, pInfo
);
676 * we need to be able to renew connections by name with support for globbing
677 * i.e. ipconfig /renew Eth* will renew all cards starting with Eth...
678 * ipconfig /renew *con* will renew all cards with 'con' in their name
691 LPTSTR lpName
= (LPTSTR
)MAKEINTRESOURCE((IDS_USAGE
>> 4) + 1);
693 hRes
= FindResource(hInstance
,
698 if ((Size
= SizeofResource(hInstance
,
701 lpUsage
= (LPTSTR
)HeapAlloc(ProcessHeap
,
707 if (LoadStringAndOem(hInstance
,
712 _tprintf(_T("%s"), lpUsage
);
720 int main(int argc
, char *argv
[])
724 BOOL DoRelease
=FALSE
;
726 BOOL DoFlushdns
=FALSE
;
727 BOOL DoRegisterdns
=FALSE
;
728 BOOL DoDisplaydns
=FALSE
;
729 BOOL DoShowclassid
=FALSE
;
730 BOOL DoSetclassid
=FALSE
;
732 hInstance
= GetModuleHandle(NULL
);
733 ProcessHeap
= GetProcessHeap();
735 /* Parse command line for options we have been given. */
736 if ( (argc
> 1)&&(argv
[1][0]=='/' || argv
[1][0]=='-') )
738 if( !_tcsicmp( &argv
[1][1], _T("?") ))
742 else if( !_tcsnicmp( &argv
[1][1], _T("ALL"), _tcslen(&argv
[1][1]) ))
746 else if( !_tcsnicmp( &argv
[1][1], _T("RELEASE"), _tcslen(&argv
[1][1]) ))
750 else if( ! _tcsnicmp( &argv
[1][1], _T("RENEW"), _tcslen(&argv
[1][1]) ))
754 else if( ! _tcsnicmp( &argv
[1][1], _T("FLUSHDNS"), _tcslen(&argv
[1][1]) ))
758 else if( ! _tcsnicmp( &argv
[1][1], _T("FLUSHREGISTERDNS"), _tcslen(&argv
[1][1]) ))
760 DoRegisterdns
= TRUE
;
762 else if( ! _tcsnicmp( &argv
[1][1], _T("DISPLAYDNS"), _tcslen(&argv
[1][1]) ))
766 else if( ! _tcsnicmp( &argv
[1][1], _T("SHOWCLASSID"), _tcslen(&argv
[1][1]) ))
768 DoShowclassid
= TRUE
;
770 else if( ! _tcsnicmp( &argv
[1][1], _T("SETCLASSID"), _tcslen(&argv
[1][1]) ))
778 case 1: /* Default behaviour if no options are given*/
781 case 2: /* Process all the options that take no parameters */
791 _tprintf(_T("\nSorry /flushdns is not implemented yet\n"));
792 else if (DoRegisterdns
)
793 _tprintf(_T("\nSorry /registerdns is not implemented yet\n"));
794 else if (DoDisplaydns
)
795 _tprintf(_T("\nSorry /displaydns is not implemented yet\n"));
799 case 3: /* Process all the options that can have 1 parameter */
801 _tprintf(_T("\nSorry /release [adapter] is not implemented yet\n"));
804 _tprintf(_T("\nSorry /renew [adapter] is not implemented yet\n"));
805 else if (DoShowclassid
)
806 _tprintf(_T("\nSorry /showclassid adapter is not implemented yet\n"));
807 else if (DoSetclassid
)
808 _tprintf(_T("\nSorry /setclassid adapter is not implemented yet\n"));
812 case 4: /* Process all the options that can have 2 parameters */
814 _tprintf(_T("\nSorry /setclassid adapter [classid]is not implemented yet\n"));