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
00037 #define TAMMSG 1100
00038 #define SFT_PORT 45
00039 #define SFTD_BUFFER_DATA 1000
00040
00041 int s1, i, gif;
00042 struct sockaddr_in server;
00043 struct hostent *hp, *gethostbyname();
00044 int id_stc;
00045
00046 char msg_rec[TAMMSG];
00047 char msg_send[TAMMSG];
00048 char buff[TAMMSG];
00049 static struct sft_hdr *sft;
00050
00055 typedef enum {
00056 CMD_SEND =0,
00057 CMD_SEND_WAIT,
00058 CMD_STOP,
00059 CMD_SEND_SEQ,
00060 CMD_FINISHED,
00061 CMD_SEND_X,
00062 CMD_SENDU,
00063 CMD_ERROR
00064 } cmd_sft;
00065
00071 struct sft_hdr{
00072 char cmd[3];
00073 char len[5];
00074 char mf[2];
00075 char nro_pkt[3];
00076 char id[4];
00077 char buff_send[SFTD_BUFFER_DATA+1];
00078
00079 };
00080
00081
00082 int main(int argc, char *argv[]);
00083 int sft_send_x(char* data);
00084 int sft_sendu(char* data);
00085 int sft_send_seq(char * data);
00086 int sft_send_loop(char * data);
00087
00088
00094 int send_pkt(void){
00095 int rval, cmd;
00096 struct sft_hdr *rec;
00097 char id[3], cmd_s[3];
00098
00099
00100
00101
00102
00103 sprintf(sft->id, "%03d", id_stc);
00104
00105
00106
00107 send(s1, (struct sft_hdr *)sft, sizeof(struct sft_hdr), 0);
00108
00109
00110
00111 bzero((struct sft_hdr *)sft, sizeof(struct sft_hdr));
00112
00113
00114 rval = recv(s1, msg_rec, TAMMSG, 0) ;
00115
00116
00117 rec = (struct sft_hdr *)msg_rec;
00118
00119 sprintf(id, "%03d", id_stc);
00120
00121 fflush(stdout);
00122
00123
00124 strncpy(cmd_s, rec->cmd, 2);
00125 cmd_s[3]='\0';
00126 cmd = strtol(cmd_s, NULL, 0);
00127
00128 return cmd;
00129
00130
00131
00132
00133 }
00134
00142 int sft_send(char* data){
00143 FILE *fp;
00144 long fp_tam=0;
00145 size_t result;
00146 char* aux='\0';
00147 char* fp_buff='\0';
00148 int i=0, vlr[30000], qt_pkt=0, j=0,l=0,m=0, resp=0;
00149 struct sft_hdr s;
00150
00151
00152 data = strtok(data, "\n");
00153 fp = fopen(data, "r");
00154 if(fp == NULL){
00155 printf("\nArquivo solicitado não existe na raiz deste diretorio.");
00156 return -1;
00157 }
00158
00159
00160 fseek (fp , 0 , SEEK_END);
00161 fp_tam = ftell (fp);
00162 rewind (fp);
00163
00164
00165 fp_buff = (char*) malloc (sizeof(char)*fp_tam);
00166 if (fp_buff == NULL) {fputs ("Memory error",stderr); return -1;}
00167
00168
00169 result = fread (fp_buff,1,fp_tam,fp);
00170 if (result != fp_tam) {fputs ("Reading error",stderr); return -1;}
00171
00172 fclose (fp);
00173
00174
00175 aux = strtok(fp_buff, " ,\"\\");
00176
00177 i=0;
00178 sscanf(aux, "0x%x", &vlr[i]);
00179 while(1){
00180 i++;
00181 aux = strtok(NULL, "\\ \",\n");
00182 if(aux==NULL) break;
00183
00184 sscanf(aux, "0x%x", &vlr[i]);
00185
00186 fflush(stdout);
00187 }
00188
00189 free (fp_buff);
00190
00191
00192
00193 qt_pkt = i/1000;
00194 m = 0;
00195
00196
00197
00198 id_stc++;
00199 if(id_stc == 999) id_stc = 0;
00200
00201
00202 if (qt_pkt > 1){
00203 for(j=0;j<qt_pkt;j++){
00204
00205
00206 sprintf(s.cmd, "%02d", CMD_SEND);
00207 sprintf(s.len, "%04d", SFTD_BUFFER_DATA);
00208 sprintf(s.mf, "1");
00209 sprintf(s.nro_pkt, "%02d", j);
00210
00211
00212 for(l=0;l<SFTD_BUFFER_DATA;l++){
00213 s.buff_send[l]= vlr[m];
00214 fflush(stdout);
00215 m++;
00216 }
00217
00218
00219 sft = &s;
00220
00221
00222 resp = send_pkt();
00223
00224
00225 if(resp== CMD_ERROR){
00226 sft = &s;
00227
00228 resp = send_pkt();
00229 }
00230 if(resp== CMD_STOP) return CMD_STOP;
00231 if(resp!= CMD_SEND_WAIT) return -1;
00232
00233 }
00234 }
00235
00236
00237
00238 j = i%1000;
00239
00240 sprintf(s.cmd, "%02d", CMD_SEND);
00241 sprintf(s.len, "%04d", j);
00242 sprintf(s.mf, "0");
00243 sprintf(s.nro_pkt, "%02d", qt_pkt);
00244
00245
00246 for(l=0;l<j;l++){
00247 s.buff_send[l] = vlr[m];
00248
00249 m++;
00250 }
00251
00252
00253 sft = &s;
00254
00255 resp = send_pkt();
00256
00257 bzero(&s, sizeof(struct sft_hdr));
00258
00259
00260
00261
00262 if(resp == CMD_ERROR){
00263 sft = &s;
00264
00265 resp = send_pkt();
00266 }
00267 if(resp == CMD_STOP) return CMD_STOP;
00268 if(resp == CMD_SEND_WAIT) return CMD_SEND_WAIT;
00269 if(resp == CMD_FINISHED) return CMD_FINISHED;
00270
00271
00272
00273
00274 return 0;
00275 }
00276
00282 int send_stop(void){
00283 struct sft_hdr s;
00284 int resp;
00285
00286 sprintf(s.cmd, "%02d", CMD_STOP);
00287 sprintf(s.len, "0");
00288 sprintf(s.mf, "0");
00289 sprintf(s.nro_pkt, "01");
00290 s.buff_send[0] = '\0';
00291
00292 id_stc++;
00293 if(id_stc == 999) id_stc = 0;
00294
00295 sft = &s;
00296 sprintf(sft->id, "%03d", id_stc);
00297
00298 resp = send_pkt();
00299
00300 if(resp!=CMD_STOP) return -1;
00301
00302 return 0;
00303 }
00304
00320 int sft_send_seq(char * data){
00321 DIR *diretorio;
00322 char path[30], conf[30];
00323 FILE *fp;
00324 long fp_tam;
00325 size_t result;
00326 char* fp_buff;
00327 int qt_arq, i, tempo, tipo, resp;
00328
00329
00330 data = strtok(data, "\n");
00331 if((diretorio = opendir(data))== NULL){
00332 printf("error opening directory %s\n",data);
00333 perror("opendir");
00334 return -1;
00335 }
00336 closedir( diretorio );
00337
00338 sprintf(path, "%s/", data);
00339
00340 fflush(stdout);
00341
00342
00343 sprintf(conf, "%s/%s.conf", data, data);
00344 fp = fopen(conf,"r");
00345 if(fp == NULL){
00346 printf("\nArquivo %s solicitado não existe na raiz deste diretorio.", conf);
00347 return -1;
00348 }
00349 fseek (fp , 0 , SEEK_END);
00350 fp_tam = ftell (fp);
00351 rewind (fp);
00352 fp_buff = (char*) malloc (sizeof(char)*fp_tam);
00353 result = fread (fp_buff,1,fp_tam,fp);
00354 fclose (fp);
00355
00356
00357 tempo = atoi(strtok(fp_buff, " ,"));
00358 qt_arq = atoi(strtok(NULL, " ,"));
00359 tipo = atoi(strtok(NULL, " ,"));
00360
00361
00362 int ll;
00363 while(1){
00364 for(i=1;i<=qt_arq;i++){
00365 sprintf(path, "%s/%d", data, i);
00366
00367 resp = sft_send(path);
00368 if (resp== CMD_STOP){
00369
00370 return CMD_STOP;
00371 }
00372
00373
00374 if(tipo==1) sleep(tempo);
00375 else if(tipo==2) for(ll=0;ll<(1000000*tempo);ll++);
00376 }
00377 }
00378
00379 return 0;
00380 }
00381
00388 int sft_send_loop(char * data){
00389 int resp;
00390
00391 resp = sft_send_seq(data);
00392
00393
00394
00395 if (resp == CMD_STOP){
00396 printf("send stop\n");
00397 send_stop();
00398 }
00399 else if (resp == CMD_FINISHED){
00400 printf("Enviado com sucesso.");
00401 }
00402 else if (resp == -1 ) return -1;
00403
00404
00405 return 0;
00406 }
00407
00408
00416 int trata_sft(){
00417
00418 char* tr;
00419 char* data;
00420 int estado, j, a, resp;
00421
00422
00423 gif = 0;
00424
00425 tr = strtok(buff, " ,.");
00426
00427 data = strtok(NULL, " ,.");
00428 j = strlen(tr);
00429 for(a=0;a<j;a++){
00430 tr[a] = toupper(tr[a]);
00431 if(tr[a] == ' '){}
00432 }
00433
00434 if(!strcmp(tr, "SEND")) estado = CMD_SEND;
00435 else if(!strcmp(tr, "SENDSEQ")) estado = CMD_SEND_SEQ;
00436 else if(!strcmp(tr, "SENDX")) estado = CMD_SEND_X;
00437 else if(!strcmp(tr, "SENDU")) estado = CMD_SENDU;
00438 else return -1;
00439
00440 switch (estado){
00441 case CMD_SEND:
00442 resp = sft_send(data);
00443 if(resp ==-1) return -1;
00444 else if (resp == CMD_FINISHED){
00445 printf("Enviado com sucesso.\n");
00446 }
00447 break;
00448 case CMD_SEND_SEQ:
00449 resp = sft_send_loop(data);
00450 if(resp ==-1) return -1;
00451 else if (resp == CMD_FINISHED){
00452 printf("Enviado com sucesso.\n");
00453 }
00454 break;
00455 case CMD_SEND_X:
00456 resp = sft_send_x(data);
00457 if(resp ==-1) return -1;
00458 else if (resp == CMD_FINISHED){
00459 printf("Enviado com sucesso.\n");
00460 }
00461 break;
00462 case CMD_SENDU:
00463 resp = sft_sendu(data);
00464 if(resp ==-1) return -1;
00465 else if (resp == CMD_FINISHED){
00466 printf("Enviado com sucesso.\n");
00467 }
00468 break;
00469 default: break;
00470 }
00471
00472 memset(sft,'\0', sizeof(sft));
00473 return 0;
00474 }
00475
00476
00484 int main(int argc, char *argv[])
00485 {
00486
00487 if (argc < 2) {
00488 printf("Erro uso correto:\n") ;
00489 printf("\n %s <hostname> \n\n", argv[0]) ;
00490 exit(1) ;
00491 }
00492
00494 s1=socket(AF_INET, SOCK_STREAM, 0);
00495
00496 if (s1<0) {
00497 perror("opening stream socket");
00498 exit(1);
00499 }
00500
00501 hp = gethostbyname(argv[1]);
00502 if (hp == 0) {
00503 fprintf(stderr,"%s: Unkown host\n",argv[1]);
00504 exit(2);
00505 }
00506
00507 server.sin_family = AF_INET;
00508 server.sin_port = htons(SFT_PORT);
00509 server.sin_addr = *((struct in_addr *)hp->h_addr);
00510 memset(&(server.sin_zero), '\0', 8);
00511
00512
00513 if (connect(s1, (struct sockaddr *)&server, sizeof server ) < 0) {
00514 perror("connectando stream socket");
00515 exit(1);
00516 }
00517
00518 printf("\nSFT Conectado com sucesso...\n") ;
00519 id_stc = 0;
00520
00521 do {
00522 printf("\nSFT>") ;
00523 fgets(buff,TAMMSG,stdin);
00524 if(strncmp(buff, "quit",4)==0) break;
00525
00526 if(trata_sft()== -1){
00527 printf("Comando nao existe\n");
00528 continue;
00529 }
00530
00531
00532
00533 } while (msg_send[0] != '.');
00534
00535 close(s1);
00536 exit(0);
00537
00538 }
00539
00540
00541
00542 int sft_send_x(char* data){
00543 FILE *fp;
00544 long fp_tam;
00545 size_t result;
00546 char* aux;
00547 char* fp_buff;
00548 int i, vlr[30000], qt_pkt, j,l,m;
00549 struct sft_hdr s;
00550
00551
00552 data = strtok(data, "\n");
00553 fp = fopen(data, "r");
00554 if(fp == NULL){
00555 printf("\nArquivo solicitado não existe na raiz deste diretorio.");
00556 return -1;
00557 }
00558
00559
00560 fseek (fp , 0 , SEEK_END);
00561 fp_tam = ftell (fp);
00562 rewind (fp);
00563
00564
00565 fp_buff = (char*) malloc (sizeof(char)*fp_tam);
00566 if (fp_buff == NULL) {fputs ("Memory error",stderr); return -1;}
00567
00568
00569 result = fread (fp_buff,1,fp_tam,fp);
00570 if (result != fp_tam) {fputs ("Reading error",stderr); return -1;}
00571
00572 fclose (fp);
00574
00575
00576 aux = strtok(fp_buff, " ,\"\\");
00577
00578 i=0;
00579 sscanf(aux, "x%x", &vlr[i]);
00580 while(1){
00581 i++;
00582 aux = strtok(NULL, "\\ \",\n");
00583 if(aux==NULL) break;
00584
00585
00586 sscanf(aux, "x%x", &vlr[i]);
00587
00588 fflush(stdout);
00589 }
00590
00591
00592 qt_pkt = i/1000;
00593 m = 0;
00594
00595
00596
00597 id_stc++;
00598 if(id_stc == 999) id_stc = 0;
00599
00600
00601
00602 sprintf(s.cmd, "%02d", CMD_SEND_X);
00603
00604 if (qt_pkt > 1){
00605 for(j=0;j<qt_pkt;j++){
00606
00607 sprintf(s.len, "1000");
00608 sprintf(s.mf,"1");
00609 sprintf(s.nro_pkt, "%02d", j);
00610
00611
00612 for(l=0;l<1000;l++){
00613 s.buff_send[l]= vlr[m];
00614 fflush(stdout);
00615 m++;
00616 }
00617 s.buff_send[l]='\0';
00618
00619 sft = &s;
00620
00621
00622 if(send_pkt()!= CMD_SEND_WAIT) return -1;
00623
00624 }
00625 }
00626
00627
00628
00629 j = i%1000;
00630 sprintf(s.len, "%04d", j);
00631 sprintf(s.mf,"0");
00632 sprintf(s.nro_pkt, "%02d", qt_pkt+1);
00633
00634
00635 for(l=0;l<j;l++){
00636 s.buff_send[l] = vlr[m];
00637
00638 m++;
00639 }
00640 s.buff_send[l]='\0';
00641
00642 sft = &s;
00643
00644 if(send_pkt()!=CMD_SEND_WAIT) return -1;
00645 printf("Arquivo enviado com sucesso.\n");
00646
00647
00648
00649
00650 return 0;
00651 }
00652
00653
00662 int sft_sendu(char* data){
00663 FILE *fp;
00664 long fp_tam=0;
00665 size_t result;
00666 char* aux='\0';
00667 char* fp_buff='\0';
00668 int tst[30000];
00669 int i=0, vlr[30000], qt_pkt=0,l=0,m=0, resp=0;
00670 struct sft_hdr s;
00671
00672
00673 data = strtok(data, "\n");
00674 fp = fopen(data, "r");
00675 if(fp == NULL){
00676 printf("\nArquivo solicitado não existe na raiz deste diretorio.");
00677 return -1;
00678 }
00679
00680
00681 fseek (fp , 0 , SEEK_END);
00682 fp_tam = ftell (fp);
00683 rewind (fp);
00684
00685
00686 fp_buff = (char*) malloc (sizeof(char)*fp_tam);
00687 if (fp_buff == NULL) {fputs ("Memory error",stderr); return -1;}
00688
00689
00690 result = fread (fp_buff,1,fp_tam,fp);
00691 if (result != fp_tam) {fputs ("Reading error",stderr); return -1;}
00692
00693 fclose (fp);
00695
00696
00697 aux = strtok(fp_buff, " ,\"\\");
00698
00699 i=0;
00700 sscanf(aux, "0x%x", &vlr[i]);
00701 while(1){
00702 i++;
00703 aux = strtok(NULL, "\\ \",\n");
00704 if(aux==NULL) break;
00705
00706
00707 sscanf(aux, "0x%x", &vlr[i]);
00708 if(i>999){
00709
00710 tst[m] = vlr[i];
00711
00712
00713 m++;
00714 }
00715
00716 fflush(stdout);
00717 }
00718
00719
00720 free (fp_buff);
00721
00722
00723 id_stc++;
00724 if(id_stc == 999) id_stc = 0;
00725
00726
00727
00728 qt_pkt = i;
00729 sprintf(s.cmd, "%02d", CMD_SENDU);
00730 sprintf(s.len, "%d", SFTD_BUFFER_DATA);
00731 sprintf(s.nro_pkt, "%02d", 0);
00732 sprintf(s.mf, "1");
00733 for(l=0;l<SFTD_BUFFER_DATA;l++){
00734 s.buff_send[l]= vlr[l];
00735 fflush(stdout);
00736 }
00737
00738 sft = &s;
00739
00740
00741 resp = send_pkt();
00742
00743
00744 if(resp== CMD_ERROR){
00745 sft = &s;
00746
00747 resp = send_pkt();
00748 }
00749 if(resp== CMD_STOP) return CMD_STOP;
00750 if(resp!= CMD_SEND_WAIT) return -1;
00751
00752
00753
00754
00755 send(s1, tst, sizeof(tst), 0);
00756
00757
00758
00759
00760 return 0;
00761 }
00762
00763
00764