From 88fe8d00444b5e99bf684f0c292ce1d4f75a879e Mon Sep 17 00:00:00 2001 From: Jehan Monnier Date: Mon, 26 Jun 2017 15:21:18 +0200 Subject: [PATCH] make sure IP encryption Engine API can be used without needing to include any private headers --- coreapi/chat.c | 11 ++++++++++ coreapi/im_encryption_engine.c | 4 ++-- coreapi/linphonecore.c | 3 ++- coreapi/private.h | 2 -- include/linphone/im_encryption_engine.h | 13 ++++++++++++ tester/message_tester.c | 28 ++++++++++++------------- 6 files changed, 42 insertions(+), 19 deletions(-) diff --git a/coreapi/chat.c b/coreapi/chat.c index e93226220..2e241d44b 100644 --- a/coreapi/chat.c +++ b/coreapi/chat.c @@ -1609,6 +1609,17 @@ const char *linphone_chat_message_get_text(const LinphoneChatMessage *msg) { return msg->message; } +int linphone_chat_message_set_text(LinphoneChatMessage *msg, const char* text) { + if (msg->message) + ms_free(msg->message); + if (text) + msg->message = ms_strdup(text); + else + msg->message = NULL; + + return 0; +} + void linphone_chat_message_add_custom_header(LinphoneChatMessage *msg, const char *header_name, const char *header_value) { msg->custom_headers = sal_custom_header_append(msg->custom_headers, header_name, header_value); diff --git a/coreapi/im_encryption_engine.c b/coreapi/im_encryption_engine.c index c09406e6e..3363b1dfe 100644 --- a/coreapi/im_encryption_engine.c +++ b/coreapi/im_encryption_engine.c @@ -66,10 +66,10 @@ void linphone_im_encryption_engine_cbs_set_user_data(LinphoneImEncryptionEngineC cbs->user_data = data; } -LinphoneImEncryptionEngine *linphone_im_encryption_engine_new(LinphoneCore *lc) { +LinphoneImEncryptionEngine *linphone_im_encryption_engine_new(void) { LinphoneImEncryptionEngine *imee = belle_sip_object_new(LinphoneImEncryptionEngine); belle_sip_object_ref(imee); - imee->lc = lc; + imee->lc = NULL; imee->callbacks = linphone_im_encryption_engine_cbs_new(); return imee; } diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c index dc0c2f7e9..9ec34b498 100644 --- a/coreapi/linphonecore.c +++ b/coreapi/linphonecore.c @@ -2410,7 +2410,7 @@ bool_t linphone_core_get_guess_hostname(LinphoneCore *lc){ } void linphone_core_enable_lime(LinphoneCore *lc, LinphoneLimeState val){ - LinphoneImEncryptionEngine *imee = linphone_im_encryption_engine_new(lc); + LinphoneImEncryptionEngine *imee = linphone_im_encryption_engine_new(); LinphoneImEncryptionEngineCbs *cbs = linphone_im_encryption_engine_get_callbacks(imee); if(lime_is_available()){ @@ -7259,6 +7259,7 @@ void linphone_core_set_im_encryption_engine(LinphoneCore *lc, LinphoneImEncrypti lc->im_encryption_engine = NULL; } if (imee) { + imee->lc = lc; lc->im_encryption_engine = linphone_im_encryption_engine_ref(imee); } } diff --git a/coreapi/private.h b/coreapi/private.h index 1825583cc..08b24bfd2 100644 --- a/coreapi/private.h +++ b/coreapi/private.h @@ -1753,8 +1753,6 @@ struct _LinphoneImEncryptionEngine { BELLE_SIP_DECLARE_VPTR_NO_EXPORT(LinphoneImEncryptionEngine); -LINPHONE_PUBLIC LinphoneImEncryptionEngine *linphone_im_encryption_engine_new(LinphoneCore *lc); - struct _LinphoneRange { belle_sip_object_t base; void *user_data; diff --git a/include/linphone/im_encryption_engine.h b/include/linphone/im_encryption_engine.h index bb04b5b7b..d4c15dc3d 100644 --- a/include/linphone/im_encryption_engine.h +++ b/include/linphone/im_encryption_engine.h @@ -183,6 +183,19 @@ LINPHONE_PUBLIC LinphoneImEncryptionEngineCbsGenerateFileTransferKeyCb linphone_ */ LINPHONE_PUBLIC void linphone_im_encryption_engine_cbs_set_generate_file_transfer_key(LinphoneImEncryptionEngineCbs *cbs, LinphoneImEncryptionEngineCbsGenerateFileTransferKeyCb cb); +/** Set a chat message text to be sent by #linphone_chat_room_send_message + * @param[in] msg LinphoneChatMessage + * @param[in] text Const char * + * @returns 0 if succeed. +*/ +LINPHONE_PUBLIC int linphone_chat_message_set_text(LinphoneChatMessage *msg, const char* text); + +/** + * Create the IM encryption engine + * @return The created the IM encryption engine +*/ +LINPHONE_PUBLIC LinphoneImEncryptionEngine *linphone_im_encryption_engine_new(void); + /** * @} */ diff --git a/tester/message_tester.c b/tester/message_tester.c index 3df2e4d0b..266c40186 100644 --- a/tester/message_tester.c +++ b/tester/message_tester.c @@ -2298,19 +2298,19 @@ void text_message_with_custom_content_type_and_lime(void) { static int im_encryption_engine_process_incoming_message_cb(LinphoneImEncryptionEngine *engine, LinphoneChatRoom *room, LinphoneChatMessage *msg) { - if (msg->content_type) { - if (strcmp(msg->content_type, "cipher/b64") == 0) { + if (linphone_chat_message_get_content_type(msg)) { + if (strcmp(linphone_chat_message_get_content_type(msg), "cipher/b64") == 0) { size_t b64Size = 0; unsigned char *output; - bctbx_base64_decode(NULL, &b64Size, (unsigned char *)msg->message, strlen(msg->message)); + bctbx_base64_decode(NULL, &b64Size, (unsigned char *)linphone_chat_message_get_text(msg), strlen(linphone_chat_message_get_text(msg))); output = (unsigned char *)ms_malloc(b64Size+1), - bctbx_base64_decode(output, &b64Size, (unsigned char *)msg->message, strlen(msg->message)); - ms_free (msg->message); + bctbx_base64_decode(output, &b64Size, (unsigned char *)linphone_chat_message_get_text(msg), strlen(linphone_chat_message_get_text(msg))); output[b64Size] = '\0'; - msg->message = (char *)output; + linphone_chat_message_set_text(msg, (char *)output); + ms_free(output); linphone_chat_message_set_content_type(msg, "text/plain"); return 0; - } else if (strcmp(msg->content_type, "text/plain") == 0) { + } else if (strcmp(linphone_chat_message_get_content_type(msg), "text/plain") == 0) { return -1; // Not encrypted, nothing to do } else { return 488; // Not acceptable @@ -2320,15 +2320,15 @@ static int im_encryption_engine_process_incoming_message_cb(LinphoneImEncryption } static int im_encryption_engine_process_outgoing_message_cb(LinphoneImEncryptionEngine *engine, LinphoneChatRoom *room, LinphoneChatMessage *msg) { - if (strcmp(msg->content_type,"text/plain") == 0) { + if (strcmp(linphone_chat_message_get_content_type(msg),"text/plain") == 0) { size_t b64Size = 0; unsigned char *output; - bctbx_base64_encode(NULL, &b64Size, (unsigned char *)msg->message, strlen(msg->message)); + bctbx_base64_encode(NULL, &b64Size, (unsigned char *)linphone_chat_message_get_text(msg), strlen(linphone_chat_message_get_text(msg))); output = (unsigned char *)ms_malloc0(b64Size+1), - bctbx_base64_encode(output, &b64Size, (unsigned char *)msg->message, strlen(msg->message)); - ms_free (msg->message); + bctbx_base64_encode(output, &b64Size, (unsigned char *)linphone_chat_message_get_text(msg), strlen(linphone_chat_message_get_text(msg))); output[b64Size] = '\0'; - msg->message = (char *)output; + linphone_chat_message_set_text(msg,(const char*)output); + ms_free(output); linphone_chat_message_set_content_type(msg, "cipher/b64"); return 0; } @@ -2339,10 +2339,10 @@ void im_encryption_engine_b64(void) { LinphoneChatMessage *chat_msg = NULL; LinphoneChatRoom* chat_room = NULL; LinphoneCoreManager* marie = linphone_core_manager_new("marie_rc"); - LinphoneImEncryptionEngine *marie_imee = linphone_im_encryption_engine_new(marie->lc); + LinphoneImEncryptionEngine *marie_imee = linphone_im_encryption_engine_new(); LinphoneImEncryptionEngineCbs *marie_cbs = linphone_im_encryption_engine_get_callbacks(marie_imee); LinphoneCoreManager* pauline = linphone_core_manager_new( "pauline_tcp_rc"); - LinphoneImEncryptionEngine *pauline_imee = linphone_im_encryption_engine_new(pauline->lc); + LinphoneImEncryptionEngine *pauline_imee = linphone_im_encryption_engine_new(); LinphoneImEncryptionEngineCbs *pauline_cbs = linphone_im_encryption_engine_get_callbacks(pauline_imee); linphone_im_encryption_engine_cbs_set_process_incoming_message(marie_cbs, im_encryption_engine_process_incoming_message_cb);