Skip to content

Commit

Permalink
Merge pull request #6 from drawpitech/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
drawbu authored Apr 13, 2024
2 parents a206990 + d9f5155 commit 1270c8c
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 54 deletions.
14 changes: 9 additions & 5 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
pkgs = inputs.nixpkgs.legacyPackages.${system};
cc = pkgs.gcc12;

deriv = name: inputs:
pkgs.stdenv.mkDerivation {
deriv = name:
pkgs.stdenv.mkDerivation rec {
inherit name;
src = ./.;

buildInputs = [cc] ++ (with pkgs; [glibc gnumake pkg-config libuuid]) ++ inputs;
buildInputs = with pkgs; [glibc gnumake pkg-config libuuid];
nativeBuildInputs = [pkgs.makeWrapper];
makeFlags = ["CC=${cc}/bin/gcc"];
hardeningDisable = ["format" "fortify"];
enableParallelBuilding = true;
Expand All @@ -26,6 +27,9 @@
mkdir -p $out/bin
cp ${name} $out/bin
'';
postFixup = ''
wrapProgram $out/bin/${name} --set LD_LIBRARY_PATH ${./libs/myteams}
'';
};
in rec {
formatter = pkgs.alejandra;
Expand All @@ -44,8 +48,8 @@
};

packages = {
server = deriv "myteams_server" [];
client = deriv "myteams_cli" [];
server = deriv "myteams_server";
client = deriv "myteams_cli";
};
});
}
5 changes: 2 additions & 3 deletions src/client/event_handling/handle_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
** handle_create
*/

#include "handle_event.h"

#include <myteams/logging_client.h>
#include <uuid/uuid.h>

#include "../libs/myteams/logging_client.h"
#include "client.h"
#include "handle_event.h"
#include "ressources_infos.h"
#include "run_client.h"
#include "utils.h"
Expand Down
2 changes: 1 addition & 1 deletion src/client/event_handling/handle_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

#include "handle_event.h"

#include <myteams/logging_client.h>
#include <uuid/uuid.h>

#include "../libs/myteams/logging_client.h"
#include "client.h"
#include "ressources_infos.h"
#include "run_client.h"
Expand Down
4 changes: 2 additions & 2 deletions src/client/run_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ static bool is_fd_ready(int fd)
struct timeval tv;
fd_set rfds;

tv.tv_sec = 1;
tv.tv_usec = 0;
tv.tv_sec = 0;
tv.tv_usec = 1000;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
if (select(fd + 1, &rfds, NULL, NULL, &tv) > 0)
Expand Down
2 changes: 1 addition & 1 deletion src/client/serve_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ run_state_t get_serv_info(connection_t *connect)

if (get_info_type(connect, code, 3) != SUCCESS) {
dprintf(STDERR_FILENO, "Error: didn't manage to read from server.\n");
return running;
return cli_exit;
}
for (size_t i = 0; i < LEN_OF(func_code_tab); i++) {
if (strncmp(func_code_tab[i].code, code, 3) == 0){
Expand Down
12 changes: 6 additions & 6 deletions src/common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ LDFLAGS :=
LDLIBS := -luuid

# ↓ myteams lib
CFLAGS += -I../../libs/myteams
LDFLAGS += -L../../libs/myteams
CFLAGS += -isystem ../../libs
LDFLAGS += -L ../../libs/myteams
LDLIBS += -lmyteams

# ↓ Binaries
Expand Down Expand Up @@ -66,7 +66,7 @@ all: $(NAME)
$(BUILD_DIR)/source/%.o: %.c
@ mkdir -p $(dir $@)
@ $(ECHO) "[${C_BOLD}${C_RED}CC${C_RESET}] $^"
@ $(CC) -o $@ -c $< $(LDFLAGS) $(LDLIBS) $(CFLAGS) $(DEPS_FLAGS) || $(DIE)
@ $(CC) -o $@ -c $< $(LDLIBS) $(CFLAGS) $(DEPS_FLAGS) || $(DIE)

$(NAME): $(OBJ)
@ $(ECHO) "[${C_BOLD}${C_YELLOW}CC${C_RESET}] ${C_GREEN}$@${C_RESET}"
Expand All @@ -76,13 +76,13 @@ $(NAME): $(OBJ)
$(BUILD_DIR)/tests/%.o: %.c
@ mkdir -p $(dir $@)
@ $(ECHO) "[${C_BOLD}${C_RED}CC${C_RESET}] $^"
@ $(CC) -o $@ -c $< $(LDFLAGS) $(LDLIBS) $(CFLAGS) $(DEPS_FLAGS) || $(DIE)
@ $(CC) -o $@ -c $< $(LDLIBS) $(CFLAGS) $(DEPS_FLAGS) || $(DIE)

# ↓ Asan
$(BUILD_DIR)/asan/%.o: %.c
@ mkdir -p $(dir $@)
@ $(ECHO) "[${C_BOLD}${C_RED}CC${C_RESET}] $^"
@ $(CC) -o $@ -c $< $(LDFLAGS) $(LDLIBS) $(CFLAGS) $(DEPS_FLAGS) || $(DIE)
@ $(CC) -o $@ -c $< $(LDLIBS) $(CFLAGS) $(DEPS_FLAGS) || $(DIE)

$(ASAN_NAME): LDFLAGS += -fsanitize=address,leak,undefined -g3
$(ASAN_NAME): LDFLAGS += -fanalyzer
Expand All @@ -95,7 +95,7 @@ $(ASAN_NAME): $(ASAN_OBJ)
$(BUILD_DIR)/prof/%.o: %.c
@ mkdir -p $(dir $@)
@ $(ECHO) "[${C_BOLD}${C_RED}CC${C_RESET}] $^"
@ $(CC) -o $@ -c $< $(LDFLAGS) $(CFLAGS) $(DEPS_FLAGS) || $(DIE)
@ $(CC) -o $@ -c $< $(CFLAGS) $(DEPS_FLAGS) || $(DIE)

$(PROF_NAME): LDFLAGS += -pg
$(PROF_NAME): $(PROF_OBJ)
Expand Down
4 changes: 2 additions & 2 deletions src/server/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ void client_init(client_t *client, int fd)
client->fd = fd;
client->buffer[0] = '\0';
client->user = NULL;
DEBUG_MSG("Client connected\n");
DEBUG_MSG("Client connected");
}

void client_disconnect(client_t *client)
{
close(client->fd);
client->fd = -1;
DEBUG_MSG("Client disconnected\n");
DEBUG_MSG("Client disconnected");
}

static void client_process_message(
Expand Down
39 changes: 22 additions & 17 deletions src/server/cmds/login.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@
** login
*/

#include <myteams/logging_server.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <uuid/uuid.h>

#include "command.h"
#include "debug.h"
#include "ressources_infos.h"
#include "server.h"
#include "../libs/myteams/logging_server.h"

static void send_info(server_t *server, client_t *client, char *code)
static void broadcast_user_info(server_t *server, client_t *client, char *code)
{
user_info_t info = {0};

strcpy(info.user_name, client->user->name);
uuid_copy(info.user_uuid, client->user->uuid);
for (size_t i = 0; i < server->clients.size; i++) {
dprintf(server->clients.arr[i].fd, "%s", code);
write(server->clients.arr[i].fd, &info, sizeof(info));
write(server->clients.arr[i].fd, code, strlen(code));
write(server->clients.arr[i].fd, &info, sizeof info);
}
}

Expand All @@ -35,9 +36,10 @@ static void create_user(server_t *server, client_t *client, char *name)
strcpy(new.name, name);
uuid_generate(new.uuid);
uuid_unparse(new.uuid, uuid_str);
append_to_array(&server->users, sizeof(user_t), &new);
append_to_array(&server->users, sizeof new, &new);
client->user = &server->users.arr[server->users.size - 1];
server_event_user_created(uuid_str, new.name);
DEBUG("User %s <%s> created", client->user->name, uuid_str);
server_event_user_created(uuid_str, client->user->name);
}

static void assign_user(client_t *client, user_t *user)
Expand All @@ -46,28 +48,31 @@ static void assign_user(client_t *client, user_t *user)

client->user = user;
uuid_unparse(user->uuid, uuid_str);
DEBUG("User %s <%s> logged", client->user->name, uuid_str);
server_event_user_logged_in(uuid_str);
}

void cmd_login(server_t *server, client_t *client)
{
int ag = 0;
char *name = get_arg(client->buffer, 0, &ag);
user_t *loged = NULL;
size_t ag = 0;
char *name = get_quoted_arg(client->buffer, 0, &ag);
user_t *user = NULL;

if (!name) {
if (name == NULL) {
dprintf(client->fd, "503 Syntax error.\n");
return;
}
for (size_t i = 0; i < server->users.size; i ++) {
if (strcmp(name, server->users.arr[i].name) == 0)
loged = &server->users.arr[i];
for (size_t i = 0; i < server->users.size; i++) {
if (strcmp(name, server->users.arr[i].name) == 0) {
user = &server->users.arr[i];
break;
}
}
if (loged)
assign_user(client, loged);
if (user != NULL)
assign_user(client, user);
else
create_user(server, client, name);
send_info(server, client, "311");
broadcast_user_info(server, client, "311");
}

void cmd_logout(server_t *server, client_t *client)
Expand All @@ -76,6 +81,6 @@ void cmd_logout(server_t *server, client_t *client)

uuid_unparse(client->thread_uuid, uuid_str);
server_event_user_logged_out(uuid_str);
send_info(server, client, "312");
broadcast_user_info(server, client, "312");
client->user = NULL;
}
32 changes: 17 additions & 15 deletions src/server/cmds/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,28 @@
** parse
*/

#include "command.h"
#include <stddef.h>
#include <string.h>

#include "command.h"

char *get_arg(char *buff, int start, int *end)
char *get_quoted_arg(char *buff, size_t start, size_t *end)
{
int i = start;
int j = 0;
char *begin = NULL;
char *end_ptr = NULL;

if (!buff)
if (buff == NULL)
return NULL;
while (buff[i] && buff[i] != '"')
i ++;
if (buff[i] == '\0' || buff[i + 1] == '\0')
buff += start;
begin = strchr(buff, '"');
if (begin == NULL)
return NULL;
i ++;
while (buff[i + j] && buff[i + j] != '"')
j ++;
if (buff[i + j] == '\0')
begin += 1;
end_ptr = strchr(begin, '"');
if (end_ptr == NULL)
return NULL;
buff[i + j] = '\0';
*end = i + j;
return buff + i;
*end_ptr = '\0';
if (end != NULL)
*end = buff - end_ptr;
return begin;
}
2 changes: 1 addition & 1 deletion src/server/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ static const struct cmd_s {
};

void exec_command(server_t *server, client_t *client);
char *get_arg(char *buff, int start, int *end);
char *get_quoted_arg(char *buff, size_t start, size_t *end);
1 change: 0 additions & 1 deletion src/server/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <unistd.h>

#include "client.h"
#include "utils.h"

/**
* @brief Append an element to an array.
Expand Down

0 comments on commit 1270c8c

Please sign in to comment.