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" },
|
|
|
|
{ "default", "false" },
|
|
|
|
};
|
|
|
|
|
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;
|
2019-04-22 21:25:50 +02:00
|
|
|
vmopts.node_id = request.origin;
|
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;
|
|
|
|
vmopts.fetch_state = false;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
handle_pdu_failure(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,
|
|
|
|
const json::object &pdu_failure)
|
|
|
|
{
|
2018-03-07 20:02:23 +01:00
|
|
|
log::error
|
|
|
|
{
|
2019-01-09 00:12:05 +01:00
|
|
|
m::log, "%s :%s | (pdu_failure) %s",
|
2018-03-07 20:02:23 +01:00
|
|
|
txn_id,
|
|
|
|
at<"origin"_>(request),
|
|
|
|
pdu_failure.get("sender", string_view{"*"}),
|
|
|
|
string_view{pdu_failure}
|
|
|
|
};
|
2017-10-25 18:42:23 +02:00
|
|
|
}
|
|
|
|
|
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"
|
|
|
|
};
|
|
|
|
|
2017-10-25 18:42:23 +02:00
|
|
|
const auto txn_id
|
|
|
|
{
|
|
|
|
request.parv[0]
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &origin
|
|
|
|
{
|
|
|
|
json::at<"origin"_>(request)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::array &edus
|
|
|
|
{
|
|
|
|
json::get<"edus"_>(request)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::array &pdus
|
|
|
|
{
|
|
|
|
json::get<"pdus"_>(request)
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::array &pdu_failures
|
|
|
|
{
|
|
|
|
json::get<"pdu_failures"_>(request)
|
|
|
|
};
|
|
|
|
|
2018-03-07 20:02:23 +01:00
|
|
|
log::debug
|
|
|
|
{
|
2019-01-09 00:12:05 +01:00
|
|
|
m::log, "%s :%s | %s --> edus:%zu pdus:%zu errors:%zu",
|
2018-03-07 20:02:23 +01:00
|
|
|
txn_id,
|
|
|
|
origin,
|
|
|
|
string(remote(client)),
|
|
|
|
edus.count(),
|
|
|
|
pdus.count(),
|
|
|
|
pdu_failures.count()
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2017-10-25 18:42:23 +02:00
|
|
|
for(const auto &pdu_failure : pdu_failures)
|
|
|
|
handle_pdu_failure(client, request, txn_id, pdu_failure);
|
|
|
|
|
2019-02-08 16:55:44 +01:00
|
|
|
handle_pdus(client, request, txn_id, pdus);
|
|
|
|
|
2018-03-07 20:02:23 +01:00
|
|
|
for(const json::object &edu : edus)
|
2017-10-25 18:42:23 +02:00
|
|
|
handle_edu(client, request, txn_id, edu);
|
|
|
|
|
2019-09-29 01:12:07 +02:00
|
|
|
return m::resource::response
|
2017-10-25 18:42:23 +02:00
|
|
|
{
|
2017-11-16 02:48:25 +01:00
|
|
|
client, http::OK
|
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
|
|
|
}
|
|
|
|
};
|