/** * send the 4.3.6 dot command (os state) to the service processor * During driver init this function is called with os state "up". * This causes the service processor to start sending heartbeats the * driver. * During driver exit the function is called with os state "down", * causing the service processor to stop the heartbeats. */ int ibmasm_send_os_state(struct service_processor *sp, int os_state) { struct command *cmd; struct os_state_command *os_state_cmd; int result = 0; cmd = ibmasm_new_command(sizeof(struct os_state_command)); if (cmd == NULL) return -ENOMEM; os_state_cmd = (struct os_state_command *)cmd->buffer; os_state_cmd->header.type = sp_write; os_state_cmd->header.command_size = 3; os_state_cmd->header.data_size = 1; os_state_cmd->header.status = 0; os_state_cmd->command[0] = 4; os_state_cmd->command[1] = 3; os_state_cmd->command[2] = 6; os_state_cmd->data = os_state; ibmasm_exec_command(sp, cmd); ibmasm_wait_for_response(cmd, IBMASM_CMD_TIMEOUT_NORMAL); if (cmd->status != IBMASM_CMD_COMPLETE) result = -ENODEV; command_put(cmd); return result; }
void ibmasm_heartbeat_exit(struct service_processor *sp) { char tsbuf[32]; dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf)); ibmasm_wait_for_response(sp->heartbeat, IBMASM_CMD_TIMEOUT_NORMAL); dbg("%s:%d at %s\n", __func__, __LINE__, get_timestamp(tsbuf)); suspend_heartbeats = 1; command_put(sp->heartbeat); }
void command_mput(int sfd_client, struct packet* chp, int n, char** filenames) { int i; char* filename; for(i = 0; i < n; i++) { filename = *(filenames + i); printf("\tProcessing file %d of %d:\t%s\n", i + 1, n, filename); command_put(sfd_client, chp, filename); } if(i != n) fprintf(stderr, "Not all files could be uploaded.\n"); }
static void call_event(int thing, short sev, void *evv) { struct evt *ev safe = safe_cast evv; if (comm_call(ev->comm, "callback:event", ev->home, thing, NULL, NULL, 0) < 0) { event_del(ev->l); event_free(ev->l); list_del(&ev->lst); command_put(ev->comm); free(ev); } }
/** * receive_command_response * called by the interrupt handler when a dot command of type command_response * was received. */ void ibmasm_receive_command_response(struct service_processor *sp, void *response, size_t size) { struct command *cmd = sp->current_command; if (!sp->current_command) return; memcpy_fromio(cmd->buffer, response, min(size, cmd->buffer_size)); cmd->status = IBMASM_CMD_COMPLETE; wake_up(&sp->current_command->wait); command_put(sp->current_command); exec_next_command(sp); }
static inline void do_exec_command(struct service_processor *sp) { char tsbuf[32]; dbg("%s:%d at %s\n", __FUNCTION__, __LINE__, get_timestamp(tsbuf)); if (ibmasm_send_i2o_message(sp)) { sp->current_command->status = IBMASM_CMD_FAILED; wake_up(&sp->current_command->wait); command_put(sp->current_command); exec_next_command(sp); } }
static void call_timeout_event(int thing, short sev, void *evv) { struct evt *ev safe = safe_cast evv; if (comm_call(ev->comm, "callback:event", ev->home, thing, NULL, NULL, 0) < 0) { event_free(ev->l); list_del(&ev->lst); command_put(ev->comm); free(ev); } else { struct timeval tv; tv.tv_sec = ev->seconds; tv.tv_usec = 0; event_add(ev->l, &tv); } }
/** * send the 4.3.5.10 dot command (driver VPD) to the service processor */ int ibmasm_send_driver_vpd(struct service_processor *sp) { struct command *command; struct dot_command_header *header; u8 *vpd_command; u8 *vpd_data; int result = 0; command = ibmasm_new_command(INIT_BUFFER_SIZE); if (command == NULL) return -ENOMEM; header = (struct dot_command_header *)command->buffer; header->type = sp_write; header->command_size = 4; header->data_size = 16; header->status = 0; header->reserved = 0; vpd_command = command->buffer + sizeof(struct dot_command_header); vpd_command[0] = 0x4; vpd_command[1] = 0x3; vpd_command[2] = 0x5; vpd_command[3] = 0xa; vpd_data = vpd_command + header->command_size; vpd_data[0] = 0; strcat(vpd_data, IBMASM_DRIVER_VPD); vpd_data[10] = 0; vpd_data[15] = 0; ibmasm_exec_command(sp, command); ibmasm_wait_for_response(command, IBMASM_CMD_TIMEOUT_NORMAL); if (command->status != IBMASM_CMD_COMPLETE) result = -ENODEV; command_put(command); return result; }
static int do_run(t_env *env, char **splitted) { if (!ft_strcmp(splitted[0], "ls")) command_ls(env, splitted); else if (!ft_strcmp(splitted[0], "cd")) command_cd(env, splitted); else if (!ft_strcmp(splitted[0], "get")) command_get(env, splitted); else if (!ft_strcmp(splitted[0], "put")) command_put(env, splitted); else if (!ft_strcmp(splitted[0], "pwd")) command_pwd(env, splitted); else if (!ft_strcmp(splitted[0], "quit") || !ft_strcmp(splitted[0], "exit")) command_quit(env, splitted); else if (!ft_strcmp(splitted[0], "touch")) command_touch(env, splitted); else if (!ft_strcmp(splitted[0], "unlink")) command_unlink(env, splitted); else return (0); return (1); }
/** * @function _process_connection * Handle and process an incoming connection. * @param thread Pointer to the thread's structure. */ static void _process_connection(comm_thread_t *thread) { uint8_t cmd; // loop on incoming requests for (; ; ) { // read command byte if (read(thread->client_sock, &cmd, sizeof(cmd)) <= 0) { close(thread->client_sock); break; } // interpret command switch (cmd) { case PROTO_PUT: command_put(thread); break; case PROTO_DELETE: command_delete(thread); break; case PROTO_GET: command_get(thread); break; } } }
int main(int argc, char* argv[]) { //BEGIN: initialization struct sockaddr_in sin_server; int sfd_client, x; size_t size_sockaddr = sizeof(struct sockaddr), size_packet = sizeof(struct packet); short int connection_id; struct packet* chp = (struct packet*) malloc(size_packet); // client host packet set0(chp); struct packet* data; // network packet if((x = sfd_client = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) er("socket()", x); memset((char*) &sin_server, 0, sizeof(struct sockaddr_in)); sin_server.sin_family = AF_INET; sin_server.sin_addr.s_addr = inet_addr(IPSERVER); sin_server.sin_port = htons(PORTSERVER); if((x = connect(sfd_client, (struct sockaddr*) &sin_server, size_sockaddr)) < 0) er("connect()", x); printf(ID "FTP Client started up. Attempting communication with server @ %s:%d...\n\n", IPSERVER, PORTSERVER); //END: initialization struct command* cmd; char lpwd[LENBUFFER], pwd[LENBUFFER]; //用作本地和FTP server端的当前工作区间名的缓冲区 char userinput[LENUSERINPUT]; while(1) { printf("\t> "); fgets(userinput, LENUSERINPUT, stdin); /* in order to give a filename with spaces, put ':' instead of ' '. If a command needs x paths, and y (y > x) paths are provided, y - x paths will be ignored. */ cmd = userinputtocommand(userinput); if(!cmd) continue; //printcommand(cmd); switch(cmd->id) { case GET: if(cmd->npaths) command_get(chp, data, sfd_client, *cmd->paths); else fprintf(stderr, "No path to file given.\n"); break; case PUT: if(cmd->npaths) command_put(chp, data, sfd_client, *cmd->paths); else fprintf(stderr, "No path to file given.\n"); break; case MGET: if(cmd->npaths) command_mget(chp, data, sfd_client, cmd->npaths, cmd->paths); else fprintf(stderr, "No path to file given.\n"); break; case MPUT: if(cmd->npaths) command_mput(chp, data, sfd_client, cmd->npaths, cmd->paths); else fprintf(stderr, "No path to file given.\n"); break; case MGETWILD: command_mgetwild(chp, data, sfd_client); break; case MPUTWILD: if(!getcwd(lpwd, sizeof lpwd)) er("getcwd()", 0); command_mputwild(chp, data, sfd_client, lpwd); break; case CD: if(cmd->npaths) command_cd(chp, data, sfd_client, *cmd->paths); else fprintf(stderr, "No path given.\n"); break; case LCD: // 改变local 工作区间 if(cmd->npaths) command_lcd(*cmd->paths); else fprintf(stderr, "No path given.\n"); break; case PWD: command_pwd(chp, data, sfd_client); break; case LPWD: // local pwd if(!getcwd(lpwd, sizeof lpwd)) er("getcwd()", 0); printf("\t%s\n", lpwd); break; case DIR_: case LS: command_ls(chp, data, sfd_client); break; case LDIR: case LLS: if(!getcwd(lpwd, sizeof lpwd)) er("getcwd()", 0); command_lls(lpwd); break; case MKDIR: if(cmd->npaths) command_mkdir(chp, data, sfd_client, *cmd->paths); else fprintf(stderr, "No path to directory given.\n"); break; case LMKDIR: if(cmd->npaths) command_lmkdir(*cmd->paths); else fprintf(stderr, "No path to directory given.\n"); break; case RGET: if(!getcwd(lpwd, sizeof lpwd)) er("getcwd()", 0); command_rget(chp, data, sfd_client); if((x = chdir(lpwd)) == -1) fprintf(stderr, "Wrong path.\n"); break; case RPUT: if(!getcwd(lpwd, sizeof lpwd)) er("getcwd()", 0); command_rput(chp, data, sfd_client); if((x = chdir(lpwd)) == -1) fprintf(stderr, "Wrong path.\n"); break; case EXIT: goto outside_client_command_loop; default: // display error break; } } outside_client_command_loop: /* chp->type = REQU; chp->conid = -1; strcpy(path, argv[1]); strcpy(chp->buffer, argv[1]); //printpacket(chp, HP); data = htonp(chp); if((x = send(sfd_client, data, size_packet, 0)) != size_packet) er("send()", x); set0(data); do { if((x = recv(sfd_client, data, size_packet, 0)) <= 0) er("recv()", x); chp = htonp(data); if(chp->type == INFO) printf(ID "Server says: %s\n", chp->buffer); else if(chp->type == DATA) { //printpacket(chp, HP); receive_file(extract_filename(path), sfd_client, chp); } } while(chp->type != TERM); fprintf(stderr, "TERM received; exiting...\n"); */ close(sfd_client); printf(ID "Done.\n"); fflush(stdout); return 0; }
void* serve_client(void* info) { int sfd_client, connection_id, x; struct packet* data = (struct packet*) malloc(size_packet); struct packet* shp; char lpwd[LENBUFFER]; struct client_info* ci = (struct client_info*) info; sfd_client = ci->sfd; connection_id = ci->cid; while(1) { if((x = recv(sfd_client, data, size_packet, 0)) == 0) { fprintf(stderr, "client closed/terminated. closing connection.\n"); break; } shp = ntohp(data); if(shp->type == TERM) break; if(shp->conid == -1) shp->conid = connection_id; if(shp->type == REQU) { switch(shp->comid) { case PWD: if(!getcwd(lpwd, sizeof lpwd)) er("getcwd()", 0); command_pwd(shp, data, sfd_client, lpwd); break; case CD: if((x = chdir(shp->buffer)) == -1) fprintf(stderr, "Wrong path.\n"); command_cd(shp, data, sfd_client, x == -1 ? "fail" : "success"); break; case MKDIR: command_mkdir(shp, data, sfd_client); break; case LS: if(!getcwd(lpwd, sizeof lpwd)) er("getcwd()", 0); command_ls(shp, data, sfd_client, lpwd); break; case GET: command_get(shp, data, sfd_client); break; case PUT: command_put(shp, data, sfd_client); break; case RGET: if(!getcwd(lpwd, sizeof lpwd)) er("getcwd()", 0); command_rget(shp, data, sfd_client); send_EOT(shp, data, sfd_client); if((x = chdir(lpwd)) == -1) fprintf(stderr, "Wrong path.\n"); default: // print error break; } /* //send info and then proceed to complete the request shp->type = INFO; strcpy(path, shp->buffer); sprintf(shp->buffer, "File found. Processing..."); data = htonp(shp); if((x = send(sfd_client, data, size_packet, 0)) != size_packet) er("send()", x); send_file(path, sfd_client, shp); send_TERM(sfd_client, shp); */ } else { //show error, send TERM and break fprintf(stderr, "packet incomprihensible. closing connection.\n"); send_TERM(shp, data, sfd_client); break; } } close(sfd_client); fflush(stdout); }
void ibmasm_heartbeat_exit(struct service_processor *sp) { command_put(sp->heartbeat); }
void* serve_client(void* info) { int sfd_client, connection_id, x; struct packet shp; char lpwd[LENBUFFER]; struct client_info* ci = (struct client_info*) info; sfd_client = ci->sfd; connection_id = ci->cid; while(1) { if(recv_packet_ret(sfd_client, &shp) < 0) { fprintf(stderr, "client ID(%d) closed/terminated. closing connection.\n", connection_id); break; } if(shp.type == TERM) break; if(shp.conid == -1) shp.conid = connection_id; if(shp.type == REQU) { switch(shp.comid) { case PWD: if(!getcwd(lpwd, sizeof lpwd)) er("getcwd()", 0); command_pwd(sfd_client, &shp, lpwd); break; case CD: if((x = chdir(shp.buffer)) == -1) fprintf(stderr, "Wrong path.\n"); command_cd(sfd_client, &shp, x == -1 ? "fail" : "success"); break; case MKDIR: command_mkdir(sfd_client, &shp); break; case LS: if(!getcwd(lpwd, sizeof lpwd)) er("getcwd()", 0); command_ls(sfd_client, &shp, lpwd); break; case GET: command_get(sfd_client, &shp); break; case PUT: command_put(sfd_client, &shp); break; case RGET: if(!getcwd(lpwd, sizeof lpwd)) er("getcwd()", 0); command_rget(sfd_client, &shp); send_EOT(sfd_client, &shp); if((x = chdir(lpwd)) == -1) fprintf(stderr, "Wrong path.\n"); break; default: // print error break; } } else { //show error, send TERM and break fprintf(stderr, "packet incomprihensible. closing connection.\n"); send_TERM(sfd_client, &shp); break; } } close(sfd_client); fflush(stdout); return NULL; }