ReactOS  0.4.14-dev-52-g6116262
route.c File Reference
#include <stdarg.h>
#include <windef.h>
#include <winbase.h>
#include <stdio.h>
#include <malloc.h>
#include <winsock2.h>
#include <iphlpapi.h>
#include <tchar.h>
Include dependency graph for route.c:

Go to the source code of this file.

Macros

#define WIN32_NO_STATUS
 
#define _INC_WINDOWS
 
#define IPBUF   17
 
#define IN_ADDR_OF(x)   *((struct in_addr *)&(x))
 

Functions

static int Usage ()
 
static int PrintRoutes ()
 
static int convert_add_cmd_line (PMIB_IPFORWARDROW RowToAdd, int argc, TCHAR **argv)
 
static int add_route (int argc, TCHAR **argv)
 
static int del_route (int argc, TCHAR **argv)
 
int _tmain (int argc, TCHAR **argv)
 

Macro Definition Documentation

◆ _INC_WINDOWS

#define _INC_WINDOWS

Definition at line 20 of file route.c.

◆ IN_ADDR_OF

#define IN_ADDR_OF (   x)    *((struct in_addr *)&(x))

Definition at line 26 of file route.c.

◆ IPBUF

#define IPBUF   17

Definition at line 25 of file route.c.

◆ WIN32_NO_STATUS

#define WIN32_NO_STATUS

Definition at line 14 of file route.c.

Function Documentation

◆ _tmain()

int _tmain ( int  argc,
TCHAR **  argv 
)

Definition at line 260 of file route.c.

261 {
262  if( argc < 2 )
263  return Usage();
264  else if ( !_tcscmp( argv[1], _T("print") ) )
265  return PrintRoutes();
266  else if( !_tcscmp( argv[1], _T("add") ) )
267  return add_route( argc-2, argv+2 );
268  else if( !_tcscmp( argv[1], _T("delete") ) )
269  return del_route( argc-2, argv+2 );
270  else
271  return Usage();
272 }
static int argc
Definition: ServiceArgs.c:12
static int add_route(int argc, TCHAR **argv)
Definition: route.c:213
int _tcscmp(const _TCHAR *s1, const _TCHAR *s2)
Definition: tcscmp.h:8
#define argv
Definition: mplay32.c:18
static int del_route(int argc, TCHAR **argv)
Definition: route.c:237
#define _T(x)
Definition: vfdio.h:22
static int Usage()
Definition: route.c:28
static int PrintRoutes()
Definition: route.c:41

◆ add_route()

static int add_route ( int  argc,
TCHAR **  argv 
)
static

Definition at line 213 of file route.c.

213  {
214  MIB_IPFORWARDROW RowToAdd = { 0 };
215  DWORD Error;
216 
217  if( argc < 2 || !convert_add_cmd_line( &RowToAdd, argc, argv ) )
218  {
219  _ftprintf( stderr,
220  _T("route add usage:\n")
221  _T("route add <target> [mask <mask>] <gw> [metric <m>]\n")
222  _T(" Adds a route to the IP route table.\n")
223  _T(" <target> is the network or host to add a route to.\n")
224  _T(" <mask> is the netmask to use (autodetected if unspecified)\n")
225  _T(" <gw> is the gateway to use to access the network\n")
226  _T(" <m> is the metric to use (lower is preferred)\n") );
227  return 1;
228  }
229 
230  if( (Error = CreateIpForwardEntry( &RowToAdd )) == ERROR_SUCCESS )
231  return 0;
232 
233  _ftprintf( stderr, _T("Route addition failed\n") );
234  return Error;
235 }
static int argc
Definition: ServiceArgs.c:12
DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
#define ERROR_SUCCESS
Definition: deptool.c:10
#define argv
Definition: mplay32.c:18
#define _ftprintf
Definition: tchar.h:518
#define _T(x)
Definition: vfdio.h:22
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL Error
Definition: chkdsk.c:66
FILE * stderr
static int convert_add_cmd_line(PMIB_IPFORWARDROW RowToAdd, int argc, TCHAR **argv)
Definition: route.c:162

Referenced by _tmain().

◆ convert_add_cmd_line()

static int convert_add_cmd_line ( PMIB_IPFORWARDROW  RowToAdd,
int  argc,
TCHAR **  argv 
)
static

Definition at line 162 of file route.c.

163  {
164  int i;
165 #ifdef UNICODE
166  char addr[16];
167 #endif
168 
169  if( argc > 1 )
170  {
171 #ifdef UNICODE
172  sprintf( addr, "%ls", argv[0] );
173  RowToAdd->dwForwardDest = inet_addr( addr );
174 #else
175  RowToAdd->dwForwardDest = inet_addr( argv[0] );
176 #endif
177  }
178  else
179  return FALSE;
180  for( i = 1; i < argc; i++ )
181  {
182  if( !_tcscmp( argv[i], _T("mask") ) )
183  {
184  i++; if( i >= argc ) return FALSE;
185 #ifdef UNICODE
186  sprintf( addr, "%ls", argv[i] );
187  RowToAdd->dwForwardMask = inet_addr( addr );
188 #else
189  RowToAdd->dwForwardMask = inet_addr( argv[i] );
190 #endif
191  }
192  else if( !_tcscmp( argv[i], _T("metric") ) )
193  {
194  i++;
195  if( i >= argc )
196  return FALSE;
197  RowToAdd->dwForwardMetric1 = _ttoi( argv[i] );
198  }
199  else
200  {
201 #ifdef UNICODE
202  sprintf( addr, "%ls", argv[i] );
203  RowToAdd->dwForwardNextHop = inet_addr( addr );
204 #else
205  RowToAdd->dwForwardNextHop = inet_addr( argv[i] );
206 #endif
207  }
208  }
209 
210  return TRUE;
211 }
static int argc
Definition: ServiceArgs.c:12
#define TRUE
Definition: types.h:120
#define inet_addr(cp)
Definition: inet.h:98
int _tcscmp(const _TCHAR *s1, const _TCHAR *s2)
Definition: tcscmp.h:8
DWORD dwForwardMask
Definition: ipmib.h:72
#define argv
Definition: mplay32.c:18
#define sprintf(buf, format,...)
Definition: sprintf.c:55
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define _T(x)
Definition: vfdio.h:22
DWORD dwForwardNextHop
Definition: ipmib.h:74
DWORD dwForwardDest
Definition: ipmib.h:71
GLenum const GLvoid * addr
Definition: glext.h:9621
int CDECL _ttoi(const _TCHAR *str)
Definition: atoi.c:10
DWORD dwForwardMetric1
Definition: ipmib.h:88

Referenced by add_route(), and del_route().

◆ del_route()

static int del_route ( int  argc,
TCHAR **  argv 
)
static

Definition at line 237 of file route.c.

238 {
239  MIB_IPFORWARDROW RowToDel = { 0 };
240  DWORD Error;
241 
242  if( argc < 2 || !convert_add_cmd_line( &RowToDel, argc, argv ) )
243  {
244  _ftprintf( stderr,
245  _T("route delete usage:\n")
246  _T("route delete <target> <gw>\n")
247  _T(" Removes a route from the IP route table.\n")
248  _T(" <target> is the network or host to add a route to.\n")
249  _T(" <gw> is the gateway to remove the route from.\n") );
250  return 1;
251  }
252 
253  if( (Error = DeleteIpForwardEntry( &RowToDel )) == ERROR_SUCCESS )
254  return 0;
255 
256  _ftprintf( stderr, _T("Route addition failed\n") );
257  return Error;
258 }
static int argc
Definition: ServiceArgs.c:12
#define ERROR_SUCCESS
Definition: deptool.c:10
DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
#define argv
Definition: mplay32.c:18
#define _ftprintf
Definition: tchar.h:518
#define _T(x)
Definition: vfdio.h:22
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL Error
Definition: chkdsk.c:66
FILE * stderr
static int convert_add_cmd_line(PMIB_IPFORWARDROW RowToAdd, int argc, TCHAR **argv)
Definition: route.c:162

Referenced by _tmain().

◆ PrintRoutes()

static int PrintRoutes ( )
static

Definition at line 41 of file route.c.

42 {
43  PMIB_IPFORWARDTABLE IpForwardTable = NULL;
44  PIP_ADAPTER_INFO pAdapterInfo = NULL;
45  ULONG Size = 0;
46  DWORD Error = 0;
47  ULONG adaptOutBufLen = sizeof(IP_ADAPTER_INFO);
48  TCHAR DefGate[16];
49  TCHAR Destination[IPBUF], Gateway[IPBUF], Netmask[IPBUF];
50  unsigned int i;
51 
52  /* set required buffer size */
53  pAdapterInfo = (IP_ADAPTER_INFO *) malloc( adaptOutBufLen );
54  if (pAdapterInfo == NULL)
55  {
57  goto Error;
58  }
59  if (GetAdaptersInfo( pAdapterInfo, &adaptOutBufLen) == ERROR_BUFFER_OVERFLOW)
60  {
61  free (pAdapterInfo);
62  pAdapterInfo = (IP_ADAPTER_INFO *) malloc (adaptOutBufLen);
63  if (pAdapterInfo == NULL)
64  {
66  goto Error;
67  }
68  }
69 
71  {
72  if (!(IpForwardTable = malloc( Size )))
73  {
75  goto Error;
76  }
77  }
78 
79  if (((Error = GetAdaptersInfo(pAdapterInfo, &adaptOutBufLen)) == NO_ERROR) &&
80  ((Error = GetIpForwardTable(IpForwardTable, &Size, TRUE)) == NO_ERROR))
81  {
82  _stprintf(DefGate,
83 #ifdef UNICODE
84  _T("%hs"),
85 #else
86  _T("%s"),
87 #endif
88  pAdapterInfo->GatewayList.IpAddress.String);
89  _tprintf(_T("===========================================================================\n"));
90  _tprintf(_T("Interface List\n"));
91  /* FIXME - sort by the index! */
92  while (pAdapterInfo)
93  {
94 #ifdef UNICODE
95  _tprintf(_T("0x%lu ........................... %hs\n"),
96 #else
97  _tprintf(_T("0x%lu ........................... %s\n"),
98 #endif
99  pAdapterInfo->Index, pAdapterInfo->Description);
100  pAdapterInfo = pAdapterInfo->Next;
101  }
102  _tprintf(_T("===========================================================================\n"));
103 
104  _tprintf(_T("===========================================================================\n"));
105  _tprintf(_T("Active Routes:\n"));
106  _tprintf( _T("%-27s%-17s%-14s%-11s%-10s\n"),
107  _T("Network Destination"),
108  _T("Netmask"),
109  _T("Gateway"),
110  _T("Interface"),
111  _T("Metric") );
112  for( i = 0; i < IpForwardTable->dwNumEntries; i++ )
113  {
115 #ifdef UNICODE
116  _T("%hs"),
117 #else
118  _T("%s"),
119 #endif
120  inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].dwForwardDest) ) );
121  _stprintf( Netmask,
122 #ifdef UNICODE
123  _T("%hs"),
124 #else
125  _T("%s"),
126 #endif
127  inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].dwForwardMask) ) );
128  _stprintf( Gateway,
129 #ifdef UNICODE
130  _T("%hs"),
131 #else
132  _T("%s"),
133 #endif
134  inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].dwForwardNextHop) ) );
135 
136  _tprintf( _T("%17s%17s%17s%16ld%9ld\n"),
137  Destination,
138  Netmask,
139  Gateway,
140  IpForwardTable->table[i].dwForwardIfIndex,
141  IpForwardTable->table[i].dwForwardMetric1 );
142  }
143  _tprintf(_T("Default Gateway:%18s\n"), DefGate);
144  _tprintf(_T("===========================================================================\n"));
145  _tprintf(_T("Persistent Routes:\n"));
146 
147  free(IpForwardTable);
148  free(pAdapterInfo);
149 
150  return ERROR_SUCCESS;
151  }
152  else
153  {
154 Error:
155  if (pAdapterInfo) free(pAdapterInfo);
156  if (IpForwardTable) free(IpForwardTable);
157  _ftprintf( stderr, _T("Route enumerate failed\n") );
158  return Error;
159  }
160 }
wchar_t UNICODE
Definition: ms-dtyp.idl:111
#define _tprintf
Definition: tchar.h:506
#define TRUE
Definition: types.h:120
char Description[MAX_ADAPTER_DESCRIPTION_LENGTH+4]
Definition: iptypes.h:56
#define ERROR_SUCCESS
Definition: deptool.c:10
#define IN_ADDR_OF(x)
Definition: route.c:26
#define ERROR_BUFFER_OVERFLOW
Definition: winerror.h:185
#define free
Definition: debug_ros.c:5
IP_ADDRESS_STRING IpAddress
Definition: iptypes.h:47
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
char String[4 *4]
Definition: iptypes.h:42
DWORD dwForwardMask
Definition: ipmib.h:72
#define IPBUF
Definition: route.c:25
#define NO_ERROR
Definition: dderror.h:5
struct _IP_ADAPTER_INFO IP_ADAPTER_INFO
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
smooth NULL
Definition: ftsmooth.c:416
#define _ftprintf
Definition: tchar.h:518
#define inet_ntoa(addr)
Definition: inet.h:100
IF_INDEX dwForwardIfIndex
Definition: ipmib.h:75
char TCHAR
Definition: xmlstorage.h:189
#define _T(x)
Definition: vfdio.h:22
DWORD dwForwardNextHop
Definition: ipmib.h:74
DWORD dwNumEntries
Definition: ipmib.h:97
DWORD dwForwardDest
Definition: ipmib.h:71
unsigned long DWORD
Definition: ntddk_ex.h:95
_In_ PUNICODE_STRING _Inout_ PUNICODE_STRING Destination
Definition: rtlfuncs.h:2891
BOOL Error
Definition: chkdsk.c:66
MIB_IPFORWARDROW table[1]
Definition: ipmib.h:98
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
#define _stprintf
Definition: utility.h:124
DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
IP_ADDR_STRING GatewayList
Definition: iptypes.h:64
DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen)
unsigned int ULONG
Definition: retypes.h:1
FILE * stderr
#define malloc
Definition: debug_ros.c:4
DWORD dwForwardMetric1
Definition: ipmib.h:88
struct _IP_ADAPTER_INFO * Next
Definition: iptypes.h:53
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10

Referenced by _tmain().

◆ Usage()

static int Usage ( void  )
static

Definition at line 28 of file route.c.

29 {
31  _T("route usage:\n")
32  _T("route print\n")
33  _T(" prints the route table\n")
34  _T("route add <target> [mask <mask>] <gw> [metric <m>]\n")
35  _T(" adds a route\n")
36  _T("route delete <target> <gw>\n")
37  _T(" deletes a route\n") );
38  return 1;
39 }
#define _ftprintf
Definition: tchar.h:518
#define _T(x)
Definition: vfdio.h:22
FILE * stderr

Referenced by _tmain().