int cmd_put(void *info, t_msg *msg) { t_handle *handle; int ret; int size; char name[BUF_SIZE]; handle = (t_handle*)info; memset(name, 0, BUF_SIZE); add_log("Client %d execute command put %s\n", 0, handle->cli_num, msg->arg); if ((ret = receive_file_info(handle, &size, name)) < 0) return (ret); if (write(handle->cli_fd, "OK", 2) < 0) return (_error("Can't write on client %d: %m\n", -1, handle->cli_num)); if ((ret = receive_file(handle, size, name)) < 0) return (ret); if (write(handle->cli_fd, "OK", 2) < 0) return (_error("Can't write on client %d: %m\n", -1, handle->cli_num)); add_log("Client %d create file %s of size %d\n", 0, handle->cli_num, msg->arg , size); return (ISUCCESS); }
int main( int argc, char * argv[] ) { // variables and data structures const char * server_hostname; // (from command line) const char * port_str; // (from command line) char filename[FILENAME_BUF_LEN]; // (from command line) char op_str[3]; int socket_fd; // socket for communicating with server long int file_len; // length of file sent by server unsigned char MD5_hash_server[16]; // array (NOT STRING) holding hex values for MD5 hash from server unsigned char * file_buf = NULL; unsigned char * MD5_hash_client[16]; // POINTER to array (NOT STRING) holding hex values for MD5 hash from client (self) enum OPERATION op = REQ; FILE * file = NULL; struct timeval time_start; struct timeval time_end; struct timeval time_elapsed; // get information from command line analyze_argc(argc, 3, &print_usage); server_hostname = argv[1]; debugprintf("server hostnamename argument: %s", server_hostname); port_str = argv[2]; debugprintf("port argument: %s", port_str); // capture start time if (gettimeofday(&time_start, NULL) == -1) { perror("error getting start time"); exit(EXIT_FAILURE); } debugprintf("start time recorded"); // connect to server socket_fd = connect_to_server(server_hostname, port_str); if (socket_fd == -1) { fprintf(stderr, "failed to connect to server, exiting now\n"); exit(EXIT_FAILURE); } // Receive operation input while(op != XIT){ printf("Please enter an operation:\n"); scanf("%s", &op_str); if(strcmp(op_str, "REQ") == 0){ op = REQ; send_operation(socket_fd, op); // gather file info & send it printf("Please enter the name of the file you would like to request:\n"); scanf("%s", &filename); send_file_info(socket_fd, filename); // receive file length from server long int file_len_net; recv_bytes(socket_fd, &file_len_net, sizeof(file_len_net), "file length"); file_len = file_len_net; debugprintf("file length received from server: %ld", file_len); // quit if file does not exist on server if (file_len == -1) { fprintf(stderr, "File does not exists\n"); close(socket_fd); exit(EXIT_SUCCESS); } // receive MD5 hash from server recv_bytes(socket_fd, MD5_hash_server, 16, "MD5 hash"); debugprintf("MD5 hash received from server"); // prepare to receive file byte array (packet by packet) from server file_buf = (unsigned char *)malloc(file_len * sizeof(unsigned char)); int i_full_pckt; int n_full_pckts = file_len / FILE_PCKT_LEN; size_t last_pckt_len = file_len % FILE_PCKT_LEN; debugprintf("expecting %d full packets from server (%zu bytes)", n_full_pckts, FILE_PCKT_LEN); if (last_pckt_len != 0) { debugprintf("last packet will be %zu bytes", last_pckt_len); } else { debugprintf("no last packet will be received"); } // recieve full packets from server for (i_full_pckt = 0; i_full_pckt < n_full_pckts; i_full_pckt++) { recv_bytes(socket_fd, &file_buf[i_full_pckt * FILE_PCKT_LEN], FILE_PCKT_LEN, "file packet"); debugprintf("full packet %d of %d received from server", (i_full_pckt + 1), n_full_pckts); } // receive last packet from server (if necessary) if (last_pckt_len != 0) { recv_bytes(socket_fd, &file_buf[n_full_pckts * FILE_PCKT_LEN], last_pckt_len, "last file packet"); debugprintf("last packet received from server"); } debugprintf("file received from server"); // create MD5 hash of file MD5_hash_of_byte_array(file_buf, file_len, MD5_hash_client); debugprintf("MD5 hash created"); // compare MD5 hashes if (cmp_MD5_hash(*MD5_hash_client, MD5_hash_server) != 0) { fprintf(stderr, "File hashes do not match – bad transfer\n"); close(socket_fd); exit(EXIT_FAILURE); } debugprintf("MD5 hashes match"); //TODO: MAKE FAIL! // write byte array to file file = fopen(filename, "wb"); size_t write_size = fwrite(file_buf, 1, file_len, file); //return value! debugprintf("file created, DONE %d bytes written", write_size); // capture end time if (gettimeofday(&time_end, NULL) == -1) { perror("error getting end time"); close(socket_fd); exit(EXIT_FAILURE); } debugprintf("end time recorded"); // calculate and print time difference and throughput timersub(&time_end, &time_start, &time_elapsed); double seconds_elapsed = time_elapsed.tv_sec + (time_elapsed.tv_usec / 1000000.0); double throughput = ((double)file_len / 1048576) / seconds_elapsed; printf("%ld bytes transferred in %f sec. Throughput: %f Megabytes/sec. File MD5sum: ", file_len, seconds_elapsed, throughput); print_MD5_hash(MD5_hash_client); printf("\n"); } else if(strcmp(op_str, "UPL") == 0){ op = UPL; send_operation(socket_fd, op); printf("Please enter the name of the file you would like to upload:\n"); } else if(strcmp(op_str, "DEL") == 0){ op = DEL; send_operation(socket_fd, op); // gather file info & send it printf("Please enter the name of the file you would like to delete:\n"); scanf("%s", &filename); send_file_info(socket_fd, filename); // Listen for if file exists uint32_t file_exists_net; recv_bytes(socket_fd, &file_exists_net, sizeof(file_exists_net), "operation"); short int file_exists = ntohs(file_exists_net); if(file_exists){ printf("Are you sure you want to delete %s (Yes/No)?\n", &filename); char confirm_str[3]; scanf("%s", &confirm_str); short int confirm; if( strcmp(confirm_str, "Yes") == 0 ){ confirm = 1; debugprintf("file delete sent to client"); } else { confirm = 0; printf("Delete abandoned by user!\n"); } uint32_t confirm_net; confirm_net = htons(confirm); send_bytes(socket_fd, &confirm_net, sizeof(confirm_net), "confirm delete sent to server"); if( confirm ){ recv_bytes(socket_fd, &confirm_net, sizeof(confirm_net), "confirm delete from server"); confirm = ntohs(confirm_net); if( confirm == 0 ){ printf("File was sucessfully deleted from the server\n"); } else printf("Error deleting file from the server\n"); } } else { debugprintf("Server says file does not exist"); } } else if(strcmp(op_str, "LIS") == 0){ op = LIS; send_operation(socket_fd, op); short int num_files; uint32_t num_files_net; recv_bytes(socket_fd, &num_files_net, sizeof(num_files_net), "client receiving num files"); num_files = ntohs(num_files_net); debugprintf("%d Files in the dir\n", num_files); int i; for(i = 0; i < num_files; i++){ receive_file_info ( socket_fd, filename); printf("%s\n", filename); } } else if(strcmp(op_str, "XIT") == 0){ op = XIT; send_operation(socket_fd, op); } } close(socket_fd); printf("Connection to host closed. Goodbye!\n"); exit(EXIT_SUCCESS); }