From dc9df45dc6578704a367ab6a72842f65b1190f77 Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Thu, 2 May 2024 00:00:34 -0400 Subject: default listen and accept impl --- src/bind.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- src/discover.c | 29 ++++++++++++++++++++++++--- src/protocol.c | 4 ++++ 3 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 src/protocol.c (limited to 'src') diff --git a/src/bind.c b/src/bind.c index 786624c..7392db3 100644 --- a/src/bind.c +++ b/src/bind.c @@ -1,12 +1,16 @@ #include "osm/bind.h" +#include "osm/utils.h" #include #include +#include #include #include #include +#include + #define MAX_ID 0xFFFF @@ -122,9 +126,8 @@ int osm_bind_local(int sockfd, const char *sock_dir) int osm_open_onboard(char *sock_dir) { int sockfd = socket(AF_LOCAL, SOCK_SEQPACKET, 0); - if (sockfd < 0) + if (sockfd == -1) { - perror("socket"); return sockfd; } @@ -140,14 +143,63 @@ int osm_open_onboard(char *sock_dir) if (bound != 0) { - perror("bind"); close(sockfd); - return bound; + return -1; } - + + listen(sockfd, 3); return sockfd; } +/** + * Listen for and return connections for a socket + * return - a vector containing all the threads which this function started + */ +Vector osm_listen_and_accept(int sockfd, thrd_start_t callback) +{ + Vector out = vect_init(sizeof(thrd_t)); + thrd_t thread; + + while(1) + { + // Try to accept a connection + errno = 0; + int fd = accept(sockfd, NULL, NULL); + if (fd == -1) + { + if (errno != ECONNABORTED && errno != EINTR) + { + break; + } + else + continue; + } + + // Create and store the connection thread + errno = 0; + int ret = thrd_create(&thread, callback, (void*)(uintptr_t)fd); + if(ret == thrd_success) + { + vect_push(&out, &thread); + } + else + { + if (ret == thrd_error) + { + perror("Error creating thread"); + } + else if (ret == thrd_nomem) + { + fprintf(stderr, "Thread out of memory. Shutting down.\n"); + } + break; + } + + } + + return out; +} + /** * Bind a new network socket * Should only be called by one process on the machine. diff --git a/src/discover.c b/src/discover.c index 1d8259a..53eef73 100644 --- a/src/discover.c +++ b/src/discover.c @@ -1,8 +1,31 @@ -#include "osm/discover.h" +#include "osm/utils.h" -Vector osm_discover_onboard(char *sock_fd) +#include +#include +#include +#include + +Vector osm_discover_onboard(char *sock_dir) { - Vector out = {0}; + Vector out = vect_init(sizeof(char)); + + DIR *d = opendir(sock_dir); + if (d) + { + struct dirent *dir; + while((dir = readdir(d)) != NULL) + { + if (dir->d_type == DT_SOCK) + { + } + } + errno = 0; + closedir(d); + } + else + { + perror("opendir"); + } return out; } diff --git a/src/protocol.c b/src/protocol.c new file mode 100644 index 0000000..b2254cb --- /dev/null +++ b/src/protocol.c @@ -0,0 +1,4 @@ +#include "osm/protocol.h" + +const char OSM_MAGIC_INIT[4] = "OSmI"; +const char OSM_MAGIC_FRAME[4] = "OSmF"; -- cgit v1.2.3