diff options
author | Dimitri Staessens <[email protected]> | 2016-03-01 16:02:42 +0100 |
---|---|---|
committer | Dimitri Staessens <[email protected]> | 2016-03-01 16:02:42 +0100 |
commit | 7360b2c459ab31339aad20dbb438247aff088fd7 (patch) | |
tree | c449c27e62bb484e5f87272c37944615616c26b3 /src/lib/sockets.c | |
parent | 687055e46ddff677000d7e4e15c21ef9a1809b4d (diff) | |
parent | 52a5113760802e16b07d9d954237871d52cff70a (diff) | |
download | ouroboros-7360b2c459ab31339aad20dbb438247aff088fd7.tar.gz ouroboros-7360b2c459ab31339aad20dbb438247aff088fd7.zip |
Merge branch 'be' of bitbucket.org:ouroboros-rina/ouroboros into be-common
Diffstat (limited to 'src/lib/sockets.c')
-rw-r--r-- | src/lib/sockets.c | 228 |
1 files changed, 145 insertions, 83 deletions
diff --git a/src/lib/sockets.c b/src/lib/sockets.c index ef4b3a47..c0331063 100644 --- a/src/lib/sockets.c +++ b/src/lib/sockets.c @@ -31,8 +31,6 @@ #include <string.h> #include <malloc.h> -#define BUFFER_SIZE 256 - int client_socket_open(char * file_name) { int sockfd; @@ -146,6 +144,14 @@ static void deser_copy_int(uint8_t * data, deser_copy_value(sizeof(int), dst, data, offset); } +static void deser_copy_size_t(uint8_t * data, + size_t * dst, + int * offset) +{ + *dst = 0; + deser_copy_value(sizeof(size_t), dst, data, offset); +} + static void deser_copy_enum(uint8_t * data, enum irm_msg_code * dst, int * offset) @@ -160,13 +166,16 @@ buffer_t * serialize_irm_msg(struct irm_msg * msg) uint8_t * data; int offset = 0; int i; - char buffer[BUFFER_SIZE]; + char ** pos; + + if (msg == NULL) + return NULL; buf = malloc(sizeof(*buf)); if (buf == NULL) return NULL; - buf->data = malloc(BUFFER_SIZE); + buf->data = malloc(IRM_MSG_BUF_SIZE); if (buf->data == NULL) { free(buf); return NULL; @@ -174,47 +183,75 @@ buffer_t * serialize_irm_msg(struct irm_msg * msg) data = buf->data; - ser_copy_value(sizeof(enum irm_msg_code), - data, - &msg->code, - &offset); + ser_copy_value(sizeof(enum irm_msg_code), data, &msg->code, &offset); + + if (msg->name == NULL || + msg->name->ap_name == NULL || + msg->name->ae_name == NULL ) { + LOG_ERR("Null pointer passed"); + free(buf->data); + free(buf); + return NULL; + } + + /* + * Every IRM message passes the name, may change + * Move to separate function when it does + */ + ser_copy_value(strlen(msg->name->ap_name) + 1, data, + msg->name->ap_name, &offset); + + ser_copy_value(sizeof(int), data, &msg->name->api_id, &offset); + + ser_copy_value(strlen(msg->name->ae_name) + 1, data, + msg->name->ae_name, &offset); + + ser_copy_value(sizeof(int), data, &msg->name->aei_id, &offset); switch (msg->code) { case IRM_CREATE_IPCP: - if (!msg->msgs.create_ipcp.name || - !msg->msgs.create_ipcp.name->ap_name || - !msg->msgs.create_ipcp.name->ae_name || - !msg->msgs.create_ipcp.ipcp_type) { + if (!msg->ipcp_type) { LOG_ERR("Null pointer passed"); free(buf->data); free(buf); return NULL; } - ser_copy_value(strlen(msg->msgs.create_ipcp.name->ap_name) + 1, - data, - msg->msgs.create_ipcp.name->ap_name, - &offset); - - ser_copy_value(sizeof(int), - data, - &msg->msgs.create_ipcp.name->api_id, - &offset); - - ser_copy_value(strlen(msg->msgs.create_ipcp.name->ae_name) + 1, - data, - msg->msgs.create_ipcp.name->ae_name, - &offset); - - ser_copy_value(sizeof(int), - data, - &msg->msgs.create_ipcp.name->aei_id, - &offset); - - ser_copy_value(strlen(msg->msgs.create_ipcp.ipcp_type) + 1, - data, - msg->msgs.create_ipcp.ipcp_type, - &offset); + ser_copy_value(strlen(msg->ipcp_type) + 1, data, + msg->ipcp_type, &offset); + break; + case IRM_DESTROY_IPCP: + break; + case IRM_BOOTSTRAP_IPCP: + /* FIXME: Fields missing, need to define dif_info properly */ + break; + case IRM_ENROLL_IPCP: + if (msg->dif_name == NULL) { + free(buf->data); + free(buf); + return NULL; + } + + ser_copy_value(strlen(msg->dif_name) + 1, data, + msg->dif_name, &offset); + + break; + case IRM_REG_IPCP: + case IRM_UNREG_IPCP: + if (msg->difs == NULL || msg->difs[0] == NULL) { + free(buf->data); + free(buf); + return NULL; + } + + ser_copy_value(sizeof(size_t), data, &msg->difs_size, &offset); + + pos = msg->difs; + for (i = 0; i < msg->difs_size; i++) { + ser_copy_value(strlen(*pos) + 1, data, *pos, &offset); + pos++; + } + break; default: LOG_ERR("Don't know that code"); @@ -225,89 +262,114 @@ buffer_t * serialize_irm_msg(struct irm_msg * msg) buf->size = offset; - for (i = 0; i < buf->size; i++) { - if (i > 0) sprintf(buffer + strlen(buffer), ":"); - sprintf(buffer + strlen(buffer), "%02X", data[i]); - } - LOG_DBGF("Serialized buffer to %s", buffer); - return buf; } struct irm_msg * deserialize_irm_msg(buffer_t * data) { struct irm_msg * msg; - char buffer[BUFFER_SIZE]; - int i; + int i, j; int offset = 0; + size_t difs_size; - if (data == NULL || - data->data == NULL) { + if (data == NULL || data->data == NULL) { LOG_ERR("Got a null pointer"); return NULL; } - memset(buffer, 0, sizeof(buffer)); - for (i = 0; i < data->size; i++) { - if (i > 0) sprintf(buffer + strlen(buffer), ":"); - sprintf(buffer + strlen(buffer), "%02X", data->data[i]); - } - LOG_DBGF("Got buffer %s", buffer); - msg = malloc(sizeof(*msg)); if (msg == NULL) { LOG_ERR("Failed to allocate memory"); return NULL; } - deser_copy_enum(data->data, - &msg->code, - &offset); + deser_copy_enum(data->data, &msg->code, &offset); + + msg->name = malloc(sizeof(*(msg->name))); + if (msg->name == NULL) { + LOG_ERR("Failed to alloc memory"); + free(msg); + return NULL; + } + + if (deser_copy_string(data->data, + &msg->name->ap_name, + &offset)) { + free(msg->name); + free(msg); + return NULL; + } + + deser_copy_int(data->data, &msg->name->api_id, &offset); + + if (deser_copy_string(data->data, + &msg->name->ae_name, + &offset)) { + free(msg->name->ap_name); + free(msg->name); + free(msg); + return NULL; + } + + deser_copy_int(data->data, &msg->name->aei_id, &offset); switch (msg->code) { case IRM_CREATE_IPCP: - msg->msgs.create_ipcp.name = - malloc(sizeof(*(msg->msgs.create_ipcp.name))); - if (!msg->msgs.create_ipcp.name) { - LOG_ERR("Failed to alloc memory"); + if (deser_copy_string(data->data, + &msg->ipcp_type, + &offset)) { + free(msg->name->ae_name); + free(msg->name->ap_name); + free(msg->name); free(msg); return NULL; } + break; + case IRM_DESTROY_IPCP: + break; + case IRM_BOOTSTRAP_IPCP: + break; + case IRM_ENROLL_IPCP: if (deser_copy_string(data->data, - &msg->msgs.create_ipcp.name->ap_name, + &msg->dif_name, &offset)) { - free(msg->msgs.create_ipcp.name); + free(msg->name->ae_name); + free(msg->name->ap_name); + free(msg->name); free(msg); return NULL; } - deser_copy_int(data->data, - &msg->msgs.create_ipcp.name->api_id, - &offset); - - if (deser_copy_string(data->data, - &msg->msgs.create_ipcp.name->ae_name, - &offset)) { - free(msg->msgs.create_ipcp.name->ap_name); - free(msg->msgs.create_ipcp.name); + break; + case IRM_REG_IPCP: + case IRM_UNREG_IPCP: + deser_copy_size_t(data->data, &difs_size, &offset); + + msg->difs = malloc(sizeof(*(msg->difs)) * difs_size); + if (msg->difs == NULL) { + free(msg->name->ae_name); + free(msg->name->ap_name); + free(msg->name); free(msg); return NULL; } - deser_copy_int(data->data, - &msg->msgs.create_ipcp.name->aei_id, - &offset); - - if (deser_copy_string(data->data, - &msg->msgs.create_ipcp.ipcp_type, - &offset)) { - free(msg->msgs.create_ipcp.name->ae_name); - free(msg->msgs.create_ipcp.name->ap_name); - free(msg->msgs.create_ipcp.name); - free(msg); - return NULL; + for (i = 0; i < difs_size; i++) { + if (deser_copy_string(data->data, + &msg->difs[i], + &offset)) { + for (j = 0; j < i; j++) + free(msg->difs[j]); + free(msg->difs); + free(msg->name->ae_name); + free(msg->name->ap_name); + free(msg->name); + free(msg); + return NULL; + } } + break; default: LOG_ERR("Don't know that code"); |