implementation of registration (not finished)
This commit is contained in:
+7
-36
@@ -3,12 +3,12 @@
|
||||
#include <netinet/in.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
+134
-1
@@ -1,13 +1,26 @@
|
||||
#include "main.h"
|
||||
#include "ui.h"
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <termios.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 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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user