diff options
author | Sander Vrijders <sander.vrijders@intec.ugent.be> | 2016-02-26 11:08:55 +0100 |
---|---|---|
committer | Sander Vrijders <sander.vrijders@intec.ugent.be> | 2016-02-26 11:44:38 +0100 |
commit | dff374d5f393a6b4bec4877aa1c35ceb70e82843 (patch) | |
tree | 64e0e7804f1ee19834f019478c7199e83512c726 | |
parent | 63633c57b1e2573890fa627dd63f7c79ee5777b8 (diff) | |
download | ouroboros-dff374d5f393a6b4bec4877aa1c35ceb70e82843.tar.gz ouroboros-dff374d5f393a6b4bec4877aa1c35ceb70e82843.zip |
lib, irmd: Address Dimitri's comments
This addresses several comments Dimitri had on the sockets layer code.
-rw-r--r-- | include/ouroboros/sockets.h | 5 | ||||
-rw-r--r-- | src/irmd/main.c | 10 | ||||
-rw-r--r-- | src/lib/irm.c | 4 | ||||
-rw-r--r-- | src/lib/sockets.c | 70 |
4 files changed, 64 insertions, 25 deletions
diff --git a/include/ouroboros/sockets.h b/include/ouroboros/sockets.h index ad9bd408..fe7ddb28 100644 --- a/include/ouroboros/sockets.h +++ b/include/ouroboros/sockets.h @@ -20,6 +20,9 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +#ifndef OUROBOROS_SOCKETS_H +#define OUROBOROS_SOCKETS_H + #define IRM_SOCK_PATH "/tmp/irm_sock" enum irm_msg_code { @@ -47,3 +50,5 @@ int server_socket_open(char * file_name); buffer_t * serialize_irm_msg(struct irm_msg * msg); struct irm_msg * deserialize_irm_msg(buffer_t * data); + +#endif diff --git a/src/irmd/main.c b/src/irmd/main.c index d2aa6cd9..137b2b61 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -29,6 +29,7 @@ #include <sys/socket.h> #include <sys/un.h> #include <stdlib.h> +#include <errno.h> #define BUF_SIZE 256 @@ -55,10 +56,10 @@ int main() if (sockfd < 0) return -1; - buf = malloc(sizeof(buf) * BUF_SIZE); - if (!buf) { + buf = malloc(sizeof(*buf) * BUF_SIZE); + if (buf == NULL) { LOG_ERR("Cannot allocate memory"); - return -1; + return -ENOMEM; } while (true) { @@ -70,6 +71,7 @@ int main() cli_sockfd = accept(sockfd, 0, 0); if (cli_sockfd < 0) { LOG_ERR("Cannot accept new connection"); + continue; } count = read(cli_sockfd, buf, BUF_SIZE); @@ -77,7 +79,7 @@ int main() buffer.size = count; buffer.data = buf; msg = deserialize_irm_msg(&buffer); - if (!msg) + if (msg == NULL) continue; LOG_DBG("Got message code %d", msg->code); diff --git a/src/lib/irm.c b/src/lib/irm.c index 1af8ed2c..2c9b530c 100644 --- a/src/lib/irm.c +++ b/src/lib/irm.c @@ -34,7 +34,7 @@ int irm_create_ipcp(rina_name_t name, struct irm_msg msg; buffer_t * buf; - if (!ipcp_type) + if (ipcp_type == NULL) return -1; sockfd = client_socket_open(IRM_SOCK_PATH); @@ -46,7 +46,7 @@ int irm_create_ipcp(rina_name_t name, msg.msgs.create_ipcp.ipcp_type = ipcp_type; buf = serialize_irm_msg(&msg); - if (!buf) + if (buf == NULL) return -1; write(sockfd, buf->data, buf->size); diff --git a/src/lib/sockets.c b/src/lib/sockets.c index 9985cd56..095c9e5c 100644 --- a/src/lib/sockets.c +++ b/src/lib/sockets.c @@ -103,7 +103,7 @@ static int serialized_string_len(uint8_t * data) while (*seek != '\0') seek++; - return seek - data + 1; + return (seek - data) + 1; } static void ser_copy_value(size_t flen, @@ -124,15 +124,18 @@ static void deser_copy_value(size_t flen, *offset += flen; } -static void deser_copy_string(uint8_t * data, - char ** dst, - int * offset) +static int deser_copy_string(uint8_t * data, + char ** dst, + int * offset) { size_t flen; flen = serialized_string_len(data + *offset); - *dst = malloc(flen + 1); + *dst = malloc(sizeof(**dst) * (flen + 1)); + if (*dst == NULL) + return -1; deser_copy_value(flen, *dst, data, offset); + return 0; } static void deser_copy_int(uint8_t * data, @@ -159,8 +162,16 @@ buffer_t * serialize_irm_msg(struct irm_msg * msg) int i; char buffer[BUFFER_SIZE]; - buf = malloc(sizeof(buf)); + buf = malloc(sizeof(*buf)); + if (buf == NULL) + return NULL; + buf->data = malloc(BUFFER_SIZE); + if (buf->data == NULL) { + free(buf); + return NULL; + } + data = buf->data; ser_copy_value(sizeof(enum irm_msg_code), @@ -230,7 +241,8 @@ struct irm_msg * deserialize_irm_msg(buffer_t * data) int i; int offset = 0; - if (!data || !data->data) { + if (data == NULL || + data->data == NULL) { LOG_ERR("Got a null pointer"); return NULL; } @@ -242,8 +254,8 @@ struct irm_msg * deserialize_irm_msg(buffer_t * data) } LOG_DBGF("Got buffer %s", buffer); - msg = malloc(sizeof(msg)); - if (!msg) { + msg = malloc(sizeof(*msg)); + if (msg == NULL) { LOG_ERR("Failed to allocate memory"); return NULL; } @@ -255,27 +267,47 @@ struct irm_msg * deserialize_irm_msg(buffer_t * data) switch (msg->code) { case IRM_CREATE_IPCP: msg->msgs.create_ipcp.name = - malloc(sizeof(msg->msgs.create_ipcp.name)); + malloc(sizeof(*(msg->msgs.create_ipcp.name))); + if (!msg->msgs.create_ipcp.name) { + LOG_ERR("Failed to alloc memory"); + free(msg); + return NULL; + } - deser_copy_string(data->data, - &msg->msgs.create_ipcp.name->ap_name, - &offset); + if (deser_copy_string(data->data, + &msg->msgs.create_ipcp.name->ap_name, + &offset)) { + free(msg->msgs.create_ipcp.name); + free(msg); + return NULL; + } deser_copy_int(data->data, &msg->msgs.create_ipcp.name->api_id, &offset); - deser_copy_string(data->data, - &msg->msgs.create_ipcp.name->ae_name, - &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); + free(msg); + return NULL; + } deser_copy_int(data->data, &msg->msgs.create_ipcp.name->aei_id, &offset); - deser_copy_string(data->data, - &msg->msgs.create_ipcp.ipcp_type, - &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; + } break; default: LOG_ERR("Don't know that code"); |