|
|
|
|
@ -468,6 +468,9 @@ int etcpmon_client_send_request(struct etcpmon_client* client,
|
|
|
|
|
(unsigned long long)get_timestamp_ms(), type, sent, total_size, sent == SOCKET_ERROR ? WSAGetLastError() : 0); |
|
|
|
|
fflush(client->log_file); |
|
|
|
|
} |
|
|
|
|
/* После частичной отправки следующий пакет уже нельзя писать в этот поток. */ |
|
|
|
|
etcpmon_client_disconnect(client); |
|
|
|
|
if (client->on_disconnected) client->on_disconnected(client->user_data); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -626,7 +629,8 @@ static void handle_response(struct etcpmon_client* client, uint8_t msg_type,
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
case ETCPMON_RSP_ERROR: { |
|
|
|
|
if (payload_size >= sizeof(struct etcpmon_rsp_error)) { |
|
|
|
|
if (payload_size > sizeof(struct etcpmon_rsp_error) && |
|
|
|
|
memchr(payload + sizeof(struct etcpmon_rsp_error), 0, payload_size - sizeof(struct etcpmon_rsp_error))) { |
|
|
|
|
struct etcpmon_rsp_error* rsp = (struct etcpmon_rsp_error*)payload; |
|
|
|
|
const char* msg = (const char*)(payload + sizeof(*rsp)); |
|
|
|
|
|
|
|
|
|
@ -639,12 +643,15 @@ static void handle_response(struct etcpmon_client* client, uint8_t msg_type,
|
|
|
|
|
if (client->on_error) { |
|
|
|
|
client->on_error(msg, client->user_data); |
|
|
|
|
} |
|
|
|
|
} else if (client->log_file) { |
|
|
|
|
fprintf(client->log_file, "[ERROR] Invalid RSP_ERROR string: size=%u\n", payload_size); |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
case ETCPMON_RSP_ACTION_RESULT: { |
|
|
|
|
if (payload_size >= sizeof(struct etcpmon_rsp_action_result)) { |
|
|
|
|
if (payload_size > sizeof(struct etcpmon_rsp_action_result) && |
|
|
|
|
memchr(payload + sizeof(struct etcpmon_rsp_action_result), 0, payload_size - sizeof(struct etcpmon_rsp_action_result))) { |
|
|
|
|
const char* text = (const char*)(payload + sizeof(struct etcpmon_rsp_action_result)); |
|
|
|
|
|
|
|
|
|
if (client->log_file) { |
|
|
|
|
@ -656,15 +663,23 @@ static void handle_response(struct etcpmon_client* client, uint8_t msg_type,
|
|
|
|
|
if (client->on_action_result) { |
|
|
|
|
client->on_action_result(text, client->user_data); |
|
|
|
|
} |
|
|
|
|
} else if (client->log_file) { |
|
|
|
|
fprintf(client->log_file, "[ERROR] Invalid RSP_ACTION_RESULT string: size=%u\n", payload_size); |
|
|
|
|
} |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
case ETCPMON_RSP_DEBUG_CONFIG: { |
|
|
|
|
if (payload_size >= 2) { |
|
|
|
|
const uint8_t* names_end = payload_size > 2 ? memchr(payload + 2, 0, payload_size - 2) : NULL; |
|
|
|
|
if (!names_end || payload[1] > ETCPMON_MAX_DEBUG_CATEGORIES || |
|
|
|
|
(size_t)(names_end - payload) + 1 + payload[1] != payload_size) { |
|
|
|
|
if (client->log_file) fprintf(client->log_file, "[ERROR] Invalid RSP_DEBUG_CONFIG: size=%u\n", payload_size); |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
{ |
|
|
|
|
client->debug_global_level = payload[0]; |
|
|
|
|
client->debug_category_count = payload[1]; |
|
|
|
|
uint16_t names_len = (uint16_t)strlen((const char*)(payload + 2)) + 1; |
|
|
|
|
uint16_t names_len = (uint16_t)(names_end - payload - 2) + 1; |
|
|
|
|
if (2 + names_len <= payload_size) { |
|
|
|
|
size_t copy_len = names_len < sizeof(client->debug_category_names) ? names_len : sizeof(client->debug_category_names) - 1; |
|
|
|
|
memcpy(client->debug_category_names, payload + 2, copy_len); |
|
|
|
|
@ -1000,8 +1015,10 @@ int etcpmon_client_send_debug_config(struct etcpmon_client* client,
|
|
|
|
|
memcpy(payload + 2, levels, count); |
|
|
|
|
uint16_t payload_size = 2 + count; |
|
|
|
|
|
|
|
|
|
struct etcpmon_msg_header hdr; |
|
|
|
|
etcpmon_build_header(&hdr, payload_size, ETCPMON_CMD_SET_DEBUG_CONFIG, 0); |
|
|
|
|
uint8_t buffer[sizeof(struct etcpmon_msg_header) + sizeof(payload)]; |
|
|
|
|
struct etcpmon_msg_header* hdr = (struct etcpmon_msg_header*)buffer; |
|
|
|
|
etcpmon_build_header(hdr, payload_size, ETCPMON_CMD_SET_DEBUG_CONFIG, 0); |
|
|
|
|
memcpy(buffer + sizeof(*hdr), payload, payload_size); |
|
|
|
|
|
|
|
|
|
if (client->log_file) { |
|
|
|
|
fprintf(client->log_file, "%llu: [LOG] CMD_SET_DEBUG_CONFIG global=%d count=%d\n", |
|
|
|
|
@ -1009,10 +1026,18 @@ int etcpmon_client_send_debug_config(struct etcpmon_client* client,
|
|
|
|
|
fflush(client->log_file); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int sent = send(client->sock, (const char*)&hdr, sizeof(hdr), 0); |
|
|
|
|
if (sent != sizeof(hdr)) return -1; |
|
|
|
|
sent = send(client->sock, (const char*)payload, payload_size, 0); |
|
|
|
|
if (sent != payload_size) return -1; |
|
|
|
|
int total_size = sizeof(*hdr) + payload_size; |
|
|
|
|
int sent = send(client->sock, (const char*)buffer, total_size, 0); |
|
|
|
|
if (sent != total_size) { |
|
|
|
|
if (client->log_file) { |
|
|
|
|
fprintf(client->log_file, "[ERROR] Failed to send debug config: sent=%d/%d error=%d\n", |
|
|
|
|
sent, total_size, sent == SOCKET_ERROR ? WSAGetLastError() : 0); |
|
|
|
|
fflush(client->log_file); |
|
|
|
|
} |
|
|
|
|
etcpmon_client_disconnect(client); |
|
|
|
|
if (client->on_disconnected) client->on_disconnected(client->user_data); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
client->debug_global_level = global_level; |
|
|
|
|
for (uint8_t i = 0; i < count; i++) client->debug_category_levels[i] = levels[i]; |
|
|
|
|
return 0; |
|
|
|
|
|