fixed some memory leaks and build it as unicode by default

svn path=/trunk/; revision=18197
This commit is contained in:
Thomas Bluemel 2005-10-01 19:01:07 +00:00
parent 16e0c94db4
commit 8ec93653e6
2 changed files with 177 additions and 63 deletions

View file

@ -22,69 +22,120 @@
int Usage() int Usage()
{ {
fprintf( stderr, _ftprintf( stderr,
"route usage:\n" _T("route usage:\n"
"route print\n" "route print\n"
" prints the route table\n" " prints the route table\n"
"route add <target> [mask <mask>] <gw> [metric <m>]\n" "route add <target> [mask <mask>] <gw> [metric <m>]\n"
" adds a route\n" " adds a route\n"
"route delete <target> <gw>\n" "route delete <target> <gw>\n"
" deletes a route\n" ); " deletes a route\n") );
return 1; return 1;
} }
int PrintRoutes() int PrintRoutes()
{ {
PMIB_IPFORWARDTABLE IpForwardTable = NULL; PMIB_IPFORWARDTABLE IpForwardTable = NULL;
PIP_ADAPTER_INFO pAdapterInfo = NULL; PIP_ADAPTER_INFO pAdapterInfo;
DWORD Error = 0;
ULONG Size = 0; ULONG Size = 0;
ULONG adaptOutBufLen; DWORD Error = 0;
ULONG adaptOutBufLen = sizeof(IP_ADAPTER_INFO);
TCHAR DefGate[16]; TCHAR DefGate[16];
char Destination[IPBUF], Gateway[IPBUF], Netmask[IPBUF]; TCHAR Destination[IPBUF], Gateway[IPBUF], Netmask[IPBUF];
unsigned int i; unsigned int i;
/* set required buffer size */ /* set required buffer size */
pAdapterInfo = (IP_ADAPTER_INFO *) malloc( sizeof(IP_ADAPTER_INFO) ); pAdapterInfo = (IP_ADAPTER_INFO *) malloc( adaptOutBufLen );
if (GetAdaptersInfo( pAdapterInfo, &adaptOutBufLen) == ERROR_INSUFFICIENT_BUFFER) 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); pAdapterInfo = (IP_ADAPTER_INFO *) malloc (adaptOutBufLen);
if (pAdapterInfo == NULL)
{
Error = ERROR_NOT_ENOUGH_MEMORY;
goto Error;
}
}
if( (GetIpForwardTable( NULL, &Size, TRUE )) == ERROR_INSUFFICIENT_BUFFER ) 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("===========================================================================\n"));
_tprintf(_T("Interface List\n")); _tprintf(_T("Interface List\n"));
/* FIXME - sort by the index! */
while (pAdapterInfo) 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; pAdapterInfo = pAdapterInfo->Next;
} }
_tprintf(_T("===========================================================================\n")); _tprintf(_T("===========================================================================\n"));
_tprintf(_T("===========================================================================\n")); _tprintf(_T("===========================================================================\n"));
_tprintf(_T("Active Routes:\n")); _tprintf(_T("Active Routes:\n"));
printf( "%-27s%-17s%-14s%-11s%-10s\n", _tprintf( _T("%-27s%-17s%-14s%-11s%-10s\n"),
"Network Destination", _T("Network Destination"),
"Netmask", _T("Netmask"),
"Gateway", _T("Gateway"),
"Interface", _T("Interface"),
"Metric" ); _T("Metric") );
for( i = 0; i < IpForwardTable->dwNumEntries; i++ ) for( i = 0; i < IpForwardTable->dwNumEntries; i++ )
{ {
strcpy( Destination, inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].dwForwardDest) ) ); _stprintf( Destination,
strcpy( Netmask, inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].dwForwardMask) ) ); #if UNICODE
strcpy( Gateway, inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].dwForwardNextHop) ) ); _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", _tprintf( _T("%17s%17s%17s%16ld%9ld\n"),
Destination, Destination,
Netmask, Netmask,
Gateway, Gateway,
IpForwardTable->table[i].dwForwardIfIndex, IpForwardTable->table[i].dwForwardIfIndex,
IpForwardTable->table[i].dwForwardMetric1 ); IpForwardTable->table[i].dwForwardMetric1 );
} }
_tprintf(_T("Default Gateway:%18s\n"), DefGate); _tprintf(_T("Default Gateway:%18s\n"), DefGate);
_tprintf(_T("===========================================================================\n")); _tprintf(_T("===========================================================================\n"));
@ -97,98 +148,159 @@ int PrintRoutes()
} }
else else
{ {
fprintf( stderr, "Route enumerate failed\n" ); Error:
_ftprintf( stderr, _T("Route enumerate failed\n") );
return Error; return Error;
} }
} }
int convert_add_cmd_line( PMIB_IPFORWARDROW RowToAdd, int convert_add_cmd_line( PMIB_IPFORWARDROW RowToAdd,
int argc, char **argv ) { int argc, TCHAR **argv ) {
int i; int i;
#if UNICODE
char addr[16];
#endif
if( argc > 1 ) if( argc > 1 )
{
#if UNICODE
sprintf( addr, "%ls", argv[0] );
RowToAdd->dwForwardDest = inet_addr( addr );
#else
RowToAdd->dwForwardDest = inet_addr( argv[0] ); RowToAdd->dwForwardDest = inet_addr( argv[0] );
#endif
}
else else
return FALSE; return FALSE;
for( i = 1; i < argc; i++ ) for( i = 1; i < argc; i++ )
{ {
if( !strcasecmp( argv[i], "mask" ) ) if( !_tcscmp( argv[i], _T("mask") ) )
{ {
i++; if( i >= argc ) return FALSE; 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] ); RowToAdd->dwForwardMask = inet_addr( argv[i] );
#endif
} }
else if( !strcasecmp( argv[i], "metric" ) ) else if( !_tcscmp( argv[i], _T("metric") ) )
{ {
i++; i++;
if( i >= argc ) if( i >= argc )
return FALSE; return FALSE;
RowToAdd->dwForwardMetric1 = atoi( argv[i] ); RowToAdd->dwForwardMetric1 = _ttoi( argv[i] );
} }
else else
{
#if UNICODE
sprintf( addr, "%ls", argv[i] );
RowToAdd->dwForwardNextHop = inet_addr( addr );
#else
RowToAdd->dwForwardNextHop = inet_addr( argv[i] ); RowToAdd->dwForwardNextHop = inet_addr( argv[i] );
#endif
}
} }
return TRUE; return TRUE;
} }
int add_route( int argc, char **argv ) { int add_route( int argc, TCHAR **argv ) {
MIB_IPFORWARDROW RowToAdd = { 0 }; MIB_IPFORWARDROW RowToAdd = { 0 };
DWORD Error; DWORD Error;
if( argc < 2 || !convert_add_cmd_line( &RowToAdd, argc, argv ) ) if( argc < 2 || !convert_add_cmd_line( &RowToAdd, argc, argv ) )
{ {
fprintf( stderr, _ftprintf( stderr,
"route add usage:\n" _T("route add usage:\n"
"route add <target> [mask <mask>] <gw> [metric <m>]\n" "route add <target> [mask <mask>] <gw> [metric <m>]\n"
" Adds a route to the IP route table.\n" " Adds a route to the IP route table.\n"
" <target> is the network or host to add a route to.\n" " <target> is the network or host to add a route to.\n"
" <mask> is the netmask to use (autodetected if unspecified)\n" " <mask> is the netmask to use (autodetected if unspecified)\n"
" <gw> is the gateway to use to access the network\n" " <gw> is the gateway to use to access the network\n"
" <m> is the metric to use (lower is preferred)\n" ); " <m> is the metric to use (lower is preferred)\n") );
return 1; return 1;
} }
if( (Error = CreateIpForwardEntry( &RowToAdd )) == ERROR_SUCCESS ) if( (Error = CreateIpForwardEntry( &RowToAdd )) == ERROR_SUCCESS )
return 0; return 0;
fprintf( stderr, "Route addition failed\n" ); _ftprintf( stderr, _T("Route addition failed\n") );
return Error; return Error;
} }
int del_route( int argc, char **argv ) int del_route( int argc, TCHAR **argv )
{ {
MIB_IPFORWARDROW RowToDel = { 0 }; MIB_IPFORWARDROW RowToDel = { 0 };
DWORD Error; DWORD Error;
if( argc < 2 || !convert_add_cmd_line( &RowToDel, argc, argv ) ) if( argc < 2 || !convert_add_cmd_line( &RowToDel, argc, argv ) )
{ {
fprintf( stderr, _ftprintf( stderr,
"route delete usage:\n" _T("route delete usage:\n"
"route delete <target> <gw>\n" "route delete <target> <gw>\n"
" Removes a route from the IP route table.\n" " Removes a route from the IP route table.\n"
" <target> is the network or host to add a route to.\n" " <target> is the network or host to add a route to.\n"
" <gw> is the gateway to remove the route from.\n" ); " <gw> is the gateway to remove the route from.\n") );
return 1; return 1;
} }
if( (Error = DeleteIpForwardEntry( &RowToDel )) == ERROR_SUCCESS ) if( (Error = DeleteIpForwardEntry( &RowToDel )) == ERROR_SUCCESS )
return 0; return 0;
fprintf( stderr, "Route addition failed\n" ); _ftprintf( stderr, _T("Route addition failed\n") );
return Error; return Error;
} }
int main( int argc, char **argv ) int _tmain( int argc, TCHAR **argv )
{ {
if( argc < 2 ) if( argc < 2 )
return Usage(); return Usage();
else if ( !strcasecmp( argv[1], "print" ) ) else if ( !_tcscmp( argv[1], _T("print") ) )
return PrintRoutes(); return PrintRoutes();
else if( !strcasecmp( argv[1], "add" ) ) else if( !_tcscmp( argv[1], _T("add") ) )
return add_route( argc-2, argv+2 ); 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 ); return del_route( argc-2, argv+2 );
else else
return Usage(); 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

View file

@ -1,6 +1,8 @@
<module name="route" type="win32cui" installbase="system32" installname="route.exe" allowwarnings="true"> <module name="route" type="win32cui" installbase="system32" installname="route.exe">
<include base="route">.</include> <include base="route">.</include>
<define name="__USE_W32API" /> <define name="__USE_W32API" />
<define name="UNICODE" />
<define name="_UNICODE" />
<library>kernel32</library> <library>kernel32</library>
<library>ws2_32</library> <library>ws2_32</library>
<library>iphlpapi</library> <library>iphlpapi</library>