summaryrefslogtreecommitdiff
path: root/src/lib/cacep.c
diff options
context:
space:
mode:
authordimitri staessens <dimitri.staessens@intec.ugent.be>2017-01-12 14:58:49 +0100
committerdimitri staessens <dimitri.staessens@intec.ugent.be>2017-01-12 14:58:49 +0100
commit4e3c360377bc9b792c2fac83e9402d7531f79bbe (patch)
treee4409fb32ed7c391f48c3c2699997fc5b75137a1 /src/lib/cacep.c
parent7cad036f812a87aafd7c0e8ed4cbea76fb4cb097 (diff)
parent15e9b779385c71d366776b83540e19e0735c4e66 (diff)
downloadouroboros-4e3c360377bc9b792c2fac83e9402d7531f79bbe.tar.gz
ouroboros-4e3c360377bc9b792c2fac83e9402d7531f79bbe.zip
Merged in sandervrijders/ouroboros/be-gam (pull request #345)
Be gam
Diffstat (limited to 'src/lib/cacep.c')
-rw-r--r--src/lib/cacep.c170
1 files changed, 170 insertions, 0 deletions
diff --git a/src/lib/cacep.c b/src/lib/cacep.c
new file mode 100644
index 00000000..22c6137f
--- /dev/null
+++ b/src/lib/cacep.c
@@ -0,0 +1,170 @@
+/*
+ * Ouroboros - Copyright (C) 2016 - 2017
+ *
+ * The Common Application Connection Establishment Phase
+ *
+ * Sander Vrijders <sander.vrijders@intec.ugent.be>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+#include <ouroboros/config.h>
+#include <ouroboros/cacep.h>
+#include <ouroboros/dev.h>
+#include <ouroboros/errno.h>
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "cacep.pb-c.h"
+typedef Cacep cacep_t;
+
+#define BUF_SIZE 2048
+
+struct cacep {
+ int fd;
+ char * name;
+ uint64_t address;
+};
+
+struct cacep * cacep_create(int fd,
+ const char * name,
+ uint64_t address)
+{
+ struct cacep * tmp;
+
+ tmp = malloc(sizeof(*tmp));
+ if (tmp == NULL)
+ return NULL;
+
+ tmp->fd = fd;
+ tmp->address = address;
+ tmp->name = strdup(name);
+ if (tmp->name == NULL) {
+ free(tmp);
+ return NULL;
+ }
+
+ return tmp;
+}
+
+int cacep_destroy(struct cacep * instance)
+{
+ if (instance == NULL)
+ return 0;
+
+ free(instance);
+
+ return 0;
+}
+
+static struct cacep_info * read_msg(struct cacep * instance)
+{
+ struct cacep_info * tmp;
+ uint8_t buf[BUF_SIZE];
+ cacep_t * msg;
+ ssize_t len;
+
+ len = flow_read(instance->fd, buf, BUF_SIZE);
+ if (len < 0)
+ return NULL;
+
+ msg = cacep__unpack(NULL, len, buf);
+ if (msg == NULL)
+ return NULL;
+
+ tmp = malloc(sizeof(*tmp));
+ if (tmp == NULL) {
+ cacep__free_unpacked(msg, NULL);
+ return NULL;
+ }
+
+ tmp->addr = msg->address;
+ tmp->name = strdup(msg->name);
+ if (tmp->name == NULL) {
+ free(tmp);
+ cacep__free_unpacked(msg, NULL);
+ return NULL;
+ }
+
+ cacep__free_unpacked(msg, NULL);
+
+ return tmp;
+}
+
+static int send_msg(struct cacep * instance)
+{
+ cacep_t msg = CACEP__INIT;
+ int ret = 0;
+ uint8_t * data = NULL;
+ size_t len = 0;
+
+ msg.name = instance->name;
+ msg.address = instance->address;
+
+ len = cacep__get_packed_size(&msg);
+ if (len == 0)
+ return -1;
+
+ data = malloc(len);
+ if (data == NULL)
+ return -ENOMEM;
+
+ cacep__pack(&msg, data);
+
+ if (flow_write(instance->fd, data, len) < 0)
+ ret = -1;
+
+ free(data);
+
+ return ret;
+}
+
+struct cacep_info * cacep_auth(struct cacep * instance)
+{
+ struct cacep_info * tmp;
+
+ if (instance == NULL)
+ return NULL;
+
+ if (send_msg(instance))
+ return NULL;
+
+ tmp = read_msg(instance);
+ if (tmp == NULL)
+ return NULL;
+
+ return tmp;
+}
+
+struct cacep_info * cacep_auth_wait(struct cacep * instance)
+{
+ struct cacep_info * tmp;
+
+ if (instance == NULL)
+ return NULL;
+
+ tmp = read_msg(instance);
+ if (tmp == NULL)
+ return NULL;
+
+ if (send_msg(instance)) {
+ free(tmp->name);
+ free(tmp);
+ return NULL;
+ }
+
+ return tmp;
+}