ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

route.c
Go to the documentation of this file.
00001 /* Poor man's route
00002  *
00003  * Supported commands:
00004  *
00005  * "print"
00006  * "add" target ["mask" mask] gw ["metric" metric]
00007  * "delete" target gw
00008  *
00009  * Goals:
00010  *
00011  * Flexible, simple
00012  */
00013 
00014 #include <stdio.h>
00015 #include <winsock2.h>
00016 #include <windows.h>
00017 #include <iphlpapi.h>
00018 #include <tchar.h>
00019 
00020 #define IPBUF 17
00021 #define IN_ADDR_OF(x) *((struct in_addr *)&(x))
00022 
00023 static int Usage()
00024 {
00025     _ftprintf( stderr,
00026                _T("route usage:\n")
00027                _T("route print\n")
00028                _T("  prints the route table\n")
00029                _T("route add <target> [mask <mask>] <gw> [metric <m>]\n")
00030                _T("  adds a route\n")
00031                _T("route delete <target> <gw>\n")
00032                _T("  deletes a route\n") );
00033     return 1;
00034 }
00035 
00036 static int PrintRoutes()
00037 {
00038     PMIB_IPFORWARDTABLE IpForwardTable = NULL;
00039     PIP_ADAPTER_INFO pAdapterInfo = NULL;
00040     ULONG Size = 0;
00041     DWORD Error = 0;
00042     ULONG adaptOutBufLen = sizeof(IP_ADAPTER_INFO);
00043     TCHAR DefGate[16];
00044     TCHAR Destination[IPBUF], Gateway[IPBUF], Netmask[IPBUF];
00045     unsigned int i;
00046 
00047     /* set required buffer size */
00048     pAdapterInfo = (IP_ADAPTER_INFO *) malloc( adaptOutBufLen );
00049     if (pAdapterInfo == NULL)
00050     {
00051         Error = ERROR_NOT_ENOUGH_MEMORY;
00052         goto Error;
00053     }
00054     if (GetAdaptersInfo( pAdapterInfo, &adaptOutBufLen) == ERROR_BUFFER_OVERFLOW)
00055     {
00056        free (pAdapterInfo);
00057        pAdapterInfo = (IP_ADAPTER_INFO *) malloc (adaptOutBufLen);
00058        if (pAdapterInfo == NULL)
00059        {
00060            Error = ERROR_NOT_ENOUGH_MEMORY;
00061            goto Error;
00062        }
00063     }
00064 
00065     if( (GetIpForwardTable( NULL, &Size, TRUE )) == ERROR_INSUFFICIENT_BUFFER )
00066     {
00067         if (!(IpForwardTable = malloc( Size )))
00068         {
00069             Error = ERROR_NOT_ENOUGH_MEMORY;
00070             goto Error;
00071         }
00072     }
00073 
00074     if (((Error = GetAdaptersInfo(pAdapterInfo, &adaptOutBufLen)) == NO_ERROR) &&
00075         ((Error = GetIpForwardTable(IpForwardTable, &Size, TRUE)) == NO_ERROR))
00076     {
00077         _stprintf(DefGate,
00078 #ifdef UNICODE
00079                   _T("%hs"),
00080 #else
00081                   _T("%s"),
00082 #endif
00083                   pAdapterInfo->GatewayList.IpAddress.String);
00084         _tprintf(_T("===========================================================================\n"));
00085         _tprintf(_T("Interface List\n"));
00086         /* FIXME - sort by the index! */
00087         while (pAdapterInfo)
00088         {
00089 #ifdef UNICODE
00090             _tprintf(_T("0x%lu ........................... %hs\n"),
00091 #else
00092             _tprintf(_T("0x%lu ........................... %s\n"),
00093 #endif
00094                      pAdapterInfo->Index, pAdapterInfo->Description);
00095             pAdapterInfo = pAdapterInfo->Next;
00096         }
00097         _tprintf(_T("===========================================================================\n"));
00098 
00099         _tprintf(_T("===========================================================================\n"));
00100         _tprintf(_T("Active Routes:\n"));
00101         _tprintf( _T("%-27s%-17s%-14s%-11s%-10s\n"),
00102                   _T("Network Destination"),
00103                   _T("Netmask"),
00104                   _T("Gateway"),
00105                   _T("Interface"),
00106                   _T("Metric") );
00107         for( i = 0; i < IpForwardTable->dwNumEntries; i++ )
00108         {
00109             _stprintf( Destination,
00110 #ifdef UNICODE
00111                        _T("%hs"),
00112 #else
00113                        _T("%s"),
00114 #endif
00115                        inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].dwForwardDest) ) );
00116             _stprintf( Netmask,
00117 #ifdef UNICODE
00118                        _T("%hs"),
00119 #else
00120                        _T("%s"),
00121 #endif
00122                        inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].dwForwardMask) ) );
00123             _stprintf( Gateway,
00124 #ifdef UNICODE
00125                        _T("%hs"),
00126 #else
00127                        _T("%s"),
00128 #endif
00129                        inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].dwForwardNextHop) ) );
00130 
00131             _tprintf( _T("%17s%17s%17s%16ld%9ld\n"),
00132                       Destination,
00133                       Netmask,
00134                       Gateway,
00135                       IpForwardTable->table[i].dwForwardIfIndex,
00136                       IpForwardTable->table[i].dwForwardMetric1 );
00137         }
00138         _tprintf(_T("Default Gateway:%18s\n"), DefGate);
00139         _tprintf(_T("===========================================================================\n"));
00140         _tprintf(_T("Persistent Routes:\n"));
00141 
00142         free(IpForwardTable);
00143         free(pAdapterInfo);
00144 
00145         return ERROR_SUCCESS;
00146     }
00147     else
00148     {
00149 Error:
00150         if (pAdapterInfo) free(pAdapterInfo);
00151         if (IpForwardTable) free(IpForwardTable);
00152         _ftprintf( stderr, _T("Route enumerate failed\n") );
00153         return Error;
00154     }
00155 }
00156 
00157 static int convert_add_cmd_line( PMIB_IPFORWARDROW RowToAdd,
00158               int argc, TCHAR **argv ) {
00159     int i;
00160 #ifdef UNICODE
00161     char addr[16];
00162 #endif
00163 
00164     if( argc > 1 )
00165     {
00166 #ifdef UNICODE
00167         sprintf( addr, "%ls", argv[0] );
00168         RowToAdd->dwForwardDest = inet_addr( addr );
00169 #else
00170         RowToAdd->dwForwardDest = inet_addr( argv[0] );
00171 #endif
00172     }
00173     else
00174         return FALSE;
00175     for( i = 1; i < argc; i++ )
00176     {
00177         if( !_tcscmp( argv[i], _T("mask") ) )
00178         {
00179             i++; if( i >= argc ) return FALSE;
00180 #ifdef UNICODE
00181             sprintf( addr, "%ls", argv[i] );
00182             RowToAdd->dwForwardMask = inet_addr( addr );
00183 #else
00184             RowToAdd->dwForwardMask = inet_addr( argv[i] );
00185 #endif
00186         }
00187         else if( !_tcscmp( argv[i], _T("metric") ) )
00188         {
00189             i++;
00190             if( i >= argc )
00191                 return FALSE;
00192             RowToAdd->dwForwardMetric1 = _ttoi( argv[i] );
00193         }
00194         else
00195         {
00196 #ifdef UNICODE
00197             sprintf( addr, "%ls", argv[i] );
00198             RowToAdd->dwForwardNextHop = inet_addr( addr );
00199 #else
00200             RowToAdd->dwForwardNextHop = inet_addr( argv[i] );
00201 #endif
00202         }
00203     }
00204 
00205     return TRUE;
00206 }
00207 
00208 static int add_route( int argc, TCHAR **argv ) {
00209     MIB_IPFORWARDROW RowToAdd = { 0 };
00210     DWORD Error;
00211 
00212     if( argc < 2 || !convert_add_cmd_line( &RowToAdd, argc, argv ) )
00213     {
00214         _ftprintf( stderr,
00215                    _T("route add usage:\n")
00216                    _T("route add <target> [mask <mask>] <gw> [metric <m>]\n")
00217                    _T("  Adds a route to the IP route table.\n")
00218                    _T("  <target> is the network or host to add a route to.\n")
00219                    _T("  <mask>   is the netmask to use (autodetected if unspecified)\n")
00220                    _T("  <gw>     is the gateway to use to access the network\n")
00221                    _T("  <m>      is the metric to use (lower is preferred)\n") );
00222         return 1;
00223     }
00224 
00225     if( (Error = CreateIpForwardEntry( &RowToAdd )) == ERROR_SUCCESS )
00226         return 0;
00227 
00228     _ftprintf( stderr, _T("Route addition failed\n") );
00229     return Error;
00230 }
00231 
00232 static int del_route( int argc, TCHAR **argv )
00233 {
00234     MIB_IPFORWARDROW RowToDel = { 0 };
00235     DWORD Error;
00236 
00237     if( argc < 2 || !convert_add_cmd_line( &RowToDel, argc, argv ) )
00238     {
00239         _ftprintf( stderr,
00240                     _T("route delete usage:\n")
00241                     _T("route delete <target> <gw>\n")
00242                     _T("  Removes a route from the IP route table.\n")
00243                     _T("  <target> is the network or host to add a route to.\n")
00244                     _T("  <gw>     is the gateway to remove the route from.\n") );
00245         return 1;
00246     }
00247 
00248     if( (Error = DeleteIpForwardEntry( &RowToDel )) == ERROR_SUCCESS )
00249         return 0;
00250 
00251     _ftprintf( stderr, _T("Route addition failed\n") );
00252     return Error;
00253 }
00254 
00255 int _tmain( int argc, TCHAR **argv )
00256 {
00257     if( argc < 2 )
00258         return Usage();
00259     else if ( !_tcscmp( argv[1], _T("print") ) )
00260         return PrintRoutes();
00261     else if( !_tcscmp( argv[1], _T("add") ) )
00262         return add_route( argc-2, argv+2 );
00263     else if( !_tcscmp( argv[1], _T("delete") ) )
00264         return del_route( argc-2, argv+2 );
00265     else
00266         return Usage();
00267 }

Generated on Sun May 27 2012 04:17:15 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.