From 87a2a578d9a477d6d8ab783c1d5992bdb53c61c2 Mon Sep 17 00:00:00 2001 From: Vitaliy Cherepanov Date: Fri, 26 Sep 2014 11:51:34 +0400 Subject: [PATCH] [FIX] replay event in start message Increase max msg size. Add ACK msg on error. Add msg body read from socket on err (too long msg and memory alloc err) Change-Id: If5c2ad8d281d610dbf0af6d67b764bbbf0b6bc03 Signed-off-by: Vitaliy Cherepanov --- daemon/da_protocol.c | 3 +++ daemon/da_protocol.h | 7 ++++--- daemon/daemon.c | 46 ++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/daemon/da_protocol.c b/daemon/da_protocol.c index a28f4ac..9b210d4 100644 --- a/daemon/da_protocol.c +++ b/daemon/da_protocol.c @@ -413,6 +413,9 @@ int parse_replay_event_seq(struct msg_buf_t *msg, } parse_deb("events num=%d\n", res->event_num); + LOGI("Replay events: count = %u; total_size = %u\n", + res->event_num, res->event_num * sizeof(*res->events)); + res->events = (struct replay_event_t *)malloc(res->event_num * sizeof(*res->events)); if (!res->events) { diff --git a/daemon/da_protocol.h b/daemon/da_protocol.h index d4201f4..38368d3 100644 --- a/daemon/da_protocol.h +++ b/daemon/da_protocol.h @@ -169,8 +169,9 @@ static const char *supported_devices_strings[] = { #define array_size(x) (sizeof(x)/sizeof((x)[0])) enum { supported_devices_count = array_size(supported_devices_strings) }; -#define DA_MSG_MAX 4096 -#define RECV_BUF_MAX 4104 // = sizeof(msg_t) +#define TARGER_MSG_MAX_LEN 4096 +#define HOST_CTL_MSG_MAX_LEN (10 * 1024 * 1024) + struct _msg_target_t { unsigned int type; unsigned int length; @@ -180,7 +181,7 @@ struct _msg_target_t { struct msg_target_t { unsigned int type; unsigned int length; - char data[DA_MSG_MAX]; + char data[TARGER_MSG_MAX_LEN]; }; enum { MSG_HEADER_LEN = offsetof(struct msg_target_t, data) }; diff --git a/daemon/daemon.c b/daemon/daemon.c index c287fa4..785d6ff 100644 --- a/daemon/daemon.c +++ b/daemon/daemon.c @@ -702,6 +702,33 @@ static int targetServerHandler(void) } } +static void recv_msg_tail(int fd, uint32_t len) +{ + char buf[512]; + uint32_t blocks; + int recv_len; + + for (blocks = len / sizeof(buf); blocks != 0; blocks--) { + recv_len = recv(fd, buf, sizeof(buf), MSG_WAITALL); + if (recv_len != sizeof(buf)) + goto error_ret; + } + + len = len % sizeof(buf); + if (len != 0) { + recv_len = recv(fd, buf, len, MSG_WAITALL); + if (recv_len == -1) + goto error_ret; + } + + return; + +error_ret: + LOGE("error or close request from host. recv_len = %d\n", + recv_len); + return; +} + static Ecore_Fd_Handler *host_ctrl_handler; static Ecore_Fd_Handler *host_data_handler; @@ -723,15 +750,24 @@ static int controlSocketHandler(int efd) // Receive header recv_len = recv(manager.host.control_socket, &msg_head, MSG_CMD_HDR_LEN, 0); + // error or close request from host - if (recv_len == -1 || recv_len == 0) + if (recv_len == -1 || recv_len == 0) { + LOGW("error or close request from host. " + "MSG_ID = 0x%08X; recv_len = %d\n", + msg_head.id, recv_len); return -11; - else { - if (msg_head.len > RECV_BUF_MAX) + } else { + if (msg_head.len > HOST_CTL_MSG_MAX_LEN) { + LOGE("Too long message. size = %u\n", msg_head.len); + recv_msg_tail(manager.host.control_socket, msg_head.len); + sendACKToHost(msg_head.id, ERR_WRONG_MESSAGE_FORMAT, 0, 0); return -1; + } msg = malloc(MSG_CMD_HDR_LEN + msg_head.len); if (!msg) { LOGE("Cannot alloc msg\n"); + recv_msg_tail(manager.host.control_socket, msg_head.len); sendACKToHost(msg_head.id, ERR_WRONG_MESSAGE_FORMAT, 0, 0); return -1; } @@ -742,6 +778,8 @@ static int controlSocketHandler(int efd) recv_len = recv(manager.host.control_socket, msg->payload, msg->len, MSG_WAITALL); if (recv_len == -1) { + LOGE("error or close request from host. recv_len = %d\n", + recv_len); free(msg); return -11; } @@ -767,7 +805,7 @@ static Eina_Bool host_ctrl_cb(void *data, Ecore_Fd_Handler *fd_handler) manager.host.data_socket = -1; //splice will fail without that ecore_main_loop_quit(); } else if (result < 0) { - LOGE("Control socket handler.\n"); + LOGE("Control socket handler. err #%d\n", result); } return ECORE_CALLBACK_RENEW; -- 2.7.4