backend: refactored tcp
This commit is contained in:
parent
d24f0ae010
commit
d5d4dc6455
@ -1,15 +1,22 @@
|
|||||||
|
|
||||||
CFLAGS = -std=c17 -Wall -Wextra -Wpedantic -Wconversion
|
CFLAGS = \
|
||||||
|
-std=c17 \
|
||||||
|
-Wall \
|
||||||
|
-Wextra \
|
||||||
|
-Wpedantic \
|
||||||
|
-Wconversion \
|
||||||
|
|
||||||
|
CC = gcc
|
||||||
|
|
||||||
HEADERS = $(wildcard *.h)
|
HEADERS = $(wildcard *.h)
|
||||||
|
|
||||||
all: compile_flags.txt server
|
all: compile_flags.txt server
|
||||||
|
|
||||||
server: main.o linux.o http.o
|
server: main.o http.o linux.o
|
||||||
gcc $^ -o $@
|
$(CC) $^ -o $@
|
||||||
|
|
||||||
%.o: %.c $(HEADERS)
|
%.o: %.c $(HEADERS)
|
||||||
gcc $< -c -o $@ $(CFLAGS)
|
$(CC) $< -c -o $@ $(CFLAGS)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf *.o server client
|
rm -rf *.o server client
|
||||||
|
@ -1,11 +1,93 @@
|
|||||||
#include "native.h"
|
#include "tcp.h"
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
void init_sockets(void)
|
struct TcpServer {
|
||||||
|
int server_socket;
|
||||||
|
struct sockaddr_in server_address;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct TcpConnection {
|
||||||
|
int client_socket;
|
||||||
|
struct sockaddr_in client_address;
|
||||||
|
};
|
||||||
|
|
||||||
|
void tcp_global_initialize_sockets(void) { }
|
||||||
|
|
||||||
|
TcpServer* tcp_server_create(const char* ip, uint16_t port)
|
||||||
{
|
{
|
||||||
|
int server_socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
if (server_socket < 0) {
|
||||||
|
printf("error: tcp: could not open socket\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &(int) { 1 }, sizeof(int)) < 0) {
|
||||||
|
printf("warning: tcp: could not setsockopt SO_REUSEADDR\n");
|
||||||
|
}
|
||||||
|
struct sockaddr_in server_address;
|
||||||
|
server_address.sin_addr.s_addr = inet_addr(ip);
|
||||||
|
server_address.sin_family = AF_INET;
|
||||||
|
server_address.sin_port = htons(port);
|
||||||
|
if (bind(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) {
|
||||||
|
printf("error: tcp: could not bind socket\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (listen(server_socket, SOMAXCONN) < 0) {
|
||||||
|
printf("error: tcp: could not listen on server\n");
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void close_socket(int socket)
|
TcpServer* self = malloc(sizeof(TcpServer));
|
||||||
{
|
*self = (TcpServer) {
|
||||||
close(socket);
|
.server_socket = server_socket,
|
||||||
|
.server_address = server_address,
|
||||||
|
};
|
||||||
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void tcp_server_destroy(TcpServer* server)
|
||||||
|
{
|
||||||
|
close(server->server_socket);
|
||||||
|
free(server);
|
||||||
|
}
|
||||||
|
|
||||||
|
TcpConnection* tcp_server_accept(TcpServer* server)
|
||||||
|
{
|
||||||
|
struct sockaddr_in client_address;
|
||||||
|
socklen_t client_size;
|
||||||
|
int client_socket
|
||||||
|
= accept(server->server_socket, (struct sockaddr*)&client_address, &client_size);
|
||||||
|
if (client_socket < 0) {
|
||||||
|
printf("error: tcp: could not accept connection\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
TcpConnection* connection = malloc(sizeof(TcpConnection));
|
||||||
|
*connection = (TcpConnection) {
|
||||||
|
.client_socket = client_socket,
|
||||||
|
.client_address = client_address,
|
||||||
|
};
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
void tcp_connection_destroy(TcpConnection* connection)
|
||||||
|
{
|
||||||
|
close(connection->client_socket);
|
||||||
|
free(connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t tcp_recieve(TcpConnection* connection, uint8_t* data, size_t amount)
|
||||||
|
{
|
||||||
|
return recv(connection->client_socket, data, amount, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t tcp_send(TcpConnection* connection, uint8_t* data, size_t amount)
|
||||||
|
{
|
||||||
|
return send(connection->client_socket, data, amount, 0);
|
||||||
|
}
|
||||||
|
111
backend/main.c
111
backend/main.c
@ -1,5 +1,5 @@
|
|||||||
#include "http.h"
|
#include "http.h"
|
||||||
#include "native.h"
|
#include "tcp.h"
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
@ -7,81 +7,53 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
const uint16_t port = 8201;
|
const uint16_t port = 8000;
|
||||||
|
|
||||||
int server_socket = 0;
|
TcpServer* server = NULL;
|
||||||
|
|
||||||
void int_handler(int a)
|
void interrupt_handler(int a)
|
||||||
{
|
{
|
||||||
(void)a;
|
(void)a;
|
||||||
printf("=== terminated ===\n");
|
printf("\nShutting down gracefully...\n");
|
||||||
if (server_socket != 0)
|
if (server != NULL)
|
||||||
close(server_socket);
|
tcp_server_destroy(server);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
init_sockets();
|
tcp_global_initialize_sockets();
|
||||||
|
|
||||||
signal(SIGINT, &int_handler);
|
signal(SIGINT, &interrupt_handler);
|
||||||
|
|
||||||
printf("starting server...\n");
|
printf("starting server...\n");
|
||||||
server_socket = socket(AF_INET, SOCK_STREAM, 0);
|
server = tcp_server_create("127.0.0.1", port);
|
||||||
if (server_socket < 0) {
|
if (server == NULL)
|
||||||
printf("error: could not open socket\n");
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
|
||||||
struct sockaddr_in server;
|
|
||||||
server.sin_addr.s_addr = inet_addr("127.0.0.1");
|
|
||||||
server.sin_family = AF_INET;
|
|
||||||
server.sin_port = htons(port);
|
|
||||||
if (bind(server_socket, (struct sockaddr*)&server, sizeof(server)) > 0) {
|
|
||||||
printf("error: could not bind socket\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if (listen(server_socket, SOMAXCONN) < 0) {
|
|
||||||
printf("error: could not listen on server");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
printf("listening on port %d\n", port);
|
printf("listening on port %d\n", port);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
struct sockaddr_in client;
|
|
||||||
socklen_t client_size;
|
|
||||||
printf("waiting for client...\n");
|
printf("waiting for client...\n");
|
||||||
int client_socket = accept(server_socket, (struct sockaddr*)&client, &client_size);
|
TcpConnection* client_connection = tcp_server_accept(server);
|
||||||
if (client_socket < 0) {
|
if (client_connection == NULL) {
|
||||||
printf("error: could not accept connection\n");
|
printf("error: could not accept client\n");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
printf("client connected\n");
|
printf("client connected\n");
|
||||||
while (true) {
|
while (true) {
|
||||||
uint8_t buffer[8192] = { 0 };
|
uint8_t buffer[8192] = { 0 };
|
||||||
ssize_t read_size = recv(client_socket, buffer, 8192, 0);
|
ssize_t recieved = tcp_recieve(client_connection, buffer, 8192);
|
||||||
if (read_size < 0) {
|
if (recieved < 0) {
|
||||||
printf("error: could not recieve\n");
|
printf("error: could not recieve\n");
|
||||||
close_socket(client_socket);
|
|
||||||
break;
|
break;
|
||||||
} else if (read_size == 0) {
|
} else if (recieved == 0) {
|
||||||
printf("client disconnected\n");
|
printf("client disconnected\n");
|
||||||
close_socket(client_socket);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
printf("=== recieved start ===\n%s\n=== recieved end ===\n", buffer);
|
|
||||||
|
|
||||||
HttpRequestHeader header
|
HttpRequestHeader header
|
||||||
= parse_http_request_header((char*)buffer, strlen((char*)buffer));
|
= parse_http_request_header((char*)buffer, strlen((char*)buffer));
|
||||||
printf("method: %d\n", header.method);
|
|
||||||
printf("path_index: %ld\n", header.path_index);
|
|
||||||
printf("path_length: %ld\n", header.path_length);
|
|
||||||
printf("content_length: %ld\n", header.content_length);
|
|
||||||
printf("body_index: %ld\n", header.body_index);
|
|
||||||
|
|
||||||
char* path = calloc(header.path_length + 1, sizeof(char));
|
char* path = calloc(header.path_length + 1, sizeof(char));
|
||||||
strncpy(path, (char*)&buffer[header.path_index], header.path_length);
|
strncpy(path, (char*)&buffer[header.path_index], header.path_length);
|
||||||
printf("path: %s\n", path);
|
|
||||||
|
|
||||||
if (strncmp(path, "/api", 4) == 0) {
|
if (strncmp(path, "/api", 4) == 0) {
|
||||||
// something something api
|
// something something api
|
||||||
} else {
|
} else {
|
||||||
@ -92,37 +64,35 @@ int main(void)
|
|||||||
"\r\n"
|
"\r\n"
|
||||||
"<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>Bad "
|
"<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>Bad "
|
||||||
"request</title></head><body><h1>Fuck you!</h1></body></html>\r\n";
|
"request</title></head><body><h1>Fuck you!</h1></body></html>\r\n";
|
||||||
ssize_t written = send(client_socket, send_buffer, sizeof(send_buffer), 0);
|
ssize_t written = tcp_send(client_connection, send_buffer, sizeof(send_buffer));
|
||||||
if (written < 0) {
|
if (written < 0) {
|
||||||
printf("error: could not write\n");
|
printf("error: could not write\n");
|
||||||
close_socket(client_socket);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else if (header.path_length == 0 || strncmp(path, "/", header.path_length) == 0) {
|
} else if (header.path_length == 0 || strncmp(path, "/", header.path_length) == 0) {
|
||||||
FILE* file = fopen("../frontend/index.html", "r");
|
FILE* file = fopen("../frontend/index.html", "r");
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
printf("error: could not open file\n");
|
printf("error: could not open file\n");
|
||||||
close_socket(client_socket);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
uint8_t send_buffer[] = "HTTP/1.1 200 OK\r\n"
|
uint8_t send_buffer[] = "HTTP/1.1 200 OK\r\n"
|
||||||
"Content-Type: text/html\r\n"
|
"Content-Type: text/html\r\n"
|
||||||
"\r\n";
|
"\r\n";
|
||||||
ssize_t written = send(client_socket, send_buffer, sizeof(send_buffer), 0);
|
ssize_t written = tcp_send(client_connection, send_buffer, sizeof(send_buffer));
|
||||||
if (written < 0) {
|
if (written < 0) {
|
||||||
printf("error: could not write\n");
|
printf("error: could not write\n");
|
||||||
close_socket(client_socket);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
char char_read;
|
char char_read;
|
||||||
while ((char_read = (char)fgetc(file)) != EOF) {
|
while ((char_read = (char)fgetc(file)) != EOF) {
|
||||||
write(client_socket, &char_read, sizeof(char));
|
tcp_send(client_connection, (uint8_t*)&char_read, sizeof(char));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
char rootpath[] = "../frontend";
|
char rootpath[] = "../frontend";
|
||||||
size_t filepath_size = sizeof(rootpath) + header.path_length + 1;
|
size_t filepath_size = sizeof(rootpath) + header.path_length + 1;
|
||||||
char* filepath = calloc(filepath_size, sizeof(char));
|
char* filepath = calloc(filepath_size, sizeof(char));
|
||||||
snprintf(filepath, filepath_size, "%s%s", rootpath, path);
|
snprintf(filepath, filepath_size, "%s%s", rootpath, path);
|
||||||
|
|
||||||
char* dot = strrchr(path, '.');
|
char* dot = strrchr(path, '.');
|
||||||
char mime_type[20] = { 0 };
|
char mime_type[20] = { 0 };
|
||||||
char file_flag[3] = { 'r', 0 };
|
char file_flag[3] = { 'r', 0 };
|
||||||
@ -132,6 +102,8 @@ int main(void)
|
|||||||
snprintf(mime_type, 20, "text/css");
|
snprintf(mime_type, 20, "text/css");
|
||||||
} else if (dot != NULL && strncmp(dot, ".js", 3) == 0) {
|
} else if (dot != NULL && strncmp(dot, ".js", 3) == 0) {
|
||||||
snprintf(mime_type, 20, "text/javascript");
|
snprintf(mime_type, 20, "text/javascript");
|
||||||
|
} else if (dot != NULL && strncmp(dot, ".map", 4) == 0) {
|
||||||
|
snprintf(mime_type, 20, "application/json");
|
||||||
} else if (dot != NULL && strncmp(dot, ".ico", 4) == 0) {
|
} else if (dot != NULL && strncmp(dot, ".ico", 4) == 0) {
|
||||||
snprintf(mime_type, 20, "image/x-icon");
|
snprintf(mime_type, 20, "image/x-icon");
|
||||||
} else if (dot != NULL && strncmp(dot, ".jpg", 4) == 0) {
|
} else if (dot != NULL && strncmp(dot, ".jpg", 4) == 0) {
|
||||||
@ -145,15 +117,12 @@ int main(void)
|
|||||||
file_flag[1] = 'b';
|
file_flag[1] = 'b';
|
||||||
} else {
|
} else {
|
||||||
printf("error: unknown file type\n");
|
printf("error: unknown file type\n");
|
||||||
close_socket(client_socket);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\n %s | %s \n", file_flag, filepath);
|
|
||||||
FILE* file = fopen(filepath, file_flag);
|
FILE* file = fopen(filepath, file_flag);
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
printf("error: could not open file\n");
|
printf("error: could not open file\n");
|
||||||
close_socket(client_socket);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,28 +130,26 @@ int main(void)
|
|||||||
"Content-Type: ";
|
"Content-Type: ";
|
||||||
char send_buffer_2[] = "\r\n"
|
char send_buffer_2[] = "\r\n"
|
||||||
"\r\n";
|
"\r\n";
|
||||||
ssize_t written
|
ssize_t written = tcp_send(
|
||||||
= send(client_socket, send_buffer_1, strlen(send_buffer_1) - 1, 0);
|
client_connection, (uint8_t*)send_buffer_1, strlen(send_buffer_1));
|
||||||
if (written < 0) {
|
if (written < 0) {
|
||||||
printf("error: could not write\n");
|
printf("error: could not write\n");
|
||||||
close_socket(client_socket);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
written = send(client_socket, mime_type, strlen(mime_type), 0);
|
written = tcp_send(client_connection, (uint8_t*)mime_type, strlen(mime_type));
|
||||||
if (written < 0) {
|
if (written < 0) {
|
||||||
printf("error: could not write\n");
|
printf("error: could not write\n");
|
||||||
close_socket(client_socket);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
written = send(client_socket, send_buffer_2, strlen(send_buffer_2), 0);
|
written = tcp_send(
|
||||||
|
client_connection, (uint8_t*)send_buffer_2, strlen(send_buffer_2));
|
||||||
if (written < 0) {
|
if (written < 0) {
|
||||||
printf("error: could not write\n");
|
printf("error: could not write\n");
|
||||||
close_socket(client_socket);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
int char_read;
|
int char_read;
|
||||||
while ((char_read = fgetc(file)) != EOF) {
|
while ((char_read = fgetc(file)) != EOF) {
|
||||||
write(client_socket, (char*)&char_read, sizeof(char));
|
tcp_send(client_connection, (uint8_t*)&char_read, sizeof(char));
|
||||||
}
|
}
|
||||||
|
|
||||||
free(filepath);
|
free(filepath);
|
||||||
@ -190,25 +157,11 @@ int main(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(path);
|
free(path);
|
||||||
|
|
||||||
/* uint8_t send_buffer[]
|
|
||||||
= "HTTP/1.1 200 OK\r\n"
|
|
||||||
"Content-Type: text/html\r\n"
|
|
||||||
"\r\n"
|
|
||||||
"<!DOCTYPE><html><head><meta charset=\"utf-8\"><title>hej med "
|
|
||||||
"dig</title></head><body><h1>fuck c hashtag</h1></body></html>\r\n"
|
|
||||||
"\r\n";
|
|
||||||
ssize_t written = send(client_socket, send_buffer, sizeof(send_buffer), 0);
|
|
||||||
if (written < 0) {
|
|
||||||
printf("error: could not write\n");
|
|
||||||
close_socket(client_socket);
|
|
||||||
break;
|
break;
|
||||||
} */
|
}
|
||||||
printf("disconnecting client\n");
|
printf("disconnecting client\n");
|
||||||
close_socket(client_socket);
|
tcp_connection_destroy(client_connection);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
tcp_server_destroy(server);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
#ifdef _WIN32
|
|
||||||
#include <WinSock2.h>
|
|
||||||
#include <WS2tcpip.h>
|
|
||||||
#include <BaseTsd.h>
|
|
||||||
typedef SSIZE_T ssize_t;
|
|
||||||
#else
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void init_sockets(void);
|
|
||||||
|
|
||||||
void close_socket(int socket);
|
|
||||||
|
|
32
backend/tcp.h
Normal file
32
backend/tcp.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#ifndef TCP_H
|
||||||
|
#define TCP_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <BaseTsd.h>
|
||||||
|
typedef SSIZE_T ssize_t;
|
||||||
|
#else
|
||||||
|
#include <sys/types.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void tcp_global_initialize_sockets(void);
|
||||||
|
|
||||||
|
typedef struct TcpServer TcpServer;
|
||||||
|
typedef struct TcpConnection TcpConnection;
|
||||||
|
|
||||||
|
// returns NULL on errors, and prints the error
|
||||||
|
TcpServer* tcp_server_create(const char* ip, uint16_t port);
|
||||||
|
void tcp_server_destroy(TcpServer* server);
|
||||||
|
|
||||||
|
// returns NULL on errors, and prints the error
|
||||||
|
TcpConnection* tcp_server_accept(TcpServer* server);
|
||||||
|
void tcp_connection_destroy(TcpConnection* connection);
|
||||||
|
|
||||||
|
// returns amount transmittet >0 on success, ==0 if client was disconnected, and <0 on error
|
||||||
|
ssize_t tcp_recieve(TcpConnection* connection, uint8_t* data, size_t amount);
|
||||||
|
// returns amount transmittet >0 on success, ==0 if client was disconnected, and <0 on error
|
||||||
|
ssize_t tcp_send(TcpConnection* connection, uint8_t* data, size_t amount);
|
||||||
|
|
||||||
|
#endif
|
@ -1,13 +1,21 @@
|
|||||||
#include "native.h"
|
#include "tcp.h"
|
||||||
|
#include <BaseTsd.h>
|
||||||
|
#include <WS2tcpip.h>
|
||||||
|
#include <WinSock2.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
void init_sockets(void)
|
struct TcpServer { };
|
||||||
{
|
|
||||||
WSADATA data;
|
|
||||||
WSAStartup(0x0202, &data);
|
|
||||||
}
|
|
||||||
|
|
||||||
void close_socket(int socket)
|
struct TcpConnection { };
|
||||||
{
|
|
||||||
closesocket(socket);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
void tcp_global_initialize_sockets(void) { }
|
||||||
|
|
||||||
|
TcpServer* tcp_server_create(const char* ip, uint16_t port) { }
|
||||||
|
|
||||||
|
void tcp_connection_destroy(TcpConnection* connection) { }
|
||||||
|
|
||||||
|
ssize_t tcp_recieve(TcpConnection* connection, uint8_t* data, size_t amount) { }
|
||||||
|
|
||||||
|
ssize_t tcp_send(TcpConnection* connection, uint8_t* data, size_t amount) { }
|
||||||
|
Loading…
Reference in New Issue
Block a user