ReactOS 0.4.16-dev-2574-g474348f
route.c File Reference
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <windef.h>
#include <winbase.h>
#include <winreg.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 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

◆ 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.

Function Documentation

◆ AddRoute()

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

Definition at line 654 of file route.c.

659{
660 MIB_IPFORWARDROW RowToAdd = { 0 };
661 DWORD Error;
662
663 if ((argc <= start) || !ConvertAddCmdLine(&RowToAdd, argc, argv, start))
664 return 1;
665
666 if (flags & DELETE_FLAG)
668
671 else
672 Error = CreateIpForwardEntry(&RowToAdd);
673 if (Error != ERROR_SUCCESS)
674 {
676 return 2;
677 }
678
679 return 0;
680}
#define IDS_ROUTE_ADD_ERROR
Definition: resource.h:6
static DWORD DeleteCustomRoutes(VOID)
Definition: route.c:609
#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:517
static DWORD CreatePersistentIpForwardEntry(_In_ PMIB_IPFORWARDROW RowToAdd)
Definition: route.c:569
#define PERSISTENT_FLAG
Definition: route.c:35
BOOL Error
Definition: chkdsk.c:66
void ConResPrintf(FILE *fp, UINT nID,...)
#define StdErr
Definition: conutils_noros.h:7
#define ERROR_SUCCESS
Definition: deptool.c:10
MonoAssembly int argc
Definition: metahost.c:107
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 340 of file route.c.

343{
344 return ((PIP_ADAPTER_ADDRESSES)elem2)->IfIndex - ((PIP_ADAPTER_ADDRESSES)elem1)->IfIndex;
345}

Referenced by PrintActiveRoutes().

◆ ConvertAddCmdLine()

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

Definition at line 517 of file route.c.

522{
523 int i;
524 char addr[16];
525
526 if (argc > start)
527 {
528 wcstombs(addr, argv[start], 16);
529 RowToAdd->dwForwardDest = inet_addr(addr);
530 }
531 else
532 return FALSE;
533
534 for (i = start + 1; i < argc; i++)
535 {
536 if (!_wcsicmp(argv[i], L"mask"))
537 {
538 i++;
539 if (i >= argc)
540 return FALSE;
541 wcstombs(addr, argv[i], 16);
542 RowToAdd->dwForwardMask = inet_addr(addr);
543 }
544 else if (!_wcsicmp(argv[i], L"metric"))
545 {
546 i++;
547 if (i >= argc)
548 return FALSE;
549 RowToAdd->dwForwardMetric1 = _wtoi(argv[i]);
550 }
551 else
552 {
553 wcstombs(addr, argv[i], 16);
554 RowToAdd->dwForwardNextHop = inet_addr(addr);
555 }
556 }
557
558 /* Restrict metric value to range 1 to 9999 */
559 if (RowToAdd->dwForwardMetric1 == 0)
560 RowToAdd->dwForwardMetric1 = 1;
561 else if (RowToAdd->dwForwardMetric1 > 9999)
562 RowToAdd->dwForwardMetric1 = 9999;
563
564 return TRUE;
565}
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
_ACRTIMP int __cdecl _wtoi(const wchar_t *)
Definition: wcs.c:2773
_ACRTIMP int __cdecl _wcsicmp(const wchar_t *, const wchar_t *)
Definition: wcs.c:159
#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
wcstombs
Definition: stdlib.h:1013

Referenced by AddRoute().

◆ CreatePersistentIpForwardEntry()

static DWORD CreatePersistentIpForwardEntry ( _In_ PMIB_IPFORWARDROW  RowToAdd)
static

Definition at line 569 of file route.c.

571{
572 WCHAR Destination[IPBUF], Gateway[IPBUF], Netmask[IPBUF];
573 WCHAR ValueName[64];
574 HKEY hKey;
576
577 mbstowcs(Destination, inet_ntoa(IN_ADDR_OF(RowToAdd->dwForwardDest)), IPBUF);
578 mbstowcs(Netmask, inet_ntoa(IN_ADDR_OF(RowToAdd->dwForwardMask)), IPBUF);
579 mbstowcs(Gateway, inet_ntoa(IN_ADDR_OF(RowToAdd->dwForwardNextHop)), IPBUF);
580
581 swprintf(ValueName, L"%s,%s,%s,%ld", Destination, Netmask, Gateway, RowToAdd->dwForwardMetric1);
582
584 L"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\PersistentRoutes",
585 0,
586 NULL,
588 KEY_WRITE,
589 NULL,
590 &hKey,
591 NULL);
592 if (Error == ERROR_SUCCESS)
593 {
595 ValueName,
596 0,
597 REG_SZ,
598 (LPBYTE)L"",
599 sizeof(WCHAR));
600
602 }
603
604 return Error;
605}
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
short WCHAR
Definition: pedump.c:58
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:565

Referenced by AddRoute().

◆ DeleteActiveRoutes()

static DWORD DeleteActiveRoutes ( PWSTR  DestinationPattern)
static

Definition at line 684 of file route.c.

686{
687 WCHAR Destination[IPBUF], Netmask[IPBUF];
688 PMIB_IPFORWARDTABLE IpForwardTable = NULL;
689 ULONG Size = 0;
691 ULONG i;
692
694 {
695 if (!(IpForwardTable = malloc(Size)))
696 {
698 goto done;
699 }
700 }
701
702 Error = GetIpForwardTable(IpForwardTable, &Size, TRUE);
703 if (Error != ERROR_SUCCESS)
704 goto done;
705
706 for (i = 0; i < IpForwardTable->dwNumEntries; i++)
707 {
709 mbstowcs(Netmask, inet_ntoa(IN_ADDR_OF(IpForwardTable->table[i].dwForwardMask)), IPBUF);
710
711 if (MatchWildcard(Destination, DestinationPattern))
712 {
713 Error = DeleteIpForwardEntry(&IpForwardTable->table[i]);
714 if (Error != ERROR_SUCCESS)
715 break;
716 }
717 }
718
719done:
720 if (IpForwardTable)
721 free(IpForwardTable);
722
723 return Error;
724}
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 609 of file route.c.

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

Referenced by AddRoute(), and DeleteRoutes().

◆ DeletePersistentRoutes()

static DWORD DeletePersistentRoutes ( PWSTR  DestinationPattern)
static

Definition at line 728 of file route.c.

730{
731
732 WCHAR szBuffer[64];
733 DWORD dwIndex = 0, dwBufferSize;
734 HKEY hKey;
737
739 L"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\PersistentRoutes",
740 0,
741 KEY_READ,
742 &hKey);
743 if (Error == ERROR_SUCCESS)
744 {
745 for (;;)
746 {
747 dwBufferSize = 64;
748
750 dwIndex,
751 szBuffer,
752 &dwBufferSize,
753 NULL,
754 NULL,
755 NULL,
756 NULL);
757 if (Error != ERROR_SUCCESS)
758 {
761
762 break;
763 }
764
767 NULL,
768 NULL,
769 NULL);
770
771 if (MatchWildcard(Destination, DestinationPattern))
772 {
773 Error = RegDeleteValueW(hKey, szBuffer);
774 if (Error != ERROR_SUCCESS)
775 break;
776 }
777
778 dwIndex++;
779 }
780
782 }
783
784 return Error;
785}
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 788 of file route.c.

793{
794 PWSTR DestinationPattern = NULL;
795 DWORD Error;
796
797 if ((argc <= start) ||
799 &DestinationPattern, NULL, NULL, NULL))
800 return 1;
801
802 if (flags & DELETE_FLAG)
804
805 Error = DeleteActiveRoutes(DestinationPattern);
806 if (Error == ERROR_SUCCESS)
807 Error = DeletePersistentRoutes(DestinationPattern);
808
809 if (Error != ERROR_SUCCESS)
810 {
812 return 2;
813 }
814
815 return 0;
816}
#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:684
static DWORD DeletePersistentRoutes(PWSTR DestinationPattern)
Definition: route.c:728

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);
302 // DWORD dwStrLen = IPV4_ADDR_STRING_MAX_LEN;
303 // WSAAddressToStringW(Ptr->Address.lpSockaddr, (DWORD)Ptr->Address.iSockaddrLength, NULL, pBuffer, &dwStrLen);
304 // (void)InetNtopW(AF_INET, &(si->sin_addr), pBuffer, IPV4_ADDR_STRING_MAX_LEN);
305 RtlIpv4AddressToStringW(&(si->sin_addr), pBuffer);
306 return;
307 }
308
309 Ptr = Ptr->Next;
310 }
311
312 swprintf(pBuffer, L"%lx", IfIndex);
313}
#define AF_INET
Definition: tcpip.h:117
_Must_inspect_result_ _In_ PFSRTL_PER_STREAM_CONTEXT Ptr
Definition: fsrtlfuncs.h:898
static SYSTEM_INFO si
Definition: virtual.c:39
NTSYSAPI PWSTR NTAPI RtlIpv4AddressToStringW(_In_ const struct in_addr *Addr, _Out_writes_(16) PWCHAR S)
PVOID pBuffer

Referenced by GetFirstIPv4AddressFromIndex().

◆ GetFirstIPv4AddressFromIndex()

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

Definition at line 317 of file route.c.

321{
322 PIP_ADAPTER_ADDRESSES Ptr = pAdapterAddresses;
323
324 while (Ptr)
325 {
326 if (Ptr->IfIndex == IfIndex)
327 {
328 FormatIPv4Address(pBuffer, Ptr->FirstUnicastAddress, Ptr->IfIndex);
329 return;
330 }
331
332 Ptr = Ptr->Next;
333 }
334
335 swprintf(pBuffer, L"%lx", IfIndex);
336}
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
_ACRTIMP size_t __cdecl wcslen(const wchar_t *)
Definition: wcs.c:2983
#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 349 of file route.c.

351{
352 PMIB_IPFORWARDTABLE IpForwardTable = NULL;
353 PIP_ADAPTER_ADDRESSES pAdapterAddresses = NULL, Ptr, *pAdapterSortArray = NULL;
354 ULONG Size = 0;
356 ULONG adaptOutBufLen = 15000;
357 WCHAR Destination[IPBUF], Gateway[IPBUF], Netmask[IPBUF], Interface[IPBUF];
358 unsigned int i, AdapterCount, AdapterIndex;
359 BOOL EntriesFound;
360 ULONG Flags = /*GAA_FLAG_SKIP_UNICAST |*/ GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST |
361 GAA_FLAG_SKIP_DNS_SERVER;
362
363 /* set required buffer size */
364 pAdapterAddresses = (PIP_ADAPTER_ADDRESSES)malloc(adaptOutBufLen);
365 if (pAdapterAddresses == NULL)
366 {
368 goto Error;
369 }
370
371 if (GetAdaptersAddresses(AF_INET, Flags, NULL, pAdapterAddresses, &adaptOutBufLen) == ERROR_BUFFER_OVERFLOW)
372 {
373 free(pAdapterAddresses);
374 pAdapterAddresses = (PIP_ADAPTER_ADDRESSES)malloc(adaptOutBufLen);
375 if (pAdapterAddresses == NULL)
376 {
378 goto Error;
379 }
380 }
381
383 {
384 if (!(IpForwardTable = malloc(Size)))
385 {
387 goto Error;
388 }
389 }
390
391 if (((Error = GetAdaptersAddresses(AF_INET, Flags, NULL, pAdapterAddresses, &adaptOutBufLen)) == NO_ERROR) &&
392 ((Error = GetIpForwardTable(IpForwardTable, &Size, TRUE)) == NO_ERROR))
393 {
396
397 AdapterCount = 0;
398 Ptr = pAdapterAddresses;
399 while (Ptr)
400 {
401 AdapterCount++;
402 Ptr = Ptr->Next;
403 }
404
405 pAdapterSortArray = (PIP_ADAPTER_ADDRESSES*)malloc(AdapterCount * sizeof(PVOID));
406 if (pAdapterSortArray == NULL)
407 {
409 goto Error;
410 }
411
412 AdapterIndex = 0;
413 Ptr = pAdapterAddresses;
414 while (Ptr)
415 {
416 pAdapterSortArray[AdapterIndex] = Ptr;
417 AdapterIndex++;
418 Ptr = Ptr->Next;
419 }
420
421 qsort(pAdapterSortArray, AdapterCount, sizeof(PVOID), CompareAdapters);
422
423 for (AdapterIndex = 0; AdapterIndex < AdapterCount; AdapterIndex++)
424 {
425 Ptr = pAdapterSortArray[AdapterIndex];
426 if (Ptr->IfType == IF_TYPE_ETHERNET_CSMACD)
427 {
429 PrintMacAddress(Ptr->PhysicalAddress, PhysicalAddress);
430 ConResPrintf(StdOut, IDS_ETHERNET_ENTRY, Ptr->IfIndex, PhysicalAddress, Ptr->Description);
431 }
432 else
433 {
434 ConResPrintf(StdOut, IDS_INTERFACE_ENTRY, Ptr->IfIndex, Ptr->Description);
435 }
436 }
438
442 EntriesFound = FALSE;
443 for (i = 0; i < IpForwardTable->dwNumEntries; i++)
444 {
446 mbstowcs(Netmask, inet_ntoa(IN_ADDR_OF(IpForwardTable->table[i].dwForwardMask)), IPBUF);
447 mbstowcs(Gateway, inet_ntoa(IN_ADDR_OF(IpForwardTable->table[i].dwForwardNextHop)), IPBUF);
448 GetFirstIPv4AddressFromIndex(Interface, pAdapterAddresses, IpForwardTable->table[i].dwForwardIfIndex);
449
450 if (MatchWildcard(Destination, DestinationPattern))
451 {
452 if (EntriesFound == FALSE)
456 Netmask,
457 Gateway,
458 Interface,
459 IpForwardTable->table[i].dwForwardMetric1);
460 EntriesFound = TRUE;
461 }
462 }
463
464 if (DestinationPattern == NULL)
465 {
466 for (i = 0; i < IpForwardTable->dwNumEntries; i++)
467 {
468 if (IpForwardTable->table[i].dwForwardDest == 0)
469 {
470 mbstowcs(Gateway, inet_ntoa(IN_ADDR_OF(IpForwardTable->table[i].dwForwardNextHop)), IPBUF);
472 }
473 }
474 }
475 else if (EntriesFound == FALSE)
477 }
478
479Error:
480 if (pAdapterSortArray)
481 free(pAdapterSortArray);
482 if (pAdapterAddresses)
483 free(pAdapterAddresses);
484 if (IpForwardTable)
485 free(IpForwardTable);
486
487 if (Error != ERROR_SUCCESS)
489
490 return Error;
491}
#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:340
static VOID GetFirstIPv4AddressFromIndex(_Out_ PWSTR pBuffer, _In_ PIP_ADAPTER_ADDRESSES pAdapterAddresses, _In_ DWORD IfIndex)
Definition: route.c:317
#define StdOut
Definition: conutils_noros.h:6
#define NO_ERROR
Definition: dderror.h:5
_ACRTIMP void __cdecl qsort(void *, size_t, size_t, int(__cdecl *)(const void *, const void *))
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 495 of file route.c.

500{
501 PWSTR DestinationPattern = NULL;
502 DWORD Error;
503
504 if (argc > start)
506 &DestinationPattern, NULL, NULL, NULL);
507
508 Error = PrintActiveRoutes(DestinationPattern);
509 if (Error == ERROR_SUCCESS)
510 Error = PrintPersistentRoutes(DestinationPattern);
511
512 return (Error == ERROR_SUCCESS) ? 0 : 2;
513}
static DWORD PrintActiveRoutes(_In_ PWSTR DestinationPattern)
Definition: route.c:349
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 819 of file route.c.

822{
823 int i, flags = 0, ret = 0;
824
825 /* Initialize the Console Standard Streams */
827
828 for (i = 1; i < argc; i++)
829 {
830 if (argv[i][0] == L'-' || argv[i][0] == L'/')
831 {
832 if (!_wcsicmp(&argv[i][1], L"f"))
833 {
835 }
836 else if (!_wcsicmp(&argv[i][1], L"p"))
837 {
839 }
840 else
841 {
842 ret = 1;
843 break;
844 }
845 }
846 else
847 {
848 if (!_wcsicmp(argv[i], L"print"))
849 ret = PrintRoutes(argc, argv, i + 1, flags);
850 else if (!_wcsicmp(argv[i], L"add"))
851 ret = AddRoute(argc, argv, i + 1, flags);
852 else if (!_wcsicmp(argv[i], L"delete"))
853 ret = DeleteRoutes(argc, argv, i + 1, flags);
854 else
855 ret = 1;
856
857 break;
858 }
859 }
860
861 if (argc == 1)
862 ret = 1;
863
864 if (ret == 1)
865 {
867// ConResPrintf(StdErr, IDS_USAGE2);
869 }
870
871 return (ret == 0) ? 0 : 1;
872}
#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:654
static int PrintRoutes(_In_ int argc, _In_ WCHAR **argv, _In_ int start, _In_ int flags)
Definition: route.c:495
static int DeleteRoutes(_In_ int argc, _In_ WCHAR **argv, _In_ int start, _In_ int flags)
Definition: route.c:788
#define ConInitStdStreams()
Definition: conutils_noros.h:5
return ret
Definition: mutex.c:146