2025-05-03 16:23:19 +02:00

81 lines
1.9 KiB
C++

#include "../common/defines.h"
#include "flow_help.h"
#include "ssl.h"
#include "tcp.h"
#include <arpa/inet.h>
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <netdb.h>
#include <netinet/in.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
if (argc < 3) {
fprintf(stderr, "Usage: %s <server_ip> <port>\n", argv[0]);
exit(EXIT_FAILURE);
}
SSL_CTX *ctx = pupes_message_client_lib::InitTLS();
int sockfd =
pupes_message_client_lib::create_socket_and_connect(argv[1], argv[2]);
SSL *ssl = pupes_message_client_lib::TLS_handshake(ctx, sockfd);
// Fork to handle sending and receiving
pid_t pid = fork();
if (pid < 0)
pupes_message_client_lib::error("Fork failed");
if (pid == 0) { // Child process: receive from server
char buffer[BUFFER_SIZE];
int bytes_read;
while (1) {
bytes_read = SSL_read(ssl, buffer, BUFFER_SIZE);
if (bytes_read > 0) {
fwrite(buffer, 1, static_cast<size_t>(bytes_read), stdout);
fflush(stdout);
memset(buffer, 0, BUFFER_SIZE);
} else {
int err = SSL_get_error(ssl, bytes_read);
if (err == SSL_ERROR_WANT_READ)
continue;
else
break;
}
}
kill(getppid(), SIGTERM); // Signal parent to terminate
exit(0);
} else { // Parent process: send to server
char buffer[BUFFER_SIZE];
int bytes_read;
while ((bytes_read = static_cast<int>(
read(STDIN_FILENO, buffer, BUFFER_SIZE))) > 0) {
if (SSL_write(ssl, buffer, bytes_read) < 0) {
fprintf(stderr, "SSL_write error: %d\n", SSL_get_error(ssl, 0));
break;
}
}
kill(pid, SIGTERM); // Signal child to terminate
}
pupes_message_client_lib::SSLcleanup(ssl, ctx, sockfd);
return 0;
}