Skip to content

Commit 046572c

Browse files
committed
accept command line lauching options.
example: --filter "outbund" --lag on --lag-time 5000; no docs yet.
1 parent e2f121b commit 046572c

10 files changed

+161
-15
lines changed

src/common.h

+14-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
#define INTEGER_MAX "__INTEGER_MAX"
1717
#define INTEGER_MIN "__INTEGER_MIN"
1818

19+
// workaround stupid vs2012 runtime check.
20+
// it would show even when seeing explicit "(short)(i);"
21+
#define I2S(x) ((short)((x) & 0xFFFF))
22+
1923

2024
#ifdef __MINGW32__
2125
#define INLINE_FUNCTION __inline__
@@ -87,7 +91,8 @@ typedef struct {
8791
/*
8892
* Static module data
8993
*/
90-
const char *name; // name of the module
94+
const char *displayName; // display name shown in ui
95+
const char *shortName; // single word name
9196
short *enabledFlag; // volatile short flag to determine enabled or not
9297
Ihandle* (*setupUIFunc)(); // return hbox as controls group
9398
void (*startUp)(); // called when starting up the module
@@ -150,7 +155,13 @@ void endTimePeriod();
150155
// elevate
151156
BOOL IsElevated();
152157
BOOL IsRunAsAdmin();
153-
BOOL tryElevate(HWND hWnd);
158+
BOOL tryElevate(HWND hWnd, BOOL silent);
154159

155160
// icons
156-
extern const unsigned char icon8x8[8*8];
161+
extern const unsigned char icon8x8[8*8];
162+
163+
// parameterized
164+
extern BOOL parameterized;
165+
void setFromParameter(Ihandle *ih, const char *field, const char *key);
166+
BOOL parseArgs(int argc, char* argv[]);
167+

src/drop.c

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <Windows.h>
44
#include "iup.h"
55
#include "common.h"
6+
#define NAME "drop"
67

78
static Ihandle *inboundCheckbox, *outboundCheckbox, *chanceInput;
89

@@ -33,6 +34,12 @@ static Ihandle* dropSetupUI() {
3334
IupSetAttribute(inboundCheckbox, "VALUE", "ON");
3435
IupSetAttribute(outboundCheckbox, "VALUE", "ON");
3536

37+
if (parameterized) {
38+
setFromParameter(inboundCheckbox, "VALUE", NAME"-inbound");
39+
setFromParameter(outboundCheckbox, "VALUE", NAME"-outbound");
40+
setFromParameter(chanceInput, "VALUE", NAME"-chance");
41+
}
42+
3643
return dropControlsBox;
3744
}
3845

@@ -68,6 +75,7 @@ static short dropProcess(PacketNode *head, PacketNode* tail) {
6875

6976
Module dropModule = {
7077
"Drop",
78+
NAME,
7179
(short*)&dropEnabled,
7280
dropSetupUI,
7381
dropStartUp,

src/duplicate.c

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <stdlib.h>
33
#include "iup.h"
44
#include "common.h"
5+
#define NAME "duplicate"
56
#define COPIES_MIN "2"
67
#define COPIES_MAX "50"
78
#define COPIES_COUNT 2
@@ -44,6 +45,13 @@ static Ihandle* dupSetupUI() {
4445
IupSetAttribute(inboundCheckbox, "VALUE", "ON");
4546
IupSetAttribute(outboundCheckbox, "VALUE", "ON");
4647

48+
if (parameterized) {
49+
setFromParameter(inboundCheckbox, "VALUE", NAME"-inbound");
50+
setFromParameter(outboundCheckbox, "VALUE", NAME"-outbound");
51+
setFromParameter(chanceInput, "VALUE", NAME"-chance");
52+
setFromParameter(countInput, "VALUE", NAME"-count");
53+
}
54+
4755
return dupControlsBox;
4856
}
4957

@@ -78,6 +86,7 @@ static short dupProcess(PacketNode *head, PacketNode *tail) {
7886

7987
Module dupModule = {
8088
"Duplicate",
89+
NAME,
8190
(short*)&dupEnabled,
8291
dupSetupUI,
8392
dupStartup,

src/elevate.c

+11-7
Original file line numberDiff line numberDiff line change
@@ -98,25 +98,29 @@ BOOL IsElevated( ) {
9898
}
9999

100100
// try elevate and error out when can't happen
101+
// is silent then no message boxes are shown
101102
// return whether to close the program
102-
BOOL tryElevate(HWND hWnd) {
103+
BOOL tryElevate(HWND hWnd, BOOL silent) {
103104
// Check the current process's "run as administrator" status.
104105
BOOL fIsRunAsAdmin;
105106
OSVERSIONINFO osver = {sizeof(osver)}; // MUST initialize with the size or GetVersionEx fails
106107
if (!GetVersionEx(&osver)) {
107-
MessageBox(hWnd, (LPCSTR)"Failed to get os version. clumsy only supports Windows Vista or above.",
108+
if (!silent) MessageBox(hWnd, (LPCSTR)"Failed to get os version. clumsy only supports Windows Vista or above.",
108109
(LPCSTR)"Aborting", MB_OK);
109110
return TRUE;
110111
} else if (osver.dwMajorVersion < 6) {
111-
MessageBox(hWnd, (LPCSTR)"Unsupported Windows version. clumsy only supports Windows Vista or above.",
112+
if (!silent) MessageBox(hWnd, (LPCSTR)"Unsupported Windows version. clumsy only supports Windows Vista or above.",
112113
(LPCSTR)"Aborting", MB_OK);
113114
return TRUE;
114115
}
115116

116117
fIsRunAsAdmin = IsRunAsAdmin();
117118
if (fIsRunAsAdmin) {
118119
return FALSE;
119-
} else {
120+
}
121+
122+
// when not silent then trying to reinvoke to elevate
123+
if (!silent) {
120124
wchar_t szPath[MAX_PATH];
121125
if (GetModuleFileName(NULL, (LPSTR)szPath, ARRAYSIZE(szPath)))
122126
{
@@ -145,8 +149,8 @@ BOOL tryElevate(HWND hWnd) {
145149
MessageBox(hWnd, (LPCSTR)"Failed to get clumsy path. Please place the executable in a normal directory.",
146150
(LPCSTR)"Aborting", MB_OK);
147151
}
148-
149-
// exit when not run as admin
150-
return TRUE;
151152
}
153+
154+
// exit when not run as admin
155+
return TRUE;
152156
}

src/lag.c

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// lagging packets
22
#include "iup.h"
33
#include "common.h"
4+
#define NAME "lag"
45
#define LAG_MIN "0"
56
#define LAG_MAX "3000"
67
#define KEEP_AT_MOST 2000
@@ -50,6 +51,12 @@ static Ihandle *lagSetupUI() {
5051
IupSetAttribute(inboundCheckbox, "VALUE", "ON");
5152
IupSetAttribute(outboundCheckbox, "VALUE", "ON");
5253

54+
if (parameterized) {
55+
setFromParameter(inboundCheckbox, "VALUE", NAME"-inbound");
56+
setFromParameter(outboundCheckbox, "VALUE", NAME"-outbound");
57+
setFromParameter(timeInput, "VALUE", NAME"-time");
58+
}
59+
5360
return lagControlsBox;
5461
}
5562

@@ -117,6 +124,7 @@ static short lagProcess(PacketNode *head, PacketNode *tail) {
117124

118125
Module lagModule = {
119126
"Lag",
127+
NAME,
120128
(short*)&lagEnabled,
121129
lagSetupUI,
122130
lagStartUp,

src/main.c

+29-4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ typedef struct {
4848
UINT filtersSize;
4949
filterRecord filters[CONFIG_MAX_RECORDS] = {0};
5050
char configBuf[CONFIG_BUF_SIZE+2]; // add some padding to write \n
51+
BOOL parameterized = 0; // parameterized flag, means reading args from command line
5152

5253
// loading up filters and fill in
5354
void loadConfig() {
@@ -147,6 +148,18 @@ void init(int argc, char* argv[]) {
147148
)
148149
);
149150

151+
// parse arguments and set globals *before* setting up UI.
152+
// arguments can be read and set after callbacks are setup
153+
// FIXME as Release is built as WindowedApp, stdout/stderr won't show
154+
LOG("argc: %d", argc);
155+
if (argc > 1) {
156+
if (!parseArgs(argc, argv)) {
157+
fprintf(stderr, "invalid argument count. ensure you're using options as \"--drop on\"");
158+
exit(-1); // fail fast.
159+
}
160+
parameterized = 1;
161+
}
162+
150163
IupSetAttribute(topFrame, "TITLE", "Filtering");
151164
IupSetAttribute(topFrame, "EXPAND", "HORIZONTAL");
152165
IupSetAttribute(filterText, "EXPAND", "HORIZONTAL");
@@ -228,6 +241,7 @@ void init(int argc, char* argv[]) {
228241
timer = IupTimer();
229242
IupSetAttribute(timer, "TIME", STR(ICON_UPDATE_MS));
230243
IupSetCallback(timer, "ACTION_CB", uiTimerCb);
244+
231245
}
232246

233247
void startup() {
@@ -267,7 +281,13 @@ static int uiOnDialogShow(Ihandle *ih, int state) {
267281
SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)icon);
268282

269283
// try elevate and decides whether to exit
270-
exit = tryElevate(hWnd);
284+
exit = tryElevate(hWnd, parameterized);
285+
if (!exit && parameterized) {
286+
setFromParameter(filterText, "VALUE", "filter");
287+
LOG("is parameterized, start filtering upon execution.");
288+
uiStartCb(filterButton);
289+
}
290+
271291
return exit ? IUP_CLOSE : IUP_DEFAULT;
272292
}
273293

@@ -322,10 +342,10 @@ static int uiToggleControls(Ihandle *ih, int state) {
322342
int controlsActive = IupGetInt(controls, "ACTIVE");
323343
if (controlsActive && !state) {
324344
IupSetAttribute(controls, "ACTIVE", "NO");
325-
InterlockedExchange16(target, (short)state);
345+
InterlockedExchange16(target, I2S(state));
326346
} else if (!controlsActive && state) {
327347
IupSetAttribute(controls, "ACTIVE", "YES");
328-
InterlockedExchange16(target, (short)state);
348+
InterlockedExchange16(target, I2S(state));
329349
}
330350

331351
return IUP_DEFAULT;
@@ -382,7 +402,7 @@ static void uiSetupModule(Module *module, Ihandle *parent) {
382402
Ihandle *groupBox, *toggle, *controls, *icon;
383403
groupBox = IupHbox(
384404
icon = IupLabel(NULL),
385-
toggle = IupToggle(module->name, NULL),
405+
toggle = IupToggle(module->displayName, NULL),
386406
IupFill(),
387407
controls = module->setupUIFunc(),
388408
NULL
@@ -403,6 +423,11 @@ static void uiSetupModule(Module *module, Ihandle *parent) {
403423
IupSetAttribute(icon, "IMAGE", "none_icon");
404424
IupSetAttribute(icon, "PADDING", "4x");
405425
module->iconHandle = icon;
426+
427+
// parameterize toggle
428+
if (parameterized) {
429+
setFromParameter(toggle, "VALUE", module->shortName);
430+
}
406431
}
407432

408433
int main(int argc, char* argv[]) {

src/ood.c

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// out of order arrange packets module
22
#include "iup.h"
33
#include "common.h"
4+
#define NAME "ood"
45
// keep a picked packet at most for KEEP_TURNS_MAX steps, or if there's no following
56
// one it would just to be sended
67
#define KEEP_TURNS_MAX 10
@@ -35,6 +36,12 @@ static Ihandle *oodSetupUI() {
3536
IupSetAttribute(inboundCheckbox, "VALUE", "ON");
3637
IupSetAttribute(outboundCheckbox, "VALUE", "ON");
3738

39+
if (parameterized) {
40+
setFromParameter(inboundCheckbox, "VALUE", NAME"-inbound");
41+
setFromParameter(outboundCheckbox, "VALUE", NAME"-outbound");
42+
setFromParameter(chanceInput, "VALUE", NAME"-chance");
43+
}
44+
3845
return oodControlsBox;
3946
}
4047

@@ -134,6 +141,7 @@ static short oodProcess(PacketNode *head, PacketNode *tail) {
134141

135142
Module oodModule = {
136143
"Out of order",
144+
NAME,
137145
(short*)&oodEnabled,
138146
oodSetupUI,
139147
oodStartUp,

src/tamper.c

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "iup.h"
33
#include "windivert.h"
44
#include "common.h"
5+
#define NAME "tamper"
56

67
static Ihandle *inboundCheckbox, *outboundCheckbox, *chanceInput, *checksumCheckbox;
78

@@ -38,6 +39,13 @@ static Ihandle* tamperSetupUI() {
3839
IupSetAttribute(outboundCheckbox, "VALUE", "ON");
3940
IupSetAttribute(checksumCheckbox, "VALUE", "ON");
4041

42+
if (parameterized) {
43+
setFromParameter(inboundCheckbox, "VALUE", NAME"-inbound");
44+
setFromParameter(outboundCheckbox, "VALUE", NAME"-outbound");
45+
setFromParameter(chanceInput, "VALUE", NAME"-chance");
46+
setFromParameter(checksumCheckbox, "VALUE", NAME"-checksum");
47+
}
48+
4149
return dupControlsBox;
4250
}
4351

@@ -114,6 +122,7 @@ static short tamperProcess(PacketNode *head, PacketNode *tail) {
114122

115123
Module tamperModule = {
116124
"Tamper",
125+
NAME,
117126
(short*)&tamperEnabled,
118127
tamperSetupUI,
119128
tamperStartup,

src/throttle.c

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// throttling packets
22
#include "iup.h"
33
#include "common.h"
4+
#define NAME "throttle"
45
#define TIME_MIN "0"
56
#define TIME_MAX "1000"
67
#define TIME_DEFAULT 30
@@ -58,6 +59,13 @@ static Ihandle *throttleSetupUI() {
5859
IupSetAttribute(inboundCheckbox, "VALUE", "ON");
5960
IupSetAttribute(outboundCheckbox, "VALUE", "ON");
6061

62+
if (parameterized) {
63+
setFromParameter(inboundCheckbox, "VALUE", NAME"-inbound");
64+
setFromParameter(outboundCheckbox, "VALUE", NAME"-outbound");
65+
setFromParameter(chanceInput, "VALUE", NAME"-chance");
66+
setFromParameter(frameInput, "VALUE", NAME"-frame");
67+
}
68+
6169
return throttleControlsBox;
6270
}
6371

@@ -129,6 +137,7 @@ static short throttleProcess(PacketNode *head, PacketNode *tail) {
129137

130138
Module throttleModule = {
131139
"Throttle",
140+
NAME,
132141
(short*)&throttleEnabled,
133142
throttleSetupUI,
134143
throttleStartUp,

0 commit comments

Comments
 (0)