Skip to content

Commit

Permalink
accept command line lauching options.
Browse files Browse the repository at this point in the history
example: --filter "outbund" --lag on --lag-time 5000; no docs yet.
  • Loading branch information
jagt committed Dec 14, 2013
1 parent e2f121b commit 046572c
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 15 deletions.
17 changes: 14 additions & 3 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#define INTEGER_MAX "__INTEGER_MAX"
#define INTEGER_MIN "__INTEGER_MIN"

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


#ifdef __MINGW32__
#define INLINE_FUNCTION __inline__
Expand Down Expand Up @@ -87,7 +91,8 @@ typedef struct {
/*
* Static module data
*/
const char *name; // name of the module
const char *displayName; // display name shown in ui
const char *shortName; // single word name
short *enabledFlag; // volatile short flag to determine enabled or not
Ihandle* (*setupUIFunc)(); // return hbox as controls group
void (*startUp)(); // called when starting up the module
Expand Down Expand Up @@ -150,7 +155,13 @@ void endTimePeriod();
// elevate
BOOL IsElevated();
BOOL IsRunAsAdmin();
BOOL tryElevate(HWND hWnd);
BOOL tryElevate(HWND hWnd, BOOL silent);

// icons
extern const unsigned char icon8x8[8*8];
extern const unsigned char icon8x8[8*8];

// parameterized
extern BOOL parameterized;
void setFromParameter(Ihandle *ih, const char *field, const char *key);
BOOL parseArgs(int argc, char* argv[]);

8 changes: 8 additions & 0 deletions src/drop.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <Windows.h>
#include "iup.h"
#include "common.h"
#define NAME "drop"

static Ihandle *inboundCheckbox, *outboundCheckbox, *chanceInput;

Expand Down Expand Up @@ -33,6 +34,12 @@ static Ihandle* dropSetupUI() {
IupSetAttribute(inboundCheckbox, "VALUE", "ON");
IupSetAttribute(outboundCheckbox, "VALUE", "ON");

if (parameterized) {
setFromParameter(inboundCheckbox, "VALUE", NAME"-inbound");
setFromParameter(outboundCheckbox, "VALUE", NAME"-outbound");
setFromParameter(chanceInput, "VALUE", NAME"-chance");
}

return dropControlsBox;
}

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

Module dropModule = {
"Drop",
NAME,
(short*)&dropEnabled,
dropSetupUI,
dropStartUp,
Expand Down
9 changes: 9 additions & 0 deletions src/duplicate.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdlib.h>
#include "iup.h"
#include "common.h"
#define NAME "duplicate"
#define COPIES_MIN "2"
#define COPIES_MAX "50"
#define COPIES_COUNT 2
Expand Down Expand Up @@ -44,6 +45,13 @@ static Ihandle* dupSetupUI() {
IupSetAttribute(inboundCheckbox, "VALUE", "ON");
IupSetAttribute(outboundCheckbox, "VALUE", "ON");

if (parameterized) {
setFromParameter(inboundCheckbox, "VALUE", NAME"-inbound");
setFromParameter(outboundCheckbox, "VALUE", NAME"-outbound");
setFromParameter(chanceInput, "VALUE", NAME"-chance");
setFromParameter(countInput, "VALUE", NAME"-count");
}

return dupControlsBox;
}

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

Module dupModule = {
"Duplicate",
NAME,
(short*)&dupEnabled,
dupSetupUI,
dupStartup,
Expand Down
18 changes: 11 additions & 7 deletions src/elevate.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,29 @@ BOOL IsElevated( ) {
}

// try elevate and error out when can't happen
// is silent then no message boxes are shown
// return whether to close the program
BOOL tryElevate(HWND hWnd) {
BOOL tryElevate(HWND hWnd, BOOL silent) {
// Check the current process's "run as administrator" status.
BOOL fIsRunAsAdmin;
OSVERSIONINFO osver = {sizeof(osver)}; // MUST initialize with the size or GetVersionEx fails
if (!GetVersionEx(&osver)) {
MessageBox(hWnd, (LPCSTR)"Failed to get os version. clumsy only supports Windows Vista or above.",
if (!silent) MessageBox(hWnd, (LPCSTR)"Failed to get os version. clumsy only supports Windows Vista or above.",
(LPCSTR)"Aborting", MB_OK);
return TRUE;
} else if (osver.dwMajorVersion < 6) {
MessageBox(hWnd, (LPCSTR)"Unsupported Windows version. clumsy only supports Windows Vista or above.",
if (!silent) MessageBox(hWnd, (LPCSTR)"Unsupported Windows version. clumsy only supports Windows Vista or above.",
(LPCSTR)"Aborting", MB_OK);
return TRUE;
}

fIsRunAsAdmin = IsRunAsAdmin();
if (fIsRunAsAdmin) {
return FALSE;
} else {
}

// when not silent then trying to reinvoke to elevate
if (!silent) {
wchar_t szPath[MAX_PATH];
if (GetModuleFileName(NULL, (LPSTR)szPath, ARRAYSIZE(szPath)))
{
Expand Down Expand Up @@ -145,8 +149,8 @@ BOOL tryElevate(HWND hWnd) {
MessageBox(hWnd, (LPCSTR)"Failed to get clumsy path. Please place the executable in a normal directory.",
(LPCSTR)"Aborting", MB_OK);
}

// exit when not run as admin
return TRUE;
}

// exit when not run as admin
return TRUE;
}
8 changes: 8 additions & 0 deletions src/lag.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// lagging packets
#include "iup.h"
#include "common.h"
#define NAME "lag"
#define LAG_MIN "0"
#define LAG_MAX "3000"
#define KEEP_AT_MOST 2000
Expand Down Expand Up @@ -50,6 +51,12 @@ static Ihandle *lagSetupUI() {
IupSetAttribute(inboundCheckbox, "VALUE", "ON");
IupSetAttribute(outboundCheckbox, "VALUE", "ON");

if (parameterized) {
setFromParameter(inboundCheckbox, "VALUE", NAME"-inbound");
setFromParameter(outboundCheckbox, "VALUE", NAME"-outbound");
setFromParameter(timeInput, "VALUE", NAME"-time");
}

return lagControlsBox;
}

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

Module lagModule = {
"Lag",
NAME,
(short*)&lagEnabled,
lagSetupUI,
lagStartUp,
Expand Down
33 changes: 29 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ typedef struct {
UINT filtersSize;
filterRecord filters[CONFIG_MAX_RECORDS] = {0};
char configBuf[CONFIG_BUF_SIZE+2]; // add some padding to write \n
BOOL parameterized = 0; // parameterized flag, means reading args from command line

// loading up filters and fill in
void loadConfig() {
Expand Down Expand Up @@ -147,6 +148,18 @@ void init(int argc, char* argv[]) {
)
);

// parse arguments and set globals *before* setting up UI.
// arguments can be read and set after callbacks are setup
// FIXME as Release is built as WindowedApp, stdout/stderr won't show
LOG("argc: %d", argc);
if (argc > 1) {
if (!parseArgs(argc, argv)) {
fprintf(stderr, "invalid argument count. ensure you're using options as \"--drop on\"");
exit(-1); // fail fast.
}
parameterized = 1;
}

IupSetAttribute(topFrame, "TITLE", "Filtering");
IupSetAttribute(topFrame, "EXPAND", "HORIZONTAL");
IupSetAttribute(filterText, "EXPAND", "HORIZONTAL");
Expand Down Expand Up @@ -228,6 +241,7 @@ void init(int argc, char* argv[]) {
timer = IupTimer();
IupSetAttribute(timer, "TIME", STR(ICON_UPDATE_MS));
IupSetCallback(timer, "ACTION_CB", uiTimerCb);

}

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

// try elevate and decides whether to exit
exit = tryElevate(hWnd);
exit = tryElevate(hWnd, parameterized);
if (!exit && parameterized) {
setFromParameter(filterText, "VALUE", "filter");
LOG("is parameterized, start filtering upon execution.");
uiStartCb(filterButton);
}

return exit ? IUP_CLOSE : IUP_DEFAULT;
}

Expand Down Expand Up @@ -322,10 +342,10 @@ static int uiToggleControls(Ihandle *ih, int state) {
int controlsActive = IupGetInt(controls, "ACTIVE");
if (controlsActive && !state) {
IupSetAttribute(controls, "ACTIVE", "NO");
InterlockedExchange16(target, (short)state);
InterlockedExchange16(target, I2S(state));
} else if (!controlsActive && state) {
IupSetAttribute(controls, "ACTIVE", "YES");
InterlockedExchange16(target, (short)state);
InterlockedExchange16(target, I2S(state));
}

return IUP_DEFAULT;
Expand Down Expand Up @@ -382,7 +402,7 @@ static void uiSetupModule(Module *module, Ihandle *parent) {
Ihandle *groupBox, *toggle, *controls, *icon;
groupBox = IupHbox(
icon = IupLabel(NULL),
toggle = IupToggle(module->name, NULL),
toggle = IupToggle(module->displayName, NULL),
IupFill(),
controls = module->setupUIFunc(),
NULL
Expand All @@ -403,6 +423,11 @@ static void uiSetupModule(Module *module, Ihandle *parent) {
IupSetAttribute(icon, "IMAGE", "none_icon");
IupSetAttribute(icon, "PADDING", "4x");
module->iconHandle = icon;

// parameterize toggle
if (parameterized) {
setFromParameter(toggle, "VALUE", module->shortName);
}
}

int main(int argc, char* argv[]) {
Expand Down
8 changes: 8 additions & 0 deletions src/ood.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// out of order arrange packets module
#include "iup.h"
#include "common.h"
#define NAME "ood"
// keep a picked packet at most for KEEP_TURNS_MAX steps, or if there's no following
// one it would just to be sended
#define KEEP_TURNS_MAX 10
Expand Down Expand Up @@ -35,6 +36,12 @@ static Ihandle *oodSetupUI() {
IupSetAttribute(inboundCheckbox, "VALUE", "ON");
IupSetAttribute(outboundCheckbox, "VALUE", "ON");

if (parameterized) {
setFromParameter(inboundCheckbox, "VALUE", NAME"-inbound");
setFromParameter(outboundCheckbox, "VALUE", NAME"-outbound");
setFromParameter(chanceInput, "VALUE", NAME"-chance");
}

return oodControlsBox;
}

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

Module oodModule = {
"Out of order",
NAME,
(short*)&oodEnabled,
oodSetupUI,
oodStartUp,
Expand Down
9 changes: 9 additions & 0 deletions src/tamper.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "iup.h"
#include "windivert.h"
#include "common.h"
#define NAME "tamper"

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

Expand Down Expand Up @@ -38,6 +39,13 @@ static Ihandle* tamperSetupUI() {
IupSetAttribute(outboundCheckbox, "VALUE", "ON");
IupSetAttribute(checksumCheckbox, "VALUE", "ON");

if (parameterized) {
setFromParameter(inboundCheckbox, "VALUE", NAME"-inbound");
setFromParameter(outboundCheckbox, "VALUE", NAME"-outbound");
setFromParameter(chanceInput, "VALUE", NAME"-chance");
setFromParameter(checksumCheckbox, "VALUE", NAME"-checksum");
}

return dupControlsBox;
}

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

Module tamperModule = {
"Tamper",
NAME,
(short*)&tamperEnabled,
tamperSetupUI,
tamperStartup,
Expand Down
9 changes: 9 additions & 0 deletions src/throttle.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// throttling packets
#include "iup.h"
#include "common.h"
#define NAME "throttle"
#define TIME_MIN "0"
#define TIME_MAX "1000"
#define TIME_DEFAULT 30
Expand Down Expand Up @@ -58,6 +59,13 @@ static Ihandle *throttleSetupUI() {
IupSetAttribute(inboundCheckbox, "VALUE", "ON");
IupSetAttribute(outboundCheckbox, "VALUE", "ON");

if (parameterized) {
setFromParameter(inboundCheckbox, "VALUE", NAME"-inbound");
setFromParameter(outboundCheckbox, "VALUE", NAME"-outbound");
setFromParameter(chanceInput, "VALUE", NAME"-chance");
setFromParameter(frameInput, "VALUE", NAME"-frame");
}

return throttleControlsBox;
}

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

Module throttleModule = {
"Throttle",
NAME,
(short*)&throttleEnabled,
throttleSetupUI,
throttleStartUp,
Expand Down
Loading

0 comments on commit 046572c

Please sign in to comment.