Skip to content

Commit

Permalink
More setup
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-botticelli committed Apr 6, 2023
1 parent efa652c commit 69f1b00
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 11 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,4 @@ Temporary Items



.vscode/
build/
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"files.associations": {
"properties.h": "c",
"__locale": "cpp",
"system_error": "cpp",
"typeinfo": "cpp"
},
"editor.rulers": [100]
}
34 changes: 31 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,39 @@ OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
DEPS := $(OBJS:.o=.d)
INC_DIRS := $(shell find $(SRC_DIRS) -type d)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
CPPFLAGS := $(INC_FLAGS) -MMD -MP -Ofast -fomit-frame-pointer
CPPFLAGS := $(INC_FLAGS) -MMD -MP -Wall -Wno-unused-command-line-argument
LDFLAGS := -pthread -lpthread

#
RELEASE_FLAGS := -Ofast -DNDEBUG -fvisibility=hidden -fstack-protector-strong \
-fomit-frame-pointer -fPIE -fstack-clash-protection -fsanitize=bounds \
-fsanitize-undefined-trap-on-error -D_FORTIFY_SOURCE=3 -flto
# RELEASE_LDFLAGS := -Wl,-z,relro,-z,now,-z,noexecstack,-z,separate-code

DEBUG_FLAGS := -O0 -g3 -fno-omit-frame-pointer -fno-common -fno-optimize-sibling-calls
ASAN_FLAGS := $(DEBUG_FLAGS) -fsanitize=address,undefined
MSAN_FLAGS := $(DEBUG_FLAGS) -fsanitize=memory -fsanitize-memory-track-origins=2
TSAN_FLAGS := $(DEBUG_FLAGS) -fsanitize=thread

.PHONY: all debug asan msan tsan clean

all: CPPFLAGS += $(RELEASE_FLAGS)
# all: LDFLAGS += $(RELEASE_LDFLAGS)
all: $(BUILD_DIR)/$(TARGET_EXEC)
strip $(BUILD_DIR)/$(TARGET_EXEC)

debug: CPPFLAGS += $(DEBUG_FLAGS)
debug: $(BUILD_DIR)/$(TARGET_EXEC)

asan: CPPFLAGS += $(ASAN_FLAGS)
asan: $(BUILD_DIR)/$(TARGET_EXEC)

msan: CPPFLAGS += $(MSAN_FLAGS)
msan: $(BUILD_DIR)/$(TARGET_EXEC)

tsan: CPPFLAGS += $(TSAN_FLAGS)
tsan: $(BUILD_DIR)/$(TARGET_EXEC)

$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS)
$(CC) $(OBJS) -o $@ $(LDFLAGS)

Expand All @@ -28,8 +58,6 @@ $(BUILD_DIR)/%.cpp.o: %.cpp
$(MKDIR_P) $(dir $@)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@

.PHONY: clean

clean:
$(RM) -r $(BUILD_DIR)

Expand Down
7 changes: 7 additions & 0 deletions src/chat_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ typedef struct {
unsigned short port;
char *name;
} Node;

typedef struct {
Node *node;
Node *nextNode;
} NodeListItem;

typedef NodeListItem *NodeList;
64 changes: 64 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,70 @@

#include "chat_node.h"

void *connection_handler(void *threadid)
{
int thread_num = (int) threadid;
int sock_desc;

int32_t inputRaw, input;
int32_t responseRaw, response;
ssize_t response_length;

struct sockaddr_in serv_addr;

sock_desc = socket(AF_INET, SOCK_STREAM, 0);
if (sock_desc < 0) {
perror("Failed to create socket!");
return EXIT_FAILURE;
}

bzero((char *) &serv_addr, sizeof(serv_addr));

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);

if (inet_pton(AF_INET, SERVER_ADDR, &serv_addr.sin_addr) <= 0) {
perror("Error: Invalid address (address not supported)!");
return EXIT_FAILURE;
}

if (connect(sock_desc, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("Error: Failed to connect to server!");
return EXIT_FAILURE;
}

printf("Client connected successfully: %d\n", thread_num);
while (true) {
printf("For thread: %d\n", thread_num);

printf("Enter input: ");
scanf("%d", &inputRaw);

input = htonl(inputRaw);

if (send(sock_desc, &input, sizeof(int32_t), 0) == -1) {
perror("Error: Send failed!");
return EXIT_FAILURE;
}

response_length = recv(sock_desc, &responseRaw, sizeof(int32_t), 0);
if (response_length != sizeof(int32_t)) {
perror("Error: Receive failed!");
return EXIT_FAILURE;
}

response = ntohl(responseRaw);

printf("Server response: %d\n", response);

sleep(5);
}

close(sock_desc);
return EXIT_SUCCESS;
}


int main(int argc, char *argv[]) {
puts("=== chat_node ===\n");

Expand Down
7 changes: 0 additions & 7 deletions src/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@

typedef uint8_t PacketHeader;

typedef struct {
Node *node;
Node *nextNode;
} NodeListItem;

typedef NodeListItem *NodeList;

typedef struct {
PacketHeader header;

Expand Down
Empty file added src/receiver_handler.c
Empty file.
Empty file added src/receiver_handler.h
Empty file.
Empty file added src/sender_handler.c
Empty file.
Empty file added src/sender_handler.h
Empty file.

0 comments on commit 69f1b00

Please sign in to comment.