sender.c

Go to the documentation of this file.
00001 
00033 #include <stdlib.h>
00034 #include <sys/stat.h>
00035 #include <errno.h>
00036 #include <time.h>
00037 #include <string.h>
00038 #include <stdio.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 #else
00045 #include <unistd.h>
00046 #endif
00047 
00048 #include "../alclib/blocking_alg.h"
00049 #include "../alclib/alc_session.h"
00050 #include "../alclib/alc_tx.h"
00051 
00052 #include "sender.h"
00053 #include "uri.h"
00054 #include "mad_zlib.h"
00055 #include "flute.h"
00056 #include "fdt.h"
00057 
00075 int send_fdt_instance(char *fdt_instance, unsigned long long fdt_inst_len, int s_id,
00076                                           int tx_mode, unsigned short eslen, unsigned int max_sblen,
00077                                           unsigned char fec_enc_id, unsigned short fec_inst_id, int verbosity) {
00078 
00079   unsigned long long transfer_len;
00080         
00081   int sentbytes = 0;
00082   int bytes_left;
00083   int nbytes;
00084   int retval = 0;
00085   unsigned int sbn = 0;
00086   char *buf = NULL;
00087 
00088   blocking_struct_t *bs;                
00089                                                                                                                           
00090   transfer_len = fdt_inst_len;
00091   bytes_left = (int)transfer_len;
00092    
00093   if(tx_mode != TX_THREAD) {
00094 
00095     if(verbosity == 4) {
00096       printf("Sending FDT Instance (ID=%i)\n", get_fdt_instance_id(s_id));
00097       fflush(stdout);
00098     }
00099     
00100     set_object_sent_bytes(s_id, 0);
00101   }
00102   else {
00103     if(verbosity == 4) {
00104       printf("Adding FDT Instance to Tx queue (ID=%i)\n", get_fdt_instance_id(s_id));
00105       fflush(stdout);
00106     }
00107   }
00108 
00109   /* Let's compute the blocking structure */
00110   
00111   bs = compute_blocking_structure(transfer_len, max_sblen, eslen);
00112 
00113   while(sbn < bs->N) {
00114 
00115     if(get_session_state(s_id) == SExiting) {
00116       free(buf);
00117       return -2;
00118     }
00119 
00120     if(sbn < bs->I) {
00121       nbytes = eslen*(bs->A_large);
00122     }
00123     else {
00124       nbytes = eslen*(bs->A_small);
00125     }
00126 
00127     nbytes = bytes_left < nbytes ? bytes_left : nbytes;
00128 
00129     buf = fdt_instance + sentbytes;
00130 
00131     retval = alc_send(s_id, tx_mode, buf, nbytes, FDT_TOI, transfer_len, eslen, max_sblen, sbn,
00132                     fec_enc_id, fec_inst_id);
00133 
00134     if(retval == -1) {
00135       free(bs);
00136       return -1;
00137     }
00138     else if(retval == -2) {
00139       free(bs);
00140       return -2;
00141     }
00142     
00143     bytes_left -= nbytes;
00144     sentbytes += nbytes;
00145 
00146     sbn++;
00147   }
00148 
00149   free(bs);
00150 
00151   if(tx_mode != TX_THREAD) {
00152     if(verbosity == 4) {
00153       printf("FDT Instance sent (ID=%i)\n", get_fdt_instance_id(s_id));
00154       fflush(stdout);
00155     }
00156   }
00157   else {
00158     if(verbosity == 4) {
00159       printf("FDT Instance added to Tx queue (ID=%i)\n", get_fdt_instance_id(s_id));
00160       fflush(stdout);
00161     }
00162   }
00163     
00164   return 0;
00165 }
00185 int send_file(char *tx_file, int s_id, int tx_mode, unsigned short es_len, unsigned int max_sb_len,
00186                           unsigned char fec_enc_id, unsigned short fec_inst_id, file_t *file,
00187                           unsigned long long fdt_inst_len, char *fdt_inst_buf, int verbosity) {
00188 
00189         unsigned long long transfer_len;
00190         unsigned long long sent = 0;
00191 
00192         int nbytes;
00193         int read_bytes;
00194         double print_percent;
00195 
00196         FILE *fp;       
00197 
00198 #ifdef _MSC_VER
00199         struct __stat64 file_stats;
00200 #else
00201         struct stat64 file_stats;
00202 #endif
00203 
00204         char *buf = NULL;
00205 
00206         unsigned int sbn = 0;
00207 
00208         unsigned int Mbytes_of_data = 0;
00209         int retval = 0;
00210         alc_session_t *s;
00211 
00212         blocking_struct_t *bs;
00213   
00214         s = get_alc_session(s_id);
00215 
00216         set_session_tx_toi(s_id, file->toi);
00217 
00218 #ifdef _MSC_VER
00219         if(_stat64(tx_file, &file_stats) == -1) {
00220 #else
00221         if(stat64(tx_file, &file_stats) == -1) {
00222 #endif
00223                 printf("Error: %s is not valid file name\n", tx_file);
00224                 fflush(stdout);
00225                 return -1;
00226         }
00227 
00228         if(file->encoding == NULL) {
00229                 transfer_len = file->content_len;
00230         }
00231         else {
00232                 transfer_len = file->transfer_len;
00233         }
00234 
00235         if(transfer_len == 0) {
00236                 printf("Tx_file %s size = 0\n", tx_file);
00237                 fflush(stdout);
00238                 return -1;
00239         }
00240 
00241 #ifdef _MSC_VER
00242     if(transfer_len > (unsigned long long)0xFFFFFFFFFFFF) {
00243 #else
00244         if(transfer_len > 0xFFFFFFFFFFFFULL) {
00245 #endif
00246                 printf("Tx_file %s too big!!\n", tx_file);
00247                 fflush(stdout);
00248                 return -1;
00249         }
00250         
00251         /* Allocate memory for buf */
00252         if(!(buf = (char*)calloc((unsigned int)(es_len*max_sb_len), sizeof(char)))) {
00253                 printf("Could not alloc memory for buf!\n");
00254                 fflush(stdout);
00255                 return -1;
00256         }
00257 
00258         /* File to send */
00259 #ifdef _MSC_VER
00260         if((fp = fopen(tx_file, "rb")) == NULL) {
00261 #else
00262         if((fp = fopen64(tx_file, "rb")) == NULL) {
00263 #endif
00264           printf("Error: unable to open tx_file %s\n", tx_file);
00265           fflush(stdout);
00266           free(buf);
00267           return -1;
00268         }
00269 
00270         if(tx_mode != TX_THREAD) {
00271           if(verbosity > 0) {
00272 #ifdef _MSC_VER
00273             printf("Sending file: %s (TOI=%I64u)\n", tx_file, file->toi);
00274 #else
00275             printf("Sending file: %s (TOI=%llu)\n", tx_file, file->toi);
00276 #endif
00277             fflush(stdout);
00278           }
00279           set_object_sent_bytes(s_id, 0);
00280           set_object_last_print_tx_percent(s_id, 0);
00281         }
00282         else {
00283           if(verbosity > 0) {
00284 #ifdef _MSC_VER
00285             printf("Adding file to Tx queue: %s (TOI=%I64u)\n", tx_file, file->toi);
00286 #else
00287             printf("Adding file to Tx queue: %s (TOI=%llu)\n", tx_file, file->toi);
00288 #endif
00289             fflush(stdout);
00290           }
00291         }
00292         
00293         /* Let's compute the blocking structure */
00294         bs = compute_blocking_structure(transfer_len, max_sb_len, es_len);
00295 
00296         while(sbn < bs->N) {
00297 
00298         if(sbn < bs->I) {
00299                         nbytes = es_len*(bs->A_large);
00300         }
00301         else {
00302                         nbytes = es_len*(bs->A_small);
00303         }
00304 
00305         memset(buf, 0, (es_len * max_sb_len));
00306         read_bytes = fread(buf, 1, (unsigned int)nbytes, fp);
00307 
00308                 if(ferror(fp)) {
00309                         printf("fread error, file: %s\n", tx_file);
00310                         fflush(stdout);
00311                         free(buf);
00312                         fclose(fp);
00313                         free(bs);
00314                         return -1;
00315                 }
00316 
00317                 if(file->encoding == NULL || strcmp(file->encoding, "gzip") == 0) {
00318                         retval = alc_send(s_id, tx_mode, buf, read_bytes, file->toi, transfer_len, es_len, max_sb_len, sbn, fec_enc_id,
00319                                                         fec_inst_id);
00320                 }
00321                 else if (strcmp(file->encoding, "pad") == 0) {
00322                   retval = alc_send(s_id, tx_mode, buf, nbytes, file->toi, transfer_len, es_len, max_sb_len, sbn, fec_enc_id,
00323                                     fec_inst_id);
00324                 }
00325 
00326                 if(tx_mode != TX_THREAD) {
00327 #ifdef _MSC_VER
00328                   if(verbosity > 2) {
00329                     printf("%u/%u Source Blocks sent (TOI=%I64u SBN=%u)\n", (sbn+1), bs->N, file->toi, sbn);
00330                     fflush(stdout);
00331                   }
00332 #else
00333                   if(verbosity > 2) {
00334                     printf("%u/%u Source Blocks sent (TOI=%llu SBN=%u)\n", (sbn+1), bs->N, file->toi, sbn);
00335                     fflush(stdout);
00336                   }
00337 #endif
00338                 }
00339                 
00340                 if(retval == -1) {
00341                   fclose(fp);
00342                   free(buf);
00343                   free(bs);
00344                   return -1;
00345                 }
00346                 else if(retval == -2) {
00347                   fclose(fp);
00348                   free(buf);
00349                   free(bs);
00350                   return -2;
00351                 }
00352 
00353                 if(fdt_inst_buf != NULL) {
00354                         sent = get_object_sent_bytes(s_id);
00355                         print_percent = get_object_last_print_tx_percent(s_id);
00356 
00357                         if((sent/FDT_INTERVAL) > Mbytes_of_data) {
00358 
00359                                 Mbytes_of_data = (unsigned int)(sent/FDT_INTERVAL);
00360                                 retval = send_fdt_instance(fdt_inst_buf, fdt_inst_len, s_id, tx_mode, s->def_eslen,
00361                                                            s->def_max_sblen, s->def_fec_enc_id, s->def_fec_inst_id,
00362                                                            verbosity);
00363                                         
00364                                 if(retval == -1) {
00365                                         free(bs);
00366                                         fclose(fp);
00367                                         free(buf);
00368                                         return -1;
00369                                 }
00370                                 else if(retval == -2) {
00371                                         free(bs);
00372                                         fclose(fp);
00373                                         free(buf);
00374                                         return -2;
00375                                 }
00376 
00377                                 set_object_sent_bytes(s_id, sent);
00378                                 set_object_last_print_tx_percent(s_id, print_percent);
00379                         }
00380                 }
00381 
00382         sbn++;
00383         }
00384 
00385         free(bs);
00386         fclose(fp);
00387         free(buf);
00388 
00389         if(tx_mode != TX_THREAD) {
00390           if(verbosity > 0) {
00391 #ifdef _MSC_VER
00392             printf("File sent: %s (TOI=%I64u)\n", tx_file, file->toi);
00393 #else 
00394             printf("File sent: %s (TOI=%llu)\n", tx_file, file->toi);
00395 #endif
00396             fflush(stdout);
00397           }
00398         }
00399         else {
00400           if(verbosity > 0) {
00401 #ifdef _MSC_VER
00402             printf("File added to Tx queue: %s (TOI=%I64u)\n", tx_file, file->toi);
00403 #else
00404             printf("File added to Tx queue: %s (TOI=%llu)\n", tx_file, file->toi);
00405 #endif
00406             fflush(stdout);
00407           }
00408         }
00409                 
00410         return 0;
00411         }
00412         
00426 char *create_fdt_instance(file_t *file, int nb_of_files, fdt_t *fdt, int s_id,
00427                                                   unsigned long long *fdt_inst_len) {
00428 
00429   file_t *tmp_file;
00430   char *fdt_inst_payload = NULL;
00431   char tmp_line[MAX_PATH_LENGTH];
00432   int i;
00433   int print_help = 0;
00434   
00435   unsigned long long size = 0;
00436   unsigned long long position = 0;
00437   
00438   alc_session_t *s;
00439   
00440   s = get_alc_session(s_id);
00441   
00442   tmp_file = file;
00443   
00444   memset(tmp_line, 0, MAX_PATH_LENGTH);
00445   sprintf(tmp_line, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n");
00446   size = strlen(tmp_line);
00447   
00448   /* Allocate memory for fdt_inst_payload */
00449   if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00450 #ifdef _MSC_VER
00451     printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00452 #else
00453     printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00454 #endif
00455     return NULL;
00456   }
00457   
00458   memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00459   position += strlen(tmp_line);
00460   
00461   memset(tmp_line, 0, MAX_PATH_LENGTH);
00462   
00463 #ifdef _MSC_VER
00464   sprintf(tmp_line, "<FDT-Instance Expires=\"%I64u\"", fdt->expires);
00465 #else
00466   sprintf(tmp_line, "<FDT-Instance Expires=\"%llu\"", fdt->expires);
00467 #endif
00468   size += strlen(tmp_line);
00469   
00470   /* Reallocate memory for fdt_inst_payload */
00471   if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00472 #ifdef _MSC_VER
00473     printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00474 #else
00475     printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00476 #endif
00477     return NULL;
00478   }
00479   
00480   memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00481   position += strlen(tmp_line);
00482   
00483   if(fdt->complete) {
00484 
00485     if(nb_of_files == fdt->nb_of_files) {
00486       memset(tmp_line, 0, MAX_PATH_LENGTH);
00487       sprintf(tmp_line, "\n\tComplete=\"true\"");
00488       size += strlen(tmp_line);
00489       
00490       /* Reallocate memory for fdt_inst_payload */
00491       if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00492 #ifdef _MSC_VER
00493         printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00494 #else
00495         printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00496 #endif
00497         return NULL;
00498       }
00499       
00500       memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00501       position += strlen(tmp_line);
00502     }
00503   }
00504 
00505   if(fdt->type != NULL) {
00506     memset(tmp_line, 0, MAX_PATH_LENGTH);
00507     sprintf(tmp_line, "\n\tContent-Type=\"%s\"", fdt->type);
00508     size += strlen(tmp_line);
00509     
00510     /* Reallocate memory for fdt_inst_payload */
00511     if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00512 #ifdef _MSC_VER
00513       printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00514 #else
00515       printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00516 #endif
00517       return NULL;
00518       
00519     }
00520     
00521     memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00522     position += strlen(tmp_line);
00523   }
00524   
00525   if(fdt->encoding != NULL) {
00526     memset(tmp_line, 0, MAX_PATH_LENGTH);
00527     sprintf(tmp_line, "\n\tContent-Encoding=\"%s\"", fdt->encoding);
00528     size += strlen(tmp_line);
00529     
00530     /* Reallocate memory for fdt_inst_payload */
00531     if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00532 #ifdef _MSC_VER
00533       printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00534 #else
00535       printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00536 #endif
00537       return NULL;
00538       
00539     }
00540     
00541     memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00542     position += strlen(tmp_line);
00543   }
00544   
00545 #ifdef FDT_INST_FEC_OTI_COMMON
00546   if(s->use_fec_oti_ext_hdr == 0) {
00547           /**** FEC-OTI parameters ****/
00548    
00549                   memset(tmp_line, 0, MAX_PATH_LENGTH);
00550                   sprintf(tmp_line, "\n\tFEC-OTI-FEC-Encoding-ID=\"%u\"", fdt->fec_enc_id);
00551                   size += strlen(tmp_line);
00552                   
00553                   /* Reallocate memory for fdt_inst_payload */
00554                   if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00555 #ifdef _MSC_VER
00556                           printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00557 #else
00558                           printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00559 #endif
00560                           return NULL;
00561                   }
00562                   
00563                   memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00564                   position += strlen(tmp_line);
00565                   
00566                   if(fdt->fec_enc_id >= 128) {
00567                           memset(tmp_line, 0, MAX_PATH_LENGTH);
00568                         sprintf(tmp_line, "\n\tFEC-OTI-FEC-Instance-ID=\"%u\"", fdt->fec_inst_id);
00569                         size += strlen(tmp_line);
00570                   
00571                         /* Reallocate memory for fdt_inst_payload */
00572                         if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00573 #ifdef _MSC_VER
00574                           printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00575 #else
00576                           printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00577 #endif
00578                           return NULL;
00579                         }
00580                   
00581                         memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00582                         position += strlen(tmp_line);
00583                  }
00584     
00585     memset(tmp_line, 0, MAX_PATH_LENGTH);
00586     sprintf(tmp_line, "\n\tFEC-OTI-Maximum-Source-Block-Length=\"%u\"",
00587             fdt->max_sb_len);
00588     size += strlen(tmp_line);
00589     
00590     /* Reallocate memory for fdt_inst_payload */
00591     if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00592 #ifdef _MSC_VER
00593       printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00594 #else
00595       printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00596 #endif
00597       return NULL;
00598     }
00599     
00600     memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00601     position += strlen(tmp_line);
00602     
00603     memset(tmp_line, 0, MAX_PATH_LENGTH);
00604     sprintf(tmp_line, "\n\tFEC-OTI-Encoding-Symbol-Length=\"%u\"",
00605             fdt->es_len);
00606     size += strlen(tmp_line);
00607     
00608     /* Reallocate memory for fdt_inst_payload */
00609     if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00610 #ifdef _MSC_VER
00611       printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00612 #else
00613       printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00614 #endif
00615       return NULL;
00616     }
00617     
00618     memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00619     position += strlen(tmp_line);
00620     
00621         if(fdt->fec_enc_id == RS_FEC_ENC_ID || fdt->fec_enc_id == SB_SYS_FEC_ENC_ID) {
00622 
00623                 memset(tmp_line, 0, MAX_PATH_LENGTH);
00624                 sprintf(tmp_line, "\n\tFEC-OTI-Max-Number-of-Encoding-Symbols=\"%u\"",
00625                         fdt->max_nb_of_es);
00626                 size += strlen(tmp_line);
00627 
00628                 /* Reallocate memory for fdt_inst_payload */
00629                 if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00630 #ifdef _MSC_VER
00631                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00632 #else
00633                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00634 #endif
00635                         return NULL;
00636                 }
00637 
00638                 memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00639                 position += strlen(tmp_line);
00640         }
00641 
00642         if(fdt->fec_enc_id == RS_FEC_ENC_ID) {
00643 
00644                 memset(tmp_line, 0, MAX_PATH_LENGTH);
00645                 sprintf(tmp_line, "\n\tFEC-OTI-Number-of-Encoding-Symbols-per-Group=\"%u\"",
00646                         fdt->nb_of_es_per_group);
00647                 size += strlen(tmp_line);
00648 
00649                 /* Reallocate memory for fdt_inst_payload */
00650                 if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00651 #ifdef _MSC_VER
00652                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00653 #else
00654                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00655 #endif
00656                         return NULL;
00657                 }
00658 
00659                 memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00660                 position += strlen(tmp_line);
00661 
00662                 memset(tmp_line, 0, MAX_PATH_LENGTH);
00663                 sprintf(tmp_line, "\n\tFEC-OTI-Finite-Field-Parameter=\"%u\"", fdt->finite_field);
00664                 size += strlen(tmp_line);
00665 
00666                 /* Reallocate memory for fdt_inst_payload */
00667                 if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00668 #ifdef _MSC_VER
00669                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00670 #else
00671                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00672 #endif
00673                         return NULL;
00674                 }
00675 
00676                 memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00677                 position += strlen(tmp_line);
00678         }
00679   }
00680 #endif
00681 
00682   memset(tmp_line, 0, MAX_PATH_LENGTH);
00683   sprintf(tmp_line, ">\n");
00684   size += strlen(tmp_line);
00685   
00686   /* Reallocate memory for fdt_inst_payload */
00687   if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00688 #ifdef _MSC_VER
00689     printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00690 #else
00691     printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00692 #endif
00693     return NULL;
00694   }
00695   
00696   memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00697   position += strlen(tmp_line);
00698   
00699   for(i = 0; i < nb_of_files; i++) {
00700     
00701     if(tmp_file == NULL) {
00702       break;
00703     }
00704     
00705     memset(tmp_line, 0, MAX_PATH_LENGTH);
00706     
00707 #ifdef _MSC_VER
00708     sprintf(tmp_line, "\t<File TOI=\"%I64u\"", tmp_file->toi);
00709 #else
00710     sprintf(tmp_line, "\t<File TOI=\"%llu\"", tmp_file->toi);
00711 #endif
00712     size += strlen(tmp_line);
00713     
00714     /* Reallocate memory for fdt_inst_payload */
00715     if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00716 #ifdef _MSC_VER
00717       printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00718 #else
00719       printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00720 #endif
00721       return NULL;
00722     }
00723     
00724     memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00725     position += strlen(tmp_line);
00726     
00727     
00728     memset(tmp_line, 0, MAX_PATH_LENGTH);
00729     sprintf(tmp_line, "\n\t\tContent-Location=\"%s\"", tmp_file->location);
00730     size += strlen(tmp_line);
00731     
00732     /* Reallocate memory for fdt_inst_payload */
00733     if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00734 #ifdef _MSC_VER
00735       printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00736 #else
00737       printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00738 #endif
00739       return NULL;
00740     }
00741     
00742     memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00743     position += strlen(tmp_line);
00744     
00745     memset(tmp_line, 0, MAX_PATH_LENGTH);
00746 #ifdef _MSC_VER
00747         sprintf(tmp_line, "\n\t\tContent-Length=\"%I64u\"", tmp_file->content_len);
00748 #else
00749                 sprintf(tmp_line, "\n\t\tContent-Length=\"%llu\"", tmp_file->content_len);
00750 #endif
00751                 size += strlen(tmp_line);
00752 
00753                 /* Reallocate memory for fdt_inst_payload */
00754                 if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00755 #ifdef _MSC_VER
00756                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00757 #else
00758                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00759 #endif
00760                         return NULL;
00761                 }
00762 
00763         memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00764                 position += strlen(tmp_line);
00765 
00766                 print_help = 0;
00767 
00768                 if(tmp_file->type != NULL) {
00769                         if(fdt->type != NULL) {
00770                                 if(strcmp(tmp_file->type, fdt->type) != 0) {
00771                                         print_help = 1;
00772                                 }
00773                         }
00774                         else {
00775                                 print_help = 1;
00776                         }
00777                 }       
00778                 if(print_help) {
00779                         memset(tmp_line, 0, MAX_PATH_LENGTH);
00780                         sprintf(tmp_line, "\n\t\tContent-Type=\"%s\"", tmp_file->type);
00781                         size += strlen(tmp_line);
00782         
00783                         /* Reallocate memory for fdt_inst_payload */
00784                         if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00785 #ifdef _MSC_VER
00786                                 printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00787 #else
00788                                 printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00789 #endif
00790                                 return NULL;
00791 
00792                         }
00793 
00794                 memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00795                         position += strlen(tmp_line);
00796                 }
00797 
00798         if(tmp_file->md5 != NULL) {
00799             memset(tmp_line, 0, MAX_PATH_LENGTH);
00800             sprintf(tmp_line, "\n\t\tContent-MD5=\"%s\"", tmp_file->md5);
00801                         size += strlen(tmp_line);
00802         
00803                         /* Reallocate memory for fdt_inst_payload */
00804                         if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00805 #ifdef _MSC_VER
00806                                 printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00807 #else
00808                                 printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00809 #endif
00810                                 return NULL;
00811                         }
00812 
00813                         memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00814                         position += strlen(tmp_line);
00815         }
00816                         
00817                 print_help = 0;
00818 
00819                 if(tmp_file->encoding != NULL) {
00820                         if(fdt->encoding != NULL) {
00821                                 if(strcmp(tmp_file->encoding, fdt->encoding) != 0) {
00822                                         print_help = 1;
00823                                 }
00824                         }
00825                         else {
00826                                 print_help = 1;
00827                         }
00828                 }       
00829                 if(print_help) {
00830                         memset(tmp_line, 0, MAX_PATH_LENGTH);
00831                         sprintf(tmp_line, "\n\t\tContent-Encoding=\"%s\"", tmp_file->encoding);
00832                         size += strlen(tmp_line);
00833 
00834                         /* Reallocate memory for fdt_inst_payload */
00835                         if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00836 #ifdef _MSC_VER
00837                                 printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00838 #else
00839                                 printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00840 #endif
00841                                 return NULL;
00842                         }
00843 
00844                         memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00845                         position += strlen(tmp_line);
00846                 }
00847                 if(tmp_file->encoding != NULL || fdt->encoding != NULL) {
00848                                 memset(tmp_line, 0, MAX_PATH_LENGTH);
00849         #ifdef _MSC_VER
00850                                 sprintf(tmp_line, "\n\t\tFEC-OTI-Transfer-Length=\"%I64u\"", tmp_file->transfer_len);
00851         #else
00852                                 sprintf(tmp_line, "\n\t\tTransfer-Length=\"%llu\"", tmp_file->transfer_len);
00853         #endif
00854                                 size += strlen(tmp_line);
00855 
00856                                 /* Reallocate memory for fdt_inst_payload */
00857                                 if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00858         #ifdef _MSC_VER
00859                                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00860         #else   
00861                                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00862         #endif
00863                                         return NULL;
00864                                 }
00865 
00866                                 memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00867                                 position += strlen(tmp_line);
00868                 }
00869 
00870                 if(s->use_fec_oti_ext_hdr == 0) {
00871                         /**** FEC-OTI parameters ****/
00872 
00873 #ifdef FDT_INST_FEC_OTI_FILE
00874 
00875                         memset(tmp_line, 0, MAX_PATH_LENGTH);
00876                         sprintf(tmp_line, "\n\t\tFEC-OTI-FEC-Encoding-ID=\"%u\"", tmp_file->fec_enc_id);
00877                         size += strlen(tmp_line);
00878         
00879                         /* Reallocate memory for fdt_inst_payload */
00880                         if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00881 #ifdef _MSC_VER
00882                                 printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00883 #else
00884                                 printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00885 #endif
00886                                 return NULL;
00887                         }
00888 
00889                         memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00890                         position += strlen(tmp_line);
00891 
00892                         if(tmp_file->fec_encoding_id >= 128) {
00893                 
00894                                 memset(tmp_line, 0, MAX_PATH_LENGTH);
00895                                 sprintf(tmp_line, "\n\t\tFEC-OTI-FEC-Instance-ID=\"%u\"", tmp_file->fec_inst_id);
00896                                 size += strlen(tmp_line);
00897         
00898                                 /* Reallocate memory for fdt_inst_payload */
00899                                 if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00900 #ifdef _MSC_VER
00901                                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00902 #else
00903                                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00904 #endif
00905                                         return NULL;
00906                                 }
00907 
00908                                 memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00909                                 position += strlen(tmp_line);
00910                         }
00911                 
00912                         memset(tmp_line, 0, MAX_PATH_LENGTH);
00913                         sprintf(tmp_line, "\n\t\tFEC-OTI-Maximum-Source-Block-Length=\"%u\"",
00914                                         tmp_file->max_sblen);
00915                         size += strlen(tmp_line);
00916         
00917                         /* Reallocate memory for fdt_inst_payload */
00918                         if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00919 #ifdef _MSC_VER
00920                                 printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00921 #else
00922                                 printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00923 #endif
00924                                 return NULL;
00925                         }
00926 
00927                         memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00928                         position += strlen(tmp_line);
00929                 
00930                         memset(tmp_line, 0, MAX_PATH_LENGTH);
00931                         sprintf(tmp_line, "\n\t\tFEC-OTI-Encoding-Symbol-Length=\"%u\"",
00932                                         tmp_file->es_len);
00933                         size += strlen(tmp_line);
00934         
00935                         /* Reallocate memory for fdt_inst_payload */
00936                         if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00937 #ifdef _MSC_VER
00938                                 printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00939 #else
00940                                 printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00941 #endif
00942                                 return NULL;
00943                         }
00944 
00945                         memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00946                         position += strlen(tmp_line);
00947         
00948                         
00949                         if(tmp_file->fec_enc_id == RS_FEC_ENC_ID || tmp_file->fec_enc_id == SB_SYS_FEC_ENC_ID) {
00950                                 memset(tmp_line, 0, MAX_PATH_LENGTH);
00951                                 sprintf(tmp_line, "\n\t\tFEC-OTI-Max-Number-of-Encoding-Symbols=\"%u\"",
00952                                                 tmp_file->max_nb_of_es);
00953                                 size += strlen(tmp_line);
00954         
00955                                 /* Reallocate memory for fdt_inst_payload */
00956                                 if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00957 #ifdef _MSC_VER
00958                                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00959 #else
00960                                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00961 #endif
00962                                         return NULL;
00963                                 }
00964 
00965                                 memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00966                                 position += strlen(tmp_line);
00967                         }
00968 
00969                         if(tmp_file->fec_enc_id == RS_FEC_ENC_ID) {
00970 
00971                                 memset(tmp_line, 0, MAX_PATH_LENGTH);
00972                                 sprintf(tmp_line, "\n\tFEC-OTI-Number-of-Encoding-Symbols-per-Group=\"%u\"",
00973                                                 tmp_file->nb_es_per_group);
00974                                 size += strlen(tmp_line);
00975 
00976                                 /* Reallocate memory for fdt_inst_payload */
00977                                 if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00978 #ifdef _MSC_VER
00979                                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00980 #else
00981                                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00982 #endif
00983                                         return NULL;
00984                                 }
00985 
00986                                 memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
00987                                 position += strlen(tmp_line);
00988 
00989                                 memset(tmp_line, 0, MAX_PATH_LENGTH);
00990                                 sprintf(tmp_line, "\n\tFEC-OTI-Finite-Field-Parameter=\"%u\"", tmp_file->finite_field);
00991                                 size += strlen(tmp_line);
00992 
00993                                 /* Reallocate memory for fdt_inst_payload */
00994                                 if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
00995 #ifdef _MSC_VER
00996                                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
00997 #else
00998                                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
00999 #endif
01000                                         return NULL;
01001                                 }
01002 
01003                                 memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
01004                                 position += strlen(tmp_line);
01005                         }
01006 
01007 #elif defined(FDT_INST_FEC_OTI_COMMON)
01008                         
01009                         if(tmp_file->fec_enc_id != fdt->fec_enc_id) {
01010                                 memset(tmp_line, 0, MAX_PATH_LENGTH);
01011                                 sprintf(tmp_line, "\n\t\tFEC-OTI-FEC-Encoding-ID=\"%u\"", tmp_file->fec_enc_id);
01012                                 size += strlen(tmp_line);
01013                 
01014                                 /* Reallocate memory for fdt_inst_payload */
01015                                 if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
01016 #ifdef _MSC_VER
01017                                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
01018 #else
01019                                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
01020 #endif
01021                                         return NULL;
01022                                 }
01023 
01024                                 memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
01025                                 position += strlen(tmp_line);
01026                         }
01027                         
01028                         if(tmp_file->fec_enc_id >= 128) {
01029 
01030                                 if(tmp_file->fec_inst_id != fdt->fec_inst_id) {
01031                                         memset(tmp_line, 0, MAX_PATH_LENGTH);
01032                                         sprintf(tmp_line, "\n\t\tFEC-OTI-FEC-Instance-ID=\"%u\"", tmp_file->fec_inst_id);
01033                                         size += strlen(tmp_line);
01034                 
01035                                         /* Reallocate memory for fdt_inst_payload */
01036                                         if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
01037 #ifdef _MSC_VER
01038                                                 printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
01039 #else
01040                                                 printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
01041 #endif
01042                                                 return NULL;
01043                                         }
01044 
01045                                         memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
01046                                         position += strlen(tmp_line);
01047                                 }
01048                         }
01049 
01050                         if(tmp_file->max_sb_len != fdt->max_sb_len) {
01051                                 memset(tmp_line, 0, MAX_PATH_LENGTH);
01052                                 sprintf(tmp_line, "\n\t\tFEC-OTI-Maximum-Source-Block-Length=\"%u\"",
01053                                                 tmp_file->max_sb_len);
01054                                 size += strlen(tmp_line);
01055                 
01056                                 /* Reallocate memory for fdt_inst_payload */
01057                                 if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
01058 #ifdef _MSC_VER
01059                                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
01060 #else
01061                                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
01062 #endif
01063                                         return NULL;
01064                                 }
01065 
01066                                 memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
01067                                 position += strlen(tmp_line);
01068                         }
01069                         if(tmp_file->es_len != fdt->es_len) {
01070                                 memset(tmp_line, 0, MAX_PATH_LENGTH);
01071                                 sprintf(tmp_line, "\n\t\tFEC-OTI-Encoding-Symbol-Length=\"%u\"",
01072                                                 tmp_file->es_len);
01073                                 size += strlen(tmp_line);
01074                 
01075                                 /* Reallocate memory for fdt_inst_payload */
01076                                 if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
01077 #ifdef _MSC_VER
01078                                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
01079 #else
01080                                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
01081 #endif
01082                                         return NULL;
01083                                 }
01084 
01085                                 memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
01086                                 position += strlen(tmp_line);
01087                         }
01088         
01089                         if(tmp_file->fec_enc_id == RS_FEC_ENC_ID || tmp_file->fec_enc_id == SB_SYS_FEC_ENC_ID) {
01090                                 if(tmp_file->max_nb_of_es != fdt->max_nb_of_es) {
01091                                         memset(tmp_line, 0, MAX_PATH_LENGTH);
01092                                         sprintf(tmp_line, "\n\t\tFEC-OTI-Max-Number-of-Encoding-Symbols=\"%u\"",
01093                                                         tmp_file->max_nb_of_es);
01094                                         size += strlen(tmp_line);
01095                         
01096                                         /* Reallocate memory for fdt_inst_payload */
01097                                         if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
01098 #ifdef _MSC_VER
01099                                                 printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
01100 #else
01101                                                 printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
01102 #endif
01103                                                 return NULL;
01104                                         }
01105 
01106                                         memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
01107                                         position += strlen(tmp_line);
01108                                 }
01109                         }
01110 
01111                         if(tmp_file->fec_enc_id == RS_FEC_ENC_ID) {
01112                                 if(tmp_file->nb_of_es_per_group != fdt->nb_of_es_per_group) {
01113                                         memset(tmp_line, 0, MAX_PATH_LENGTH);
01114                                         sprintf(tmp_line, "\n\tFEC-OTI-Number-of-Encoding-Symbols-per-Group=\"%u\"", tmp_file->nb_of_es_per_group);
01115                                         size += strlen(tmp_line);
01116 
01117                                         /* Reallocate memory for fdt_inst_payload */
01118                                         if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
01119 #ifdef _MSC_VER
01120                                                 printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
01121 #else
01122                                                 printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
01123 #endif
01124                                                 return NULL;
01125                                         }
01126 
01127                                         memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
01128                                         position += strlen(tmp_line);
01129                                 }
01130         
01131                                 if(tmp_file->finite_field != fdt->finite_field) {
01132                                         
01133                                         memset(tmp_line, 0, MAX_PATH_LENGTH);
01134                                         sprintf(tmp_line, "\n\tFEC-OTI-Finite-Field-Parameter=\"%u\"", tmp_file->finite_field);
01135                                         size += strlen(tmp_line);
01136 
01137                                         /* Reallocate memory for fdt_inst_payload */
01138                                         if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
01139 #ifdef _MSC_VER
01140                                                 printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
01141 #else
01142                                                 printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
01143 #endif
01144                                                 return NULL;
01145                                         }
01146 
01147                                         memcpy((fdt_inst_payload + (unsigned int)position), tmp_line, strlen(tmp_line));
01148                                         position += strlen(tmp_line);
01149                                 }
01150                         }
01151 #endif
01152                 }
01153                 
01154                 memset(tmp_line, 0, MAX_PATH_LENGTH);
01155         sprintf(tmp_line, "/>\n");
01156                 size += strlen(tmp_line);
01157         
01158                 /* Reallocate memory for fdt_inst_payload */
01159                 if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
01160 #ifdef _MSC_VER
01161                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
01162 #else
01163                         printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
01164 #endif
01165                         return NULL;
01166                 }
01167 
01168                 memcpy((fdt_inst_payload + position), tmp_line, strlen(tmp_line));
01169                 position += strlen(tmp_line);
01170 
01171                 if(tmp_file->next == NULL) {
01172                         break;
01173                 }
01174 
01175                 tmp_file = tmp_file->next;
01176         }
01177  
01178         memset(tmp_line, 0, MAX_PATH_LENGTH);
01179     sprintf(tmp_line, "</FDT-Instance>\n");
01180         size += strlen(tmp_line);
01181 
01182         size++;
01183         
01184         /* Reallocate memory for fdt_inst_payload */
01185         if(!(fdt_inst_payload = (char*)realloc(fdt_inst_payload, ((unsigned int)size * sizeof(char))))) {
01186 #ifdef _MSC_VER
01187                 printf("Could not (re)alloc memory for fdt_inst_payload, size: %I64u!\n", size);
01188 #else
01189                 printf("Could not (re)alloc memory for fdt_inst_payload, size: %llu!\n", size);
01190 #endif
01191                 return NULL;
01192         }
01193 
01194     memcpy((fdt_inst_payload + position), tmp_line, strlen(tmp_line));
01195         position += strlen(tmp_line);
01196         
01197         *(fdt_inst_payload + position) = '\0';
01198 
01199         *fdt_inst_len = size - 1;
01200 
01201         return fdt_inst_payload;
01202 }
01203 
01204 #ifdef USE_ZLIB
01205 
01217 int remove_gz_files(char *fdt_file, char *base_dir, char *file_path) {
01218         
01219         uri_t *uri;
01220         char path[MAX_PATH_LENGTH];
01221         file_t *file;
01222         fdt_t *fdt;
01223         char *buf = NULL;
01224         struct stat     file_stats;
01225         unsigned int fdt_length = 0; 
01226     FILE *fp;
01227         int nbytes;
01228 
01229 #ifdef _MSC_VER
01230         int j;
01231 #endif
01232         
01233         if(stat(fdt_file, &file_stats) == -1) {
01234                 printf("Error: %s is not valid file name\n", fdt_file);
01235                 fflush(stdout);
01236                 return -1;
01237         }
01238 
01239         fdt_length = file_stats.st_size;
01240 
01241         /* Allocate memory for buf, to read fdt file to it */
01242         if(!(buf = (char*)calloc((fdt_length+1), sizeof(char)))) {
01243                 printf("Could not alloc memory for fdt buffer!\n");
01244                 fflush(stdout);
01245                 return  -1;
01246         }
01247 
01248         if((fp = fopen(fdt_file, "rb")) == NULL) {
01249                 printf("Error: unable to open fdt_file %s\n", fdt_file);
01250                 fflush(stdout);
01251                 free(buf);
01252                 return -1;
01253         }
01254 
01255         nbytes = fread(buf, 1, fdt_length, fp);
01256 
01257         if(nbytes <= 0) {
01258                 printf("fread error\n");
01259                 fflush(stdout);
01260                 fclose(fp);
01261                 free(buf);
01262                 return -1;
01263         }
01264 
01265         fdt = decode_fdt_payload(buf);
01266         free(buf);
01267         fclose(fp);
01268 
01269         if(fdt == NULL) {
01270                 return -1;
01271         }
01272         
01273         file = fdt->file_list;
01274 
01275         while(file != NULL) {
01276 
01277                 uri = parse_uri(file->location, strlen(file->location));
01278 
01279                 memset(path, 0, MAX_PATH_LENGTH);
01280 
01281                 if(!(strcmp(base_dir, "") == 0)) {
01282                         strcpy(path, base_dir);
01283                         strcat(path, "/");
01284                 }
01285                 strcat(path, uri->path);
01286                 strcat(path, GZ_SUFFIX);
01287 
01288 #ifdef _MSC_VER
01289                 for(j = 0; j < (int)strlen(path); j++) {
01290                         if(path[j] == '/') {
01291                                 path[j] = '\\';
01292                         }
01293                 }
01294 #endif
01295 
01296                 if(remove(path) == -1) {
01297                         printf("%s: errno %i\n", path, errno);
01298                         fflush(stdout);
01299                 }
01300 
01301                 file = file->next;
01302         }
01303 
01304         return 0;
01305 }
01306 
01307 #endif
01308 
01309 
01321 int fdtbasedsend(flute_sender_t *sender, int tx_mode, arguments_t *a) {
01322   
01323   char *fdt_inst_buf = NULL;
01324   unsigned long long fdt_inst_len = 0; 
01325 
01326 #ifdef USE_ZLIB
01327   char *compr_fdt_inst_buf = NULL;
01328   unsigned long long compr_fdt_inst_buf_len = 0;
01329 #endif
01330   
01331 #ifdef _MSC_VER
01332   int j;
01333 #endif
01334     
01335   file_t *file;
01336   uri_t *uri;
01337   
01338   char path[MAX_PATH_LENGTH];
01339   
01340   static int nb_of_instances_in_last_round = 0;
01341   
01342   unsigned short file_es_len = a->alc_a.es_len;
01343   unsigned int file_max_sb_len = a->alc_a.max_sb_len;
01344   unsigned char file_fec_enc_id = a->alc_a.fec_enc_id;
01345   unsigned short file_fec_inst_id = a->alc_a.fec_inst_id;
01346   
01347   time_t systime;
01348   
01349   unsigned long long curr_time;
01350   
01351   
01352   int sent = 0;
01353   BOOL incomplete_fdt = FALSE;
01354   
01355   time(&systime);
01356   curr_time = systime + 2208988800U;
01357 
01358   /*
01359   set_session_tx_toi(s_id, 0);
01360   */
01361 
01362   set_fdt_instance_id(sender->s_id, (get_fdt_instance_id(sender->s_id) - nb_of_instances_in_last_round));
01363   nb_of_instances_in_last_round = 0;
01364 
01365   if(sender->fdt->expires < curr_time) {
01366     set_session_state(sender->s_id, SExiting);
01367     return -2;
01368   }
01369 
01370   if(sender->fdt->complete || a->complete_fdt > 0) {
01371      
01372     if(!sender->fdt->complete) {
01373       sender->fdt->complete = TRUE;
01374     }
01375     
01376     /* Send "Complete FDT" with all File definitions at the beginning of the carousel */
01377     
01378     file = sender->fdt->file_list;
01379     
01380     fdt_inst_buf = create_fdt_instance(file, sender->fdt->nb_of_files, sender->fdt, sender->s_id, &fdt_inst_len);
01381 
01382     if(fdt_inst_buf == NULL) {
01383       return -1;
01384     }
01385     
01386 #ifdef USE_ZLIB
01387     if(a->alc_a.encode_content == ZLIB_FDT || a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) {
01388       
01389       compr_fdt_inst_buf = buffer_zlib_compress(fdt_inst_buf, fdt_inst_len, &compr_fdt_inst_buf_len);
01390       
01391       if(compr_fdt_inst_buf == NULL) {
01392                 free(fdt_inst_buf);
01393                 return -1;
01394       } 
01395         }
01396 #endif
01397     
01398 #ifdef USE_ZLIB
01399     if(a->alc_a.encode_content == ZLIB_FDT || a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) {
01400       sent = send_fdt_instance(compr_fdt_inst_buf, compr_fdt_inst_buf_len, sender->s_id, tx_mode, a->alc_a.es_len,
01401                                a->alc_a.max_sb_len, a->alc_a.fec_enc_id, a->alc_a.fec_inst_id, a->alc_a.verbosity);
01402     }
01403     else {
01404       sent = send_fdt_instance(fdt_inst_buf, fdt_inst_len, sender->s_id, tx_mode, a->alc_a.es_len, a->alc_a.max_sb_len,
01405                                a->alc_a.fec_enc_id, a->alc_a.fec_inst_id, a->alc_a.verbosity);
01406     }
01407 #else
01408     sent = send_fdt_instance(fdt_inst_buf, fdt_inst_len, sender->s_id, tx_mode, a->alc_a.es_len, a->alc_a.max_sb_len,
01409                              a->alc_a.fec_enc_id, a->alc_a.fec_inst_id, a->alc_a.verbosity);
01410 #endif
01411 
01412     if(a->complete_fdt != 2) {
01413         set_fdt_instance_id(sender->s_id, (get_fdt_instance_id(sender->s_id) + 1));
01414         nb_of_instances_in_last_round++;
01415         }
01416 
01417 #ifdef USE_ZLIB
01418         if(a->alc_a.encode_content == ZLIB_FDT || a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) {
01419           free(compr_fdt_inst_buf);
01420           compr_fdt_inst_buf = NULL;
01421           compr_fdt_inst_buf_len = 0;
01422         }
01423 #endif
01424         free(fdt_inst_buf);
01425         fdt_inst_buf = NULL;
01426         fdt_inst_len = 0; 
01427     
01428     if(sent == -1) {
01429       return -1;
01430     }
01431     else if(sent == -2) {               
01432           return -2;
01433     }
01434   }
01435   
01436   file = sender->fdt->file_list;
01437   
01438   while(file != NULL) { /* loop for sending all files in fdt */
01439         
01440     if(file->toi == 0) {
01441                 incomplete_fdt = TRUE;
01442     }
01443     else if(file->location == NULL) {
01444                 incomplete_fdt = TRUE;
01445     }
01446     
01447     if(file->encoding != NULL) {
01448 #ifdef USE_ZLIB
01449                 if(!strcmp(file->encoding, "gzip") || !strcmp(file->encoding, "pad")) {
01450 #else
01451                 if(!strcmp(file->encoding, "pad")) {
01452 #endif
01453                         if(file->content_len == 0) {
01454                                 incomplete_fdt = TRUE;
01455                         }
01456                         else if(file->transfer_len == 0) {
01457                                 incomplete_fdt = TRUE;
01458                         }
01459                 }
01460         }
01461         
01462         if(incomplete_fdt) {
01463 #ifdef _MSC_VER
01464                 printf("FDT does not contain enough File information, TOI: %I64u\n", file->toi);
01465 #else
01466                 printf("FDT does not contain enough File information, TOI: %llu\n", file->toi);
01467 #endif
01468                 fflush(stdout);
01469                 return -1;
01470         }
01471         
01472         if((file->encoding != NULL) && (strcmp(a->file_path, "") == 0)) {
01473                 printf("Content encoding is not supported with -f option.\n");
01474                 fflush(stdout);
01475                 return -1;
01476         }
01477       
01478         uri = parse_uri(file->location, strlen(file->location));
01479       
01480     memset(path, 0, MAX_PATH_LENGTH);
01481       
01482       if(!(strcmp(a->alc_a.base_dir, "") == 0)) {
01483         strcpy(path, a->alc_a.base_dir);
01484         strcat(path, "/");
01485       }
01486       strcat(path, uri->path);
01487       
01488 #ifdef USE_ZLIB
01489       if(file->encoding != NULL) {
01490         if(!strcmp(file->encoding, "gzip")) {
01491           strcat(path, GZ_SUFFIX);                     
01492         }
01493       }
01494 #endif
01495                 
01496       if(tx_mode != TX_THREAD) {
01497         if(((file->es_len != 0) && (file->max_sb_len != 0))) {
01498           file_es_len = file->es_len;
01499           file_max_sb_len = file->max_sb_len;
01500         }
01501       }
01502       
01503       if(file->fec_enc_id != -1) {
01504         if(file->fec_enc_id != file_fec_enc_id) {
01505           file_fec_enc_id = (unsigned char)file->fec_enc_id;
01506           file_fec_inst_id = (unsigned short)file->fec_inst_id;
01507         }
01508       }
01509       
01510 #ifdef _MSC_VER
01511       for(j = 0; j < (int)strlen(path); j++) {
01512         if(*(path + j) == '/') {
01513           *(path + j) = '\\';
01514         }
01515       }
01516 #endif
01517      
01518           if(a->complete_fdt != 2) {
01519                   
01520                 /* Send FDT Instance with next File definition before the file is sent */
01521 
01522                 fdt_inst_buf = create_fdt_instance(file, 1, sender->fdt, sender->s_id, &fdt_inst_len);
01523       
01524                 if(fdt_inst_buf == NULL) {
01525                         free_uri(uri);
01526                         return -1;
01527                 }
01528       
01529 #ifdef USE_ZLIB
01530                 if(a->alc_a.encode_content == ZLIB_FDT || a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) {
01531         
01532                         compr_fdt_inst_buf = buffer_zlib_compress(fdt_inst_buf, fdt_inst_len, &compr_fdt_inst_buf_len);
01533         
01534                         if(compr_fdt_inst_buf == NULL) {
01535                                 free_uri(uri);
01536                                 free(fdt_inst_buf);
01537                                 return -1;
01538                         }       
01539                 }
01540 #endif
01541       
01542 #ifdef USE_ZLIB
01543                 if(a->alc_a.encode_content == ZLIB_FDT || a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) {
01544                         sent = send_fdt_instance(compr_fdt_inst_buf, compr_fdt_inst_buf_len, sender->s_id, tx_mode, a->alc_a.es_len,
01545                                    a->alc_a.max_sb_len, a->alc_a.fec_enc_id, a->alc_a.fec_inst_id, a->alc_a.verbosity);
01546                 }
01547                 else {
01548                         sent = send_fdt_instance(fdt_inst_buf, fdt_inst_len, sender->s_id, tx_mode, a->alc_a.es_len, a->alc_a.max_sb_len,
01549                                    a->alc_a.fec_enc_id, a->alc_a.fec_inst_id, a->alc_a.verbosity);
01550                 }
01551 #else
01552                 sent = send_fdt_instance(fdt_inst_buf, fdt_inst_len, sender->s_id, tx_mode, a->alc_a.es_len, a->alc_a.max_sb_len,
01553                                  a->alc_a.fec_enc_id, a->alc_a.fec_inst_id, a->alc_a.verbosity);
01554 #endif
01555         
01556                 if(sent == -1) {
01557           
01558 #ifdef USE_ZLIB
01559                         if(a->alc_a.encode_content == ZLIB_FDT || a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) {
01560                                 free(compr_fdt_inst_buf);
01561                         }
01562 #endif
01563                         free(fdt_inst_buf);
01564                         free_uri(uri);
01565                         return -1;
01566                 }
01567                 else if(sent == -2) {
01568           
01569 #ifdef USE_ZLIB
01570                         if(a->alc_a.encode_content == ZLIB_FDT || a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) {
01571                                 free(compr_fdt_inst_buf);
01572                         }
01573 #endif
01574                         free(fdt_inst_buf);
01575                         free_uri(uri);
01576                         return -2;
01577                 }
01578           }
01579         
01580 #ifdef USE_ZLIB
01581       if(a->alc_a.encode_content == ZLIB_FDT || a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) {
01582                         sent = send_file(path, sender->s_id, tx_mode, file_es_len, file_max_sb_len, file_fec_enc_id,
01583                          file_fec_inst_id, file, compr_fdt_inst_buf_len, compr_fdt_inst_buf,
01584                          a->alc_a.verbosity);
01585       }
01586       else {
01587                         sent = send_file(path, sender->s_id, tx_mode, file_es_len, file_max_sb_len, file_fec_enc_id,
01588                          file_fec_inst_id, file, fdt_inst_len, fdt_inst_buf, a->alc_a.verbosity);       
01589       }
01590 #else
01591       sent = send_file(path, sender->s_id, tx_mode, file_es_len, file_max_sb_len, file_fec_enc_id,
01592                        file_fec_inst_id, file, fdt_inst_len, fdt_inst_buf, a->alc_a.verbosity); 
01593 #endif
01594 
01595       free_uri(uri);
01596 
01597       if((a->complete_fdt != 2)) {
01598         
01599                 set_fdt_instance_id(sender->s_id, (get_fdt_instance_id(sender->s_id) + 1));
01600                 nb_of_instances_in_last_round++;
01601       
01602 #ifdef USE_ZLIB
01603                 if(a->alc_a.encode_content == ZLIB_FDT || a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) {
01604                         free(compr_fdt_inst_buf);
01605                 }
01606 #endif
01607                 free(fdt_inst_buf);
01608       }
01609       
01610       if(sent == -1) {
01611                 return -1;
01612       }
01613       else if(sent == -2) {               
01614                  return -2;
01615       }
01616       
01617       file = file->next;      
01618     }
01619 
01620     return 0;
01621   }
01622 
01632 BOOL IsFECOTIInFDT(fdt_t *fdt) {
01633         file_t *file = NULL;
01634         BOOL fec_oti_in_fdt = TRUE;
01635 
01636         file =  fdt->file_list;
01637  
01638         while(file != NULL) { /* loop for checking all files in fdt */
01639         
01640                 if(file->fec_enc_id == -1) {
01641                         fec_oti_in_fdt = FALSE;
01642                         break;
01643                 }
01644                 else if(((file->fec_enc_id >= 128) && (file->fec_inst_id == -1))) {
01645                         fec_oti_in_fdt = FALSE;
01646                         break;
01647                 }
01648                 else if(file->max_sb_len == 0) {
01649                         fec_oti_in_fdt = FALSE;
01650                         break;
01651                 }
01652                 else if(file->es_len == 0) {
01653                         fec_oti_in_fdt = FALSE;
01654                         break;
01655                 }
01656                 else if(((file->fec_enc_id == SB_SYS_FEC_ENC_ID) &&
01657                                  (file->max_nb_of_es == 0))) {
01658                         fec_oti_in_fdt = FALSE;
01659                         break;
01660                 }
01661                         
01662                 file = file->next;
01663         }
01664 
01665         return fec_oti_in_fdt;
01666 }
01667 
01668 int sender_in_fdt_based_mode(arguments_t *a, flute_sender_t *sender) {
01669 
01670   int retval = 0;
01671   int retcode = 0;
01672   
01673   alc_session_t *s;
01674   
01675   time_t systime;
01676   BOOL is_printed = FALSE;
01677   
01678   unsigned long long curr_time;
01679 
01680   struct stat fdt_file_stats;
01681   FILE *fp;
01682 
01683   char *buf = NULL;
01684 
01685   unsigned long long fdt_length = 0;
01686   long long nbytes = 0;
01687 
01688   BOOL is_fec_oti_in_fdt = TRUE;
01689 
01690   if(stat(a->fdt_file, &fdt_file_stats) == -1) {
01691     printf("Error: %s is not valid file name\n", a->fdt_file);
01692     fflush(stdout);
01693     return -1;
01694   }
01695 
01696   fdt_length = fdt_file_stats.st_size;
01697 
01698   /* Allocate memory for buf, to read fdt file to it */
01699   if(!(buf = (char*)calloc((unsigned int)(fdt_length + 1), sizeof(char)))) {
01700     printf("Could not alloc memory for fdt buffer!\n");
01701     return -1;
01702   }
01703 
01704   if((fp = fopen(a->fdt_file, "rb")) == NULL) {
01705     printf("Error: unable to open FDT file %s\n", a->fdt_file);
01706     fflush(stdout);
01707     free(buf);
01708     return -1;
01709   }
01710 
01711   nbytes = fread(buf, 1, (unsigned int)fdt_length, fp);
01712 
01713   if(nbytes <= 0) {
01714     printf("fread error\n");
01715     fflush(stdout);
01716     fclose(fp);
01717     free(buf);
01718     return -1;
01719   }
01720 
01721   sender->fdt = decode_fdt_payload(buf);
01722 
01723   free(buf);
01724   fclose(fp);
01725 
01726   if(sender->fdt == NULL) {
01727     return -1;
01728   }
01729 
01730   time(&systime);
01731   curr_time = systime + 2208988800U;
01732 
01733   if(sender->fdt->expires < curr_time) {
01734     set_session_state(sender->s_id, SExiting);
01735     return -2;
01736   }
01737   
01738   if(a->alc_a.use_fec_oti_ext_hdr == 0) {
01739         /* check that we have FEC OTI in FDT */ 
01740         
01741         is_fec_oti_in_fdt = IsFECOTIInFDT(sender->fdt);
01742 
01743         if(is_fec_oti_in_fdt == FALSE) {                        
01744 #ifdef _MSC_VER
01745                 printf("FDT does not contain enough FEC-OTI information\n");
01746 #else
01747                 printf("FDT does not contain enough FEC-OTI information\n");
01748 #endif
01749                 fflush(stdout);
01750                 return -1;
01751         }
01752   }
01753 
01754   s = get_alc_session(sender->s_id);
01755   
01756   if(a->alc_a.start_time != 0) {
01757     while(1) {
01758       
01759       time(&systime);
01760       curr_time = systime + 2208988800U;
01761       
01762       if(a->alc_a.start_time > curr_time) {
01763         
01764         if(!is_printed) {
01765           printf("Waiting for session start time...\n");
01766           fflush(stdout);
01767           is_printed = TRUE;
01768         }
01769         
01770 #ifdef _MSC_VER
01771         Sleep(1000);
01772 #else
01773         sleep(1);
01774 #endif
01775       }
01776       else {
01777         break;
01778       }
01779       
01780       if(s->state == SExiting) {
01781         return -2;
01782       }
01783     }
01784   }
01785 
01786   while(a->alc_a.nb_tx) {
01787     
01788     if(a->alc_a.cc_id == Null) {
01789       
01790       if(a->alc_a.nb_channel == 1) {              
01791                 retcode = fdtbasedsend(sender, NO_TX_THREAD, a);
01792       }
01793       else {
01794                 retcode = fdtbasedsend(sender, TX_THREAD, a);
01795       }
01796     }
01797     else if(a->alc_a.cc_id == RLC) {
01798       retcode = fdtbasedsend(sender, TX_THREAD, a);
01799     }
01800 
01801     if(retcode == -1) {
01802       printf("\nError: fdtbasedsend() failed\n");
01803       retval = -1;
01804       break;
01805     }
01806     else if(retcode == -2) {
01807       retval = -2;
01808       break;
01809     }
01810 
01811     if(!a->cont) {
01812       a->alc_a.nb_tx--;
01813     }
01814     
01815     if(((a->alc_a.cc_id == RLC) || ((a->alc_a.cc_id == Null) && (a->alc_a.nb_channel != 1)))) {
01816       
01817       while(s->tx_queue_begin != NULL) {
01818         
01819         if(s->state == SExiting) {
01820 #ifdef USE_ZLIB
01821           if(a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) {
01822             remove_gz_files(a->fdt_file, a->alc_a.base_dir, a->file_path);
01823           }
01824 #endif
01825           return -2;
01826         }
01827 #ifdef _MSC_VER
01828         Sleep(1);
01829 #else
01830         usleep(1000);
01831 #endif                  
01832       }
01833     }
01834   }
01835  
01836 #ifdef USE_ZLIB
01837   if(a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) {
01838     remove_gz_files(a->fdt_file, a->alc_a.base_dir, a->file_path);
01839   }
01840 #endif
01841 
01842   return retval;
01843 }

Generated on Fri Mar 9 19:59:54 2007 for MAD-FCL by  doxygen 1.5.0