int ssh_shell_channel_send(void* ssh_session, const char* write_buffer) { SSH_SESSION* p_session; p_session = (SSH_SESSION*)ssh_session; COMMAND_LIST* add_command_data; if(!(login_check(p_session))) { debug_log(SSH_TERMINAL_NO_SESSION_ERROR, "ssh_shell_channel_send : no session."); return SSH_TERMINAL_NO_SESSION_ERROR; } if(p_session->session_param->channel_type != CHANNEL_TYPE_SHELL) { debug_log(SSH_TERMINAL_INVALID_CHANNEL_TYPE, "ssh_shell_channel_send : channel type is not shell."); return SSH_TERMINAL_INVALID_CHANNEL_TYPE; } if(!(p_session->thread_param->exec_command_thread_flag)) { debug_log(SSH_TERMINAL_NOT_INIT_THREAD, "ssh_shell_channel_send : not init channel r/w thread."); return SSH_TERMINAL_NOT_INIT_THREAD; } add_command_data = create_command_data(write_buffer); safety_queue_push_back(p_session->thread_param->command_queue_mutex, (ORI_DATA*)add_command_data, p_session->command_queue); return 0; }
int change_shell_channel_pty_size(void* ssh_session, int pty_width, int pty_heigth) { SSH_SESSION* p_session; int ec; p_session = (SSH_SESSION*)ssh_session; if(!(login_check(p_session))) { debug_log(SSH_TERMINAL_NO_SESSION_ERROR, "change_shell_channel_pty_size : no session."); return SSH_TERMINAL_NO_SESSION_ERROR; } if(p_session->session_param->channel_type != CHANNEL_TYPE_SHELL) { debug_log(SSH_TERMINAL_INVALID_CHANNEL_TYPE, "change_shell_channel_pty_size : channel type is not shell."); return SSH_TERMINAL_INVALID_CHANNEL_TYPE; } while((ec = libssh2_channel_request_pty_size(p_session->session_param->channel, pty_width, pty_heigth)) == LIBSSH2_ERROR_EAGAIN) { waitsocket(p_session->session_param->sock, p_session->session_param->session, TIMEOUT_SEC); } if(ec < 0) { debug_log(SSH_TERMINAL_CHANGE_PTY_SIZE_ERROR, "change_shell_channel_pty_size : channel request pty size failed."); return SSH_TERMINAL_CHANGE_PTY_SIZE_ERROR; } return 0; }
int ssh_shell_channel_init(void* ssh_session, int (*callback)(char*, const char*, void*), void* obj) { SSH_SESSION* p_session = (SSH_SESSION*)ssh_session; if(!(login_check(p_session))) { debug_log(SSH_TERMINAL_NO_SESSION_ERROR, "ssh_channel_init : no session."); return SSH_TERMINAL_NO_SESSION_ERROR; } if(p_session->session_param->channel_type != CHANNEL_TYPE_SHELL) { debug_log(SSH_TERMINAL_INVALID_CHANNEL_TYPE, "ssh_shell_channel_init : channel type is not shell."); return SSH_TERMINAL_INVALID_CHANNEL_TYPE; } if(p_session->thread_param->exec_command_thread_flag) { debug_log(SSH_TERMINAL_ALREADY_RUN_THREAD, "ssh_channel_init : already run thread."); return SSH_TERMINAL_ALREADY_RUN_THREAD; } command_queue_init(p_session); p_session->thread_param->exec_command_thread_flag = TRUE; p_session->thread_param->exec_command_thread = ori_thread_create(shell_channel_read_write_thread, (void*)p_session); p_session->callback = callback; p_session->obj = obj; return 0; }
int ssh_exec_command(void* ssh_session, const char* command, int (*callback)(char*, const char*, void*), void* obj) { int ec; SSH_SESSION* p_session; p_session = (SSH_SESSION*)ssh_session; if(!(login_check(p_session))) { debug_log(SSH_TERMINAL_NO_SESSION_ERROR, "ssh_exec_command : no session."); return SSH_TERMINAL_NO_SESSION_ERROR; } if(p_session->session_param->channel_type != CHANNEL_TYPE_EXEC) { debug_log(SSH_TERMINAL_INVALID_CHANNEL_TYPE, "ssh_exec_command : channel type is not exec."); return SSH_TERMINAL_INVALID_CHANNEL_TYPE; } if(p_session->session_param->async_mode) { debug_log(SSH_TERMINAL_ASYNC_MODE_ERROR, "ssh_exec_command : async mode must be false."); return SSH_TERMINAL_ASYNC_MODE_ERROR; } if(p_session->session_param->channel == NULL) { debug_log(SSH_TERMINAL_NOT_OPEN_CHANNEL, "ssh_exec_command : don't open channel."); return SSH_TERMINAL_NOT_OPEN_CHANNEL; } ec = exec_channel_command_exec(p_session, command, callback, obj); return ec; }
int async_send_loop_command_exec_channel(void* ssh_session, const char* command, int interval) { SSH_SESSION* p_session; COMMAND_LIST* add_command; p_session = (SSH_SESSION*)ssh_session; if(!(login_check(p_session))) { debug_log(SSH_TERMINAL_NO_SESSION_ERROR, "async_send_loop_command_exec_channel : no session."); return SSH_TERMINAL_NO_SESSION_ERROR; } if(p_session->session_param->channel_type != CHANNEL_TYPE_EXEC) { debug_log(SSH_TERMINAL_INVALID_CHANNEL_TYPE, "async_send_loop_command_exec_channel : channel type is not exec."); return SSH_TERMINAL_INVALID_CHANNEL_TYPE; } if(!(p_session->session_param->async_mode)) { debug_log(SSH_TERMINAL_ASYNC_MODE_ERROR, "async_send_loop_command_exec_channel : async mode must be true."); return SSH_TERMINAL_ASYNC_MODE_ERROR; } if(!(p_session->thread_param->command_timer_thread_flag)) { debug_log(SSH_TERMINAL_NOT_INIT_THREAD, "async_send_loop_command_exec_channel : not init exec channel thread."); return SSH_TERMINAL_NOT_INIT_THREAD; } add_command = create_command_data(command); safety_add_list_insert_with_condition(p_session->thread_param->add_list_mutex, (ORI_DATA*)add_command, p_session->add_list, interval, p_session->thread_param->add_list_condition); return 0; }
int close_ssh_connection(void* ssh_session) { int ec; SSH_SESSION* p_session; p_session = (SSH_SESSION*)ssh_session; if(!(login_check(p_session))) { debug_log(SSH_TERMINAL_NO_SESSION_ERROR, "close_ssh_connection : no session."); return SSH_TERMINAL_NO_SESSION_ERROR; } if(p_session->session_param->channel != NULL) { exit_thread(ssh_session); while((ec = libssh2_channel_close(p_session->session_param->channel)) == LIBSSH2_ERROR_EAGAIN) { waitsocket(p_session->session_param->sock, p_session->session_param->session, TIMEOUT_SEC); } libssh2_channel_free(p_session->session_param->channel); p_session->session_param->channel = NULL; } while((ec = libssh2_session_disconnect(p_session->session_param->session, "normal")) == LIBSSH2_ERROR_EAGAIN) { waitsocket(p_session->session_param->sock, p_session->session_param->session, TIMEOUT_SEC); } if(ec < 0) { debug_log(SSH_TERMINAL_SESSION_DISCONNECT_ERROR, "close_ssh_connection : disconnect failed."); return SSH_TERMINAL_SESSION_DISCONNECT_ERROR; } libssh2_session_free(p_session->session_param->session); close(p_session->session_param->sock); free(p_session->session_param); ori_thread_mutex_free(p_session->thread_param->command_queue_mutex); ori_thread_mutex_free(p_session->thread_param->add_list_mutex); ori_thread_condition_free(p_session->thread_param->command_queue_condition); ori_thread_condition_free(p_session->thread_param->add_list_condition); free(p_session->thread_param); free(p_session); return 0; }
int ssh_exec_channel_async_init(void* ssh_session, int (*callback)(char*, const char*, void*), void* obj) { SSH_SESSION* p_session; p_session = (SSH_SESSION*)ssh_session; if(!(login_check(p_session))) { debug_log(SSH_TERMINAL_NO_SESSION_ERROR, "ssh_exec_channel_async_init : no session."); return SSH_TERMINAL_NO_SESSION_ERROR; } if(p_session->session_param->channel_type != CHANNEL_TYPE_EXEC) { debug_log(SSH_TERMINAL_INVALID_CHANNEL_TYPE, "ssh_exec_channel_async_init : channel type is not exec."); return SSH_TERMINAL_INVALID_CHANNEL_TYPE; } if(!(p_session->session_param->async_mode)) { debug_log(SSH_TERMINAL_ASYNC_MODE_ERROR, "ssh_exec_channel_async_init : async mode must be true."); return SSH_TERMINAL_ASYNC_MODE_ERROR; } if(p_session->thread_param->command_timer_thread_flag) { debug_log(SSH_TERMINAL_ALREADY_RUN_THREAD, "ssh_exec_channel_async_init : already run thread."); return SSH_TERMINAL_ALREADY_RUN_THREAD; } command_queue_init(p_session); add_list_init(p_session); p_session->thread_param->command_timer_thread_flag = TRUE; p_session->thread_param->command_timer_thread = ori_thread_create(exec_channel_command_timer_thread, (void*)p_session); p_session->callback = callback; p_session->obj = obj; return 0; }
int ssh_exec_channel_open(void* ssh_session, Boolean async_mode) { SSH_SESSION* p_session; LIBSSH2_CHANNEL* channel; p_session = (SSH_SESSION*)ssh_session; if(!(login_check(p_session))) { debug_log(SSH_TERMINAL_NO_SESSION_ERROR, "ssh_exec_channel_open : no session."); return SSH_TERMINAL_NO_SESSION_ERROR; } if(p_session->session_param->channel != NULL) { debug_log(SSH_TERMINAL_ALREADY_CHANNEL_OPEN, "ssh_exec_channel_open : already channel open."); return SSH_TERMINAL_ALREADY_CHANNEL_OPEN; } while((channel = libssh2_channel_open_session(p_session->session_param->session)) == NULL && libssh2_session_last_error(p_session->session_param->session, NULL, NULL, 0) == LIBSSH2_ERROR_EAGAIN) { waitsocket(p_session->session_param->sock, p_session->session_param->session, TIMEOUT_SEC); } if(channel == NULL) { debug_log(SSH_TERMINAL_CHANNEL_OPEN_ERROR, "ssh_exec_channel_open : channel open failed."); return SSH_TERMINAL_CHANNEL_OPEN_ERROR; } p_session->session_param->channel = channel; p_session->session_param->channel_type = CHANNEL_TYPE_EXEC; p_session->session_param->async_mode = async_mode; return 0; }
int login_t(const chat_t *buf) { int fd, ret; char tmp[BUFSISE]; fd = open("data/contracts.db", O_RDONLY); if (fd == -1) { perror("open"); exit(1); } sprintf(tmp, "%s#%s#", buf->name.buf, (char *)crypt(buf->passwd.buf, JAR)); ret = login_check(tmp, fd); if (ret == -1) { close(fd); return -1; } if (ret == 2) { close(fd); return 1; } }
int ssh_shell_channel_open(void* ssh_session) { SSH_SESSION* p_session; LIBSSH2_CHANNEL* channel; int ec; p_session = (SSH_SESSION*)ssh_session; if(!(login_check(p_session))) { debug_log(SSH_TERMINAL_NO_SESSION_ERROR, "ssh_shell_channel_open : no session."); return SSH_TERMINAL_NO_SESSION_ERROR; } if(p_session->session_param->channel != NULL) { debug_log(SSH_TERMINAL_ALREADY_CHANNEL_OPEN, "ssh_shell_channel_open : already channel open."); return SSH_TERMINAL_ALREADY_CHANNEL_OPEN; } while((channel = libssh2_channel_open_session(p_session->session_param->session)) == NULL && libssh2_session_last_error(p_session->session_param->session, NULL, NULL, 0) == LIBSSH2_ERROR_EAGAIN) { waitsocket(p_session->session_param->sock, p_session->session_param->session, TIMEOUT_SEC); } if(channel == NULL) { debug_log(SSH_TERMINAL_CHANNEL_OPEN_ERROR, "ssh_shell_channel_open : channel open failed."); return SSH_TERMINAL_CHANNEL_OPEN_ERROR; } while((ec = libssh2_channel_request_pty(channel, "vt100")) == LIBSSH2_ERROR_EAGAIN) { waitsocket(p_session->session_param->sock, p_session->session_param->session, TIMEOUT_SEC);; } if(ec < 0){ debug_log(SSH_TERMINAL_REQUEST_PTY_ERROR, "ssh_shell_channel_open : request pty failed."); while(libssh2_channel_close(channel) == LIBSSH2_ERROR_EAGAIN) { waitsocket(p_session->session_param->sock, p_session->session_param->session, TIMEOUT_SEC); } libssh2_channel_free(channel); return SSH_TERMINAL_REQUEST_PTY_ERROR; } while((ec = libssh2_channel_request_pty_size(channel, DEFAULT_PTY_SIZE_WIDTH, DEFAULT_PTY_SIZE_HEIGHT)) == LIBSSH2_ERROR_EAGAIN) { waitsocket(p_session->session_param->sock, p_session->session_param->session, TIMEOUT_SEC); } if(ec < 0){ debug_log(SSH_TERMINAL_REQUEST_PTY_ERROR, "ssh_shell_channel_open : request pty failed."); while(libssh2_channel_close(channel) == LIBSSH2_ERROR_EAGAIN) { waitsocket(p_session->session_param->sock, p_session->session_param->session, TIMEOUT_SEC); } libssh2_channel_free(channel); return SSH_TERMINAL_REQUEST_PTY_ERROR; } while((ec = libssh2_channel_shell(channel)) == LIBSSH2_ERROR_EAGAIN) { waitsocket(p_session->session_param->sock, p_session->session_param->session, TIMEOUT_SEC); } if(ec < 0) { debug_log(SSH_TERMINAL_SHELL_ERROR, "ssh_shell_channel_open : channel shell failed."); while(libssh2_channel_close(channel) == LIBSSH2_ERROR_EAGAIN) { waitsocket(p_session->session_param->sock, p_session->session_param->session, TIMEOUT_SEC); } libssh2_channel_free(channel); return SSH_TERMINAL_SHELL_ERROR; } p_session->session_param->channel = channel; p_session->session_param->channel_type = CHANNEL_TYPE_SHELL; return 0; }
void* client_connect(void* info){ thread_info* new_info = (thread_info* )info; int sockfd = new_info -> sockfd; std::string addr = new_info -> address; int port = new_info -> port; std::cout << sockfd << " " << addr << " " << port << '\n'; /*int read_size; char client_message[2000]; char welcome_message[200]; strcpy(welcome_message, "Welcome to simple P2P server !\n"); write(sockfd, welcome_message, strlen(welcome_message)); //test block login::Login login; login.set_id("testID"); login.set_passwd("testPasswd"); int pkg_size = login.ByteSize() + 4; char* pkt = new char[pkg_size]; google::protobuf::io::ArrayOutputStream aos(pkt, pkg_size); google::protobuf::io::CodedOutputStream* coded_output = new google::protobuf::io::CodedOutputStream(&aos); coded_output -> WriteVarint32(login.ByteSize()); login.SerializeToCodedStream(coded_output); write(sockfd, pkt, pkg_size);*/ //test block update(); login::Login login; while( true ){ int count; char buffer[4]; count = recv(sockfd, buffer, 4, MSG_PEEK); if(count == -1) perror("Recv with error"); else{ ACTION request; request = readAction(sockfd, readHdr(buffer)); if ( request == LOGIN ) { login = login_check(sockfd, addr, port); } else if ( request == REGIST ) { regist_check(sockfd); } else if ( request == DELETEACCOUNT ) { delete_account(sockfd); } else if ( request == SEARCHINFO ) { search_info(sockfd); } else if ( request == DOWNLOAD ) { download_p2p(sockfd); } else if ( request == CHAT ) { chat(sockfd); } else if (request == LOGOUT ) { logout(sockfd, file_sets); break; } else if (request == RECVFILEINFO ) { recv_file_info(sockfd, file_sets, login); } else if (request == ONLINEINFO){ send_online_info(sockfd); } else if (request == PORTREQUEST) { //send_port(sockfd); } else { printf("%sBugssssssssssss%s\n", ANSI_COLOR_RED, ANSI_COLOR_RESET); } } } /*while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 ){ client_message[read_size] = '\0'; write(sock , client_message , strlen(client_message)); memset(client_message, 0, 2000); } if(read_size == 0) { puts("Client disconnected"); fflush(stdout); }else if(read_size == -1){ perror("recv failed"); }*/ //printf("Connection left\n"); return 0; }