2004-11-21 Casper S. Hornstrup <chorns@users.sourceforge.net>
[reactos.git] / reactos / apps / utils / net / ipconfig / ipconfig.c
1 /*
2 * ipconfig - display IP stack parameters.
3 *
4 * This source code is in the PUBLIC DOMAIN and has NO WARRANTY.
5 *
6 * Robert Dickenson <robd@reactos.org>, August 15, 2002.
7 */
8 #include <stdio.h>
9 #include <windows.h>
10 #include <tchar.h>
11 #include <time.h>
12
13 #include <iptypes.h>
14 #include <ipexport.h>
15 #include <iphlpapi.h>
16
17 #ifdef _DEBUG
18 #include "trace.h"
19 #endif
20
21 ////////////////////////////////////////////////////////////////////////////////
22
23 /* imported from iphlpapi.dll
24
25 GetAdapterOrderMap
26 GetInterfaceInfo
27 GetIpStatsFromStack
28 NhGetInterfaceNameFromGuid
29 NhpAllocateAndGetInterfaceInfoFromStack
30
31 */
32
33 static TCHAR* GetNodeTypeName(int nNodeType)
34 {
35 switch (nNodeType) {
36 case 0: return _T("zero");
37 case 1: return _T("one");
38 case 2: return _T("two");
39 case 3: return _T("three");
40 case 4: return _T("mixed");
41 default: return _T("unknown");
42 }
43 }
44
45 static void ShowNetworkFixedInfo()
46 {
47 FIXED_INFO* pFixedInfo = NULL;
48 ULONG OutBufLen = 0;
49 DWORD result;
50
51 result = GetNetworkParams(NULL, &OutBufLen);
52 if (result == ERROR_BUFFER_OVERFLOW) {
53 pFixedInfo = (FIXED_INFO*)malloc(OutBufLen);
54 if (!pFixedInfo) {
55 _tprintf(_T("ERROR: failed to allocate 0x%08X bytes of memory\n"), OutBufLen);
56 return;
57 }
58 } else {
59 _tprintf(_T("ERROR: GetNetworkParams() failed to report required buffer size.\n"));
60 return;
61 }
62
63 result = GetNetworkParams(pFixedInfo, &OutBufLen);
64 if (result == ERROR_SUCCESS) {
65 IP_ADDR_STRING* pIPAddr;
66
67 printf("\tHostName. . . . . . . . . . . : %s\n", pFixedInfo->HostName);
68 printf("\tDomainName. . . . . . . . . . : %s\n", pFixedInfo->DomainName);
69 //
70 printf("\tDNS Servers . . . . . . . . . : %s\n", pFixedInfo->DnsServerList.IpAddress.String);
71 pIPAddr = pFixedInfo->DnsServerList.Next;
72 while (pIPAddr) {
73 printf("\t\t\t\t : %s\n", pIPAddr->IpAddress.String);
74 pIPAddr = pIPAddr->Next;
75 }
76 //
77 _tprintf(_T("\tNodeType. . . . . . . . . . . : %d (%s)\n"), pFixedInfo->NodeType, GetNodeTypeName(pFixedInfo->NodeType));
78 printf("\tScopeId . . . . . . . . . . . : %s\n", pFixedInfo->ScopeId);
79 _tprintf(_T("\tEnableRouting . . . . . . . . : %s\n"), pFixedInfo->EnableRouting ? _T("yes") : _T("no"));
80 _tprintf(_T("\tEnableProxy . . . . . . . . . : %s\n"), pFixedInfo->EnableProxy ? _T("yes") : _T("no"));
81 _tprintf(_T("\tEnableDns . . . . . . . . . . : %s\n"), pFixedInfo->EnableDns ? _T("yes") : _T("no"));
82 _tprintf(_T("\n"));
83 //_tprintf(_T("\n"),);
84 //_tprintf(_T("GetNetworkParams() returned with %d\n"), pIfTable->NumAdapters);
85
86 // _tprintf(_T("\tConnection specific DNS suffix: %s\n"), pFixedInfo->EnableDns ? _T("yes") : _T("no"));
87
88 } else {
89 switch (result) {
90 case ERROR_BUFFER_OVERFLOW:
91 _tprintf(_T("The buffer size indicated by the pOutBufLen parameter is too small to hold the adapter information. The pOutBufLen parameter points to the required size\n"));
92 break;
93 case ERROR_INVALID_PARAMETER:
94 _tprintf(_T("The pOutBufLen parameter is NULL, or the calling process does not have read/write access to the memory pointed to by pOutBufLen, or the calling process does not have write access to the memory pointed to by the pAdapterInfo parameter\n"));
95 break;
96 case ERROR_NO_DATA:
97 _tprintf(_T("No adapter information exists for the local computer\n"));
98 break;
99 case ERROR_NOT_SUPPORTED:
100 _tprintf(_T("This function is not supported on the operating system in use on the local system\n"));
101 break;
102 default:
103 _tprintf(_T("0x%08X - Use FormatMessage to obtain the message string for the returned error\n"), result);
104 break;
105 }
106 }
107 }
108
109 static void ShowNetworkInterfaces()
110 {
111 IP_INTERFACE_INFO* pIfTable = NULL;
112 DWORD result;
113 DWORD dwNumIf;
114 DWORD dwOutBufLen = 0;
115
116 if ((result = GetNumberOfInterfaces(&dwNumIf)) != NO_ERROR) {
117 _tprintf(_T("GetNumberOfInterfaces() failed with code 0x%08X - Use FormatMessage to obtain the message string for the returned error\n"), result);
118 return;
119 } else {
120 _tprintf(_T("GetNumberOfInterfaces() returned %d\n"), dwNumIf);
121 }
122
123 result = GetInterfaceInfo(pIfTable, &dwOutBufLen);
124 // dwOutBufLen = sizeof(IP_INTERFACE_INFO) + dwNumIf * sizeof(IP_ADAPTER_INDEX_MAP);
125 // _tprintf(_T("GetNumberOfInterfaces() returned %d, dwOutBufLen %d\n"), dwNumIf, dwOutBufLen);
126 // _tprintf(_T("sizeof(IP_INTERFACE_INFO) %d, sizeof(IP_ADAPTER_INDEX_MAP) %d\n"), sizeof(IP_INTERFACE_INFO), sizeof(IP_ADAPTER_INDEX_MAP));
127
128 pIfTable = (IP_INTERFACE_INFO*)malloc(dwOutBufLen);
129 if (!pIfTable) {
130 _tprintf(_T("ERROR: failed to allocate 0x%08X bytes of memory\n"), dwOutBufLen);
131 return;
132 }
133 /*
134 typedef struct _IP_ADAPTER_INDEX_MAP {
135 ULONG Index; // adapter index
136 WCHAR Name[MAX_ADAPTER_NAME]; // name of the adapter
137 } IP_ADAPTER_INDEX_MAP, * PIP_ADAPTER_INDEX_MAP;
138
139 typedef struct _IP_INTERFACE_INFO {
140 LONG NumAdapters; // number of adapters in array
141 IP_ADAPTER_INDEX_MAP Adapter[1]; // adapter indices and names
142 } IP_INTERFACE_INFO,*PIP_INTERFACE_INFO;
143 */
144 result = GetInterfaceInfo(pIfTable, &dwOutBufLen);
145 if (result == NO_ERROR) {
146 int i;
147 _tprintf(_T("GetInterfaceInfo() returned with %d adaptor entries\n"), pIfTable->NumAdapters);
148 for (i = 0; i < pIfTable->NumAdapters; i++) {
149 wprintf(L"[%d] %s\n", i + 1, pIfTable->Adapter[i].Name);
150 //wprintf(L"[%d] %s\n", pIfTable->Adapter[i].Index, pIfTable->Adapter[i].Name);
151
152 // \DEVICE\TCPIP_{DB0E61C1-3498-4C5F-B599-59CDE8A1E357}
153 // \DEVICE\TCPIP_{BD445697-0945-4591-AE7F-2AB0F383CA87}
154 // \DEVICE\TCPIP_{6D87DC08-6BC5-4E78-AB5F-18CAB785CFFE}
155
156 //HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NetBT\Parameters\Interfaces\Tcpip_{DB0E61C1-3498-4C5F-B599-59CDE8A1E357}
157 }
158 } else {
159 switch (result) {
160 case ERROR_INVALID_PARAMETER:
161 _tprintf(_T("The dwOutBufLen parameter is NULL, or GetInterfaceInterface is unable to write to the memory pointed to by the dwOutBufLen parameter\n"));
162 break;
163 case ERROR_INSUFFICIENT_BUFFER:
164 _tprintf(_T("The buffer pointed to by the pIfTable parameter is not large enough. The required size is returned in the DWORD variable pointed to by the dwOutBufLen parameter\n"));
165 _tprintf(_T("\tdwOutBufLen: %d\n"), dwOutBufLen);
166 break;
167 case ERROR_NOT_SUPPORTED:
168 _tprintf(_T("This function is not supported on the operating system in use on the local system\n"));
169 break;
170 default:
171 _tprintf(_T("0x%08X - Use FormatMessage to obtain the message string for the returned error\n"), result);
172 break;
173 }
174 }
175 free(pIfTable);
176 }
177
178 /*
179 typedef struct _IP_ADAPTER_INFO {
180 struct _IP_ADAPTER_INFO* Next;
181 DWORD ComboIndex;
182 char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4];
183 1 char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4];
184 UINT AddressLength;
185 2 BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH];
186 DWORD Index;
187 UINT Type;
188 3 UINT DhcpEnabled;
189 5 PIP_ADDR_STRING CurrentIpAddress;
190 IP_ADDR_STRING IpAddressList;
191 7 IP_ADDR_STRING GatewayList;
192 8 IP_ADDR_STRING DhcpServer;
193 BOOL HaveWins;
194 IP_ADDR_STRING PrimaryWinsServer;
195 IP_ADDR_STRING SecondaryWinsServer;
196 a time_t LeaseObtained;
197 b time_t LeaseExpires;
198 } IP_ADAPTER_INFO, *PIP_ADAPTER_INFO;
199 */
200 /*
201 Ethernet adapter VMware Virtual Ethernet Adapter (Network Address Translation (NAT) for VMnet8):
202
203 Connection-specific DNS Suffix . :
204 1 Description . . . . . . . . . . . : VMware Virtual Ethernet Adapter (Network Address Translation (NAT) for VMnet8)
205 2 Physical Address. . . . . . . . . : 00-50-56-C0-00-08
206 3 DHCP Enabled. . . . . . . . . . . : Yes
207 Autoconfiguration Enabled . . . . : Yes
208 5 IP Address. . . . . . . . . . . . : 192.168.136.1
209 Subnet Mask . . . . . . . . . . . : 255.255.255.0
210 7 Default Gateway . . . . . . . . . :
211 8 DHCP Server . . . . . . . . . . . : 192.168.136.254
212 DNS Servers . . . . . . . . . . . :
213 a Lease Obtained. . . . . . . . . . : Monday, 30 December 2002 5:56:53 PM
214 b Lease Expires . . . . . . . . . . : Monday, 30 December 2002 6:26:53 PM
215 */
216 static void ShowAdapterInfo()
217 {
218 IP_ADAPTER_INFO* pAdaptorInfo;
219 ULONG ulOutBufLen;
220 DWORD dwRetVal;
221
222 _tprintf(_T("\nAdaptor Information\t\n"));
223 pAdaptorInfo = (IP_ADAPTER_INFO*)GlobalAlloc(GPTR, sizeof(IP_ADAPTER_INFO));
224 ulOutBufLen = sizeof(IP_ADAPTER_INFO);
225
226 if (ERROR_BUFFER_OVERFLOW == GetAdaptersInfo(pAdaptorInfo, &ulOutBufLen)) {
227 GlobalFree(pAdaptorInfo);
228 pAdaptorInfo = (IP_ADAPTER_INFO*)GlobalAlloc(GPTR, ulOutBufLen);
229 }
230 if (dwRetVal = GetAdaptersInfo(pAdaptorInfo, &ulOutBufLen)) {
231 _tprintf(_T("Call to GetAdaptersInfo failed. Return Value: %08x\n"), dwRetVal);
232 } else {
233 while (pAdaptorInfo) {
234 printf(" AdapterName: %s\n", pAdaptorInfo->AdapterName);
235 printf(" Description: %s\n", pAdaptorInfo->Description);
236 pAdaptorInfo = pAdaptorInfo->Next;
237 }
238 }
239 }
240
241 const char szUsage[] = { "USAGE:\n" \
242 " ipconfig [/? | /all | /release [adapter] | /renew [adapter]\n" \
243 " | /flushdns | /registerdns\n" \
244 " | /showclassid adapter\n" \
245 " | /showclassid adapter\n" \
246 " | /setclassid adapter [classidtoset] ]\n" \
247 "\n" \
248 "adapter Full name or pattern with '*' and '?' to 'match',\n" \
249 " * matches any character, ? matches one character.\n" \
250 " Options\n" \
251 " /? Display this help message.\n" \
252 " /all Display full configuration information.\n" \
253 " /release Release the IP address for the specified adapter.\n" \
254 " /renew Renew the IP address for the specified adapter.\n" \
255 " /flushdns Purges the DNS Resolver cache.\n" \
256 " /registerdns Refreshes all DHCP leases and re-registers DNS names\n" \
257 " /displaydns Display the contents of the DNS Resolver Cache.\n" \
258 " /showclassid Displays all the dhcp class IDs allowed for adapter.\n" \
259 " /setclassid Modifies the dhcp class id.\n" \
260 "\n" \
261 "The default is to display only the IP address, subnet mask and\n" \
262 "default gateway for each adapter bound to TCP/IP.\n"
263 };
264 /*
265 "\n" \
266 "For Release and Renew, if no adapter name is specified, then the IP address\n" \
267 "leases for all adapters bound to TCP/IP will be released or renewed.\n" \
268 "\n" \
269 "For SetClassID, if no class id is specified, then the classid is removed.\n" \
270 "\n" \
271 "Examples:\n" \
272 " > ipconfig ... Show information.\n" \
273 " > ipconfig /all ... Show detailed information\n" \
274 " > ipconfig /renew ... renew all adapaters\n" \
275 " > ipconfig /renew EL* ... renew adapters named EL....\n" \
276 " > ipconfig /release *ELINK?21* ... release all matching adapters,\n" \
277 eg. ELINK-21, myELELINKi21adapter.\n"
278 */
279
280 static void usage(void)
281 {
282 fputs(szUsage, stderr);
283 }
284
285
286 int main(int argc, char *argv[])
287 {
288 // 10.0.0.100 // As of build 0.0.20 this is hardcoded in the ip stack
289
290 if (argc > 1) {
291 usage();
292 return 1;
293 }
294 _tprintf(_T("ReactOS IP Configuration\n"));
295 ShowNetworkFixedInfo();
296 ShowNetworkInterfaces();
297 ShowAdapterInfo();
298 return 0;
299 }