diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..be7aca5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +webpage diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1b2504c --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +SRC=main.c +BIN=webpage +LIB=ssl +FLAGS=-Wall -Wextra + +all: $(BIN) + +$(BIN) : $(SRC) + gcc $(FLAGS) -o $(BIN) $(SRC) -l$(LIB) + +run : $(BIN) + ./$(BIN) + +clean : + rm $(BIN) + +.PHONY : run clean diff --git a/main.c b/main.c index 6428a84..e1a00ba 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,68 @@ #include +#include +#include +#include +#include + +char page[1024]; + +void getPage(void){ + int sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd == -1){ + perror("ERROR: Could not create socket.\n"); + exit(-1); + } + + struct sockaddr_in addr = { + .sin_family = AF_INET, + .sin_port = htons(443), + .sin_addr = htonl(0x08080808) + }; + + if (connect(sockfd, (const struct sockaddr*)&addr, sizeof(addr)) == -1){ + perror("ERROR: Could not connect.\n"); + exit(-1); + } + + SSL_CTX* ctx = SSL_CTX_new(TLS_method()); + if (ctx == NULL){ + perror("ERROR: Could not create SSL_CTX object.\n"); + exit(-1); + } + SSL* ssl = SSL_new(ctx); + if (ssl == NULL){ + perror("ERROR: Could not create SSL structure.\n"); + exit(-1); + } + if (SSL_set_fd(ssl, sockfd) != 1){ + perror("ERROR: Could not connect SSL object file descriptor.\n"); + exit(-1); + } + if (SSL_connect(ssl) != 1){ + perror("ERROR: Could not initiate TLS/SSL connection.\n"); + exit(-1); + } + + char* request = "GET /\r\n\r\n"; + if (SSL_write(ssl, request, strlen(request)) <= 0){ + perror("ERROR: Could not send request.\n"); + exit(-1); + } + char buffer[1024] = {0}; + int readbyte = SSL_read(ssl, buffer, 1023); + if (readbyte <= 0){ + perror("ERROR: Could not read from connection.\n"); + exit(-1); + } + + strcpy(page, buffer); +} int main(){ - printf("Hello World!\n"); + + getPage(); + + printf("Web Page:\n%s\n", page); + return 0; }