aboutsummaryrefslogtreecommitdiffstats
path: root/echld_test.c
blob: fff59dfc8911617532ff9495300cfd4284fa4ef6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* echld-test.c
 *  basic test framework for echld
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * Copyright (c) 2013 by Luis Ontanon <luis@ontanon.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <config.h>

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif

#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif

#include <sys/time.h>
#include <sys/uio.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <glib.h>
#include <glib/gprintf.h>

#include "echld/echld.h"
#include "echld/echld-util.h"

#include "epan/epan.h"
#include "wsutil/str_util.h"

typedef char* (*cmd_cb_t)(char** params, char** err);

typedef struct _cmd_t {
	const char* txt;
	cmd_cb_t cb;
	int args_taken;
	const char* help;
} cmd_t;


#define MAX_PARAMSETS 16

static enc_msg_t* paramsets[MAX_PARAMSETS] = {
	NULL,NULL,NULL,NULL, NULL,NULL,NULL,NULL,
	NULL,NULL,NULL,NULL, NULL,NULL,NULL,NULL
};

static int nps = 0;

static char* ps_cmd(char** pars _U_, char** err _U_) {
	int n_id = nps++;

	if (n_id >= MAX_PARAMSETS) {
		*err = g_strdup("Max Num of Paramsets reached");
		return NULL;
	}

	paramsets[n_id] = echld_new_child_params();

	return g_strdup_printf("New Paramset ps_id=%d",n_id);
}

static char* psadd_cmd(char** params _U_, char** err) {
	int ps_id = (int) strtol(params[1], NULL, 10);

	if (ps_id >= nps) {
		*err = g_strdup_printf("No paramset pd_is=%d",ps_id);
		return NULL;
	}

	echld_new_child_params_add_params(paramsets[ps_id], params[2], params[3], NULL);

	return g_strdup_printf("PSAdd ps_id=%d %s='%s'", ps_id, params[2], params[3]);
}

static char* psmerge_cmd(char** params, char** err) {
	int ps1_id = (int) strtol(params[1], NULL, 10);
	int ps2_id = (int) strtol(params[2], NULL, 10);
	int n_id;

	if (ps1_id >= nps) {
		*err = g_strdup_printf("No paramset pd_is=%d",ps1_id);
		return NULL;
	}

	if (ps2_id >= nps) {
		*err = g_strdup_printf("No paramset pd_is=%d",ps2_id);
		return NULL;
	}

	n_id = nps++;

	if (n_id >= MAX_PARAMSETS) {
		*err = g_strdup_printf("Max Num of Paramsets reached");
		return NULL;
	}

	paramsets[n_id] = echld_new_child_params_merge(paramsets[ps1_id],paramsets[ps2_id]);

	return g_strdup_printf("Merged Paramset ps1_id=%d ps2_id=%d ps_id=%d",ps1_id, ps2_id, n_id);
}

static char* new_child_cmd(char** params, char** err) {
	int ps_id = (int) strtol(params[1], NULL, 10);
	int child;

	if (ps_id >= nps) {
		*err = g_strdup_printf("No paramset pd_is=%d",ps_id);
		return NULL;
	}

	child = echld_new(paramsets[ps_id],NULL,NULL);

	if (child <= 0) {
		*err = g_strdup("No child\n");
		return NULL;
	}

	return g_strdup_printf("New chld_id=%d\n",child);
}


void ping_cb(long usec, void* data) {
	int ping_id = *((int*)data);

	if (usec >= 0) {
		fprintf(stdout, "Ping ping_id=%d returned in %dus\n",ping_id,(int)usec);
	} else {
		fprintf(stdout, "Ping ping_id=%d erored\n",ping_id);
	}

	g_free(data);
}


static char* ping_cmd(char** params, char** err) {
	int child = (int) strtol(params[1], NULL, 10);
	static int ping_id = 0;
	int* ping_data = g_new(int,1);

	*ping_data = ping_id++;

	if (!echld_ping(child,ping_cb,ping_data)) {
		*err = g_strdup_printf("Could not send ping child=%d",child);
		return NULL;
	} else {
		return g_strdup_printf("Ping sent child=%d",child);
	}
}

void param_cb(const char* param, const char* value, const char* error, void* data _U_) {
	if (error) {
		fprintf(stdout, "Param Set Error msg=%s\n", error );
	} else {
		fprintf(stdout, "Param: param='%s' val='%s'\n", param, value );
	}
}

static char* set_cmd(char** params, char** err) {
	int child = (int) strtol(params[1], NULL, 10);
	char* param = params[2];
	char* value = params[3];

	if ( ! echld_set_param(child,param,value,param_cb,NULL) ) {
		*err = g_strdup_printf("Failed to SET child=%d param='%s' value='%s'",child,param,value);
		return NULL;
	} else {
		return g_strdup_printf("Set command sent child=%d param='%s' value='%s'",child,param,value);
	}
}

static char* get_cmd(char** params, char** err) {
	int child = (int) strtol(params[1], NULL, 10);
	char* param = params[2];

	if ( ! echld_get_param(child,param,param_cb,NULL) ) {
		*err = g_strdup_printf("Failed to GET child=%d param='%s'",child,param);
		return NULL;
	} else {
		return g_strdup_printf("Get command sent child=%d param='%s'",child,param);
	}
}

static void close_cb(const char* error, void* data) {
	if (error) {
		fprintf(stdout, "Close Error msg=%s\n", error );
	} else {
		fprintf(stdout, "Closed: child=%d\n", *((int*)data) );
	}
}

static char* close_cmd(char** params, char** err) {
	int child = (int) strtol(params[1], NULL, 10);
	int* cmdp = g_new(int,1);
	*cmdp = child;
	if ( ! echld_close(child,close_cb,cmdp) ) {
		*err = g_strdup_printf("Could not close child=%d",child);
		return NULL;
	} else {
		return g_strdup_printf("CLose command sent child=%d",child);
	}
}
int keep_going = 1;

static char* quit_cmd(char** params _U_, char** err _U_) {
	keep_going = 0;
	return g_strdup("Quitting");
}

static char* help_cmd(char**, char**);

static char* open_file_cmd(char** pars _U_, char** err _U_) {
	*err = g_strdup("Not Implemented");
	return NULL;
}

static char* prepare_capture_cmd(char** pars _U_, char** err _U_) {
	*err = g_strdup("Not Implemented");
	return NULL;
}

static char* start_capture_cmd(char** pars _U_, char** err _U_) {
	*err = g_strdup("Not Implemented");
	return NULL;
}

static char* get_sum_cmd(char** pars _U_, char** err _U_) {
	*err = g_strdup("Not Implemented");
	return NULL;
}

static char* get_tree_cmd(char** pars _U_, char** err _U_) {
	*err = g_strdup("Not Implemented");
	return NULL;
}

static char* get_buf_cmd(char** pars _U_, char** err _U_) {
	*err = g_strdup("Not Implemented");
	return NULL;
}

static char* stop_cmd(char** pars _U_, char** err _U_) {
	*err = g_strdup("Not Implemented");
	return NULL;
}

static char* note_cmd(char** pars _U_, char** err _U_) {
	*err = g_strdup("Not Implemented");
	return NULL;
}

static char* apply_cmd(char** pars _U_, char** err _U_) {
	*err = g_strdup("Not Implemented");
	return NULL;
}

static char* save_cmd(char** pars _U_, char** err _U_) {
	*err = g_strdup("Not Implemented");
	return NULL;
}

static char* run_cmd(char** pars, char** err _U_);




cmd_t commands[] = {
	{ "QUIT", quit_cmd, 0, "QUIT"},
	{ "HELP", help_cmd, 0, "HELP"},
	{ "RUN", run_cmd, 1, "RUN filename"},
	{ "PS", ps_cmd, 1, "PS [dummy]"},
	{ "PSADD", psadd_cmd, 4, "PSADD ps_id param value"},
	{ "PSMERGE", psmerge_cmd, 4, "PSMERGE ps_id ps_id"},
	{ "NEW", new_child_cmd, 1, "NEW ps_id"},
	{ "PING", ping_cmd, 1, "PING child_id"},
	{ "SET", set_cmd, 3, "SET child_id param_name param_val"},
	{ "GET", get_cmd, 2, "GET child_id param_name"},
	{ "CLOSE", close_cmd, 1, "CLOSE child_id"},
	{ "FILE", open_file_cmd, 2, "FILE child_id filename"},
	{ "PREP", prepare_capture_cmd, 2, "PREP child_id intfname params"},
	{ "START",start_capture_cmd,1,"START child_id"},
	{ "SUM", get_sum_cmd,2,"SUM child_id packet_range"},
	{ "TREE", get_tree_cmd,2,"TREE child_id packet_range"},
	{ "BUFF", get_buf_cmd,2,"BUFF child_id buffer_name"},
	{ "STOP", stop_cmd,1,"STOP child_id"},
	{ "NOTE", note_cmd,1,"NOTE child_id framenum note..."},
	{ "APPLY", apply_cmd,1,"APPLY child_id dfilter"},
	{ "SAVE", save_cmd,1,"SAVE child_id filename params"},
	{ NULL, NULL, 0, NULL }
};

static char* help_cmd(char** params _U_, char** err _U_) {
	GString* out = g_string_new("Commands:\n");
	cmd_t* c = commands;

	for (;c->txt;c++) {
		g_string_append_printf(out,"%s\n",c->help);
	}
	return g_string_free(out,FALSE);
}


static int invoke_cmd(FILE* in_fp) {
	size_t len;
	char* cmd_line;

	if(( cmd_line = fgetln(in_fp,&len) )) {
		cmd_t* c = commands;
		cmd_line[len] = 0;
		g_strchomp(cmd_line);

		for (;c->txt;c++) {
			if ( strcasestr(cmd_line, c->txt) == cmd_line ) {
				char** params = g_strsplit(cmd_line, " ", c->args_taken+1);
				char* err = NULL;
				char* str = c->cb(params,&err);

				if (err) {
					fprintf(stdout, "Error: %s\n", err);
					g_free(err);
				} else {
					fprintf(stdout, "%s\n", str);
					g_free(str);
				}

				g_strfreev(params);
				return TRUE;
			}
		}

		fprintf(stdout, "Error: no such command %s\n", cmd_line);
		return TRUE;
	} else {
		return FALSE;
	}
}

static char* run_cmd(char** pars, char** err _U_) {
	FILE* fp = fopen(pars[1],"r");
	while(invoke_cmd(fp)) { ; }
	fclose(fp);
	return NULL;
}


int got_param = 0;


int main(int argc _U_, char** argv _U_) {
	struct timeval tv;
	int tot_cycles = 0;
	echld_init_t init = {ECHLD_ENCODING_JSON,argv[0],main,NULL,NULL,NULL,NULL,NULL,NULL};


	tv.tv_sec = 5;
	tv.tv_usec = 0;

	echld_set_parent_dbg_level(5);

	echld_initialize(&init);

	do {
		fd_set rfds;
		fd_set efds;
		int nfds;

		FD_ZERO(&rfds);
		FD_ZERO(&efds);
		FD_SET(0,&rfds);
		FD_SET(0,&efds);

		nfds = echld_select(FD_SETSIZE, &rfds, NULL, &efds, &tv);

		if (FD_ISSET(0,&rfds)) {
			invoke_cmd(stdin);
		}

		tot_cycles++;
	} while( keep_going );

	fprintf(stderr, "Done: tot_cycles=%d\n", tot_cycles );

	echld_terminate();
	return 0;
}

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 8
 * tab-width: 8
 * indent-tabs-mode: t
 * End:
 *
 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
 * :indentSize=8:tabSize=8:noTabs=false:
 */