Compare commits
4 Commits
06d835e2b9
...
6c080a438c
Author | SHA1 | Date | |
---|---|---|---|
6c080a438c | |||
97ee13591e | |||
2586f1a937 | |||
e688023383 |
@ -41,6 +41,8 @@ enum WHY2_CHAT_SERVER_TYPE //TYPE OF SERVER
|
|||||||
|
|
||||||
#define WHY2_INVALID_POINTER (void*) 0xffffffffffffffff
|
#define WHY2_INVALID_POINTER (void*) 0xffffffffffffffff
|
||||||
|
|
||||||
|
#define WHY2_CHAT_MESSAGE_DELIMITER ';' //SEPARATES RECEIVED MESSAGES
|
||||||
|
|
||||||
//(SERVER -> CLIENT) CODES
|
//(SERVER -> CLIENT) CODES
|
||||||
#define WHY2_CHAT_CODE_SERVER_CLIENT_KEY_EXCHANGE "SC0" //TELL CLIENT YOU ARE SHARING YOUR PUBLIC KEY
|
#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
|
#define WHY2_CHAT_CODE_ACCEPT_MESSAGES "SC1" //TELL CLIENT THEY CAN SEND MESSAGES
|
||||||
|
@ -116,7 +116,7 @@ char *why2_chat_base64_encode(char *message, size_t *length)
|
|||||||
BIO_get_mem_ptr(bio, &buffer_ptr);
|
BIO_get_mem_ptr(bio, &buffer_ptr);
|
||||||
|
|
||||||
//COPY
|
//COPY
|
||||||
encoded_message = why2_malloc(buffer_ptr -> length + why2_count_int_length((int) *length) + 2);
|
encoded_message = why2_malloc(buffer_ptr -> length + why2_count_int_length((int) *length) + 3);
|
||||||
memcpy(encoded_message, buffer_ptr -> data, buffer_ptr -> length);
|
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
|
sprintf(encoded_message + buffer_ptr -> length, "%c%zu%c", WHY2_CHAT_BASE64_LENGTH_DELIMITER, *length, '\0'); //APPEND LENGTH
|
||||||
|
@ -237,7 +237,9 @@ char *read_socket_raw(int socket, char *key)
|
|||||||
}
|
}
|
||||||
|
|
||||||
char *content_buffer = NULL;
|
char *content_buffer = NULL;
|
||||||
|
char *output;
|
||||||
size_t content_size = 0;
|
size_t content_size = 0;
|
||||||
|
size_t read_size = 0;
|
||||||
char *wait_buffer = why2_malloc(2); //TEMP
|
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)
|
//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)
|
||||||
@ -247,40 +249,49 @@ char *read_socket_raw(int socket, char *key)
|
|||||||
//FIND THE RECEIVED SIZE
|
//FIND THE RECEIVED SIZE
|
||||||
ioctl(socket, FIONREAD, &content_size);
|
ioctl(socket, FIONREAD, &content_size);
|
||||||
|
|
||||||
|
if (content_size == 0) return NULL; //CLIENT PROBABLY QUIT
|
||||||
|
|
||||||
//ALLOCATE
|
//ALLOCATE
|
||||||
content_buffer = why2_malloc(content_size + 1);
|
content_buffer = why2_malloc(content_size + 1);
|
||||||
|
|
||||||
for (size_t i = 0; i < content_size; i++)
|
for (; read_size < content_size; read_size++)
|
||||||
{
|
{
|
||||||
//END OF MESSAGE REACHED (COULD BE CAUSED BY FAST SENT MESSAGES)
|
//END OF MESSAGE REACHED (COULD BE CAUSED BY FAST SENT MESSAGES)
|
||||||
if (i >= 2 && strncmp(content_buffer + i - 2, "\"}", 2) == 0) break;
|
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;
|
||||||
|
|
||||||
//READ JSON MESSAGE
|
//READ JSON MESSAGE
|
||||||
if (recv(socket, content_buffer + i, 1, 0) != 1) //READ THE MESSAGE BY CHARACTERS
|
if (recv(socket, content_buffer + read_size, 1, 0) != 1) //READ THE MESSAGE BY CHARACTERS
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Socket probably read wrongly!\n");
|
fprintf(stderr, "Socket probably read wrongly!\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
content_buffer[content_size] = '\0'; //NULL TERM
|
//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';
|
||||||
|
|
||||||
encrypt_decrypt_message(&content_buffer, key, DECRYPTION); //DECRYPT
|
encrypt_decrypt_message(&output, key, DECRYPTION); //DECRYPT
|
||||||
|
|
||||||
//VALIDATE JSON FORMAT
|
//VALIDATE JSON FORMAT
|
||||||
struct json_object *json = json_tokener_parse(content_buffer);
|
struct json_object *json = json_tokener_parse(output);
|
||||||
if (json == NULL)
|
if (json == NULL)
|
||||||
{
|
{
|
||||||
//RESET content_buffer
|
//RESET output
|
||||||
why2_deallocate(content_buffer);
|
why2_deallocate(output);
|
||||||
content_buffer = NULL;
|
output = NULL;
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
//DEALLOCATION
|
//DEALLOCATION
|
||||||
json_object_put(json);
|
json_object_put(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
return content_buffer;
|
//DEALLOCATION
|
||||||
|
why2_deallocate(content_buffer);
|
||||||
|
|
||||||
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *read_socket_raw_thread(void *socket)
|
void *read_socket_raw_thread(void *socket)
|
||||||
@ -480,6 +491,7 @@ void send_socket(char *text, char *username, char *why2_key, int socket, why2_bo
|
|||||||
{
|
{
|
||||||
//VARIABLES
|
//VARIABLES
|
||||||
char *output = why2_strdup("");
|
char *output = why2_strdup("");
|
||||||
|
char *buffer;
|
||||||
struct json_object *json = json_tokener_parse("{}");
|
struct json_object *json = json_tokener_parse("{}");
|
||||||
|
|
||||||
if (text != NULL)
|
if (text != NULL)
|
||||||
@ -538,6 +550,18 @@ void send_socket(char *text, char *username, char *why2_key, int socket, why2_bo
|
|||||||
|
|
||||||
encrypt_decrypt_message(&output, why2_key, ENCRYPTION); //ENCRYPT
|
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
|
||||||
send(socket, output, strlen(output), 0);
|
send(socket, output, strlen(output), 0);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user