Compare commits

..

No commits in common. "6c080a438c1e477af2e7657d6bd27c888a307fe6" and "06d835e2b94982961bc2f47dcb3ea02e0643dcad" have entirely different histories.

3 changed files with 11 additions and 37 deletions

View File

@ -41,8 +41,6 @@ enum WHY2_CHAT_SERVER_TYPE //TYPE OF SERVER
#define WHY2_INVALID_POINTER (void*) 0xffffffffffffffff
#define WHY2_CHAT_MESSAGE_DELIMITER ';' //SEPARATES RECEIVED MESSAGES
//(SERVER -> CLIENT) CODES
#define WHY2_CHAT_CODE_SERVER_CLIENT_KEY_EXCHANGE "SC0" //TELL CLIENT YOU ARE SHARING YOUR PUBLIC KEY
#define WHY2_CHAT_CODE_ACCEPT_MESSAGES "SC1" //TELL CLIENT THEY CAN SEND MESSAGES

View File

@ -116,7 +116,7 @@ char *why2_chat_base64_encode(char *message, size_t *length)
BIO_get_mem_ptr(bio, &buffer_ptr);
//COPY
encoded_message = why2_malloc(buffer_ptr -> length + why2_count_int_length((int) *length) + 3);
encoded_message = why2_malloc(buffer_ptr -> length + why2_count_int_length((int) *length) + 2);
memcpy(encoded_message, buffer_ptr -> data, buffer_ptr -> length);
sprintf(encoded_message + buffer_ptr -> length, "%c%zu%c", WHY2_CHAT_BASE64_LENGTH_DELIMITER, *length, '\0'); //APPEND LENGTH

View File

@ -237,9 +237,7 @@ char *read_socket_raw(int socket, char *key)
}
char *content_buffer = NULL;
char *output;
size_t content_size = 0;
size_t read_size = 0;
char *wait_buffer = why2_malloc(2); //TEMP
//WAIT TILl RECEIVED MSG (ik it sucks but i can't think of better solution; anyways, this is way more convenient than infinite loop that makes my computer go wroom wroom)
@ -249,49 +247,40 @@ char *read_socket_raw(int socket, char *key)
//FIND THE RECEIVED SIZE
ioctl(socket, FIONREAD, &content_size);
if (content_size == 0) return NULL; //CLIENT PROBABLY QUIT
//ALLOCATE
content_buffer = why2_malloc(content_size + 1);
for (; read_size < content_size; read_size++)
for (size_t i = 0; i < content_size; i++)
{
//END OF MESSAGE REACHED (COULD BE CAUSED BY FAST SENT MESSAGES)
if (key == NULL && read_size >= 2 && strncmp(content_buffer + read_size - 2, "\"}", 2) == 0) break;
if (key != NULL && read_size >= 1 && content_buffer[read_size - 1] == WHY2_CHAT_MESSAGE_DELIMITER) break;
if (i >= 2 && strncmp(content_buffer + i - 2, "\"}", 2) == 0) break;
//READ JSON MESSAGE
if (recv(socket, content_buffer + read_size, 1, 0) != 1) //READ THE MESSAGE BY CHARACTERS
if (recv(socket, content_buffer + i, 1, 0) != 1) //READ THE MESSAGE BY CHARACTERS
{
fprintf(stderr, "Socket probably read wrongly!\n");
break;
}
}
//TRUNCATE content_buffer (REMOVE DELIMITER IF ENCRYPTED)
output = why2_malloc(read_size + (key != NULL ? 1 : 2));
memcpy(output, content_buffer, read_size - (key != NULL ? 1 : 0));
output[read_size - (key != NULL ? 1 : 0)] = '\0';
content_buffer[content_size] = '\0'; //NULL TERM
encrypt_decrypt_message(&output, key, DECRYPTION); //DECRYPT
encrypt_decrypt_message(&content_buffer, key, DECRYPTION); //DECRYPT
//VALIDATE JSON FORMAT
struct json_object *json = json_tokener_parse(output);
struct json_object *json = json_tokener_parse(content_buffer);
if (json == NULL)
{
//RESET output
why2_deallocate(output);
output = NULL;
//RESET content_buffer
why2_deallocate(content_buffer);
content_buffer = NULL;
} else
{
//DEALLOCATION
json_object_put(json);
}
//DEALLOCATION
why2_deallocate(content_buffer);
return output;
return content_buffer;
}
void *read_socket_raw_thread(void *socket)
@ -491,7 +480,6 @@ void send_socket(char *text, char *username, char *why2_key, int socket, why2_bo
{
//VARIABLES
char *output = why2_strdup("");
char *buffer;
struct json_object *json = json_tokener_parse("{}");
if (text != NULL)
@ -550,18 +538,6 @@ void send_socket(char *text, char *username, char *why2_key, int socket, why2_bo
encrypt_decrypt_message(&output, why2_key, ENCRYPTION); //ENCRYPT
//APPEND DELIMITER IF ENCRYPTED
if (why2_key != NULL)
{
//COPY output TO buffer
buffer = why2_malloc(strlen(output) + 2);
sprintf(buffer, "%s%c%c", output, WHY2_CHAT_MESSAGE_DELIMITER, '\0');
//POINT output TO THE NEW buffer
why2_deallocate(output);
output = buffer;
}
//SEND
send(socket, output, strlen(output), 0);