From 11714ed6c9adc5b6f90bffa1b23d5bb5b2e20bd5 Mon Sep 17 00:00:00 2001 From: Ivan Davydov Date: Sun, 29 Jun 2025 00:08:38 +0300 Subject: Iuj ŝanĝoj... MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CREDITS | 5 +++-- Makefile | 3 ++- acccre-cmdparser.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ acccre-server.c | 22 +++++++++++++------ acccre-server.h | 8 ++++++- client.py | 13 ++++++------ 6 files changed, 96 insertions(+), 17 deletions(-) create mode 100644 acccre-cmdparser.c diff --git a/CREDITS b/CREDITS index 115c90e..cf5543d 100644 --- a/CREDITS +++ b/CREDITS @@ -1,4 +1,5 @@ Server ====== -* https://github.com/JeffreytheCoder/Simple-HTTP-Server/blob/master/server.c -* https://codereview.stackexchange.com/questions/284179/proper-implementation-of-signal-handler-and-multithreading-pthread +* Starting point - +* Combined with previous item - +* Request parser - diff --git a/Makefile b/Makefile index 796bed8..cb84ff2 100644 --- a/Makefile +++ b/Makefile @@ -3,8 +3,9 @@ CC = tcc CFLAGS = -Wall #CPPFLAGS = -D UNIX LDLIBS = -lpthread -lrt +FILES = acccre-server.o #acccre-cmdparser.o all: acccre-server -acccre-server: acccre-server.o +acccre-server: $(FILES) clean: rm -f *.o acccre-server diff --git a/acccre-cmdparser.c b/acccre-cmdparser.c new file mode 100644 index 0000000..4212296 --- /dev/null +++ b/acccre-cmdparser.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include + +/* TODO */ +static int run_cmd(const char *cmd, int argc, char *argv[]) { + puts(cmd); + for (int i = 0; i < argc; i++) { + puts(argv[i]); + } + + return 0; +} + +/* Tricks with pointers */ +int parse(char *str) { + char *cmd = str; + str += strcspn(str, separator); + if (*str) { + *str++ = 0; + } + + char *str_counter = str; + int i = 0; + for (; *str_counter; i++) { + str_counter += strspn(str, separator); + if (!*str_counter) { + break; + } + *str_counter++ = 0; + + str_counter += strcspn(str_counter, separator); + if(!str) { + return EINVAL; + } + *str++ = 0; + } + + char *args[i]; + for (int j = 0; *str; j++) { + str += strspn(str, separator); + if (!*str) { + break; + } + *str++ = 0; + + args[j] = str; + str += strcspn(str, separator); + if(!str) { + return EINVAL; + } + *str++ = 0; + + /* TODO: parse the string left to an array of arguments and pass command + * itself and arguments to the run_cmd() right after end of this loop + */ + } + + int response = run_cmd(cmd, i, args); + return response; +} diff --git a/acccre-server.c b/acccre-server.c index a3c1896..a645b93 100644 --- a/acccre-server.c +++ b/acccre-server.c @@ -1,5 +1,6 @@ #ifdef __unix__ #include "acccre-server.h" +#include "acccre-cmdparser.c" #include #include @@ -10,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -24,17 +26,21 @@ volatile sig_atomic_t sig_flag = 0; static void sig_handler(int signo) { char *sig_name = strsignal(signo); char *sig_msg = " signal is caught\n"; - write(STDOUT_FILENO, sig_name, strlen(sig_name)); - write(STDOUT_FILENO, sig_msg, strlen(sig_msg)); + char *msg = malloc(strlen(sig_name) + strlen(sig_msg) + 1); + strcat(strcpy(msg, sig_name), sig_msg); + write(STDOUT_FILENO, msg, strlen(msg)); + free(msg); + //write(STDOUT_FILENO, sig_name, strlen(sig_name)); + //write(STDOUT_FILENO, sig_msg, strlen(sig_msg)); sig_flag = 1; } -void *process(void *arg) { +static void *process(void *arg) { int client_fd = *((int *)arg); char *buf = malloc(sizeof(acccre_size_t)); /* TODO: make a protocol */ - while (sig_flag == 0) { + //while (sig_flag == 0) { if (recv(client_fd, buf, sizeof(acccre_size_t), 0) > 0) { /* Now, parse the request... */ acccre_size_t msg_len = *buf; @@ -42,8 +48,10 @@ void *process(void *arg) { buf = realloc(buf, msg_len); recv(client_fd, buf, msg_len, 0); - char *msg = buf; - printf("%s\n", msg); + + int response = parse(buf); + send(client_fd, &response, sizeof(response), 0); + /* printf("%s\n", msg); */ /* ... char *response = (char *)malloc(BUFFER_SIZE * sizeof(char)); @@ -52,7 +60,7 @@ void *process(void *arg) { size_t response_len;*/ /* ... */ } - } + //} close(client_fd); free(arg); diff --git a/acccre-server.h b/acccre-server.h index d030c6b..5db8b24 100644 --- a/acccre-server.h +++ b/acccre-server.h @@ -20,9 +20,15 @@ const int sigs[] = { SIGXFSZ }; +int parse(char *str); + +void *reg(void *args); +void *chpasswd(void *args); +void *nop(void *args); + #define BUFFER_SIZE 100 #define DEFAULT_PATH "./acccre.sock" /* Taken from https://pubs.opengroup.org/onlinepubs/9799919799/ */ #define MAX_SUN_PATH sizeof(((struct sockaddr_un *)0)->sun_path) typedef uint16_t acccre_size_t; - +const char *separator = "\t"; diff --git a/client.py b/client.py index 26548c2..0c0fcfc 100755 --- a/client.py +++ b/client.py @@ -15,13 +15,14 @@ except FileNotFoundError as e: exit(e) if len(argv) >= 3 and argv[2]: - msg = argv[2].encode("ascii") + cmd = argv[2].encode("ascii") else: - msg = b"hello, world!" + cmd = b"hello" # 1 char in Python is 2 bytes instead of 1 byte in C -#msg_len = acccre_size(getsizeof(msg)) -msg_len = acccre_size(2) -sun.send(msg_len) -sun.send(msg) +cmd += b"\tserver\tworld\thello" +cmd_len = acccre_size(getsizeof(cmd)) +sun.send(cmd_len) +sun.send(cmd) +print(str(sun.recv(5))) sun.close -- cgit v1.2.3