From 8ec93653e690da9175413110bedd516a7a114406 Mon Sep 17 00:00:00 2001 From: Thomas Bluemel Date: Sat, 1 Oct 2005 19:01:07 +0000 Subject: [PATCH] fixed some memory leaks and build it as unicode by default svn path=/trunk/; revision=18197 --- reactos/apps/utils/net/route/route.c | 236 ++++++++++++++++++------- reactos/apps/utils/net/route/route.xml | 4 +- 2 files changed, 177 insertions(+), 63 deletions(-) diff --git a/reactos/apps/utils/net/route/route.c b/reactos/apps/utils/net/route/route.c index c3325861745..f2144ace2ee 100644 --- a/reactos/apps/utils/net/route/route.c +++ b/reactos/apps/utils/net/route/route.c @@ -22,69 +22,120 @@ int Usage() { - fprintf( stderr, - "route usage:\n" - "route print\n" - " prints the route table\n" - "route add [mask ] [metric ]\n" - " adds a route\n" - "route delete \n" - " deletes a route\n" ); + _ftprintf( stderr, + _T("route usage:\n" + "route print\n" + " prints the route table\n" + "route add [mask ] [metric ]\n" + " adds a route\n" + "route delete \n" + " deletes a route\n") ); return 1; } int PrintRoutes() { PMIB_IPFORWARDTABLE IpForwardTable = NULL; - PIP_ADAPTER_INFO pAdapterInfo = NULL; - DWORD Error = 0; + PIP_ADAPTER_INFO pAdapterInfo; ULONG Size = 0; - ULONG adaptOutBufLen; + DWORD Error = 0; + ULONG adaptOutBufLen = sizeof(IP_ADAPTER_INFO); TCHAR DefGate[16]; - char Destination[IPBUF], Gateway[IPBUF], Netmask[IPBUF]; + TCHAR Destination[IPBUF], Gateway[IPBUF], Netmask[IPBUF]; unsigned int i; /* set required buffer size */ - pAdapterInfo = (IP_ADAPTER_INFO *) malloc( sizeof(IP_ADAPTER_INFO) ); - if (GetAdaptersInfo( pAdapterInfo, &adaptOutBufLen) == ERROR_INSUFFICIENT_BUFFER) + pAdapterInfo = (IP_ADAPTER_INFO *) malloc( adaptOutBufLen ); + if (pAdapterInfo == NULL) + { + Error = ERROR_NOT_ENOUGH_MEMORY; + goto Error; + } + if (GetAdaptersInfo( pAdapterInfo, &adaptOutBufLen) == ERROR_BUFFER_OVERFLOW) + { + free (pAdapterInfo); pAdapterInfo = (IP_ADAPTER_INFO *) malloc (adaptOutBufLen); + if (pAdapterInfo == NULL) + { + Error = ERROR_NOT_ENOUGH_MEMORY; + goto Error; + } + } if( (GetIpForwardTable( NULL, &Size, TRUE )) == ERROR_INSUFFICIENT_BUFFER ) - IpForwardTable = malloc( Size ); - - if ((Error = (GetAdaptersInfo(pAdapterInfo, &adaptOutBufLen) == NO_ERROR)) && - (Error = (GetIpForwardTable(IpForwardTable, &Size, TRUE) == NO_ERROR))) { - _tcsncpy(DefGate, pAdapterInfo->GatewayList.IpAddress.String, 16); + if (!(IpForwardTable = malloc( Size ))) + { + free(pAdapterInfo); + Error = ERROR_NOT_ENOUGH_MEMORY; + goto Error; + } + } + + if (((Error = GetAdaptersInfo(pAdapterInfo, &adaptOutBufLen)) == NO_ERROR) && + ((Error = GetIpForwardTable(IpForwardTable, &Size, TRUE)) == NO_ERROR)) + { + _stprintf(DefGate, +#if UNICODE + _T("%hs"), +#else + _T("%s"), +#endif + pAdapterInfo->GatewayList.IpAddress.String); _tprintf(_T("===========================================================================\n")); _tprintf(_T("Interface List\n")); + /* FIXME - sort by the index! */ while (pAdapterInfo) { - _tprintf(_T("0x%lu ........................... %s\n"), pAdapterInfo->Index, pAdapterInfo->Description); + _tprintf(_T("0x%lu ........................... " +#if UNICODE + "%hs\n"), +#else + "%s\n"), +#endif + pAdapterInfo->Index, pAdapterInfo->Description); pAdapterInfo = pAdapterInfo->Next; } _tprintf(_T("===========================================================================\n")); _tprintf(_T("===========================================================================\n")); _tprintf(_T("Active Routes:\n")); - printf( "%-27s%-17s%-14s%-11s%-10s\n", - "Network Destination", - "Netmask", - "Gateway", - "Interface", - "Metric" ); + _tprintf( _T("%-27s%-17s%-14s%-11s%-10s\n"), + _T("Network Destination"), + _T("Netmask"), + _T("Gateway"), + _T("Interface"), + _T("Metric") ); for( i = 0; i < IpForwardTable->dwNumEntries; i++ ) { - strcpy( Destination, inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].dwForwardDest) ) ); - strcpy( Netmask, inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].dwForwardMask) ) ); - strcpy( Gateway, inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].dwForwardNextHop) ) ); + _stprintf( Destination, +#if UNICODE + _T("%hs"), +#else + _T("%s"), +#endif + inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].dwForwardDest) ) ); + _stprintf( Netmask, +#if UNICODE + _T("%hs"), +#else + _T("%s"), +#endif + inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].dwForwardMask) ) ); + _stprintf( Gateway, +#if UNICODE + _T("%hs"), +#else + _T("%s"), +#endif + inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].dwForwardNextHop) ) ); - printf( "%17s%17s%17s%16ld%9ld\n", - Destination, - Netmask, - Gateway, - IpForwardTable->table[i].dwForwardIfIndex, - IpForwardTable->table[i].dwForwardMetric1 ); + _tprintf( _T("%17s%17s%17s%16ld%9ld\n"), + Destination, + Netmask, + Gateway, + IpForwardTable->table[i].dwForwardIfIndex, + IpForwardTable->table[i].dwForwardMetric1 ); } _tprintf(_T("Default Gateway:%18s\n"), DefGate); _tprintf(_T("===========================================================================\n")); @@ -97,98 +148,159 @@ int PrintRoutes() } else { - fprintf( stderr, "Route enumerate failed\n" ); +Error: + _ftprintf( stderr, _T("Route enumerate failed\n") ); return Error; } } int convert_add_cmd_line( PMIB_IPFORWARDROW RowToAdd, - int argc, char **argv ) { + int argc, TCHAR **argv ) { int i; +#if UNICODE + char addr[16]; +#endif if( argc > 1 ) + { +#if UNICODE + sprintf( addr, "%ls", argv[0] ); + RowToAdd->dwForwardDest = inet_addr( addr ); +#else RowToAdd->dwForwardDest = inet_addr( argv[0] ); +#endif + } else return FALSE; for( i = 1; i < argc; i++ ) { - if( !strcasecmp( argv[i], "mask" ) ) + if( !_tcscmp( argv[i], _T("mask") ) ) { i++; if( i >= argc ) return FALSE; +#if UNICODE + sprintf( addr, "%ls", argv[i] ); + RowToAdd->dwForwardDest = inet_addr( addr ); +#else RowToAdd->dwForwardMask = inet_addr( argv[i] ); +#endif } - else if( !strcasecmp( argv[i], "metric" ) ) + else if( !_tcscmp( argv[i], _T("metric") ) ) { i++; if( i >= argc ) return FALSE; - RowToAdd->dwForwardMetric1 = atoi( argv[i] ); + RowToAdd->dwForwardMetric1 = _ttoi( argv[i] ); } else + { +#if UNICODE + sprintf( addr, "%ls", argv[i] ); + RowToAdd->dwForwardNextHop = inet_addr( addr ); +#else RowToAdd->dwForwardNextHop = inet_addr( argv[i] ); +#endif + } } return TRUE; } -int add_route( int argc, char **argv ) { +int add_route( int argc, TCHAR **argv ) { MIB_IPFORWARDROW RowToAdd = { 0 }; DWORD Error; if( argc < 2 || !convert_add_cmd_line( &RowToAdd, argc, argv ) ) { - fprintf( stderr, - "route add usage:\n" - "route add [mask ] [metric ]\n" - " Adds a route to the IP route table.\n" - " is the network or host to add a route to.\n" - " is the netmask to use (autodetected if unspecified)\n" - " is the gateway to use to access the network\n" - " is the metric to use (lower is preferred)\n" ); + _ftprintf( stderr, + _T("route add usage:\n" + "route add [mask ] [metric ]\n" + " Adds a route to the IP route table.\n" + " is the network or host to add a route to.\n" + " is the netmask to use (autodetected if unspecified)\n" + " is the gateway to use to access the network\n" + " is the metric to use (lower is preferred)\n") ); return 1; } if( (Error = CreateIpForwardEntry( &RowToAdd )) == ERROR_SUCCESS ) return 0; - fprintf( stderr, "Route addition failed\n" ); + _ftprintf( stderr, _T("Route addition failed\n") ); return Error; } -int del_route( int argc, char **argv ) +int del_route( int argc, TCHAR **argv ) { MIB_IPFORWARDROW RowToDel = { 0 }; DWORD Error; if( argc < 2 || !convert_add_cmd_line( &RowToDel, argc, argv ) ) { - fprintf( stderr, - "route delete usage:\n" - "route delete \n" - " Removes a route from the IP route table.\n" - " is the network or host to add a route to.\n" - " is the gateway to remove the route from.\n" ); + _ftprintf( stderr, + _T("route delete usage:\n" + "route delete \n" + " Removes a route from the IP route table.\n" + " is the network or host to add a route to.\n" + " is the gateway to remove the route from.\n") ); return 1; } if( (Error = DeleteIpForwardEntry( &RowToDel )) == ERROR_SUCCESS ) return 0; - fprintf( stderr, "Route addition failed\n" ); + _ftprintf( stderr, _T("Route addition failed\n") ); return Error; } -int main( int argc, char **argv ) +int _tmain( int argc, TCHAR **argv ) { if( argc < 2 ) return Usage(); - else if ( !strcasecmp( argv[1], "print" ) ) + else if ( !_tcscmp( argv[1], _T("print") ) ) return PrintRoutes(); - else if( !strcasecmp( argv[1], "add" ) ) + else if( !_tcscmp( argv[1], _T("add") ) ) return add_route( argc-2, argv+2 ); - else if( !strcasecmp( argv[1], "delete" ) ) + else if( !_tcscmp( argv[1], _T("delete") ) ) return del_route( argc-2, argv+2 ); else return Usage(); } + +#if defined(_UNICODE) && defined(__GNUC__) +/* HACK - MINGW HAS NO OFFICIAL SUPPORT FOR wmain()!!! */ +int main( int argc, char **argv ) +{ + WCHAR **argvW; + int i, j, Ret = 1; + + if ((argvW = malloc(argc * sizeof(WCHAR*)))) + { + /* convert the arguments */ + for (i = 0, j = 0; i < argc; i++) + { + if (!(argvW[i] = malloc((strlen(argv[i]) + 1) * sizeof(WCHAR)))) + { + j++; + } + swprintf(argvW[i], L"%hs", argv[i]); + } + + if (j == 0) + { + /* no error converting the parameters, call wmain() */ + Ret = wmain(argc, argvW); + } + + /* free the arguments */ + for (i = 0; i < argc; i++) + { + if (argvW[i]) + free(argvW[i]); + } + free(argvW); + } + + return Ret; +} +#endif diff --git a/reactos/apps/utils/net/route/route.xml b/reactos/apps/utils/net/route/route.xml index d509507b71d..66c14726196 100644 --- a/reactos/apps/utils/net/route/route.xml +++ b/reactos/apps/utils/net/route/route.xml @@ -1,6 +1,8 @@ - + . + + kernel32 ws2_32 iphlpapi