Skip to content

Commit

Permalink
make apply_fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Ovchinnikov committed Jan 29, 2024
1 parent f13b86b commit c3fa88f
Showing 1 changed file with 67 additions and 58 deletions.
125 changes: 67 additions & 58 deletions sources/mdb_iamproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit c3fa88f

Please sign in to comment.