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,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
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);
|
|
|
|
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
|
|
|
|
handle_pdu(client &client,
|
|
|
|
const resource::request::object<m::txn> &request,
|
|
|
|
const string_view &txn_id,
|
|
|
|
const m::event &event)
|
|
|
|
{
|
2018-03-07 20:02:23 +01:00
|
|
|
m::vm::opts vmopts;
|
|
|
|
vmopts.non_conform.set(m::event::conforms::MISSING_PREV_STATE);
|
|
|
|
vmopts.non_conform.set(m::event::conforms::MISSING_MEMBERSHIP);
|
|
|
|
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
|
|
|
|
{
|
|
|
|
event, vmopts
|
|
|
|
};
|
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)
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
if(unlikely(my_host(request.origin)))
|
|
|
|
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);
|
|
|
|
|
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);
|
|
|
|
|
2018-03-07 20:02:23 +01:00
|
|
|
for(const json::object &pdu : pdus)
|
|
|
|
handle_pdu(client, request, txn_id, pdu);
|
2017-10-25 18:42:23 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|