From 2336b7bb0150933a333547d3075ae262d3fc7dbc Mon Sep 17 00:00:00 2001 From: Alexander Zhylin Date: Fri, 7 Nov 2025 17:42:59 +0200 Subject: [PATCH] implementation of registration (not finished) --- .gitignore | 4 +- src/connection.c | 43 +++------------ src/main.h | 2 + src/query.c | 3 ++ src/server.c | 135 ++++++++++++++++++++++++++++++++++++++++++++++- src/ui.c | 26 +++++++++ src/ui.h | 3 ++ 7 files changed, 178 insertions(+), 38 deletions(-) create mode 100644 src/ui.c create mode 100644 src/ui.h diff --git a/.gitignore b/.gitignore index a31f63b..ea1fc4e 100644 --- a/.gitignore +++ b/.gitignore @@ -68,4 +68,6 @@ build .vscode #Downloads contents -Downloads \ No newline at end of file +Downloads + +deps.mk \ No newline at end of file diff --git a/src/connection.c b/src/connection.c index 4526aa9..311feb1 100644 --- a/src/connection.c +++ b/src/connection.c @@ -3,12 +3,12 @@ #include #include #include +#include #include #include #include #include #include -#include void connect_to_server(int sd, params_t *params) { struct sockaddr_in server; @@ -43,46 +43,17 @@ void clear_params(params_t *params) { void get_missing_params(params_t *params) { size_t lsize; - char *buf = NULL; - struct termios ts_hide, ts_show; - struct sockaddr_in addr; + char *bufptr = NULL; - if (isatty(0)) { - tcgetattr(0, &ts_show); - memcpy(&ts_hide, &ts_show, sizeof(ts_show)); - ts_hide.c_lflag &= ~ECHO; - } + struct sockaddr_in addr; if (!params->addr) { printf("host> "); - getline(&buf, &lsize, stdin); - inet_aton(buf, &(addr.sin_addr)); + getline(&bufptr, &lsize, stdin); + inet_aton(bufptr, &(addr.sin_addr)); params->addr = addr.sin_addr.s_addr; - free(buf); - buf = NULL; - } - if (params->uname == NULL) { - printf("login> "); - getline(&(params->uname), &lsize, stdin); - lsize = strlen(params->uname); - params->uname[lsize - 1] = '\0'; /* get rid of '\n' */ - free(buf); - buf = NULL; - } - if (params->pass == NULL) { - if (isatty(0)) { - tcsetattr(0, TCSANOW, &ts_hide); - } - printf("password> "); - getline(&(params->pass), &lsize, stdin); - if (isatty(0)) { - tcsetattr(0, TCSANOW, &ts_show); - putchar('\n'); - } - lsize = strlen(params->pass); - params->pass[lsize - 1] = '\0'; /* get rid of '\n' */ - free(buf); - buf = NULL; + free(bufptr); + bufptr = NULL; } } diff --git a/src/main.h b/src/main.h index 33b3d8f..adbbc24 100644 --- a/src/main.h +++ b/src/main.h @@ -17,6 +17,7 @@ typedef struct params { enum state { WAIT_SERVER_INIT, WAIT_SERVER, + WAIT_REGISTER, WAIT_CLIENT, UPLOAD_FILE, STATE_FILE_LIST, @@ -25,6 +26,7 @@ enum state { STATE_UPLOAD_PARAMS, STATE_UPLOAD_FILE, STATE_UPLOAD_REQUESTED, + STATE_ASK_USER_BEFORE_LOGIN, ERR, }; typedef struct p_file { diff --git a/src/query.c b/src/query.c index 1cd17b7..4d0a4c1 100644 --- a/src/query.c +++ b/src/query.c @@ -100,6 +100,7 @@ int process_query(query_args_t *query_args, file_args_t *file_args) { case WAIT_SERVER: case WAIT_SERVER_INIT: case WAIT_CLIENT: + case WAIT_REGISTER: wait_side(query_args); break; case UPLOAD_FILE: @@ -135,6 +136,8 @@ int process_query(query_args_t *query_args, file_args_t *file_args) { query_args->state = WAIT_CLIENT; } break; + case STATE_ASK_USER_BEFORE_LOGIN: + break; } return 0; } diff --git a/src/server.c b/src/server.c index abb9177..f9a597b 100644 --- a/src/server.c +++ b/src/server.c @@ -1,13 +1,26 @@ #include "main.h" +#include "ui.h" +#include #include +#include +#include #include #include #include +#include +#include +#include #include +void ask_uname_and_password(params_t *params); +void ask_register(params_t *params); + int process_server_command(char *line, int l_len, query_args_t *q_args) { int ws_pos = l_len; params_t *params = q_args->params; + const char *login_options[] = {"Username", "Anonymous", "Register", NULL}; + uint32_t answer; + char r_buf[INBUFSIZE]; char *cptr = strchr(line, ' '); if (cptr != NULL && cptr > line) @@ -15,12 +28,39 @@ int process_server_command(char *line, int l_len, query_args_t *q_args) { /* LOGIN */ if (!strncmp(line, "login>", ws_pos)) { + if (params->uname == NULL) { + fflush(stdout); + answer = print_ask_list("Select login type:", login_options); + switch (answer) { + case 1: + ask_uname_and_password(q_args->params); + break; + case 2: + params->uname = malloc(sizeof("anonymous")); + strcpy(params->uname, "anonymous"); + q_args->state = WAIT_CLIENT; + break; + case 3: + write(q_args->sd, "register\n", sizeof "register\n"); + q_args->state = WAIT_REGISTER; + ask_register(q_args->params); + sprintf(r_buf, "%s %s\n", params->uname, params->pass); + write(q_args->sd, r_buf, strlen(r_buf)); + return 3; + default: + return -1; + } + } write(q_args->sd, params->uname, strlen(params->uname)); + return 0; } /* PASSWORD */ if (!strncmp(line, "password>", ws_pos)) { + if (q_args->params->pass == NULL) { + return 1; + } write(q_args->sd, params->pass, strlen(params->pass)); return 0; } @@ -43,6 +83,7 @@ int process_server_command(char *line, int l_len, query_args_t *q_args) { /* WELCOME MES */ if (!strncmp(line, "Welcome, ", ws_pos)) { + printf("%s\n", line); print_prompt(q_args->params); q_args->state = WAIT_CLIENT; return 0; @@ -63,7 +104,9 @@ int process_server_command(char *line, int l_len, query_args_t *q_args) { } static void file_receive(int sd, char *line) { - char fname[128]; char command[32]; size_t fsize; + char fname[128]; + char command[32]; + size_t fsize; sscanf(line, "%s %s %zu", command, fname, &fsize); char buf[INBUFSIZE]; int rlen; @@ -85,4 +128,94 @@ static void file_receive(int sd, char *line) { } close(file_d); +} + +void ask_uname_and_password(params_t *params) { + size_t lsize; + char *bufptr = NULL; + + struct termios ts_hide, ts_show; + + if (isatty(0)) { + tcgetattr(0, &ts_show); + memcpy(&ts_hide, &ts_show, sizeof(ts_show)); + ts_hide.c_lflag &= ~ECHO; + } + + if (params->uname == NULL) { + printf("login> "); + fflush(stdout); + getline(&(params->uname), &lsize, stdin); + lsize = strlen(params->uname); + params->uname[lsize - 1] = '\0'; /* get rid of '\n' */ + } + + if (params->pass == NULL) { + if (isatty(0)) { + tcsetattr(0, TCSANOW, &ts_hide); + } + printf("password> "); + getline(&(params->pass), &lsize, stdin); + if (isatty(0)) { + tcsetattr(0, TCSANOW, &ts_show); + putchar('\n'); + } + lsize = strlen(params->pass); + params->pass[lsize - 1] = '\0'; /* get rid of '\n' */ + free(bufptr); + bufptr = NULL; + } +} + +void ask_register(params_t *params) { + size_t lsize; + char *bufptr = NULL; + + struct termios ts_hide, ts_show; + + if (isatty(0)) { + tcgetattr(0, &ts_show); + memcpy(&ts_hide, &ts_show, sizeof(ts_show)); + ts_hide.c_lflag &= ~ECHO; + } + + if (params->uname == NULL) { + printf("username> "); + fflush(stdout); + getline(&(params->uname), &lsize, stdin); + lsize = strlen(params->uname); + params->uname[lsize - 1] = '\0'; /* get rid of '\n' */ + free(bufptr); + bufptr = NULL; + } + + if (params->pass == NULL) { + if (isatty(0)) { + tcsetattr(0, TCSANOW, &ts_hide); + } + do { + if (params->pass != NULL) { + printf("\nERROR: password do not match! Try again!\n"); + free(params->pass); + params->pass = NULL; + } + if (bufptr != NULL) { + free(bufptr); + bufptr = NULL; + } + printf("password> "); + getline(&(params->pass), &lsize, stdin); + printf("\npassword (repeat)> "); + getline(&(bufptr), &lsize, stdin); + } while(strcmp(bufptr, params->pass)); + + if (isatty(0)) { + tcsetattr(0, TCSANOW, &ts_show); + putchar('\n'); + } + lsize = strlen(params->pass); + params->pass[lsize - 1] = '\0'; /* get rid of '\n' */ + free(bufptr); + bufptr = NULL; + } } \ No newline at end of file diff --git a/src/ui.c b/src/ui.c new file mode 100644 index 0000000..836650d --- /dev/null +++ b/src/ui.c @@ -0,0 +1,26 @@ +#include +#include +#include + +uint32_t print_ask_list(char title[], const char *options[]) { + char *option; + char c; + uint32_t i, answer; + + printf("%s\n", title); + + for (i = 0; options[i] != NULL; i++) { + printf("[%u] %s\n", i + 1, options[i]); + } + + write(STDOUT_FILENO, "> ", sizeof "> "); + + while (!scanf("%u", &answer) || answer > i) { + printf("Input format is not correct! You need to specify between 1 and " + "%u\n > ", + i); + } + while ((c = getchar()) != '\n' && c != EOF); + + return answer; +} \ No newline at end of file diff --git a/src/ui.h b/src/ui.h new file mode 100644 index 0000000..9cc68e4 --- /dev/null +++ b/src/ui.h @@ -0,0 +1,3 @@ +#include + +uint32_t print_ask_list(char *title, const char *options[]); \ No newline at end of file