📄 flute.c
字号:
printf("\nWhich one to download: "); fflush(stdout); if(fgets(input, 100, stdin) != NULL) { #ifdef WIN32 retcode = sscanf(input, "%I64u", &rec_toi);#else retcode = sscanf(input, "%llu", &rec_toi);#endif if(!((retcode == 0) || (retcode == EOF))) { file = fdt_th_args->fdt->file_list; valid_toi = false; expired_toi = false; while(file != NULL) { if(file->toi == rec_toi) { time(&systime); curr_time = systime + 2208988800U; if(file->expires < curr_time) { if(!a->alc_a.accept_expired_fdt_inst) { expired_toi = true;#ifdef WIN32 printf("\nToi: %I64u Expired.\n", rec_toi); #else printf("\nToi: %llu Expired.\n", rec_toi); #endif if(file->next != NULL) { file->next->prev = file->prev; } if(file->prev != NULL) { file->prev->next = file->next; } if(file == fdt_th_args->fdt->file_list) { fdt_th_args->fdt->file_list = file->next; } free_file(file); break; } else {#ifdef WIN32 printf("\nToi: %I64u Expired, receiving anyway.\n", rec_toi); #else printf("\nToi: %llu Expired, receiving anyway.\n", rec_toi); #endif } } valid_toi = true; uri = parse_uri(file->location, strlen(file->location)); filepath = get_uri_host_and_path(uri); if(file->encoding == NULL) { content_enc_algo = 0; } else { if(strcmp(file->encoding, "pad") == 0) { content_enc_algo = PAD; }#ifdef USE_ZLIB else if(strcmp(file->encoding, "gzip") == 0) { content_enc_algo = GZIP; }#endif else { content_enc_algo = -1; break; } } retval = set_wanted_object(*s_id, file->toi, file->toi_len, file->es_len, file->max_sb_len, file->fec_inst_id, file->fec_enc_id, file->max_nb_of_es, content_enc_algo); break; } file = file->next; } if(expired_toi) { free_uri(uri); free(filepath); continue; } if(!valid_toi) {#ifdef WIN32 printf("\nToi: %I64u invalid.\n", rec_toi); #else printf("\nToi: %llu invalid.\n", rec_toi); #endif fflush(stdout); continue; } if(content_enc_algo == -1) { printf("Content-Encoding: %s not supported\n", file->encoding); fflush(stdout); continue; } retcode = recvfile(*s_id, filepath, file->toi, file->file_len, file->md5, a->alc_a.rx_memory_mode, file->encoding, a->alc_a.verbosity); if(retcode == -1) { free(filepath); free_uri(uri); printf("\nError: recvfile() failed\n"); fflush(stdout); return -1; } else if(retcode == -2) { free(filepath); free_uri(uri); return -2; } else if(retcode == -3) { free(filepath); free_uri(uri); return -3; }#ifdef WIN32 if(((retcode != -4) && (a->open_file))) { ShellExecute(NULL, "Open", filepath, NULL, NULL, SW_SHOWNORMAL); }#endif free(filepath); } } } else if(command == 'q') { break; } else { continue; } } } } return 0;}/* * This function is flute receiver's file list 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_file_list_mode(int *s_id, arguments_t *a, fdt_th_args_t *fdt_th_args) { file_t *file; file_t *next_file; time_t systime; #ifdef WIN32 ULONGLONG curr_time;#else unsigned long long curr_time;#endif char *buf = NULL; #ifdef WIN32 ULONGLONG toi; ULONGLONG toi_len;#else unsigned long long toi; unsigned long long toi_len; #endif int fd; char *ptr; int point; int ch = '/'; int i; char *tmp = NULL; char fullpath[MAX_PATH]; char filename[MAX_PATH]; char *filepath = NULL; bool file_to_receive = false; int retval = 0; int retcode = 0; char *filetoken = NULL; char *filetable[100]; int file_nb = 0; int b; char tmp_filename[MAX_PATH]; char *tmp_file_name = NULL; uri_t *uri = NULL; unsigned char content_enc_algo; /*struct stat file_stats;*/#ifdef WIN32 struct __stat64 file_stats; #else struct stat64 file_stats; #endif#ifdef USE_OPENSSL char *md5 = NULL;#endif bool any_files_received = false; char* session_basedir = get_session_basedir(*s_id); char *cont_desc = NULL; if(strcmp(a->sdp_file, "") != 0) { cont_desc = sdp_attr_get(a->sdp, "content-desc"); } printf("FLUTE Receiver in file list mode\n"); if(cont_desc != NULL) { printf("Session content information available at:\n"); printf("%s\n", cont_desc); } /* Without TOI-number */ /* check wanted file names */ filetoken = strtok(a->file_path, ","); printf("Wanted file(s):"); fflush(stdout); while(filetoken != NULL) { filetable[file_nb] = filetoken; printf(" %s", filetable[file_nb]); fflush(stdout); file_nb++; filetoken = strtok(NULL, ","); } printf("\n"); fflush(stdout); while(fdt_th_args->fdt == NULL) { if(get_session_state(*s_id) == SExiting) { #ifdef WIN32 Sleep(500);#else usleep(500000);#endif return -2; } else if(get_session_state(*s_id) == STxStopped) { #ifdef WIN32 Sleep(500);#else usleep(500000);#endif return -3; } #ifdef WIN32 Sleep(1);#else usleep(1000);#endif continue; } /* receive wanted files */ while(!all_files_received(filetable, file_nb)) { if(get_session_state(*s_id) == SExiting) { return -2; } else if(get_session_state(*s_id) == STxStopped) { return -3; } file_to_receive = false; file = fdt_th_args->fdt->file_list; while(file != NULL) { for(b = 0; b < file_nb; b++) { if(filetable[b] == NULL) { continue; } else { if(strstr(file->location, filetable[b]) != NULL) { time(&systime); curr_time = systime + 2208988800U; if(((file->expires < curr_time) && (!a->alc_a.accept_expired_fdt_inst))) { if(file->next != NULL) { file->next->prev = file->prev; } if(file->prev != NULL) { file->prev->next = file->next; } if(file == fdt_th_args->fdt->file_list) { fdt_th_args->fdt->file_list = file->next; } free_file(file); continue; } if(file->is_downloaded != 2) { if(file->encoding == NULL) { content_enc_algo = 0; } else { if(strcmp(file->encoding, "pad") == 0) { content_enc_algo = PAD; }#ifdef USE_ZLIB else if(strcmp(file->encoding, "gzip") == 0) { content_enc_algo = GZIP; }#endif else { printf("Content-Encoding: %s not supported\n", file->encoding); fflush(stdout); file = file->next; file->is_downloaded = 2; continue; } } file_to_receive = true; retcode = set_wanted_object(*s_id, file->toi, file->toi_len, file->es_len, file->max_sb_len, file->fec_inst_id, file->fec_enc_id, file->max_nb_of_es, content_enc_algo); if(retcode < 0) { set_session_state(*s_id, SExiting); return -1; } file->is_downloaded = 1; } continue; } } } file = file->next; } i = 0; toi = 0; toi_len = 0; if(file_to_receive == false) { if(fdt_th_args->fdt->complete) { if(!any_files_received) { printf("No wanted file(s) in the session\n"); fflush(stdout); } break; }#ifdef WIN32 Sleep(1);#else usleep(1000);#endif continue; } if(a->alc_a.rx_memory_mode == 1) { tmp_file_name = alc_recv3(*s_id, &toi, &retcode); if(tmp_file_name == NULL) { return retcode; } memset(tmp_filename, 0, MAX_PATH); memcpy(tmp_filename, tmp_file_name, strlen(tmp_file_name)); free(tmp_file_name); next_file = fdt_th_args->fdt->file_list; /* Find correct file structure, to get the file->location for file creation purpose */ while(next_file != NULL) { file = next_file; if(file->toi == toi) { file->is_downloaded = 2; any_files_received = true; break; } next_file = file->next; } } else if(a->alc_a.rx_memory_mode == 2) { printf("-b:2 option is not supported with file list mode.\n"); fflush(stdout); return -1; } else { buf = alc_recv2(*s_id, &toi, &toi_len, &retcode); if(buf == NULL) { return retcode; } next_file = fdt_th_args->fdt->file_list; /* Find correct file structure, to get the file->location for file creation purpose */ while(next_file != NULL) { file = next_file; if(file->toi == toi) { file->is_downloaded = 2; break; } next_file = file->next; } /* open tmp file and write buffer to it */ memset(tmp_filename, 0, MAX_PATH); if(file->encoding == NULL) {#ifdef WIN32 sprintf(tmp_filename, "%s\\object%I64u", session_basedir, toi);#else sprintf(tmp_filename, "%s/object%llu", session_basedir, toi);#endif }#ifdef USE_ZLIB else if(strcmp(file->encoding, "gzip") == 0) {#ifdef WIN32 sprintf(tmp_filename, "%s\\object%I64u%s", session_basedir, toi, GZ_SUFFIX);#else sprintf(tmp_filename, "%s/object%llu%s", session_basedir, toi, GZ_SUFFIX);#endif }#endif else if(strcmp(file->encoding, "pad") == 0) {#ifdef WIN32 sprintf(tmp_filename, "%s\\object%I64u%s", session_basedir, toi, PAD_SUFFIX);#else sprintf(tmp_filename, "%s/object%llu%s", session_basedir, toi, PAD_SUFFIX);#endif } #ifdef WIN32 if((fd = open((const char*)tmp_filename, _O_WRONLY | _O_CREAT | _O_BINARY | _O_TRUNC , _S_IWRITE)) < 0) {#else if((fd = open(tmp_filename, O_WRONLY | O_CREAT | O_TRUNC , S_IRWXU)) < 0) {#endif printf("Error: unable to open file %s\n", tmp_filename); fflush(stdout); free(buf); return MEM_ERROR; } if(write(fd, buf, (unsigned int)toi_len) == -1) {#ifdef WIN32 printf("write error, toi: %I64u\n", toi);#else printf("write error, toi: %llu\n", toi);#endif fflush(stdout); free(buf); close(fd); return MEM_ERROR; } free(buf); close(fd); }#ifdef USE_OPENSSL if(file->md5 != NULL) { md5 = file_md5(tmp_filename); if(md5 == NULL) { printf("MD5 check failed!\n"); fflush(stdout); remove(tmp_filename); return -4; } else{ if(strcmp(md5, file->md5) != 0) { printf("MD5 check failed!\n"); fflush(stdout); remove(tmp_filename);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -