diff options
author | dimitri staessens <dimitri.staessens@ugent.be> | 2017-02-21 08:02:37 +0100 |
---|---|---|
committer | dimitri staessens <dimitri.staessens@ugent.be> | 2017-02-21 11:55:36 +0100 |
commit | adc6766221327f99ab484d66f6f92050ec9e62d7 (patch) | |
tree | cd0facb94108552b0b9e75619af4e42d77be1a17 /src/lib/pol/cacep_anonymous_auth.c | |
parent | 354554c76cc2f9f30c7fd8edaeb2e3cc91c85332 (diff) | |
download | ouroboros-adc6766221327f99ab484d66f6f92050ec9e62d7.tar.gz ouroboros-adc6766221327f99ab484d66f6f92050ec9e62d7.zip |
lib: Exchange protocol info during CACEP
This exchanges a protocol name, a protocol version and concrete syntax
for the protocol upon CACEP. For CDAP, only version 1 and GPB are
supported. No lists for other supported versions or syntaxes are
exchanged (but the proto file supports it). CACEP fails if there is a
mismatch between the protocol names, version and syntax specified by
the communicating parties.
Diffstat (limited to 'src/lib/pol/cacep_anonymous_auth.c')
-rw-r--r-- | src/lib/pol/cacep_anonymous_auth.c | 129 |
1 files changed, 123 insertions, 6 deletions
diff --git a/src/lib/pol/cacep_anonymous_auth.c b/src/lib/pol/cacep_anonymous_auth.c index d450fdc5..1ad8a533 100644 --- a/src/lib/pol/cacep_anonymous_auth.c +++ b/src/lib/pol/cacep_anonymous_auth.c @@ -24,7 +24,10 @@ #include <ouroboros/config.h> #include <ouroboros/cacep.h> #include <ouroboros/time_utils.h> +#include <ouroboros/dev.h> +#include <ouroboros/errno.h> +#include "cacep_proto.h" #include "cacep_anonymous_auth.h" #include <stdlib.h> @@ -32,6 +35,11 @@ #include <string.h> #include <stdio.h> +#include "cacep_anonymous_auth.pb-c.h" +typedef CacepAnonymousAuthMsg cacep_anonymous_auth_msg_t; +typedef CacepProtoMsg cacep_proto_msg_t; + +#define BUF_SIZE 2048 #define NAME_LEN 8 /* this policy generates a hex string */ @@ -61,21 +69,130 @@ static struct cacep_info * anonymous_info(void) return info; } +static struct cacep_info * read_msg(int fd) +{ + struct cacep_info * tmp; + uint8_t buf[BUF_SIZE]; + cacep_anonymous_auth_msg_t * msg; + ssize_t len; + + len = flow_read(fd, buf, BUF_SIZE); + if (len < 0) + return NULL; + + msg = cacep_anonymous_auth_msg__unpack(NULL, len, buf); + if (msg == NULL) + return NULL; + + tmp = anonymous_info(); + if (tmp == NULL) { + cacep_anonymous_auth_msg__free_unpacked(msg, NULL); + return NULL; + } + + tmp->proto.protocol = strdup(msg->proto->protocol); + if (tmp->proto.protocol == NULL) { + free(tmp); + cacep_anonymous_auth_msg__free_unpacked(msg, NULL); + return NULL; + } + + tmp->proto.pref_version = msg->proto->pref_version; + tmp->proto.pref_syntax = code_to_syntax(msg->proto->pref_syntax); + if (tmp->proto.pref_syntax < 0) { + free(tmp->proto.protocol); + free(tmp); + cacep_anonymous_auth_msg__free_unpacked(msg, NULL); + return NULL; + } + + cacep_anonymous_auth_msg__free_unpacked(msg, NULL); + + return tmp; +} + +static int send_msg(int fd, + const struct cacep_info * info) +{ + cacep_anonymous_auth_msg_t msg = CACEP_ANONYMOUS_AUTH_MSG__INIT; + cacep_proto_msg_t cmsg = CACEP_PROTO_MSG__INIT; + int ret = 0; + uint8_t * data = NULL; + size_t len = 0; + + cmsg.protocol = info->proto.protocol; + cmsg.pref_version = info->proto.pref_version; + cmsg.pref_syntax = syntax_to_code(info->proto.pref_syntax); + if (cmsg.pref_syntax < 0) + return -1; + + msg.proto = &cmsg; + + len = cacep_anonymous_auth_msg__get_packed_size(&msg); + if (len == 0) + return -1; + + data = malloc(len); + if (data == NULL) + return -ENOMEM; + + cacep_anonymous_auth_msg__pack(&msg, data); + + if (flow_write(fd, data, len) < 0) + ret = -1; + + free(data); + + return ret; +} + struct cacep_info * cacep_anonymous_auth(int fd, const struct cacep_info * info) { - (void) fd; - (void) info; + struct cacep_info * tmp; - return anonymous_info(); + if (send_msg(fd, info)) + return NULL; + + tmp = read_msg(fd); + if (tmp == NULL) + return NULL; + + if (strcmp(info->proto.protocol, tmp->proto.protocol) || + info->proto.pref_version != tmp->proto.pref_version || + info->proto.pref_syntax != tmp->proto.pref_syntax) { + free(tmp); + return NULL; + } + + tmp->data = NULL; + + return tmp; } struct cacep_info * cacep_anonymous_auth_wait(int fd, const struct cacep_info * info) { - (void) fd; - (void) info; + struct cacep_info * tmp; + + tmp = read_msg(fd); + if (tmp == NULL) + return NULL; + + if (send_msg(fd, info)) { + free(tmp); + return NULL; + } + + if (strcmp(info->proto.protocol, tmp->proto.protocol) || + info->proto.pref_version != tmp->proto.pref_version || + info->proto.pref_syntax != tmp->proto.pref_syntax) { + free(tmp); + return NULL; + } + + tmp->data = NULL; - return anonymous_info(); + return tmp; } |