repair_sender.c

Go to the documentation of this file.
00001 
00033 #include <stdlib.h>
00034 #include <stdio.h>
00035 #include <sys/types.h>
00036 #include <sys/stat.h>
00037 #include <fcntl.h>
00038 #include <string.h>
00039 #include <time.h>
00040 
00041 #ifdef _MSC_VER
00042 #include <windows.h>
00043 #include <io.h>
00044 #else
00045 #include <unistd.h>
00046 #include <arpa/inet.h>
00047 #endif
00048 
00049 #include "../flutelib/flute.h"
00050 #include "../flutelib/uri.h"
00051 #include "../flutelib/mad_zlib.h"
00052 
00053 #include "../alclib/blocking_alg.h"
00054 #include "../alclib/alc_session.h"
00055 #include "../alclib/alc_tx.h"
00056 
00057 #include "repair_sender.h"
00058 
00059 miss_obj_t *miss_obj_list;  
00068 miss_block_t* create_miss_block(void) {
00069   
00070   miss_block_t *miss_block = NULL;
00071   
00072   if (!(miss_block = (miss_block_t*)calloc(1, sizeof(miss_block_t)))) {
00073     printf("Could not alloc memory for missing block!\n");
00074     return NULL;
00075   }
00076   
00077   return miss_block;
00078 }
00079 
00087 miss_obj_t* create_miss_object(void) {
00088   
00089   miss_obj_t *miss_obj = NULL;
00090   
00091   if (!(miss_obj = (miss_obj_t*)calloc(1, sizeof(miss_obj_t)))) {
00092     printf("Could not alloc memory for missing object!\n");
00093     return NULL;
00094   }
00095   
00096   return miss_obj;
00097 }
00098 
00106 void insert_miss_object(miss_obj_t *miss_obj) {
00107   
00108   miss_obj_t * tmp = miss_obj_list;
00109   
00110   if(tmp == NULL) {
00111     miss_obj_list = miss_obj;
00112   }
00113   else {
00114     for(;;) {
00115       if(miss_obj->toi < tmp->toi) {
00116         
00117         if(tmp->prev == NULL) {
00118           miss_obj->next = tmp;
00119           miss_obj->prev = tmp->prev;
00120           
00121           tmp->prev = miss_obj;
00122           
00123           miss_obj_list = miss_obj;
00124         }
00125         else {
00126           miss_obj->next = tmp;
00127           miss_obj->prev = tmp->prev;
00128           
00129           tmp->prev->next = miss_obj;
00130           tmp->prev = miss_obj;
00131         }
00132         break;
00133       }
00134       
00135       if(tmp->next == NULL) {
00136         
00137         miss_obj->next = tmp->next;
00138         miss_obj->prev = tmp;
00139         
00140         tmp->next = miss_obj;
00141         break;
00142       }
00143       
00144       tmp = tmp->next;
00145     }
00146   }
00147 }
00148 
00157 void insert_miss_block(miss_block_t *miss_block, miss_obj_t *miss_obj) {
00158   
00159   miss_block_t *tmp;
00160   
00161   tmp = miss_obj->block_list;
00162   
00163   if(tmp == NULL) {
00164     miss_obj->block_list = miss_block;
00165   }
00166   else {
00167     for(;;) {
00168       if(miss_block->sbn < tmp->sbn) {
00169         
00170         if(tmp->prev == NULL) {
00171           miss_block->next = tmp;
00172           miss_block->prev = tmp->prev;
00173           
00174           tmp->prev = miss_block;
00175           
00176           miss_obj->block_list = miss_block;
00177         }
00178         else {
00179           miss_block->next = tmp;
00180           miss_block->prev = tmp->prev;
00181           
00182           tmp->prev->next = miss_block;
00183           tmp->prev = miss_block;
00184           
00185         }
00186         break;
00187       }
00188       if(tmp->next == NULL) {
00189         miss_block->next = tmp->next;
00190         miss_block->prev = tmp;
00191         
00192         tmp->next = miss_block;
00193         break;
00194       }
00195       tmp = tmp->next;
00196     }
00197   }
00198 }
00199 
00209 miss_obj_t* miss_object_exist(unsigned long long toi) {
00210   
00211   miss_obj_t *miss_obj = miss_obj_list;
00212   
00213   if(miss_obj != NULL) {
00214     for(;;) {
00215       if(miss_obj->toi == toi) {
00216         break;
00217       }
00218       if(miss_obj->next == NULL) {
00219         miss_obj = NULL;
00220         break;
00221       }
00222       miss_obj = miss_obj->next;
00223     }
00224   }
00225   
00226   return miss_obj;
00227 }
00228 
00239 miss_block_t* miss_block_exist(unsigned int sbn, miss_obj_t *miss_obj) {
00240   
00241   miss_block_t *miss_block;
00242   
00243   miss_block = miss_obj->block_list;
00244   
00245   if(miss_block != NULL) {
00246     for(;;) {
00247       if(miss_block->sbn == sbn) {
00248         break;
00249       }
00250       if(miss_block->next == NULL) {
00251         miss_block = NULL;
00252         break;
00253       }
00254       miss_block = miss_block->next;
00255     }
00256   }
00257   
00258   return miss_block;
00259 }
00260 
00266 void free_miss_object_list(void) {
00267   
00268   miss_obj_t *miss_obj;
00269   miss_obj_t *next_miss_obj;
00270   miss_block_t *miss_block;
00271   miss_block_t *next_miss_block;
00272   
00273   next_miss_obj = miss_obj_list;
00274   
00275   while(next_miss_obj != NULL) {
00276     
00277     miss_obj = next_miss_obj;
00278     
00279     next_miss_block = miss_obj->block_list;
00280     
00281     while(next_miss_block != NULL) {
00282       miss_block = next_miss_block;
00283       next_miss_block = miss_block->next;
00284       free(miss_block);
00285     }
00286     
00287     next_miss_obj = miss_obj->next;
00288     free(miss_obj);
00289   }
00290 }
00291 
00309 int send_repair_data(char *file_path, int *s_id, int tx_mode, unsigned long long toi, unsigned short es_len,
00310                                          unsigned int max_sb_len, unsigned char fec_enc_id, unsigned short fec_inst_id,
00311                                          miss_obj_t *miss_obj) {
00312   
00313   unsigned long long transfer_len;
00314 
00315   int sent = 0;
00316   int nbytes;
00317   int fp;
00318 #ifdef _MSC_VER
00319   struct __stat64 file_stats;
00320 #else
00321   struct stat64 file_stats;
00322 #endif
00323 
00324   char *buf = NULL;
00325   
00326   unsigned int sbn = 0;
00327   blocking_struct_t *bs;
00328   
00329   miss_block_t *miss_block = NULL;
00330 
00331   unsigned long long pos;
00332 
00333 #ifdef _MSC_VER
00334   if(_stat64(file_path, &file_stats) == -1) {
00335 #else
00336   if(stat64(file_path, &file_stats) == -1) {
00337 #endif
00338     printf("Error: %s is not valid file name\n", file_path);
00339     fflush(stdout);
00340     return -1;
00341   }
00342   
00343  transfer_len = (unsigned long long)file_stats.st_size;
00344   
00345   if(transfer_len == 0) {
00346     printf("Tx_file %s size = 0\n", file_path);
00347     fflush(stdout);
00348     return -1;
00349   }
00350   
00351   if(transfer_len > 0xFFFFFFFFFFFFULL) {
00352     printf("Tx_file %s too big!!\n", file_path);
00353     fflush(stdout);
00354     return -1;
00355   }
00356   
00357   /* Allocate memory for buf */
00358   if(!(buf = (char*)calloc((unsigned int)(es_len*max_sb_len), sizeof(char)))) {
00359     printf("Could not alloc memory for buf!\n");
00360     fflush(stdout);
00361     return -1;
00362   }
00363 
00364 #ifdef _MSC_VER
00365   if((fp = open(file_path, _O_BINARY, 0)) < 0) {
00366 #else
00367   if((fp = open64(file_path, 0, 0)) < 0) {
00368 #endif
00369       printf("Error: unable to open file %s\n", file_path);
00370       fflush(stdout);
00371       free(buf);
00372       return -1;
00373   }
00374 
00375   printf("\nSending file: %s\n\n", file_path);
00376   fflush(stdout);
00377   
00378   set_object_sent_bytes(*s_id, 0);
00379   set_object_last_print_tx_percent(*s_id, 0);
00380   
00381   /* Let's compute the blocking structure */
00382   bs = compute_blocking_structure(transfer_len, max_sb_len, es_len);
00383   
00384   while(sbn < bs->N) {
00385     
00386     miss_block = miss_block_exist(sbn, miss_obj);
00387 
00388     if(((miss_obj->block_list != NULL) && (miss_block == NULL))) {
00389       sbn++;
00390       continue;
00391     }
00392 
00393     if(sbn < bs->I) {
00394       nbytes = es_len * bs->A_large;
00395       pos = (unsigned long long)sbn * (unsigned long long)bs->A_large * (unsigned long long)es_len;
00396     }
00397     else {
00398       nbytes = es_len * bs->A_small;
00399       pos = ( ( ( (unsigned long long)bs->I * (unsigned long long)bs->A_large) +
00400                 ((unsigned long long)sbn - (unsigned long long)bs->I ) * (unsigned long long)bs->A_small )* (unsigned long long)es_len );
00401     }
00402 
00403 #ifdef _MSC_VER
00404     if((_lseeki64(fp, pos, SEEK_SET)) < 0) {
00405 #else
00406     if((lseek64(fp, pos, SEEK_SET)) < 0) {
00407 #endif
00408       printf("lseek error\n");
00409       fflush(stdout);
00410       free(buf);
00411       close(fp);
00412       return -1;
00413     }
00414     
00415     memset(buf, 0, (es_len * max_sb_len));
00416     nbytes = read(fp, buf, (unsigned int)nbytes);
00417     
00418     if(nbytes < 0) {
00419       printf("read error, file: %s\n", file_path);
00420       fflush(stdout);
00421       free(buf);
00422       close(fp);
00423       return -1;
00424     }
00425 
00426     sent = alc_send(*s_id, tx_mode, buf, nbytes, toi, transfer_len, es_len, max_sb_len, sbn, fec_enc_id, fec_inst_id);
00427     
00428     if(sent == -1) {
00429       /*free(sent_sbn);*/
00430       close(fp);
00431       free(buf);
00432       free(bs);
00433       return -1;
00434     }
00435     else if(sent == -2) {
00436       close(fp);
00437       free(buf);
00438       free(bs);
00439       return -2;
00440     }
00441     
00442     sbn++;
00443   }
00444   
00445   free(bs);
00446   close(fp);
00447   free(buf);
00448   
00449   printf("\nFile: %s sent.\n", file_path);
00450   fflush(stdout);
00451   
00452   return 0;
00453 }
00454   
00465 int repair_sender_in_fdt_based_mode(int *s_id, arguments_t *a) {
00466   
00467   char *buf = NULL;
00468   
00469 #ifdef _MSC_VER
00470   int j;
00471 #endif
00472   
00473   FILE *fp;
00474   struct stat file_stats;
00475   
00476   fdt_t *fdt;
00477   file_t *file;
00478   uri_t *uri;
00479   
00480   char path[MAX_PATH_LENGTH];
00481   
00482   static time_t fdt_mtime = 0;                                  /* Time of last FDT modification */
00483   
00484   unsigned short tmp_eslen = a->alc_a.es_len;
00485   unsigned int tmp_max_sblen = a->alc_a.max_sb_len;
00486   unsigned char tmp_fec_enc_id = a->alc_a.fec_enc_id;
00487   unsigned short tmp_fec_inst_id = a->alc_a.fec_inst_id;
00488   
00489   time_t systime;
00490   
00491   unsigned long long curr_time;
00492   unsigned long long fdt_length = 0;
00493   unsigned long long sent = 0;
00494   long long nbytes = 0;
00495   
00496   BOOL incomplete_fdt = FALSE;
00497   
00498   unsigned long long toi;
00499 
00500   unsigned int sbn;
00501   
00502   struct stat   repair_file_stats;
00503   char *repair_file_buf = NULL;
00504   unsigned repair_file_nbytes;
00505   FILE *repair_file_fp;
00506   
00507   miss_obj_t *miss_obj = NULL;
00508   miss_block_t *miss_block = NULL;
00509   miss_block_t *next_miss_block = NULL;
00510 
00511   char *tmp = NULL;
00512   
00513   if(stat(a->requested_blocks_file, &repair_file_stats) == -1) {
00514     printf("Error: %s is not valid file name\n", a->requested_blocks_file);
00515     fflush(stdout);
00516     return -1;
00517   }
00518   
00519   /* Allocate memory for buf */
00520   if(!(repair_file_buf = (char*)calloc((repair_file_stats.st_size + 1), sizeof(char)))) {
00521     printf("Could not alloc memory for buffer!\n");
00522     fflush(stdout);
00523     return -1;
00524   }
00525   
00526   if((repair_file_fp = fopen(a->requested_blocks_file, "rb")) == NULL) {
00527     free(repair_file_buf);
00528     return -1;
00529   }
00530   
00531   repair_file_nbytes = (unsigned int)fread(repair_file_buf, 1, repair_file_stats.st_size, repair_file_fp);
00532   
00533   if(repair_file_nbytes <= 0) {
00534     fclose(repair_file_fp);
00535     free(repair_file_buf);
00536     return -1;
00537   }
00538   
00539   fclose(repair_file_fp);
00540   
00541   /* TODO: TOI is 64bits, now using only 32bits */
00542   
00543   tmp = strtok(repair_file_buf, ":");
00544   
00545   while(tmp != NULL) {
00546           
00547     toi = (unsigned long long)atoi(tmp);
00548     tmp = strtok(NULL, "\n");
00549 
00550     if(tmp[0] == '*') {
00551 
00552       /* Check if missing object already exist */
00553       miss_obj = miss_object_exist(toi);
00554 
00555       if(miss_obj == NULL) {
00556 
00557         miss_obj = create_miss_object();
00558 
00559         if(miss_obj == NULL) {
00560           return MEM_ERROR;
00561         }
00562 
00563         miss_obj->toi = toi;
00564 
00565         insert_miss_object(miss_obj);
00566       }
00567       else {
00568         next_miss_block = miss_obj->block_list;
00569 
00570         while(next_miss_block != NULL) {
00571           miss_block = next_miss_block;
00572           next_miss_block = miss_block->next;
00573           free(miss_block);
00574         }
00575         
00576         miss_obj->block_list = NULL;
00577       }
00578 
00579     }
00580     else {
00581       
00582       sbn = atoi(tmp);
00583 
00584       /* Check if missing object already exist */
00585       miss_obj = miss_object_exist(toi);
00586     
00587       if(miss_obj == NULL) {
00588         
00589         miss_obj = create_miss_object();
00590         
00591         if(miss_obj == NULL) {
00592           return MEM_ERROR;
00593         }
00594         
00595         miss_obj->toi = toi;
00596         
00597         insert_miss_object(miss_obj);
00598       }
00599       
00600       /* Check if missing block already exist */
00601       miss_block = miss_block_exist(sbn, miss_obj);
00602       
00603       if(miss_block == NULL) {
00604         
00605         miss_block = create_miss_block();
00606         
00607         if(miss_block == NULL) {
00608           return MEM_ERROR;
00609         }
00610         
00611         miss_block->sbn = sbn;
00612         
00613         insert_miss_block(miss_block, miss_obj);
00614       }
00615     }
00616 
00617     tmp = strtok(NULL, ":");
00618   }
00619   
00620   free(repair_file_buf);
00621         
00622   if(stat(a->fdt_file, &file_stats) == -1) {
00623     printf("Error: %s is not valid file name\n", a->fdt_file);
00624     fflush(stdout);
00625     free_miss_object_list();
00626     return -1;
00627   }
00628   
00629   fdt_length = (unsigned long long)file_stats.st_size;
00630   
00631   /* If someone has modified the fdt file */
00632   
00633   if(fdt_mtime != file_stats.st_mtime) {
00634     fdt_mtime = file_stats.st_mtime;
00635   }
00636   
00637   /* Allocate memory for buf, to read fdt file to it */
00638   if(!(buf = (char*)calloc((unsigned int)(fdt_length + 1), sizeof(char)))) {
00639     printf("Could not alloc memory for fdt buffer!\n");
00640     free_miss_object_list();
00641     return -1;
00642   }
00643   
00644   if((fp = fopen(a->fdt_file, "rb")) == NULL) {
00645     printf("Error: unable to open tx_file %s\n", a->fdt_file);
00646     fflush(stdout);
00647     free(buf);
00648     free_miss_object_list();
00649     return -1;
00650   }
00651   
00652   nbytes = fread(buf, 1, (unsigned int)fdt_length, fp);
00653   
00654   if(nbytes <= 0) {
00655     printf("fread error\n");
00656     fflush(stdout);
00657     fclose(fp);
00658     free(buf);
00659     free_miss_object_list();
00660     return -1;
00661   }
00662   
00663   fdt = decode_fdt_payload(buf);
00664   free(buf);
00665   fclose(fp);
00666   
00667   if(fdt == NULL) {
00668     free_miss_object_list();
00669     return -1;
00670   }
00671   
00672   time(&systime);
00673   curr_time = systime + 2208988800U;
00674   
00675   if(fdt->expires < curr_time) {
00676     set_session_state(*s_id, SExiting);
00677     FreeFDT(fdt);
00678     free_miss_object_list();
00679     return -2;
00680   }
00681 
00682   while(a->alc_a.nb_tx) {
00683 
00684     file = fdt->file_list;
00685   
00686     while(file != NULL) { /* loop for sending all files in fdt */
00687     
00688       miss_obj = miss_object_exist(file->toi);
00689     
00690       if(miss_obj == NULL) {
00691         file = file->next;
00692         continue;
00693       }
00694       
00695       if(a->alc_a.use_fec_oti_ext_hdr == 0) {
00696         
00697         if(file->fec_enc_id == -1) {
00698           incomplete_fdt = TRUE;
00699         }
00700         else if(((file->fec_enc_id > 127) && (file->fec_inst_id == -1))) {
00701           incomplete_fdt = TRUE;
00702         }
00703         else if(file->max_sb_len == 0) {
00704           incomplete_fdt = TRUE;
00705         }
00706         else if(file->es_len == 0) {
00707           incomplete_fdt = TRUE;
00708         }
00709         else if(((file->fec_enc_id == SB_SYS_FEC_ENC_ID) &&
00710                  (file->max_nb_of_es == 0))) {
00711           incomplete_fdt = TRUE;
00712         }
00713         
00714         if(incomplete_fdt) {
00715 #ifdef _MSC_VER
00716           printf("FDT does not contain FEC-OTI information, TOI: %I64u\n", file->toi);
00717 #else
00718           printf("FDT does not contain FEC-OTI information, TOI: %llu\n", file->toi);
00719 #endif
00720           fflush(stdout);
00721           FreeFDT(fdt);
00722           free_miss_object_list();
00723           return -1;
00724         }
00725         
00726         if(file->toi == 0) {
00727           incomplete_fdt = TRUE;
00728         }
00729         else if(file->location == NULL) {
00730           incomplete_fdt = TRUE;
00731         }
00732         
00733 #ifdef USE_ZLIB
00734         if(((a->alc_a.encode_content == 2) && (file->transfer_len == 0))) {
00735           incomplete_fdt = TRUE;
00736         }
00737         else if(((a->alc_a.encode_content != 2) && (file->transfer_len == 0))) {
00738           incomplete_fdt = TRUE;
00739         }
00740 #else   
00741         if(file->content_len == 0) {
00742           incomplete_fdt = TRUE;
00743         }       
00744 #endif
00745         
00746         if(incomplete_fdt) {
00747 #ifdef _MSC_VER
00748           printf("FDT does not contain enough File information, TOI: %I64u\n", file->toi);
00749 #else
00750           printf("FDT does not contain enough File information, TOI: %llu\n", file->toi);
00751 #endif
00752           fflush(stdout);
00753           FreeFDT(fdt);
00754           free_miss_object_list();
00755           return -1;
00756         }
00757       }
00758       
00759 #ifdef _MSC_VER
00760       Sleep(500);
00761 #else
00762       usleep(500000);
00763 #endif
00764       uri = parse_uri(file->location, (int)strlen(file->location));
00765       
00766       memset(path, 0, MAX_PATH_LENGTH);
00767       
00768       if(!(strcmp(a->alc_a.base_dir, "") == 0)) {
00769         strcpy(path, a->alc_a.base_dir);
00770         strcat(path, "/");
00771       }
00772       strcat(path, uri->path);
00773       
00774 #ifdef USE_ZLIB
00775       if(a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) {
00776         strcat(path, GZ_SUFFIX);        
00777       }
00778 #endif
00779       if(file->next == NULL) {
00780         set_session_a_flag_usage(*s_id);
00781       }
00782       
00783       if(((file->es_len != 0) && (file->max_sb_len != 0))) {
00784         
00785         tmp_eslen = file->es_len;
00786         tmp_max_sblen = file->max_sb_len;
00787       }
00788       
00789       if(file->fec_enc_id != -1) {
00790         if(file->fec_enc_id != tmp_fec_enc_id) {
00791           tmp_fec_enc_id = (unsigned char)file->fec_enc_id;
00792           tmp_fec_inst_id = (unsigned short)file->fec_inst_id;
00793         }
00794       }
00795       
00796 #ifdef _MSC_VER
00797       for(j = 0; j < (int)strlen(path); j++) {
00798         if(*(path + j) == '/') {
00799           *(path + j) = '\\';
00800         }
00801       }
00802 #endif
00803       
00804       sent = send_repair_data(path, s_id, NO_TX_THREAD, file->toi, tmp_eslen, tmp_max_sblen, tmp_fec_enc_id,
00805                               tmp_fec_inst_id, miss_obj);
00806       
00807       free_uri(uri);
00808       
00809       if(sent == -1) {
00810         FreeFDT(fdt);
00811         free_miss_object_list();
00812         return -1;
00813       }
00814       else if(sent == -2) {
00815         FreeFDT(fdt);
00816         return -2;
00817       }
00818       
00819       file = file->next;
00820     
00821       /* Sleep between files so that receiver can decode them */
00822       
00823 #ifdef _MSC_VER
00824       Sleep(500);
00825 #else
00826       usleep(500000);
00827 #endif
00828     }
00829 
00830     if(!a->cont) {
00831       a->alc_a.nb_tx--;
00832     }
00833   }
00834   
00835   FreeFDT(fdt);
00836   free_miss_object_list();
00837   
00838   return 0;
00839 }
00840 
00841 int repair_sender(arguments_t *a, int *s_id) {
00842   
00843   unsigned short i;
00844   int j;
00845   int retval = 0;
00846 
00847   struct sockaddr_in ipv4;
00848   struct sockaddr_in6 ipv6;
00849  
00850   char addrs[MAX_CHANNELS_IN_SESSION][INET6_ADDRSTRLEN];        /* Mcast addresses on which to send */
00851   char ports[MAX_CHANNELS_IN_SESSION][MAX_PORT_LENGTH]; /* Local port numbers  */
00852   
00853   time_t systime;
00854   unsigned long long curr_time;
00855 
00856   char *sdp_buf = NULL;
00857   FILE *sdp_fp;
00858   struct stat sdp_file_stats;
00859   int nbytes;
00860 
00861 #ifdef _MSC_VER
00862   int addr_size;
00863 #endif
00864   
00865   if(strcmp(a->repair, "") != 0) {
00866     
00867     retval = parse_repair_sender_conf_file(a);
00868     
00869     if(retval == -1) {
00870       return -1;
00871     }
00872     
00873     retval = parse_flute_conf_file(a);
00874     
00875     if(retval == -1) {
00876       return -1;
00877     }
00878   }
00879   
00880   if(strcmp(a->sdp_file, "") != 0) {
00881     
00882     if(stat(a->sdp_file, &sdp_file_stats) == -1) {
00883       
00884       printf("Error: %s is not valid file name\n", a->sdp_file);
00885       fflush(stdout);
00886       return -1;
00887     }
00888     
00889     /* Allocate memory for buf */
00890     if(!(sdp_buf = (char*)calloc((sdp_file_stats.st_size + 1), sizeof(char)))) {
00891       printf("Could not alloc memory for sdp buffer!\n");
00892       fflush(stdout);
00893       return -1;
00894     }
00895     
00896     if((sdp_fp = fopen(a->sdp_file, "rb")) == NULL) {
00897       printf("Error: unable to open sdp_file %s\n", a->sdp_file);
00898       fflush(stdout);
00899       free(sdp_buf);
00900       return -1;
00901     }
00902     
00903     nbytes = (int)fread(sdp_buf, 1, sdp_file_stats.st_size, sdp_fp); 
00904     
00905     if(parse_sdp_file(a, addrs, ports, sdp_buf) == -1) {
00906       return -1;
00907     }
00908   }
00909     else {
00910     if(a->alc_a.addr_family == PF_INET) {
00911 
00912       for(j = 0; j < a->alc_a.nb_channel; j++) {
00913             memset(addrs[j], 0, INET6_ADDRSTRLEN);
00914             ipv4.sin_addr.s_addr = htonl(ntohl(inet_addr(a->alc_a.addr)) + j);
00915             sprintf(addrs[j], "%s", inet_ntoa(ipv4.sin_addr));
00916 
00917             memset(ports[j], 0, MAX_PORT_LENGTH);
00918             sprintf(ports[j], "%i", (atoi(a->alc_a.port) + j));
00919       }
00920     }
00921     else if(a->alc_a.addr_family == PF_INET6) {
00922 
00923 #ifdef _MSC_VER
00924           addr_size = sizeof(struct sockaddr_in6);
00925           WSAStringToAddress((char*)a->alc_a.addr, AF_INET6, NULL, (struct sockaddr*)&ipv6, &addr_size);
00926 #else 
00927           inet_pton(AF_INET6, a->alc_a.addr, &ipv6.sin6_addr);
00928 #endif
00929 
00930       for(j = 0; j < a->alc_a.nb_channel; j++) {
00931 
00932             memset(addrs[j], 0, INET6_ADDRSTRLEN);
00933         
00934 #ifdef _MSC_VER
00935             addr_size = sizeof(addrs[j]);
00936                 WSAAddressToString((struct sockaddr*)&ipv6, sizeof(struct sockaddr_in6),
00937                         NULL, addrs[j], &addr_size);
00938 #else
00939             inet_ntop(AF_INET6, &ipv6.sin6_addr, addrs[j], sizeof(addrs[j]));
00940 #endif
00941 
00942           memset(ports[j], 0, MAX_PORT_LENGTH);
00943       sprintf(ports[j], "%i", (atoi(a->alc_a.port) + j));
00944 
00945         if(j < (a->alc_a.nb_channel - 1)) {
00946           if(increase_ipv6_address(&ipv6.sin6_addr) == -1) {
00947             printf("Increasing IPv6 address %s is not possible\n", addrs[j]);
00948             return -1;
00949           }
00950         }
00951       }
00952     }
00953   }
00954   
00955   if(a->alc_a.stop_time != 0) {
00956     time(&systime);
00957     curr_time = systime + 2208988800U;
00958     
00959     if(a->alc_a.stop_time <= curr_time) {
00960       printf("Session end time reached\n");
00961       fflush(stdout);
00962       return -1;
00963     }
00964   }
00965   
00966   *s_id = open_alc_session(&a->alc_a);
00967 
00968   for(i = 0; (int)i < a->alc_a.nb_channel; i++) {
00969     
00970     retval = add_alc_channel(*s_id, ports[i], addrs[i], a->alc_a.intface, a->alc_a.intface_name);
00971     
00972     if(retval == -1) {
00973       
00974       close_alc_session(*s_id);
00975       return -1;
00976     }   
00977   }
00978     
00979   retval = repair_sender_in_fdt_based_mode(s_id, a);
00980   
00981   return retval;
00982 }
00983  
00984 int parse_repair_sender_conf_file(arguments_t *a) {
00985   
00986    char *buf = NULL;
00987    FILE *fp;
00988    struct stat file_stats;
00989    int nbytes;
00990   
00991    char *tmp = NULL;
00992   
00993    if(stat(a->repair, &file_stats) == -1) {
00994      printf("Error: %s is not valid file name\n", a->repair);
00995      fflush(stdout);
00996      return -1;
00997    }
00998   
00999    /* Allocate memory for buf */
01000    if(!(buf = (char*)calloc((file_stats.st_size + 1), sizeof(char)))) {
01001      printf("Could not alloc memory for buffer!\n");
01002      fflush(stdout);
01003      return -1;
01004    }
01005   
01006    if((fp = fopen(a->repair, "rb")) == NULL) {
01007      printf("Error: unable to open file %s\n", a->repair);
01008      fflush(stdout);
01009      free(buf);
01010      return -1;
01011    }
01012   
01013    nbytes = (int)fread(buf, 1, file_stats.st_size, fp);
01014   
01015    if(nbytes <= 0) {
01016      free(buf);
01017      fclose(fp);
01018      return -1;
01019    }
01020   
01021    fclose(fp);
01022   
01023    tmp = strtok (buf, "=");
01024   
01025    while(tmp != NULL) {
01026 
01027      if(strcmp(tmp, "RequestedBlocksFile") == 0) {
01028        tmp = strtok(NULL, "\n");
01029        memset(a->requested_blocks_file, 0, MAX_PATH_LENGTH);
01030        strcpy(a->requested_blocks_file, tmp);
01031       
01032        if(a->requested_blocks_file[strlen(a->requested_blocks_file)-1] == '\r') {
01033          a->requested_blocks_file[strlen(a->requested_blocks_file)-1] = '\0';
01034        }
01035      }
01036      else if(strcmp(tmp, "PTMRepairSDPFile") == 0) {
01037        tmp = strtok(NULL, "\n");
01038        memset(a->sdp_file, 0, MAX_PATH_LENGTH);
01039        strcpy(a->sdp_file, tmp);
01040       
01041        if(a->sdp_file[strlen(a->sdp_file)-1] == '\r') {
01042          a->sdp_file[strlen(a->sdp_file)-1] = '\0';
01043        }
01044      }
01045      else if(strcmp(tmp, "FluteConfFile") == 0) {
01046        tmp = strtok(NULL, "\n");
01047        memset(a->flute_conf_file, 0, MAX_PATH_LENGTH);
01048        strcpy(a->flute_conf_file, tmp);
01049       
01050        if(a->flute_conf_file[strlen(a->flute_conf_file)-1] == '\r') {
01051          a->flute_conf_file[strlen(a->flute_conf_file)-1] = '\0';
01052        }
01053      }
01054     
01055      tmp = strtok (NULL, "=");
01056    }
01057  
01058    free(buf);
01059   
01060    return 0;
01061  }
01062 
01063 int parse_flute_conf_file(arguments_t *a) {
01064   
01065    char *buf = NULL;
01066    FILE *fp;
01067    struct stat file_stats;
01068    int nbytes;
01069    char *tmp = NULL;
01070   
01071    if(stat(a->flute_conf_file, &file_stats) == -1) {
01072      printf("Error: %s is not valid file name\n", a->flute_conf_file);
01073      fflush(stdout);
01074      return -1;
01075    }
01076   
01077    if(!(buf = (char*)calloc((file_stats.st_size + 1), sizeof(char)))) {
01078      printf("Could not alloc memory for buffer!\n");
01079      fflush(stdout);
01080      return -1;
01081    }
01082   
01083    if((fp = fopen(a->flute_conf_file, "rb")) == NULL) {
01084      printf("Error: unable to open file %s\n", a->flute_conf_file);
01085      fflush(stdout);
01086      free(buf);
01087      return -1;
01088    }
01089   
01090    nbytes = (int)fread(buf, 1, file_stats.st_size, fp); 
01091   
01092    if(nbytes <= 0) {
01093      free(buf);
01094      fclose(fp);
01095      return -1;
01096    }
01097    fclose(fp);
01098   
01099    tmp = strtok (buf, "=");
01100   
01101    while(tmp != NULL) {
01102 
01103      if(strcmp(tmp, "BaseDir") == 0) {
01104        tmp = strtok(NULL, "\n");
01105       
01106        memset(a->alc_a.base_dir, 0, MAX_PATH_LENGTH);
01107        strcpy(a->alc_a.base_dir, tmp);
01108       
01109        if(a->alc_a.base_dir[strlen(a->alc_a.base_dir)-1] == '\r') {
01110          a->alc_a.base_dir[strlen(a->alc_a.base_dir)-1] = '\0';
01111        }
01112      }
01113      else if(strcmp(tmp, "FECRatio") == 0) {
01114        tmp = strtok(NULL, "\n");
01115        a->alc_a.fec_ratio = (unsigned short)atoi(tmp);
01116      }
01117      else if(strcmp(tmp, "FDTFile") == 0) {
01118        tmp = strtok(NULL, "\n");
01119 
01120        memset(a->fdt_file, 0, MAX_PATH_LENGTH);
01121        strcpy(a->fdt_file, tmp);
01122       
01123        if(a->fdt_file[strlen(a->fdt_file)-1] == '\r') {
01124          a->fdt_file[strlen(a->fdt_file)-1] = '\0';
01125        }
01126      }
01127     
01128      tmp = strtok (NULL, "=");
01129    }
01130   
01131    free(buf);
01132   
01133    return 0;
01134  }

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