37
37
* This program does nothing except divert packets and re-inject them. This is
38
38
* useful for performance testing.
39
39
*
40
- * usage: netdump .exe windivert-filter num-threads
40
+ * usage: passthru .exe [ windivert-filter] [ num-threads] [batch-size] [priority]
41
41
*/
42
42
43
43
#include <winsock2.h>
48
48
#include "windivert.h"
49
49
50
50
#define MTU 1500
51
- static int batch = 1 ;
51
+
52
+ typedef struct
53
+ {
54
+ HANDLE handle ;
55
+ int batch ;
56
+ } CONFIG , * PCONFIG ;
52
57
53
58
static DWORD passthru (LPVOID arg );
54
59
@@ -57,37 +62,53 @@ static DWORD passthru(LPVOID arg);
57
62
*/
58
63
int __cdecl main (int argc , char * * argv )
59
64
{
60
- int num_threads , priority = 0 , i ;
65
+ const char * filter = "true" ;
66
+ int threads = 1 , batch = 1 , priority = 0 ;
67
+ int i ;
61
68
HANDLE handle , thread ;
69
+ CONFIG config ;
62
70
63
- if (argc < 3 || argc > 5 )
71
+ if (argc > 5 )
64
72
{
65
- fprintf (stderr , "usage: %s filter num-threads [batch] [priority]\n" ,
66
- argv [0 ]);
73
+ fprintf (stderr , "usage: %s [ filter] [ num-threads] [batch-size] "
74
+ "[priority]\n" , argv [0 ]);
67
75
exit (EXIT_FAILURE );
68
76
}
69
- num_threads = atoi (argv [2 ]);
70
- if (num_threads < 1 || num_threads > 64 )
77
+ if (argc >= 2 )
71
78
{
72
- fprintf (stderr , "error: invalid number of threads\n" );
73
- exit (EXIT_FAILURE );
79
+ filter = argv [1 ];
74
80
}
75
- if (argc >= 4 )
81
+ if (argc >= 3 )
76
82
{
77
- batch = atoi (argv [3 ]);
83
+ threads = atoi (argv [2 ]);
84
+ if (threads < 1 || threads > 64 )
85
+ {
86
+ fprintf (stderr , "error: invalid number of threads\n" );
87
+ exit (EXIT_FAILURE );
88
+ }
78
89
}
79
- if (batch <= 0 || batch > WINDIVERT_BATCH_MAX )
90
+ if (argc >= 4 )
80
91
{
81
- fprintf (stderr , "error: invalid batch size\n" );
82
- exit (EXIT_FAILURE );
92
+ batch = atoi (argv [3 ]);
93
+ if (batch <= 0 || batch > WINDIVERT_BATCH_MAX )
94
+ {
95
+ fprintf (stderr , "error: invalid batch size\n" );
96
+ exit (EXIT_FAILURE );
97
+ }
83
98
}
84
99
if (argc >= 5 )
85
100
{
86
101
priority = atoi (argv [4 ]);
102
+ if (priority < WINDIVERT_PRIORITY_LOWEST ||
103
+ priority > WINDIVERT_PRIORITY_HIGHEST )
104
+ {
105
+ fprintf (stderr , "error: invalid priority value\n" );
106
+ exit (EXIT_FAILURE );
107
+ }
87
108
}
88
109
89
110
// Divert traffic matching the filter:
90
- handle = WinDivertOpen (argv [ 1 ] , WINDIVERT_LAYER_NETWORK , (INT16 )priority ,
111
+ handle = WinDivertOpen (filter , WINDIVERT_LAYER_NETWORK , (INT16 )priority ,
91
112
0 );
92
113
if (handle == INVALID_HANDLE_VALUE )
93
114
{
@@ -102,10 +123,12 @@ int __cdecl main(int argc, char **argv)
102
123
}
103
124
104
125
// Start the threads
105
- for (i = 1 ; i < num_threads ; i ++ )
126
+ config .handle = handle ;
127
+ config .batch = batch ;
128
+ for (i = 1 ; i < threads ; i ++ )
106
129
{
107
130
thread = CreateThread (NULL , 1 , (LPTHREAD_START_ROUTINE )passthru ,
108
- (LPVOID )handle , 0 , NULL );
131
+ (LPVOID )& config , 0 , NULL );
109
132
if (thread == NULL )
110
133
{
111
134
fprintf (stderr , "error: failed to start passthru thread (%d)\n" ,
@@ -115,7 +138,7 @@ int __cdecl main(int argc, char **argv)
115
138
}
116
139
117
140
// Main thread:
118
- passthru ((LPVOID )handle );
141
+ passthru ((LPVOID )& config );
119
142
120
143
return 0 ;
121
144
}
@@ -126,7 +149,12 @@ static DWORD passthru(LPVOID arg)
126
149
UINT8 * packet ;
127
150
UINT packet_len , addr_len ;
128
151
WINDIVERT_ADDRESS * addr ;
129
- HANDLE handle = (HANDLE )arg ;
152
+ PCONFIG config = (PCONFIG )arg ;
153
+ HANDLE handle ;
154
+ int batch ;
155
+
156
+ handle = config -> handle ;
157
+ batch = config -> batch ;
130
158
131
159
packet = (UINT8 * )malloc (batch * MTU );
132
160
addr = (WINDIVERT_ADDRESS * )malloc (batch * sizeof (WINDIVERT_ADDRESS ));
0 commit comments