00001
00021 #include <sys/types.h>
00022 #include <sys/socket.h>
00023 #include <sys/stat.h>
00024 #include <sys/types.h>
00025 #include <dirent.h>
00026 #include <unistd.h>
00027 #include <netinet/in.h>
00028 #include <netdb.h>
00029 #include <stdio.h>
00030 #include <string.h>
00031 #include <stdlib.h>
00032 #include <unistd.h>
00033 #include <ctype.h>
00034
00035
00036 #define TAMMSG 1100
00037 #define SFT_PORT 45
00038 #define SFTD_BUFFER_DATA 1000
00039
00040 int s1, i, gif;
00041 struct sockaddr_in server;
00042 struct hostent *hp, *gethostbyname();
00043 int id_stc;
00044
00045 char msg_rec[TAMMSG];
00046 char msg_send[TAMMSG];
00047 char buff[TAMMSG];
00048 struct sft_hdr *sft;
00049
00054 typedef enum {
00055 CMD_SEND =0,
00056 CMD_SEND_WAIT,
00057 CMD_STOP,
00058 CMD_SEND_SEQ,
00059 CMD_FINISHED,
00060 CMD_SEND_X,
00061 CMD_SENDU,
00062 CMD_ERROR
00063 } cmd_sft;
00064
00068 struct sft_hdr{
00069 char cmd[2];
00070 char len[4];
00071 char mf;
00072 char nro_pkt[2];
00073 char buff_send[SFTD_BUFFER_DATA];
00074
00075 };
00076
00077
00078 int main(int argc, char *argv[]);
00079 int sft_send_x(char* data);
00080 int sft_sendu(char* data);
00081 int sft_send_seq(char * data);
00082 int sft_send_loop(char * data);
00083
00084
00090 int send_pkt(void){
00091 int rval;
00092 struct sft_hdr *rec;
00093 char id[3];
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 send(s1, (struct sft_hdr *)sft, sizeof(struct sft_hdr), 0);
00104
00105
00106 bzero((struct sft_hdr *)sft, sizeof(struct sft_hdr));
00107
00108
00109 rval = recv(s1, msg_rec, TAMMSG, 0) ;
00110
00111
00112 rec = (struct sft_hdr *)msg_rec;
00113
00114 sprintf(id, "%03d", id_stc);
00115
00116 fflush(stdout);
00117
00118 if (!strncmp(rec->cmd, "01",2)) return CMD_SEND_WAIT;
00119 if (!strncmp(rec->cmd, "02",2)) return CMD_STOP;
00120
00121 return -1;
00122 }
00123
00131 int sft_send(char* data){
00132 FILE *fp;
00133 long fp_tam=0;
00134 size_t result;
00135 char* aux='\0';
00136 char* fp_buff='\0';
00137 int i=0, vlr[30000], qt_pkt=0, j=0,l=0,m=0, resp=0;
00138 struct sft_hdr s;
00139
00140
00141 data = strtok(data, "\n");
00142 fp = fopen(data, "r");
00143 if(fp == NULL){
00144 printf("\nArquivo solicitado não existe na raiz deste diretorio.");
00145 return -1;
00146 }
00147
00148
00149 fseek (fp , 0 , SEEK_END);
00150 fp_tam = ftell (fp);
00151 rewind (fp);
00152
00153
00154 fp_buff = (char*) malloc (sizeof(char)*fp_tam);
00155 if (fp_buff == NULL) {fputs ("Memory error",stderr); return -1;}
00156
00157
00158 result = fread (fp_buff,1,fp_tam,fp);
00159 if (result != fp_tam) {fputs ("Reading error",stderr); return -1;}
00160
00161 fclose (fp);
00162
00163
00164 aux = strtok(fp_buff, " ,\"\\");
00165
00166 i=0;
00167 sscanf(aux, "0x%x", &vlr[i]);
00168 while(1){
00169 i++;
00170 aux = strtok(NULL, "\\ \",\n");
00171 if(aux==NULL) break;
00172
00173 sscanf(aux, "0x%x", &vlr[i]);
00174
00175 fflush(stdout);
00176 }
00177
00178 free (fp_buff);
00179
00180
00181
00182 qt_pkt = i/1000;
00183 m = 0;
00184
00185
00186
00187 id_stc++;
00188 if(id_stc == 999) id_stc = 0;
00189
00190
00191 if (qt_pkt > 1){
00192 for(j=0;j<qt_pkt;j++){
00193
00194
00195 sprintf(s.cmd, "%02d", CMD_SEND);
00196 sprintf(s.len, "%d", SFTD_BUFFER_DATA);
00197 s.mf = '1';
00198 sprintf(s.nro_pkt, "%02d", j);
00199
00200
00201 for(l=0;l<SFTD_BUFFER_DATA;l++){
00202 s.buff_send[l]= vlr[m];
00203 fflush(stdout);
00204 m++;
00205 }
00206
00207 sft = &s;
00208
00209
00210 resp = send_pkt();
00211
00212
00213 if(resp== CMD_ERROR){
00214 sft = &s;
00215
00216 resp = send_pkt();
00217 }
00218 if(resp== CMD_STOP) return CMD_STOP;
00219 if(resp!= CMD_SEND_WAIT) return -1;
00220
00221 }
00222 }
00223
00224
00225
00226 j = i%1000;
00227
00228 sprintf(s.cmd, "%02d", CMD_SEND);
00229 sprintf(s.len, "%04d", j);
00230 s.mf = '0';
00231 sprintf(s.nro_pkt, "%02d", qt_pkt);
00232
00233
00234 for(l=0;l<j;l++){
00235 s.buff_send[l] = vlr[m];
00236
00237 m++;
00238 }
00239 sft = &s;
00240
00241 resp = send_pkt();
00242
00243 bzero(&s, sizeof(struct sft_hdr));
00244
00245
00246
00247
00248 if(resp== CMD_ERROR){
00249 sft = &s;
00250
00251 resp = send_pkt();
00252 }
00253 if(resp== CMD_STOP) return CMD_STOP;
00254 if(resp== CMD_SEND_WAIT) return CMD_SEND_WAIT;
00255
00256
00257
00258
00259
00260 return 0;
00261 }
00262
00268 int send_stop(void){
00269 struct sft_hdr s;
00270 sprintf(s.cmd, "%02d", CMD_STOP);
00271 sprintf(s.len, "0");
00272 s.mf = '0';
00273 sprintf(s.nro_pkt, "01");
00274 s.buff_send[0] = '0';
00275
00276 id_stc++;
00277 if(id_stc == 999) id_stc = 0;
00278
00279 sft = &s;
00280
00281 send(s1, (struct sft_hdr *)sft, sizeof(struct sft_hdr), 0);
00282
00283
00284
00285
00286 return 0;
00287 }
00288
00304 int sft_send_seq(char * data){
00305 DIR *diretorio;
00306 char path[30], conf[30];
00307 FILE *fp;
00308 long fp_tam;
00309 size_t result;
00310 char* fp_buff;
00311 int qt_arq, i, tempo, tipo;
00312
00313
00314 data = strtok(data, "\n");
00315 if((diretorio = opendir(data))== NULL){
00316 printf("error opening directory %s\n",data);
00317 perror("opendir");
00318 return -1;
00319 }
00320 closedir( diretorio );
00321
00322 sprintf(path, "%s/", data);
00323
00324 fflush(stdout);
00325
00326
00327 sprintf(conf, "%s/%s.conf", data, data);
00328 fp = fopen(conf,"r");
00329 if(fp == NULL){
00330 printf("\nArquivo %s solicitado não existe na raiz deste diretorio.", conf);
00331 return -1;
00332 }
00333 fseek (fp , 0 , SEEK_END);
00334 fp_tam = ftell (fp);
00335 rewind (fp);
00336 fp_buff = (char*) malloc (sizeof(char)*fp_tam);
00337 result = fread (fp_buff,1,fp_tam,fp);
00338 fclose (fp);
00339
00340
00341 tempo = atoi(strtok(fp_buff, " ,"));
00342 qt_arq = atoi(strtok(NULL, " ,"));
00343 tipo = atoi(strtok(NULL, " ,"));
00344
00345
00346 int ll;
00347 while(1){
00348 for(i=1;i<=qt_arq;i++){
00349 sprintf(path, "%s/%d", data, i);
00350
00351 if (sft_send(path)== CMD_STOP){
00352
00353 return CMD_STOP;
00354 }
00355
00356
00357 if(tipo==1) sleep(tempo);
00358 else if(tipo==2) for(ll=0;ll<(1000000*tempo);ll++);
00359 }
00360 }
00361
00362 return 0;
00363 }
00364
00371 int sft_send_loop(char * data){
00372 int resp;
00373
00374 resp = sft_send_seq(data);
00375
00376
00377
00378 if ( resp == CMD_STOP){
00379 printf("send stop\n");
00380 send_stop();
00381
00382 }
00383 else if (resp == -1 ) return -1;
00384
00385
00386 return 0;
00387 }
00388
00389
00397 int trata_sft(){
00398
00399 char* tr;
00400 char* data;
00401 int estado, j, a;
00402
00403
00404 gif = 0;
00405
00406 tr = strtok(buff, " ,.");
00407
00408 data = strtok(NULL, " ,.");
00409 j = strlen(tr);
00410 for(a=0;a<j;a++){
00411 tr[a] = toupper(tr[a]);
00412 if(tr[a] == ' '){}
00413 }
00414
00415 if(!strcmp(tr, "SEND")) estado = CMD_SEND;
00416 else if(!strcmp(tr, "SENDSEQ")) estado = CMD_SEND_SEQ;
00417 else if(!strcmp(tr, "SENDX")) estado = CMD_SEND_X;
00418 else if(!strcmp(tr, "SENDU")) estado = CMD_SENDU;
00419 else return -1;
00420
00421 switch (estado){
00422 case CMD_SEND:
00423 if(sft_send(data)==-1) return -1;
00424 break;
00425 case CMD_SEND_SEQ:
00426 if(sft_send_loop(data)==-1) return -1;
00427 break;
00428 case CMD_SEND_X:
00429 if(sft_send_x(data)==-1) return -1;
00430 break;
00431 case CMD_SENDU:
00432 if(sft_sendu(data)==-1) return -1;
00433 break;
00434 default: break;
00435 }
00436
00437 memset(sft,'\0', sizeof(sft));
00438 return 0;
00439 }
00440
00441
00449 int main(int argc, char *argv[])
00450 {
00451
00452 if (argc < 2) {
00453 printf("Erro uso correto:\n") ;
00454 printf("\n %s <hostname> \n\n", argv[0]) ;
00455 exit(1) ;
00456 }
00457
00459 s1=socket(AF_INET, SOCK_STREAM, 0);
00460
00461 if (s1<0) {
00462 perror("opening stream socket");
00463 exit(1);
00464 }
00465
00466 hp = gethostbyname(argv[1]);
00467 if (hp == 0) {
00468 fprintf(stderr,"%s: Unkown host\n",argv[1]);
00469 exit(2);
00470 }
00471
00472 server.sin_family = AF_INET;
00473 server.sin_port = htons(SFT_PORT);
00474 server.sin_addr = *((struct in_addr *)hp->h_addr);
00475 memset(&(server.sin_zero), '\0', 8);
00476
00477
00478 if (connect(s1, (struct sockaddr *)&server, sizeof server ) < 0) {
00479 perror("connectando stream socket");
00480 exit(1);
00481 }
00482
00483 printf("\nSFT Conectado com sucesso...\n") ;
00484 id_stc = 0;
00485
00486 do {
00487 printf("\nSFT>") ;
00488 fgets(buff,TAMMSG,stdin);
00489 if(strncmp(buff, "quit",4)==0) break;
00490
00491 if(trata_sft()== -1){
00492 printf("Comando nao existe\n");
00493 continue;
00494 }
00495
00496
00497
00498 } while (msg_send[0] != '.');
00499
00500 close(s1);
00501 exit(0);
00502
00503 }
00504
00505
00506
00507 int sft_send_x(char* data){
00508 FILE *fp;
00509 long fp_tam;
00510 size_t result;
00511 char* aux;
00512 char* fp_buff;
00513 int i, vlr[30000], qt_pkt, j,l,m;
00514 struct sft_hdr s;
00515
00516
00517 data = strtok(data, "\n");
00518 fp = fopen(data, "r");
00519 if(fp == NULL){
00520 printf("\nArquivo solicitado não existe na raiz deste diretorio.");
00521 return -1;
00522 }
00523
00524
00525 fseek (fp , 0 , SEEK_END);
00526 fp_tam = ftell (fp);
00527 rewind (fp);
00528
00529
00530 fp_buff = (char*) malloc (sizeof(char)*fp_tam);
00531 if (fp_buff == NULL) {fputs ("Memory error",stderr); return -1;}
00532
00533
00534 result = fread (fp_buff,1,fp_tam,fp);
00535 if (result != fp_tam) {fputs ("Reading error",stderr); return -1;}
00536
00537 fclose (fp);
00539
00540
00541 aux = strtok(fp_buff, " ,\"\\");
00542
00543 i=0;
00544 sscanf(aux, "x%x", &vlr[i]);
00545 while(1){
00546 i++;
00547 aux = strtok(NULL, "\\ \",\n");
00548 if(aux==NULL) break;
00549
00550
00551 sscanf(aux, "x%x", &vlr[i]);
00552
00553 fflush(stdout);
00554 }
00555
00556
00557 qt_pkt = i/1000;
00558 m = 0;
00559
00560
00561
00562 id_stc++;
00563 if(id_stc == 999) id_stc = 0;
00564
00565
00566
00567 sprintf(s.cmd, "%02d", CMD_SEND_X);
00568
00569 if (qt_pkt > 1){
00570 for(j=0;j<qt_pkt;j++){
00571
00572 sprintf(s.len, "1000");
00573 s.mf = '1';
00574 sprintf(s.nro_pkt, "%02d", j);
00575
00576
00577 for(l=0;l<1000;l++){
00578 s.buff_send[l]= vlr[m];
00579 fflush(stdout);
00580 m++;
00581 }
00582
00583 sft = &s;
00584
00585
00586 if(send_pkt()!= CMD_SEND_WAIT) return -1;
00587
00588 }
00589 }
00590
00591
00592
00593 j = i%1000;
00594 sprintf(s.len, "%04d", j);
00595 s.mf = '0';
00596 sprintf(s.nro_pkt, "%02d", qt_pkt+1);
00597
00598
00599 for(l=0;l<j;l++){
00600 s.buff_send[l] = vlr[m];
00601
00602 m++;
00603 }
00604 sft = &s;
00605
00606 if(send_pkt()!=CMD_SEND_WAIT) return -1;
00607 printf("Arquivo enviado com sucesso.\n");
00608
00609
00610
00611
00612 return 0;
00613 }
00614
00615
00624 int sft_sendu(char* data){
00625 FILE *fp;
00626 long fp_tam=0;
00627 size_t result;
00628 char* aux='\0';
00629 char* fp_buff='\0';
00630 int tst[30000];
00631 int i=0, vlr[30000], qt_pkt=0,l=0,m=0, resp=0;
00632 struct sft_hdr s;
00633
00634
00635 data = strtok(data, "\n");
00636 fp = fopen(data, "r");
00637 if(fp == NULL){
00638 printf("\nArquivo solicitado não existe na raiz deste diretorio.");
00639 return -1;
00640 }
00641
00642
00643 fseek (fp , 0 , SEEK_END);
00644 fp_tam = ftell (fp);
00645 rewind (fp);
00646
00647
00648 fp_buff = (char*) malloc (sizeof(char)*fp_tam);
00649 if (fp_buff == NULL) {fputs ("Memory error",stderr); return -1;}
00650
00651
00652 result = fread (fp_buff,1,fp_tam,fp);
00653 if (result != fp_tam) {fputs ("Reading error",stderr); return -1;}
00654
00655 fclose (fp);
00657
00658
00659 aux = strtok(fp_buff, " ,\"\\");
00660
00661 i=0;
00662 sscanf(aux, "0x%x", &vlr[i]);
00663 while(1){
00664 i++;
00665 aux = strtok(NULL, "\\ \",\n");
00666 if(aux==NULL) break;
00667
00668
00669 sscanf(aux, "0x%x", &vlr[i]);
00670 if(i>999){
00671
00672 tst[m] = vlr[i];
00673
00674
00675 m++;
00676 }
00677
00678 fflush(stdout);
00679 }
00680
00681
00682 free (fp_buff);
00683
00684
00685 id_stc++;
00686 if(id_stc == 999) id_stc = 0;
00687
00688
00689
00690 qt_pkt = i;
00691 sprintf(s.cmd, "%02d", CMD_SENDU);
00692 sprintf(s.len, "%d", SFTD_BUFFER_DATA);
00693 sprintf(s.nro_pkt, "%02d", 0);
00694 s.mf = '1';
00695 for(l=0;l<SFTD_BUFFER_DATA;l++){
00696 s.buff_send[l]= vlr[l];
00697 fflush(stdout);
00698 }
00699
00700 sft = &s;
00701
00702
00703 resp = send_pkt();
00704
00705
00706 if(resp== CMD_ERROR){
00707 sft = &s;
00708
00709 resp = send_pkt();
00710 }
00711 if(resp== CMD_STOP) return CMD_STOP;
00712 if(resp!= CMD_SEND_WAIT) return -1;
00713
00714
00715
00716
00717 send(s1, tst, sizeof(tst), 0);
00718
00719
00720
00721
00722 return 0;
00723 }
00724
00725
00726