From c3fa88fb2cb97911cb04de7e0c1df480dc9aec1b Mon Sep 17 00:00:00 2001 From: Andrey Ovchinnikov Date: Mon, 29 Jan 2024 09:55:18 +0300 Subject: [PATCH] make apply_fmt --- sources/mdb_iamproxy.c | 125 ++++++++++++++++++++++------------------- 1 file changed, 67 insertions(+), 58 deletions(-) diff --git a/sources/mdb_iamproxy.c b/sources/mdb_iamproxy.c index 8518fca5d..10267ce22 100644 --- a/sources/mdb_iamproxy.c +++ b/sources/mdb_iamproxy.c @@ -35,71 +35,80 @@ int mdb_iamproxy_recv_from_socket(int socket_fd, char *msg_body) { - /*GET COMMON MSG INFO AND ALLOCATE RESOURCES*/ - uint8_t buffer[8]; - uint64_t body_size = 0; - uint64_t received = 0; - - /*RECEIVE HEADER*/ - while (received < MDB_IAMPROXY_DEFAULT_HEADER_SIZE) { - int rt = recv(socket_fd, buffer + received, MDB_IAMPROXY_DEFAULT_HEADER_SIZE - received, 0); - if (rt < 0) { - return MDB_IAMPROXY_CONN_ERROR; - } - received += rt;; - } - for (int i = 0; i < MDB_IAMPROXY_DEFAULT_HEADER_SIZE; ++i) { - body_size |= (((uint64_t)buffer[i]) << (i * MDB_IAMPROXY_BYTE_SIZE)); - } - - /*RECEIVE BODY*/ - received = 0; - while (received < body_size) { - int rt = recv(socket_fd, msg_body + received, body_size - received, 0); - if (rt < 0) { - return MDB_IAMPROXY_CONN_ERROR; - } - received += rt; - } - - return MDB_IAMPROXY_CONN_ACCEPTED; + /*GET COMMON MSG INFO AND ALLOCATE RESOURCES*/ + uint8_t buffer[8]; + uint64_t body_size = 0; + uint64_t received = 0; + + /*RECEIVE HEADER*/ + while (received < MDB_IAMPROXY_DEFAULT_HEADER_SIZE) { + int rt = recv(socket_fd, buffer + received, + MDB_IAMPROXY_DEFAULT_HEADER_SIZE - received, 0); + if (rt < 0) { + return MDB_IAMPROXY_CONN_ERROR; + } + received += rt; + ; + } + for (int i = 0; i < MDB_IAMPROXY_DEFAULT_HEADER_SIZE; ++i) { + body_size |= + (((uint64_t)buffer[i]) << (i * MDB_IAMPROXY_BYTE_SIZE)); + } + + /*RECEIVE BODY*/ + received = 0; + while (received < body_size) { + int rt = recv(socket_fd, msg_body + received, + body_size - received, 0); + if (rt < 0) { + return MDB_IAMPROXY_CONN_ERROR; + } + received += rt; + } + + return MDB_IAMPROXY_CONN_ACCEPTED; } int mdb_iamproxy_send_to_socket(int socket_fd, const char *send_msg) { - /*GET COMMON MSG INFO AND ALLOCATE BUFFER*/ - int32_t send_result = MDB_IAMPROXY_RES_OK; - uint64_t body_size = strlen(send_msg) + 1; // stores size of message (add one byte for 'c\0') - uint64_t current_body_size = body_size; - uint64_t msg_size = sizeof(body_size) + body_size; - uint64_t sent = 0; // stores byte-size of sended info - char *msg = (char *)calloc(msg_size, sizeof(*msg)); // allocate memory for msg buffer - if (msg == NULL) { // error during allocating memory for msg buffer - send_result = MDB_IAMPROXY_RES_ERROR; - goto free_end; - } - - /*COPY ALL DATA TO BUFFER FOR SENDING*/ - for (int i = 0; i < MDB_IAMPROXY_DEFAULT_HEADER_SIZE; ++i) { // coping header to msg buffer - msg[i] = (current_body_size & 0xFF); - current_body_size >>= MDB_IAMPROXY_BYTE_SIZE; - } - memcpy(msg + sizeof(body_size), send_msg, body_size); // coping body to msg buffer - - /*SEND TO SOCKET*/ - while (sent < msg_size) { - int rt = send(socket_fd, msg + sent, msg_size - sent, 0); - if (rt < 0) { - send_result = MDB_IAMPROXY_RES_ERROR; - goto free_start; - } - sent += rt; - } + /*GET COMMON MSG INFO AND ALLOCATE BUFFER*/ + int32_t send_result = MDB_IAMPROXY_RES_OK; + uint64_t body_size = + strlen(send_msg) + + 1; // stores size of message (add one byte for 'c\0') + uint64_t current_body_size = body_size; + uint64_t msg_size = sizeof(body_size) + body_size; + uint64_t sent = 0; // stores byte-size of sended info + char *msg = (char *)calloc( + msg_size, sizeof(*msg)); // allocate memory for msg buffer + if (msg == NULL) { // error during allocating memory for msg buffer + send_result = MDB_IAMPROXY_RES_ERROR; + goto free_end; + } + + /*COPY ALL DATA TO BUFFER FOR SENDING*/ + for (int i = 0; i < MDB_IAMPROXY_DEFAULT_HEADER_SIZE; + ++i) { // coping header to msg buffer + msg[i] = (current_body_size & 0xFF); + current_body_size >>= MDB_IAMPROXY_BYTE_SIZE; + } + memcpy(msg + sizeof(body_size), send_msg, + body_size); // coping body to msg buffer + + /*SEND TO SOCKET*/ + while (sent < msg_size) { + int rt = send(socket_fd, msg + sent, msg_size - sent, 0); + if (rt < 0) { + send_result = MDB_IAMPROXY_RES_ERROR; + goto free_start; + } + sent += rt; + } free_start: - free(msg); + free(msg); free_end: - return send_result; + return send_result; } int mdb_iamproxy_authenticate_user(const char *username, const char *token,