2 * IP configuration utility
4 * Copyright 2008 Andrew Riedi
5 * Copyright 2010 Andrew Nguyen
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include <wine/unicode.h>
29 static int ipconfig_vprintfW(const WCHAR *msg, __ms_va_list va_args)
33 WCHAR msg_buffer[8192];
35 wlen = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, msg, 0, 0, msg_buffer,
36 sizeof(msg_buffer)/sizeof(*msg_buffer), &va_args);
38 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL);
44 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
45 * back to WriteFile(), assuming the console encoding is still the right
48 len = WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen,
50 msgA = HeapAlloc(GetProcessHeap(), 0, len);
54 WideCharToMultiByte(GetConsoleOutputCP(), 0, msg_buffer, wlen, msgA, len,
56 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
57 HeapFree(GetProcessHeap(), 0, msgA);
63 static int CDECL ipconfig_printfW(const WCHAR *msg, ...)
68 __ms_va_start(va_args, msg);
69 len = ipconfig_vprintfW(msg, va_args);
75 static int CDECL ipconfig_message_printfW(int msg, ...)
78 WCHAR msg_buffer[8192];
81 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
82 sizeof(msg_buffer)/sizeof(WCHAR));
84 __ms_va_start(va_args, msg);
85 len = ipconfig_vprintfW(msg_buffer, va_args);
91 static int ipconfig_message(int msg)
93 static const WCHAR formatW[] = {'%','1',0};
94 WCHAR msg_buffer[8192];
96 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
97 sizeof(msg_buffer)/sizeof(WCHAR));
99 return ipconfig_printfW(formatW, msg_buffer);
102 static const WCHAR *iftype_to_string(DWORD type)
104 static WCHAR msg_buffer[50];
110 case IF_TYPE_ETHERNET_CSMACD:
111 /* The loopback adapter appears as an Ethernet device. */
112 case IF_TYPE_SOFTWARE_LOOPBACK:
113 msg = STRING_ETHERNET;
116 msg = STRING_UNKNOWN;
119 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
120 sizeof(msg_buffer)/sizeof(WCHAR));
125 static void print_field(int msg, const WCHAR *value)
127 static const WCHAR formatW[] = {' ',' ',' ',' ','%','1',':',' ','%','2','\n',0};
129 WCHAR field[] = {'.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',
130 ' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ','.',' ',0};
131 WCHAR name_buffer[sizeof(field)/sizeof(WCHAR)];
133 LoadStringW(GetModuleHandleW(NULL), msg, name_buffer, sizeof(name_buffer)/sizeof(WCHAR));
134 memcpy(field, name_buffer, sizeof(WCHAR) * min(strlenW(name_buffer), sizeof(field)/sizeof(WCHAR) - 1));
136 ipconfig_printfW(formatW, field, value);
139 static void print_value(const WCHAR *value)
141 static const WCHAR formatW[] = {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
142 ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
143 ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
144 ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',
147 ipconfig_printfW(formatW, value);
150 static BOOL socket_address_to_string(WCHAR *buf, DWORD len, SOCKET_ADDRESS *addr)
152 return WSAAddressToStringW(addr->lpSockaddr,
153 addr->iSockaddrLength, NULL,
157 static void print_basic_information(void)
159 IP_ADAPTER_ADDRESSES *adapters;
162 if (GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_ALL_GATEWAYS,
163 NULL, NULL, &out) == ERROR_BUFFER_OVERFLOW)
165 adapters = HeapAlloc(GetProcessHeap(), 0, out);
169 if (GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_ALL_GATEWAYS,
170 NULL, adapters, &out) == ERROR_SUCCESS)
172 IP_ADAPTER_ADDRESSES *p;
174 for (p = adapters; p; p = p->Next)
176 static const WCHAR newlineW[] = {'\n',0};
177 static const WCHAR emptyW[] = {0};
179 IP_ADAPTER_UNICAST_ADDRESS *addr;
180 IP_ADAPTER_GATEWAY_ADDRESS_LH *gateway;
183 ipconfig_message_printfW(STRING_ADAPTER_FRIENDLY, iftype_to_string(p->IfType), p->FriendlyName);
184 ipconfig_printfW(newlineW);
185 print_field(STRING_CONN_DNS_SUFFIX, p->DnsSuffix);
187 for (addr = p->FirstUnicastAddress; addr; addr = addr->Next)
189 if (socket_address_to_string(addr_buf, sizeof(addr_buf)/sizeof(WCHAR), &addr->Address))
190 print_field(STRING_IP_ADDRESS, addr_buf);
191 /* FIXME: Output corresponding subnet mask. */
194 if (p->FirstGatewayAddress)
196 if (socket_address_to_string(addr_buf, sizeof(addr_buf)/sizeof(WCHAR), &p->FirstGatewayAddress->Address))
197 print_field(STRING_DEFAULT_GATEWAY, addr_buf);
199 for (gateway = p->FirstGatewayAddress->Next; gateway; gateway = gateway->Next)
201 if (socket_address_to_string(addr_buf, sizeof(addr_buf)/sizeof(WCHAR), &gateway->Address))
202 print_value(addr_buf);
206 print_field(STRING_DEFAULT_GATEWAY, emptyW);
208 ipconfig_printfW(newlineW);
212 HeapFree(GetProcessHeap(), 0, adapters);
216 static const WCHAR *nodetype_to_string(DWORD type)
218 static WCHAR msg_buffer[50];
224 case BROADCAST_NODETYPE:
225 msg = STRING_BROADCAST;
227 case PEER_TO_PEER_NODETYPE:
228 msg = STRING_PEER_TO_PEER;
233 case HYBRID_NODETYPE:
237 msg = STRING_UNKNOWN;
240 LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer,
241 sizeof(msg_buffer)/sizeof(WCHAR));
246 static WCHAR *physaddr_to_string(WCHAR *buf, BYTE *addr, DWORD len)
248 static const WCHAR fmtW[] = {'%','0','2','X','-',0};
249 static const WCHAR fmt2W[] = {'%','0','2','X',0};
258 for (i = 0; i < len - 1; i++)
260 sprintfW(p, fmtW, addr[i]);
263 sprintfW(p, fmt2W, addr[i]);
269 static const WCHAR *boolean_to_string(int value)
271 static WCHAR msg_buffer[15];
273 LoadStringW(GetModuleHandleW(NULL), value ? STRING_YES : STRING_NO,
274 msg_buffer, sizeof(msg_buffer)/sizeof(WCHAR));
279 static void print_full_information(void)
281 static const WCHAR newlineW[] = {'\n',0};
282 static const WCHAR emptyW[] = {0};
285 IP_ADAPTER_ADDRESSES *adapters;
288 if (GetNetworkParams(NULL, &out) == ERROR_BUFFER_OVERFLOW)
290 info = HeapAlloc(GetProcessHeap(), 0, out);
294 if (GetNetworkParams(info, &out) == ERROR_SUCCESS)
296 WCHAR hostnameW[MAX_HOSTNAME_LEN + 4];
298 MultiByteToWideChar(CP_ACP, 0, info->HostName, -1, hostnameW, sizeof(hostnameW)/sizeof(hostnameW[0]));
299 print_field(STRING_HOSTNAME, hostnameW);
301 /* FIXME: Output primary DNS suffix. */
303 print_field(STRING_NODE_TYPE, nodetype_to_string(info->NodeType));
304 print_field(STRING_IP_ROUTING, boolean_to_string(info->EnableRouting));
306 /* FIXME: Output WINS proxy status and DNS suffix search list. */
308 ipconfig_printfW(newlineW);
311 HeapFree(GetProcessHeap(), 0, info);
314 if (GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_ALL_GATEWAYS,
315 NULL, NULL, &out) == ERROR_BUFFER_OVERFLOW)
317 adapters = HeapAlloc(GetProcessHeap(), 0, out);
321 if (GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_ALL_GATEWAYS,
322 NULL, adapters, &out) == ERROR_SUCCESS)
324 IP_ADAPTER_ADDRESSES *p;
326 for (p = adapters; p; p = p->Next)
328 IP_ADAPTER_UNICAST_ADDRESS *addr;
329 WCHAR physaddr_buf[3 * MAX_ADAPTER_ADDRESS_LENGTH];
330 IP_ADAPTER_GATEWAY_ADDRESS_LH *gateway;
333 ipconfig_message_printfW(STRING_ADAPTER_FRIENDLY, iftype_to_string(p->IfType), p->FriendlyName);
334 ipconfig_printfW(newlineW);
335 print_field(STRING_CONN_DNS_SUFFIX, p->DnsSuffix);
336 print_field(STRING_DESCRIPTION, p->Description);
337 print_field(STRING_PHYS_ADDR, physaddr_to_string(physaddr_buf, p->PhysicalAddress, p->PhysicalAddressLength));
338 print_field(STRING_DHCP_ENABLED, boolean_to_string(p->Flags & IP_ADAPTER_DHCP_ENABLED));
340 /* FIXME: Output autoconfiguration status. */
342 for (addr = p->FirstUnicastAddress; addr; addr = addr->Next)
344 if (socket_address_to_string(addr_buf, sizeof(addr_buf)/sizeof(WCHAR), &addr->Address))
345 print_field(STRING_IP_ADDRESS, addr_buf);
346 /* FIXME: Output corresponding subnet mask. */
349 if (p->FirstGatewayAddress)
351 if (socket_address_to_string(addr_buf, sizeof(addr_buf)/sizeof(WCHAR), &p->FirstGatewayAddress->Address))
352 print_field(STRING_DEFAULT_GATEWAY, addr_buf);
354 for (gateway = p->FirstGatewayAddress->Next; gateway; gateway = gateway->Next)
356 if (socket_address_to_string(addr_buf, sizeof(addr_buf)/sizeof(WCHAR), &gateway->Address))
357 print_value(addr_buf);
361 print_field(STRING_DEFAULT_GATEWAY, emptyW);
363 ipconfig_printfW(newlineW);
367 HeapFree(GetProcessHeap(), 0, adapters);
371 int wmain(int argc, WCHAR *argv[])
373 static const WCHAR slashHelp[] = {'/','?',0};
374 static const WCHAR slashAll[] = {'/','a','l','l',0};
378 if (WSAStartup(MAKEWORD(2, 2), &data))
383 if (!strcmpW(slashHelp, argv[1]))
385 ipconfig_message(STRING_USAGE);
389 else if (!strcmpiW(slashAll, argv[1]))
393 ipconfig_message(STRING_INVALID_CMDLINE);
394 ipconfig_message(STRING_USAGE);
399 print_full_information();
403 ipconfig_message(STRING_INVALID_CMDLINE);
404 ipconfig_message(STRING_USAGE);
410 print_basic_information();