From a16b33031c7bcf2eabf1e0c3571000234b7740df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sun, 24 Jan 2021 22:46:48 +0100 Subject: Attach service --- include/erebos/attach.h | 49 ++++++++++++++++++++++ include/erebos/identity.h | 1 + include/erebos/network.h | 20 ++++++++- include/erebos/pairing.h | 104 ++++++++++++++++++++++++++++++++++++++++++++++ include/erebos/storage.h | 9 ++++ 5 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 include/erebos/attach.h create mode 100644 include/erebos/pairing.h (limited to 'include/erebos') diff --git a/include/erebos/attach.h b/include/erebos/attach.h new file mode 100644 index 0000000..14e6af3 --- /dev/null +++ b/include/erebos/attach.h @@ -0,0 +1,49 @@ +#pragma once + +#include + +#include +#include +#include +#include +#include + +namespace erebos { + +using std::mutex; +using std::optional; +using std::promise; +using std::string; +using std::vector; + +struct AttachIdentity; + +class AttachService : public PairingService +{ +public: + AttachService(); + virtual ~AttachService(); + + UUID uuid() const override; + + void attachTo(const Peer &); + +protected: + virtual Stored handlePairingComplete(const Peer &) override; + virtual void handlePairingResult(Context &, Stored) override; + + mutex handlerLock; +}; + +template class Signed; + +struct AttachIdentity +{ + Stored> identity; + vector> keys; + + static AttachIdentity load(const Ref &); + Ref store(const Storage &) const; +}; + +} diff --git a/include/erebos/identity.h b/include/erebos/identity.h index 66a4afb..aeddd08 100644 --- a/include/erebos/identity.h +++ b/include/erebos/identity.h @@ -20,6 +20,7 @@ public: std::optional owner() const; const Identity & finalOwner() const; + Stored keyIdentity() const; Stored keyMessage() const; bool sameAs(const Identity &) const; diff --git a/include/erebos/network.h b/include/erebos/network.h index 8f3debe..5a1fca6 100644 --- a/include/erebos/network.h +++ b/include/erebos/network.h @@ -10,10 +10,15 @@ namespace erebos { class Server { + struct Priv; public: Server(const Head &, std::vector> &&); + Server(const std::shared_ptr &); ~Server(); + const Head & localHead() const; + + const Identity & identity() const; template S & svc(); class PeerList & peerList() const; @@ -22,7 +27,6 @@ public: private: Service & svcHelper(const std::type_info &); - struct Priv; const std::shared_ptr p; }; @@ -39,13 +43,27 @@ public: Peer(const std::shared_ptr & p); ~Peer(); + Server server() const; + + const Storage & tempStorage() const; + const PartialStorage & partialStorage() const; + std::string name() const; std::optional identity() const; bool hasChannel() const; bool send(UUID, const Ref &) const; + bool send(UUID, const Object &) const; + + bool operator==(const Peer & other) const; + bool operator!=(const Peer & other) const; + bool operator<(const Peer & other) const; + bool operator<=(const Peer & other) const; + bool operator>(const Peer & other) const; + bool operator>=(const Peer & other) const; private: + bool send(UUID, const Ref &, const Object &) const; std::shared_ptr p; }; diff --git a/include/erebos/pairing.h b/include/erebos/pairing.h new file mode 100644 index 0000000..f0952d8 --- /dev/null +++ b/include/erebos/pairing.h @@ -0,0 +1,104 @@ +#pragma once + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace erebos { + +using std::function; +using std::future; +using std::map; +using std::mutex; +using std::string; +using std::variant; +using std::vector; + +/** + * Template-less base class for the paring functionality that does not depend + * on the result parameter. + */ +class PairingServiceBase : public Service +{ +public: + typedef function RequestInitHook; + void onRequestInit(RequestInitHook); + + typedef function(const Peer &, string)> ConfirmHook; + void onResponse(ConfirmHook); + void onRequest(ConfirmHook); + + typedef function RequestNonceFailedHook; + void onRequestNonceFailed(RequestNonceFailedHook); + +protected: + void requestPairing(UUID serviceId, const Peer & peer); + virtual void handle(Context &) override; + virtual Ref handlePairingCompleteRef(const Peer &) = 0; + virtual void handlePairingResult(Context &) = 0; + +private: + static vector nonceDigest(const Identity & id1, const Identity & id2, + const vector & nonce1, const vector & nonce2); + static string confirmationNumber(const vector &); + void waitForConfirmation(Peer peer, string confirm); + + RequestInitHook requestInitHook; + ConfirmHook responseHook; + ConfirmHook requestHook; + RequestNonceFailedHook requestNonceFailedHook; + + optional result; + + enum class StatePhase { + NoPairing, + OurRequest, + OurRequestConfirm, + OurRequestReady, + PeerRequest, + PeerRequestConfirm, + PairingDone, + PairingFailed + }; + + struct State { + StatePhase phase; + vector nonce; + vector peerCheck; + }; + + map peerStates; + mutex stateLock; +}; + +template +class PairingService : public PairingServiceBase +{ +protected: + virtual Stored handlePairingComplete(const Peer &) = 0; + virtual void handlePairingResult(Context &, Stored) = 0; + + virtual Ref handlePairingCompleteRef(const Peer &) override final; + virtual void handlePairingResult(Context &) override final; +}; + +template +Ref PairingService::handlePairingCompleteRef(const Peer & peer) +{ + return handlePairingComplete(peer).ref(); +} + +template +void PairingService::handlePairingResult(Context & ctx) +{ + handlePairingResult(ctx, Stored::load(ctx.ref())); +} + +} diff --git a/include/erebos/storage.h b/include/erebos/storage.h index 29eaa8f..4f67a4b 100644 --- a/include/erebos/storage.h +++ b/include/erebos/storage.h @@ -123,6 +123,9 @@ public: explicit operator std::string() const; bool isZero() const; + static Digest of(const std::vector & content); + template static Digest of(const ObjectT &); + const std::array & arr() const { return value; } bool operator==(const Digest & other) const { return value == other.value; } @@ -136,6 +139,12 @@ private: std::array value; }; +template +Digest Digest::of(const ObjectT & obj) +{ + return Digest::of(obj.encode()); +} + class PartialRef { public: -- cgit v1.2.3