ipconfig.c 12 KB
Newer Older
1 2 3
/*
 * IP configuration utility
 *
4
 * Copyright 2008 Andrew Riedi
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * Copyright 2010 Andrew Nguyen
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

22 23
#define NONAMELESSUNION

24
#include <stdio.h>
25
#include <winsock2.h>
26
#include <windows.h>
27
#include <iphlpapi.h>
28

29 30
#include "ipconfig.h"

31
static int ipconfig_vprintfW(const WCHAR *msg, va_list va_args)
32 33
{
    int wlen;
34
    DWORD count;
35 36
    WCHAR msg_buffer[8192];

37
    wlen = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, msg, 0, 0, msg_buffer,
38
                          ARRAY_SIZE(msg_buffer), &va_args);
39

40
    if (!WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), msg_buffer, wlen, &count, NULL))
41 42 43 44
    {
        DWORD len;
        char *msgA;

45
        /* On Windows WriteConsoleW() fails if the output is redirected. So fall
46
         * back to WriteFile() with OEM code page.
47
         */
48
        len = WideCharToMultiByte(GetOEMCP(), 0, msg_buffer, wlen,
49 50 51 52 53
            NULL, 0, NULL, NULL);
        msgA = HeapAlloc(GetProcessHeap(), 0, len);
        if (!msgA)
            return 0;

54
        WideCharToMultiByte(GetOEMCP(), 0, msg_buffer, wlen, msgA, len, NULL, NULL);
55 56 57 58 59 60 61
        WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msgA, len, &count, FALSE);
        HeapFree(GetProcessHeap(), 0, msgA);
    }

    return count;
}

62
static int WINAPIV ipconfig_printfW(const WCHAR *msg, ...)
63
{
64
    va_list va_args;
65 66
    int len;

67
    va_start(va_args, msg);
68
    len = ipconfig_vprintfW(msg, va_args);
69
    va_end(va_args);
70 71 72 73

    return len;
}

74
static int WINAPIV ipconfig_message_printfW(int msg, ...)
75
{
76
    va_list va_args;
77 78 79
    WCHAR msg_buffer[8192];
    int len;

80
    LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer, ARRAY_SIZE(msg_buffer));
81

82
    va_start(va_args, msg);
83
    len = ipconfig_vprintfW(msg_buffer, va_args);
84
    va_end(va_args);
85 86 87 88

    return len;
}

89 90 91 92
static int ipconfig_message(int msg)
{
    WCHAR msg_buffer[8192];

93
    LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer, ARRAY_SIZE(msg_buffer));
94

95
    return ipconfig_printfW(L"%1", msg_buffer);
96 97
}

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
static const WCHAR *iftype_to_string(DWORD type)
{
    static WCHAR msg_buffer[50];

    int msg;

    switch (type)
    {
    case IF_TYPE_ETHERNET_CSMACD:
    /* The loopback adapter appears as an Ethernet device. */
    case IF_TYPE_SOFTWARE_LOOPBACK:
        msg = STRING_ETHERNET;
        break;
    default:
        msg = STRING_UNKNOWN;
    }

115
    LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer, ARRAY_SIZE(msg_buffer));
116 117 118 119 120 121

    return msg_buffer;
}

static void print_field(int msg, const WCHAR *value)
{
122
    WCHAR field[] = L". . . . . . . . . . . . . . . . . ";
123
    WCHAR name_buffer[ARRAY_SIZE(field)];
124

125
    LoadStringW(GetModuleHandleW(NULL), msg, name_buffer, ARRAY_SIZE(name_buffer));
126
    memcpy(field, name_buffer, sizeof(WCHAR) * min(lstrlenW(name_buffer), ARRAY_SIZE(field) - 1));
127

128
    ipconfig_printfW(L"    %1: %2\n", field, value);
129 130
}

131 132
static void print_value(const WCHAR *value)
{
133
    ipconfig_printfW(L"                                        %1\n", value);
134 135
}

136 137 138 139 140 141 142
static BOOL socket_address_to_string(WCHAR *buf, DWORD len, SOCKET_ADDRESS *addr)
{
    return WSAAddressToStringW(addr->lpSockaddr,
                               addr->iSockaddrLength, NULL,
                               buf, &len) == 0;
}

143 144 145 146 147
static void print_basic_information(void)
{
    IP_ADAPTER_ADDRESSES *adapters;
    ULONG out = 0;

148 149
    if (GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_ALL_GATEWAYS,
                             NULL, NULL, &out) == ERROR_BUFFER_OVERFLOW)
150 151 152 153 154
    {
        adapters = HeapAlloc(GetProcessHeap(), 0, out);
        if (!adapters)
            exit(1);

155 156
        if (GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_ALL_GATEWAYS,
                                 NULL, adapters, &out) == ERROR_SUCCESS)
157 158 159 160 161 162
        {
            IP_ADAPTER_ADDRESSES *p;

            for (p = adapters; p; p = p->Next)
            {
                IP_ADAPTER_UNICAST_ADDRESS *addr;
163
                IP_ADAPTER_GATEWAY_ADDRESS_LH *gateway;
164
                WCHAR addr_buf[54];
165 166

                ipconfig_message_printfW(STRING_ADAPTER_FRIENDLY, iftype_to_string(p->IfType), p->FriendlyName);
167
                ipconfig_printfW(L"\n");
168 169 170 171
                print_field(STRING_CONN_DNS_SUFFIX, p->DnsSuffix);

                for (addr = p->FirstUnicastAddress; addr; addr = addr->Next)
                {
172
                    if (addr->Address.lpSockaddr->sa_family == AF_INET &&
173
                        socket_address_to_string(addr_buf, ARRAY_SIZE(addr_buf), &addr->Address))
174
                        print_field(STRING_IP_ADDRESS, addr_buf);
175
                    else if (addr->Address.lpSockaddr->sa_family == AF_INET6 &&
176
                             socket_address_to_string(addr_buf, ARRAY_SIZE(addr_buf), &addr->Address))
177
                        print_field(STRING_IP6_ADDRESS, addr_buf);
178 179 180
                    /* FIXME: Output corresponding subnet mask. */
                }

181 182
                if (p->FirstGatewayAddress)
                {
183
                    if (socket_address_to_string(addr_buf, ARRAY_SIZE(addr_buf), &p->FirstGatewayAddress->Address))
184 185 186 187
                        print_field(STRING_DEFAULT_GATEWAY, addr_buf);

                    for (gateway = p->FirstGatewayAddress->Next; gateway; gateway = gateway->Next)
                    {
188
                        if (socket_address_to_string(addr_buf, ARRAY_SIZE(addr_buf), &gateway->Address))
189 190 191 192
                            print_value(addr_buf);
                    }
                }
                else
193
                    print_field(STRING_DEFAULT_GATEWAY, L"");
194

195
                ipconfig_printfW(L"\n");
196 197 198 199 200 201 202
            }
        }

        HeapFree(GetProcessHeap(), 0, adapters);
    }
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
static const WCHAR *nodetype_to_string(DWORD type)
{
    static WCHAR msg_buffer[50];

    int msg;

    switch (type)
    {
    case BROADCAST_NODETYPE:
        msg = STRING_BROADCAST;
        break;
    case PEER_TO_PEER_NODETYPE:
        msg = STRING_PEER_TO_PEER;
        break;
    case MIXED_NODETYPE:
        msg = STRING_MIXED;
        break;
    case HYBRID_NODETYPE:
        msg = STRING_HYBRID;
        break;
    default:
        msg = STRING_UNKNOWN;
    }

227
    LoadStringW(GetModuleHandleW(NULL), msg, msg_buffer, ARRAY_SIZE(msg_buffer));
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

    return msg_buffer;
}

static WCHAR *physaddr_to_string(WCHAR *buf, BYTE *addr, DWORD len)
{
    if (!len)
        *buf = '\0';
    else
    {
        WCHAR *p = buf;
        DWORD i;

        for (i = 0; i < len - 1; i++)
        {
243
            swprintf(p, 4, L"%02X-", addr[i]);
244 245
            p += 3;
        }
246
        swprintf(p, 3, L"%02X", addr[i]);
247 248 249 250 251
    }

    return buf;
}

252 253 254 255 256
static const WCHAR *boolean_to_string(int value)
{
    static WCHAR msg_buffer[15];

    LoadStringW(GetModuleHandleW(NULL), value ? STRING_YES : STRING_NO,
257
        msg_buffer, ARRAY_SIZE(msg_buffer));
258 259 260 261

    return msg_buffer;
}

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
static void print_full_information(void)
{
    FIXED_INFO *info;
    IP_ADAPTER_ADDRESSES *adapters;
    ULONG out = 0;

    if (GetNetworkParams(NULL, &out) == ERROR_BUFFER_OVERFLOW)
    {
        info = HeapAlloc(GetProcessHeap(), 0, out);
        if (!info)
            exit(1);

        if (GetNetworkParams(info, &out) == ERROR_SUCCESS)
        {
            WCHAR hostnameW[MAX_HOSTNAME_LEN + 4];
277
            WCHAR dnssuffixW[MAX_DOMAIN_NAME_LEN + 4];
278

279
            MultiByteToWideChar(CP_ACP, 0, info->HostName, -1, hostnameW, ARRAY_SIZE(hostnameW));
280 281
            print_field(STRING_HOSTNAME, hostnameW);

282 283
            MultiByteToWideChar(CP_ACP, 0, info->DomainName, -1, dnssuffixW, ARRAY_SIZE(dnssuffixW));
            print_field(STRING_PRIMARY_DNS_SUFFIX, dnssuffixW);
284 285

            print_field(STRING_NODE_TYPE, nodetype_to_string(info->NodeType));
286
            print_field(STRING_IP_ROUTING, boolean_to_string(info->EnableRouting));
287 288 289

            /* FIXME: Output WINS proxy status and DNS suffix search list. */

290
            ipconfig_printfW(L"\n");
291 292 293 294 295
        }

        HeapFree(GetProcessHeap(), 0, info);
    }

296 297
    if (GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_ALL_GATEWAYS,
                             NULL, NULL, &out) == ERROR_BUFFER_OVERFLOW)
298 299 300 301 302
    {
        adapters = HeapAlloc(GetProcessHeap(), 0, out);
        if (!adapters)
            exit(1);

303 304
        if (GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_ALL_GATEWAYS,
                                 NULL, adapters, &out) == ERROR_SUCCESS)
305 306 307 308 309 310 311
        {
            IP_ADAPTER_ADDRESSES *p;

            for (p = adapters; p; p = p->Next)
            {
                IP_ADAPTER_UNICAST_ADDRESS *addr;
                WCHAR physaddr_buf[3 * MAX_ADAPTER_ADDRESS_LENGTH];
312
                IP_ADAPTER_GATEWAY_ADDRESS_LH *gateway;
313
                WCHAR addr_buf[54];
314 315

                ipconfig_message_printfW(STRING_ADAPTER_FRIENDLY, iftype_to_string(p->IfType), p->FriendlyName);
316
                ipconfig_printfW(L"\n");
317 318 319
                print_field(STRING_CONN_DNS_SUFFIX, p->DnsSuffix);
                print_field(STRING_DESCRIPTION, p->Description);
                print_field(STRING_PHYS_ADDR, physaddr_to_string(physaddr_buf, p->PhysicalAddress, p->PhysicalAddressLength));
320
                print_field(STRING_DHCP_ENABLED, boolean_to_string(p->u1.Flags & IP_ADAPTER_DHCP_ENABLED));
321 322 323 324 325

                /* FIXME: Output autoconfiguration status. */

                for (addr = p->FirstUnicastAddress; addr; addr = addr->Next)
                {
326
                    if (addr->Address.lpSockaddr->sa_family == AF_INET &&
327
                        socket_address_to_string(addr_buf, ARRAY_SIZE(addr_buf), &addr->Address))
328
                        print_field(STRING_IP_ADDRESS, addr_buf);
329
                    else if (addr->Address.lpSockaddr->sa_family == AF_INET6 &&
330
                             socket_address_to_string(addr_buf, ARRAY_SIZE(addr_buf), &addr->Address))
331
                        print_field(STRING_IP6_ADDRESS, addr_buf);
332 333 334
                    /* FIXME: Output corresponding subnet mask. */
                }

335 336
                if (p->FirstGatewayAddress)
                {
337
                    if (socket_address_to_string(addr_buf, ARRAY_SIZE(addr_buf), &p->FirstGatewayAddress->Address))
338 339 340 341
                        print_field(STRING_DEFAULT_GATEWAY, addr_buf);

                    for (gateway = p->FirstGatewayAddress->Next; gateway; gateway = gateway->Next)
                    {
342
                        if (socket_address_to_string(addr_buf, ARRAY_SIZE(addr_buf), &gateway->Address))
343 344 345 346
                            print_value(addr_buf);
                    }
                }
                else
347
                    print_field(STRING_DEFAULT_GATEWAY, L"");
348

349
                ipconfig_printfW(L"\n");
350 351 352 353 354 355 356
            }
        }

        HeapFree(GetProcessHeap(), 0, adapters);
    }
}

357
int __cdecl wmain(int argc, WCHAR *argv[])
358
{
359 360 361 362 363
    WSADATA data;

    if (WSAStartup(MAKEWORD(2, 2), &data))
        return 1;

364 365
    if (argc > 1)
    {
366
        if (!lstrcmpW(L"/?", argv[1]))
367 368
        {
            ipconfig_message(STRING_USAGE);
369
            WSACleanup();
370 371
            return 1;
        }
372
        else if (!wcsicmp(L"/all", argv[1]))
373 374 375 376 377
        {
            if (argv[2])
            {
                ipconfig_message(STRING_INVALID_CMDLINE);
                ipconfig_message(STRING_USAGE);
378
                WSACleanup();
379 380 381
                return 1;
            }

382
            print_full_information();
383 384 385 386 387
        }
        else
        {
            ipconfig_message(STRING_INVALID_CMDLINE);
            ipconfig_message(STRING_USAGE);
388
            WSACleanup();
389 390 391
            return 1;
        }
    }
392 393
    else
        print_basic_information();
394

395
    WSACleanup();
396 397
    return 0;
}