From a3c320fd757566a3a8dd58d054ad984fd104445c Mon Sep 17 00:00:00 2001 From: Luis Ontanon Date: Sat, 29 Jun 2013 01:16:48 +0000 Subject: One further iteration, get things right, put placeholders for things to do. svn path=/trunk/; revision=50227 --- echld/child.c | 332 ++++++++++++++++++++++++++++------------------------- echld/common.c | 1 + echld/dispatcher.c | 9 +- echld/echld-int.h | 1 + echld/echld.h | 1 + 5 files changed, 185 insertions(+), 159 deletions(-) (limited to 'echld') diff --git a/echld/child.c b/echld/child.c index 1823cd2c24..19a04f40e4 100644 --- a/echld/child.c +++ b/echld/child.c @@ -109,8 +109,8 @@ static echld_bool_t param_set_dbg_level(char* val , char** err ) { static long dbg_resp(GByteArray* em, echld_msg_type_t t) { long st = echld_write_frame(child.fds.pipe_to_parent, em, child.chld_id, t, child.reqh_id, NULL); - child_debug(1, "SND fd=%d ch=%d ty='%c' rh=%d msg='%s'", - child.fds.pipe_to_parent, child.chld_id, t, child.reqh_id, (st>0?"ok":strerror(errno)) ); + child_debug(1, "SND fd=%d ch=%d ty='%s' rh=%d msg='%s'", + child.fds.pipe_to_parent, child.chld_id, TY(t), child.reqh_id, (st>0?"ok":strerror(errno)) ); return st; } @@ -176,100 +176,6 @@ void child_err(int e, unsigned reqh_id, const char* fmt, ...) { } -static char* intflist2json(GList* if_list) { - /* blatantly stolen from print_machine_readable_interfaces in dumpcap.c */ -#define ADDRSTRLEN 46 /* Covers IPv4 & IPv6 */ - - int i; - GList *if_entry; - if_info_t *if_info; - GSList *addr; - if_addr_t *if_addr; - char addr_str[ADDRSTRLEN]; - GString *str = g_string_new("={ "); - char* s; - - i = 1; /* Interface id number */ - for (if_entry = g_list_first(if_list); if_entry != NULL; - if_entry = g_list_next(if_entry)) { - if_info = (if_info_t *)if_entry->data; - g_string_append_printf(str,"%d={ intf='%s',", i++, if_info->name); - - /* - * Print the contents of the if_entry struct in a parseable format. - * Each if_entry element is tab-separated. Addresses are comma- - * separated. - */ - /* XXX - Make sure our description doesn't contain a tab */ - if (if_info->vendor_description != NULL) - g_string_append_printf(str," vnd_desc='%s',", if_info->vendor_description); - - /* XXX - Make sure our friendly name doesn't contain a tab */ - if (if_info->friendly_name != NULL) - g_string_append_printf(str," name='%s', addrs=[ ", if_info->friendly_name); - - for (addr = g_slist_nth(if_info->addrs, 0); addr != NULL; - addr = g_slist_next(addr)) { - - if_addr = (if_addr_t *)addr->data; - switch(if_addr->ifat_type) { - case IF_AT_IPv4: - if (inet_ntop(AF_INET, &if_addr->addr.ip4_addr, addr_str, - ADDRSTRLEN)) { - g_string_append_printf(str,"%s", addr_str); - } else { - g_string_append(str,""); - } - break; - case IF_AT_IPv6: - if (inet_ntop(AF_INET6, &if_addr->addr.ip6_addr, - addr_str, ADDRSTRLEN)) { - g_string_append_printf(str,"%s", addr_str); - } else { - g_string_append(str,""); - } - break; - default: - g_string_append_printf(str,"", if_addr->ifat_type); - } - } - - g_string_append(str," ]"); /* addrs */ - - - if (if_info->loopback) - g_string_append(str,", loopback=1"); - else - g_string_append(str,", loopback=0"); - - g_string_append(str,"}, "); - } - - g_string_truncate(str,str->len - 2); /* the comma and space */ - g_string_append(str,"}"); - - s=str->str; - g_string_free(str,FALSE); - return s; -} - -static void child_start_interface_listing(void) { - -} - -static gboolean child_open_file(int chld_id _U_, int reqh_id _U_, const char* filename, guint8* buff, int buff_len) { - const char* reason = "Unimplemented"; // this ain't a good reason to fail! - g_snprintf(buff,buff_len,"Cannot open file=%s reason=%s",filename,reason); - return FALSE; -} - -static gboolean child_open_interface(int chld_id _U_, int reqh_id _U_, const char* intf_name, const char* params _U_, guint8* err_buf, int errbuff_len) { - const char* reason = "Unimplemented"; // this ain't a good reason to fail! - g_snprintf(err_buf,errbuff_len,"Cannot open interface=%s reason=%s",intf_name,reason); - return FALSE; -} - - static char* param_get_cwd(char** err ) { char* pwd = getcwd(NULL, 128); @@ -282,7 +188,7 @@ static char* param_get_cwd(char** err ) { static echld_bool_t param_set_cwd(char* val , char** err ) { /* XXX SANITIZE */ if (chdir(val) != 0) { - *err = g_strdup_printf("(%d)'%s'",errno,strerror(errno)); + *err = g_strdup_printf("cannot chdir reas='%s'",strerror(errno)); return FALSE; } @@ -327,6 +233,64 @@ static param_t* get_paramset(char* name) { } + + + + +static void child_open_file(char* filename) { + CHILD_DBG((2,"CMD open file filename='%s'",filename)); + child_err(ECHLD_ERR_UNIMPLEMENTED,child.reqh_id,"open file not implemented yet!"); + child.state = READING; +} + +static void child_open_interface(char* intf, char* pars) { + CHILD_DBG((2,"CMD open interface intf='%s', params='%s'",intf,pars)); + child_err(ECHLD_ERR_UNIMPLEMENTED,child.reqh_id,"open interface not implemented yet!"); + child.state = READY; +} + +static void child_start_capture(void) { + CHILD_DBG((2,"CMD start capture")); + child_err(ECHLD_ERR_UNIMPLEMENTED,child.reqh_id,"start capture not implemented yet!"); + child.state = CAPTURING; +} + +static void child_stop_capure(void) { + CHILD_DBG((2,"CMD stop capture")); + child_err(ECHLD_ERR_UNIMPLEMENTED,child.reqh_id,"stop capture not implemented yet!"); + child.state = DONE; +} + +static void child_get_summary(char* range) { + CHILD_DBG((2,"CMD get summary range='%s'",range)); + child_err(ECHLD_ERR_UNIMPLEMENTED,child.reqh_id,"get_summary not implemented yet!"); +} + +static void child_get_tree(char* range) { + CHILD_DBG((2,"CMD get tree range='%s'",range)); + child_err(ECHLD_ERR_UNIMPLEMENTED,child.reqh_id,"get_tree not implemented yet!"); +} + +static void child_get_buffer(char* name) { + CHILD_DBG((2,"CMD get buffer name='%s'",name)); + child_err(ECHLD_ERR_UNIMPLEMENTED,child.reqh_id,"get_buffer not implemented yet!"); +} + +static void child_add_note(int framenum, char* note) { + CHILD_DBG((2,"CMD add note framenum=%d note='%s'",framenum,note)); + child_err(ECHLD_ERR_UNIMPLEMENTED,child.reqh_id,"add_note not implemented yet!"); +} + +static void child_apply_filter(char* filter) { + CHILD_DBG((2,"CMD apply filter filter='%s'",filter)); + child_err(ECHLD_ERR_UNIMPLEMENTED,child.reqh_id,"apply_filter not implemented yet!"); +} + +static void child_save_file(char* filename, char* pars) { + CHILD_DBG((2,"CMD save file filename='%s' params='%s'",filename,pars)); + child_err(ECHLD_ERR_UNIMPLEMENTED,child.reqh_id,"save_file not implemented yet!"); +} + static long child_receive(guint8* b, size_t len, echld_chld_id_t chld_id, echld_msg_type_t type, echld_reqh_id_t reqh_id, void* data _U_) { GByteArray ba; GByteArray* gba; @@ -337,28 +301,18 @@ static long child_receive(guint8* b, size_t len, echld_chld_id_t chld_id, echld_ // gettimeofday(&(child.now), NULL); - if (child.chld_id != chld_id) { - if (child.chld_id == 0) { - if ( type == ECHLD_NEW_CHILD) { - child.chld_id = chld_id; - // more init needed for sure - CHILD_DBG((1,"chld_id set, sending HELLO")); - CHILD_RESP(NULL,ECHLD_HELLO); - return 0; - } else { - child_err(ECHLD_ERR_WRONG_MSG,reqh_id, - "not yet initialized: chld_id:%d msg_type='%c'",chld_id,type); - return 0; - } - } - + if (child.chld_id != chld_id) { child_err(ECHLD_ERR_WRONG_MSG,reqh_id, - "chld_id: own:%d given:%d msg_type='%c'",child.chld_id,chld_id,type); + "chld_id: own:%d given:%d msg_type='%s'",child.chld_id,chld_id,TY(type)); return 0; } - switch(type) { + case ECHLD_NEW_CHILD: { + child.state = IDLE; + CHILD_RESP(NULL,ECHLD_HELLO); + break; + } case ECHLD_PING: ba.data = b; ba.len = (guint)len; @@ -439,28 +393,116 @@ static long child_receive(guint8* b, size_t len, echld_chld_id_t chld_id, echld_ CHILD_DBG((1,"Bye")); exit(0); break; - case ECHLD_OPEN_INTERFACE: - case ECHLD_OPEN_FILE: + case ECHLD_OPEN_FILE: { + char* filename; if (child.state != IDLE) goto wrong_state; - goto not_implemented; - case ECHLD_START_CAPTURE: - if (child.state != READY) goto wrong_state; - goto not_implemented; - case ECHLD_STOP_CAPTURE: + + if ( child.dec->open_file(b,len,&filename) ) { + child_open_file(filename); + } else { + child_err(ECHLD_DECODE_ERROR,reqh_id,"cannot decode open_file"); + } + break; + } + case ECHLD_OPEN_INTERFACE: { + char* intf; + char* pars; + if (child.state != IDLE) goto wrong_state; - goto not_implemented; - case ECHLD_GET_SUM: - case ECHLD_GET_TREE: - case ECHLD_GET_BUFFER: - if (child.state != READING && child.state != CAPTURING ) goto wrong_state; - goto not_implemented; - case ECHLD_ADD_NOTE: - case ECHLD_APPLY_FILTER: - case ECHLD_SAVE_FILE: - if (child.state != DONE ) goto wrong_state; - goto not_implemented; + + if ( child.dec->open_interface(b,len,&intf,&pars) ) { + child_open_interface(intf,pars); + } else { + child_err(ECHLD_DECODE_ERROR,reqh_id, "cannot decode open_interface"); + } + break; + } + case ECHLD_START_CAPTURE:{ + if (child.state != READY) goto wrong_state; + child_start_capture(); + break; + } + case ECHLD_STOP_CAPTURE: { + if (child.state != CAPTURING) goto wrong_state; + child_stop_capure(); + break; + } + case ECHLD_GET_SUM: { + char* range; + + if (child.state != CAPTURING || child.state != READING || child.state != DONE) goto wrong_state; + + if ( child.dec->get_sum(b,len,&range) ) { + child_get_summary(range); + } else { + child_err(ECHLD_DECODE_ERROR,reqh_id,"cannot decode get_summary"); + } + break; + } + case ECHLD_GET_TREE:{ + char* range; + + if (child.state != CAPTURING && child.state != READING && child.state != DONE) goto wrong_state; + + if ( child.dec->get_tree(b,len,&range) ) { + child_get_tree(range); + } else { + child_err(ECHLD_DECODE_ERROR,reqh_id,"cannot decode get_tree"); + } + break; + } + case ECHLD_GET_BUFFER:{ + char* name; + + if (child.state != CAPTURING && child.state != READING && child.state != DONE) goto wrong_state; + + if ( child.dec->get_buffer(b,len,&name) ) { + child_get_buffer(name); + } else { + child_err(ECHLD_DECODE_ERROR,reqh_id,"cannot decode get_buffer"); + } + break; + } + case ECHLD_ADD_NOTE: { + int framenum; + char* note; + + if (child.state != CAPTURING && child.state != READING && child.state != DONE) goto wrong_state; + + if ( child.dec->add_note(b,len,&framenum,¬e) ) { + child_add_note(framenum,note); + } else { + child_err(ECHLD_DECODE_ERROR,reqh_id,"cannot decode get_buffer"); + } + break; + } + case ECHLD_APPLY_FILTER: { + char* filter; + + if (child.state != DONE) goto wrong_state; + + if ( child.dec->apply_filter(b,len,&filter) ) { + child_apply_filter(filter); + } else { + child_err(ECHLD_DECODE_ERROR,reqh_id,"cannot decode apply_filter"); + } + break; + } + case ECHLD_SAVE_FILE: { + char* filename; + char* pars; + + if (child.state != DONE) goto wrong_state; + + if ( child.dec->save_file(b,len,&filename,&pars) ) { + child_save_file(filename,pars); + } else { + child_err(ECHLD_DECODE_ERROR,reqh_id,"cannot decode save_file"); + } + break; + } default: - child_err(ECHLD_ERR_WRONG_MSG,reqh_id,"chld_id=%d msg_type='%c'",chld_id,type); + child_err(ECHLD_ERR_WRONG_MSG,reqh_id,"wrong message: chld_id=%d msg_type='%s'",chld_id,TY(type)); break; } @@ -468,15 +510,11 @@ static long child_receive(guint8* b, size_t len, echld_chld_id_t chld_id, echld_ misencoded: // dump the misencoded message (b,blen) - child_err(ECHLD_ERR_WRONG_MSG,reqh_id,"misencoded msg msg_type='%c'",type); + child_err(ECHLD_ERR_WRONG_MSG,reqh_id,"misencoded msg msg_type='%s'",TY(type); return 0; wrong_state: - child_err(ECHLD_ERR_WRONG_MSG,reqh_id,"unexpected message: received in wrong state='%c', msg_type='%c'",child.state,type); - return 0; - - not_implemented: - child_err(ECHLD_ERR_UNIMPLEMENTED,reqh_id,"unimplemented message: received in wrong state='%c', msg_type='%c'",child.state,type); + child_err(ECHLD_ERR_WRONG_MSG,reqh_id,"unexpected message: received in wrong state='%s', msg_type='%s'",ST(child.state),TY(type)); return 0; } @@ -488,11 +526,6 @@ static void child_dumpcap_read(void) { CHILD_DBG((2,"child_dumpcap_read")); } -static void child_read_file(void) { - // this folk manages the reading of the file after open file has opened it - CHILD_DBG((2,"child_read_file")); -} - int echld_child_loop(void) { int disp_from = child.parent.fd; int disp_to = child.fds.pipe_to_parent; @@ -562,10 +595,6 @@ int echld_child_loop(void) { if (child.fds.pipe_from_dumpcap > 0 && FD_ISSET(child.fds.pipe_from_dumpcap,&rfds) ) { child_dumpcap_read(); } - - if (child.fds.file_being_read > 0 && FD_ISSET(child.fds.pipe_from_dumpcap,&rfds) ) { - child_read_file(); - } } while(1); @@ -575,10 +604,3 @@ int echld_child_loop(void) { } -extern void echld_unused(void) { - intflist2json(NULL); - child_start_interface_listing(); - child_open_file(0, 0, NULL, NULL, 0); - child_open_interface(0, 0, NULL, NULL, NULL, 0); -} - diff --git a/echld/common.c b/echld/common.c index 20fbe874fc..7eee1817a3 100644 --- a/echld/common.c +++ b/echld/common.c @@ -515,6 +515,7 @@ static child_decoder_t child_decoder = { x2str_dec, str_dec, str_dec, + str_dec, int_str_dec, str_dec, x2str_dec diff --git a/echld/dispatcher.c b/echld/dispatcher.c index 6357c2d49e..e300c06c1d 100644 --- a/echld/dispatcher.c +++ b/echld/dispatcher.c @@ -121,13 +121,13 @@ static long dbg_r = 0; #define DISP_DBG_INIT() do { debug_fp = stderr; DCOM(); } while(0) #define DISP_DBG_START(fname) do { debug_fp = fopen(fname,"a"); DCOM(); DISP_DBG((0,"Log Started")); } while(0) #define DISP_WRITE(FD,BA,CH,T,RH) ( dbg_r = echld_write_frame(FD,BA,CH,T,RH,NULL), DISP_DBG((1,"SND fd=%d ch=%d ty='%s' rh=%d msg='%s'",FD,CH,TY(T),RH, (dbg_r>0?"ok":strerror(errno)))), dbg_r ) -#define CHLD_SET_STATE(c,st) DISP_DBG((1,"Child[%d] State %s => %s",(c)->chld_id,ST((c)->state),ST((c)->state=(st)) )) +#define CHLD_SET_STATE(c,st) do { DISP_DBG((1,"Child[%d] State %s => %s",(c)->chld_id, ST((c)->state), ST((st)) )); (c)->state=(st); } while(0) #else #define DISP_DBG(attrs) #define DISP_DBG_INIT() #define DISP_DBG_START(fname) #define DISP_WRITE(FD,BA,CH,T,RH) echld_write_frame(FD,BA,CH,T,RH,NULL) -#define CHLD_SET_STATE(c,st) ((c)->state=(st)) +#define CHLD_SET_STATE(c,st) ((c)->state = (st)) #endif #define DISP_RESP(B,T) (DISP_WRITE( dispatcher->parent_out, (B), 0, (T), dispatcher->reqh_id)) @@ -595,11 +595,12 @@ static void detach_new_child(enc_msg_t* em, echld_chld_id_t chld_id) { c->write_fd = pipe_to_child; c->pid = pid; c->chld_id = chld_id; - c->state = CREATING; c->closing = FALSE; + CHLD_SET_STATE(c,CREATING); + DISP_DBG((4,"Child Forked pid=%d chld_id=%d from_fd=%d to_fd=%d", - pid, c->chld_id, pipe_from_child, pipe_to_child)); + pid, c->chld_id, pipe_from_child, pipe_to_child)); start_wait_time.tv_sec = (int)(start_wait_time_us / 1000000); start_wait_time.tv_usec = (int)(start_wait_time_us % 1000000); diff --git a/echld/echld-int.h b/echld/echld-int.h index 5cdbef2a3a..1c5e563915 100644 --- a/echld/echld-int.h +++ b/echld/echld-int.h @@ -158,6 +158,7 @@ typedef struct _child_in { echld_bool_t (*open_interface) (guint8*, size_t, char** intf_name, char** params); echld_bool_t (*get_sum) (guint8*, size_t, char** range); echld_bool_t (*get_tree) (guint8*, size_t, char** range); + echld_bool_t (*get_buffer) (guint8*, size_t, char** name); echld_bool_t (*add_note) (guint8*, size_t, int* packet_number, char** note); echld_bool_t (*apply_filter) (guint8*, size_t, char** filter); echld_bool_t (*save_file) (guint8*, size_t, char** filename, char** params); diff --git a/echld/echld.h b/echld/echld.h index 23c8f24f7c..551402cb15 100644 --- a/echld/echld.h +++ b/echld/echld.h @@ -377,6 +377,7 @@ enum _echld_error { ECHLD_CANNOT_SET_PARAM, ECHLD_CANNOT_GET_PARAM, ECHLD_ERR_CRASHED_CHILD, + ECHLD_DECODE_ERROR, ECHLD_ERR_OTHER }; -- cgit v1.2.1