bakalarska_praca/rs232_kanal/server.c

107 lines
2.4 KiB
C
Raw Normal View History

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-03-12 16:22:08 +00:00
// Datum poslednej upravy: 12.3.2020 //
2020-03-11 21:01:54 +00:00
//////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wolfssl/ssl.h>
#include <wolfssl/certs_test.h>
#include <wolfssl/wolfcrypt/types.h>
#ifdef _WIN32
#include <Windows.h>
2020-03-30 01:32:44 +00:00
#include <io.h>
#include <fcntl.h>
#define O_NOCTTY 0
2020-03-11 21:01:54 +00:00
#else
#include <unistd.h>
#endif
#include "../kniznica/kryptografia.h"
2020-03-30 01:32:44 +00:00
#include "../kniznica/komunikacia.h"
2020-03-11 21:01:54 +00:00
#include "../kniznica/rs232.h"
2020-03-30 01:32:44 +00:00
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;
}
2020-03-11 21:01:54 +00:00
int main(int argc, char const *argv[])
{
WOLFSSL *ssl;
WOLFSSL_CTX *ctx = NULL;
2020-03-30 01:32:44 +00:00
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);
2020-03-11 21:01:54 +00:00
2020-03-30 01:32:44 +00:00
int uspech;
/* uspech = rs232_otvorit_rozhranie(cislo_rozhrania, rychlost, rezim, 0);
if(uspech == -1)
2020-03-11 21:01:54 +00:00
{
2020-03-30 01:32:44 +00:00
return -1;
2020-03-11 21:01:54 +00:00
}
2020-03-30 01:32:44 +00:00
*/
2020-03-11 21:01:54 +00:00
if((ctx = nastavit_ctx_server()) == NULL)
{
return -1;
}
2020-03-30 01:32:44 +00:00
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;
2020-03-11 21:01:54 +00:00
wolfSSL_SetIOSend(ctx, rs232_zapis);
wolfSSL_SetIORecv(ctx, rs232_citanie);
if ((ssl = wolfSSL_new(ctx)) == NULL)
{
printf("Nepodarilo sa vytvorit ssl relaciu\n");
wolfSSL_CTX_free(ctx);
return -1;
}
2020-03-30 01:32:44 +00:00
wolfSSL_set_fd(ssl, rs232_prijat);
wolfSSL_set_using_nonblock(ssl, rs232_prijat);
printf("bol som tu\n");
while(uspech != SSL_SUCCESS)
2020-03-11 21:01:54 +00:00
{
2020-03-30 01:32:44 +00:00
uspech = wolfSSL_accept(ssl);
printf("%d\n", uspech);
2020-03-11 21:01:54 +00:00
}
2020-03-30 01:32:44 +00:00
printf("bol som aj tu tu\n");
char sprava[100];
wolfSSL_read(ssl, sprava, 10);
printf("%s\n", sprava);
2020-03-11 21:01:54 +00:00
2020-03-30 01:32:44 +00:00
RS232_CloseComport(cislo_rozhrania);
2020-03-11 21:01:54 +00:00
return 0;
}