summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyle Gunger <kgunger12@gmail.com>2024-05-01 03:11:07 -0400
committerKyle Gunger <kgunger12@gmail.com>2024-05-01 03:11:07 -0400
commitfeccf0adf3e6861b11a7768669a63c327f77ec10 (patch)
tree3aab13f202d0963ce74900dba8a508e68fe93614
parent21938c4ed611cd0945d62502abdb74527a533e78 (diff)
Vector code
-rw-r--r--Makefile6
-rw-r--r--include/osm/bind.h3
-rw-r--r--include/osm/device.h10
-rw-r--r--include/osm/discover.h9
-rw-r--r--include/osm/utils.h52
-rw-r--r--src/bind.c42
-rw-r--r--src/discover.c10
-rw-r--r--src/vector.c101
8 files changed, 226 insertions, 7 deletions
diff --git a/Makefile b/Makefile
index 9c1555e..f2e5640 100644
--- a/Makefile
+++ b/Makefile
@@ -12,6 +12,12 @@ CFLAGS ?= -Werror -Wall
build: build_dir $(OBJS)
$(CC) -shared -o $(BUILD_DIR)/libopensmarts.so $(addprefix $(OBJ_DIR)/, $(OBJS))
+install:
+ install -m 755 ./build/libopensmarts.so /usr/lib64/libopensmarts.so
+ rm -rf /usr/include/osm
+ mkdir -p /usr/include/osm
+ cp -r ./include/osm /usr/include
+
%.o: $(SRC_DIR)/%.c
$(CC) $(CFLAGS) -c -fpic -I$(INCLUDE_DIR) -o $(BUILD_DIR)/artifacts/$@ $<
diff --git a/include/osm/bind.h b/include/osm/bind.h
index 5107c52..e1627c6 100644
--- a/include/osm/bind.h
+++ b/include/osm/bind.h
@@ -1,3 +1,5 @@
+#ifndef OSM_BIND_H
+#define OSM_BIND_H
/**
* Bind to the next available onboard socket in the given directory
@@ -12,3 +14,4 @@ int osm_bind_local(int sockfd, const char *sock_dir);
*/
int osm_open_onboard(char *sock_dir);
+#endif
diff --git a/include/osm/device.h b/include/osm/device.h
new file mode 100644
index 0000000..51a4ff2
--- /dev/null
+++ b/include/osm/device.h
@@ -0,0 +1,10 @@
+#ifndef OSM_DEVICE_H
+#define OSM_DEVICE_H
+
+#include <osm/utils.h>
+
+typedef struct {
+
+} OSMDevice;
+
+#endif
diff --git a/include/osm/discover.h b/include/osm/discover.h
new file mode 100644
index 0000000..68bda60
--- /dev/null
+++ b/include/osm/discover.h
@@ -0,0 +1,9 @@
+#ifndef OSM_DISCOVER_H
+#define OSM_DISCOVER_H
+
+#include <osm/utils.h>
+
+Vector osm_discover_onboard(char *sock_dir);
+
+#endif
+
diff --git a/include/osm/utils.h b/include/osm/utils.h
new file mode 100644
index 0000000..5a53f41
--- /dev/null
+++ b/include/osm/utils.h
@@ -0,0 +1,52 @@
+#ifndef OSM_UTILS_H
+#define OSM_UTILS_H
+
+// Vector utilities
+
+/**
+ * Vector represents a dynamic array
+ */
+typedef struct {
+ unsigned int count, size, elsz;
+ void *data;
+} Vector;
+
+Vector vect_init(unsigned int elsz);
+
+/**
+ * Add an element to an arbitrary index in the vector
+ */
+void vect_add(Vector *vec, unsigned int index);
+
+/**
+ * Remove an element from an arbitrary index in the vector
+ */
+void vect_remove(Vector *vec, unsigned int index);
+
+/**
+ * Push an element to the end of the vector
+ */
+void vect_push(Vector *vec, void *el);
+
+/**
+ * Pop an element from the end of the vector
+ */
+void vect_pop(Vector *vec);
+
+/**
+ * Get an element from the vector
+ */
+void *vect_get(Vector *vec, unsigned int index);
+
+/**
+ * Set an element inside the vector
+ */
+void *vect_set(Vector *vec, unsigned int index, void *el);
+
+/**
+ * Remove all associated data from the vector
+ */
+void vect_end(Vector *vect);
+
+
+#endif
diff --git a/src/bind.c b/src/bind.c
index 2545003..786624c 100644
--- a/src/bind.c
+++ b/src/bind.c
@@ -7,6 +7,9 @@
#include <sys/socket.h>
#include <sys/un.h>
+
+#define MAX_ID 0xFFFF
+
/**
* The required characters to represent an id as a file name
* id - the id to get translated to a file name
@@ -78,19 +81,31 @@ int osm_bind_local(int sockfd, const char *sock_dir)
}
// sequentially try to bind
- int id = 0;
+ uint id = 0;
+ uint offset = offsetof(struct sockaddr_un, sun_path);
while(_need_chars(id) + len < sizeof(name.sun_path) - 1)
{
+ if (id > MAX_ID)
+ return -1;
+
// write file name
_write_name(id, name.sun_path + len);
// try to bind
- int size = offsetof(struct sockaddr_un, sun_path) + strlen(name.sun_path);
+ errno = 0;
+ int err = bind(sockfd, (struct sockaddr *) &name, offset + strlen(name.sun_path));
- if (bind(sockfd, (struct sockaddr *) &name, size) > 0)
+ if (err == 0)
{
+ printf("Bound!\n");
return 0;
}
+ else if (errno != EADDRINUSE)
+ {
+ // If not bound and it's not due to an address in use error
+ // then we have an actual problem
+ return -1;
+ }
// inc id
id++;
@@ -106,7 +121,7 @@ int osm_bind_local(int sockfd, const char *sock_dir)
*/
int osm_open_onboard(char *sock_dir)
{
- int sockfd = socket(AF_LOCAL, SOCK_DGRAM, 0);
+ int sockfd = socket(AF_LOCAL, SOCK_SEQPACKET, 0);
if (sockfd < 0)
{
perror("socket");
@@ -123,7 +138,7 @@ int osm_open_onboard(char *sock_dir)
bound = osm_bind_local(sockfd, sock_dir);
}
- if (bound < 0)
+ if (bound != 0)
{
perror("bind");
close(sockfd);
@@ -133,6 +148,19 @@ int osm_open_onboard(char *sock_dir)
return sockfd;
}
-
-
+/**
+ * Bind a new network socket
+ * Should only be called by one process on the machine.
+ *
+ * If more than one device needs to be exposed through this system, use a
+ * master process to handle internet traffic and export each device as a
+ * sub-device.
+ *
+ * return - a negative number on error, or the
+ */
+int osm_open_network()
+{
+ int sockfd = socket(AF_INET6, SOCK_STREAM, 0);
+ return sockfd;
+}
diff --git a/src/discover.c b/src/discover.c
new file mode 100644
index 0000000..1d8259a
--- /dev/null
+++ b/src/discover.c
@@ -0,0 +1,10 @@
+#include "osm/discover.h"
+
+Vector osm_discover_onboard(char *sock_fd)
+{
+ Vector out = {0};
+
+ return out;
+}
+
+
diff --git a/src/vector.c b/src/vector.c
new file mode 100644
index 0000000..72b2a4f
--- /dev/null
+++ b/src/vector.c
@@ -0,0 +1,101 @@
+#include "../include/osm/utils.h"
+#include <stdlib.h>
+#include <string.h>
+
+#define VECT_MAX_GROW 1024
+#define VECT_INIT_CAP 4
+
+/**
+ * Initialize a new vector
+ */
+Vector vect_init(unsigned int elsz)
+{
+ Vector out = {
+ .elsz = elsz,
+ .size = VECT_INIT_CAP,
+ .count = 0
+ };
+
+ out.data = malloc(out.elsz * out.size);
+
+ return out;
+}
+
+/**
+ * Unexported function for auto-growing the vector
+ */
+void _vect_grow(Vector *vec)
+{
+ if (vec->elsz == 0)
+ {
+ return;
+ }
+
+ if (vec->size == 0)
+ {
+ vec->size = VECT_INIT_CAP;
+ vec->data = realloc(vec->data, vec->size * vec->elsz);
+ return;
+ }
+
+ if (vec->size > VECT_MAX_GROW)
+ {
+ vec->size += VECT_MAX_GROW;
+ }
+ else
+ {
+ vec->size *= 2;
+ }
+
+ vec->data = realloc(vec->data, vec->size * vec->elsz);
+}
+
+/**
+ * Unexported function for auto-shrinking the vector
+ */
+void _vect_shrink(Vector *vec)
+{
+ if (vec->size == 1)
+ {
+ return;
+ }
+
+ vec->size /= 2;
+ vec->data = realloc(vec->data, vec->size * vec->elsz);
+}
+
+/**
+ * Push a new element to the end of the vector
+ */
+void vect_push(Vector *vec, void *el)
+{
+ memcpy(vec->data + (vec->count * vec->elsz), el, vec->elsz);
+ vec->count++;
+
+ if (vec->size == vec->count)
+ {
+ _vect_grow(vec);
+ }
+}
+
+/**
+ * Pop an element off of the end of the vector
+ */
+void vect_pop(Vector *vec)
+{
+ vec->count--;
+
+ if (vec->count < vec->size / 3)
+ {
+ _vect_shrink(vec);
+ }
+}
+
+void vect_end(Vector *vec)
+{
+ vec->size = 0;
+ vec->count = 0;
+ vec->elsz = 0;
+ free(vec->data);
+ vec->data = NULL;
+}