summaryrefslogtreecommitdiff
path: root/src/lib/cacep.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/cacep.c')
-rw-r--r--src/lib/cacep.c160
1 files changed, 55 insertions, 105 deletions
diff --git a/src/lib/cacep.c b/src/lib/cacep.c
index 00557444..a2c5c3d2 100644
--- a/src/lib/cacep.c
+++ b/src/lib/cacep.c
@@ -1,9 +1,10 @@
/*
* Ouroboros - Copyright (C) 2016 - 2017
*
- * The Common Application Connection Establishment Phase
+ * The Common Application Connection Establishment Protocol
*
- * Sander Vrijders <sander.vrijders@intec.ugent.be>
+ * Dimitri Staessens <dimitri.staessens@ugent.be>
+ * Sander Vrijders <sander.vrijders@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
@@ -29,93 +30,53 @@
#include <string.h>
#include "cacep.pb-c.h"
-typedef Cacep cacep_t;
+typedef CacepMsg cacep_msg_t;
-#define BUF_SIZE 2048
+#define BUF_SIZE 64
-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->name);
- free(instance);
-
- return 0;
-}
-
-static struct cacep_info * read_msg(struct cacep * instance)
+int read_msg(int fd,
+ struct conn_info * info)
{
- struct cacep_info * tmp;
- uint8_t buf[BUF_SIZE];
- cacep_t * msg;
- ssize_t len;
+ uint8_t buf[BUF_SIZE];
+ cacep_msg_t * msg;
+ ssize_t len;
- len = flow_read(instance->fd, buf, BUF_SIZE);
+ len = flow_read(fd, buf, BUF_SIZE);
if (len < 0)
- return NULL;
+ return -1;
- msg = cacep__unpack(NULL, len, buf);
+ msg = cacep_msg__unpack(NULL, len, buf);
if (msg == NULL)
- return NULL;
+ return -1;
- tmp = malloc(sizeof(*tmp));
- if (tmp == NULL) {
- cacep__free_unpacked(msg, NULL);
- return NULL;
- }
+ strcpy(info->ae_name, msg->ae_name);
+ strcpy(info->protocol, msg->protocol);
- tmp->addr = msg->address;
- tmp->name = strdup(msg->name);
- if (tmp->name == NULL) {
- free(tmp);
- cacep__free_unpacked(msg, NULL);
- return NULL;
- }
+ info->pref_version = msg->pref_version;
+ info->pref_syntax = msg->pref_syntax;
+ info->addr = msg->address;
- cacep__free_unpacked(msg, NULL);
+ cacep_msg__free_unpacked(msg, NULL);
- return tmp;
+ return 0;
}
-static int send_msg(struct cacep * instance)
+static int send_msg(int fd,
+ const struct conn_info * info)
{
- cacep_t msg = CACEP__INIT;
- int ret = 0;
- uint8_t * data = NULL;
- size_t len = 0;
-
- msg.name = instance->name;
- msg.address = instance->address;
+ cacep_msg_t msg = CACEP_MSG__INIT;
+ uint8_t * data = NULL;
+ size_t len = 0;
+
+ msg.ae_name = (char *) info->ae_name;
+ msg.protocol = (char *) info->protocol;
+ msg.address = info->addr;
+ msg.pref_version = info->pref_version;
+ msg.pref_syntax = info->pref_syntax;
+ if (msg.pref_syntax < 0)
+ return -1;
- len = cacep__get_packed_size(&msg);
+ len = cacep_msg__get_packed_size(&msg);
if (len == 0)
return -1;
@@ -123,49 +84,38 @@ static int send_msg(struct cacep * instance)
if (data == NULL)
return -ENOMEM;
- cacep__pack(&msg, data);
+ cacep_msg__pack(&msg, data);
- if (flow_write(instance->fd, data, len) < 0)
- ret = -1;
+ if (flow_write(fd, data, len) < 0) {
+ free(data);
+ return -1;
+ }
free(data);
- return ret;
+ return 0;
}
-struct cacep_info * cacep_auth(struct cacep * instance)
+int cacep_snd(int fd,
+ const struct conn_info * in)
{
- struct cacep_info * tmp;
-
- if (instance == NULL)
- return NULL;
-
- if (send_msg(instance))
- return NULL;
+ if (in == NULL)
+ return -EINVAL;
- tmp = read_msg(instance);
- if (tmp == NULL)
- return NULL;
+ if (send_msg(fd, in))
+ return -1;
- return tmp;
+ return 0;
}
-struct cacep_info * cacep_auth_wait(struct cacep * instance)
+int cacep_rcv(int fd,
+ struct conn_info * out)
{
- struct cacep_info * tmp;
-
- if (instance == NULL)
- return NULL;
-
- tmp = read_msg(instance);
- if (tmp == NULL)
- return NULL;
+ if (out == NULL)
+ return -EINVAL;
- if (send_msg(instance)) {
- free(tmp->name);
- free(tmp);
- return NULL;
- }
+ if (read_msg(fd, out))
+ return -1;
- return tmp;
+ return 0;
}