2018-02-04 03:22:01 +01: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.
|
2017-10-25 18:42:23 +02:00
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
2018-03-14 21:36:13 +01:00
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
2017-11-16 02:48:25 +01:00
|
|
|
{
|
2018-03-14 21:36:13 +01:00
|
|
|
"federation send"
|
2017-11-16 02:48:25 +01:00
|
|
|
};
|
|
|
|
|
2022-08-31 21:40:43 +02:00
|
|
|
log::log
|
|
|
|
txn_log
|
|
|
|
{
|
|
|
|
"m.txn"
|
|
|
|
};
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource
|
2017-10-25 18:42:23 +02:00
|
|
|
send_resource
|
|
|
|
{
|
2017-12-12 21:26:39 +01:00
|
|
|
"/_matrix/federation/v1/send/",
|
2017-10-25 18:42:23 +02:00
|
|
|
{
|
2017-12-12 21:26:39 +01:00
|
|
|
"federation send",
|
2017-10-25 18:42:23 +02:00
|
|
|
resource::DIRECTORY,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-22 22:45:09 +01:00
|
|
|
conf::item<bool>
|
|
|
|
allow_self
|
|
|
|
{
|
|
|
|
{ "name", "ircd.federation.send.allow_self" },
|
2020-03-04 01:14:47 +01:00
|
|
|
{ "default", false },
|
2019-02-22 22:45:09 +01:00
|
|
|
};
|
|
|
|
|
2020-01-08 00:14:47 +01:00
|
|
|
conf::item<size_t>
|
|
|
|
eval_max_per_node
|
|
|
|
{
|
|
|
|
{ "name", "ircd.federation.send.eval.max_per_node" },
|
2020-04-20 09:29:41 +02:00
|
|
|
{ "default", 4L },
|
2020-01-08 00:14:47 +01:00
|
|
|
};
|
|
|
|
|
2020-03-04 01:15:04 +01:00
|
|
|
conf::item<bool>
|
|
|
|
fetch_state
|
|
|
|
{
|
|
|
|
{ "name", "ircd.federation.send.fetch_state" },
|
|
|
|
{ "default", true },
|
|
|
|
};
|
|
|
|
|
|
|
|
conf::item<bool>
|
|
|
|
fetch_prev
|
|
|
|
{
|
|
|
|
{ "name", "ircd.federation.send.fetch_prev" },
|
|
|
|
{ "default", true },
|
|
|
|
};
|
|
|
|
|
2020-12-15 20:51:06 +01:00
|
|
|
static void
|
2017-10-25 18:42:23 +02:00
|
|
|
handle_edu(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request::object<m::txn> &request,
|
2017-10-25 18:42:23 +02:00
|
|
|
const string_view &txn_id,
|
2018-03-07 20:02:23 +01:00
|
|
|
const m::edu &edu)
|
2017-10-25 18:42:23 +02:00
|
|
|
{
|
2018-03-07 20:02:23 +01:00
|
|
|
m::event event;
|
2020-04-12 23:55:54 +02:00
|
|
|
json::get<"origin"_>(event) = request.node_id;
|
2018-03-07 20:02:23 +01:00
|
|
|
json::get<"origin_server_ts"_>(event) = at<"origin_server_ts"_>(request);
|
|
|
|
json::get<"content"_>(event) = at<"content"_>(edu);
|
|
|
|
json::get<"type"_>(event) = at<"edu_type"_>(edu);
|
2018-05-21 04:10:56 +02:00
|
|
|
json::get<"depth"_>(event) = json::undefined_number;
|
2018-03-07 20:02:23 +01:00
|
|
|
|
|
|
|
m::vm::opts vmopts;
|
2020-03-26 18:28:05 +01:00
|
|
|
vmopts.nothrows = -1U;
|
2020-04-12 23:55:54 +02:00
|
|
|
vmopts.node_id = request.node_id;
|
2020-01-07 01:22:30 +01:00
|
|
|
vmopts.txn_id = txn_id;
|
2019-09-14 00:26:44 +02:00
|
|
|
vmopts.edu = true;
|
2019-02-28 03:07:38 +01:00
|
|
|
vmopts.notify_clients = false;
|
|
|
|
vmopts.notify_servers = false;
|
2018-03-07 20:02:23 +01:00
|
|
|
m::vm::eval eval
|
|
|
|
{
|
|
|
|
event, vmopts
|
|
|
|
};
|
2017-10-25 18:42:23 +02:00
|
|
|
}
|
|
|
|
|
2020-12-15 20:51:06 +01:00
|
|
|
static void
|
2019-02-08 16:55:44 +01:00
|
|
|
handle_pdus(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request::object<m::txn> &request,
|
2019-02-08 16:55:44 +01:00
|
|
|
const string_view &txn_id,
|
2020-12-15 20:51:06 +01:00
|
|
|
const json::array &pdus,
|
|
|
|
json::stack &out)
|
2017-10-25 18:42:23 +02:00
|
|
|
{
|
2020-12-15 20:51:06 +01:00
|
|
|
json::stack::object out_pdus
|
|
|
|
{
|
|
|
|
out, "pdus"
|
|
|
|
};
|
|
|
|
|
2018-03-07 20:02:23 +01:00
|
|
|
m::vm::opts vmopts;
|
2020-12-15 20:51:06 +01:00
|
|
|
vmopts.out = &out_pdus;
|
2019-09-13 22:32:20 +02:00
|
|
|
vmopts.warnlog = 0;
|
|
|
|
vmopts.infolog_accept = true;
|
2018-03-07 20:02:23 +01:00
|
|
|
vmopts.nothrows = -1U;
|
2020-04-12 23:55:54 +02:00
|
|
|
vmopts.node_id = request.node_id;
|
2020-01-07 01:22:30 +01:00
|
|
|
vmopts.txn_id = txn_id;
|
2020-05-12 05:24:54 +02:00
|
|
|
vmopts.phase.set(m::vm::phase::FETCH_PREV, bool(fetch_prev));
|
|
|
|
vmopts.phase.set(m::vm::phase::FETCH_STATE, bool(fetch_state));
|
2020-04-17 23:28:18 +02:00
|
|
|
vmopts.fetch_prev_wait_count = -1;
|
2018-03-07 20:02:23 +01:00
|
|
|
m::vm::eval eval
|
|
|
|
{
|
2019-02-08 16:55:44 +01:00
|
|
|
pdus, vmopts
|
2018-03-07 20:02:23 +01:00
|
|
|
};
|
2017-10-25 18:42:23 +02:00
|
|
|
}
|
|
|
|
|
2020-12-15 20:51:06 +01:00
|
|
|
static void
|
2020-03-26 18:28:05 +01:00
|
|
|
handle_txn(client &client,
|
|
|
|
const m::resource::request::object<m::txn> &request,
|
|
|
|
const string_view &txn_id,
|
2020-12-15 20:51:06 +01:00
|
|
|
json::stack &out)
|
2020-03-26 18:28:05 +01:00
|
|
|
try
|
|
|
|
{
|
2020-04-02 23:41:11 +02:00
|
|
|
// We process PDU's before EDU's and we process all PDU's at once by
|
|
|
|
// passing the complete array. The events are sorted and dependencies
|
|
|
|
// are detected within the array. If we looped here for eval'ing one
|
|
|
|
// at a time we'd risk issuing fetch requests for prev_events which may
|
|
|
|
// exist in the same array, etc.
|
2020-12-15 20:51:06 +01:00
|
|
|
if(!empty(json::get<"pdus"_>(request)))
|
|
|
|
handle_pdus(client, request, txn_id, json::get<"pdus"_>(request), out);
|
2020-04-02 23:41:11 +02:00
|
|
|
|
|
|
|
// We process EDU's after PDU's. This is because checks on EDU's may
|
|
|
|
// depend on updates provided by PDU's in the same txn; for example:
|
|
|
|
// 1. user X joins room Y. 2. user X starts typing in room Y. Note that
|
|
|
|
// we also process EDU's one at a time since there is no dependency graph
|
|
|
|
// or anything like that so if this loop wasn't here it would just be
|
|
|
|
// somewhere else.
|
2020-12-15 20:51:06 +01:00
|
|
|
for(const json::object edu : json::get<"edus"_>(request))
|
2020-03-26 18:28:05 +01:00
|
|
|
handle_edu(client, request, txn_id, edu);
|
|
|
|
}
|
2020-03-31 23:50:46 +02:00
|
|
|
catch(const m::vm::error &e)
|
2020-03-26 18:28:05 +01:00
|
|
|
{
|
2020-03-31 23:50:46 +02:00
|
|
|
const json::object &content
|
|
|
|
{
|
|
|
|
e.content
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::string error[]
|
|
|
|
{
|
|
|
|
content["errcode"],
|
|
|
|
content["error"]
|
|
|
|
};
|
|
|
|
|
|
|
|
log::error
|
|
|
|
{
|
2022-08-31 21:40:43 +02:00
|
|
|
txn_log, "Unhandled error processing txn '%s' from '%s' :%s :%s :%s",
|
2020-03-31 23:50:46 +02:00
|
|
|
txn_id,
|
2020-04-12 23:55:54 +02:00
|
|
|
request.node_id,
|
2020-03-31 23:50:46 +02:00
|
|
|
e.what(),
|
|
|
|
error[0],
|
|
|
|
error[1],
|
|
|
|
};
|
|
|
|
|
2020-03-26 18:28:05 +01:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2020-05-05 10:03:45 +02:00
|
|
|
log::error
|
2020-03-26 18:28:05 +01:00
|
|
|
{
|
2022-08-31 21:40:43 +02:00
|
|
|
txn_log, "Unhandled error processing txn '%s' from '%s' :%s",
|
2020-03-26 18:28:05 +01:00
|
|
|
txn_id,
|
2020-04-12 23:55:54 +02:00
|
|
|
request.node_id,
|
2020-03-26 18:28:05 +01:00
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::response
|
2017-10-25 18:42:23 +02:00
|
|
|
handle_put(client &client,
|
2019-09-29 01:12:07 +02:00
|
|
|
const m::resource::request::object<m::txn> &request)
|
2017-10-25 18:42:23 +02:00
|
|
|
{
|
2019-02-18 19:33:35 +01:00
|
|
|
if(request.parv.size() < 1)
|
|
|
|
throw m::NEED_MORE_PARAMS
|
|
|
|
{
|
|
|
|
"txn_id path parameter required"
|
|
|
|
};
|
|
|
|
|
2020-01-07 01:22:57 +01:00
|
|
|
char txn_id_buf[128];
|
2017-10-25 18:42:23 +02:00
|
|
|
const auto txn_id
|
|
|
|
{
|
2020-01-07 01:22:57 +01:00
|
|
|
url::decode(txn_id_buf, request.parv[0])
|
2017-10-25 18:42:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &origin
|
|
|
|
{
|
|
|
|
json::at<"origin"_>(request)
|
|
|
|
};
|
|
|
|
|
2020-04-12 23:55:54 +02:00
|
|
|
if(origin && origin != request.node_id)
|
2020-01-07 01:31:04 +01:00
|
|
|
throw m::ACCESS_DENIED
|
|
|
|
{
|
|
|
|
"txn[%s] originating from '%s' not accepted when relayed by '%s'",
|
|
|
|
txn_id,
|
|
|
|
origin,
|
2020-04-12 23:55:54 +02:00
|
|
|
request.node_id,
|
2020-01-07 01:31:04 +01:00
|
|
|
};
|
|
|
|
|
2018-09-05 08:41:29 +02:00
|
|
|
// Don't accept sends to ourself for whatever reason (i.e a 127.0.0.1
|
|
|
|
// leaked into the target list). This should be a 500 so it's not
|
|
|
|
// considered success or cached as failure by the sender's state.
|
2020-04-12 23:55:54 +02:00
|
|
|
if(unlikely(my_host(request.node_id)) && !bool(allow_self))
|
2018-09-05 08:41:29 +02:00
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
"M_SEND_TO_SELF", "Tried to send %s from myself to myself.",
|
|
|
|
txn_id
|
|
|
|
};
|
|
|
|
|
2020-01-08 00:14:47 +01:00
|
|
|
size_t evals{0};
|
2020-04-20 09:29:41 +02:00
|
|
|
bool txn_in_progress{false};
|
|
|
|
m::vm::eval::for_each([&txn_id, &request, &evals, &txn_in_progress]
|
2022-06-24 04:18:05 +02:00
|
|
|
(const auto &eval) noexcept
|
2020-01-07 01:59:27 +01:00
|
|
|
{
|
2020-04-20 09:29:41 +02:00
|
|
|
assert(eval.opts);
|
|
|
|
const bool match_node
|
2020-01-07 01:59:27 +01:00
|
|
|
{
|
2020-04-20 09:29:41 +02:00
|
|
|
eval.opts->node_id == request.node_id
|
|
|
|
};
|
2020-01-07 01:59:27 +01:00
|
|
|
|
2020-04-20 09:29:41 +02:00
|
|
|
const bool match_txn
|
2020-01-07 01:59:27 +01:00
|
|
|
{
|
2020-04-20 09:29:41 +02:00
|
|
|
match_node &&
|
|
|
|
eval.opts->txn_id == txn_id
|
2020-01-07 01:59:27 +01:00
|
|
|
};
|
|
|
|
|
2020-04-20 09:29:41 +02:00
|
|
|
evals += match_node;
|
|
|
|
txn_in_progress |= match_txn;
|
|
|
|
return evals < size_t(eval_max_per_node);
|
|
|
|
});
|
|
|
|
|
2020-01-08 00:14:47 +01:00
|
|
|
if(evals >= size_t(eval_max_per_node))
|
|
|
|
return m::resource::response
|
|
|
|
{
|
|
|
|
client, http::TOO_MANY_REQUESTS
|
|
|
|
};
|
|
|
|
|
2020-04-20 09:29:41 +02:00
|
|
|
if(txn_in_progress)
|
|
|
|
return m::resource::response
|
|
|
|
{
|
|
|
|
client, http::ACCEPTED
|
|
|
|
};
|
|
|
|
|
2022-08-31 21:40:43 +02:00
|
|
|
char rembuf[96];
|
|
|
|
log::logf
|
|
|
|
{
|
|
|
|
txn_log, log::level::DEBUG,
|
|
|
|
"%s %zu$B pdu:%zu %zu$B edu:%zu %zu %s :%s",
|
|
|
|
txn_id,
|
|
|
|
size(string_view(json::get<"pdus"_>(request))),
|
|
|
|
json::get<"pdus"_>(request).count(),
|
|
|
|
size(string_view(json::get<"edus"_>(request))),
|
|
|
|
json::get<"edus"_>(request).count(),
|
|
|
|
evals,
|
|
|
|
string(rembuf, remote(client)),
|
|
|
|
origin,
|
|
|
|
};
|
|
|
|
|
2020-12-15 20:51:06 +01:00
|
|
|
char chunk[1536];
|
|
|
|
m::resource::response::chunked response
|
2020-03-26 18:28:05 +01:00
|
|
|
{
|
2020-12-15 20:51:06 +01:00
|
|
|
client, http::OK, 0UL, chunk
|
2020-03-26 18:28:05 +01:00
|
|
|
};
|
2017-10-25 18:42:23 +02:00
|
|
|
|
2020-12-15 20:51:06 +01:00
|
|
|
json::stack out
|
2017-10-25 18:42:23 +02:00
|
|
|
{
|
2020-12-15 20:51:06 +01:00
|
|
|
response.buf, response.flusher()
|
2017-10-25 18:42:23 +02:00
|
|
|
};
|
2020-12-15 20:51:06 +01:00
|
|
|
|
2021-01-12 14:45:14 +01:00
|
|
|
json::stack::object top
|
|
|
|
{
|
|
|
|
out
|
|
|
|
};
|
|
|
|
|
2020-12-15 20:51:06 +01:00
|
|
|
handle_txn(client, request, txn_id, out);
|
2022-06-25 22:47:43 +02:00
|
|
|
return response;
|
2017-10-25 18:42:23 +02:00
|
|
|
}
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
m::resource::method
|
2018-03-14 21:36:13 +01:00
|
|
|
method_put
|
2017-10-25 18:42:23 +02:00
|
|
|
{
|
|
|
|
send_resource, "PUT", handle_put,
|
|
|
|
{
|
2018-02-11 06:28:49 +01:00
|
|
|
method_put.VERIFY_ORIGIN,
|
2018-04-16 01:42:13 +02:00
|
|
|
|
|
|
|
// Coarse timeout
|
2019-09-09 21:12:42 +02:00
|
|
|
90s, //TODO: conf
|
2018-04-16 01:42:13 +02:00
|
|
|
|
|
|
|
// Payload maximum
|
2018-02-11 06:28:49 +01:00
|
|
|
4_MiB // larger = HTTP 413 //TODO: conf
|
2017-10-25 18:42:23 +02:00
|
|
|
}
|
|
|
|
};
|