/*if dealed return 0, else return -1*/ int process_deal(CLT_T* pclt){ struct server_data *psd = NULL; struct list_head* pcmd_head = NULL; struct cmd_info* pcmd_info = NULL; psd = get_server(); if(psd==NULL || pclt==NULL){ return -1; } pcmd_head = &psd->sd_cmdinfo_head; post_server(); /*match method*/ int i; /*select and deal*/ cmd_select(pclt, pcmd_head); /*clean client recive buffer*/ pclt->ci_rlen = 0; bzero(pclt->ci_rbuf, sizeof(pclt->ci_rbuf)); #if 0 //old deal method for(i=0;i<MAX_METHOD;i++){ PM_T* ppm = ppm_tab[i]; if(NULL == ppm) continue; size_t hlen = strlen(ppm->pm_header); if(!strncmp(ppm->pm_header,pclt->ci_rbuf,hlen)){ method_fresh(ppm); if(ppm->pm_init && ppm->pm_stat==PMS_UNINIT){ ppm->pm_init(ppm->pm_ctrl_file); ppm_tab[i]->pm_stat = PMS_INITED; } if(ppm->pm_deal){ if(!sem_trywait(&pclt->ci_sem)){ printf("[%s][%s]\n",__func__,ppm->pm_header); pclt->ci_rlen -= hlen; memcpy(pclt->ci_rbuf,pclt->ci_rbuf+hlen,pclt->ci_rlen); sem_post(&pclt->ci_sem); printf("[process_deal][ID][%s]\n",pclt->ci_rbuf); ppm->pm_deal(pclt); return 0; } } } } #endif if(!sem_trywait(&pclt->ci_sem)){ sprintf(pclt->ci_wbuf, "ERROR DATA"); pclt->ci_wlen = strlen(pclt->ci_wbuf); sem_post(&pclt->ci_sem); } return -1; }
int do_net(int argc, char **argv) { return cmd_select(cmds, argc, argv, do_help); }
int main(int argc, char **argv) { static const struct option options[] = { { "json", no_argument, NULL, 'j' }, { "help", no_argument, NULL, 'h' }, { "pretty", no_argument, NULL, 'p' }, { "version", no_argument, NULL, 'V' }, { "bpffs", no_argument, NULL, 'f' }, { "mapcompat", no_argument, NULL, 'm' }, { "nomount", no_argument, NULL, 'n' }, { 0 } }; int opt, ret; last_do_help = do_help; pretty_output = false; json_output = false; show_pinned = false; block_mount = false; bin_name = argv[0]; hash_init(prog_table.table); hash_init(map_table.table); opterr = 0; while ((opt = getopt_long(argc, argv, "Vhpjfmn", options, NULL)) >= 0) { switch (opt) { case 'V': return do_version(argc, argv); case 'h': return do_help(argc, argv); case 'p': pretty_output = true; /* fall through */ case 'j': if (!json_output) { json_wtr = jsonw_new(stdout); if (!json_wtr) { p_err("failed to create JSON writer"); return -1; } json_output = true; } jsonw_pretty(json_wtr, pretty_output); break; case 'f': show_pinned = true; break; case 'm': bpf_flags = MAPS_RELAX_COMPAT; break; case 'n': block_mount = true; break; default: p_err("unrecognized option '%s'", argv[optind - 1]); if (json_output) clean_and_exit(-1); else usage(); } } argc -= optind; argv += optind; if (argc < 0) usage(); ret = cmd_select(cmds, argc, argv, do_help); if (json_output) jsonw_destroy(&json_wtr); if (show_pinned) { delete_pinned_obj_table(&prog_table); delete_pinned_obj_table(&map_table); } return ret; }
static int do_batch(int argc, char **argv) { char buf[BATCH_LINE_LEN_MAX], contline[BATCH_LINE_LEN_MAX]; char *n_argv[BATCH_ARG_NB_MAX]; unsigned int lines = 0; int n_argc; FILE *fp; char *cp; int err; int i; if (argc < 2) { p_err("too few parameters for batch"); return -1; } else if (!is_prefix(*argv, "file")) { p_err("expected 'file', got: %s", *argv); return -1; } else if (argc > 2) { p_err("too many parameters for batch"); return -1; } NEXT_ARG(); if (!strcmp(*argv, "-")) fp = stdin; else fp = fopen(*argv, "r"); if (!fp) { p_err("Can't open file (%s): %s", *argv, strerror(errno)); return -1; } if (json_output) jsonw_start_array(json_wtr); while (fgets(buf, sizeof(buf), fp)) { cp = strchr(buf, '#'); if (cp) *cp = '\0'; if (strlen(buf) == sizeof(buf) - 1) { errno = E2BIG; break; } /* Append continuation lines if any (coming after a line ending * with '\' in the batch file). */ while ((cp = strstr(buf, "\\\n")) != NULL) { if (!fgets(contline, sizeof(contline), fp) || strlen(contline) == 0) { p_err("missing continuation line on command %d", lines); err = -1; goto err_close; } cp = strchr(contline, '#'); if (cp) *cp = '\0'; if (strlen(buf) + strlen(contline) + 1 > sizeof(buf)) { p_err("command %d is too long", lines); err = -1; goto err_close; } buf[strlen(buf) - 2] = '\0'; strcat(buf, contline); } n_argc = make_args(buf, n_argv, BATCH_ARG_NB_MAX, lines); if (!n_argc) continue; if (n_argc < 0) goto err_close; if (json_output) { jsonw_start_object(json_wtr); jsonw_name(json_wtr, "command"); jsonw_start_array(json_wtr); for (i = 0; i < n_argc; i++) jsonw_string(json_wtr, n_argv[i]); jsonw_end_array(json_wtr); jsonw_name(json_wtr, "output"); } err = cmd_select(cmds, n_argc, n_argv, do_help); if (json_output) jsonw_end_object(json_wtr); if (err) goto err_close; lines++; } if (errno && errno != ENOENT) { p_err("reading batch file failed: %s", strerror(errno)); err = -1; } else { if (!json_output) printf("processed %d commands\n", lines); err = 0; } err_close: if (fp != stdin) fclose(fp); if (json_output) jsonw_end_array(json_wtr); return err; }
/* Client command loop only considers console inputs + controller messages */ void run_client(char *infile_name) { if (!start_cmd(infile_name)) return; while (!cmd_done()) { /* Select among controller port, and connections to routers */ FD_ZERO(&cset); maxcfd = 0; add_cfd(controller_fd); cmd_select(maxcfd+1, &cset, NULL, NULL, NULL); if (cmd_done()) break; int fd; for (fd = 0; fd <= maxcfd; fd++) { if (!FD_ISSET(fd, &cset)) continue; bool eof; chunk_ptr msg = chunk_read(fd, &eof); if (eof) { /* Unexpected EOF */ if (fd == controller_fd) { err(true, "Unexpected EOF from controller (fatal)"); } else { err(false, "Unexpected EOF from unexpected source. fd %d (ignored)", fd); } close(fd); continue; } if (msg == NULL) { err(false, "Could not read chunk from fd %d (ignored)", fd); continue; } word_t h = chunk_get_word(msg, 0); unsigned code = msg_get_header_code(h); if (fd == controller_fd) { /* Message from controller */ switch(code) { case MSG_DO_FLUSH: chunk_free(msg); #if RPT >= 5 report(5, "Received flush message from controller"); #endif if (flush_helper) { flush_helper(); } break; case MSG_STAT: #if RPT >= 5 report(5, "Received summary statistics from controller"); #endif if (stat_helper) { /* Get a copy of the byte usage from mem. allocator */ stat_helper(msg); } chunk_free(msg); /* Client can proceed with next command */ unblock_console(); break; case MSG_KILL: chunk_free(msg); #if RPT >= 1 report(1, "Received kill message from controller"); #endif finish_cmd(); break; case MSG_GC_START: chunk_free(msg); gc_start(code); break; case MSG_GC_FINISH: chunk_free(msg); gc_finish(code); break; default: chunk_free(msg); err(false, "Unknown message code %u from controller (ignored)", code); } } else { err(false, "Received message from unknown source. fd %d (ignored)", fd); close(fd); continue; } } } }
static void run_controller(char *infile_name) { if (!start_cmd(infile_name)) return; while (!cmd_done()) { FD_ZERO(&set); int fd; word_t w; unsigned ip; unsigned port; add_fd(listen_fd); keyvalue_iterstart(new_conn_map); /* Check for messages from newly connected clients, workers, and routers */ while (keyvalue_iternext(new_conn_map, &w, NULL)) { fd = w; add_fd(fd); } if (need_routers == 0) { /* Accept messages from workers */ set_iterstart(worker_fd_set); while (set_iternext(worker_fd_set, &w)) { fd = w; add_fd(fd); } /* Accept messages from clients */ set_iterstart(client_fd_set); while (set_iternext(client_fd_set, &w)) { fd = w; add_fd(fd); } } cmd_select(maxfd+1, &set, NULL, NULL, NULL); for (fd = 0; fd <= maxfd; fd++) { if (!FD_ISSET(fd, &set)) continue; if (fd == listen_fd) { unsigned ip; int connfd = accept_connection(fd, &ip); keyvalue_insert(new_conn_map, (word_t) connfd, (word_t) ip); #if RPT >= 4 report(4, "Accepted new connection. Connfd = %d, IP = 0x%x", connfd, ip); #endif continue; } bool eof; chunk_ptr msg = chunk_read(fd, &eof); if (eof) { /* Unexpected EOF */ if (keyvalue_remove(new_conn_map, (word_t) fd, NULL, NULL)) { err(false, "Unexpected EOF from new connection, fd %d", fd); } else if (set_member(worker_fd_set, (word_t) fd, true)) { err(false, "Unexpected EOF from connected worker, fd %d. Shutting down", fd); /* Shut down system */ finish_cmd(); } else if (set_member(client_fd_set, (word_t) fd, true)) { #if RPT >= 3 report(3, "Disconnection from client (fd %d)", fd); #endif if (need_client_fd_set && set_member(need_client_fd_set, (word_t) fd, false)) { #if RPT >= 3 report(3, "Removing client from GC activities"); #endif handle_gc_msg(MSG_GC_FINISH, 0, fd, true); } } else { err(false, "Unexpected EOF from unknown source, fd %d", fd); } close(fd); continue; } if (msg == NULL) { err(false, "Could not read chunk from fd %d (ignored)", fd); continue; } word_t h = chunk_get_word(msg, 0); unsigned code = msg_get_header_code(h); #if RPT >= 5 report(5, "Received message with code %d from fd %d", code, fd); #endif if (keyvalue_remove(new_conn_map, (word_t) fd, NULL, &w)) { ip = w; chunk_free(msg); /* Should be a registration message */ switch (code) { case MSG_REGISTER_ROUTER: if (need_routers == 0) { err(false, "Unexpected router registration. (Ignored)"); close(fd); break; } port = msg_get_header_port(h); word_t node_id = msg_build_node_id(port, ip); set_insert(router_addr_set, node_id); set_insert(router_fd_set, (word_t) fd); #if RPT >= 4 report(4, "Added router with fd %d. IP 0x%x. Port %u", fd, ip, port); #endif need_routers --; if (need_routers == 0) { #if RPT >= 2 report(2, "All routers connected"); #endif /* Have gotten all of the necessary routers. Notify any registered workers */ set_iterstart(worker_fd_set); int wfd; while (set_iternext(worker_fd_set, &w)) { wfd = w; add_agent(wfd, false); } } break; case MSG_REGISTER_WORKER: if (worker_fd_set->nelements >= worker_cnt) { err(false, "Unexpected worker registration. (Ignored)"); close(fd); break; } set_insert(worker_fd_set, (word_t) fd); #if RPT >= 4 report(4, "Added worker with fd %d", fd); #endif if (need_routers == 0) add_agent(fd, false); break; case MSG_REGISTER_CLIENT: if (gc_state == GC_READY) { set_insert(client_fd_set, (word_t) fd); #if RPT >= 4 report(4, "Added client with fd %d", fd); #endif if (need_workers == 0) add_agent(fd, true); } else { if (!defer_client_fd_set) { defer_client_fd_set = word_set_new(); } set_insert(defer_client_fd_set, (word_t) fd); #if RPT >= 3 report(3, "Deferring client with fd %d until GC completed", fd); #endif } break; default: err(false, "Unexpected message code %u from new connection", code); break; } } else if (set_member(worker_fd_set, (word_t) fd, false)) { /* Message from worker */ switch (code) { unsigned agent; unsigned gen; case MSG_READY_WORKER: chunk_free(msg); if (need_workers == 0) { err(false, "Unexpected worker ready. (Ignored)"); close(fd); break; } need_workers--; if (need_workers == 0) { #if RPT >= 2 report(2, "All workers connected"); #endif /* Notify any pending clients */ set_iterstart(client_fd_set); int cfd; while (set_iternext(client_fd_set, &w)) { cfd = w; add_agent(cfd, true); } } break; case MSG_STAT: /* Message gets stashed away. Don't free it */ add_stat_message(msg); break; case MSG_CLIOP_ACK: /* Worker acknowledging receipt of global operation info */ agent = msg_get_header_agent(h); int client_fd = receive_global_op_worker_ack(agent); if (client_fd >= 0) { /* Have received complete set of acknowledgements. */ /* Send ack to client */ if (chunk_write(client_fd, msg)) { #if RPT >= 6 report(6, "Sent ack to client for global operation with id %u", agent); #endif } else { err(false, "Failed to send ack to client for global operation with id %u. Fd %d", agent, client_fd); } } chunk_free(msg); break; case MSG_GC_START: case MSG_GC_FINISH: handle_gc_msg(code, 0, fd, false); chunk_free(msg); break; case MSG_GC_REQUEST: gen = msg_get_header_generation(h); chunk_free(msg); handle_gc_msg(code, gen, fd, false); break; default: chunk_free(msg); err(false, "Unexpected message code %u from worker", code); } } else if (set_member(client_fd_set, (word_t) fd, false)) { /* Message from client */ switch(code){ unsigned agent; word_t w; case MSG_KILL: /* Shutdown entire system */ chunk_free(msg); #if RPT >= 2 report(2, "Remote request to kill system"); #endif finish_cmd(); return; case MSG_DO_FLUSH: /* Initiate a flush operation */ chunk_free(msg); flush_requestor_fd = fd; do_controller_flush_cmd(0, NULL); break; case MSG_CLIOP_DATA: /* Request for global operation from client */ agent = msg_get_header_agent(h); add_global_op(agent, fd); /* Send message to all workers */ set_iterstart(worker_fd_set); while (set_iternext(worker_fd_set, &w)) { int worker_fd = (int) w; if (chunk_write(worker_fd, msg)) { #if RPT >= 6 report(6, "Sent global operation information with id %u to worker with fd %d", agent, worker_fd); #endif } else { err(false, "Failed to send global operation information with id %u to worker with fd %d", agent, worker_fd); } } chunk_free(msg); break; case MSG_CLIOP_ACK: /* Completion of global operation by client */ agent = msg_get_header_agent(h); /* Send message to all workers */ set_iterstart(worker_fd_set); while (set_iternext(worker_fd_set, &w)) { int worker_fd = (int) w; if (chunk_write(worker_fd, msg)) { #if RPT >= 6 report(6, "Sent global operation acknowledgement with id %u to worker with fd %d", agent, worker_fd); #endif } else { err(false, "Failed to send global operation acknowledgement with id %u to worker with fd %d", agent, worker_fd); } } chunk_free(msg); break; case MSG_GC_START: case MSG_GC_FINISH: handle_gc_msg(code, 0, fd, true); chunk_free(msg); break; default: err(false, "Unexpected message code %u from client", code); } } else { chunk_free(msg); err(false, "Unexpected message on fd %d (Ignored)", fd); } } } }
/* Menu */ static int menu_run(void) { adv_bool done; int userkey; menu_base = 0; menu_rel = 0; menu_rel_max = MENU_DY; menu_max = crtc_container_max(&the_modes); menu_base_max = menu_max - menu_rel_max; if (menu_base_max < 0) menu_base_max = 0; done = 0; while (!done) { draw_text_index(BAR_X, BAR_Y1+1, BAR_DX); draw_text_bar(BAR_X, BAR_Y1, BAR_Y2, BAR_DX); draw_text_info(INFO_X, INFO_Y, INFO_DX, INFO_DY, menu_base + menu_rel); menu_draw(MENU_X, MENU_Y, MENU_DX, MENU_DY); video_wait_vsync(); target_idle(); os_poll(); userkey = inputb_get(); switch (userkey) { case INPUTB_UP: cmd_gotopos(menu_base + menu_rel - 1); break; case INPUTB_DOWN: cmd_gotopos(menu_base + menu_rel + 1); break; case INPUTB_HOME: { int i = menu_base + menu_rel - 1; if (i<0) i = 0; while (i>0 && !(crtc_container_pos(&the_modes, i)->user_flags & MODE_FLAGS_USER_BIT0)) --i; cmd_gotopos(i); break; } case INPUTB_END: { int i = menu_base + menu_rel + 1; if (i >= menu_max) i = menu_max - 1; while (i < menu_max - 1 && !(crtc_container_pos(&the_modes, i)->user_flags & MODE_FLAGS_USER_BIT0)) ++i; cmd_gotopos(i); break; } case INPUTB_PGDN: cmd_gotopos(menu_base + menu_rel + menu_rel_max); break; case INPUTB_PGUP: cmd_gotopos(menu_base + menu_rel - menu_rel_max); break; case INPUTB_F2: cmd_save(); break; case INPUTB_LEFT : case INPUTB_RIGHT : cmd_type(userkey); break; case INPUTB_ESC: done = cmd_exit(); break; case INPUTB_SPACE: cmd_select(); cmd_gotopos(menu_base + menu_rel + 1); break; case INPUTB_ENTER: if (cmd_onvideo_test() != 0) { text_reset(); draw_text_error(); } else { text_reset(); } break; case INPUTB_F9: if (cmd_onvideo_calib() != 0) { text_reset(); draw_text_error(); } else { text_reset(); } break; case INPUTB_F10: if (cmd_onvideo_animate() != 0) { text_reset(); draw_text_error(); } else { text_reset(); } break; case INPUTB_TAB : cmd_rename(); break; case INPUTB_F5 : if (cmd_modeline_create(1) !=0) { text_reset(); draw_text_error(); } else { text_reset(); } break; case INPUTB_F6 : if (cmd_modeline_create(0) !=0) { text_reset(); draw_text_error(); } else { text_reset(); } break; case INPUTB_F7 : cmd_copy(); break; case INPUTB_F8 : if (cmd_mode_clock() !=0) { text_reset(); draw_text_error(); } else { text_reset(); } break; case INPUTB_DEL : cmd_del(); cmd_gotopos(menu_base + menu_rel); break; case INPUTB_F1: draw_text_help(); break; default: if (cmd_offvideo_test(userkey) != 0) { draw_text_error(); } break; } } return userkey; }
int main( void ) #endif { iu8 i, len, b; /* TODO: On error? */ hal_init(); /* Send ATR */ /* TODO: Possible from EEPROM? */ hal_io_sendByteT0( 0x3B ); #if CONF_WITH_LOGGING==1 log_init(); #endif resplen = 0; #if CONF_WITH_TRANSACTIONS==1 /* Commit transactions not yet done. */ /* TODO: On error? */ ta_commit(); #endif /* CONF_WITH_TRANSACTIONS */ /* Initialize FS state in RAM. */ /* TODO: On error? */ fs_init(); #if CONF_WITH_PINAUTH==1 /* Initialize authentication state. */ /* TODO: On error? */ auth_init(); #endif /* CONF_WITH_PINAUTH==1 */ if( !(hal_eeprom_read( &len, ATR_LEN_ADDR, 1 ) && (len<=ATR_MAXLEN)) ) for(;;) {} /* XXX good loop mechanism? what is it anyway */ for( i=1; i<len; i++ ) { if( !hal_eeprom_read( &b, ATR_ADDR+i, 1 ) ) for(;;) {} hal_io_sendByteT0( b ); } /* Command loop */ for(;;) { for( i=0; i<5; i++ ) { header[i] = hal_io_recByteT0(); } #if CONF_WITH_KEYAUTH==1 if( challvalidity ) challvalidity--; #endif /* CONF_WITH_KEYAUTH==1 */ #if CONF_WITH_TRNG==1 hal_rnd_addEntropy(); #endif /* CONF_WITH_TRNG==1 */ if( (header[0]&0xFC)==CLA_PROP ) { switch( header[1]&0xFE ) { #if CONF_WITH_TESTCMDS==1 case INS_WRITE: cmd_write(); break; case INS_READ: cmd_read(); break; #endif /* CONF_WITH_TESTCMDS==1 */ #if CONF_WITH_FUNNY==1 case INS_LED: cmd_led(); break; #endif /* CONF_WITH_FUNNY==1 */ #if CONF_WITH_PINCMDS==1 case INS_CHANGE_PIN: cmd_changeUnblockPIN(); break; #endif /* CONF_WITH_PINCMDS==1 */ #if CONF_WITH_CREATECMD==1 case INS_CREATE: cmd_create(); break; #endif /* CONF_WITH_CREATECMD==1 */ #if CONF_WITH_DELETECMD==1 case INS_DELETE: cmd_delete(); break; #endif /* CONF_WITH_DELETECMD==1 */ #if CONF_WITH_KEYCMDS==1 case INS_EXTERNAL_AUTH: cmd_extAuth(); break; #endif /* CONF_WITH_KEYCMDS==1 */ #if CONF_WITH_KEYCMDS==1 case INS_GET_CHALLENGE: cmd_getChallenge(); break; #endif /* CONF_WITH_KEYCMDS==1 */ case INS_GET_RESPONSE: cmd_getResponse(); break; #if CONF_WITH_KEYCMDS==1 case INS_INTERNAL_AUTH: cmd_intAuth(); break; #endif /* CONF_WITH_KEYCMDS==1 */ case INS_READ_BINARY: cmd_readBinary(); break; case INS_SELECT: cmd_select(); break; #if CONF_WITH_PINCMDS==1 case INS_UNBLOCK_PIN: cmd_changeUnblockPIN(); break; #endif /* CONF_WITH_PINCMDS==1 */ case INS_UPDATE_BINARY: cmd_updateBinary(); break; #if CONF_WITH_KEYCMDS==1 case INS_VERIFY_KEY: cmd_verifyKeyPIN(); break; #endif /* CONF_WITH_KEYCMDS==1 */ #if CONF_WITH_PINCMDS==1 case INS_VERIFY_PIN: cmd_verifyKeyPIN(); break; #endif /* CONF_WITH_PINCMDS==1 */ default: t0_sendWord( SW_WRONG_INS ); } } else { t0_sendWord( SW_WRONG_CLA ); } #if CONF_WITH_TRNG==1 hal_rnd_addEntropy(); #endif /* Return the SW in sw */ t0_sendSw(); } }