From 45ad14035a06e4947b1cc1d908bb665646c1f2a0 Mon Sep 17 00:00:00 2001 From: Sander Vrijders Date: Tue, 1 Mar 2016 15:08:37 +0100 Subject: lib, irmd, tools: Provide more IRM messages This provides the other messages that are used to communicate between the library and the IRM Daemon. The IRM tool just calls the library right now to see if it works. A full fledged program will be provided in a next commit. --- src/lib/irm.c | 116 +++++++++++++++++++-------- src/lib/sockets.c | 228 ++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 229 insertions(+), 115 deletions(-) (limited to 'src/lib') diff --git a/src/lib/irm.c b/src/lib/irm.c index 69a98039..97000029 100644 --- a/src/lib/irm.c +++ b/src/lib/irm.c @@ -27,81 +27,133 @@ #include #include +static int send_irm_msg(struct irm_msg * msg) +{ + int sockfd; + buffer_t * buf; + + sockfd = client_socket_open(IRM_SOCK_PATH); + if (sockfd < 0) + return -1; + + buf = serialize_irm_msg(msg); + if (buf == NULL) { + close(sockfd); + return -1; + } + + if (write(sockfd, buf->data, buf->size) == -1) { + close(sockfd); + return -1; + } + + close(sockfd); + return 0; +} + int irm_create_ipcp(rina_name_t name, char * ipcp_type) { - int sockfd; struct irm_msg msg; - buffer_t * buf; if (ipcp_type == NULL) return -1; - sockfd = client_socket_open(IRM_SOCK_PATH); - if (sockfd < 0) - return -1; - msg.code = IRM_CREATE_IPCP; - msg.msgs.create_ipcp.name = &name; - msg.msgs.create_ipcp.ipcp_type = ipcp_type; - - buf = serialize_irm_msg(&msg); - if (buf == NULL) { - close(sockfd); - return -1; - } + msg.name = &name; + msg.ipcp_type = ipcp_type; - if (write(sockfd, buf->data, buf->size) == -1) { - close(sockfd); + if (send_irm_msg(&msg)) { + LOG_ERR("Failed to send message to daemon"); return -1; } - close(sockfd); return 0; } -int irm_destroy_ipcp(int ipcp_id) +int irm_destroy_ipcp(rina_name_t name) { + struct irm_msg msg; + + msg.code = IRM_DESTROY_IPCP; + msg.name = &name; + + if (send_irm_msg(&msg)) { + LOG_ERR("Failed to send message to daemon"); + return -1; + } return 0; } -int irm_bootstrap_ipcp(int ipcp_id, +int irm_bootstrap_ipcp(rina_name_t name, struct dif_info info) { + struct irm_msg msg; + + msg.code = IRM_BOOTSTRAP_IPCP; + msg.name = &name; + msg.info = &info; + + if (send_irm_msg(&msg)) { + LOG_ERR("Failed to send message to daemon"); + return -1; + } return 0; } -int irm_enroll_ipcp(int ipcp_id, +int irm_enroll_ipcp(rina_name_t name, char * dif_name) { + struct irm_msg msg; - return 0; -} + msg.code = IRM_ENROLL_IPCP; + msg.name = &name; + msg.dif_name = dif_name; -int irm_reg_ipcp(int ipcp_id, - char ** difs) -{ + if (send_irm_msg(&msg)) { + LOG_ERR("Failed to send message to daemon"); + return -1; + } return 0; } -int irm_unreg_ipcp(int ipcp_id, - char ** difs) +int irm_reg_ipcp(rina_name_t name, + char ** difs, + size_t difs_size) { + struct irm_msg msg; - return 0; -} + msg.code = IRM_REG_IPCP; + msg.name = &name; + msg.difs = difs; + msg.difs_size = difs_size; -char ** irm_list_ipcps() -{ + if (send_irm_msg(&msg)) { + LOG_ERR("Failed to send message to daemon"); + return -1; + } return 0; } -char ** irm_list_ipcp_types() +int irm_unreg_ipcp(rina_name_t name, + char ** difs, + size_t difs_size) { + struct irm_msg msg; + + msg.code = IRM_UNREG_IPCP; + msg.name = &name; + msg.difs = difs; + msg.difs_size = difs_size; + + if (send_irm_msg(&msg)) { + LOG_ERR("Failed to send message to daemon"); + return -1; + } return 0; } 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 #include -#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"); -- cgit v1.2.3