#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#define HOSTNAME "public-privacy.com" // You can change this to test other sites
#define PORT "443" // HTTPS default port
#define PATH "/robots.txt" // Change this to the desired path, e.g., "/" for homepage
#define BUFFER_SIZE 4096
// --- Function to handle errors and clean up ---
void handle_error(const char* msg, SSL* ssl, SSL_CTX* ctx, int sockfd) {
perror(msg);
// Print OpenSSL errors with more details
ERR_print_errors_fp(stderr);
if (ssl) SSL_free(ssl);
if (ctx) SSL_CTX_free(ctx);
if (sockfd != -1) close(sockfd);
exit(EXIT_FAILURE);
}
int main() {
struct addrinfo hints, *res, *p;
int sockfd = -1;
SSL_CTX *ctx = NULL;
SSL *ssl = NULL;
char buffer[BUFFER_SIZE];
int bytes_received;
// 1. Initialize OpenSSL library
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
// 2. Create a new SSL context using TLS_client_method() for better compatibility
// This avoids SSLv3 and focuses on TLS protocols.
ctx = SSL_CTX_new(TLS_client_method());
if (!ctx) {
handle_error("Error creating SSL context", NULL, NULL, -1);
}
// 3. Set minimum TLS version to TLS 1.2 to avoid older, insecure versions
if (SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION) != 1) {
handle_error("Error setting minimum TLS version", NULL, ctx, -1);
}
// 4. Set a flexible cipher list to increase compatibility (you can customize this)
// "DEFAULT" includes commonly supported ciphers; alternatives: "HIGH" or "ALL" (less secure)
if (SSL_CTX_set_cipher_list(ctx, "DEFAULT") != 1) {
handle_error("Error setting cipher list", NULL, ctx, -1);
}
// 5. Set up the address information for DNS lookup
struct addrinfo hints_struct;
memset(&hints_struct, 0, sizeof(hints_struct));
hints_struct.ai_family = AF_UNSPEC; // IPv4 or IPv6
hints_struct.ai_socktype = SOCK_STREAM; // TCP stream socket
if (getaddrinfo(HOSTNAME, PORT, &hints_struct, &res) != 0) {
handle_error("getaddrinfo failed", NULL, ctx, -1);
}
// 6. Loop through the resolved addresses and connect to the first one that works
for (p = res; p != NULL; p = p->ai_next) {
sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (sockfd == -1) {
perror("socket failed, trying next address...");
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
sockfd = -1;
perror("connect failed, trying next address...");
continue;
}
break; // Connection successful
}
freeaddrinfo(res);
if (sockfd == -1) {
fprintf(stderr, "Could not connect to the server.\n");
SSL_CTX_free(ctx); // Clean up context
return EXIT_FAILURE;
}
// 7. Create an SSL session object and attach it to the socket
ssl = SSL_new(ctx);
if (!ssl) {
handle_error("Error creating SSL session", NULL, ctx, sockfd);
}
SSL_set_fd(ssl, sockfd);
// 8. Perform the TLS/SSL handshake with improved error handling
if (SSL_connect(ssl) <= 0) {
int ssl_err = SSL_get_error(ssl, SSL_connect(ssl)); // Get the error code
fprintf(stderr, "SSL handshake failed with error code: %d\n", ssl_err);
fprintf(stderr, "SSL alert number: %d\n", SSL_get_alert_type(ssl)); // Print alert type for debugging
ERR_print_errors_fp(stderr); // Print detailed SSL errors
SSL_free(ssl);
SSL_CTX_free(ctx);
close(sockfd);
return EXIT_FAILURE;
}
// 9. Construct the HTTP GET request
char request[256];
snprintf(request, sizeof(request),
"GET %s HTTP/1.1\r\n"
"Host: %s\r\n"
"User-Agent: C-lang-Robots-Fetcher/1.0\r\n"
"Connection: close\r\n\r\n",
PATH, HOSTNAME);
// 10. Send the request over the secure TLS channel
if (SSL_write(ssl, request, strlen(request)) <= 0) {
handle_error("Error writing to SSL connection", ssl, ctx, sockfd);
}
// 11. Read the response from the secure TLS channel
printf("--- Receiving data from %s%s ---\n\n", HOSTNAME, PATH);
while ((bytes_received = SSL_read(ssl, buffer, sizeof(buffer) - 1)) > 0) {
buffer[bytes_received] = '\0'; // Null-terminate the buffer
printf("%s", buffer);
}
// Check for errors during reading
if (bytes_received < 0) {
int ssl_err = SSL_get_error(ssl, bytes_received);
if (ssl_err != SSL_ERROR_ZERO_RETURN && ssl_err != SSL_ERROR_WANT_READ) {
fprintf(stderr, "Error reading from SSL connection: %d\n", ssl_err);
ERR_print_errors_fp(stderr);
}
}
printf("\n--- End of data ---\n");
// 12. Clean up resources
SSL_shutdown(ssl);
SSL_free(ssl);
SSL_CTX_free(ctx);
close(sockfd);
return 0;
}