diff --git a/kniznica/komunikacia.c b/kniznica/komunikacia.c index beeee7e..f6cd2c4 100644 --- a/kniznica/komunikacia.c +++ b/kniznica/komunikacia.c @@ -10,6 +10,7 @@ #include "kryptografia.h" hashovacia_funkcia h_funkcia; +int rs232_rozhranie; void nastav_funkciu(hashovacia_funkcia funkcia) { @@ -285,3 +286,16 @@ int cakat_na_komunikaciu(int cislo_portu) } return cislo_soketu; } + +int rs232_otvorit_rozhranie(int cislo_rozhrania, int rychlost, const char* rezim, int riadenie_toku_dat) +{ + int uspech; + rs232_rozhranie = cislo_rozhrania; + uspech = RS232_OpenComport(cislo_rozhrania, rychlost, rezim, riadenie_toku_dat); + if(uspech == 1) + { + fprintf(stderr, "Nebolo mozne otvorit seriove rozhranie\n"); + return -1; + } +} + diff --git a/kniznica/komunikacia.h b/kniznica/komunikacia.h index 08fd4b4..27fb43e 100644 --- a/kniznica/komunikacia.h +++ b/kniznica/komunikacia.h @@ -15,6 +15,8 @@ #include #include "kryptografia.h" +#include "rs232.h" + #define VELKOST_CESTY 64 //velkost retazca, kt. uklada lokaciu suboru #define VELKOST_SUBOR 32 //velkost retazca, kt. uklada velkost prenasaneho suboru @@ -38,11 +40,17 @@ #else #include #include + #include + #include + #include #include #include #include #include #include + #include + #include + #include #define ukoncit_soket close #endif @@ -71,26 +79,9 @@ int cakat_na_komunikaciu(int cislo_portu); // RS232 KOMUNIKACNY KANAL // ///////////////////////////// -/** - * rs232_zapis: umoznuje zapis dat do serialoveho portu - * @parameter WOLSSL* ssl : ukazuje na aktualnu relaciu - * @parameter char* buf : ukazuje na buffer, do kt. zapise wolfssl zasifrovany text na odoslanie - * @parameter int sz : velkost buffera - * @parameter void* ctx : ukazuje na WOLFSSL_CTX strukturu - * @vrati int - */ -int rs232_zapis(WOLFSSL *ssl, char *buf, int sz, void *ctx); +int rs232_otvorit_rozhranie(int cislo_rozhrania, int rychlost, const char* rezim, int riadenie_toku_dat); + -/** - * rs232_citanie: umoznuje citanie dat zo serialoveho portu - * @parameter WOLFSSL* ssl : ukazuje na aktualnu relaciu - * @parameter char* buf : ukazuje na buffer, kde prichadzajuci zasifrovany text by mal byt nakopirovany - * aby ho wolfssl mohla odsifrovat - * @parameter int sz : velkost buffera - * @parameter void* ctx : ukazuje na WOLFSSL_CTX strukturu - * @vrati int - */ -int rs232_citanie(WOLFSSL *ssl, char *buf, int sz, void *ctx); ////////////////////// // PRACA SO SUBORMI // diff --git a/kniznica/rs232.c b/kniznica/rs232.c index 6b0e872..00499f4 100644 --- a/kniznica/rs232.c +++ b/kniznica/rs232.c @@ -859,15 +859,4 @@ int RS232_GetPortnr(const char *devname) } return -1; /* device not found */ -} - - - - - - - - - - - +} \ No newline at end of file diff --git a/rs232_kanal/Makefile b/rs232_kanal/Makefile index d34c2fe..d4dd89d 100644 --- a/rs232_kanal/Makefile +++ b/rs232_kanal/Makefile @@ -1,4 +1,3 @@ -################################################## ## Bakalarska praca ## ## Meno studenta: Tomas Lukac ## ## Veduci BP: prof. Ing. Milos Drutarovsky CSc. ## @@ -11,31 +10,45 @@ CC = gcc #prepinace pre prekladac CFLAGS = -Wcpp -I./../wolfssl_hlavickove_subory/ -LIB32 = -L. -lwolfssl -lws2_32 -Os -LIB64 = -L. -lwolfssl -lws2_32 -m64 +LIB = -L. -lwolfssl + +ifeq ($(OS), Windows_NT) + LIB += -lws2_32 + ifeq ($(PROCESSOR_ARCHITECTURE), AMD64) + LIB += -m64 + endif + ifeq ($(PROCESSOR_ARCHITECTURE), x86) + LIB += -Os + endif +else + UNAME_S := $(shell uname -s) + ifeq ($(UNAME_S), Linux) + LIB += + endif +endif all: klient server klient: klient.o kryptografia.o komunikacia.o rs232.o - $(CC) $(CFLAGS) -o klient klient.o kryptografia.o komunikacia.o rs232.o $(LIB32) + $(CC) $(CFLAGS) -o klient klient.o kryptografia.o komunikacia.o rs232.o $(LIB) server: server.o kryptografia.o komunikacia.o rs232.o - $(CC) $(CFLAGS) -o server server.o kryptografia.o komunikacia.o rs232.o $(LIB32) + $(CC) $(CFLAGS) -o server server.o kryptografia.o komunikacia.o rs232.o $(LIB) klient.o: klient.c - $(CC) -c klient.c $(LIB32) + $(CC) $(CFLAGS) -c klient.c $(LIB) server.o: server.c - $(CC) -c server.c $(LIB32) + $(CC) $(CFLAGS) -c server.c $(LIB) kryptografia.o: ../kniznica/kryptografia.c ../kniznica/kryptografia.h - $(CC) -c ../kniznica/kryptografia.c $(LIB32) + $(CC) $(CFLAGS) -c ../kniznica/kryptografia.c $(LIB) komunikacia.o: ../kniznica/komunikacia.c ../kniznica/komunikacia.h - $(CC) -c ../kniznica/komunikacia.c $(LIB32) + $(CC) $(CFLAGS) -c ../kniznica/komunikacia.c $(LIB) rs232.o: ../kniznica/rs232.c ../kniznica/rs232.h - $(CC) -c ../kniznica/rs232.c $(LIB32) + $(CC) $(CFLAGS) -c ../kniznica/rs232.c $(LIB) .PHONY: clean diff --git a/rs232_kanal/klient.c b/rs232_kanal/klient.c index 20c5b68..533d582 100644 --- a/rs232_kanal/klient.c +++ b/rs232_kanal/klient.c @@ -9,45 +9,75 @@ #include #include #include -#include #include #include #include #ifdef _WIN32 #include +#include +#include +#define O_NOCTTY 0 #else #include #endif #include "../kniznica/kryptografia.h" +#include "../kniznica/komunikacia.h" #include "../kniznica/rs232.h" -int cislo_portu = 2; -int baud_frekvencia = 9600; -char rezim[]={'8','N','1', 0}; +int rs232_poslat; +int rs232_prijat; + +int rs232_citanie(WOLFSSL *ssl, char *buf, int sz, void *ctx) +{ + (void)ssl; + (void)ctx; + int uspech = 0; + while(uspech <= 0) + { + uspech = (int)read(rs232_prijat, buf, (size_t)sz); + } + return uspech; +} + +int rs232_zapis(WOLFSSL *ssl, char *buf, int sz, void *ctx) +{ + (void)ssl; + (void)ctx; + int uspech = 0; + uspech = (int) write(rs232_poslat, buf, (size_t)sz); + return uspech; +} int main(int argc, char const *argv[]) { + char rezim[]={'8','N','1', 0}; + int cislo_rozhrania = 4; + int rychlost = 9600; WOLFSSL *ssl; WOLFSSL_CTX *ctx = NULL; - if(RS232_OpenComport(cislo_portu, baud_frekvencia, rezim, 1)) + rs232_poslat = open("server.txt", O_WRONLY | O_NOCTTY); + rs232_prijat = open("klient.txt", O_RDONLY | O_NOCTTY); + + int uspech; + /* uspech = rs232_otvorit_rozhranie(cislo_rozhrania, rychlost, rezim, 0); + if(uspech == -1) { - printf("Nebolo mozne otvorit serialovy port\n"); - } + return -1; + }*/ if((ctx = nastavit_ctx_klient()) == NULL) { return -1; } - const char* subor_certifikat = "../certifikaty/klient.pem"; - const char* subor_kluc = "../certifikaty/klient.key"; - if(!nacitat_certifikaty(ctx, subor_certifikat, subor_kluc)) return -1; + const char* subor_certifikat = "../certifikaty/klient/klient_rsa.pem"; + const char* subor_kluc = "../certifikaty/klient/klient_rsa.key"; + if(nacitat_certifikaty(ctx, subor_certifikat, subor_kluc) == -1) return -1; wolfSSL_SetIOSend(ctx, rs232_zapis); wolfSSL_SetIORecv(ctx, rs232_citanie); - if ((ssl = wolfSSL_new(ctx)) == NULL) { @@ -56,16 +86,30 @@ int main(int argc, char const *argv[]) return -1; } - wolfSSL_set_fd(ssl, *(int*)ctx); - wolfSSL_set_using_nonblock(ssl, *(int*)ctx); + wolfSSL_set_fd(ssl, rs232_prijat); + wolfSSL_set_using_nonblock(ssl, rs232_prijat); - int uspech = 0; - if(wolfSSL_connect(ssl) != SSL_SUCCESS) + printf("bol som tu\n"); + + while(uspech != SSL_SUCCESS) { - printf("Nepodarilo sa pripojit%d\n", uspech); - return -1; + uspech |= wolfSSL_connect(ssl); + printf("bol sss\n"); + if(uspech != SSL_SUCCESS) + { + printf("Nepodarilo sa pripojit%d\n", uspech); + return -1; + } + else + { + printf("oukej\n"); + } } + printf("bol som aj tu tu\n"); - RS232_CloseComport(cislo_portu); + + wolfSSL_write(ssl, "nieco", 10); + + RS232_CloseComport(cislo_rozhrania); return 0; } \ No newline at end of file diff --git a/rs232_kanal/klient.txt b/rs232_kanal/klient.txt new file mode 100644 index 0000000..75b83b4 Binary files /dev/null and b/rs232_kanal/klient.txt differ diff --git a/rs232_kanal/server.c b/rs232_kanal/server.c index 14b0796..553ed6f 100644 --- a/rs232_kanal/server.c +++ b/rs232_kanal/server.c @@ -9,42 +9,71 @@ #include #include #include -#include #include #include #include #ifdef _WIN32 #include +#include +#include +#define O_NOCTTY 0 #else #include #endif #include "../kniznica/kryptografia.h" +#include "../kniznica/komunikacia.h" #include "../kniznica/rs232.h" -int cislo_portu = 3; -int baud_frekvencia = 9600; -char rezim[]={'8','N','1', 0}; +int rs232_poslat; +int rs232_prijat; + +int rs232_citanie(WOLFSSL *ssl, char *buf, int sz, void *ctx) +{ + int uspech = 0; + while(uspech <= 0) + { + uspech = read(rs232_prijat, buf, sz); + } + return uspech; +} + +int rs232_zapis(WOLFSSL *ssl, char *buf, int sz, void *ctx) +{ + int uspech = 0; + uspech = write(rs232_poslat, buf, sz); + return uspech; +} int main(int argc, char const *argv[]) { WOLFSSL *ssl; WOLFSSL_CTX *ctx = NULL; + int cislo_rozhrania = 5; + int rychlost = 9600; + char rezim[]={'8','N','1', 0}; + + rs232_poslat = open("klient.txt", O_WRONLY | O_NOCTTY); + printf("%d\n", rs232_poslat); + rs232_prijat = open("server.txt", O_RDONLY | O_NOCTTY); + printf("%d\n", rs232_prijat); - //otvorenie portu pre citanie - if(RS232_OpenComport(cislo_portu, baud_frekvencia, rezim, 1)) + + int uspech; + /* uspech = rs232_otvorit_rozhranie(cislo_rozhrania, rychlost, rezim, 0); + if(uspech == -1) { - printf("Nebolo mozne otvorit serialovy port\n"); + return -1; } - +*/ if((ctx = nastavit_ctx_server()) == NULL) { return -1; } - const char* subor_certifikat = "../certifikaty/klient.pem"; - const char* subor_kluc = "../certifikaty/klient.key"; - if(!nacitat_certifikaty(ctx, subor_certifikat, subor_kluc)) return -1; + const char* subor_certifikat = "../certifikaty/server/server_rsa.pem"; + const char* subor_kluc = "../certifikaty/server/server_rsa.key"; + if(nacitat_certifikaty(ctx, subor_certifikat, subor_kluc) == -1) return -1; wolfSSL_SetIOSend(ctx, rs232_zapis); wolfSSL_SetIORecv(ctx, rs232_citanie); @@ -56,13 +85,23 @@ int main(int argc, char const *argv[]) return -1; } - int ret; - if(wolfSSL_accept(ssl) == -1) + wolfSSL_set_fd(ssl, rs232_prijat); + wolfSSL_set_using_nonblock(ssl, rs232_prijat); + + printf("bol som tu\n"); + while(uspech != SSL_SUCCESS) { - return -1; + uspech = wolfSSL_accept(ssl); + printf("%d\n", uspech); } + printf("bol som aj tu tu\n"); - RS232_CloseComport(cislo_portu); + + char sprava[100]; + wolfSSL_read(ssl, sprava, 10); + printf("%s\n", sprava); + + RS232_CloseComport(cislo_rozhrania); return 0; } \ No newline at end of file diff --git a/rs232_kanal/server.txt b/rs232_kanal/server.txt new file mode 100644 index 0000000..f50f1d9 Binary files /dev/null and b/rs232_kanal/server.txt differ diff --git a/rs232_kanal/wolfssl.dll b/rs232_kanal/wolfssl.dll index 882927c..635a2d4 100644 Binary files a/rs232_kanal/wolfssl.dll and b/rs232_kanal/wolfssl.dll differ diff --git a/rs232_kanal/wolfssl_32.dll b/rs232_kanal/wolfssl_32.dll new file mode 100644 index 0000000..882927c Binary files /dev/null and b/rs232_kanal/wolfssl_32.dll differ diff --git a/rs232_kanal/wolfssl_64.dll b/rs232_kanal/wolfssl_64.dll deleted file mode 100644 index 1d45e0f..0000000 Binary files a/rs232_kanal/wolfssl_64.dll and /dev/null differ diff --git a/teoria/serial.txt b/teoria/serial.txt new file mode 100644 index 0000000..bd0cde0 --- /dev/null +++ b/teoria/serial.txt @@ -0,0 +1,19 @@ +seriove rozhranie + - prenasa data po jednom bite + vyhody: lacnejsie kable a mensie konektory + - vacsinou myslime rozhrania vyuzivajuce asynchronne seriove protokoly, napr. rs232 rozhranie , rozhrania na vstavanych + zariadeniach, a ine + - vacsina seriovych rozhrani su obojsmerne: mozu prijimat aj posielat data + - vyhody + 1. mozu si medzi sebou vymienat rozne typy informacii + aplikacie vyuzivajuce seriove rozhrania casto zahrnuju citacie senzory, prepinace alebo ine vstupy + riadenie motoru, rele, displeje a ine vystupy + 2. lacny a dostupny HW + ak nie je k dispozicii seriovy port, existuje usb/serial konvertor + 3. okrem start, stop a paritnych bitov pridanych do kazdeho prenasaneho bajtu, seriove rozhrania + nepridavaju ziadne informacie ku prenasanym datam, narozdiel od usb, ethernet, kt. pouzivaju sofistikovane protokoly + ktore definuju format prenasanych dat. hardware a firmware musia implementovat tieto protokoly, co pridava komplexnost + kt. niektore aplikacie nepotrebuju + 4. kable mzou byt dlhe + 5. jednotlive os poskytuju ovladace pre pristup k seriovym rozhraniam + program. jazyky poskytuju triedy, kniznice alebo ine nastroje na seriovu komunikaciu \ No newline at end of file