HACK: pinfo->private_data points to smb_info again
[wireshark-wip.git] / echld_test.c
blob5c5317f2b1abbb185b11d548cacfa5a9bf85e0d1
1 /* echld-test.c
2 * basic test framework for echld
4 * $Id$
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * Copyright (c) 2013 by Luis Ontanon <luis@ontanon.org>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "config.h"
29 #ifdef HAVE_FCNTL_H
30 #include <fcntl.h>
31 #endif
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
35 #endif
37 #include <sys/time.h>
38 #include <sys/uio.h>
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
44 #include <signal.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
49 #include <glib.h>
50 #include <glib/gprintf.h>
52 #include "echld/echld.h"
53 #include "echld/echld-util.h"
55 #include "epan/epan.h"
56 #include "wsutil/str_util.h"
58 typedef char* (*cmd_cb_t)(char** params, char** err);
60 typedef struct _cmd_t {
61 const char* txt;
62 cmd_cb_t cb;
63 int args_taken;
64 const char* help;
65 } cmd_t;
68 #define MAX_PARAMSETS 16
70 static enc_msg_t* paramsets[MAX_PARAMSETS] = {
71 NULL,NULL,NULL,NULL, NULL,NULL,NULL,NULL,
72 NULL,NULL,NULL,NULL, NULL,NULL,NULL,NULL
75 static int nps = 0;
77 static char* ps_cmd(char** pars _U_, char** err _U_) {
78 int n_id = nps++;
80 if (n_id >= MAX_PARAMSETS) {
81 *err = g_strdup("Max Num of Paramsets reached");
82 return NULL;
85 paramsets[n_id] = echld_new_child_params();
87 return g_strdup_printf("New Paramset ps_id=%d",n_id);
90 static char* psadd_cmd(char** params _U_, char** err) {
91 int ps_id = (int) strtol(params[1], NULL, 10);
93 if (ps_id >= nps) {
94 *err = g_strdup_printf("No paramset pd_is=%d",ps_id);
95 return NULL;
98 echld_new_child_params_add_params(paramsets[ps_id], params[2], params[3], NULL);
100 return g_strdup_printf("PSAdd ps_id=%d %s='%s'", ps_id, params[2], params[3]);
103 static char* psmerge_cmd(char** params, char** err) {
104 int ps1_id = (int) strtol(params[1], NULL, 10);
105 int ps2_id = (int) strtol(params[2], NULL, 10);
106 int n_id;
108 if (ps1_id >= nps) {
109 *err = g_strdup_printf("No paramset pd_is=%d",ps1_id);
110 return NULL;
113 if (ps2_id >= nps) {
114 *err = g_strdup_printf("No paramset pd_is=%d",ps2_id);
115 return NULL;
118 n_id = nps++;
120 if (n_id >= MAX_PARAMSETS) {
121 *err = g_strdup_printf("Max Num of Paramsets reached");
122 return NULL;
125 paramsets[n_id] = echld_new_child_params_merge(paramsets[ps1_id],paramsets[ps2_id]);
127 return g_strdup_printf("Merged Paramset ps1_id=%d ps2_id=%d ps_id=%d",ps1_id, ps2_id, n_id);
130 static char* new_child_cmd(char** params, char** err) {
131 int ps_id = (int) strtol(params[1], NULL, 10);
132 int child;
134 if (ps_id >= nps) {
135 *err = g_strdup_printf("No paramset pd_is=%d",ps_id);
136 return NULL;
139 child = echld_new(paramsets[ps_id],NULL);
141 if (child <= 0) {
142 *err = g_strdup("No child\n");
143 return NULL;
146 return g_strdup_printf("New chld_id=%d\n",child);;
150 void ping_cb(long usec, void* data) {
151 int ping_id = *((int*)data);
153 if (usec >= 0) {
154 fprintf(stdout, "Ping ping_id=%d returned in %dus\n",ping_id,(int)usec);
155 } else {
156 fprintf(stdout, "Ping ping_id=%d erored\n",ping_id);
159 g_free(data);
163 static char* ping_cmd(char** params, char** err) {
164 int child = (int) strtol(params[1], NULL, 10);
165 static int ping_id = 0;
166 int* ping_data = g_new(int,1);
168 *ping_data = ping_id++;
170 if (!echld_ping(child,ping_cb,ping_data)) {
171 *err = g_strdup_printf("Could not send ping child=%d",child);
172 return NULL;
173 } else {
174 return g_strdup_printf("Ping sent child=%d",child);
178 void param_cb(const char* param, const char* value, const char* error, void* data _U_) {
179 if (error) {
180 fprintf(stdout, "Param Set Error msg=%s\n", error );
181 } else {
182 fprintf(stdout, "Param: param='%s' val='%s'\n", param, value );
186 static char* set_cmd(char** params, char** err) {
187 int child = (int) strtol(params[1], NULL, 10);
188 char* param = params[2];
189 char* value = params[3];
191 if ( ! echld_set_param(child,param,value,param_cb,NULL) ) {
192 *err = g_strdup_printf("Failed to SET child=%d param='%s' value='%s'",child,param,value);
193 return NULL;
194 } else {
195 return g_strdup_printf("Set command sent child=%d param='%s' value='%s'",child,param,value);
199 static char* get_cmd(char** params, char** err) {
200 int child = (int) strtol(params[1], NULL, 10);
201 char* param = params[2];
203 if ( ! echld_get_param(child,param,param_cb,NULL) ) {
204 *err = g_strdup_printf("Failed to GET child=%d param='%s'",child,param);
205 return NULL;
206 } else {
207 return g_strdup_printf("Get command sent child=%d param='%s'",child,param);
211 static void close_cb(const char* error, void* data) {
212 if (error) {
213 fprintf(stdout, "Close Error msg=%s\n", error );
214 } else {
215 fprintf(stdout, "Closed: child=%d\n", *((int*)data) );
219 static char* close_cmd(char** params, char** err) {
220 int child = (int) strtol(params[1], NULL, 10);
221 int* cmdp = g_new(int,1);
222 *cmdp = child;
223 if ( ! echld_close(child,close_cb,cmdp) ) {
224 *err = g_strdup_printf("Could not close child=%d",child);
225 return NULL;
226 } else {
227 return g_strdup_printf("CLose command sent child=%d",child);
230 int keep_going = 1;
232 static char* quit_cmd(char** params _U_, char** err _U_) {
233 keep_going = 0;
234 return g_strdup("Quitting");
237 static char* help_cmd(char**, char**);
239 static char* open_file_cmd(char** pars _U_, char** err _U_) {
240 *err = g_strdup("Not Implemented");
241 return NULL;
244 static char* prepare_capture_cmd(char** pars _U_, char** err _U_) {
245 *err = g_strdup("Not Implemented");
246 return NULL;
249 static char* start_capture_cmd(char** pars _U_, char** err _U_) {
250 *err = g_strdup("Not Implemented");
251 return NULL;
254 static char* get_sum_cmd(char** pars _U_, char** err _U_) {
255 *err = g_strdup("Not Implemented");
256 return NULL;
259 static char* get_tree_cmd(char** pars _U_, char** err _U_) {
260 *err = g_strdup("Not Implemented");
261 return NULL;
264 static char* get_buf_cmd(char** pars _U_, char** err _U_) {
265 *err = g_strdup("Not Implemented");
266 return NULL;
269 static char* stop_cmd(char** pars _U_, char** err _U_) {
270 *err = g_strdup("Not Implemented");
271 return NULL;
274 static char* note_cmd(char** pars _U_, char** err _U_) {
275 *err = g_strdup("Not Implemented");
276 return NULL;
279 static char* apply_cmd(char** pars _U_, char** err _U_) {
280 *err = g_strdup("Not Implemented");
281 return NULL;
284 static char* save_cmd(char** pars _U_, char** err _U_) {
285 *err = g_strdup("Not Implemented");
286 return NULL;
289 static char* run_cmd(char** pars, char** err _U_);
294 cmd_t commands[] = {
295 { "QUIT", quit_cmd, 0, "QUIT"},
296 { "HELP", help_cmd, 0, "HELP"},
297 { "RUN", run_cmd, 1, "RUN filename"},
298 { "PS", ps_cmd, 1, "PS [dummy]"},
299 { "PSADD", psadd_cmd, 4, "PSADD ps_id param value"},
300 { "PSMERGE", psmerge_cmd, 4, "PSMERGE ps_id ps_id"},
301 { "NEW", new_child_cmd, 1, "NEW ps_id"},
302 { "PING", ping_cmd, 1, "PING child_id"},
303 { "SET", set_cmd, 3, "SET child_id param_name param_val"},
304 { "GET", get_cmd, 2, "GET child_id param_name"},
305 { "CLOSE", close_cmd, 1, "CLOSE child_id"},
306 { "FILE", open_file_cmd, 2, "FILE child_id filename"},
307 { "PREP", prepare_capture_cmd, 2, "PREP child_id intfname params"},
308 { "START",start_capture_cmd,1,"START child_id"},
309 { "SUM", get_sum_cmd,2,"SUM child_id packet_range"},
310 { "TREE", get_tree_cmd,2,"TREE child_id packet_range"},
311 { "BUFF", get_buf_cmd,2,"BUFF child_id buffer_name"},
312 { "STOP", stop_cmd,1,"STOP child_id"},
313 { "NOTE", note_cmd,1,"NOTE child_id framenum note..."},
314 { "APPLY", apply_cmd,1,"APPLY child_id dfilter"},
315 { "SAVE", save_cmd,1,"SAVE child_id filename params"},
316 { NULL, NULL, 0, NULL }
319 static char* help_cmd(char** params _U_, char** err _U_) {
320 GString* out = g_string_new("Commands:\n");
321 cmd_t* c = commands;
322 char* s;
324 for (;c->txt;c++) {
325 g_string_append_printf(out,"%s\n",c->help);
327 s = out->str;
328 g_string_free(out,FALSE);
329 return s;
333 static int invoke_cmd(FILE* in_fp) {
334 size_t len;
335 char* cmd_line;
337 if(( cmd_line = fgetln(in_fp,&len) )) {
338 cmd_t* c = commands;
339 cmd_line[len] = 0;
340 g_strchomp(cmd_line);
342 for (;c->txt;c++) {
343 if ( strcasestr(cmd_line, c->txt) == cmd_line ) {
344 char** params = g_strsplit(cmd_line, " ", c->args_taken+1);
345 char* err = NULL;
346 char* str = c->cb(params,&err);
348 if (err) {
349 fprintf(stdout, "Error: %s\n", err);
350 g_free(err);
351 } else {
352 fprintf(stdout, "%s\n", str);
353 g_free(str);
356 g_strfreev(params);
357 return TRUE;
361 fprintf(stdout, "Error: no such command %s\n", cmd_line);
362 return TRUE;
363 } else {
364 return FALSE;
368 static char* run_cmd(char** pars, char** err _U_) {
369 FILE* fp = fopen(pars[1],"r");
370 while(invoke_cmd(fp)) { ; }
371 fclose(fp);
372 return NULL;
376 int got_param = 0;
379 int main(int argc _U_, char** argv _U_) {
380 struct timeval tv;
381 int tot_cycles = 0;
382 echld_init_t init = {ECHLD_ENCODING_JSON,argv[0],main,NULL,NULL,NULL,NULL};
385 tv.tv_sec = 5;
386 tv.tv_usec = 0;
388 echld_set_parent_dbg_level(5);
390 echld_initialize(&init);
392 do {
393 fd_set rfds;
394 fd_set efds;
395 int nfds;
397 FD_ZERO(&rfds);
398 FD_ZERO(&efds);
399 FD_SET(0,&rfds);
400 FD_SET(0,&efds);
402 nfds = echld_select(FD_SETSIZE, &rfds, NULL, &efds, &tv);
404 if (FD_ISSET(0,&rfds)) {
405 invoke_cmd(stdin);
408 tot_cycles++;
409 } while( keep_going );
411 fprintf(stderr, "Done: tot_cycles=%d\n", tot_cycles );
413 echld_terminate();
414 return 0;