⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iproute.cpp

📁 几个用c语言写的网络协议实践的程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*---------------------------------------------------------------------------
    Copyright (c) 1998 - 2000  Microsoft Corporation
    Module Name: IpRoute.exe   
    File       : IpRoute.cpp
    Description: This file demonstrates the use of IP Helper APIs to
                 manipulate routing table.
    Author:
    Frank Li            April 18, 1998
    
    Revision History:
    Who         When        What
    --------    --------    ----------------------------------
    Frank Li    04-18-98    created   
---------------------------------------------------------------------------*/

#include "IpRoute.h"

void Usage(char * pszProgramName)
{
    printf("Manipulates network routing tables.\n\n");

    printf("%s -p                                               ...Prints route table.\n", pszProgramName);
    printf("%s -a destination netmask gateway interface [metric]...Adds a route.\n", pszProgramName);
    printf("%s -d destination                                   ...Deletes routes to\n", pszProgramName);
    printf("                                                            destination.\n\n");
    
    printf("destination  Specifies the destination host.\n\n");

    printf("netmask      Specifies a subnet mask value to be associated\n");
    printf("             with this route entry.\n\n");
    
    printf("gateway      Specifies gateway.\n\n");

    printf("interface    Specifies the interface ip.\n\n");

    printf("metric       The cost for this destination\n\n");

    printf("Diagnostic Notes:\n\n");
    printf("Invalid MASK generates an error, that is when (DEST & MASK) != DEST.\n");
    printf("Example> IpRoute -a 157.0.0.0 155.0.0.0 157.55.80.1 157.55.80.9\n");
    printf("         IpRoute: Invalid Mask 155.0.0.0\n\n");

    printf("Examples:\n\n");

    printf("> IpRoute -p\n");
    printf("> IpRoute -a 157.0.0.0    255.0.0.0 157.55.80.1  157.55.80.9           1\n");
    printf("             ^destination ^mask     ^gateway     ^existing interface   ^metric\n");
    printf("> IpRoute -p\n");
    printf("> IpRoute -d 157.0.0.0\n");
    printf("> IpRoute -p\n");

    WSACleanup();
    exit(1);
}


void _cdecl main(int argc, char **argv)
{
    WORD wVersionRequested = MAKEWORD(1,1);
    WSADATA wsaData;
    int nRet;

    nRet = WSAStartup(wVersionRequested, &wsaData);
    if (wsaData.wVersion != wVersionRequested)
    {    
        fprintf(stderr,"\n Wrong version\n");
        return;
    }

    if ((argc < 2) || (argv[1][0] != '-'))        
        Usage("IpRoute");
    if (strlen(argv[1]) > 2)     
        Usage("IpRoute");  

    switch(argv[1][1]) 
    {    
    case 'p':
        // Print routing table
        DoGetIpForwardTable();
        break;
    case 'a':   
        //Adds an entry into routing table   
        if (argc == 6)
            DoSetIpForwardEntry(argv[2], argv[3], argv[4], argv[5]); // dest mask gateway if
        else if (argc == 7)
        {
            DWORD dwMetric;
            if (sscanf(argv[6], "%u", &dwMetric ) == 1)
                DoSetIpForwardEntry(argv[2], argv[3], argv[4], argv[5], dwMetric);
            else
                printf("IpRoute: Bad argument %s\n", argv[6]);
        }
        else
            Usage("IpRoute");
        break;    
         
    case 'd':       
        //Delete an entry from the routing table    
        if (argc == 3)
            DoDeleteIpForwardEntry(argv[2]);
        else
            Usage("IpRoute");                
        break;  
    default:    
        // help
        Usage("IpRoute");        
        break;    
    }
    WSACleanup();
}

void DoGetIpForwardTable()
{
    DWORD dwStatus;
    PMIB_IPFORWARDTABLE pIpRouteTab = NULL; // Ip routing table
    

    if ( (dwStatus = MyGetIpForwardTable(pIpRouteTab, TRUE)) == NO_ERROR)
    {
        PrintIpForwardTable(pIpRouteTab);
        free(pIpRouteTab);
        return;
    }
    else if ( dwStatus == ERROR_NO_DATA)
    {
        printf("No entries in route table.\n");
        if (pIpRouteTab)
            free (pIpRouteTab);
        return;
    }
    else
    {
        if (pIpRouteTab)
            free (pIpRouteTab);
        printf("IpRoute returned 0x%x\n", dwStatus);
        return;
    }
}

void DoSetIpForwardEntry(char* pszDest, char* pszNetMask, char* pszGateway, char* pszInterface, DWORD dwMetric)
{
    DWORD dwStatus;

    MIB_IPFORWARDROW routeEntry;            // Ip routing table row entry
    PMIB_IPADDRTABLE pIpAddrTable = NULL;   // Ip Addr Table
    DWORD dwIfIndex;                        // Interface index number  
    DWORD dwIfMask;                         // Interface Subnet Mask
    DWORD dwIfIpAddr;                       // Interface Ip Address
    
    memset(&routeEntry, 0, sizeof(MIB_IPFORWARDROW));
    

    // converting and checking input arguments...
    if (pszDest == NULL || pszNetMask == NULL || pszGateway == NULL)
    {
        printf("IpRoute: Bad Argument\n");
        return;
    }

    routeEntry.dwForwardDest = inet_addr(pszDest); // convert dotted ip addr. to ip addr.
    if (routeEntry.dwForwardDest == INADDR_NONE)
    {
        printf("IpRoute: Bad Destination %s\n", pszDest);
        return;
    }

    routeEntry.dwForwardMask = inet_addr(pszNetMask);
    if ( (routeEntry.dwForwardMask == INADDR_NONE) && 
         (strcmp("255.255.255.255", pszNetMask) != 0) )
    {
        printf("IpRoute: Bad Mask %s\n", pszNetMask);
        return;
    }

    routeEntry.dwForwardNextHop = inet_addr(pszGateway);
    if (routeEntry.dwForwardNextHop == INADDR_NONE)
    {
        printf("IpRoute: Bad Gateway %s\n", pszGateway);
        return;
    }

    if ( (routeEntry.dwForwardDest & routeEntry.dwForwardMask) != routeEntry.dwForwardDest)
    {
        printf("IpRoute: Invalid Mask %s\n", pszNetMask);
        return;
        
    }

    dwIfIpAddr = inet_addr(pszInterface);
    if (dwIfIpAddr == INADDR_NONE)
    {
        printf("IpRoute: Bad Interface %s\n", pszInterface);
        return;
    }


    // Check if we have the given interface
    if ( (dwStatus = MyGetIpAddrTable(pIpAddrTable)) != NO_ERROR)
    {
        printf("GetIpAddrTable returned 0x%x\n", dwStatus);
        if (pIpAddrTable)
            free(pIpAddrTable);
        return;
    }
    assert(pIpAddrTable);
    if ( InterfaceIpToIdxAndMask(pIpAddrTable, pszInterface, dwIfIndex, dwIfMask) == FALSE)
    {
        printf("IpRoute: Bad Argument %s\n", pszInterface);
        return;
    }
    free(pIpAddrTable);


    if ( (routeEntry.dwForwardNextHop & dwIfMask) != (dwIfIpAddr & dwIfMask) )
    {
        printf("IpRoute: Gateway %s and Interface %s are not in the same subnet.\n", pszGateway, pszInterface);
        return;
    }

    routeEntry.dwForwardIfIndex = dwIfIndex;
    
    routeEntry.dwForwardMetric1 = dwMetric;

    // some default values
    routeEntry.dwForwardProto = MIB_IPPROTO_LOCAL;
    routeEntry.dwForwardMetric2 = (DWORD)-1;
    routeEntry.dwForwardMetric3 = (DWORD)-1;
    routeEntry.dwForwardMetric4 = (DWORD)-1;
    
    dwStatus = SetIpForwardEntry(&routeEntry); 
    if (dwStatus != NO_ERROR)
    {
        printf("IpRoute: couldn't add (%s), dwStatus = %lu.\n",
                    pszDest, dwStatus);
    }

}


void DoDeleteIpForwardEntry(char* pszDest)
{
    DWORD dwStatus, dwDelStatus, i;
    PMIB_IPFORWARDTABLE pIpRouteTab = NULL; // Ip routing table
    MIB_IPFORWARDROW routeEntry;            // Ip routing table row entry
    DWORD dwForwardDest = 0;
    bool fDeleted = FALSE;
    

    memset(&routeEntry, 0, sizeof(MIB_IPFORWARDROW));
    dwForwardDest = inet_addr(pszDest); // convert dotted ip addr. to ip addr.
    if (dwForwardDest == INADDR_NONE)
    {
        printf("IpRoute: Bad Destination %s\n", pszDest);
        return;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -