⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 receiver.c

📁 这个程序实现了FLUTE协议
💻 C
📖 第 1 页 / 共 2 页
字号:
      buf = alc_recv2(s_id, &toi, &toi_len, &retcode);            if(buf == NULL) {	return retcode;      }            next_file = 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);    }        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    }#ifdef USE_OPENSSL    if(file->md5 != NULL) {	      md5 = file_md5(tmp_filename);	      if(md5 == NULL) {	  #ifdef WIN32	printf("MD5 check failed (TOI=%I64u)!\n", file->toi);#else	printf("MD5 check failed (TOI=%llu)!\n", file->toi);#endif	fflush(stdout);	remove(tmp_filename);	file->is_downloaded = 1;	  	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);	continue;      }      else{	if(strcmp(md5, file->md5) != 0) {#ifdef WIN32	  printf("MD5 check failed (TOI=%I64u)!\n", file->toi);#else	  printf("MD5 check failed (TOI=%llu)!\n", file->toi);#endif	  fflush(stdout);	  remove(tmp_filename);	  free(md5);	  file->is_downloaded = 1;	  	  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);	  continue;	}		free(md5);      }    }#endif    if(file->encoding != NULL) {	      if(strcmp(file->encoding, "pad") == 0) {	  	retcode = padding_decoder(tmp_filename, (int)file->file_len);	  	if(retcode == -1) {	  free(tmp_filename);	  return -1;	}		*(tmp_filename + (strlen(tmp_filename) - PAD_SUFFIX_LEN)) = '\0';	  	if(stat(tmp_filename, &file_stats) == -1) {	  printf("Error: %s is not valid file name\n", tmp_filename);	  fflush(stdout);	  return -1;	}		if(file_stats.st_size != file->file_len) {	  printf("stats: %i file: %i\n", (int)file_stats.st_size, (int)file->file_len);	  printf("Error: padding decoding failed, file size not ok.\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';		if(stat(tmp_filename, &file_stats) == -1) {	  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, file-size not ok.\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-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));        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(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);		printf("fullpath: %s\n", fullpath);		printf("errno: %i\n", errno);		fflush(stdout);      }    }        if(rx_memory_mode == 2){      memset(tmp_filename, 0, MAX_PATH);#ifdef WIN32      sprintf(tmp_filename, "%s\\st%I64u", session_basedir, toi);#else      sprintf(tmp_filename, "%s/st%llu", session_basedir, toi);#endif      retval = remove(tmp_filename);      if(retval == -1) {	printf("errno: %i\n", errno);	fflush(stdout);      }    }    if(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);    }        free_uri(uri);		    free(tmp);    free(filepath);    #ifdef WIN32    if(openfile) {      ShellExecute(NULL, "Open", fullpath, NULL, NULL, SW_SHOWNORMAL);    }#endif	          #ifdef WIN32    Sleep(1);#else    usleep(1000);#endif  }  return 1;}/*   * This function receives and decodes FDT Instance. *  * Params:	void *s: Pointer to alc session	 * * Return:	void * */void* fdt_thread(void *a) {    fdt_th_args_t *args;  char *buf = NULL;  #ifdef WIN32  ULONGLONG buflen = 0;#else  unsigned long long buflen = 0;#endif    int updated;    fdt_t *fdt_instance;  time_t systime;  file_t *file;  file_t *next_file;  int retval;  #ifdef WIN32  ULONGLONG curr_time;#else  unsigned long long curr_time;#endif  #ifdef USE_ZLIB  char *uncompr_buf = NULL;  #ifdef WIN32  ULONGLONG uncompr_buflen = 0;#else  unsigned long long uncompr_buflen = 0;#endif  #endif  unsigned char content_enc_algo = 0;  unsigned char fdt_content_enc_algo = 0;  int fdt_instance_id = 0;    args = (fdt_th_args_t *)a;   while(get_session_state(args->s_id) == SActive) {        time(&systime);    curr_time = systime + 2208988800U;        /* Get initial fdt */    if(args->fdt == NULL) {            buf = fdt_recv(args->s_id, &buflen, &retval, &fdt_content_enc_algo, &fdt_instance_id);            if(buf == NULL) {		if(retval == -1) {	  continue;	}#ifdef WIN32	_endthread();#else	pthread_exit(0);#endif      }#ifdef USE_ZLIB      if(fdt_content_enc_algo == ZLIB) {	uncompr_buf = buffer_zlib_uncompress(buf, buflen, &uncompr_buflen);		if(uncompr_buf == NULL) {	  free(buf);	  continue;	}	fdt_instance = decode_fdt_payload(uncompr_buf);	free(uncompr_buf);      }      else {	fdt_instance = decode_fdt_payload(buf);      }#else       fdt_instance = decode_fdt_payload(buf);#endif            if(fdt_instance == NULL) {	free(buf);	continue;      }            if(fdt_instance->expires < curr_time) {		if(!args->accept_expired_fdt_inst) {	  	  if(args->verbosity == 4) {	    printf("Expired FDT Instance received, discarding\n");	    fflush(stdout);	  }	  free(buf);	  FreeFDT(fdt_instance);	  continue;	}	else {	  if(args->verbosity == 4) {	    printf("Expired FDT Instance received, using it anyway\n");	    fflush(stdout);	  }	}      }            args->fdt = fdt_instance;            if(args->verbosity == 4) {	printf("FDT Instance received (ID=%i)\n", fdt_instance_id);	printf("FDT updated, new file description(s) added\n");	fflush(stdout);	PrintFDT(fdt_instance, args->s_id);      }      free(buf);            next_file = args->fdt->file_list;	      while(next_file != NULL) {	file = next_file;		if(file->is_downloaded == 0) {	  	  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->is_downloaded = 2;	      next_file = file->next;	      continue;	    }	  }	  if(args->rx_automatic) {	    retval = set_wanted_object(args->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(retval < 0) {	      /* Memory error */	    }	    else {	      file->is_downloaded = 1;	    }	  }	  else if( (args->filetoken != NULL) && (strstr(file->location, args->filetoken) != NULL) ) {	    retval = set_wanted_object(args->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(retval < 0) {	      /* Memory error */	    }	    else {	      file->is_downloaded = 1;	    }	  }	}		 	next_file = file->next;      }    }    else { /* Receive new FDT Instance when it comes */      updated = 0;            buf = fdt_recv(args->s_id, &buflen, &retval, &fdt_content_enc_algo, &fdt_instance_id);            if(buf == NULL) {		if(retval == -1) {	  continue;	}	#ifdef WIN32	_endthread();#else	pthread_exit(0);#endif      }#ifdef USE_ZLIB      if(fdt_content_enc_algo == ZLIB) {	uncompr_buf = buffer_zlib_uncompress(buf, buflen, &uncompr_buflen);		if(uncompr_buf == NULL) {	  free(buf);	  continue;	}	fdt_instance = decode_fdt_payload(uncompr_buf);	free(uncompr_buf);      }      else {	fdt_instance = decode_fdt_payload(buf);      }#else       fdt_instance = decode_fdt_payload(buf);#endif            if(fdt_instance == NULL) {	free(buf);	continue;      }            if(fdt_instance->expires < curr_time) {	if(!args->accept_expired_fdt_inst) {	  if(args->verbosity == 4) {	    printf("Expired FDT Instance received, discarding\n");	    fflush(stdout);	  }	  FreeFDT(fdt_instance);	  free(buf);	  continue;	}	else {	  if(args->verbosity == 4) {	    printf("Expired FDT Instance received, using it anyway\n");	    fflush(stdout);	  }	}      }            if(args->verbosity == 4) {	printf("FDT Instance received (ID=%i)\n", fdt_instance_id);	fflush(stdout);      }      free(buf);            if((fdt_instance->complete == true)&&(args->fdt->complete == false)&&(fdt_instance_id == 0)) {	args->fdt->complete = true;      }            updated = update_fdt(args->fdt, fdt_instance);            if(updated < 0) {	continue;      }      else if(updated == 1) {	if(args->verbosity == 4) {	  printf("FDT updated, file description(s) complemented\n");	  fflush(stdout);	}      }      else if(updated == 2) {	if(args->verbosity == 4) {	  printf("FDT updated, new file description(s) added\n");	  fflush(stdout);	  PrintFDT(fdt_instance, args->s_id);	}		next_file = args->fdt->file_list;		while(next_file != NULL) {	  file = next_file;	  	  if(file->is_downloaded == 0) {	    	    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->is_downloaded = 2;		next_file = file->next;		continue;	      }	    }	    	    if(args->rx_automatic) {  	      	      retval = set_wanted_object(args->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(retval < 0) {		/* Memory error */	      }	      else {		file->is_downloaded = 1;	      }	    }	    else if(((args->filetoken != NULL) && (strstr(file->location, args->filetoken) != NULL))) {	      retval = set_wanted_object(args->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(retval < 0) {		/* Memory error */	      }	      else {		file->is_downloaded = 1;	      }	    }	  }		  	  next_file = file->next;	}      }            FreeFDT(fdt_instance);	}  }#ifdef WIN32  _endthread();#else  pthread_exit(0);#endif	return NULL;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -