receiver.c

Go to the documentation of this file.
00001 
00032 #include <stdlib.h>
00033 #include <stdio.h>
00034 #include <sys/stat.h>
00035 #include <fcntl.h>
00036 #include <errno.h>
00037 #include <time.h>
00038 #include <string.h>
00039 
00040 #ifdef _MSC_VER
00041 #define _WINSOCKAPI_   /* Prevent inclusion of winsock.h in windows.h */
00042 #include <windows.h>
00043 #include <process.h>
00044 #include <io.h>
00045 #include <direct.h>
00046 #include <shellapi.h>
00047 #else
00048 #include <ctype.h>
00049 #include <unistd.h>
00050 #endif
00051 
00052 #include "../alclib/alc_session.h"
00053 #include "../alclib/alc_rx.h"
00054 
00055 #include "receiver.h"
00056 #include "mad_zlib.h"
00057 #include "mad_md5.h"
00058 #include "padding_encoding.h"
00059 #include "uri.h"
00060 #include "fdt.h"
00061 
00062 
00072  int all_files_received(char *file_uri_table[FILE_URI_TABLE_SIZE]) {
00073 
00074    int i;
00075    int retval = 1;
00076 
00077    for(i = 0; i < FILE_URI_TABLE_SIZE; i++) {
00078      if(file_uri_table[i] != NULL) {
00079        retval = 0;
00080      }
00081    }
00082 
00083    return retval;
00084  }
00085 
00094 void set_file_received(char *file_uri_table[FILE_URI_TABLE_SIZE], char *file_uri) {
00095 
00096   int i;
00097   
00098   for(i = 0; i < FILE_URI_TABLE_SIZE; i++) {
00099     
00100     if(file_uri_table[i] != NULL) {
00101       if(strcmp(file_uri, file_uri_table[i]) == 0) {
00102         file_uri_table[i] = NULL;
00103         break;
00104       }
00105     }
00106   }
00107 }
00108 
00126 int recvfile(int s_id, char *filepath, unsigned long long toi,
00127              unsigned long long file_len,
00128              char *md5, int rx_memory_mode, char *encoding, int verbosity) {
00129   
00130 #ifdef USE_ZLIB
00131   char *uncompr_buf = NULL;
00132   unsigned long long uncompr_buflen = 0;
00133 #endif
00134 
00135   unsigned char fdt_cont_enc_algo = 0;
00136   int fdt_instance_id = 0;
00137 
00138   struct stat file_stats;
00139   
00140   unsigned long long recvbytes = 0;
00141   
00142   char *buf = NULL;
00143   int fd;
00144   int j;
00145 
00146   char filename[MAX_PATH_LENGTH];
00147   char tmp_filename[MAX_PATH_LENGTH];
00148   
00149   char *tmp_file_name;
00150   int retcode = 0;
00151 
00152   int retval;
00153   char fullpath[MAX_PATH_LENGTH];
00154   char *tmp = NULL;
00155   char *ptr;
00156   int point;
00157   int ch = '/';
00158   int i = 0;
00159   
00160 #ifdef USE_OPENSSL
00161   char *md5_digest = NULL;
00162 #endif
00163   
00164   char* session_basedir = get_session_basedir(s_id);
00165 
00166   if(filepath != NULL) {
00167 
00168     if(verbosity > 0) {  
00169 #ifdef _MSC_VER
00170       printf("Receiving file: %s (TOI=%I64u)\n", filepath, toi);
00171 #else
00172       printf("Receiving file: %s (TOI=%llu)\n", filepath, toi);
00173 #endif
00174       fflush(stdout);
00175     }
00176 
00177     for(j = 0; j < (int)strlen(filepath); j++) {
00178       if(*(filepath + j) == '\\') {
00179         *(filepath + j) = '/';
00180       }
00181     }
00182   }
00183   else {
00184 
00185     if(verbosity > 0) {
00186 #ifdef _MSC_VER
00187       printf("Receiving object: TOI=%I64u\n", toi);
00188 #else
00189       printf("Receiving object: TOI=%llu\n", toi);
00190 #endif
00191       fflush(stdout);
00192     }
00193   }
00194 
00195   if(toi == FDT_TOI) {
00196     
00197     buf = fdt_recv(s_id, &recvbytes, &retcode, &fdt_cont_enc_algo, &fdt_instance_id);
00198     
00199     if(buf == NULL) {
00200       return retcode;
00201     }
00202 
00203     /* open tmp file */
00204 
00205     memset(tmp_filename, 0, MAX_PATH_LENGTH);
00206     sprintf(tmp_filename, "%s/%s", session_basedir, "object_XXXXXX");
00207     mktemp(tmp_filename);
00208 
00209 #ifdef _MSC_VER
00210     if((fd = open((const char*)tmp_filename,
00211                   _O_WRONLY | _O_CREAT | _O_BINARY | _O_TRUNC , _S_IWRITE)) < 0) {
00212 #else
00213     if((fd = open(tmp_filename, O_WRONLY | O_CREAT | O_TRUNC , S_IRWXU)) < 0) {
00214 #endif
00215       printf("Error: unable to open file %s\n", tmp_filename);
00216       fflush(stdout);
00217       return MEM_ERROR;
00218     }
00219     
00220     if(fdt_cont_enc_algo == 0) {
00221 
00222       /* write buf to tmp file */
00223 
00224       if(write(fd, buf, (unsigned int)recvbytes) == -1) {
00225 #ifdef _MSC_VER
00226         printf("write error, toi: %I64u\n", toi);
00227 #else
00228         printf("write error, toi: %llu\n", toi);
00229 #endif
00230         fflush(stdout);
00231         free(buf);
00232         close(fd);
00233         return MEM_ERROR;
00234       }
00235       free(buf);
00236     }
00237 #ifdef USE_ZLIB
00238     else if(fdt_cont_enc_algo == ZLIB) {
00239       uncompr_buf = buffer_zlib_uncompress(buf, recvbytes, &uncompr_buflen);
00240       
00241       if(uncompr_buf == NULL) {
00242         free(buf);
00243         return -1;
00244       }
00245       free(buf);
00246       
00247       /* write uncompr_buf to tmp file */
00248       
00249       if(write(fd, uncompr_buf, (unsigned int)uncompr_buflen) == -1) {
00250 #ifdef _MSC_VER
00251         printf("write error, toi: %I64u\n", toi);
00252 #else
00253         printf("write error, toi: %llu\n", toi);
00254 #endif
00255         fflush(stdout);
00256         free(uncompr_buf);
00257         close(fd);
00258         return -1;
00259       }
00260       free(uncompr_buf);
00261     }
00262 #endif
00263     close(fd);
00264   }
00265   else {        
00266     if(rx_memory_mode == 1) {
00267       
00268       tmp_file_name = alc_recv3(s_id, &toi, &retcode);
00269       
00270       if(tmp_file_name == NULL) {
00271         return retcode;
00272       }
00273       
00274       memset(tmp_filename, 0, MAX_PATH_LENGTH);
00275       memcpy(tmp_filename, tmp_file_name, strlen(tmp_file_name));
00276       free(tmp_file_name);    
00277     }
00278     else if(rx_memory_mode == 2) {
00279       printf("-b:2 option is not supported with object mode\n");
00280       fflush(stdout);
00281       return -1;
00282     }
00283     else {
00284       
00285       buf = alc_recv(s_id, toi, &recvbytes, &retcode);
00286       
00287       if(buf == NULL) {
00288         return retcode;
00289       }
00290 
00291       /* open tmp file */
00292 
00293       memset(tmp_filename, 0, MAX_PATH_LENGTH);
00294 
00295       if(encoding == NULL) {
00296            sprintf(tmp_filename, "%s/%s", session_basedir, "object_XXXXXX");
00297            mktemp(tmp_filename);
00298       }
00299 #ifdef USE_ZLIB
00300       else if(strcmp(encoding, "gzip") == 0) {
00301            sprintf(tmp_filename, "%s/%s", session_basedir, "object_XXXXXX");
00302            strcat(tmp_filename, GZ_SUFFIX);
00303            mktemp(tmp_filename);
00304       }
00305 #endif
00306       else if(strcmp(encoding, "pad") == 0) {
00307            sprintf(tmp_filename, "%s/%s", session_basedir, "object_XXXXXX");
00308            strcat(filename, PAD_SUFFIX);
00309            mktemp(tmp_filename);
00310       }
00311 
00312 #ifdef _MSC_VER
00313       if((fd = open((const char*)tmp_filename,
00314                     _O_WRONLY | _O_CREAT | _O_BINARY | _O_TRUNC , _S_IWRITE)) < 0) {
00315 #else
00316       if((fd = open(tmp_filename, O_WRONLY | O_CREAT | O_TRUNC , S_IRWXU)) < 0) {
00317 #endif
00318         printf("Error: unable to open file %s\n", tmp_filename);
00319         fflush(stdout);
00320         return -1;
00321       }
00322       
00323       /* write buffer to tmp file */
00324       
00325       if(write(fd, buf, (unsigned int)recvbytes) == -1) {
00326 #ifdef _MSC_VER
00327         printf("write error, toi: %I64u\n", toi);
00328 #else
00329         printf("write error, toi: %llu\n", toi);
00330 #endif
00331         fflush(stdout);
00332         free(buf);
00333         close(fd);
00334         return -1;
00335       }
00336       
00337       free(buf);
00338       close(fd);
00339     }
00340       
00341 #ifdef USE_OPENSSL
00342     if(md5 != NULL) {
00343             
00344       md5_digest = file_md5(tmp_filename);
00345       
00346       if(md5_digest == NULL) {
00347         printf("MD5 check failed!\n");
00348         fflush(stdout);
00349         remove(tmp_filename);
00350         return -4;
00351       }
00352       else{
00353         if(strcmp(md5, md5_digest) != 0) {
00354           printf("MD5 check failed!\n");
00355           fflush(stdout);
00356           remove(tmp_filename);
00357           free(md5_digest);
00358           return -4;
00359         }
00360         
00361         free(md5_digest);
00362       }
00363     }
00364 #endif
00365     if(encoding != NULL) {
00366       
00367       if(strcmp(encoding, "pad") == 0) {
00368         
00369         retcode = padding_decoder(tmp_filename, (int)file_len);
00370         
00371         if(retcode == -1) {
00372           return -1;             
00373         }
00374         
00375         *(tmp_filename + (strlen(tmp_filename) - PAD_SUFFIX_LEN)) = '\0';
00376         
00377         if(file_len != 0) {
00378           
00379           if(stat(tmp_filename, &file_stats) == -1) {
00380             printf("Error: %s is not valid file name\n", tmp_filename);
00381             fflush(stdout);
00382             return -1;
00383           }
00384           if(file_stats.st_size != file_len) {
00385             printf("Error: padding decoding failed\n");
00386             remove(tmp_filename);
00387             fflush(stdout);
00388             return -1;
00389           }
00390         }
00391       }
00392 #ifdef USE_ZLIB
00393       else if(strcmp(encoding, "gzip") == 0) {
00394         
00395         retcode = file_gzip_uncompress(tmp_filename);
00396         
00397         if(retcode == -1) {
00398           return -1;
00399         }
00400         
00401         *(tmp_filename + (strlen(tmp_filename) - GZ_SUFFIX_LEN)) = '\0';
00402         
00403         if(file_len != 0) {
00404           
00405           if(stat(tmp_filename, &file_stats) == -1) {
00406             printf("Error: %s is not valid file name\n", tmp_filename);
00407             fflush(stdout);
00408             return -1;
00409           }
00410           
00411           if(file_stats.st_size != file_len) {
00412             printf("Error: uncompression failed\n");
00413             remove(tmp_filename);
00414             fflush(stdout);
00415             return -1;
00416           }
00417         }
00418       }
00419 #endif
00420     }
00421   }
00422   
00423   if(filepath == NULL) {
00424 
00425     if(verbosity > 0) {      
00426 #ifdef _MSC_VER
00427       printf("Object received: TOI=%I64u\n", toi);
00428 #else
00429       printf("Object received: TOI=%llu\n", toi);
00430 #endif
00431       fflush(stdout);
00432     }
00433 
00434     return 1;
00435   }
00436 
00437   if(!(tmp = (char*)calloc((strlen(filepath) + 1), sizeof(char)))) {
00438     printf("Could not alloc memory for tmp (filepath)!\n");
00439     fflush(stdout);    
00440     return -1;
00441   }
00442   
00443   memcpy(tmp, filepath, strlen(filepath));
00444   
00445   ptr = strchr(tmp, ch);
00446   
00447   memset(fullpath, 0, MAX_PATH_LENGTH);
00448   memcpy(fullpath, session_basedir, strlen(session_basedir));
00449   
00450   if(ptr != NULL) {
00451     
00452     while(ptr != NULL) {
00453       
00454       i++;
00455       
00456       point = (int)(ptr - tmp);
00457       
00458       memset(filename, 0, MAX_PATH_LENGTH);
00459       memcpy((fullpath + strlen(fullpath)), "/", 1);
00460       memcpy((fullpath + strlen(fullpath)), tmp, point);
00461       
00462       memcpy(filename, (tmp + point + 1), (strlen(tmp) - (point + 1)));
00463       
00464 #ifdef _MSC_VER
00465       if(mkdir(fullpath) < 0) {                                 
00466 #else           
00467       if(mkdir(fullpath, S_IRWXU) < 0) {
00468 #endif
00469         if(errno != EEXIST) {
00470           printf("mkdir failed: cannot create directory %s (errno=%i)\n", fullpath, errno);
00471           fflush(stdout);
00472           
00473           if(toi == FDT_TOI) {
00474 #ifdef USE_ZLIB
00475             if(fdt_cont_enc_algo == ZLIB) {
00476               free(uncompr_buf);
00477             }
00478             else {
00479               free(buf);
00480             }
00481 #else
00482             free(buf);
00483 #endif
00484           }
00485           free(tmp);
00486           return -1;
00487         }
00488       }
00489       
00490       strcpy(tmp, filename);
00491       ptr = strchr(tmp, ch);
00492     }
00493     memcpy((fullpath + strlen(fullpath)), "/", 1);
00494         memcpy((fullpath + strlen(fullpath)), filename, strlen(filename));
00495   }
00496   else{
00497     memcpy((fullpath + strlen(fullpath)), "/", 1);
00498     memcpy((fullpath + strlen(fullpath)), filepath, strlen(filepath));
00499   }
00500     
00501   if(rename(tmp_filename, fullpath) < 0) {
00502       
00503     if(errno == EEXIST) {
00504       retval = remove(fullpath);
00505       
00506       if(retval == -1) {
00507         printf("errno: %i\n", errno);
00508       }
00509       
00510       if(rename(tmp_filename, fullpath) < 0) {
00511         printf("rename() error1\n");
00512       }
00513     }
00514     else {
00515       printf("rename() error2\n");
00516     }
00517   }
00518     
00519   free(tmp);
00520     
00521   if(verbosity > 0) {
00522 #ifdef _MSC_VER
00523     printf("File received: %s (TOI=%I64u)\n", filepath, toi);
00524 #else
00525     printf("File received: %s (TOI=%llu)\n", filepath, toi);
00526 #endif
00527     fflush(stdout);
00528   }
00529   
00530   return 1;
00531 }
00532 
00545 int fdtbasedrecv(int rx_memory_mode, BOOL openfile, flute_receiver_t *receiver) {
00546   file_t *file = NULL;
00547   file_t *next_file = NULL;
00548   time_t systime;
00549   
00550   unsigned long long curr_time;
00551 
00552   unsigned long long toi;
00553   unsigned long long transfer_len;
00554   int i;
00555   char filename[MAX_PATH_LENGTH];
00556   
00557   BOOL is_all_files_received;
00558   BOOL is_printed = FALSE;
00559   BOOL any_files_received = FALSE;
00560   
00561   int retcode;
00562   
00563   unsigned char content_enc_algo = 0;
00564   struct stat file_stats;
00565   int retval;
00566   char *tmp = NULL;
00567   char fullpath[MAX_PATH_LENGTH];
00568   char *filepath = NULL;
00569   char *ptr = NULL;
00570   int point;
00571   int ch = '/';
00572   uri_t *uri = NULL;
00573 
00574 #ifdef USE_OPENSSL
00575   char *md5= NULL;
00576 #endif
00577   
00578   char* session_basedir = NULL;
00579   
00580   /* rx_memory_mode == 1 || 2 */
00581   char *tmp_file_name;
00582 
00583   /* rx_memory_mode == 0 */
00584   char *buf = NULL;
00585   int fd;
00586 
00587   char tmp_filename[MAX_PATH_LENGTH];
00588 
00589   session_basedir = get_session_basedir(receiver->s_id);
00590   
00591   while(1) {
00592     
00593     if(get_session_state(receiver->s_id) == SExiting) {
00594       return -2;
00595     }
00596     else if(get_session_state(receiver->s_id) == STxStopped) {
00597       return -3;
00598     }
00599     
00600     is_all_files_received = TRUE;
00601     
00602     time(&systime);
00603     curr_time = systime + 2208988800U;
00604     
00605     next_file = receiver->fdt->file_list;
00606     
00607     while(next_file != NULL) {
00608       
00609       file = next_file;
00610       i = 0;
00611       
00612       if(((file->expires < curr_time) && (!receiver->accept_expired_fdt_inst))) {
00613         
00614         if(file->next != NULL) {
00615           file->next->prev = file->prev;
00616         }
00617         
00618         if(file->prev != NULL) {
00619           file->prev->next = file->next;
00620         }
00621         
00622         if(file == receiver->fdt->file_list) {
00623           receiver->fdt->file_list = file->next;
00624         }
00625         
00626         if(file->encoding != NULL) {
00627           free(file->encoding);
00628         }                                       
00629         
00630         if(file->location != NULL) {
00631           free(file->location);
00632         }                                                       
00633         
00634         if(file->md5 != NULL) {
00635           free(file->md5);
00636         }
00637         
00638         if(file->type != NULL) {
00639           free(file->type);
00640         }
00641         
00642         next_file = file->next;
00643         free(file);
00644 
00645         continue;
00646       }
00647       
00648       if(receiver->rx_automatic) {
00649         if(file->status != 2) {
00650           is_all_files_received = FALSE;
00651           is_printed = FALSE;
00652         }
00653         if(file->status == 2) {
00654           any_files_received = TRUE;
00655         }
00656       }
00657       else if(receiver->wildcard_token != NULL && strstr(file->location, receiver->wildcard_token) != NULL) {
00658         if(file->status != 2) {
00659           is_all_files_received = FALSE;
00660           is_printed = FALSE;
00661         }
00662         if(file->status == 2) {
00663           any_files_received = TRUE;
00664         }
00665       }
00666       else {
00667         if(all_files_received(receiver->file_uri_table)){
00668           return -2;
00669         }
00670       }
00671       
00672       next_file = file->next;
00673     }
00674     
00675     i = 0;
00676     retcode = 0;
00677     toi = 0;
00678     
00679     if(rx_memory_mode == 0) {
00680       transfer_len = 0;
00681     }
00682 
00683     if(receiver->rx_automatic || receiver->wildcard_token != NULL) { 
00684     
00685       if(is_all_files_received) {
00686         
00687         if(receiver->fdt->complete) {
00688           
00689           if(any_files_received) {
00690             if(receiver->verbosity > 0) {
00691               printf("All files received\n");
00692               fflush(stdout);
00693             }
00694           }
00695           else {
00696             if(receiver->verbosity > 0) {
00697               printf("No wanted files in the session\n");
00698               fflush(stdout);
00699             }
00700           }
00701         
00702           return 1;
00703         }
00704         else {
00705         
00706           if(((!is_printed) && (any_files_received))) {
00707             
00708             if(receiver->verbosity > 0) {
00709               printf("All files received, waiting for new files\n");
00710               fflush(stdout);
00711             }
00712             
00713             is_printed = TRUE;
00714           }
00715           
00716 #ifdef _MSC_VER
00717           Sleep(1);
00718 #else
00719           usleep(1000);
00720 #endif
00721           continue;
00722         }
00723       }
00724     }
00725     
00726     if(rx_memory_mode == 1 || rx_memory_mode == 2) {
00727       
00728       tmp_file_name = alc_recv3(receiver->s_id, &toi, &retcode);
00729       
00730       if(tmp_file_name == NULL) {
00731                 return retcode;
00732       }
00733       memset(tmp_filename, 0, MAX_PATH_LENGTH);
00734       memcpy(tmp_filename, tmp_file_name, strlen(tmp_file_name));
00735       free(tmp_file_name);
00736 
00737       next_file = receiver->fdt->file_list;
00738       
00739       /* Find correct file structure, to get the file->location for file creation purpose */
00740       
00741       while(next_file != NULL) {
00742                 file = next_file;
00743         
00744                 if(file->toi == toi) {
00745                 file->status = 2;
00746                 break;
00747                 }
00748         
00749                 next_file = file->next;
00750       }
00751     }
00752     else {
00753       
00754       buf = alc_recv2(receiver->s_id, &toi, &transfer_len, &retcode);
00755       
00756       if(buf == NULL) {
00757         return retcode;
00758       }
00759       
00760       next_file = receiver->fdt->file_list;
00761 
00762       /* Find correct file structure, to get the file->location for file creation purpose */
00763 
00764       while(next_file != NULL) {
00765         file = next_file;
00766 
00767         if(file->toi == toi) {
00768           file->status = 2;
00769           break;
00770         }
00771 
00772         next_file = file->next;
00773       }
00774 
00775       /* open tmp file and write buffer to it */
00776       
00777       memset(tmp_filename, 0, MAX_PATH_LENGTH);
00778       
00779       if(file->encoding == NULL) {
00780         sprintf(tmp_filename, "%s/%s", session_basedir, "object_XXXXXX");
00781         mktemp(tmp_filename);
00782       }
00783 #ifdef USE_ZLIB
00784       else if(strcmp(file->encoding, "gzip") == 0) {
00785         sprintf(tmp_filename, "%s/%s", session_basedir, "object_XXXXXX");
00786         strcat(filename, GZ_SUFFIX);
00787         mktemp(tmp_filename);
00788       }
00789 #endif
00790       else if(strcmp(file->encoding, "pad") == 0) {
00791         sprintf(tmp_filename, "%s/%s", session_basedir, "object_XXXXXX");
00792         strcat(filename, PAD_SUFFIX);
00793         mktemp(tmp_filename);
00794       }
00795 
00796 #ifdef _MSC_VER
00797       if((fd = open((const char*)tmp_filename,
00798                     _O_WRONLY | _O_CREAT | _O_BINARY | _O_TRUNC , _S_IWRITE)) < 0) {
00799 #else
00800       if((fd = open(tmp_filename, O_WRONLY | O_CREAT | O_TRUNC , S_IRWXU)) < 0) {
00801 #endif
00802         printf("Error: unable to open file %s\n", tmp_filename);
00803         fflush(stdout);
00804         free(buf);
00805         return MEM_ERROR;
00806       }
00807 
00808       if(write(fd, buf, (unsigned int)transfer_len) == -1) {
00809 #ifdef _MSC_VER
00810         printf("write error, toi: %I64u\n", toi);
00811 #else
00812         printf("write error, toi: %llu\n", toi);
00813 #endif
00814         fflush(stdout);
00815         free(buf);
00816         close(fd);
00817         return MEM_ERROR;
00818       }
00819 
00820       free(buf);
00821       close(fd);
00822     }
00823     
00824     if(file->encoding == NULL) {
00825       content_enc_algo = 0;
00826     }
00827     else {
00828       if(strcmp(file->encoding, "pad") == 0) {
00829                 content_enc_algo = PAD;
00830       }
00831 #ifdef USE_ZLIB
00832       else if(strcmp(file->encoding, "gzip") == 0) {
00833                 content_enc_algo = GZIP;
00834       }
00835 #endif
00836     }
00837 
00838 #ifdef USE_OPENSSL
00839     if(file->md5 != NULL) {
00840         
00841       md5 = file_md5(tmp_filename);
00842         
00843       if(md5 == NULL) {
00844           
00845 #ifdef _MSC_VER
00846         printf("MD5 check failed (TOI=%I64u)!\n", file->toi);
00847 #else
00848         printf("MD5 check failed (TOI=%llu)!\n", file->toi);
00849 #endif
00850         fflush(stdout);
00851         remove(tmp_filename);
00852         file->status = 1;
00853           
00854         set_wanted_object(receiver->s_id, file->toi, file->transfer_len, file->es_len,
00855                           file->max_sb_len,
00856                           file->fec_inst_id, file->fec_enc_id,
00857                           file->max_nb_of_es, content_enc_algo, file->finite_field,
00858                           file->nb_of_es_per_group
00859                           );
00860         continue;
00861       }
00862       else{
00863         if(strcmp(md5, file->md5) != 0) {
00864 #ifdef _MSC_VER
00865           printf("MD5 check failed (TOI=%I64u)!\n", file->toi);
00866 #else
00867           printf("MD5 check failed (TOI=%llu)!\n", file->toi);
00868 #endif
00869           fflush(stdout);
00870           remove(tmp_filename);
00871           free(md5);
00872           file->status = 1;
00873           
00874           set_wanted_object(receiver->s_id, file->toi, file->transfer_len, file->es_len,
00875                             file->max_sb_len,
00876                             file->fec_inst_id, file->fec_enc_id,
00877                             file->max_nb_of_es,
00878                                 content_enc_algo, file->finite_field, file->nb_of_es_per_group
00879                             );
00880           continue;
00881         }
00882         
00883         free(md5);
00884       }
00885     }
00886 #endif
00887 
00888     if(file->encoding != NULL) {
00889         
00890       if(strcmp(file->encoding, "pad") == 0) {
00891           
00892         retcode = padding_decoder(tmp_filename, (int)file->content_len);
00893           
00894         if(retcode == -1) {
00895           free(tmp_filename);
00896           return -1;
00897         }
00898         
00899         *(tmp_filename + (strlen(tmp_filename) - PAD_SUFFIX_LEN)) = '\0';
00900           
00901         if(stat(tmp_filename, &file_stats) == -1) {
00902           printf("Error: %s is not valid file name\n", tmp_filename);
00903           fflush(stdout);
00904           return -1;
00905         }
00906         
00907         if(file_stats.st_size != file->content_len) {
00908           printf("stats: %i file: %i\n", (int)file_stats.st_size, (int)file->content_len);
00909           printf("Error: padding decoding failed, file size not ok.\n");   
00910           fflush(stdout);
00911           remove(tmp_filename);
00912           return -1;
00913         }
00914       }
00915 #ifdef USE_ZLIB
00916       else if(strcmp(file->encoding, "gzip") == 0) {
00917           
00918         retcode = file_gzip_uncompress(tmp_filename);
00919           
00920         if(retcode == -1) {
00921           return -1;
00922         }
00923         
00924         *(tmp_filename + (strlen(tmp_filename) - GZ_SUFFIX_LEN)) = '\0';
00925         
00926         if(stat(tmp_filename, &file_stats) == -1) {
00927           printf("Error: %s is not valid file name\n", tmp_filename);
00928           fflush(stdout);
00929           return -1;
00930         }
00931         
00932         if(file_stats.st_size != file->content_len) {
00933           printf("Error: uncompression failed, file-size not ok.\n");
00934           fflush(stdout);
00935           remove(tmp_filename);
00936           return -1;
00937         }
00938       }
00939 #endif
00940     }
00941     
00942     uri = parse_uri(file->location, strlen(file->location));
00943   
00944     filepath = get_uri_host_and_path(uri);
00945     
00946     if(!(tmp = (char*)calloc((strlen(filepath) + 1), sizeof(char)))) {
00947       printf("Could not alloc memory for tmp-filepath!\n");
00948       fflush(stdout);
00949       
00950       free(filepath);
00951       free_uri(uri);
00952       return -1;
00953     }
00954     
00955     memcpy(tmp, filepath, strlen(filepath));
00956     
00957     ptr = strchr(tmp, ch);
00958     
00959     memset(fullpath, 0, MAX_PATH_LENGTH);
00960     memcpy(fullpath, session_basedir, strlen(session_basedir));
00961     
00962     if(ptr != NULL) {
00963       
00964       while(ptr != NULL) {
00965         
00966         i++;
00967         
00968         point = (int)(ptr - tmp);
00969         
00970         memset(filename, 0, MAX_PATH_LENGTH);
00971         memcpy((fullpath + strlen(fullpath)), "/", 1);
00972         memcpy((fullpath + strlen(fullpath)), tmp, point);
00973         
00974         memcpy(filename, (tmp + point + 1), (strlen(tmp) - (point + 1)));
00975         
00976 #ifdef _MSC_VER
00977         if(_mkdir(fullpath) != 0) {                                     
00978 #else           
00979         if(mkdir(fullpath, S_IRWXU) != 0) {
00980 #endif
00981           if(errno != EEXIST) {
00982             printf("mkdir failed: cannot create directory %s (errno=%i)\n", fullpath, errno);
00983             fflush(stdout);
00984             
00985             free(tmp);
00986             free(filepath);
00987             free_uri(uri);
00988             return -1;
00989           }
00990         }
00991         
00992         strcpy(tmp, filename);
00993         ptr = strchr(tmp, ch);
00994       }
00995       memcpy((fullpath + strlen(fullpath)), "/", 1);
00996       memcpy((fullpath + strlen(fullpath)), filename, strlen(filename));
00997     }
00998     else{
00999       memcpy((fullpath + strlen(fullpath)), "/", 1);
01000       memcpy((fullpath + strlen(fullpath)), filepath, strlen(filepath));
01001     }
01002 
01003     if(rename(tmp_filename, fullpath) != 0) {
01004 
01005       if(errno == EEXIST) {
01006         
01007                 retval = remove(fullpath);
01008         
01009                 if(retval == -1) {
01010                         printf("errno: %i\n", errno);
01011                         fflush(stdout);
01012                 }
01013         
01014                 if(rename(tmp_filename, fullpath) < 0) {
01015                   printf("rename() error1: %s\n", tmp_filename);
01016                   fflush(stdout);
01017                 }
01018       }
01019       else {
01020                 printf("rename() error2: %s\n", tmp_filename);
01021                 printf("fullpath: %s\n", fullpath);
01022                 printf("errno: %i\n", errno);
01023                 fflush(stdout);
01024       }
01025     }
01026     
01027     if(receiver->verbosity > 0) {
01028 #ifdef _MSC_VER
01029       printf("File received: %s (TOI=%I64u)\n", file->location, toi);
01030 #else
01031       printf("File received: %s (TOI=%llu)\n", file->location, toi);
01032 #endif
01033       fflush(stdout);
01034     }
01035 
01036     if(!(receiver->rx_automatic || receiver->wildcard_token != NULL)) {
01037       set_file_received(receiver->file_uri_table, file->location);
01038     }
01039 
01040     free_uri(uri);              
01041     free(tmp);
01042     free(filepath);
01043     
01044 #ifdef _MSC_VER
01045     if(openfile) {
01046                 ShellExecute(NULL, "Open", fullpath, NULL, NULL, SW_SHOWNORMAL);
01047     }
01048 #endif      
01049     
01050 #ifdef _MSC_VER
01051     Sleep(1);
01052 #else
01053     usleep(1000);
01054 #endif
01055   }
01056   return 1;
01057 }
01058 
01059 int receiver_in_fdt_based_mode(arguments_t *a, flute_receiver_t *receiver) {
01060 
01061   int retval = 0;
01062   int retcode = 0;
01063   char *cont_desc = NULL;
01064  
01065   if(strcmp(a->sdp_file, "") != 0) {
01066     cont_desc = sdp_attr_get(a->sdp, "content-desc");
01067   }
01068 
01069   if(a->rx_automatic) {
01070     if(a->alc_a.verbosity > 0) {
01071       printf("FLUTE Receiver in automatic mode\n");
01072     }
01073   }
01074   else if(strchr(a->file_path, '*') != NULL) {
01075     if(a->alc_a.verbosity > 0) {
01076       printf("FLUTE Receiver in wild card mode\n");
01077     }
01078   }
01079   else {
01080     if(a->alc_a.verbosity > 0) {
01081       printf("FLUTE Receiver in fileURI list mode\n");
01082     }
01083   }
01084   
01085   if(cont_desc != NULL) {
01086     if(a->alc_a.verbosity > 0) {
01087       printf("Session content information available at:\n");
01088       printf("%s\n", cont_desc);
01089     }
01090   }
01091   
01092   fflush(stdout);
01093 
01094   while(receiver->fdt == NULL) {
01095 
01096     if(get_session_state(receiver->s_id) == SExiting) {
01097       return -2;
01098     }
01099     else if(get_session_state(receiver->s_id) == STxStopped) {
01100       return -3;
01101     }
01102 
01103 #ifdef _MSC_VER
01104     Sleep(1);
01105 #else
01106     usleep(1000);
01107 #endif
01108     continue;
01109   }
01110   
01111   retcode = fdtbasedrecv(a->alc_a.rx_memory_mode,
01112 #ifdef _MSC_VER
01113                          a->open_file,
01114 #else 
01115                          0,
01116 #endif
01117                          receiver);
01118   
01119   if(retcode == -1) {
01120     printf("Error: fdtbasedrecv() failed\n");
01121     fflush(stdout);
01122     retval = -1;
01123   }
01124   else if(retcode == -2) {
01125     retval = -2;
01126   }
01127   else if(retcode == -3) {
01128     retval = -3;
01129   }
01130 
01131   return retval;
01132 }
01133 
01134 int receiver_in_ui_mode(arguments_t *a, flute_receiver_t *receiver) {
01135         
01136         file_t *file;
01137         char input[100];
01138 
01139         unsigned long long rec_toi;
01140 
01141         char command;
01142         BOOL valid_toi = FALSE;
01143         BOOL expired_toi = FALSE;
01144         char *filepath = NULL;
01145         uri_t *uri = NULL;
01146 
01147         int retval = 0;
01148         int retcode = 0;
01149 
01150         time_t systime;
01151         unsigned long long curr_time;
01152 
01153         int content_enc_algo = -1;
01154 
01155         char *cont_desc = NULL;
01156 
01157         if(strcmp(a->sdp_file, "") != 0) {
01158           cont_desc = sdp_attr_get(a->sdp, "content-desc");
01159         }
01160 
01161         printf("FLUTE Receiver in UI-mode\n");
01162 
01163         if(cont_desc != NULL) {
01164           printf("Session content information available at:\n");
01165           printf("%s\n", cont_desc);
01166         }
01167         
01168         fflush(stdout);
01169 
01170         while(receiver->fdt == NULL) {
01171 
01172                 if(get_session_state(receiver->s_id) == SExiting) {
01173                         return -2;
01174                 }
01175                 else if(get_session_state(receiver->s_id) == STxStopped) {
01176                         return -3;
01177                 }
01178 
01179 #ifdef _MSC_VER
01180                 Sleep(1);
01181 #else
01182                 usleep(1000);
01183 #endif
01184                 continue;
01185         }
01186 
01187         while(1) {
01188 
01189                 if(get_session_state(receiver->s_id) == SExiting) {                     
01190                         return -2;
01191                 }
01192                 else if(get_session_state(receiver->s_id) == STxStopped) {
01193                         return -3;
01194                 }
01195         
01196                 printf(">> Command: (D)ownload file (Q)uit\n");
01197                 printf("-> ");
01198 
01199                 if(fgets(input, 100, stdin) != NULL) {
01200 
01201                         if(sscanf(input, "%c", &command) != EOF) {
01202 
01203                                 command = tolower(command);
01204                                                         
01205                                 if(command == 'd') {
01206                                         printf("Files to download:\n");
01207                                         fflush(stdout);
01208                                         
01209                                         file = receiver->fdt->file_list;
01210 
01211                                         while(file != NULL) {
01212 #ifdef _MSC_VER
01213                                                 printf("\t%I64u) %s\n", file->toi, file->location);
01214 #else
01215                                                 printf("\t%llu) %s\n", file->toi, file->location);
01216 #endif
01217                                                 fflush(stdout);
01218                                                 file = file->next;
01219                                         }
01220 
01221                                         printf("\nWhich one to download: ");
01222                                         fflush(stdout);
01223 
01224                                         if(fgets(input, 100, stdin) != NULL) {
01225                                                 
01226 #ifdef _MSC_VER
01227                                                 retcode = sscanf(input, "%I64u", &rec_toi);
01228 #else
01229                                                 retcode = sscanf(input, "%llu", &rec_toi);
01230 #endif
01231                                                 if(!((retcode == 0) || (retcode == EOF))) {
01232                                 
01233                                                         file = receiver->fdt->file_list;
01234 
01235                                                         valid_toi = FALSE;
01236                                                         expired_toi = FALSE;
01237                         
01238                                                         while(file != NULL) {
01239 
01240                                                                 if(file->toi == rec_toi) {
01241 
01242                                                                         time(&systime);
01243                                                                         curr_time = systime + 2208988800U;
01244 
01245                                                                         if(file->expires < curr_time) {
01246                                                                                                         
01247                                                                                 if(!a->alc_a.accept_expired_fdt_inst) {
01248 
01249                                                                                         expired_toi = TRUE;
01250 #ifdef _MSC_VER
01251                                                                                         printf("\nToi: %I64u Expired.\n", rec_toi);        
01252 #else
01253                                                                                         printf("\nToi: %llu Expired.\n", rec_toi);  
01254 #endif
01255                                                                                         if(file->next != NULL) {
01256                                                                                                 file->next->prev = file->prev;
01257                                                                                         }
01258                                                                                         if(file->prev != NULL) {
01259                                                                                                 file->prev->next = file->next;
01260                                                                                         }
01261                                                                                         if(file == receiver->fdt->file_list) {
01262                                                                                                 receiver->fdt->file_list = file->next;
01263                                                                                         }
01264 
01265                                                                                         free_file(file);
01266                                                                                         break;
01267                                                                                 }
01268                                                                                 else {
01269 #ifdef _MSC_VER
01270                                                                                         printf("\nToi: %I64u Expired, receiving anyway.\n", rec_toi);        
01271 #else
01272                                                                                         printf("\nToi: %llu Expired, receiving anyway.\n", rec_toi);  
01273 #endif                                                                                  
01274                                                                                 }
01275                                                                         }
01276 
01277                                                                         valid_toi = TRUE;
01278 
01279                                                                         uri = parse_uri(file->location, strlen(file->location));
01280         
01281                                                                         filepath = get_uri_host_and_path(uri);
01282                                                                                                                                                                                 
01283                                                                         if(file->encoding == NULL) {
01284                                                                                 content_enc_algo = 0;   
01285                                                                         }
01286                                                                         else {
01287                                                                                 if(strcmp(file->encoding, "pad") == 0) {
01288                                                                                         content_enc_algo = PAD;
01289                                                                                 }
01290 #ifdef USE_ZLIB
01291                                                                                 else if(strcmp(file->encoding, "gzip") == 0) {
01292                                                                                         content_enc_algo = GZIP;        
01293                                                                                 }
01294 #endif
01295                                                                                 else {
01296                                                                                         content_enc_algo = -1;
01297                                                                                         break;
01298                                                                                 }
01299                                                                         }
01300 
01301                                                                         retval = set_wanted_object(receiver->s_id, file->toi,
01302                                                                                                    file->transfer_len,
01303                                                                                                    file->es_len,
01304                                                                                                    file->max_sb_len,
01305                                                                                                    file->fec_inst_id,
01306                                                                                                    file->fec_enc_id,
01307                                                                                                    file->max_nb_of_es,
01308                                                                                                    content_enc_algo,
01309                                                                                                    file->finite_field,
01310                                                                                                    file->nb_of_es_per_group
01311                                                                                                    );
01312                                                                         break;
01313                                                                 }
01314                                                                 file = file->next;
01315                                                         }
01316 
01317                                                         if(expired_toi) {
01318                                                                 free_uri(uri);
01319                                                                 free(filepath);
01320                                                                 continue;
01321                             }
01322 
01323                                                         if(!valid_toi) {
01324 #ifdef _MSC_VER
01325                                                                 printf("\nToi: %I64u invalid.\n", rec_toi);        
01326 #else
01327                                                                 printf("\nToi: %llu invalid.\n", rec_toi);  
01328 #endif       
01329                                                                 fflush(stdout);
01330                                                                 continue;
01331                             }
01332 
01333                                                         if(content_enc_algo == -1) {
01334                                                                 printf("Content-Encoding: %s not supported\n", file->encoding);
01335                                                                 fflush(stdout);
01336                                                                 continue;
01337                                                         }
01338                                                         retcode = recvfile(receiver->s_id, filepath, file->toi, file->content_len, file->md5,
01339                                                                            a->alc_a.rx_memory_mode, file->encoding, a->alc_a.verbosity);
01340                                                                                                         
01341                                                         if(retcode == -1) {
01342                                                                 free(filepath);
01343                                                                 free_uri(uri);
01344                                                                 printf("\nError: recvfile() failed\n");
01345                                                                 fflush(stdout);
01346                                                                 return -1;
01347                                                         }
01348                                                         else if(retcode == -2) {
01349                                                                 free(filepath);
01350                                                                 free_uri(uri);
01351                                                                 return -2;
01352                                                         }
01353                                                         else if(retcode == -3) {
01354                                                                 free(filepath);
01355                                                                 free_uri(uri);
01356                                                                 return -3;
01357                                                         }
01358 
01359 #ifdef _MSC_VER
01360                                                         if(((retcode != -4) && (a->open_file))) {
01361                                                                 ShellExecute(NULL, "Open", filepath, NULL, NULL, SW_SHOWNORMAL); 
01362                                                         }
01363 #endif
01364                                                         free(filepath);
01365                                                 }
01366                                         }
01367                                 }
01368                                 else if(command == 'q') {
01369                                         break;
01370                                 }
01371                                 else {
01372                                         continue;
01373                                 }
01374                         }
01375                 }
01376         }
01377 
01378         return 0;
01379 }
01380 
01381 int receiver_in_object_mode(int *s_id, arguments_t *a) {
01382 
01383         int retval = 0;
01384         int retcode;
01385         div_t div_max_n;
01386         unsigned short max_n;
01387 
01388         char *cont_desc = NULL;
01389         
01390         div_max_n = div((a->alc_a.max_sb_len * (100 + a->alc_a.fec_ratio)), 100);
01391         max_n = (unsigned short)div_max_n.quot;
01392 
01393         if(strcmp(a->sdp_file, "") != 0) {
01394                 cont_desc = sdp_attr_get(a->sdp, "content-desc");
01395         }
01396 
01397         if(a->alc_a.verbosity > 0) {
01398           printf("FLUTE Receiver in object mode\n");
01399         }
01400 
01401         if(cont_desc != NULL) {
01402           if(a->alc_a.verbosity > 0) {
01403             printf("Session content information available at:\n");
01404             printf("%s\n", cont_desc);
01405           }
01406         }
01407 
01408         fflush(stdout);
01409 
01410         set_wanted_object(*s_id, a->toi, 0, a->alc_a.es_len, a->alc_a.max_sb_len,
01411                           a->alc_a.fec_inst_id, a->alc_a.fec_enc_id, max_n, 0, 0, 0
01412                           );
01413 
01414         retcode = recvfile(*s_id, NULL, a->toi, 0, NULL, a->alc_a.rx_memory_mode, NULL, a->alc_a.verbosity);    
01415 
01416         if(retcode == -1) {
01417                 printf("\nError: recvfile() failed\n");
01418                 fflush(stdout);
01419                 retval = -1;
01420         }
01421         else if(retcode == -2) {
01422                 retval =  -2;
01423         }
01424         else if(retcode == -3) {
01425                 retval =  -3;
01426         }
01427         else {
01428 #ifdef _MSC_VER
01429                 if(a->open_file) {
01430                         ShellExecute(NULL, "Open", a->file_path, NULL, NULL, SW_SHOWNORMAL); 
01431                 }
01432 #endif
01433         }
01434 
01435         return retval;
01436 }
01437 
01438 void* fdt_thread(void *s) {
01439   
01440   int i;
01441   flute_receiver_t *receiver;
01442   char *buf = NULL;
01443   
01444   unsigned long long buflen = 0;
01445   
01446   int updated;
01447   
01448   fdt_t *fdt_instance;
01449   time_t systime;
01450   file_t *file;
01451   file_t *next_file;
01452   int retval;
01453   
01454   unsigned long long curr_time;
01455   
01456 #ifdef USE_ZLIB
01457   char *uncompr_buf = NULL;
01458   
01459   unsigned long long uncompr_buflen = 0;
01460   
01461 #endif
01462   unsigned char content_enc_algo = 0;
01463 
01464   unsigned char fdt_content_enc_algo = 0;
01465   int fdt_instance_id = 0;
01466   
01467   receiver = (flute_receiver_t*)s;
01468  
01469   while(get_session_state(receiver->s_id) == SActive) {
01470     
01471     time(&systime);
01472     curr_time = systime + 2208988800U;
01473     
01474     /* Get initial fdt */
01475     if(receiver->fdt == NULL) {
01476       
01477       buf = fdt_recv(receiver->s_id, &buflen, &retval, &fdt_content_enc_algo, &fdt_instance_id);
01478       
01479       if(buf == NULL) {
01480         
01481             if(retval == -1) {
01482               continue;
01483             }
01484 
01485 #ifdef _MSC_VER
01486             _endthread();
01487 #else
01488             pthread_exit(0);
01489 #endif
01490       }
01491 
01492 #ifdef USE_ZLIB
01493       if(fdt_content_enc_algo == ZLIB) {
01494             uncompr_buf = buffer_zlib_uncompress(buf, buflen, &uncompr_buflen);
01495         
01496             if(uncompr_buf == NULL) {
01497               free(buf);
01498               continue;
01499             }
01500             fdt_instance = decode_fdt_payload(uncompr_buf);
01501               free(uncompr_buf);
01502         }
01503         else {
01504              fdt_instance = decode_fdt_payload(buf);
01505         }
01506 #else 
01507       fdt_instance = decode_fdt_payload(buf);
01508 #endif
01509       
01510       if(fdt_instance == NULL) {
01511         free(buf);
01512         continue;
01513       }
01514       
01515       if(fdt_instance->expires < curr_time) {
01516         
01517         if(!receiver->accept_expired_fdt_inst) {
01518           
01519           if(receiver->verbosity == 4) {
01520             printf("Expired FDT Instance received, discarding\n");
01521             fflush(stdout);
01522           }
01523           
01524           free(buf);
01525           FreeFDT(fdt_instance);
01526           continue;
01527         }
01528         else {
01529           if(receiver->verbosity == 4) {
01530             printf("Expired FDT Instance received, using it anyway\n");
01531             fflush(stdout);
01532           }
01533         }
01534       }
01535       
01536       receiver->fdt = fdt_instance;
01537       
01538       if(receiver->verbosity == 4) {
01539         printf("FDT Instance received (ID=%i)\n", fdt_instance_id);
01540         printf("FDT updated, new file description(s) added\n");
01541         fflush(stdout);
01542         PrintFDT(fdt_instance, receiver->s_id);
01543       }
01544       
01545       free(buf);
01546       
01547       next_file = receiver->fdt->file_list;
01548       
01549       while(next_file != NULL) {
01550         i = 0;
01551         file = next_file;
01552         
01553         if(file->status == 0) {
01554           
01555           if(file->encoding == NULL) {
01556             content_enc_algo = 0;
01557           }
01558           else {
01559             if(strcmp(file->encoding, "pad") == 0) {
01560               content_enc_algo = PAD;   
01561             }
01562 #ifdef USE_ZLIB
01563             else if(strcmp(file->encoding, "gzip") == 0) {
01564               content_enc_algo = GZIP;
01565             }
01566 #endif
01567             else {
01568               printf("Content-Encoding: %s not supported\n", file->encoding);
01569               fflush(stdout);
01570               file->status = 2;
01571               next_file = file->next;
01572               continue;
01573             }
01574           }
01575           
01576           if(receiver->rx_automatic) {
01577             
01578             retval = set_wanted_object(receiver->s_id, file->toi, file->transfer_len,
01579                                        file->es_len,
01580                                        file->max_sb_len,
01581                                        file->fec_inst_id,
01582                                        file->fec_enc_id,
01583                                        file->max_nb_of_es, content_enc_algo,
01584                                            file->finite_field, file->nb_of_es_per_group
01585                                        );
01586             
01587             if(retval < 0) {
01588               /* Memory error */
01589             }
01590             else {
01591               file->status = 1;
01592             }
01593           }
01594           else if(receiver->wildcard_token != NULL) { 
01595             if(strstr(file->location, receiver->wildcard_token) != NULL) {
01596               retval = set_wanted_object(receiver->s_id, file->toi, file->transfer_len,
01597                                          file->es_len,
01598                                          file->max_sb_len,
01599                                          file->fec_inst_id,
01600                                          file->fec_enc_id,
01601                                          file->max_nb_of_es, content_enc_algo,
01602                                          file->finite_field, file->nb_of_es_per_group
01603                                          );
01604               
01605               if(retval < 0) {
01606                 /* Memory error */
01607               }
01608               else {
01609                 file->status = 1;
01610               }
01611                 }
01612           }
01613           else {
01614 
01615             for(i = 0; i < FILE_URI_TABLE_SIZE; i++) {
01616               
01617               if(receiver->file_uri_table[i] == NULL) {
01618                 continue;
01619               }
01620               
01621               if(strcmp(file->location, receiver->file_uri_table[i]) == 0) {
01622                 
01623                 retval = set_wanted_object(receiver->s_id, file->toi, file->transfer_len,
01624                                            file->es_len,
01625                                            file->max_sb_len,
01626                                            file->fec_inst_id,
01627                                            file->fec_enc_id,
01628                                            file->max_nb_of_es, content_enc_algo,
01629                                            file->finite_field, file->nb_of_es_per_group
01630                                            );
01631                 
01632                 if(retval < 0) {
01633                   /* Memory error */
01634                 }
01635                 else {
01636                   file->status = 1;
01637                   break;
01638                 }
01639               }
01640             }
01641           }
01642         }
01643         
01644         next_file = file->next;
01645           }
01646 
01647         set_fdt_instance_parsed(receiver->s_id);
01648         }
01649     else { /* Receive new FDT Instance when it comes */
01650       updated = 0;
01651       
01652       buf = fdt_recv(receiver->s_id, &buflen, &retval, &fdt_content_enc_algo, &fdt_instance_id);
01653       
01654       if(buf == NULL) {
01655         
01656         if(retval == -1) {
01657           continue;
01658         }
01659         
01660 #ifdef _MSC_VER
01661         _endthread();
01662 #else
01663         pthread_exit(0);
01664 #endif
01665       }
01666 
01667 #ifdef USE_ZLIB
01668       if(fdt_content_enc_algo == ZLIB) {
01669         uncompr_buf = buffer_zlib_uncompress(buf, buflen, &uncompr_buflen);
01670         
01671         if(uncompr_buf == NULL) {
01672           free(buf);
01673           continue;
01674         }
01675         fdt_instance = decode_fdt_payload(uncompr_buf);
01676         free(uncompr_buf);
01677       }
01678       else {
01679         fdt_instance = decode_fdt_payload(buf);
01680       }
01681 #else 
01682       fdt_instance = decode_fdt_payload(buf);
01683 #endif
01684       
01685       if(fdt_instance == NULL) {
01686         free(buf);
01687         continue;
01688       }
01689       
01690       if(fdt_instance->expires < curr_time) {
01691         if(!receiver->accept_expired_fdt_inst) {
01692           if(receiver->verbosity == 4) {
01693             printf("Expired FDT Instance received, discarding\n");
01694             fflush(stdout);
01695           }
01696           FreeFDT(fdt_instance);
01697           free(buf);
01698           continue;
01699         }
01700         else {
01701           if(receiver->verbosity == 4) {
01702             printf("Expired FDT Instance received, using it anyway\n");
01703             fflush(stdout);
01704           }
01705         }
01706       }
01707       
01708       if(receiver->verbosity == 4) {
01709         printf("FDT Instance received (ID=%i)\n", fdt_instance_id);
01710         fflush(stdout);
01711       }
01712 
01713       free(buf);
01714       
01715       if((fdt_instance->complete == TRUE)&&(receiver->fdt->complete == FALSE)&&(fdt_instance_id == 0)) {
01716         receiver->fdt->complete = TRUE;
01717       }
01718       
01719       updated = update_fdt(receiver->fdt, fdt_instance);
01720       
01721       if(updated < 0) {
01722         continue;
01723       }
01724       else if(updated == 1) {
01725         if(receiver->verbosity == 4) {
01726           printf("FDT updated, file description(s) complemented\n");
01727           fflush(stdout);
01728         }
01729       }
01730       else if(updated == 2) {
01731         if(receiver->verbosity == 4) {
01732           printf("FDT updated, new file description(s) added\n");
01733           fflush(stdout);
01734           PrintFDT(fdt_instance, receiver->s_id);
01735         }
01736         
01737         next_file = receiver->fdt->file_list;
01738         
01739         while(next_file != NULL) {
01740           file = next_file;
01741           
01742           if(file->status == 0) {
01743             
01744             if(file->encoding == NULL) {
01745               content_enc_algo = 0;     
01746             }
01747             else {
01748               if(strcmp(file->encoding, "pad") == 0) {
01749                 content_enc_algo = PAD; 
01750               }
01751 #ifdef USE_ZLIB
01752               else if(strcmp(file->encoding, "gzip") == 0) { 
01753                 content_enc_algo = GZIP; 
01754               }
01755 #endif
01756               else {
01757                 printf("Content-Encoding: %s not supported\n", file->encoding);
01758                 fflush(stdout);
01759                 file->status = 1;
01760               }
01761             }
01762             
01763             if(receiver->rx_automatic) {
01764               
01765               retval = set_wanted_object(receiver->s_id, file->toi, file->transfer_len,
01766                                          file->es_len,
01767                                          file->max_sb_len,
01768                                          file->fec_inst_id,
01769                                          file->fec_enc_id,
01770                                          file->max_nb_of_es, content_enc_algo,
01771                                          file->finite_field, file->nb_of_es_per_group
01772                                          );
01773               
01774               if(retval < 0) {
01775                 /* Memory error */
01776               }
01777               else {
01778                 file->status = 1;
01779               }
01780             }
01781             else if(receiver->wildcard_token != NULL) {
01782               if(strstr(file->location, receiver->wildcard_token) != NULL) {
01783                 retval = set_wanted_object(receiver->s_id, file->toi, file->transfer_len,
01784                                            file->es_len,
01785                                            file->max_sb_len,
01786                                            file->fec_inst_id,
01787                                            file->fec_enc_id,
01788                                            file->max_nb_of_es, content_enc_algo,
01789                                            file->finite_field, file->nb_of_es_per_group
01790                                            );
01791 
01792                 if(retval < 0) {
01793                   /* Memory error */
01794                 }
01795                 else {
01796                   file->status = 1;
01797                 }
01798               }
01799             }
01800             else {
01801               
01802               for(i = 0; i < FILE_URI_TABLE_SIZE; i++) {
01803                 
01804                 if(receiver->file_uri_table[i] == NULL) {
01805                   continue;
01806                 }
01807 
01808                 if(strcmp(file->location, receiver->file_uri_table[i]) == 0) {
01809 
01810                   retval = set_wanted_object(receiver->s_id, file->toi, file->transfer_len,
01811                                              file->es_len,
01812                                              file->max_sb_len,
01813                                              file->fec_inst_id,
01814                                              file->fec_enc_id,
01815                                              file->max_nb_of_es, content_enc_algo,
01816                                                  file->finite_field, file->nb_of_es_per_group
01817                                              );
01818                   
01819                   if(retval < 0) {
01820                     /* Memory error */
01821                   }
01822                   else {
01823                     file->status = 1;
01824                     break;
01825                   }
01826                 }
01827               } 
01828             }
01829           }     
01830           next_file = file->next;
01831         }
01832           }
01833 
01834       FreeFDT(fdt_instance);
01835 
01836           set_fdt_instance_parsed(receiver->s_id);
01837         }
01838   }
01839 
01840 #ifdef _MSC_VER
01841   _endthread();
01842 #else
01843   pthread_exit(0);
01844 #endif
01845 
01846         return NULL;
01847 }

Generated on Fri Mar 9 20:08:52 2007 for MAD-FCL by  doxygen 1.5.0