From 7aea4c126d147def620b479a50d0038633cc82bf Mon Sep 17 00:00:00 2001 From: Warwick Date: Fri, 28 Jul 2023 16:23:21 +0100 Subject: [PATCH] Added some msg functions for handling sockets --- src/client/main.c | 12 ++++---- src/server/main.c | 49 ++++++------------------------ urchin-util/include/error.h | 6 ++-- urchin-util/include/message.h | 12 ++++++-- urchin-util/src/error.c | 6 ++-- urchin-util/src/message.c | 57 +++++++++++++++++++++++++++++++++-- 6 files changed, 85 insertions(+), 57 deletions(-) diff --git a/src/client/main.c b/src/client/main.c index dd2e2eb..fd60171 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -30,7 +30,7 @@ int main(int argc, char *argv[]) { char msgbuffer[255]; if (argc < 2) { - error_crash("Not enough server info has been provided"); + err__crash("Not enough server info has been provided"); } if (argc > 2) { @@ -42,11 +42,11 @@ int main(int argc, char *argv[]) { // Create socket sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) - error_crash("Can't open socket."); + err__crash("Can't open socket."); server = gethostbyname(argv[1]); if (server == NULL) - error_crash("No such host exists :("); + err__crash("No such host exists :("); serv_addr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, @@ -54,19 +54,19 @@ int main(int argc, char *argv[]) { serv_addr.sin_port = htons(portno); errflag = connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); if (errflag < 0) - error_crash("Failed to connect to host"); + err__crash("Failed to connect to host"); while (1) { bzero(msgbuffer, sizeof msgbuffer); fgets(msgbuffer, sizeof msgbuffer, stdin); errflag = write(sockfd, msgbuffer, strlen(msgbuffer)); if (errflag < 0) - error_crash("Failed to write message"); + err__crash("Failed to write message"); bzero(msgbuffer, sizeof msgbuffer); errflag = read(sockfd, msgbuffer, sizeof msgbuffer); if (errflag < 0) - error_crash("Failed to read message"); + err__crash("Failed to read message"); printf("Server: %s", msgbuffer); diff --git a/src/server/main.c b/src/server/main.c index fb06a99..f605e4f 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -13,10 +13,9 @@ int main(int argc, char *argv[]) { int a = 1; - msg_amsg_test(a); // Set up containers for file descriptor id's - int sockfd, clientsockfd, portno, errflag; + int serv_sockfd, cli_sockfd, portno, errflag; char msgbuffer[255]; // Set port number. @@ -32,46 +31,18 @@ int main(int argc, char *argv[]) { // touched until it's used // Create socket - sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (sockfd < 0) { - error_crash("Can't open socket."); - } + serv_sockfd = msg__create_serv_connection(portno); - // Make sure there is no junk data in serve_addr - bzero((char *)&serv_addr, sizeof serv_addr); - - // Tell the connection to use ipv4 and port number - serv_addr.sin_family = AF_INET; - serv_addr.sin_addr.s_addr = INADDR_ANY; - serv_addr.sin_port = htons(portno); - - // Bind Socket - errflag = bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); - if (errflag < 0) { - error_crash("Can't bind socket"); - } - - errflag = listen(sockfd, 5); - if (errflag < 0) { - error_crash("Can't listen for connections"); - } - - char portstr[5]; // Buffer to store string version of port - snprintf(portstr, sizeof portstr, "%d", portno); - printf("Server Listening on port: %s\n", portstr); - - clientsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen); - if (clientsockfd < 0) { - error_crash("Can't accept connection"); - } + // Accept connection + cli_sockfd = msg__accept_cli_connection(serv_sockfd); while (1) { // Empty message buffer bzero(msgbuffer, sizeof msgbuffer); - errflag = read(clientsockfd, msgbuffer, sizeof msgbuffer); + errflag = read(cli_sockfd, msgbuffer, sizeof msgbuffer); if (errflag < 0) { - error_crash("Didn't recieve message from connection"); + err__crash("Didn't recieve message from connection"); } printf("Client: %s\n", msgbuffer); @@ -79,9 +50,9 @@ int main(int argc, char *argv[]) { bzero(msgbuffer, sizeof msgbuffer); fgets(msgbuffer, sizeof msgbuffer, stdin); - errflag = write(clientsockfd, msgbuffer, strlen(msgbuffer)); + errflag = write(cli_sockfd, msgbuffer, strlen(msgbuffer)); if (errflag < 0) { - error_crash("Couldn't write response message"); + err__crash("Couldn't write response message"); } int cmp = strncmp("Bye", msgbuffer, 3); @@ -89,8 +60,8 @@ int main(int argc, char *argv[]) { break; } - close(clientsockfd); - close(sockfd); + msg__destroy_connection(cli_sockfd); + msg__destroy_connection(serv_sockfd); return EXIT_SUCCESS; } diff --git a/urchin-util/include/error.h b/urchin-util/include/error.h index b40f069..a231440 100644 --- a/urchin-util/include/error.h +++ b/urchin-util/include/error.h @@ -4,8 +4,8 @@ #include #include -void error_crash(const char *msg); +void err__crash(const char *msg); -void error_warn(const char *msg); +void err__warn(const char *msg); -void error_log(const char *msg); +void err__log(const char *msg); diff --git a/urchin-util/include/message.h b/urchin-util/include/message.h index 28e67b2..37b082f 100644 --- a/urchin-util/include/message.h +++ b/urchin-util/include/message.h @@ -1,15 +1,21 @@ #pragma once #include "error.h" +#include //sockaddr_in #include #include #include +#include // sockaddr +#include -void msg_amsg_test(int a); +void msg__amsg_test(int a); // Creates a socket set's it up to listen to listen on the specified port and // returns the file descriptor -int msg_create_serv_connection(int portno); +int msg__create_serv_connection(int portno); + +// Closes socket connection +void msg__destroy_connection(int sockfd); // Creates a socket for the client and returns the file descriptor -int msg_accept_cli_connection(); +int msg__accept_cli_connection(int server_sockfd); diff --git a/urchin-util/src/error.c b/urchin-util/src/error.c index 7b3dc6e..338f7c5 100644 --- a/urchin-util/src/error.c +++ b/urchin-util/src/error.c @@ -2,12 +2,12 @@ #include "protobuf_gen/amessage.pb-c.h" #define MAX_MSG_SIZE 1024 -void error_crash(const char *msg) { +void err__crash(const char *msg) { fprintf(stderr, "error crash: %s\n", msg); exit(EXIT_FAILURE); } -void error_warn(const char *msg) { fprintf(stderr, "warning: %s\n", msg); } +void err__warn(const char *msg) { fprintf(stderr, "warning: %s\n", msg); } // TODO: Save the logs somewhere -void error_log(const char *msg) { fprintf(stderr, "log: %s\n", msg); } +void err__log(const char *msg) { fprintf(stderr, "log: %s\n", msg); } diff --git a/urchin-util/src/message.c b/urchin-util/src/message.c index 4e9c959..2e88f9e 100644 --- a/urchin-util/src/message.c +++ b/urchin-util/src/message.c @@ -1,8 +1,9 @@ #include "message.h" -#define MAX_MSG_SIZE 1024 #include "protobuf_gen/amessage.pb-c.h" -void msg_amsg_test(int a) { +#define MAX_MSG_SIZE 1024 + +void msg__amsg_test(int a) { AMessage msg = AMESSAGE__INIT; // The massage format void *buf; // Buffer to store serialized data unsigned len; // length of serialized data @@ -28,7 +29,7 @@ void msg_amsg_test(int a) { msg2 = amessage__unpack(NULL, msg_len, buf2); if (msg2 == NULL) { - error_crash("error unpacking incoming message\n"); + err__crash("error unpacking incoming message\n"); } // display the message's fields. @@ -40,3 +41,53 @@ void msg_amsg_test(int a) { // Free the unpacked message amessage__free_unpacked(msg2, NULL); } + +int msg__create_serv_connection(int portno) { + int sockfd, clientsockfd, errflag; + struct sockaddr_in serv_addr; + + // Create socket + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd < 0) { + err__crash("Can't open socket."); + } + + // Make sure there is no junk data in serve_addr + bzero((char *)&serv_addr, sizeof serv_addr); + + // Tell the connection to use ipv4 and port number + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = INADDR_ANY; + serv_addr.sin_port = htons(portno); + + // Bind Socket + errflag = bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); + if (errflag < 0) { + err__crash("Can't bind socket"); + } + + errflag = listen(sockfd, 5); + if (errflag < 0) { + err__crash("Can't listen for connections"); + } + char successmsg[80]; // Buffer for success message + snprintf(successmsg, sizeof successmsg, "Server funning on port: %d", portno); + err__log(successmsg); + + return sockfd; +} + +void msg__destroy_connection(int sockfd) { close(sockfd); } + +int msg__accept_cli_connection(int server_sockfd) { + struct sockaddr_in cli_addr; + socklen_t clilen = sizeof cli_addr; + + int cli_sockfd = accept(server_sockfd, (struct sockaddr *)&cli_addr, &clilen); + + if (cli_sockfd < 0) { + err__crash("Can't accept connection"); + } + + return cli_sockfd; +}