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
|
|
|
};
|
|
|
|
|
2018-03-14 21:36:13 +01:00
|
|
|
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,
|
|
|
|
const resource::request::object<m::txn> &request,
|
|
|
|
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-02-28 03:07:38 +01:00
|
|
|
vmopts.notify_clients = false;
|
|
|
|
vmopts.notify_servers = false;
|
2018-03-07 20:02:23 +01:00
|
|
|
vmopts.non_conform.set(m::event::conforms::INVALID_OR_MISSING_EVENT_ID);
|
|
|
|
vmopts.non_conform.set(m::event::conforms::INVALID_OR_MISSING_ROOM_ID);
|
|
|
|
vmopts.non_conform.set(m::event::conforms::INVALID_OR_MISSING_SENDER_ID);
|
2018-03-14 21:36:13 +01:00
|
|
|
vmopts.non_conform.set(m::event::conforms::MISSING_ORIGIN_SIGNATURE);
|
|
|
|
vmopts.non_conform.set(m::event::conforms::MISSING_SIGNATURES);
|
2018-03-07 20:02:23 +01:00
|
|
|
vmopts.non_conform.set(m::event::conforms::MISSING_PREV_EVENTS);
|
2019-03-09 22:12:58 +01:00
|
|
|
vmopts.non_conform.set(m::event::conforms::MISSING_AUTH_EVENTS);
|
2018-03-07 20:02:23 +01:00
|
|
|
vmopts.non_conform.set(m::event::conforms::MISSING_PREV_STATE);
|
|
|
|
vmopts.non_conform.set(m::event::conforms::DEPTH_ZERO);
|
|
|
|
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,
|
|
|
|
const resource::request::object<m::txn> &request,
|
|
|
|
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;
|
|
|
|
vmopts.non_conform.set(m::event::conforms::MISSING_PREV_STATE);
|
|
|
|
vmopts.prev_check_exists = false;
|
|
|
|
vmopts.nothrows = -1U;
|
2018-04-14 01:22:13 +02:00
|
|
|
vmopts.infolog_accept = true;
|
|
|
|
vmopts.warnlog |= m::vm::fault::STATE;
|
|
|
|
vmopts.errorlog &= ~m::vm::fault::STATE;
|
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,
|
|
|
|
const resource::request::object<m::txn> &request,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
handle_put(client &client,
|
|
|
|
const resource::request::object<m::txn> &request)
|
|
|
|
{
|
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);
|
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
2017-11-16 02:48:25 +01:00
|
|
|
client, http::OK
|
2017-10-25 18:42:23 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-14 21:36:13 +01:00
|
|
|
resource::method
|
|
|
|
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
|
2018-09-05 07:49:38 +02:00
|
|
|
45s,
|
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
|
|
|
}
|
|
|
|
};
|