Created framework for new network utility applications.
[reactos.git] / rosapps / 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 #ifdef __GNUC__
14 #undef WINAPI
15 #define WINAPI
16 #endif
17
18 #include <iptypes.h>
19 #include <ipexport.h>
20 #include <iphlpapi.h>
21
22 #ifdef _DEBUG
23 #include "trace.h"
24 #endif
25
26 ////////////////////////////////////////////////////////////////////////////////
27
28 /* imported from iphlpapi.dll
29
30 GetAdapterOrderMap
31 GetInterfaceInfo
32 GetIpStatsFromStack
33 NhGetInterfaceNameFromGuid
34 NhpAllocateAndGetInterfaceInfoFromStack
35
36 */
37
38 TCHAR* GetNodeTypeName(int nNodeType)
39 {
40 switch (nNodeType) {
41 case 0:
42 return _T("zero");
43 case 1:
44 return _T("one");
45 case 2:
46 return _T("two");
47 case 3:
48 return _T("three");
49 case 4:
50 return _T("mixed");
51 default:
52 return _T("unknown");
53 }
54 }
55
56 void ShowNetworkFixedInfo()
57 {
58 FIXED_INFO FixedInfo;
59 ULONG OutBufLen = sizeof(FIXED_INFO);
60 DWORD result;
61
62
63 result = GetNetworkParams(&FixedInfo, &OutBufLen);
64 if (result == ERROR_SUCCESS) {
65 printf("\tHostName. . . . . . . . . . . : %s\n", FixedInfo.HostName);
66 printf("\tDomainName. . . . . . . . . . : %s\n", FixedInfo.DomainName);
67 _tprintf(_T("\tNodeType. . . . . . . . . . . : %d (%s)\n"), FixedInfo.NodeType, GetNodeTypeName(FixedInfo.NodeType));
68 printf("\tScopeId . . . . . . . . . . . : %s\n", FixedInfo.ScopeId);
69 _tprintf(_T("\tEnableRouting . . . . . . . . : %s\n"), FixedInfo.EnableRouting ? _T("yes") : _T("no"));
70 _tprintf(_T("\tEnableProxy . . . . . . . . . : %s\n"), FixedInfo.EnableProxy ? _T("yes") : _T("no"));
71 _tprintf(_T("\tEnableDns . . . . . . . . . . : %s\n"), FixedInfo.EnableDns ? _T("yes") : _T("no"));
72 _tprintf(_T("\n"));
73 //_tprintf(_T("\n"), );
74 //_tprintf(_T("GetNetworkParams() returned with %d\n"), pIfTable->NumAdapters);
75
76 // _tprintf(_T("\tConnection specific DNS suffix: %s\n"), FixedInfo.EnableDns ? _T("yes") : _T("no"));
77
78 } else {
79 switch (result) {
80 case ERROR_BUFFER_OVERFLOW:
81 _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"));
82 break;
83 case ERROR_INVALID_PARAMETER:
84 _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"));
85 break;
86 case ERROR_NO_DATA:
87 _tprintf(_T("No adapter information exists for the local computer\n"));
88 break;
89 case ERROR_NOT_SUPPORTED:
90 _tprintf(_T("This function is not supported on the operating system in use on the local system\n"));
91 break;
92 default:
93 _tprintf(_T("0x%08X - Use FormatMessage to obtain the message string for the returned error\n"), result);
94 break;
95 }
96 }
97 }
98
99 void ShowNetworkInterfaces()
100 {
101 IP_INTERFACE_INFO* pIfTable = NULL;
102 DWORD result;
103 DWORD dwNumIf;
104 DWORD dwOutBufLen = 0;
105
106 if ((result = GetNumberOfInterfaces(&dwNumIf)) != NO_ERROR) {
107 _tprintf(_T("GetNumberOfInterfaces() failed with code 0x%08X - Use FormatMessage to obtain the message string for the returned error\n"), result);
108 return;
109 }
110
111 result = GetInterfaceInfo(pIfTable, &dwOutBufLen);
112 // dwOutBufLen = sizeof(IP_INTERFACE_INFO) + dwNumIf * sizeof(IP_ADAPTER_INDEX_MAP);
113 // _tprintf(_T("GetNumberOfInterfaces() returned %d, dwOutBufLen %d\n"), dwNumIf, dwOutBufLen);
114 // _tprintf(_T("sizeof(IP_INTERFACE_INFO) %d, sizeof(IP_ADAPTER_INDEX_MAP) %d\n"), sizeof(IP_INTERFACE_INFO), sizeof(IP_ADAPTER_INDEX_MAP));
115
116 pIfTable = (IP_INTERFACE_INFO*)malloc(dwOutBufLen);
117 if (!pIfTable) {
118 _tprintf(_T("ERROR: failed to allocate 0x%08X bytes of memory\n"), dwOutBufLen);
119 return;
120 }
121 result = GetInterfaceInfo(pIfTable, &dwOutBufLen);
122 if (result == NO_ERROR) {
123 int i;
124 //_tprintf(_T("GetInterfaceInfo() returned with %d adaptor entries\n"), pIfTable->NumAdapters);
125 for (i = 0; i < pIfTable->NumAdapters; i++) {
126 wprintf(L"[%d] %s\n", i, pIfTable->Adapter[i].Name);
127
128 // \DEVICE\TCPIP_{DB0E61C1-3498-4C5F-B599-59CDE8A1E357}
129 // \DEVICE\TCPIP_{BD445697-0945-4591-AE7F-2AB0F383CA87}
130 // \DEVICE\TCPIP_{6D87DC08-6BC5-4E78-AB5F-18CAB785CFFE}
131
132 //HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NetBT\Parameters\Interfaces\Tcpip_{DB0E61C1-3498-4C5F-B599-59CDE8A1E357}
133 }
134 } else {
135 switch (result) {
136 case ERROR_INVALID_PARAMETER:
137 _tprintf(_T("The dwOutBufLen parameter is NULL, or GetInterfaceInterface is unable to write to the memory pointed to by the dwOutBufLen parameter\n"));
138 break;
139 case ERROR_INSUFFICIENT_BUFFER:
140 _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"));
141 _tprintf(_T("\tdwOutBufLen: %d\n"), dwOutBufLen);
142 break;
143 case ERROR_NOT_SUPPORTED:
144 _tprintf(_T("This function is not supported on the operating system in use on the local system\n"));
145 break;
146 default:
147 _tprintf(_T("0x%08X - Use FormatMessage to obtain the message string for the returned error\n"), result);
148 break;
149 }
150 }
151 free(pIfTable);
152 }
153
154 const char szUsage[] = { "USAGE:\n" \
155 " ipconfig [/? | /all | /release [adapter] | /renew [adapter]\n" \
156 " | /flushdns | /registerdns\n" \
157 " | /showclassid adapter\n" \
158 " | /showclassid adapter\n" \
159 " | /setclassid adapter [classidtoset] ]\n" \
160 "\n" \
161 "adapter Full name or pattern with '*' and '?' to 'match',\n" \
162 " * matches any character, ? matches one character.\n" \
163 " Options\n" \
164 " /? Display this help message.\n" \
165 " /all Display full configuration information.\n" \
166 " /release Release the IP address for the specified adapter.\n" \
167 " /renew Renew the IP address for the specified adapter.\n" \
168 " /flushdns Purges the DNS Resolver cache.\n" \
169 " /registerdns Refreshes all DHCP leases and re-registers DNS names\n" \
170 " /displaydns Display the contents of the DNS Resolver Cache.\n" \
171 " /showclassid Displays all the dhcp class IDs allowed for adapter.\n" \
172 " /setclassid Modifies the dhcp class id.\n" \
173 "\n" \
174 "The default is to display only the IP address, subnet mask and\n" \
175 "default gateway for each adapter bound to TCP/IP.\n"
176 };
177
178 /*
179 "\n" \
180 "For Release and Renew, if no adapter name is specified, then the IP address\n" \
181 "leases for all adapters bound to TCP/IP will be released or renewed.\n" \
182 "\n" \
183 "For SetClassID, if no class id is specified, then the classid is removed.\n" \
184 "\n" \
185 "Examples:\n" \
186 " > ipconfig ... Show information.\n" \
187 " > ipconfig /all ... Show detailed information\n" \
188 " > ipconfig /renew ... renew all adapaters\n" \
189 " > ipconfig /renew EL* ... renew adapters named EL....\n" \
190 " > ipconfig /release *ELINK?21* ... release all matching adapters,\n" \
191 eg. ELINK-21, myELELINKi21adapter.\n"
192 */
193
194 void usage(void)
195 {
196 fputs(szUsage, stderr);
197 }
198
199
200 int main(int argc, char *argv[])
201 {
202 if (argc > 1) {
203 usage();
204 return 1;
205 }
206 _tprintf(_T("ReactOS IP Configuration\n"));
207 ShowNetworkFixedInfo();
208 ShowNetworkInterfaces();
209 return 0;
210 }
211