Mercurial
comparison playground/main.c @ 25:342726584be2
[Bun] Fixed how bun would be ran within bazel.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Thu, 09 Oct 2025 06:41:02 -0700 |
| parents | fa2b8af609d9 |
| children | 75de5903355c |
comparison
equal
deleted
inserted
replaced
| 24:7d3fa1a7a854 | 25:342726584be2 |
|---|---|
| 1 #include <stdio.h> | 1 #include <stdio.h> |
| 2 #include <stdlib.h> | |
| 3 #include <string.h> | |
| 4 #include <unistd.h> | |
| 5 #include <fcntl.h> | |
| 6 #include <errno.h> | |
| 7 #include <netdb.h> | |
| 8 #include <arpa/inet.h> | |
| 9 #include <openssl/ssl.h> | |
| 10 #include <openssl/err.h> | |
| 11 | 2 |
| 12 #define INITIAL_BUFFER_CAPACITY 8192 | 3 struct Node { |
| 4 char *value; | |
| 5 struct Node *left; | |
| 6 struct Node *right; | |
| 7 }; | |
| 13 | 8 |
| 14 void init_openssl() | 9 void *recurr(struct Node *root, int depth) |
| 15 { | 10 { |
| 16 SSL_load_error_strings(); | 11 if (depth) |
| 17 OpenSSL_add_ssl_algorithms(); | 12 return; |
| 13 root.left = | |
| 18 } | 14 } |
| 19 | 15 |
| 20 int main(void) | 16 int main() |
| 21 { | 17 { |
| 22 const char *host = "www.google.com"; | 18 struct Node root = {} |
| 23 const char *port = "443"; | 19 struct Node **queue = {} |
| 24 | |
| 25 struct addrinfo hints = {0}, *res; | |
| 26 hints.ai_family = AF_UNSPEC; | |
| 27 hints.ai_socktype = SOCK_STREAM; | |
| 28 | |
| 29 if (getaddrinfo(host, port, &hints, &res) != 0) | |
| 30 { | |
| 31 perror("getaddrinfo"); | |
| 32 return 1; | |
| 33 } | |
| 34 | |
| 35 int sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); | |
| 36 if (sockfd < 0) | |
| 37 { | |
| 38 perror("socket"); | |
| 39 return 1; | |
| 40 } | |
| 41 | |
| 42 if (connect(sockfd, res->ai_addr, res->ai_addrlen) != 0) | |
| 43 { | |
| 44 perror("connect"); | |
| 45 return 1; | |
| 46 } | |
| 47 | |
| 48 freeaddrinfo(res); | |
| 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; | |
| 94 } | 20 } |