implementation of registration (not finished)

This commit is contained in:
2025-11-07 17:42:59 +02:00
parent 3ef6ff6c41
commit 2336b7bb01
7 changed files with 178 additions and 38 deletions
+3 -1
View File
@@ -68,4 +68,6 @@ build
.vscode .vscode
#Downloads contents #Downloads contents
Downloads Downloads
deps.mk
+7 -36
View File
@@ -3,12 +3,12 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <sys/select.h> #include <sys/select.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
#include <termios.h> #include <termios.h>
#include <unistd.h> #include <unistd.h>
#include <string.h>
void connect_to_server(int sd, params_t *params) { void connect_to_server(int sd, params_t *params) {
struct sockaddr_in server; struct sockaddr_in server;
@@ -43,46 +43,17 @@ void clear_params(params_t *params) {
void get_missing_params(params_t *params) { void get_missing_params(params_t *params) {
size_t lsize; size_t lsize;
char *buf = NULL; char *bufptr = NULL;
struct termios ts_hide, ts_show;
struct sockaddr_in addr;
if (isatty(0)) { struct sockaddr_in addr;
tcgetattr(0, &ts_show);
memcpy(&ts_hide, &ts_show, sizeof(ts_show));
ts_hide.c_lflag &= ~ECHO;
}
if (!params->addr) { if (!params->addr) {
printf("host> "); printf("host> ");
getline(&buf, &lsize, stdin); getline(&bufptr, &lsize, stdin);
inet_aton(buf, &(addr.sin_addr)); inet_aton(bufptr, &(addr.sin_addr));
params->addr = addr.sin_addr.s_addr; params->addr = addr.sin_addr.s_addr;
free(buf); free(bufptr);
buf = NULL; bufptr = 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;
} }
} }
+2
View File
@@ -17,6 +17,7 @@ typedef struct params {
enum state { enum state {
WAIT_SERVER_INIT, WAIT_SERVER_INIT,
WAIT_SERVER, WAIT_SERVER,
WAIT_REGISTER,
WAIT_CLIENT, WAIT_CLIENT,
UPLOAD_FILE, UPLOAD_FILE,
STATE_FILE_LIST, STATE_FILE_LIST,
@@ -25,6 +26,7 @@ enum state {
STATE_UPLOAD_PARAMS, STATE_UPLOAD_PARAMS,
STATE_UPLOAD_FILE, STATE_UPLOAD_FILE,
STATE_UPLOAD_REQUESTED, STATE_UPLOAD_REQUESTED,
STATE_ASK_USER_BEFORE_LOGIN,
ERR, ERR,
}; };
typedef struct p_file { typedef struct p_file {
+3
View File
@@ -100,6 +100,7 @@ int process_query(query_args_t *query_args, file_args_t *file_args) {
case WAIT_SERVER: case WAIT_SERVER:
case WAIT_SERVER_INIT: case WAIT_SERVER_INIT:
case WAIT_CLIENT: case WAIT_CLIENT:
case WAIT_REGISTER:
wait_side(query_args); wait_side(query_args);
break; break;
case UPLOAD_FILE: 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; query_args->state = WAIT_CLIENT;
} }
break; break;
case STATE_ASK_USER_BEFORE_LOGIN:
break;
} }
return 0; return 0;
} }
+134 -1
View File
@@ -1,13 +1,26 @@
#include "main.h" #include "main.h"
#include "ui.h"
#include <arpa/inet.h>
#include <fcntl.h> #include <fcntl.h>
#include <netinet/in.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <termios.h>
#include <unistd.h> #include <unistd.h>
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 process_server_command(char *line, int l_len, query_args_t *q_args) {
int ws_pos = l_len; int ws_pos = l_len;
params_t *params = q_args->params; 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, ' '); char *cptr = strchr(line, ' ');
if (cptr != NULL && cptr > 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 */ /* LOGIN */
if (!strncmp(line, "login>", ws_pos)) { 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)); write(q_args->sd, params->uname, strlen(params->uname));
return 0; return 0;
} }
/* PASSWORD */ /* PASSWORD */
if (!strncmp(line, "password>", ws_pos)) { if (!strncmp(line, "password>", ws_pos)) {
if (q_args->params->pass == NULL) {
return 1;
}
write(q_args->sd, params->pass, strlen(params->pass)); write(q_args->sd, params->pass, strlen(params->pass));
return 0; return 0;
} }
@@ -43,6 +83,7 @@ int process_server_command(char *line, int l_len, query_args_t *q_args) {
/* WELCOME MES */ /* WELCOME MES */
if (!strncmp(line, "Welcome, ", ws_pos)) { if (!strncmp(line, "Welcome, ", ws_pos)) {
printf("%s\n", line);
print_prompt(q_args->params); print_prompt(q_args->params);
q_args->state = WAIT_CLIENT; q_args->state = WAIT_CLIENT;
return 0; 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) { 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); sscanf(line, "%s %s %zu", command, fname, &fsize);
char buf[INBUFSIZE]; char buf[INBUFSIZE];
int rlen; int rlen;
@@ -85,4 +128,94 @@ static void file_receive(int sd, char *line) {
} }
close(file_d); 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;
}
} }
+26
View File
@@ -0,0 +1,26 @@
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
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;
}
+3
View File
@@ -0,0 +1,3 @@
#include <stdint.h>
uint32_t print_ask_list(char *title, const char *options[]);