diff options
author | Sander Vrijders <sander.vrijders@intec.ugent.be> | 2016-03-30 17:25:30 +0200 |
---|---|---|
committer | Sander Vrijders <sander.vrijders@intec.ugent.be> | 2016-03-30 18:02:25 +0200 |
commit | 08941177f030b77fb44238a7e589322d2e0fcaa2 (patch) | |
tree | e823d98180ea74d476466a862223a69d52308ffd /src/irmd/main.c | |
parent | a3f002d6f2d102588f988c99eb16c64a68706dd2 (diff) | |
download | ouroboros-08941177f030b77fb44238a7e589322d2e0fcaa2.tar.gz ouroboros-08941177f030b77fb44238a7e589322d2e0fcaa2.zip |
lib, irmd: Update communication with IRMd
All messages sent to the IRMd now also get a reply back with the
result of the operation.
Diffstat (limited to 'src/irmd/main.c')
-rw-r--r-- | src/irmd/main.c | 311 |
1 files changed, 230 insertions, 81 deletions
diff --git a/src/irmd/main.c b/src/irmd/main.c index 547286e8..af0f85e2 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -46,8 +46,9 @@ struct irm { struct list_head name_to_pid; }; -static pid_t find_pid_by_name(struct irm * instance, - instance_name_t * api) +struct irm * instance = NULL; + +static pid_t find_pid_by_name(instance_name_t * api) { struct list_head * pos; @@ -64,9 +65,8 @@ static pid_t find_pid_by_name(struct irm * instance, return 0; } -static void create_ipcp(struct irm * instance, - instance_name_t * api, - char * ipcp_type) +static int create_ipcp(instance_name_t * api, + char * ipcp_type) { pid_t pid; struct name_to_pid_entry * tmp; @@ -74,12 +74,12 @@ static void create_ipcp(struct irm * instance, pid = ipcp_create(api, ipcp_type); if (pid == -1) { LOG_ERR("Failed to create IPCP"); - return; + return -1; } tmp = malloc(sizeof(*tmp)); if (tmp == NULL) - return; + return -1; INIT_LIST_HEAD(&tmp->next); @@ -87,25 +87,25 @@ static void create_ipcp(struct irm * instance, tmp->api = instance_name_dup(api); if (tmp->api == NULL) { free(tmp); - return; + return -1; } LOG_DBG("Created IPC process with pid %d", pid); list_add(&tmp->next, &instance->name_to_pid); + return 0; } -static void destroy_ipcp(struct irm * instance, - instance_name_t * api) +static int destroy_ipcp(instance_name_t * api) { pid_t pid = 0; struct list_head * pos; struct list_head * n; - pid = find_pid_by_name(instance, api); + pid = find_pid_by_name(api); if (pid == 0) { LOG_ERR("No such IPCP"); - return; + return -1; } LOG_DBG("Destroying ipcp with pid %d", pid); @@ -120,92 +120,159 @@ static void destroy_ipcp(struct irm * instance, if (instance_name_cmp(api, tmp->api) == 0) list_del(&tmp->next); } + + return 0; } -static void bootstrap_ipcp(struct irm * instance, - instance_name_t * api, - struct dif_config * conf) +static int bootstrap_ipcp(instance_name_t * api, + struct dif_config * conf) { pid_t pid = 0; - pid = find_pid_by_name(instance, api); + pid = find_pid_by_name(api); if (pid == 0) { LOG_ERR("No such IPCP"); - return; + return -1; } - if (ipcp_bootstrap(pid, conf)) + if (ipcp_bootstrap(pid, conf)) { LOG_ERR("Could not bootstrap IPCP"); + return -1; + } + + return 0; } -static void enroll_ipcp(struct irm * instance, - instance_name_t * api, - char * dif_name) +static int enroll_ipcp(instance_name_t * api, + char * dif_name) { pid_t pid = 0; char * member; char ** n_1_difs = NULL; ssize_t n_1_difs_size = 0; - pid = find_pid_by_name(instance, api); + pid = find_pid_by_name(api); if (pid == 0) { LOG_ERR("No such IPCP"); - return; + return -1; } member = da_resolve_daf(dif_name); if (member == NULL) { LOG_ERR("Could not find a member of that DIF"); - return; + return -1; } n_1_difs_size = da_resolve_dap(member, n_1_difs); if (n_1_difs_size != 0) if (ipcp_enroll(pid, dif_name, member, - n_1_difs, n_1_difs_size)) + n_1_difs, n_1_difs_size)) { LOG_ERR("Could not enroll IPCP"); + return -1; + } + + return 0; } -static void reg_ipcp(struct irm * instance, - instance_name_t * api, - char ** difs, - size_t difs_size) +static int reg_ipcp(instance_name_t * api, + char ** difs, + size_t difs_size) { pid_t pid = 0; - pid = find_pid_by_name(instance, api); + pid = find_pid_by_name(api); if (pid == 0) { LOG_ERR("No such IPCP"); - return; + return -1; } - if (ipcp_reg(pid, difs, difs_size)) + if (ipcp_reg(pid, difs, difs_size)) { LOG_ERR("Could not register IPCP to N-1 DIF(s)"); + return -1; + } + + return 0; } -static void unreg_ipcp(struct irm * instance, - instance_name_t * api, - char ** difs, - size_t difs_size) +static int unreg_ipcp(instance_name_t * api, + char ** difs, + size_t difs_size) { pid_t pid = 0; - pid = find_pid_by_name(instance, api); + pid = find_pid_by_name(api); if (pid == 0) { LOG_ERR("No such IPCP"); - return; + return -1; } - if (ipcp_unreg(pid, difs, difs_size)) + if (ipcp_unreg(pid, difs, difs_size)) { LOG_ERR("Could not unregister IPCP from N-1 DIF(s)"); + return -1; + } + + return 0; +} + +static int ap_reg(char * ap_name, + char ** difs, + size_t difs_size) +{ + return -1; +} + +static int ap_unreg(char * ap_name, + char ** difs, + size_t difs_size) +{ + return -1; +} + +static int flow_accept(int fd, + char * ap_name, + char * ae_name) +{ + return -1; +} + +static int flow_alloc_resp(int fd, + int result) +{ + + return -1; +} + +static int flow_alloc(char * dst_ap_name, + char * src_ap_name, + char * src_ae_name, + struct qos_spec * qos, + int oflags) +{ + return -1; +} + +static int flow_alloc_res(int fd) +{ + + return -1; +} + +static int flow_dealloc(int fd) +{ + return -1; +} + +static int flow_cntl(int fd, + int oflags) +{ + return -1; } /* FIXME: Close sockfd on closing and release irm */ int main() { - struct irm * instance = NULL; - int sockfd; - uint8_t buf[IRM_MSG_BUF_SIZE]; + int sockfd; + uint8_t buf[IRM_MSG_BUF_SIZE]; instance = malloc(sizeof(*instance)); if (instance == NULL) @@ -222,6 +289,10 @@ int main() irm_msg_t * msg; ssize_t count; instance_name_t api; + buffer_t buffer; + irm_msg_t ret_msg = IRM_MSG__INIT; + + ret_msg.code = IRM_MSG_CODE__IRM_REPLY; cli_sockfd = accept(sockfd, 0, 0); if (cli_sockfd < 0) { @@ -230,47 +301,125 @@ int main() } count = read(cli_sockfd, buf, IRM_MSG_BUF_SIZE); - if (count > 0) { - msg = irm_msg__unpack(NULL, count, buf); - if (msg == NULL) - continue; - - api.name = msg->ap_name; - api.id = msg->api_id; - - switch (msg->code) { - case IRM_MSG_CODE__IRM_CREATE_IPCP: - create_ipcp(instance, &api, msg->ipcp_type); - break; - case IRM_MSG_CODE__IRM_DESTROY_IPCP: - destroy_ipcp(instance, &api); - break; - case IRM_MSG_CODE__IRM_BOOTSTRAP_IPCP: - bootstrap_ipcp(instance, &api, NULL); - break; - case IRM_MSG_CODE__IRM_ENROLL_IPCP: - if (msg->n_dif_name != 1) - continue; - enroll_ipcp(instance, &api, msg->dif_name[0]); - break; - case IRM_MSG_CODE__IRM_REG_IPCP: - reg_ipcp(instance, &api, - msg->dif_name, - msg->n_dif_name); - break; - case IRM_MSG_CODE__IRM_UNREG_IPCP: - unreg_ipcp(instance, &api, - msg->dif_name, - msg->n_dif_name); - break; - default: - LOG_ERR("Don't know that message code"); - break; - } - - irm_msg__free_unpacked(msg, NULL); + if (count <= 0) { + LOG_ERR("Failed to read from socket"); + close(cli_sockfd); + continue; + } + + msg = irm_msg__unpack(NULL, count, buf); + if (msg == NULL) { + close(cli_sockfd); + continue; + } + + api.name = msg->ap_name; + api.id = msg->api_id; + + switch (msg->code) { + case IRM_MSG_CODE__IRM_CREATE_IPCP: + ret_msg.has_result = true; + ret_msg.result = create_ipcp(&api, + msg->ipcp_type); + break; + case IRM_MSG_CODE__IRM_DESTROY_IPCP: + ret_msg.has_result = true; + ret_msg.result = destroy_ipcp(&api); + break; + case IRM_MSG_CODE__IRM_BOOTSTRAP_IPCP: + ret_msg.has_result = true; + ret_msg.result = bootstrap_ipcp(&api, NULL); + break; + case IRM_MSG_CODE__IRM_ENROLL_IPCP: + ret_msg.has_result = true; + ret_msg.result = enroll_ipcp(&api, + msg->dif_name[0]); + break; + case IRM_MSG_CODE__IRM_REG_IPCP: + ret_msg.has_result = true; + ret_msg.result = reg_ipcp(&api, + msg->dif_name, + msg->n_dif_name); + break; + case IRM_MSG_CODE__IRM_UNREG_IPCP: + ret_msg.has_result = true; + ret_msg.result = unreg_ipcp(&api, + msg->dif_name, + msg->n_dif_name); + break; + case IRM_MSG_CODE__IRM_AP_REG: + ret_msg.has_fd = true; + ret_msg.fd = ap_reg(msg->ap_name, + msg->dif_name, + msg->n_dif_name); + break; + case IRM_MSG_CODE__IRM_AP_UNREG: + ret_msg.has_result = true; + ret_msg.result = ap_unreg(msg->ap_name, + msg->dif_name, + msg->n_dif_name); + break; + case IRM_MSG_CODE__IRM_FLOW_ACCEPT: + ret_msg.has_fd = true; + ret_msg.fd = flow_accept(msg->fd, + ret_msg.ap_name, + ret_msg.ae_name); + break; + case IRM_MSG_CODE__IRM_FLOW_ALLOC_RESP: + ret_msg.has_result = true; + ret_msg.result = flow_alloc_resp(msg->fd, + msg->result); + break; + case IRM_MSG_CODE__IRM_FLOW_ALLOC: + ret_msg.has_fd = true; + ret_msg.fd = flow_alloc(msg->dst_ap_name, + msg->ap_name, + msg->ae_name, + NULL, + msg->oflags); + break; + case IRM_MSG_CODE__IRM_FLOW_ALLOC_RES: + ret_msg.has_result = true; + ret_msg.result = flow_alloc_res(msg->fd); + break; + case IRM_MSG_CODE__IRM_FLOW_DEALLOC: + ret_msg.has_result = true; + ret_msg.result = flow_dealloc(msg->fd); + break; + case IRM_MSG_CODE__IRM_FLOW_CONTROL: + ret_msg.has_result = true; + ret_msg.result = flow_cntl(msg->fd, + msg->oflags); + break; + default: + LOG_ERR("Don't know that message code"); + break; + } + + irm_msg__free_unpacked(msg, NULL); + + buffer.size = irm_msg__get_packed_size(&ret_msg); + if (buffer.size == 0) { + LOG_ERR("Failed to send reply message"); + close(cli_sockfd); + continue; + } + + buffer.data = malloc(buffer.size); + if (buffer.data == NULL) { + close(cli_sockfd); + continue; + } + + irm_msg__pack(&ret_msg, buffer.data); + + if (write(cli_sockfd, buffer.data, buffer.size) == -1) { + free(buffer.data); + close(cli_sockfd); + continue; } + free(buffer.data); close(cli_sockfd); } |