commit 2b86380ff43262cb8bb2569d026f47be6a03d16e Author: Dm1tr1y147 Date: Mon Jun 22 15:59:38 2020 +0500 Initial commit: created project structure, makefile, some static files for future server tests and interphase to create a listening socket diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..08d8ba5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +bin/ +obj/ +tmp/ +.vscode/ \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5ff6e30 --- /dev/null +++ b/Makefile @@ -0,0 +1,32 @@ +CPPFLAGS := -Iinclude -MMD -MP +CFLAGS := -Wall +LDFLAGS := -Llib +LDLIBS := -lm + +SRC_DIR := src +OBJ_DIR := obj +BIN_DIR := bin + +EXE := $(BIN_DIR)/server + +SRC := $(wildcard $(SRC_DIR)/*.c) + +OBJ := $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) + +all: $(EXE) + +$(EXE): $(OBJ) | $(BIN_DIR) + $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR) + $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ + +$(BIN_DIR) $(OBJ_DIR): + mkdir -p $@ + +clean: + @$(RM) -rv $(BIN_DIR) $(OBJ_DIR) + +-include $(OBJ:.o=.d) + +.PHONY: all, clean \ No newline at end of file diff --git a/include/netw.h b/include/netw.h new file mode 100644 index 0000000..8896080 --- /dev/null +++ b/include/netw.h @@ -0,0 +1,2 @@ +int get_listener_socket(char *port); +void *get_in_addr(struct sockaddr *sa); \ No newline at end of file diff --git a/include/utils.h b/include/utils.h new file mode 100644 index 0000000..fe6de80 --- /dev/null +++ b/include/utils.h @@ -0,0 +1 @@ +void err_msg(char *msg); \ No newline at end of file diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..a9c1859 --- /dev/null +++ b/readme.txt @@ -0,0 +1 @@ +Webserver in c. !Experimental! \ No newline at end of file diff --git a/src/netw.c b/src/netw.c new file mode 100644 index 0000000..5a822ff --- /dev/null +++ b/src/netw.c @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include + +#include "../include/utils.h" + +#define BACKLOG 10 + +/** + * @brief Get the listener socket object + * + * @param {char *} port + * @return int + */ +int get_listener_socket(char *port) +{ + int sockfd; + struct addrinfo hints, *servinfo, *p; + int yes = 1; + + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_PASSIVE; + + if (getaddrinfo(NULL, port, &hints, &servinfo) != 0) + { + err_msg("can't get addres info"); + return -1; + } + + for (p = servinfo; p != NULL; p = p->ai_next) + { + if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0) + { + err_msg("can't create socket"); + continue; + } + + if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) < 0) + { + err_msg("can't set socket options"); + close(sockfd); + freeaddrinfo(servinfo); + return -2; + } + + if (bind(sockfd, p->ai_addr, p->ai_addrlen) < 0) + { + close(sockfd); + err_msg("can't bind socket"); + continue; + } + + break; + } + + freeaddrinfo(servinfo); + + if (p == NULL) + { + err_msg("failed to find local adress"); + return -3; + } + + if (listen(sockfd, BACKLOG) < 0) + { + err_msg("failed to listen"); + return -4; + } + + return sockfd; +} + +void *get_in_addr(struct sockaddr *sa) +{ + if (sa->sa_family == AF_INET) { + return &(((struct sockaddr_in*)sa)->sin_addr); + } + + return &(((struct sockaddr_in6*)sa)->sin6_addr); +} \ No newline at end of file diff --git a/src/server.c b/src/server.c new file mode 100644 index 0000000..05267d7 --- /dev/null +++ b/src/server.c @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../include/netw.h" +#include "../include/utils.h" +/** + * Main + */ +int main(int argc, char *argv[]) +{ + int client_fd; + struct sockaddr_storage cli_addr; + char s[INET6_ADDRSTRLEN]; + + if (argc != 2) + { + err_msg("Usage: \n"); + exit(1); + } + char *port = argv[1]; + + int listenfd = get_listener_socket(port); + if (listenfd < 0) + { + err_msg("!fatal! couldn't create listening socket"); + exit(1); + } + printf("Waiting for connection on port %s...\n", port); + + while (1) + { + socklen_t sin_size = sizeof cli_addr; + + client_fd = accept(listenfd, (struct sockaddr *)&cli_addr, &sin_size); + if (client_fd < 0) + { + err_msg("couldn't accept client connection"); + continue; + } + + // | Doesn't work properly yet + // v + + int pid = fork(); + + if (pid < 0) + err_msg("fork failed"); + else if (pid == 0) + { + close(listenfd); + + inet_ntop(cli_addr.ss_family, get_in_addr((struct sockaddr *)&cli_addr), s, sizeof s); + printf("Got connection from %s\n", s); + + // connection(client_fd); // Implement + exit(0); + } + else + close(client_fd); + } + + return 0; +} \ No newline at end of file diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..734dfc6 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,10 @@ +#include + +/** + * @brief Prints error + * + * @param {char *} msg +*/ +void err_msg (char *msg) { + fprintf(stderr, "Error: %s\n", msg); +} \ No newline at end of file diff --git a/static/404.html b/static/404.html new file mode 100644 index 0000000..234e744 --- /dev/null +++ b/static/404.html @@ -0,0 +1,13 @@ + + + + + 404 Not Found + + + +

Not Found

+

The requested URL was not found on this server.

+ + + \ No newline at end of file diff --git a/static/about/index.html b/static/about/index.html new file mode 100644 index 0000000..33001dc --- /dev/null +++ b/static/about/index.html @@ -0,0 +1,16 @@ + + + + + + + About + + + + Back +

Me

+ the pig + + + \ No newline at end of file diff --git a/static/about/me.jpg b/static/about/me.jpg new file mode 100644 index 0000000..041d8ca Binary files /dev/null and b/static/about/me.jpg differ diff --git a/static/favicon.ico b/static/favicon.ico new file mode 100644 index 0000000..870d9e9 Binary files /dev/null and b/static/favicon.ico differ diff --git a/static/index.html b/static/index.html new file mode 100644 index 0000000..e969b51 --- /dev/null +++ b/static/index.html @@ -0,0 +1,12 @@ + + + + + + Test + + +

Testing paragraf

+ About me + + \ No newline at end of file