mogens_og_karen/src/server/main.cc
2024-02-11 22:36:58 +01:00

91 lines
2.1 KiB
C++

#include <arpa/inet.h>
#include <iostream>
#include <netinet/in.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <ostream>
#include <sys/socket.h>
#include <unistd.h>
using std::cout, std::endl, std::string;
/*
* Remember to build with the flags "-L/usr/lib -lssl -lcrypto"
*/
int main() {
SSL_library_init();
SSL_load_error_strings();
SSL_CTX *ctx = SSL_CTX_new(TLS_server_method());
if (!ctx) {
// TODO Handle error
}
// Load certificate
if (SSL_CTX_use_certificate_file(ctx, "./ssl/server.crt",
SSL_FILETYPE_PEM) <= 0) {
// TODO Handle error
}
// Load certificate private key
if (SSL_CTX_use_PrivateKey_file(ctx, "./ssl/server.key",
SSL_FILETYPE_PEM) <= 0) {
// TODO Handle error
}
cout << "Initializing server" << endl;
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == -1) {
// TODO error
}
sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(8080);
serverAddress.sin_addr.s_addr = inet_addr("127.0.0.50");
int bindStatus = bind(serverSocket, (struct sockaddr *)&serverAddress,
sizeof(serverAddress));
if (bindStatus == -1) {
// TODO error
}
int listenStatus = listen(serverSocket, 5);
if (listenStatus == -1) {
// TODO error
}
int clientSocket = accept(serverSocket, nullptr, nullptr);
if (clientSocket == -1) {
// TODO error
}
SSL *ssl = SSL_new(ctx);
SSL_set_fd(ssl, clientSocket);
// Perform the SSL negotiation
if (SSL_accept(ssl)) {
// TODO Handle error
}
while (true) {
char buffer[1024] = {0};
SSL_read(ssl, buffer, sizeof(buffer) - 1);
if (strlen(buffer) <= 0) {
cout << "Client seems to have just straight up left :(" << endl;
break;
}
cout << buffer << endl;
}
SSL_free(ssl);
SSL_CTX_free(ctx);
ERR_free_strings();
close(serverSocket);
return 0;
}