This commit is contained in:
_N3m0 2023-12-30 14:09:04 +01:00
parent d2b24cf094
commit 96a9650a59
3 changed files with 81 additions and 1 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
webpage

17
Makefile Normal file
View File

@ -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

64
main.c
View File

@ -1,6 +1,68 @@
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <openssl/ssl.h>
#include <string.h>
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;
}