00001 #include <sys/types.h>
00002 #include <sys/socket.h>
00003 #include <sys/stat.h>
00004 #include <sys/types.h>
00005 #include <dirent.h>
00006 #include <unistd.h>
00007 #include <netinet/in.h>
00008 #include <netdb.h>
00009 #include <stdio.h>
00010 #include <string.h>
00011 #include <stdlib.h>
00012 #include <unistd.h>
00013 #include <ctype.h>
00014
00015
00016 #include "sft.h"
00017
00018
00019 static struct sft_hdr *sft;
00020
00021
00027 int send_pkt(void){
00028 int rval, cmd;
00029 struct sft_hdr *rec;
00030 char id[3], cmd_s[3];
00031
00032
00033
00034
00035
00036 sprintf(sft->id, "%03d", id_stc);
00037
00038
00039
00040 send(s1, (struct sft_hdr *)sft, sizeof(struct sft_hdr), 0);
00041
00042
00043
00044 bzero((struct sft_hdr *)sft, sizeof(struct sft_hdr));
00045
00046
00047 rval = recv(s1, msg_rec, TAMMSG, 0) ;
00048
00049
00050 rec = (struct sft_hdr *)msg_rec;
00051
00052 sprintf(id, "%03d", id_stc);
00053
00054 fflush(stdout);
00055
00056
00057 strncpy(cmd_s, rec->cmd, 2);
00058 cmd_s[3]='\0';
00059 cmd = strtol(cmd_s, NULL, 0);
00060
00061 return cmd;
00062
00063
00064
00065
00066 }
00067
00075 int sft_send(char* data){
00076 FILE *fp;
00077 long fp_tam=0;
00078 size_t result;
00079 char* aux='\0';
00080 char* fp_buff='\0';
00081 int i=0, vlr[30000], qt_pkt=0, j=0,l=0,m=0, resp=0;
00082 struct sft_hdr s;
00083
00084
00085 data = strtok(data, "\n");
00086 fp = fopen(data, "r");
00087 if(fp == NULL){
00088 printf("\nArquivo solicitado não existe na raiz deste diretorio.");
00089 return -1;
00090 }
00091
00092
00093 fseek (fp , 0 , SEEK_END);
00094 fp_tam = ftell (fp);
00095 rewind (fp);
00096
00097
00098 fp_buff = (char*) malloc (sizeof(char)*fp_tam);
00099 if (fp_buff == NULL) {fputs ("Memory error",stderr); return -1;}
00100
00101
00102 result = fread (fp_buff,1,fp_tam,fp);
00103 if (result != fp_tam) {fputs ("Reading error",stderr); return -1;}
00104
00105 fclose (fp);
00106
00107
00108 aux = strtok(fp_buff, " ,\"\\");
00109
00110 i=0;
00111 sscanf(aux, "0x%x", &vlr[i]);
00112 while(1){
00113 i++;
00114 aux = strtok(NULL, "\\ \",\n");
00115 if(aux==NULL) break;
00116
00117 sscanf(aux, "0x%x", &vlr[i]);
00118
00119 fflush(stdout);
00120 }
00121
00122 free (fp_buff);
00123
00124
00125
00126 qt_pkt = i/1000;
00127 m = 0;
00128
00129
00130
00131 id_stc++;
00132 if(id_stc == 999) id_stc = 0;
00133
00134
00135 if (qt_pkt > 1){
00136 for(j=0;j<qt_pkt;j++){
00137
00138
00139 sprintf(s.cmd, "%02d", CMD_SEND);
00140 sprintf(s.len, "%04d", SFTD_BUFFER_DATA);
00141 sprintf(s.mf, "1");
00142 sprintf(s.nro_pkt, "%02d", j);
00143
00144
00145 for(l=0;l<SFTD_BUFFER_DATA;l++){
00146 s.buff_send[l]= vlr[m];
00147 fflush(stdout);
00148 m++;
00149 }
00150
00151
00152 sft = &s;
00153
00154
00155 resp = send_pkt();
00156
00157
00158 if(resp== CMD_ERROR){
00159 sft = &s;
00160
00161 resp = send_pkt();
00162 }
00163 if(resp== CMD_STOP) return CMD_STOP;
00164 if(resp!= CMD_SEND_WAIT) return -1;
00165
00166 }
00167 }
00168
00169
00170
00171 j = i%1000;
00172
00173 sprintf(s.cmd, "%02d", CMD_SEND);
00174 sprintf(s.len, "%04d", j);
00175 sprintf(s.mf, "0");
00176 sprintf(s.nro_pkt, "%02d", qt_pkt);
00177
00178
00179 for(l=0;l<j;l++){
00180 s.buff_send[l] = vlr[m];
00181
00182 m++;
00183 }
00184
00185
00186 sft = &s;
00187
00188 resp = send_pkt();
00189
00190 bzero(&s, sizeof(struct sft_hdr));
00191
00192
00193
00194
00195 if(resp == CMD_ERROR){
00196 sft = &s;
00197
00198 resp = send_pkt();
00199 }
00200 if(resp == CMD_STOP) return CMD_STOP;
00201 if(resp == CMD_SEND_WAIT) return CMD_SEND_WAIT;
00202 if(resp == CMD_FINISHED) return CMD_FINISHED;
00203
00204
00205
00206
00207 return 0;
00208 }
00209
00215 int send_stop(void){
00216 struct sft_hdr s;
00217 int resp;
00218
00219 sprintf(s.cmd, "%02d", CMD_STOP);
00220 sprintf(s.len, "0");
00221 sprintf(s.mf, "0");
00222 sprintf(s.nro_pkt, "01");
00223 s.buff_send[0] = '\0';
00224
00225 id_stc++;
00226 if(id_stc == 999) id_stc = 0;
00227
00228 sft = &s;
00229 sprintf(sft->id, "%03d", id_stc);
00230
00231 resp = send_pkt();
00232
00233 if(resp!=CMD_STOP) return -1;
00234
00235 return 0;
00236 }
00237
00246 int sft_send_seq(char * data){
00247 DIR *diretorio;
00248 char path[30], conf[30];
00249 FILE *fp;
00250 long fp_tam;
00251 size_t result;
00252 char* fp_buff;
00253 int qt_arq, i, tempo, tipo, resp;
00254
00255
00256 data = strtok(data, "\n");
00257 if((diretorio = opendir(data))== NULL){
00258 printf("error opening directory %s\n",data);
00259 perror("opendir");
00260 return -1;
00261 }
00262 closedir( diretorio );
00263
00264 sprintf(path, "%s/", data);
00265
00266 fflush(stdout);
00267
00268
00269 sprintf(conf, "%s/%s.conf", data, data);
00270 fp = fopen(conf,"r");
00271 if(fp == NULL){
00272 printf("\nArquivo %s solicitado não existe na raiz deste diretorio.", conf);
00273 return -1;
00274 }
00275 fseek (fp , 0 , SEEK_END);
00276 fp_tam = ftell (fp);
00277 rewind (fp);
00278 fp_buff = (char*) malloc (sizeof(char)*fp_tam);
00279 result = fread (fp_buff,1,fp_tam,fp);
00280 fclose (fp);
00281
00282
00283 tempo = atoi(strtok(fp_buff, " ,"));
00284 qt_arq = atoi(strtok(NULL, " ,"));
00285 tipo = atoi(strtok(NULL, " ,"));
00286
00287
00288 int ll;
00289 while(1){
00290 for(i=1;i<=qt_arq;i++){
00291 sprintf(path, "%s/%d", data, i);
00292
00293 resp = sft_send(path);
00294 if (resp== CMD_STOP){
00295
00296 return CMD_STOP;
00297 }
00298
00299
00300 if(tipo==1) sleep(tempo);
00301 else if(tipo==2) for(ll=0;ll<(1000000*tempo);ll++);
00302 }
00303 }
00304
00305 return 0;
00306 }
00307
00314 int sft_send_loop(char * data){
00315 int resp;
00316
00317 resp = sft_send_seq(data);
00318
00319
00320
00321 if (resp == CMD_STOP){
00322 printf("send stop\n");
00323 send_stop();
00324 }
00325 else if (resp == CMD_FINISHED){
00326 printf("Enviado com sucesso.");
00327 }
00328 else if (resp == -1 ) return -1;
00329
00330
00331 return 0;
00332 }
00333
00334
00342 int trata_sft(){
00343
00344 char* tr;
00345 char* data;
00346 int estado, j, a, resp;
00347
00348
00349 gif = 0;
00350
00351 tr = strtok(buff, " ,.");
00352
00353 data = strtok(NULL, " ,.");
00354 j = strlen(tr);
00355 for(a=0;a<j;a++){
00356 tr[a] = toupper(tr[a]);
00357 if(tr[a] == ' '){}
00358 }
00359
00360 if(!strcmp(tr, "SEND")) estado = CMD_SEND;
00361 else if(!strcmp(tr, "SENDSEQ")) estado = CMD_SEND_SEQ;
00362 else if(!strcmp(tr, "SENDX")) estado = CMD_SEND_X;
00363 else if(!strcmp(tr, "SENDU")) estado = CMD_SENDU;
00364 else return -1;
00365
00366 switch (estado){
00367 case CMD_SEND:
00368 resp = sft_send(data);
00369 if(resp ==-1) return -1;
00370 else if (resp == CMD_FINISHED){
00371 printf("Enviado com sucesso.\n");
00372 }
00373 break;
00374 case CMD_SEND_SEQ:
00375 resp = sft_send_loop(data);
00376 if(resp ==-1) return -1;
00377 else if (resp == CMD_FINISHED){
00378 printf("Enviado com sucesso.\n");
00379 }
00380 break;
00381 case CMD_SEND_X:
00382 resp = sft_send_x(data);
00383 if(resp ==-1) return -1;
00384 else if (resp == CMD_FINISHED){
00385 printf("Enviado com sucesso.\n");
00386 }
00387 break;
00388 case CMD_SENDU:
00389 resp = sft_sendu(data);
00390 if(resp ==-1) return -1;
00391 else if (resp == CMD_FINISHED){
00392 printf("Enviado com sucesso.\n");
00393 }
00394 break;
00395 default: break;
00396 }
00397
00398 memset(sft,'\0', sizeof(sft));
00399 return 0;
00400 }
00401
00402
00403
00404
00405 int sft_send_x(char* data){
00406 FILE *fp;
00407 long fp_tam;
00408 size_t result;
00409 char* aux;
00410 char* fp_buff;
00411 int i, vlr[30000], qt_pkt, j,l,m;
00412 struct sft_hdr s;
00413
00414
00415 data = strtok(data, "\n");
00416 fp = fopen(data, "r");
00417 if(fp == NULL){
00418 printf("\nArquivo solicitado não existe na raiz deste diretorio.");
00419 return -1;
00420 }
00421
00422
00423 fseek (fp , 0 , SEEK_END);
00424 fp_tam = ftell (fp);
00425 rewind (fp);
00426
00427
00428 fp_buff = (char*) malloc (sizeof(char)*fp_tam);
00429 if (fp_buff == NULL) {fputs ("Memory error",stderr); return -1;}
00430
00431
00432 result = fread (fp_buff,1,fp_tam,fp);
00433 if (result != fp_tam) {fputs ("Reading error",stderr); return -1;}
00434
00435 fclose (fp);
00437
00438
00439 aux = strtok(fp_buff, " ,\"\\");
00440
00441 i=0;
00442 sscanf(aux, "x%x", &vlr[i]);
00443 while(1){
00444 i++;
00445 aux = strtok(NULL, "\\ \",\n");
00446 if(aux==NULL) break;
00447
00448
00449 sscanf(aux, "x%x", &vlr[i]);
00450
00451 fflush(stdout);
00452 }
00453
00454
00455 qt_pkt = i/1000;
00456 m = 0;
00457
00458
00459
00460 id_stc++;
00461 if(id_stc == 999) id_stc = 0;
00462
00463
00464
00465 sprintf(s.cmd, "%02d", CMD_SEND_X);
00466
00467 if (qt_pkt > 1){
00468 for(j=0;j<qt_pkt;j++){
00469
00470 sprintf(s.len, "1000");
00471 sprintf(s.mf,"1");
00472 sprintf(s.nro_pkt, "%02d", j);
00473
00474
00475 for(l=0;l<1000;l++){
00476 s.buff_send[l]= vlr[m];
00477 fflush(stdout);
00478 m++;
00479 }
00480 s.buff_send[l]='\0';
00481
00482 sft = &s;
00483
00484
00485 if(send_pkt()!= CMD_SEND_WAIT) return -1;
00486
00487 }
00488 }
00489
00490
00491
00492 j = i%1000;
00493 sprintf(s.len, "%04d", j);
00494 sprintf(s.mf,"0");
00495 sprintf(s.nro_pkt, "%02d", qt_pkt+1);
00496
00497
00498 for(l=0;l<j;l++){
00499 s.buff_send[l] = vlr[m];
00500
00501 m++;
00502 }
00503 s.buff_send[l]='\0';
00504
00505 sft = &s;
00506
00507 if(send_pkt()!=CMD_SEND_WAIT) return -1;
00508 printf("Arquivo enviado com sucesso.\n");
00509
00510
00511
00512
00513 return 0;
00514 }
00515
00516
00525 int sft_sendu(char* data){
00526 FILE *fp;
00527 long fp_tam=0;
00528 size_t result;
00529 char* aux='\0';
00530 char* fp_buff='\0';
00531 int tst[30000];
00532 int i=0, vlr[30000], qt_pkt=0,l=0,m=0, resp=0;
00533 struct sft_hdr s;
00534
00535
00536 data = strtok(data, "\n");
00537 fp = fopen(data, "r");
00538 if(fp == NULL){
00539 printf("\nArquivo solicitado não existe na raiz deste diretorio.");
00540 return -1;
00541 }
00542
00543
00544 fseek (fp , 0 , SEEK_END);
00545 fp_tam = ftell (fp);
00546 rewind (fp);
00547
00548
00549 fp_buff = (char*) malloc (sizeof(char)*fp_tam);
00550 if (fp_buff == NULL) {fputs ("Memory error",stderr); return -1;}
00551
00552
00553 result = fread (fp_buff,1,fp_tam,fp);
00554 if (result != fp_tam) {fputs ("Reading error",stderr); return -1;}
00555
00556 fclose (fp);
00558
00559
00560 aux = strtok(fp_buff, " ,\"\\");
00561
00562 i=0;
00563 sscanf(aux, "0x%x", &vlr[i]);
00564 while(1){
00565 i++;
00566 aux = strtok(NULL, "\\ \",\n");
00567 if(aux==NULL) break;
00568
00569
00570 sscanf(aux, "0x%x", &vlr[i]);
00571 if(i>999){
00572
00573 tst[m] = vlr[i];
00574
00575
00576 m++;
00577 }
00578
00579 fflush(stdout);
00580 }
00581
00582
00583 free (fp_buff);
00584
00585
00586 id_stc++;
00587 if(id_stc == 999) id_stc = 0;
00588
00589
00590
00591 qt_pkt = i;
00592 sprintf(s.cmd, "%02d", CMD_SENDU);
00593 sprintf(s.len, "%d", SFTD_BUFFER_DATA);
00594 sprintf(s.nro_pkt, "%02d", 0);
00595 sprintf(s.mf, "1");
00596 for(l=0;l<SFTD_BUFFER_DATA;l++){
00597 s.buff_send[l]= vlr[l];
00598 fflush(stdout);
00599 }
00600
00601 sft = &s;
00602
00603
00604 resp = send_pkt();
00605
00606
00607 if(resp== CMD_ERROR){
00608 sft = &s;
00609
00610 resp = send_pkt();
00611 }
00612 if(resp== CMD_STOP) return CMD_STOP;
00613 if(resp!= CMD_SEND_WAIT) return -1;
00614
00615
00616
00617
00618 send(s1, tst, sizeof(tst), 0);
00619
00620
00621
00622
00623 return 0;
00624 }