2020-03-11 21:01:54 +00:00
|
|
|
//////////////////////////////////////////////////
|
|
|
|
// Bakalarska praca //
|
|
|
|
// Meno studenta: Tomas Lukac //
|
|
|
|
// Veduci BP: prof. Ing. Milos Drutarovsky CSc. //
|
|
|
|
// Skola: KEMT FEI TUKE //
|
2020-04-07 20:11:21 +00:00
|
|
|
// Datum poslednej upravy: 6.4.2020 //
|
2020-03-11 21:01:54 +00:00
|
|
|
//////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "komunikacia.h"
|
|
|
|
#include "kryptografia.h"
|
2020-04-07 20:11:21 +00:00
|
|
|
#include "rs232.h"
|
2020-03-11 21:01:54 +00:00
|
|
|
|
2020-04-10 17:04:14 +00:00
|
|
|
int poslat_subor(WOLFSSL* ssl, WOLFSSL_CTX* ctx, const char* cesta, nastavenia_aplikacie *nastavenia)
|
2020-03-11 21:01:54 +00:00
|
|
|
{
|
2020-03-12 16:22:08 +00:00
|
|
|
int uspech;
|
|
|
|
|
|
|
|
//odoslanie nazvu (resp. cesty) suboru
|
|
|
|
uspech = wolfSSL_write(ssl, cesta, VELKOST_CESTY);
|
|
|
|
if(uspech <= 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nastala chyba pri posielani dat o subore.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-03-11 21:01:54 +00:00
|
|
|
|
|
|
|
FILE* subor = fopen(cesta, "r");
|
|
|
|
if(subor == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nebolo mozne najst pozadovany subor.\n");
|
|
|
|
ukoncit_spojenie(ssl, ctx);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Posielam subor.\n");
|
|
|
|
|
|
|
|
//nastavi ukazovatel na koniec suboru
|
|
|
|
//a zisti velkost suboru
|
|
|
|
fseek(subor, 0, SEEK_END);
|
|
|
|
long velkost = ftell(subor);
|
2020-03-12 16:22:08 +00:00
|
|
|
char velkost_suboru[VELKOST_SUBOR];
|
2020-03-11 21:01:54 +00:00
|
|
|
sprintf(velkost_suboru, "%ld", velkost);
|
|
|
|
printf("Velkost suboru: %s bajtov\n", velkost_suboru);
|
2020-03-12 16:22:08 +00:00
|
|
|
wolfSSL_write(ssl, velkost_suboru, VELKOST_SUBOR);
|
2020-03-11 21:01:54 +00:00
|
|
|
|
|
|
|
//nastavi ukazovatel na zaciatok suboru
|
|
|
|
//a nacita data zo suboru do pola
|
|
|
|
char* pole = malloc(velkost);
|
|
|
|
char* pole_uk = pole;
|
|
|
|
fseek(subor, 0, SEEK_SET);
|
|
|
|
fread(pole, 1, velkost, subor);
|
|
|
|
fclose(subor);
|
|
|
|
|
|
|
|
//posielanie jednotlivych bajtov
|
|
|
|
for(int i = 0; i < velkost + 1; ++i)
|
|
|
|
{
|
2020-03-13 09:25:19 +00:00
|
|
|
uspech = wolfSSL_write(ssl, pole_uk, velkost);
|
2020-03-12 16:22:08 +00:00
|
|
|
if(uspech <= 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nastala chyba pri posielani suboru.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-03-11 21:01:54 +00:00
|
|
|
pole_uk += 1;
|
|
|
|
}
|
|
|
|
printf("Subor bol uspesne odoslany.\n");
|
2020-03-19 12:55:34 +00:00
|
|
|
|
|
|
|
//generovanie a poslanie kontrolneho suctu serveru pre kontrolu
|
|
|
|
byte* kontrolny_sucet;
|
|
|
|
int velkost_kontrolneho_suctu;
|
2020-04-07 20:11:21 +00:00
|
|
|
kontrolny_sucet = generovat_kontrolny_sucet_suboru(nastavenia->funkcia, cesta, &velkost_kontrolneho_suctu);
|
2020-03-19 12:55:34 +00:00
|
|
|
for(int i = 0; i < velkost_kontrolneho_suctu; i++)
|
|
|
|
{
|
|
|
|
kontrolny_sucet[i] = (char)kontrolny_sucet[i];
|
|
|
|
}
|
|
|
|
wolfSSL_write(ssl, kontrolny_sucet, velkost_kontrolneho_suctu);
|
|
|
|
if(uspech <= 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nastala chyba pri posielani kontrolneho suctu.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-03-11 21:01:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 20:11:21 +00:00
|
|
|
int prijat_subor(WOLFSSL* ssl, WOLFSSL_CTX* ctx, nastavenia_aplikacie* nastavenia)
|
2020-03-11 21:01:54 +00:00
|
|
|
{
|
2020-03-12 16:22:08 +00:00
|
|
|
int uspech;
|
|
|
|
|
|
|
|
//prijem dat o velkosti suboru
|
|
|
|
char* cesta = calloc(VELKOST_CESTY, sizeof(char));
|
|
|
|
uspech = wolfSSL_read(ssl, cesta, VELKOST_CESTY);
|
|
|
|
if(uspech <= 0)
|
|
|
|
{
|
2020-03-13 09:25:19 +00:00
|
|
|
fprintf(stderr, "Nastala chyba pri prijati dat o subore\n");
|
2020-03-12 16:22:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2020-03-11 21:01:54 +00:00
|
|
|
printf("Prebieha prijimanie suboru %s\n", cesta);
|
|
|
|
|
|
|
|
//ziskanie informacie od klienta o velkosti odoslaneho suboru
|
|
|
|
char velkost_suboru[32];
|
2020-03-13 09:25:19 +00:00
|
|
|
uspech = wolfSSL_read(ssl, velkost_suboru, VELKOST_SUBOR);
|
|
|
|
if(uspech <= 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nastala chyba pri prijati velkosti suboru\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2020-03-11 21:01:54 +00:00
|
|
|
long velkost = atol(velkost_suboru);
|
|
|
|
if(velkost < 1)
|
|
|
|
{
|
|
|
|
printf("Nastala chyba pri prijati suboru\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Velkost suboru: %s bajtov\n", velkost_suboru);
|
|
|
|
}
|
2020-03-12 16:22:08 +00:00
|
|
|
|
2020-03-11 21:01:54 +00:00
|
|
|
//prijem jednotlivych bajtov
|
|
|
|
char* pole = malloc(velkost);
|
|
|
|
char* pole_uk = pole;
|
|
|
|
for(int i = 0; i < velkost + 1; ++i)
|
|
|
|
{
|
2020-03-12 16:22:08 +00:00
|
|
|
uspech = wolfSSL_read(ssl, pole_uk, velkost);
|
|
|
|
if(uspech <= 0)
|
|
|
|
{
|
2020-03-19 12:55:34 +00:00
|
|
|
fprintf(stderr, "Nastala chyba pri prijati suboru.\n");
|
2020-03-12 16:22:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2020-03-11 21:01:54 +00:00
|
|
|
pole_uk += 1;
|
|
|
|
}
|
|
|
|
printf("Subor bol uspesne prijaty.\n");
|
2020-03-12 16:22:08 +00:00
|
|
|
|
2020-03-19 12:55:34 +00:00
|
|
|
//vypocet vlastneho kontrolneho suctu
|
|
|
|
byte* kontrolny_sucet;
|
|
|
|
int velkost_kontrolneho_suctu;
|
2020-04-07 20:11:21 +00:00
|
|
|
kontrolny_sucet = generovat_kontrolny_sucet_suboru(nastavenia->funkcia, cesta, &velkost_kontrolneho_suctu);
|
2020-03-19 12:55:34 +00:00
|
|
|
|
|
|
|
//prijem hashu, ktory vypocital server
|
|
|
|
char* prijaty_kontrolny_sucet = calloc(velkost_kontrolneho_suctu, sizeof(char));
|
|
|
|
uspech = wolfSSL_read(ssl, prijaty_kontrolny_sucet, velkost_kontrolneho_suctu);
|
|
|
|
if(uspech <= 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nastala chyba pri prijati kontrolneho suctu.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-03-12 16:22:08 +00:00
|
|
|
//kontrola ci sa prijaty a vypocitany kontrolny sucet suboru zhoduju
|
2020-03-19 12:55:34 +00:00
|
|
|
for(int i = 0; i < velkost_kontrolneho_suctu; i++)
|
|
|
|
{
|
|
|
|
kontrolny_sucet[i] = (char)kontrolny_sucet[i];
|
|
|
|
}
|
|
|
|
printf("%s\n", prijaty_kontrolny_sucet);
|
|
|
|
printf("%s\n", kontrolny_sucet);
|
|
|
|
if(!strcmp(prijaty_kontrolny_sucet, kontrolny_sucet))
|
2020-03-12 16:22:08 +00:00
|
|
|
{
|
|
|
|
printf("Subor prisiel v poriadku.\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-19 12:55:34 +00:00
|
|
|
fprintf(stderr, "Kontrolne sucty sa nezhoduju.\n");
|
|
|
|
printf("Subor neprisiel v poriadku alebo neboli pouzite rovnake funkcie.\n");
|
2020-03-12 16:22:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2020-03-11 21:01:54 +00:00
|
|
|
|
|
|
|
//zapis nacitanych dat do suboru
|
|
|
|
FILE* subor = fopen(cesta, "w");
|
|
|
|
fwrite(pole, 1, velkost, subor);
|
|
|
|
fclose(subor);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pripojit_na_server(char *ip_adresa, int cislo_portu, int pocet_sekund)
|
|
|
|
{
|
|
|
|
struct timeval casovy_interval;
|
|
|
|
int uspech;
|
|
|
|
int cislo_soketu;
|
|
|
|
struct hostent* hostitel;
|
|
|
|
struct sockaddr_in adresa;
|
|
|
|
fd_set sada_soketov;
|
|
|
|
socklen_t velkost_soketu;
|
|
|
|
int optval;
|
|
|
|
|
|
|
|
if((hostitel = gethostbyname(ip_adresa)) == NULL)
|
|
|
|
{
|
|
|
|
printf("Nastala chyba pri spracovani nazvu hostitela.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
printf("Vytvaranie soketu...\n");
|
|
|
|
cislo_soketu = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
memset(&adresa, 0, sizeof(adresa));
|
|
|
|
adresa.sin_family = AF_INET; //IPv4
|
|
|
|
adresa.sin_port = htons(cislo_portu);
|
|
|
|
adresa.sin_addr.s_addr = *(long*)(hostitel->h_addr);
|
|
|
|
|
|
|
|
//pokus o pripojenie s casovym intervalom
|
|
|
|
uspech = connect(cislo_soketu, (struct sockaddr*)&adresa, sizeof(adresa));
|
|
|
|
if(uspech < 0)
|
|
|
|
{
|
2020-04-07 20:11:21 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
if(errno == WSAEINPROGRESS )
|
|
|
|
#else
|
|
|
|
if(errno == EINPROGRESS )
|
|
|
|
#endif
|
2020-03-11 21:01:54 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Nebolo mozne okamzite vytvorit spojenie\n");
|
|
|
|
do
|
|
|
|
{
|
|
|
|
casovy_interval.tv_sec = pocet_sekund;
|
|
|
|
casovy_interval.tv_usec = 0;
|
|
|
|
FD_ZERO(&sada_soketov);
|
|
|
|
FD_SET(cislo_soketu, &sada_soketov);
|
|
|
|
uspech = select(cislo_soketu + 1, NULL, &sada_soketov, NULL, &casovy_interval);
|
2020-04-07 20:11:21 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
if (uspech < 0 && errno != WSAEINTR)
|
|
|
|
#else
|
2020-03-11 21:01:54 +00:00
|
|
|
if (uspech < 0 && errno != EINTR)
|
2020-04-07 20:11:21 +00:00
|
|
|
#endif
|
2020-03-11 21:01:54 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Nastala chyba pri pokuse o vytvorenie spojenia\nCislo chyby: %d\nPopis: %s\n", errno, strerror(errno));
|
|
|
|
ukoncit_soket(cislo_soketu);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
//bol zvoleny soket
|
|
|
|
else if(uspech > 0)
|
|
|
|
{
|
|
|
|
velkost_soketu = sizeof(int);
|
|
|
|
if (getsockopt(cislo_soketu, SOL_SOCKET, SO_ERROR, (void*)(&optval), &velkost_soketu) < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nastala chyba v nastaveni soketu\nCislo chyby: %d\nPopis: %s\n", errno, strerror(errno));
|
|
|
|
ukoncit_soket(cislo_soketu);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
if(optval)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nastala chyba v spojeni\nCislo chyby: %d\nPopis: %s\n", optval, strerror(optval));
|
|
|
|
ukoncit_soket(cislo_soketu);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Casovy interval vyprsal\n");
|
|
|
|
ukoncit_soket(cislo_soketu);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
} while(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Spojenie zlyhalo.\nCislo chyby: %d\nPopis: %s\n", errno, strerror(errno));
|
|
|
|
ukoncit_soket(cislo_soketu);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cislo_soketu;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cakat_na_komunikaciu(int cislo_portu)
|
|
|
|
{
|
|
|
|
int cislo_soketu;
|
|
|
|
struct sockaddr_in adresa;
|
|
|
|
|
|
|
|
printf("Vytvaranie socketu...\n");
|
|
|
|
cislo_soketu = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
memset(&adresa, 0, sizeof(adresa));
|
|
|
|
adresa.sin_family = AF_INET; //IPv4
|
|
|
|
adresa.sin_port = htons(cislo_portu);
|
|
|
|
adresa.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
|
|
|
if(bind(cislo_soketu, (struct sockaddr*)&adresa, sizeof(adresa)) != 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Nebolo mozne priradit soket ku danemu portu.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2020-03-12 16:22:08 +00:00
|
|
|
|
|
|
|
//maximalna velkost do ktorej rada cakajucich spojeni pre soket moze rast
|
|
|
|
int velkost_radu = 10;
|
|
|
|
|
|
|
|
if(listen(cislo_soketu, velkost_radu) != 0)
|
2020-03-11 21:01:54 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Na danom porte nie je mozne cakat na komunikaciu.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Server caka na komunikaciu na porte %d.\n", cislo_portu);
|
|
|
|
}
|
|
|
|
return cislo_soketu;
|
|
|
|
}
|
2020-03-30 01:32:44 +00:00
|
|
|
|
2020-04-07 20:11:21 +00:00
|
|
|
|
2020-03-30 01:32:44 +00:00
|
|
|
|