Compare commits

...

2 Commits

5 changed files with 90 additions and 52 deletions

View File

@ -170,7 +170,11 @@ static void kill_group_friend_connection(const GC_Session *c, const GC_Chat *cha
uint16_t gc_get_wrapped_packet_size(uint16_t length, Net_Packet_Type packet_type) uint16_t gc_get_wrapped_packet_size(uint16_t length, Net_Packet_Type packet_type)
{ {
if (packet_type == NET_PACKET_GC_LOSSY) {
assert(length <= MAX_GC_CUSTOM_LOSSY_PACKET_SIZE);
} else {
assert(length <= MAX_GC_PACKET_CHUNK_SIZE); assert(length <= MAX_GC_PACKET_CHUNK_SIZE);
}
const uint16_t min_header_size = packet_type == NET_PACKET_GC_LOSSY const uint16_t min_header_size = packet_type == NET_PACKET_GC_LOSSY
? GC_MIN_LOSSY_PAYLOAD_SIZE ? GC_MIN_LOSSY_PAYLOAD_SIZE
@ -215,10 +219,14 @@ GC_Connection *get_gc_connection(const GC_Chat *chat, int peer_number)
} }
/** Returns the amount of empty padding a packet of designated length should have. */ /** Returns the amount of empty padding a packet of designated length should have. */
static uint16_t group_packet_padding_length(uint16_t length) static uint16_t group_packet_padding_length(uint16_t length, uint8_t net_packet_type)
{ {
if (net_packet_type == NET_PACKET_GC_LOSSY) {
return (MAX_GC_CUSTOM_LOSSY_PACKET_SIZE - length) % GC_MAX_PACKET_PADDING;
} else {
return (MAX_GC_PACKET_CHUNK_SIZE - length) % GC_MAX_PACKET_PADDING; return (MAX_GC_PACKET_CHUNK_SIZE - length) % GC_MAX_PACKET_PADDING;
} }
}
void gc_get_self_nick(const GC_Chat *chat, uint8_t *nick) void gc_get_self_nick(const GC_Chat *chat, uint8_t *nick)
{ {
@ -1473,7 +1481,7 @@ int group_packet_wrap(
uint16_t packet_size, const uint8_t *data, uint16_t length, uint64_t message_id, uint16_t packet_size, const uint8_t *data, uint16_t length, uint64_t message_id,
uint8_t gp_packet_type, uint8_t net_packet_type) uint8_t gp_packet_type, uint8_t net_packet_type)
{ {
const uint16_t padding_len = group_packet_padding_length(length); const uint16_t padding_len = group_packet_padding_length(length, net_packet_type);
const uint16_t min_packet_size = net_packet_type == NET_PACKET_GC_LOSSLESS const uint16_t min_packet_size = net_packet_type == NET_PACKET_GC_LOSSLESS
? length + padding_len + CRYPTO_MAC_SIZE + 1 + ENC_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + GC_MESSAGE_ID_BYTES + 1 ? length + padding_len + CRYPTO_MAC_SIZE + 1 + ENC_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + GC_MESSAGE_ID_BYTES + 1
: length + padding_len + CRYPTO_MAC_SIZE + 1 + ENC_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + 1; : length + padding_len + CRYPTO_MAC_SIZE + 1 + ENC_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + 1;
@ -1483,10 +1491,17 @@ int group_packet_wrap(
return -1; return -1;
} }
if (net_packet_type == NET_PACKET_GC_LOSSY) {
if (length > MAX_GC_CUSTOM_LOSSY_PACKET_SIZE) {
LOGGER_ERROR(log, "Packet payload size (%u) exceeds maximum (%u)", length, MAX_GC_CUSTOM_LOSSY_PACKET_SIZE);
return -1;
}
} else {
if (length > MAX_GC_PACKET_CHUNK_SIZE) { if (length > MAX_GC_PACKET_CHUNK_SIZE) {
LOGGER_ERROR(log, "Packet payload size (%u) exceeds maximum (%u)", length, MAX_GC_PACKET_CHUNK_SIZE); LOGGER_ERROR(log, "Packet payload size (%u) exceeds maximum (%u)", length, MAX_GC_PACKET_CHUNK_SIZE);
return -1; return -1;
} }
}
uint8_t *plain = (uint8_t *)malloc(packet_size); uint8_t *plain = (uint8_t *)malloc(packet_size);
@ -1551,7 +1566,7 @@ non_null()
static bool send_lossy_group_packet(const GC_Chat *chat, const GC_Connection *gconn, const uint8_t *data, static bool send_lossy_group_packet(const GC_Chat *chat, const GC_Connection *gconn, const uint8_t *data,
uint16_t length, uint8_t packet_type) uint16_t length, uint8_t packet_type)
{ {
assert(length <= MAX_GC_PACKET_CHUNK_SIZE); assert(length <= MAX_GC_CUSTOM_LOSSY_PACKET_SIZE);
if (!gconn->handshaked || gconn->pending_delete) { if (!gconn->handshaked || gconn->pending_delete) {
return false; return false;
@ -6392,13 +6407,13 @@ static int handle_gc_udp_packet(void *object, const IP_Port *ipp, const uint8_t
if (length <= MIN_UDP_PACKET_SIZE) { if (length <= MIN_UDP_PACKET_SIZE) {
LOGGER_WARNING(m->log, "Got UDP packet with invalid length: %u (expected %u to %u)", length, LOGGER_WARNING(m->log, "Got UDP packet with invalid length: %u (expected %u to %u)", length,
MIN_UDP_PACKET_SIZE, MAX_GC_PACKET_CHUNK_SIZE + MIN_UDP_PACKET_SIZE + ENC_PUBLIC_KEY_SIZE); MIN_UDP_PACKET_SIZE, MAX_GC_CUSTOM_LOSSY_PACKET_SIZE + MIN_UDP_PACKET_SIZE + ENC_PUBLIC_KEY_SIZE);
return -1; return -1;
} }
if (length > MAX_GC_PACKET_CHUNK_SIZE + MIN_UDP_PACKET_SIZE + ENC_PUBLIC_KEY_SIZE) { if (length > MAX_GC_CUSTOM_LOSSY_PACKET_SIZE + MIN_UDP_PACKET_SIZE + ENC_PUBLIC_KEY_SIZE) {
LOGGER_WARNING(m->log, "Got UDP packet with invalid length: %u (expected %u to %u)", length, LOGGER_WARNING(m->log, "Got UDP packet with invalid length: %u (expected %u to %u)", length,
MIN_UDP_PACKET_SIZE, MAX_GC_PACKET_CHUNK_SIZE + MIN_UDP_PACKET_SIZE + ENC_PUBLIC_KEY_SIZE); MIN_UDP_PACKET_SIZE, MAX_GC_CUSTOM_LOSSY_PACKET_SIZE + MIN_UDP_PACKET_SIZE + ENC_PUBLIC_KEY_SIZE);
return -1; return -1;
} }

View File

@ -32,7 +32,7 @@
#define MAX_GC_MESSAGE_SIZE GROUP_MAX_MESSAGE_LENGTH #define MAX_GC_MESSAGE_SIZE GROUP_MAX_MESSAGE_LENGTH
#define MAX_GC_MESSAGE_RAW_SIZE (MAX_GC_MESSAGE_SIZE + GC_MESSAGE_PSEUDO_ID_SIZE) #define MAX_GC_MESSAGE_RAW_SIZE (MAX_GC_MESSAGE_SIZE + GC_MESSAGE_PSEUDO_ID_SIZE)
#define MAX_GC_CUSTOM_LOSSLESS_PACKET_SIZE 1373 #define MAX_GC_CUSTOM_LOSSLESS_PACKET_SIZE 1373
#define MAX_GC_CUSTOM_LOSSY_PACKET_SIZE MAX_GC_PACKET_CHUNK_SIZE #define MAX_GC_CUSTOM_LOSSY_PACKET_SIZE 1000
#define MAX_GC_PASSWORD_SIZE 32 #define MAX_GC_PASSWORD_SIZE 32
#define MAX_GC_SAVED_INVITES 10 #define MAX_GC_SAVED_INVITES 10
#define MAX_GC_PEERS_DEFAULT 100 #define MAX_GC_PEERS_DEFAULT 100

View File

@ -3310,7 +3310,7 @@ uint32_t tox_group_max_message_length(void);
/** /**
* Maximum length of a group custom lossy packet. * Maximum length of a group custom lossy packet.
*/ */
#define TOX_GROUP_MAX_CUSTOM_LOSSY_PACKET_LENGTH 500 #define TOX_GROUP_MAX_CUSTOM_LOSSY_PACKET_LENGTH 1000
uint32_t tox_group_max_custom_lossy_packet_length(void); uint32_t tox_group_max_custom_lossy_packet_length(void);

View File

@ -61,6 +61,9 @@ void ChatGui4::render(void) {
_contact_tc.update(); _contact_tc.update();
_msg_tc.update(); _msg_tc.update();
_fss.render();
_sip.render();
const ImGuiViewport* viewport = ImGui::GetMainViewport(); const ImGuiViewport* viewport = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(viewport->WorkPos); ImGui::SetNextWindowPos(viewport->WorkPos);
ImGui::SetNextWindowSize(viewport->WorkSize); ImGui::SetNextWindowSize(viewport->WorkSize);
@ -366,43 +369,22 @@ void ChatGui4::render(void) {
if (ImGui::Button("paste\nfile", {-FLT_MIN, 0})) { if (ImGui::Button("paste\nfile", {-FLT_MIN, 0})) {
const auto* mime_type = clipboardHasImage(); const auto* mime_type = clipboardHasImage();
if (mime_type != nullptr) { // making sure if (mime_type != nullptr) { // making sure
size_t data_size = 0; pasteFile(mime_type);
void* data = SDL_GetClipboardData(mime_type, &data_size);
std::cout << "CG: pasted image of size " << data_size << " mime " << mime_type << "\n";
_sip.sendMemory(
static_cast<const uint8_t*>(data), data_size,
[this](const auto& img_data, const auto file_ext) {
// create file name
// TODO: move this into sip
std::ostringstream tmp_file_name {"tomato_Image_", std::ios_base::ate};
{
const auto now = std::chrono::system_clock::now();
const auto ctime = std::chrono::system_clock::to_time_t(now);
tmp_file_name
<< std::put_time(std::localtime(&ctime), "%F_%H-%M-%S")
<< "."
<< std::setfill('0') << std::setw(3)
<< std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch() - std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch())).count()
<< file_ext
;
} }
//} else if (ImGui::IsItemClicked(ImGuiMouseButton_Right)) {
} else if (ImGui::BeginPopupContextItem(nullptr, ImGuiMouseButton_Right)) {
const static std::vector<const char*> image_mime_types {
"image/webp",
"image/png",
"image/gif",
"image/jpeg",
"image/bmp",
};
std::cout << "tmp image path " << tmp_file_name.str() << "\n"; for (const char* mime_type : image_mime_types) {
if (ImGui::MenuItem(mime_type)) {
const std::filesystem::path tmp_send_file_path = "tmp_send_files"; pasteFile(mime_type);
std::filesystem::create_directories(tmp_send_file_path); }
const auto tmp_file_path = tmp_send_file_path / tmp_file_name.str();
std::ofstream(tmp_file_path, std::ios_base::out | std::ios_base::binary)
.write(reinterpret_cast<const char*>(img_data.data()), img_data.size());
_rmm.sendFilePath(*_selected_contact, tmp_file_name.str(), tmp_file_path.u8string());
},
[](){}
);
SDL_free(data); // free data
} }
} }
//ImGui::EndDisabled(); //ImGui::EndDisabled();
@ -428,9 +410,6 @@ void ChatGui4::render(void) {
} }
ImGui::End(); ImGui::End();
_fss.render();
_sip.render();
_contact_tc.workLoadQueue(); _contact_tc.workLoadQueue();
_msg_tc.workLoadQueue(); _msg_tc.workLoadQueue();
} }
@ -812,3 +791,45 @@ bool ChatGui4::renderSubContactListContact(const Contact3 c, const bool selected
return ImGui::Selectable(label.c_str(), selected); return ImGui::Selectable(label.c_str(), selected);
} }
void ChatGui4::pasteFile(const char* mime_type) {
size_t data_size = 0;
void* data = SDL_GetClipboardData(mime_type, &data_size);
// if image
std::cout << "CG: pasted image of size " << data_size << " mime " << mime_type << "\n";
_sip.sendMemory(
static_cast<const uint8_t*>(data), data_size,
[this](const auto& img_data, const auto file_ext) {
// create file name
// TODO: move this into sip
std::ostringstream tmp_file_name {"tomato_Image_", std::ios_base::ate};
{
const auto now = std::chrono::system_clock::now();
const auto ctime = std::chrono::system_clock::to_time_t(now);
tmp_file_name
<< std::put_time(std::localtime(&ctime), "%F_%H-%M-%S")
<< "."
<< std::setfill('0') << std::setw(3)
<< std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch() - std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch())).count()
<< file_ext
;
}
std::cout << "tmp image path " << tmp_file_name.str() << "\n";
const std::filesystem::path tmp_send_file_path = "tmp_send_files";
std::filesystem::create_directories(tmp_send_file_path);
const auto tmp_file_path = tmp_send_file_path / tmp_file_name.str();
std::ofstream(tmp_file_path, std::ios_base::out | std::ios_base::binary)
.write(reinterpret_cast<const char*>(img_data.data()), img_data.size());
_rmm.sendFilePath(*_selected_contact, tmp_file_name.str(), tmp_file_path.u8string());
},
[](){}
);
SDL_free(data); // free data
}

View File

@ -59,6 +59,8 @@ class ChatGui4 {
bool renderContactListContactBig(const Contact3 c, const bool selected); bool renderContactListContactBig(const Contact3 c, const bool selected);
bool renderContactListContactSmall(const Contact3 c, const bool selected) const; bool renderContactListContactSmall(const Contact3 c, const bool selected) const;
bool renderSubContactListContact(const Contact3 c, const bool selected) const; bool renderSubContactListContact(const Contact3 c, const bool selected) const;
void pasteFile(const char* mime_type);
}; };