Mercurial
diff playground/main.c @ 18:fa2b8af609d9
[Seobeo] Fixed a bug with pathing. Support SSL.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Mon, 06 Oct 2025 08:21:34 -0700 |
| parents | d97ec3ded2ae |
| children | 342726584be2 |
line wrap: on
line diff
--- a/playground/main.c Sat Oct 04 07:53:12 2025 -0700 +++ b/playground/main.c Mon Oct 06 08:21:34 2025 -0700 @@ -1,83 +1,94 @@ -// #include <pthread.h> -// #include <stdio.h> -// #include <stdlib.h> -// #define NUM_THREADS 5 -// -// void *PrintHello(void *threadid) -// { -// long tid; -// tid = (long)threadid; -// printf("Hello World! It's me, thread #%ld!\n", tid); -// pthread_exit(NULL); -// } -// -// int main (int argc, char *argv[]) -// { -// pthread_t threads[NUM_THREADS]; -// int rc; -// long t; -// for(t = 0; t < NUM_THREADS; t++) -// { -// printf("In main: creating thread %ld\n", t); -// rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t); -// if (rc) -// { -// printf("ERROR; return code from pthread_create() is %d\n", rc); -// exit(-1); -// } -// } -// -// /* Last thing that main() should do */ -// pthread_exit(NULL); -// } +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> +#include <errno.h> +#include <netdb.h> +#include <arpa/inet.h> +#include <openssl/ssl.h> +#include <openssl/err.h> -#include <sys/event.h> -#include <err.h> -#include <fcntl.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> +#define INITIAL_BUFFER_CAPACITY 8192 -int main(int argc, char **argv) +void init_openssl() +{ + SSL_load_error_strings(); + OpenSSL_add_ssl_algorithms(); +} + +int main(void) { - struct kevent event; /* Event we want to monitor */ - struct kevent tevent; /* Event triggered */ - int kq, fd, ret; + const char *host = "www.google.com"; + const char *port = "443"; - if (argc != 2) - err(EXIT_FAILURE, "Usage: %s path\n", argv[0]); + struct addrinfo hints = {0}, *res; + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + + if (getaddrinfo(host, port, &hints, &res) != 0) + { + perror("getaddrinfo"); + return 1; + } - fd = open(argv[1], O_RDONLY); - if (fd == -1) - err(EXIT_FAILURE, "Failed to open '%s'", argv[1]); + int sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); + if (sockfd < 0) + { + perror("socket"); + return 1; + } - /* Create kqueue. */ - kq = kqueue(); - if (kq == -1) - err(EXIT_FAILURE, "kqueue() failed"); + if (connect(sockfd, res->ai_addr, res->ai_addrlen) != 0) + { + perror("connect"); + return 1; + } - /* Initialize kevent structure. */ - EV_SET(&event, fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, - NOTE_WRITE | NOTE_ATTRIB, - 0, NULL); + freeaddrinfo(res); + + init_openssl(); + SSL_CTX *ctx = SSL_CTX_new(TLS_client_method()); + SSL_CTX_set_default_verify_paths(ctx); + + SSL *ssl = SSL_new(ctx); + SSL_set_fd(ssl, sockfd); - /* Attach event to the kqueue. */ - ret = kevent(kq, &event, 1, NULL, 0, NULL); - if (ret == -1) - err(EXIT_FAILURE, "kevent register"); + // ✅ use real hostname for SNI + SSL_set_tlsext_host_name(ssl, host); + + // ✅ perform blocking handshake for simplicity + fcntl(sockfd, F_SETFL, 0); + + if (SSL_connect(ssl) != 1) + { + fprintf(stderr, "SSL_connect failed\n"); + ERR_print_errors_fp(stderr); + return 1; + } + + printf("✅ SSL handshake successful!\n"); - for (;;) - { - /* Sleep until something happens. */ - ret = kevent(kq, NULL, 0, &tevent, 1, NULL); - if (ret == -1) { - err(EXIT_FAILURE, "kevent wait"); - } else if (ret > 0) { - if (tevent.flags & EV_ERROR) - errx(EXIT_FAILURE, "Event error: %s", strerror(event.data)); - else - printf("Something was written in '%s'\n", argv[1]); - } - } + // send a basic HTTP request + const char *req = + "GET / HTTP/1.1\r\n" + "Host: www.google.com\r\n" + "Connection: close\r\n\r\n"; + SSL_write(ssl, req, strlen(req)); + + char buf[INITIAL_BUFFER_CAPACITY]; + int bytes; + while ((bytes = SSL_read(ssl, buf, sizeof(buf) - 1)) > 0) + { + buf[bytes] = '\0'; + printf("%s", buf); + } + + SSL_shutdown(ssl); + SSL_free(ssl); + SSL_CTX_free(ctx); + close(sockfd); + EVP_cleanup(); + return 0; }