ReactOS 0.4.16-dev-1946-g52006dd
route.c File Reference
#include <stdarg.h>
#include <stdlib.h>
#include <windef.h>
#include <winbase.h>
#include <winreg.h>
#include <stdio.h>
#include <malloc.h>
#include <winsock2.h>
#include <iphlpapi.h>
#include <ws2tcpip.h>
#include <ip2string.h>
#include <conutils.h>
#include "resource.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))
 
#define DELETE_FLAG   0x1
 
#define PERSISTENT_FLAG   0x2
 

Functions

static BOOL MatchWildcard (_In_ PWSTR Text, _In_ PWSTR Pattern)
 
static VOID ParsePersistentRouteValue (_In_ PWSTR RouteValue, _Out_ PWSTR *Destination, _Out_ PWSTR *Netmask, _Out_ PWSTR *Gateway, _Out_ PWSTR *Metric)
 
static BOOL ParseCmdLine (_In_ int argc, _In_ WCHAR **argv, _In_ int start, _Out_ PWSTR *Destination, _Out_ PWSTR *Netmask, _Out_ PWSTR *Gateway, _Out_ PWSTR *Metric)
 
static VOID PrintMacAddress (_In_ PBYTE Mac, _In_ PWSTR Buffer)
 
static DWORD PrintPersistentRoutes (_In_ PWSTR Filter)
 
static VOID FormatIPv4Address (_Out_ PWCHAR pBuffer, _In_ PIP_ADAPTER_UNICAST_ADDRESS UnicastAddress, _In_ DWORD IfIndex)
 
static VOID GetFirstIPv4AddressFromIndex (_Out_ PWSTR pBuffer, _In_ PIP_ADAPTER_ADDRESSES pAdapterAddresses, _In_ DWORD IfIndex)
 
static int CompareAdapters (_In_ const void *elem1, _In_ const void *elem2)
 
static DWORD PrintActiveRoutes (_In_ PWSTR DestinationPattern)
 
static int PrintRoutes (_In_ int argc, _In_ WCHAR **argv, _In_ int start, _In_ int flags)
 
static BOOL ConvertAddCmdLine (_Out_ PMIB_IPFORWARDROW RowToAdd, _In_ int argc, _In_ WCHAR **argv, _In_ int start)
 
static DWORD CreatePersistentIpForwardEntry (_In_ PMIB_IPFORWARDROW RowToAdd)
 
static DWORD DeleteCustomRoutes (VOID)
 
static int AddRoute (_In_ int argc, _In_ WCHAR **argv, _In_ int start, _In_ int flags)
 
static DWORD DeleteActiveRoutes (PWSTR DestinationPattern)
 
static DWORD DeletePersistentRoutes (PWSTR DestinationPattern)
 
static int DeleteRoutes (_In_ int argc, _In_ WCHAR **argv, _In_ int start, _In_ int flags)
 
int wmain (_In_ int argc, _In_ WCHAR **argv)
 

Macro Definition Documentation

◆ _INC_WINDOWS

#define _INC_WINDOWS

Definition at line 22 of file route.c.

◆ DELETE_FLAG

#define DELETE_FLAG   0x1

Definition at line 34 of file route.c.

◆ IN_ADDR_OF

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

Definition at line 32 of file route.c.

◆ IPBUF

#define IPBUF   17

Definition at line 31 of file route.c.

◆ PERSISTENT_FLAG

#define PERSISTENT_FLAG   0x2

Definition at line 35 of file route.c.

◆ WIN32_NO_STATUS

#define WIN32_NO_STATUS

Definition at line 14 of file route.c.

Function Documentation

◆ AddRoute()

static int AddRoute ( _In_ int  argc,
_In_ WCHAR **  argv,
_In_ int  start,
_In_ int  flags 
)
static

Definition at line 651 of file route.c.

656{
657 MIB_IPFORWARDROW RowToAdd = { 0 };
658 DWORD Error;
659
660 if ((argc <= start) || !ConvertAddCmdLine(&RowToAdd, argc, argv, start))
661 return 1;
662
663 if (flags & DELETE_FLAG)
665
668 else
669 Error = CreateIpForwardEntry(&RowToAdd);
670 if (Error != ERROR_SUCCESS)
671 {
673 return 2;
674 }
675
676 return 0;
677}
static int argc
Definition: ServiceArgs.c:12
void ConResPrintf(FILE *fp, UINT nID,...)
Definition: fc.c:33
#define StdErr
Definition: fc.c:15
#define IDS_ROUTE_ADD_ERROR
Definition: resource.h:6
static DWORD DeleteCustomRoutes(VOID)
Definition: route.c:606
#define DELETE_FLAG
Definition: route.c:34
static BOOL ConvertAddCmdLine(_Out_ PMIB_IPFORWARDROW RowToAdd, _In_ int argc, _In_ WCHAR **argv, _In_ int start)
Definition: route.c:514
static DWORD CreatePersistentIpForwardEntry(_In_ PMIB_IPFORWARDROW RowToAdd)
Definition: route.c:566
#define PERSISTENT_FLAG
Definition: route.c:35
BOOL Error
Definition: chkdsk.c:66
#define ERROR_SUCCESS
Definition: deptool.c:10
unsigned long DWORD
Definition: ntddk_ex.h:95
GLuint start
Definition: gl.h:1545
GLbitfield flags
Definition: glext.h:7161
DWORD WINAPI CreateIpForwardEntry(PMIB_IPFORWARDROW pRoute)
#define argv
Definition: mplay32.c:18

Referenced by wmain().

◆ CompareAdapters()

static int CompareAdapters ( _In_ const void elem1,
_In_ const void elem2 
)
static

Definition at line 337 of file route.c.

340{
341 return ((PIP_ADAPTER_ADDRESSES)elem2)->IfIndex - ((PIP_ADAPTER_ADDRESSES)elem1)->IfIndex;
342}

Referenced by PrintActiveRoutes().

◆ ConvertAddCmdLine()

static BOOL ConvertAddCmdLine ( _Out_ PMIB_IPFORWARDROW  RowToAdd,
_In_ int  argc,
_In_ WCHAR **  argv,
_In_ int  start 
)
static

Definition at line 514 of file route.c.

519{
520 int i;
521 char addr[16];
522
523 if (argc > start)
524 {
525 wcstombs(addr, argv[start], 16);
526 RowToAdd->dwForwardDest = inet_addr(addr);
527 }
528 else
529 return FALSE;
530
531 for (i = start + 1; i < argc; i++)
532 {
533 if (!_wcsicmp(argv[i], L"mask"))
534 {
535 i++;
536 if (i >= argc)
537 return FALSE;
538 wcstombs(addr, argv[i], 16);
539 RowToAdd->dwForwardMask = inet_addr(addr);
540 }
541 else if (!_wcsicmp(argv[i], L"metric"))
542 {
543 i++;
544 if (i >= argc)
545 return FALSE;
546 RowToAdd->dwForwardMetric1 = _wtoi(argv[i]);
547 }
548 else
549 {
550 wcstombs(addr, argv[i], 16);
551 RowToAdd->dwForwardNextHop = inet_addr(addr);
552 }
553 }
554
555 /* Restrict metric value to range 1 to 9999 */
556 if (RowToAdd->dwForwardMetric1 == 0)
557 RowToAdd->dwForwardMetric1 = 1;
558 else if (RowToAdd->dwForwardMetric1 > 9999)
559 RowToAdd->dwForwardMetric1 = 9999;
560
561 return TRUE;
562}
ULONG WSAAPI inet_addr(IN CONST CHAR FAR *cp)
Definition: addrconv.c:71
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define L(x)
Definition: resources.c:13
GLenum const GLvoid * addr
Definition: glext.h:9621
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
_Check_return_ _CRTIMP int __cdecl _wtoi(_In_z_ const wchar_t *_Str)
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
wcstombs
Definition: stdlib.h:1013

Referenced by AddRoute().

◆ CreatePersistentIpForwardEntry()

static DWORD CreatePersistentIpForwardEntry ( _In_ PMIB_IPFORWARDROW  RowToAdd)
static

Definition at line 566 of file route.c.

568{
569 WCHAR Destination[IPBUF], Gateway[IPBUF], Netmask[IPBUF];
570 WCHAR ValueName[64];
571 HKEY hKey;
573
574 mbstowcs(Destination, inet_ntoa(IN_ADDR_OF(RowToAdd->dwForwardDest)), IPBUF);
575 mbstowcs(Netmask, inet_ntoa(IN_ADDR_OF(RowToAdd->dwForwardMask)), IPBUF);
576 mbstowcs(Gateway, inet_ntoa(IN_ADDR_OF(RowToAdd->dwForwardNextHop)), IPBUF);
577
578 swprintf(ValueName, L"%s,%s,%s,%ld", Destination, Netmask, Gateway, RowToAdd->dwForwardMetric1);
579
581 L"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\PersistentRoutes",
582 0,
583 NULL,
585 KEY_WRITE,
586 NULL,
587 &hKey,
588 NULL);
589 if (Error == ERROR_SUCCESS)
590 {
592 ValueName,
593 0,
594 REG_SZ,
595 (LPBYTE)L"",
596 sizeof(WCHAR));
597
599 }
600
601 return Error;
602}
CHAR FAR *WSAAPI inet_ntoa(IN IN_ADDR in)
Definition: addrconv.c:160
#define IPBUF
Definition: route.c:31
#define IN_ADDR_OF(x)
Definition: route.c:32
#define RegCloseKey(hKey)
Definition: registry.h:49
#define NULL
Definition: types.h:112
LONG WINAPI RegCreateKeyExW(_In_ HKEY hKey, _In_ LPCWSTR lpSubKey, _In_ DWORD Reserved, _In_opt_ LPWSTR lpClass, _In_ DWORD dwOptions, _In_ REGSAM samDesired, _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes, _Out_ PHKEY phkResult, _Out_opt_ LPDWORD lpdwDisposition)
Definition: reg.c:1096
#define swprintf
Definition: precomp.h:40
FxAutoRegKey hKey
#define REG_SZ
Definition: layer.c:22
_In_ PUNICODE_STRING _Inout_ PUNICODE_STRING Destination
Definition: rtlfuncs.h:3051
#define REG_OPTION_NON_VOLATILE
Definition: nt_native.h:1060
#define KEY_WRITE
Definition: nt_native.h:1034
mbstowcs
Definition: stdlib.h:925
unsigned char * LPBYTE
Definition: typedefs.h:53
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING ValueName
Definition: wdfregistry.h:243
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
#define RegSetValueEx
Definition: winreg.h:540
__wchar_t WCHAR
Definition: xmlstorage.h:180

Referenced by AddRoute().

◆ DeleteActiveRoutes()

static DWORD DeleteActiveRoutes ( PWSTR  DestinationPattern)
static

Definition at line 681 of file route.c.

683{
684 WCHAR Destination[IPBUF], Netmask[IPBUF];
685 PMIB_IPFORWARDTABLE IpForwardTable = NULL;
686 ULONG Size = 0;
688 ULONG i;
689
691 {
692 if (!(IpForwardTable = malloc(Size)))
693 {
695 goto done;
696 }
697 }
698
699 Error = GetIpForwardTable(IpForwardTable, &Size, TRUE);
700 if (Error != ERROR_SUCCESS)
701 goto done;
702
703 for (i = 0; i < IpForwardTable->dwNumEntries; i++)
704 {
706 mbstowcs(Netmask, inet_ntoa(IN_ADDR_OF(IpForwardTable->table[i].dwForwardMask)), IPBUF);
707
708 if (MatchWildcard(Destination, DestinationPattern))
709 {
710 Error = DeleteIpForwardEntry(&IpForwardTable->table[i]);
711 if (Error != ERROR_SUCCESS)
712 break;
713 }
714 }
715
716done:
717 if (IpForwardTable)
718 free(IpForwardTable);
719
720 return Error;
721}
static BOOL MatchWildcard(_In_ PWSTR Text, _In_ PWSTR Pattern)
Definition: route.c:39
#define ERROR_NOT_ENOUGH_MEMORY
Definition: dderror.h:7
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
DWORD WINAPI GetIpForwardTable(PMIB_IPFORWARDTABLE pIpForwardTable, PULONG pdwSize, BOOL bOrder)
DWORD WINAPI DeleteIpForwardEntry(PMIB_IPFORWARDROW pRoute)
DWORD dwForwardDest
Definition: ipmib.h:71
DWORD dwForwardMask
Definition: ipmib.h:72
DWORD dwNumEntries
Definition: ipmib.h:97
MIB_IPFORWARDROW table[1]
Definition: ipmib.h:98
uint32_t ULONG
Definition: typedefs.h:59
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4539

Referenced by DeleteRoutes().

◆ DeleteCustomRoutes()

static DWORD DeleteCustomRoutes ( VOID  )
static

Definition at line 606 of file route.c.

607{
608 WCHAR Destination[IPBUF], Netmask[IPBUF];
609 PMIB_IPFORWARDTABLE IpForwardTable = NULL;
610 ULONG Size = 0;
612 ULONG i;
613
615 {
616 if (!(IpForwardTable = malloc(Size)))
617 {
619 goto done;
620 }
621 }
622
623 Error = GetIpForwardTable(IpForwardTable, &Size, TRUE);
624 if (Error != ERROR_SUCCESS)
625 goto done;
626
627 for (i = 0; i < IpForwardTable->dwNumEntries; i++)
628 {
630 mbstowcs(Netmask, inet_ntoa(IN_ADDR_OF(IpForwardTable->table[i].dwForwardMask)), IPBUF);
631
632 if ((wcscmp(Netmask, L"255.255.255.255") != 0) &&
633 ((wcscmp(Destination, L"127.0.0.0") != 0) || (wcscmp(Netmask, L"255.0.0.0") != 0)) &&
634 ((wcscmp(Destination, L"224.0.0.0") != 0) || (wcscmp(Netmask, L"240.0.0.0") != 0)))
635 {
636 Error = DeleteIpForwardEntry(&IpForwardTable->table[i]);
637 if (Error != ERROR_SUCCESS)
638 break;
639 }
640 }
641
642done:
643 if (IpForwardTable)
644 free(IpForwardTable);
645
646 return Error;
647}
_Check_return_ _CRTIMP int __cdecl wcscmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)

Referenced by AddRoute(), and DeleteRoutes().

◆ DeletePersistentRoutes()

static DWORD DeletePersistentRoutes ( PWSTR  DestinationPattern)
static

Definition at line 725 of file route.c.

727{
728
729 WCHAR szBuffer[64];
730 DWORD dwIndex = 0, dwBufferSize;
731 HKEY hKey;
734
736 L"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\PersistentRoutes",
737 0,
738 KEY_READ,
739 &hKey);
740 if (Error == ERROR_SUCCESS)
741 {
742 for (;;)
743 {
744 dwBufferSize = 64;
745
747 dwIndex,
748 szBuffer,
749 &dwBufferSize,
750 NULL,
751 NULL,
752 NULL,
753 NULL);
754 if (Error != ERROR_SUCCESS)
755 {
758
759 break;
760 }
761
764 NULL,
765 NULL,
766 NULL);
767
768 if (MatchWildcard(Destination, DestinationPattern))
769 {
770 Error = RegDeleteValueW(hKey, szBuffer);
771 if (Error != ERROR_SUCCESS)
772 break;
773 }
774
775 dwIndex++;
776 }
777
779 }
780
781 return Error;
782}
static VOID ParsePersistentRouteValue(_In_ PWSTR RouteValue, _Out_ PWSTR *Destination, _Out_ PWSTR *Netmask, _Out_ PWSTR *Gateway, _Out_ PWSTR *Metric)
Definition: route.c:91
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3333
LONG WINAPI RegDeleteValueW(HKEY hKey, LPCWSTR lpValueName)
Definition: reg.c:2330
LONG WINAPI RegEnumValueW(_In_ HKEY hKey, _In_ DWORD index, _Out_ LPWSTR value, _Inout_ PDWORD val_count, _Reserved_ PDWORD reserved, _Out_opt_ PDWORD type, _Out_opt_ LPBYTE data, _Inout_opt_ PDWORD count)
Definition: reg.c:2830
#define ERROR_NO_MORE_ITEMS
Definition: compat.h:105
#define KEY_READ
Definition: nt_native.h:1026
uint16_t * PWSTR
Definition: typedefs.h:56

Referenced by DeleteRoutes().

◆ DeleteRoutes()

static int DeleteRoutes ( _In_ int  argc,
_In_ WCHAR **  argv,
_In_ int  start,
_In_ int  flags 
)
static

Definition at line 785 of file route.c.

790{
791 PWSTR DestinationPattern = NULL;
792 DWORD Error;
793
794 if ((argc <= start) ||
796 &DestinationPattern, NULL, NULL, NULL))
797 return 1;
798
799 if (flags & DELETE_FLAG)
801
802 Error = DeleteActiveRoutes(DestinationPattern);
803 if (Error == ERROR_SUCCESS)
804 Error = DeletePersistentRoutes(DestinationPattern);
805
806 if (Error != ERROR_SUCCESS)
807 {
809 return 2;
810 }
811
812 return 0;
813}
#define IDS_ROUTE_DEL_ERROR
Definition: resource.h:7
static BOOL ParseCmdLine(_In_ int argc, _In_ WCHAR **argv, _In_ int start, _Out_ PWSTR *Destination, _Out_ PWSTR *Netmask, _Out_ PWSTR *Gateway, _Out_ PWSTR *Metric)
Definition: route.c:151
static DWORD DeleteActiveRoutes(PWSTR DestinationPattern)
Definition: route.c:681
static DWORD DeletePersistentRoutes(PWSTR DestinationPattern)
Definition: route.c:725

Referenced by wmain().

◆ FormatIPv4Address()

static VOID FormatIPv4Address ( _Out_ PWCHAR  pBuffer,
_In_ PIP_ADAPTER_UNICAST_ADDRESS  UnicastAddress,
_In_ DWORD  IfIndex 
)
static

Definition at line 290 of file route.c.

294{
295 PIP_ADAPTER_UNICAST_ADDRESS Ptr = UnicastAddress;
296
297 while (Ptr)
298 {
299 if (Ptr->Address.lpSockaddr->sa_family == AF_INET)
300 {
301 struct sockaddr_in *si = (struct sockaddr_in *)(Ptr->Address.lpSockaddr);
303 return;
304 }
305
306 Ptr = Ptr->Next;
307 }
308
309 swprintf(pBuffer, L"%lx", IfIndex);
310}
#define AF_INET
Definition: tcpip.h:117
_Must_inspect_result_ _In_ PFSRTL_PER_STREAM_CONTEXT Ptr
Definition: fsrtlfuncs.h:898
NTSYSAPI PWSTR NTAPI RtlIpv4AddressToStringW(_In_ const struct in_addr *Addr, _Out_writes_(16) PWCHAR S)
PVOID pBuffer
struct in_addr sin_addr
Definition: winsock.h:506

Referenced by GetFirstIPv4AddressFromIndex().

◆ GetFirstIPv4AddressFromIndex()

static VOID GetFirstIPv4AddressFromIndex ( _Out_ PWSTR  pBuffer,
_In_ PIP_ADAPTER_ADDRESSES  pAdapterAddresses,
_In_ DWORD  IfIndex 
)
static

Definition at line 314 of file route.c.

318{
319 PIP_ADAPTER_ADDRESSES Ptr = pAdapterAddresses;
320
321 while (Ptr)
322 {
323 if (Ptr->IfIndex == IfIndex)
324 {
325 FormatIPv4Address(pBuffer, Ptr->FirstUnicastAddress, Ptr->IfIndex);
326 return;
327 }
328
329 Ptr = Ptr->Next;
330 }
331
332 swprintf(pBuffer, L"%lx", IfIndex);
333}
static VOID FormatIPv4Address(_Out_ PWCHAR pBuffer, _In_ PIP_ADAPTER_UNICAST_ADDRESS UnicastAddress, _In_ DWORD IfIndex)
Definition: route.c:290

Referenced by PrintActiveRoutes().

◆ MatchWildcard()

static BOOL MatchWildcard ( _In_ PWSTR  Text,
_In_ PWSTR  Pattern 
)
static

Definition at line 39 of file route.c.

42{
43 size_t TextLength, PatternLength, TextIndex = 0, PatternIndex = 0;
44 size_t StartIndex = -1, MatchIndex = 0;
45
46 if (Pattern == NULL)
47 return TRUE;
48
49 TextLength = wcslen(Text);
50 PatternLength = wcslen(Pattern);
51
52 while (TextIndex < TextLength)
53 {
54 if ((PatternIndex < PatternLength) &&
55 ((Pattern[PatternIndex] == L'?') ||
56 (towlower(Pattern[PatternIndex]) == towlower(Text[TextIndex]))))
57 {
58 TextIndex++;
59 PatternIndex++;
60 }
61 else if ((PatternIndex < PatternLength) &&
62 (Pattern[PatternIndex] == L'*'))
63 {
64 StartIndex = PatternIndex;
65 MatchIndex = TextIndex;
66 PatternIndex++;
67 }
68 else if (StartIndex != -1)
69 {
70 PatternIndex = StartIndex + 1;
71 MatchIndex++;
72 TextIndex = MatchIndex;
73 }
74 else
75 {
76 return FALSE;
77 }
78 }
79
80 while ((PatternIndex < PatternLength) &&
81 (Pattern[PatternIndex] == L'*'))
82 {
83 PatternIndex++;
84 }
85
86 return (PatternIndex == PatternLength);
87}
char * Text
Definition: combotst.c:136
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define towlower(c)
Definition: wctype.h:97

Referenced by DeleteActiveRoutes(), DeletePersistentRoutes(), PrintActiveRoutes(), and PrintPersistentRoutes().

◆ ParseCmdLine()

static BOOL ParseCmdLine ( _In_ int  argc,
_In_ WCHAR **  argv,
_In_ int  start,
_Out_ PWSTR Destination,
_Out_ PWSTR Netmask,
_Out_ PWSTR Gateway,
_Out_ PWSTR Metric 
)
static

Definition at line 151 of file route.c.

159{
160 int i;
161
162 if (Destination)
163 *Destination = NULL;
164
165 if (Netmask)
166 *Netmask = NULL;
167
168 if (Gateway)
169 *Gateway = NULL;
170
171 if (Metric)
172 *Metric = NULL;
173
174 if (argc > start)
175 {
176 if (Destination)
178 }
179 else
180 return FALSE;
181
182 for (i = start + 1; i < argc; i++)
183 {
184 if (!_wcsicmp(argv[i], L"mask"))
185 {
186 i++;
187 if (i >= argc)
188 return FALSE;
189 if (Netmask)
190 *Netmask = argv[i];
191 }
192 else if (!_wcsicmp(argv[i], L"metric"))
193 {
194 i++;
195 if (i >= argc)
196 return FALSE;
197 if (Metric)
198 *Metric = argv[i];
199 }
200 else
201 {
202 if (Gateway)
203 *Gateway = argv[i];
204 }
205 }
206
207 return TRUE;
208}

Referenced by DeleteRoutes(), and PrintRoutes().

◆ ParsePersistentRouteValue()

static VOID ParsePersistentRouteValue ( _In_ PWSTR  RouteValue,
_Out_ PWSTR Destination,
_Out_ PWSTR Netmask,
_Out_ PWSTR Gateway,
_Out_ PWSTR Metric 
)
static

Definition at line 91 of file route.c.

97{
98 PWSTR Ptr, DestPtr;
99
100 if (Destination)
101 *Destination = NULL;
102
103 if (Netmask)
104 *Netmask = NULL;
105
106 if (Gateway)
107 *Gateway = NULL;
108
109 if (Metric)
110 *Metric = NULL;
111
112 DestPtr = RouteValue;
113 if (Destination)
114 *Destination = DestPtr;
115
116 Ptr = wcschr(DestPtr, L',');
117 if (Ptr == NULL)
118 return;
119
120 *Ptr = L'\0';
121 Ptr++;
122
123 DestPtr = Ptr;
124 if (Netmask)
125 *Netmask = DestPtr;
126
127 Ptr = wcschr(DestPtr, L',');
128 if (Ptr == NULL)
129 return;
130
131 *Ptr = L'\0';
132 Ptr++;
133
134 DestPtr = Ptr;
135 if (Gateway)
136 *Gateway = DestPtr;
137
138 Ptr = wcschr(DestPtr, L',');
139 if (Ptr == NULL)
140 return;
141
142 *Ptr = L'\0';
143 Ptr++;
144
145 if (Metric)
146 *Metric = Ptr;
147}
#define wcschr
Definition: compat.h:17

Referenced by DeletePersistentRoutes(), and PrintPersistentRoutes().

◆ PrintActiveRoutes()

static DWORD PrintActiveRoutes ( _In_ PWSTR  DestinationPattern)
static

Definition at line 346 of file route.c.

348{
349 PMIB_IPFORWARDTABLE IpForwardTable = NULL;
350 PIP_ADAPTER_ADDRESSES pAdapterAddresses = NULL, Ptr, *pAdapterSortArray = NULL;
351 ULONG Size = 0;
353 ULONG adaptOutBufLen = 15000;
354 WCHAR Destination[IPBUF], Gateway[IPBUF], Netmask[IPBUF], Interface[IPBUF];
355 unsigned int i, AdapterCount, AdapterIndex;
356 BOOL EntriesFound;
357 ULONG Flags = /*GAA_FLAG_SKIP_UNICAST |*/ GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST |
358 GAA_FLAG_SKIP_DNS_SERVER;
359
360 /* set required buffer size */
361 pAdapterAddresses = (PIP_ADAPTER_ADDRESSES)malloc(adaptOutBufLen);
362 if (pAdapterAddresses == NULL)
363 {
365 goto Error;
366 }
367
368 if (GetAdaptersAddresses(AF_INET, Flags, NULL, pAdapterAddresses, &adaptOutBufLen) == ERROR_BUFFER_OVERFLOW)
369 {
370 free(pAdapterAddresses);
371 pAdapterAddresses = (PIP_ADAPTER_ADDRESSES)malloc(adaptOutBufLen);
372 if (pAdapterAddresses == NULL)
373 {
375 goto Error;
376 }
377 }
378
380 {
381 if (!(IpForwardTable = malloc(Size)))
382 {
384 goto Error;
385 }
386 }
387
388 if (((Error = GetAdaptersAddresses(AF_INET, Flags, NULL, pAdapterAddresses, &adaptOutBufLen)) == NO_ERROR) &&
389 ((Error = GetIpForwardTable(IpForwardTable, &Size, TRUE)) == NO_ERROR))
390 {
393
394 AdapterCount = 0;
395 Ptr = pAdapterAddresses;
396 while (Ptr)
397 {
398 AdapterCount++;
399 Ptr = Ptr->Next;
400 }
401
402 pAdapterSortArray = (PIP_ADAPTER_ADDRESSES*)malloc(AdapterCount * sizeof(PVOID));
403 if (pAdapterSortArray == NULL)
404 {
406 goto Error;
407 }
408
409 AdapterIndex = 0;
410 Ptr = pAdapterAddresses;
411 while (Ptr)
412 {
413 pAdapterSortArray[AdapterIndex] = Ptr;
414 AdapterIndex++;
415 Ptr = Ptr->Next;
416 }
417
418 qsort(pAdapterSortArray, AdapterCount, sizeof(PVOID), CompareAdapters);
419
420 for (AdapterIndex = 0; AdapterIndex < AdapterCount; AdapterIndex++)
421 {
422 Ptr = pAdapterSortArray[AdapterIndex];
423 if (Ptr->IfType == IF_TYPE_ETHERNET_CSMACD)
424 {
426 PrintMacAddress(Ptr->PhysicalAddress, PhysicalAddress);
427 ConResPrintf(StdOut, IDS_ETHERNET_ENTRY, Ptr->IfIndex, PhysicalAddress, Ptr->Description);
428 }
429 else
430 {
431 ConResPrintf(StdOut, IDS_INTERFACE_ENTRY, Ptr->IfIndex, Ptr->Description);
432 }
433 }
435
439 EntriesFound = FALSE;
440 for (i = 0; i < IpForwardTable->dwNumEntries; i++)
441 {
443 mbstowcs(Netmask, inet_ntoa(IN_ADDR_OF(IpForwardTable->table[i].dwForwardMask)), IPBUF);
444 mbstowcs(Gateway, inet_ntoa(IN_ADDR_OF(IpForwardTable->table[i].dwForwardNextHop)), IPBUF);
445 GetFirstIPv4AddressFromIndex(Interface, pAdapterAddresses, IpForwardTable->table[i].dwForwardIfIndex);
446
447 if (MatchWildcard(Destination, DestinationPattern))
448 {
449 if (EntriesFound == FALSE)
453 Netmask,
454 Gateway,
455 Interface,
456 IpForwardTable->table[i].dwForwardMetric1);
457 EntriesFound = TRUE;
458 }
459 }
460
461 if (DestinationPattern == NULL)
462 {
463 for (i = 0; i < IpForwardTable->dwNumEntries; i++)
464 {
465 if (IpForwardTable->table[i].dwForwardDest == 0)
466 {
467 mbstowcs(Gateway, inet_ntoa(IN_ADDR_OF(IpForwardTable->table[i].dwForwardNextHop)), IPBUF);
469 }
470 }
471 }
472 else if (EntriesFound == FALSE)
474 }
475
476Error:
477 if (pAdapterSortArray)
478 free(pAdapterSortArray);
479 if (pAdapterAddresses)
480 free(pAdapterAddresses);
481 if (IpForwardTable)
482 free(IpForwardTable);
483
484 if (Error != ERROR_SUCCESS)
486
487 return Error;
488}
#define StdOut
Definition: fc.c:14
#define IDS_NONE
Definition: resource.h:144
#define IDS_INTERFACE_LIST
Definition: resource.h:10
#define IDS_INTERFACE_ENTRY
Definition: resource.h:11
#define IDS_ETHERNET_ENTRY
Definition: resource.h:12
#define IDS_IPV4_ROUTE_TABLE
Definition: resource.h:13
#define IDS_ACTIVE_ROUTES
Definition: resource.h:14
#define IDS_ROUTES_ENTRY
Definition: resource.h:17
#define IDS_SEPARATOR
Definition: resource.h:9
#define IDS_DEFAULT_GATEWAY
Definition: resource.h:18
#define IDS_ROUTES_HEADER
Definition: resource.h:19
#define IDS_ROUTE_ENUM_ERROR
Definition: resource.h:8
static VOID PrintMacAddress(_In_ PBYTE Mac, _In_ PWSTR Buffer)
Definition: route.c:212
static int CompareAdapters(_In_ const void *elem1, _In_ const void *elem2)
Definition: route.c:337
static VOID GetFirstIPv4AddressFromIndex(_Out_ PWSTR pBuffer, _In_ PIP_ADAPTER_ADDRESSES pAdapterAddresses, _In_ DWORD IfIndex)
Definition: route.c:314
void __cdecl qsort(_Inout_updates_bytes_(_NumOfElements *_SizeOfElements) void *_Base, _In_ size_t _NumOfElements, _In_ size_t _SizeOfElements, _In_ int(__cdecl *_PtFuncCompare)(const void *, const void *))
#define NO_ERROR
Definition: dderror.h:5
unsigned int BOOL
Definition: ntddk_ex.h:94
#define IF_TYPE_ETHERNET_CSMACD
Definition: ipifcons.h:26
DWORD dwForwardNextHop
Definition: ipmib.h:74
DWORD dwForwardMetric1
Definition: ipmib.h:88
IF_INDEX dwForwardIfIndex
Definition: ipmib.h:75
_Must_inspect_result_ _In_ WDFDEVICE _In_ LPCGUID _Out_ PINTERFACE Interface
Definition: wdffdo.h:465
#define ERROR_BUFFER_OVERFLOW
Definition: winerror.h:307
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
_Must_inspect_result_ typedef _In_ PHYSICAL_ADDRESS PhysicalAddress
Definition: iotypes.h:1098

Referenced by PrintRoutes().

◆ PrintMacAddress()

static VOID PrintMacAddress ( _In_ PBYTE  Mac,
_In_ PWSTR  Buffer 
)
static

Definition at line 212 of file route.c.

215{
216 swprintf(Buffer, L"%02X %02X %02X %02X %02X %02X ",
217 Mac[0], Mac[1], Mac[2], Mac[3], Mac[4], Mac[5]);
218}
Definition: bufpool.h:45

Referenced by PrintActiveRoutes().

◆ PrintPersistentRoutes()

static DWORD PrintPersistentRoutes ( _In_ PWSTR  Filter)
static

Definition at line 222 of file route.c.

224{
225 WCHAR szBuffer[64];
226 DWORD dwIndex = 0, dwBufferSize;
227 BOOL EntriesFound = FALSE;
228 HKEY hKey;
230 PWSTR Destination, Netmask, Gateway, Metric;
231
234
236 L"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\PersistentRoutes",
237 0,
238 KEY_READ,
239 &hKey);
240 if (Error == ERROR_SUCCESS)
241 {
242 for (;;)
243 {
244 dwBufferSize = 64;
245
247 dwIndex,
248 szBuffer,
249 &dwBufferSize,
250 NULL,
251 NULL,
252 NULL,
253 NULL);
254 if (Error != ERROR_SUCCESS)
255 {
258
259 break;
260 }
261
264 &Netmask,
265 &Gateway,
266 &Metric);
267
269 {
270 if (EntriesFound == FALSE)
272 ConResPrintf(StdOut, IDS_PERSISTENT_ENTRY, Destination, Netmask, Gateway, Metric);
273 EntriesFound = TRUE;
274 }
275
276 dwIndex++;
277 }
278
280 }
281
282 if (EntriesFound == FALSE)
284
285 return Error;
286}
#define IDS_PERSISTENT_ROUTES
Definition: resource.h:15
#define IDS_PERSISTENT_HEADER
Definition: resource.h:20
#define IDS_PERSISTENT_ENTRY
Definition: resource.h:16
_Must_inspect_result_ _In_opt_ PFLT_FILTER Filter
Definition: fltkernel.h:1801

Referenced by PrintRoutes().

◆ PrintRoutes()

static int PrintRoutes ( _In_ int  argc,
_In_ WCHAR **  argv,
_In_ int  start,
_In_ int  flags 
)
static

Definition at line 492 of file route.c.

497{
498 PWSTR DestinationPattern = NULL;
499 DWORD Error;
500
501 if (argc > start)
503 &DestinationPattern, NULL, NULL, NULL);
504
505 Error = PrintActiveRoutes(DestinationPattern);
506 if (Error == ERROR_SUCCESS)
507 Error = PrintPersistentRoutes(DestinationPattern);
508
509 return (Error == ERROR_SUCCESS) ? 0 : 2;
510}
static DWORD PrintActiveRoutes(_In_ PWSTR DestinationPattern)
Definition: route.c:346
static DWORD PrintPersistentRoutes(_In_ PWSTR Filter)
Definition: route.c:222

Referenced by wmain().

◆ wmain()

int wmain ( _In_ int  argc,
_In_ WCHAR **  argv 
)

Definition at line 816 of file route.c.

819{
820 int i, flags = 0, ret = 0;
821
822 /* Initialize the Console Standard Streams */
824
825 for (i = 1; i < argc; i++)
826 {
827 if (argv[i][0] == L'-' || argv[i][0] == L'/')
828 {
829 if (!_wcsicmp(&argv[i][1], L"f"))
830 {
832 }
833 else if (!_wcsicmp(&argv[i][1], L"p"))
834 {
836 }
837 else
838 {
839 ret = 1;
840 break;
841 }
842 }
843 else
844 {
845 if (!_wcsicmp(argv[i], L"print"))
846 ret = PrintRoutes(argc, argv, i + 1, flags);
847 else if (!_wcsicmp(argv[i], L"add"))
848 ret = AddRoute(argc, argv, i + 1, flags);
849 else if (!_wcsicmp(argv[i], L"delete"))
850 ret = DeleteRoutes(argc, argv, i + 1, flags);
851 else
852 ret = 1;
853
854 break;
855 }
856 }
857
858 if (argc == 1)
859 ret = 1;
860
861 if (ret == 1)
862 {
864// ConResPrintf(StdErr, IDS_USAGE2);
866 }
867
868 return (ret == 0) ? 0 : 1;
869}
#define ConInitStdStreams()
Definition: fc.c:13
#define IDS_USAGE3
Definition: resource.h:5
#define IDS_USAGE1
Definition: resource.h:3
static int AddRoute(_In_ int argc, _In_ WCHAR **argv, _In_ int start, _In_ int flags)
Definition: route.c:651
static int PrintRoutes(_In_ int argc, _In_ WCHAR **argv, _In_ int start, _In_ int flags)
Definition: route.c:492
static int DeleteRoutes(_In_ int argc, _In_ WCHAR **argv, _In_ int start, _In_ int flags)
Definition: route.c:785
return ret
Definition: mutex.c:146