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
|
|
|
};
|
|
|
|
|
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" },
|
|
|
|
{ "default", 1L },
|
|
|
|
};
|
|
|
|
|
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 },
|
|
|
|
};
|
|
|
|
|
2017-10-25 18:42:23 +02:00
|
|
|
void
|
|
|
|
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;
|
2018-09-06 15:00:21 +02:00
|
|
|
json::get<"origin"_>(event) = request.origin;
|
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;
|
|
|
|
vmopts.nothrows &= ~m::vm::fault::INTERRUPT;
|
2019-04-22 21:25:50 +02:00
|
|
|
vmopts.node_id = request.origin;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
const json::array &pdus)
|
2017-10-25 18:42:23 +02:00
|
|
|
{
|
2018-03-07 20:02:23 +01:00
|
|
|
m::vm::opts vmopts;
|
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;
|
2019-09-09 02:39:36 +02:00
|
|
|
vmopts.nothrows &= ~m::vm::fault::INTERRUPT;
|
2019-09-13 22:32:20 +02:00
|
|
|
vmopts.node_id = request.origin;
|
2020-01-07 01:22:30 +01:00
|
|
|
vmopts.txn_id = txn_id;
|
2020-03-04 01:15:04 +01:00
|
|
|
vmopts.fetch_prev = bool(fetch_state);
|
|
|
|
vmopts.fetch_state = bool(fetch_prev);
|
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-03-26 18:28:05 +01:00
|
|
|
json::object
|
|
|
|
handle_txn(client &client,
|
|
|
|
const m::resource::request::object<m::txn> &request,
|
|
|
|
const string_view &txn_id,
|
|
|
|
unique_mutable_buffer &buf)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto &pdus
|
|
|
|
{
|
|
|
|
json::get<"pdus"_>(request)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &edus
|
|
|
|
{
|
|
|
|
json::get<"edus"_>(request)
|
|
|
|
};
|
|
|
|
|
|
|
|
handle_pdus(client, request, txn_id, pdus);
|
|
|
|
|
|
|
|
for(const json::object &edu : edus)
|
|
|
|
handle_edu(client, request, txn_id, edu);
|
|
|
|
|
|
|
|
return json::empty_object;
|
|
|
|
}
|
|
|
|
catch(const ctx::interrupted &)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
m::log, "Unhandled error processing txn '%s' from '%s' :%s",
|
|
|
|
txn_id,
|
|
|
|
request.origin,
|
|
|
|
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-03-26 18:28:05 +01:00
|
|
|
char rembuf[64];
|
2018-03-07 20:02:23 +01:00
|
|
|
log::debug
|
|
|
|
{
|
2020-03-26 18:43:17 +01:00
|
|
|
m::log, "%s :%s | %s --> edus:%zu pdus:%zu",
|
2018-03-07 20:02:23 +01:00
|
|
|
txn_id,
|
|
|
|
origin,
|
2020-03-26 18:28:05 +01:00
|
|
|
string(rembuf, remote(client)),
|
|
|
|
json::get<"edus"_>(request).count(),
|
|
|
|
json::get<"pdus"_>(request).count(),
|
2018-03-07 20:02:23 +01:00
|
|
|
};
|
|
|
|
|
2020-01-07 01:31:04 +01:00
|
|
|
if(origin && origin != request.origin)
|
|
|
|
throw m::ACCESS_DENIED
|
|
|
|
{
|
|
|
|
"txn[%s] originating from '%s' not accepted when relayed by '%s'",
|
|
|
|
txn_id,
|
|
|
|
origin,
|
|
|
|
request.origin,
|
|
|
|
};
|
|
|
|
|
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.
|
2019-02-22 22:45:09 +01:00
|
|
|
if(unlikely(my_host(request.origin)) && !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-01-07 01:59:27 +01:00
|
|
|
const bool txn_in_progress
|
|
|
|
{
|
2020-01-08 00:14:47 +01:00
|
|
|
!m::vm::eval::for_each([&txn_id, &request, &evals]
|
2020-01-07 01:59:27 +01:00
|
|
|
(const auto &eval)
|
|
|
|
{
|
|
|
|
assert(eval.opts);
|
2020-01-08 00:14:47 +01:00
|
|
|
const bool match_node
|
2020-01-07 01:59:27 +01:00
|
|
|
{
|
2020-01-08 00:14:47 +01:00
|
|
|
eval.opts->node_id == request.origin
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool match_txn
|
|
|
|
{
|
|
|
|
match_node &&
|
2020-01-07 01:59:27 +01:00
|
|
|
eval.opts->txn_id == txn_id
|
|
|
|
};
|
|
|
|
|
2020-01-08 00:14:47 +01:00
|
|
|
evals += match_node;
|
|
|
|
return !match_txn; // false to break; for_each() returns false
|
2020-01-07 01:59:27 +01:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
if(txn_in_progress)
|
|
|
|
return m::resource::response
|
|
|
|
{
|
|
|
|
client, http::ACCEPTED
|
|
|
|
};
|
|
|
|
|
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-03-26 18:28:05 +01:00
|
|
|
// Lazy-allocated response buffer; only for error transcription
|
|
|
|
unique_mutable_buffer response_buffer;
|
|
|
|
const json::object &response
|
|
|
|
{
|
|
|
|
handle_txn(client, request, txn_id, response_buffer)
|
|
|
|
};
|
2017-10-25 18:42:23 +02:00
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
return m::resource::response
|
2017-10-25 18:42:23 +02:00
|
|
|
{
|
2020-03-26 18:28:05 +01:00
|
|
|
client, 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
|
|
|
}
|
|
|
|
};
|