Skip to content

Commit 3d4f52e

Browse files
committed
Cleanup the passthru sample program.
1 parent 1cc1778 commit 3d4f52e

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

examples/passthru/passthru.c

+48-20
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* This program does nothing except divert packets and re-inject them. This is
3838
* useful for performance testing.
3939
*
40-
* usage: netdump.exe windivert-filter num-threads
40+
* usage: passthru.exe [windivert-filter] [num-threads] [batch-size] [priority]
4141
*/
4242

4343
#include <winsock2.h>
@@ -48,7 +48,12 @@
4848
#include "windivert.h"
4949

5050
#define MTU 1500
51-
static int batch = 1;
51+
52+
typedef struct
53+
{
54+
HANDLE handle;
55+
int batch;
56+
} CONFIG, *PCONFIG;
5257

5358
static DWORD passthru(LPVOID arg);
5459

@@ -57,37 +62,53 @@ static DWORD passthru(LPVOID arg);
5762
*/
5863
int __cdecl main(int argc, char **argv)
5964
{
60-
int num_threads, priority = 0, i;
65+
const char *filter = "true";
66+
int threads = 1, batch = 1, priority = 0;
67+
int i;
6168
HANDLE handle, thread;
69+
CONFIG config;
6270

63-
if (argc < 3 || argc > 5)
71+
if (argc > 5)
6472
{
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]);
6775
exit(EXIT_FAILURE);
6876
}
69-
num_threads = atoi(argv[2]);
70-
if (num_threads < 1 || num_threads > 64)
77+
if (argc >= 2)
7178
{
72-
fprintf(stderr, "error: invalid number of threads\n");
73-
exit(EXIT_FAILURE);
79+
filter = argv[1];
7480
}
75-
if (argc >= 4)
81+
if (argc >= 3)
7682
{
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+
}
7889
}
79-
if (batch <= 0 || batch > WINDIVERT_BATCH_MAX)
90+
if (argc >= 4)
8091
{
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+
}
8398
}
8499
if (argc >= 5)
85100
{
86101
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+
}
87108
}
88109

89110
// Divert traffic matching the filter:
90-
handle = WinDivertOpen(argv[1], WINDIVERT_LAYER_NETWORK, (INT16)priority,
111+
handle = WinDivertOpen(filter, WINDIVERT_LAYER_NETWORK, (INT16)priority,
91112
0);
92113
if (handle == INVALID_HANDLE_VALUE)
93114
{
@@ -102,10 +123,12 @@ int __cdecl main(int argc, char **argv)
102123
}
103124

104125
// 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++)
106129
{
107130
thread = CreateThread(NULL, 1, (LPTHREAD_START_ROUTINE)passthru,
108-
(LPVOID)handle, 0, NULL);
131+
(LPVOID)&config, 0, NULL);
109132
if (thread == NULL)
110133
{
111134
fprintf(stderr, "error: failed to start passthru thread (%d)\n",
@@ -115,7 +138,7 @@ int __cdecl main(int argc, char **argv)
115138
}
116139

117140
// Main thread:
118-
passthru((LPVOID)handle);
141+
passthru((LPVOID)&config);
119142

120143
return 0;
121144
}
@@ -126,7 +149,12 @@ static DWORD passthru(LPVOID arg)
126149
UINT8 *packet;
127150
UINT packet_len, addr_len;
128151
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;
130158

131159
packet = (UINT8 *)malloc(batch * MTU);
132160
addr = (WINDIVERT_ADDRESS *)malloc(batch * sizeof(WINDIVERT_ADDRESS));

0 commit comments

Comments
 (0)