2019-04-14 18:12:29 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
* Retroshare events service *
|
|
|
|
* *
|
|
|
|
* libretroshare: retroshare core library *
|
|
|
|
* *
|
|
|
|
* Copyright (C) 2019 Gioacchino Mazzurco <gio@eigenlab.org> *
|
|
|
|
* *
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Lesser General Public License as *
|
|
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
|
|
* License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
*******************************************************************************/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
#include "util/rsmemory.h"
|
2019-12-01 16:35:16 -05:00
|
|
|
#include "util/rsurl.h"
|
2019-04-14 18:12:29 -04:00
|
|
|
#include "serialiser/rsserializable.h"
|
|
|
|
#include "serialiser/rstypeserializer.h"
|
|
|
|
|
|
|
|
class RsEvents;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pointer to global instance of RsEvents service implementation
|
|
|
|
* @jsonapi{development}
|
2019-04-15 04:37:21 -04:00
|
|
|
*
|
|
|
|
* TODO: this should become std::weak_ptr once we have a reasonable services
|
|
|
|
* management.
|
2019-04-14 18:12:29 -04:00
|
|
|
*/
|
2019-08-27 05:59:38 -04:00
|
|
|
extern RsEvents* rsEvents;
|
2019-04-14 18:12:29 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Events types.
|
|
|
|
* When creating a new type of event, add a new type here and use that to
|
|
|
|
* initialize mType in the constructor of your derivative of @see RsEvent
|
|
|
|
*/
|
|
|
|
enum class RsEventType : uint32_t
|
|
|
|
{
|
|
|
|
NONE = 0, /// Used to detect uninitialized event
|
|
|
|
|
2019-05-02 05:28:34 -04:00
|
|
|
/// @see RsBroadcastDiscovery
|
|
|
|
BROADCAST_DISCOVERY_PEER_FOUND = 1,
|
|
|
|
|
|
|
|
/// @see RsDiscPendingPgpReceivedEvent
|
2019-05-02 10:23:46 -04:00
|
|
|
GOSSIP_DISCOVERY_INVITE_RECEIVED = 2,
|
2019-05-02 05:28:34 -04:00
|
|
|
|
|
|
|
/// @see AuthSSL
|
|
|
|
AUTHSSL_CONNECTION_AUTENTICATION = 3,
|
|
|
|
|
|
|
|
/// @see pqissl
|
2019-12-02 14:53:51 -05:00
|
|
|
PEER_CONNECTION = 4,
|
2019-04-14 18:12:29 -04:00
|
|
|
|
2019-04-23 11:20:23 -04:00
|
|
|
/// @see RsGxsChanges
|
|
|
|
GXS_CHANGES = 5,
|
|
|
|
|
2019-05-12 18:11:54 -04:00
|
|
|
/// Emitted when a peer state changes, @see RsPeers
|
|
|
|
PEER_STATE_CHANGED = 6,
|
|
|
|
|
2019-09-16 12:40:07 -04:00
|
|
|
/// @see RsMailStatusEvent
|
|
|
|
MAIL_STATUS_CHANGE = 7,
|
|
|
|
|
2019-12-11 18:00:51 -05:00
|
|
|
/// @see RsGxsCircleEvent
|
|
|
|
GXS_CIRCLES = 8,
|
|
|
|
|
2019-04-14 18:12:29 -04:00
|
|
|
MAX /// Used to detect invalid event type passed
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This struct is not meant to be used directly, you should create events type
|
|
|
|
* deriving from it.
|
|
|
|
*/
|
|
|
|
struct RsEvent : RsSerializable
|
|
|
|
{
|
2019-04-15 05:17:26 -04:00
|
|
|
protected:
|
2019-04-14 18:12:29 -04:00
|
|
|
RsEvent(RsEventType type) :
|
|
|
|
mType(type), mTimePoint(std::chrono::system_clock::now()) {}
|
|
|
|
|
2019-04-15 05:17:26 -04:00
|
|
|
RsEvent() = delete;
|
|
|
|
|
|
|
|
public:
|
2019-04-14 18:12:29 -04:00
|
|
|
RsEventType mType;
|
|
|
|
std::chrono::system_clock::time_point mTimePoint;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Derived types must call this method at beginning of their implementation
|
|
|
|
* of serial_process
|
|
|
|
* @see RsSerializable
|
|
|
|
*/
|
2019-05-02 05:28:34 -04:00
|
|
|
void serial_process( RsGenericSerializer::SerializeJob j,
|
|
|
|
RsGenericSerializer::SerializeContext& ctx) override
|
2019-04-14 18:12:29 -04:00
|
|
|
{
|
|
|
|
RS_SERIAL_PROCESS(mType);
|
|
|
|
|
|
|
|
rstime_t mTime = std::chrono::system_clock::to_time_t(mTimePoint);
|
|
|
|
RS_SERIAL_PROCESS(mTime);
|
|
|
|
mTimePoint = std::chrono::system_clock::from_time_t(mTime);
|
|
|
|
}
|
2019-04-15 05:17:26 -04:00
|
|
|
|
2019-05-02 05:28:34 -04:00
|
|
|
~RsEvent() override;
|
2019-04-14 18:12:29 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef uint32_t RsEventsHandlerId_t;
|
|
|
|
|
|
|
|
class RsEvents
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @brief Post event to the event queue.
|
|
|
|
* @param[in] event
|
|
|
|
* @param[out] errorMessage Optional storage for error messsage, meaningful
|
|
|
|
* only on failure.
|
|
|
|
* @return False on error, true otherwise.
|
|
|
|
*/
|
|
|
|
virtual bool postEvent(
|
2019-08-27 05:59:38 -04:00
|
|
|
std::shared_ptr<const RsEvent> event,
|
2019-04-14 18:12:29 -04:00
|
|
|
std::string& errorMessage = RS_DEFAULT_STORAGE_PARAM(std::string)
|
|
|
|
) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Send event directly to handlers. Blocking API
|
2019-08-27 05:59:38 -04:00
|
|
|
* The handlers get exectuded on the caller thread.
|
2019-04-14 18:12:29 -04:00
|
|
|
* @param[in] event
|
|
|
|
* @param[out] errorMessage Optional storage for error messsage, meaningful
|
|
|
|
* only on failure.
|
|
|
|
* @return False on error, true otherwise.
|
|
|
|
*/
|
|
|
|
virtual bool sendEvent(
|
2019-08-27 05:59:38 -04:00
|
|
|
std::shared_ptr<const RsEvent> event,
|
2019-04-14 18:12:29 -04:00
|
|
|
std::string& errorMessage = RS_DEFAULT_STORAGE_PARAM(std::string)
|
|
|
|
) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Generate unique handler identifier
|
|
|
|
* @return generate Id
|
|
|
|
*/
|
|
|
|
virtual RsEventsHandlerId_t generateUniqueHandlerId() = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Register events handler
|
|
|
|
* Every time an event is dispatced the registered events handlers will get
|
|
|
|
* their method handleEvent called with the event passed as paramether.
|
|
|
|
* @jsonapi{development,manualwrapper}
|
|
|
|
* @param multiCallback Function that will be called each time an event
|
|
|
|
* is dispatched.
|
|
|
|
* @param[inout] hId Optional storage for handler id, useful to
|
|
|
|
* eventually unregister the handler later. The
|
|
|
|
* value may be provided to the function call but
|
|
|
|
* must habe been generated with
|
|
|
|
* @see generateUniqueHandlerId()
|
|
|
|
* @return False on error, true otherwise.
|
|
|
|
*/
|
|
|
|
virtual bool registerEventsHandler(
|
2019-08-27 05:59:38 -04:00
|
|
|
std::function<void(std::shared_ptr<const RsEvent>)> multiCallback,
|
2019-04-14 18:12:29 -04:00
|
|
|
RsEventsHandlerId_t& hId = RS_DEFAULT_STORAGE_PARAM(RsEventsHandlerId_t, 0)
|
|
|
|
) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Unregister event handler
|
|
|
|
* @param[in] hId Id of the event handler to unregister
|
|
|
|
* @return True if the handler id has been found, false otherwise.
|
|
|
|
*/
|
|
|
|
virtual bool unregisterEventsHandler(RsEventsHandlerId_t hId) = 0;
|
|
|
|
|
|
|
|
virtual ~RsEvents();
|
|
|
|
};
|
2019-12-01 16:35:16 -05:00
|
|
|
|
|
|
|
//===================================================================================================//
|
2019-12-02 14:53:51 -05:00
|
|
|
// Connexion and security events //
|
2019-12-01 16:35:16 -05:00
|
|
|
//===================================================================================================//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event triggered by AuthSSL when authentication of a connection attempt either
|
|
|
|
* fail or success
|
|
|
|
*/
|
|
|
|
struct RsAuthSslConnectionAutenticationEvent : RsEvent
|
|
|
|
{
|
2019-12-02 14:53:51 -05:00
|
|
|
RsAuthSslConnectionAutenticationEvent() : RsEvent(RsEventType::AUTHSSL_CONNECTION_AUTENTICATION) {}
|
2019-12-01 16:35:16 -05:00
|
|
|
|
2019-12-02 14:53:51 -05:00
|
|
|
enum ConnectionErrorCode: uint8_t {
|
|
|
|
NO_ERROR = 0x00,
|
2019-12-01 16:35:16 -05:00
|
|
|
MISSING_AUTHENTICATION_INFO = 0x01,
|
|
|
|
PGP_SIGNATURE_VALIDATION_FAILED = 0x02,
|
|
|
|
MISMATCHED_PGP_ID = 0x03,
|
|
|
|
NO_CERTIFICATE_SUPPLIED = 0x04,
|
|
|
|
NOT_A_FRIEND = 0x05,
|
|
|
|
MISSING_CERTIFICATE = 0x06,
|
|
|
|
IP_IS_BLACKLISTED = 0x07,
|
2019-12-05 17:43:38 -05:00
|
|
|
PEER_REFUSED_CONNECTION = 0x08,
|
|
|
|
UNKNOWN_ERROR = 0x09,
|
2019-12-01 16:35:16 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
RsPeerId mSslId;
|
|
|
|
std::string mSslCn;
|
|
|
|
RsPgpId mPgpId;
|
|
|
|
RsUrl mLocator;
|
|
|
|
std::string mErrorMsg;
|
2019-12-02 14:53:51 -05:00
|
|
|
ConnectionErrorCode mErrorCode;
|
2019-12-01 16:35:16 -05:00
|
|
|
|
|
|
|
///* @see RsEvent @see RsSerializable
|
|
|
|
void serial_process( RsGenericSerializer::SerializeJob j,
|
|
|
|
RsGenericSerializer::SerializeContext& ctx) override
|
|
|
|
{
|
|
|
|
RsEvent::serial_process(j, ctx);
|
|
|
|
RS_SERIAL_PROCESS(mSslId);
|
|
|
|
RS_SERIAL_PROCESS(mSslCn);
|
|
|
|
RS_SERIAL_PROCESS(mPgpId);
|
|
|
|
RS_SERIAL_PROCESS(mLocator);
|
|
|
|
RS_SERIAL_PROCESS(mErrorMsg);
|
|
|
|
RS_SERIAL_PROCESS(mErrorCode);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-02 14:53:51 -05:00
|
|
|
struct RsConnectionEvent : RsEvent
|
|
|
|
{
|
|
|
|
RsConnectionEvent()
|
|
|
|
: RsEvent(RsEventType::PEER_CONNECTION),
|
2019-12-07 15:43:28 -05:00
|
|
|
mConnectionInfoCode(UNKNOWN) {}
|
2019-12-02 14:53:51 -05:00
|
|
|
|
|
|
|
enum ConnectionType: uint8_t {
|
|
|
|
UNKNOWN = 0x00,
|
|
|
|
PEER_CONNECTED = 0x01,
|
|
|
|
PEER_DISCONNECTED = 0x02,
|
2019-12-07 15:43:28 -05:00
|
|
|
PEER_TIME_SHIFT = 0x03, // mStrInfo1 = time shift in seconds
|
|
|
|
PEER_REPORTS_WRONG_IP = 0x04, // mStrInfo1 = address reported, mStrInfo2 = own address
|
2019-12-02 14:53:51 -05:00
|
|
|
};
|
|
|
|
|
2019-12-07 15:43:28 -05:00
|
|
|
ConnectionType mConnectionInfoCode;
|
2019-12-02 14:53:51 -05:00
|
|
|
RsPeerId mSslId;
|
2019-12-07 15:43:28 -05:00
|
|
|
std::string mStrInfo1;
|
|
|
|
std::string mStrInfo2;
|
2019-12-02 14:53:51 -05:00
|
|
|
|
|
|
|
///* @see RsEvent @see RsSerializable
|
|
|
|
void serial_process( RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) override
|
|
|
|
{
|
|
|
|
RsEvent::serial_process(j, ctx);
|
2019-12-07 15:43:28 -05:00
|
|
|
RS_SERIAL_PROCESS(mConnectionInfoCode);
|
2019-12-02 14:53:51 -05:00
|
|
|
RS_SERIAL_PROCESS(mSslId);
|
2019-12-07 16:26:26 -05:00
|
|
|
RS_SERIAL_PROCESS(mStrInfo1);
|
|
|
|
RS_SERIAL_PROCESS(mStrInfo2);
|
2019-12-02 14:53:51 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|