📄 flute.c
字号:
free(md5); return -4; } free(md5); } }#endif if(file->encoding != NULL) { if(strcmp(file->encoding, "pad") == 0) { retcode = padding_decoder(tmp_filename, file->file_len); if(retcode == -1) { return -1; } *(tmp_filename + (strlen(tmp_filename) - PAD_SUFFIX_LEN)) = '\0'; #ifdef WIN32 if(_stat64(tmp_filename, &file_stats) == -1) {#else if(stat64(tmp_filename, &file_stats) == -1) {#endif printf("Error: %s is not valid file name\n", tmp_filename); fflush(stdout); return -1; } if(file_stats.st_size != file->file_len) { printf("Error: padding encoding failed\n"); fflush(stdout); remove(tmp_filename); return -1; } }#ifdef USE_ZLIB else if(strcmp(file->encoding, "gzip") == 0) { retcode = file_gzip_uncompress(tmp_filename); if(retcode == -1) { return -1; } *(tmp_filename + (strlen(tmp_filename) - GZ_SUFFIX_LEN)) = '\0';#ifdef WIN32 if(_stat64(tmp_filename, &file_stats) == -1) {#else if(stat64(tmp_filename, &file_stats) == -1) {#endif printf("Error: %s is not valid file name\n", tmp_filename); fflush(stdout); return -1; } if(file_stats.st_size != file->file_len) { printf("Error: uncompression failed\n"); fflush(stdout); remove(tmp_filename); return -1; } }#endif } uri = parse_uri(file->location, strlen(file->location)); filepath = get_uri_host_and_path(uri); if(!(tmp = (char*)calloc((strlen(filepath) + 1), sizeof(char)))) { printf("Could not alloc memory for tmp (file location)!\n"); free(filepath); free_uri(uri); return -1; } memcpy(tmp, filepath, strlen(filepath)); ptr = strchr(tmp, ch); memset(fullpath, 0, MAX_PATH); memcpy(fullpath, session_basedir, strlen(session_basedir)); if(ptr != NULL) { while(ptr != NULL) { i++; point = (int)(ptr - tmp); memset(filename, 0, MAX_PATH); #ifdef WIN32 memcpy((fullpath + strlen(fullpath)), "\\", 1);#else memcpy((fullpath + strlen(fullpath)), "/", 1);#endif memcpy((fullpath + strlen(fullpath)), tmp, point); memcpy(filename, (tmp + point + 1), (strlen(tmp) - (point + 1))); #ifdef WIN32 if(mkdir(fullpath) < 0) { #else if(mkdir(fullpath, S_IRWXU) < 0) {#endif if(errno != EEXIST) { printf("mkdir failed: cannot create directory %s (errno=%i)\n", fullpath, errno); fflush(stdout); free(tmp); free(filepath); free_uri(uri); return -1; } } strcpy(tmp, filename); ptr = strchr(tmp, ch); }#ifdef WIN32 memcpy((fullpath + strlen(fullpath)), "\\", 1);#else memcpy((fullpath + strlen(fullpath)), "/", 1);#endif memcpy((fullpath + strlen(fullpath)), filename, strlen(filename)); } else{#ifdef WIN32 memcpy((fullpath + strlen(fullpath)), "\\", 1);#else memcpy((fullpath + strlen(fullpath)), "/", 1);#endif memcpy((fullpath + strlen(fullpath)), filepath, strlen(filepath)); } /* move and rename tmp_file */ if(rename(tmp_filename, fullpath) < 0) { if(errno == EEXIST) { retval = remove(fullpath); if(retval == -1) { printf("errno: %i\n", errno); } if(rename(tmp_filename, fullpath) < 0) { printf("rename() error1\n"); } } else { printf("rename() error2\n"); } } free_uri(uri); free(tmp); if(a->alc_a.verbosity > 0) {#ifdef WIN32 printf("File received: %s (TOI=%I64u)\n", filepath, toi);#else printf("File received: %s (TOI=%llu)\n", filepath, toi);#endif fflush(stdout); } #ifdef WIN32 if(a->open_file) { ShellExecute(NULL, "Open", fullpath, NULL, NULL, SW_SHOWNORMAL); }#endif file_received(filetable, file_nb, filepath); free(filepath); } return retval;}/* * This function is flute receiver's object mode receiving function. * * Params: int *s_id: Pointer to session identifier, * arguments_t *a: Pointer to arguments structure where command line arguments are parsed. * * Return: int: Different values (0, -1, -2, -3) depending on how function ends. * */int receiver_in_object_mode(int *s_id, arguments_t *a) { int retval = 0; int retcode; div_t div_max_n; unsigned short max_n; char *cont_desc = NULL; div_max_n = div((a->alc_a.max_sb_len * (100 + a->alc_a.fec_ratio)), 100); max_n = (unsigned short)div_max_n.quot; if(strcmp(a->sdp_file, "") != 0) { cont_desc = sdp_attr_get(a->sdp, "content-desc"); } printf("FLUTE Receiver in object mode\n"); if(cont_desc != NULL) { printf("Session content information available at:\n"); printf("%s\n", cont_desc); } fflush(stdout); set_wanted_object(*s_id, a->toi, 0, a->alc_a.es_len, a->alc_a.max_sb_len, a->alc_a.fec_inst_id, a->alc_a.fec_enc_id, max_n, 0); retcode = recvfile(*s_id, NULL, a->toi, 0, NULL, a->alc_a.rx_memory_mode, NULL, a->alc_a.verbosity); if(retcode == -1) { printf("\nError: recvfile() failed\n"); fflush(stdout); retval = -1; } else if(retcode == -2) { retval = -2; } else if(retcode == -3) { retval = -3; } else {#ifdef WIN32 if(a->open_file) { ShellExecute(NULL, "Open", a->file_path, NULL, NULL, SW_SHOWNORMAL); }#endif } return retval;}/* * This function checks if all wanted files are received. * * Params: char *filetable[10]: Pointers to wanted file names, * int file_nb: Number of wanted files. * * Return: int: 1 when all files are received, 0 otherwise * */int all_files_received(char *filetable[10], int file_nb) { int i; int retval = 1; for(i = 0; i < file_nb; i++) { if(filetable[i] != NULL) { retval = 0; } } return retval;}/* * This function sets file defined as received. * * Params: char *filetable[10]: Pointers to wanted file names, * int file_nb: Number of wanted files, * char *filepath: Pointer to file's path * * Return: void * */void file_received(char *filetable[10], int file_nb, char *filepath) { int i; for(i = 0; i < file_nb; i++) { if(filetable[i] != NULL) { if(strstr(filepath, filetable[i]) != NULL) { filetable[i] = NULL; break; } } }}/* * This function names all uncompleted objects. * * Params: int *s_id: Pointer to session identifier, * fdt_th_args_t *fdt_th_args: Pointer to structure conmtaining FDT and some other information. * * Return: int: Different values (0, -1) depending on how function ends. * */int name_incomplete_objects(fdt_th_args_t *fdt_th_args) { trans_obj_t *to = NULL; file_t *file = NULL; file_t *next_file = NULL; uri_t *uri = NULL; char *filepath = NULL; char *tmp = NULL; char* session_basedir = NULL; char *ptr; int point; int ch = '/'; int i; char fullpath[MAX_PATH]; char filename[MAX_PATH]; int retval; char *pad_tmp_filename = NULL; char *tmp_filename = NULL; session_basedir = get_session_basedir(fdt_th_args->s_id); to = get_alc_session(fdt_th_args->s_id)->obj_list; while(to != NULL) { next_file = fdt_th_args->fdt->file_list; while(next_file != NULL) { file = next_file; if(file->toi == to->toi) { break; } next_file = file->next; } if(file->encoding != NULL && strcmp(file->encoding, "gzip") == 0) { printf("Cannot rename content encoded incomplete object.\n"); to = to->next; continue; } uri = parse_uri(file->location, strlen(file->location)); filepath = get_uri_host_and_path(uri); if(!(tmp = (char*)calloc((strlen(filepath) + 1), sizeof(char)))) { printf("Could not alloc memory for tmp-filepath!\n"); fflush(stdout); free(filepath); free_uri(uri); return -1; } memcpy(tmp, filepath, strlen(filepath)); ptr = strchr(tmp, ch); memset(fullpath, 0, MAX_PATH); memcpy(fullpath, session_basedir, strlen(session_basedir)); i = 0; if(ptr != NULL) { while(ptr != NULL) { i++; point = (int)(ptr - tmp); memset(filename, 0, MAX_PATH); #ifdef WIN32 memcpy((fullpath + strlen(fullpath)), "\\", 1);#else memcpy((fullpath + strlen(fullpath)), "/", 1);#endif memcpy((fullpath + strlen(fullpath)), tmp, point); memcpy(filename, (tmp + point + 1), (strlen(tmp) - (point + 1))); #ifdef WIN32 if(mkdir(fullpath) < 0) { #else if(mkdir(fullpath, S_IRWXU) < 0) {#endif if(errno != EEXIST) { printf("mkdir failed: cannot create directory %s (errno=%i)\n", fullpath, errno); fflush(stdout); free(tmp); free(filepath); free_uri(uri); return -1; } } strcpy(tmp, filename); ptr = strchr(tmp, ch); }#ifdef WIN32 memcpy((fullpath + strlen(fullpath)), "\\", 1);#else memcpy((fullpath + strlen(fullpath)), "/", 1);#endif memcpy((fullpath + strlen(fullpath)), filename, strlen(filename)); } else{#ifdef WIN32 memcpy((fullpath + strlen(fullpath)), "\\", 1);#else memcpy((fullpath + strlen(fullpath)), "/", 1);#endif memcpy((fullpath + strlen(fullpath)), filepath, strlen(filepath)); } if(!(tmp_filename = (char*)calloc((strlen(to->tmp_filename) + 1), sizeof(char)))) { printf("Could not alloc memory for tmp_filename!\n"); free(tmp); free(filepath); free_uri(uri); return -1; } if(file->encoding == NULL) { memcpy(tmp_filename, to->tmp_filename, strlen(to->tmp_filename)); free_object(to, get_alc_session(fdt_th_args->s_id), 1); } else if(strcmp(file->encoding, "pad") == 0) { if(!(pad_tmp_filename = (char*)calloc((strlen(to->tmp_filename) + 1), sizeof(char)))) { printf("Could not alloc memory for pad_tmp_filename!\n"); free(tmp); free(filepath); free_uri(uri); return -1; } memcpy(pad_tmp_filename, to->tmp_filename, strlen(to->tmp_filename)); ptr = strstr(to->tmp_filename, PAD_SUFFIX); memcpy(tmp_filename, to->tmp_filename, (ptr - to->tmp_filename)); free_object(to, get_alc_session(fdt_th_args->s_id), 1); retval = padding_decoder(pad_tmp_filename, (int)file->file_len); if(retval == -1) { free(tmp_filename); free(pad_tmp_filename); free(tmp); free(filepath); free_uri(uri); return -1; } free(pad_tmp_filename); } if(rename(tmp_filename, fullpath) < 0) { if(errno == EEXIST) { retval = remove(fullpath); if(retval == -1) { printf("errno: %i\n", errno); fflush(stdout); } if(rename(tmp_filename, fullpath) < 0) { printf("rename() error1: %s\n", tmp_filename); fflush(stdout); } } else { printf("rename() error2: %s\n", tmp_filename); fflush(stdout); } } free(tmp); free(filepath); free_uri(uri); free(tmp_filename); to = get_alc_session(fdt_th_args->s_id)->obj_list; } return 0;} /* * This function calculates the amount of data the flute sender will send (including UDP/IP). * * Params: arguments_t *a: argument struct, * int *s_id: Pointer to session identifier, * int *ch_id: Pointer to channel identifier. * * Return: unsigned long long: Session size in bytes. * */#ifdef WIN32ULONGLONG #elseunsigned long long#endif flute_sender_size(arguments_t *a, int *s_id, int *ch_id) { bool old_flag = a->alc_a.calculate_session_size; int old_flag2 = a->alc_a.verbosity; int old_flag3 = a->alc_a.nb_tx; a->alc_a.calculate_session_size = true; a->alc_a.verbosity = 0; if(a->cont) { printf("Session size can't be calculated due to continuous Tx mode.\n"); a->alc_a.calculate_session_size = old_flag; a->alc_a.verbosity = old_flag2; a->alc_a.nb_tx = old_flag3; return 0; } flute_sender(a, s_id, ch_id); a->alc_a.calculate_session_size = old_flag; a->alc_a.verbosity = old_flag2; a->alc_a.nb_tx = old_flag3; return get_session_sent_bytes(*s_id);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -