2018-04-28 03:02:42 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Federation :General Library and Utils"
|
|
|
|
};
|
|
|
|
|
2019-04-11 14:29:59 +02:00
|
|
|
namespace ircd::m::feds
|
2018-04-28 03:02:42 +02:00
|
|
|
{
|
2019-04-19 01:23:03 +02:00
|
|
|
struct request_base;
|
|
|
|
|
2019-04-12 09:09:32 +02:00
|
|
|
template<class T>
|
|
|
|
struct request;
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
static bool
|
2019-04-19 01:23:03 +02:00
|
|
|
handler(const opts &, const closure &, std::list<std::unique_ptr<request_base>> &);
|
2019-04-12 09:09:32 +02:00
|
|
|
|
|
|
|
template<class T>
|
2019-04-19 01:23:03 +02:00
|
|
|
static std::list<std::unique_ptr<request_base>>
|
2019-04-12 09:09:32 +02:00
|
|
|
creator(const opts &, const std::function<T (request<T> &, const string_view &origin)> &);
|
2019-04-18 14:16:21 +02:00
|
|
|
|
|
|
|
static bool head(const opts &, const closure &);
|
|
|
|
static bool auth(const opts &, const closure &);
|
|
|
|
static bool event(const opts &, const closure &);
|
|
|
|
static bool state(const opts &, const closure &);
|
|
|
|
static bool backfill(const opts &, const closure &);
|
|
|
|
static bool version(const opts &, const closure &);
|
|
|
|
static bool keys(const opts &, const closure &);
|
|
|
|
|
2019-04-19 00:25:01 +02:00
|
|
|
bool execute(const vector_view<const opts> &opts, const closure &closure);
|
2018-04-30 14:43:56 +02:00
|
|
|
}
|
|
|
|
|
2019-04-19 01:23:03 +02:00
|
|
|
//
|
|
|
|
// request_base
|
|
|
|
//
|
|
|
|
|
|
|
|
/// Polymorphic non-template base
|
|
|
|
struct ircd::m::feds::request_base
|
|
|
|
{
|
|
|
|
const feds::opts *opts {nullptr};
|
|
|
|
|
|
|
|
request_base(const feds::opts &opts)
|
|
|
|
:opts{&opts}
|
|
|
|
{}
|
|
|
|
|
|
|
|
request_base() = default;
|
|
|
|
virtual ~request_base() noexcept;
|
|
|
|
};
|
|
|
|
|
|
|
|
ircd::m::feds::request_base::~request_base()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// request
|
|
|
|
//
|
|
|
|
|
2019-04-11 14:29:59 +02:00
|
|
|
template<class T>
|
|
|
|
struct ircd::m::feds::request
|
2019-04-19 01:23:03 +02:00
|
|
|
:request_base
|
|
|
|
,T
|
2018-04-30 14:43:56 +02:00
|
|
|
{
|
|
|
|
char origin[256];
|
2019-04-11 14:29:59 +02:00
|
|
|
char buf[8_KiB];
|
2018-04-30 14:43:56 +02:00
|
|
|
|
2019-04-19 01:23:03 +02:00
|
|
|
request(const feds::opts &opts, const std::function<T (request &)> &closure)
|
|
|
|
:request_base{opts}
|
|
|
|
,T(closure(*this))
|
2019-04-12 09:31:07 +02:00
|
|
|
{}
|
|
|
|
|
2019-04-11 14:29:59 +02:00
|
|
|
request(request &&) = delete;
|
|
|
|
request(const request &) = delete;
|
2019-04-19 01:23:03 +02:00
|
|
|
~request() noexcept;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
ircd::m::feds::request<T>::~request()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
if(this->valid())
|
2019-04-17 02:56:11 +02:00
|
|
|
{
|
2019-04-19 01:23:03 +02:00
|
|
|
server::cancel(*this);
|
|
|
|
this->wait();
|
2019-04-17 02:56:11 +02:00
|
|
|
}
|
2019-04-19 01:23:03 +02:00
|
|
|
}
|
2018-04-28 03:02:42 +02:00
|
|
|
|
2019-04-12 09:31:07 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// m/feds.h
|
|
|
|
//
|
2018-05-05 12:23:56 +02:00
|
|
|
|
2019-04-12 09:53:32 +02:00
|
|
|
bool
|
|
|
|
IRCD_MODULE_EXPORT
|
2019-04-19 00:25:01 +02:00
|
|
|
ircd::m::feds::execute(const vector_view<const opts> &optsv,
|
2019-04-18 14:16:21 +02:00
|
|
|
const closure &closure)
|
|
|
|
{
|
2019-04-19 00:25:01 +02:00
|
|
|
for(const auto &opts : optsv) switch(opts.op)
|
2019-04-18 14:16:21 +02:00
|
|
|
{
|
|
|
|
case op::head: return head(opts, closure);
|
|
|
|
case op::auth: return auth(opts, closure);
|
|
|
|
case op::event: return event(opts, closure);
|
|
|
|
case op::state: return state(opts, closure);
|
|
|
|
case op::backfill: return backfill(opts, closure);
|
|
|
|
case op::version: return version(opts, closure);
|
|
|
|
case op::keys: return keys(opts, closure);
|
|
|
|
case op::noop: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::feds::keys(const opts &opts,
|
|
|
|
const closure &closure)
|
2019-04-12 09:53:32 +02:00
|
|
|
{
|
|
|
|
const auto make_request{[&opts]
|
|
|
|
(auto &request, const auto &origin)
|
|
|
|
{
|
|
|
|
m::v1::key::query::opts v1opts;
|
|
|
|
v1opts.dynamic = false;
|
|
|
|
v1opts.remote = string_view
|
|
|
|
{
|
2019-04-15 23:28:05 +02:00
|
|
|
strlcpy{request.origin, origin}
|
2019-04-12 09:53:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const m::v1::key::server_key server_key
|
|
|
|
{
|
|
|
|
opts.arg[0], opts.arg[1]
|
|
|
|
};
|
|
|
|
|
|
|
|
return m::v1::key::query
|
|
|
|
{
|
|
|
|
{&server_key, 1}, request.buf, std::move(v1opts)
|
|
|
|
};
|
|
|
|
}};
|
|
|
|
|
|
|
|
auto requests
|
|
|
|
{
|
|
|
|
creator<m::v1::key::query>(opts, make_request)
|
|
|
|
};
|
|
|
|
|
2019-04-19 01:23:03 +02:00
|
|
|
return handler<m::v1::key::query>(opts, closure, requests);
|
2019-04-12 09:53:32 +02:00
|
|
|
}
|
|
|
|
|
2019-04-12 09:09:32 +02:00
|
|
|
bool
|
|
|
|
ircd::m::feds::version(const opts &opts,
|
|
|
|
const closure &closure)
|
|
|
|
{
|
|
|
|
static const auto make_request{[]
|
|
|
|
(auto &request, const auto &origin)
|
|
|
|
{
|
|
|
|
m::v1::version::opts opts;
|
|
|
|
opts.dynamic = false;
|
|
|
|
opts.remote = string_view
|
|
|
|
{
|
2019-04-15 23:28:05 +02:00
|
|
|
strlcpy{request.origin, origin}
|
2019-04-12 09:09:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return m::v1::version
|
|
|
|
{
|
|
|
|
request.buf, std::move(opts)
|
|
|
|
};
|
|
|
|
}};
|
|
|
|
|
|
|
|
auto requests
|
|
|
|
{
|
|
|
|
creator<m::v1::version>(opts, make_request)
|
|
|
|
};
|
|
|
|
|
2019-04-19 01:23:03 +02:00
|
|
|
return handler<m::v1::version>(opts, closure, requests);
|
2019-04-12 09:09:32 +02:00
|
|
|
}
|
|
|
|
|
2019-04-12 10:53:57 +02:00
|
|
|
bool
|
|
|
|
ircd::m::feds::backfill(const opts &opts,
|
|
|
|
const closure &closure)
|
|
|
|
{
|
|
|
|
const auto make_request{[&opts]
|
|
|
|
(auto &request, const auto &origin)
|
|
|
|
{
|
|
|
|
m::v1::backfill::opts v1opts;
|
|
|
|
v1opts.dynamic = true;
|
|
|
|
v1opts.event_id = opts.event_id;
|
|
|
|
v1opts.limit = opts.argi[0];
|
|
|
|
v1opts.remote = string_view
|
|
|
|
{
|
2019-04-15 23:28:05 +02:00
|
|
|
strlcpy{request.origin, origin}
|
2019-04-12 10:53:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return m::v1::backfill
|
|
|
|
{
|
|
|
|
opts.room_id, request.buf, std::move(v1opts)
|
|
|
|
};
|
|
|
|
}};
|
|
|
|
|
|
|
|
auto requests
|
|
|
|
{
|
|
|
|
creator<m::v1::backfill>(opts, make_request)
|
|
|
|
};
|
|
|
|
|
2019-04-19 01:23:03 +02:00
|
|
|
return handler<m::v1::backfill>(opts, closure, requests);
|
2019-04-12 10:53:57 +02:00
|
|
|
}
|
|
|
|
|
2019-04-11 14:29:59 +02:00
|
|
|
bool
|
|
|
|
ircd::m::feds::state(const opts &opts,
|
|
|
|
const closure &closure)
|
2018-05-05 12:23:56 +02:00
|
|
|
{
|
2019-04-11 14:29:59 +02:00
|
|
|
const auto make_request{[&opts]
|
|
|
|
(auto &request, const auto &origin)
|
|
|
|
{
|
|
|
|
m::v1::state::opts v1opts;
|
|
|
|
v1opts.dynamic = true;
|
2019-04-12 09:52:29 +02:00
|
|
|
v1opts.ids_only = opts.arg[0] == "ids";
|
2019-04-11 14:29:59 +02:00
|
|
|
v1opts.event_id = opts.event_id;
|
|
|
|
v1opts.remote = string_view
|
|
|
|
{
|
|
|
|
strlcpy{request.origin, origin}
|
|
|
|
};
|
2018-05-05 12:23:56 +02:00
|
|
|
|
2019-04-11 14:29:59 +02:00
|
|
|
return m::v1::state
|
|
|
|
{
|
|
|
|
opts.room_id, request.buf, std::move(v1opts)
|
|
|
|
};
|
|
|
|
}};
|
2018-05-05 12:23:56 +02:00
|
|
|
|
2019-04-12 09:09:32 +02:00
|
|
|
auto requests
|
2018-05-05 12:23:56 +02:00
|
|
|
{
|
2019-04-12 09:09:32 +02:00
|
|
|
creator<m::v1::state>(opts, make_request)
|
2019-04-11 14:29:59 +02:00
|
|
|
};
|
2018-05-05 12:23:56 +02:00
|
|
|
|
2019-04-19 01:23:03 +02:00
|
|
|
return handler<m::v1::state>(opts, closure, requests);
|
2018-05-05 12:23:56 +02:00
|
|
|
}
|
|
|
|
|
2019-04-12 09:16:27 +02:00
|
|
|
bool
|
|
|
|
ircd::m::feds::event(const opts &opts,
|
|
|
|
const closure &closure)
|
|
|
|
{
|
|
|
|
const auto make_request{[&opts]
|
|
|
|
(auto &request, const auto &origin)
|
|
|
|
{
|
|
|
|
m::v1::event::opts v1opts;
|
|
|
|
v1opts.dynamic = true;
|
|
|
|
v1opts.remote = string_view
|
|
|
|
{
|
2019-04-15 23:28:05 +02:00
|
|
|
strlcpy{request.origin, origin}
|
2019-04-12 09:16:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return m::v1::event
|
|
|
|
{
|
|
|
|
opts.event_id, request.buf, std::move(v1opts)
|
|
|
|
};
|
|
|
|
}};
|
|
|
|
|
|
|
|
auto requests
|
|
|
|
{
|
|
|
|
creator<m::v1::event>(opts, make_request)
|
|
|
|
};
|
|
|
|
|
2019-04-19 01:23:03 +02:00
|
|
|
return handler<m::v1::event>(opts, closure, requests);
|
2019-04-12 09:16:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-18 13:07:21 +02:00
|
|
|
bool
|
|
|
|
ircd::m::feds::auth(const opts &opts,
|
|
|
|
const closure &closure)
|
|
|
|
{
|
|
|
|
const auto make_request{[&opts]
|
|
|
|
(auto &request, const auto &origin)
|
|
|
|
{
|
|
|
|
m::v1::event_auth::opts v1opts;
|
|
|
|
v1opts.dynamic = true;
|
|
|
|
v1opts.remote = string_view
|
|
|
|
{
|
|
|
|
strlcpy{request.origin, origin}
|
|
|
|
};
|
|
|
|
|
|
|
|
return m::v1::event_auth
|
|
|
|
{
|
|
|
|
opts.room_id, opts.user_id, request.buf, std::move(v1opts)
|
|
|
|
};
|
|
|
|
}};
|
|
|
|
|
|
|
|
auto requests
|
|
|
|
{
|
|
|
|
creator<m::v1::event_auth>(opts, make_request)
|
|
|
|
};
|
|
|
|
|
2019-04-19 01:23:03 +02:00
|
|
|
return handler<m::v1::event_auth>(opts, closure, requests);
|
2019-04-18 13:07:21 +02:00
|
|
|
}
|
|
|
|
|
2019-04-11 14:29:59 +02:00
|
|
|
bool
|
|
|
|
ircd::m::feds::head(const opts &opts,
|
|
|
|
const closure &closure)
|
2018-05-05 12:23:56 +02:00
|
|
|
{
|
2019-04-11 14:29:59 +02:00
|
|
|
const auto make_request{[&opts]
|
|
|
|
(auto &request, const auto &origin)
|
2018-05-05 12:23:56 +02:00
|
|
|
{
|
2019-04-11 14:29:59 +02:00
|
|
|
m::v1::make_join::opts v1opts;
|
|
|
|
v1opts.remote = string_view
|
|
|
|
{
|
|
|
|
strlcpy{request.origin, origin}
|
|
|
|
};
|
2018-05-05 12:23:56 +02:00
|
|
|
|
2019-04-11 14:29:59 +02:00
|
|
|
return m::v1::make_join
|
|
|
|
{
|
|
|
|
opts.room_id, opts.user_id, request.buf, std::move(v1opts)
|
|
|
|
};
|
|
|
|
}};
|
|
|
|
|
2019-04-12 09:09:32 +02:00
|
|
|
auto requests
|
2018-05-05 12:23:56 +02:00
|
|
|
{
|
2019-04-12 09:09:32 +02:00
|
|
|
creator<m::v1::make_join>(opts, make_request)
|
2018-05-05 12:23:56 +02:00
|
|
|
};
|
|
|
|
|
2019-04-19 01:23:03 +02:00
|
|
|
return handler<m::v1::make_join>(opts, closure, requests);
|
2018-04-28 03:02:42 +02:00
|
|
|
}
|
|
|
|
|
2019-04-12 09:31:07 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// (internal)
|
|
|
|
//
|
|
|
|
|
|
|
|
template<class T>
|
2019-04-19 01:23:03 +02:00
|
|
|
std::list<std::unique_ptr<m::feds::request_base>>
|
2019-04-12 09:31:07 +02:00
|
|
|
ircd::m::feds::creator(const opts &opts,
|
|
|
|
const std::function<T (request<T> &, const string_view &origin)> &closure)
|
|
|
|
{
|
|
|
|
assert(opts.room_id);
|
|
|
|
const m::room::origins origins
|
|
|
|
{
|
|
|
|
opts.room_id
|
|
|
|
};
|
|
|
|
|
2019-04-19 01:23:03 +02:00
|
|
|
std::list<std::unique_ptr<request_base>> ret;
|
|
|
|
origins.for_each([&opts, &ret, &closure]
|
2019-04-12 09:31:07 +02:00
|
|
|
(const string_view &origin)
|
|
|
|
{
|
|
|
|
if(!server::errmsg(origin)) try
|
|
|
|
{
|
2019-04-19 01:23:03 +02:00
|
|
|
ret.emplace_back(std::make_unique<request<T>>(opts, [&closure, &origin]
|
2019-04-12 09:31:07 +02:00
|
|
|
(auto &request)
|
|
|
|
{
|
|
|
|
return closure(request, origin);
|
2019-04-19 01:23:03 +02:00
|
|
|
}));
|
2019-04-12 09:31:07 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
bool
|
|
|
|
ircd::m::feds::handler(const opts &opts,
|
|
|
|
const closure &closure,
|
2019-04-19 01:23:03 +02:00
|
|
|
std::list<std::unique_ptr<request_base>> &reqs)
|
2019-04-12 09:31:07 +02:00
|
|
|
{
|
|
|
|
const auto when
|
|
|
|
{
|
|
|
|
now<steady_point>() + opts.timeout
|
|
|
|
};
|
|
|
|
|
|
|
|
while(!reqs.empty())
|
|
|
|
{
|
2019-04-19 01:23:03 +02:00
|
|
|
static const auto dereferencer{[]
|
|
|
|
(auto &iterator) -> T &
|
|
|
|
{
|
|
|
|
return dynamic_cast<T &>(**iterator);
|
|
|
|
}};
|
|
|
|
|
2019-04-12 09:31:07 +02:00
|
|
|
auto next
|
|
|
|
{
|
2019-04-19 01:23:03 +02:00
|
|
|
ctx::when_any(begin(reqs), end(reqs), dereferencer)
|
2019-04-12 09:31:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!next.wait_until(when, std::nothrow))
|
|
|
|
break;
|
|
|
|
|
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
next.get()
|
|
|
|
};
|
|
|
|
|
2019-04-18 08:02:33 +02:00
|
|
|
assert(it != end(reqs));
|
2019-04-19 01:23:03 +02:00
|
|
|
|
|
|
|
auto &req(dynamic_cast<feds::request<T> &>(**it)); try
|
2019-04-12 09:31:07 +02:00
|
|
|
{
|
|
|
|
const auto code{req.get()};
|
|
|
|
const json::array &array{req.in.content};
|
|
|
|
const json::object &object{req.in.content};
|
2019-04-18 13:15:13 +02:00
|
|
|
if(!closure({&opts, req.origin, {}, object, array}))
|
2019-04-12 09:31:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch(const std::exception &)
|
|
|
|
{
|
|
|
|
const ctx::exception_handler eptr;
|
2019-04-18 08:02:33 +02:00
|
|
|
const std::exception_ptr &eptr_(eptr);
|
2019-04-18 13:15:13 +02:00
|
|
|
if(!closure({&opts, req.origin, eptr_}))
|
2019-04-12 09:31:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
reqs.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|