Archived
1
0
Fork 0

💡 Add more explanations

This commit is contained in:
Simon V. Lejel 2024-02-13 17:21:24 +01:00
parent 1ff5a07a1f
commit 422d0b0bb0
Signed by: sl
GPG key ID: 6544A0430A2CFFAD

View file

@ -35,17 +35,17 @@ string getCtxError() {
int main() {
// Inistialize the SSL library
// Initializes the SSL library
if (OpenSSL_version_num() < 0x10100000L) {
// Old version, deprecated as of version 1.1.0
SSL_library_init();
// Loads human readable error strings
// Automatically inistialized in newer versions
// Automatically initialized in newer versions
SSL_load_error_strings();
} else {
// New version
// Initalizes both SSl and crypto and is generally better
// Initializes both SSL and crypto and is generally better
OPENSSL_init_ssl(0, nullptr);
}
@ -74,32 +74,55 @@ int main() {
}
cout << "Initializing server" << endl;
// Initializes a new TCP socket in the IPv4 family
// 0 tells the OS to automatically pick the correct protocol (TCP)
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == -1) {
cout << "Server socket failed" << endl;
return serverSocket;
}
sockaddr_in serverAddress;
// Specify that we use the IPv4 family
serverAddress.sin_family = AF_INET;
// Specify the port number
// htons converts it to bytes
serverAddress.sin_port = htons(8080);
// Specify the listen address
// inet_addr converts the string to bytes
serverAddress.sin_addr.s_addr = inet_addr("127.0.0.50");
// Bind the serverSocket to the serverAddress
// serverAddress is cast to a sockaddr
int bindStatus = bind(serverSocket, (struct sockaddr *)&serverAddress,
sizeof(serverAddress));
if (bindStatus == -1) {
cout << "Bind failed" << endl;
return bindStatus;
}
int listenStatus = listen(serverSocket, 5);
// Prepare to accept connections
// Only 1 is allowed, others will be denied as the server exits when a
// client leaves
int listenStatus = listen(serverSocket, 1);
if (listenStatus == -1) {
cout << "Listen failed" << endl;
return listenStatus;
}
// Await connection from a client
// We don't need the client's address nor do we know the length of it
int clientSocket = accept(serverSocket, nullptr, nullptr);
if (clientSocket == -1) {
cout << "Client socket failed" << endl;
return clientSocket;
}
// Attach our SSL context to the socket
SSL *ssl = SSL_new(ctx);
SSL_set_fd(ssl, clientSocket);
@ -110,25 +133,41 @@ int main() {
char *errStr = ERR_error_string(ERR_get_error(), nullptr);
cout << "SSL_accept failed with SSL error: " << sslError << endl;
cout << "OpenSSL error: " << errStr << endl;
return sslAcceptCode;
} else {
cout << "negotiated SSL" << endl;
}
while (true) {
// Define buffer for incoming data
char buffer[1024] = {0};
// Read the incoming data and save it to the buffer
SSL_read(ssl, buffer, sizeof(buffer) - 1);
// If the buffer is empty then the client has closed the connection
// Therefore just exit the while loop
if (strlen(buffer) <= 0) {
cout << "Client seems to have just straight up left :(" << endl;
break;
}
// The buffer contains data
// Print the data the client sent
cout << buffer << endl;
}
// Deallocate the SSL objects
SSL_free(ssl);
// Deallocate the SSL context
SSL_CTX_free(ctx);
// Deallocate the SSL error strings
ERR_free_strings();
// Close the network socket
close(serverSocket);
return 0;