//TODO: help verify void handle_list_file(char* prefix) { log_info("handle_list_file(%s)\n", prefix); char buf[200] = ""; uint8_t buf_size = 0; struct cfs_dir dir; int ret = cfs_opendir(&dir, ""); if (ret == -1) { log_info("cfs_opendir() failed: %d\n", ret); return; } while (ret != -1) { struct cfs_dirent dirent; ret = cfs_readdir(&dir, &dirent); if (ret != -1) { log_info("file:%s, %d\n", dirent.name, dirent.size); char* short_name = filter_filename_by_prefix(prefix, dirent.name); uint8_t len = strlen(short_name) + 1; if (buf_size + len >= sizeof(buf) - 1) break; strcat(buf, short_name); strcat(buf, ";"); buf_size += len; } } cfs_closedir(&dir); send_file_list(buf); }
//----------------------------------------------------------------------------- void *connection(void *user) { //struct timeval curTime; struct User *_user = (struct User *)user; int e; int rc = 1; int sockfd = _user->sockfd; pthread_detach(pthread_self()); //automatically clears the threads memory on exit fd_set rdset, wrset; int s = -1; int max_fd = sockfd + 1; struct timeval selectTimeout; selectTimeout.tv_sec = TIMEOUT / 1000; selectTimeout.tv_usec = (TIMEOUT % 1000) * 1000; FD_ZERO(&rdset); FD_ZERO(&wrset); char *buf = calloc(100,1); for (;;) { // start monitoring for reads or timeout if (s <= 0) { FD_SET(sockfd, &rdset); } s = select(max_fd, &rdset, &wrset, NULL, &selectTimeout); if (s == -1) { printf("ERROR: Socket error. Exiting.\n"); exit(1); } /* * Here we handle all of the incoming text from a particular client. */ if(s > 0) { bzero(buf,100); rc = recv(sockfd,buf,99,0); if (rc > 0) { char *cmd = malloc(24); cmd = strtok(buf," "); // which command are we processing? if (strcmp(cmd,GET_CMD) == 0) { char *filename = calloc(FILENAME_LEN,1); filename = strtok(NULL,"\0"); send_file_info(sockfd, filename); } else if (strcmp(cmd,LIST_CMD) == 0) { send_file_list(sockfd); } else if (strcmp(cmd,USERS_CMD) == 0) { send_user_list(sockfd); } else if (strcmp(cmd,SEND_LIST_CMD) == 0) { receive_file_list(sockfd,_user); } else if (strcmp(cmd,EXIT_CMD) == 0) { //if I received an 'exit' message from this client pthread_mutex_lock(&mutex); remove_user(_user); pthread_mutex_unlock(&mutex); pthread_exit(NULL); printf("Shouldn't see this!\n"); //loop through global client list and //e =write(..); if (e == -1) //broken pipe.. someone has left the room { pthread_mutex_lock(&mutex); //so remove them from our list pthread_mutex_unlock(&mutex); } } else { printf("Unknown command received: %s\n",cmd); } fflush(stdout); } } } //A requirement for 5273 students: //if I received a new files list from this client, the //server must “Push”/send the new updated hash table to all clients it is connected to. //should probably never get here return 0; }
/* * This is called once the connection has been negotiated. It is used * for rsyncd, remote-shell, and local connections. */ int client_run(int f_in, int f_out, pid_t pid, int argc, char *argv[]) { struct file_list *flist = NULL; int status = 0, status2 = 0; char *local_name = NULL; cleanup_child_pid = pid; if (!read_batch) { set_nonblocking(f_in); set_nonblocking(f_out); } io_set_sock_fds(f_in, f_out); setup_protocol(f_out,f_in); if (protocol_version >= 23 && !read_batch) io_start_multiplex_in(); /* We set our stderr file handle to blocking because ssh might have * set it to non-blocking. This can be particularly troublesome if * stderr is a clone of stdout, because ssh would have set our stdout * to non-blocking at the same time (which can easily cause us to lose * output from our print statements). This kluge shouldn't cause ssh * any problems for how we use it. Note also that we delayed setting * this until after the above protocol setup so that we know for sure * that ssh is done twiddling its file descriptors. */ set_blocking(STDERR_FILENO); if (am_sender) { keep_dirlinks = 0; /* Must be disabled on the sender. */ io_start_buffering_out(); if (!filesfrom_host) set_msg_fd_in(f_in); send_filter_list(f_out); if (filesfrom_host) filesfrom_fd = f_in; if (write_batch && !am_server) start_write_batch(f_out); flist = send_file_list(f_out, argc, argv); set_msg_fd_in(-1); if (verbose > 3) rprintf(FINFO,"file list sent\n"); the_file_list = flist; io_flush(NORMAL_FLUSH); send_files(flist,f_out,f_in); io_flush(FULL_FLUSH); handle_stats(-1); if (protocol_version >= 24) read_final_goodbye(f_in, f_out); if (pid != -1) { if (verbose > 3) rprintf(FINFO,"client_run waiting on %d\n", (int) pid); io_flush(FULL_FLUSH); wait_process(pid, &status); } output_summary(); io_flush(FULL_FLUSH); exit_cleanup(status); } if (need_messages_from_generator && !read_batch) io_start_multiplex_out(); if (argc == 0) list_only |= 1; send_filter_list(read_batch ? -1 : f_out); if (filesfrom_fd >= 0) { io_set_filesfrom_fds(filesfrom_fd, f_out); filesfrom_fd = -1; } if (write_batch && !am_server) start_write_batch(f_in); flist = recv_file_list(f_in); the_file_list = flist; if (flist && flist->count > 0) { local_name = get_local_name(flist, argv[0]); status2 = do_recv(f_in, f_out, flist, local_name); } else { handle_stats(-1); output_summary(); } if (pid != -1) { if (verbose > 3) rprintf(FINFO,"client_run2 waiting on %d\n", (int) pid); io_flush(FULL_FLUSH); wait_process(pid, &status); } return MAX(status, status2); }
static void do_server_sender(int f_in, int f_out, int argc,char *argv[]) { int i; struct file_list *flist; char *dir = argv[0]; if (verbose > 2) { rprintf(FINFO, "server_sender starting pid=%ld\n", (long)getpid()); } if (am_daemon && lp_write_only(module_id)) { rprintf(FERROR, "ERROR: module is write only\n"); exit_cleanup(RERR_SYNTAX); return; } if (am_daemon && lp_read_only(module_id) && remove_sent_files) { rprintf(FERROR, "ERROR: --remove-sent-files cannot be used with a read-only module\n"); exit_cleanup(RERR_SYNTAX); return; } if (!relative_paths && !push_dir(dir)) { rsyserr(FERROR, errno, "push_dir#3 %s failed", full_fname(dir)); exit_cleanup(RERR_FILESELECT); } argc--; argv++; if (strcmp(dir,".")) { int l = strlen(dir); if (strcmp(dir,"/") == 0) l = 0; for (i = 0; i < argc; i++) argv[i] += l+1; } if (argc == 0 && (recurse || list_only)) { argc = 1; argv--; argv[0] = "."; } flist = send_file_list(f_out,argc,argv); if (!flist || flist->count == 0) { exit_cleanup(0); } the_file_list = flist; io_start_buffering_in(); io_start_buffering_out(); send_files(flist,f_out,f_in); io_flush(FULL_FLUSH); handle_stats(f_out); if (protocol_version >= 24) read_final_goodbye(f_in, f_out); io_flush(FULL_FLUSH); exit_cleanup(0); }