From 1394cd7204a5a864671ddbee7463ce2453deb3cc Mon Sep 17 00:00:00 2001 From: MrMelon Date: Sat, 24 Apr 2021 11:19:44 +0100 Subject: [PATCH] Add shared binary --- src/impl/shared/config_reader.c | 68 ++++++++++++++++++++++++++++ src/impl/shared/config_writer.c | 46 +++++++++++++++++++ src/impl/shared/unix_socket_client.c | 36 +++++++++++++++ src/impl/shared/unix_socket_server.c | 57 +++++++++++++++++++++++ 4 files changed, 207 insertions(+) create mode 100644 src/impl/shared/config_reader.c create mode 100644 src/impl/shared/config_writer.c create mode 100644 src/impl/shared/unix_socket_client.c create mode 100644 src/impl/shared/unix_socket_server.c diff --git a/src/impl/shared/config_reader.c b/src/impl/shared/config_reader.c new file mode 100644 index 0000000..338711d --- /dev/null +++ b/src/impl/shared/config_reader.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include + +#include "config_file.h" + +/* + * Example usage + +FILE *fcr; +fcr = fopen("./test-read.cfg", "r"); + +char *ckey; +char *cval; +while(config_read(fcr, &ckey, &cval)) { + fprintf(stderr, "key: %s\nval: %s\n\n",ckey,cval); +} + +fclose(fcr); + + */ + +keyvaluepair_t config_read(FILE *fp) { + keyvaluepair_t result; + result.key = ""; + result.value = ""; + result.loop = false; + + // If the input file doesn't exist then just exit + if (fp == NULL) { + fprintf(stderr, "Can't read from a non-existant file"); + return result; + } + + // Max line length is 250 + int MAX_LEN = 250; + char buffer[MAX_LEN]; + + // Grab a line but if there aren't any return false + if(!fgets(buffer, MAX_LEN - 1, fp)) return result; + + // Find the newline and remove it + buffer[strcspn(buffer, "\n")] = 0; + + // Get the char at " = " and calculate the offset + char *midchar = strstr(buffer, " = "); + int midpoint = midchar - buffer; + + // Make a char array big enough for the key and copy the key into it + char keytoken[midpoint+1]; + strncpy(keytoken, buffer, midpoint); + keytoken[midpoint]=0; + + // Make a char array big enough for the rest and copy from 3 chars after midchar + char valtoken[MAX_LEN-midpoint+1]; + strcpy(valtoken, buffer+midpoint+3); + valtoken[MAX_LEN-midpoint]=0; + + // Return them via the char array pointers + result.key = malloc(midpoint+1); + result.value = malloc(MAX_LEN-midpoint+1); + strcpy(result.key, keytoken); + strcpy(result.value, valtoken); + result.loop = true; + + return result; +} diff --git a/src/impl/shared/config_writer.c b/src/impl/shared/config_writer.c new file mode 100644 index 0000000..a90bd55 --- /dev/null +++ b/src/impl/shared/config_writer.c @@ -0,0 +1,46 @@ +#include + +#include "config_file.h" + +/* + * Example usage + +FILE *fcw; +fcw = fopen("./test-write.cfg", "w"); + +int count = 5; + +char *testkeys[] = { + "test1", + "bb", + "key", + "value", + "idk" +}; +char *testvalues[] = { + "test2", + "dd", + "to", + "pair", + "maybe" +}; + +for(int i=0; i +#include +#include +#include +#include + +int unix_socket_client_start(char *sock_path) { + struct sockaddr_un addr; + + // Create a new client socket with domain: AF_UNIX, type: SOCK_STREAM, protocol: 0 + int sfd = socket(AF_UNIX, SOCK_STREAM, 0); + + // Make sure socket's file descriptor is legit. + if (sfd == -1) { + fprintf(stderr,"ERROR: socket is not legit"); + return -1; + } + + // + // Construct server address, and make the connection. + // + memset(&addr, 0, sizeof(struct sockaddr_un)); + addr.sun_family = AF_UNIX; + strncpy(addr.sun_path, sock_path, sizeof(addr.sun_path) - 1); + + // Connects the active socket referred to be sfd to the listening socket + // whose address is specified by addr. + if (connect(sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) == -1) { + fprintf(stderr,"Failed to connect"); + return -1; + } + + return sfd; +} \ No newline at end of file diff --git a/src/impl/shared/unix_socket_server.c b/src/impl/shared/unix_socket_server.c new file mode 100644 index 0000000..2410624 --- /dev/null +++ b/src/impl/shared/unix_socket_server.c @@ -0,0 +1,57 @@ +#include "unix_sockets.h" + +#include +#include +#include +#include +#include + +int unix_socket_server_start(char *sock_path) { + struct sockaddr_un addr; + + // Create a new server socket with domain: AF_UNIX, type: SOCK_STREAM, protocol: 0 + int sfd = socket(AF_UNIX, SOCK_STREAM, 0); + printf("Server socket fd = %d\n", sfd); + + // Make sure socket's file descriptor is legit. + if (sfd == -1) { + fprintf(stderr,"ERROR: socket is not legit"); + return -1; + } + + // Make sure the address we're planning to use isn't too long. + if (strlen(sock_path) > sizeof(addr.sun_path) - 1) { + fprintf(stderr,"Server socket path too long: %s",sock_path); + return -1; + } + + // Delete any file that already exists at the address. Make sure the deletion + // succeeds. If the error is just that the file/directory doesn't exist, it's fine. + if (remove(sock_path) == -1 && errno != ENOENT) { + fprintf(stderr,"remove-%s",sock_path); + return -1; + } + + // Zero out the address, and set family and path. + memset(&addr, 0, sizeof(struct sockaddr_un)); + addr.sun_family = AF_UNIX; + strncpy(addr.sun_path, sock_path, sizeof(addr.sun_path) - 1); + + // Bind the socket to the address. Note that we're binding the server socket + // to a well-known address so that clients know where to connect. + if (bind(sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) == -1) { + fprintf(stderr,"Failed to bind"); + return -1; + } + + // The listen call marks the socket as *passive*. The socket will subsequently + // be used to accept connections from *active* sockets. + // listen cannot be called on a connected socket (a socket on which a connect() + // has been succesfully performed or a socket returned by a call to accept()). + if (listen(sfd, BACKLOG) == -1) { + fprintf(stderr,"Failed to listen"); + return -1; + } + + return sfd; +}