< IPv6 IPConfig Program Example | IP Helper Functions Main | GetIpAddrTable & AddIPAddress Examples >


 

 

IP Helper Functions 13 Part 7

 

 

What do we have in this chapter 13 part 7?

  1. DHCP Renew and Release Program Example

  2. Retrieving the IPv4 routing table with GetIpForwardTable() Example

 

DHCP Renew and Release Program Example

 

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

 

IP Helper Functions: DHCP Renew and Release 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.

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

 

/*

Abstract:

 

    This module illustrates how to programmatically release and renew IP addresses

    obtained through DHCP.  This program also demonstrates how to retrieve existing

    network adapter configuration information.

 

    IP address assignment from DHCP can be updated using the IpRenewAddress() and

    IpReleaseAddress() IP Helper APIs.  These APIs require you to understand adapter

    index numbers.  In Windows, every network adapter has a unique index ID.  Adapter

    index IDs can be retrieved using the GetAdaptersInfo() IP Helper API.  This program

    features a list option that displays current network adapter configuration

    information showing all adapters index numbers associated with their corresponding

    network adaptors.

 

    IPrenewrelease [ -l ] [ -r<index id> ] [ -n<index id> ]

 

        -l List adapters with corresponding index ID information

        -r Release IP address for adapter index ID

        -n Renew IP address for adapter index ID

 

Original author: Jim Ohlund

Revision History:

*/

 

#include <windows.h>

// Link to Iphlpapi.lib

#include <iphlpapi.h>

#include <stdio.h>

 

void Usage(progname)

{

    printf("Usage: %s [ -l ] [ -r<index id> ] [ -n<index id>]\n\n", progname);

            printf("\t -l List adapters with corresponding index ID information\n"

            "\t -r Release IP address for adapter index ID\n"

            "\t -n Renew IP address for adapter index ID\n");

}

 

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

{

    DWORD InterfaceInfoSize = 0;

    PIP_INTERFACE_INFO pInterfaceInfo;

    DWORD Err;

    INT i;

    PIP_ADAPTER_INFO pAdapterInfo, pAdapt;

    DWORD AdapterInfoSize = 0;

    DWORD Index = -1;

    BOOL OptList = FALSE;

    BOOL OptRelease = FALSE;

    BOOL OptRenew = FALSE;

 

    if (argc < 2)

    {

        Usage(argv[0]);

        return;

    }

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

    {

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

        {

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

            {

                case 'l':

                    OptList = TRUE;

                    break;

                case 'r':

                    OptRelease = TRUE;

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

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

                    break;

                case 'n':

                    OptRenew = TRUE;

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

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

                    break;

                default:

                    Usage(argv[0]);

                    return;

                }

        }

        else

        {

            Usage(argv[0]);

            return;

        }

    }

 

    // Check options

    if ((OptRelease && Index == -1) || (OptRenew && Index == -1))

    {

        Usage(argv[0]);

        return;

    }

 

    if (OptList)

    {

        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() for PIP_ADAPTER_INFO struct 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() should be fine!\n");

 

        pAdapt = pAdapterInfo;

 

        printf("\nIndex     Adapter\n");

        while (pAdapt)

        {

            printf("%-10.i%s\n", pAdapt->Index, pAdapt->Description);

            pAdapt = pAdapt->Next;

        }

        printf("\n");

    }

 

    if ((Err = GetInterfaceInfo(NULL, &InterfaceInfoSize)) != 0)

    {

        if (Err != ERROR_INSUFFICIENT_BUFFER)

        {

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

            return;

        }

    }

    else

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

 

    // Allocate memory from sizing information

    if ((pInterfaceInfo = (PIP_INTERFACE_INFO) GlobalAlloc(GPTR, InterfaceInfoSize)) == NULL)

    {

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

        return;

    }

    else

        printf("GlobalAlloc() for PIP_INTERFACE_INFO struct is OK!\n");

 

    // Get actual adapter information

    if ((Err = GetInterfaceInfo(pInterfaceInfo, &InterfaceInfoSize)) != 0)

    {

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

        return;

    }

    else

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

 

    if (OptRelease)

    {

        for (i = 0; i < pInterfaceInfo->NumAdapters; i++)

            if (Index == pInterfaceInfo->Adapter[i].Index)

            {

                if ((Err = IpReleaseAddress(&pInterfaceInfo->Adapter[i])) != 0)

                {

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

                    return;

                }

                else

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

                break;

            }

    }

 

    if (OptRenew)

    {

        for (i = 0; i < pInterfaceInfo->NumAdapters; i++)

            if (Index == pInterfaceInfo->Adapter[i].Index)

            {

                if ((Err = IpRenewAddress(&pInterfaceInfo->Adapter[i])) != 0)

                {

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

                    return;

                }

                else

                    printf("IpRenewAddress() should be OK!\n");

                break;

            }

    }

}

 

Build and run the project. The DHCP server (and client) must be available and working.

 

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

IP Helper Functions: DHCP Renew and Release Program Example sample output without any argument

 

IP Helper Functions: DHCP Renew and Release Program Example sample output with -l option

 

Retrieving the IPv4 routing table with GetIpForwardTable() Example

 

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

 

IP Helper Functions: Retrieving the IPv4 routing table with GetIpForwardTable() Example

 

Add the following source code.

 

// Description:

//          The following example retrieves the IP routing table

//          then prints some fields for each route in the table

 

// Link with Ws2_32.lib

#include <winsock2.h>

#include <ws2tcpip.h>

// Link to Iphlpapi.lib

#include <iphlpapi.h>

#include <stdio.h>

 

/* Note: could also use malloc() and free() */

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))

#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))

 

int main(int argc, char **argv)

{

            // Declare and initialize variables

    // variables used for GetIfForwardTable

    PMIB_IPFORWARDTABLE pIpForwardTable;

    DWORD dwSize = 0;

    DWORD dwRetVal = 0;

    struct in_addr IpAddr;

    int i;

 

    char szDestIp[128];

    char szMaskIp[128];

    char szGatewayIp[128];

 

    pIpForwardTable = (MIB_IPFORWARDTABLE *) MALLOC(sizeof (MIB_IPFORWARDTABLE));

    if (pIpForwardTable == NULL)

    {

        printf("Error allocating memory!\n");

        return 1;

    }

    else

        printf("Allocating memory to pIpForwardTable is OK!\n");

 

    if (GetIpForwardTable(pIpForwardTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER)

    {

        printf("Not enough memory, re-allocate...\n");

        FREE(pIpForwardTable);

        pIpForwardTable = (MIB_IPFORWARDTABLE *) MALLOC(dwSize);

        if (pIpForwardTable == NULL)

        {

            printf("Error allocating memory!\n");

            return 1;

        }

        else

            printf("Re-allocating memory to pIpForwardTable is OK!\n");

    }

 

            // Note that the IPv4 addresses returned in

            // GetIpForwardTable entries are in network byte order

            if ((dwRetVal = GetIpForwardTable(pIpForwardTable, &dwSize, 0)) == NO_ERROR)

            {

            printf("\tNumber of entries: %d\n", (int) pIpForwardTable->dwNumEntries);

            for (i = 0; i < (int) pIpForwardTable->dwNumEntries; i++)

            {

            // Convert IPv4 addresses to strings

            IpAddr.S_un.S_addr = (u_long) pIpForwardTable->table[i].dwForwardDest;

            strcpy_s(szDestIp, sizeof (szDestIp), inet_ntoa(IpAddr));

            IpAddr.S_un.S_addr = (u_long) pIpForwardTable->table[i].dwForwardMask;

            strcpy_s(szMaskIp, sizeof (szMaskIp), inet_ntoa(IpAddr));

            IpAddr.S_un.S_addr = (u_long) pIpForwardTable->table[i].dwForwardNextHop;

            strcpy_s(szGatewayIp, sizeof (szGatewayIp), inet_ntoa(IpAddr));

 

            printf("\n\tRoute[%d] Dest IP: %s\n", i, szDestIp);

            printf("\tRoute[%d] Subnet Mask: %s\n", i, szMaskIp);

            printf("\tRoute[%d] Next Hop: %s\n", i, szGatewayIp);

            printf("\tRoute[%d] If Index: %ld\n", i, pIpForwardTable->table[i].dwForwardIfIndex);

            printf("\tRoute[%d] Type: %ld - ", i, pIpForwardTable->table[i].dwForwardType);

 

 

 

 

            switch (pIpForwardTable->table[i].dwForwardType)

            {

            case MIB_IPROUTE_TYPE_OTHER:

                printf("other\n");

                break;

            case MIB_IPROUTE_TYPE_INVALID:

                printf("invalid route\n");

                break;

            case MIB_IPROUTE_TYPE_DIRECT:

                printf("local route where next hop is final destination\n");

                break;

            case MIB_IPROUTE_TYPE_INDIRECT:

                printf

                    ("remote route where next hop is not final destination\n");

                break;

            default:

                printf("UNKNOWN Type value\n");

                break;

            }

 

            printf("\tRoute[%d] Proto: %ld - ", i, pIpForwardTable->table[i].dwForwardProto);

 

            switch (pIpForwardTable->table[i].dwForwardProto)

            {

            case MIB_IPPROTO_OTHER:

                printf("other\n");

                break;

            case MIB_IPPROTO_LOCAL:

                printf("local interface\n");

                break;

            case MIB_IPPROTO_NETMGMT:

                printf("static route set through network management \n");

                break;

            case MIB_IPPROTO_ICMP:

                printf("result of ICMP redirect\n");

                break;

            case MIB_IPPROTO_EGP:

                printf("Exterior Gateway Protocol (EGP)\n");

                break;

            case MIB_IPPROTO_GGP:

                printf("Gateway-to-Gateway Protocol (GGP)\n");

                break;

            case MIB_IPPROTO_HELLO:

                printf("Hello protocol\n");

                break;

            case MIB_IPPROTO_RIP:

                printf("Routing Information Protocol (RIP)\n");

                break;

            case MIB_IPPROTO_IS_IS:

                printf

                    ("Intermediate System-to-Intermediate System (IS-IS) protocol\n");

                break;

            case MIB_IPPROTO_ES_IS:

                printf("End System-to-Intermediate System (ES-IS) protocol\n");

                break;

            case MIB_IPPROTO_CISCO:

                printf("Cisco Interior Gateway Routing Protocol (IGRP)\n");

                break;

            case MIB_IPPROTO_BBN:

                printf("BBN Internet Gateway Protocol (IGP) using SPF\n");

                break;

            case MIB_IPPROTO_OSPF:

                printf("Open Shortest Path First (OSPF) protocol\n");

                break;

            case MIB_IPPROTO_BGP:

                printf("Border Gateway Protocol (BGP)\n");

                break;

            case MIB_IPPROTO_NT_AUTOSTATIC:

                printf("special Windows auto static route\n");

                break;

            case MIB_IPPROTO_NT_STATIC:

                printf("special Windows static route\n");

                break;

            case MIB_IPPROTO_NT_STATIC_NON_DOD:

                printf

                    ("special Windows static route not based on Internet standards\n");

                break;

            default:

                printf("UNKNOWN Proto value\n");

                break;

            }

 

            printf("\tRoute[%d] Age: %ld\n", i, pIpForwardTable->table[i].dwForwardAge);

            printf("\tRoute[%d] Metric1: %ld\n", i, pIpForwardTable->table[i].dwForwardMetric1);

        }

        FREE(pIpForwardTable);

        return 0;

}

else

{

            printf("\tGetIpForwardTable failed.\n");

            FREE(pIpForwardTable);

            return 1;

}

}

 

Build and run the project.

 

IP Helper Functions: Retrieving the IPv4 routing table with GetIpForwardTable() program example in action

 

 

 


< IPv6 IPConfig Program Example | IP Helper Functions Main | GetIpAddrTable & AddIPAddress Examples >