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
32 int LoadStringAndOem(HINSTANCE hInst
,
41 szTmp
= (LPTSTR
)HeapAlloc(ProcessHeap
, 0, byteSize
);
46 res
= LoadString(hInst
, uID
, szTmp
, byteSize
);
47 CharToOem(szTmp
, szNode
);
48 HeapFree(ProcessHeap
, 0, szTmp
);
52 LPTSTR
GetNodeTypeName(UINT NodeType
)
54 static TCHAR szNode
[14];
59 if (!LoadStringAndOem(hInstance
, IDS_BCAST
, szNode
, sizeof(szNode
)))
64 if (!LoadStringAndOem(hInstance
, IDS_P2P
, szNode
, sizeof(szNode
)))
69 if (!LoadStringAndOem(hInstance
, IDS_MIXED
, szNode
, sizeof(szNode
)))
74 if (!LoadStringAndOem(hInstance
, IDS_HYBRID
, szNode
, sizeof(szNode
)))
79 if (!LoadStringAndOem(hInstance
, IDS_UNKNOWN
, szNode
, sizeof(szNode
)))
88 LPTSTR
GetInterfaceTypeName(UINT InterfaceType
)
90 static TCHAR szIntType
[25];
92 switch (InterfaceType
)
94 case MIB_IF_TYPE_OTHER
:
95 if (!LoadStringAndOem(hInstance
, IDS_OTHER
, szIntType
, sizeof(szIntType
)))
99 case MIB_IF_TYPE_ETHERNET
:
100 if (!LoadStringAndOem(hInstance
, IDS_ETH
, szIntType
, sizeof(szIntType
)))
104 case MIB_IF_TYPE_TOKENRING
:
105 if (!LoadStringAndOem(hInstance
, IDS_TOKEN
, szIntType
, sizeof(szIntType
)))
109 case MIB_IF_TYPE_FDDI
:
110 if (!LoadStringAndOem(hInstance
, IDS_FDDI
, szIntType
, sizeof(szIntType
)))
114 case MIB_IF_TYPE_PPP
:
115 if (!LoadStringAndOem(hInstance
, IDS_PPP
, szIntType
, sizeof(szIntType
)))
119 case MIB_IF_TYPE_LOOPBACK
:
120 if (!LoadStringAndOem(hInstance
, IDS_LOOP
, szIntType
, sizeof(szIntType
)))
124 case MIB_IF_TYPE_SLIP
:
125 if (!LoadStringAndOem(hInstance
, IDS_SLIP
, szIntType
, sizeof(szIntType
)))
130 if (!LoadStringAndOem(hInstance
, IDS_UNKNOWN
, szIntType
, sizeof(szIntType
)))
139 /* print MAC address */
140 PTCHAR
PrintMacAddr(PBYTE Mac
)
142 static TCHAR MacAddr
[20];
144 _stprintf(MacAddr
, _T("%02x-%02x-%02x-%02x-%02x-%02x"),
145 Mac
[0], Mac
[1], Mac
[2], Mac
[3], Mac
[4], Mac
[5]);
151 VOID
DoFormatMessage(LONG ErrorCode
)
157 ErrorCode
= GetLastError();
159 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|
160 FORMAT_MESSAGE_FROM_SYSTEM
|
161 FORMAT_MESSAGE_IGNORE_INSERTS
,
164 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), /* Default language */
169 _tprintf(_T("%s"), (LPTSTR
)lpMsgBuf
);
175 LPTSTR
GetConnectionType(LPTSTR lpClass
)
178 LPTSTR ConType
= NULL
;
179 LPTSTR ConTypeTmp
= NULL
;
181 LPTSTR PrePath
= _T("SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\");
182 LPTSTR PostPath
= _T("\\Connection");
187 /* don't overflow the buffer */
188 PathSize
= lstrlen(PrePath
) + lstrlen(lpClass
) + lstrlen(PostPath
) + 1;
192 wsprintf(Path
, _T("%s%s%s"), PrePath
, lpClass
, PostPath
);
194 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
198 &hKey
) == ERROR_SUCCESS
)
200 if(RegQueryValueEx(hKey
,
205 &dwDataSize
) == ERROR_SUCCESS
)
207 ConTypeTmp
= (LPTSTR
)HeapAlloc(ProcessHeap
,
211 ConType
= (LPTSTR
)HeapAlloc(ProcessHeap
,
214 if (ConType
&& ConTypeTmp
)
216 if(RegQueryValueEx(hKey
,
221 &dwDataSize
) != ERROR_SUCCESS
)
223 HeapFree(ProcessHeap
,
227 HeapFree(ProcessHeap
,
234 if (ConType
) CharToOem(ConTypeTmp
, ConType
);
246 LPTSTR
GetConnectionDescription(LPTSTR lpClass
)
248 HKEY hBaseKey
= NULL
;
249 HKEY hClassKey
= NULL
;
250 LPTSTR lpKeyClass
= NULL
;
251 LPTSTR lpConDesc
= NULL
;
252 LPTSTR lpPath
= NULL
;
253 TCHAR szPrePath
[] = _T("SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}\\");
258 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
262 &hBaseKey
) != ERROR_SUCCESS
)
274 if ((Status
= RegEnumKeyEx(hBaseKey
,
281 NULL
)) != ERROR_SUCCESS
)
283 if (Status
== ERROR_NO_MORE_ITEMS
)
285 DoFormatMessage(Status
);
293 PathSize
= lstrlen(szPrePath
) + lstrlen(szName
) + 1;
294 lpPath
= (LPTSTR
)HeapAlloc(ProcessHeap
,
296 PathSize
* sizeof(TCHAR
));
300 wsprintf(lpPath
, _T("%s%s"), szPrePath
, szName
);
302 //MessageBox(NULL, lpPath, NULL, 0);
304 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
,
308 &hClassKey
) != ERROR_SUCCESS
)
313 HeapFree(ProcessHeap
, 0, lpPath
);
316 if(RegQueryValueEx(hClassKey
,
317 _T("NetCfgInstanceId"),
321 &dwDataSize
) == ERROR_SUCCESS
)
323 lpKeyClass
= (LPTSTR
)HeapAlloc(ProcessHeap
,
326 if (lpKeyClass
== NULL
)
329 if(RegQueryValueEx(hClassKey
,
330 _T("NetCfgInstanceId"),
334 &dwDataSize
) != ERROR_SUCCESS
)
336 HeapFree(ProcessHeap
, 0, lpKeyClass
);
344 if (!lstrcmp(lpClass
, lpKeyClass
))
346 HeapFree(ProcessHeap
, 0, lpKeyClass
);
349 if(RegQueryValueEx(hClassKey
,
354 &dwDataSize
) == ERROR_SUCCESS
)
356 lpConDesc
= (LPTSTR
)HeapAlloc(ProcessHeap
,
359 if (lpConDesc
== NULL
)
362 if(RegQueryValueEx(hClassKey
,
367 &dwDataSize
) != ERROR_SUCCESS
)
381 if (hBaseKey
!= NULL
)
382 RegCloseKey(hBaseKey
);
383 if (hClassKey
!= NULL
)
384 RegCloseKey(hClassKey
);
385 if (lpConDesc
!= NULL
)
386 HeapFree(ProcessHeap
, 0, lpPath
);
387 if (lpConDesc
!= NULL
)
388 HeapFree(ProcessHeap
, 0, lpKeyClass
);
394 VOID
ShowInfo(BOOL bAll
)
397 PIP_ADAPTER_INFO pAdapterInfo
= NULL
;
398 PIP_ADAPTER_INFO pAdapter
= NULL
;
399 ULONG adaptOutBufLen
= 0;
400 PFIXED_INFO pFixedInfo
= NULL
;
401 ULONG netOutBufLen
= 0;
404 /* call GetAdaptersInfo to obtain the adapter info */
405 ret
= GetAdaptersInfo(pAdapterInfo
, &adaptOutBufLen
);
406 if (ret
== ERROR_BUFFER_OVERFLOW
)
408 pAdapterInfo
= (IP_ADAPTER_INFO
*)HeapAlloc(ProcessHeap
, 0, adaptOutBufLen
);
409 if (pAdapterInfo
== NULL
)
412 ret
= GetAdaptersInfo(pAdapterInfo
, &adaptOutBufLen
);
416 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
422 if( ERROR_NO_DATA
!= ret
)
429 /* call GetNetworkParams to obtain the network info */
430 if(GetNetworkParams(pFixedInfo
, &netOutBufLen
) == ERROR_BUFFER_OVERFLOW
)
432 pFixedInfo
= (FIXED_INFO
*)HeapAlloc(ProcessHeap
, 0, netOutBufLen
);
433 if (pFixedInfo
== NULL
)
436 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
439 if (GetNetworkParams(pFixedInfo
, &netOutBufLen
) != NO_ERROR
)
443 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
444 HeapFree(ProcessHeap
, 0, pFixedInfo
);
451 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
456 pAdapter
= pAdapterInfo
;
458 _tprintf(_T("\nReactOS IP Configuration\n\n"));
461 _tprintf(_T("\tHost Name . . . . . . . . . . . . : %s\n"), pFixedInfo
->HostName
);
462 _tprintf(_T("\tPrimary DNS Suffix. . . . . . . . : \n"));
463 _tprintf(_T("\tNode Type . . . . . . . . . . . . : %s\n"), GetNodeTypeName(pFixedInfo
->NodeType
));
464 if (pFixedInfo
->EnableRouting
)
465 _tprintf(_T("\tIP Routing Enabled. . . . . . . . : Yes\n"));
467 _tprintf(_T("\tIP Routing Enabled. . . . . . . . : No\n"));
468 if (pAdapter
&& pAdapter
->HaveWins
)
469 _tprintf(_T("\tWINS Proxy enabled. . . . . . . . : Yes\n"));
471 _tprintf(_T("\tWINS Proxy enabled. . . . . . . . : No\n"));
472 _tprintf(_T("\tDNS Suffix Search List. . . . . . : %s\n"), pFixedInfo
->DomainName
);
477 LPTSTR IntType
, myConType
;
479 mibEntry
.dwIndex
= pAdapter
->Index
;
480 GetIfEntry(&mibEntry
);
482 IntType
= GetInterfaceTypeName(pAdapter
->Type
);
483 myConType
= GetConnectionType(pAdapter
->AdapterName
);
485 _tprintf(_T("\n%s %s: \n\n"), IntType
, myConType
);
487 if (myConType
!= NULL
) HeapFree(ProcessHeap
, 0, myConType
);
489 /* check if the adapter is connected to the media */
490 if (mibEntry
.dwOperStatus
!= MIB_IF_OPER_STATUS_CONNECTED
&& mibEntry
.dwOperStatus
!= MIB_IF_OPER_STATUS_OPERATIONAL
)
492 _tprintf(_T("\tMedia State . . . . . . . . . . . : Media disconnected\n"));
493 pAdapter
= pAdapter
->Next
;
497 _tprintf(_T("\tConnection-specific DNS Suffix. . : %s\n"), pFixedInfo
->DomainName
);
501 LPTSTR lpDesc
= GetConnectionDescription(pAdapter
->AdapterName
);
502 _tprintf(_T("\tDescription . . . . . . . . . . . : %s\n"), lpDesc
);
503 HeapFree(ProcessHeap
, 0, lpDesc
);
504 _tprintf(_T("\tPhysical Address. . . . . . . . . : %s\n"), PrintMacAddr(pAdapter
->Address
));
505 if (pAdapter
->DhcpEnabled
)
506 _tprintf(_T("\tDHCP Enabled. . . . . . . . . . . : Yes\n"));
508 _tprintf(_T("\tDHCP Enabled. . . . . . . . . . . : No\n"));
509 _tprintf(_T("\tAutoconfiguration Enabled . . . . : \n"));
512 _tprintf(_T("\tIP Address. . . . . . . . . . . . : %s\n"), pAdapter
->IpAddressList
.IpAddress
.String
);
513 _tprintf(_T("\tSubnet Mask . . . . . . . . . . . : %s\n"), pAdapter
->IpAddressList
.IpMask
.String
);
514 if (pAdapter
->GatewayList
.IpAddress
.String
[0] != '0')
515 _tprintf(_T("\tDefault Gateway . . . . . . . . . : %s\n"), pAdapter
->GatewayList
.IpAddress
.String
);
517 _tprintf(_T("\tDefault Gateway . . . . . . . . . :\n"));
521 PIP_ADDR_STRING pIPAddr
;
523 if (pAdapter
->DhcpEnabled
)
524 _tprintf(_T("\tDHCP Server . . . . . . . . . . . : %s\n"), pAdapter
->DhcpServer
.IpAddress
.String
);
526 _tprintf(_T("\tDNS Servers . . . . . . . . . . . : "));
527 _tprintf(_T("%s\n"), pFixedInfo
->DnsServerList
.IpAddress
.String
);
528 pIPAddr
= pFixedInfo
->DnsServerList
.Next
;
531 _tprintf(_T("\t\t\t\t\t %s\n"), pIPAddr
->IpAddress
.String
);
532 pIPAddr
= pIPAddr
->Next
;
535 if (pAdapter
->HaveWins
)
537 _tprintf(_T("\tPrimary WINS Server . . . . . . . : %s\n"), pAdapter
->PrimaryWinsServer
.IpAddress
.String
);
538 _tprintf(_T("\tSecondard WINS Server . . . . . . : %s\n"), pAdapter
->SecondaryWinsServer
.IpAddress
.String
);
541 if (pAdapter
->DhcpEnabled
)
543 _tprintf(_T("\tLease Obtained. . . . . . . . . . : %s"), _tasctime(localtime(&pAdapter
->LeaseObtained
)));
544 _tprintf(_T("\tLease Expires . . . . . . . . . . : %s"), _tasctime(localtime(&pAdapter
->LeaseExpires
)));
549 pAdapter
= pAdapter
->Next
;
553 HeapFree(ProcessHeap
, 0, pFixedInfo
);
555 HeapFree(ProcessHeap
, 0, pAdapterInfo
);
558 VOID
Release(LPTSTR Index
)
560 IP_ADAPTER_INDEX_MAP AdapterInfo
;
564 /* if interface is not given, query GetInterfaceInfo */
567 PIP_INTERFACE_INFO pInfo
= NULL
;
568 ULONG ulOutBufLen
= 0;
570 if (GetInterfaceInfo(pInfo
, &ulOutBufLen
) == ERROR_INSUFFICIENT_BUFFER
)
572 pInfo
= (IP_INTERFACE_INFO
*)HeapAlloc(ProcessHeap
, 0, ulOutBufLen
);
576 if (GetInterfaceInfo(pInfo
, &ulOutBufLen
) == NO_ERROR
)
578 for (i
= 0; i
< pInfo
->NumAdapters
; i
++)
580 CopyMemory(&AdapterInfo
, &pInfo
->Adapter
[i
], sizeof(IP_ADAPTER_INDEX_MAP
));
581 _tprintf(_T("name - %S\n"), pInfo
->Adapter
[i
].Name
);
583 /* Call IpReleaseAddress to release the IP address on the specified adapter. */
584 if ((ret
= IpReleaseAddress(&AdapterInfo
)) != NO_ERROR
)
586 _tprintf(_T("\nAn error occured while releasing interface %S : \n"), AdapterInfo
.Name
);
587 DoFormatMessage(ret
);
591 HeapFree(ProcessHeap
, 0, pInfo
);
596 HeapFree(ProcessHeap
, 0, pInfo
);
610 * we need to be able to release connections by name with support for globbing
611 * i.e. ipconfig /release Eth* will release all cards starting with Eth...
612 * ipconfig /release *con* will release all cards with 'con' in their name
620 VOID
Renew(LPTSTR Index
)
622 IP_ADAPTER_INDEX_MAP AdapterInfo
;
625 /* if interface is not given, query GetInterfaceInfo */
628 PIP_INTERFACE_INFO pInfo
;
629 ULONG ulOutBufLen
= 0;
631 pInfo
= (IP_INTERFACE_INFO
*)HeapAlloc(ProcessHeap
, 0, sizeof(IP_INTERFACE_INFO
));
634 _tprintf(_T("memory allocation error"));
638 /* Make an initial call to GetInterfaceInfo to get
639 * the necessary size into the ulOutBufLen variable */
640 if ( GetInterfaceInfo(pInfo
, &ulOutBufLen
) == ERROR_INSUFFICIENT_BUFFER
)
642 HeapFree(ProcessHeap
, 0, pInfo
);
643 pInfo
= (IP_INTERFACE_INFO
*)HeapAlloc(ProcessHeap
, 0, ulOutBufLen
);
646 _tprintf(_T("memory allocation error"));
651 /* Make a second call to GetInterfaceInfo to get the actual data we want */
652 if (GetInterfaceInfo(pInfo
, &ulOutBufLen
) == NO_ERROR
)
654 for (i
= 0; i
< pInfo
->NumAdapters
; i
++)
656 CopyMemory(&AdapterInfo
, &pInfo
->Adapter
[i
], sizeof(IP_ADAPTER_INDEX_MAP
));
657 _tprintf(_T("name - %S\n"), pInfo
->Adapter
[i
].Name
);
660 /* Call IpRenewAddress to renew the IP address on the specified adapter. */
661 if (IpRenewAddress(&AdapterInfo
) != NO_ERROR
)
663 _tprintf(_T("\nAn error occured while renew interface %s : "), _T("*name*"));
670 _tprintf(_T("\nGetInterfaceInfo failed : "));
674 HeapFree(ProcessHeap
, 0, pInfo
);
680 * we need to be able to renew connections by name with support for globbing
681 * i.e. ipconfig /renew Eth* will renew all cards starting with Eth...
682 * ipconfig /renew *con* will renew all cards with 'con' in their name
695 LPTSTR lpName
= (LPTSTR
)MAKEINTRESOURCE((IDS_USAGE
>> 4) + 1);
697 hRes
= FindResource(hInstance
,
702 if ((Size
= SizeofResource(hInstance
,
705 lpUsage
= (LPTSTR
)HeapAlloc(ProcessHeap
,
711 if (LoadStringAndOem(hInstance
,
716 _tprintf(_T("%s"), lpUsage
);
724 int main(int argc
, char *argv
[])
728 BOOL DoRelease
=FALSE
;
730 BOOL DoFlushdns
=FALSE
;
731 BOOL DoRegisterdns
=FALSE
;
732 BOOL DoDisplaydns
=FALSE
;
733 BOOL DoShowclassid
=FALSE
;
734 BOOL DoSetclassid
=FALSE
;
736 hInstance
= GetModuleHandle(NULL
);
737 ProcessHeap
= GetProcessHeap();
739 /* Parse command line for options we have been given. */
740 if ( (argc
> 1)&&(argv
[1][0]=='/' || argv
[1][0]=='-') )
742 if( !_tcsicmp( &argv
[1][1], _T("?") ))
746 else if( !_tcsnicmp( &argv
[1][1], _T("ALL"), _tcslen(&argv
[1][1]) ))
750 else if( !_tcsnicmp( &argv
[1][1], _T("RELEASE"), _tcslen(&argv
[1][1]) ))
754 else if( ! _tcsnicmp( &argv
[1][1], _T("RENEW"), _tcslen(&argv
[1][1]) ))
758 else if( ! _tcsnicmp( &argv
[1][1], _T("FLUSHDNS"), _tcslen(&argv
[1][1]) ))
762 else if( ! _tcsnicmp( &argv
[1][1], _T("FLUSHREGISTERDNS"), _tcslen(&argv
[1][1]) ))
764 DoRegisterdns
= TRUE
;
766 else if( ! _tcsnicmp( &argv
[1][1], _T("DISPLAYDNS"), _tcslen(&argv
[1][1]) ))
770 else if( ! _tcsnicmp( &argv
[1][1], _T("SHOWCLASSID"), _tcslen(&argv
[1][1]) ))
772 DoShowclassid
= TRUE
;
774 else if( ! _tcsnicmp( &argv
[1][1], _T("SETCLASSID"), _tcslen(&argv
[1][1]) ))
782 case 1: /* Default behaviour if no options are given*/
785 case 2: /* Process all the options that take no parameters */
795 _tprintf(_T("\nSorry /flushdns is not implemented yet\n"));
796 else if (DoRegisterdns
)
797 _tprintf(_T("\nSorry /registerdns is not implemented yet\n"));
798 else if (DoDisplaydns
)
799 _tprintf(_T("\nSorry /displaydns is not implemented yet\n"));
803 case 3: /* Process all the options that can have 1 parameter */
805 _tprintf(_T("\nSorry /release [adapter] is not implemented yet\n"));
808 _tprintf(_T("\nSorry /renew [adapter] is not implemented yet\n"));
809 else if (DoShowclassid
)
810 _tprintf(_T("\nSorry /showclassid adapter is not implemented yet\n"));
811 else if (DoSetclassid
)
812 _tprintf(_T("\nSorry /setclassid adapter is not implemented yet\n"));
816 case 4: /* Process all the options that can have 2 parameters */
818 _tprintf(_T("\nSorry /setclassid adapter [classid]is not implemented yet\n"));