diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/dev.c | 125 | ||||
-rw-r--r-- | src/lib/sockets.c | 469 |
2 files changed, 125 insertions, 469 deletions
diff --git a/src/lib/dev.c b/src/lib/dev.c index 7c0c8a15..5c11d8bf 100644 --- a/src/lib/dev.c +++ b/src/lib/dev.c @@ -32,30 +32,29 @@ int ap_reg(char * ap_name, char ** difs, size_t difs_size) { - struct irm_msg msg; - struct irm_msg * recv_msg = NULL; + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; int fd = 0; if (ap_name == NULL || difs == NULL || - difs_size == 0) { - LOG_ERR("Invalid arguments"); - return -1; + difs_size == 0 || + difs[0] == NULL) { + return -EINVAL; } - msg.code = IRM_AP_REG; + msg.code = IRM_MSG_CODE__IRM_AP_REG; msg.ap_name = ap_name; - msg.difs = difs; - msg.difs_size = difs_size; + msg.dif_name = difs; + msg.n_dif_name = difs_size; - recv_msg = send_recv_irmd_msg(&msg); + recv_msg = send_recv_irm_msg(&msg); if (recv_msg == NULL) { - LOG_ERR("Failed to send and receive message"); return -1; } fd = recv_msg->fd; - free(recv_msg); + irm_msg__free_unpacked(recv_msg, NULL); return fd; } @@ -64,21 +63,21 @@ int ap_unreg(char * ap_name, char ** difs, size_t difs_size) { - struct irm_msg msg; + irm_msg_t msg = IRM_MSG__INIT; if (ap_name == NULL || difs == NULL || - difs_size == 0) { - LOG_ERR("Invalid arguments"); - return -1; + difs_size == 0 || + difs[0] == NULL) { + return -EINVAL; } - msg.code = IRM_AP_UNREG; + msg.code = IRM_MSG_CODE__IRM_AP_UNREG; msg.ap_name = ap_name; - msg.difs = difs; - msg.difs_size = difs_size; + msg.dif_name = difs; + msg.n_dif_name = difs_size; - if (send_irmd_msg(&msg)) { + if (send_irm_msg(&msg)) { LOG_ERR("Failed to send message to daemon"); return -1; } @@ -90,45 +89,47 @@ int flow_accept(int fd, char * ap_name, char * ae_name) { - struct irm_msg msg; - struct irm_msg * recv_msg = NULL; + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; int cli_fd = 0; if (ap_name == NULL) { - LOG_ERR("Invalid arguments"); - return -1; + return -EINVAL; } - msg.code = IRM_FLOW_ACCEPT; + msg.code = IRM_MSG_CODE__IRM_FLOW_ACCEPT; + msg.has_fd = true; msg.fd = fd; - recv_msg = send_recv_irmd_msg(&msg); + recv_msg = send_recv_irm_msg(&msg); if (recv_msg == NULL) { - LOG_ERR("Failed to send and receive message"); return -1; } + if (recv_msg->has_fd == false) { + irm_msg__free_unpacked(recv_msg, NULL); + return -1; + } cli_fd = recv_msg->fd; ap_name = recv_msg->ap_name; - if (ae_name == NULL) - ae_name = ""; - else - ae_name = recv_msg->ae_name; - free(recv_msg); + ae_name = recv_msg->ae_name; + irm_msg__free_unpacked(recv_msg, NULL); return cli_fd; } int flow_alloc_resp(int fd, int result) { - struct irm_msg msg; + irm_msg_t msg = IRM_MSG__INIT; - msg.code = IRM_FLOW_ALLOC_RESP; + msg.code = IRM_MSG_CODE__IRM_FLOW_ALLOC_RESP; + msg.has_fd = true; msg.fd = fd; + msg.has_result = true; msg.result = result; - if (send_irmd_msg(&msg)) { + if (send_irm_msg(&msg)) { LOG_ERR("Failed to send message to daemon"); return -1; } @@ -142,67 +143,74 @@ int flow_alloc(char * dst_ap_name, struct qos_spec * qos, int oflags) { - struct irm_msg msg; - struct irm_msg * recv_msg = NULL; + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; int fd = 0; if (dst_ap_name == NULL || - src_ap_name == NULL) { + src_ap_name == NULL || + qos == NULL) { LOG_ERR("Invalid arguments"); return -1; } - msg.code = IRM_FLOW_ALLOC; + msg.code = IRM_MSG_CODE__IRM_FLOW_ALLOC; msg.dst_ap_name = dst_ap_name; msg.ap_name = src_ap_name; - if (src_ae_name == NULL) - msg.ae_name = ""; - else - msg.ae_name = src_ae_name; - msg.qos = qos; + msg.ae_name = src_ae_name; + msg.has_oflags = true; msg.oflags = oflags; - recv_msg = send_recv_irmd_msg(&msg); + recv_msg = send_recv_irm_msg(&msg); if (recv_msg == NULL) { - LOG_ERR("Failed to send and receive message"); return -1; } - fd = recv_msg->fd; - free(recv_msg); + if (recv_msg->has_fd == false) { + irm_msg__free_unpacked(recv_msg, NULL); + return -1; + } + fd = recv_msg->fd; + irm_msg__free_unpacked(recv_msg, NULL); return fd; } int flow_alloc_res(int fd) { - struct irm_msg msg; - struct irm_msg * recv_msg = NULL; + irm_msg_t msg = IRM_MSG__INIT; + irm_msg_t * recv_msg = NULL; int result = 0; - msg.code = IRM_FLOW_ALLOC_RES; + msg.code = IRM_MSG_CODE__IRM_FLOW_ALLOC_RES; + msg.has_fd = true; msg.fd = fd; - recv_msg = send_recv_irmd_msg(&msg); + recv_msg = send_recv_irm_msg(&msg); if (recv_msg == NULL) { - LOG_ERR("Failed to send and receive message"); + return -1; + } + + if (recv_msg->has_result == false) { + irm_msg__free_unpacked(recv_msg, NULL); return -1; } result = recv_msg->result; - free(recv_msg); + irm_msg__free_unpacked(recv_msg, NULL); return result; } int flow_dealloc(int fd) { - struct irm_msg msg; + irm_msg_t msg = IRM_MSG__INIT; - msg.code = IRM_FLOW_DEALLOC; + msg.code = IRM_MSG_CODE__IRM_FLOW_DEALLOC; + msg.has_fd = true; msg.fd = fd; - if (send_irmd_msg(&msg)) { + if (send_irm_msg(&msg)) { LOG_ERR("Failed to send message to daemon"); return -1; } @@ -212,12 +220,13 @@ int flow_dealloc(int fd) int flow_cntl(int fd, int oflags) { - struct irm_msg msg; + irm_msg_t msg = IRM_MSG__INIT; + msg.has_fd = true; msg.fd = fd; msg.oflags = oflags; - if (send_irmd_msg(&msg)) { + if (send_irm_msg(&msg)) { LOG_ERR("Failed to send message to daemon"); return -1; } diff --git a/src/lib/sockets.c b/src/lib/sockets.c index b98f6bbc..fcd5d55a 100644 --- a/src/lib/sockets.c +++ b/src/lib/sockets.c @@ -96,83 +96,94 @@ int server_socket_open(char * file_name) return sockfd; } -int send_irmd_msg(struct irm_msg * msg) +int send_irm_msg(irm_msg_t * 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) { - free(buf->data); - free(buf); - close(sockfd); - return -1; - } - - free(buf->data); - free(buf); - - close(sockfd); - return 0; + int sockfd; + buffer_t buf; + + sockfd = client_socket_open(IRM_SOCK_PATH); + if (sockfd < 0) + return -1; + + buf.size = irm_msg__get_packed_size(msg); + if (buf.size == 0) { + close(sockfd); + return -1; + } + + LOG_DBG("Size will be %lu", buf.size); + buf.data = malloc(buf.size); + if (buf.data == NULL) { + close(sockfd); + return -ENOMEM; + } + + irm_msg__pack(msg, buf.data); + + if (write(sockfd, buf.data, buf.size) == -1) { + free(buf.data); + close(sockfd); + return -1; + } + + free(buf.data); + + close(sockfd); + return 0; } -struct irm_msg * send_recv_irmd_msg(struct irm_msg * msg) +irm_msg_t * send_recv_irm_msg(irm_msg_t * msg) { int sockfd; - buffer_t * buf; + buffer_t buf; ssize_t count = 0; - struct irm_msg * recv_msg = NULL; + irm_msg_t * recv_msg = NULL; sockfd = client_socket_open(IRM_SOCK_PATH); if (sockfd < 0) return NULL; - buf = serialize_irm_msg(msg); - if (buf == NULL) { + buf.size = irm_msg__get_packed_size(msg); + if (buf.size == 0) { close(sockfd); return NULL; } - if (write(sockfd, buf->data, buf->size) == -1) { - free(buf->data); - free(buf); + LOG_DBG("Size will be %lu", buf.size); + buf.data = malloc(buf.size); + if (buf.data == NULL) { close(sockfd); return NULL; } - count = read(sockfd, buf->data, IRM_MSG_BUF_SIZE); - if (count <= 0) { - free(buf->data); - free(buf); + irm_msg__pack(msg, buf.data); + + if (write(sockfd, buf.data, buf.size) == -1) { + free(buf.data); close(sockfd); return NULL; } - recv_msg = deserialize_irm_msg(buf); + count = read(sockfd, buf.data, IRM_MSG_BUF_SIZE); + if (count <= 0) { + free(buf.data); + close(sockfd); + return NULL; + } - free(buf->data); - free(buf); + recv_msg = irm_msg__unpack(NULL, count, buf.data); + if (recv_msg == NULL) { + free(buf.data); + close(sockfd); + return NULL; + } + free(buf.data); close(sockfd); return recv_msg; } -int send_irm_msg(irm_msg_t * msg) -{ - - return -1; -} - char * ipcp_sock_path(pid_t pid) { char * full_name = NULL; @@ -223,16 +234,6 @@ static void ser_copy_value(size_t flen, *offset += flen; } -static void ser_copy_name(rina_name_t * name, - uint8_t * data, - int * offset) -{ - ser_copy_value(strlen(name->ap_name) + 1, - data, name->ap_name, offset); - ser_copy_value(sizeof(int), data, - &name->api_id, offset); -} - static void deser_copy_value(size_t flen, void * dst, void * src, @@ -256,14 +257,6 @@ static int deser_copy_string(uint8_t * data, return 0; } -static void deser_copy_int(uint8_t * data, - unsigned int * dst, - int * offset) -{ - *dst = 0; - deser_copy_value(sizeof(int), dst, data, offset); -} - static void deser_copy_size_t(uint8_t * data, size_t * dst, int * offset) @@ -272,28 +265,6 @@ static void deser_copy_size_t(uint8_t * data, deser_copy_value(sizeof(size_t), dst, data, offset); } -static rina_name_t * deser_copy_name(uint8_t * data, - int * offset) -{ - rina_name_t * name; - - name = name_create(); - if (name == NULL) { - LOG_ERR("Failed to alloc memory"); - return NULL; - } - - if (deser_copy_string(data, &name->ap_name, offset)) { - name_destroy(name); - return NULL; - } - - deser_copy_int(data, &name->api_id, offset); - - return name; -} - - /* Move these to a separate file? */ static buffer_t * buffer_create() { @@ -321,330 +292,6 @@ static void buffer_destroy(buffer_t * buf) free(buf); } -buffer_t * serialize_irm_msg(struct irm_msg * msg) -{ - buffer_t * buf; - uint8_t * data; - int offset = 0; - int i; - char ** pos; - - if (msg == NULL) - return NULL; - - buf = buffer_create(); - if (buf == NULL) - return NULL; - - data = buf->data; - - ser_copy_value(sizeof(enum irm_msg_code), data, &msg->code, &offset); - - ser_copy_name(msg->name, data, &offset); - - switch (msg->code) { - case IRM_CREATE_IPCP: - if (msg->ipcp_type == NULL || - !name_is_ok(msg->name)) { - LOG_ERR("Null pointer passed"); - buffer_destroy(buf); - return NULL; - } - ser_copy_name(msg->name, data, &offset); - ser_copy_value(strlen(msg->ipcp_type) + 1, data, - msg->ipcp_type, &offset); - break; - case IRM_DESTROY_IPCP: - if (!name_is_ok(msg->name)) { - LOG_ERR("Null pointer passed"); - buffer_destroy(buf); - return NULL; - } - ser_copy_name(msg->name, data, &offset); - break; - case IRM_BOOTSTRAP_IPCP: - if (!name_is_ok(msg->name)) { - LOG_ERR("Null pointer passed"); - buffer_destroy(buf); - return NULL; - } - ser_copy_name(msg->name, data, &offset); - /* FIXME: Fields missing, need to define dif_conf properly */ - break; - case IRM_ENROLL_IPCP: - if (msg->dif_name == NULL || - !name_is_ok(msg->name)) { - buffer_destroy(buf); - return NULL; - } - - ser_copy_name(msg->name, data, &offset); - 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 || - !name_is_ok(msg->name)) { - buffer_destroy(buf); - return NULL; - } - - ser_copy_name(msg->name, data, &offset); - - 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; - case IRM_AP_REG: - case IRM_AP_UNREG: - if (msg->ap_name == NULL || - msg->difs == NULL || - msg->difs[0] == NULL) { - LOG_ERR("Invalid arguments"); - buffer_destroy(buf); - return NULL; - } - - ser_copy_value(strlen(msg->ap_name) + 1, - data, msg->ap_name, &offset); - - 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; - case IRM_FLOW_ACCEPT: - if (msg->ap_name == NULL || - msg->ae_name == NULL) { - LOG_ERR("Invalid arguments"); - buffer_destroy(buf); - return NULL; - } - - ser_copy_value(sizeof(int), data, &msg->fd, &offset); - - ser_copy_value(strlen(msg->ap_name) + 1, - data, msg->ap_name, &offset); - - ser_copy_value(strlen(msg->ae_name) + 1, - data, msg->ae_name, &offset); - - break; - case IRM_FLOW_ALLOC_RESP: - ser_copy_value(sizeof(int), data, &msg->fd, &offset); - - ser_copy_value(sizeof(int), data, &msg->result, &offset); - - break; - case IRM_FLOW_ALLOC: - if (msg->ap_name == NULL || - msg->ae_name == NULL || - msg->dst_ap_name == NULL || - msg->qos == NULL) { - LOG_ERR("Invalid arguments"); - buffer_destroy(buf); - return NULL; - } - - ser_copy_value(strlen(msg->dst_ap_name) + 1, - data, msg->dst_ap_name, &offset); - - ser_copy_value(strlen(msg->ap_name) + 1, - data, msg->ap_name, &offset); - - ser_copy_value(strlen(msg->ae_name) + 1, - data, msg->ae_name, &offset); - - /* FIXME: Serialize qos spec here */ - - ser_copy_value(sizeof(int), data, &msg->oflags, &offset); - - break; - case IRM_FLOW_ALLOC_RES: - case IRM_FLOW_DEALLOC: - ser_copy_value(sizeof(int), data, &msg->fd, &offset); - break; - case IRM_FLOW_CONTROL: - ser_copy_value(sizeof(int), data, &msg->fd, &offset); - ser_copy_value(sizeof(int), data, &msg->oflags, &offset); - break; - case IRM_FLOW_WRITE: - LOG_MISSING; - break; - case IRM_FLOW_READ: - LOG_MISSING; - break; - case IRM_AP_REG_R: - case IRM_FLOW_ALLOC_R: - ser_copy_value(sizeof(int), data, &msg->fd, &offset); - break; - case IRM_FLOW_ACCEPT_R: - ser_copy_value(sizeof(int), data, &msg->fd, &offset); - - ser_copy_value(strlen(msg->ap_name) + 1, - data, msg->ap_name, &offset); - - ser_copy_value(strlen(msg->ae_name) + 1, - data, msg->ae_name, &offset); - break; - case IRM_FLOW_ALLOC_RES_R: - ser_copy_value(sizeof(int), data, &msg->result, &offset); - break; - default: - LOG_ERR("Don't know that code"); - buffer_destroy(buf); - return NULL; - } - - buf->size = offset; - - return buf; -} - -struct irm_msg * deserialize_irm_msg(buffer_t * data) -{ - struct irm_msg * msg; - int i, j; - int offset = 0; - size_t difs_size; - - if (data == NULL || data->data == NULL) { - LOG_ERR("Got a null pointer"); - return NULL; - } - - msg = malloc(sizeof(*msg)); - if (msg == NULL) { - LOG_ERR("Failed to allocate memory"); - return NULL; - } - - deser_copy_value(sizeof(enum irm_msg_code), - &msg->code, data->data, &offset); - - switch (msg->code) { - case IRM_CREATE_IPCP: - msg->name = deser_copy_name(data->data, &offset); - if (msg->name == NULL) { - LOG_ERR("Failed to reconstruct name"); - free(msg); - return NULL; - } - - if (deser_copy_string(data->data, - &msg->ipcp_type, - &offset)) { - name_destroy(msg->name); - free(msg); - return NULL; - } - - break; - case IRM_DESTROY_IPCP: - msg->name = deser_copy_name(data->data, &offset); - if (msg->name == NULL) { - LOG_ERR("Failed to reconstruct name"); - free(msg); - return NULL; - } - - break; - case IRM_BOOTSTRAP_IPCP: - msg->name = deser_copy_name(data->data, &offset); - if (msg->name == NULL) { - LOG_ERR("Failed to reconstruct name"); - free(msg); - return NULL; - } - - break; - case IRM_ENROLL_IPCP: - msg->name = deser_copy_name(data->data, &offset); - if (msg->name == NULL) { - LOG_ERR("Failed to reconstruct name"); - free(msg); - return NULL; - } - - if (deser_copy_string(data->data, - &msg->dif_name, - &offset)) { - name_destroy(msg->name); - free(msg); - return NULL; - } - - break; - case IRM_REG_IPCP: - case IRM_UNREG_IPCP: - msg->name = deser_copy_name(data->data, &offset); - if (msg->name == NULL) { - LOG_ERR("Failed to reconstruct name"); - free(msg); - return NULL; - } - - deser_copy_size_t(data->data, &difs_size, &offset); - msg->difs_size = difs_size; - - msg->difs = malloc(sizeof(*(msg->difs)) * difs_size); - if (msg->difs == NULL) { - name_destroy(msg->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); - name_destroy(msg->name); - free(msg); - return NULL; - } - } - - break; - case IRM_AP_REG: - case IRM_AP_REG_R: - case IRM_AP_UNREG: - case IRM_FLOW_ACCEPT: - case IRM_FLOW_ACCEPT_R: - case IRM_FLOW_ALLOC_RESP: - case IRM_FLOW_ALLOC: - case IRM_FLOW_ALLOC_R: - case IRM_FLOW_ALLOC_RES: - case IRM_FLOW_ALLOC_RES_R: - case IRM_FLOW_DEALLOC: - case IRM_FLOW_CONTROL: - case IRM_FLOW_WRITE: - case IRM_FLOW_READ: - LOG_MISSING; - break; - default: - LOG_ERR("Don't know that code"); - free(msg); - return NULL; - } - - return msg; -} - buffer_t * serialize_ipcp_msg(struct ipcp_msg * msg) { buffer_t * buf = NULL; |