comparison 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
comparison
equal deleted inserted replaced
17:d97ec3ded2ae 18:fa2b8af609d9
1 // #include <pthread.h> 1 #include <stdio.h>
2 // #include <stdio.h> 2 #include <stdlib.h>
3 // #include <stdlib.h> 3 #include <string.h>
4 // #define NUM_THREADS 5 4 #include <unistd.h>
5 // 5 #include <fcntl.h>
6 // void *PrintHello(void *threadid) 6 #include <errno.h>
7 // { 7 #include <netdb.h>
8 // long tid; 8 #include <arpa/inet.h>
9 // tid = (long)threadid; 9 #include <openssl/ssl.h>
10 // printf("Hello World! It's me, thread #%ld!\n", tid); 10 #include <openssl/err.h>
11 // pthread_exit(NULL);
12 // }
13 //
14 // int main (int argc, char *argv[])
15 // {
16 // pthread_t threads[NUM_THREADS];
17 // int rc;
18 // long t;
19 // for(t = 0; t < NUM_THREADS; t++)
20 // {
21 // printf("In main: creating thread %ld\n", t);
22 // rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
23 // if (rc)
24 // {
25 // printf("ERROR; return code from pthread_create() is %d\n", rc);
26 // exit(-1);
27 // }
28 // }
29 //
30 // /* Last thing that main() should do */
31 // pthread_exit(NULL);
32 // }
33 11
34 #include <sys/event.h> 12 #define INITIAL_BUFFER_CAPACITY 8192
35 #include <err.h>
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 13
41 int main(int argc, char **argv) 14 void init_openssl()
42 { 15 {
43 struct kevent event; /* Event we want to monitor */ 16 SSL_load_error_strings();
44 struct kevent tevent; /* Event triggered */ 17 OpenSSL_add_ssl_algorithms();
45 int kq, fd, ret; 18 }
46 19
47 if (argc != 2) 20 int main(void)
48 err(EXIT_FAILURE, "Usage: %s path\n", argv[0]); 21 {
22 const char *host = "www.google.com";
23 const char *port = "443";
49 24
50 fd = open(argv[1], O_RDONLY); 25 struct addrinfo hints = {0}, *res;
51 if (fd == -1) 26 hints.ai_family = AF_UNSPEC;
52 err(EXIT_FAILURE, "Failed to open '%s'", argv[1]); 27 hints.ai_socktype = SOCK_STREAM;
53 28
54 /* Create kqueue. */ 29 if (getaddrinfo(host, port, &hints, &res) != 0)
55 kq = kqueue(); 30 {
56 if (kq == -1) 31 perror("getaddrinfo");
57 err(EXIT_FAILURE, "kqueue() failed"); 32 return 1;
33 }
58 34
59 /* Initialize kevent structure. */ 35 int sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
60 EV_SET(&event, fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, 36 if (sockfd < 0)
61 NOTE_WRITE | NOTE_ATTRIB, 37 {
62 0, NULL); 38 perror("socket");
39 return 1;
40 }
63 41
64 /* Attach event to the kqueue. */ 42 if (connect(sockfd, res->ai_addr, res->ai_addrlen) != 0)
65 ret = kevent(kq, &event, 1, NULL, 0, NULL); 43 {
66 if (ret == -1) 44 perror("connect");
67 err(EXIT_FAILURE, "kevent register"); 45 return 1;
46 }
68 47
69 for (;;) 48 freeaddrinfo(res);
70 {
71 /* Sleep until something happens. */
72 ret = kevent(kq, NULL, 0, &tevent, 1, NULL);
73 if (ret == -1) {
74 err(EXIT_FAILURE, "kevent wait");
75 } else if (ret > 0) {
76 if (tevent.flags & EV_ERROR)
77 errx(EXIT_FAILURE, "Event error: %s", strerror(event.data));
78 else
79 printf("Something was written in '%s'\n", argv[1]);
80 }
81 }
82 49
50 init_openssl();
51 SSL_CTX *ctx = SSL_CTX_new(TLS_client_method());
52 SSL_CTX_set_default_verify_paths(ctx);
53
54 SSL *ssl = SSL_new(ctx);
55 SSL_set_fd(ssl, sockfd);
56
57 // ✅ use real hostname for SNI
58 SSL_set_tlsext_host_name(ssl, host);
59
60 // ✅ perform blocking handshake for simplicity
61 fcntl(sockfd, F_SETFL, 0);
62
63 if (SSL_connect(ssl) != 1)
64 {
65 fprintf(stderr, "SSL_connect failed\n");
66 ERR_print_errors_fp(stderr);
67 return 1;
68 }
69
70 printf("✅ SSL handshake successful!\n");
71
72 // send a basic HTTP request
73 const char *req =
74 "GET / HTTP/1.1\r\n"
75 "Host: www.google.com\r\n"
76 "Connection: close\r\n\r\n";
77 SSL_write(ssl, req, strlen(req));
78
79 char buf[INITIAL_BUFFER_CAPACITY];
80 int bytes;
81
82 while ((bytes = SSL_read(ssl, buf, sizeof(buf) - 1)) > 0)
83 {
84 buf[bytes] = '\0';
85 printf("%s", buf);
86 }
87
88 SSL_shutdown(ssl);
89 SSL_free(ssl);
90 SSL_CTX_free(ctx);
91 close(sockfd);
92 EVP_cleanup();
93 return 0;
83 } 94 }