Commit a63faf6f authored by louiz’'s avatar louiz’

Use libuuid to generate unique IDs for iq and adhoc sessions

parent 8eb9e535
...@@ -18,6 +18,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/") ...@@ -18,6 +18,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
include(FindEXPAT) include(FindEXPAT)
find_package(EXPAT REQUIRED) find_package(EXPAT REQUIRED)
find_package(Iconv REQUIRED) find_package(Iconv REQUIRED)
find_package(Libuuid REQUIRED)
find_package(Libidn) find_package(Libidn)
find_package(SystemdDaemon) find_package(SystemdDaemon)
...@@ -26,6 +27,7 @@ include_directories("${CMAKE_CURRENT_SOURCE_DIR}/src/") ...@@ -26,6 +27,7 @@ include_directories("${CMAKE_CURRENT_SOURCE_DIR}/src/")
include_directories("${CMAKE_CURRENT_BINARY_DIR}/src/") include_directories("${CMAKE_CURRENT_BINARY_DIR}/src/")
include_directories(${EXPAT_INCLUDE_DIRS}) include_directories(${EXPAT_INCLUDE_DIRS})
include_directories(${ICONV_INCLUDE_DIRS}) include_directories(${ICONV_INCLUDE_DIRS})
include_directories(${LIBUUID_INCLUDE_DIRS})
if(LIBIDN_FOUND) if(LIBIDN_FOUND)
include_directories(${LIBIDN_INCLUDE_DIRS}) include_directories(${LIBIDN_INCLUDE_DIRS})
...@@ -109,7 +111,8 @@ file(GLOB source_xmpp ...@@ -109,7 +111,8 @@ file(GLOB source_xmpp
src/xmpp/*.[hc]pp) src/xmpp/*.[hc]pp)
add_library(xmpp STATIC ${source_xmpp}) add_library(xmpp STATIC ${source_xmpp})
target_link_libraries(xmpp bridge network utils logger target_link_libraries(xmpp bridge network utils logger
${EXPAT_LIBRARIES}) ${EXPAT_LIBRARIES}
${LIBUUID_LIBRARIES})
if(LIBIDN_FOUND) if(LIBIDN_FOUND)
target_link_libraries(xmpp ${LIBIDN_LIBRARIES}) target_link_libraries(xmpp ${LIBIDN_LIBRARIES})
endif() endif()
......
# - Find libuuid
# Find the libuuid library
#
# This module defines the following variables:
# LIBUUID_FOUND - True if library and include directory are found
# If set to TRUE, the following are also defined:
# LIBUUID_INCLUDE_DIRS - The directory where to find the header file
# LIBUUID_LIBRARIES - Where to find the library file
#
# For conveniance, these variables are also set. They have the same values
# than the variables above. The user can thus choose his/her prefered way
# to write them.
# LIBUUID_INCLUDE_DIR
# LIBUUID_LIBRARY
#
# This file is in the public domain
include(FindPkgConfig)
pkg_check_modules(LIBUUID uuid)
if(NOT LIBUUID_FOUND)
find_path(LIBUUID_INCLUDE_DIRS NAMES uuid.h
PATH uuid
DOC "The libuuid include directory")
find_library(LIBUUID_LIBRARIES NAMES uuid
DOC "The libuuid library")
# Use some standard module to handle the QUIETLY and REQUIRED arguments, and
# set LIBUUID_FOUND to TRUE if these two variables are set.
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(LIBUUID REQUIRED_VARS LIBUUID_LIBRARIES LIBUUID_INCLUDE_DIRS)
# Compatibility for all the ways of writing these variables
if(LIBUUID_FOUND)
set(LIBUUID_INCLUDE_DIR ${LIBUUID_INCLUDE_DIRS})
set(LIBUUID_LIBRARY ${LIBUUID_LIBRARIES})
endif()
endif()
mark_as_advanced(LIBUUID_INCLUDE_DIRS LIBUUID_LIBRARIES)
...@@ -121,9 +121,14 @@ int main() ...@@ -121,9 +121,14 @@ int main()
/** /**
* Id generation * Id generation
*/ */
assert(XmppComponent::next_id() == "0"); std::cout << color << "Testing id generation…" << reset << std::endl;
assert(XmppComponent::next_id() == "1"); const std::string first_uuid = XmppComponent::next_id();
assert(XmppComponent::next_id() == "2"); const std::string second_uuid = XmppComponent::next_id();
std::cout << first_uuid << std::endl;
std::cout << second_uuid << std::endl;
assert(first_uuid.size() == 36);
assert(second_uuid.size() == 36);
assert(first_uuid != second_uuid);
/** /**
* Utils * Utils
......
...@@ -13,14 +13,14 @@ ...@@ -13,14 +13,14 @@
#include <config.h> #include <config.h>
#include <uuid.h>
#ifdef SYSTEMDDAEMON_FOUND #ifdef SYSTEMDDAEMON_FOUND
# include <systemd/sd-daemon.h> # include <systemd/sd-daemon.h>
#endif #endif
using namespace std::string_literals; using namespace std::string_literals;
unsigned long XmppComponent::current_id = 0;
static std::set<std::string> kickable_errors{ static std::set<std::string> kickable_errors{
"gone", "gone",
"internal-server-error", "internal-server-error",
...@@ -934,5 +934,9 @@ void XmppComponent::send_iq_version_request(const std::string& from, ...@@ -934,5 +934,9 @@ void XmppComponent::send_iq_version_request(const std::string& from,
std::string XmppComponent::next_id() std::string XmppComponent::next_id()
{ {
return std::to_string(XmppComponent::current_id++); char uuid_str[37];
uuid_t uuid;
uuid_generate(uuid);
uuid_unparse(uuid, uuid_str);
return uuid_str;
} }
...@@ -235,8 +235,6 @@ private: ...@@ -235,8 +235,6 @@ private:
*/ */
std::unordered_map<std::string, std::unique_ptr<Bridge>> bridges; std::unordered_map<std::string, std::unique_ptr<Bridge>> bridges;
static unsigned long current_id;
AdhocCommandsHandler adhoc_commands_handler; AdhocCommandsHandler adhoc_commands_handler;
XmppComponent(const XmppComponent&) = delete; XmppComponent(const XmppComponent&) = delete;
XmppComponent(XmppComponent&&) = delete; XmppComponent(XmppComponent&&) = delete;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment