Skip to content

Commit

Permalink
cmd line arguments added
Browse files Browse the repository at this point in the history
  • Loading branch information
High-Voltaged committed Feb 5, 2022
1 parent 545e4e0 commit 4583f29
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 49 deletions.
7 changes: 4 additions & 3 deletions client/inc/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
unsigned long get_current_time();
char* get_string_time(unsigned long seconds);
t_avatar_color get_avatar_color();
void handle_error(const char* error);

//GUI UTILS
void on_crossing (GtkWidget *widget, GdkEventCrossing *event);
Expand Down Expand Up @@ -155,12 +156,12 @@ t_response_code get_response_code(cJSON* json);
t_request_type get_request_type(cJSON* json);
void update_last_chat_msg(t_chat* chat_to_update, t_msg* new_msg);

void handle_get_user_image(int user_id, char** avatar_path);
void handle_update_user_image(char *path);
t_response_code handle_get_user_image(int user_id, char** avatar_path);
t_response_code handle_update_user_image(char *path);

void client_init(int server_fd, SSL *ssl, SSL_CTX* ctx);
void client_cleanup(bool is_client_exit);
void connect_to_server(int port, int* server_fd, SSL_CTX **ctx, SSL **ssl);
void connect_to_server(const char* ip_address, int port, int* server_fd, SSL_CTX **ctx, SSL **ssl);
void handle_arg_errors(char** argv);

void init_ssl(SSL_CTX **ctx);
Expand Down
2 changes: 1 addition & 1 deletion client/src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ int main(int argc, char **argv) {
SSL_CTX *ctx = NULL;
SSL *ssl = NULL;

connect_to_server(atoi(argv[1]), &server_socket, &ctx, &ssl);
connect_to_server(argv[1], atoi(argv[1]), &server_socket, &ctx, &ssl);
client_init(server_socket, ssl, ctx);

gtk_init(&argc, &argv);
Expand Down
19 changes: 14 additions & 5 deletions client/src/client_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ void client_init(int server_fd, SSL *ssl, SSL_CTX* ctx) {
// Check if command line arguments are valid
void handle_arg_errors(char** argv) {

if (argv[1] == NULL /* || argv[2] == NULL*/) {
if (argv[1] == NULL /*|| argv[2] == NULL*/) {
mx_printerr("usage: ./uchat [ip] [port]\n");
pthread_exit((void*)EXIT_FAILURE);
exit(EXIT_FAILURE);
}

}

// Establish a connection with the server via the port
void connect_to_server(int port, int* server_fd, SSL_CTX **ctx, SSL **ssl) {
void connect_to_server(const char* ip_address, int port, int* server_fd, SSL_CTX **ctx, SSL **ssl) {

struct sockaddr_in server_addr;

Expand All @@ -46,12 +46,12 @@ void connect_to_server(int port, int* server_fd, SSL_CTX **ctx, SSL **ssl) {
server_addr.sin_port = htons(port);

if ((*server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
logger(strerror(errno), ERROR_LOG);
handle_error(strerror(errno));
exit(EXIT_FAILURE);
}

if (connect(*server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
logger(strerror(errno), ERROR_LOG);
handle_error(strerror(errno));
exit(EXIT_FAILURE);
}

Expand All @@ -78,3 +78,12 @@ char* get_log_name() {
return log_name;

}

void handle_error(const char* error) {

char* err_str = mx_strjoin(error, "\n");
mx_printerr(err_str);
logger(err_str, ERROR_LOG);
mx_strdel(&err_str);

}
11 changes: 6 additions & 5 deletions client/src/request_handlers/get_user_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static char* get_file_path_for(int user_id) {

}

void handle_get_user_image(int user_id, char** avatar_path) {
t_response_code handle_get_user_image(int user_id, char** avatar_path) {

// Send request for receiving user avatar
cJSON *json = cJSON_CreateObject();
Expand All @@ -38,7 +38,7 @@ void handle_get_user_image(int user_id, char** avatar_path) {
if ((fp = fopen(file_path, "wb+")) == NULL) {
printf("Cannot open image file\n");
mx_strdel(&file_path);
return;
return R_FILE_ERROR;
}

// Reciving len of encoded image
Expand All @@ -47,7 +47,7 @@ void handle_get_user_image(int user_id, char** avatar_path) {
if(recv(utils->server_fd, &len_encoded, sizeof(int), 0) == 0) {
printf("Error while receiving length\n");
mx_strdel(&file_path);
return;
return R_FILE_ERROR;
}

// Reciving encoded image
Expand All @@ -66,17 +66,18 @@ void handle_get_user_image(int user_id, char** avatar_path) {
if (ferror(fp)) {
printf("fwrite() failed\n");
mx_strdel(&file_path);
return;
return R_FILE_ERROR;
}
int r;
if ((r = fclose(fp)) == EOF) {
printf("Cannot close file handler\n");
mx_strdel(&file_path);
return;
return R_FILE_ERROR;
}

*avatar_path = mx_strdup(file_path);
mx_strdel(&file_path);
free(decoded);
return R_SUCCESS;

}
18 changes: 12 additions & 6 deletions client/src/request_handlers/update_user_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ void send_image_to_server(int *socket, void *buffer, size_t length) {
}
}

void handle_update_user_image(char *path) {
t_response_code handle_update_user_image(char *path) {

// Send request for saving user avatar
cJSON *json = cJSON_CreateObject();
Expand All @@ -25,7 +25,7 @@ void handle_update_user_image(char *path) {
FILE *fp;
if((fp = fopen(path, "rb")) == NULL) {
printf("Cannot open file\n");
return;
return R_FILE_ERROR;
}
int r;

Expand All @@ -35,15 +35,17 @@ void handle_update_user_image(char *path) {
printf("fseek() failed\n");
if ((r = fclose(fp)) == EOF) {
printf("Cannot close file\n");
}
}
return R_FILE_ERROR;
}

long flen = ftell(fp);
if (flen == -1) {
printf("ftell() failed\n");
if ((r = fclose(fp)) == EOF) {
printf("Cannot close file\n");
}
}
return R_FILE_ERROR;
}

fseek(fp, 0, SEEK_SET);
Expand All @@ -52,7 +54,8 @@ void handle_update_user_image(char *path) {
r = fclose(fp);
if (r == EOF) {
printf("Cannot close file\n");
}
}
return R_FILE_ERROR;
}

// Read the data of the file which will be sent to server
Expand All @@ -62,7 +65,8 @@ void handle_update_user_image(char *path) {
printf("fread() failed\n");
if ((r = fclose(fp)) == EOF) {
printf("Cannot close file\n");
}
}
return R_FILE_ERROR;
}

// Encode readed data
Expand All @@ -84,6 +88,8 @@ void handle_update_user_image(char *path) {

if ((r = fclose(fp)) == EOF) {
printf("Cannot close file\n");
return R_FILE_ERROR;
}
return R_SUCCESS;

}
4 changes: 2 additions & 2 deletions client/src/ssl_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ void init_ssl(SSL_CTX **ctx) {

*ctx = SSL_CTX_new(TLS_client_method());
if (*ctx == NULL) {
logger(strerror(errno), ERROR_LOG);
handle_error(strerror(errno));
exit(EXIT_FAILURE);
}
}
Expand All @@ -18,7 +18,7 @@ void connect_ssl(SSL **ssl, int* server_fd, SSL_CTX **ctx) {
SSL_set_fd(*ssl, *server_fd);

if (SSL_connect(*ssl) == -1) {
logger(strerror(errno), ERROR_LOG);
handle_error(strerror(errno));
exit(EXIT_FAILURE);
}
}
Expand Down
3 changes: 0 additions & 3 deletions memcheck.sh

This file was deleted.

1 change: 1 addition & 0 deletions server/inc/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void send_server_response(SSL* ssl, t_response_code code, t_request_type req_typ
void client_cleanup(t_server_utils* utils, bool is_client_exit);
int server_socket_init(struct sockaddr* serv_address, socklen_t address_size);
void new_client_create(SSL* ssl, int client_socket);
void handle_error(const char* error);

// REQUEST HANDLERS

Expand Down
6 changes: 3 additions & 3 deletions server/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ int main(int argc, char *argv[]) {

SSL_CTX *ctx = NULL;
SSL *ssl = NULL;
ssl_init(&ctx);
ssl_init(&ctx);

serv_address.sin_family = AF_INET;
serv_address.sin_addr.s_addr = INADDR_ANY;
serv_address.sin_port = htons(8080); // atoi(argv[1])
serv_address.sin_port = htons(atoi(argv[1]));

int server_socket = server_socket_init((struct sockaddr *)&serv_address, address_size);

Expand All @@ -32,7 +32,7 @@ int main(int argc, char *argv[]) {

} else {

logger(strerror(errno), ERROR_LOG);
handle_error(strerror(errno));

}
}
Expand Down
26 changes: 20 additions & 6 deletions server/src/server_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ int server_socket_init(struct sockaddr* serv_address, socklen_t address_size) {

int server_socket = 0;
if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
handle_error(strerror(errno));
exit(EXIT_FAILURE);
}

if (bind(server_socket, serv_address, address_size) == -1) {
handle_error(strerror(errno));
exit(EXIT_FAILURE);
}

listen(server_socket, 10);
if (listen(server_socket, 10) == -1) {
handle_error(strerror(errno));
// exit(EXIT_FAILURE);
}
return server_socket;

}
Expand All @@ -21,7 +26,7 @@ void new_client_create(SSL* ssl, int client_socket) {
pthread_t thread;

if (SSL_accept(ssl) == -1) {
logger(strerror(errno), ERROR_LOG);
handle_error(strerror(errno));
exit(EXIT_FAILURE);
}

Expand All @@ -43,10 +48,10 @@ void new_client_create(SSL* ssl, int client_socket) {
// Check if command line arguments are valid
void handle_arg_errors(char** argv) {

// if (argv[1] == NULL) {
// mx_printerr("usage: ./uchat_server [port]\n");
// exit(EXIT_FAILURE);
// }
if (argv[1] == NULL) {
mx_printerr("usage: ./uchat_server [port]\n");
exit(EXIT_FAILURE);
}

}

Expand Down Expand Up @@ -74,3 +79,12 @@ void daemon_init() {
signal(SIGTERM, SIG_DFL);

}

void handle_error(const char* error) {

char* err_str = mx_strjoin(error, "\n");
mx_printerr(err_str);
logger(err_str, ERROR_LOG);
mx_strdel(&err_str);

}
8 changes: 4 additions & 4 deletions server/src/server_utils/ssl_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ void ssl_sertificate(SSL_CTX *ctx, const char* cert, const char* key) {

if (SSL_CTX_use_certificate_file(ctx, cert, SSL_FILETYPE_PEM) <= 0)
{
logger(strerror(errno), ERROR_LOG);
handle_error(strerror(errno));
exit(EXIT_FAILURE);
}

if (SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM) <= 0)
{
logger(strerror(errno), ERROR_LOG);
handle_error(strerror(errno));
exit(EXIT_FAILURE);
}

if (!SSL_CTX_check_private_key(ctx))
{
logger(strerror(errno), ERROR_LOG);
handle_error(strerror(errno));
exit(EXIT_FAILURE);
}

Expand All @@ -32,7 +32,7 @@ void ssl_init(SSL_CTX **ctx) {
*ctx = SSL_CTX_new(TLS_server_method());

if (!*ctx) {
logger(strerror(errno), ERROR_LOG);
handle_error(strerror(errno));
exit(EXIT_FAILURE);
}

Expand Down
2 changes: 1 addition & 1 deletion utils/inc/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ typedef enum e_response_code {
R_DB_FAILURE,
R_JSON_FAILURE,
R_INVALID_INPUT,
R_FILE_ERROR,

R_USR_EXISTS,
R_USR_NOENT,
Expand Down Expand Up @@ -134,6 +135,5 @@ t_chat* mx_get_chat_by_name(t_chat* list, const char* name);
t_msg* mx_get_msg_by_id(t_msg* list, int id);
void mx_clear_msg(t_msg** p);
t_msg* mx_get_last_msg_node(t_msg* list);
void mx_print_msg(t_msg* msg); // remove

#endif
4 changes: 2 additions & 2 deletions utils/src/db_lists/chat_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ t_chat *mx_create_chat(int id, const char* name, int permissions, int chat_color
new_node->id = id;
new_node->name = name ? mx_strdup(name) : NULL;
new_node->permissions = permissions;
new_node->avatar_color = chat_color;
new_node->new_msg_count = 0;
new_node->messages = NULL;
new_node->new_messages = NULL;
new_node->last_new_msg = NULL;
new_node->new_msg_count = 0;
new_node->avatar_color = chat_color;
new_node->next = NULL;
return new_node;
}
Expand Down
8 changes: 0 additions & 8 deletions utils/src/db_lists/msg_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,3 @@ int mx_get_msg_idx_by_id(t_msg* list, int id) {

}

void mx_print_msg(t_msg* msg) {

char str[200];
sprintf(str, "This is a t_msg msg:\n\ttext: %s, chat_id: %d, sender_id: %d, sender_name: %s, date: %s\n",
msg->text, msg->chat_id, msg->sender_id, msg->sender_name, msg->date_str);
logger(str, INFO_LOG);

}

0 comments on commit 4583f29

Please sign in to comment.