< ARP: Add, Delete & Send | IP Helper Functions Main | IPv6 IPConfig Program Example >


 

 

IP Helper Functions 13 Part 5

 

 

What do we have in this chapter 13 part 5?

  1. Changing IP Program Example

  2. IPConfig Program Example

 

Changing IP Program Example

 

Create a new empty Win32 console mode application and add the project/solution name.

 

IP Helper Functions: Changing the IP program example

 

Add the following source code.

 

/******************************************************************************\

*       This is a part of the Microsoft Source Code Samples.

*       Copyright 1996 - 1998 Microsoft Corporation.

*       All rights reserved.

*       This source code is only intended as a supplement to

*       Microsoft Development Tools and/or WinHelp documentation.

*       See these sources for detailed information regarding the

*       Microsoft samples programs.

\******************************************************************************/

 

/*

Module Name: IPChangeExamplesrc.cpp

 

Abstract:

 

    This module illustrates how to programmatically change an IP address for

    a specific network adapter on your machine.  This program also demonstrates how

    to retrieve existing network adapter IP configuration information.

 

    IP configurations can be changed for a specific network adapter by using the

    AddIpAddress() and DeleteIpAddress() Ip Helper APIs.  These APIs require you to

    understand adapter index numbers and IP context numbers.  In Windows, every network

    adapter has a unique index ID and every IP address has a unique context ID.

    Adapter index IDs and IP context numbers can be retrieved using the GetAdaptersInfo()

    IP Helper API.  This program features a list option that displays current network

    adapter configuration information by showing all adapters index numbers and IP

    address context numbers associated with their corresponding network adaptors.

 

    IPChangeExample [ -l ] [ -a -n<index id> -i<ip address> -m<subnet mask> ]

                 [ -d -c<context id>]

 

    -l List adapter index IDs and IP Address context ID information

    -a Add IP Address option

    -d Delete IP Address option

    -i IP Address to specify with -a option

    -m Subnet Mask to specify with -a option

    -c IP context ID for an existing IP address

    -n Index ID of an existing network adapter

 

Original author: Jim Ohlund 21-Apr-98

Revision History:

*/

 

// Link to ws2_32.lib

#include <winsock2.h>

#include <windows.h>

#include <stdio.h>

// Link to Iphlpapi.lib

#include <iphlpapi.h>

 

void Usage(void)

{

    printf("Usage: IPChangeExample [ -l ] [ -a -n<index id> -i<ip address> -m<subnet mask> ] "

        "[ -d -c<context id>]\n\n"

        "\t -l List adapter index IDs and IP Address context ID information\n"

        "\t -a Add IP Address option\n"

        "\t -d Delete IP Address option\n"

        "\t -i IP Address to specify with -a option\n"

        "\t -m Subnet Mask to specify with -a option\n"

        "\t -c IP context ID for an existing IP address\n"

        "\t -n Index ID of an existing network adapter\n");

}

 

void main(int argc, char *argv[])

{

    WSADATA wsaData;

    int i;

    UINT j;

    ULONG NTEContext = 0;

    ULONG NTEInstance;

    IPAddr NewIP;

    IPAddr NewMask;

    DWORD Index;

    DWORD Context;

    CHAR NewIPStr[64];

    CHAR NewMaskStr[64];

 

    PIP_ADAPTER_INFO pAdapterInfo, pAdapt;

    PIP_ADDR_STRING pAddrStr;

    DWORD AdapterInfoSize;

    DWORD Err;

    BOOL OptList = FALSE;

    BOOL OptAdd = FALSE;

    BOOL OptDel = FALSE;

 

    NewIPStr[0] = '\0';

    NewMaskStr[0] = '\0';

    Context = Index = -1;

 

    if(argc < 2)

    {

        Usage();

        exit(1);

    }

 

    for (i = 1; i < argc; i++)

    {

        if ((argv[i][0] == '-') || (argv[i][0] == '/'))

        {

            switch(tolower(argv[i][1]))

            {

                case 'l':

                    OptList = TRUE;

                    break;

                case 'a':

                    OptAdd = TRUE;

                    break;

                case 'c':

                    if (strlen(argv[i]) > 2)

                        Context = atoi(&argv[i][2]);

                    break;

                case 'd':

                    OptDel = TRUE;

                    break;

                case 'i':

                    if (strlen(argv[i]) > 2)

                        strcpy_s(NewIPStr, sizeof(NewIPStr), &argv[i][2]);

                    break;

                case 'm':

                    if (strlen(argv[i]) > 2)

                        strcpy_s(NewMaskStr, sizeof(NewMaskStr), &argv[i][2]);

                    break;

                case 'n':

                    if (strlen(argv[i]) > 2)

                        Index = atoi(&argv[i][2]);

                    break;

                default:

                    printf("default\n");

                    Usage();

                    return;

                }

        }

        else

        {

            printf("else\n");

            Usage();

            return;

        }

    }

 

    // Check options

    if ((OptAdd && (Index == -1 || NewIPStr[0] == '\0' || NewMaskStr[0] == '\0'))   || (OptDel && Context == -1))

    {

        Usage();

        return;

    }

 

    if(WSAStartup(MAKEWORD(2,2), &wsaData) != 0)

    {

        printf("WSAStartup() is OK!\n");

        return;

    }

    else

        printf("WSAStartup() is OK!\n");

 

    // Get sizing information about all adapters

    AdapterInfoSize = 0;

    if ((Err = GetAdaptersInfo(NULL, &AdapterInfoSize)) != 0)

    {

        if (Err != ERROR_BUFFER_OVERFLOW)

        {

            printf("GetAdaptersInfo sizing failed with error %d\n", Err);

            return;

        }

    }

 

    // Allocate memory from sizing information

    if ((pAdapterInfo = (PIP_ADAPTER_INFO) GlobalAlloc(GPTR, AdapterInfoSize)) == NULL)

    {

        printf("Memory allocation error\n");

        return;

    }

 

    // Get actual adapter information

    if ((Err = GetAdaptersInfo(pAdapterInfo, &AdapterInfoSize)) != 0)

    {

        printf("GetAdaptersInfo failed with error %d\n", Err);

        return;

    }

 

    if (OptList)

    {

        printf("MAC Address - Adapter\n"

            "Index     Context   Ip Address          Subnet Mask\n"

            "--------------------------------------------------------------\n");

 

        pAdapt = pAdapterInfo;

 

        while (pAdapt)

        {

            for (j=0; j<pAdapt->AddressLength; j++)

            {

                if (j == (pAdapt->AddressLength - 1))

                    printf("%.2X - ",(int)pAdapt->Address[j]);

                else

                    printf("%.2X-",(int)pAdapt->Address[j]);

            }

            printf("%s\n", pAdapt->Description);

 

            pAddrStr = &(pAdapt->IpAddressList);

            while(pAddrStr)

            {

                printf("%-10.d%-10.d%-20.20s%s\n", pAdapt->Index, pAddrStr->Context, pAddrStr->IpAddress.String, pAddrStr->IpMask.String);

                pAddrStr = pAddrStr->Next;

            }

            pAdapt = pAdapt->Next;

        }

    }

 

    if (OptAdd)

    {

        NewIP = inet_addr(NewIPStr);

        NewMask = inet_addr(NewMaskStr);

        if ((Err = AddIPAddress(NewIP, NewMask, Index, &NTEContext, &NTEInstance)) != 0)

        {

            printf("AddIPAddress failed with error %d, %d\n", NTEContext, Err);

            return;

        }

    }

 

    if (OptDel)

    {

        if ((Err = DeleteIPAddress(Context)) != 0)

            printf("DeleteIPAddress failed %d\n", Err);

    }

 

    if(WSACleanup() == 0)

         printf("WSACleanup() is pretty fine!\n");

    else

         printf("WSACleanup() failed with error code %d\n", WSAGetLastError());

}

 

Build and run the project.

 

-------------------------------------------------------

IP Helper Functions: Running the IP manipulation program example without any argument

 

IP Helper Functions: Running the IP manipulation program example with -l argument

 

IPConfig Program Example

 

Create a new empty Win32 console mode application and add the project/solution name.

 

IP Helper Functions: IPconfig program example

 

Add the following source code.

 

/******************************************************************************\

*       This is a part of the Microsoft Source Code Samples.

*       Copyright 1996 - 1998 Microsoft Corporation.

*       All rights reserved.

*       This source code is only intended as a supplement to

*       Microsoft Development Tools and/or WinHelp documentation.

*       See these sources for detailed information regarding the

*       Microsoft samples programs.

\******************************************************************************/

/*

Module Name: IpconfigExamplesrc.cpp

 

Abstract:

 

    This module illustrates how to programmatically retrieve IP configuration

    information similar to the IPCONFIG.EXE utility.  It demonstrates how to use

    the IP Helper APIs GetNetworkParams() and GetAdaptersInfo().

 

Original author: Jim Ohlund 21-Apr-98

Revision History:

*/

 

#include <windows.h>

// Link to Iphlpapi.lib

#include <iphlpapi.h>

#include <stdio.h>

#include <time.h>

 

void main(void)

{

    DWORD Err;

    PFIXED_INFO pFixedInfo;

    DWORD FixedInfoSize = 0;

    struct tm newtime, newtime1;

    char buf[sizeof(newtime)];

    errno_t Err1;

 

    PIP_ADAPTER_INFO pAdapterInfo, pAdapt;

    DWORD AdapterInfoSize;

    PIP_ADDR_STRING pAddrStr;

 

    // Get the main IP configuration information for this machine using a FIXED_INFO structure

    if ((Err = GetNetworkParams(NULL, &FixedInfoSize)) != 0)

    {

        if (Err != ERROR_BUFFER_OVERFLOW)

        {

            printf("GetNetworkParams() sizing failed with error %d\n", Err);

            return;

        }

        else

           printf("GetNetworkParams() is OK!\n");

    }

 

    // Allocate memory from sizing information

    if ((pFixedInfo = (PFIXED_INFO) GlobalAlloc(GPTR, FixedInfoSize)) == NULL)

    {

        printf("Memory allocation error!\n");

        return;

    }

    else

        printf("GlobalAlloc() is OK!\n");

 

    if ((Err = GetNetworkParams(pFixedInfo, &FixedInfoSize)) == 0)

    {

        printf("GetNetworkParams() is OK!\n");

        printf("\tHost Name . . . . . . . . . : %s\n", pFixedInfo->HostName);

        printf("\tDNS Servers . . . . . . . . : %s\n", pFixedInfo->DnsServerList.IpAddress.String);

        pAddrStr = pFixedInfo->DnsServerList.Next;

        while(pAddrStr)

        {

            printf("%52s\n", pAddrStr->IpAddress.String);

            pAddrStr = pAddrStr->Next;

        }

 

        printf("\tNode Type . . . . . . . . . : ");

        switch (pFixedInfo->NodeType)

        {

            case 1:

                printf("%s\n", "Broadcast");

                break;

            case 2:

                printf("%s\n", "Peer-to-peer");

                break;

            case 4:

                printf("%s\n", "Mixed");

                break;

            case 8:

                printf("%s\n", "Hybrid");

                break;

            default:

                printf("None!\n");

        }

 

        printf("\tNetBIOS Scope ID. . . . . . : %s\n", pFixedInfo->ScopeId);

        printf("\tIP Routing Enabled. . . . . : %s\n", (pFixedInfo->EnableRouting ? "yes" : "no"));

        printf("\tWINS Proxy Enabled. . . . . : %s\n", (pFixedInfo->EnableProxy ? "yes" : "no"));

        printf("\tNetBIOS Resolution Uses DNS : %s\n", (pFixedInfo->EnableDns ? "yes" : "no"));

    }

    else

    {

        printf("GetNetworkParams() failed with error %d\n", Err);

        return;

    }

 

    // Enumerate all of the adapter specific information using the IP_ADAPTER_INFO structure.

    // Note:  IP_ADAPTER_INFO contains a linked list of adapter entries.

    AdapterInfoSize = 0;

    if ((Err = GetAdaptersInfo(NULL, &AdapterInfoSize)) != 0)

    {

        if (Err != ERROR_BUFFER_OVERFLOW)

        {

            printf("GetAdaptersInfo() sizing failed with error %d\n", Err);

            return;

        }

        else

            printf("GetAdaptersInfo() should be fine!\n");

    }

 

    // Allocate memory from sizing information

    if ((pAdapterInfo = (PIP_ADAPTER_INFO) GlobalAlloc(GPTR, AdapterInfoSize)) == NULL)

    {

        printf("Memory allocation error!\n");

        return;

    }

    else

        printf("GlobalAlloc() is OK!\n");

 

    // Get actual adapter information

    if ((Err = GetAdaptersInfo(pAdapterInfo, &AdapterInfoSize)) != 0)

    {

        printf("GetAdaptersInfo() failed with error %d\n", Err);

        return;

    }

    else

        printf("GetAdaptersInfo() is OK!\n");

 

    pAdapt = pAdapterInfo;

 

    while (pAdapt)

    {

        switch (pAdapt->Type)

        {

            case MIB_IF_TYPE_ETHERNET:

                printf("\nEthernet adapter ");

                break;

            case MIB_IF_TYPE_TOKENRING:

                printf("\nToken Ring adapter ");

                break;

            case MIB_IF_TYPE_FDDI:

                printf("\nFDDI adapter ");

                break;

            case MIB_IF_TYPE_PPP:

                printf("\nPPP adapter ");

                break;

            case MIB_IF_TYPE_LOOPBACK:

                printf("\nLoopback adapter ");

                break;

            case MIB_IF_TYPE_SLIP:

                printf("\nSlip adapter ");

                break;

            case MIB_IF_TYPE_OTHER:

            default:

                printf("\nOther adapter ");

        }

        printf("%s:\n\n", pAdapt->AdapterName);

 

        printf("\tDescription . . . . . . . . : %s\n", pAdapt->Description);

 

        printf("\tPhysical Address. . . . . . : ");

        for (UINT i=0; i<pAdapt->AddressLength; i++)

        {

            if (i == (pAdapt->AddressLength - 1))

                printf("%.2X\n",(int)pAdapt->Address[i]);

            else

                printf("%.2X-",(int)pAdapt->Address[i]);

        }

 

        printf("\tDHCP Enabled. . . . . . . . : %s\n", (pAdapt->DhcpEnabled ? "yes" : "no"));

 

        pAddrStr = &(pAdapt->IpAddressList);

        while(pAddrStr)

        {

            printf("\tIP Address. . . . . . . . . : %s\n", pAddrStr->IpAddress.String);

            printf("\tSubnet Mask . . . . . . . . : %s\n", pAddrStr->IpMask.String);

            pAddrStr = pAddrStr->Next;

        }

 

        printf("\tDefault Gateway . . . . . . : %s\n", pAdapt->GatewayList.IpAddress.String);

        pAddrStr = pAdapt->GatewayList.Next;

        while(pAddrStr)

        {

            printf("%52s\n", pAddrStr->IpAddress.String);

            pAddrStr = pAddrStr->Next;

        }

 

        printf("\tDHCP Server . . . . . . . . : %s\n", pAdapt->DhcpServer.IpAddress.String);

        printf("\tPrimary WINS Server . . . . : %s\n", pAdapt->PrimaryWinsServer.IpAddress.String);

        printf("\tSecondary WINS Server . . . : %s\n", pAdapt->SecondaryWinsServer.IpAddress.String);

 

                        // Display coordinated universal time - GMT

                        // LeaseObtained member is only valid when the DhcpEnabled member is non-zero

                        if(pAdapt->DhcpEnabled != 0)

                        {

                                    gmtime_s(&newtime, &pAdapt->LeaseObtained);

                                    Err1 = asctime_s(buf, sizeof(newtime), &newtime );

                                   

                                    if(Err1 == 0)

                                    {

                                                printf("err = %d\n", Err1);

                                                printf( "\tLease Obtained. . . . . . . : %s", buf );

                                    }

                                   

                                    // LeaseExpires member is only valid when the DhcpEnabled member is non-zero.

                                    gmtime_s(&newtime, &pAdapt->LeaseExpires);

                                    Err1 = asctime_s(buf, sizeof(newtime1), &newtime );

 

                                    if(Err1 == 0)

                                    {

                                                printf("err = %d\n", Err1);

                                                printf( "\tLease Expires . . . . . . . : %s", buf );

                                    }

                        }

                        else

                        {

                                    printf("\tDHCP not enabled! pAdapt->DhcpEnabled = %d\n", pAdapt->DhcpEnabled);

                                    printf("\t Then, no info for lease obtained & expires!\n");

                        }

 

        pAdapt = pAdapt->Next;

    }

}

 

Build and run the project.

 

IP Helper Functions: Running the IPconfig program example

 

 

 


< ARP: Add, Delete & Send | IP Helper Functions Main | IPv6 IPConfig Program Example >