summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bind.c42
-rw-r--r--src/discover.c10
-rw-r--r--src/vector.c101
3 files changed, 146 insertions, 7 deletions
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;
+}